Adding DropList with available drives instead of directory page

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


Description

The following example demonstrates, how to add a droplist with available drives instead of adding the directory page. Calculates the free space of every drive and adds only those drives that they have enough space for the uncompressed installation. Provided as a starting point for the subject at this forum thread.

The Code

!define SIZE "100" ;add here total uncompressed data size in Mb of your application
!define APPNAME "My Application"
Name '${APPNAME}'
OutFile 'test.exe'
LicenseData '${NSISDIR}\License.txt'
LicenseBkColor 0xFFFFFF
ShowInstDetails show
AllowRootDirInstall true
 
!include WinMessages.nsh
!include FileFunc.nsh
!insertmacro GetDrives
!insertmacro DriveSpace
 
Page License
Page Custom CustomCreate CustomLeave
Page InstFiles
 
Section "boo"
        SetOutPath '$INSTDIR'
SectionEnd
 
Function CustomCreate
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Settings' 'NumFields' '6'
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Top' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Right' '-6'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Bottom' '17'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Text' \
         'Select Installation drive:'
 
         StrCpy $R2 0
         StrCpy $R0 ''
         ${GetDrives} "HDD" GetDrivesCallBack
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Type' 'DropList'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Left' '30'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Top' '26'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Right' '-31'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Bottom' '100'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Flags' 'Notify'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'State' '$R1'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'ListItems' '$R0'
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Top' '109'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Right' '59'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Bottom' '119'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Text' \
         'Space required:'
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Left' '60'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Top' '109'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Right' '-5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Bottom' '119'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Text' \
         '${SIZE} Mb'
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Top' '120'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Right' '59'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Bottom' '130'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Text' \
         'Space available:'
 
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Left' '60'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Top' '120'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Right' '-5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Bottom' '130'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Text' \
         '$R3 Mb'
 
         push $0
         InstallOptions::Dialog '$PLUGINSDIR\custom.ini'
         pop $0
         pop $0
FunctionEnd
 
Function CustomLeave
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Settings' 'State'
        StrCmp $0 '2' 0 next
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        StrCpy $0 $0 3
        ${DriveSpace} "$0" "/D=F /S=M" $R3
        WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Text' \
        '$R3 Mb'
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 6' 'HWND'
        SendMessage $0 ${WM_SETTEXT} 0 'STR:$R3 Mb'
        Abort
 
     next:
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        StrCpy '$INSTDIR' '$0'
FunctionEnd
 
Function GetDrivesCallBack
         ${DriveSpace} "$9" "/D=F /S=M" $R4
         IntCmp $R4 '${SIZE}' end end def
      def:
         StrCmp $R2 '0' 0 next
         StrCpy $R3 '$R4'
         StrCpy $R1 '$9${APPNAME}'
         IntOp $R2 $R2 + 1
      next:
         StrCpy $R0 '$R0$9${APPNAME}|'
      end:
	 Push $0
FunctionEnd
 
Function .onInit
         InitPluginsDir
         GetTempFileName $0
         Rename $0 '$PLUGINSDIR\custom.ini'
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.