Managing Sections on Runtime

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


Description

The following example on how to manage sections on runtime provived for the subject at this forum thread.

The Code

; the following piece could be used also as a header e.g. myheader.nsh
##          HEADER PART BEGINS HERE
!define SF_USELECTED  0
!define SF_SELECTED   1
!define SF_SECGRP     2
!define SF_BOLD       8
!define SF_RO         16
!define SF_EXPAND     32
###############################
 
!macro SecSelect SecId
  Push $0
  IntOp $0 ${SF_SELECTED} | ${SF_RO}
  SectionSetFlags ${SecId} $0
  SectionSetInstTypes ${SecId} 1
  Pop $0
!macroend
 
!define SelectSection '!insertmacro SecSelect'
#################################
 
!macro SecUnSelect SecId
  Push $0
  IntOp $0 ${SF_USELECTED} | ${SF_RO}
  SectionSetFlags ${SecId} $0
  SectionSetText  ${SecId} ""
  Pop $0
!macroend
 
!define UnSelectSection '!insertmacro SecUnSelect'
###################################
 
!macro SecExtract SecId
  Push $0
  IntOp $0 ${SF_USELECTED} | ${SF_RO}
  SectionSetFlags ${SecId} $0
  SectionSetInstTypes ${SecId} 2
  Pop $0
!macroend
 
!define SetSectionExtract '!insertmacro SecExtract'
###################################
 
!macro Groups GroupId
  Push $0
  SectionGetFlags ${GroupId} $0
  IntOp $0 $0 | ${SF_RO}
  IntOp $0 $0 ^ ${SF_BOLD}
  IntOp $0 $0 ^ ${SF_EXPAND}
  SectionSetFlags ${GroupId} $0
  Pop $0
!macroend
 
!define SetSectionGroup "!insertmacro Groups"
####################################
 
!macro GroupRO GroupId
  Push $0
  IntOp $0 ${SF_SECGRP} | ${SF_RO}
  SectionSetFlags ${GroupId} $0
  Pop $0
!macroend
 
!define MakeGroupReadOnly '!insertmacro GroupRO'
 
##        HEADER PART ENDS HERE
 
; !include myheader.nsh
 
Name InstTest
OutFile 'InstTest.exe'
 
InstType "Install"
InstType "Extract"
InstType /NOCUSTOM
 
var nakedking
 
!include MUI.nsh
 
!insertmacro MUI_PAGE_LICENSE '${NSISDIR}\license.txt'
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
 
!insertmacro MUI_LANGUAGE "English"
 
SectionGroup "Install Java" GRP_1
Section "Install Java SDK" SEC_1
;;;;;;;;;;;;;;
SectionEnd
 
Section "Install MySQL database server" SEC_2
;;;;;;;;;;;;;;
SectionEnd
 
Section "ISA Manager" SEC_3
;;;;;;;;;;;;;;;
SectionEnd
 
Section "Configure Database" SEC_4
;;;;;;;;;;;;;
SectionEnd
 
Section "Update Quantel database" SEC_5
;;;;;;;;;;;;;;
SectionEnd
 
Section "Downdate Quantel database" SEC_6
;;;;;;;;;;;;;
SectionEnd
SectionGroupEnd
 
SectionGroup "Extract Files" GRP_2
Section "File Extract Only" SEC_7
;;;;;;;;;;;;;;
SectionEnd
SectionGroupEnd
 
Function .onInit
  MessageBox MB_YESNOCANCEL|MB_ICONQUESTION \
  'Select YES to watch the action of the first instance.$\r$\n\
  Select NO to watch the action of the second instance.$\r$\n\
  Or select CANCEL to watch the king naked.' \
  IDYES condition1 IDNO condition2
  StrCpy '$nakedking' '1'
  goto exit
 
 condition1:
 
        ${SelectSection} ${SEC_1}
 
        ${SelectSection} ${SEC_5}
 
        ${UnSelectSection} ${SEC_2}
 
        ${UnSelectSection} ${SEC_3}
 
        ${UnSelectSection} ${SEC_4}
 
        ${UnSelectSection} ${SEC_6}
 
        ${SetSectionExtract} ${SEC_7}
 
        ${MakeGroupReadOnly} ${GRP_2}
        ${SetSectionGroup} ${GRP_1}
  goto exit
 
 condition2:
 
         ${SelectSection} ${SEC_1}
 
         ${SelectSection} ${SEC_5}
 
         ${SelectSection} ${SEC_2}
 
         ${SelectSection} ${SEC_3}
 
         ${UnSelectSection} ${SEC_4}
 
         ${UnSelectSection} ${SEC_6}
 
         ${SetSectionExtract} ${SEC_7}
 
         ${MakeGroupReadOnly} ${GRP_2}
         ${SetSectionGroup} ${GRP_1}
 
 exit:
FunctionEnd
 
Function .onSelChange
         StrCmp '$nakedking' '1' +1 +2
         MessageBox MB_OK|MB_ICONINFORMATION 'The king is naked' IDOK end
         ${SetSectionGroup} ${GRP_1}
         ${SetSectionGroup} ${GRP_2}
  end:
FunctionEnd

License

This script is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this code.

Permission is granted to anyone to use this script for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this script must not be misrepresented; you must not claim that you wrote the original code. If you use this script in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered versions must be plainly marked as such, and must not be misrepresented as being the original script.
  3. This notice may not be removed or altered from any distribution.