summaryrefslogtreecommitdiff
path: root/svx/source/dialog/weldeditview.cxx
blob: 2e1ff335b9c722d007b6801165e3fda882054af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <config_wasm_strip.h>

#include <basegfx/matrix/b2dhommatrix.hxx>
#include <com/sun/star/accessibility/AccessibleRole.hpp>
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
#include <com/sun/star/accessibility/XAccessible.hpp>
#include <com/sun/star/accessibility/XAccessibleComponent.hpp>
#include <com/sun/star/accessibility/XAccessibleContext.hpp>
#include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <drawinglayer/processor2d/processor2dtools.hxx>
#include <comphelper/lok.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/fhgtitem.hxx>
#include <editeng/fontitem.hxx>
#include <editeng/outliner.hxx>
#include <editeng/unoedhlp.hxx>
#include <editeng/unoedsrc.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <osl/diagnose.h>
#include <svl/itempool.hxx>
#include <svl/itemset.hxx>
#include <sal/log.hxx>
#include <svx/sdr/overlay/overlayselection.hxx>
#include <svtools/optionsdrawinglayer.hxx>
#include <svx/AccessibleTextHelper.hxx>
#include <svx/weldeditview.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/cursor.hxx>
#include <vcl/event.hxx>
#include <vcl/ptrstyle.hxx>
#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
#include <vcl/window.hxx>
#include <vcl/uitest/uiobject.hxx>

void WeldEditView::SetText(const OUString& rStr) { GetEditEngine()->SetText(rStr); }

OUString WeldEditView::GetText() const { return GetEditEngine()->GetText(); }

void WeldEditView::SetModifyHdl(const Link<LinkParamNone*, void>& rLink)
{
    GetEditEngine()->SetModifyHdl(rLink);
}

EditView* WeldEditView::GetEditView() const { return m_xEditView.get(); }

EditEngine* WeldEditView::GetEditEngine() const { return m_xEditEngine.get(); }

bool WeldEditView::HasSelection() const
{
    EditView* pEditView = GetEditView();
    return pEditView && pEditView->HasSelection();
}

void WeldEditView::Delete()
{
    if (EditView* pEditView = GetEditView())
        pEditView->DeleteSelected();
}

void WeldEditView::Cut()
{
    if (EditView* pEditView = GetEditView())
        pEditView->Cut();
}

void WeldEditView::Copy()
{
    if (EditView* pEditView = GetEditView())
        pEditView->Copy();
}

void WeldEditView::Paste()
{
    if (EditView* pEditView = GetEditView())
        pEditView->Paste();
}

WeldEditView::WeldEditView()
    : m_bAcceptsTab(false)
{
}

// tdf#127033 want to use UI font so override makeEditEngine to enable that
void WeldEditView::makeEditEngine()
{
    rtl::Reference<SfxItemPool> pItemPool = EditEngine::CreatePool();

    vcl::Font aAppFont(Application::GetSettings().GetStyleSettings().GetAppFont());

    pItemPool->SetPoolDefaultItem(SvxFontItem(aAppFont.GetFamilyType(), aAppFont.GetFamilyName(),
                                              "", PITCH_DONTKNOW, RTL_TEXTENCODING_DONTKNOW,
                                              EE_CHAR_FONTINFO));
    pItemPool->SetPoolDefaultItem(SvxFontItem(aAppFont.GetFamilyType(), aAppFont.GetFamilyName(),
                                              "", PITCH_DONTKNOW, RTL_TEXTENCODING_DONTKNOW,
                                              EE_CHAR_FONTINFO_CJK));
    pItemPool->SetPoolDefaultItem(SvxFontItem(aAppFont.GetFamilyType(), aAppFont.GetFamilyName(),
                                              "", PITCH_DONTKNOW, RTL_TEXTENCODING_DONTKNOW,
                                              EE_CHAR_FONTINFO_CTL));

    pItemPool->SetPoolDefaultItem(
        SvxFontHeightItem(aAppFont.GetFontHeight() * 20, 100, EE_CHAR_FONTHEIGHT));
    pItemPool->SetPoolDefaultItem(
        SvxFontHeightItem(aAppFont.GetFontHeight() * 20, 100, EE_CHAR_FONTHEIGHT_CJK));
    pItemPool->SetPoolDefaultItem(
        SvxFontHeightItem(aAppFont.GetFontHeight() * 20, 100, EE_CHAR_FONTHEIGHT_CTL));

    m_xEditEngine.reset(new EditEngine(pItemPool.get()));
}

void WeldEditView::Resize()
{
    if (EditView* pEditView = GetEditView())
    {
        OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
        Size aOutputSize(rDevice.PixelToLogic(GetOutputSizePixel()));
        // Resizes the edit engine to adjust to the size of the output area
        pEditView->SetOutputArea(tools::Rectangle(Point(0, 0), aOutputSize));
        GetEditEngine()->SetPaperSize(aOutputSize);
        pEditView->ShowCursor();

        const tools::Long nMaxVisAreaStart
            = pEditView->GetEditEngine()->GetTextHeight() - aOutputSize.Height();
        tools::Rectangle aVisArea(pEditView->GetVisArea());
        if (aVisArea.Top() > nMaxVisAreaStart)
        {
            aVisArea.SetTop(std::max<tools::Long>(nMaxVisAreaStart, 0));
            aVisArea.SetSize(aOutputSize);
            pEditView->SetVisArea(aVisArea);
            pEditView->ShowCursor();
        }

        EditViewScrollStateChange();
    }
    weld::CustomWidgetController::Resize();
}

void WeldEditView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
{
    DoPaint(rRenderContext, rRect);
}

void WeldEditView::DoPaint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
{
    rRenderContext.Push(vcl::PushFlags::ALL);
    rRenderContext.SetClipRegion();

    std::vector<tools::Rectangle> aLogicRects;

    if (EditView* pEditView = GetEditView())
    {
        pEditView->Paint(comphelper::LibreOfficeKit::isActive() ? rRenderContext.PixelToLogic(rRect)
                                                                : rRect,
                         &rRenderContext);

        if (HasFocus())
        {
            pEditView->ShowCursor(false);
            vcl::Cursor* pCursor = pEditView->GetCursor();
            pCursor->DrawToDevice(rRenderContext);
        }

        // get logic selection
        pEditView->GetSelectionRectangles(aLogicRects);
    }

    if (!aLogicRects.empty())
    {
        std::vector<basegfx::B2DRange> aLogicRanges;
        aLogicRanges.reserve(aLogicRects.size());

        tools::Long nMinX(LONG_MAX), nMaxX(0), nMinY(LONG_MAX), nMaxY(0);
        for (const auto& aRect : aLogicRects)
        {
            nMinX = std::min(nMinX, aRect.Left());
            nMinY = std::min(nMinY, aRect.Top());
            nMaxX = std::max(nMaxX, aRect.Right());
            nMaxY = std::max(nMaxY, aRect.Bottom());
        }

        const Size aLogicPixel(rRenderContext.PixelToLogic(Size(1, 1)));
        for (const auto& aRect : aLogicRects)
        {
            // Extend each range by one pixel so multiple lines touch each
            // other if adjacent, so the whole set is drawn with a single
            // border around the lot. But keep the selection within the
            // original max extents.
            auto nTop = aRect.Top();
            if (nTop > nMinY)
                nTop -= aLogicPixel.Height();
            auto nBottom = aRect.Bottom();
            if (nBottom < nMaxY)
                nBottom += aLogicPixel.Height();
            auto nLeft = aRect.Left();
            if (nLeft > nMinX)
                nLeft -= aLogicPixel.Width();
            auto nRight = aRect.Right();
            if (nRight < nMaxX)
                nRight += aLogicPixel.Width();

            aLogicRanges.emplace_back(nLeft, nTop, nRight, nBottom);
        }

        // get the system's highlight color
        const Color aHighlight(SvtOptionsDrawinglayer::getHilightColor());

        sdr::overlay::OverlaySelection aCursorOverlay(sdr::overlay::OverlayType::Transparent,
                                                      aHighlight, std::move(aLogicRanges), true);

        drawinglayer::geometry::ViewInformation2D aViewInformation2D;
        aViewInformation2D.setViewTransformation(rRenderContext.GetViewTransformation());
        aViewInformation2D.setViewport(vcl::unotools::b2DRectangleFromRectangle(rRect));

        std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> xProcessor(
            drawinglayer::processor2d::createProcessor2DFromOutputDevice(rRenderContext,
                                                                         aViewInformation2D));

        xProcessor->process(aCursorOverlay.getOverlayObjectPrimitive2DSequence());
    }

    rRenderContext.Pop();
}

bool WeldEditView::MouseMove(const MouseEvent& rMEvt)
{
    EditView* pEditView = GetEditView();
    return pEditView && pEditView->MouseMove(rMEvt);
}

bool WeldEditView::MouseButtonDown(const MouseEvent& rMEvt)
{
    if (!IsMouseCaptured())
        CaptureMouse();

    if (!HasFocus() && CanFocus())
        GrabFocus();

    EditView* pEditView = GetEditView();
    return pEditView && pEditView->MouseButtonDown(rMEvt);
}

bool WeldEditView::MouseButtonUp(const MouseEvent& rMEvt)
{
    if (IsMouseCaptured())
        ReleaseMouse();
    EditView* pEditView = GetEditView();
    return pEditView && pEditView->MouseButtonUp(rMEvt);
}

bool WeldEditView::KeyInput(const KeyEvent& rKEvt)
{
    EditView* pEditView = GetEditView();

    sal_uInt16 nKey = rKEvt.GetKeyCode().GetCode();

    if (nKey == KEY_TAB && !GetAcceptsTab())
    {
        return false;
    }
    else if (pEditView && !pEditView->PostKeyEvent(rKEvt))
    {
        if (rKEvt.GetKeyCode().IsMod1() && !rKEvt.GetKeyCode().IsMod2())
        {
            if (nKey == KEY_A)
            {
                EditEngine* pEditEngine = GetEditEngine();
                sal_Int32 nPar = pEditEngine->GetParagraphCount();
                if (nPar)
                {
                    sal_Int32 nLen = pEditEngine->GetTextLen(nPar - 1);
                    pEditView->SetSelection(ESelection(0, 0, nPar - 1, nLen));
                }
                return true;
            }
        }

        return false;
    }

    return true;
}

bool WeldEditView::Command(const CommandEvent& rCEvt)
{
    EditView* pEditView = GetEditView();
    if (!pEditView)
        return false;
    return pEditView->Command(rCEvt);
}

Point WeldEditView::EditViewPointerPosPixel() const
{
    return GetDrawingArea()->get_pointer_position();
}

class WeldEditAccessible;

namespace
{
class WeldViewForwarder : public SvxViewForwarder
{
    WeldEditAccessible& m_rEditAcc;

    WeldViewForwarder(const WeldViewForwarder&) = delete;
    WeldViewForwarder& operator=(const WeldViewForwarder&) = delete;

public:
    explicit WeldViewForwarder(WeldEditAccessible& rAcc)
        : m_rEditAcc(rAcc)
    {
    }

    virtual bool IsValid() const override;
    virtual Point LogicToPixel(const Point& rPoint, const MapMode& rMapMode) const override;
    virtual Point PixelToLogic(const Point& rPoint, const MapMode& rMapMode) const override;
};
}

class WeldEditAccessible;

namespace
{
class WeldEditSource;

/* analog to SvxEditEngineForwarder */
class WeldTextForwarder : public SvxTextForwarder
{
    WeldEditAccessible& m_rEditAcc;
    WeldEditSource& m_rEditSource;

    DECL_LINK(NotifyHdl, EENotify&, void);

    WeldTextForwarder(const WeldTextForwarder&) = delete;
    WeldTextForwarder& operator=(const WeldTextForwarder&) = delete;

public:
    WeldTextForwarder(WeldEditAccessible& rAcc, WeldEditSource& rSource);
    virtual ~WeldTextForwarder() override;

    virtual sal_Int32 GetParagraphCount() const override;
    virtual sal_Int32 GetTextLen(sal_Int32 nParagraph) const override;
    virtual OUString GetText(const ESelection& rSel) const override;
    virtual SfxItemSet GetAttribs(const ESelection& rSel, EditEngineAttribs nOnlyHardAttrib
                                                          = EditEngineAttribs::All) const override;
    virtual SfxItemSet GetParaAttribs(sal_Int32 nPara) const override;
    virtual void SetParaAttribs(sal_Int32 nPara, const SfxItemSet& rSet) override;
    virtual void RemoveAttribs(const ESelection& rSelection) override;
    virtual void GetPortions(sal_Int32 nPara, std::vector<sal_Int32>& rList) const override;

    virtual SfxItemState GetItemState(const ESelection& rSel, sal_uInt16 nWhich) const override;
    virtual SfxItemState GetItemState(sal_Int32 nPara, sal_uInt16 nWhich) const override;

    virtual void QuickInsertText(const OUString& rText, const ESelection& rSel) override;
    virtual void QuickInsertField(const SvxFieldItem& rFld, const ESelection& rSel) override;
    virtual void QuickSetAttribs(const SfxItemSet& rSet, const ESelection& rSel) override;
    virtual void QuickInsertLineBreak(const ESelection& rSel) override;

    virtual SfxItemPool* GetPool() const override;

    virtual OUString CalcFieldValue(const SvxFieldItem& rField, sal_Int32 nPara, sal_Int32 nPos,
                                    std::optional<Color>& rpTxtColor,
                                    std::optional<Color>& rpFldColor) override;
    virtual void FieldClicked(const SvxFieldItem&) override;
    virtual bool IsValid() const override;

    virtual LanguageType GetLanguage(sal_Int32, sal_Int32) const override;
    virtual sal_Int32 GetFieldCount(sal_Int32 nPara) const override;
    virtual EFieldInfo GetFieldInfo(sal_Int32 nPara, sal_uInt16 nField) const override;
    virtual EBulletInfo GetBulletInfo(sal_Int32 nPara) const override;
    virtual tools::Rectangle GetCharBounds(sal_Int32 nPara, sal_Int32 nIndex) const override;
    virtual tools::Rectangle GetParaBounds(sal_Int32 nPara) const override;
    virtual MapMode GetMapMode() const override;
    virtual OutputDevice* GetRefDevice() const override;
    virtual bool GetIndexAtPoint(const Point&, sal_Int32& nPara, sal_Int32& nIndex) const override;
    virtual bool GetWordIndices(sal_Int32 nPara, sal_Int32 nIndex, sal_Int32& nStart,
                                sal_Int32& nEnd) const override;
    virtual bool GetAttributeRun(sal_Int32& nStartIndex, sal_Int32& nEndIndex, sal_Int32 nPara,
                                 sal_Int32 nIndex, bool bInCell = false) const override;
    virtual sal_Int32 GetLineCount(sal_Int32 nPara) const override;
    virtual sal_Int32 GetLineLen(sal_Int32 nPara, sal_Int32 nLine) const override;
    virtual void GetLineBoundaries(/*out*/ sal_Int32& rStart, /*out*/ sal_Int32& rEnd,
                                   sal_Int32 nParagraph, sal_Int32 nLine) const override;
    virtual sal_Int32 GetLineNumberAtIndex(sal_Int32 nPara, sal_Int32 nLine) const override;
    virtual bool Delete(const ESelection&) override;
    virtual bool InsertText(const OUString&, const ESelection&) override;
    virtual bool QuickFormatDoc(bool bFull = false) override;

    virtual sal_Int16 GetDepth(sal_Int32 nPara) const override;
    virtual bool SetDepth(sal_Int32 nPara, sal_Int16 nNewDepth) override;

    virtual const SfxItemSet* GetEmptyItemSetPtr() override;
    // implementation functions for XParagraphAppend and XTextPortionAppend
    virtual void AppendParagraph() override;
    virtual sal_Int32 AppendTextPortion(sal_Int32 nPara, const OUString& rText,
                                        const SfxItemSet& rSet) override;

    virtual void CopyText(const SvxTextForwarder& rSource) override;
};

/* analog to SvxEditEngineViewForwarder */
class WeldEditViewForwarder : public SvxEditViewForwarder
{
    WeldEditAccessible& m_rEditAcc;

    WeldEditViewForwarder(const WeldEditViewForwarder&) = delete;
    WeldEditViewForwarder& operator=(const WeldEditViewForwarder&) = delete;

public:
    explicit WeldEditViewForwarder(WeldEditAccessible& rAcc);

    virtual bool IsValid() const override;

    virtual Point LogicToPixel(const Point& rPoint, const MapMode& rMapMode) const override;
    virtual Point PixelToLogic(const Point& rPoint, const MapMode& rMapMode) const override;

    virtual bool GetSelection(ESelection& rSelection) const override;
    virtual bool SetSelection(const ESelection& rSelection) override;
    virtual bool Copy() override;
    virtual bool Cut() override;
    virtual bool Paste() override;
};

class WeldEditSource : public SvxEditSource
{
    SfxBroadcaster m_aBroadCaster;
    WeldViewForwarder m_aViewFwd;
    WeldTextForwarder m_aTextFwd;
    WeldEditViewForwarder m_aEditViewFwd;
    WeldEditAccessible& m_rEditAcc;

    WeldEditSource(const WeldEditSource& rSrc)
        : SvxEditSource()
        , m_aViewFwd(rSrc.m_rEditAcc)
        , m_aTextFwd(rSrc.m_rEditAcc, *this)
        , m_aEditViewFwd(rSrc.m_rEditAcc)
        , m_rEditAcc(rSrc.m_rEditAcc)
    {
    }

    WeldEditSource& operator=(const WeldEditSource&) = delete;

public:
    WeldEditSource(WeldEditAccessible& rAcc)
        : m_aViewFwd(rAcc)
        , m_aTextFwd(rAcc, *this)
        , m_aEditViewFwd(rAcc)
        , m_rEditAcc(rAcc)
    {
    }

    virtual std::unique_ptr<SvxEditSource> Clone() const override
    {
        return std::unique_ptr<SvxEditSource>(new WeldEditSource(*this));
    }

    virtual SvxTextForwarder* GetTextForwarder() override { return &m_aTextFwd; }

    virtual SvxViewForwarder* GetViewForwarder() override { return &m_aViewFwd; }

    virtual SvxEditViewForwarder* GetEditViewForwarder(bool /*bCreate*/) override
    {
        return &m_aEditViewFwd;
    }

    virtual void UpdateData() override
    {
        // would possibly only by needed if the XText interface is implemented
        // and its text needs to be updated.
    }
    virtual SfxBroadcaster& GetBroadcaster() const override
    {
        return const_cast<WeldEditSource*>(this)->m_aBroadCaster;
    }
};
}

typedef cppu::WeakImplHelper<css::lang::XServiceInfo, css::accessibility::XAccessible,
                             css::accessibility::XAccessibleComponent,
                             css::accessibility::XAccessibleContext,
                             css::accessibility::XAccessibleEventBroadcaster>
    WeldEditAccessibleBaseClass;

class WeldEditAccessible : public WeldEditAccessibleBaseClass
{
    weld::CustomWidgetController* m_pController;
    EditEngine* m_pEditEngine;
    EditView* m_pEditView;
    std::unique_ptr<::accessibility::AccessibleTextHelper> m_xTextHelper;

public:
    WeldEditAccessible(weld::CustomWidgetController* pController)
        : m_pController(pController)
        , m_pEditEngine(nullptr)
        , m_pEditView(nullptr)
    {
    }

    ::accessibility::AccessibleTextHelper* GetTextHelper() { return m_xTextHelper.get(); }

    void Init(EditEngine* pEditEngine, EditView* pEditView)
    {
        m_pEditEngine = pEditEngine;
        m_pEditView = pEditView;
        m_xTextHelper.reset(
            new ::accessibility::AccessibleTextHelper(std::make_unique<WeldEditSource>(*this)));
        m_xTextHelper->SetEventSource(this);
    }

    EditEngine* GetEditEngine() { return m_pEditEngine; }
    EditView* GetEditView() { return m_pEditView; }

    void ClearWin()
    {
        // remove handler before current object gets destroyed
        // (avoid handler being called for already dead object)
        m_pEditEngine->SetNotifyHdl(Link<EENotify&, void>());

        m_pEditEngine = nullptr;
        m_pEditView = nullptr;
        m_pController = nullptr; // implicitly results in AccessibleStateType::DEFUNC set

        //! make TextHelper implicitly release C++ references to some core objects
        m_xTextHelper->SetEditSource(::std::unique_ptr<SvxEditSource>());

        //! make TextHelper release references
        //! (e.g. the one set by the 'SetEventSource' call)
        m_xTextHelper->Dispose();
        m_xTextHelper.reset();
    }

    // XAccessible
    virtual css::uno::Reference<css::accessibility::XAccessibleContext>
        SAL_CALL getAccessibleContext() override
    {
        return this;
    }

    // XAccessibleComponent
    virtual sal_Bool SAL_CALL containsPoint(const css::awt::Point& rPoint) override
    {
        //! the arguments coordinates are relative to the current window !
        //! Thus the top left-point is (0, 0)
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        Size aSz(m_pController->GetOutputSizePixel());
        return rPoint.X >= 0 && rPoint.Y >= 0 && rPoint.X < aSz.Width() && rPoint.Y < aSz.Height();
    }

    virtual css::uno::Reference<css::accessibility::XAccessible>
        SAL_CALL getAccessibleAtPoint(const css::awt::Point& rPoint) override
    {
        SolarMutexGuard aGuard;
        if (!m_xTextHelper)
            throw css::uno::RuntimeException();

        return m_xTextHelper->GetAt(rPoint);
    }

    virtual css::awt::Rectangle SAL_CALL getBounds() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        const Point aOutPos;
        const Size aOutSize(m_pController->GetOutputSizePixel());
        css::awt::Rectangle aRet;

        aRet.X = aOutPos.X();
        aRet.Y = aOutPos.Y();
        aRet.Width = aOutSize.Width();
        aRet.Height = aOutSize.Height();

        return aRet;
    }

    virtual css::awt::Point SAL_CALL getLocation() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        const css::awt::Rectangle aRect(getBounds());
        css::awt::Point aRet;

        aRet.X = aRect.X;
        aRet.Y = aRect.Y;

        return aRet;
    }

    virtual css::awt::Point SAL_CALL getLocationOnScreen() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        css::awt::Point aScreenLoc(0, 0);

        if (weld::DrawingArea* pDrawingArea = m_pController->GetDrawingArea())
        {
            Point aPos = pDrawingArea->get_accessible_location_on_screen();
            aScreenLoc.X = aPos.X();
            aScreenLoc.Y = aPos.Y();
        }

        return aScreenLoc;
    }

    virtual css::awt::Size SAL_CALL getSize() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        Size aSz(m_pController->GetOutputSizePixel());
        return css::awt::Size(aSz.Width(), aSz.Height());
    }

    virtual void SAL_CALL grabFocus() override { m_pController->GrabFocus(); }

    virtual sal_Int32 SAL_CALL getForeground() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        Color nCol = m_pEditEngine->GetAutoColor();
        return static_cast<sal_Int32>(nCol);
    }

    virtual sal_Int32 SAL_CALL getBackground() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        Color nCol = m_pEditEngine->GetBackgroundColor();
        return static_cast<sal_Int32>(nCol);
    }

    // XAccessibleContext
    virtual sal_Int64 SAL_CALL getAccessibleChildCount() override
    {
        if (m_xTextHelper)
            return m_xTextHelper->GetChildCount();
        return 0;
    }

    virtual css::uno::Reference<css::accessibility::XAccessible>
        SAL_CALL getAccessibleChild(sal_Int64 i) override
    {
        if (m_xTextHelper)
            return m_xTextHelper->GetChild(i);
        throw css::lang::IndexOutOfBoundsException(); // there is no child...
    }

    virtual css::uno::Reference<css::accessibility::XAccessible>
        SAL_CALL getAccessibleParent() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        return m_pController->GetDrawingArea()->get_accessible_parent();
    }

    virtual sal_Int64 SAL_CALL getAccessibleIndexInParent() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        // -1 for child not found/no parent (according to specification)
        sal_Int64 nRet = -1;

        css::uno::Reference<css::accessibility::XAccessible> xParent(getAccessibleParent());
        if (!xParent)
            return nRet;

        try
        {
            css::uno::Reference<css::accessibility::XAccessibleContext> xParentContext(
                xParent->getAccessibleContext());

            //  iterate over parent's children and search for this object
            if (xParentContext.is())
            {
                sal_Int64 nChildCount = xParentContext->getAccessibleChildCount();
                for (sal_Int64 nChild = 0; (nChild < nChildCount) && (-1 == nRet); ++nChild)
                {
                    css::uno::Reference<css::accessibility::XAccessible> xChild(
                        xParentContext->getAccessibleChild(nChild));
                    if (xChild.get() == this)
                        nRet = nChild;
                }
            }
        }
        catch (const css::uno::Exception&)
        {
            TOOLS_WARN_EXCEPTION("svx", "WeldEditAccessible::getAccessibleIndexInParent");
        }

        return nRet;
    }

    virtual sal_Int16 SAL_CALL getAccessibleRole() override
    {
        return css::accessibility::AccessibleRole::TEXT_FRAME;
    }

    virtual OUString SAL_CALL getAccessibleDescription() override
    {
        SolarMutexGuard aGuard;

        OUString aRet;

        if (m_pController)
        {
            aRet = m_pController->GetAccessibleDescription();
        }

        return aRet;
    }

    virtual OUString SAL_CALL getAccessibleName() override
    {
        SolarMutexGuard aGuard;

        OUString aRet;

        if (m_pController)
        {
            aRet = m_pController->GetAccessibleName();
        }

        return aRet;
    }

    virtual css::uno::Reference<css::accessibility::XAccessibleRelationSet>
        SAL_CALL getAccessibleRelationSet() override
    {
        SolarMutexGuard aGuard;
        if (!m_pController)
            throw css::uno::RuntimeException();

        return m_pController->GetDrawingArea()->get_accessible_relation_set();
    }

    virtual sal_Int64 SAL_CALL getAccessibleStateSet() override
    {
        SolarMutexGuard aGuard;
        sal_Int64 nStateSet = 0;

        if (!m_pController || !m_xTextHelper)
            nStateSet |= css::accessibility::AccessibleStateType::DEFUNC;
        else
        {
            nStateSet |= css::accessibility::AccessibleStateType::MULTI_LINE;
            nStateSet |= css::accessibility::AccessibleStateType::ENABLED;
            nStateSet |= css::accessibility::AccessibleStateType::EDITABLE;
            nStateSet |= css::accessibility::AccessibleStateType::FOCUSABLE;
            nStateSet |= css::accessibility::AccessibleStateType::SELECTABLE;
            if (m_pController->HasFocus())
                nStateSet |= css::accessibility::AccessibleStateType::FOCUSED;
            if (m_pController->IsActive())
                nStateSet |= css::accessibility::AccessibleStateType::ACTIVE;
            if (m_pController->IsVisible())
                nStateSet |= css::accessibility::AccessibleStateType::SHOWING;
            if (m_pController->IsReallyVisible())
                nStateSet |= css::accessibility::AccessibleStateType::VISIBLE;
            if (COL_TRANSPARENT != m_pEditEngine->GetBackgroundColor())
                nStateSet |= css::accessibility::AccessibleStateType::OPAQUE;
        }

        return nStateSet;
    }

    virtual css::lang::Locale SAL_CALL getLocale() override
    {
        SolarMutexGuard aGuard;
        return LanguageTag(m_pEditEngine->GetDefaultLanguage()).getLocale();
    }

    // XAccessibleEventBroadcaster
    virtual void SAL_CALL addAccessibleEventListener(
        const css::uno::Reference<css::accessibility::XAccessibleEventListener>& rListener) override
    {
        if (!m_xTextHelper) // not disposing (about to destroy view shell)
            return;
        m_xTextHelper->AddEventListener(rListener);
    }

    virtual void SAL_CALL removeAccessibleEventListener(
        const css::uno::Reference<css::accessibility::XAccessibleEventListener>& rListener) override
    {
        if (!m_xTextHelper) // not disposing (about to destroy view shell)
            return;
        m_xTextHelper->RemoveEventListener(rListener);
    }

    virtual OUString SAL_CALL getImplementationName() override { return "WeldEditAccessible"; }

    virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override
    {
        return cppu::supportsService(this, rServiceName);
    }

    virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
    {
        return { "css::accessibility::Accessible", "css::accessibility::AccessibleComponent",
                 "css::accessibility::AccessibleContext" };
    }
};

css::uno::Reference<css::accessibility::XAccessible> WeldEditView::CreateAccessible()
{
#if !ENABLE_WASM_STRIP_ACCESSIBILITY
    if (!m_xAccessible.is())
        m_xAccessible.set(new WeldEditAccessible(this));
#endif
    return m_xAccessible;
}

WeldEditView::~WeldEditView()
{
#if !ENABLE_WASM_STRIP_ACCESSIBILITY
    if (m_xAccessible.is())
    {
        m_xAccessible->ClearWin(); // make Accessible nonfunctional
        m_xAccessible.clear();
    }
#endif
}

bool WeldViewForwarder::IsValid() const { return m_rEditAcc.GetEditView() != nullptr; }

Point WeldViewForwarder::LogicToPixel(const Point& rPoint, const MapMode& rMapMode) const
{
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (!pEditView)
        return Point();
    OutputDevice& rOutDev = pEditView->GetOutputDevice();
    MapMode aMapMode(rOutDev.GetMapMode());
    Point aPoint(OutputDevice::LogicToLogic(rPoint, rMapMode, MapMode(aMapMode.GetMapUnit())));
    aMapMode.SetOrigin(Point());
    return rOutDev.LogicToPixel(aPoint, aMapMode);
}

Point WeldViewForwarder::PixelToLogic(const Point& rPoint, const MapMode& rMapMode) const
{
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (!pEditView)
        return Point();
    OutputDevice& rOutDev = pEditView->GetOutputDevice();
    MapMode aMapMode(rOutDev.GetMapMode());
    aMapMode.SetOrigin(Point());
    Point aPoint(rOutDev.PixelToLogic(rPoint, aMapMode));
    return OutputDevice::LogicToLogic(aPoint, MapMode(aMapMode.GetMapUnit()), rMapMode);
}

WeldTextForwarder::WeldTextForwarder(WeldEditAccessible& rAcc, WeldEditSource& rSource)
    : m_rEditAcc(rAcc)
    , m_rEditSource(rSource)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->SetNotifyHdl(LINK(this, WeldTextForwarder, NotifyHdl));
}

WeldTextForwarder::~WeldTextForwarder()
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->SetNotifyHdl(Link<EENotify&, void>());
}

IMPL_LINK(WeldTextForwarder, NotifyHdl, EENotify&, rNotify, void)
{
    if (EditEngine* pEditEngine = m_rEditAcc.GetEditEngine())
    {
        if (rNotify.eNotificationType == EE_NOTIFY_PROCESSNOTIFICATIONS
            && !pEditEngine->IsUpdateLayout())
        {
            // tdf#143088 an UpdateMode of false will just to on to cause
            // AccessibleTextHelper_Impl::GetTextForwarder to throw an
            // exception as a Frozen EditEngine is considered Invalid so return
            // early instead
            return;
        }
    }

    ::std::unique_ptr<SfxHint> aHint = SvxEditSourceHelper::EENotification2Hint(&rNotify);
    if (aHint)
        m_rEditSource.GetBroadcaster().Broadcast(*aHint);
}

sal_Int32 WeldTextForwarder::GetParagraphCount() const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetParagraphCount() : 0;
}

sal_Int32 WeldTextForwarder::GetTextLen(sal_Int32 nParagraph) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetTextLen(nParagraph) : 0;
}

OUString WeldTextForwarder::GetText(const ESelection& rSel) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    OUString aRet;
    if (pEditEngine)
        aRet = pEditEngine->GetText(rSel);
    return convertLineEnd(aRet, GetSystemLineEnd());
}

SfxItemSet WeldTextForwarder::GetAttribs(const ESelection& rSel,
                                         EditEngineAttribs nOnlyHardAttrib) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    assert(pEditEngine && "EditEngine missing");
    if (rSel.nStartPara == rSel.nEndPara)
    {
        GetAttribsFlags nFlags = GetAttribsFlags::NONE;
        switch (nOnlyHardAttrib)
        {
            case EditEngineAttribs::All:
                nFlags = GetAttribsFlags::ALL;
                break;
            case EditEngineAttribs::OnlyHard:
                nFlags = GetAttribsFlags::CHARATTRIBS;
                break;
            default:
                SAL_WARN("svx", "unknown flags for WeldTextForwarder::GetAttribs");
        }

        return pEditEngine->GetAttribs(rSel.nStartPara, rSel.nStartPos, rSel.nEndPos, nFlags);
    }
    else
    {
        return pEditEngine->GetAttribs(rSel, nOnlyHardAttrib);
    }
}

SfxItemSet WeldTextForwarder::GetParaAttribs(sal_Int32 nPara) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    assert(pEditEngine && "EditEngine missing");

    SfxItemSet aSet(pEditEngine->GetParaAttribs(nPara));

    sal_uInt16 nWhich = EE_PARA_START;
    while (nWhich <= EE_PARA_END)
    {
        if (aSet.GetItemState(nWhich) != SfxItemState::SET)
        {
            if (pEditEngine->HasParaAttrib(nPara, nWhich))
                aSet.Put(pEditEngine->GetParaAttrib(nPara, nWhich));
        }
        nWhich++;
    }

    return aSet;
}

void WeldTextForwarder::SetParaAttribs(sal_Int32 nPara, const SfxItemSet& rSet)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->SetParaAttribs(nPara, rSet);
}

SfxItemPool* WeldTextForwarder::GetPool() const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetEmptyItemSet().GetPool() : nullptr;
}

void WeldTextForwarder::RemoveAttribs(const ESelection& rSelection)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->RemoveAttribs(rSelection, false /*bRemoveParaAttribs*/, 0);
}

void WeldTextForwarder::GetPortions(sal_Int32 nPara, std::vector<sal_Int32>& rList) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->GetPortions(nPara, rList);
}

void WeldTextForwarder::QuickInsertText(const OUString& rText, const ESelection& rSel)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->QuickInsertText(rText, rSel);
}

void WeldTextForwarder::QuickInsertLineBreak(const ESelection& rSel)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->QuickInsertLineBreak(rSel);
}

void WeldTextForwarder::QuickInsertField(const SvxFieldItem& rFld, const ESelection& rSel)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->QuickInsertField(rFld, rSel);
}

void WeldTextForwarder::QuickSetAttribs(const SfxItemSet& rSet, const ESelection& rSel)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->QuickSetAttribs(rSet, rSel);
}

bool WeldTextForwarder::IsValid() const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    // cannot reliably query EditEngine state
    // while in the middle of an update
    return pEditEngine && pEditEngine->IsUpdateLayout();
}

OUString WeldTextForwarder::CalcFieldValue(const SvxFieldItem& rField, sal_Int32 nPara,
                                           sal_Int32 nPos, std::optional<Color>& rpTxtColor,
                                           std::optional<Color>& rpFldColor)
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->CalcFieldValue(rField, nPara, nPos, rpTxtColor, rpFldColor)
                       : OUString();
}

void WeldTextForwarder::FieldClicked(const SvxFieldItem&) {}

static SfxItemState GetSvxEditEngineItemState(EditEngine const& rEditEngine, const ESelection& rSel,
                                              sal_uInt16 nWhich)
{
    std::vector<EECharAttrib> aAttribs;

    const SfxPoolItem* pLastItem = nullptr;

    SfxItemState eState = SfxItemState::DEFAULT;

    // check all paragraphs inside the selection
    for (sal_Int32 nPara = rSel.nStartPara; nPara <= rSel.nEndPara; nPara++)
    {
        SfxItemState eParaState = SfxItemState::DEFAULT;

        // calculate start and endpos for this paragraph
        sal_Int32 nPos = 0;
        if (rSel.nStartPara == nPara)
            nPos = rSel.nStartPos;

        sal_Int32 nEndPos = rSel.nEndPos;
        if (rSel.nEndPara != nPara)
            nEndPos = rEditEngine.GetTextLen(nPara);

        // get list of char attribs
        rEditEngine.GetCharAttribs(nPara, aAttribs);

        bool bEmpty = true; // we found no item inside the selection of this paragraph
        bool bGaps = false; // we found items but there are gaps between them
        sal_Int32 nLastEnd = nPos;

        const SfxPoolItem* pParaItem = nullptr;

        for (const auto& rAttrib : aAttribs)
        {
            OSL_ENSURE(rAttrib.pAttr, "GetCharAttribs gives corrupt data");

            const bool bEmptyPortion = (rAttrib.nStart == rAttrib.nEnd);
            if ((!bEmptyPortion && (rAttrib.nStart >= nEndPos))
                || (bEmptyPortion && (rAttrib.nStart > nEndPos)))
                break; // break if we are already behind our selection

            if ((!bEmptyPortion && (rAttrib.nEnd <= nPos))
                || (bEmptyPortion && (rAttrib.nEnd < nPos)))
                continue; // or if the attribute ends before our selection

            if (rAttrib.pAttr->Which() != nWhich)
                continue; // skip if is not the searched item

            // if we already found an item
            if (pParaItem)
            {
                // ... and its different to this one than the state is don't care
                if (*pParaItem != *(rAttrib.pAttr))
                    return SfxItemState::DONTCARE;
            }
            else
            {
                pParaItem = rAttrib.pAttr;
            }

            if (bEmpty)
                bEmpty = false;

            if (!bGaps && rAttrib.nStart > nLastEnd)
                bGaps = true;

            nLastEnd = rAttrib.nEnd;
        }

        if (!bEmpty && !bGaps && nLastEnd < (nEndPos - 1))
            bGaps = true;
        if (bEmpty)
            eParaState = SfxItemState::DEFAULT;
        else if (bGaps)
            eParaState = SfxItemState::DONTCARE;
        else
            eParaState = SfxItemState::SET;

        // if we already found an item check if we found the same
        if (pLastItem)
        {
            if ((pParaItem == nullptr) || (*pLastItem != *pParaItem))
                return SfxItemState::DONTCARE;
        }
        else
        {
            pLastItem = pParaItem;
            eState = eParaState;
        }
    }

    return eState;
}

SfxItemState WeldTextForwarder::GetItemState(const ESelection& rSel, sal_uInt16 nWhich) const
{
    SfxItemState nState = SfxItemState::DISABLED;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        nState = GetSvxEditEngineItemState(*pEditEngine, rSel, nWhich);
    return nState;
}

SfxItemState WeldTextForwarder::GetItemState(sal_Int32 nPara, sal_uInt16 nWhich) const
{
    SfxItemState nState = SfxItemState::DISABLED;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        const SfxItemSet& rSet = pEditEngine->GetParaAttribs(nPara);
        nState = rSet.GetItemState(nWhich);
    }
    return nState;
}

LanguageType WeldTextForwarder::GetLanguage(sal_Int32 nPara, sal_Int32 nIndex) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetLanguage(nPara, nIndex).nLang : LANGUAGE_NONE;
}

sal_Int32 WeldTextForwarder::GetFieldCount(sal_Int32 nPara) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetFieldCount(nPara) : 0;
}

EFieldInfo WeldTextForwarder::GetFieldInfo(sal_Int32 nPara, sal_uInt16 nField) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetFieldInfo(nPara, nField) : EFieldInfo();
}

EBulletInfo WeldTextForwarder::GetBulletInfo(sal_Int32 /*nPara*/) const { return EBulletInfo(); }

tools::Rectangle WeldTextForwarder::GetCharBounds(sal_Int32 nPara, sal_Int32 nIndex) const
{
    tools::Rectangle aRect(0, 0, 0, 0);
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();

    if (pEditEngine)
    {
        // Handle virtual position one-past-the end of the string
        if (nIndex >= pEditEngine->GetTextLen(nPara))
        {
            if (nIndex)
                aRect = pEditEngine->GetCharacterBounds(EPosition(nPara, nIndex - 1));

            aRect.Move(aRect.Right() - aRect.Left(), 0);
            aRect.SetSize(Size(1, pEditEngine->GetTextHeight()));
        }
        else
        {
            aRect = pEditEngine->GetCharacterBounds(EPosition(nPara, nIndex));
        }
    }
    return aRect;
}

tools::Rectangle WeldTextForwarder::GetParaBounds(sal_Int32 nPara) const
{
    tools::Rectangle aRect(0, 0, 0, 0);
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();

    if (pEditEngine)
    {
        const Point aPnt = pEditEngine->GetDocPosTopLeft(nPara);
        const sal_Int32 nWidth = pEditEngine->CalcTextWidth();
        const sal_Int32 nHeight = pEditEngine->GetTextHeight(nPara);
        aRect = tools::Rectangle(aPnt.X(), aPnt.Y(), aPnt.X() + nWidth, aPnt.Y() + nHeight);
    }

    return aRect;
}

MapMode WeldTextForwarder::GetMapMode() const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetRefMapMode() : MapMode(MapUnit::Map100thMM);
}

OutputDevice* WeldTextForwarder::GetRefDevice() const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetRefDevice() : nullptr;
}

bool WeldTextForwarder::GetIndexAtPoint(const Point& rPos, sal_Int32& nPara,
                                        sal_Int32& nIndex) const
{
    bool bRes = false;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        EPosition aDocPos = pEditEngine->FindDocPosition(rPos);
        nPara = aDocPos.nPara;
        nIndex = aDocPos.nIndex;
        bRes = true;
    }
    return bRes;
}

bool WeldTextForwarder::GetWordIndices(sal_Int32 nPara, sal_Int32 nIndex, sal_Int32& nStart,
                                       sal_Int32& nEnd) const
{
    bool bRes = false;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        ESelection aRes = pEditEngine->GetWord(ESelection(nPara, nIndex, nPara, nIndex),
                                               css::i18n::WordType::DICTIONARY_WORD);

        if (aRes.nStartPara == nPara && aRes.nStartPara == aRes.nEndPara)
        {
            nStart = aRes.nStartPos;
            nEnd = aRes.nEndPos;

            bRes = true;
        }
    }

    return bRes;
}

bool WeldTextForwarder::GetAttributeRun(sal_Int32& nStartIndex, sal_Int32& nEndIndex,
                                        sal_Int32 nPara, sal_Int32 nIndex, bool bInCell) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (!pEditEngine)
        return false;
    SvxEditSourceHelper::GetAttributeRun(nStartIndex, nEndIndex, *pEditEngine, nPara, nIndex,
                                         bInCell);
    return true;
}

sal_Int32 WeldTextForwarder::GetLineCount(sal_Int32 nPara) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetLineCount(nPara) : 0;
}

sal_Int32 WeldTextForwarder::GetLineLen(sal_Int32 nPara, sal_Int32 nLine) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetLineLen(nPara, nLine) : 0;
}

void WeldTextForwarder::GetLineBoundaries(/*out*/ sal_Int32& rStart, /*out*/ sal_Int32& rEnd,
                                          sal_Int32 nPara, sal_Int32 nLine) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
        pEditEngine->GetLineBoundaries(rStart, rEnd, nPara, nLine);
    else
        rStart = rEnd = 0;
}

sal_Int32 WeldTextForwarder::GetLineNumberAtIndex(sal_Int32 nPara, sal_Int32 nIndex) const
{
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    return pEditEngine ? pEditEngine->GetLineNumberAtIndex(nPara, nIndex) : 0;
}

bool WeldTextForwarder::QuickFormatDoc(bool /*bFull*/)
{
    bool bRes = false;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        pEditEngine->QuickFormatDoc();
        bRes = true;
    }
    return bRes;
}

sal_Int16 WeldTextForwarder::GetDepth(sal_Int32 /*nPara*/) const
{
    // math has no outliner...
    return -1;
}

bool WeldTextForwarder::SetDepth(sal_Int32 /*nPara*/, sal_Int16 nNewDepth)
{
    // math has no outliner...
    return -1 == nNewDepth; // is it the value from 'GetDepth' ?
}

bool WeldTextForwarder::Delete(const ESelection& rSelection)
{
    bool bRes = false;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        pEditEngine->QuickDelete(rSelection);
        pEditEngine->QuickFormatDoc();
        bRes = true;
    }
    return bRes;
}

bool WeldTextForwarder::InsertText(const OUString& rStr, const ESelection& rSelection)
{
    bool bRes = false;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        pEditEngine->QuickInsertText(rStr, rSelection);
        pEditEngine->QuickFormatDoc();
        bRes = true;
    }
    return bRes;
}

const SfxItemSet* WeldTextForwarder::GetEmptyItemSetPtr()
{
    const SfxItemSet* pItemSet = nullptr;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        pItemSet = &pEditEngine->GetEmptyItemSet();
    }
    return pItemSet;
}

void WeldTextForwarder::AppendParagraph()
{
    // append an empty paragraph
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine)
    {
        sal_Int32 nParaCount = pEditEngine->GetParagraphCount();
        pEditEngine->InsertParagraph(nParaCount, OUString());
    }
}

sal_Int32 WeldTextForwarder::AppendTextPortion(sal_Int32 nPara, const OUString& rText,
                                               const SfxItemSet& rSet)
{
    sal_uInt16 nRes = 0;
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine && nPara < pEditEngine->GetParagraphCount())
    {
        // append text
        ESelection aSel(nPara, pEditEngine->GetTextLen(nPara));
        pEditEngine->QuickInsertText(rText, aSel);

        // set attributes for new appended text
        nRes = aSel.nEndPos = pEditEngine->GetTextLen(nPara);
        pEditEngine->QuickSetAttribs(rSet, aSel);
    }
    return nRes;
}

void WeldTextForwarder::CopyText(const SvxTextForwarder& rSource)
{
    const WeldTextForwarder* pSourceForwarder = dynamic_cast<const WeldTextForwarder*>(&rSource);
    if (!pSourceForwarder)
        return;
    EditEngine* pSourceEditEngine = pSourceForwarder->m_rEditAcc.GetEditEngine();
    EditEngine* pEditEngine = m_rEditAcc.GetEditEngine();
    if (pEditEngine && pSourceEditEngine)
    {
        std::unique_ptr<EditTextObject> pNewTextObject = pSourceEditEngine->CreateTextObject();
        pEditEngine->SetText(*pNewTextObject);
    }
}

WeldEditViewForwarder::WeldEditViewForwarder(WeldEditAccessible& rAcc)
    : m_rEditAcc(rAcc)
{
}

bool WeldEditViewForwarder::IsValid() const { return m_rEditAcc.GetEditView() != nullptr; }

Point WeldEditViewForwarder::LogicToPixel(const Point& rPoint, const MapMode& rMapMode) const
{
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (!pEditView)
        return Point();
    OutputDevice& rOutDev = pEditView->GetOutputDevice();
    MapMode aMapMode(rOutDev.GetMapMode());
    Point aPoint(OutputDevice::LogicToLogic(rPoint, rMapMode, MapMode(aMapMode.GetMapUnit())));
    aMapMode.SetOrigin(Point());
    return rOutDev.LogicToPixel(aPoint, aMapMode);
}

Point WeldEditViewForwarder::PixelToLogic(const Point& rPoint, const MapMode& rMapMode) const
{
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (!pEditView)
        return Point();
    OutputDevice& rOutDev = pEditView->GetOutputDevice();
    MapMode aMapMode(rOutDev.GetMapMode());
    aMapMode.SetOrigin(Point());
    Point aPoint(rOutDev.PixelToLogic(rPoint, aMapMode));
    return OutputDevice::LogicToLogic(aPoint, MapMode(aMapMode.GetMapUnit()), rMapMode);
}

bool WeldEditViewForwarder::GetSelection(ESelection& rSelection) const
{
    bool bRes = false;
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (pEditView)
    {
        rSelection = pEditView->GetSelection();
        bRes = true;
    }
    return bRes;
}

bool WeldEditViewForwarder::SetSelection(const ESelection& rSelection)
{
    bool bRes = false;
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (pEditView)
    {
        pEditView->SetSelection(rSelection);
        bRes = true;
    }
    return bRes;
}

bool WeldEditViewForwarder::Copy()
{
    bool bRes = false;
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (pEditView)
    {
        pEditView->Copy();
        bRes = true;
    }
    return bRes;
}

bool WeldEditViewForwarder::Cut()
{
    bool bRes = false;
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (pEditView)
    {
        pEditView->Cut();
        bRes = true;
    }
    return bRes;
}

bool WeldEditViewForwarder::Paste()
{
    bool bRes = false;
    EditView* pEditView = m_rEditAcc.GetEditView();
    if (pEditView)
    {
        pEditView->Paste();
        bRes = true;
    }
    return bRes;
}

void WeldEditView::SetDrawingArea(weld::DrawingArea* pDrawingArea)
{
    Size aSize(pDrawingArea->get_size_request());
    if (aSize.Width() == -1)
        aSize.setWidth(500);
    if (aSize.Height() == -1)
        aSize.setHeight(100);
    pDrawingArea->set_size_request(aSize.Width(), aSize.Height());

    SetOutputSizePixel(aSize);

    weld::CustomWidgetController::SetDrawingArea(pDrawingArea);

    EnableRTL(false);

    const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
    Color aBgColor = rStyleSettings.GetWindowColor();

    OutputDevice& rDevice = pDrawingArea->get_ref_device();

    rDevice.SetMapMode(MapMode(MapUnit::MapTwip));
    rDevice.SetBackground(aBgColor);

    Size aOutputSize(rDevice.PixelToLogic(aSize));

    makeEditEngine();
    m_xEditEngine->SetPaperSize(aOutputSize);
    m_xEditEngine->SetRefDevice(&rDevice);

    m_xEditEngine->SetControlWord(m_xEditEngine->GetControlWord() | EEControlBits::MARKFIELDS);

    m_xEditView.reset(new EditView(m_xEditEngine.get(), nullptr));
    m_xEditView->setEditViewCallbacks(this);
    m_xEditView->SetOutputArea(tools::Rectangle(Point(0, 0), aOutputSize));

    m_xEditView->SetBackgroundColor(aBgColor);
    m_xEditEngine->SetBackgroundColor(aBgColor);
    m_xEditEngine->InsertView(m_xEditView.get());

    pDrawingArea->set_cursor(PointerStyle::Text);

#if !ENABLE_WASM_STRIP_ACCESSIBILITY
    InitAccessible();
#endif
}

#if !ENABLE_WASM_STRIP_ACCESSIBILITY
void WeldEditView::InitAccessible()
{
    if (m_xAccessible.is())
        m_xAccessible->Init(GetEditEngine(), GetEditView());
}
#endif

int WeldEditView::GetSurroundingText(OUString& rSurrounding)
{
    EditView* pEditView = GetEditView();
    if (!pEditView)
        return -1;
    rSurrounding = pEditView->GetSurroundingText();
    return pEditView->GetSurroundingTextSelection().Min();
}

bool WeldEditView::DeleteSurroundingText(const Selection& rRange)
{
    EditView* pEditView = GetEditView();
    if (!pEditView)
        return false;
    return pEditView->DeleteSurroundingText(rRange);
}

void WeldEditView::GetFocus()
{
    EditView* pEditView = GetEditView();
    if (pEditView)
    {
        pEditView->ShowCursor(false);
        Invalidate(); // redraw with cursor
    }

    weld::CustomWidgetController::GetFocus();

#if !ENABLE_WASM_STRIP_ACCESSIBILITY
    if (m_xAccessible.is())
    {
        // Note: will implicitly send the AccessibleStateType::FOCUSED event
        ::accessibility::AccessibleTextHelper* pHelper = m_xAccessible->GetTextHelper();
        if (pHelper)
            pHelper->SetFocus();
    }
#endif
}

void WeldEditView::LoseFocus()
{
    weld::CustomWidgetController::LoseFocus();
    Invalidate(); // redraw without cursor

#if !ENABLE_WASM_STRIP_ACCESSIBILITY
    if (m_xAccessible.is())
    {
        // Note: will implicitly send the AccessibleStateType::FOCUSED event
        ::accessibility::AccessibleTextHelper* pHelper = m_xAccessible->GetTextHelper();
        if (pHelper)
            pHelper->SetFocus(false);
    }
#endif
}

bool WeldEditView::CanFocus() const { return true; }

css::uno::Reference<css::datatransfer::dnd::XDropTarget> WeldEditView::GetDropTarget()
{
    if (!m_xDropTarget)
        m_xDropTarget = weld::CustomWidgetController::GetDropTarget();
    return m_xDropTarget;
}

css::uno::Reference<css::datatransfer::clipboard::XClipboard> WeldEditView::GetClipboard() const
{
    return weld::CustomWidgetController::GetClipboard();
}

namespace
{
class WeldEditViewUIObject final : public DrawingAreaUIObject
{
private:
    WeldEditView* mpEditView;

public:
    WeldEditViewUIObject(const VclPtr<vcl::Window>& rDrawingArea)
        : DrawingAreaUIObject(rDrawingArea)
        , mpEditView(static_cast<WeldEditView*>(mpController))
    {
    }

    static std::unique_ptr<UIObject> create(vcl::Window* pWindow)
    {
        return std::unique_ptr<UIObject>(new WeldEditViewUIObject(pWindow));
    }

    virtual StringMap get_state() override
    {
        StringMap aMap = WindowUIObject::get_state();
        aMap["Text"] = mpEditView->GetText();
        return aMap;
    }

private:
    virtual OUString get_name() const override { return "WeldEditViewUIObject"; }
};
}

FactoryFunction WeldEditView::GetUITestFactory() const { return WeldEditViewUIObject::create; }

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */