Skip to content
⌘ NSIS Forum Archive

Section control using flags

37 posts

extremecarver#
After searching for a working solution for ages - I am back to this one. Radiobuttons macro Is so inherently bugged that I will not use it anymore.
This works as expected - except you can deselect all options - so I changed the rest of my installer for that ( I will think how I can make sure that if no section is installed, it calls the uninstaller if it exists). Radiobuttons only works if nothing is selected beforehand.

Function .onSelChange
${IfThen} $0 = -1 ${|} Return ${|} ; I don't care about InstType changes
${If} $0 < ${sec1}
${OrIf} $0 > ${sec3}
Return ; I don't care about other sections
${EndIf}

!macro SelectOnlyMe sid
${IfThen} $0 <> ${sid} ${|} !insertmacro UnselectSection ${sid} ${|}
!macroend

!insertmacro SelectOnlyMe ${sec1}
!insertmacro SelectOnlyMe ${sec2}
!insertmacro SelectOnlyMe ${sec3}
FunctionEnd
I do not think this can be adapted well to more than 3 sections. But for 3 sections this works perfectly.
Anders#
This SelectOnlyMe code is some random code you found on Stackoverflow? I'm pretty sure I wrote it and it is possible the person wanted to be able to unselect the radio.

Try NSIS\Examples\one-section.nsi instead...

Or if you must:

!include Sections.nsh
!include LogicLib.nsh
page components
page instfiles
Section 1 sec1
SectionEnd 
Section 2 sec2
SectionEnd 
Section 3 sec3
SectionEnd 
Section 4 sec4
SectionEnd 
Function .onSelChange
!macro ActAsRadio first last
${If} $0 >= ${first}
${AndIf} $0 <= ${last}
    ${ForEach} $1 ${first} ${last} + 1
        ${If} $0 <> $1
            !insertmacro UnselectSection $1
        ${Else}
            !insertmacro SelectSection $1
        ${EndIf}
    ${Next}
${EndIf}
!macroend
!insertmacro ActAsRadio ${sec1} ${sec4}
FunctionEnd
Function .onInit
StrCpy $0 ${sec2} ; Default radio for this group
Call .onSelChange ; Enforce it
FunctionEnd 
extremecarver#
NSIS\Examples\one-section.nsi is the one where it went havoc if I preselected an option. Also the radio buttons do not seem to work well with a single section group and many other problems.

Thanks for your code example. I think yes I found that code on stack overflow. I now wrote about 500 lines of nsis code to work around the possibility that someone unselects all of option 1-3... Still tearing my hair out as the logic then becomes very complicated...
extremecarver#
I'm having another problem with the predefined selections. The section size is changing depending on which section is selected. That works well as soon as the user actively clicked on a section. However if the section comes preselected due to insertmacro SelectSection it is not enforced.

Function .onInit
StrCpy $1 ""
ReadRegStr $1 HKLM "SOFTWARE\Garmin\MapSource\Families\${REG_KEY}"
"SECTION_1"
StrCmp $1 "1" 0 Continue11
;MessageBox MB_OK "$1 -SelectSection ${sec1}"
!insertmacro SelectSection ${sec1}
Goto Continue111
Continue11:
Continue111:

StrCpy $2 ""
ReadRegStr $2 HKLM "SOFTWARE\Garmin\MapSource\Families\${REG_KEY}"
"SECTION_2"
StrCmp $2 "1" 0 Continue12
!insertmacro SelectSection ${sec2}
Goto Continue121
Continue12:
Continue121:

.....
..... and some more sections

Call .onSelChange

${If} ${SectionIsSelected} ${sec2}
SectionSetSize ${sec4} 10
${Else}
SectionSetSize ${sec4} 10000
${EndIf}

${If} ${SectionIsSelected} ${sec3}
SectionSetSize ${sec5} 10
${Else}
SectionSetSize ${sec5} 10000
${EndIf}

${If} ${SectionIsSelected} ${sec4}
${AndIf} ${SectionIsSelected} ${sec2}
SectionSetSize ${sec4} 10
${EndIf}

..... some more conditions
So before I actively change section 1-3 (only one can be selected or none which works) - the SectionSetSize will not work correctly or at all.

In effect the idea is if section 2 is selected - then sec4 is size 10 (because it's the same files mainly getting installed. If not then it is 10000. If sec3 is selected - then sec5 size is 10, if not then 10000

But this code only works after actually selecting one of sec1 to sec3. Not with the preselection via !insertmacro SelectSection
extremecarver#
well - maybe I should have added the code in an attachement, instead inline.
So I will try again.

The following section setsize only works if sec1 sec2 or sec3 has been actively selected. Not if it comes preselected via on.init..

Not sure what I'm doing wrong here or if there is a bug...

I attached the relevant code...
Anders#
Ideally example code should be

- Self contained
- As minimal as possible
- Compile without changes

and your attached code is none of those things.

Please take my example code above and modify it until it exhibits the problem you are talking about.
extremecarver#
After a long time I found the error:
I was missing the line (in this example for Section sec1) which needs to come after: !insertmacro SelectSection ${sec1}
StrCpy $0 ${sec1}

This needs to be placed after every SelectSection macro else it will not work if set by default reading from registry.

Only that way some additional Actions under .onselchange will work if the radio button is ticked from registry/pre definition. Otherwise the following code will only work correctly after a radio button was manually activated/deactivated.

Function .onSelChange
${If} ${SectionIsSelected} ${sec3}
SectionSetSize ${sec4} 100
${EndIf}
FunctionEnd
Or full example:
Function .onInit

StrCpy $1 ""

ReadRegStr $1 HKLM "SOFTWARE\Garmin\MapSource\Families\${REG_KEY}"

"SECTION_1"

StrCmp $1 "1" 0 Continue11

;MessageBox MB_OK "$1 -SelectSection ${sec1}"

!insertmacro SelectSection ${sec1}
StrCpy $0 ${sec1} ; Default radio for this group

Goto Continue111

Continue11:

Continue111:

......
.....
....

Call .onSelChange