RadioButtons: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
(missing LogicLib include)
Line 90: Line 90:
   ;
   ;
   ; See one-section.nsi for an example of usage
   ; See one-section.nsi for an example of usage
  !include LogicLib.nsh


   !macro StartRadioButtons _d
   !macro StartRadioButtons _d

Revision as of 20:20, 20 December 2005

Author: NHOCSUNG (talk, contrib)


Description

Macros for mutually exclusive section selection.

Usage

  !insertmacro SectionRadioButtons "0" "0,1,2"
 
or
 
  !insertmacro SectionRadioButtons "${g1o1}" "${g1o1},${g1o2},${g1o3}"

Example

; one-section.nsi
;
; This example demonstrates how to control section selection.
; It allows only one of the sections of a group to be selected.
 
;--------------------------------
 
; Section define/macro header file
; See this header file for more info
 
!include "Sections.nsh"
 
;--------------------------------
 
Name "One Section"
OutFile "one-section.exe"
 
;--------------------------------
 
; Pages
 
Page components
Page instfiles
 
;--------------------------------
 
; Sections
 
Section "Group 1 - Option 1" g1o1
SectionEnd
 
Section /o "Group 1 - Option 2" g1o2
SectionEnd
 
Section /o "Group 1 - Option 3" g1o3
SectionEnd
 
Section "Group 2 - Option 1" g2o1
SectionEnd
 
Section /o "Group 2 - Option 2" g2o2
SectionEnd
 
Section /o "Group 2 - Option 3" g2o3
SectionEnd
 
;--------------------------------
 
; Functions
 
Function .onInit
 
  !insertmacro RadioGetChecked "0" "0,1,2"
  !insertmacro RadioGetChecked "${g2o1}" "${g2o1},${g2o2},${g2o3}"
 
FunctionEnd
 
Function .onSelChange
 
  !insertmacro SectionRadioButtons "0" "0,1,2"
  !insertmacro SectionRadioButtons "${g2o1}" "${g2o1},${g2o2},${g2o3}"
 
FunctionEnd

Macros

  ;--------------------------------
 
  ; Macros for mutually exclusive section selection
  ; Written by Tim Gallagher
  ;
  ; See one-section.nsi for an example of usage
 
  !include LogicLib.nsh
 
  !macro StartRadioButtons _d
    !verbose push
    !verbose ${LOGICLIB_VERBOSITY}
 
    !ifndef _CheckedButton${_d}
      var /GLOBAL _CheckedButton${_d}
      !define _CheckedButton${_d}
    !endif
 
    ${IfThen} $_CheckedButton${_d} == "" ${|} StrCpy $_CheckedButton${_d} `${_d}` ${|}
    !define CheckedButton `$_CheckedButton${_d}`
 
    Push $0
    Push $1
 
    SectionGetFlags `${CheckedButton}` $0
    IntOp $0 $0 & ${SECTION_OFF}
    SectionSetFlags `${CheckedButton}` $0
 
    StrCpy $1 `${CheckedButton}`
 
    !verbose pop
  !macroend
 
  ; A radio button
  !macro RadioButton _s
    !verbose push
    !verbose ${LOGICLIB_VERBOSITY}
 
    ${If} ${SectionIsSelected} `${_s}`
      StrCpy `${CheckedButton}` `${_s}`
    ${EndIf}
 
    !verbose pop
  !macroend
 
  ; Ends the radio button block
  !macro EndRadioButtons
    !verbose push
    !verbose ${LOGICLIB_VERBOSITY}
 
    ${If} $1 == `${CheckedButton}`                        ; selection hasn't changed
      SectionGetFlags `${CheckedButton}` $0
      IntOp $0 $0 | ${SF_SELECTED}
      SectionSetFlags `${CheckedButton}` $0
    ${EndIf}
 
    Pop $1
    Pop $0
 
    !undef CheckedButton
    !verbose pop
  !macroend
 
  !macro SectionRadioButtons _d _e
    Push $2
    Push $3
    Push $4
    Push $5
    Push $6
    Push $7
 
    !insertmacro StartRadioButtons ${_d}
 
    StrCpy $3 0
    StrLen $5 `${_e}`
    ${For} $2 0 $5
      StrCpy $4 `${_e}` 1 $2
      ${If} $4 == ","
        IntOp $3 $3 + 1
      ${EndIf}
    ${Next}
 
    StrCpy $7 `${_e}`
    StrCpy $4 0
    ${Do}
 
      StrLen $5 `$7`
      ${For} $2 0 $5
        StrCpy $6 `$7` 1 $2
        ${If} $6 == ","
          StrCpy $6 `$7` $2
          IntOp $2 $2 + 1
          StrCpy $7 `$7` "" $2
          IntOp $4 $4 + 1
          ${Break}
        ${EndIf}
      ${Next}
 
      ${If} $4 <= $3
      ${AndIf} $6 != ""
        !insertmacro RadioButton $6
        ${If} $4 == $3
          !insertmacro RadioButton $7
        ${EndIf}
      ${EndIf}
 
    ${LoopUntil} $4 >= $3
 
    !insertmacro EndRadioButtons
 
    Pop $7
    Pop $6
    Pop $5
    Pop $4
    Pop $3
    Pop $2
  !macroend
 
  !macro RadioGetChecked _d _e
    Push $2
    Push $3
    Push $4
    Push $5
    Push $6
    Push $7
 
    !ifndef _CheckedButton${_d}
      var /GLOBAL _CheckedButton${_d}
      !define _CheckedButton${_d}
    !endif
 
    StrCpy $3 0
    StrLen $5 `${_e}`
    ${For} $2 0 $5
      StrCpy $4 `${_e}` 1 $2
      ${If} $4 == ","
        IntOp $3 $3 + 1
      ${EndIf}
    ${Next}
 
    StrCpy $7 `${_e}`
    StrCpy $4 0
    ${Do}
 
      StrLen $5 `$7`
      ${For} $2 0 $5
        StrCpy $6 `$7` 1 $2
        ${If} $6 == ","
          StrCpy $6 `$7` $2
          IntOp $2 $2 + 1
          StrCpy $7 `$7` "" $2
          IntOp $4 $4 + 1
          ${Break}
        ${EndIf}
      ${Next}
 
      ${If} $4 <= $3
      ${AndIf} $6 != ""
 
        ${If} ${SectionIsSelected} $6
          StrCpy $_CheckedButton${_d} "$6"
          ${ExitDo}
        ${EndIf}
 
        ${If} $4 == $3
        ${AndIf} ${SectionIsSelected} $7
          StrCpy $_CheckedButton${_d} "$7"
        ${EndIf}
      ${EndIf}
 
    ${LoopUntil} $4 >= $3
 
    Pop $7
    Pop $6
    Pop $5
    Pop $4
    Pop $3
    Pop $2
  !macroend