/ tools / windows / tool_setup / choice_page.iss.inc
choice_page.iss.inc
  1  var
  2    ChoicePagePrepare: array of TNotifyEvent;
  3    ChoicePageSelectionChange: array of TNotifyEvent;
  4    ChoicePageValidate: array of TWizardPageButtonEvent;
  5    ChoicePageMaxTag: Integer;
  6    ChoicePages: array of TInputOptionWizardPage;
  7  
  8  procedure ChoicePageOnClickCheck(Sender: TObject);
  9  var
 10    ListBox: TNewCheckListBox;
 11    Id: Integer;
 12  begin
 13    ListBox := TNewCheckListBox(Sender);
 14    Id := Integer(ListBox.Tag);
 15    ChoicePageSelectionChange[Id](ChoicePages[Id]);
 16  end;
 17  
 18  function ChoicePageGetInput(Page: TInputOptionWizardPage): TNewEdit;
 19  begin
 20    Result := TNewEdit(Page.FindComponent('ChoicePageInput'));
 21  end;
 22  
 23  function ChoicePageGetLabel(Page: TInputOptionWizardPage): TNewStaticText;
 24  begin
 25    Result := TNewStaticText(Page.FindComponent('ChoicePageLabel'));
 26  end;
 27  
 28  function ChoicePageGetButton(Page: TInputOptionWizardPage): TNewButton;
 29  begin
 30    Result := TNewButton(Page.FindComponent('ChoicePageBrowseButton'));
 31  end;
 32  
 33  procedure ChoicePageSetEditLabel(Page: TInputOptionWizardPage; Caption: String);
 34  var
 35    InputLabel: TNewStaticText;
 36  begin
 37    InputLabel := ChoicePageGetLabel(Page);
 38    InputLabel.Caption := Caption;
 39  end;
 40  
 41  function ChoicePageGetInputText(Page: TInputOptionWizardPage): String;
 42  begin
 43    Result := ChoicePageGetInput(Page).Text;
 44  end;
 45  
 46  procedure ChoicePageSetInputText(Page: TInputOptionWizardPage; Text: String);
 47  begin
 48    ChoicePageGetInput(Page).Text := Text;
 49  end;
 50  
 51  procedure ChoicePageSetInputEnabled(Page: TInputOptionWizardPage; Enabled: Boolean);
 52  begin
 53    ChoicePageGetLabel(Page).Enabled := Enabled;
 54    ChoicePageGetInput(Page).Enabled := Enabled;
 55    ChoicePageGetButton(Page).Enabled := Enabled;
 56  end;
 57  
 58  
 59  procedure ChoicePageOnBrowseButtonClick(Sender: TObject);
 60  var
 61    Button: TNewButton;
 62    Page: TInputOptionWizardPage;
 63    InputLabel: TNewStaticText;
 64    Input: TNewEdit;
 65    Dir: String;
 66  begin
 67    Button := TNewButton(Sender);
 68    Page := TInputOptionWizardPage(Button.Owner);
 69    Input := ChoicePageGetInput(Page);
 70    InputLabel := ChoicePageGetLabel(Page);
 71    Dir := Input.Text;
 72    if BrowseForFolder(InputLabel.Caption, Dir, True) then
 73    begin
 74      Input.Text := Dir;
 75    end;
 76  end;
 77  
 78  <event('CurPageChanged')>
 79  procedure ChoicePageOnCurPageChanged(CurPageID: Integer);
 80  var
 81    i: Integer;
 82  begin
 83    for i := 1 to ChoicePageMaxTag do
 84    begin
 85      if ChoicePages[i].ID = CurPageID then
 86      begin
 87        ChoicePagePrepare[i](ChoicePages[i]);
 88        break;
 89      end;
 90    end;
 91  end;
 92  
 93  <event('NextButtonClick')>
 94  function ChoicePageOnNextButtonClick(CurPageID: Integer): Boolean;
 95  var
 96    i: Integer;
 97  begin
 98    Result := True;
 99    for i := 1 to ChoicePageMaxTag do
100    begin
101      if ChoicePages[i].ID = CurPageID then
102      begin
103        Result := ChoicePageValidate[i](ChoicePages[i]);
104        break;
105      end;
106    end;
107  end;
108  
109  <event('InitializeWizard')>
110  procedure InitChoicePages();
111  begin
112    ChoicePages := [ ];
113    ChoicePagePrepare := [ ];
114    ChoicePageSelectionChange := [ ];
115    ChoicePageValidate := [ ];
116  end;
117  
118  function FindLinkInText(Text: String): String;
119  var
120    Tmp: String;
121    LinkStartPos, LinkEndPos: Integer;
122  begin
123    Result := '';
124    Tmp := Text;
125    LinkStartPos := Pos('https://', Tmp);
126    if LinkStartPos = 0 then exit;
127    Delete(Tmp, 1, LinkStartPos - 1);
128  
129    { Try to find the end of the link }
130    LinkEndPos := 0
131    if LinkEndPos = 0 then LinkEndPos := Pos(' ', Tmp);
132    if LinkEndPos = 0 then LinkEndPos := Pos(',', Tmp);
133    if LinkEndPos = 0 then LinkEndPos := Pos('.', Tmp);
134    if LinkEndPos = 0 then LinkEndPos := Length(Tmp);
135    Delete(Text, LinkEndPos, Length(Tmp));
136  
137    Log('Found link in "' + Text + '": "' + Tmp + '"');
138    Result := Tmp;
139  end;
140  
141  procedure OnStaticTextClick(Sender: TObject);
142  var
143    StaticText: TNewStaticText;
144    Link: String;
145    Err: Integer;
146  begin
147    StaticText := TNewStaticText(Sender);
148    Link := FindLinkInText(StaticText.Caption);
149    if Link = '' then
150      exit;
151  
152    ShellExec('open', Link, '', '', SW_SHOWNORMAL, ewNoWait, Err);
153  end;
154  
155  procedure MakeStaticTextClickable(StaticText: TNewStaticText);
156  begin
157    if FindLinkInText(StaticText.Caption) = '' then
158      exit;
159  
160    StaticText.OnClick := @OnStaticTextClick;
161    StaticText.Cursor := crHand;
162  end;
163  
164  function ChoicePageCreate(
165            const AfterID: Integer;
166            const Caption, Description, SubCaption, EditCaption: String;
167            HasDirectoryChooser: Boolean;
168            Prepare: TNotifyEvent;
169            SelectionChange: TNotifyEvent;
170            Validate: TWizardPageButtonEvent): TInputOptionWizardPage;
171  var
172    VSpace, Y : Integer;
173    ChoicePage: TInputOptionWizardPage;
174    InputLabel: TNewStaticText;
175    Input: TNewEdit;
176    Button: TNewButton;
177  
178  begin
179    ChoicePageMaxTag := ChoicePageMaxTag + 1;
180    VSpace := ScaleY(8);
181    ChoicePage := CreateInputOptionPage(AfterID, Caption,
182                    Description, SubCaption, True, True);
183  
184    MakeStaticTextClickable(ChoicePage.SubCaptionLabel);
185  
186    ChoicePage.Tag := ChoicePageMaxTag;
187    ChoicePage.CheckListBox.OnClickCheck := @ChoicePageOnClickCheck;
188    ChoicePage.CheckListBox.Tag := ChoicePageMaxTag;
189  
190    if HasDirectoryChooser then
191    begin
192      ChoicePage.CheckListBox.Anchors := [ akLeft, akTop, akRight ];
193      ChoicePage.CheckListBox.Height := ChoicePage.CheckListBox.Height - ScaleY(60);
194      Y := ChoicePage.CheckListBox.Top + ChoicePage.CheckListBox.Height + VSpace;
195  
196      InputLabel := TNewStaticText.Create(ChoicePage);
197      with InputLabel do
198      begin
199        Top := Y;
200        Anchors := [akTop, akLeft, akRight];
201        Caption := EditCaption;
202        AutoSize := True;
203        Parent := ChoicePage.Surface;
204        Name := 'ChoicePageLabel';
205      end;
206      MakeStaticTextClickable(InputLabel);
207      Y := Y + InputLabel.Height + VSpace;
208  
209      Input := TNewEdit.Create(ChoicePage);
210      with Input do
211      begin
212        Top := Y;
213        Anchors := [akTop, akLeft, akRight];
214        Parent := ChoicePage.Surface;
215        Name := 'ChoicePageInput';
216        Text := '';
217      end;
218  
219      Button := TNewButton.Create(ChoicePage);
220      with Button do
221      begin
222        Anchors := [akTop, akRight];
223        Parent := ChoicePage.Surface;
224        Width := WizardForm.NextButton.Width;
225        Height := WizardForm.NextButton.Height;
226        Top := Y - (Height - Input.Height) / 2;
227        Left := ChoicePage.SurfaceWidth - Button.Width;
228        Name := 'ChoicePageBrowseButton';
229        Caption := SetupMessage(msgButtonWizardBrowse);
230        OnClick := @ChoicePageOnBrowseButtonClick;
231      end;
232  
233      Input.Width := Button.Left - ScaleX(8);
234    end;
235  
236    SetArrayLength(ChoicePages, ChoicePageMaxTag+1);
237    SetArrayLength(ChoicePagePrepare, ChoicePageMaxTag+1);
238    SetArrayLength(ChoicePageSelectionChange, ChoicePageMaxTag+1);
239    SetArrayLength(ChoicePageValidate, ChoicePageMaxTag+1);
240  
241    ChoicePages[ChoicePageMaxTag] := ChoicePage;
242    ChoicePagePrepare[ChoicePageMaxTag] := Prepare;
243    ChoicePageSelectionChange[ChoicePageMaxTag] := SelectionChange;
244    ChoicePageValidate[ChoicePageMaxTag] := Validate;
245  
246    Result := ChoicePage;
247  end;