Debian Codegraph @

src/delphi/castlefmxutils.pas

raw
1 {
2 Copyright 2023-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Utilities for Delphi FMX (FireMonkey). }
17 unit CastleFmxUtils;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Types, FMX.Dialogs,
24 CastleFileFilters;
25
26 { Convert file filters into FMX Dialog.Filter, Dialog.FilterIndex.
27 Suitable for both open and save dialogs (in FMX, TSaveDialog
28 descends from TOpenDialog).
29
30 Input filters are either given as a string FileFilters
31 (encoded just like for TFileFilterList.AddFiltersFromString),
32 or as TFileFilterList instance.
33
34 Output filters are set as appropriate properties of given Dialog instance.
35
36 When AllFields is false, then filters starting with "All " in the name,
37 like "All files", "All images", are not included in the output.
38
39 @groupBegin }
40 procedure FileFiltersToDialog(const FileFilters: string;
41 const Dialog: TOpenDialog; const AllFields: boolean = true); overload;
42 procedure FileFiltersToDialog(FFList: TFileFilterList;
43 const Dialog: TOpenDialog; const AllFields: boolean = true); overload;
44 { @groupEnd }
45
46 {$ifdef LINUX}
47 { Set mouse position, in screen coordinates.
48 WidgetNativeHandle is a GTK widget pointer, used to determine
49 display and screen where to set the pointer. }
50 procedure FmxSetMousePos(const WidgetNativeHandle: Pointer;
51 const Point: TPointF);
52 {$endif}
53
54 implementation
55
56 uses CTypes, CastleLog;
57
58 procedure FileFiltersToDialog(const FileFilters: string;
59 const Dialog: TOpenDialog; const AllFields: boolean = true);
60 var
61 OutFilter: String;
62 OutFilterIndex: Integer;
63 begin
64 TFileFilterList.LclFmxFiltersFromString(FileFilters,
65 OutFilter, OutFilterIndex, AllFields);
66 Dialog.Filter := OutFilter;
67 Dialog.FilterIndex := OutFilterIndex;
68 end;
69
70 procedure FileFiltersToDialog(FFList: TFileFilterList;
71 const Dialog: TOpenDialog; const AllFields: boolean = true);
72 var
73 OutFilter: String;
74 OutFilterIndex: Integer;
75 begin
76 FFList.LclFmxFilters(OutFilter, OutFilterIndex, AllFields);
77 Dialog.Filter := OutFilter;
78 Dialog.FilterIndex := OutFilterIndex;
79 end;
80
81 {$ifdef LINUX}
82 type
83 PGdkDevice = Pointer;
84 PGdkScreen = Pointer;
85 PGdkDisplay = Pointer;
86 PGdkDeviceManager = Pointer;
87 PGtkWidget = Pointer;
88
89 procedure gdk_device_warp(Device: PGdkDevice; Screen: PGdkScreen; X, Y: CInt); cdecl; external 'libgtk-3.so.0';
90
91 //function gdk_display_get_default: PGdkDisplay; cdecl; external 'libgtk-3.so.0';
92 function gtk_widget_get_display(widget: PGtkWidget): PGdkDisplay; cdecl; external 'libgtk-3.so.0';
93 function gdk_display_get_device_manager(display: PGdkDisplay): PGdkDeviceManager; cdecl; external 'libgtk-3.so.0';
94 function gdk_device_manager_get_client_pointer(manager: PGdkDeviceManager): PGdkDevice; cdecl; external 'libgtk-3.so.0';
95
96 //function gdk_screen_get_default: PGdkScreen; cdecl; external 'libgtk-3.so.0';
97 function gtk_widget_get_screen(widget: PGtkWidget): PGdkScreen; cdecl; external 'libgtk-3.so.0';
98
99 procedure FmxSetMousePos(const WidgetNativeHandle: Pointer;
100 const Point: TPointF);
101 var
102 Display: PGdkDisplay;
103 DeviceManager: PGdkDeviceManager;
104 Device: PGdkDevice;
105 Screen: PGdkScreen;
106 begin
107 { Get main device (mouse) following
108 https://stackoverflow.com/questions/24844489/how-to-use-gdk-device-get-position .
109 We use Display and Screen that correspond to the WidgetNativeHandle .
110 Then we "warp" (set mouse position) using
111 https://docs.gtk.org/gdk3/method.Device.warp.html . }
112
113 Display := gtk_widget_get_display(WidgetNativeHandle);
114 DeviceManager := gdk_display_get_device_manager(Display);
115 Device := gdk_device_manager_get_client_pointer(DeviceManager);
116
117 Screen := gtk_widget_get_screen(WidgetNativeHandle);
118
119 //WritelnLog('Mouse', 'Warping mouse to %f %f', [Point.X, Point.Y]);
120 gdk_device_warp(Device, Screen, Round(Point.X), Round(Point.Y));
121 end;
122 {$endif}
123
124 end.