NSIS nsDialog How to check status of a RadioButton outside of page - nsis

I'm trying to create a custom page via nsDialog, with radio buttons that then affect a section further on. The issue I have is that the values do not seem to propagate beyond the CustomPage funtion, seen in the example below:
Var RADIO_APPLE
Var RADIO_BANANA
Function CustomPage
nsDialogs::Create 1018
${NSD_CreateRadioButton} 0 0 100% 10u "Apple"
Pop $RADIO_APPLE
${NSD_CreateRadioButton} 0 20 100% 10u "Banana"
Pop $RADIO_BANANA
${NSD_Check} $RADIO_APPLE
nsDialogs::Show
${NSD_GetState} $RADIO_APPLE $0
${NSD_GetState} $RADIO_BANANA $1
MessageBox MB_OK "Apple $0 Banana $1"
FunctionEnd
Section "-CustomSection"
${NSD_GetState} $RADIO_APPLE $0
${NSD_GetState} $RADIO_BANANA $1
MessageBox MB_OK "Apple $0 Banana $1"
SectionEnd
This is obviously a gist, ignoring the includes and other pages, but when I build the full version of this I see
Apple 1 Banana 0
on the message box raised inside CustomPage, but see
Apple 0 Banana 0
when the section is run.
I've read through https://nsis.sourceforge.io/NsDialogs_FAQ#How_to_easily_handle_radiobutton_selections and this solution gives me the same outcome.
Is there something I'm missing to make $RADIO_* available in the section?
Thanks

You should not store important long-lasting state in the registers, especially in $0 because it is used by other pages and plug-ins. The components page for example uses $0 in one of the callback functions.
Your checkbox handles on the other hand are only used on that page so you could use $1 and $2.
The other issue is, you can't rely on reading control data after nsDialogs::Show returns. You are supposed to use the leave callback to validate and store user input.
!include nsDialogs.nsh
Page Custom MyCreate MyLeave
Page InstFiles
Var Apple
Var Banana
Function .onInit
StrCpy $Banana 1 ; Make banana the default
FunctionEnd
Function MyCreate
nsDialogs::Create 1018
Pop $0
${NSD_CreateRadioButton} 0 0 100% 10u "Apple"
Pop $1
${NSD_CreateRadioButton} 0 20 100% 10u "Banana"
Pop $2
${NSD_SetState} $1 $Apple
${NSD_SetState} $2 $Banana
nsDialogs::Show
FunctionEnd
Function MyLeave
${NSD_GetState} $1 $Apple
${NSD_GetState} $2 $Banana
FunctionEnd
Section "-CustomSection"
MessageBox MB_OK "Apple $Apple Banana $Banana"
SectionEnd

Related

NSIS: How do I allow multiple user choice screens that select the sections to install?

I'm attempting to create an installer that asks the user a series of questions to decide which components to install. Each choice (potentially) influences the available options in later choices (else I would just do a normal components page—I don't want to give the user invalid options).
How do I accomplish such a thing? If I just use the components page, all the options are shown, some combinations of which are completely invalid. I don't want to let the user select those. Is it possible to leave out the components page?
Here's a minimal working example of what I'm trying. (Sorry it's long, I couldn't really simplify the dialog code.)
!include nsDialogs.nsh
!include Sections.nsh
Name "mwe"
OutFile "mwe.exe"
InstallDir C:\mwe
Var hwnd
Var Level1Opt
Page custom SelectLevel1Opt ProcessLevel1
Function SelectLevel1Opt
nsDialogs::Create 1018
pop $hwnd
${NSD_CreateLabel} 0 0 100% 12u "Please select level 1 option"
Pop $hwnd
${NSD_CreateRadioButton} 10% 12u 100% 12u "Level 1 A"
Pop $hwnd
nsDialogs::SetUserData $hwnd "Level 1 A"
${NSD_OnClick} $hwnd SetLevel1
${NSD_CreateRadioButton} 10% 24u 100% 12u "Level 1 B"
Pop $hwnd
nsDialogs::SetUserData $hwnd "Level 1 B"
${NSD_OnClick} $hwnd SetLevel1
nsDialogs::Show
FunctionEnd
Function SetLevel1
Pop $hwnd
nsDialogs::GetUserData $hwnd
Pop $Level1Opt
MessageBox MB_OK "Selected: $Level1Opt"
FunctionEnd
Function ProcessLevel1
${If} $Level1Opt == "Level 1 A"
!insertmacro SelectSection Level1A
${ElseIf} $Level1Opt == "Level 1 B"
!insertmacro SelectSection Level1B
${EndIf}
FunctionEnd
Page directory
Page instfiles
Section ""
MessageBox MB_OK "Common Install"
SectionEnd
Section /o "" Level1A
MessageBox MB_OK "Level 1 A"
SectionEnd
Section /o "" Level1B
MessageBox MB_OK "Level 1 B"
SectionEnd
No matter what I choose, neither Level1A nor Level1B sections get run. The selection from the dialog is correctly detected in the handler and the post function. However, selecting the sections isn't causing them to run. Even if I add a components page, neither of them is selected.
I looked in Selection.nsh, and the example it refers to (one-section.nsi) doesn't really do what I want, because it uses the components page. (I also don't understand quite how it works.)
What am I doing wrong? In what way am I misunderstanding the way NSIS is supposed to work?
As idleberg says, the correct syntax is !insertmacro SelectSection ${Level1A} but you get a warning because the section id is not defined until after its Section instruction in your .nsi. You need to move the functions that use ${Level1A} below the sections in your source code:
!include nsDialogs.nsh
!include Sections.nsh
Page custom SelectLevel1Opt ProcessLevel1
Page instfiles
Section ""
MessageBox MB_OK "Common Install"
SectionEnd
Section /o "a" Level1A
MessageBox MB_OK "Level 1 A"
SectionEnd
Section /o "b" Level1B
MessageBox MB_OK "Level 1 B"
SectionEnd
Var hInnerDialog
Var hL1A
Var hL1B
Function SelectLevel1Opt
nsDialogs::Create 1018
pop $hInnerDialog
${NSD_CreateLabel} 0 0 100% 12u "Please select level 1 option"
Pop $0
${NSD_CreateRadioButton} 10% 12u 100% 12u "Level 1 A"
Pop $hL1A
nsDialogs::SetUserData $hL1A ${Level1A} ; Only used by the generic function
${NSD_CreateRadioButton} 10% 24u 100% 12u "Level 1 B"
Pop $hL1B
nsDialogs::SetUserData $hL1B ${Level1B} ; Only used by the generic function
nsDialogs::Show
FunctionEnd
Function ProcessLevel1
${NSD_GetState} $hL1A $1
${If} $1 <> ${BST_UNCHECKED}
!insertmacro SelectSection ${Level1A}
!insertmacro UnselectSection ${Level1B}
${Else}
!insertmacro SelectSection ${Level1B}
!insertmacro UnselectSection ${Level1A}
${EndIf}
FunctionEnd
The ProcessLevel1 function can also be implemented as a loop if there are many radio buttons:
Function ProcessLevel1
StrCpy $0 ""
loop:
FindWindow $0 "${__NSD_RadioButton_CLASS}" "" $hInnerDialog $0
System::Call "USER32::GetWindowLong(p$0,i${GWL_STYLE})i.r1"
IntOp $1 $1 & ${BS_AUTORADIOBUTTON}
${If} $1 = ${BS_AUTORADIOBUTTON} ; Is it a auto radio button?
nsDialogs::GetUserData $0 ; Get the section id
Pop $2
${NSD_GetState} $0 $1
${If} $1 <> ${BST_UNCHECKED}
!insertmacro SelectSection $2
${Else}
!insertmacro UnselectSection $2
${EndIf}
${EndIf}
IntCmp $0 0 "" loop loop
FunctionEnd

nsDialogs::Show accepts ENTER key even if all buttons from UI are destroyed

I have a small bootstrapper with custom UI, which contains two pages, one to configure the install, and the other showing a progressbar for the download and install of the payload.
My problem:
If the user presses Enter key when in the second page, the installer exits, even if it didn't finish installing.
I removed all the controls I could from the UI with:
GetDlgItem $1 $HWNDPARENT 1 //(2, and 3)
System::Call `User32::DestroyWindow(i $1)`
in the onGuiInit function, and the first page ignores enter and space keys now, but the second page still exits on Enter key.
I have no other components on that page except some labels, a progressbar, and a slideshow (nsisSlideshow plugin).
In the background I have a thread which downloads and installs the payload.
The pages are declared like this:
Page Custom Options_Show Options_Leave
Page Custom Progress_Show Progress_Leave
So, long story short, when I press enter, the nsDialogs::Show function returns, killing the installer.
Any way I can stop it from doing this?
You really should try to stop forward page changes by calling Abort in the page leave callback function.
NSIS only blocks clicks from buttons that exist and are disabled.
!include nsDialogs.nsh
Page Custom Options_Show #Options_Leave
Page Custom Progress_Show #Progress_Leave
Function DisableBottomButtons
GetDlgItem $0 $hwndparent 1
ShowWindow $0 0
EnableWindow $0 0
GetDlgItem $0 $hwndparent 2
ShowWindow $0 0
EnableWindow $0 0
GetDlgItem $0 $hwndparent 3
ShowWindow $0 0
EnableWindow $0 0
FunctionEnd
Function Options_Show
nsDialogs::Create 1018
Pop $0
${NSD_CreateButton} 0 20 100% 12u "Go to next page"
Pop $0
${NSD_OnClick} $0 GoToNextPage
Call DisableBottomButtons
nsDialogs::Show
FunctionEnd
Function GoToNextPage
GetDlgItem $0 $hwndparent 1
EnableWindow $0 1
SendMessage $hwndparent ${WM_COMMAND} 1 0
FunctionEnd
Function Progress_Show
nsDialogs::Create 1018
Pop $0
${NSD_CreateButton} 0 20 100% 12u "Exit"
Pop $0
${NSD_OnClick} $0 ExitApp
Call DisableBottomButtons
nsDialogs::Show
FunctionEnd
Function ExitApp
MessageBox mb_ok "Bye"
; Faking a click on Next/Close will not work when the button is disabled,
; this uses a slightly ugly trick to bypass that. GoToNextPage does it properly.
!define /math WM_NOTIFY_OUTER_NEXT ${WM_USER} + 0x8
SendMessage $hwndparent ${WM_NOTIFY_OUTER_NEXT} 1 0
FunctionEnd
First, small code snippet:
!macro EnableNextButtonM IsEnable
Push $1
GetDlgItem $1 $HWNDPARENT 1 ;next button
EnableWindow $1 ${IsEnable}
Pop $1
!macroend
!define EnableNextButton "!insertmacro EnableNextButtonM"
Then, in function Progress_Show you should call
${EnableNextButton} 0
before
nsDialogs::Show
to disable Next button before it will be shown.
After download completion (at the download callback) call
${EnableNextButton} 1
to enable it again.
You variant with .onGuiInit does not work because it calls once - before first page will be shown. But every next page will be created dynamically again, thus, all UI page must be implemented into page custom -Pre function.

NSIS onblur or onfocus event equivalent

I need to be able to fill out a second text box based on content from another (first) text box, and I need to do it when the first text box loses focus (or when the second text box gains focus).
I am not able to do what I need to do using OnChange because it is triggered after every keypress, but the value of the first text box needs to be evaluated only after the user finishes inputting.
How would I do this? There are no OnBlur or OnFocus event handlers, and the existing ones OnChange, OnClick, OnBack, OnNotify can't seem to do the job.
I don't think nsDialogs can handle this, you probably have to use the WndSubclass plug-in:
Page Custom MyPage
Page InstFiles
!include LogicLib.nsh
!include nsDialogs.nsh
!include WinMessages.nsh
!include WndSubclass.nsh
Var EditName
Var EditNick
Var NickSubProc
Function MyPage
nsDialogs::Create 1018
Pop $0
${NSD_CreateText} 7% 0 77% 12u "Joe Sixpack"
Pop $EditName
${NSD_CreateText} 7% 14u 77% 12u ""
Pop $EditNick
${WndSubclass_Subclass} $EditNick NickSubProc $NickSubProc $NickSubProc
nsDialogs::Show
FunctionEnd
Function NickSubProc
${If} $2 = ${WM_SETFOCUS}
${If} $3 = $EditName ; $EditName lost focus?
${OrIf} $3 = 0 ; or no previous focus?
${NSD_GetText} $EditName $4
${If} $4 != ""
${NSD_SetText} $EditNick "Foo $4 Bar"
SendMessage $EditNick ${EM_SETSEL} 0 -1
${EndIf}
${EndIf}
${EndIf}
FunctionEnd
Section
SectionEnd

How to use REMOVE or REPAIR feature in NSIS script?

I have used nsis script for creating installer.When i run my installer second time with same name,REPAIR and REMOVE should be check and do the corresponding operation.I have find out my application already installed or not using following codes,
Function checkinstall
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\My app" "UninstallString"
IfFileExists $R0 +1 NotInstalled
call nsDialogpage
NotInstalled:
FunctionEnd
Function nsDialogpage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateRadioButton} 0 5u 100% 10u "Repair"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_OnClick} $hwnd ???
${NSD_CreateRadioButton} 0 25u 100% 56u "Remove"
Pop $hwnd
${NSD_OnClick} $hwnd ???
nsDialogs::Show
If the user select repair button it should overwrites existing installation path else uninstall existing installed and continue with new one.what am i need do to replace the (???) of the above code
page custom checkinstall
!insertmacro MUI_PAGE_DIRECTORY
My next page is Directory selection.so i need to call this page? How to achieve this?
1.How can i call un installer function if the user selects remove button?
Function un.Init, section /o -un.Main UNSEC000,section -un.post UNSE001
these are the un installer funtions.How can i call these functions? i have tried call method but it did not work.
You need to specify a callback function, like in the nsDialogs documentation, look for the nsDialogsPageLeave function in this example:
!include nsDialogs.nsh
!include LogicLib.nsh
Name nsDialogs
OutFile nsDialogs.exe
XPStyle on
Var Dialog
Var Label
Var Text
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
Function nsDialogsPage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 0 13u 100% -13u "Type something here..."
Pop $Text
${NSD_OnChange} $Text nsDialogsPageTextChange
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
${NSD_GetText} $Text $0
MessageBox MB_OK "You typed:$\n$\n$0"
FunctionEnd
Function nsDialogsPageTextChange
Pop $1 # $1 == $ Text
${NSD_GetText} $Text $0
${If} $0 == "hello"
MessageBox MB_OK "right back at ya!"
${EndIf}
FunctionEnd
Section
DetailPrint "hello world"
SectionEnd

Retain data on custom pages when back button is pressed

Incase of custom pages in NSIS script is there any way to retain the data entered by user when back button is pressed (when the installer is running)?
There are a couple of ways to do this. Either way you need to store your data in globals.
1) Use a "Leave" function.
Page custom EnterCustom LeaveCustom
; Create two variables for each value/widget pair
Var Text
Var TextWidget
Var Check
Var CheckWidget
Function EnterCustom
nsDialogs::Create 1018
Pop $0
${NSD_CreateText} 0 0 80u 12u $Text
Pop $TextWidget
${NSD_CreateCheckBox} 0 26u 80u 12u "Check this box"
Pop $CheckWidget
${NSD_SetState} $CheckWidget $Check
nsDialogs::Show
FunctionEnd
Function LeaveCustom
${NSD_GetText} $TextWidget $Text
${NSD_GetState} $CheckWidget $Check
FunctionEnd
The only problem with this method is that LeaveCustom only gets called if you hit the next button. So if you edit the fields then click the Back button your changes are lost. The changes are however saved if you go forward then come back.
2) Use the OnChange callback.
This is a little more complicated but solves the problem with the previous method.
Page custom EnterCustom
Var Initialized
; Create two variables for each value/widget pair
Var Text
Var TextWidget
Var Check
Var CheckWidget
Function EnterCustom
nsDialogs::Create 1018
Pop $0
${If} $Initialized != "True"
; Set defaults for all your values here
StrCpy $Text "Initial Value"
StrCpy $Check ${BST_UNCHECKED}
StrCpy $Initialized "True"
${EndIf}
; Create and configure all of your widgets
${NSD_CreateText} 0 0 80u 12u $Text
Pop $TextWidget
${NSD_OnChange} $TextWidget OnTextChange
${NSD_CreateCheckBox} 0 26u 80u 12u "Check this box"
Pop $CheckWidget
${NSD_SetState} $CheckWidget $Check
${NSD_OnClick} $CheckWidget OnCheckClick
nsDialogs::Show
FunctionEnd
; Create a callback function for each Widget
Function OnTextChange
Pop $0 ; Widget handle is on stack
${NSD_GetText} $TextWidget $Text
FunctionEnd
Function OnCheckClick
Pop $0 ; Widget handle is on stack
${NSD_GetState} $CheckWidget $Check
FunctionEnd
Some widgets, e.g. RadioButtons and CheckBoxes, use the OnClick function instead. Also the ComboBox does not work well with this method. However, a DropList, which does not seem to be documented, can usually replace it and works fine.
Radio buttons are also a little tricky because only the click callback for the selected button is called. I solved this by updating all of the radio button values in each radio button click callback.
Messy/tedious but it works.
I know it's an old question, but I've landed here from my googling.
You can use NSD_OnBack (or call nsDialogs::OnBack directly) to set OnBack callback.
Here is the code snipet:
Function portsSelectionPage
nsDialogs::Create 1018
Pop $0
${NSD_CreateNumber} 70u 0 40u 12u $TomcatPort
Pop $TomcatPortHWND
${NSD_CreateNumber} 70u 14u 40u 12u $PostgresPort
Pop $PostgresPortHWND
nsDialogs::Show
${NSD_OnBack} "portsSelectionPageLeave"
FunctionEnd
Function portsSelectionPageLeave
${NSD_GetText} $TomcatPortHWND $TomcatPort
${NSD_GetText} $PostgresPortHWND $PostgresPort
FunctionEnd
You can store data in a global variable, or in a .ini in $pluginsdir
!include nsDialogs.nsh
!include LogicLib.nsh
Name nsDialogs
OutFile nsDialogs.exe
XPStyle on
Var Dialog
Var Label
Var Text
Var Text_State
Var Checkbox
Var Checkbox_State
Page custom nsDialogsPage nsDialogsPageLeave
Page license
Page instfiles
Function .onInit
StrCpy $Text_State "Type something here..."
FunctionEnd
Function nsDialogsPage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 0 13u 100% 12u $Text_State
Pop $Text
${NSD_CreateCheckbox} 0 30u 100% 10u "&Something"
Pop $Checkbox
${If} $Checkbox_State == ${BST_CHECKED}
${NSD_Check} $Checkbox
${EndIf}
# alternative for the above ${If}:
#${NSD_SetState} $Checkbox_State
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
${NSD_GetText} $Text $Text_State
${NSD_GetState} $Checkbox $Checkbox_State
FunctionEnd
Section
DetailPrint "hello world"
SectionEnd
more information http://nsis.sourceforge.net/Docs/nsDialogs/Readme.html#step-memory

Resources