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
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
/* -*- 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 <com/sun/star/i18n/WordType.hpp>
#include <editeng/editdata.hxx>
#include <editeng/editeng.hxx>
#include <editeng/editobj.hxx>
#include <editeng/editstat.hxx>
#include <editeng/outlobj.hxx>
#include <editeng/unotext.hxx>
#include <svl/itemiter.hxx>
#include <svl/style.hxx>
#include <svl/whiter.hxx>
#include <svtools/accessibilityoptions.hxx>
#include <svx/sdtfchim.hxx>
#include <svx/selectioncontroller.hxx>
#include <svx/svdedxv.hxx>
#include <svx/svdetc.hxx>
#include <svx/svdotable.hxx>
#include <svx/svdotext.hxx>
#include <svx/svdoutl.hxx>
#include <svx/svdpage.hxx>
#include <svx/svdpagv.hxx>
#include <svx/svdundo.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/commandevent.hxx>
#include <vcl/cursor.hxx>
#include <vcl/weld.hxx>
#include <comphelper/lok.hxx>
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <drawinglayer/processor2d/processor2dtools.hxx>
#include <editeng/outliner.hxx>
#include <sal/log.hxx>
#include <sdr/overlay/overlaytools.hxx>
#include <sfx2/viewsh.hxx>
#include <svx/dialmgr.hxx>
#include <svx/sdr/overlay/overlaymanager.hxx>
#include <svx/sdr/overlay/overlayselection.hxx>
#include <svx/sdr/table/tablecontroller.hxx>
#include <svx/sdrpagewindow.hxx>
#include <svx/sdrpaintwindow.hxx>
#include <svx/sdrundomanager.hxx>
#include <svx/strings.hrc>
#include <svx/svdviter.hxx>
#include <textchain.hxx>
#include <textchaincursor.hxx>
#include <tools/debug.hxx>
#include <vcl/svapp.hxx>

#include <memory>

void SdrObjEditView::ImpClearVars()
{
    bQuickTextEditMode = true;
    pTextEditOutliner.reset();
    pTextEditOutlinerView = nullptr;
    pTextEditPV = nullptr;
    pTextEditWin = nullptr;
    pTextEditCursorBuffer = nullptr;
    bTextEditNewObj = false;
    bMacroDown = false;
    pMacroObj = nullptr;
    pMacroPV = nullptr;
    pMacroWin = nullptr;
    nMacroTol = 0;
    bTextEditDontDelete = false;
    bTextEditOnlyOneView = false;
}

SdrObjEditView::SdrObjEditView(SdrModel& rSdrModel, OutputDevice* pOut)
    : SdrGlueEditView(rSdrModel, pOut)
    , mpOldTextEditUndoManager(nullptr)
{
    ImpClearVars();
}

SdrObjEditView::~SdrObjEditView()
{
    pTextEditWin = nullptr; // so there's no ShowCursor in SdrEndTextEdit
    assert(!IsTextEdit());
    if (IsTextEdit())
        SdrEndTextEdit();
    pTextEditOutliner.reset();
    assert(nullptr == mpOldTextEditUndoManager); // should have been reset
}

bool SdrObjEditView::IsAction() const { return IsMacroObj() || SdrGlueEditView::IsAction(); }

void SdrObjEditView::MovAction(const Point& rPnt)
{
    if (IsMacroObj())
        MovMacroObj(rPnt);
    SdrGlueEditView::MovAction(rPnt);
}

void SdrObjEditView::EndAction()
{
    if (IsMacroObj())
        EndMacroObj();
    SdrGlueEditView::EndAction();
}

void SdrObjEditView::BckAction()
{
    BrkMacroObj();
    SdrGlueEditView::BckAction();
}

void SdrObjEditView::BrkAction()
{
    BrkMacroObj();
    SdrGlueEditView::BrkAction();
}

SdrPageView* SdrObjEditView::ShowSdrPage(SdrPage* pPage)
{
    SdrPageView* pPageView = SdrGlueEditView::ShowSdrPage(pPage);

    if (comphelper::LibreOfficeKit::isActive() && pPageView)
    {
        // Check if other views have an active text edit on the same page as
        // this one.
        SdrViewIter aIter(pPageView->GetPage());
        for (SdrView* pView = aIter.FirstView(); pView; pView = aIter.NextView())
        {
            if (pView == this || !pView->IsTextEdit())
                continue;

            OutputDevice* pOutDev = GetFirstOutputDevice();
            if (!pOutDev || pOutDev->GetOutDevType() != OUTDEV_WINDOW)
                continue;

            // Found one, so create an outliner view, to get invalidations when
            // the text edit changes.
            // Call GetSfxViewShell() to make sure ImpMakeOutlinerView()
            // registers the view shell of this draw view, and not the view
            // shell of pView.
            OutlinerView* pOutlinerView = pView->ImpMakeOutlinerView(
                static_cast<vcl::Window*>(pOutDev), nullptr, GetSfxViewShell());
            pOutlinerView->HideCursor();
            pView->GetTextEditOutliner()->InsertView(pOutlinerView);
        }
    }

    return pPageView;
}

namespace
{
/// Removes outliner views registered in other draw views that use pOutputDevice.
void lcl_RemoveTextEditOutlinerViews(SdrObjEditView const* pThis, SdrPageView const* pPageView,
                                     OutputDevice const* pOutputDevice)
{
    if (!comphelper::LibreOfficeKit::isActive())
        return;

    if (!pPageView)
        return;

    if (!pOutputDevice || pOutputDevice->GetOutDevType() != OUTDEV_WINDOW)
        return;

    SdrViewIter aIter(pPageView->GetPage());
    for (SdrView* pView = aIter.FirstView(); pView; pView = aIter.NextView())
    {
        if (pView == pThis || !pView->IsTextEdit())
            continue;

        SdrOutliner* pOutliner = pView->GetTextEditOutliner();
        for (size_t nView = 0; nView < pOutliner->GetViewCount(); ++nView)
        {
            OutlinerView* pOutlinerView = pOutliner->GetView(nView);
            if (pOutlinerView->GetWindow() != pOutputDevice)
                continue;

            pOutliner->RemoveView(pOutlinerView);
            delete pOutlinerView;
        }
    }
}
}

void SdrObjEditView::HideSdrPage()
{
    lcl_RemoveTextEditOutlinerViews(this, GetSdrPageView(), GetFirstOutputDevice());

    SdrGlueEditView::HideSdrPage();
}

void SdrObjEditView::TakeActionRect(tools::Rectangle& rRect) const
{
    if (IsMacroObj())
    {
        rRect = pMacroObj->GetCurrentBoundRect();
    }
    else
    {
        SdrGlueEditView::TakeActionRect(rRect);
    }
}

void SdrObjEditView::Notify(SfxBroadcaster& rBC, const SfxHint& rHint)
{
    SdrGlueEditView::Notify(rBC, rHint);
    if (pTextEditOutliner != nullptr)
    {
        // change of printer while editing
        if (rHint.GetId() == SfxHintId::ThisIsAnSdrHint)
        {
            const SdrHint* pSdrHint = static_cast<const SdrHint*>(&rHint);
            SdrHintKind eKind = pSdrHint->GetKind();
            if (eKind == SdrHintKind::RefDeviceChange)
            {
                pTextEditOutliner->SetRefDevice(mpModel->GetRefDevice());
            }
            if (eKind == SdrHintKind::DefaultTabChange)
            {
                pTextEditOutliner->SetDefTab(mpModel->GetDefaultTabulator());
            }
        }
    }
}

void SdrObjEditView::ModelHasChanged()
{
    SdrGlueEditView::ModelHasChanged();
    if (mxTextEditObj.is() && !mxTextEditObj->IsInserted())
        SdrEndTextEdit(); // object deleted
    // TextEditObj changed?
    if (IsTextEdit())
    {
        SdrTextObj* pTextObj = mxTextEditObj.get();
        if (pTextObj != nullptr)
        {
            size_t nOutlViewCnt = pTextEditOutliner->GetViewCount();
            bool bAreaChg = false;
            bool bAnchorChg = false;
            bool bColorChg = false;
            bool bContourFrame = pTextObj->IsContourTextFrame();
            EEAnchorMode eNewAnchor(EEAnchorMode::VCenterHCenter);
            tools::Rectangle aOldArea(aMinTextEditArea);
            aOldArea.Union(aTextEditArea);
            Color aNewColor;
            { // check area
                Size aPaperMin1;
                Size aPaperMax1;
                tools::Rectangle aEditArea1;
                tools::Rectangle aMinArea1;
                pTextObj->TakeTextEditArea(&aPaperMin1, &aPaperMax1, &aEditArea1, &aMinArea1);
                Point aPvOfs(pTextObj->GetTextEditOffset());

                // add possible GridOffset to up-to-now view-independent EditAreas
                basegfx::B2DVector aGridOffset(0.0, 0.0);
                if (getPossibleGridOffsetForSdrObject(aGridOffset, pTextObj, GetSdrPageView()))
                {
                    const Point aOffset(basegfx::fround(aGridOffset.getX()),
                                        basegfx::fround(aGridOffset.getY()));

                    aEditArea1 += aOffset;
                    aMinArea1 += aOffset;
                }

                aEditArea1.Move(aPvOfs.X(), aPvOfs.Y());
                aMinArea1.Move(aPvOfs.X(), aPvOfs.Y());
                tools::Rectangle aNewArea(aMinArea1);
                aNewArea.Union(aEditArea1);

                if (aNewArea != aOldArea || aEditArea1 != aTextEditArea
                    || aMinArea1 != aMinTextEditArea
                    || pTextEditOutliner->GetMinAutoPaperSize() != aPaperMin1
                    || pTextEditOutliner->GetMaxAutoPaperSize() != aPaperMax1)
                {
                    aTextEditArea = aEditArea1;
                    aMinTextEditArea = aMinArea1;
                    pTextEditOutliner->SetUpdateMode(false);
                    pTextEditOutliner->SetMinAutoPaperSize(aPaperMin1);
                    pTextEditOutliner->SetMaxAutoPaperSize(aPaperMax1);
                    pTextEditOutliner->SetPaperSize(Size(0, 0)); // re-format Outliner
                    if (!bContourFrame)
                    {
                        pTextEditOutliner->ClearPolygon();
                        EEControlBits nStat = pTextEditOutliner->GetControlWord();
                        nStat |= EEControlBits::AUTOPAGESIZE;
                        pTextEditOutliner->SetControlWord(nStat);
                    }
                    else
                    {
                        EEControlBits nStat = pTextEditOutliner->GetControlWord();
                        nStat &= ~EEControlBits::AUTOPAGESIZE;
                        pTextEditOutliner->SetControlWord(nStat);
                        tools::Rectangle aAnchorRect;
                        pTextObj->TakeTextAnchorRect(aAnchorRect);
                        pTextObj->ImpSetContourPolygon(*pTextEditOutliner, aAnchorRect, true);
                    }
                    for (size_t nOV = 0; nOV < nOutlViewCnt; nOV++)
                    {
                        OutlinerView* pOLV = pTextEditOutliner->GetView(nOV);
                        EVControlBits nStat0 = pOLV->GetControlWord();
                        EVControlBits nStat = nStat0;
                        // AutoViewSize only if not ContourFrame.
                        if (!bContourFrame)
                            nStat |= EVControlBits::AUTOSIZE;
                        else
                            nStat &= ~EVControlBits::AUTOSIZE;
                        if (nStat != nStat0)
                            pOLV->SetControlWord(nStat);
                    }
                    pTextEditOutliner->SetUpdateMode(true);
                    bAreaChg = true;
                }
            }
            if (pTextEditOutlinerView != nullptr)
            { // check fill and anchor
                EEAnchorMode eOldAnchor = pTextEditOutlinerView->GetAnchorMode();
                eNewAnchor = pTextObj->GetOutlinerViewAnchorMode();
                bAnchorChg = eOldAnchor != eNewAnchor;
                Color aOldColor(pTextEditOutlinerView->GetBackgroundColor());
                aNewColor = GetTextEditBackgroundColor(*this);
                bColorChg = aOldColor != aNewColor;
            }
            // refresh always when it's a contour frame. That
            // refresh is necessary since it triggers the repaint
            // which makes the Handles visible. Changes at TakeTextRect()
            // seem to have resulted in a case where no refresh is executed.
            // Before that, a refresh must have been always executed
            // (else this error would have happened earlier), thus I
            // even think here a refresh should be done always.
            // Since follow-up problems cannot even be guessed I only
            // add this one more case to the if below.
            // BTW: It's VERY bad style that here, inside ModelHasChanged()
            // the outliner is again massively changed for the text object
            // in text edit mode. Normally, all necessary data should be
            // set at SdrBeginTextEdit(). Some changes and value assigns in
            // SdrBeginTextEdit() are completely useless since they are set here
            // again on ModelHasChanged().
            if (bContourFrame || bAreaChg || bAnchorChg || bColorChg)
            {
                for (size_t nOV = 0; nOV < nOutlViewCnt; nOV++)
                {
                    OutlinerView* pOLV = pTextEditOutliner->GetView(nOV);
                    { // invalidate old OutlinerView area
                        vcl::Window* pWin = pOLV->GetWindow();
                        tools::Rectangle aTmpRect(aOldArea);
                        sal_uInt16 nPixSiz = pOLV->GetInvalidateMore() + 1;
                        Size aMore(pWin->PixelToLogic(Size(nPixSiz, nPixSiz)));
                        aTmpRect.AdjustLeft(-(aMore.Width()));
                        aTmpRect.AdjustRight(aMore.Width());
                        aTmpRect.AdjustTop(-(aMore.Height()));
                        aTmpRect.AdjustBottom(aMore.Height());
                        InvalidateOneWin(*pWin, aTmpRect);
                    }
                    if (bAnchorChg)
                        pOLV->SetAnchorMode(eNewAnchor);
                    if (bColorChg)
                        pOLV->SetBackgroundColor(aNewColor);

                    pOLV->SetOutputArea(
                        aTextEditArea); // because otherwise, we're not re-anchoring correctly
                    ImpInvalidateOutlinerView(*pOLV);
                }
                pTextEditOutlinerView->ShowCursor();
            }
        }
        ImpMakeTextCursorAreaVisible();
    }
}

namespace
{
/**
        Helper class to visualize the content of an active EditView as an
        OverlayObject. These objects work with Primitives and are handled
        from the OverlayManager(s) in place as needed.

        It allows complete visualization of the content of the active
        EditView without the need of Invalidates triggered by the EditView
        and thus avoiding potentially expensive repaints by using the
        automatically buffered Overlay mechanism.

        It buffers as much as possible locally and *only* triggers a real
        change (see call to objectChange()) when really needed.
     */
class TextEditOverlayObject : public sdr::overlay::OverlayObject
{
protected:
    /// local access to associated sdr::overlay::OverlaySelection
    sdr::overlay::OverlaySelection* mpOverlaySelection;

    /// local definition depends on active OutlinerView
    OutlinerView& mrOutlinerView;

    /// geometry definitions with buffering
    basegfx::B2DRange maLastRange;
    basegfx::B2DRange maRange;

    /// text content definitions with buffering
    drawinglayer::primitive2d::Primitive2DContainer maTextPrimitives;
    drawinglayer::primitive2d::Primitive2DContainer maLastTextPrimitives;

    /// bitfield
    bool mbVisualizeSurroundingFrame : 1;

    // geometry creation for OverlayObject, can use local *Last* values
    virtual drawinglayer::primitive2d::Primitive2DContainer
    createOverlayObjectPrimitive2DSequence() override;

public:
    TextEditOverlayObject(const Color& rColor, OutlinerView& rOutlinerView,
                          bool bVisualizeSurroundingFrame);
    virtual ~TextEditOverlayObject() override;

    // data read access
    const sdr::overlay::OverlaySelection* getOverlaySelection() const { return mpOverlaySelection; }
    const OutlinerView& getOutlinerView() const { return mrOutlinerView; }

    /// override to check conditions for last createOverlayObjectPrimitive2DSequence
    virtual drawinglayer::primitive2d::Primitive2DContainer
    getOverlayObjectPrimitive2DSequence() const override;

    // data write access. In this OverlayObject we only have the
    // callback that triggers detecting if something *has* changed
    void checkDataChange(const basegfx::B2DRange& rMinTextEditArea);
    void checkSelectionChange();
};

drawinglayer::primitive2d::Primitive2DContainer
TextEditOverlayObject::createOverlayObjectPrimitive2DSequence()
{
    drawinglayer::primitive2d::Primitive2DContainer aRetval;

    /// outer frame visualization
    if (mbVisualizeSurroundingFrame)
    {
        const SvtOptionsDrawinglayer aSvtOptionsDrawinglayer;
        const double fTransparence(aSvtOptionsDrawinglayer.GetTransparentSelectionPercent() * 0.01);
        const sal_uInt16 nPixSiz(getOutlinerView().GetInvalidateMore() - 1);

        aRetval.push_back(new drawinglayer::primitive2d::OverlayRectanglePrimitive(
            maRange, getBaseColor().getBColor(), fTransparence, std::max(6, nPixSiz - 2), // grow
            0.0, // shrink
            0.0));
    }

    // add buffered TextPrimitives
    aRetval.append(maTextPrimitives);

    return aRetval;
}

TextEditOverlayObject::TextEditOverlayObject(const Color& rColor, OutlinerView& rOutlinerView,
                                             bool bVisualizeSurroundingFrame)
    : OverlayObject(rColor)
    , mpOverlaySelection(nullptr)
    , mrOutlinerView(rOutlinerView)
    , maLastRange()
    , maRange()
    , maTextPrimitives()
    , maLastTextPrimitives()
    , mbVisualizeSurroundingFrame(bVisualizeSurroundingFrame)
{
    // no AA for TextEdit overlay
    allowAntiAliase(false);

    // create local OverlaySelection - this is an integral part of EditText
    // visualization
    const std::vector<basegfx::B2DRange> aEmptySelection{};
    mpOverlaySelection = new sdr::overlay::OverlaySelection(sdr::overlay::OverlayType::Transparent,
                                                            rColor, aEmptySelection, true);
}

TextEditOverlayObject::~TextEditOverlayObject()
{
    if (getOverlaySelection())
    {
        delete mpOverlaySelection;
        mpOverlaySelection = nullptr;
    }

    if (getOverlayManager())
    {
        getOverlayManager()->remove(*this);
    }
}

drawinglayer::primitive2d::Primitive2DContainer
TextEditOverlayObject::getOverlayObjectPrimitive2DSequence() const
{
    if (!getPrimitive2DSequence().empty())
    {
        if (!maRange.equal(maLastRange) || maLastTextPrimitives != maTextPrimitives)
        {
            // conditions of last local decomposition have changed, delete to force new evaluation
            const_cast<TextEditOverlayObject*>(this)->resetPrimitive2DSequence();
        }
    }

    if (getPrimitive2DSequence().empty())
    {
        // remember new buffered values
        const_cast<TextEditOverlayObject*>(this)->maLastRange = maRange;
        const_cast<TextEditOverlayObject*>(this)->maLastTextPrimitives = maTextPrimitives;
    }

    // call base implementation
    return OverlayObject::getOverlayObjectPrimitive2DSequence();
}

void TextEditOverlayObject::checkDataChange(const basegfx::B2DRange& rMinTextEditArea)
{
    bool bObjectChange(false);

    // check current range
    const tools::Rectangle aOutArea(mrOutlinerView.GetOutputArea());
    basegfx::B2DRange aNewRange = vcl::unotools::b2DRectangleFromRectangle(aOutArea);
    aNewRange.expand(rMinTextEditArea);

    if (aNewRange != maRange)
    {
        maRange = aNewRange;
        bObjectChange = true;
    }

    // check if text primitives did change
    SdrOutliner* pSdrOutliner = dynamic_cast<SdrOutliner*>(getOutlinerView().GetOutliner());

    if (pSdrOutliner)
    {
        // get TextPrimitives directly from active Outliner
        basegfx::B2DHomMatrix aNewTransformA;
        basegfx::B2DHomMatrix aNewTransformB;
        basegfx::B2DRange aClipRange;
        drawinglayer::primitive2d::Primitive2DContainer aNewTextPrimitives;

        // active Outliner is always in unified oriented coordinate system (currently)
        // so just translate to TopLeft of visible Range. Keep in mind that top-left
        // depends on vertical text and top-to-bottom text attributes
        const tools::Rectangle aVisArea(mrOutlinerView.GetVisArea());
        const bool bVerticalWriting(pSdrOutliner->IsVertical());
        const bool bTopToBottom(pSdrOutliner->IsTopToBottom());
        const double fStartInX(bVerticalWriting && bTopToBottom
                                   ? aOutArea.Right() - aVisArea.Left()
                                   : aOutArea.Left() - aVisArea.Left());
        const double fStartInY(bVerticalWriting && !bTopToBottom
                                   ? aOutArea.Bottom() - aVisArea.Top()
                                   : aOutArea.Top() - aVisArea.Top());

        aNewTransformB.translate(fStartInX, fStartInY);

        // get the current TextPrimitives. This is the most expensive part
        // of this mechanism, it *may* be possible to buffer layouted
        // primitives per ParaPortion with/in/dependent on the EditEngine
        // content if needed. For now, get and compare
        SdrTextObj::impDecomposeBlockTextPrimitiveDirect(
            aNewTextPrimitives, *pSdrOutliner, aNewTransformA, aNewTransformB, aClipRange);

        if (aNewTextPrimitives != maTextPrimitives)
        {
            maTextPrimitives = aNewTextPrimitives;
            bObjectChange = true;
        }
    }

    if (bObjectChange)
    {
        // if there really *was* a change signal the OverlayManager to
        // refresh this object's visualization
        objectChange();

        // on data change, always do a SelectionChange, too
        // since the selection is an integral part of text visualization
        checkSelectionChange();
    }
}

void TextEditOverlayObject::checkSelectionChange()
{
    if (getOverlaySelection() && getOverlayManager())
    {
        std::vector<tools::Rectangle> aLogicRects;
        std::vector<basegfx::B2DRange> aLogicRanges;
        const Size aLogicPixel(getOverlayManager()->getOutputDevice().PixelToLogic(Size(1, 1)));

        // get logic selection
        getOutlinerView().GetSelectionRectangles(aLogicRects);

        aLogicRanges.reserve(aLogicRects.size());
        for (const auto& aRect : aLogicRects)
        {
            // convert from logic Rectangles to logic Ranges, do not forget to add
            // one Unit (in this case logical units for one pixel, pre-calculated)
            aLogicRanges.emplace_back(<--- Consider using std::transform algorithm instead of a raw loop.
                aRect.Left() - aLogicPixel.Width(), aRect.Top() - aLogicPixel.Height(),
                aRect.Right() + aLogicPixel.Width(), aRect.Bottom() + aLogicPixel.Height());
        }

        mpOverlaySelection->setRanges(aLogicRanges);
    }
}
} // end of anonymous namespace

// TextEdit

// callback from the active EditView, forward to evtl. existing instances of the
// TextEditOverlayObject(s). This will additionally update the selection which
// is an integral part of the text visualization
void SdrObjEditView::EditViewInvalidate(const tools::Rectangle&) const
{
    if (IsTextEdit())
    {
        // MinTextRange may have changed. Forward it, too
        const basegfx::B2DRange aMinTextRange
            = vcl::unotools::b2DRectangleFromRectangle(aMinTextEditArea);

        for (sal_uInt32 a(0); a < maTEOverlayGroup.count(); a++)
        {
            TextEditOverlayObject* pCandidate
                = dynamic_cast<TextEditOverlayObject*>(&maTEOverlayGroup.getOverlayObject(a));

            if (pCandidate)
            {
                pCandidate->checkDataChange(aMinTextRange);
            }
        }
    }
}

// callback from the active EditView, forward to evtl. existing instances of the
// TextEditOverlayObject(s). This cvall *only* updates the selection visualization
// which is e.g. used when only the selection is changed, but not the text
void SdrObjEditView::EditViewSelectionChange() const
{
    if (IsTextEdit())
    {
        for (sal_uInt32 a(0); a < maTEOverlayGroup.count(); a++)
        {
            TextEditOverlayObject* pCandidate
                = dynamic_cast<TextEditOverlayObject*>(&maTEOverlayGroup.getOverlayObject(a));

            if (pCandidate)
            {
                pCandidate->checkSelectionChange();
            }
        }
    }
}

OutputDevice& SdrObjEditView::EditViewOutputDevice() const { return *pTextEditWin; }

void SdrObjEditView::TextEditDrawing(SdrPaintWindow& rPaintWindow) const
{
    if (!comphelper::LibreOfficeKit::isActive())
    {
        // adapt all TextEditOverlayObject(s), so call EditViewInvalidate()
        // to update accordingly (will update selection, too). Suppress new
        // stuff when LibreOfficeKit is active
        EditViewInvalidate(tools::Rectangle());
    }
    else
    {
        // draw old text edit stuff
        if (IsTextEdit())
        {
            const SdrOutliner* pActiveOutliner = GetTextEditOutliner();

            if (pActiveOutliner)
            {
                const sal_uInt32 nViewCount(pActiveOutliner->GetViewCount());

                if (nViewCount)
                {
                    const vcl::Region& rRedrawRegion = rPaintWindow.GetRedrawRegion();
                    const tools::Rectangle aCheckRect(rRedrawRegion.GetBoundRect());

                    for (sal_uInt32 i(0); i < nViewCount; i++)
                    {
                        OutlinerView* pOLV = pActiveOutliner->GetView(i);

                        // If rPaintWindow knows that the output device is a render
                        // context and is aware of the underlying vcl::Window,
                        // compare against that; that's how double-buffering can
                        // still find the matching OutlinerView.
                        OutputDevice* pOutputDevice = rPaintWindow.GetWindow()
                                                          ? rPaintWindow.GetWindow()
                                                          : &rPaintWindow.GetOutputDevice();
                        if (pOLV->GetWindow() == pOutputDevice
                            || comphelper::LibreOfficeKit::isActive())
                        {
                            ImpPaintOutlinerView(*pOLV, aCheckRect,
                                                 rPaintWindow.GetTargetOutputDevice());
                            return;
                        }
                    }
                }
            }
        }
    }
}

void SdrObjEditView::ImpPaintOutlinerView(OutlinerView& rOutlView, const tools::Rectangle& rRect,
                                          OutputDevice& rTargetDevice) const
{
    const SdrTextObj* pText = GetTextEditObject();
    bool bTextFrame(pText && pText->IsTextFrame());
    bool bFitToSize(pTextEditOutliner->GetControlWord() & EEControlBits::STRETCHING);
    bool bModified(pTextEditOutliner->IsModified());
    tools::Rectangle aBlankRect(rOutlView.GetOutputArea());
    aBlankRect.Union(aMinTextEditArea);
    tools::Rectangle aPixRect(rTargetDevice.LogicToPixel(aBlankRect));

    // in the tiled rendering case, the setup is incomplete, and we very
    // easily get an empty rRect on input - that will cause that everything is
    // clipped; happens in case of editing text inside a shape in Calc.
    // FIXME would be better to complete the setup so that we don't get an
    // empty rRect here
    if (!comphelper::LibreOfficeKit::isActive() || !rRect.IsEmpty())
        aBlankRect.Intersection(rRect);

    rOutlView.GetOutliner()->SetUpdateMode(true); // Bugfix #22596#
    rOutlView.Paint(aBlankRect, &rTargetDevice);

    if (!bModified)
    {
        pTextEditOutliner->ClearModifyFlag();
    }

    if (bTextFrame && !bFitToSize)
    {
        // completely reworked to use primitives; this ensures same look and functionality
        const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
        std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> xProcessor(
            drawinglayer::processor2d::createProcessor2DFromOutputDevice(rTargetDevice,
                                                                         aViewInformation2D));

        if (xProcessor)
        {
            const bool bMapModeEnabled(rTargetDevice.IsMapModeEnabled());
            const basegfx::B2DRange aRange = vcl::unotools::b2DRectangleFromRectangle(aPixRect);
            const SvtOptionsDrawinglayer aSvtOptionsDrawinglayer;
            const Color aHilightColor(aSvtOptionsDrawinglayer.getHilightColor());
            const double fTransparence(aSvtOptionsDrawinglayer.GetTransparentSelectionPercent()
                                       * 0.01);
            const sal_uInt16 nPixSiz(rOutlView.GetInvalidateMore() - 1);
            const drawinglayer::primitive2d::Primitive2DReference xReference(
                new drawinglayer::primitive2d::OverlayRectanglePrimitive(
                    aRange, aHilightColor.getBColor(), fTransparence,
                    std::max(6, nPixSiz - 2), // grow
                    0.0, // shrink
                    0.0));
            const drawinglayer::primitive2d::Primitive2DContainer aSequence{ xReference };

            rTargetDevice.EnableMapMode(false);
            xProcessor->process(aSequence);
            rTargetDevice.EnableMapMode(bMapModeEnabled);
        }
    }

    rOutlView.ShowCursor(/*bGotoCursor=*/true, /*bActivate=*/true);
}

void SdrObjEditView::ImpInvalidateOutlinerView(OutlinerView const& rOutlView) const
{
    vcl::Window* pWin = rOutlView.GetWindow();

    if (pWin)
    {
        const SdrTextObj* pText = GetTextEditObject();
        bool bTextFrame(pText && pText->IsTextFrame());
        bool bFitToSize(pText && pText->IsFitToSize());

        if (bTextFrame && !bFitToSize)
        {
            tools::Rectangle aBlankRect(rOutlView.GetOutputArea());
            aBlankRect.Union(aMinTextEditArea);
            tools::Rectangle aPixRect(pWin->LogicToPixel(aBlankRect));
            sal_uInt16 nPixSiz(rOutlView.GetInvalidateMore() - 1);

            aPixRect.AdjustLeft(-1);
            aPixRect.AdjustTop(-1);
            aPixRect.AdjustRight(1);
            aPixRect.AdjustBottom(1);

            {
                // limit xPixRect because of driver problems when pixel coordinates are too far out
                Size aMaxXY(pWin->GetOutputSizePixel());
                long a(2 * nPixSiz);
                long nMaxX(aMaxXY.Width() + a);
                long nMaxY(aMaxXY.Height() + a);

                if (aPixRect.Left() < -a)
                    aPixRect.SetLeft(-a);
                if (aPixRect.Top() < -a)
                    aPixRect.SetTop(-a);
                if (aPixRect.Right() > nMaxX)
                    aPixRect.SetRight(nMaxX);
                if (aPixRect.Bottom() > nMaxY)
                    aPixRect.SetBottom(nMaxY);
            }

            tools::Rectangle aOuterPix(aPixRect);
            aOuterPix.AdjustLeft(-nPixSiz);
            aOuterPix.AdjustTop(-nPixSiz);
            aOuterPix.AdjustRight(nPixSiz);
            aOuterPix.AdjustBottom(nPixSiz);

            bool bMapModeEnabled(pWin->IsMapModeEnabled());
            pWin->EnableMapMode(false);
            pWin->Invalidate(aOuterPix);
            pWin->EnableMapMode(bMapModeEnabled);
        }
    }
}

OutlinerView* SdrObjEditView::ImpMakeOutlinerView(vcl::Window* pWin, OutlinerView* pGivenView,
                                                  SfxViewShell* pViewShell) const
{
    // background
    Color aBackground(GetTextEditBackgroundColor(*this));
    SdrTextObj* pText = mxTextEditObj.get();
    bool bTextFrame = pText != nullptr && pText->IsTextFrame();
    bool bContourFrame = pText != nullptr && pText->IsContourTextFrame();
    // create OutlinerView
    OutlinerView* pOutlView = pGivenView;
    pTextEditOutliner->SetUpdateMode(false);

    if (pOutlView == nullptr)
    {
        pOutlView = new OutlinerView(pTextEditOutliner.get(), pWin);
    }
    else
    {
        pOutlView->SetWindow(pWin);
    }

    // disallow scrolling
    EVControlBits nStat = pOutlView->GetControlWord();
    nStat &= ~EVControlBits::AUTOSCROLL;
    // AutoViewSize only if not ContourFrame.
    if (!bContourFrame)
        nStat |= EVControlBits::AUTOSIZE;
    if (bTextFrame)
    {
        sal_uInt16 nPixSiz = maHdlList.GetHdlSize() * 2 + 1;
        nStat |= EVControlBits::INVONEMORE;
        pOutlView->SetInvalidateMore(nPixSiz);
    }
    pOutlView->SetControlWord(nStat);
    pOutlView->SetBackgroundColor(aBackground);

    // In case we're in the process of constructing a new view shell,
    // SfxViewShell::Current() may still point to the old one. So if possible,
    // depend on the application owning this draw view to provide the view
    // shell.
    SfxViewShell* pSfxViewShell = pViewShell ? pViewShell : GetSfxViewShell();
    pOutlView->RegisterViewShell(pSfxViewShell ? pSfxViewShell : SfxViewShell::Current());

    if (pText != nullptr)
    {
        pOutlView->SetAnchorMode(pText->GetOutlinerViewAnchorMode());
        pTextEditOutliner->SetFixedCellHeight(
            pText->GetMergedItem(SDRATTR_TEXT_USEFIXEDCELLHEIGHT).GetValue());
    }
    // do update before setting output area so that aTextEditArea can be recalculated
    pTextEditOutliner->SetUpdateMode(true);
    pOutlView->SetOutputArea(aTextEditArea);
    ImpInvalidateOutlinerView(*pOutlView);
    return pOutlView;
}

IMPL_LINK(SdrObjEditView, ImpOutlinerStatusEventHdl, EditStatus&, rEditStat, void)
{
    if (pTextEditOutliner)
    {
        SdrTextObj* pTextObj = mxTextEditObj.get();
        if (pTextObj)
        {
            pTextObj->onEditOutlinerStatusEvent(&rEditStat);
        }
    }
}

void SdrObjEditView::ImpChainingEventHdl()
{
    if (pTextEditOutliner)
    {
        SdrTextObj* pTextObj = mxTextEditObj.get();
        OutlinerView* pOLV = GetTextEditOutlinerView();
        if (pTextObj && pOLV)
        {
            TextChain* pTextChain = pTextObj->GetTextChain();

            // XXX: IsChainable and GetNilChainingEvent are a bit mixed up atm
            if (!pTextObj->IsChainable())
            {
                return;
            }
            // This is true during an underflow-caused overflow (with pEdtOutl->SetText())
            if (pTextChain->GetNilChainingEvent(pTextObj))
            {
                return;
            }

            // We prevent to trigger further handling of overflow/underflow for pTextObj
            pTextChain->SetNilChainingEvent(pTextObj, true); // XXX

            // Save previous selection pos // NOTE: It must be done to have the right CursorEvent in KeyInput
            pTextChain->SetPreChainingSel(pTextObj, pOLV->GetSelection());
            //maPreChainingSel = new ESelection(pOLV->GetSelection());

            // Handling Undo
            const int nText = 0; // XXX: hardcoded index (SdrTextObj::getText handles only 0)

            const bool bUndoEnabled = GetModel() && IsUndoEnabled();
            std::unique_ptr<SdrUndoObjSetText> pTxtUndo;
            if (bUndoEnabled)
                pTxtUndo.reset(
                    dynamic_cast<SdrUndoObjSetText*>(GetModel()
                                                         ->GetSdrUndoFactory()
                                                         .CreateUndoObjectSetText(*pTextObj, nText)
                                                         .release()));

            // trigger actual chaining
            pTextObj->onChainingEvent();

            if (pTxtUndo)
            {
                pTxtUndo->AfterSetText();
                if (!pTxtUndo->IsDifferent())
                {
                    pTxtUndo.reset();
                }
            }

            if (pTxtUndo)
                AddUndo(std::move(pTxtUndo));

            //maCursorEvent = new CursorChainingEvent(pTextChain->GetCursorEvent(pTextObj));
            //SdrTextObj *pNextLink = pTextObj->GetNextLinkInChain();

            // NOTE: Must be called. Don't let the function return if you set it to true and not reset it
            pTextChain->SetNilChainingEvent(pTextObj, false);
        }
        else
        {
            // XXX
            SAL_INFO("svx.chaining", "[OnChaining] No Edit Outliner View");
        }
    }
}

IMPL_LINK_NOARG(SdrObjEditView, ImpAfterCutOrPasteChainingEventHdl, LinkParamNone*, void)
{
    SdrTextObj* pTextObj = GetTextEditObject();
    if (!pTextObj)
        return;
    ImpChainingEventHdl();
    TextChainCursorManager aCursorManager(this, pTextObj);
    ImpMoveCursorAfterChainingEvent(&aCursorManager);
}

void SdrObjEditView::ImpMoveCursorAfterChainingEvent(TextChainCursorManager* pCursorManager)
{
    if (!mxTextEditObj.is() || !pCursorManager)
        return;

    SdrTextObj* pTextObj = mxTextEditObj.get();

    // Check if it has links to move it to
    if (!pTextObj || !pTextObj->IsChainable())
        return;

    TextChain* pTextChain = pTextObj->GetTextChain();
    ESelection aNewSel = pTextChain->GetPostChainingSel(pTextObj);

    pCursorManager->HandleCursorEventAfterChaining(pTextChain->GetCursorEvent(pTextObj), aNewSel);

    // Reset event
    pTextChain->SetCursorEvent(pTextObj, CursorChainingEvent::NULL_EVENT);
}

IMPL_LINK(SdrObjEditView, ImpOutlinerCalcFieldValueHdl, EditFieldInfo*, pFI, void)
{
    bool bOk = false;
    OUString& rStr = pFI->GetRepresentation();
    rStr.clear();
    SdrTextObj* pTextObj = mxTextEditObj.get();
    if (pTextObj != nullptr)
    {
        std::optional<Color> pTxtCol;
        std::optional<Color> pFldCol;
        bOk = pTextObj->CalcFieldValue(pFI->GetField(), pFI->GetPara(), pFI->GetPos(), true,
                                       pTxtCol, pFldCol, rStr);
        if (bOk)
        {
            if (pTxtCol)
            {
                pFI->SetTextColor(*pTxtCol);
            }
            if (pFldCol)
            {
                pFI->SetFieldColor(*pFldCol);
            }
            else
            {
                pFI->SetFieldColor(COL_LIGHTGRAY); // TODO: remove this later on (357)
            }
        }
    }
    Outliner& rDrawOutl = mpModel->GetDrawOutliner(pTextObj);
    Link<EditFieldInfo*, void> aDrawOutlLink = rDrawOutl.GetCalcFieldValueHdl();
    if (!bOk && aDrawOutlLink.IsSet())
    {
        aDrawOutlLink.Call(pFI);
        bOk = !rStr.isEmpty();
    }
    if (!bOk)
    {
        aOldCalcFieldValueLink.Call(pFI);
    }
}

IMPL_LINK_NOARG(SdrObjEditView, EndTextEditHdl, SdrUndoManager*, void) { SdrEndTextEdit(); }

SdrUndoManager* SdrObjEditView::getSdrUndoManagerForEnhancedTextEdit() const
{
    // default returns registered UndoManager
    return GetModel() ? dynamic_cast<SdrUndoManager*>(GetModel()->GetSdrUndoManager()) : nullptr;
}

bool SdrObjEditView::SdrBeginTextEdit(SdrObject* pObj_, SdrPageView* pPV, vcl::Window* pWin,
                                      bool bIsNewObj, SdrOutliner* pGivenOutliner,
                                      OutlinerView* pGivenOutlinerView, bool bDontDeleteOutliner,
                                      bool bOnlyOneView, bool bGrabFocus)
{
    // FIXME cannot be an assert() yet, the code is not ready for that;
    // eg. press F7 in Impress when you are inside a text object with spelling
    // mistakes => boom; and it is unclear how to avoid that
    SAL_WARN_IF(IsTextEdit(), "svx", "SdrBeginTextEdit called when IsTextEdit() is already true.");
    // FIXME this encourages all sorts of bad habits and should be removed
    SdrEndTextEdit();

    SdrTextObj* pObj = dynamic_cast<SdrTextObj*>(pObj_);
    if (!pObj)
        return false; // currently only possible with text objects

    if (bGrabFocus && pWin)
    {
        // attention, this call may cause an EndTextEdit() call to this view
        pWin->GrabFocus(); // to force the cursor into the edit view
    }

    bTextEditDontDelete = bDontDeleteOutliner && pGivenOutliner != nullptr;
    bTextEditOnlyOneView = bOnlyOneView;
    bTextEditNewObj = bIsNewObj;
    const sal_uInt32 nWinCount(PaintWindowCount());
    sal_uInt32 i;
    bool bBrk(false);

    if (!pWin)
    {
        for (i = 0; i < nWinCount && !pWin; i++)
        {
            SdrPaintWindow* pPaintWindow = GetPaintWindow(i);

            if (OUTDEV_WINDOW == pPaintWindow->GetOutputDevice().GetOutDevType())
            {
                pWin = static_cast<vcl::Window*>(&pPaintWindow->GetOutputDevice());
            }
        }

        // break, when no window exists
        if (!pWin)
        {
            bBrk = true;
        }
    }

    if (!bBrk && !pPV)
    {
        pPV = GetSdrPageView();

        // break, when no PageView for the object exists
        if (!pPV)
        {
            bBrk = true;
        }
    }

    // no TextEdit on objects in locked Layer
    if (pPV && pPV->GetLockedLayers().IsSet(pObj->GetLayer()))
    {
        bBrk = true;
    }

    if (pTextEditOutliner)
    {
        OSL_FAIL("SdrObjEditView::SdrBeginTextEdit(): Old Outliner still exists.");
        pTextEditOutliner.reset();
    }

    if (!bBrk)
    {
        pTextEditWin = pWin;
        pTextEditPV = pPV;
        mxTextEditObj.reset(pObj);
        if (pGivenOutliner)
        {
            pTextEditOutliner.reset(pGivenOutliner);
            pGivenOutliner = nullptr; // so we don't delete it on the error path
        }
        else
            pTextEditOutliner = SdrMakeOutliner(OutlinerMode::TextObject,
                                                mxTextEditObj->getSdrModelFromSdrObject());

        {
            SvtAccessibilityOptions aOptions;
            pTextEditOutliner->ForceAutoColor(aOptions.GetIsAutomaticFontColor());
        }

        aOldCalcFieldValueLink = pTextEditOutliner->GetCalcFieldValueHdl();
        // FieldHdl has to be set by SdrBeginTextEdit, because this call an UpdateFields
        pTextEditOutliner->SetCalcFieldValueHdl(
            LINK(this, SdrObjEditView, ImpOutlinerCalcFieldValueHdl));
        pTextEditOutliner->SetBeginPasteOrDropHdl(LINK(this, SdrObjEditView, BeginPasteOrDropHdl));
        pTextEditOutliner->SetEndPasteOrDropHdl(LINK(this, SdrObjEditView, EndPasteOrDropHdl));

        // It is just necessary to make the visualized page known. Set it.
        pTextEditOutliner->setVisualizedPage(pPV->GetPage());

        pTextEditOutliner->SetTextObjNoInit(mxTextEditObj.get());

        if (mxTextEditObj->BegTextEdit(*pTextEditOutliner))
        {
            SdrTextObj* pTextObj = mxTextEditObj.get();
            DBG_ASSERT(pTextObj, "svx::SdrObjEditView::BegTextEdit(), no text object?");
            if (!pTextObj)
                return false;

            // switch off any running TextAnimations
            pTextObj->SetTextAnimationAllowed(false);

            // remember old cursor
            if (pTextEditOutliner->GetViewCount() != 0)
            {
                pTextEditOutliner->RemoveView(static_cast<size_t>(0));
            }

            // Determine EditArea via TakeTextEditArea.
            // TODO: This could theoretically be left out, because TakeTextRect() calculates the aTextEditArea,
            // but aMinTextEditArea has to happen, too (therefore leaving this in right now)
            pTextObj->TakeTextEditArea(nullptr, nullptr, &aTextEditArea, &aMinTextEditArea);

            tools::Rectangle aTextRect;
            tools::Rectangle aAnchorRect;
            pTextObj->TakeTextRect(*pTextEditOutliner, aTextRect, true,
                                   &aAnchorRect /* Give true here, not false */);

            if (!pTextObj->IsContourTextFrame())
            {
                // FitToSize not together with ContourFrame, for now
                if (pTextObj->IsFitToSize())
                    aTextRect = aAnchorRect;
            }

            aTextEditArea = aTextRect;

            // add possible GridOffset to up-to-now view-independent EditAreas
            basegfx::B2DVector aGridOffset(0.0, 0.0);
            if (getPossibleGridOffsetForSdrObject(aGridOffset, pTextObj, pPV))
            {
                const Point aOffset(basegfx::fround(aGridOffset.getX()),
                                    basegfx::fround(aGridOffset.getY()));

                aTextEditArea += aOffset;
                aMinTextEditArea += aOffset;
            }

            Point aPvOfs(pTextObj->GetTextEditOffset());
            aTextEditArea.Move(aPvOfs.X(), aPvOfs.Y());
            aMinTextEditArea.Move(aPvOfs.X(), aPvOfs.Y());
            pTextEditCursorBuffer = pWin->GetCursor();

            maHdlList.SetMoveOutside(true);

            // Since IsMarkHdlWhenTextEdit() is ignored, it is necessary
            // to call AdjustMarkHdl() always.
            AdjustMarkHdl();

            pTextEditOutlinerView = ImpMakeOutlinerView(pWin, pGivenOutlinerView);

            if (!comphelper::LibreOfficeKit::isActive() && pTextEditOutlinerView)
            {
                // activate visualization of EditView on Overlay, suppress when
                // LibreOfficeKit is active
                pTextEditOutlinerView->GetEditView().setEditViewCallbacks(this);

                const SvtOptionsDrawinglayer aSvtOptionsDrawinglayer;
                const Color aHilightColor(aSvtOptionsDrawinglayer.getHilightColor());
                const SdrTextObj* pText = GetTextEditObject();
                const bool bTextFrame(pText && pText->IsTextFrame());
                const bool bFitToSize(pTextEditOutliner->GetControlWord()
                                      & EEControlBits::STRETCHING);
                const bool bVisualizeSurroundingFrame(bTextFrame && !bFitToSize);
                SdrPageView* pPageView = GetSdrPageView();

                if (pPageView)
                {
                    for (sal_uInt32 b(0); b < pPageView->PageWindowCount(); b++)
                    {
                        const SdrPageWindow& rPageWindow = *pPageView->GetPageWindow(b);

                        if (rPageWindow.GetPaintWindow().OutputToWindow())
                        {
                            const rtl::Reference<sdr::overlay::OverlayManager>& xManager
                                = rPageWindow.GetOverlayManager();
                            if (xManager.is())
                            {
                                std::unique_ptr<TextEditOverlayObject> pNewTextEditOverlayObject(
                                    new TextEditOverlayObject(aHilightColor, *pTextEditOutlinerView,
                                                              bVisualizeSurroundingFrame));

                                xManager->add(*pNewTextEditOverlayObject);
                                xManager->add(const_cast<sdr::overlay::OverlaySelection&>(
                                    *pNewTextEditOverlayObject->getOverlaySelection()));

                                maTEOverlayGroup.append(std::move(pNewTextEditOverlayObject));
                            }
                        }
                    }
                }
            }

            // check if this view is already inserted
            size_t i2, nCount = pTextEditOutliner->GetViewCount();
            for (i2 = 0; i2 < nCount; i2++)
            {
                if (pTextEditOutliner->GetView(i2) == pTextEditOutlinerView)
                    break;
            }

            if (i2 == nCount)
                pTextEditOutliner->InsertView(pTextEditOutlinerView, 0);

            maHdlList.SetMoveOutside(false);
            maHdlList.SetMoveOutside(true);

            // register all windows as OutlinerViews with the Outliner
            if (!bOnlyOneView)
            {
                for (i = 0; i < nWinCount; i++)
                {
                    SdrPaintWindow* pPaintWindow = GetPaintWindow(i);
                    OutputDevice& rOutDev = pPaintWindow->GetOutputDevice();

                    if (&rOutDev != pWin && OUTDEV_WINDOW == rOutDev.GetOutDevType())
                    {
                        OutlinerView* pOutlView
                            = ImpMakeOutlinerView(static_cast<vcl::Window*>(&rOutDev), nullptr);
                        pTextEditOutliner->InsertView(pOutlView, static_cast<sal_uInt16>(i));
                    }
                }

                if (comphelper::LibreOfficeKit::isActive())
                {
                    // Register an outliner view for all other sdr views that
                    // show the same page, so that when the text edit changes,
                    // all interested windows get an invalidation.
                    SdrViewIter aIter(pObj->getSdrPageFromSdrObject());
                    for (SdrView* pView = aIter.FirstView(); pView; pView = aIter.NextView())
                    {
                        if (pView == this)
                            continue;

                        for (sal_uInt32 nViewPaintWindow = 0;
                             nViewPaintWindow < pView->PaintWindowCount(); ++nViewPaintWindow)
                        {
                            SdrPaintWindow* pPaintWindow = pView->GetPaintWindow(nViewPaintWindow);
                            OutputDevice& rOutDev = pPaintWindow->GetOutputDevice();

                            if (&rOutDev != pWin && OUTDEV_WINDOW == rOutDev.GetOutDevType())
                            {
                                OutlinerView* pOutlView = ImpMakeOutlinerView(
                                    static_cast<vcl::Window*>(&rOutDev), nullptr);
                                pOutlView->HideCursor();
                                static_cast<vcl::Window*>(&rOutDev)->SetCursor(nullptr);
                                pTextEditOutliner->InsertView(pOutlView);
                            }
                        }
                    }
                }
            }

            pTextEditOutlinerView->ShowCursor();
            pTextEditOutliner->SetStatusEventHdl(
                LINK(this, SdrObjEditView, ImpOutlinerStatusEventHdl));
            if (pTextObj->IsChainable())
            {
                pTextEditOutlinerView->SetEndCutPasteLinkHdl(
                    LINK(this, SdrObjEditView, ImpAfterCutOrPasteChainingEventHdl));
            }

            pTextEditOutliner->ClearModifyFlag();

            if (pTextObj->IsFitToSize())
            {
                pWin->Invalidate(aTextEditArea);
            }

            if (GetModel())
            {
                SdrHint aHint(SdrHintKind::BeginEdit, *pTextObj);
                GetModel()->Broadcast(aHint);
            }

            pTextEditOutliner->setVisualizedPage(nullptr);

            if (mxSelectionController.is())
                mxSelectionController->onSelectionHasChanged();

            if (GetModel() && IsUndoEnabled()
                && !GetModel()->GetDisableTextEditUsesCommonUndoManager())
            {
                SdrUndoManager* pSdrUndoManager = getSdrUndoManagerForEnhancedTextEdit();

                if (pSdrUndoManager)
                {
                    // we have an outliner, undo manager and it's an EditUndoManager, exchange
                    // the document undo manager and the default one from the outliner and tell
                    // it that text edit starts by setting a callback if it needs to end text edit mode.
                    assert(nullptr == mpOldTextEditUndoManager);

                    mpOldTextEditUndoManager = pTextEditOutliner->SetUndoManager(pSdrUndoManager);
                    pSdrUndoManager->SetEndTextEditHdl(LINK(this, SdrObjEditView, EndTextEditHdl));
                }
                else
                {
                    OSL_ENSURE(false,
                               "The document undo manager is not derived from SdrUndoManager (!)");
                }
            }

            return true; // ran fine, let TextEdit run now
        }
        else
        {
            pTextEditOutliner->SetCalcFieldValueHdl(aOldCalcFieldValueLink);
            pTextEditOutliner->SetBeginPasteOrDropHdl(Link<PasteOrDropInfos*, void>());
            pTextEditOutliner->SetEndPasteOrDropHdl(Link<PasteOrDropInfos*, void>());
        }
    }
    if (pTextEditOutliner != nullptr)
    {
        pTextEditOutliner->setVisualizedPage(nullptr);
    }

    // something went wrong...
    if (!bDontDeleteOutliner)
    {
        delete pGivenOutliner;
        if (pGivenOutlinerView != nullptr)
        {
            delete pGivenOutlinerView;
            pGivenOutlinerView = nullptr;
        }
    }
    pTextEditOutliner.reset();

    pTextEditOutlinerView = nullptr;
    mxTextEditObj.reset(nullptr);
    pTextEditPV = nullptr;
    pTextEditWin = nullptr;
    maHdlList.SetMoveOutside(false);

    return false;
}

SdrEndTextEditKind SdrObjEditView::SdrEndTextEdit(bool bDontDeleteReally)
{
    SdrEndTextEditKind eRet = SdrEndTextEditKind::Unchanged;
    SdrTextObj* pTEObj = mxTextEditObj.get();
    vcl::Window* pTEWin = pTextEditWin;
    OutlinerView* pTEOutlinerView = pTextEditOutlinerView;
    vcl::Cursor* pTECursorBuffer = pTextEditCursorBuffer;
    SdrUndoManager* pUndoEditUndoManager = nullptr;
    bool bNeedToUndoSavedRedoTextEdit(false);

    if (GetModel() && IsUndoEnabled() && pTEObj && pTextEditOutliner
        && !GetModel()->GetDisableTextEditUsesCommonUndoManager())
    {
        // change back the UndoManager to the remembered original one
        SfxUndoManager* pOriginal = pTextEditOutliner->SetUndoManager(mpOldTextEditUndoManager);
        mpOldTextEditUndoManager = nullptr;

        if (pOriginal)
        {
            // check if we got back our document undo manager
            SdrUndoManager* pSdrUndoManager = getSdrUndoManagerForEnhancedTextEdit();

            if (pSdrUndoManager && dynamic_cast<SdrUndoManager*>(pOriginal) == pSdrUndoManager)
            {
                if (pSdrUndoManager->isEndTextEditTriggeredFromUndo())
                {
                    // remember the UndoManager where missing Undos have to be triggered after end
                    // text edit. When the undo had triggered the end text edit, the original action
                    // which had to be undone originally is not yet undone.
                    pUndoEditUndoManager = pSdrUndoManager;

                    // We are ending text edit; if text edit was triggered from undo, execute all redos
                    // to create a complete text change undo action for the redo buffer. Also mark this
                    // state when at least one redo was executed; the created extra TextChange needs to
                    // be undone in addition to the first real undo outside the text edit changes
                    while (pSdrUndoManager->GetRedoActionCount())
                    {
                        bNeedToUndoSavedRedoTextEdit = true;
                        pSdrUndoManager->Redo();
                    }
                }

                // reset the callback link and let the undo manager cleanup all text edit
                // undo actions to get the stack back to the form before the text edit
                pSdrUndoManager->SetEndTextEditHdl(Link<SdrUndoManager*, void>());
            }
            else
            {
                OSL_ENSURE(false, "Got UndoManager back in SdrEndTextEdit which is NOT the "
                                  "expected document UndoManager (!)");
                delete pOriginal;
            }
        }
    }
    else
    {
        assert(nullptr == mpOldTextEditUndoManager); // cannot be restored!
    }

    if (GetModel() && mxTextEditObj.is())
    {
        SdrHint aHint(SdrHintKind::EndEdit, *mxTextEditObj);
        GetModel()->Broadcast(aHint);
    }

    // if new mechanism was used, clean it up. At cleanup no need to check
    // for LibreOfficeKit
    if (pTextEditOutlinerView)
    {
        pTextEditOutlinerView->GetEditView().setEditViewCallbacks(nullptr);
        maTEOverlayGroup.clear();
    }

    mxTextEditObj.reset(nullptr);
    pTextEditPV = nullptr;
    pTextEditWin = nullptr;
    SdrOutliner* pTEOutliner = pTextEditOutliner.release();
    pTextEditOutlinerView = nullptr;
    pTextEditCursorBuffer = nullptr;
    aTextEditArea = tools::Rectangle();

    if (pTEOutliner != nullptr)
    {
        bool bModified = pTEOutliner->IsModified();
        if (pTEOutlinerView != nullptr)
        {
            pTEOutlinerView->HideCursor();
        }
        if (pTEObj != nullptr)
        {
            pTEOutliner->CompleteOnlineSpelling();

            std::unique_ptr<SdrUndoObjSetText> pTxtUndo;

            if (bModified)
            {
                sal_Int32 nText;
                for (nText = 0; nText < pTEObj->getTextCount(); ++nText)
                    if (pTEObj->getText(nText) == pTEObj->getActiveText())
                        break;

                pTxtUndo.reset(
                    dynamic_cast<SdrUndoObjSetText*>(GetModel()
                                                         ->GetSdrUndoFactory()
                                                         .CreateUndoObjectSetText(*pTEObj, nText)
                                                         .release()));
            }
            DBG_ASSERT(!bModified || pTxtUndo,
                       "svx::SdrObjEditView::EndTextEdit(), could not create undo action!");
            // Set old CalcFieldValue-Handler again, this
            // has to happen before Obj::EndTextEdit(), as this does UpdateFields().
            pTEOutliner->SetCalcFieldValueHdl(aOldCalcFieldValueLink);
            pTEOutliner->SetBeginPasteOrDropHdl(Link<PasteOrDropInfos*, void>());
            pTEOutliner->SetEndPasteOrDropHdl(Link<PasteOrDropInfos*, void>());

            const bool bUndo = IsUndoEnabled();
            if (bUndo)
            {
                EndTextEditAllViews();
                OUString aObjName(pTEObj->TakeObjNameSingul());
                BegUndo(SvxResId(STR_UndoObjSetText), aObjName);
            }

            pTEObj->EndTextEdit(*pTEOutliner);

            if ((pTEObj->GetRotateAngle() != 0)
                || (dynamic_cast<const SdrTextObj*>(pTEObj) != nullptr && pTEObj->IsFontwork()))
            {
                pTEObj->ActionChanged();
            }

            if (pTxtUndo != nullptr)
            {
                pTxtUndo->AfterSetText();
                if (!pTxtUndo->IsDifferent())
                {
                    pTxtUndo.reset();
                }
            }
            // check deletion of entire TextObj
            std::unique_ptr<SdrUndoAction> pDelUndo;
            bool bDelObj = false;
            if (bTextEditNewObj)
            {
                bDelObj = pTEObj->IsTextFrame() && !pTEObj->HasText() && !pTEObj->IsEmptyPresObj()
                          && !pTEObj->HasFill() && !pTEObj->HasLine();

                if (pTEObj->IsInserted() && bDelObj
                    && pTEObj->GetObjInventor() == SdrInventor::Default && !bDontDeleteReally)
                {
                    SdrObjKind eIdent = static_cast<SdrObjKind>(pTEObj->GetObjIdentifier());
                    if (eIdent == OBJ_TEXT)
                    {
                        pDelUndo = GetModel()->GetSdrUndoFactory().CreateUndoDeleteObject(*pTEObj);
                    }
                }
            }
            if (pTxtUndo)
            {
                if (bUndo)
                    AddUndo(std::move(pTxtUndo));
                eRet = SdrEndTextEditKind::Changed;
            }
            if (pDelUndo != nullptr)
            {
                if (bUndo)
                {
                    AddUndo(std::move(pDelUndo));
                }
                eRet = SdrEndTextEditKind::Deleted;
                DBG_ASSERT(pTEObj->getParentSdrObjListFromSdrObject() != nullptr,
                           "SdrObjEditView::SdrEndTextEdit(): Fatal: Object edited doesn't have an "
                           "ObjList!");
                if (pTEObj->getParentSdrObjListFromSdrObject() != nullptr)
                {
                    pTEObj->getParentSdrObjListFromSdrObject()->RemoveObject(pTEObj->GetOrdNum());
                    CheckMarked(); // remove selection immediately...
                }
            }
            else if (bDelObj)
            { // for Writer: the app has to do the deletion itself.
                eRet = SdrEndTextEditKind::ShouldBeDeleted;
            }

            if (bUndo)
                EndUndo(); // EndUndo after Remove, in case UndoStack is deleted immediately

            // Switch on any TextAnimation again after TextEdit
            if (dynamic_cast<const SdrTextObj*>(pTEObj) != nullptr)
            {
                pTEObj->SetTextAnimationAllowed(true);
            }

            // Since IsMarkHdlWhenTextEdit() is ignored, it is necessary
            // to call AdjustMarkHdl() always.
            AdjustMarkHdl();
        }
        // delete all OutlinerViews
        for (size_t i = pTEOutliner->GetViewCount(); i > 0;)
        {
            i--;
            OutlinerView* pOLV = pTEOutliner->GetView(i);
            sal_uInt16 nMorePix = pOLV->GetInvalidateMore() + 10;
            vcl::Window* pWin = pOLV->GetWindow();
            tools::Rectangle aRect(pOLV->GetOutputArea());
            pTEOutliner->RemoveView(i);
            if (!bTextEditDontDelete || i != 0)
            {
                // may not own the zeroth one
                delete pOLV;
            }
            aRect.Union(aTextEditArea);
            aRect.Union(aMinTextEditArea);
            aRect = pWin->LogicToPixel(aRect);
            aRect.AdjustLeft(-nMorePix);
            aRect.AdjustTop(-nMorePix);
            aRect.AdjustRight(nMorePix);
            aRect.AdjustBottom(nMorePix);
            aRect = pWin->PixelToLogic(aRect);
            InvalidateOneWin(*pWin, aRect);
            pWin->SetFillColor();
            pWin->SetLineColor(COL_BLACK);
        }
        // and now the Outliner itself
        if (!bTextEditDontDelete)
            delete pTEOutliner;
        else
            pTEOutliner->Clear();
        if (pTEWin != nullptr)
        {
            pTEWin->SetCursor(pTECursorBuffer);
        }
        maHdlList.SetMoveOutside(false);
        if (eRet != SdrEndTextEditKind::Unchanged)
        {
            GetMarkedObjectListWriteAccess().SetNameDirty();
        }
    }

    if (pTEObj && !pTEObj->getSdrModelFromSdrObject().isLocked() && pTEObj->GetBroadcaster())
    {
        SdrHint aHint(SdrHintKind::EndEdit, *pTEObj);
        const_cast<SfxBroadcaster*>(pTEObj->GetBroadcaster())->Broadcast(aHint);
    }

    if (pUndoEditUndoManager)
    {
        if (bNeedToUndoSavedRedoTextEdit)
        {
            // undo the text edit action since it was created as part of an EndTextEdit
            // callback from undo itself. This needs to be done after the call to
            // FmFormView::SdrEndTextEdit since it gets created there
            pUndoEditUndoManager->Undo();
        }

        // trigger the Undo which was not executed, but lead to this
        // end text edit
        pUndoEditUndoManager->Undo();
    }

    return eRet;
}

// info about TextEdit. Default is false.
bool SdrObjEditView::IsTextEdit() const { return mxTextEditObj.is(); }

// info about TextEditPageView. Default is 0L.
SdrPageView* SdrObjEditView::GetTextEditPageView() const { return pTextEditPV; }

OutlinerView* SdrObjEditView::ImpFindOutlinerView(vcl::Window const* pWin) const
{
    if (pWin == nullptr)
        return nullptr;
    if (pTextEditOutliner == nullptr)
        return nullptr;
    OutlinerView* pNewView = nullptr;
    size_t nWinCount = pTextEditOutliner->GetViewCount();
    for (size_t i = 0; i < nWinCount && pNewView == nullptr; i++)
    {
        OutlinerView* pView = pTextEditOutliner->GetView(i);
        if (pView->GetWindow() == pWin)
            pNewView = pView;
    }
    return pNewView;
}

void SdrObjEditView::SetTextEditWin(vcl::Window* pWin)
{
    if (mxTextEditObj.is() && pWin != nullptr && pWin != pTextEditWin)
    {
        OutlinerView* pNewView = ImpFindOutlinerView(pWin);
        if (pNewView != nullptr && pNewView != pTextEditOutlinerView)
        {
            if (pTextEditOutlinerView != nullptr)
            {
                pTextEditOutlinerView->HideCursor();
            }
            pTextEditOutlinerView = pNewView;
            pTextEditWin = pWin;
            pWin->GrabFocus(); // Make the cursor blink here as well
            pNewView->ShowCursor();
            ImpMakeTextCursorAreaVisible();
        }
    }
}

bool SdrObjEditView::IsTextEditHit(const Point& rHit) const
{
    bool bOk = false;
    if (mxTextEditObj.is())
    {
        tools::Rectangle aEditArea;
        OutlinerView* pOLV = pTextEditOutliner->GetView(0);
        if (pOLV != nullptr)
        {
            aEditArea.Union(pOLV->GetOutputArea());
        }
        bOk = aEditArea.IsInside(rHit);
        if (bOk)
        { // check if any characters were actually hit
            Point aPnt(rHit);
            aPnt -= aEditArea.TopLeft();
            long nHitTol = 2000;
            OutputDevice* pRef = pTextEditOutliner->GetRefDevice();
            if (pRef)
                nHitTol = OutputDevice::LogicToLogic(nHitTol, MapUnit::Map100thMM,
                                                     pRef->GetMapMode().GetMapUnit());

            bOk = pTextEditOutliner->IsTextPos(aPnt, static_cast<sal_uInt16>(nHitTol));
        }
    }
    return bOk;
}

bool SdrObjEditView::IsTextEditFrameHit(const Point& rHit) const
{
    bool bOk = false;
    if (mxTextEditObj.is())
    {
        SdrTextObj* pText = mxTextEditObj.get();
        OutlinerView* pOLV = pTextEditOutliner->GetView(0);
        if (pOLV)
        {
            vcl::Window* pWin = pOLV->GetWindow();
            if (pText != nullptr && pText->IsTextFrame() && pWin != nullptr)
            {
                sal_uInt16 nPixSiz = pOLV->GetInvalidateMore();
                tools::Rectangle aEditArea(aMinTextEditArea);
                aEditArea.Union(pOLV->GetOutputArea());
                if (!aEditArea.IsInside(rHit))
                {
                    Size aSiz(pWin->PixelToLogic(Size(nPixSiz, nPixSiz)));
                    aEditArea.AdjustLeft(-(aSiz.Width()));
                    aEditArea.AdjustTop(-(aSiz.Height()));
                    aEditArea.AdjustRight(aSiz.Width());
                    aEditArea.AdjustBottom(aSiz.Height());
                    bOk = aEditArea.IsInside(rHit);
                }
            }
        }
    }
    return bOk;
}

TextChainCursorManager* SdrObjEditView::ImpHandleMotionThroughBoxesKeyInput(const KeyEvent& rKEvt,
                                                                            bool* bOutHandled)
{
    *bOutHandled = false;

    SdrTextObj* pTextObj = mxTextEditObj.get();
    if (!pTextObj)
        return nullptr;

    if (!pTextObj->GetNextLinkInChain() && !pTextObj->GetPrevLinkInChain())
        return nullptr;

    TextChainCursorManager* pCursorManager = new TextChainCursorManager(this, pTextObj);
    if (pCursorManager->HandleKeyEvent(rKEvt))
    {
        // Possibly do other stuff here if necessary...
        // XXX: Careful with the checks below (in KeyInput) for pWin and co. You should do them here I guess.
        *bOutHandled = true;
    }

    return pCursorManager;
}

bool SdrObjEditView::KeyInput(const KeyEvent& rKEvt, vcl::Window* pWin)
{
    if (pTextEditOutlinerView)
    {
        /* Start special handling of keys within a chain */
        // We possibly move to another box before any handling
        bool bHandled = false;
        std::unique_ptr<TextChainCursorManager> xCursorManager(
            ImpHandleMotionThroughBoxesKeyInput(rKEvt, &bHandled));
        if (bHandled)
            return true;
        /* End special handling of keys within a chain */

        if (pTextEditOutlinerView->PostKeyEvent(rKEvt, pWin))
        {
            if (mpModel)
            {
                if (pTextEditOutliner && pTextEditOutliner->IsModified())
                    mpModel->SetChanged();
            }

            /* Start chaining processing */
            ImpChainingEventHdl();
            ImpMoveCursorAfterChainingEvent(xCursorManager.get());
            /* End chaining processing */

            if (pWin != nullptr && pWin != pTextEditWin)
                SetTextEditWin(pWin);
            ImpMakeTextCursorAreaVisible();
            return true;
        }
    }
    return SdrGlueEditView::KeyInput(rKEvt, pWin);
}

bool SdrObjEditView::MouseButtonDown(const MouseEvent& rMEvt, OutputDevice* pWin)
{
    if (pTextEditOutlinerView != nullptr)
    {
        bool bPostIt = pTextEditOutliner->IsInSelectionMode();
        if (!bPostIt)
        {
            Point aPt(rMEvt.GetPosPixel());
            if (pWin != nullptr)
                aPt = pWin->PixelToLogic(aPt);
            else if (pTextEditWin != nullptr)
                aPt = pTextEditWin->PixelToLogic(aPt);
            bPostIt = IsTextEditHit(aPt);
        }
        if (bPostIt)
        {
            Point aPixPos(rMEvt.GetPosPixel());
            if (pWin)
            {
                tools::Rectangle aR(pWin->LogicToPixel(pTextEditOutlinerView->GetOutputArea()));
                if (aPixPos.X() < aR.Left())
                    aPixPos.setX(aR.Left());
                if (aPixPos.X() > aR.Right())
                    aPixPos.setX(aR.Right());
                if (aPixPos.Y() < aR.Top())
                    aPixPos.setY(aR.Top());
                if (aPixPos.Y() > aR.Bottom())
                    aPixPos.setY(aR.Bottom());
            }
            MouseEvent aMEvt(aPixPos, rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(),
                             rMEvt.GetModifier());
            if (pTextEditOutlinerView->MouseButtonDown(aMEvt))
            {
                if (pWin != nullptr && pWin != pTextEditWin
                    && pWin->GetOutDevType() == OUTDEV_WINDOW)
                    SetTextEditWin(static_cast<vcl::Window*>(pWin));
                ImpMakeTextCursorAreaVisible();
                return true;
            }
        }
    }
    return SdrGlueEditView::MouseButtonDown(rMEvt, pWin);
}

bool SdrObjEditView::MouseButtonUp(const MouseEvent& rMEvt, OutputDevice* pWin)
{
    if (pTextEditOutlinerView != nullptr)
    {
        bool bPostIt = pTextEditOutliner->IsInSelectionMode();
        if (!bPostIt)
        {
            Point aPt(rMEvt.GetPosPixel());
            if (pWin != nullptr)
                aPt = pWin->PixelToLogic(aPt);
            else if (pTextEditWin != nullptr)
                aPt = pTextEditWin->PixelToLogic(aPt);
            bPostIt = IsTextEditHit(aPt);
        }
        if (bPostIt && pWin)
        {
            Point aPixPos(rMEvt.GetPosPixel());
            tools::Rectangle aR(pWin->LogicToPixel(pTextEditOutlinerView->GetOutputArea()));
            if (aPixPos.X() < aR.Left())
                aPixPos.setX(aR.Left());
            if (aPixPos.X() > aR.Right())
                aPixPos.setX(aR.Right());
            if (aPixPos.Y() < aR.Top())
                aPixPos.setY(aR.Top());
            if (aPixPos.Y() > aR.Bottom())
                aPixPos.setY(aR.Bottom());
            MouseEvent aMEvt(aPixPos, rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(),
                             rMEvt.GetModifier());
            if (pTextEditOutlinerView->MouseButtonUp(aMEvt))
            {
                ImpMakeTextCursorAreaVisible();
                return true;
            }
        }
    }
    return SdrGlueEditView::MouseButtonUp(rMEvt, pWin);
}

bool SdrObjEditView::MouseMove(const MouseEvent& rMEvt, OutputDevice* pWin)
{
    if (pTextEditOutlinerView != nullptr)
    {
        bool bSelMode = pTextEditOutliner->IsInSelectionMode();
        bool bPostIt = bSelMode;
        if (!bPostIt)
        {
            Point aPt(rMEvt.GetPosPixel());
            if (pWin)
                aPt = pWin->PixelToLogic(aPt);
            else if (pTextEditWin)
                aPt = pTextEditWin->PixelToLogic(aPt);
            bPostIt = IsTextEditHit(aPt);
        }
        if (bPostIt)
        {
            Point aPixPos(rMEvt.GetPosPixel());
            tools::Rectangle aR(pTextEditOutlinerView->GetOutputArea());
            if (pWin)
                aR = pWin->LogicToPixel(aR);
            else if (pTextEditWin)
                aR = pTextEditWin->LogicToPixel(aR);
            if (aPixPos.X() < aR.Left())
                aPixPos.setX(aR.Left());
            if (aPixPos.X() > aR.Right())
                aPixPos.setX(aR.Right());
            if (aPixPos.Y() < aR.Top())
                aPixPos.setY(aR.Top());
            if (aPixPos.Y() > aR.Bottom())
                aPixPos.setY(aR.Bottom());
            MouseEvent aMEvt(aPixPos, rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(),
                             rMEvt.GetModifier());
            if (pTextEditOutlinerView->MouseMove(aMEvt) && bSelMode)
            {
                ImpMakeTextCursorAreaVisible();
                return true;
            }
        }
    }
    return SdrGlueEditView::MouseMove(rMEvt, pWin);
}

bool SdrObjEditView::Command(const CommandEvent& rCEvt, vcl::Window* pWin)
{
    // as long as OutlinerView returns a sal_Bool, it only gets CommandEventId::StartDrag
    if (pTextEditOutlinerView != nullptr)
    {
        if (rCEvt.GetCommand() == CommandEventId::StartDrag)
        {
            bool bPostIt = pTextEditOutliner->IsInSelectionMode() || !rCEvt.IsMouseEvent();
            if (!bPostIt && rCEvt.IsMouseEvent())
            {
                Point aPt(rCEvt.GetMousePosPixel());
                if (pWin != nullptr)
                    aPt = pWin->PixelToLogic(aPt);
                else if (pTextEditWin != nullptr)
                    aPt = pTextEditWin->PixelToLogic(aPt);
                bPostIt = IsTextEditHit(aPt);
            }
            if (bPostIt)
            {
                Point aPixPos(rCEvt.GetMousePosPixel());
                if (rCEvt.IsMouseEvent() && pWin)
                {
                    tools::Rectangle aR(pWin->LogicToPixel(pTextEditOutlinerView->GetOutputArea()));
                    if (aPixPos.X() < aR.Left())
                        aPixPos.setX(aR.Left());
                    if (aPixPos.X() > aR.Right())
                        aPixPos.setX(aR.Right());
                    if (aPixPos.Y() < aR.Top())
                        aPixPos.setY(aR.Top());
                    if (aPixPos.Y() > aR.Bottom())
                        aPixPos.setY(aR.Bottom());
                }
                CommandEvent aCEvt(aPixPos, rCEvt.GetCommand(), rCEvt.IsMouseEvent());
                // Command is void at the OutlinerView, sadly
                pTextEditOutlinerView->Command(aCEvt);
                if (pWin != nullptr && pWin != pTextEditWin)
                    SetTextEditWin(pWin);
                ImpMakeTextCursorAreaVisible();
                return true;
            }
        }
        else
        {
            pTextEditOutlinerView->Command(rCEvt);
            return true;
        }
    }
    return SdrGlueEditView::Command(rCEvt, pWin);
}

bool SdrObjEditView::ImpIsTextEditAllSelected() const
{
    bool bRet = false;
    if (pTextEditOutliner != nullptr && pTextEditOutlinerView != nullptr)
    {
        if (SdrTextObj::HasTextImpl(pTextEditOutliner.get()))
        {
            const sal_Int32 nParaCnt = pTextEditOutliner->GetParagraphCount();
            Paragraph* pLastPara = pTextEditOutliner->GetParagraph(nParaCnt > 1 ? nParaCnt - 1 : 0);

            ESelection aESel(pTextEditOutlinerView->GetSelection());
            if (aESel.nStartPara == 0 && aESel.nStartPos == 0 && aESel.nEndPara == (nParaCnt - 1))
            {
                if (pTextEditOutliner->GetText(pLastPara).getLength() == aESel.nEndPos)
                    bRet = true;
            }
            // in case the selection was done backwards
            if (!bRet && aESel.nEndPara == 0 && aESel.nEndPos == 0
                && aESel.nStartPara == (nParaCnt - 1))
            {
                if (pTextEditOutliner->GetText(pLastPara).getLength() == aESel.nStartPos)
                    bRet = true;
            }
        }
        else
        {
            bRet = true;
        }
    }
    return bRet;
}

void SdrObjEditView::ImpMakeTextCursorAreaVisible()
{
    if (pTextEditOutlinerView != nullptr && pTextEditWin != nullptr)
    {
        vcl::Cursor* pCsr = pTextEditWin->GetCursor();
        if (pCsr != nullptr)
        {
            Size aSiz(pCsr->GetSize());
            if (!aSiz.IsEmpty())
            {
                MakeVisible(tools::Rectangle(pCsr->GetPos(), aSiz), *pTextEditWin);
            }
        }
    }
}

SvtScriptType SdrObjEditView::GetScriptType() const
{
    SvtScriptType nScriptType = SvtScriptType::NONE;

    if (IsTextEdit())
    {
        if (mxTextEditObj->GetOutlinerParaObject())
            nScriptType = mxTextEditObj->GetOutlinerParaObject()->GetTextObject().GetScriptType();

        if (pTextEditOutlinerView)
            nScriptType = pTextEditOutlinerView->GetSelectedScriptType();
    }
    else
    {
        const size_t nMarkCount(GetMarkedObjectCount());

        for (size_t i = 0; i < nMarkCount; ++i)
        {
            OutlinerParaObject* pParaObj = GetMarkedObjectByIndex(i)->GetOutlinerParaObject();

            if (pParaObj)
            {
                nScriptType |= pParaObj->GetTextObject().GetScriptType();
            }
        }
    }

    if (nScriptType == SvtScriptType::NONE)
        nScriptType = SvtScriptType::LATIN;

    return nScriptType;
}

void SdrObjEditView::GetAttributes(SfxItemSet& rTargetSet, bool bOnlyHardAttr) const
{
    if (mxSelectionController.is())
        if (mxSelectionController->GetAttributes(rTargetSet, bOnlyHardAttr))
            return;

    if (IsTextEdit())
    {
        DBG_ASSERT(pTextEditOutlinerView != nullptr,
                   "SdrObjEditView::GetAttributes(): pTextEditOutlinerView=NULL");
        DBG_ASSERT(pTextEditOutliner != nullptr,
                   "SdrObjEditView::GetAttributes(): pTextEditOutliner=NULL");

        // take care of bOnlyHardAttr(!)
        if (!bOnlyHardAttr && mxTextEditObj->GetStyleSheet())
            rTargetSet.Put(mxTextEditObj->GetStyleSheet()->GetItemSet());

        // add object attributes
        rTargetSet.Put(mxTextEditObj->GetMergedItemSet());

        if (pTextEditOutlinerView)
        {
            // FALSE= regard InvalidItems as "holes," not as Default
            rTargetSet.Put(pTextEditOutlinerView->GetAttribs(), false);
        }

        if (GetMarkedObjectCount() == 1 && GetMarkedObjectByIndex(0) == mxTextEditObj.get())
        {
            MergeNotPersistAttrFromMarked(rTargetSet);
        }
    }
    else
    {
        SdrGlueEditView::GetAttributes(rTargetSet, bOnlyHardAttr);
    }
}

bool SdrObjEditView::SetAttributes(const SfxItemSet& rSet, bool bReplaceAll)
{
    bool bRet = false;
    bool bTextEdit = pTextEditOutlinerView != nullptr && mxTextEditObj.is();
    bool bAllTextSelected = ImpIsTextEditAllSelected();
    const SfxItemSet* pSet = &rSet;

    if (!bTextEdit)
    {
        // no TextEdit active -> all Items to drawing object
        if (mxSelectionController.is())
            bRet = mxSelectionController->SetAttributes(*pSet, bReplaceAll);

        if (!bRet)
        {
            SdrGlueEditView::SetAttributes(*pSet, bReplaceAll);
            bRet = true;
        }
    }
    else
    {
#ifdef DBG_UTIL
        {
            bool bHasEEFeatureItems = false;
            SfxItemIter aIter(rSet);
            for (const SfxPoolItem* pItem = aIter.GetCurItem(); !bHasEEFeatureItems && pItem;
                 pItem = aIter.NextItem())
            {
                if (!IsInvalidItem(pItem))
                {
                    sal_uInt16 nW = pItem->Which();
                    if (nW >= EE_FEATURE_START && nW <= EE_FEATURE_END)
                        bHasEEFeatureItems = true;
                }
            }

            if (bHasEEFeatureItems)
            {
                std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(
                    nullptr, VclMessageType::Info, VclButtonsType::Ok,
                    "SdrObjEditView::SetAttributes(): Setting EE_FEATURE items "
                    "at the SdrView does not make sense! It only leads to "
                    "overhead and unreadable documents."));
                xInfoBox->run();
            }
        }
#endif

        bool bOnlyEEItems;
        bool bNoEEItems = !SearchOutlinerItems(*pSet, bReplaceAll, &bOnlyEEItems);
        // everything selected? -> attributes to the border, too
        // if no EEItems, attributes to the border only
        if (bAllTextSelected || bNoEEItems)
        {
            if (mxSelectionController.is())
                bRet = mxSelectionController->SetAttributes(*pSet, bReplaceAll);

            if (!bRet)
            {
                const bool bUndo = IsUndoEnabled();

                if (bUndo)
                {
                    BegUndo(ImpGetDescriptionString(STR_EditSetAttributes));
                    AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*mxTextEditObj));

                    // If this is a text object also rescue the OutlinerParaObject since
                    // applying attributes to the object may change text layout when
                    // multiple portions exist with multiple formats. If an OutlinerParaObject
                    // really exists and needs to be rescued is evaluated in the undo
                    // implementation itself.
                    bool bRescueText = mxTextEditObj;

                    AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoAttrObject(
                        *mxTextEditObj, false, !bNoEEItems || bRescueText));
                    EndUndo();
                }

                mxTextEditObj->SetMergedItemSetAndBroadcast(*pSet, bReplaceAll);

                FlushComeBackTimer(); // to set ModeHasChanged immediately
            }
        }
        else if (!bOnlyEEItems)
        {
            // Otherwise split Set, if necessary.
            // Now we build an ItemSet aSet that doesn't contain EE_Items from
            // *pSet (otherwise it would be a copy).
            std::unique_ptr<sal_uInt16[]> pNewWhichTable
                = RemoveWhichRange(pSet->GetRanges(), EE_ITEMS_START, EE_ITEMS_END);
            SfxItemSet aSet(mpModel->GetItemPool(), pNewWhichTable.get());
            pNewWhichTable.reset();
            SfxWhichIter aIter(aSet);
            sal_uInt16 nWhich = aIter.FirstWhich();
            while (nWhich != 0)
            {
                const SfxPoolItem* pItem;
                SfxItemState eState = pSet->GetItemState(nWhich, false, &pItem);
                if (eState == SfxItemState::SET)
                    aSet.Put(*pItem);
                nWhich = aIter.NextWhich();
            }

            if (mxSelectionController.is())
                bRet = mxSelectionController->SetAttributes(aSet, bReplaceAll);

            if (!bRet)
            {
                if (IsUndoEnabled())
                {
                    BegUndo(ImpGetDescriptionString(STR_EditSetAttributes));
                    AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*mxTextEditObj));
                    AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoAttrObject(*mxTextEditObj));
                    EndUndo();
                }

                mxTextEditObj->SetMergedItemSetAndBroadcast(aSet, bReplaceAll);

                if (GetMarkedObjectCount() == 1 && GetMarkedObjectByIndex(0) == mxTextEditObj.get())
                {
                    SetNotPersistAttrToMarked(aSet);
                }
            }
            FlushComeBackTimer();
        }
        if (!bNoEEItems)
        {
            // and now the attributes to the EditEngine
            if (bReplaceAll)
            {
                pTextEditOutlinerView->RemoveAttribs(true);
            }
            pTextEditOutlinerView->SetAttribs(rSet);

            Outliner* pTEOutliner = pTextEditOutlinerView->GetOutliner();
            if (mpModel && pTEOutliner && pTEOutliner->IsModified())
                mpModel->SetChanged();

            ImpMakeTextCursorAreaVisible();
        }
        bRet = true;
    }
    return bRet;
}

SfxStyleSheet* SdrObjEditView::GetStyleSheet() const
{
    SfxStyleSheet* pSheet = nullptr;

    if (mxSelectionController.is())
    {
        if (mxSelectionController->GetStyleSheet(pSheet))
            return pSheet;
    }

    if (pTextEditOutlinerView)
    {
        pSheet = pTextEditOutlinerView->GetStyleSheet();
    }
    else
    {
        pSheet = SdrGlueEditView::GetStyleSheet();
    }
    return pSheet;
}

void SdrObjEditView::SetStyleSheet(SfxStyleSheet* pStyleSheet, bool bDontRemoveHardAttr)
{
    if (mxSelectionController.is())
    {
        if (mxSelectionController->SetStyleSheet(pStyleSheet, bDontRemoveHardAttr))
            return;
    }

    // if we are currently in edit mode we must also set the stylesheet
    // on all paragraphs in the Outliner for the edit view
    if (nullptr != pTextEditOutlinerView)
    {
        Outliner* pOutliner = pTextEditOutlinerView->GetOutliner();

        const sal_Int32 nParaCount = pOutliner->GetParagraphCount();
        for (sal_Int32 nPara = 0; nPara < nParaCount; nPara++)
        {
            pOutliner->SetStyleSheet(nPara, pStyleSheet);
        }
    }

    SdrGlueEditView::SetStyleSheet(pStyleSheet, bDontRemoveHardAttr);
}

void SdrObjEditView::AddWindowToPaintView(OutputDevice* pNewWin, vcl::Window* pWindow)
{
    SdrGlueEditView::AddWindowToPaintView(pNewWin, pWindow);

    if (mxTextEditObj.is() && !bTextEditOnlyOneView && pNewWin->GetOutDevType() == OUTDEV_WINDOW)
    {
        OutlinerView* pOutlView = ImpMakeOutlinerView(static_cast<vcl::Window*>(pNewWin), nullptr);
        pTextEditOutliner->InsertView(pOutlView);
    }
}

void SdrObjEditView::DeleteWindowFromPaintView(OutputDevice* pOldWin)
{
    SdrGlueEditView::DeleteWindowFromPaintView(pOldWin);

    if (mxTextEditObj.is() && !bTextEditOnlyOneView && pOldWin->GetOutDevType() == OUTDEV_WINDOW)
    {
        for (size_t i = pTextEditOutliner->GetViewCount(); i > 0;)
        {
            i--;
            OutlinerView* pOLV = pTextEditOutliner->GetView(i);
            if (pOLV && pOLV->GetWindow() == static_cast<vcl::Window*>(pOldWin))
            {
                pTextEditOutliner->RemoveView(i);
            }
        }
    }

    lcl_RemoveTextEditOutlinerViews(this, GetSdrPageView(), pOldWin);
}

bool SdrObjEditView::IsTextEditInSelectionMode() const
{
    return pTextEditOutliner != nullptr && pTextEditOutliner->IsInSelectionMode();
}

// MacroMode

void SdrObjEditView::BegMacroObj(const Point& rPnt, short nTol, SdrObject* pObj, SdrPageView* pPV,
                                 vcl::Window* pWin)
{
    BrkMacroObj();
    if (pObj != nullptr && pPV != nullptr && pWin != nullptr && pObj->HasMacro())
    {
        nTol = ImpGetHitTolLogic(nTol, nullptr);
        pMacroObj = pObj;
        pMacroPV = pPV;
        pMacroWin = pWin;
        bMacroDown = false;
        nMacroTol = sal_uInt16(nTol);
        aMacroDownPos = rPnt;
        MovMacroObj(rPnt);
    }
}

void SdrObjEditView::ImpMacroUp(const Point& rUpPos)
{
    if (pMacroObj != nullptr && bMacroDown)
    {
        SdrObjMacroHitRec aHitRec;
        aHitRec.aPos = rUpPos;
        aHitRec.nTol = nMacroTol;
        aHitRec.pVisiLayer = &pMacroPV->GetVisibleLayers();
        aHitRec.pPageView = pMacroPV;
        pMacroObj->PaintMacro(*pMacroWin, tools::Rectangle(), aHitRec);
        bMacroDown = false;
    }
}

void SdrObjEditView::ImpMacroDown(const Point& rDownPos)
{
    if (pMacroObj != nullptr && !bMacroDown)
    {
        SdrObjMacroHitRec aHitRec;
        aHitRec.aPos = rDownPos;
        aHitRec.nTol = nMacroTol;
        aHitRec.pVisiLayer = &pMacroPV->GetVisibleLayers();
        aHitRec.pPageView = pMacroPV;
        pMacroObj->PaintMacro(*pMacroWin, tools::Rectangle(), aHitRec);
        bMacroDown = true;
    }
}

void SdrObjEditView::MovMacroObj(const Point& rPnt)
{
    if (pMacroObj != nullptr)
    {
        SdrObjMacroHitRec aHitRec;
        aHitRec.aPos = rPnt;
        aHitRec.nTol = nMacroTol;
        aHitRec.pVisiLayer = &pMacroPV->GetVisibleLayers();
        aHitRec.pPageView = pMacroPV;
        bool bDown = pMacroObj->IsMacroHit(aHitRec);
        if (bDown)
            ImpMacroDown(rPnt);
        else
            ImpMacroUp(rPnt);
    }
}

void SdrObjEditView::BrkMacroObj()
{
    if (pMacroObj != nullptr)
    {
        ImpMacroUp(aMacroDownPos);
        pMacroObj = nullptr;
        pMacroPV = nullptr;
        pMacroWin = nullptr;
    }
}

bool SdrObjEditView::EndMacroObj()
{
    if (pMacroObj != nullptr && bMacroDown)
    {
        ImpMacroUp(aMacroDownPos);
        SdrObjMacroHitRec aHitRec;
        aHitRec.aPos = aMacroDownPos;
        aHitRec.nTol = nMacroTol;
        aHitRec.pVisiLayer = &pMacroPV->GetVisibleLayers();
        aHitRec.pPageView = pMacroPV;
        bool bRet = pMacroObj->DoMacro(aHitRec);
        pMacroObj = nullptr;
        pMacroPV = nullptr;
        pMacroWin = nullptr;
        return bRet;
    }
    else
    {
        BrkMacroObj();
        return false;
    }
}

/** fills the given any with a XTextCursor for the current text selection.
    Leaves the any untouched if there currently is no text selected */
void SdrObjEditView::getTextSelection(css::uno::Any& rSelection)
{
    if (IsTextEdit())
    {
        OutlinerView* pOutlinerView = GetTextEditOutlinerView();
        if (pOutlinerView && pOutlinerView->HasSelection())
        {
            SdrObject* pObj = GetTextEditObject();

            if (pObj)
            {
                css::uno::Reference<css::text::XText> xText(pObj->getUnoShape(),
                                                            css::uno::UNO_QUERY);
                if (xText.is())
                {
                    SvxUnoTextBase* pRange
                        = comphelper::getUnoTunnelImplementation<SvxUnoTextBase>(xText);
                    if (pRange)
                    {
                        rSelection
                            <<= pRange->createTextCursorBySelection(pOutlinerView->GetSelection());
                    }
                }
            }
        }
    }
}

/* check if we have a single selection and that single object likes
    to handle the mouse and keyboard events itself

    TODO: the selection controller should be queried from the
    object specific view contact. Currently this method only
    works for tables.
*/
void SdrObjEditView::MarkListHasChanged()
{
    SdrGlueEditView::MarkListHasChanged();

    if (mxSelectionController.is())
    {
        mxLastSelectionController = mxSelectionController;
        mxSelectionController->onSelectionHasChanged();
    }

    mxSelectionController.clear();

    const SdrMarkList& rMarkList = GetMarkedObjectList();
    if (rMarkList.GetMarkCount() == 1)
    {
        const SdrObject* pObj(rMarkList.GetMark(0)->GetMarkedSdrObj());
        SdrView* pView(dynamic_cast<SdrView*>(this));

        // check for table
        if (pObj && pView && (pObj->GetObjInventor() == SdrInventor::Default)
            && (pObj->GetObjIdentifier() == OBJ_TABLE))
        {
            mxSelectionController = sdr::table::CreateTableController(
                *pView, static_cast<const sdr::table::SdrTableObj&>(*pObj),
                mxLastSelectionController);

            if (mxSelectionController.is())
            {
                mxLastSelectionController.clear();
                mxSelectionController->onSelectionHasChanged();
            }
        }
    }
}

IMPL_LINK(SdrObjEditView, EndPasteOrDropHdl, PasteOrDropInfos*, pInfo, void)
{
    OnEndPasteOrDrop(pInfo);
}

IMPL_LINK(SdrObjEditView, BeginPasteOrDropHdl, PasteOrDropInfos*, pInfo, void)
{
    OnBeginPasteOrDrop(pInfo);
}

void SdrObjEditView::OnBeginPasteOrDrop(PasteOrDropInfos*)
{
    // applications can derive from these virtual methods to do something before a drop or paste operation
}

void SdrObjEditView::OnEndPasteOrDrop(PasteOrDropInfos*)
{
    // applications can derive from these virtual methods to do something before a drop or paste operation
}

sal_uInt16 SdrObjEditView::GetSelectionLevel() const
{
    sal_uInt16 nLevel = 0xFFFF;
    if (IsTextEdit())
    {
        DBG_ASSERT(pTextEditOutlinerView != nullptr,
                   "SdrObjEditView::GetAttributes(): pTextEditOutlinerView=NULL");
        DBG_ASSERT(pTextEditOutliner != nullptr,
                   "SdrObjEditView::GetAttributes(): pTextEditOutliner=NULL");
        if (pTextEditOutlinerView)
        {
            //start and end position
            ESelection aSelect = pTextEditOutlinerView->GetSelection();
            sal_uInt16 nStartPara = ::std::min(aSelect.nStartPara, aSelect.nEndPara);
            sal_uInt16 nEndPara = ::std::max(aSelect.nStartPara, aSelect.nEndPara);
            //get level from each paragraph
            nLevel = 0;
            for (sal_uInt16 nPara = nStartPara; nPara <= nEndPara; nPara++)
            {
                sal_uInt16 nParaDepth
                    = 1 << static_cast<sal_uInt16>(pTextEditOutliner->GetDepth(nPara));
                if (!(nLevel & nParaDepth))
                    nLevel += nParaDepth;
            }
            //reduce one level for Outliner Object
            //if( nLevel > 0 && GetTextEditObject()->GetObjIdentifier() == OBJ_OUTLINETEXT )
            //  nLevel = nLevel >> 1;
            //no bullet paragraph selected
            if (nLevel == 0)
                nLevel = 0xFFFF;
        }
    }
    return nLevel;
}

bool SdrObjEditView::SupportsFormatPaintbrush(SdrInventor nObjectInventor,
                                              sal_uInt16 nObjectIdentifier)
{
    if (nObjectInventor != SdrInventor::Default && nObjectInventor != SdrInventor::E3d)
        return false;
    switch (nObjectIdentifier)
    {
        case OBJ_NONE:
        case OBJ_GRUP:
            return false;
        case OBJ_LINE:
        case OBJ_RECT:
        case OBJ_CIRC:
        case OBJ_SECT:
        case OBJ_CARC:
        case OBJ_CCUT:
        case OBJ_POLY:
        case OBJ_PLIN:
        case OBJ_PATHLINE:
        case OBJ_PATHFILL:
        case OBJ_FREELINE:
        case OBJ_FREEFILL:
        case OBJ_SPLNLINE:
        case OBJ_SPLNFILL:
        case OBJ_TEXT:
        case OBJ_TITLETEXT:
        case OBJ_OUTLINETEXT:
        case OBJ_GRAF:
        case OBJ_OLE2:
        case OBJ_TABLE:
            return true;
        case OBJ_EDGE:
        case OBJ_CAPTION:
            return false;
        case OBJ_PATHPOLY:
        case OBJ_PATHPLIN:
            return true;
        case OBJ_PAGE:
        case OBJ_MEASURE:
        case OBJ_FRAME:
        case OBJ_UNO:
            return false;
        case OBJ_CUSTOMSHAPE:
            return true;
        default:
            return false;
    }
}

static const sal_uInt16* GetFormatRangeImpl(bool bTextOnly)
{
    static const sal_uInt16 gRanges[] = { SDRATTR_SHADOW_FIRST,
                                          SDRATTR_SHADOW_LAST,
                                          SDRATTR_GRAF_FIRST,
                                          SDRATTR_GRAF_LAST,
                                          SDRATTR_TABLE_FIRST,
                                          SDRATTR_TABLE_LAST,
                                          XATTR_LINE_FIRST,
                                          XATTR_LINE_LAST,
                                          XATTR_FILL_FIRST,
                                          XATTRSET_FILL,
                                          EE_PARA_START,
                                          EE_PARA_END, // text-only from here on
                                          EE_CHAR_START,
                                          EE_CHAR_END,
                                          SDRATTR_MISC_FIRST,
                                          SDRATTR_MISC_LAST, // table cell formats
                                          0,
                                          0 };
    return &gRanges[bTextOnly ? 10 : 0];
}

void SdrObjEditView::TakeFormatPaintBrush(std::shared_ptr<SfxItemSet>& rFormatSet)
{
    const SdrMarkList& rMarkList = GetMarkedObjectList();
    if (rMarkList.GetMarkCount() > 0)
    {
        OutlinerView* pOLV = GetTextEditOutlinerView();

        rFormatSet = std::make_shared<SfxItemSet>(GetModel()->GetItemPool(),
                                                  GetFormatRangeImpl(pOLV != nullptr));
        if (pOLV)
        {
            rFormatSet->Put(pOLV->GetAttribs());
        }
        else
        {
            const bool bOnlyHardAttr = false;
            rFormatSet->Put(GetAttrFromMarked(bOnlyHardAttr));
        }

        // check for cloning from table cell, in which case we need to copy cell-specific formatting attributes
        const SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
        if (pObj && (pObj->GetObjInventor() == SdrInventor::Default)
            && (pObj->GetObjIdentifier() == OBJ_TABLE))
        {
            auto pTable = static_cast<const sdr::table::SdrTableObj*>(pObj);
            if (mxSelectionController.is() && pTable->getActiveCell().is())
            {
                mxSelectionController->GetAttributes(*rFormatSet, false);
            }
        }
    }
}

static SfxItemSet CreatePaintSet(const sal_uInt16* pRanges, SfxItemPool& rPool,
                                 const SfxItemSet& rSourceSet, const SfxItemSet& rTargetSet,
                                 bool bNoCharacterFormats, bool bNoParagraphFormats)
{
    SfxItemSet aPaintSet(rPool, pRanges);

    while (*pRanges)
    {
        sal_uInt16 nWhich = *pRanges++;
        const sal_uInt16 nLastWhich = *pRanges++;

        if (bNoCharacterFormats && (nWhich == EE_CHAR_START))
            continue;

        if (bNoParagraphFormats && (nWhich == EE_PARA_START))
            continue;

        for (; nWhich < nLastWhich; nWhich++)
        {
            const SfxPoolItem* pSourceItem = rSourceSet.GetItem(nWhich);
            const SfxPoolItem* pTargetItem = rTargetSet.GetItem(nWhich);

            if ((pSourceItem && !pTargetItem)
                || (pSourceItem && pTargetItem && *pSourceItem != *pTargetItem))
            {
                aPaintSet.Put(*pSourceItem);
            }
        }
    }
    return aPaintSet;
}

void SdrObjEditView::ApplyFormatPaintBrushToText(SfxItemSet const& rFormatSet, SdrTextObj& rTextObj,
                                                 SdrText* pText, bool bNoCharacterFormats,
                                                 bool bNoParagraphFormats)
{
    OutlinerParaObject* pParaObj = pText ? pText->GetOutlinerParaObject() : nullptr;
    if (pParaObj)
    {
        SdrOutliner& rOutliner = rTextObj.ImpGetDrawOutliner();
        rOutliner.SetText(*pParaObj);

        sal_Int32 nParaCount(rOutliner.GetParagraphCount());

        if (nParaCount)
        {
            for (sal_Int32 nPara = 0; nPara < nParaCount; nPara++)
            {
                if (!bNoCharacterFormats)
                    rOutliner.RemoveCharAttribs(nPara);

                SfxItemSet aSet(rOutliner.GetParaAttribs(nPara));
                aSet.Put(CreatePaintSet(GetFormatRangeImpl(true), *aSet.GetPool(), rFormatSet, aSet,
                                        bNoCharacterFormats, bNoParagraphFormats));
                rOutliner.SetParaAttribs(nPara, aSet);
            }

            std::unique_ptr<OutlinerParaObject> pTemp = rOutliner.CreateParaObject(0, nParaCount);
            rOutliner.Clear();

            rTextObj.NbcSetOutlinerParaObjectForText(std::move(pTemp), pText);
        }
    }
}

void SdrObjEditView::ApplyFormatPaintBrush(SfxItemSet& rFormatSet, bool bNoCharacterFormats,
                                           bool bNoParagraphFormats)
{
    if (mxSelectionController.is()
        && mxSelectionController->ApplyFormatPaintBrush(rFormatSet, bNoCharacterFormats,
                                                        bNoParagraphFormats))
    {
        return;
    }

    OutlinerView* pOLV = GetTextEditOutlinerView();
    const SdrMarkList& rMarkList = GetMarkedObjectList();
    if (!pOLV)
    {
        SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
        const SfxItemSet& rShapeSet = pObj->GetMergedItemSet();

        // if not in text edit mode (aka the user selected text or clicked on a word)
        // apply formatting attributes to selected shape
        // All formatting items (see ranges above) that are unequal in selected shape and
        // the format paintbrush are hard set on the selected shape.

        const sal_uInt16* pRanges = rFormatSet.GetRanges();
        bool bTextOnly = true;

        while (*pRanges)
        {
            if ((*pRanges != EE_PARA_START) && (*pRanges != EE_CHAR_START))
            {
                bTextOnly = false;
                break;
            }
            pRanges += 2;
        }

        if (!bTextOnly)
        {
            SfxItemSet aPaintSet(CreatePaintSet(GetFormatRangeImpl(false), *rShapeSet.GetPool(),
                                                rFormatSet, rShapeSet, bNoCharacterFormats,
                                                bNoParagraphFormats));
            SetAttrToMarked(aPaintSet, false /*bReplaceAll*/);
        }

        // now apply character and paragraph formatting to text, if the shape has any
        SdrTextObj* pTextObj = dynamic_cast<SdrTextObj*>(pObj);
        if (pTextObj)
        {
            sal_Int32 nText = pTextObj->getTextCount();

            while (--nText >= 0)
            {
                SdrText* pText = pTextObj->getText(nText);
                ApplyFormatPaintBrushToText(rFormatSet, *pTextObj, pText, bNoCharacterFormats,
                                            bNoParagraphFormats);
            }
        }
    }
    else
    {
        ::Outliner* pOutliner = pOLV->GetOutliner();
        if (pOutliner)
        {
            const EditEngine& rEditEngine = pOutliner->GetEditEngine();

            ESelection aSel(pOLV->GetSelection());
            if (!aSel.HasRange())
                pOLV->SetSelection(rEditEngine.GetWord(aSel, css::i18n::WordType::DICTIONARY_WORD));

            const bool bRemoveParaAttribs = !bNoParagraphFormats;
            pOLV->RemoveAttribsKeepLanguages(bRemoveParaAttribs);
            SfxItemSet aSet(pOLV->GetAttribs());
            SfxItemSet aPaintSet(CreatePaintSet(GetFormatRangeImpl(true), *aSet.GetPool(),
                                                rFormatSet, aSet, bNoCharacterFormats,
                                                bNoParagraphFormats));
            pOLV->SetAttribs(aPaintSet);
        }
    }

    // check for cloning to table cell, in which case we need to copy cell-specific formatting attributes
    SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
    if (pObj && (pObj->GetObjInventor() == SdrInventor::Default)
        && (pObj->GetObjIdentifier() == OBJ_TABLE))
    {
        auto pTable = static_cast<sdr::table::SdrTableObj*>(pObj);
        if (pTable->getActiveCell().is() && mxSelectionController.is())
        {
            mxSelectionController->SetAttributes(rFormatSet, false);
        }
    }
}

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