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
|
/* -*- 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 <sal/config.h>
#include <com/sun/star/accessibility/AccessibleEventId.hpp>
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XFramesSupplier.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <comphelper/lok.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/servicehelper.hxx>
#include <comphelper/storagehelper.hxx>
#include <comphelper/string.hxx>
#include <i18nutil/unicode.hxx>
#include <officecfg/Office/Common.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/docfilt.hxx>
#include <sfx2/docinsert.hxx>
#include <sfx2/filedlghelper.hxx>
#include <sfx2/infobar.hxx>
#include <sfx2/msg.hxx>
#include <sfx2/objface.hxx>
#include <sfx2/printer.hxx>
#include <sfx2/request.hxx>
#include <sfx2/sfxbasecontroller.hxx>
#include <sfx2/sidebar/SidebarChildWindow.hxx>
#include <sfx2/viewfac.hxx>
#include <svl/eitem.hxx>
#include <svl/itemset.hxx>
#include <svl/poolitem.hxx>
#include <svl/stritem.hxx>
#include <vcl/transfer.hxx>
#include <svtools/colorcfg.hxx>
#include <svl/whiter.hxx>
#include <svx/sidebar/SelectionChangeHandler.hxx>
#include <svx/zoomslideritem.hxx>
#include <editeng/editeng.hxx>
#include <editeng/editview.hxx>
#include <svx/svxdlg.hxx>
#include <sfx2/zoomitem.hxx>
#include <vcl/commandevent.hxx>
#include <vcl/event.hxx>
#include <vcl/settings.hxx>
#include <vcl/virdev.hxx>
#include <sal/log.hxx>
#include <tools/svborder.hxx>
#include <o3tl/string_view.hxx>
#include <unotools/streamwrap.hxx>
#include <unomodel.hxx>
#include <view.hxx>
#include <cfgitem.hxx>
#include <dialog.hxx>
#include <document.hxx>
#include <starmath.hrc>
#include <strings.hrc>
#include <smmod.hxx>
#include <mathmlimport.hxx>
#include <cursor.hxx>
#include "accessibility.hxx"
#include <ElementsDockingWindow.hxx>
#include <helpids.h>
#define MINZOOM sal_uInt16(25)
#define MAXZOOM sal_uInt16(800)
// space around the edit window, in pixels
// fdo#69111: Increased border on the top so that the window is
// easier to tear off.
#define CMD_BOX_PADDING 3
#define CMD_BOX_PADDING_TOP 11
#define ShellClass_SmViewShell
#include <smslots.hxx>
using namespace css;
using namespace css::accessibility;
using namespace css::uno;
SmGraphicWindow::SmGraphicWindow(SmViewShell& rShell)
: InterimItemWindow(&rShell.GetViewFrame()->GetWindow(), "modules/smath/ui/mathwindow.ui", "MathWindow")
, nLinePixH(GetSettings().GetStyleSettings().GetScrollBarSize())
, nColumnPixW(nLinePixH)
, nZoom(100)
// continue to use user-scrolling to make this work equivalent to how it 'always' worked
, mxScrolledWindow(m_xBuilder->weld_scrolled_window("scrolledwindow", true))
, mxGraphic(new SmGraphicWidget(rShell, *this))
, mxGraphicWin(new weld::CustomWeld(*m_xBuilder, "mathview", *mxGraphic))
{
InitControlBase(mxGraphic->GetDrawingArea());
mxScrolledWindow->connect_hadjustment_changed(LINK(this, SmGraphicWindow, ScrollHdl));
mxScrolledWindow->connect_vadjustment_changed(LINK(this, SmGraphicWindow, ScrollHdl));
// docking windows are usually hidden (often already done in the
// resource) and will be shown by the sfx framework.
Hide();
}
void SmGraphicWindow::dispose()
{
InitControlBase(nullptr);
mxGraphicWin.reset();
mxGraphic.reset();
mxScrolledWindow.reset();
InterimItemWindow::dispose();
}
SmGraphicWindow::~SmGraphicWindow()
{
disposeOnce();
}
void SmGraphicWindow::Resize()
{
InterimItemWindow::Resize();
// get the new output-size in pixel
Size aOutPixSz = GetOutputSizePixel();
// determine the size of the output-area and if we need scrollbars
const auto nScrSize = mxScrolledWindow->get_scroll_thickness();
bool bVVisible = false; // by default no vertical-ScrollBar
bool bHVisible = false; // by default no horizontal-ScrollBar
bool bChanged; // determines if a visiblility was changed
do
{
bChanged = false;
// does we need a vertical ScrollBar
if ( aOutPixSz.Width() < aTotPixSz.Width() && !bHVisible )
{
bHVisible = true;
aOutPixSz.AdjustHeight( -nScrSize );
bChanged = true;
}
// does we need a horizontal ScrollBar
if ( aOutPixSz.Height() < aTotPixSz.Height() && !bVVisible )
{
bVVisible = true;
aOutPixSz.AdjustWidth( -nScrSize );
bChanged = true;
}
}
while ( bChanged ); // until no visibility has changed
// store the old offset and map-mode
MapMode aMap(GetGraphicMapMode());
Point aOldPixOffset(aPixOffset);
// justify (right/bottom borders should never exceed the virtual window)
Size aPixDelta;
if ( aPixOffset.X() < 0 &&
aPixOffset.X() + aTotPixSz.Width() < aOutPixSz.Width() )
aPixDelta.setWidth(
aOutPixSz.Width() - ( aPixOffset.X() + aTotPixSz.Width() ) );
if ( aPixOffset.Y() < 0 &&
aPixOffset.Y() + aTotPixSz.Height() < aOutPixSz.Height() )
aPixDelta.setHeight(
aOutPixSz.Height() - ( aPixOffset.Y() + aTotPixSz.Height() ) );
if ( aPixDelta.Width() || aPixDelta.Height() )
{
aPixOffset.AdjustX(aPixDelta.Width() );
aPixOffset.AdjustY(aPixDelta.Height() );
}
// for axis without scrollbar restore the origin
if ( !bVVisible || !bHVisible )
{
aPixOffset = Point(
bHVisible
? aPixOffset.X()
: (aOutPixSz.Width()-aTotPixSz.Width()) / 2,
bVVisible
? aPixOffset.Y()
: (aOutPixSz.Height()-aTotPixSz.Height()) / 2 );
}
if (bHVisible && mxScrolledWindow->get_hpolicy() == VclPolicyType::NEVER)
aPixOffset.setX( 0 );
if (bVVisible && mxScrolledWindow->get_vpolicy() == VclPolicyType::NEVER)
aPixOffset.setY( 0 );
// select the shifted map-mode
if (aPixOffset != aOldPixOffset)
SetGraphicMapMode(aMap);
// show or hide scrollbars
mxScrolledWindow->set_vpolicy(bVVisible ? VclPolicyType::ALWAYS : VclPolicyType::NEVER);
mxScrolledWindow->set_hpolicy(bHVisible ? VclPolicyType::ALWAYS : VclPolicyType::NEVER);
// resize scrollbars and set their ranges
if ( bHVisible )
{
mxScrolledWindow->hadjustment_configure(-aPixOffset.X(), 0, aTotPixSz.Width(), nColumnPixW,
aOutPixSz.Width(), aOutPixSz.Width());
}
if ( bVVisible )
{
mxScrolledWindow->vadjustment_configure(-aPixOffset.Y(), 0, aTotPixSz.Height(), nLinePixH,
aOutPixSz.Height(), aOutPixSz.Height());
}
}
IMPL_LINK_NOARG(SmGraphicWindow, ScrollHdl, weld::ScrolledWindow&, void)
{
MapMode aMap(GetGraphicMapMode());
Point aNewPixOffset(aPixOffset);
// scrolling horizontally?
if (mxScrolledWindow->get_hpolicy() == VclPolicyType::ALWAYS)
aNewPixOffset.setX(-mxScrolledWindow->hadjustment_get_value());
// scrolling vertically?
if (mxScrolledWindow->get_vpolicy() == VclPolicyType::ALWAYS)
aNewPixOffset.setY(-mxScrolledWindow->vadjustment_get_value());
// scrolling?
if (aPixOffset == aNewPixOffset)
return;
// recompute the logical scroll units
aPixOffset = aNewPixOffset;
SetGraphicMapMode(aMap);
}
void SmGraphicWindow::SetGraphicMapMode(const MapMode& rNewMapMode)
{
OutputDevice& rDevice = mxGraphic->GetDrawingArea()->get_ref_device();
MapMode aMap( rNewMapMode );
aMap.SetOrigin( aMap.GetOrigin() + rDevice.PixelToLogic( aPixOffset, aMap ) );
rDevice.SetMapMode( aMap );
mxGraphic->Invalidate();
}
MapMode SmGraphicWindow::GetGraphicMapMode() const
{
OutputDevice& rDevice = mxGraphic->GetDrawingArea()->get_ref_device();
MapMode aMap(rDevice.GetMapMode());
aMap.SetOrigin( aMap.GetOrigin() - rDevice.PixelToLogic( aPixOffset ) );
return aMap;
}
void SmGraphicWindow::SetTotalSize( const Size& rNewSize )
{
OutputDevice& rDevice = mxGraphic->GetDrawingArea()->get_ref_device();
aTotPixSz = rDevice.LogicToPixel(rNewSize);
Resize();
}
Size SmGraphicWindow::GetTotalSize() const
{
OutputDevice& rDevice = mxGraphic->GetDrawingArea()->get_ref_device();
return rDevice.PixelToLogic(aTotPixSz);
}
void SmGraphicWindow::ShowContextMenu(const CommandEvent& rCEvt)
{
GetParent()->ToTop();
Point aPos(5, 5);
if (rCEvt.IsMouseEvent())
aPos = rCEvt.GetMousePosPixel();
// added for replaceability of context menus
SfxDispatcher::ExecutePopup( this, &aPos );
}
SmGraphicWidget::SmGraphicWidget(SmViewShell& rShell, SmGraphicWindow& rGraphicWindow)
: mrGraphicWindow(rGraphicWindow)
, bIsCursorVisible(false)
, bIsLineVisible(false)
, aCaretBlinkTimer("SmGraphicWidget aCaretBlinkTimer")
, mrViewShell(rShell)
{
}
void SmGraphicWidget::SetDrawingArea(weld::DrawingArea* pDrawingArea)
{
weld::CustomWidgetController::SetDrawingArea(pDrawingArea);
OutputDevice& rDevice = pDrawingArea->get_ref_device();
rDevice.SetBackground(SM_MOD()->GetColorConfig().GetColorValue(svtools::DOCCOLOR).nColor);
const Fraction aFraction(1, 1);
rDevice.SetMapMode(MapMode(MapUnit::Map100thMM, Point(), aFraction, aFraction));
SetTotalSize();
SetHelpId(HID_SMA_WIN_DOCUMENT);
ShowLine(false);
CaretBlinkInit();
}
SmGraphicWidget::~SmGraphicWidget()
{
if (mxAccessible.is())
mxAccessible->ClearWin(); // make Accessible nonfunctional
mxAccessible.clear();
CaretBlinkStop();
}
bool SmGraphicWidget::MouseButtonDown(const MouseEvent& rMEvt)
{
GrabFocus();
// set formula-cursor and selection of edit window according to the
// position clicked at
SAL_WARN_IF( rMEvt.GetClicks() == 0, "starmath", "0 clicks" );
if ( !rMEvt.IsLeft() )
return true;
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
// get click position relative to formula
Point aPos(rDevice.PixelToLogic(rMEvt.GetPosPixel()) - GetFormulaDrawPos());
const SmNode *pTree = mrViewShell.GetDoc()->GetFormulaTree();
if (!pTree)
return true;
if (SmViewShell::IsInlineEditEnabled()) {
mrViewShell.GetDoc()->GetCursor().MoveTo(&rDevice, aPos, !rMEvt.IsShift());
return true;
}
const SmNode *pNode = nullptr;
// if it was clicked inside the formula then get the appropriate node
if (pTree->OrientedDist(aPos) <= 0)
pNode = pTree->FindRectClosestTo(aPos);
if (!pNode)
return true;
SmEditWindow* pEdit = mrViewShell.GetEditWindow();
if (!pEdit)
return true;
// set selection to the beginning of the token
pEdit->SetSelection(pNode->GetSelection());
SetCursor(pNode);
// allow for immediate editing and
//! implicitly synchronize the cursor position mark in this window
pEdit->GrabFocus();
return true;
}
bool SmGraphicWidget::MouseMove(const MouseEvent &rMEvt)
{
if (rMEvt.IsLeft() && SmViewShell::IsInlineEditEnabled())
{
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
Point aPos(rDevice.PixelToLogic(rMEvt.GetPosPixel()) - GetFormulaDrawPos());
mrViewShell.GetDoc()->GetCursor().MoveTo(&rDevice, aPos, false);
CaretBlinkStop();
SetIsCursorVisible(true);
CaretBlinkStart();
RepaintViewShellDoc();
}
return true;
}
void SmGraphicWidget::GetFocus()
{
if (!SmViewShell::IsInlineEditEnabled())
return;
if (mrViewShell.GetEditWindow())
mrViewShell.GetEditWindow()->Flush();
//Let view shell know what insertions should be done in visual editor
mrViewShell.SetInsertIntoEditWindow(false);
SetIsCursorVisible(true);
ShowLine(true);
CaretBlinkStart();
RepaintViewShellDoc();
}
void SmGraphicWidget::LoseFocus()
{
if (mxAccessible.is())
{
uno::Any aOldValue, aNewValue;
aOldValue <<= AccessibleStateType::FOCUSED;
// aNewValue remains empty
mxAccessible->LaunchEvent( AccessibleEventId::STATE_CHANGED,
aOldValue, aNewValue );
}
if (!SmViewShell::IsInlineEditEnabled())
return;
SetIsCursorVisible(false);
ShowLine(false);
CaretBlinkStop();
RepaintViewShellDoc();
}
void SmGraphicWidget::RepaintViewShellDoc()
{
SmDocShell* pDoc = mrViewShell.GetDoc();
if (pDoc)
pDoc->Repaint();
}
IMPL_LINK_NOARG(SmGraphicWidget, CaretBlinkTimerHdl, Timer *, void)
{
if (IsCursorVisible())
SetIsCursorVisible(false);
else
SetIsCursorVisible(true);
RepaintViewShellDoc();
}
void SmGraphicWidget::CaretBlinkInit()
{
aCaretBlinkTimer.SetInvokeHandler(LINK(this, SmGraphicWidget, CaretBlinkTimerHdl));
aCaretBlinkTimer.SetTimeout(Application::GetSettings().GetStyleSettings().GetCursorBlinkTime());
}
void SmGraphicWidget::CaretBlinkStart()
{
if (!SmViewShell::IsInlineEditEnabled())
return;
if (aCaretBlinkTimer.GetTimeout() != STYLE_CURSOR_NOBLINKTIME)
aCaretBlinkTimer.Start();
}
void SmGraphicWidget::CaretBlinkStop()
{
if (!SmViewShell::IsInlineEditEnabled())
return;
aCaretBlinkTimer.Stop();
}
// shows or hides the formula-cursor depending on 'bShow' is true or not
void SmGraphicWidget::ShowCursor(bool bShow)
{
if (SmViewShell::IsInlineEditEnabled())
return;
bool bInvert = bShow != IsCursorVisible();
if (bInvert)
{
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
InvertFocusRect(rDevice, aCursorRect);
}
SetIsCursorVisible(bShow);
}
void SmGraphicWidget::ShowLine(bool bShow)
{
if (!SmViewShell::IsInlineEditEnabled())
return;
bIsLineVisible = bShow;
}
void SmGraphicWidget::SetCursor(const SmNode *pNode)
{
if (SmViewShell::IsInlineEditEnabled())
return;
const SmNode *pTree = mrViewShell.GetDoc()->GetFormulaTree();
// get appropriate rectangle
Point aOffset (pNode->GetTopLeft() - pTree->GetTopLeft()),
aTLPos (GetFormulaDrawPos() + aOffset);
aTLPos.AdjustX( -(pNode->GetItalicLeftSpace()) );
Size aSize (pNode->GetItalicSize());
SetCursor(tools::Rectangle(aTLPos, aSize));
}
void SmGraphicWidget::SetCursor(const tools::Rectangle &rRect)
// sets cursor to new position (rectangle) 'rRect'.
// The old cursor will be removed, and the new one will be shown if
// that is activated in the ConfigItem
{
if (SmViewShell::IsInlineEditEnabled())
return;
SmModule *pp = SM_MOD();
if (IsCursorVisible())
ShowCursor(false); // clean up remainings of old cursor
aCursorRect = rRect;
if (pp->GetConfig()->IsShowFormulaCursor())
ShowCursor(true); // draw new cursor
}
const SmNode * SmGraphicWidget::SetCursorPos(sal_uInt16 nRow, sal_uInt16 nCol)
// looks for a VISIBLE node in the formula tree with its token at
// (or around) the position 'nRow', 'nCol' in the edit window
// (row and column numbering starts with 1 there!).
// If there is such a node the formula-cursor is set to cover that nodes
// rectangle. If not the formula-cursor will be hidden.
// In any case the search result is being returned.
{
if (SmViewShell::IsInlineEditEnabled())
return nullptr;
// find visible node with token at nRow, nCol
const SmNode *pTree = mrViewShell.GetDoc()->GetFormulaTree(),
*pNode = nullptr;
if (pTree)
pNode = pTree->FindTokenAt(nRow, nCol);
if (pNode)
SetCursor(pNode);
else
ShowCursor(false);
return pNode;
}
void SmGraphicWidget::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
{
SmDocShell& rDoc = *mrViewShell.GetDoc();
Point aPoint;
rDoc.DrawFormula(rRenderContext, aPoint, true); //! modifies aPoint to be the topleft
//! corner of the formula
aFormulaDrawPos = aPoint;
if (SmViewShell::IsInlineEditEnabled())
{
//Draw cursor if any...
if (mrViewShell.GetDoc()->HasCursor() && IsLineVisible())
mrViewShell.GetDoc()->GetCursor().Draw(rRenderContext, aPoint, IsCursorVisible());
}
else
{
SetIsCursorVisible(false); // (old) cursor must be drawn again
const SmEditWindow* pEdit = mrViewShell.GetEditWindow();
if (pEdit)
{ // get new position for formula-cursor (for possible altered formula)
sal_Int32 nRow;
sal_uInt16 nCol;
SmGetLeftSelectionPart(pEdit->GetSelection(), nRow, nCol);
const SmNode *pFound = SetCursorPos(static_cast<sal_uInt16>(nRow), nCol);
SmModule *pp = SM_MOD();
if (pFound && pp->GetConfig()->IsShowFormulaCursor())
ShowCursor(true);
}
}
}
void SmGraphicWidget::SetTotalSize()
{
SmDocShell &rDoc = *mrViewShell.GetDoc();
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
const Size aTmp(rDevice.PixelToLogic(rDevice.LogicToPixel(rDoc.GetSize())));
if (aTmp != mrGraphicWindow.GetTotalSize())
mrGraphicWindow.SetTotalSize(aTmp);
}
namespace
{
SmBracketType BracketTypeOf(sal_uInt32 c)
{
switch (c)
{
case '(':
case ')':
return SmBracketType::Round;
case '[':
case ']':
return SmBracketType::Square;
case '{':
case '}':
return SmBracketType::Curly;
}
assert(false); // Unreachable
return SmBracketType::Round;
}
bool CharInput(sal_uInt32 c, SmCursor& rCursor, OutputDevice& rDevice)
{
switch (c)
{
case 0:
return false;
case ' ':
rCursor.InsertElement(BlankElement);
break;
case '!':
rCursor.InsertElement(FactorialElement);
break;
case '%':
rCursor.InsertElement(PercentElement);
break;
case '*':
rCursor.InsertElement(CDotElement);
break;
case '+':
rCursor.InsertElement(PlusElement);
break;
case '-':
rCursor.InsertElement(MinusElement);
break;
case '<':
rCursor.InsertElement(LessThanElement);
break;
case '=':
rCursor.InsertElement(EqualElement);
break;
case '>':
rCursor.InsertElement(GreaterThanElement);
break;
case '^':
rCursor.InsertSubSup(RSUP);
break;
case '_':
rCursor.InsertSubSup(RSUB);
break;
case '/':
rCursor.InsertFraction();
break;
case '(':
case '[':
case '{':
rCursor.InsertBrackets(BracketTypeOf(c));
break;
case ')':
case ']':
case '}':
if (rCursor.IsAtTailOfBracket(BracketTypeOf(c)))
{
rCursor.Move(&rDevice, MoveRight);
break;
}
[[fallthrough]];
default:
rCursor.InsertText(OUString(&c, 1));
break;
}
return true;
}
}
bool SmGraphicWidget::KeyInput(const KeyEvent& rKEvt)
{
if (!SmViewShell::IsInlineEditEnabled())
return mrViewShell.KeyInput(rKEvt);
bool bConsumed = true;
SmCursor& rCursor = mrViewShell.GetDoc()->GetCursor();
KeyFuncType eFunc = rKEvt.GetKeyCode().GetFunction();
if (eFunc == KeyFuncType::COPY)
rCursor.Copy();
else if (eFunc == KeyFuncType::CUT)
rCursor.Cut();
else if (eFunc == KeyFuncType::PASTE)
rCursor.Paste();
else {
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
switch(nCode)
{
case KEY_LEFT:
{
rCursor.Move(&rDevice, MoveLeft, !rKEvt.GetKeyCode().IsShift());
}break;
case KEY_RIGHT:
{
rCursor.Move(&rDevice, MoveRight, !rKEvt.GetKeyCode().IsShift());
}break;
case KEY_UP:
{
rCursor.Move(&rDevice, MoveUp, !rKEvt.GetKeyCode().IsShift());
}break;
case KEY_DOWN:
{
rCursor.Move(&rDevice, MoveDown, !rKEvt.GetKeyCode().IsShift());
}break;
case KEY_RETURN:
{
if(!rKEvt.GetKeyCode().IsShift())
rCursor.InsertRow();
}break;
case KEY_DELETE:
{
if(!rCursor.HasSelection()){
rCursor.Move(&rDevice, MoveRight, false);
if(rCursor.HasComplexSelection()) break;
}
rCursor.Delete();
}break;
case KEY_BACKSPACE:
{
rCursor.DeletePrev(&rDevice);
}break;
default:
{
if (!CharInput(rKEvt.GetCharCode(), rCursor, rDevice))
bConsumed = mrViewShell.KeyInput(rKEvt);
}
}
}
CaretBlinkStop();
CaretBlinkStart();
SetIsCursorVisible(true);
RepaintViewShellDoc();
return bConsumed;
}
bool SmGraphicWidget::Command(const CommandEvent& rCEvt)
{
bool bCallBase = true;
if (!mrViewShell.GetViewFrame()->GetFrame().IsInPlace())
{
switch ( rCEvt.GetCommand() )
{
case CommandEventId::ContextMenu:
// purely for "ExecutePopup" taking a vcl::Window and
// we assume SmGraphicWindow 0,0 is at SmEditWindow 0,0
mrGraphicWindow.ShowContextMenu(rCEvt);
bCallBase = false;
break;
case CommandEventId::Wheel:
{
const CommandWheelData* pWData = rCEvt.GetWheelData();
if ( pWData && CommandWheelMode::ZOOM == pWData->GetMode() )
{
sal_uInt16 nTmpZoom = mrGraphicWindow.GetZoom();
if( 0 > pWData->GetDelta() )
nTmpZoom -= 10;
else
nTmpZoom += 10;
mrGraphicWindow.SetZoom(nTmpZoom);
bCallBase = false;
}
}
break;
default: break;
}
}
else
{
switch (rCEvt.GetCommand())
{
case CommandEventId::ExtTextInput:
if (comphelper::LibreOfficeKit::isActive())
{
const CommandExtTextInputData* pData = rCEvt.GetExtTextInputData();
assert(pData);
const OUString& rText = pData->GetText();
SmCursor& rCursor = mrViewShell.GetDoc()->GetCursor();
OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
for (sal_Int32 i = 0; i < rText.getLength();)
CharInput(rText.iterateCodePoints(&i), rCursor, rDevice);
bCallBase = false;
}
break;
default:
break;
}
}
return !bCallBase;
}
void SmGraphicWindow::SetZoom(sal_uInt16 Factor)
{
nZoom = std::clamp(Factor, MINZOOM, MAXZOOM);
Fraction aFraction(nZoom, 100);
SetGraphicMapMode(MapMode(MapUnit::Map100thMM, Point(), aFraction, aFraction));
mxGraphic->SetTotalSize();
SmViewShell& rViewSh = mxGraphic->GetView();
rViewSh.GetViewFrame()->GetBindings().Invalidate(SID_ATTR_ZOOM);
rViewSh.GetViewFrame()->GetBindings().Invalidate(SID_ATTR_ZOOMSLIDER);
}
void SmGraphicWindow::ZoomToFitInWindow()
{
SmViewShell& rViewSh = mxGraphic->GetView();
SmDocShell& rDoc = *rViewSh.GetDoc();
// set defined mapmode before calling 'LogicToPixel' below
SetGraphicMapMode(MapMode(MapUnit::Map100thMM));
OutputDevice& rDevice = mxGraphic->GetDrawingArea()->get_ref_device();
Size aSize(rDevice.LogicToPixel(rDoc.GetSize()));
Size aWindowSize(GetSizePixel());
if (!aSize.IsEmpty())
{
tools::Long nVal = std::min ((85 * aWindowSize.Width()) / aSize.Width(),
(85 * aWindowSize.Height()) / aSize.Height());
SetZoom ( sal::static_int_cast< sal_uInt16 >(nVal) );
}
}
uno::Reference< XAccessible > SmGraphicWidget::CreateAccessible()
{
if (!mxAccessible.is())
{
mxAccessible = new SmGraphicAccessible( this );
}
return mxAccessible;
}
/**************************************************************************/
SmGraphicController::SmGraphicController(SmGraphicWidget &rSmGraphic,
sal_uInt16 nId_,
SfxBindings &rBindings) :
SfxControllerItem(nId_, rBindings),
rGraphic(rSmGraphic)
{
}
void SmGraphicController::StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
{
rGraphic.SetTotalSize();
rGraphic.Invalidate();
SfxControllerItem::StateChangedAtToolBoxControl (nSID, eState, pState);
}
/**************************************************************************/
SmEditController::SmEditController(SmEditWindow &rSmEdit,
sal_uInt16 nId_,
SfxBindings &rBindings) :
SfxControllerItem(nId_, rBindings),
rEdit(rSmEdit)
{
}
void SmEditController::StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
{
const SfxStringItem *pItem = dynamic_cast<const SfxStringItem*>( pState);
if ((pItem != nullptr) && (rEdit.GetText() != pItem->GetValue()))
rEdit.SetText(pItem->GetValue());
SfxControllerItem::StateChangedAtToolBoxControl (nSID, eState, pState);
}
/**************************************************************************/
SmCmdBoxWindow::SmCmdBoxWindow(SfxBindings *pBindings_, SfxChildWindow *pChildWindow,
vcl::Window *pParent)
: SfxDockingWindow(pBindings_, pChildWindow, pParent, "EditWindow", "modules/smath/ui/editwindow.ui")
, m_xEdit(new SmEditWindow(*this, *m_xBuilder))
, aController(*m_xEdit, SID_TEXT, *pBindings_)
, bExiting(false)
, aInitialFocusTimer("SmCmdBoxWindow aInitialFocusTimer")
{
set_id("math_edit");
SetHelpId( HID_SMA_COMMAND_WIN );
SetSizePixel(LogicToPixel(Size(292 , 94), MapMode(MapUnit::MapAppFont)));
SetText(SmResId(STR_CMDBOXWINDOW));
Hide();
// Don't try to grab focus in LOK inline edit mode
if (!comphelper::LibreOfficeKit::isActive())
{
aInitialFocusTimer.SetInvokeHandler(LINK(this, SmCmdBoxWindow, InitialFocusTimerHdl));
aInitialFocusTimer.SetTimeout(100);
}
}
Point SmCmdBoxWindow::WidgetToWindowPos(const weld::Widget& rWidget, const Point& rPos)
{
Point aRet(rPos);
int x(0), y(0), width(0), height(0);
rWidget.get_extents_relative_to(*m_xContainer, x, y, width, height);
aRet.Move(x, y);
aRet.Move(m_xBox->GetPosPixel().X(), m_xBox->GetPosPixel().Y());
return aRet;
}
void SmCmdBoxWindow::ShowContextMenu(const Point& rPos)
{
ToTop();
SmViewShell *pViewSh = GetView();
if (pViewSh)
pViewSh->GetViewFrame()->GetDispatcher()->ExecutePopup("edit", this, &rPos);
}
void SmCmdBoxWindow::Command(const CommandEvent& rCEvt)
{
if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
{
ShowContextMenu(rCEvt.GetMousePosPixel());
return;
}
SfxDockingWindow::Command(rCEvt);
}
SmCmdBoxWindow::~SmCmdBoxWindow ()
{
disposeOnce();
}
void SmCmdBoxWindow::dispose()
{
aInitialFocusTimer.Stop();
bExiting = true;
aController.dispose();
m_xEdit.reset();
SfxDockingWindow::dispose();
}
SmViewShell * SmCmdBoxWindow::GetView()
{
SfxDispatcher *pDispatcher = GetBindings().GetDispatcher();
SfxViewShell *pView = pDispatcher ? pDispatcher->GetFrame()->GetViewShell() : nullptr;
return dynamic_cast<SmViewShell*>( pView);
}
Size SmCmdBoxWindow::CalcDockingSize(SfxChildAlignment eAlign)
{
switch (eAlign)
{
case SfxChildAlignment::LEFT:
case SfxChildAlignment::RIGHT:
return Size();
default:
break;
}
return SfxDockingWindow::CalcDockingSize(eAlign);
}
SfxChildAlignment SmCmdBoxWindow::CheckAlignment(SfxChildAlignment eActual,
SfxChildAlignment eWish)
{
switch (eWish)
{
case SfxChildAlignment::TOP:
case SfxChildAlignment::BOTTOM:
case SfxChildAlignment::NOALIGNMENT:
return eWish;
default:
break;
}
return eActual;
}
void SmCmdBoxWindow::StateChanged( StateChangedType nStateChange )
{
if (StateChangedType::InitShow == nStateChange)
{
Resize(); // avoid SmEditWindow not being painted correctly
// set initial position of window in floating mode
if (IsFloatingMode())
AdjustPosition(); //! don't change pos in docking-mode !
aInitialFocusTimer.Start();
}
SfxDockingWindow::StateChanged( nStateChange );
}
IMPL_LINK_NOARG( SmCmdBoxWindow, InitialFocusTimerHdl, Timer *, void )
{
// We want to have the focus in the edit window once Math has been opened
// to allow for immediate typing.
// Problem: There is no proper way to do this
// Thus: this timer based solution has been implemented (see GrabFocus below)
// Follow-up problem (#i114910): grabbing the focus may bust the help system since
// it relies on getting the current frame which conflicts with grabbing the focus.
// Thus aside from the 'GrabFocus' call everything else is to get the
// help reliably working despite using 'GrabFocus'.
try
{
uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( comphelper::getProcessComponentContext() );
m_xEdit->GrabFocus();
SmViewShell* pView = GetView();
assert(pView);
bool bInPlace = pView->GetViewFrame()->GetFrame().IsInPlace();
uno::Reference< frame::XFrame > xFrame( GetBindings().GetDispatcher()->GetFrame()->GetFrame().GetFrameInterface());
if ( bInPlace )
{
uno::Reference<container::XChild> xModel(pView->GetDoc()->GetModel(),
uno::UNO_QUERY_THROW);
uno::Reference< frame::XModel > xParent( xModel->getParent(), uno::UNO_QUERY_THROW );
uno::Reference< frame::XController > xParentCtrler( xParent->getCurrentController() );
uno::Reference< frame::XFramesSupplier > xParentFrame( xParentCtrler->getFrame(), uno::UNO_QUERY_THROW );
xParentFrame->setActiveFrame( xFrame );
}
else
{
xDesktop->setActiveFrame( xFrame );
}
}
catch (uno::Exception &)
{
SAL_WARN( "starmath", "failed to properly set initial focus to edit window" );
}
}
void SmCmdBoxWindow::AdjustPosition()
{
const tools::Rectangle aRect( Point(), GetParent()->GetOutputSizePixel() );
Point aTopLeft( Point( aRect.Left(),
aRect.Bottom() - GetSizePixel().Height() ) );
Point aPos( GetParent()->OutputToScreenPixel( aTopLeft ) );
if (aPos.X() < 0)
aPos.setX( 0 );
if (aPos.Y() < 0)
aPos.setY( 0 );
SetPosPixel( aPos );
}
void SmCmdBoxWindow::ToggleFloatingMode()
{
SfxDockingWindow::ToggleFloatingMode();
if (GetFloatingWindow())
GetFloatingWindow()->SetMinOutputSizePixel(Size (200, 50));
}
void SmCmdBoxWindow::GetFocus()
{
if (!bExiting)
m_xEdit->GrabFocus();
}
SFX_IMPL_DOCKINGWINDOW_WITHID(SmCmdBoxWrapper, SID_CMDBOXWINDOW);
SmCmdBoxWrapper::SmCmdBoxWrapper(vcl::Window *pParentWindow, sal_uInt16 nId,
SfxBindings *pBindings,
SfxChildWinInfo *pInfo) :
SfxChildWindow(pParentWindow, nId)
{
VclPtrInstance<SmCmdBoxWindow> pDialog(pBindings, this, pParentWindow);
SetWindow(pDialog);
// make window docked to the bottom initially (after first start)
SetAlignment(SfxChildAlignment::BOTTOM);
pDialog->setDeferredProperties();
pDialog->set_border_width(CMD_BOX_PADDING);
pDialog->set_margin_top(CMD_BOX_PADDING_TOP);
pDialog->Initialize(pInfo);
}
SFX_IMPL_SUPERCLASS_INTERFACE(SmViewShell, SfxViewShell)
void SmViewShell::InitInterface_Impl()
{
GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_TOOLS,
SfxVisibilityFlags::Standard | SfxVisibilityFlags::FullScreen | SfxVisibilityFlags::Server,
ToolbarId::Math_Toolbox);
//Dummy-Objectbar, to avoid quiver while activating
GetStaticInterface()->RegisterChildWindow(SmCmdBoxWrapper::GetChildWindowId());
GetStaticInterface()->RegisterChildWindow(SmElementsDockingWindowWrapper::GetChildWindowId());
GetStaticInterface()->RegisterChildWindow(SfxInfoBarContainerChild::GetChildWindowId());
GetStaticInterface()->RegisterChildWindow(::sfx2::sidebar::SidebarChildWindow::GetChildWindowId());
}
SFX_IMPL_NAMED_VIEWFACTORY(SmViewShell, "Default")
{
SFX_VIEW_REGISTRATION(SmDocShell);
}
void SmViewShell::InnerResizePixel(const Point &rOfs, const Size &rSize, bool)
{
Size aObjSize = GetObjectShell()->GetVisArea().GetSize();
if ( !aObjSize.IsEmpty() )
{
Size aProvidedSize = GetWindow()->PixelToLogic(rSize, MapMode(MapUnit::Map100thMM));
Fraction aZoomX(aProvidedSize.Width(), aObjSize.Width());
Fraction aZoomY(aProvidedSize.Height(), aObjSize.Height());
MapMode aMap(mxGraphicWindow->GetGraphicMapMode());
aMap.SetScaleX(aZoomX);
aMap.SetScaleY(aZoomY);
mxGraphicWindow->SetGraphicMapMode(aMap);
}
SetBorderPixel( SvBorder() );
mxGraphicWindow->SetPosSizePixel(rOfs, rSize);
GetGraphicWidget().SetTotalSize();
}
void SmViewShell::OuterResizePixel(const Point &rOfs, const Size &rSize)
{
mxGraphicWindow->SetPosSizePixel(rOfs, rSize);
if (GetDoc()->IsPreview())
mxGraphicWindow->ZoomToFitInWindow();
}
void SmViewShell::QueryObjAreaPixel( tools::Rectangle& rRect ) const
{
rRect.SetSize(mxGraphicWindow->GetSizePixel());
}
void SmViewShell::SetZoomFactor( const Fraction &rX, const Fraction &rY )
{
const Fraction &rFrac = std::min(rX, rY);
mxGraphicWindow->SetZoom(sal::static_int_cast<sal_uInt16>(tools::Long(rFrac * Fraction( 100, 1 ))));
//To avoid rounding errors base class regulates crooked values too
//if necessary
SfxViewShell::SetZoomFactor( rX, rY );
}
Size SmViewShell::GetTextLineSize(OutputDevice const & rDevice, const OUString& rLine)
{
Size aSize(rDevice.GetTextWidth(rLine), rDevice.GetTextHeight());
const tools::Long nTabPos = rLine.isEmpty() ? 0 : rDevice.approximate_digit_width() * 8;
if (nTabPos)
{
aSize.setWidth( 0 );
sal_Int32 nPos = 0;
do
{
if (nPos > 0)
aSize.setWidth( ((aSize.Width() / nTabPos) + 1) * nTabPos );
const OUString aText = rLine.getToken(0, '\t', nPos);
aSize.AdjustWidth(rDevice.GetTextWidth(aText) );
}
while (nPos >= 0);
}
return aSize;
}
Size SmViewShell::GetTextSize(OutputDevice const & rDevice, std::u16string_view rText, tools::Long MaxWidth)
{
Size aSize;
Size aTextSize;
if (rText.empty())
return aTextSize;
sal_Int32 nPos = 0;
do
{
OUString aLine( o3tl::getToken(rText, 0, '\n', nPos) );
aLine = aLine.replaceAll("\r", "");
aSize = GetTextLineSize(rDevice, aLine);
if (aSize.Width() > MaxWidth)
{
do
{
OUString aText;
sal_Int32 m = aLine.getLength();
sal_Int32 nLen = m;
for (sal_Int32 n = 0; n < nLen; n++)
{
sal_Unicode cLineChar = aLine[n];
if ((cLineChar == ' ') || (cLineChar == '\t'))
{
aText = aLine.copy(0, n);
if (GetTextLineSize(rDevice, aText).Width() < MaxWidth)
m = n;
else
break;
}
}
aText = aLine.copy(0, m);
aLine = aLine.replaceAt(0, m, u"");
aSize = GetTextLineSize(rDevice, aText);
aTextSize.AdjustHeight(aSize.Height() );
aTextSize.setWidth( std::clamp(aSize.Width(), aTextSize.Width(), MaxWidth) );
aLine = comphelper::string::stripStart(aLine, ' ');
aLine = comphelper::string::stripStart(aLine, '\t');
aLine = comphelper::string::stripStart(aLine, ' ');
}
while (!aLine.isEmpty());
}
else
{
aTextSize.AdjustHeight(aSize.Height() );
aTextSize.setWidth( std::max(aTextSize.Width(), aSize.Width()) );
}
}
while (nPos >= 0);
return aTextSize;
}
void SmViewShell::DrawTextLine(OutputDevice& rDevice, const Point& rPosition, const OUString& rLine)
{
Point aPoint(rPosition);
const tools::Long nTabPos = rLine.isEmpty() ? 0 : rDevice.approximate_digit_width() * 8;
if (nTabPos)
{
sal_Int32 nPos = 0;
do
{
if (nPos > 0)
aPoint.setX( ((aPoint.X() / nTabPos) + 1) * nTabPos );
OUString aText = rLine.getToken(0, '\t', nPos);
rDevice.DrawText(aPoint, aText);
aPoint.AdjustX(rDevice.GetTextWidth(aText) );
}
while ( nPos >= 0 );
}
else
rDevice.DrawText(aPoint, rLine);
}
void SmViewShell::DrawText(OutputDevice& rDevice, const Point& rPosition, std::u16string_view rText, sal_uInt16 MaxWidth)
{
if (rText.empty())
return;
Point aPoint(rPosition);
Size aSize;
sal_Int32 nPos = 0;
do
{
OUString aLine( o3tl::getToken(rText, 0, '\n', nPos) );
aLine = aLine.replaceAll("\r", "");
aSize = GetTextLineSize(rDevice, aLine);
if (aSize.Width() > MaxWidth)
{
do
{
OUString aText;
sal_Int32 m = aLine.getLength();
sal_Int32 nLen = m;
for (sal_Int32 n = 0; n < nLen; n++)
{
sal_Unicode cLineChar = aLine[n];
if ((cLineChar == ' ') || (cLineChar == '\t'))
{
aText = aLine.copy(0, n);
if (GetTextLineSize(rDevice, aText).Width() < MaxWidth)
m = n;
else
break;
}
}
aText = aLine.copy(0, m);
aLine = aLine.replaceAt(0, m, u"");
DrawTextLine(rDevice, aPoint, aText);
aPoint.AdjustY(aSize.Height() );
aLine = comphelper::string::stripStart(aLine, ' ');
aLine = comphelper::string::stripStart(aLine, '\t');
aLine = comphelper::string::stripStart(aLine, ' ');
}
while (GetTextLineSize(rDevice, aLine).Width() > MaxWidth);
// print the remaining text
if (!aLine.isEmpty())
{
DrawTextLine(rDevice, aPoint, aLine);
aPoint.AdjustY(aSize.Height() );
}
}
else
{
DrawTextLine(rDevice, aPoint, aLine);
aPoint.AdjustY(aSize.Height() );
}
}
while ( nPos >= 0 );
}
void SmViewShell::Impl_Print(OutputDevice &rOutDev, const SmPrintUIOptions &rPrintUIOptions, tools::Rectangle aOutRect )
{
const bool bIsPrintTitle = rPrintUIOptions.getBoolValue( PRTUIOPT_TITLE_ROW, true );
const bool bIsPrintFrame = rPrintUIOptions.getBoolValue( PRTUIOPT_BORDER, true );
const bool bIsPrintFormulaText = rPrintUIOptions.getBoolValue( PRTUIOPT_FORMULA_TEXT, true );
SmPrintSize ePrintSize( static_cast< SmPrintSize >( rPrintUIOptions.getIntValue( PRTUIOPT_PRINT_FORMAT, PRINT_SIZE_NORMAL ) ));
const sal_uInt16 nZoomFactor = static_cast< sal_uInt16 >(rPrintUIOptions.getIntValue( PRTUIOPT_PRINT_SCALE, 100 ));
rOutDev.Push();
rOutDev.SetLineColor( COL_BLACK );
// output text on top
if (bIsPrintTitle)
{
Size aSize600 (0, 600);
Size aSize650 (0, 650);
vcl::Font aFont(FAMILY_DONTKNOW, aSize600);
aFont.SetAlignment(ALIGN_TOP);
aFont.SetWeight(WEIGHT_BOLD);
aFont.SetFontSize(aSize650);
aFont.SetColor( COL_BLACK );
rOutDev.SetFont(aFont);
Size aTitleSize (GetTextSize(rOutDev, GetDoc()->GetTitle(), aOutRect.GetWidth() - 200));
aFont.SetWeight(WEIGHT_NORMAL);
aFont.SetFontSize(aSize600);
rOutDev.SetFont(aFont);
Size aDescSize (GetTextSize(rOutDev, GetDoc()->GetComment(), aOutRect.GetWidth() - 200));
if (bIsPrintFrame)
rOutDev.DrawRect(tools::Rectangle(aOutRect.TopLeft(),
Size(aOutRect.GetWidth(), 100 + aTitleSize.Height() + 200 + aDescSize.Height() + 100)));
aOutRect.AdjustTop(200 );
// output title
aFont.SetWeight(WEIGHT_BOLD);
aFont.SetFontSize(aSize650);
rOutDev.SetFont(aFont);
Point aPoint(aOutRect.Left() + (aOutRect.GetWidth() - aTitleSize.Width()) / 2,
aOutRect.Top());
DrawText(rOutDev, aPoint, GetDoc()->GetTitle(),
sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
aOutRect.AdjustTop(aTitleSize.Height() + 200 );
// output description
aFont.SetWeight(WEIGHT_NORMAL);
aFont.SetFontSize(aSize600);
rOutDev.SetFont(aFont);
aPoint.setX( aOutRect.Left() + (aOutRect.GetWidth() - aDescSize.Width()) / 2 );
aPoint.setY( aOutRect.Top() );
DrawText(rOutDev, aPoint, GetDoc()->GetComment(),
sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
aOutRect.AdjustTop(aDescSize.Height() + 300 );
}
// output text on bottom
if (bIsPrintFormulaText)
{
vcl::Font aFont(FAMILY_DONTKNOW, Size(0, 600));
aFont.SetAlignment(ALIGN_TOP);
aFont.SetColor( COL_BLACK );
// get size
rOutDev.SetFont(aFont);
Size aSize (GetTextSize(rOutDev, GetDoc()->GetText(), aOutRect.GetWidth() - 200));
aOutRect.AdjustBottom( -(aSize.Height() + 600) );
if (bIsPrintFrame)
rOutDev.DrawRect(tools::Rectangle(aOutRect.BottomLeft(),
Size(aOutRect.GetWidth(), 200 + aSize.Height() + 200)));
Point aPoint (aOutRect.Left() + (aOutRect.GetWidth() - aSize.Width()) / 2,
aOutRect.Bottom() + 300);
DrawText(rOutDev, aPoint, GetDoc()->GetText(),
sal::static_int_cast< sal_uInt16 >(aOutRect.GetWidth() - 200));
aOutRect.AdjustBottom( -200 );
}
if (bIsPrintFrame)
rOutDev.DrawRect(aOutRect);
aOutRect.AdjustTop(100 );
aOutRect.AdjustLeft(100 );
aOutRect.AdjustBottom( -100 );
aOutRect.AdjustRight( -100 );
Size aSize (GetDoc()->GetSize());
MapMode OutputMapMode;
// PDF export should always use PRINT_SIZE_NORMAL ...
if (!rPrintUIOptions.getBoolValue( "IsPrinter" ) )
ePrintSize = PRINT_SIZE_NORMAL;
switch (ePrintSize)
{
case PRINT_SIZE_NORMAL:
OutputMapMode = MapMode(MapUnit::Map100thMM);
break;
case PRINT_SIZE_SCALED:
if (!aSize.IsEmpty())
{
Size OutputSize (rOutDev.LogicToPixel(Size(aOutRect.GetWidth(),
aOutRect.GetHeight()), MapMode(MapUnit::Map100thMM)));
Size GraphicSize (rOutDev.LogicToPixel(aSize, MapMode(MapUnit::Map100thMM)));
sal_uInt16 nZ = sal::static_int_cast<sal_uInt16>(std::min(tools::Long(Fraction(OutputSize.Width() * 100, GraphicSize.Width())),
tools::Long(Fraction(OutputSize.Height() * 100, GraphicSize.Height()))));
nZ -= 10;
Fraction aFraction (std::clamp(nZ, MINZOOM, sal_uInt16(100)));
OutputMapMode = MapMode(MapUnit::Map100thMM, Point(), aFraction, aFraction);
}
else
OutputMapMode = MapMode(MapUnit::Map100thMM);
break;
case PRINT_SIZE_ZOOMED:
{
Fraction aFraction( nZoomFactor, 100 );
OutputMapMode = MapMode(MapUnit::Map100thMM, Point(), aFraction, aFraction);
break;
}
}
aSize = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aSize, OutputMapMode),
MapMode(MapUnit::Map100thMM));
Point aPos (aOutRect.Left() + (aOutRect.GetWidth() - aSize.Width()) / 2,
aOutRect.Top() + (aOutRect.GetHeight() - aSize.Height()) / 2);
aPos = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aPos, MapMode(MapUnit::Map100thMM)),
OutputMapMode);
aOutRect = rOutDev.PixelToLogic(rOutDev.LogicToPixel(aOutRect, MapMode(MapUnit::Map100thMM)),
OutputMapMode);
rOutDev.SetMapMode(OutputMapMode);
rOutDev.SetClipRegion(vcl::Region(aOutRect));
GetDoc()->DrawFormula(rOutDev, aPos);
rOutDev.SetClipRegion();
rOutDev.Pop();
}
SfxPrinter* SmViewShell::GetPrinter(bool bCreate)
{
SmDocShell* pDoc = GetDoc();
if (pDoc->HasPrinter() || bCreate)
return pDoc->GetPrinter();
return nullptr;
}
sal_uInt16 SmViewShell::SetPrinter(SfxPrinter *pNewPrinter, SfxPrinterChangeFlags nDiffFlags )
{
SfxPrinter *pOld = GetDoc()->GetPrinter();
if ( pOld && pOld->IsPrinting() )
return SFX_PRINTERROR_BUSY;
if ((nDiffFlags & SfxPrinterChangeFlags::PRINTER) == SfxPrinterChangeFlags::PRINTER)
GetDoc()->SetPrinter( pNewPrinter );
if ((nDiffFlags & SfxPrinterChangeFlags::OPTIONS) == SfxPrinterChangeFlags::OPTIONS)
{
SmModule *pp = SM_MOD();
pp->GetConfig()->ItemSetToConfig(pNewPrinter->GetOptions());
}
return 0;
}
bool SmViewShell::HasPrintOptionsPage() const
{
return true;
}
std::unique_ptr<SfxTabPage> SmViewShell::CreatePrintOptionsPage(weld::Container* pPage, weld::DialogController* pController,
const SfxItemSet &rOptions)
{
return SmPrintOptionsTabPage::Create(pPage, pController, rOptions);
}
SmEditWindow *SmViewShell::GetEditWindow()
{
SmCmdBoxWrapper* pWrapper = static_cast<SmCmdBoxWrapper*>(
GetViewFrame()->GetChildWindow(SmCmdBoxWrapper::GetChildWindowId()));
if (pWrapper != nullptr)
{
SmEditWindow& rEditWin = pWrapper->GetEditWindow();
return &rEditWin;
}
return nullptr;
}
SmElementsDockingWindow* SmViewShell::GetDockingWindow()
{
auto eldockwinwrap = GetViewFrame()->GetChildWindow(SmElementsDockingWindowWrapper::GetChildWindowId());
if(eldockwinwrap)
return dynamic_cast<SmElementsDockingWindow*>(eldockwinwrap->GetWindow());
else
return nullptr;
}
void SmViewShell::SetStatusText(const OUString& rText)
{
maStatusText = rText;
GetViewFrame()->GetBindings().Invalidate(SID_TEXTSTATUS);
}
void SmViewShell::ShowError(const SmErrorDesc* pErrorDesc)
{
assert(GetDoc());
if (pErrorDesc || nullptr != (pErrorDesc = GetDoc()->GetParser()->GetError()) )
{
SetStatusText( pErrorDesc->m_aText );
GetEditWindow()->MarkError( Point( pErrorDesc->m_pNode->GetColumn(),
pErrorDesc->m_pNode->GetRow()));
}
}
void SmViewShell::NextError()
{
assert(GetDoc());
const SmErrorDesc *pErrorDesc = GetDoc()->GetParser()->NextError();
if (pErrorDesc)
ShowError( pErrorDesc );
}
void SmViewShell::PrevError()
{
assert(GetDoc());
const SmErrorDesc *pErrorDesc = GetDoc()->GetParser()->PrevError();
if (pErrorDesc)
ShowError( pErrorDesc );
}
void SmViewShell::Insert( SfxMedium& rMedium )
{
SmDocShell *pDoc = GetDoc();
bool bRet = false;
uno::Reference <embed::XStorage> xStorage = rMedium.GetStorage();
if (xStorage.is() && xStorage->getElementNames().hasElements())
{
if (xStorage->hasByName("content.xml"))
{
// is this a fabulous math package ?
Reference<css::frame::XModel> xModel(pDoc->GetModel());
SmXMLImportWrapper aEquation(xModel); //!! modifies the result of pDoc->GetText() !!
bRet = ERRCODE_NONE == aEquation.Import(rMedium);
}
}
if (!bRet)
return;
OUString aText = pDoc->GetText();
SmEditWindow *pEditWin = GetEditWindow();
if (pEditWin)
pEditWin->InsertText( aText );
else
{
SAL_WARN( "starmath", "EditWindow missing" );
}
pDoc->Parse();
pDoc->SetModified();
SfxBindings &rBnd = GetViewFrame()->GetBindings();
rBnd.Invalidate(SID_GRAPHIC_SM);
rBnd.Invalidate(SID_TEXT);
}
void SmViewShell::InsertFrom(SfxMedium &rMedium)
{
bool bSuccess = false;
SmDocShell* pDoc = GetDoc();
SvStream* pStream = rMedium.GetInStream();
if (pStream)
{
const OUString& rFltName = rMedium.GetFilter()->GetFilterName();
if ( rFltName == MATHML_XML )
{
Reference<css::frame::XModel> xModel(pDoc->GetModel());
SmXMLImportWrapper aEquation(xModel); //!! modifies the result of pDoc->GetText() !!
bSuccess = ERRCODE_NONE == aEquation.Import(rMedium);
}
}
if (!bSuccess)
return;
OUString aText = pDoc->GetText();
SmEditWindow *pEditWin = GetEditWindow();
if (pEditWin)
pEditWin->InsertText(aText);
else
SAL_WARN( "starmath", "EditWindow missing" );
pDoc->Parse();
pDoc->SetModified();
SfxBindings& rBnd = GetViewFrame()->GetBindings();
rBnd.Invalidate(SID_GRAPHIC_SM);
rBnd.Invalidate(SID_TEXT);
}
void SmViewShell::Execute(SfxRequest& rReq)
{
SmEditWindow *pWin = GetEditWindow();
switch (rReq.GetSlot())
{
case SID_FORMULACURSOR:
{
SmModule *pp = SM_MOD();
const SfxItemSet *pArgs = rReq.GetArgs();
const SfxPoolItem *pItem;
bool bVal;
if ( pArgs &&
SfxItemState::SET == pArgs->GetItemState( SID_FORMULACURSOR, false, &pItem))
bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
else
bVal = !pp->GetConfig()->IsShowFormulaCursor();
pp->GetConfig()->SetShowFormulaCursor(bVal);
if (!IsInlineEditEnabled())
GetGraphicWidget().ShowCursor(bVal);
break;
}
case SID_DRAW:
if (pWin)
{
GetDoc()->SetText( pWin->GetText() );
SetStatusText(OUString());
ShowError( nullptr );
GetDoc()->Repaint();
}
break;
case SID_ZOOM_OPTIMAL:
mxGraphicWindow->ZoomToFitInWindow();
break;
case SID_ZOOMIN:
mxGraphicWindow->SetZoom(mxGraphicWindow->GetZoom() + 25);
break;
case SID_ZOOMOUT:
SAL_WARN_IF( mxGraphicWindow->GetZoom() < 25, "starmath", "incorrect sal_uInt16 argument" );
mxGraphicWindow->SetZoom(mxGraphicWindow->GetZoom() - 25);
break;
case SID_COPYOBJECT:
{
//TODO/LATER: does not work because of UNO Tunneling - will be fixed later
Reference< datatransfer::XTransferable > xTrans( GetDoc()->GetModel(), uno::UNO_QUERY );
if( xTrans.is() )
{
auto pTrans = comphelper::getFromUnoTunnel<TransferableHelper>(xTrans);
if (pTrans)
{
SmEditWindow *pEditWin = GetEditWindow();
pTrans->CopyToClipboard(pEditWin->GetClipboard());
}
}
}
break;
case SID_PASTEOBJECT:
{
SmEditWindow *pEditWin = GetEditWindow();
TransferableDataHelper aData(TransferableDataHelper::CreateFromClipboard(pEditWin->GetClipboard()));
uno::Reference < io::XInputStream > xStrm;
SotClipboardFormatId nId;
if( aData.GetTransferable().is() &&
( aData.HasFormat( nId = SotClipboardFormatId::EMBEDDED_OBJ ) ||
(aData.HasFormat( SotClipboardFormatId::OBJECTDESCRIPTOR ) &&
aData.HasFormat( nId = SotClipboardFormatId::EMBED_SOURCE ))))
xStrm = aData.GetInputStream(nId, OUString());
if (xStrm.is())
{
try
{
uno::Reference < embed::XStorage > xStorage =
::comphelper::OStorageHelper::GetStorageFromInputStream( xStrm, ::comphelper::getProcessComponentContext() );
SfxMedium aMedium( xStorage, OUString() );
Insert( aMedium );
GetDoc()->UpdateText();
}
catch (uno::Exception &)
{
SAL_WARN( "starmath", "SmViewShell::Execute (SID_PASTEOBJECT): failed to get storage from input stream" );
}
}
}
break;
case SID_CUT:
if (pWin)
pWin->Cut();
break;
case SID_COPY:
if (pWin)
{
if (pWin->IsAllSelected())
{
GetViewFrame()->GetDispatcher()->ExecuteList(
SID_COPYOBJECT, SfxCallMode::RECORD,
{ new SfxVoidItem(SID_COPYOBJECT) });
}
else
pWin->Copy();
}
break;
case SID_PASTE:
{
bool bCallExec = nullptr == pWin;
if( !bCallExec )
{
SmEditWindow *pEditWin = GetEditWindow();
TransferableDataHelper aDataHelper(
TransferableDataHelper::CreateFromClipboard(
pEditWin->GetClipboard()));
if( aDataHelper.GetTransferable().is() &&
aDataHelper.HasFormat( SotClipboardFormatId::STRING ))
pWin->Paste();
else
bCallExec = true;
}
if( bCallExec )
{
GetViewFrame()->GetDispatcher()->ExecuteList(
SID_PASTEOBJECT, SfxCallMode::RECORD,
{ new SfxVoidItem(SID_PASTEOBJECT) });
}
}
break;
case SID_DELETE:
if (pWin)
pWin->Delete();
break;
case SID_SELECT:
if (pWin)
pWin->SelectAll();
break;
case SID_INSERTCOMMANDTEXT:
{
const SfxStringItem& rItem = static_cast<const SfxStringItem&>(rReq.GetArgs()->Get(SID_INSERTCOMMANDTEXT));
if (pWin && (mbInsertIntoEditWindow || !IsInlineEditEnabled()))
{
pWin->InsertText(rItem.GetValue());
}
if (IsInlineEditEnabled() && (GetDoc() && !mbInsertIntoEditWindow))
{
GetDoc()->GetCursor().InsertCommandText(rItem.GetValue());
GetGraphicWidget().GrabFocus();
}
break;
}
case SID_INSERTSPECIAL:
{
const SfxStringItem& rItem =
static_cast<const SfxStringItem&>(rReq.GetArgs()->Get(SID_INSERTSPECIAL));
if (pWin && (mbInsertIntoEditWindow || !IsInlineEditEnabled()))
pWin->InsertText(rItem.GetValue());
if (IsInlineEditEnabled() && (GetDoc() && !mbInsertIntoEditWindow))
GetDoc()->GetCursor().InsertSpecial(rItem.GetValue());
break;
}
case SID_IMPORT_FORMULA:
{
mpRequest.reset(new SfxRequest( rReq ));
mpDocInserter.reset(new ::sfx2::DocumentInserter(pWin ? pWin->GetFrameWeld() : nullptr,
GetDoc()->GetFactory().GetFactoryName()));
mpDocInserter->StartExecuteModal( LINK( this, SmViewShell, DialogClosedHdl ) );
break;
}
case SID_IMPORT_MATHML_CLIPBOARD:
{
SmEditWindow *pEditWin = GetEditWindow();
TransferableDataHelper aDataHelper(TransferableDataHelper::CreateFromClipboard(pEditWin->GetClipboard()));
uno::Reference < io::XInputStream > xStrm;
if ( aDataHelper.GetTransferable().is() )
{
SotClipboardFormatId nId = SotClipboardFormatId::MATHML;
if (aDataHelper.HasFormat(nId))
{
xStrm = aDataHelper.GetInputStream(nId, "");
if (xStrm.is())
{
SfxMedium aClipboardMedium;
aClipboardMedium.GetItemSet(); //generate initial itemset, not sure if necessary
std::shared_ptr<const SfxFilter> pMathFilter =
SfxFilter::GetFilterByName(MATHML_XML);
aClipboardMedium.SetFilter(pMathFilter);
aClipboardMedium.setStreamToLoadFrom(xStrm, true /*bIsReadOnly*/);
InsertFrom(aClipboardMedium);
GetDoc()->UpdateText();
}
}
else
{
nId = SotClipboardFormatId::STRING;
if (aDataHelper.HasFormat(nId))
{
// In case of FORMAT_STRING no stream exists, need to generate one
OUString aString;
if (aDataHelper.GetString( nId, aString))
{
// tdf#117091 force xml declaration to exist
if (!aString.startsWith("<?xml"))
aString = "<?xml version=\"1.0\"?>\n" + aString;
SfxMedium aClipboardMedium;
aClipboardMedium.GetItemSet(); //generates initial itemset, not sure if necessary
std::shared_ptr<const SfxFilter> pMathFilter =
SfxFilter::GetFilterByName(MATHML_XML);
aClipboardMedium.SetFilter(pMathFilter);
SvMemoryStream aStrm( const_cast<sal_Unicode *>(aString.getStr()), aString.getLength() * sizeof(sal_Unicode), StreamMode::READ);
uno::Reference<io::XInputStream> xStrm2( new ::utl::OInputStreamWrapper(aStrm) );
aClipboardMedium.setStreamToLoadFrom(xStrm2, true /*bIsReadOnly*/);
InsertFrom(aClipboardMedium);
GetDoc()->UpdateText();
}
}
}
}
break;
}
case SID_NEXTERR:
NextError();
if (pWin)
pWin->GrabFocus();
break;
case SID_PREVERR:
PrevError();
if (pWin)
pWin->GrabFocus();
break;
case SID_NEXTMARK:
if (pWin)
{
pWin->SelNextMark();
pWin->GrabFocus();
}
break;
case SID_PREVMARK:
if (pWin)
{
pWin->SelPrevMark();
pWin->GrabFocus();
}
break;
case SID_TEXTSTATUS:
{
if (rReq.GetArgs() != nullptr)
{
const SfxStringItem& rItem =
static_cast<const SfxStringItem&>(rReq.GetArgs()->Get(SID_TEXTSTATUS));
SetStatusText(rItem.GetValue());
}
break;
}
case SID_GETEDITTEXT:
if (pWin && !pWin->GetText().isEmpty())
GetDoc()->SetText( pWin->GetText() );
break;
case SID_ATTR_ZOOM:
{
if ( !GetViewFrame()->GetFrame().IsInPlace() )
{
const SfxItemSet *pSet = rReq.GetArgs();
if ( pSet )
{
ZoomByItemSet(pSet);
}
else
{
SfxItemSetFixed<SID_ATTR_ZOOM, SID_ATTR_ZOOM> aSet( SmDocShell::GetPool() );
aSet.Put( SvxZoomItem( SvxZoomType::PERCENT, mxGraphicWindow->GetZoom()));
SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
ScopedVclPtr<AbstractSvxZoomDialog> xDlg(pFact->CreateSvxZoomDialog(GetViewFrame()->GetWindow().GetFrameWeld(), aSet));
xDlg->SetLimits( MINZOOM, MAXZOOM );
if (xDlg->Execute() != RET_CANCEL)
ZoomByItemSet(xDlg->GetOutputItemSet());
}
}
}
break;
case SID_ATTR_ZOOMSLIDER:
{
const SfxItemSet *pArgs = rReq.GetArgs();
const SfxPoolItem* pItem;
if ( pArgs && SfxItemState::SET == pArgs->GetItemState(SID_ATTR_ZOOMSLIDER, true, &pItem ) )
{
const sal_uInt16 nCurrentZoom = static_cast<const SvxZoomSliderItem *>(pItem)->GetValue();
mxGraphicWindow->SetZoom(nCurrentZoom);
}
}
break;
case SID_ELEMENTSDOCKINGWINDOW:
{
GetViewFrame()->ToggleChildWindow( SmElementsDockingWindowWrapper::GetChildWindowId() );
GetViewFrame()->GetBindings().Invalidate( SID_ELEMENTSDOCKINGWINDOW );
rReq.Ignore ();
}
break;
case SID_UNICODE_NOTATION_TOGGLE:
{
EditEngine* pEditEngine = nullptr;
if( pWin )
pEditEngine = pWin->GetEditEngine();
EditView* pEditView = nullptr;
if( pEditEngine )
pEditView = pEditEngine->GetView();
if( pEditView )
{
const OUString sInput = pEditView->GetSurroundingText();
ESelection aSel( pWin->GetSelection() );
if ( aSel.nStartPos > aSel.nEndPos )
aSel.nEndPos = aSel.nStartPos;
//calculate a valid end-position by reading logical characters
sal_Int32 nUtf16Pos=0;
while( (nUtf16Pos < sInput.getLength()) && (nUtf16Pos < aSel.nEndPos) )
{
sInput.iterateCodePoints(&nUtf16Pos);
if( nUtf16Pos > aSel.nEndPos )
aSel.nEndPos = nUtf16Pos;
}
ToggleUnicodeCodepoint aToggle;
while( nUtf16Pos && aToggle.AllowMoreInput( sInput[nUtf16Pos-1]) )
--nUtf16Pos;
const OUString sReplacement = aToggle.ReplacementString();
if( !sReplacement.isEmpty() )
{
pEditView->SetSelection( aSel );
pEditEngine->UndoActionStart(EDITUNDO_REPLACEALL);
aSel.nStartPos = aSel.nEndPos - aToggle.StringToReplace().getLength();
pWin->SetSelection( aSel );
pEditView->InsertText( sReplacement, true );
pEditEngine->UndoActionEnd();
pWin->Flush();
}
}
}
break;
case SID_SYMBOLS_CATALOGUE:
{
// get device used to retrieve the FontList
SmDocShell *pDoc = GetDoc();
OutputDevice *pDev = pDoc->GetPrinter();
if (!pDev || pDev->GetFontFaceCollectionCount() == 0)
pDev = &SM_MOD()->GetDefaultVirtualDev();
SAL_WARN_IF( !pDev, "starmath", "device for font list missing" );
SmModule *pp = SM_MOD();
SmSymbolDialog aDialog(pWin ? pWin->GetFrameWeld() : nullptr, pDev, pp->GetSymbolManager(), *this);
aDialog.run();
}
break;
}
rReq.Done();
}
void SmViewShell::GetState(SfxItemSet &rSet)
{
SfxWhichIter aIter(rSet);
SmEditWindow *pEditWin = GetEditWindow();
for (sal_uInt16 nWh = aIter.FirstWhich(); nWh != 0; nWh = aIter.NextWhich())
{
switch (nWh)
{
case SID_CUT:
case SID_COPY:
case SID_DELETE:
if (! pEditWin || ! pEditWin->IsSelected())
rSet.DisableItem(nWh);
break;
case SID_PASTE:
if (pEditWin)
{
TransferableDataHelper aDataHelper(
TransferableDataHelper::CreateFromClipboard(
pEditWin->GetClipboard()) );
mbPasteState = aDataHelper.GetTransferable().is() &&
( aDataHelper.HasFormat( SotClipboardFormatId::STRING ) ||
aDataHelper.HasFormat( SotClipboardFormatId::EMBEDDED_OBJ ) ||
(aDataHelper.HasFormat( SotClipboardFormatId::OBJECTDESCRIPTOR )
&& aDataHelper.HasFormat( SotClipboardFormatId::EMBED_SOURCE )));
}
if( !mbPasteState )
rSet.DisableItem( nWh );
break;
case SID_ATTR_ZOOM:
rSet.Put(SvxZoomItem( SvxZoomType::PERCENT, mxGraphicWindow->GetZoom()));
[[fallthrough]];
case SID_ZOOMIN:
case SID_ZOOMOUT:
case SID_ZOOM_OPTIMAL:
if ( GetViewFrame()->GetFrame().IsInPlace() )
rSet.DisableItem( nWh );
break;
case SID_ATTR_ZOOMSLIDER :
{
const sal_uInt16 nCurrentZoom = mxGraphicWindow->GetZoom();
SvxZoomSliderItem aZoomSliderItem( nCurrentZoom, MINZOOM, MAXZOOM );
aZoomSliderItem.AddSnappingPoint( 100 );
rSet.Put( aZoomSliderItem );
}
break;
case SID_NEXTERR:
case SID_PREVERR:
case SID_NEXTMARK:
case SID_PREVMARK:
case SID_DRAW:
case SID_SELECT:
if (! pEditWin || pEditWin->IsEmpty())
rSet.DisableItem(nWh);
break;
case SID_TEXTSTATUS:
{
rSet.Put(SfxStringItem(nWh, maStatusText));
}
break;
case SID_FORMULACURSOR:
{
SmModule *pp = SM_MOD();
rSet.Put(SfxBoolItem(nWh, pp->GetConfig()->IsShowFormulaCursor()));
}
break;
case SID_ELEMENTSDOCKINGWINDOW:
{
bool bState = false;
SfxChildWindow *pChildWnd = GetViewFrame()->
GetChildWindow( SmElementsDockingWindowWrapper::GetChildWindowId() );
if (pChildWnd && pChildWnd->GetWindow()->IsVisible())
bState = true;
rSet.Put(SfxBoolItem(SID_ELEMENTSDOCKINGWINDOW, bState));
}
break;
}
}
}
namespace
{
class SmController : public SfxBaseController
{
public:
SmController(SfxViewShell& rViewShell)
: SfxBaseController(&rViewShell)
, mpSelectionChangeHandler(new svx::sidebar::SelectionChangeHandler(
GetContextName, this, vcl::EnumContext::Context::Math))
{
mpSelectionChangeHandler->Connect();
rViewShell.SetContextName(GetContextName());
}
~SmController() { mpSelectionChangeHandler->Disconnect(); }
// css::frame::XController
void SAL_CALL attachFrame(const css::uno::Reference<css::frame::XFrame>& xFrame) override
{
SfxBaseController::attachFrame(xFrame);
mpSelectionChangeHandler->selectionChanged({}); // Installs the correct context
}
private:
static OUString GetContextName() { return "Math"; } // Static constant for now
rtl::Reference<svx::sidebar::SelectionChangeHandler> mpSelectionChangeHandler;
};
}
SmViewShell::SmViewShell(SfxViewFrame *pFrame_, SfxViewShell *)
: SfxViewShell(pFrame_, SfxViewShellFlags::HAS_PRINTOPTIONS)
, mxGraphicWindow(VclPtr<SmGraphicWindow>::Create(*this))
, maGraphicController(mxGraphicWindow->GetGraphicWidget(), SID_GRAPHIC_SM, pFrame_->GetBindings())
, mbPasteState(false)
, mbInsertIntoEditWindow(false)
{
SetStatusText(OUString());
SetWindow(mxGraphicWindow.get());
SfxShell::SetName("SmView");
SfxShell::SetUndoManager( &GetDoc()->GetEditEngine().GetUndoManager() );
SetController(new SmController(*this));
}
SmViewShell::~SmViewShell()
{
//!! this view shell is not active anymore !!
// Thus 'SmGetActiveView' will give a 0 pointer.
// Thus we need to supply this view as argument
SmEditWindow *pEditWin = GetEditWindow();
if (pEditWin)
pEditWin->DeleteEditView();
mxGraphicWindow.disposeAndClear();
}
void SmViewShell::Deactivate( bool bIsMDIActivate )
{
SmEditWindow *pEdit = GetEditWindow();
if ( pEdit )
pEdit->Flush();
SfxViewShell::Deactivate( bIsMDIActivate );
}
void SmViewShell::Activate( bool bIsMDIActivate )
{
SfxViewShell::Activate( bIsMDIActivate );
if (comphelper::LibreOfficeKit::isActive())
{
// In LOK, activate in-place editing
GetGraphicWidget().GrabFocus();
}
else if (SmEditWindow *pEdit = GetEditWindow())
{
//! Since there is no way to be informed if a "drag and drop"
//! event has taken place, we call SetText here in order to
//! synchronize the GraphicWindow display with the text in the
//! EditEngine.
SmDocShell *pDoc = GetDoc();
pDoc->SetText( pDoc->GetEditEngine().GetText() );
if ( bIsMDIActivate )
pEdit->GrabFocus();
}
}
IMPL_LINK( SmViewShell, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
{
assert(_pFileDlg && "SmViewShell::DialogClosedHdl(): no file dialog");
assert(mpDocInserter && "ScDocShell::DialogClosedHdl(): no document inserter");
if ( ERRCODE_NONE == _pFileDlg->GetError() )
{
std::unique_ptr<SfxMedium> pMedium = mpDocInserter->CreateMedium();
if ( pMedium )
{
if ( pMedium->IsStorage() )
Insert( *pMedium );
else
InsertFrom( *pMedium );
pMedium.reset();
SmDocShell* pDoc = GetDoc();
pDoc->UpdateText();
pDoc->ArrangeFormula();
pDoc->Repaint();
// adjust window, repaint, increment ModifyCount,...
GetViewFrame()->GetBindings().Invalidate(SID_GRAPHIC_SM);
}
}
mpRequest->SetReturnValue( SfxBoolItem( mpRequest->GetSlot(), true ) );
mpRequest->Done();
}
void SmViewShell::Notify( SfxBroadcaster& , const SfxHint& rHint )
{
switch( rHint.GetId() )
{
case SfxHintId::ModeChanged:
case SfxHintId::DocChanged:
GetViewFrame()->GetBindings().InvalidateAll(false);
break;
default:
break;
}
}
bool SmViewShell::IsInlineEditEnabled()
{
return comphelper::LibreOfficeKit::isActive()
|| officecfg::Office::Common::Misc::ExperimentalMode::get();
}
void SmViewShell::ZoomByItemSet(const SfxItemSet *pSet)
{
assert(pSet);
const SvxZoomItem &rZoom = pSet->Get(SID_ATTR_ZOOM);
switch( rZoom.GetType() )
{
case SvxZoomType::PERCENT:
mxGraphicWindow->SetZoom(sal::static_int_cast<sal_uInt16>(rZoom.GetValue ()));
break;
case SvxZoomType::OPTIMAL:
mxGraphicWindow->ZoomToFitInWindow();
break;
case SvxZoomType::PAGEWIDTH:
case SvxZoomType::WHOLEPAGE:
{
const MapMode aMap( MapUnit::Map100thMM );
SfxPrinter *pPrinter = GetPrinter( true );
tools::Rectangle OutputRect(Point(), pPrinter->GetOutputSize());
Size OutputSize(pPrinter->LogicToPixel(Size(OutputRect.GetWidth(),
OutputRect.GetHeight()), aMap));
Size GraphicSize(pPrinter->LogicToPixel(GetDoc()->GetSize(), aMap));
sal_uInt16 nZ = sal::static_int_cast<sal_uInt16>(std::min(tools::Long(Fraction(OutputSize.Width() * 100, GraphicSize.Width())),
tools::Long(Fraction(OutputSize.Height() * 100, GraphicSize.Height()))));
mxGraphicWindow->SetZoom(nZ);
break;
}
default:
break;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|