Blueeyes789
Blueeyes789

Reputation: 573

Inno Setup Update custom TSetupForm caption with nested TNewNotebook page change

I want to use custom uninstall wizard pages as in my Inno Setup script shown in question Custom Uninstall page (not MsgBox).

The problem I have is that the Caption of the TSetupForm does not update, when a page changes.

I tried using the following code.

[Code]

const
  ControlGap = 5; 

procedure UpdateButtonsState(Form: TSetupForm);
var
  Notebook: TNewNotebook;
  BtnBack, BtnNext: TButton;
begin
  Notebook := TNewNotebook(Form.FindComponent('Notebook'));
  BtnBack := TButton(Form.FindComponent('BtnBack'));
  BtnNext := TButton(Form.FindComponent('BtnNext'));

  BtnBack.Enabled := (Notebook.ActivePage <> Notebook.Pages[0]);
  if Notebook.ActivePage <> Notebook.Pages[Notebook.PageCount - 1] then
  begin
    BtnNext.Caption := SetupMessage(msgButtonNext)
    BtnNext.ModalResult := mrNone;
  end
  else
  begin
    BtnNext.Caption := SetupMessage(msgButtonFinish);
    BtnNext.ModalResult := mrYes;
  end;
end;

procedure BtnPageChangeClick(Sender: TObject);
var
  NextPage: TNewNotebookPage;
  Notebook: TNewNotebook;
  Form: TWinControl;
  Button, BtnBack, BtnNext: TButton;
begin
  Button := TButton(Sender);
  Form := Button;
  while not (Form is TSetupForm) do
    Form := Form.Parent;
  Notebook := TNewNotebook(Form.FindComponent('Notebook'));
  BtnBack := TButton(Form.FindComponent('BtnBack'));
  BtnNext := TButton(Form.FindComponent('BtnNext'));

  if (Button = BtnBack) and (Notebook.ActivePage = Notebook.Pages[0]) then
    NextPage := nil
  else
  if (Button = BtnNext) and (Notebook.ActivePage = Notebook.Pages[Notebook.PageCount - 1]) then
    NextPage := nil
  else
    NextPage := Notebook.FindNextPage(Notebook.ActivePage, Button = BtnNext);
  Notebook.ActivePage := NextPage;

  UpdateButtonsState(TSetupForm(Form));
end;

function AddPage(NotebookForm: TSetupForm): TNewNotebookPage;
var
  Notebook: TNewNotebook;
begin
  Notebook := TNewNotebook(NotebookForm.FindComponent('Notebook'));
  Result := TNewNotebookPage.Create(Notebook);
  Result.Notebook:=Notebook;
  Result.Parent:=Notebook;
  Result.Align := alClient;
  if Notebook.ActivePage = nil then 
    Notebook.ActivePage := Result;
  UpdateButtonsState(NotebookForm);
end;

function CreateNotebookForm: TSetupForm;
var
  Notebook: TNewNotebook;
  NotebookPage: TNewNotebookPage;
  Pan: TPanel;
  TmpLabel: TLabel;
  MaxWidth, i: Integer;
  BtnBack, BtnNext, BtnCancel: TButton;
  BtnLabelMsgIDs: Array Of TSetupMessageID;
begin
  Result := CreateCustomForm;
  Result.SetBounds(0, 0, UninstallProgressForm.Width, UninstallProgressForm.Height);
  Result.Position := poOwnerFormCenter;

  Notebook := TNewNotebook.Create(Result);
  Notebook.Parent := Result;
  Notebook.Name := 'Notebook'; 
  Notebook.Align := alClient;

  Pan := TPanel.Create(Result);
  Pan.Parent := Notebook;
  Pan.Caption := '';
  Pan.Align := alBottom;

  BtnNext := TNewButton.Create(Result);
  with BtnNext do
  begin
    Parent := Pan;
    Name := 'BtnNext'; 
    Caption := SetupMessage(msgButtonNext);
    OnClick := @BtnPageChangeClick;
    ParentFont := True;
  end;

  BtnBack := TNewButton.Create(Result);
  with BtnBack do
  begin
    Parent := Pan;
    Caption := SetupMessage(msgButtonBack);
    Name := 'BtnBack'; 
    OnClick := @BtnPageChangeClick;
    ParentFont := True;
  end;

  BtnCancel := TNewButton.Create(Result);
  with BtnCancel do
  begin
    Parent := Pan;
    Name := 'BtnCancel';  
    Caption := SetupMessage(msgButtonCancel);
    ModalResult := mrCancel;
    Cancel := True;
    ParentFont := True;
  end;

  TmpLabel := TLabel.Create(Result);
  with TmpLabel do
  begin
    Left := 0;
    Top := 0;
    Parent := Pan;
    ParentFont := True;
    Visible := False;
    WordWrap := False;
    Autosize := True;
  end;

  SetArrayLength(BtnLabelMsgIDs, 4);
  BtnLabelMsgIDs[0] := msgButtonBack;
  BtnLabelMsgIDs[1] := msgButtonNext;
  BtnLabelMsgIDs[2] := msgButtonCancel;
  BtnLabelMsgIDs[3] := msgButtonFinish;
  MaxWidth := 0;
  for i := Low(BtnLabelMsgIDs) to High(BtnLabelMsgIDs) do
  begin
    TmpLabel.Caption := SetupMessage(BtnLabelMsgIDs[i]) + 'WWW'; 
    if MaxWidth < TmpLabel.Width then
      MaxWidth := TmpLabel.Width;
  end;

  TmpLabel.Caption := 'Yy'; 

  with BtnBack do
  begin
    Width := MaxWidth;
    Height := TmpLabel.Height*2;
    Left := Parent.ClientWidth - 3*(MaxWidth + ScaleX(ControlGap));
    Top := (Parent.ClientHeight - Height) div 2;
  end;
  with BtnNext do
  begin
    Width := MaxWidth;
    Height := TmpLabel.Height*2;
    Left := Parent.ClientWidth - 2*(MaxWidth + ScaleX(ControlGap));
    Top := (Parent.ClientHeight - Height) div 2;
  end;
  with BtnCancel do
  begin
    Width := MaxWidth;
    Height := TmpLabel.Height*2;
    Left := Parent.ClientWidth - 1*(MaxWidth + ScaleX(ControlGap));
    Top := (Parent.ClientHeight - Height) div 2;
  end;
end;

procedure InitializeUninstallProgressForm;
var
  Form: TSetupForm;
  i: Integer;
  NotebookPage: TNewNotebookPage;
  ModResult: Integer;
begin
  Form := CreateNotebookForm;
  for i := 1 to 4 do
  begin
    NotebookPage := AddPage(Form);
    with NotebookPage do
    begin
      Color := clWindow;
      with TLabel.Create(Form) do
      begin
        Parent := NotebookPage;
        SetBounds(0, 0, 50, 30);
        Autosize := true;
        Font.Size := 14;
        Caption := 'Label ' + IntToStr(i);
      end;
      Form.Caption := 'CAPTION - ' + IntToStr(i);  
      {<<<NEVER UPDATES AND KEEPS SHOWING "CAPTION - 4">>>>}
    end;
  end;

  ModResult := Form.ShowModal;
  if ModResult = mrYes then
    MsgBox('Continuing uninstall', mbInformation, MB_OK)
  else
  begin
    MsgBox('Cancelled', mbInformation, MB_OK);
    Abort;
  end;
end;

Please help me to find why Caption of the TSetupForm (Here it is declared as Form) never updates.

Thanks in advance.

Upvotes: 0

Views: 685

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

Of course, it doesn't. You have to update the form caption as the page changes.

A good place for this is the end of the UpdateButtonsState function:

procedure UpdateButtonsState(Form: TSetupForm);
{ ... }
begin
  { ... }
  if Notebook.ActivePage <> nil then
    Form.Caption := 'CAPTION - ' + IntToStr(Notebook.ActivePage.PageIndex + 1);
end;

Upvotes: 1

Related Questions