Inno Setup Uncheck a task when another task is checked - inno-setup

I am trying to intercept the WizardForm.TasksList.OnClickCheck event so that I can uncheck a task when another task is selected. I know that normally radio buttons would be used in this situation, but automatically unchecking one task when another is selected works better here due to the use of multiple hierarchical tasks and the fact that if radio buttons are used, you always have to have one of the two selected when at the top of the task subtree. Redesigning the task hierarchy is not feasible in order to maintain consistency, as this is to include two temporary tasks that will be removed in a future version of the installer. I have written the following to do this:
var
DefaultTasksClickCheck: TNotifyEvent;
{ Uncheck tasks based on what other tasks are selected }
procedure UpdateTasks();
var
intIndex: Integer;
begin
with WizardForm.TasksList do
begin
if IsTaskSelected('Task1') then
begin
intIndex := WizardForm.TasksList.Items.IndexOf('Task36 Description');
CheckItem(intIndex, coUncheck);
end;
if IsTaskSelected('Task36') then
begin
intIndex := WizardForm.TasksList.Items.IndexOf('Task1 Description');
CheckItem(intIndex, coUncheck);
end;
end;
end;
{ Update the task states if the task states change and restore the original event handler procedure }
procedure TasksClickCheck(Sender: TObject);
begin
DefaultTasksClickCheck(Sender);
UpdateTasks;
end;
procedure InitializeWizard();
begin
{ Store the original Tasks Page OnClickCheck event procedure and assign custom procedure }
DefaultTasksClickCheck := WizardForm.TasksList.OnClickCheck;
WizardForm.TasksList.OnClickCheck := #TasksClickCheck;
end;
However, when I run the code, I get an:
Out of Proc Range
error, when clicking any checkbox, with DefaultTasksClickCheck(Sender); highlighted as the offending line. If I comment out this line, I no longer get the error, but am obviously no longer restoring the original event handler and it still doesn't check and uncheck the tasks correctly, with Task36 uncheckable when Task1 is checked. What have I done wrong?

The WizardForm.TasksList.OnClickCheck is not assigned by the Inno Setup itself (contrary to WizardForm.ComponentsList.OnClickCheck), so you cannot call it.
To fix the problem, either:
completely remove the DefaultTasksClickCheck;
or if you want to be covered in case the event starts being used in future versions of Inno Setup, check if it is nil before calling it.
You cannot know what task was checked most recently in the OnClickCheck handler. So you have to remember the previously checked task to correctly decide what task to unselect.
[Tasks]
Name: Task1; Description: "Task1 Description"
Name: Task36; Description: "Task36 Description"; Flags: unchecked
[Code]
var
DefaultTasksClickCheck: TNotifyEvent;
Task1Selected: Boolean;
procedure UpdateTasks;
var
Index: Integer;
begin
{ Task1 was just checked, uncheck Task36 }
if (not Task1Selected) and IsTaskSelected('Task1') then
begin
Index := WizardForm.TasksList.Items.IndexOf('Task36 Description');
WizardForm.TasksList.CheckItem(Index, coUncheck);
Task1Selected := True;
end
else
{ Task36 was just checked, uncheck Task1 }
if Task1Selected and IsTaskSelected('Task36') then
begin
Index := WizardForm.TasksList.Items.IndexOf('Task1 Description');
WizardForm.TasksList.CheckItem(Index, coUncheck);
Task1Selected := False;
end;
end;
procedure TasksClickCheck(Sender: TObject);
begin
if DefaultTasksClickCheck <> nil then
DefaultTasksClickCheck(Sender);
UpdateTasks;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpSelectTasks then
begin
{ Only now is the task list initialized, check what is the current state }
{ This is particularly important during upgrades, }
{ when the task does not have its default state }
Task1Selected := IsTaskSelected('Task1');
end;
end;
procedure InitializeWizard();
begin
DefaultTasksClickCheck := WizardForm.TasksList.OnClickCheck;
WizardForm.TasksList.OnClickCheck := #TasksClickCheck;
end;
In Inno Setup 6, instead of using indexes, you can also use task names with use of WizardIsTaskSelected and WizardSelectTasks. For an example, see Inno Setup: how to auto select a component if another component is selected?.
For a more generic solution to detecting what item has been checked, see Inno Setup Detect changed task/item in TasksList.OnClickCheck event.

Related

Inno Setup: How to use WizardForm.TasksList.Items in InitializeWizard?

I am having issues figuring out why I cannot manipulate the task checkboxes during InitializeWizard, but I can with CurPageChanged:
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"
Name: "Option1"; Description: "Option1"
[Code]
procedure CurPageChanged(CurPageID: Integer);
var Index: Integer;
begin
if CurPageID = wpSelectTasks then
begin
Index := WizardForm.TasksList.Items.IndexOf('Option1');
if Index <> -1 then
MsgBox('Touch device checkbox found.', mbInformation, MB_OK); { THIS WORKS!! }
end;
end;
procedure InitializeWizard();
var Index: Integer;
begin
Index := WizardForm.TasksList.Items.IndexOf('Option1');
if Index <> -1 then
MsgBox('Touch device checkbox found.', mbInformation, MB_OK); { THIS DOES NOT WORK }
end;
Can I not use WizardForm.TasksList.Items in InitializeWizard? I want to be able to call WizardForm.TasksList.Checked[Index] := False; or possibly disable it but I'd rather do it on initialization instead of having to avoid calling code, if the user hits the back button and returns to the wpSelectTasks.
Because task list is populated based on selected components.
Hence, the task list is not known in InitializeWizard yet. The task list is (re)generated, based on select components, whenever the wpSelectTasks page is entered.
So, as you have found out, the earliest moment, you can work with TasksList is CurPageChanged(wpSelectTasks).
When unchecking the task, make sure you do not uncheck it, when user is going back to the tasks page. Actually, you should probably uncheck it on the first visit of the page only.

downloading files over the internet, if the component has been selected (WITH Inno Tools Downloader)

This code actually download me the files and it does not matter whether the selected component is "test" or not. I want those two files download, if you select a component, can do that? I use Inno Inno Setup 5 + Tools Downloader)
[Components]
Name: Dictionaries; Description: "test"; Types: Full; ExtraDiskSpaceRequired: 50;
[Languages]
Name: english; MessagesFile: compiler:Default.isl
#include ReadReg(HKEY_LOCAL_MACHINE,'Software\Sherlock Software\InnoTools\Downloader','ScriptPath','');
[Code]
procedure InitializeWizard();
begin
itd_init;
itd_addfile('http://www.sherlocksoftware.org/petz/files/dogz5.zip',expandconstant('{tmp}\dogz5.zip'));
itd_addfile('http://www.sherlocksoftware.org/petz/files/petz4.zip',expandconstant('{tmp}\petz4.zip'));
itd_downloadafter(wpReady);
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep=ssInstall then begin
filecopy(expandconstant('{tmp}\dogz5.zip'),expandconstant('{app}\dogz5.zip'),false);
filecopy(expandconstant('{tmp}\petz4.zip'),expandconstant('{app}\petz4.zip'),false);
end;
end;
Yes, that's possible. Your are looking for a little helper function called
IsComponentSelected().
It's basically a boolean tester accepting a component name from the [components] and returning the checkbox value (selected=true).
// for a single component
if IsComponentSelected('NameOfTheComponent') then idpAddFile(URL, ...);`
// multiple components with one selection
if IsComponentSelected('dictionaries') then
begin
idpAddFile(URL1, ...);
idpAddFile(URL2, ...);
end;
Comment by TLama:
In which event and where to enqueue the download files?
I would suggest to use the NextButtonClick event with a condition, that the current (CurPage) has to be the component selection screen (wpSelectComponents).
In other words: when you are on the component selection screen and press next, only the selected components are added to the downloader.
The code could look like this:
function NextButtonClick(CurPage: Integer): Boolean;
(*
Called when the user clicks the Next button.
If you return True, the wizard will move to the next page.
If you return False, it will remain on the current page (specified by CurPageID).
*)
begin
if CurPage = wpSelectComponents then
begin
if IsComponentSelected('NameOfTheComponent') then idpAddFile(URL, ...);
end; // of wpSelectComponents
Result := True;
end;
Sidenote: you might switch your download lib to https://code.google.com/p/inno-download-plugin/ This has better features, including decent https support and is actively maintained. InnoTools Download by SherlockSoftware is outdated (2008).

Change AppId using [Code] just before the installation in Inno Setup

In a setup I give the user the option to install either a 32 or 64 bit version using radio buttons.
I then want to append either _32 or _64 to the AppId.
I know I can change the AppId using scripted constants but the needed function is called while Setup is starting. But at this point the radio buttons do not yet exist and thus I receive the error "Could not call proc".
I consulted the Inno Setup help and I read that you can change the AppId at any given point before the installation process has started (if I understood correctly).
So how do I manage to do this?
I am looking forward to your answers!
Some of the {code:...} functions for certain directive values are called more than one time and the AppId is one of them. To be more specific, it is called twice. Once before a wizard form is created and once before the installation starts. What you can do is just to check if the check box you're trying to get the value from exists. You can simply ask if it's Assigned like follows:
[Setup]
AppId={code:GetAppID}
...
[Code]
var
Ver32RadioButton: TNewRadioButton;
Ver64RadioButton: TNewRadioButton;
function GetAppID(const Value: string): string;
var
AppID: string;
begin
// check by using Assigned function, if the component you're trying to get a
// value from exists; the Assigned will return False for the first time when
// the GetAppID function will be called since even WizardForm not yet exists
if Assigned(Ver32RadioButton) then
begin
AppID := 'FDFD4A34-4A4C-4795-9B0E-04E5AB0C374D';
if Ver32RadioButton.Checked then
Result := AppID + '_32'
else
Result := AppID + '_64';
end;
end;
procedure InitializeWizard;
var
VerPage: TWizardPage;
begin
VerPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');
Ver32RadioButton := TNewRadioButton.Create(WizardForm);
Ver32RadioButton.Parent := VerPage.Surface;
Ver32RadioButton.Checked := True;
Ver32RadioButton.Caption := 'Install 32-bit version';
Ver64RadioButton := TNewRadioButton.Create(WizardForm);
Ver64RadioButton.Parent := VerPage.Surface;
Ver64RadioButton.Top := Ver32RadioButton.Top + Ver32RadioButton.Height + 4;
Ver64RadioButton.Caption := 'Install 64-bit version';
end;

Skipping custom pages based on optional components in Inno Setup

In a prior question I asked how to have three optional components, where the user could also specify the locations for each component separately (e.g. a code part and two HTML web applications). #Miral gave me a great answer which I have now implemented:
three components in three user defined locations
I have a small esthetic issue remaining. I am always creating and asking the user for a CreateInputDirPage, in the wizard. The question comes after the wpSelectComponents.
Question: How do I skip the page if the component was not selected. That is, how do I skip my custom page?
I have a feeling it has to do with ShouldSkipPage(). But I have no idea what the PageID for my custom page is, and how to test to see what components were selected.
function ShouldSkipPage(PageID: Integer): Boolean;
The wizard calls this event function to determine whether or not a particular page (specified by PageID) should be shown at all. If you return True, the page will be skipped; if you return False, the page may be shown.
My script is enclosed below:
[Components]
Name: "Watson"; Description: "Watson Component"; Types: onlywatson full
Name: "Toby"; Description: "Toby Component"; Types: onlytoby full
Name: "Sherlock"; Description: "Sherlock Component"; Types: onlysherlock full
[Code]
var
TobyDirPage: TInputDirWizardPage;
SherlockDirPage: TInputDirWizardPage;
procedure InitializeWizard;
begin
TobyDirPage := CreateInputDirPage(wpSelectComponents,
'Select Location for Toby Web Pages', 'Where should we store the sample Toby application files?',
'The sample Toby stand-alone map application will be saved in the following folder.'#13#10#13#10 +
'To continue, click Next. If you would like to select a different folder, click Browse.',
False, 'New Folder');
{ Add item (with an empty caption) }
TobyDirPage.Add('');
{ Set initial value (optional) }
TobyDirPage.Values[0] := ExpandConstant('c:\wwwroot\Toby');
SherlockDirPage := CreateInputDirPage(wpSelectComponents,
'Select Location for Sherlock Web Pages', 'Where should we store the Sherlock Catalog Search Tool?',
'Sherlock.html and it'#39 + 's associated files will be saved in the following folder.'#13#10#13#10 +
'To continue, click Next. If you would like to select a different folder, click Browse.',
False, 'New Folder');
{ Add item (with an empty caption) }
SherlockDirPage.Add('');
{ Set initial value (optional) }
SherlockDirPage.Values[0] := ExpandConstant('c:\wwwroot\Sherlock');
end;
function GetTobyDir(Param: String): String;
begin
{ Return the selected TobyDir }
Result := TobyDirPage.Values[0];
end;
function GetSherlockDir(Param: String): String;
begin
{ Return the selected TobyDir }
Result := SherlockDirPage.Values[0];
end;
As you correctly forefelt, you need to use the ShouldSkipPage event handler to conditionally skip the page. To check if a certain component is selected use the IsComponentSelected function and finally, to get ID of your custom page you need to store its ID. Putting all together might give you the following example script:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[Components]
Name: "help"; Description: "Help File";
[Code]
var
CustomPageID: Integer;
procedure InitializeWizard;
var
CustomPage: TInputDirWizardPage;
begin
CustomPage := CreateInputDirPage(wpSelectComponents, 'Caption',
'Description', 'SubCaption', False, 'NewFolderName');
CustomPage.Add('Input');
{ store your custom page ID to further use in the ShouldSkipPage event }
CustomPageID := CustomPage.ID;
end;
function ShouldSkipPage(PageID: Integer): Boolean;
begin
{ initialize result to not skip any page (not necessary, but safer) }
Result := False;
{ if the page that is asked to be skipped is your custom page, then... }
if PageID = CustomPageID then
{ if the component is not selected, skip the page }
Result := not IsComponentSelected('help');
end;
My take on this is to use the TWizardPageShouldSkipEvent, I've made only a case-and-point script:
[Code]
var
TobyDirPage: TInputDirWizardPage;
function SkipEvent (Sender: TWizardPage): Boolean;
begin
Result := not IsComponentSelected('Toby');
end;
procedure InitializeWizard;
begin
TobyDirPage := CreateInputDirPage(wpSelectComponents, yadda yadda yadda
TobyDirPage.OnShouldSkipPage := #SkipEvent;
end;
Now, OnShouldSkipPage fires right after pressing Next on wpSelectComponents and before TobyDirPage gets painted and since you can attach that event to the page itself you don't need to fiddle with PageID's.

Launch custom code via tasks in Inno Setup

I want to execute some code if a user checks a corresponding checkbox during the install. From reading the help file, it looks like the only way to use the task is to associate it with an entry in the Files/Icons/etc. section. I'd really like to associate it with a procedure in the Code section. Can this be done and if so, how?
You don't need to define your own wizard page. You can just add them to the additional tasks page.
[Tasks]
Name: associate; Description:"&Associate .ext files with this version of my program"; \
GroupDescription: "File association:"
[Code]
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
if CurPageID = wpSelectTasks then
begin
if WizardIsTaskSelected('taskname') then
MsgBox('First task has been checked.', mbInformation, MB_OK);
else
MsgBox('First task has NOT been checked.', mbInformation, MB_OK);
end;
end;
Credit goes to TLama for this post.
You do that by adding a custom wizard page that has check boxes, and execute the code for all selected check boxes when the user clicks "Next" on that page:
[Code]
var
ActionPage: TInputOptionWizardPage;
procedure InitializeWizard;
begin
ActionPage := CreateInputOptionPage(wpReady,
'Optional Actions Test', 'Which actions should be performed?',
'Please select all optional actions you want to be performed, then click Next.',
False, False);
ActionPage.Add('Action 1');
ActionPage.Add('Action 2');
ActionPage.Add('Action 3');
ActionPage.Values[0] := True;
ActionPage.Values[1] := False;
ActionPage.Values[2] := False;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
if CurPageID = ActionPage.ID then begin
if ActionPage.Values[0] then
MsgBox('Action 1', mbInformation, MB_OK);
if ActionPage.Values[1] then
MsgBox('Action 2', mbInformation, MB_OK);
if ActionPage.Values[2] then
MsgBox('Action 3', mbInformation, MB_OK);
end;
end;
The check boxes can either be standard controls or items in a list box, see the Inno Setup documentation on Pascal Scripting for details.
If you want your code to be executed depending on whether a certain component or task has been selected, then use the WizardIsComponentSelected() and WizardIsTaskSelected() functions instead.

Resources