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
|
{%MainUnit ../forms.pp}
{******************************************************************************
TApplication
******************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
{ $define DebugHintWindow}
function FindApplicationComponent(const ComponentName: string): TComponent;
// Note: this function is used by TReader to auto rename forms to unique names.
begin
if Application.FindGlobalComponentEnabled then
begin
// ignore designer forms (the IDE registers its own functions to handle them)
Result:=Application.FindComponent(ComponentName);
if Result=nil then
Result:=Screen.FindNonDesignerForm(ComponentName);
if Result=nil then
Result:=Screen.FindNonDesignerDataModule(ComponentName);
end
else
Result:=nil;
//debugln('FindApplicationComponent ComponentName="',ComponentName,'" Result=',DbgSName(Result));
end;
function GetControlShortHint(Control: TControl): String;
begin
Result := '';
while (Control <> nil) and (Result = '') do
begin
Result := GetShortHint(Control.Hint);
Control := Control.Parent;
end;
end;
function GetHintControl(Control: TControl): TControl;
begin
Result := Control;
while (Result <> nil) and (not Result.ShowHint) do
Result := Result.Parent;
if (Result <> nil)and
([csDesigning, csDestroying, csLoading] * Result.ComponentState <> []) then
Result := nil;
end;
function GetHintInfoAt(CursorPos: TPoint): THintInfoAtMouse;
begin
Result.MousePos := CursorPos;
Result.Control := GetHintControl(FindControlAtPosition(Result.MousePos, True));
Result.ControlHasHint := Assigned(Result.Control) and Assigned(Application) and
Application.ShowHint and (GetCapture = 0) and
((GetKeyState(VK_LBUTTON) and $80) = 0) and
((GetKeyState(VK_MBUTTON) and $80) = 0) and
((GetKeyState(VK_RBUTTON) and $80) = 0);
if Result.ControlHasHint then
begin
// if there is a modal form, then don't show hints for other forms
if Assigned(Screen.FFocusedForm) and
(fsModal in Screen.FFocusedForm.FormState) and
(GetParentForm(Result.Control) <> Screen.FFocusedForm) then
Result.ControlHasHint := False;
end;
end;
// Callback function for SysUtils.OnGetApplicationName;
function GetApplicationName: string;
begin
if Assigned(Application) then
Result := Application.Title;
end;
{------------------------------------------------------------------------------
TApplication Constructor
------------------------------------------------------------------------------}
constructor TApplication.Create(AOwner: TComponent);
var
LangDefault, LangFallback: String;
begin
LCLProc.SendApplicationMessageFunction:=@SendApplicationMsg;
FShowButtonGlyphs := sbgAlways;
FShowMenuGlyphs := sbgAlways;
FMainForm := nil;
FModalLevel := 0;
FMouseControl := nil;
FHintColor := DefHintColor;
FHintPause := DefHintPause;
FHintShortCuts := True;
FHintShortPause := DefHintShortPause;
FHintHidePause := DefHintHidePause;
FHintHidePausePerChar := DefHintHidePausePerChar;
FMoveFormFocusToChildren := True;
FShowHint := true;
FShowMainForm := true;
FRestoreStayOnTop := nil;
FOnIdle := nil;
FIcon := TIcon.Create;
FIcon.OnChange := @IconChanged;
FNavigation := [anoTabToSelectNext,anoReturnForDefaultControl,
anoEscapeForCancelControl,anoF1ForHelp,anoArrowToSelectNextInParent];
FUpdateFormatSettings := True;
ApplicationActionComponent:=Self;
OnMenuPopupHandler:=@MenuPopupHandler;
System.InitCriticalSection(FAsyncCall.CritSec);
FFindGlobalComponentEnabled:=true;
RegisterFindGlobalComponentProc(@FindApplicationComponent);
{$ifndef wince}// remove ifdef when gettext is fixed
LCLGetLanguageIDs(LangDefault, LangFallback);
if LangDefault <> '' then
FBidiMode := Direction(LangDefault)
else
FBidiMode := Direction(LangFallback);
{$else}
FBidiMode := bdLeftToRight;
{$endif}
FMainFormOnTaskBar := False;
inherited Create(AOwner);
CaptureExceptions:=true;
FOldExitProc:=ExitProc;
ExitProc:=@BeforeFinalization;
OnGetApplicationName := @GetApplicationName;
end;
{------------------------------------------------------------------------------
TApplication Destructor
------------------------------------------------------------------------------}
destructor TApplication.Destroy;
var
HandlerType: TApplicationHandlerType;
begin
if Self=nil then
RaiseGDBException('TApplication.Destroy Self=nil');
Include(FFlags,AppDestroying);
if Assigned(FOnDestroy) then FOnDestroy(Self);
ExitProc:=FOldExitProc;
ProcessAsyncCallQueue;
if OnMenuPopupHandler=@MenuPopupHandler then
OnMenuPopupHandler:=nil;
// shutting down
CancelHint;
ShowHint := False;
// destroying
ApplicationActionComponent:=nil;
FreeThenNil(FIcon);
FreeIconHandles;
FreeThenNil(FRestoreStayOnTop);
for HandlerType:=Low(TApplicationHandlerType) to High(TApplicationHandlerType) do
FreeThenNil(FApplicationHandlers[HandlerType]);
UnregisterFindGlobalComponentProc(@FindApplicationComponent);
inherited Destroy;
Include(FFlags,AppDoNotCallAsyncQueue);
ProcessAsyncCallQueue;
System.DoneCriticalSection(FAsyncCall.CritSec);
// restore exception handling
CaptureExceptions:=false;
LCLProc.SendApplicationMessageFunction:=nil;
OnGetApplicationName := nil;
end;
procedure TApplication.ActivateHint(CursorPos: TPoint; CheckHintControlChange: Boolean);
var
Info: THintInfoAtMouse;
HintControlChanged: Boolean;
begin
Info := GetHintInfoAt(CursorPos);
{$ifdef DebugHintWindow}
DebugLn('TApplication.DoOnMouseMove Info.ControlHasHint=',dbgs(Info.ControlHasHint),' Type=',dbgs(ord(FHintTimerType)),' FHintControl=',DbgSName(FHintControl),' Info.Control=',DbgSName(Info.Control));
{$endif}
HintControlChanged := not CheckHintControlChange or (FHintControl <> Info.Control);
if Info.ControlHasHint then
begin
if HintControlChanged then
begin
StopHintTimer;
HideHint;
FHintControl := Info.Control;
FHintRect := FHintControl.BoundsRect;
end;
case FHintTimerType of
ahttNone, ahttHideHint:
//react only if the hint control changed or if the mouse leave the previously set hint rect
if HintControlChanged or (not PtInRect(FHintRect, FHintControl.ScreenToClient(Info.MousePos))) then
begin
//if a hint is visible immediately query the app to show a new hint...
if FHintTimerType = ahttHideHint then
ShowHintWindow(Info);
//...if there's no hint window visible at this point than schedule a new query
if (FHintTimerType = ahttNone) or (FHintWindow = nil) or not FHintWindow.Visible then
StartHintTimer(HintPause, ahttShowHint);
end;
ahttShowHint:
StartHintTimer(HintPause, ahttShowHint);
end;
end
else
CancelHint;
end;
{------------------------------------------------------------------------------
TApplication BringToFront
------------------------------------------------------------------------------}
procedure TApplication.BringToFront;
begin
WidgetSet.AppBringToFront;
end;
{------------------------------------------------------------------------------
TApplication Messagebox
------------------------------------------------------------------------------}
function TApplication.MessageBox(Text, Caption: PChar; Flags: Longint) : Integer;
begin
if Assigned(MessageBoxFunction) then
Result:=MessageBoxFunction(Text,Caption,Flags)
else begin
DebugLn('WARNING: TApplication.MessageBox: no MessageBoxFunction');
DebugLn(' Caption="',Caption,'"');
DebugLn(' Text="',Text,'"');
DebugLn(' Flags=',DbgS(Flags));
Result:=0;
end;
end;
{------------------------------------------------------------------------------
TApplication GetExename
------------------------------------------------------------------------------}
function TApplication.GetExename: String;
Begin
Result := ParamStrUTF8(0);
end;
function TApplication.GetMainFormHandle: HWND;
var
i: Integer;
begin
Result := 0;
if Assigned(OnGetMainFormHandle) then
OnGetMainFormHandle(Result);
i := FApplicationHandlers[ahtGetMainFormHandle].Count;
while (Result = 0) and FApplicationHandlers[ahtGetMainFormHandle].NextDownIndex(i) do
TGetHandleEvent(FApplicationHandlers[ahtGetMainFormHandle][i])(Result);
if (Result = 0) and Assigned(MainForm) then
Result := MainForm.Handle;
end;
{------------------------------------------------------------------------------
TApplication Notification "Performs Application Level Operations"
------------------------------------------------------------------------------}
procedure TApplication.Notification(AComponent : TComponent;
Operation : TOperation);
begin
if Operation = opRemove then begin
FLastMouseControlValid:=false;
if AComponent=FMouseControl then
FMouseControl:=nil;
if AComponent=FCreatingForm then
FCreatingForm:=nil;
if AComponent=FHintWindow then
FHintWindow:=nil;
if AComponent=FHintTimer then
FHintTimer:=nil;
if FComponentsToRelease<>nil then
FComponentsToRelease.Remove(AComponent);
if FComponentsReleasing<>nil then
FComponentsReleasing.Remove(AComponent);
if AComponent = MainForm then begin
FMainForm:= nil;
Terminate;
end;
end;
inherited Notification(AComponent,Operation);
end;
{------------------------------------------------------------------------------
Method: TApplication.ControlDestroyed
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
procedure TApplication.ControlDestroyed(AControl: TControl);
begin
FLastMouseControlValid:=false;
if AControl=FMouseControl then FMouseControl:=nil;
if AControl = MainForm then FMainForm:= nil;
if AControl = FCreatingForm then FCreatingForm:= nil;
if Screen.FActiveControl = AControl then Screen.FActiveControl := nil;
if Screen.FActiveCustomForm = AControl then
begin
Screen.FActiveCustomForm := nil;
Screen.FActiveForm := nil;
end;
if Screen.FFocusedForm = AControl then Screen.FFocusedForm := nil;
if FHintControl = AControl then FHintControl:=nil;
Screen.UpdateLastActive;
end;
{------------------------------------------------------------------------------
Method: TApplication.Minimize
Params: None
Returns: Nothing
Minimizes the application.
------------------------------------------------------------------------------}
procedure TApplication.Minimize;
begin
WidgetSet.AppMinimize;
end;
procedure TApplication.ModalStarted;
begin
inc(FModalLevel);
if (FModalLevel = 1) then
begin
if Assigned(FOnModalBegin) then
FOnModalBegin(Self);
FApplicationHandlers[ahtModalBegin].CallNotifyEvents(Self);
end;
end;
procedure TApplication.ModalFinished;
begin
dec(FModalLevel);
if (FModalLevel = 0) then
begin
if Assigned(FOnModalEnd) then
FOnModalEnd(Self);
FApplicationHandlers[ahtModalEnd].CallNotifyEvents(Self);
end;
end;
{------------------------------------------------------------------------------
Method: TApplication.Restore
Params: None
Returns: Nothing
Restore minimized application.
------------------------------------------------------------------------------}
procedure TApplication.Restore;
begin
WidgetSet.AppRestore;
end;
{------------------------------------------------------------------------------
TApplication ProcesssMessages "Enter the messageloop and process until empty"
------------------------------------------------------------------------------}
procedure TApplication.ProcessMessages;
begin
WidgetSet.AppProcessMessages;
ProcessAsyncCallQueue;
end;
{------------------------------------------------------------------------------
Method: TApplication.Idle
Params: Wait: wait till something happens
Returns: Nothing
Invoked when the application enters the idle state
------------------------------------------------------------------------------}
procedure TApplication.Idle(Wait: Boolean);
var
Done: Boolean;
begin
ReleaseComponents;
ProcessAsyncCallQueue;
Done := True;
if (FIdleLockCount=0) then begin
if Assigned(FOnIdle) then FOnIdle(Self, Done);
if Done then
NotifyIdleHandler(Done);
end;
if Done
then begin
// wait till something happens
if (FIdleLockCount=0) then
DoIdleActions;
Include(FFlags,AppWaiting);
Exclude(FFlags,AppIdleEndSent);
if Wait then
WidgetSet.AppWaitMessage;
if (FIdleLockCount=0) then
DoOnIdleEnd;
Exclude(FFlags,AppWaiting);
end;
end;
{------------------------------------------------------------------------------
TApplication HintMouseMEssage
------------------------------------------------------------------------------}
procedure TApplication.HintMouseMessage(Control : TControl;
var AMessage : TLMessage);
begin
// ToDo
end;
{------------------------------------------------------------------------------
TApplication Initialize
Makes a call to the component engine to provide any initialization that
needs to occur.
------------------------------------------------------------------------------}
procedure TApplication.Initialize;
var
Res: TFPResourceHandle;
begin
inherited Initialize;
// interface object and screen
if (WidgetSet=nil) or (WidgetSet.ClassType = TWidgetSet)
then begin
DebugLn('ERROR: ',rsNoWidgetSet);
raise Exception.Create(rsNoWidgetSet);
end;
WidgetSet.AppInit(ScreenInfo);
ScreenInfo.Initialized := True;
Screen.UpdateScreen;
// set that we are initialized => all exceptions will be handled by our HandleException
include(FFlags, AppInitialized);
// application icon
if LazarusResources.Find('MAINICON') <> nil then
Icon.LoadFromLazarusResource('MAINICON')
else
begin
Res := FindResource(HInstance, PChar('MAINICON'), PChar(RT_GROUP_ICON));
if Res <> 0 then
Icon.LoadFromResourceHandle(Hinstance, Res);
end;
end;
{------------------------------------------------------------------------------
Method: TApplication.UpdateMouseHint
Params: None
Returns: Nothing
Handles mouse Idle
------------------------------------------------------------------------------}
procedure TApplication.UpdateMouseHint(CurrentControl: TControl);
var
HintControl: TControl;
begin
HintControl := GetHintControl(CurrentControl);
if HintControl = nil then
Hint := ''
else
Hint := GetLongHint(HintControl.Hint);
end;
procedure TApplication.SetCaptureExceptions(const AValue: boolean);
begin
if FCaptureExceptions=AValue then exit;
FCaptureExceptions:=AValue;
if FCaptureExceptions then begin
// capture exceptions
// store old exceptproc
if FOldExceptProc=nil then
FOldExceptProc:=ExceptProc;
ExceptProc:=@ExceptionOccurred;
end else begin
// do not capture exceptions
if ExceptProc=@ExceptionOccurred then begin
// restore old exceptproc
ExceptProc:=FOldExceptProc;
FOldExceptProc:=nil;
end;
end;
end;
function TApplication.HelpCommand(Command: Word; Data: PtrInt): Boolean;
var
CallHelp: Boolean;
begin
CallHelp := True;
Result := DoOnHelp(Command, Data, CallHelp);
if Result then
Exit;
if CallHelp then
begin
// TODO: call help
end;
end;
{------------------------------------------------------------------------------
function TApplication.GetControlAtMouse: TControl;
------------------------------------------------------------------------------}
function TApplication.GetControlAtMouse: TControl;
var
P: TPoint;
begin
GetCursorPos(P);
//debugln(['TApplication.GetControlAtMouse p=',dbgs(p),' FLastMousePos=',dbgs(FLastMousePos)]);
if FLastMouseControlValid and (P.X = FLastMousePos.x) and (P.Y = FLastMousePos.Y) then
Result := FLastMouseControl
else
Result := FindControlAtPosition(P, False);
if Assigned(Result) and (csDesigning in Result.ComponentState) then
Result := nil;
if Assigned(Result) then
begin
FLastMouseControlValid := True;
FLastMousePos := p;
FLastMouseControl := Result;
end;
end;
procedure TApplication.SetBidiMode(const AValue: TBiDiMode) ;
begin
if AValue <> FBidiMode then
begin
FBidiMode := AValue;
NotifyCustomForms(CM_PARENTBIDIMODECHANGED);
end;
end;
procedure TApplication.SetFlags(const AValue: TApplicationFlags);
begin
{ Only allow AppNoExceptionMessages to be changed }
FFlags := Flags - [AppNoExceptionMessages] + AValue*[AppNoExceptionMessages];
end;
procedure TApplication.SetMainFormOnTaskBar(const AValue: Boolean);
begin
if FMainFormOnTaskBar = AValue then exit;
FMainFormOnTaskBar := AValue;
WidgetSet.AppSetMainFormOnTaskBar(FMainFormOnTaskBar);
end;
procedure TApplication.SetNavigation(const AValue: TApplicationNavigationOptions);
begin
if FNavigation=AValue then exit;
FNavigation:=AValue;
end;
procedure TApplication.SetShowButtonGlyphs(const AValue: TApplicationShowGlyphs);
begin
if FShowButtonGlyphs = AValue then
Exit;
FShowButtonGlyphs := AValue;
NotifyCustomForms(CM_APPSHOWBTNGLYPHCHANGED);
end;
procedure TApplication.SetShowMenuGlyphs(const AValue: TApplicationShowGlyphs);
begin
if FShowMenuGlyphs = AValue then
Exit;
FShowMenuGlyphs := AValue;
NotifyCustomForms(CM_APPSHOWMENUGLYPHCHANGED);
end;
procedure TApplication.SetTaskBarBehavior(const AValue: TTaskBarBehavior);
var
i: Integer;
FormToUpdate: TCustomForm;
begin
if FTaskBarBehavior=AValue then exit;
FTaskBarBehavior:=AValue;
for i := 0 to Screen.CustomFormCount-1 do
begin
FormToUpdate := Screen.CustomForms[i];
if FormToUpdate.ShowInTaskBar = stDefault then
FormToUpdate.UpdateShowInTaskBar;
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.UpdateMouseControl(NewMouseControl: TControl);
------------------------------------------------------------------------------}
procedure TApplication.UpdateMouseControl(NewMouseControl: TControl);
begin
//debugln(['TApplication.UpdateMouseControl Old=',DbgSName(FMouseControl),' New=',DbgSName(NewMouseControl)]);
if FMouseControl = NewMouseControl then
Exit;
if (FMouseControl <> nil) then
begin
//DebugLn' MOUSELEAVE=',FMouseControl.Name,':',FMouseControl.ClassName);
FMouseControl.Perform(CM_MOUSELEAVE, 0, 0);
end;
FMouseControl := NewMouseControl;
Application.UpdateMouseHint(FMouseControl);
if (FMouseControl <> nil) then
begin
//DebugLn' MOUSEENTER=',FMouseControl.Name,':',FMouseControl.ClassName);
FMouseControl.Perform(CM_MOUSEENTER, 0, 0);
end;
end;
{------------------------------------------------------------------------------
Method: TApplication.SetIcon
Params: the new icon
------------------------------------------------------------------------------}
procedure TApplication.SetIcon(AValue: TIcon);
begin
FIcon.Assign(AValue);
end;
{------------------------------------------------------------------------------
procedure TApplication.SetShowHint(const AValue: Boolean);
------------------------------------------------------------------------------}
procedure TApplication.SetShowHint(const AValue: Boolean);
begin
if FShowHint = AValue then
exit;
FShowHint := AValue;
if FShowHint then
begin
//
end else
begin
FreeThenNil(FHintWindow);
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.SetTitle(const AValue: String);
------------------------------------------------------------------------------}
procedure TApplication.SetTitle(const AValue: String);
begin
inherited SetTitle(AValue);
WidgetSet.AppSetTitle(GetTitle);
end;
{------------------------------------------------------------------------------
procedure TApplication.StopHintTimer;
------------------------------------------------------------------------------}
procedure TApplication.StopHintTimer;
begin
if FHintTimer <> nil then
FHintTimer.Enabled := False;
end;
{------------------------------------------------------------------------------
procedure TApplication.ValidateHelpSystem;
------------------------------------------------------------------------------}
function TApplication.ValidateHelpSystem: Boolean;
begin
Result := HelpManager <> nil;
end;
{------------------------------------------------------------------------------
procedure TApplication.NotifyIdleHandler(var Done: Boolean);
Done = true will wait for the next message
Done = false will repeat calling the OnIdle handlers
------------------------------------------------------------------------------}
procedure TApplication.NotifyIdleHandler(var Done: Boolean);
var
i: LongInt;
begin
i:=FApplicationHandlers[ahtIdle].Count;
while FApplicationHandlers[ahtIdle].NextDownIndex(i) do begin
TIdleEvent(FApplicationHandlers[ahtIdle][i])(Self,Done);
if not Done then exit;
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.NotifyIdleEndHandler;
------------------------------------------------------------------------------}
procedure TApplication.NotifyIdleEndHandler;
begin
FApplicationHandlers[ahtIdleEnd].CallNotifyEvents(Self);
end;
procedure TApplication.NotifyActivateHandler;
begin
if Assigned(OnActivate) then OnActivate(Self);
FApplicationHandlers[ahtActivate].CallNotifyEvents(Self);
end;
procedure TApplication.NotifyDeactivateHandler;
begin
if Assigned(OnDeactivate) then OnDeactivate(Self);
FApplicationHandlers[ahtDeactivate].CallNotifyEvents(Self);
end;
procedure TApplication.NotifyCustomForms(Msg: Word);
var
i: integer;
begin
for i := 0 to Screen.CustomFormCount - 1 do
Screen.CustomForms[i].Perform(Msg, 0, 0);
end;
{------------------------------------------------------------------------------
function TApplication.IsHintMsg(var Msg: TMsg): Boolean;
------------------------------------------------------------------------------}
function TApplication.IsHintMsg(var Msg: TMsg): Boolean;
begin
Result := False;
end;
function TApplication.DoOnHelp(Command: Word; Data: PtrInt; var CallHelp: Boolean): Boolean;
var
ActiveForm: TCustomForm;
i: LongInt;
begin
ActiveForm := Screen.ActiveCustomForm;
if Assigned(ActiveForm) and Assigned(ActiveForm.FOnHelp) then
Result := ActiveForm.FOnHelp(Command, Data, CallHelp)
else
begin
if Assigned(FOnHelp) then
Result := FOnHelp(Command, Data, CallHelp)
else
Result := False;
i := FApplicationHandlers[ahtHelp].Count;
while not Result and FApplicationHandlers[ahtHelp].NextDownIndex(i) do
Result := THelpEvent(FApplicationHandlers[ahtHelp][i])(Command, Data, CallHelp);
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.DoOnMouseMove;
------------------------------------------------------------------------------}
procedure TApplication.DoOnMouseMove;
var
CursorPos: TPoint;
begin
if not GetCursorPos(CursorPos) then
Exit;
ActivateHint(CursorPos, True);
end;
{------------------------------------------------------------------------------
procedure TApplication.ShowHintWindow(const Info: THintInfoAtMouse);
------------------------------------------------------------------------------}
procedure TApplication.ShowHintWindow(const Info: THintInfoAtMouse);
function GetCursorHeightMargin: integer;
begin
Result:=25;
end;
var
ClientOrigin, ParentOrigin: TPoint;
HintInfo: THintInfo;
CanShow: Boolean;
HintWinRect: TRect;
CurHeight: Integer;
i: LongInt;
begin
if not FShowHint or (FHintControl=nil) then
Exit;
{$ifdef DebugHintWindow}
debugln('TApplication.ShowHintWindow A OldHint="',Hint,'" NewHint="',GetShortHint(Info.Control.Hint),'"');
{$endif}
CurHeight:=GetCursorHeightMargin;
HintInfo.HintControl := FHintControl;
HintInfo.HintPos := Info.MousePos;
// to reduce flicker
HintInfo.HintPos.X:=HintInfo.HintPos.X and (not $F);
HintInfo.HintPos.Y:=HintInfo.HintPos.Y and (not $F);
Inc(HintInfo.HintPos.Y, CurHeight);
HintInfo.HintMaxWidth := Screen.Width;
HintInfo.HintColor := FHintColor;
HintInfo.CursorRect := FHintControl.BoundsRect;
ClientOrigin := FHintControl.ClientOrigin;
ParentOrigin.X := 0;
ParentOrigin.Y := 0;
if FHintControl.Parent <> nil then
ParentOrigin := FHintControl.Parent.ClientOrigin;
OffsetRect(HintInfo.CursorRect, ParentOrigin.X - ClientOrigin.X,
ParentOrigin.Y - ClientOrigin.Y);
HintInfo.CursorPos := FHintControl.ScreenToClient(Info.MousePos);
HintInfo.HintStr := GetControlShortHint(Info.Control);
HintInfo.ReshowTimeout := 0;
HintInfo.HideTimeout := FHintHidePause
+FHintHidePausePerChar*length(HintInfo.HintStr);
HintInfo.HintWindowClass := HintWindowClass;
HintInfo.HintData := nil;
CanShow := FHintControl.Perform(CM_HINTSHOW, 0, LParam(PtrUInt(@HintInfo))) = 0;
if (HintInfo.HintWindowClass=nil)
or (not HintInfo.HintWindowClass.InheritsFrom(THintWindow)) then
HintInfo.HintWindowClass := HintWindowClass;
if CanShow then begin
if Assigned(FOnShowHint) then
FOnShowHint(HintInfo.HintStr, CanShow, HintInfo);
i:=FApplicationHandlers[ahtShowHint].Count;
while FApplicationHandlers[ahtShowHint].NextDownIndex(i) do
TShowHintEvent(FApplicationHandlers[ahtShowHint][i])(HintInfo.HintStr, CanShow, HintInfo);
end;
if CanShow and (FHintControl <> nil) and (HintInfo.HintStr <> '') then
begin
// create hint window
if (FHintWindow<>nil) and (FHintWindow.ClassType<>HintInfo.HintWindowClass)
then
FreeThenNil(FHintWindow);
if FHintWindow=nil then
begin
FHintWindow:=THintWindowClass(HintInfo.HintWindowClass).Create(Self);
with FHintWindow do
begin
Visible := False;
Caption := '';
AutoHide := False;
end;
end;
// make the hint have the same BiDiMode as the activating control
FHintWindow.BiDiMode := FHintControl.BiDiMode;
// calculate the width of the hint based on HintStr and MaxWidth
with HintInfo do
HintWinRect := FHintWindow.CalcHintRect(HintMaxWidth, HintStr, HintData);
//Position HintWindow depending on LTR/RTL
if FHintWindow.UseRightToLeftAlignment then
OffsetRect(HintWinRect, HintInfo.HintPos.X - (HintWinRect.Right - HintWinRect.Left), HintInfo.HintPos.Y)
else
OffsetRect(HintWinRect, HintInfo.HintPos.X, HintInfo.HintPos.Y);
//DebugLn(['TApplication.ShowHintWindow HintStr="',HintInfo.HintStr,'" HintWinRect=',dbgs(HintWinRect)]);
{if FHintWindow.UseRightToLeftAlignment then
with HintWinRect do
begin
Dec(Left, FHintWindow.Canvas.TextWidth(HintInfo.HintStr) + 5);
Dec(Right, FHintWindow.Canvas.TextWidth(HintInfo.HintStr) + 5);
end;}
// Convert the client's rect to screen coordinates
{with HintInfo do
begin
FHintCursorRect.TopLeft :=
FHintControl.ClientToScreen(CursorRect.TopLeft);
FHintCursorRect.BottomRight :=
FHintControl.ClientToScreen(CursorRect.BottomRight);
end;}
FHintWindow.Color := HintInfo.HintColor;
//DebugLn(['TApplication.ShowHintWindow FHintWindow.Color=',dbgs(FHintWindow.Color),' HintInfo.HintColor=',dbgs(HintInfo.HintColor)]);
//debugln('TApplication.ShowHintWindow B HintWinRect=',dbgs(HintWinRect),' HintStr="',DbgStr(HintInfo.HintStr),'"');
FHintWindow.ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
FHintRect := HintInfo.CursorRect;
// start hide timer
StartHintTimer(HintInfo.HideTimeout,ahttHideHint);
end
else
HideHint;
{$ifdef DebugHintWindow}
DebugLn(['TApplication.ShowHintWindow Info.ControlHasHint=',
Info.ControlHasHint, ' Type=', ord(FHintTimerType)]);
{$endif}
end;
{------------------------------------------------------------------------------
procedure TApplication.StartHintTimer(Interval: integer;
TimerType: TAppHintTimerType);
------------------------------------------------------------------------------}
procedure TApplication.StartHintTimer(Interval: integer;
TimerType: TAppHintTimerType);
begin
{$ifdef DebugHintWindow}
debugln('TApplication.StartHintTimer ',dbgs(Interval));
{$endif}
StopHintTimer;
FHintTimerType := TimerType;
if Interval > 0 then
begin
if FHintTimer = nil then
FHintTimer := TCustomTimer.Create(Self);
FHintTimer.Interval := Interval;
FHintTimer.OnTimer := @OnHintTimer;
FHintTimer.Enabled := True;
end
else
OnHintTimer(Self);
end;
{------------------------------------------------------------------------------
procedure TApplication.OnHintTimer(Sender: TObject);
------------------------------------------------------------------------------}
procedure TApplication.OnHintTimer(Sender: TObject);
var
Info: THintInfoAtMouse;
CursorPos: TPoint;
begin
{$ifdef DebugHintWindow}
DebugLn('TApplication.OnHintTimer Type=', IntToStr(ord(FHintTimerType)));
{$endif}
StopHintTimer;
case FHintTimerType of
ahttShowHint:
begin
if not GetCursorPos(CursorPos) then
HideHint
else
begin
Info := GetHintInfoAt(CursorPos);
if Info.ControlHasHint then
ShowHintWindow(Info)
else
HideHint;
end;
end;
ahttHideHint:
begin
HideHint;
FHintTimerType := ahttNone;
end
else
HideHint;
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.UpdateVisible;
------------------------------------------------------------------------------}
procedure TApplication.UpdateVisible;
function AppUseSingleButton: Boolean;
begin
Result := (TaskBarBehavior = tbSingleButton)
or ((TaskBarBehavior = tbDefault)
and (WidgetSet.GetLCLCapability(lcNeedMininimizeAppWithMainForm) = LCL_CAPABILITY_YES));
end;
function UseAppTaskbarItem(AForm: TCustomForm): Boolean; inline;
begin
Result := (AForm = MainForm) or (AForm.ShowInTaskBar = stNever)
or ((AForm.ShowInTaskBar = stDefault) and AppUseSingleButton);
end;
function HasVisibleForms: Boolean;
var
i: integer;
AForm: TCustomForm;
begin
Result := False;
// how to count correct? Do we need to count TCustomForms exclude THintWindow
// or just count TForm descendants?
for i := 0 to Screen.FormCount - 1 do
begin
AForm := Screen.Forms[i];
if (AForm.Parent = nil)
and AForm.Showing // check showing (not Visible)
and (not (csDestroyingHandle in AForm.ControlState))
and UseAppTaskbarItem(AForm) then
begin
Result := True;
break;
end;
end;
end;
begin
// if there are visible forms wich shares application taskbar item then application
// task bar item must be visible too else hide it
WidgetSet.AppSetVisible(HasVisibleForms);
end;
{------------------------------------------------------------------------------
procedure TApplication.DoIdleActions;
------------------------------------------------------------------------------}
procedure TApplication.DoIdleActions;
var
i: Integer;
CurForm: TCustomForm;
begin
i := 0;
while i < Screen.CustomFormCount do begin { While loop to allow number of forms to change during loop }
CurForm:=Screen.CustomForms[i];
if CurForm.HandleAllocated and CurForm.Visible and CurForm.Enabled then
CurForm.UpdateActions;
Inc(i);
end;
// hide splashscreen(s)
i := Screen.CustomFormCount-1;
while i >=0 do begin { While loop to allow number of forms to change during loop }
CurForm:=Screen.CustomForms[i];
if CurForm.FormStyle=fsSplash then
CurForm.Hide;
i:=Min(i,Screen.CustomFormCount)-1;
end;
end;
procedure TApplication.MenuPopupHandler(Sender: TObject);
begin
HideHint;
end;
{------------------------------------------------------------------------------
Method: TApplication.ProcessAsyncCallQueue
Call all methods queued to be called (QueueAsyncCall)
------------------------------------------------------------------------------}
procedure TApplication.ProcessAsyncCallQueue;
var
lItem: PAsyncCallQueueItem;
Event: TDataEvent;
Data: PtrInt;
begin
with FAsyncCall do begin
// move the items of NextQueue to CurQueue, keep the order
System.EnterCriticalsection(CritSec);
try
if Next.Top<>nil then
begin
if Cur.Last<>nil then
begin
assert(Cur.Top <> nil, 'TApplication.ProcessAsyncCallQueue: Last entry found, while Top not assigned');
Cur.Last^.NextItem:=Next.Top;
Next.Top^.PrevItem:=Cur.Last;
end else begin
assert(Cur.Top = nil, 'TApplication.ProcessAsyncCallQueue: Last entry found, while Top not assigned');
Cur.Top:=Next.Top;
end;
Cur.Last:=Next.Last;
Next.Top:=nil;
Next.Last:=nil;
end;
finally
System.LeaveCriticalsection(CritSec);
end;
// process items from top to last in 'Cur' queue
// this can create new items, which are added to the 'Next' queue
// or it can call ProcessAsyncCallQueue, for example via calling
// Application.ProcesssMessages
// Using a second queue avoids an endless loop, when an event adds a new event.
repeat
// remove top item from queue
System.EnterCriticalSection(CritSec);
try
if Cur.Top=nil then exit;
lItem:=Cur.Top;
Cur.Top := lItem^.NextItem;
if Cur.Top = nil then
Cur.Last := nil
else
Cur.Top^.PrevItem := nil;
// free item
Event:=lItem^.Method;
Data:=lItem^.Data;
Dispose(lItem);
finally
System.LeaveCriticalSection(CritSec);
end;
// call event
Event(Data);
until false;
end;
end;
procedure TApplication.DoBeforeFinalization;
var
i: Integer;
begin
if Self=nil then exit;
for i := ComponentCount - 1 downto 0 do
begin
// DebugLn('TApplication.DoBeforeFinalization ',DbgSName(Components[i]));
if i < ComponentCount then
Components[i].Free;
end;
end;
function TApplication.GetParams(Index: Integer): string;
begin
Result:=ParamStrUTF8(Index);
end;
{------------------------------------------------------------------------------
Method: TApplication.IconChanged
------------------------------------------------------------------------------}
procedure TApplication.IconChanged(Sender: TObject);
var
i: integer;
CurForm: TCustomForm;
begin
FreeIconHandles;
Widgetset.AppSetIcon(SmallIconHandle, BigIconHandle);
i := Screen.CustomFormCount-1;
while i >=0 do begin { While loop to allow number of forms to change during loop }
CurForm:=Screen.CustomForms[i];
CurForm.Perform(CM_ICONCHANGED, 0, 0);
i:=Min(i,Screen.CustomFormCount)-1;
end;
end;
{------------------------------------------------------------------------------
Method: TApplication.SmallIconHandle
Returns: handle of application icon
------------------------------------------------------------------------------}
function TApplication.SmallIconHandle: HIcon;
begin
if not Icon.Empty then
begin
if FSmallIconHandle = 0 then
begin
Icon.OnChange := nil;
Icon.Current := Icon.GetBestIndexForSize(Size(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)));
FSmallIconHandle := Icon.ReleaseHandle;
Icon.OnChange := @IconChanged;
end;
Result := FSmallIconHandle;
end
else
Result := 0;
end;
{------------------------------------------------------------------------------
Method: TApplication.BigIconHandle
Returns: handle of application icon
------------------------------------------------------------------------------}
function TApplication.BigIconHandle: HIcon;
begin
if not Icon.Empty then
begin
if FBigIconHandle = 0 then
begin
Icon.OnChange := nil;
Icon.Current := Icon.GetBestIndexForSize(Size(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)));
FBigIconHandle := Icon.ReleaseHandle;
Icon.OnChange := @IconChanged;
end;
Result := FBigIconHandle;
end
else
Result := 0;
end;
{------------------------------------------------------------------------------
Method: TApplication.GetTitle
Returns: title of application
------------------------------------------------------------------------------}
function TApplication.GetTitle: string;
begin
Result := inherited Title;
if Result = '' then
Result := ExtractFileNameOnly(GetExeName);
end;
procedure TApplication.FreeIconHandles;
begin
if FSmallIconHandle <> 0 then
begin
DestroyIcon(FSmallIconHandle);
FSmallIconHandle := 0;
end;
if FBigIconHandle <> 0 then
begin
DestroyIcon(FBigIconHandle);
FBigIconHandle := 0;
end;
end;
{------------------------------------------------------------------------------
Method: TApplication.HandleException
Params: Sender
Returns: Nothing
------------------------------------------------------------------------------}
procedure TApplication.HandleException(Sender: TObject);
var
i: LongInt;
Skip: Boolean;
begin
if Self = nil then
Exit;
if AppHandlingException in FFlags then
begin
// there was an exception during showing the exception -> break the circle
DebugLn('TApplication.HandleException: ',
'there was another exception during showing the first exception');
HaltingProgram:=true;
DumpExceptionBackTrace;
Halt;
end;
Include(FFlags,AppHandlingException);
if StopOnException then
inherited Terminate;
Skip := ExceptObject is EAbort;
if not (AppNoExceptionMessages in FFlags) then
begin
// before we do anything, write it down
if ExceptObject is Exception then
begin
if not Skip then
begin
DebugLn('TApplication.HandleException ',Exception(ExceptObject).Message);
DumpExceptionBackTrace;
end;
end else
begin
DebugLn('TApplication.HandleException Strange Exception ');
DumpExceptionBackTrace;
end;
end;
// release capture and hide all forms with stay on top, so that
// a message can be shown
if GetCapture <> 0 then SendMessage(GetCapture, LM_CANCELMODE, 0, 0);
if not Skip then
RemoveStayOnTop(True);
// handle the exception
if ExceptObject is Exception then
begin
if not Skip then
begin
i := FApplicationHandlers[ahtException].Count;
if Assigned(OnException) or (i > 0) then
begin
if Assigned(OnException) then
OnException(Sender, Exception(ExceptObject));
while FApplicationHandlers[ahtException].NextDownIndex(i) do
TExceptionEvent(FApplicationHandlers[ahtException][i])(Sender, Exception(ExceptObject));
end
else
ShowException(Exception(ExceptObject));
end;
end
else
SysUtils.ShowException(ExceptObject, ExceptAddr);
if not Skip then
RestoreStayOnTop(True);
Exclude(FFlags, AppHandlingException);
end;
{------------------------------------------------------------------------------
Method: TApplication.HandleMessage
Params: None
Returns: Nothing
Handles all messages first then the Idle
------------------------------------------------------------------------------}
procedure TApplication.HandleMessage;
begin
WidgetSet.AppProcessMessages; // process all events
if not Terminated then Idle(true);
end;
{------------------------------------------------------------------------------
function TApplication.HelpContext(Context: THelpContext): Boolean;
------------------------------------------------------------------------------}
function TApplication.HelpContext(Context: THelpContext): Boolean;
var
CallHelp: Boolean;
begin
CallHelp := True;
Result := DoOnHelp(HELP_CONTEXT, Context, CallHelp);
if not CallHelp then
Exit;
if ValidateHelpSystem then
Result := ShowHelpOrErrorForContext('', Context) = shrSuccess
else
Result := False;
end;
{------------------------------------------------------------------------------
function TApplication.HelpKeyword(const Keyword: String): Boolean;
------------------------------------------------------------------------------}
function TApplication.HelpKeyword(const Keyword: String): Boolean;
var
CallHelp: Boolean;
begin
CallHelp := True;
Result := DoOnHelp(HELP_COMMAND, PtrInt(PChar(Keyword)), CallHelp);
if not CallHelp then
Exit;
if ValidateHelpSystem then
Result := ShowHelpOrErrorForKeyword('', Keyword) = shrSuccess
else
Result := False;
end;
procedure TApplication.ShowHelpForObject(Sender: TObject);
begin
if Sender is TControl then
TControl(Sender).ShowHelp;
end;
{------------------------------------------------------------------------------
procedure TApplication.RemoveStayOnTop;
------------------------------------------------------------------------------}
procedure TApplication.RemoveStayOnTop(const ASystemTopAlso: Boolean = False);
var
i: Integer;
AForm: TCustomForm;
begin
if WidgetSet.AppRemoveStayOnTopFlags(ASystemTopAlso) then
Exit;
if Screen = nil then
Exit;
for i := 0 to Screen.CustomFormCount - 1 do
begin
AForm := Screen.CustomForms[i];
if (AForm.Parent <> nil) or not AForm.Visible then
Continue;
if (AForm.FormStyle in fsAllNonSystemStayOnTop) then
begin
AForm.FormStyle := fsNormal;
if FRestoreStayOnTop = nil then
FRestoreStayOnTop := TList.Create;
if FRestoreStayOnTop.IndexOf(AForm) = -1 then
FRestoreStayOnTop.Add(AForm);
end;
end;
end;
procedure TApplication.RestoreStayOnTop(const ASystemTopAlso: Boolean = False);
var
i: integer;
begin
if WidgetSet.AppRestoreStayOnTopFlags(ASystemTopAlso) then
Exit;
if FRestoreStayOnTop <> nil then
for i := FRestoreStayOnTop.Count - 1 downto 0 do
begin
TCustomForm(FRestoreStayOnTop[i]).FormStyle := fsStayOnTop;
FRestoreStayOnTop.Delete(i);
end;
end;
{------------------------------------------------------------------------------
function TApplication.IsWaiting: boolean;
------------------------------------------------------------------------------}
function TApplication.IsWaiting: boolean;
begin
Result:=AppWaiting in FFlags;
end;
{------------------------------------------------------------------------------
procedure TApplication.CancelHint;
------------------------------------------------------------------------------}
procedure TApplication.CancelHint;
begin
StopHintTimer;
HideHint;
FHintControl := nil;
FHintTimerType := ahttNone;
end;
{------------------------------------------------------------------------------
procedure TApplication.HideHint;
------------------------------------------------------------------------------}
procedure TApplication.HideHint;
begin
if FHintWindow <> nil then
FHintWindow.Visible := False;
end;
{------------------------------------------------------------------------------
TApplication Run
MainForm is loaded and control is passed to event processor.
------------------------------------------------------------------------------}
procedure TApplication.Run;
begin
if (FMainForm <> nil) and FShowMainForm then FMainForm.Show;
WidgetSet.AppRun(@RunLoop);
end;
{------------------------------------------------------------------------------
TApplication RunLoop
control is passed to event processor.
------------------------------------------------------------------------------}
procedure TApplication.RunLoop;
begin
repeat
if CaptureExceptions then
try // run with try..except
HandleMessage;
except
HandleException(Self);
end
else
HandleMessage; // run without try..except
until Terminated;
end;
procedure TApplication.Activate(Data: PtrInt);
begin
if AppActive in FFlags then exit;
Include(FFlags, AppActive);
NotifyActivateHandler;
end;
procedure TApplication.Deactivate(Data: PtrInt);
begin
if (AppDestroying in FFlags) or (not (AppActive in FFlags)) then Exit;
// widgetset has passed deactivate or no control
// of this application has got the focus.
// Force=True means that IntfAppDeactivate called us
if Data = 1 then //TODO: or not Assigned(FindControl(GetFocus)) then
begin
Exclude(FFlags, AppActive);
NotifyDeactivateHandler;
end;
end;
{------------------------------------------------------------------------------}
{ TApplication WndPRoc }
{ }
{------------------------------------------------------------------------------}
procedure TApplication.WndProc(var AMessage : TLMessage);
begin
case AMessage.Msg of
CM_ACTIONEXECUTE, CM_ACTIONUPDATE: AMessage.Result := LResult(DispatchAction(AMessage.Msg, TBasicAction(AMessage.LParam)));
else
Dispatch(AMessage);
end;
end;
function TApplication.DispatchAction(Msg: Longint; Action: TBasicAction): Boolean;
var
Form: TCustomForm;
begin
Result := False;
Form := Screen.ActiveForm;
if (Form <> nil) and (Form.Perform(Msg, 0, PtrInt(Action)) = 1) then
Result := True
else
if (MainForm <> Form) and (MainForm <> nil) and (MainForm.Perform(Msg, 0, PtrInt(Action)) = 1) then
Result := True;
// Disable action if no "user" handler is available
if (not Result) and (Action is TCustomAction) and
TCustomAction(Action).Enabled and TCustomAction(Action).DisableIfNoHandler then
TCustomAction(Action).Enabled := Assigned(Action.OnExecute);
end;
procedure TApplication.AddHandler(HandlerType: TApplicationHandlerType;
const Handler: TMethod; AsFirst: Boolean);
begin
if Handler.Code=nil then RaiseGDBException('TApplication.AddHandler');
if FApplicationHandlers[HandlerType]=nil then
FApplicationHandlers[HandlerType]:=TMethodList.Create;
FApplicationHandlers[HandlerType].Add(Handler,not AsFirst);
end;
procedure TApplication.RemoveHandler(HandlerType: TApplicationHandlerType;
const Handler: TMethod);
begin
FApplicationHandlers[HandlerType].Remove(Handler);
end;
function TApplication.GetConsoleApplication: boolean;
begin
Result:=false;
end;
procedure TApplication.SetHint(const AValue: string);
begin
if FHint = AValue then
Exit;
FHint := AValue;
if Assigned(FOnHint) or (FApplicationHandlers[ahtHint].Count > 0) then
begin
if Assigned(FOnHint) then
FOnHint(Self);
FApplicationHandlers[ahtHint].CallNotifyEvents(Self);
end else
begin
// Send THintAction
with TCustomHintAction.Create(Self) do
begin
Hint := FHint;
try
Execute;
finally
Free;
end;
end;
end;
end;
procedure TApplication.SetHintColor(const AValue: TColor);
begin
if FHintColor = AValue then
exit;
FHintColor := AValue;
if FHintWindow <> nil then
FHintWindow.Color := FHintColor;
end;
procedure TApplication.DoOnIdleEnd;
begin
if (AppIdleEndSent in FFlags) then exit;
if Assigned(OnIdleEnd) then OnIdleEnd(Self);
NotifyIdleEndHandler;
Include(FFlags,AppIdleEndSent);
end;
function TApplication.GetActive: boolean;
begin
Result := AppActive in Flags;
end;
{------------------------------------------------------------------------------
function TApplication.GetCurrentHelpFile: string;
------------------------------------------------------------------------------}
function TApplication.GetCurrentHelpFile: string;
var
ActiveForm: TCustomForm;
begin
ActiveForm := Screen.ActiveCustomForm;
if Assigned(ActiveForm) and (ActiveForm.FHelpFile <> '') then
Result := ActiveForm.HelpFile
else
Result := HelpFile;
end;
{------------------------------------------------------------------------------
TApplication ShowException
------------------------------------------------------------------------------}
procedure TApplication.ShowException(E: Exception);
var
Msg: string;
MsgResult: Integer;
begin
if AppNoExceptionMessages in FFlags then exit;
Msg := E.Message;
if FindInvalidUTF8Character(PChar(Msg), Length(Msg), False) > 0 then
Msg := AnsiToUtf8(Msg);
if (Msg <> '') and (Msg[length(Msg)] <> '.') then Msg := Msg + '.';
if (not Terminated) and (Self <> nil) and (AppInitialized in FFlags) then
begin
DisableIdleHandler;
try
MsgResult:=MessageBox(PChar(
Format(rsPressOkToIgnoreAndRiskDataCorruptionPressCancelToK,
[Msg, LineEnding+LineEnding, LineEnding])),
PChar(GetTitle), MB_OKCANCEL + MB_ICONERROR);
finally
EnableIdleHandler;
end;
if MsgResult<>mrOk then
begin
Include(FFlags, AppNoExceptionMessages);
HaltingProgram := True;
Halt;
end;
end else
inherited ShowException(E);
end;
{------------------------------------------------------------------------------
TApplication Terminate
Class is terminated and the component engine is shutdown
------------------------------------------------------------------------------}
procedure TApplication.Terminate;
begin
inherited Terminate;
WidgetSet.AppTerminate;
end;
procedure TApplication.DisableIdleHandler;
begin
inc(FIdleLockCount);
end;
procedure TApplication.EnableIdleHandler;
begin
if FIdleLockCount<=0 then
RaiseGDBException('TApplication.EnableIdleHandler');
dec(FIdleLockCount);
end;
{------------------------------------------------------------------------------
procedure TApplication.NotifyUserInputHandler;
------------------------------------------------------------------------------}
procedure TApplication.NotifyUserInputHandler(Sender: TObject; Msg: Cardinal);
var
i: integer;
begin
FLastMouseControlValid := False;
case Msg of
LM_MOUSEMOVE:
DoOnMouseMove;
else
CancelHint;
end;
if not Assigned(Sender) then
Sender := Self;
if Assigned(FOnUserInput) then
FOnUserInput(Sender, Msg);
i := FApplicationHandlers[ahtUserInput].Count;
while FApplicationHandlers[ahtUserInput].NextDownIndex(i) do
TOnUserInputEvent(FApplicationHandlers[ahtUserInput][i])(Sender, Msg);
end;
procedure TApplication.NotifyKeyDownBeforeHandler(Sender: TObject;
var Key: Word; Shift: TShiftState);
var
i: Integer;
begin
i:=FApplicationHandlers[ahtKeyDownBefore].Count;
while FApplicationHandlers[ahtKeyDownBefore].NextDownIndex(i) do
TKeyEvent(FApplicationHandlers[ahtKeyDownBefore][i])(Sender,Key,Shift);
end;
procedure TApplication.NotifyKeyDownHandler(Sender: TObject;
var Key: Word; Shift: TShiftState);
var
i: Integer;
begin
i := FApplicationHandlers[ahtKeyDownAfter].Count;
while FApplicationHandlers[ahtKeyDownAfter].NextDownIndex(i) do
TKeyEvent(FApplicationHandlers[ahtKeyDownAfter][i])(Sender, Key, Shift);
if WidgetSet.IsHelpKey(Key, Shift) and
(Widgetset.GetLCLCapability(lcLMHelpSupport) = LCL_CAPABILITY_NO) then
ShowHelpForObject(Sender);
end;
procedure TApplication.ControlKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
AControl: TWinControl;
begin
if Sender is TWinControl then
begin
AControl := TWinControl(Sender);
//debugln('TApplication.ControlKeyDown A ',DbgSName(AControl));
FLastKeyDownSender := AControl;
// handle navigation key
DoTabKey(AControl, Key, Shift);
DoArrowKey(AControl, Key, Shift);
end
else
FLastKeyDownSender := nil;
//DebugLn(['TApplication.ControlKeyDown Sender=',DbgSName(Sender),' Key=',Key,' Shift=',dbgs(Shift)]);
FLastKeyDownKey := Key;
FLastKeyDownShift := Shift;
end;
procedure TApplication.ControlKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
AControl: TWinControl;
begin
if Key = VK_UNKNOWN then exit;
if Sender is TWinControl then
begin
AControl := TWinControl(Sender);
//debugln('TApplication.ControlKeyUp A ',DbgSName(AControl),' Key=',dbgs(Key),' Shift=',dbgs(Shift));
if FLastKeyDownKey = VK_UNKNOWN then
begin
// key was already handled in key down
//debugln('TApplication.ControlKeyUp key was handled in key down');
Exit;
end;
if (Key <> FLastKeyDownKey) or (Shift <> FLastKeyDownShift) or (AControl <> FLastKeyDownSender) then
begin
// a key up, without key down
//debugln('TApplication.ControlKeyUp key was handled in key down or in key up');
Exit;
end;
// handle special navigation keys
DoReturnKey(AControl, Key, Shift);
DoEscapeKey(AControl, Key, Shift);
end;
FLastKeyDownKey := VK_UNKNOWN;
end;
procedure TApplication.AddOnIdleHandler(Handler: TIdleEvent;
AsFirst: Boolean);
begin
AddHandler(ahtIdle,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnIdleHandler(Handler: TIdleEvent);
begin
RemoveHandler(ahtIdle,TMethod(Handler));
end;
procedure TApplication.AddOnIdleEndHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtIdleEnd,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnIdleEndHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtIdleEnd,TMethod(Handler));
end;
procedure TApplication.AddOnUserInputHandler(Handler: TOnUserInputEvent;
AsFirst: Boolean);
begin
AddHandler(ahtUserInput,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnUserInputHandler(Handler: TOnUserInputEvent);
begin
RemoveHandler(ahtUserInput,TMethod(Handler));
end;
procedure TApplication.AddOnKeyDownBeforeHandler(Handler: TKeyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtKeyDownBefore,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnKeyDownBeforeHandler(Handler: TKeyEvent);
begin
RemoveHandler(ahtKeyDownBefore,TMethod(Handler));
end;
procedure TApplication.AddOnKeyDownHandler(Handler: TKeyEvent; AsFirst: Boolean);
begin
AddHandler(ahtKeyDownAfter,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnKeyDownHandler(Handler: TKeyEvent);
begin
RemoveHandler(ahtKeyDownAfter,TMethod(Handler));
end;
procedure TApplication.AddOnActivateHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtActivate,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnActivateHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtActivate,TMethod(Handler));
end;
procedure TApplication.AddOnDeactivateHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtDeactivate,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnDeactivateHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtDeactivate,TMethod(Handler));
end;
procedure TApplication.AddOnExceptionHandler(Handler: TExceptionEvent;
AsFirst: Boolean);
begin
AddHandler(ahtException,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnExceptionHandler(Handler: TExceptionEvent);
begin
RemoveHandler(ahtException,TMethod(Handler));
end;
procedure TApplication.AddOnEndSessionHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtEndSession,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnEndSessionHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtEndSession,TMethod(Handler));
end;
procedure TApplication.AddOnQueryEndSessionHandler(
Handler: TQueryEndSessionEvent; AsFirst: Boolean);
begin
AddHandler(ahtQueryEndSession,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnQueryEndSessionHandler(
Handler: TQueryEndSessionEvent);
begin
RemoveHandler(ahtQueryEndSession,TMethod(Handler));
end;
procedure TApplication.AddOnMinimizeHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtMinimize,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnMinimizeHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtMinimize,TMethod(Handler));
end;
procedure TApplication.AddOnModalBeginHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtModalBegin,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnModalBeginHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtModalBegin,TMethod(Handler));
end;
procedure TApplication.AddOnModalEndHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtModalEnd,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnModalEndHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtModalEnd,TMethod(Handler));
end;
procedure TApplication.AddOnRestoreHandler(Handler: TNotifyEvent;
AsFirst: Boolean);
begin
AddHandler(ahtRestore,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnRestoreHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtRestore,TMethod(Handler));
end;
procedure TApplication.AddOnDropFilesHandler(Handler: TDropFilesEvent;
AsFirst: Boolean);
begin
AddHandler(ahtDropFiles,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnDropFilesHandler(Handler: TDropFilesEvent);
begin
RemoveHandler(ahtDropFiles,TMethod(Handler));
end;
procedure TApplication.AddOnHelpHandler(Handler: THelpEvent; AsFirst: Boolean);
begin
AddHandler(ahtHelp,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnHelpHandler(Handler: THelpEvent);
begin
RemoveHandler(ahtHelp,TMethod(Handler));
end;
procedure TApplication.AddOnHintHandler(Handler: TNotifyEvent; AsFirst: Boolean);
begin
AddHandler(ahtHint,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnHintHandler(Handler: TNotifyEvent);
begin
RemoveHandler(ahtHint,TMethod(Handler));
end;
procedure TApplication.AddOnShowHintHandler(Handler: TShowHintEvent;
AsFirst: Boolean);
begin
AddHandler(ahtShowHint,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnShowHintHandler(Handler: TShowHintEvent);
begin
RemoveHandler(ahtShowHint,TMethod(Handler));
end;
procedure TApplication.AddOnGetMainFormHandleHandler(Handler: TGetHandleEvent;
AsFirst: Boolean);
begin
AddHandler(ahtGetMainFormHandle,TMethod(Handler),AsFirst);
end;
procedure TApplication.RemoveOnGetMainFormHandleHandler(Handler: TGetHandleEvent);
begin
RemoveHandler(ahtGetMainFormHandle,TMethod(Handler));
end;
procedure TApplication.RemoveAllHandlersOfObject(AnObject: TObject);
var
HandlerType: TApplicationHandlerType;
begin
for HandlerType:=Low(TApplicationHandlerType) to High(TApplicationHandlerType) do
FApplicationHandlers[HandlerType].RemoveAllMethodsOfObject(AnObject);
end;
{------------------------------------------------------------------------------
procedure TApplication.IntfEndSession;
------------------------------------------------------------------------------}
procedure TApplication.IntfEndSession;
begin
if Assigned(FOnEndSession) then FOnEndSession(Self);
FApplicationHandlers[ahtEndSession].CallNotifyEvents(Self);
end;
procedure TApplication.IntfAppActivate(const Async: Boolean = False);
begin
if Async then
QueueAsyncCall(@Activate, 1)
else
Activate(1);
end;
procedure TApplication.IntfAppDeactivate(const Async: Boolean = False);
begin
if Async then
QueueAsyncCall(@Deactivate, 1)
else
Deactivate(1);
end;
{------------------------------------------------------------------------------
procedure TApplication.IntfQueryEndSession(var Cancel: Boolean);
------------------------------------------------------------------------------}
procedure TApplication.IntfQueryEndSession(var Cancel: Boolean);
var
i: LongInt;
begin
if Assigned(FOnQueryEndSession) then FOnQueryEndSession(Cancel);
i:=FApplicationHandlers[ahtQueryEndSession].Count;
while FApplicationHandlers[ahtQueryEndSession].NextDownIndex(i) do
TQueryEndSessionEvent(FApplicationHandlers[ahtQueryEndSession][i])(Cancel);
end;
{------------------------------------------------------------------------------
procedure TApplication.IntfAppMinimize;
------------------------------------------------------------------------------}
procedure TApplication.IntfAppMinimize;
begin
if Assigned(FOnMinimize) then FOnMinimize(Self);
FApplicationHandlers[ahtMinimize].CallNotifyEvents(Self);
end;
{------------------------------------------------------------------------------
procedure TApplication.IntfAppRestore;
------------------------------------------------------------------------------}
procedure TApplication.IntfAppRestore;
begin
Screen.RestoreLastActive;
if Assigned(FOnRestore) then FOnRestore(Self);
FApplicationHandlers[ahtRestore].CallNotifyEvents(Self);
end;
{------------------------------------------------------------------------------
Method: TApplication.IntfDropFiles
Params: FileNames - Dropped files
Invokes OnDropFilesEvent of the application.
This function is called by the interface.
------------------------------------------------------------------------------}
procedure TApplication.IntfDropFiles(const FileNames: array of String);
var
i: LongInt;
begin
if Assigned(FOnDropFiles) then FOnDropFiles(Self, FileNames);
i:=FApplicationHandlers[ahtDropFiles].Count;
while FApplicationHandlers[ahtDropFiles].NextDownIndex(i) do
TDropFilesEvent(FApplicationHandlers[ahtDropFiles][i])(Self,Filenames);
end;
procedure TApplication.IntfSettingsChange;
begin
if FUpdateFormatSettings then
{$ifdef win32}
GetFormatSettings
{$endif};
end;
procedure TApplication.IntfThemeOptionChange(AThemeServices: TThemeServices;
AOption: TThemeOption);
begin
case AOption of
toShowButtonImages:
if ShowButtonGlyphs = sbgSystem then
NotifyCustomForms(CM_APPSHOWBTNGLYPHCHANGED);
toShowMenuImages:
if ShowMenuGlyphs = sbgSystem then
NotifyCustomForms(CM_APPSHOWMENUGLYPHCHANGED);
end;
end;
function TApplication.IsRightToLeft: Boolean;
begin
Result := (BiDiMode <> bdLeftToRight);
end;
procedure TApplication.DoArrowKey(AControl: TWinControl; var Key: Word;
Shift: TShiftState);
begin
if (Key in [VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN]) and (Shift = []) and
(AControl.Perform(CM_WANTSPECIALKEY, Key, 0) = 0) and
(AControl.Perform(LM_GETDLGCODE, 0, 0) and DLGC_WANTARROWS = 0) and
(anoArrowToSelectNextInParent in Navigation) and AControl.Focused and
Assigned(AControl.Parent) then
begin
// traverse controls inside parent
AControl.Parent.SelectNext(AControl, Key in [VK_RIGHT, VK_DOWN], False);
Key := VK_UNKNOWN;
end;
end;
{------------------------------------------------------------------------------
procedure TApplication.DoBeforeMouseMessage(CurMouseControl: TControl);
------------------------------------------------------------------------------}
procedure TApplication.DoBeforeMouseMessage(CurMouseControl: TControl);
begin
//debugln(['TApplication.DoBeforeMouseMessage ',DbgSName(CurMouseControl)]);
UpdateMouseControl(CurMouseControl);
end;
function TApplication.IsShortcut(var Message: TLMKey): boolean;
var
ModalForm: TCustomForm;
begin
Result := false;
if Assigned(FOnShortcut) then
begin
FOnShortcut(Message, Result);
if Result then
Exit;
end;
// next: if there is a modal form, let it handle the short cut
ModalForm := Screen.GetCurrentModalForm;
if Assigned(ModalForm) and IsWindowEnabled(ModalForm.Handle) then
Result := ModalForm.IsShortcut(Message)
else
begin
// no modal form - let the current focused form handle the shortcut
if Assigned(Screen.ActiveCustomForm) and IsWindowEnabled(Screen.ActiveCustomForm.Handle) then
begin
Result := Screen.ActiveCustomForm.IsShortcut(Message);
if Result then Exit;
end;
// let the main form handle the shortcut
if Assigned(MainForm) and (Screen.ActiveCustomForm <> MainForm) and IsWindowEnabled(MainForm.Handle) then
begin
Result := FMainForm.IsShortcut(Message);
if Result then Exit;
end;
end;
end;
procedure TApplication.DoEscapeKey(AControl: TWinControl; var Key: Word;
Shift: TShiftState);
var
Form: TCustomForm;
begin
if (Shift = []) and (Key = VK_ESCAPE) and
(AControl.Perform(CM_WANTSPECIALKEY, Key, 0) = 0) and
(AControl.Perform(LM_GETDLGCODE, 0, 0) and DLGC_WANTALLKEYS = 0) and
(anoEscapeForCancelControl in Navigation) then
begin
Form := GetParentForm(AControl);
if Assigned(Form) and Assigned(Form.CancelControl) then
begin
//debugln('TApplication.ControlKeyUp VK_ESCAPE ', Acontrol.Name);
Form.CancelControl.ExecuteCancelAction;
Key := VK_UNKNOWN;
end;
end;
end;
procedure TApplication.DoReturnKey(AControl: TWinControl; var Key: Word;
Shift: TShiftState);
var
Form: TCustomForm;
lDefaultControl: TControl;
begin
if (Shift = []) and (Key = VK_RETURN) and
(AControl.Perform(CM_WANTSPECIALKEY, Key, 0) = 0) and
(AControl.Perform(LM_GETDLGCODE, 0, 0) and DLGC_WANTALLKEYS = 0) and
(anoReturnForDefaultControl in Navigation) then
begin
//DebugLn(['TApplication.DoReturnKey ',DbgSName(AControl)]);
Form := GetParentForm(AControl);
if Assigned(Form) then
begin
lDefaultControl := Form.ActiveDefaultControl;
if lDefaultControl = nil then
lDefaultControl := Form.DefaultControl;
if Assigned(lDefaultControl)
and ((lDefaultControl.Parent = nil) or (lDefaultControl.Parent.CanFocus))
and lDefaultControl.Enabled and lDefaultControl.Visible then
begin
//debugln('TApplication.ControlKeyUp VK_RETURN ', Acontrol.Name);
lDefaultControl.ExecuteDefaultAction;
Key := VK_UNKNOWN;
end;
end;
end;
end;
procedure TApplication.DoTabKey(AControl: TWinControl; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_TAB) and ((Shift - [ssShift]) = []) and
(AControl.Perform(CM_WANTSPECIALKEY, Key, 0) = 0) and
(AControl.Perform(LM_GETDLGCODE, 0, 0) and DLGC_WANTTAB = 0) and
(anoTabToSelectNext in Navigation) and AControl.Focused then
begin
// traverse tabstop controls inside form
AControl.PerformTab(not (ssShift in Shift));
Key := VK_UNKNOWN;
end;
end;
{------------------------------------------------------------------------------
TApplication CreateForm
Note: The name is confusing and only kept for Delphi compatibility. It can
create any kind of components.
Create a Component instance and sets the pointer to the component variable
and loads the component. If it is a form it will be added to the applications
forms list
------------------------------------------------------------------------------}
procedure TApplication.CreateForm(InstanceClass: TComponentClass;
out Reference);
var
Instance: TComponent;
ok: boolean;
AForm: TForm;
begin
// Allocate the instance, without calling the constructor
Instance := TComponent(InstanceClass.NewInstance);
// set the Reference before the constructor is called, so that
// events and constructors can refer to it
TComponent(Reference) := Instance;
ok:=false;
try
if (FCreatingForm=nil) and (Instance is TForm) then
FCreatingForm:=TForm(Instance);
Instance.Create(Self);
ok:=true;
finally
if not ok then begin
TComponent(Reference) := nil;
if FCreatingForm=Instance then
FCreatingForm:=nil;
end;
end;
if (Instance is TForm) then
begin
AForm := TForm(Instance);
UpdateMainForm(AForm);
if FMainForm = AForm then
AForm.HandleNeeded;
if AForm.FormStyle = fsSplash then
begin
// show the splash form and handle the paint message
AForm.Show;
AForm.Invalidate;
ProcessMessages;
end;
end;
{$IFDEF AfterConstructionDataModuleNotWorking}
if (Instance is TDataModule) then
begin
TDataModule(instance).AfterConstruction;
end;
{$ENDIF}
end;
procedure TApplication.UpdateMainForm(AForm: TForm);
begin
if (FMainForm = nil)
and (FCreatingForm=AForm)
and (not (AppDestroying in FFlags))
and not (AForm.FormStyle in [fsMDIChild, fsSplash])
then
FMainForm := AForm;
end;
procedure TApplication.QueueAsyncCall(const AMethod: TDataEvent; Data: PtrInt);
var
lItem: PAsyncCallQueueItem;
begin
if AppDoNotCallAsyncQueue in FFlags then
raise Exception.Create('TApplication.QueueAsyncCall already shut down');
New(lItem);
lItem^.Method := AMethod;
lItem^.Data := Data;
lItem^.NextItem := nil;
System.EnterCriticalsection(FAsyncCall.CritSec);
try
with FAsyncCall.Next do begin
lItem^.PrevItem := Last;
if Last<>nil then begin
assert(Top <> nil, 'TApplication.QueueAsyncCall: Top entry missing (but last is assigned)');
Last^.NextItem := lItem
end else begin
assert(Last = nil, 'TApplication.QueueAsyncCall: Last entry found, while Top not assigned');
Top := lItem;
end;
Last := lItem;
end;
finally
System.LeaveCriticalsection(FAsyncCall.CritSec);
end;
if Assigned(WakeMainThread) then
WakeMainThread(nil);
end;
procedure TApplication.RemoveAsyncCalls(const AnObject: TObject);
procedure DoRemoveAsyncCalls(var AQueue: TAsyncCallQueue);
var
lItem, lItem2: PAsyncCallQueueItem;
begin
lItem := AQueue.Last;
while lItem <> nil do begin
if TMethod(lItem^.Method).Data = Pointer(AnObject) then begin
if lItem^.NextItem <> nil then
lItem^.NextItem^.PrevItem := lItem^.PrevItem;
if lItem^.PrevItem <> nil then
lItem^.PrevItem^.NextItem := lItem^.NextItem;
if lItem = AQueue.Last then
AQueue.Last := lItem^.PrevItem;
if lItem = AQueue.Top then
AQueue.Top := lItem^.NextItem;
lItem2 := lItem;
lItem := lItem^.PrevItem;
Dispose(lItem2);
end
else
lItem := lItem^.PrevItem;
end;
end;
begin
if AppDoNotCallAsyncQueue in FFlags then
raise Exception.Create('TApplication.QueueAsyncCall already shut down');
System.EnterCriticalsection(FAsyncCall.CritSec);
try
DoRemoveAsyncCalls(FAsyncCall.Cur);
DoRemoveAsyncCalls(FAsyncCall.Next);
finally
System.LeaveCriticalSection(FAsyncCall.CritSec);
end;
end;
procedure TApplication.FreeComponent(Data: PtrInt);
begin
if Data<>0 then
DebugLn(['HINT: TApplication.FreeComponent Data<>0 ignored']);
ReleaseComponents;
end;
procedure TApplication.ReleaseComponents;
var
Component: TComponent;
begin
if FComponentsReleasing<>nil then exit; // currently releasing
if (FComponentsToRelease<>nil) then begin
if FComponentsToRelease.Count=0 then begin
FreeAndNil(FComponentsToRelease);
exit;
end;
// free components
// Notes:
// - check TLCLComponent.LCLRefCount=0
// - during freeing new components can be added to the FComponentsToRelease
// - components can be removed from FComponentsToRelease and FComponentsReleasing
FComponentsReleasing:=FComponentsToRelease;
FComponentsToRelease:=nil;
try
while (FComponentsReleasing<>nil) and (FComponentsReleasing.Count>0) do
begin
Component:=TComponent(FComponentsReleasing[0]);
FComponentsReleasing.Delete(0);
if (Component is TLCLComponent)
and (TLCLComponent(Component).LCLRefCount>0) then begin
// add again to FComponentsToRelease
ReleaseComponent(Component);
end else begin
// this might free some more components from FComponentsReleasing
Component.Free;
end;
end;
finally
// add remaining to FComponentsToRelease
while (FComponentsReleasing<>nil) and (FComponentsReleasing.Count>0) do
begin
Component:=TComponent(FComponentsReleasing[0]);
FComponentsReleasing.Delete(0);
ReleaseComponent(Component);
end;
FreeAndNil(FComponentsReleasing);
end;
end;
end;
procedure TApplication.ReleaseComponent(AComponent: TComponent);
var
IsFirstItem: Boolean;
begin
if csDestroying in AComponent.ComponentState then exit;
//DebugLn(['TApplication.ReleaseComponent ',DbgSName(AComponent)]);
if AppDestroying in FFlags then begin
// free immediately
AComponent.Free;
end else begin
// free later
// => add to the FComponentsToRelease
IsFirstItem:=FComponentsToRelease=nil;
if IsFirstItem then
FComponentsToRelease:=TFPList.Create
else if FComponentsToRelease.IndexOf(AComponent)>=0 then
exit;
FComponentsToRelease.Add(AComponent);
AComponent.FreeNotification(Self);
if IsFirstItem then
QueueAsyncCall(@FreeComponent, 0);
end;
end;
function TApplication.ExecuteAction(ExeAction: TBasicAction): Boolean;
begin
Result := False;
if Assigned(FOnActionExecute) then FOnActionExecute(ExeAction, Result);
end;
function TApplication.UpdateAction(TheAction: TBasicAction): Boolean;
begin
Result := False;
if Assigned(FOnActionUpdate) then FOnActionUpdate(TheAction, Result);
end;
function TApplication.IsRTLLang(ALang: String): Boolean;
var
lng : String;
p : word;
function sep_pos : word; inline;
begin
Result := Pos('-', lng);
if Result = 0 then
Result := Pos('_', lng);
end;
begin
lng := LowerCase(ALang);
p := sep_pos;
if p > 0 then
lng := copy(lng, 1, p-1);
Result := (lng = 'ar') or // Arabic
(lng = 'he') or // Hebrew
(lng = 'yi') or // Yiddish
// The languages bellow usually use arabic as the language name
(lng = 'dv') or
(lng = 'ps') or
(lng = 'az') or
(lng = 'fa') or
(lng = 'ks') or
(lng = 'ku') or
(lng = 'pa') or
(lng = 'sd') or
(lng = 'tk') or
(lng = 'ug') or
(lng = 'ur') { or
Not sure about the following languages ...
They do not have 2 letters ISO standard are they in use ?
(lng = 'jpr') or
(lng = 'syr') or
(lng = 'nqo') or
(lng = 'jrb')
}
;
end;
function TApplication.Direction(ALang: String): TBiDiMode;
const
BidiModeMap: array[Boolean] of TBiDiMode = (bdLeftToRight, bdRightToLeft);
begin
Result := BidiModeMap[IsRTLLang(ALang)];
end;
|