Mutually Exclusive Sections

From NSIS Wiki
Jump to navigationJump to search
Author: KiCHiK (talk, contrib)


Description

This example was written for the forum user japheth. The original thread can be found here.

In this example the user can choose only one of the two sections. Unlike one-section he can choose none as well, but not both.

The Script

# This example demonstrates how to control section selection.
# It allows only one of two sections to be selected at any
# given time, but unlike one-section, it allows nither
# to be selected as well.
#
# Please note that the initial value will not be what you expect
# it to be if you are are using InstType because InstType is taken
# into consideration after the .onInit, where the initial state is
# set, executes.
# To use this code with InstType you will either have to specify
# you first InstType to match the initial value you set in .onInit
# (only StrCpy $1 ${sec1} is important in this case because the
# other set the section and that will be done by InstType), or set
# the initial section selection from .onSelChange when it is called
# for the first time.
 
Name example
OutFile "mutually exclusive.exe"
 
ComponentText "please choose zero or one but the default"
 
!define SF_SELECTED   1
!define SF_SUBSEC     2
!define SF_SUBSECEND  4
!define SF_BOLD       8
!define SF_RO         16
!define SF_EXPAND     32
 
!define SECTION_OFF   0xFFFFFFFE
 
Section "Required"
  SectionIn RO
SectionEnd
 
Section "i hate #2" sec1
SectionEnd
 
Section "i hate #1" sec2
SectionEnd
 
Function .onInit
  Push $0
 
  StrCpy $R9 ${sec1} ; Gotta remember which section we are at now...
  SectionGetFlags ${sec1} $0
  IntOp $0 $0 | ${SF_SELECTED}
  SectionSetFlags ${sec1} $0
 
  SectionGetFlags ${sec2} $0
  IntOp $0 $0 & ${SECTION_OFF}
  SectionSetFlags ${sec2} $0
 
  Pop $0
FunctionEnd
 
Function .onSelChange
  Push $0
 
  StrCmp $R9 ${sec1} check_sec1
 
    SectionGetFlags ${sec1} $0
    IntOp $0 $0 & ${SF_SELECTED}
    IntCmp $0 ${SF_SELECTED} 0 done done
      StrCpy $R9 ${sec1}
      SectionGetFlags ${sec2} $0
      IntOp $0 $0 & ${SECTION_OFF}
      SectionSetFlags ${sec2} $0
 
    Goto done
 
  check_sec1:
 
    SectionGetFlags ${sec2} $0
    IntOp $0 $0 & ${SF_SELECTED}
    IntCmp $0 ${SF_SELECTED} 0 done done
      StrCpy $R9 ${sec2}
      SectionGetFlags ${sec1} $0
      IntOp $0 $0 & ${SECTION_OFF}
      SectionSetFlags ${sec1} $0
 
  done:
 
  Pop $0
FunctionEnd