Retrieve section index in .onSelChange and state change

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


Description

This example shows how to retrieve the section index of a changed section checkbox in the component page.

As soon a checkbox is activated/deactivated the .onSelChange callback function is called. The function shows which section (section index) has been activated or deactivated.

Remember to adjust the maximal number of sections ("SECTIONCOUNT").

Example

;--------------------------------
;Include Modern UI
 
  !define LOGICLIB_SECTIONCMP
 
  !include "MUI.nsh"
  !include "logiclib.nsh"
  !include "sections.nsh"
 
;--------------------------------
;General
 
  ;Name and file
  Name "Test"
  OutFile "test.exe"
 
;--------------------------------
;Pages
 
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_INSTFILES
 
Section A secA
SectionEnd
 
Section /o B secB
SectionEnd
 
Section C secC
SectionEnd
 
Section D secD
SectionEnd
 
!define SECTIONCOUNT 3 ; total - 1
 
;--------------------------------
;Save selected sections
!macro SaveSections VAR
  StrCpy ${VAR} 0
  ${ForEach} $R0 ${SECTIONCOUNT} 0 - 1
    IntOp ${VAR} ${VAR} << 1
    ${If} ${SectionIsSelected} $R0
      IntOp ${VAR} ${VAR} + 1
    ${EndIf}
  ${Next}
!macroend
 
Function .onSelChange
  ; save selected sections to compare
  !insertmacro SaveSections $R1
 
  ; save original number for comparison
  Push $R1
  ; XOR both results, find out which sections changed
  IntOp $R3 $R2 ^ $R1
 
  ${For} $R0 0 ${SECTIONCOUNT}
    ; check if section has changed
    IntOp $R4 $R3 & 1
    ${If} $R4 == 1
      IntOp $R5 $R1 & 1
      SectionGetText $R0 $R6
      ${If} $R5 == 1
        MessageBox MB_OK "Section '$R6' ($R0) has been activated."
      ${Else}
        MessageBox MB_OK "Section '$R6' ($R0) has been deactivated."
      ${EndIf}
      ; :
      ; :
    ${EndIf}
    ; go to next section
    IntOp $R3 $R3 >> 1
    IntOp $R1 $R1 >> 1
  ${Next}
 
  Pop $R2
 
FunctionEnd
 
Function .onInit
  ; save initial selected sections
  !insertmacro SaveSections $R2
FunctionEnd
 
;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"