Archive: Must select at least one Component?


Must select at least one Component?
  Hello all.

My installer has 3 sections, I think I would like to make sure the user selects at least 1 of them. Is there a way I can prevent them from continuing past the components page if no component was selected? Thanks all.


check out one-section.nsi in the examples folder.


Mirakulix, I checked out the one-section.nsi, and it looks like that would allow me to ensure ONLY 1 component could be selected. I would like to ensure that AT LEAST 1 component is selected before leaving the components page. Did I miss something in the one-section.nsi? Thanks for your help.

p.s. I think what would be great would be to have the next button grayed out until at least one component is selected.


I also tried the following, just to see if i could detect if the first section was selected, but this does not work. "SectionGetFlags SEC01 $SEC01" sets $SEC01 to 1 no matter what.

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "ComponentsLeave"
!insertmacro MUI_PAGE_COMPONENTS

Function ComponentsLeave
SectionGetFlags SEC01 $SEC01
StrCmp $SEC01 "*" End NotSelected
NotSelected:
Abort
End:
FunctionEnd

(where * would be the value if the section was selected)


While you wrote your code, I wrote mine.:igor: It's quite the same, though.


Name "One or more Sections"

>OutFile "one-section.exe"
>!define SF_SELECTED 1

>;--------------------------------
; Pages

Page components"" "" componentsLeave
Page instfiles

>;--------------------------------
; Sections

Section!Required
SectionIn RO
SectionEnd

Section"Group 1 - Option 1" g1o1
SectionEnd

Section/o "Group 1 - Option 2" g1o2
SectionEnd

Section/o "Group 1 - Option 3" g1o3
SectionEnd

>;--------------------------------
; Functions

>; $1 counts the number of selected sections
>Function ComponentsLeave
SectionGetFlags"${g1o1}" $0
IntOp$1 $0 & ${SF_SELECTED}
SectionGetFlags "${g1o2}" $0
IntOp$0 $0 & ${SF_SELECTED}
IntOp $1 $1 + $0
SectionGetFlags"${g1o3}" $0
IntOp$0 $0 & ${SF_SELECTED}
IntOp $1 $1 + $0
StrCmp$1 "0" 0 +3
MessageBox MB_OK
|MB_ICONSTOP "You must select at least one Section."
Abort ;stay at page
FunctionEnd
>
SectionGetFlags SEC01 $SEC01 ;$ missing?

It's a compile-time define for a Section, not a variable.
Should be ${SEC01}

-Stu


Mirakulix, thanks! Its working very well. I try to never put any code into my script without fully understanding it, and I have to be honest, I’m still very new at the NSIS, but learning fast I hope. Could you explain the significance of "!define SF_SELECTED 1" ? I looked in the documentation, but it seems it only relates to silent installs. :igor: Thanks again!


It's a compile-time define which sets the value of ${SF_SELECTED} to be "1" on compile.

-Stu


This has nothing to do with the compiler, I just defined the constant SF_SELECTED = 1 for later use (to try to indicate that this line masks out the is-selected-bit):
IntOp $1 $0 & ${SF_SELECTED}


Would this work too?:


ComponentsLeave

SectionGetFlags"${SEC01}" $0
StrCmp$0 1 End
SectionGetFlags"${SEC02}" $0
StrCmp$0 1 End
SectionGetFlags"${SEC03}" $0
StrCmp$0 1 End
MessageBox MB_OK
|MB_ICONSTOP "You must select at least one Section."
Abort
End:
>FunctionEnd
>
This way I can skip the "!define SF_SELECTED 1", and it shortens up the code a tad. Thanks again everyone.

You can safely replace the ${SF_SELECTED} with 1, but you can (should) not omit masking out the lowest bit.
SectionGetFlags returns an integer of
which only the lowest bit specifies if the section is selected.
You can only omit the masking if you are absolutely sure that no other options (Bold font, subsection, read-only) are turned on for these sections.


Mirakulix, so what you saying is that it is safer to include the "!define SF_SELECTED 1" line, correct? Ok, no problem, I can add that back. I'm using the MUI, so should this line be up at the top, say in the ;MUI Settings area? Thanks.


There is no need to define SF_SELECTED yourself. Simply include Sections.nsh.


Right here?

; MUI 1.67 compatible ------
!include "MUI.nsh"
!include "Sections.nsh"

I did not have that or the !define SF_SELECTED 1, and it seemed still work, was i just getting lucky? Thanks all.


If you replace all occurrences of ${SF_SELECTED} with 1s, you don't need to !define ${SF_SELECTED} because this constant is not appearing in your code at all.
And if you don't use any other constants or macros from Sections.nsh, you need not to !include Sections.nsh


Sounds good. Thanks all, you guys are all very patient and helpful.


Can the One or more Sections script be changed so it only allows one of three components but also allows for none to be installed. As it is now one is selected all the time, all of them can't be unchecked.


Mirakulix, all Afrow was saying was that the !define command sets the ${SF_SELECTED} variable to be '1' at compile time, not at runtime. Which is correct.