-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
FMX.Platform.Win1.pas
4581 lines (4143 loc) · 149 KB
/
FMX.Platform.Win1.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-2023 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
System.Messaging, Winapi.CommCtrl, Winapi.Windows, Winapi.ActiveX, System.Types, Winapi.Messages, 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
FForm: TCommonCustomForm;
FWnd: HWND;
FZOrderManager: TWinZOrderManager;
FBufferBitmap: THandle;
FBitmapInfo: TBitmapInfo;
FBufferBits: Pointer;
FBufferHandle: THandle;
FBufferSize: TSize;
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: TRectF);
procedure SetWndBounds(const Value: TRect);
procedure SetClientSize(const Value: TSizeF);
procedure CalculateClientSizeFromWindow;
procedure ApplyWndBoundsToWnd;
procedure ApplyWndClientSizeToWnd;
procedure CalcNearestIntegerMultiple;
function GetNearestIntegerMultiple: Integer;
function GetWndBorderSize: TSize;
protected
function GetBounds: TRectF; virtual;
function GetClientSize: TSizeF; virtual;
function GetWndBounds: TRect; virtual;
function GetWndClientSize: TSize; virtual;
function GetTransparency: Boolean; virtual;
function GetScale: Single; override;
{ WinAPI Messages }
procedure WMDpiChanged(var AMessage: TWMDpi); message WM_DPICHANGED;
procedure WMWindowPosChanging(var AMessage: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
procedure WMWindowPosChanged(var AMessage: TWMWindowPosChanged); message WM_WINDOWPOSCHANGED;
procedure WMSize(var AMessage: TWMSize); message WM_SIZE;
procedure WMGetMinMaxInfo(var Message: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
public
constructor Create(const AForm: TCommonCustomForm; const AWnd: HWND);
destructor Destroy; override;
class procedure SetForcedScale(const ANewScale: Single);
procedure SetForcedScaleForForm(const ANewScale: Single);
{ Buffer }
procedure CreateBuffer(const Width, Height: Integer {px});
procedure ResizeBuffer(const Width, Height: Integer {px});
procedure FreeBuffer;
{ Rounding to match scale }
/// <summary>Rounds physical size of client window to match scale and logical client window size.</summary>
function RoundWndClientSizeToMatchScale(const AWndClientSize: TSize): TSize;
/// <summary>Rounds physical size of window to match scale and logical window bounds.</summary>
function RoundWndSizeToMatchScale(const AWndSize: TSize): TSize;
{ PX to DP conversions }
/// <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 NearestIntegerMultiple: Integer read GetNearestIntegerMultiple;
public
/// <summary>Native WinAPI window handle.</summary>
property Wnd: HWND read FWnd;
/// <summary>Returns related FMX form.</summary>
property Form: TCommonCustomForm read FForm;
/// <summary>The Z-Order manager responsible for using native controls on this form.</summary>
property ZOrderManager: TWinZOrderManager read GetZOrderManager;
/// <summary>Is form transparent or not?</summary>
property Transparency: Boolean read GetTransparency;
{ Buffer }
property BufferBits: Pointer read FBufferBits;
property BufferHandle: THandle read FBufferHandle;
/// <summary>Allocated buffer size for native window in px.</summary>
property BufferSize: TSize read FBufferSize;
{ Frame/Bounds }
/// <summary>Logical form's client size (dp).</summary>
property ClientSize: TSizeF read GetClientSize write SetClientSize;
/// <summary>Logical form bounds (dp).</summary>
property Bounds: TRectF read GetBounds write SetBounds;
/// <summary>Physical size of client part (px).</summary>
property WndClientSize: TSize read GetWndClientSize;
/// <summary>Physical form bounds (px).</summary>
property WndBounds: TRect read GetWndBounds write SetWndBounds;
/// <summary>Total physical thickness of window frame (px).</summary>
property WndBorderSize: TSize read GetWndBorderSize;
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 PxToDp(const AValue: TPoint): TPointF;
function DpToPx(const AValue: TPointF): TPoint;
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
IDC_NODROP = PChar(32760);
IDC_DRAG = PChar(32759);
IDC_MULTIDRAG = PChar(32756);
IDC_SQLWAIT = PChar(32755);
type
TApplicationHWNDProc = function: HWND;
procedure RegisterApplicationHWNDProc(const Proc: TApplicationHWNDProc);
procedure ShutDown; cdecl;
implementation
{$SCOPEDENUMS OFF}
uses
System.IOUtils, Winapi.CommDlg, 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, FMX.Platform.Screen.Win, FMX.Platform.SaveState.Win, FMX.Platform.Device.Win,
FMX.Platform.Metrics.Win, FMX.VirtualKeyboard.Win, FMX.Platform.Timer.Win, FMX.Platform.Logger.Win,
FMX.Platform.Menu.Win;
type
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 }
TFullScreenSavedState = record
BorderStyle: TFmxFormBorderStyle;
WindowState: TWindowState;
Position: TPointF;
Size: TSizeF;
IsFullscreen: Boolean;
end;
TFormInfo = class
WasLeftMouseButtonPressed: Boolean;
end;
TImmManager = class;
TPlatformWin = class(TInterfacedObject, IFMXApplicationService, IFMXWindowService, IFMXDragDropService,
IFMXCursorService, IFMXMouseService, IFMXTextService, IFMXContextService, IFMXCanvasService, IFMXWindowBorderService,
IFMXFullScreenWindowService, IFMXGestureRecognizersService, IFMXWindowsTouchService, IFMXKeyMappingService,
IFMXWindowConstraintsService)
private
FTitle: string;
FDefaultTitle: string;
FApplicationHWNDProc: TApplicationHWNDProc;
FApplicationHWND: HWND;
FIsOutApplicationHWND: Boolean;
FFullScreenSupport: TDictionary<TCommonCustomForm, TFullScreenSavedState>;
FDiableUpdateState: Boolean;
FThreadSyncHandle: HWND;
FInPaintUpdateRects: TDictionary<TWindowHandle, TUpdateRects>;
FTerminating: Boolean;
FRunning: Boolean;
FCursor: TCursor;
FCaptionChangedId: Integer;
FMultiTouchManager: TMultiTouchManagerWin;
FEnabledInteractiveGestures: TInteractiveGestures;
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;
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;
{ 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;
{ IFMXWindowConstraintsService }
procedure SetConstraints(const AForm: TCommonCustomForm; const AMinWidth, AMinHeight, AMaxWidth, AMaxHeight: Single);
{ IFMXFullScreenWindowService }
// 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;
{ IFMXTextService }
function GetTextServiceClass: TTextServiceClass;
{ IFMXContextService }
procedure RegisterContextClasses;
procedure UnregisterContextClasses;
{ IFMXCanvasService }
procedure RegisterCanvasClasses;
procedure UnregisterCanvasClasses;
{ 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);
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;
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;
function HasMarkedText: Boolean; 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;
/// <summary>Returns caret position in <c>MarkedText</c> bounds.</summary>
property MarkedTextCursorPosition: Integer read FMarkedTextCursorPosition write SetMarkedTextCursorPosition;
end;
var
VirtualKeyboardWin: TWinVirtualKeyboard;
MultiDisplayWin: TWinMultiDisplay;
MenuServiceWin: TWinMenuService;
const
CF_FMOBJECT = CF_PRIVATEFIRST + 1;
var
WindowAtom: TAtom;
WindowAtomString: string;
PlatformWin: TPlatformWin;
CapturedGestureControl: TComponent;
/// <summary>This registered window message is responsible for getting an instance of the component by handle.</summary>
FM_GET_OBJECT_INSTANCE: DWORD;
ControlAtom: TAtom;
ControlAtomString: string;
function PxToDp(const AValue: TPoint): TPointF;
begin
if MultiDisplayWin = nil then
Result := AValue
else
Result := MultiDisplayWin.PxToDp(AValue);
end;
function DpToPx(const AValue: TPointF): TPoint;
begin
if MultiDisplayWin = nil then
Result := AValue.Truncate
else
Result := MultiDisplayWin.DpToPx(AValue);
end;
function FindWindow(Handle: HWND): TCommonCustomForm;
begin
if Handle = 0 then
Result := nil
else if GlobalFindAtomW(PChar(WindowAtomString)) = WindowAtom then
Result := Pointer(GetProp(Handle, MakeIntAtom(WindowAtom)))
else
Result := nil;
end;
function ObjectFromHWnd(Handle: HWnd): TWinControl;
var
OwningProcess: DWORD;
ProcessId: DWORD;
begin
ProcessId := GetCurrentProcessId;
if (GetWindowThreadProcessId(Handle, OwningProcess) <> 0) and (OwningProcess = ProcessId) then
Result := Pointer(SendMessage(Handle, FM_GET_OBJECT_INSTANCE, ProcessId, 0))
else
Result := nil;
end;
{ Find a TControl given a window handle }
{ The global atom table is trashed when the user logs off. The extra test
below protects UI interactive services after the user logs off.
Added additional tests to enure that Handle is at least within the same
process since otherwise a bogus result can occur due to problems with
GlobalFindAtom in Winapi.Windows. }
function FindNativeControl(const AHandle: HWND): TControl;
var
OwningProcess: DWORD;
begin
Result := nil;
if (AHandle <> 0) and (GetWindowThreadProcessID(AHandle, OwningProcess) <> 0) and (OwningProcess = GetCurrentProcessId) then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result := Pointer(GetProp(AHandle, MakeIntAtom(ControlAtom)))
else
Result := ObjectFromHWnd(AHandle);
end;
end;
function IsFMXControl(const AHandle: HWND): Boolean;
begin
Result := FindNativeControl(AHandle) <> nil;
end;
function FmxHandleToHWND(const FmxHandle: TWindowHandle): HWND;
begin
if FmxHandle <> nil then
Result := WindowHandleToPlatform(FmxHandle).Wnd
else
Result := 0;
end;
function WindowHandleToPlatform(const AHandle: TWindowHandle): TWinWindowHandle;
begin
Result := TWinWindowHandle(AHandle);
end;
function FormToHWND(Form: TCommonCustomForm): HWND;
begin
if (Form <> nil) and (Form.Handle is TWinWindowHandle) then
Result := TWinWindowHandle(Form.Handle).Wnd
else
Result := 0;
end;
function ApplicationHWND: HWND;
begin
if PlatformWin <> nil then
Result := PlatformWin.GetApplicationHWND
else
Result := 0;
end;
procedure DisableProcessWindowsGhosting;
var
DisableProcessWindowsGhostingProc: procedure;
begin
DisableProcessWindowsGhostingProc := GetProcAddress(GetModuleHandle('user32.dll'), 'DisableProcessWindowsGhosting');
if Assigned(DisableProcessWindowsGhostingProc) then
DisableProcessWindowsGhostingProc;
end;
procedure RegisterCorePlatformServices;
var
MetricsService: TWinMetricsServices;
begin
PlatformWin := TPlatformWin.Create;
TPlatformServices.Current.AddPlatformService(IFMXApplicationService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXTimerService, PlatformWin.TimerService);
TPlatformServices.Current.AddPlatformService(IFMXWindowService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXWindowConstraintsService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDragDropService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXCursorService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXMouseService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXTextService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXContextService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXCanvasService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXWindowBorderService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXLoggingService, TWinLoggerService.Create);
TPlatformServices.Current.AddPlatformService(IFMXFullScreenWindowService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXGestureRecognizersService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXWindowsTouchService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXKeyMappingService, PlatformWin);
TPlatformServices.Current.AddPlatformService(IFMXDeviceService, TWinDeviceServices.Create);
TPlatformServices.Current.AddPlatformService(IFMXSaveStateService, TWinSaveStateService.Create);
MetricsService := TWinMetricsServices.Create;
TPlatformServices.Current.AddPlatformService(IFMXLocaleService, MetricsService);
TPlatformServices.Current.AddPlatformService(IFMXDefaultMetricsService, MetricsService);
TPlatformServices.Current.AddPlatformService(IFMXSystemInformationService, MetricsService);
TPlatformServices.Current.AddPlatformService(IFMXSystemFontService, MetricsService);
TPlatformServices.Current.AddPlatformService(IFMXListingService, MetricsService);
VirtualKeyboardWin := TWinVirtualKeyboard.Create;
TPlatformServices.Current.AddPlatformService(IFMXVirtualKeyboardService, VirtualKeyboardWin);
MultiDisplayWin := TWinMultiDisplay.Create;
TPlatformServices.Current.AddPlatformService(IFMXMultiDisplayService, MultiDisplayWin);
TPlatformServices.Current.AddPlatformService(IFMXScreenService, MultiDisplayWin);
TPlatformServices.Current.AddPlatformService(IFMXDeviceMetricsService, MultiDisplayWin);
MenuServiceWin := TWinMenuService.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
if TPlatformServices.Current <> nil then
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(IFMXWindowConstraintsService);
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;
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));
ControlAtomString := Format('ControlOfs%.8X%.8X', [HInstance, GetCurrentThreadID]);
ControlAtom := GlobalAddAtom(PChar(ControlAtomString));
FM_GET_OBJECT_INSTANCE := RegisterWindowMessage(PChar('FM_GET_OBJECT_INSTANCE')); // Do not localize
FFullScreenSupport := TDictionary<TCommonCustomForm, TFullScreenSavedState>.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);
FreeAndNil(FInPaintUpdateRects);
FreeAndNil(FFullScreenSupport);
FAcceleratorKeyRegistry := nil;
FTimerService := nil;
FreeAndNil(FKeyMapping);
GlobalDeleteAtom(ControlAtom);
GlobalDeleteAtom(WindowAtom);
if FThreadSyncHandle <> 0 then
DeallocateHWnd(FThreadSyncHandle);
if PlatformWin = Self then
PlatformWin := nil;
inherited;
end;
{ App }
{$WARN SYMBOL_PLATFORM OFF}
procedure TPlatformWin.Run;
var
MainForm: TCommonCustomForm;
begin
FRunning := True;
Application.RealCreateForms;
UpdateAppTitle;
if Application.MainForm <> nil then
begin
MainForm := Application.MainForm;
case CmdShow of
SW_SHOWMINNOACTIVE:
MainForm.WindowState := TWindowState.wsMinimized;
SW_SHOWMAXIMIZED:
MainForm.WindowState := TWindowState.wsMaximized;
end;
end;
repeat
try
Application.HandleMessage;
except
Application.HandleException(Self);
end;
until Application.Terminated;
end;
{$WARN SYMBOL_PLATFORM DEFAULT}
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;
TMessageManager.DefaultManager.SendMessage(nil, TApplicationTerminatingMessage.Create);
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 not FIsOutApplicationHWND and (FApplicationHWND <> 0) then
begin
WinApi.Windows.DestroyWindow(FApplicationHWND);
FApplicationHWND := 0;
end;
end;
function TPlatformWin.HandleMessage: Boolean;
const
CN_BASE = $BC00;
function IsMainForm(const AHandle: HWND): Boolean;
begin
Result := (Application.MainForm <> nil) and (AHandle = FormToHWND(Application.MainForm));
end;
function DefineTargetControlHandle(const AWnd: HWND): HWND;
begin
if IsMainForm(AWnd) then
Result := FormToHWND(Application.MainForm)
else
begin
Result := AWnd;
// Find the nearest Native FMX component. Non-FMX Control windows wont know what
// to do with CN_BASE offset messages anyway. TOleControl.WndProc needs this for TranslateAccelerator
while not IsFMXControl(Result) and (Result <> 0) do
Result := GetParent(Result);
if Result = 0 then
Result := AWnd;
end;
end;
function IsKeyMsg(var Msg: TMsg): Boolean;
var
Wnd: HWND;
WndProcessID, ProcessID: Cardinal;
begin
if not InRange(Msg.Message, WM_KEYFIRST, WM_KEYLAST) then
Exit(False);
Result := False;
Wnd := GetCapture;
if Wnd = 0 then
begin
Wnd := DefineTargetControlHandle(Msg.HWnd);
Result := SendMessage(Wnd, CN_BASE + Msg.Message, Msg.WParam, Msg.LParam) <> 0;
end
else
begin
GetWindowThreadProcessId(Wnd, WndProcessId);
GetWindowThreadProcessId(ApplicationHWND, ProcessId);
if (WndProcessID = ProcessID) then
Result := SendMessage(Wnd, CN_BASE + Msg.Message, Msg.WParam, Msg.LParam) <> 0;
end;
end;
var
Msg: TMsg;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message = WM_QUIT then
Application.Terminated := True
else if not IsKeyMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
if FIsPostQuitMessage then
PostQuitMessage(0);
end;
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:
Result[I] := TMarkedTextAttribute.Converted;
ATTR_TARGET_NOTCONVERTED:
Result[I] := TMarkedTextAttribute.TargetNotConverted;
ATTR_INPUT_ERROR:
Result[I] := TMarkedTextAttribute.InputError;
end;
end;
procedure TTextServiceWin.SetMarkedTextCursorPosition(const Value: Integer);
begin
FMarkedTextCursorPosition := Value;
end;
procedure TTextServiceWin.InternalSetMarkedText(const AMarkedText: string);
var
TextInput: ITextInput;
begin
if FIsInputting and Supports(Owner, ITextInput, TextInput) then
begin
FMarkedText := AMarkedText;
FMarkedTextCursorPosition := EnsureRange(FMarkedTextCursorPosition, 0, FMarkedText.Length);
TextInput.IMEStateUpdated;
end;
end;
procedure TTextServiceWin.InternalStartIMEInput;
var
TextInput: ITextInput;
begin
if not FIsInputting and Supports(Owner, ITextInput, TextInput) then
begin
FIsInputting := True;
Reset;
TextInput.StartIMEInput;
end;
end;
procedure TTextServiceWin.MarkedTextPositionChanged;
begin
inherited;
if FIsInputting then
RefreshImePosition;
end;
procedure TTextServiceWin.RecreateImmContext(const AFormHandle: TWindowHandle);
var
IMC: HIMC;
OldIMC: HIMC;