-
Notifications
You must be signed in to change notification settings - Fork 15
/
FMX.Platform.Win.pas
6648 lines (6063 loc) · 207 KB
/
FMX.Platform.Win.pas
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
{*******************************************************}
{ }
{ Delphi FireMonkey Platform }
{ }
{ Copyright(c) 2011-2021 Embarcadero Technologies, Inc. }
{ All rights reserved }
{ }
{*******************************************************}
unit FMX.Platform.Win;
(*$HPPEMIT '#if defined(WIN32) && defined(CreateWindow)'*)
(*$HPPEMIT ' #define __SAVE_CREATEWINDOW CreateWindow'*)
(*$HPPEMIT ' #undef CreateWindow'*)
(*$HPPEMIT '#endif'*)
(*$HPPEMIT END '#if defined(__SAVE_CREATEWINDOW)'*)
(*$HPPEMIT END ' #define CreateWindow __SAVE_CREATEWINDOW'*)
(*$HPPEMIT END ' #undef __SAVE_CREATEWINDOW'*)
(*$HPPEMIT END '#endif'*)
{$HPPEMIT NOUSINGNAMESPACE}
{$R-}
interface
{$SCOPEDENUMS ON}
uses
Winapi.CommCtrl, Winapi.Windows, Winapi.ActiveX, System.Types, System.Classes, System.UITypes, System.UIConsts,
System.Generics.Collections, FMX.Forms, FMX.Platform, FMX.Types, FMX.Graphics, FMX.ZOrder.Win;
type
TWinDropTarget = class;
PRgnRects = ^TRgnRects;
TRgnRects = array [0..MaxInt div SizeOf(TRect) - 1] of TRect;
TUpdateRects = array of TRectF;
TWinWindowHandle = class(TWindowHandle)
private class var
FForcedScale: Single;
private
FWnd: HWND;
FZOrderManager: TWinZOrderManager;
FBufferBitmap: THandle;
FBitmapInfo: TBitmapInfo;
FBufferBits: Pointer;
FBufferHandle: THandle;
FBufferSize: TSize;
FForm: TCommonCustomForm;
FDisableDeactivate: Boolean;
FWinDropTarget: TWinDropTarget;
FCurrentScale: Single;
FClientSize: TSizeF;
FWndClientSize: TSize;
FBounds: TRectF;
FWndBounds: TRect;
FNearestIntegerMultiple: Integer;
procedure UpdateLayer;
function GetZOrderManager: TWinZOrderManager;
procedure SetBounds(const Value: TRect);
procedure SetWndBounds(const Value: TRect);
procedure SetClientSize(const Value: TSize);
procedure UpdateClientSize;
procedure SetWindowSizeByClientSize;
procedure CalcNearestIntegerMultiple;
function GetNearestIntegerMultiple: Integer;
protected
function GetBounds: TRect; virtual;
function GetClientSize: TSize; virtual;
function GetWndBounds: TRect; virtual;
function GetWndClientSize: TSize; virtual;
function GetTransparency: Boolean; virtual;
function GetScale: Single; override;
public
constructor Create(const AForm: TCommonCustomForm; const AWnd: HWND);
destructor Destroy; override;
class procedure SetForcedScale(NewScale: Single);
procedure CreateBuffer(const Width, Height: Integer);
procedure ResizeBuffer(const Width, Height: Integer);
procedure FreeBuffer;
/// <summary>Rounds physical window bounds to match scale and logical window bounds.</summary>
procedure CorrectWindowSize(const WindowPos: PWindowPos);
procedure ScaleChanged;
procedure DpiChanged(const NewDpi: Integer);
/// <summary>Converts physical rect to logical.</summary>
function WndToForm(const Rect: TRect): TRectF; overload;
/// <summary>Converts physical rect to logical.</summary>
function WndToForm(const Rect: TRectF): TRectF; overload;
/// <summary>Converts logical rect to physical.</summary>
function FormToWnd(const Rect: TRectF): TRectF;
property Wnd: HWND read FWnd;
property Form: TCommonCustomForm read FForm;
property BufferBits: Pointer read FBufferBits;
property BufferHandle: THandle read FBufferHandle;
/// <summary>Allocated buffer size for native window.</summary>
property BufferSize: TSize read FBufferSize;
property ZOrderManager: TWinZOrderManager read GetZOrderManager;
property Transparency: Boolean read GetTransparency;
/// <summary>Logical form's client size.</summary>
property ClientSize: TSize read GetClientSize write SetClientSize;
property NearestIntegerMultiple: Integer read GetNearestIntegerMultiple;
/// <summary>Logical form bounds.</summary>
property Bounds: TRect read GetBounds write SetBounds;
/// <summary>Logical form bounds.</summary>
property BoundsF: TRectF read FBounds;
/// <summary>Physical form size.</summary>
property WndClientSize: TSize read GetWndClientSize;
/// <summary>Physical form bounds.</summary>
property WndBounds: TRect read GetWndBounds write SetWndBounds;
end;
TWinDropTarget = class(TComponent, IDropTarget)
private
Form: TCommonCustomForm;
function GetDataObject: TDragObject;
function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HRESULT; stdcall;
function DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HRESULT; stdcall;
function DragLeave: HRESULT; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HRESULT; stdcall;
end;
function FindWindow(Handle: HWND): TCommonCustomForm;
function WindowHandleToPlatform(const AHandle: TWindowHandle): TWinWindowHandle;
function FmxHandleToHWND(const FmxHandle: TWindowHandle): HWND;
function FormToHWND(Form: TCommonCustomForm): HWND;
function ApplicationHWND: HWND;
procedure RegisterCorePlatformServices;
procedure UnregisterCorePlatformServices;
const
// We cannot change constant values in update, however we reallocated resources. So new values are placed in the comments below.
IDC_NODROP = PChar(32767); // -> 32760
IDC_DRAG = PChar(32766); // -> 32759
IDC_MULTIDRAG = PChar(32763); // -> 32756
IDC_SQLWAIT = PChar(32762); // -> 32755
type
TApplicationHWNDProc = function: HWND;
procedure RegisterApplicationHWNDProc(const Proc: TApplicationHWNDProc);
procedure ShutDown; cdecl;
implementation
{$SCOPEDENUMS OFF}
uses
System.Messaging, System.IOUtils, Winapi.CommDlg, Winapi.Messages, Winapi.ShlObj, Winapi.MMSystem, Winapi.ShellAPI,
Winapi.MultiMon, Winapi.Imm, Winapi.UxTheme, Winapi.ShellScaling, System.Variants, System.SysUtils, System.Math,
System.Math.Vectors, System.StrUtils, System.DateUtils, System.RTLConsts, System.SyncObjs, System.Rtti, System.Devices,
FMX.Consts, FMX.Menus, FMX.Helpers.Win, FMX.Printer, FMX.Printer.Win, FMX.Dialogs.Win, FMX.Canvas.GDIP, FMX.Canvas.D2D,
FMX.Context.DX9, FMX.Context.DX11, FMX.Canvas.GPU, FMX.Forms.Border.Win, FMX.Controls.Win, FMX.Gestures.Win,
FMX.TextLayout, FMX.Text, FMX.Types3D, FMX.VirtualKeyboard, FMX.Controls, FMX.BehaviorManager, FMX.Styles,
FMX.MultiTouch.Win, FMX.ImgList, FMX.WebBrowser, FMX.Surfaces, FMX.Utils, FMX.KeyMapping, FMX.AcceleratorKey,
FMX.AcceleratorKey.Win;
type
EUnavailableMenuId = class(Exception);
TOpenMenuItem = class(TMenuItem);
MySTGMEDIUM = record // for compatibility
Tymed: DWORD;
Case Integer Of
0:
(HBITMAP: HBITMAP; UnkForRelease: Pointer { IUnknown } );
1:
(HMETAFILEPICT: THandle);
2:
(HENHMETAFILE: THandle);
3:
(HGLOBAL: HGLOBAL);
4:
(lpszFileName: POleStr);
5:
(stm: Pointer { IStream } );
6:
(stg: Pointer { IStorage } );
end;
{ TDropSource }
TDataObjectInfo = record
FormatEtc: TFormatEtc;
StgMedium: TStgMedium;
OwnedByDataObject: Boolean;
end;
TDataObjectInfoArray = array of TDataObjectInfo;
TDropSource = class(TComponent, IDataObject, IDropSource)
private
Data: TDragObject;
Formats: TDataObjectInfoArray;
{ IDropSource }
function QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HRESULT; stdcall;
function GiveFeedback(dwEffect: Longint): HRESULT; stdcall;
{ IDataObject }
function GetData(const FormatEtcIn: TFormatEtc; out Medium: TStgMedium): HRESULT; stdcall;
function GetDataHere(const FormatEtc: TFormatEtc; out Medium: TStgMedium): HRESULT; stdcall;
function QueryGetData(const FormatEtc: TFormatEtc): HRESULT; stdcall;
function GetCanonicalFormatEtc(const FormatEtc: TFormatEtc; out FormatEtcout: TFormatEtc): HRESULT; stdcall;
function SetData(const FormatEtc: TFormatEtc; var Medium: TStgMedium; fRelease: BOOL): HRESULT; stdcall;
function EnumFormatEtc(dwDirection: Longint; out EnumFormatEtc: IEnumFormatEtc): HRESULT; stdcall;
function dAdvise(const FormatEtc: TFormatEtc; advf: Longint; const advsink: IAdviseSink;
out dwConnection: Longint): HRESULT; stdcall;
function dUnadvise(dwConnection: Longint): HRESULT; stdcall;
function EnumdAdvise(out EnumAdvise: IEnumStatData): HRESULT; stdcall;
{ For IDropSourceHelper }
function FindFormatEtc(TestFormatEtc: TFormatEtc): Integer;
function EqualFormatEtc(FormatEtc1, FormatEtc2: TFormatEtc): Boolean;
function HGlobalClone(HGLOBAL: THandle): THandle;
function RetrieveOwnedStgMedium(Format: TFormatEtc; var StgMedium: TStgMedium): HRESULT;
function StgMediumIncRef(const InStgMedium: TStgMedium; var OutStgMedium: TStgMedium;
CopyInMedium: Boolean): HRESULT;
function CanonicalIUnknown(const TestUnknown: IUnknown): IUnknown;
end;
type
{ TPlatformWin }
TFullScreenParams = record
BorderStyle: TFmxFormBorderStyle;
WindowState: TWindowState;
Position: TPoint;
Size: TPoint;
public
function IsFullScreen: Boolean;
procedure Clean;
end;
TFormInfo = class
WasLeftMouseButtonPressed: Boolean;
end;
TWinTimerService = class;
TImmManager = class;
TPlatformWin = class(TInterfacedObject, IFMXApplicationService, IFMXSystemFontService,
IFMXWindowService, IFMXDragDropService, IFMXCursorService, IFMXMouseService,
IFMXScreenService, IFMXLocaleService, IFMXTextService, IFMXContextService, IFMXCanvasService, IFMXDeviceService,
IFMXWindowBorderService, IFMXSystemInformationService, IFMXLoggingService, IFMXFullScreenWindowService,
IFMXListingService, IFMXSaveStateService, IFMXDeviceMetricsService, IFMXGestureRecognizersService,
IFMXWindowsTouchService, IFMXDefaultMetricsService, IFMXKeyMappingService)
private const
DefaultWindowsFontSize = 12;
private
FTitle: string;
FDefaultTitle: string;
FApplicationHWNDProc: TApplicationHWNDProc;
FApplicationHWND: HWND;
FIsOutApplicationHWND: Boolean;
FFullScreenSupport: TDictionary<TCommonCustomForm, TFullScreenParams>;
FDiableUpdateState: Boolean;
FThreadSyncHandle: HWND;
FInPaintUpdateRects: TDictionary<TWindowHandle, TUpdateRects>;
FTerminating: Boolean;
FRunning: Boolean;
FCursor: TCursor;
FCaptionChangedId: Integer;
FMultiTouchManager: TMultiTouchManagerWin;
FEnabledInteractiveGestures: TInteractiveGestures;
FSaveStateStoragePath: string;
FDragAndDropActive: Boolean;
FKeyMapping: TKeyMapping;
FAcceleratorKeyRegistry: IFMXAcceleratorKeyRegistryService;
FIsPostQuitMessage: Boolean;
FTimerService: TWinTimerService;
FFormsInfo: TObjectDictionary<TCommonCustomForm, TFormInfo>;
{ IMM }
FImmManagers: TObjectDictionary<TCommonCustomForm, TImmManager>;
procedure ThreadSync(var Msg: TMessage);
procedure WakeMainThread(Sender: TObject);
function CreateAppHandle: HWND;
procedure MinimizeApp;
procedure RestoreApp;
// IFMXListingService
function GetListingHeaderBehaviors: TListingHeaderBehaviors;
function GetListingSearchFeatures: TListingSearchFeatures;
function GetListingTransitionFeatures: TListingTransitionFeatures;
function GetListingEditModeFeatures: TListingEditModeFeatures;
function IFMXListingService.GetHeaderBehaviors = GetListingHeaderBehaviors;
function IFMXListingService.GetSearchFeatures = GetListingSearchFeatures;
function IFMXListingService.GetTransitionFeatures = GetListingTransitionFeatures;
function IFMXListingService.GetEditModeFeatures = GetListingEditModeFeatures;
// IFMXSaveStateService
function GetSaveStateFileName(const ABlockName: string): string;
function GetSaveStateBlock(const ABlockName: string; const ABlockData: TStream): Boolean;
function SetSaveStateBlock(const ABlockName: string; const ABlockData: TStream): Boolean;
function GetSaveStateStoragePath: string;
procedure SetSaveStateStoragePath(const ANewPath: string);
function GetSaveStateNotifications: Boolean;
function IFMXSaveStateService.GetBlock = GetSaveStateBlock;
function IFMXSaveStateService.SetBlock = SetSaveStateBlock;
function IFMXSaveStateService.GetStoragePath = GetSaveStateStoragePath;
procedure IFMXSaveStateService.SetStoragePath = SetSaveStateStoragePath;
function IFMXSaveStateService.GetNotifications = GetSaveStateNotifications;
// IFMXDeviceMetricsService
function GetDisplayMetrics: TDeviceDisplayMetrics;
procedure UpdateAppTitle;
procedure CaptionChangedHandler(const Sender: TObject; const Msg: System.Messaging.TMessage);
function GetApplicationHWND: HWND;
procedure SetApplicationHWNDProc(const Value: TApplicationHWNDProc);
procedure UpdateApplicationHwnd;
//IFMXKeyMappingService
/// <summary>Registers a platform key as the given virtual key.</summary>
function RegisterKeyMapping(const PlatformKey, VirtualKey: Word; const KeyKind: TKeyKind): Boolean;
/// <summary>Unegisters a platform key as the given virtual key.</summary>
function UnregisterKeyMapping(const PlatformKey: Word): Boolean;
/// <summary>Obtains the virtual key from a given platform key.</summary>
function PlatformKeyToVirtualKey(const PlatformKey: Word; var KeyKind: TKeyKind): Word;
/// <summary>Obtains the platform key from a given virtual key.</summary>
function VirtualKeyToPlatformKey(const VirtualKey: Word): Word;
function GetImmManager(const Index: TCommonCustomForm): TImmManager;
function GetFormInfo(const Index: TCommonCustomForm): TFormInfo;
public
constructor Create;
destructor Destroy; override;
{ IFMXApplicationService }
procedure Run;
function HandleMessage: Boolean;
procedure WaitMessage;
function GetDefaultTitle: string;
function GetTitle: string;
procedure SetTitle(const Value: string);
function Terminating: Boolean;
function Running: Boolean;
procedure Terminate;
function GetVersionString: string;
property ApplicationHWND: HWND read GetApplicationHWND;
property ApplicationHWNDProc: TApplicationHWNDProc read FApplicationHWNDProc write SetApplicationHWNDProc;
{ IFMXSystemFontService }
function GetDefaultFontFamilyName: string;
function GetDefaultFontSize: Single;
{ IFMXWindowService }
function FindForm(const AHandle: TWindowHandle): TCommonCustomForm;
function CreateWindow(const AForm: TCommonCustomForm): TWindowHandle;
procedure DestroyWindow(const AForm: TCommonCustomForm);
procedure ReleaseWindow(const AForm: TCommonCustomForm);
procedure SetWindowState(const AForm: TCommonCustomForm; const AState: TWindowState);
procedure ShowWindow(const AForm: TCommonCustomForm);
procedure HideWindow(const AForm: TCommonCustomForm);
procedure BringToFront(const AForm: TCommonCustomForm);
procedure SendToBack(const AForm: TCommonCustomForm);
procedure Activate(const AForm: TCommonCustomForm);
function ShowWindowModal(const AForm: TCommonCustomForm): TModalResult;
function CanShowModal: Boolean;
procedure InvalidateWindowRect(const AForm: TCommonCustomForm; R: TRectF);
procedure InvalidateImmediately(const AForm: TCommonCustomForm);
procedure SetWindowRect(const AForm: TCommonCustomForm; ARect: TRectF);
function GetWindowRect(const AForm: TCommonCustomForm): TRectF;
function GetClientSize(const AForm: TCommonCustomForm): TPointF;
procedure SetClientSize(const AForm: TCommonCustomForm; const ASize: TPointF);
procedure SetWindowCaption(const AForm: TCommonCustomForm; const ACaption: string);
procedure SetCapture(const AForm: TCommonCustomForm);
procedure ReleaseCapture(const AForm: TCommonCustomForm);
function ClientToScreen(const AForm: TCommonCustomForm; const Point: TPointF): TPointF;
function ScreenToClient(const AForm: TCommonCustomForm; const Point: TPointF): TPointF;
function GetWindowScale(const AForm: TCommonCustomForm): Single;
// for desingtime and testing only
procedure SetFullScreen(const AForm: TCommonCustomForm; const AValue: Boolean);
function GetFullScreen(const AForm: TCommonCustomForm): Boolean;
procedure SetShowFullScreenIcon(const AForm: TCommonCustomForm; const AValue: Boolean);
{ IFMXWindowBorderService }
function CreateWindowBorder(const AForm: TCommonCustomForm): TWindowBorder;
{ IFMXDragDropService }
procedure BeginDragDrop(AForm: TCommonCustomForm; const Data: TDragObject; ABitmap: TBitmap);
{ IFMXCursorService }
procedure SetCursor(const ACursor: TCursor);
function GetCursor: TCursor;
{ IFMXMouseService }
function GetMousePos: TPointF;
{ IFMXScreenService }
function GetScreenSize: TPointF;
function GetScreenScale: Single;
function GetScreenOrientation: TScreenOrientation;
procedure SetSupportedScreenOrientations(const AOrientations: TScreenOrientations);
{ IFMXLocaleService }
function GetCurrentLangID: string;
function GetLocaleFirstDayOfWeek: string;
function GetFirstWeekday: Byte;
{ IFMXTextService }
function GetTextServiceClass: TTextServiceClass;
{ IFMXContextService }
procedure RegisterContextClasses;
procedure UnregisterContextClasses;
{ IFMXCanvasService }
procedure RegisterCanvasClasses;
procedure UnregisterCanvasClasses;
{ IFMXSystemInformationService }
function GetScrollingBehaviour: TScrollingBehaviours;
function GetMinScrollThumbSize: Single;
function GetCaretWidth: Integer;
function GetMenuShowDelay: Integer;
{ IFMXLoggingService }
procedure Log(const AFormat: string; const AParams: array of const);
{ IFMXGestureRecognizersService }
procedure AddRecognizer(const ARec: TInteractiveGesture; const AForm: TCommonCustomForm);
procedure RemoveRecognizer(const ARec: TInteractiveGesture; const AForm: TCommonCustomForm);
{ IFMXWindowsTouchService }
procedure HookTouchHandler(const AForm: TCommonCustomForm);
procedure UnhookTouchHandler(const AForm: TCommonCustomForm);
{ IFMXDeviceService }
function GetModel: string;
function GetFeatures: TDeviceFeatures;
function GetDeviceClass: TDeviceInfo.TDeviceClass;
{ IFMXDefaultMetricsService }
function SupportsDefaultSize(const AComponent: TComponentKind): Boolean;
function GetDefaultSize(const AComponent: TComponentKind): TSize;
property ImmManager[const Index: TCommonCustomForm]: TImmManager read GetImmManager;
property TimerService: TWinTimerService read FTimerService;
/// <summary>Returns additional platform dependent data about fmx form.</summary>
property FormInfo[const Index: TCommonCustomForm]: TFormInfo read GetFormInfo;
end;
TWin32TimerInfo = record
TimerID: UIntPtr; // the Windows timer ID for this timer
TimerHandle: TFmxHandle; // the unique FMX Handle for this timer
TimerFunc: TTimerProc; // owner function to handle timer
end;
TWinTimerService = class(TInterfacedObject, IFMXTimerService)
private
FHandleCounter: TFmxHandle;
FTimers: TList<TWin32TimerInfo>;
FPerformanceFrequency: Int64;
FTerminating: Boolean;
procedure DestroyTimers;
{ Handlers }
class procedure TimerCallback(window_hwnd: HWND; Msg: Longint; idEvent: UINT; dwTime: Longint); static; stdcall;
procedure ApplicationTerminatingHandler(const Sender: TObject; const Msg: System.Messaging.TMessage);
public
constructor Create;
destructor Destroy; override;
{ IFMXTimerService }
function CreateTimer(AInterval: Integer; ATimerFunc: TTimerProc): TFmxHandle;
function DestroyTimer(Timer: TFmxHandle): Boolean;
function GetTick: Double;
end;
TMenuLooperWin = class
private
FView: IMenuView;
function IsItemSelectable(const Item: TFmxObject): Boolean;
procedure SelectFirstMenuItem(const AView: IMenuView);
procedure SelectLastMenuItem(const AView: IMenuView);
procedure SelectNextMenuItem(const AView: IMenuView; const ABackward: Boolean);
function BackwardSelectNextMenuItem(const AView: IMenuView; const AStartInd, AEndInd: Integer): Boolean;
function ForwardSelectNextMenuItem(const AView: IMenuView; const AStartInd, AEndInd: Integer): Boolean;
public
procedure StartLoop(const AView: IMenuView);
procedure EndLoop(const AView: IMenuView);
end;
TWin32MenuInfo = record
MenuID: Integer;
FMXMenuItem: TMenuItem;
constructor Create(const AMenuId: Integer; const AnItem: TMenuItem);
end;
TMenuServiceWin = class(TInterfacedObject, IFMXMenuService)
private type
TState = (CreatingOSMenu, DestroyingMenuItem);
TStates = set of TState;
TMenuId = Integer;
private
FHMenuMap: TDictionary<TFmxHandle, TWin32MenuInfo>;
FHMenuIdMap: TDictionary<TMenuId, TFmxHandle>;
FStates: TStates;
FMenuLooper: TMenuLooperWin;
{ Menu Item Id}
function GenerateMenuId: TMenuId;
function AssignNewIdToMenu(const AParentMenu, AMenu: HMENU): TMenuId;
function FindMenuInfoById(const AMenuItemId: TMenuId; var AMenuInfo: TWin32MenuInfo): Boolean;
{ Removing }
procedure DestroysAllMenuItems(const AMenu: IItemsContainer);
procedure RemoveMenuFromMaps(const AMenuHandle: TFmxHandle);
{ Menu Item Bitmap }
procedure AddBitmapToMenu(const AParentMenu: HMENU; const AMenuItemId: TMenuId; const ABitmap: HBITMAP);
procedure RemoveBitmapFromMenu(const AParentMenu, AMenu: HMENU);
protected
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
procedure WMInitMenuPopup(var Message: TWMInitMenuPopup); message WM_INITMENUPOPUP;
procedure WMMenuSelect(var Message: TWMMenuSelect); message WM_MENUSELECT;
public
constructor Create;
destructor Destroy; override;
{ IFMXMenuService }
procedure StartMenuLoop(const AView: IMenuView);
function ShortCutToText(ShortCut: TShortCut): string;
procedure ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState);
function TextToShortCut(Text: string): Integer;
procedure CreateOSMenu(AForm: TCommonCustomForm; const AMenu: IItemsContainer);
procedure UpdateMenuItem(const AItem: IItemsContainer; AChange: TMenuItemChanges);
procedure DestroyMenuItem(const AItem: IItemsContainer);
function IsMenuBarOnWindowBorder: Boolean;
procedure UpdateMenuBar;
end;
TVirtualKeyboardWin = class(TInterfacedObject, IFMXVirtualKeyboardService)
private type
TvkbState = (None, Hidden, Shown);
private
FPath: string;
FExeName: string;
FWndClassName: string;
FHTmerLang: TFmxHandle;
FHTmerVisible: TFmxHandle;
FKBPresent: Boolean;
FFormHandle: HWND;
FInst: HINST;
FError: Boolean;
FLastvkbState: TvkbState;
FLastHandle: HWND;
FLastTime: TDateTime;
FNewvkbState: TvkbState;
FWait: Boolean;
FStepActivate: Integer;
FCodeKeyboard: HKL;
FTimerService: IFMXTimerService;
procedure KillTimerLang;
procedure TimerLangProc;
procedure StartTimerLang;
procedure KillTimerVisible;
procedure TimerVisibleProc;
procedure StartTimerVisible;
function FindKeyValue(const Key: HKey; const Name, Value, SubKeyName, SubValueName: string): string;
function GetVirtualKeyboardState: TVirtualKeyboardStates;
procedure vkbExecute(FormHandle: HWND);
function vkbHandle: HWND;
function vkbState: TvkbState;
function GetVKBounds: TRect;
protected
procedure Clear;
function IsAutoShow: Boolean;
public
constructor Create;
destructor Destroy; override;
function ShowVirtualKeyboard(const AControl: TFmxObject): Boolean;
function HideVirtualKeyboard: Boolean;
procedure SetTransientState(Value: Boolean);
property VirtualKeyboardState: TVirtualKeyboardStates read GetVirtualKeyboardState;
property ExeName: string read FExeName write FExeName;
property Path: string read FPath write FPath;
property WndClassName: string read FWndClassName write FWndClassName;
end;
{ TMultiDisplayWin }
TOrderedDictionary<TKey, TValue> = class
private
FValues: TList<TValue>;
FIndex: TDictionary<TKey, Integer>;
function GetCount: Integer;
function GetValues(const AIndex: Integer): TValue;
public
constructor Create;
destructor Destroy; override;
procedure Add(const AKey: TKey; const AValue: TValue);
function TryGetValue(const AKey: TKey; out AValue: TValue): Boolean;
procedure Clear;
property Values[const AIndex: Integer]: TValue read GetValues; default;
property Count: Integer read GetCount;
end;
TMultiDisplayWin = class(TInterfacedObject, IFMXMultiDisplayService)
private type
TParameter = (DisplayCount, Displays, WorkareaRect, DesktopRect);
TParameters = set of TParameter;
private
FDisplays: TOrderedDictionary<HMONITOR, TDisplay>;
FDisplayCount: Integer;
FWorkAreaRect: TRect;
FDesktopRect: TRect;
FOutdatedParameters: TParameters;
procedure AddDisplay(const AMonitorHandle: HMONITOR);
function FindDisplay(const AMonitorHandle: HMONITOR): TDisplay;
procedure UpdateDisplaysIfNeeded;
class function ScaleRect(const ARect: TRect; const AScale: Single): TRect;
class function GetMonitorScale(const AHandle: HMONITOR): Single;
public
constructor Create;
destructor Destroy; override;
{ IFMXMultiDisplayService }
procedure UpdateDisplayInformation;
function GetDisplayCount: Integer;
function GetWorkAreaRect: TRect;
function GetDesktopRect: TRect;
function GetDisplay(const AIndex: Integer): TDisplay;
function GetDesktopCenterRect(const ASize: TSize): TRect;
function DisplayFromWindow(const AHandle: TWindowHandle): TDisplay;
function DisplayFromPoint(const AHandle: TWindowHandle; const APoint: TPoint): TDisplay;
end;
TOpenForm = class(TCommonCustomForm);
TTextServiceWin = class;
TImmManager = class
private
[Weak] FForm: TCommonCustomForm;
function GetFormHandle: HWND;
procedure UpdateCompositionAttributes(const AContext: HIMC; const ATextService: TTextServiceWin);
procedure UpdateCompositionCursorPos(const AContext: HIMC; const ATextService: TTextServiceWin);
procedure ProcessImeParameters(const AContext: HIMC; const AParameters: LPARAM; const ATextService: TTextServiceWin);
protected
procedure WMStartComposition(var Message: TMessage); message WM_IME_STARTCOMPOSITION;
procedure WMComposition(var Message: TMessage); message WM_IME_COMPOSITION;
procedure WMEndComposition(var Message: TMessage); message WM_IME_ENDCOMPOSITION;
procedure WMSetContext(var Message: TMessage); message WM_IME_SETCONTEXT;
procedure WMNotify(var Message: TMessage); message WM_IME_NOTIFY;
function GetComposition(const AContext: HIMC): string;
function GetResultComposition(const AContext: HIMC): string;
public
constructor Create(const AForm: TCommonCustomForm);
function UpdateIMEWindowPosition: LRESULT;
property Form: TCommonCustomForm read FForm;
property FormHandle: HWND read GetFormHandle;
end;
{ Text Service }
TTextServiceWin = class(TTextService)
private const
LCID_Korean_Default = (SUBLANG_KOREAN shl 10) + LANG_KOREAN;
private
FMarkedText: string;
FIsInputting: Boolean;
FMarkedTextCursorPosition: Integer;
procedure SetMarkedTextCursorPosition(const Value: Integer);
procedure RecreateImmContext(const AFormHandle: TWindowHandle);
procedure Reset;
protected
procedure MarkedTextPositionChanged; override;
procedure CaretPositionChanged; override;
function GetMarketTextAttributes: TArray<TMarkedTextAttribute>; override;
public
CompAttrBuf: array of Byte;
procedure InternalSetMarkedText(const AMarkedText: string); override;
function InternalGetMarkedText: string; override;
procedure InternalStartIMEInput;
procedure InternalEndIMEInput;
procedure RefreshImePosition; override;
function TargetClausePosition: TPoint; override;
procedure EnterControl(const AFormHandle: TWindowHandle); override;
procedure ExitControl(const AFormHandle: TWindowHandle); override;
{ Deprecated }
procedure DrawSingleLine(const ACanvas: TCanvas; const ARect: TRectF; const AFirstVisibleChar: Integer;
const AFont: TFont; const AOpacity: Single; const AFlags: TFillTextFlags; const ATextAlign: TTextAlign;
const AVTextAlign: TTextAlign = TTextAlign.Center; const AWordWrap: Boolean = False); overload; override;
procedure DrawSingleLine(const ACanvas: TCanvas; const S: string; const ARect: TRectF; const Font: TFont;
const AOpacity: Single; const Flags: TFillTextFlags; const ATextAlign: TTextAlign;
const AVTextAlign: TTextAlign = TTextAlign.Center; const AWordWrap: Boolean = False); overload; override;
function HasMarkedText: Boolean; override;
/// <summary>Returns caret position in <c>MarkedText</c> bounds.</summary>
property MarkedTextCursorPosition: Integer read FMarkedTextCursorPosition write SetMarkedTextCursorPosition;
end;
var
VirtualKeyboardWin: TVirtualKeyboardWin;
MultiDisplayWin: TMultiDisplayWin;
MenuServiceWin: TMenuServiceWin;
const
CF_FMOBJECT = CF_PRIVATEFIRST + 1;
var
WindowAtom: TAtom;
WindowAtomString: string;
PlatformWin: TPlatformWin;
CapturedGestureControl: TComponent;
procedure DisableProcessWindowsGhosting;
var
DisableProcessWindowsGhostingProc: procedure;
begin
DisableProcessWindowsGhostingProc := GetProcAddress(GetModuleHandle('user32.dll'), 'DisableProcessWindowsGhosting');
if Assigned(DisableProcessWindowsGhostingProc) then
DisableProcessWindowsGhostingProc;
end;
procedure RegisterCorePlatformServices;
begin
PlatformWin := TPlatformWin.Create;
TPlatformServices.Current.AddPlatformService(IFMXApplicationService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXSystemFontService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXTimerService, PlatformWin.TimerService);
TPlatformServices.Current.AddPlatformService(IFMXWindowService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDragDropService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXCursorService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXMouseService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXScreenService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXLocaleService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXTextService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXContextService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXCanvasService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXWindowBorderService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXSystemInformationService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXLoggingService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXFullScreenWindowService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXListingService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXSaveStateService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDeviceMetricsService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXGestureRecognizersService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXWindowsTouchService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDeviceService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDefaultMetricsService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXKeyMappingService, PlatformWin);
VirtualKeyboardWin := TVirtualKeyboardWin.Create;
TPlatformServices.Current.AddPlatformService(IFMXVirtualKeyboardService, VirtualKeyboardWin);
MultiDisplayWin := TMultiDisplayWin.Create;
TPlatformServices.Current.AddPlatformService(IFMXMultiDisplayService, MultiDisplayWin);
MenuServiceWin := TMenuServiceWin.Create;
TPlatformServices.Current.AddPlatformService(IFMXMenuService, MenuServiceWin);
// If application becomes inactive while a modal dialog is opened it may hang, so we have to DisableProcessWindowsGhosting
DisableProcessWindowsGhosting;
end;
procedure UnregisterCorePlatformServices;
begin
TPlatformServices.Current.RemovePlatformService(IFMXMenuService);
TPlatformServices.Current.RemovePlatformService(IFMXDeviceService);
TPlatformServices.Current.RemovePlatformService(IFMXDeviceMetricsService);
TPlatformServices.Current.RemovePlatformService(IFMXApplicationService);
TPlatformServices.Current.RemovePlatformService(IFMXSystemFontService);
TPlatformServices.Current.RemovePlatformService(IFMXTimerService);
TPlatformServices.Current.RemovePlatformService(IFMXWindowService);
TPlatformServices.Current.RemovePlatformService(IFMXDragDropService);
TPlatformServices.Current.RemovePlatformService(IFMXCursorService);
TPlatformServices.Current.RemovePlatformService(IFMXMouseService);
TPlatformServices.Current.RemovePlatformService(IFMXScreenService);
TPlatformServices.Current.RemovePlatformService(IFMXLocaleService);
TPlatformServices.Current.RemovePlatformService(IFMXTextService);
TPlatformServices.Current.RemovePlatformService(IFMXContextService);
TPlatformServices.Current.RemovePlatformService(IFMXCanvasService);
TPlatformServices.Current.RemovePlatformService(IFMXWindowBorderService);
TPlatformServices.Current.RemovePlatformService(IFMXSystemInformationService);
TPlatformServices.Current.RemovePlatformService(IFMXFullScreenWindowService);
TPlatformServices.Current.RemovePlatformService(IFMXVirtualKeyboardService);
TPlatformServices.Current.RemovePlatformService(IFMXDefaultMetricsService);
TPlatformServices.Current.RemovePlatformService(IFMXLoggingService);
TPlatformServices.Current.RemovePlatformService(IFMXListingService);
TPlatformServices.Current.RemovePlatformService(IFMXSaveStateService);
TPlatformServices.Current.RemovePlatformService(IFMXGestureRecognizersService);
TPlatformServices.Current.RemovePlatformService(IFMXWindowsTouchService);
TPlatformServices.Current.RemovePlatformService(IFMXDefaultMetricsService);
TPlatformServices.Current.RemovePlatformService(IFMXKeyMappingService);
end;
procedure RaiseIfNil(const AObject: TObject; const AArgumentName: string);
begin
if AObject = nil then
raise EArgumentException.CreateFmt(SParamIsNil, [AArgumentName]);
end;
{ TPlatformWin }
constructor TPlatformWin.Create;
begin
inherited;
FIsOutApplicationHWND := False;
WindowAtomString := Format('FIREMONKEY%.8X', [GetCurrentProcessID]);
WindowAtom := GlobalAddAtomW(PChar(WindowAtomString));
FFullScreenSupport := TDictionary<TCommonCustomForm, TFullScreenParams>.Create;
FInPaintUpdateRects := TDictionary<TWindowHandle, TUpdateRects>.Create;
FThreadSyncHandle := AllocateHWnd(ThreadSync);
FKeyMapping := TKeyMapping.Create;
FAcceleratorKeyRegistry := TWinAcceleratorKeyRegistry.Create;
System.Classes.WakeMainThread := WakeMainThread;
Application := TApplication.Create(nil);
FCaptionChangedId := TMessageManager.DefaultManager.SubscribeToMessage(TMainCaptionChangedMessage,
CaptionChangedHandler);
FRunning := False;
FImmManagers := TObjectDictionary<TCommonCustomForm, TImmManager>.Create([doOwnsValues]);
FFormsInfo := TObjectDictionary<TCommonCustomForm, TFormInfo>.Create([doOwnsValues]);
FTimerService := TWinTimerService.Create;
end;
destructor TPlatformWin.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TMainCaptionChangedMessage, FCaptionChangedId);
FreeAndNil(FFormsInfo);
FreeAndNil(FImmManagers);
FreeAndNil(Application);
FInPaintUpdateRects.Free;
FFullScreenSupport.Free;
FAcceleratorKeyRegistry := nil;
FTimerService := nil;
FKeyMapping.Free;
GlobalDeleteAtom(WindowAtom);
if FThreadSyncHandle <> 0 then
DeallocateHWnd(FThreadSyncHandle);
if PlatformWin = Self then
PlatformWin := nil;
inherited;
end;
{ App }
procedure TPlatformWin.Run;
begin
FRunning := True;
Application.RealCreateForms;
UpdateAppTitle;
repeat
try
Application.HandleMessage;
except
Application.HandleException(Self);
end;
until Application.Terminated;
end;
function TPlatformWin.Terminating: Boolean;
begin
Result := FTerminating;
end;
function TPlatformWin.Running: Boolean;
begin
Result := FRunning;
end;
procedure TPlatformWin.Terminate;
begin
FRunning := False;
FTerminating := True;
FIsPostQuitMessage := True;
FMultiTouchManager.Free; // release multitouch early so that it does not hold reference to services
// We don't need destroy application handle, if we receive it from out through ApplicationHWNDProc.
if IsLibrary and FIsOutApplicationHWND and (FApplicationHWND <> 0) then
begin
WinApi.Windows.DestroyWindow(FApplicationHWND);
FApplicationHWND := 0;
end;
end;
function TPlatformWin.HandleMessage: Boolean;
var
Msg: TMsg;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
if FIsPostQuitMessage then
PostQuitMessage(0);
end
else
Application.Terminated := True;
end;
end;
procedure TPlatformWin.WaitMessage;
begin
Winapi.Windows.WaitMessage;
end;
function TPlatformWin.GetDefaultTitle: string;
begin
Result := FDefaultTitle;
end;
function TPlatformWin.GetTitle: string;
begin
Result := FTitle;
end;
procedure TPlatformWin.SetTitle(const Value: string);
begin
if FTitle <> Value then
begin
if (Value = SAppDefault) and (FDefaultTitle <> '') then
FTitle := FDefaultTitle
else
FTitle := Value;
UpdateAppTitle;
end;
end;
function TPlatformWin.GetVersionString: string;
const
UndefinedVersionInfo = Cardinal(-1);
var
VersionInfo: Cardinal;
begin
VersionInfo := GetFileVersion(ParamStr(0));
if VersionInfo <> UndefinedVersionInfo then
Result := Format('%d.%d', [HiWord(VersionInfo), LoWord(VersionInfo)])
else
Result := string.Empty;
end;
procedure TPlatformWin.UpdateApplicationHwnd;
var
OldApplicationHandler: HWND;
begin
OldApplicationHandler := FApplicationHWND;
// The first, we try to extract application handle from IDE.
if Assigned(FApplicationHWNDProc) then
begin
FApplicationHWND := FApplicationHWNDProc;
FIsOutApplicationHWND := FApplicationHWND <> 0;
end;
// If IDE or user doesn't set outter application handle, we create our own, if it wasn't created before.
if FApplicationHWND = 0 then
begin
FApplicationHWND := CreateAppHandle;
FIsOutApplicationHWND := False;
end;
if OldApplicationHandler <> FApplicationHWND then
UpdateAppTitle;
end;
procedure TPlatformWin.UpdateAppTitle;
begin
if FApplicationHWND <> 0 then
begin
{ Don't update the title when working in the IDE }
if (Application <> nil) and (Application.MainForm <> nil) and ((FTitle = SAppDefault) or
(FTitle = GetDefaultTitle)) then
SetWindowText(FApplicationHWND, PChar(Application.MainForm.Caption))
else if FTitle.IsEmpty then
SetWindowText(FApplicationHWND, PChar(GetDefaultTitle))
else
SetWindowText(FApplicationHWND, PChar(FTitle));
end;
end;
procedure TPlatformWin.CaptionChangedHandler(const Sender: TObject; const Msg: System.Messaging.TMessage);
begin
UpdateAppTitle;
end;
procedure TPlatformWin.WakeMainThread(Sender: TObject);
begin
PostMessage(FThreadSyncHandle, WM_NULL, 0, 0);
end;
{ Text Service }
function TTextServiceWin.GetMarketTextAttributes: TArray<TMarkedTextAttribute>;
var
I: Integer;
begin
if FMarkedText.IsEmpty or (Length(CompAttrBuf) = 0) then
begin
Result := [];
Exit;
end;
SetLength(Result, FMarkedText.Length);
for I := 0 to FMarkedText.Length - 1 do
case CompAttrBuf[I] of
ATTR_INPUT:
Result[I] := TMarkedTextAttribute.Input;
ATTR_TARGET_CONVERTED:
Result[I] := TMarkedTextAttribute.TargetConverted;
ATTR_CONVERTED: