@@ -6,7 +6,8 @@ interface
6
6
7
7
uses
8
8
System.Classes, System.SysUtils, FMX.Forms,
9
- PythonEngine, WrapFmxTypes, WrapDelphiClasses, WrapFmxControls;
9
+ PythonEngine, WrapFmxTypes, WrapDelphiClasses, WrapFmxControls, WrapDelphi,
10
+ System.TypInfo, System.UITypes;
10
11
11
12
type
12
13
TPyDelphiApplication = class (TPyDelphiComponent)
@@ -20,6 +21,24 @@ TPyDelphiApplication = class(TPyDelphiComponent)
20
21
property DelphiObject: TApplication read GetDelphiObject write SetDelphiObject;
21
22
end ;
22
23
24
+ TCloseQueryEventHandler = class (TEventHandler)
25
+ protected
26
+ procedure DoEvent (Sender: TObject; var CanClose : Boolean);
27
+ public
28
+ constructor Create(PyDelphiWrapper : TPyDelphiWrapper; Component : TObject;
29
+ PropertyInfo : PPropInfo; Callable : PPyObject); override;
30
+ class function GetTypeInfo : PTypeInfo; override;
31
+ end ;
32
+
33
+ TCloseEventHandler = class (TEventHandler)
34
+ protected
35
+ procedure DoEvent (Sender: TObject; var Action: TCloseAction);
36
+ public
37
+ constructor Create(PyDelphiWrapper : TPyDelphiWrapper; Component : TObject;
38
+ PropertyInfo : PPropInfo; Callable : PPyObject); override;
39
+ class function GetTypeInfo : PTypeInfo; override;
40
+ end ;
41
+
23
42
TPyDelphiCommonCustomForm = class (TPyDelphiFmxObject)
24
43
private
25
44
function GetDelphiObject : TCommonCustomForm;
@@ -93,7 +112,7 @@ EInvalidFormClass = class(Exception);
93
112
implementation
94
113
95
114
uses
96
- WrapDelphi, System.Types;
115
+ System.Types;
97
116
98
117
{ Register the wrappers, the globals and the constants }
99
118
type
@@ -137,6 +156,9 @@ procedure TFormsRegistration.RegisterWrappers(
137
156
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiForm);
138
157
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiFrame);
139
158
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiScreen);
159
+
160
+ APyDelphiWrapper.EventHandlers.RegisterHandler(TCloseQueryEventHandler);
161
+ APyDelphiWrapper.EventHandlers.RegisterHandler(TCloseEventHandler);
140
162
end ;
141
163
142
164
{ TPyDelphiApplication }
@@ -156,6 +178,101 @@ procedure TPyDelphiApplication.SetDelphiObject(const Value: TApplication);
156
178
inherited DelphiObject := Value ;
157
179
end ;
158
180
181
+ { TCloseQueryEventHandler }
182
+
183
+ constructor TCloseQueryEventHandler.Create(PyDelphiWrapper: TPyDelphiWrapper;
184
+ Component: TObject; PropertyInfo: PPropInfo; Callable: PPyObject);
185
+ var
186
+ LMethod : TMethod;
187
+ begin
188
+ inherited ;
189
+ LMethod.Code := @TCloseQueryEventHandler.DoEvent;
190
+ LMethod.Data := Self;
191
+ SetMethodProp(Component, PropertyInfo, LMethod);
192
+ end ;
193
+
194
+ procedure TCloseQueryEventHandler.DoEvent (Sender: TObject;
195
+ var CanClose: Boolean);
196
+ var
197
+ LPyObject, LPyTuple, LPyResult, LPyCanClose : PPyObject;
198
+ LVarParam : TPyDelphiVarParameter;
199
+ begin
200
+ Assert(Assigned(PyDelphiWrapper));
201
+ if Assigned(Callable) and PythonOK then
202
+ with GetPythonEngine do begin
203
+ LPyObject := PyDelphiWrapper.Wrap(Sender);
204
+ LPyCanClose := CreateVarParam(PyDelphiWrapper, CanClose);
205
+ LVarParam := PythonToDelphi(LPyCanClose) as TPyDelphiVarParameter;
206
+ LPyTuple := PyTuple_New(2 );
207
+ GetPythonEngine.PyTuple_SetItem(LPyTuple, 0 , LPyObject);
208
+ GetPythonEngine.PyTuple_SetItem(LPyTuple, 1 , LPyCanClose);
209
+ try
210
+ LPyResult := PyObject_CallObject(Callable, LPyTuple);
211
+ if Assigned(LPyResult) then
212
+ begin
213
+ Py_DECREF(LPyResult);
214
+ CanClose := PyObject_IsTrue(LVarParam.Value ) = 1 ;
215
+ end ;
216
+ finally
217
+ Py_DECREF(LPyTuple);
218
+ end ;
219
+ CheckError;
220
+ end ;
221
+ end ;
222
+
223
+ class function TCloseQueryEventHandler.GetTypeInfo : PTypeInfo;
224
+ begin
225
+ Result := System.TypeInfo(TCloseQueryEvent);
226
+ end ;
227
+
228
+ { TCloseEventHandler }
229
+
230
+ constructor TCloseEventHandler.Create(PyDelphiWrapper: TPyDelphiWrapper;
231
+ Component: TObject; PropertyInfo: PPropInfo; Callable: PPyObject);
232
+ var
233
+ LMethod : TMethod;
234
+ begin
235
+ inherited ;
236
+ LMethod.Code := @TCloseEventHandler.DoEvent;
237
+ LMethod.Data := Self;
238
+ SetMethodProp(Component, PropertyInfo, LMethod);
239
+ end ;
240
+
241
+ procedure TCloseEventHandler.DoEvent (Sender: TObject; var Action: TCloseAction);
242
+ var
243
+ LPyObject, LPyTuple, LPyResult, LPyAction : PPyObject;
244
+ LVarParam : TPyDelphiVarParameter;
245
+ begin
246
+ Assert(Assigned(PyDelphiWrapper));
247
+ if Assigned(Callable) and PythonOK then
248
+ with GetPythonEngine do begin
249
+ LPyObject := PyDelphiWrapper.Wrap(Sender);
250
+ LPyAction := CreateVarParam(PyDelphiWrapper, NativeInt(Action));
251
+ LVarParam := PythonToDelphi(LPyAction) as TPyDelphiVarParameter;
252
+ LPyTuple := PyTuple_New(2 );
253
+ GetPythonEngine.PyTuple_SetItem(LPyTuple, 0 , LPyObject);
254
+ GetPythonEngine.PyTuple_SetItem(LPyTuple, 1 , LPyAction);
255
+ try
256
+ LPyResult := PyObject_CallObject(Callable, LPyTuple);
257
+ if Assigned(LPyResult) then
258
+ begin
259
+ Py_DECREF(LPyResult);
260
+ if PyLong_Check(LVarParam.Value ) and
261
+ CheckEnum(' TCloseAction' , PyLong_AsLong(LVarParam.Value ), Ord(Low(TCloseAction)), Ord(High(TCloseAction))) then
262
+ Action := TCloseAction(PyLong_AsLong(LVarParam.Value ));
263
+ end ;
264
+ finally
265
+ Py_DECREF(LPyTuple);
266
+ end ;
267
+ CheckError;
268
+ end ;
269
+ end ;
270
+
271
+ class function TCloseEventHandler.GetTypeInfo : PTypeInfo;
272
+ begin
273
+ Result := System.TypeInfo(TCloseEvent);
274
+ end ;
275
+
159
276
{ TPyDelphiCommonCustomForm }
160
277
161
278
function TPyDelphiCommonCustomForm.CreateComponent (
@@ -312,7 +429,7 @@ procedure TPyDelphiScreen.SetDelphiObject(const Value: TScreen);
312
429
inherited DelphiObject := Value ;
313
430
end ;
314
431
315
- initialization
432
+ Initialization
316
433
RegisteredUnits.Add(TFormsRegistration.Create);
317
434
318
435
end .
0 commit comments