forked from lmbelo/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapFmxForms.pas
More file actions
318 lines (264 loc) · 8.64 KB
/
Copy pathWrapFmxForms.pas
File metadata and controls
318 lines (264 loc) · 8.64 KB
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
{$I ..\Definition.Inc}
unit WrapFmxForms;
interface
uses
System.Classes, System.SysUtils, FMX.Forms,
PythonEngine, WrapFmxTypes, WrapDelphiClasses, WrapFmxControls;
type
TPyDelphiApplication = class(TPyDelphiComponent)
private
function GetDelphiObject: TApplication;
procedure SetDelphiObject(const Value: TApplication);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TApplication read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiCommonCustomForm = class(TPyDelphiFmxObject)
private
function GetDelphiObject: TCommonCustomForm;
procedure SetDelphiObject(const Value: TCommonCustomForm);
function HasFormRes(const AClass: TClass): boolean;
public
function CreateComponent(AOwner: TComponent): TComponent; override;
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TCommonCustomForm read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiCustomForm = class(TPyDelphiCommonCustomForm)
private
function GetDelphiObject: TCustomForm;
procedure SetDelphiObject(const Value: TCustomForm);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TCustomForm read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiCustomPopupForm = class(TPyDelphiCustomForm)
private
function GetDelphiObject: TCustomPopupForm;
procedure SetDelphiObject(const Value: TCustomPopupForm);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TCustomPopupForm read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiForm = class(TPyDelphiCustomForm)
private
function GetDelphiObject: TForm;
procedure SetDelphiObject(const Value: TForm);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TForm read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiFrame = class(TPyDelphiControl)
private
function GetDelphiObject: TFrame;
procedure SetDelphiObject(const Value: TFrame);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TFrame read GetDelphiObject write SetDelphiObject;
end;
TPyDelphiScreen = class(TPyDelphiComponent)
private
function GetDelphiObject: TScreen;
procedure SetDelphiObject(const Value: TScreen);
public
// Class methods
class function DelphiObjectClass: TClass; override;
// Properties
property DelphiObject: TScreen read GetDelphiObject write SetDelphiObject;
end;
EInvalidFormClass = class(Exception);
implementation
uses
WrapDelphi, System.Types;
{ Register the wrappers, the globals and the constants }
type
TFormsRegistration = class(TRegisteredUnit)
public
function Name : string; override;
procedure RegisterWrappers(APyDelphiWrapper : TPyDelphiWrapper); override;
procedure DefineVars(APyDelphiWrapper : TPyDelphiWrapper); override;
procedure DefineFunctions(APyDelphiWrapper : TPyDelphiWrapper); override;
end;
{ TFormsRegistration }
procedure TFormsRegistration.DefineFunctions(
APyDelphiWrapper: TPyDelphiWrapper);
begin
inherited;
end;
procedure TFormsRegistration.DefineVars(APyDelphiWrapper: TPyDelphiWrapper);
begin
inherited;
// Singletons
APyDelphiWrapper.DefineVar('Application', Application);
APyDelphiWrapper.DefineVar('Screen', Screen);
end;
function TFormsRegistration.Name: string;
begin
Result := 'Forms';
end;
procedure TFormsRegistration.RegisterWrappers(
APyDelphiWrapper: TPyDelphiWrapper);
begin
inherited;
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiApplication);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiCommonCustomForm);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiCustomForm);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiCustomPopupForm);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiForm);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiFrame);
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiScreen);
end;
{ TPyDelphiApplication }
class function TPyDelphiApplication.DelphiObjectClass: TClass;
begin
Result := TApplication;
end;
function TPyDelphiApplication.GetDelphiObject: TApplication;
begin
Result := TApplication(inherited DelphiObject);
end;
procedure TPyDelphiApplication.SetDelphiObject(const Value: TApplication);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiCommonCustomForm }
function TPyDelphiCommonCustomForm.CreateComponent(
AOwner: TComponent): TComponent;
type
TCommonCustomFormClass = class of TCommonCustomForm;
var
LClass: TClass;
LFormClass: TCommonCustomFormClass;
LClassName: string;
begin
LFormClass := nil;
//get de default form class
if DelphiObjectClass.InheritsFrom(TCommonCustomForm) then
LFormClass := TCommonCustomFormClass(DelphiObjectClass);
//if we have a subclass of our Form wrapper, then check if we can find a
//Delphi class that would have the same name as the Python class.
//This would allow Python to instanciate an existing Delphi form class,
//insted of only using a blank form.
if (ob_type <> PythonType.TheTypePtr) then begin
LClassName := string(ob_type.tp_name);
LClass := GetClass(LClassName);
if not Assigned(LClass) then
LClass := GetClass('T' + LClassName);
if Assigned(LClass) and LClass.InheritsFrom(TCommonCustomForm) then
LFormClass := TCommonCustomFormClass(LClass);
end;
if not Assigned(LFormClass) then
raise EInvalidFormClass.CreateFmt('Type %s is not a valid form class', [
DelphiObjectClass.ClassName]);
//if it's not a design form, so we create it as a non-resourced form,
//using the non-resourced constructor.
//if the Owner is TApplication, then we have to call its CreateForm method,
//otherwise we won't have a mian form defined, as the main form is the first
//created form. Of course, this is a concern only when Python controls all the
//GUI and calls Apllication.Run by itself.
if not HasFormRes(LFormClass) then
Result := LFormClass.CreateNew(AOwner)
else if (AOwner = Application) then
Application.CreateForm(LFormClass, Result)
else
Result := LFormClass.Create(AOwner);
end;
class function TPyDelphiCommonCustomForm.DelphiObjectClass: TClass;
begin
Result := TCommonCustomForm;
end;
function TPyDelphiCommonCustomForm.GetDelphiObject: TCommonCustomForm;
begin
Result := TCommonCustomForm(inherited DelphiObject);
end;
function TPyDelphiCommonCustomForm.HasFormRes(const AClass: TClass): boolean;
begin
Result := FindResource(
FindResourceHInstance(FindClassHInstance(AClass)),
PChar(AClass.ClassName), PChar(RT_RCDATA)) <> 0;
end;
procedure TPyDelphiCommonCustomForm.SetDelphiObject(
const Value: TCommonCustomForm);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiCustomForm }
class function TPyDelphiCustomForm.DelphiObjectClass: TClass;
begin
Result := TCustomForm;
end;
function TPyDelphiCustomForm.GetDelphiObject: TCustomForm;
begin
Result := TCustomForm(inherited DelphiObject);
end;
procedure TPyDelphiCustomForm.SetDelphiObject(const Value: TCustomForm);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiCustomPopupForm }
class function TPyDelphiCustomPopupForm.DelphiObjectClass: TClass;
begin
Result := TCustomPopupForm;
end;
function TPyDelphiCustomPopupForm.GetDelphiObject: TCustomPopupForm;
begin
Result := TCustomPopupForm(inherited DelphiObject);
end;
procedure TPyDelphiCustomPopupForm.SetDelphiObject(
const Value: TCustomPopupForm);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiForm }
class function TPyDelphiForm.DelphiObjectClass: TClass;
begin
Result := TForm;
end;
function TPyDelphiForm.GetDelphiObject: TForm;
begin
Result := TForm(inherited DelphiObject);
end;
procedure TPyDelphiForm.SetDelphiObject(const Value: TForm);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiFrame }
class function TPyDelphiFrame.DelphiObjectClass: TClass;
begin
Result := TFrame;
end;
function TPyDelphiFrame.GetDelphiObject: TFrame;
begin
Result := TFrame(inherited DelphiObject);
end;
procedure TPyDelphiFrame.SetDelphiObject(const Value: TFrame);
begin
inherited DelphiObject := Value;
end;
{ TPyDelphiScreen }
class function TPyDelphiScreen.DelphiObjectClass: TClass;
begin
Result := TScreen;
end;
function TPyDelphiScreen.GetDelphiObject: TScreen;
begin
Result := TScreen(inherited DelphiObject);
end;
procedure TPyDelphiScreen.SetDelphiObject(const Value: TScreen);
begin
inherited DelphiObject := Value;
end;
initialization
RegisteredUnits.Add(TFormsRegistration.Create);
end.