How do I only disable a section in a certain InstType, but force it on otherwise (even in the "Custom" InstType)? - nsis

I have a Section that is set to RO and occurs in all InstTypes except for a special one.
When the user first selects the special InstType and then selects "Custom" as the InstType the section is unchecked and can't be checked again in the "Custom" InstType.
So everything is fine except that when the users selects the "Custom" InstType the Section should always be checked again.
Only in the special InstType use case it should be unchecked.
How can I achieve this?

You can enforce whatever logic you want in .onSelChange:
!include WinMessages.nsh
!include LogicLib.nsh
!include Sections.nsh
Page Components
Page InstFiles
InstType "Normal1"
!define CIT_Special 1
InstType "Special"
InstType "Normal2"
Section Foo
SectionIn 1 2 3
SectionEnd
Section Bar SEC_Special
SectionIn 1 3
SectionIn RO
SectionEnd
Section Baz
SectionIn 1 2 ;3 << Not the same as Foo just to have some sort of difference
SectionEnd
Function .onSelChange
;Normally you would call GetCurInstType here, but it seems we need a little hack to detect the custom section
FindWindow $1 "#32770" "" $HWNDPARENT
GetDlgItem $1 $1 0x3F9
SendMessage $1 ${CB_GETCURSEL} 0 0 $2
SendMessage $1 ${CB_GETITEMDATA} $2 0 $2
${If} $2 = ${CIT_Special}
!insertmacro UnselectSection ${SEC_Special}
${Else}
!insertmacro SelectSection ${SEC_Special}
${EndIf}
FunctionEnd

Related

Remove button pane at the bottom in NSIS installer

I'm using NSIS to create an installer which will only have custom pages.
The custom page shouldn't have the default buttons (back, next or cancel), but only 1 button in the center of the page.
Is there any way I can remove the full bottom pane, as I have highlighted in the image attached ?
I have hid the buttons, but I would like to remove the separator line and the section below it as well.
This is code I have:
;--------------------------------
;Include Modern UI
!include "MUI2.nsh"
;--------------------------------
;General
;Name and file
Name "Custom Test"
OutFile "custom_installer.exe"
;Default installation folder
InstallDir "$LOCALAPPDATA\Custom Test"
;Remove default branding text of Nullsoft
BrandingText " "
;--------------------------------
;Interface Settings
!define MUI_LICENSEPAGE_BGCOLOR "814EFA"
!define MUI_ABORTWARNING
!define MUI_BGCOLOR "000000"
;--------------------------------
;Functions
Function Start
nsDialogs::Create 1044
Pop $0
SetCtlColors $0 222425 FBFBFB
${NSD_CreateButton} 20 170 90% 15u INSTALL
GetDlgItem $0 $HWNDPARENT 3 ; Back Button
GetDlgItem $1 $HWNDPARENT 1 ; Next/Close Button
GetDlgItem $2 $HWNDPARENT 2 ; Cancel Button
ShowWindow $0 ${SW_HIDE}
ShowWindow $1 ${SW_HIDE}
ShowWindow $2 ${SW_HIDE}
nsDialogs::Show
FunctionEnd
;--------------------------------
;Pages
Page custom Start
!insertmacro MUI_PAGE_COMPONENTS
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
SetOutPath "$INSTDIR"
SectionEnd
And this is how the installer page looks like now:
What about using SetBrandingText " " to hide the horizontal line and coloring the bottom area with SetCtlColors $0 222425 FBFBFB so it will look like the rest of the dialog?
Is that acceptable for you or you want to make the dialog smaller (lower its height)?

NSIS - Skip certain dialogs using command-line arguments?

Is there a way to make the NSIS installer skip certain dialogs?
It has these command-line arguments,
/S, /NCRC and /D=dir
Although /S and /NCRC can used for silent and unattended modes, is there are command-line arguments to make the installer skip certain dialogs in the installer and show the rest of the dialog? For example. skip the Welcome dialog and the next two dialogs and go to the fourth one.
/S, /NCRC and /D= are the only installer parameters with built-in support, anything else you have to handle yourself.
Pages can be skipped by calling Abort in the page pre callback. It is also possible to jump forward a specific number of pages. The GetOptions macro can be used to parse the command line.
OutFile Test.exe
RequestExecutionLevel user
InstallDir $Temp
!include LogicLib.nsh
!include FileFunc.nsh
Page License LicPre
Page Components CmpPre
Page Directory "" DiShow
Page InstFiles
Var SkippedL
Var SkippedC
!macro AbortIfCmdlineParam Switch Var
${GetParameters} $0
ClearErrors
${GetOptions} $0 "${Switch}" $0
${IfNot} ${Errors}
${If} ${Var} = 0
StrCpy ${Var} 1
Abort
${EndIf}
${EndIf}
!macroend
Function LicPre
!insertmacro AbortIfCmdlineParam "/SkipL" $SkippedL
FunctionEnd
Function CmpPre
!insertmacro AbortIfCmdlineParam "/SkipC" $SkippedC
FunctionEnd
Function DiShow
# Disable back button if both pages skipped, this is optional
${If} $SkippedL <> 0
${AndIf} $SkippedC <> 0
GetDlgItem $0 $hwndparent 3
EnableWindow $0 0
${EndIf}
FunctionEnd
Section
SectionEnd
Run as Test /SkipL /SkipC to skip both.
Or:
OutFile Test.exe
RequestExecutionLevel user
InstallDir $Temp
!include LogicLib.nsh
!include FileFunc.nsh
Page License "" LicShow
Page Components
Page Directory
Page InstFiles
Function LicShow
Var /Global HasSkipped
${GetParameters} $0
ClearErrors
${GetOptions} $0 "/Skip=" $0
${IfNot} ${Errors}
${AndIf} $0 < 4 ; Don't let user skip InstFiles
${AndIf} $HasSkipped = 0
StrCpy $HasSkipped 1
SendMessage $HWNDPARENT 0x408 $0 ""
${EndIf}
FunctionEnd
Section
SectionEnd
...and run as Test /Skip=2.

NSIS unattended options

I want to build a NSIS-script, which has three section
section Main
section Minor
section Shared
Shared is invisible and would be installed, if Main or Minor is checked. If I start the installer, every section (Main, Minor) is checked.
Now it should be able to define the section (in silent install). What have I to change, to only install Main or Minor or Both?
Name "Test"
Outfile "Test.exe"
;RequestExecutionLevel ?
!include "Sections.nsh"
!include "LogicLib.nsh"
!include "FileFunc.nsh" ;For GetOptions
Page Components "" "" EnforceSectionDependencies
Page InstFiles
Section /o "Main" SID_MAIN
DetailPrint Main
SectionEnd
Section /o "Minor" SID_MINOR
DetailPrint Minor
SectionEnd
Section "" SID_SHARED
DetailPrint Shared
SectionEnd
!macro CheckSectionSwitch sw sid
${GetOptions} $0 '${sw}' $9
${IfNot} ${Errors}
StrCpy $1 1
!insertmacro SelectSection ${sid}
${EndIf}
!macroend
Function .onInit
${GetParameters} $0
StrCpy $1 0 ;Any section swithes?
ClearErrors
!insertmacro CheckSectionSwitch '/Main' ${SID_MAIN}
!insertmacro CheckSectionSwitch '/Minor' ${SID_MINOR}
${If} $1 = 0
;Set defaults
!insertmacro SelectSection ${SID_MAIN}
!insertmacro SelectSection ${SID_MINOR}
${EndIf}
call EnforceSectionDependencies
FunctionEnd
Function EnforceSectionDependencies
!insertmacro UnselectSection ${SID_SHARED}
${If} ${SectionIsSelected} ${SID_MAIN}
${OrIf} ${SectionIsSelected} ${SID_MINOR}
!insertmacro SelectSection ${SID_SHARED}
${EndIf}
FunctionEnd
You should look at the Section management part of the documentation, notably the SectionSetFlags to change the sections selections.
Also, maybe that the How to control Section selections, while using SubSections & InstTypes example will be useful.

How can I get a finish button after a nsDialogs page

I'm trying to create a post install configuration page in my nsis script using nsDialogs. My script for gathering the input and executing the configuration works however I'm never presented with a finish/close/exit button after I'm done. Currently my pages declaration looks like:
Page directory
Page instfiles
Page custom nsDialogsPage nsDialogsPageLeave
How can I get a finish/exit/done button to show after nsDialogsPageLeave executes?
The classic NSIS UI does not have a finish page, the instfiles page is usually the last page and it will show a "finish button" after all the sections have executed. You can set the text of any button to the same string with SendMessage $hwndButton ${WM_SETTEXT} 0 "STR:$(^CloseBtn)" if you want to provide your own finish page.
Most installers request the required information before the instfiles page, if you cannot do this then you might want to use the Modern UI, it will provide a finish page for you:
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
Page custom nsDialogsPage nsDialogsPageLeave
!insertmacro MUI_PAGE_FINISH
It was a little unclear to me if you wanted two pages; a input page and then a finish page or a combined input/finish page. A combined page is a little weird but it is possible:
!define AppName "Test"
Name "${AppName}"
Outfile "${AppName} setup.exe"
InstallDir $temp
!include LogicLib.nsh
!include WinMessages.nsh
!include nsDialogs.nsh
Var MyEndConfigPageStage
Page Directory
Page InstFiles
Page Custom MyEndConfigPageCreate MyEndConfigPageLeave /EnableCancel
Function MyEndConfigPageCreate
StrCpy $MyEndConfigPageStage 0
GetDlgItem $0 $hwndparent 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:&Apply"
nsDialogs::Create 1018
Pop $0
${NSD_CreateCheckBox} 0 13u 100% -13u "FooBar"
Pop $1
nsDialogs::Show
FunctionEnd
Function MyEndConfigPageLeave
${If} $MyEndConfigPageStage > 0
Return
${EndIf}
${NSD_GetState} $1 $2
ClearErrors
WriteIniStr "$instdir\settings.ini" Dummy "FooBar" $2
${If} ${Errors}
MessageBox mb_iconstop "Unable to apply settings!"
Abort
${EndIf}
IntOp $MyEndConfigPageStage $MyEndConfigPageStage + 1
GetDlgItem $0 $hwndparent 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:$(^CloseBtn)"
GetDlgItem $0 $hwndparent 2
EnableWindow $0 0 ;Disable cancel
EnableWindow $1 0 ;Disable the checkbox
Abort
FunctionEnd
Section
SectionEnd

How do I make a section required for a sectiongroup in NSIS?

I want to make a section inside of a sectiongroup required for that section group but not required for the entire install if the group is unchecked. I've tried "SectionIn R0" but this makes that section required for the entire install and I only want it to be required if they select the group itself.
SectionGroup "group"
Section "required for section group"
SectionIn RO
SectionEnd
Section "optional"
SectionEnd
SectionGroupEnd
You can't do this with just properties, you need some actual code:
!include Sections.nsh
!include LogicLib.nsh
SectionGroup /e "group"
Section "required for section group" SEC_REQ
SectionIn RO
SectionEnd
Section "optional" SEC_OPT
SectionEnd
SectionGroupEnd
Function .onSelChange
${If} ${SectionIsSelected} ${SEC_OPT}
!define /math MYSECTIONFLAGS ${SF_SELECTED} | ${SF_RO}
!insertmacro SetSectionFlag ${SEC_REQ} ${MYSECTIONFLAGS}
!undef MYSECTIONFLAGS
${Else}
!insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
FunctionEnd
Note that there is a "bug" when unchecking the section group itself, to workaround that, you need to keep some global state since nsis does not tell you which section generated the notification. The following code has better logic:
!include Sections.nsh
!include LogicLib.nsh
SectionGroup /e "group" SEC_GRP
Section "required for section group" SEC_REQ
SectionIn RO
SectionEnd
Section "optional" SEC_OPT
SectionEnd
Section "" PRIVSEC_TOGGLESTATE ;hidden section to keep track of state
SectionEnd
SectionGroupEnd
Function .onSelChange
!define /math SECFLAGS_SELRO ${SF_SELECTED} | ${SF_RO}
${IfNot} ${SectionIsSelected} ${PRIVSEC_TOGGLESTATE}
${AndIf} ${SectionIsReadOnly} ${SEC_REQ}
!insertmacro ClearSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_OPT}
!insertmacro SetSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO}
${Else}
!insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_REQ}
!insertmacro SelectSection ${PRIVSEC_TOGGLESTATE}
${Else}
!insertmacro UnselectSection ${PRIVSEC_TOGGLESTATE}
${EndIf}
!undef SECFLAGS_SELRO
FunctionEnd
You can also set the sections as mandatory sections by using the SectionSetFlags command. You simply need to add it to the .onInit function as follows:
SectionGroup "group" Sec01
Section "required for section group"
SectionIn RO
SectionEnd
Section "optional" Sec02
SectionEnd
SectionGroupEnd
Function .onInit
SectionSetFlags ${Sec01} 17
FunctionEnd
This will gray "group" out, but it will be selected and installed as expected.
You can also setup a custom function which is being called when user clicks "Next" but no option is selected. In this function, you can check if any of Sections is selected or, as I did, check the variable which is set to "nothingSelected" on init, then modified when any section is created. If you put "Abort" in the custom function, the installer will stay on the same page. Here is a snippet:
page license
page directory
page components "" "" testSelection
page instfiles
function .onInit
StrCpy $somethingSelected "nothingSelected"
functionEnd
function testSelection
${If} $somethingSelected == "nothingSelected"
MessageBox MB_OK "Please select any option"
Abort
${EndIf}
functionEnd
function .onSelChange
#selecting mutually exclusive sections (removed for clarity)
StrCpy $somethingSelected $1
functionEnd

Resources