5
5
interface
6
6
7
7
uses
8
- FMX.Forms, PythonEngine, WrapDelphiClasses, WrapFmxTypes, WrapFmxControls;
8
+ System.Classes, FMX.Forms,
9
+ PythonEngine, WrapFmxTypes, WrapDelphiClasses, WrapFmxControls;
9
10
10
11
type
11
12
TPyDelphiApplication = class (TPyDelphiComponent)
@@ -23,7 +24,9 @@ TPyDelphiCommonCustomForm = class(TPyDelphiFmxObject)
23
24
private
24
25
function GetDelphiObject : TCommonCustomForm;
25
26
procedure SetDelphiObject (const Value : TCommonCustomForm);
27
+ function HasFormRes (const AClass: TClass): boolean;
26
28
public
29
+ function CreateComponent (AOwner: TComponent): TComponent; override;
27
30
// Class methods
28
31
class function DelphiObjectClass : TClass; override;
29
32
// Properties
@@ -88,7 +91,7 @@ TPyDelphiScreen = class(TPyDelphiComponent)
88
91
implementation
89
92
90
93
uses
91
- WrapDelphi;
94
+ WrapDelphi, System.Types ;
92
95
93
96
{ Register the wrappers, the globals and the constants }
94
97
type
@@ -153,6 +156,46 @@ procedure TPyDelphiApplication.SetDelphiObject(const Value: TApplication);
153
156
154
157
{ TPyDelphiCommonCustomForm }
155
158
159
+ function TPyDelphiCommonCustomForm.CreateComponent (
160
+ AOwner: TComponent): TComponent;
161
+ type
162
+ TCommonCustomFormClass = class of TCommonCustomForm;
163
+ var
164
+ LClass: TClass;
165
+ LFormClass: TCommonCustomFormClass;
166
+ LClassName: string;
167
+ begin
168
+ // if we have a subclass of our Form wrapper, then check if we can find a
169
+ // Delphi class that would have the same name as the Python class.
170
+ // This would allow Python to instanciate an existing Delphi form class,
171
+ // insted of only using a blank form.
172
+ if (ob_type <> PythonType.TheTypePtr) then begin
173
+ LClassName := string(ob_type.tp_name);
174
+ LClass := GetClass(LClassName);
175
+ if not Assigned(LClass) then
176
+ LClass := GetClass(' T' + LClassName);
177
+ if Assigned(LClass) and LClass.InheritsFrom(TCommonCustomForm) then
178
+ LFormClass := TCommonCustomFormClass(LClass);
179
+ end else begin
180
+ // get de default form class
181
+ if DelphiObjectClass.InheritsFrom(TCommonCustomForm) then
182
+ LFormClass := TCommonCustomFormClass(DelphiObjectClass);
183
+ end ;
184
+
185
+ // if it's not a design form, so we create it as a non-resourced form,
186
+ // using the non-resourced constructor.
187
+ // if the Owner is TApplication, then we have to call its CreateForm method,
188
+ // otherwise we won't have a mian form defined, as the main form is the first
189
+ // created form. Of course, this is a concern only when Python controls all the
190
+ // GUI and calls Apllication.Run by itself.
191
+ if not HasFormRes(LFormClass) then
192
+ Result := LFormClass.CreateNew(AOwner)
193
+ else if (AOwner = Application) then
194
+ Application.CreateForm(LFormClass, Result)
195
+ else
196
+ Result := LFormClass.Create(AOwner);
197
+ end ;
198
+
156
199
class function TPyDelphiCommonCustomForm.DelphiObjectClass : TClass;
157
200
begin
158
201
Result := TCommonCustomForm;
@@ -163,6 +206,13 @@ function TPyDelphiCommonCustomForm.GetDelphiObject: TCommonCustomForm;
163
206
Result := TCommonCustomForm(inherited DelphiObject);
164
207
end ;
165
208
209
+ function TPyDelphiCommonCustomForm.HasFormRes (const AClass: TClass): boolean;
210
+ begin
211
+ Result := FindResource(
212
+ FindResourceHInstance(FindClassHInstance(AClass)),
213
+ PChar(AClass.ClassName), PChar(RT_RCDATA)) <> 0 ;
214
+ end ;
215
+
166
216
procedure TPyDelphiCommonCustomForm.SetDelphiObject (
167
217
const Value : TCommonCustomForm);
168
218
begin
0 commit comments