Skip to content

Commit

Permalink
Add AppCreateForm wrapper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanrussell authored Jan 2, 2025
1 parent 7b6a56d commit 974b258
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Projects/Setup.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ program Setup;

{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Copyright (C) 1997-2025 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Expand Down Expand Up @@ -300,7 +300,6 @@ begin
Application.MainFormOnTaskBar := True;
InitializeSetup;
MainForm := TMainForm.Create(Application);
Application.CreateForm(TWizardForm, WizardForm);
MainForm.InitializeWizard;
except
{ Halt on any exception }
Expand Down
1 change: 1 addition & 0 deletions Projects/Src/Setup.MainForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ procedure TMainForm.SetStep(const AStep: TSetupStep; const HandleExceptions: Boo

procedure TMainForm.InitializeWizard;
begin
WizardForm := AppCreateForm(TWizardForm) as TWizardForm;
if CodeRunner <> nil then begin
try
CodeRunner.RunProcedures('InitializeWizard', [''], False);
Expand Down
7 changes: 4 additions & 3 deletions Projects/Src/Setup.SelectLanguageForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{
Inno Setup
Copyright (C) 1997-2019 Jordan Russell
Copyright (C) 1997-2025 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Expand Down Expand Up @@ -36,7 +36,8 @@ function AskForLanguage: Boolean;
implementation

uses
Shared.Struct, SetupLdrAndSetup.Messages, Shared.SetupMessageIDs, Setup.MainFunc;
Shared.Struct, SetupLdrAndSetup.Messages, Shared.SetupMessageIDs,
Setup.MainFunc, Shared.CommonFunc.Vcl;

{$R *.DFM}

Expand All @@ -48,7 +49,7 @@ function AskForLanguage: Boolean;
I, J: Integer;
LangEntry: PSetupLanguageEntry;
begin
Application.CreateForm(TSelectLanguageForm, LangForm);
LangForm := AppCreateForm(TSelectLanguageForm) as TSelectLanguageForm;
try
for I := 0 to Entries[seLanguage].Count-1 do begin
LangEntry := Entries[seLanguage][I];
Expand Down
4 changes: 2 additions & 2 deletions Projects/Src/Setup.Uninstall.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Copyright (C) 1997-2025 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Expand Down Expand Up @@ -91,7 +91,7 @@ function TExtUninstallLog.ShouldRemoveSharedFile(const Filename: String): Boolea

procedure InitializeUninstallProgressForm;
begin
Application.CreateForm(TUninstallProgressForm, UninstallProgressForm);
UninstallProgressForm := AppCreateForm(TUninstallProgressForm) as TUninstallProgressForm;
UninstallProgressForm.Initialize(Title, UninstLog.AppName, ufModernStyle in UninstLog.Flags);
if CodeRunner <> nil then begin
try
Expand Down
19 changes: 18 additions & 1 deletion Projects/Src/Shared.CommonFunc.Vcl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Copyright (C) 1997-2025 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Expand Down Expand Up @@ -37,6 +37,7 @@ TWindowDisabler = class
const
EnableColor: array[Boolean] of TColor = (clBtnFace, clWindow);

function AppCreateForm(const AClass: TCustomFormClass): TCustomForm;
procedure UpdateHorizontalExtent(const ListBox: TCustomListBox);
function MinimizePathName(const Filename: String; const Font: TFont;
MaxLen: Integer): String;
Expand Down Expand Up @@ -68,6 +69,22 @@ implementation
MessageBoxCallbackParam: LongInt;
MessageBoxCallbackActive: Boolean;

function AppCreateForm(const AClass: TCustomFormClass): TCustomForm;
{ Creates a form, making it the main form if there isn't one already.
Usage: AppCreateForm(TMyForm) as TMyForm
This is a wrapper around Application.CreateForm, but with these advantages:
- Safety: Returns a typed value instead of writing to an untyped parameter.
- Safety: When used in an assignment statement: MyForm := AppCreateForm(...)
the variable isn't modified until the form is fully constructed and the
function exits. Application.CreateForm writes to its parameter, making the
value public, before the form's constructor is executed, which could allow
code outside the form to access the form before it's fully constructed.
- When the result is casted with "as", it works with type inference.
- When used in the .dpr, the Delphi IDE will never touch it. }
begin
Application.CreateForm(AClass, Result);
end;

type
TListBoxAccess = class(TCustomListBox);

Expand Down

0 comments on commit 974b258

Please sign in to comment.