55interface
66
77uses
8- FMX.Forms, PythonEngine, WrapDelphiClasses, WrapFmxTypes, WrapFmxControls;
8+ System.Classes, FMX.Forms,
9+ PythonEngine, WrapFmxTypes, WrapDelphiClasses, WrapFmxControls;
910
1011type
1112 TPyDelphiApplication = class (TPyDelphiComponent)
@@ -23,7 +24,9 @@ TPyDelphiCommonCustomForm = class(TPyDelphiFmxObject)
2324 private
2425 function GetDelphiObject : TCommonCustomForm;
2526 procedure SetDelphiObject (const Value : TCommonCustomForm);
27+ function HasFormRes (const AClass: TClass): boolean;
2628 public
29+ function CreateComponent (AOwner: TComponent): TComponent; override;
2730 // Class methods
2831 class function DelphiObjectClass : TClass; override;
2932 // Properties
@@ -88,7 +91,7 @@ TPyDelphiScreen = class(TPyDelphiComponent)
8891implementation
8992
9093uses
91- WrapDelphi;
94+ WrapDelphi, System.Types ;
9295
9396{ Register the wrappers, the globals and the constants }
9497type
@@ -153,6 +156,46 @@ procedure TPyDelphiApplication.SetDelphiObject(const Value: TApplication);
153156
154157{ TPyDelphiCommonCustomForm }
155158
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+
156199class function TPyDelphiCommonCustomForm.DelphiObjectClass : TClass;
157200begin
158201 Result := TCommonCustomForm;
@@ -163,6 +206,13 @@ function TPyDelphiCommonCustomForm.GetDelphiObject: TCommonCustomForm;
163206 Result := TCommonCustomForm(inherited DelphiObject);
164207end ;
165208
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+
166216procedure TPyDelphiCommonCustomForm.SetDelphiObject (
167217 const Value : TCommonCustomForm);
168218begin
0 commit comments