NSIS Change Header Image Based on Language Selection - nsis

I am using NSIS to create an installer. How can I change the header image (MUI_HEADERIMAGE_BITMAP) based on the users language selection at install?

!define MUI_HEADERIMAGE
!define MUI_CUSTOMFUNCTION_GUIINIT myGuiInit
!include MUI2.nsh
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_COMPONENTS
...
!insertmacro MUI_LANGUAGE English
!insertmacro MUI_LANGUAGE Swedish
Function .onInit
!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd
Function myGUIInit
InitPluginsDir
${If} ${LANG_ENGLISH} = $Language
File "/oname=$PluginsDir\langspecifichdr.bmp" "${NSISDIR}\Contrib\Graphics\Header\orange.bmp"
${Else}
File "/oname=$PluginsDir\langspecifichdr.bmp" "${NSISDIR}\Contrib\Graphics\Header\win.bmp"
${EndIf}
SetBrandingImage /IMGID=1046 "$PluginsDir\langspecifichdr.bmp"
FunctionEnd

Related

How to set the Focus to a License Agreement Checkbox control using NSIS

I wanted to set the by default Focus to the "License Agreement" checkbox in the License Agreement Page using NSIS. And then the taborder should work.
Please help me how to set the focus?
Below is my code snippet:
!include LogicLib.nsh
!define MUI_TEXT_WELCOME_INFO_TITLE $(welcometitle)
!define MUI_TEXT_WELCOME_INFO_TEXT $(welcometext)
!insertmacro MUI_PAGE_WELCOME
!define MUI_LICENSEPAGE_CHECKBOX ""
!define MUI_INNERTEXT_LICENSE_BOTTOM ""
!define MUI_INNERTEXT_LICENSE_TOP ""
!define MUI_INNERTEXT_LICENSE_BOTTOM_CHECKBOX ""
!define MUI_TEXT_LICENSE_TITLE $(licensetitle)
!define MUI_TEXT_LICENSE_SUBTITLE $(licensesubtitle)
!define MUI_LICENSEPAGE_CHECKBOX_TEXT $(licensecheckboxtext)
!insertmacro MUI_PAGE_LICENSE "C:\Program Files (x86)\NSIS\Docs\Modern UI 2\license.txt"
!define MUI_DIRECTORYPAGE_TEXT_TOP $(mydirtoptext)
!define MUI_TEXT_DIRECTORY_TITLE $(mydirtitle)
!define MUI_TEXT_DIRECTORY_SUBTITLE $(mydirsubtitle)
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_TEXT_FINISH_INFO_TITLE $(Finishpagetitle)
!define MUI_TEXT_FINISH_INFO_TEXT $(Finishpagesubtitle)
!define MUI_FINISHPAGE_RUN ""
!define MUI_FINISHPAGE_RUN_TEXT $(FinishonlineReg)
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_INSTFILES
;--------------------------------
!insertmacro MUI_LANGUAGE "English"
; The stuff to install
Section "My TestApp (required)"
SectionEnd
The Windows UI guidelines says:
For keyboard navigation, tab order should flow in a logical order, generally from left to right, top to bottom.
but if you think you must force then you can do this:
!include WinMessages.nsh
!include MUI2.nsh
!insertmacro MUI_PAGE_WELCOME
!define MUI_LICENSEPAGE_CHECKBOX ""
!define MUI_PAGE_CUSTOMFUNCTION_SHOW myForceLicenseFocus
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Function myForceLicenseFocus
!if ${MUI_SYSVERSION} >= 2.0
FindWindow $0 "BUTTON" "" $mui.LicensePage
!else
FindWindow $0 "#32770" "" $hWndParent
FindWindow $0 "BUTTON" "" $0
!endif
System::Call 'USER32::SetFocus(pr0)'
!define /ifndef WM_CHANGEUISTATE 0x127
System::Call 'USER32::PostMessage(pr0,i${WM_CHANGEUISTATE},p0x30002,p0)' ; This forces the checkbox to display the focus rectangle
System::Call 'USER32::SetWindowPos(pr0,p0,i,i,i,i,i0x13)' ; Change tab order
FunctionEnd

Skipping pages: Button should say "Install" instead of "Next"

I have an installer using the Modern UI 2 (MUI 2) that installs up to three components into different locations.
I use a MUI_PAGE_COMPONENTS page and multiple MUI_PAGE_DIRECTORY pages which are skipped automatically if the corresponding section is not selected (like described here).
So far so good.
However, if the last of the three components is not chosen to be installed, the button on the previous (not-skipped) directory page should say Install instead of Next, because next the last directory page will be skipped and the installation will be executed.
Since we already know which page will be skipped after we leave the components page, I wonder if there is a way to make it work?
Minimal example:
!include MUI2.nsh
InstallDir $EXEDIR
OutFile "skip.exe"
RequestExecutionLevel user
ShowInstDetails show
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_PRE directoryPreA
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_PRE directoryPreB
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section "A" SEC_A
DetailPrint "Installing A..."
SectionEnd
Section /o "B" SEC_B
DetailPrint "Installing B..."
SectionEnd
Function directoryPreA
# Skips the directory page for A if not chosen for installation.
${Unless} ${SectionIsSelected} ${SEC_A}
Abort
${EndUnless}
FunctionEnd
Function directoryPreB
# Skips the directory page for B if not chosen for installation.
${Unless} ${SectionIsSelected} ${SEC_B}
Abort
${EndUnless}
FunctionEnd
!include MUI2.nsh
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_DIRECTORYPAGE_TEXT_TOP "Dir:A"
!define MUI_PAGE_CUSTOMFUNCTION_PRE directoryPreA
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_DIRECTORYPAGE_TEXT_TOP "Dir:B"
!define MUI_PAGE_CUSTOMFUNCTION_PRE directoryPreB
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section "A" SEC_A
DetailPrint "Installing A..."
SectionEnd
Section /o "B" SEC_B
DetailPrint "Installing B..."
SectionEnd
Function .onSelChange
GetDlgItem $0 $HWNDPARENT 1
${If} ${SectionIsSelected} ${SEC_A}
${OrIf} ${SectionIsSelected} ${SEC_B}
SendMessage $0 ${WM_SETTEXT} 0 "STR:$(^NextBtn)"
${Else}
SendMessage $0 ${WM_SETTEXT} 0 "STR:$(^InstallBtn)"
${EndIf}
FunctionEnd
Function directoryPreA
# Skips the directory page for A if not chosen for installation.
${IfNot} ${SectionIsSelected} ${SEC_A}
Abort
${ElseIfNot} ${SectionIsSelected} ${SEC_B}
GetDlgItem $0 $HWNDPARENT 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:$(^InstallBtn)"
${EndIf}
FunctionEnd
Function directoryPreB
# Skips the directory page for B if not chosen for installation.
${Unless} ${SectionIsSelected} ${SEC_B}
Abort
${EndUnless}
FunctionEnd

Nsis: Change Title bar for Two installations in one installer

I have two installations in one installer from:
http://nsis.sourceforge.net/Two_installations_in_one_installer
How can I customize the title bar (setup name) for the two installations?
NSIS is not really designed to do this so you have to set it manually every time a page changes:
!include MUI.nsh
!include WinMessages.nsh
Caption " "
!define MUI_PAGE_CUSTOMFUNCTION_SHOW lic1
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW inst1
!insertmacro MUI_PAGE_INSTFILES
!define MUI_PAGE_CUSTOMFUNCTION_SHOW lic2
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW inst2
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Function lic1
SendMessage $HWNDPARENT ${WM_SETTEXT} 0 "STR:Foo$(^LicenseSubCaption)"
FunctionEnd
Function inst1
SendMessage $HWNDPARENT ${WM_SETTEXT} 0 "STR:Foo$(^InstallingSubCaption)"
FunctionEnd
Function lic2
SendMessage $HWNDPARENT ${WM_SETTEXT} 0 "STR:Bar$(^LicenseSubCaption)"
FunctionEnd
Function inst2
SendMessage $HWNDPARENT ${WM_SETTEXT} 0 "STR:Bar$(^InstallingSubCaption)"
FunctionEnd

NSIS nsDialog has disabled back/next/cancel buttons?

I'm following the NSIS nsDialog tutorial with a goal to gather some user input I will use to write into a config file later. The NSISEclipse plugin has generated an MUI2 template that has gotten me started and successfully installs the files.
I have used the nsDialog tutorial to generate a dialog that does get emitted exactly where I want it to, but for some reason the "Back/Next/Cancel" buttons are all grey. I don't see where in the nsDialog documentation it says to enable/disable those buttons.
Here's my code:
Var Dialog
Var Label
Var MyTextBox
Section configLocationDialog
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 10% 20u 80% 12u "Hello World"
Pop $MyTextbox
nsDialogs::Show
SectionEnd
And, the dialog once displayed:
What do I need to change to tell NSIS to allow back/next/cancel?
EDIT More code was asked for. Here's the whole thing as it exists now.
# Auto-generated by EclipseNSIS Script Wizard
# Dec 19, 2013 3:48:34 PM
Name SecureKeypad
# General Symbol Definitions
!define REGKEY "SOFTWARE\$(^Name)"
!define VERSION 1.0.0
!define COMPANY **COMPANY**
!define URL http://www.**COMPANY**.com
# MultiUser Symbol Definitions
!define MULTIUSER_EXECUTIONLEVEL Standard
!define MULTIUSER_INSTALLMODE_COMMANDLINE
!define MULTIUSER_INSTALLMODE_INSTDIR $PROGRAMFILES\SecureKeypad
!define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_KEY "${REGKEY}"
!define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_VALUE "Path"
# MUI Symbol Definitions
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install-colorful.ico"
!define MUI_FINISHPAGE_NOAUTOCLOSE
!define MUI_STARTMENUPAGE_REGISTRY_ROOT HKLM
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_REGISTRY_KEY ${REGKEY}
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME StartMenuGroup
!define MUI_STARTMENUPAGE_DEFAULTFOLDER SecureKeypad
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall-colorful.ico"
!define MUI_UNFINISHPAGE_NOAUTOCLOSE
# Included files
#!include MultiUser.nsh
!include LogicLib.nsh
!include nsDialogs.nsh
!include AdvReplaceInFile.nsh
!include Sections.nsh
!include MUI2.nsh
# Variables
Var StartMenuGroup
# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
# Installer languages
!insertmacro MUI_LANGUAGE English
# Installer attributes
OutFile setup.exe
CRCCheck on
InstallDir $PROGRAMFILES\${COMPANY}\SecureKeypad
XPStyle on
ShowInstDetails show
VIProductVersion 1.0.0.0
VIAddVersionKey ProductName SecureKeypad
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey CompanyWebsite "${URL}"
VIAddVersionKey FileVersion "${VERSION}"
VIAddVersionKey FileDescription ""
VIAddVersionKey LegalCopyright ""
InstallDirRegKey HKLM "${REGKEY}" Path
ShowUninstDetails show
# Input dialogs
Var Dialog
Var Label
Var MyTextbox
#Page custom configLocationDialog configLocationDialogLeave
#Page custom doConfiguration
Section configLocationDialog
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
GetFunctionAddress $0 "configLocationDialogLeave"
nsDialogs::OnBack $Dialog $0
#nsDialogs::OnNext $Dialog
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 10% 20u 80% 12u "Hello World"
Pop $MyTextbox
nsDialogs::Show
SectionEnd
Function configLocationDialogLeave
${NSD_GetText} $MyTextbox $0
MessageBox mb_ok $0
FunctionEnd
# Installer sections
Section -Main SEC0000
SetOutPath $INSTDIR
SetOverwrite on
File installable-content\CYBSSecurity.dll
File installable-content\SecureKeypad.exe
File installable-content\SecureKeypad.exe.config
File installable-content\SecureKeypad.config
File installable-content\SecureKeypad.frmSecureKeypad.resources
WriteRegStr HKLM "${REGKEY}\Components" Main 1
!insertmacro AdvReplaceInFile $INSTDIR\SecureKeypad.exe.config "##configFile##" $INSTDIR
SectionEnd
Section -post SEC0001
WriteRegStr HKLM "${REGKEY}" Path $INSTDIR
SetOutPath $INSTDIR
WriteUninstaller $INSTDIR\uninstall.exe
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
SetOutPath $SMPROGRAMS\$StartMenuGroup
CreateShortcut "$SMPROGRAMS\$StartMenuGroup\Uninstall $(^Name).lnk" $INSTDIR\uninstall.exe
!insertmacro MUI_STARTMENU_WRITE_END
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayName "$(^Name)"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayVersion "${VERSION}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" Publisher "${COMPANY}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" URLInfoAbout "${URL}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayIcon $INSTDIR\uninstall.exe
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" UninstallString $INSTDIR\uninstall.exe
WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoModify 1
WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoRepair 1
SectionEnd
# Macro for selecting uninstaller sections
!macro SELECT_UNSECTION SECTION_NAME UNSECTION_ID
Push $R0
ReadRegStr $R0 HKLM "${REGKEY}\Components" "${SECTION_NAME}"
StrCmp $R0 1 0 next${UNSECTION_ID}
!insertmacro SelectSection "${UNSECTION_ID}"
GoTo done${UNSECTION_ID}
next${UNSECTION_ID}:
!insertmacro UnselectSection "${UNSECTION_ID}"
done${UNSECTION_ID}:
Pop $R0
!macroend
# Uninstaller sections
Section /o -un.Main UNSEC0000
Delete /REBOOTOK $INSTDIR\SecureKeypad.frmSecureKeypad.resources
Delete /REBOOTOK $INSTDIR\SecureKeypad.exe.config
Delete /REBOOTOK $INSTDIR\SecureKeypad.exe
Delete /REBOOTOK $INSTDIR\<sensored>.dll
DeleteRegValue HKLM "${REGKEY}\Components" Main
SectionEnd
Section -un.post UNSEC0001
DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\Uninstall $(^Name).lnk"
Delete /REBOOTOK $INSTDIR\uninstall.exe
DeleteRegValue HKLM "${REGKEY}" StartMenuGroup
DeleteRegValue HKLM "${REGKEY}" Path
DeleteRegKey /IfEmpty HKLM "${REGKEY}\Components"
DeleteRegKey /IfEmpty HKLM "${REGKEY}"
RmDir /REBOOTOK $SMPROGRAMS\$StartMenuGroup
RmDir /REBOOTOK $INSTDIR
SectionEnd
# Installer functions
Function .onInit
InitPluginsDir
FunctionEnd
# Uninstaller functions
Function un.onInit
!insertmacro MUI_STARTMENU_GETFOLDER Application $StartMenuGroup
!insertmacro SELECT_UNSECTION Main ${UNSEC0000}
FunctionEnd
Calling nsDialogs::Create/Show in a section is never valid, they can only be called in the create callback function of a custom page.
Some buttons will be disabled after the instfiles page (Already installed, cannot cancel or go back) but this is done by NSIS itself, not nsDialogs...
If you put your call to nsDialogs:Create in a section it will be run when the installer is installing files and such. In that state you won't be able to keep navigating through the pages in the installer and you are stuck.
Your notice about your custom page running at the end of the installer is really what you should be trying to fix, not dismissing it as the wrong way (it's the only way). NSIS-scripts are quite simple in many ways. For example, pages are loaded in the order that they are declared. So when you declare that you want MUI_PAGE_WELCOME, MUI_PAGE_DIRECTORY and the others you are also setting the order of them.
# Installer pages
!insertmacro MUI_PAGE_WELCOME # First page
!insertmacro MUI_PAGE_DIRECTORY # Second page
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup # Third page
!insertmacro MUI_PAGE_INSTFILES # Etc
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
So if you then try to keep all your code together and add your custom page alongside the functions that handle it, it will undoubtedly be the last page in that order.
# Installer pages
!insertmacro MUI_PAGE_WELCOME # First page
!insertmacro MUI_PAGE_DIRECTORY # Second page
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup # Third page
...
# Input dialogs
Var Dialog
Var Label
Var MyTextbox
Page custom configLocationDialog configLocationDialogLeave # Last page
Section configLocationDialog
nsDialogs::Create 1018
Pop $Dialog
So to illustrate this I took your code and brewed it down to basically the page you wanted.
# Included files
!include LogicLib.nsh
!include nsDialogs.nsh
!include Sections.nsh
!include MUI2.nsh
# Installer pages
!insertmacro MUI_PAGE_WELCOME # First page
Page custom configLocationDialog configLocationDialogLeave # Second page!
!insertmacro MUI_PAGE_FINISH # Last page
# Installer languages
!insertmacro MUI_LANGUAGE English
# Input dialogs
Var Dialog
Var Label
Var MyTextbox
Function configLocationDialog
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
GetFunctionAddress $0 "configLocationDialogLeave"
nsDialogs::OnBack $Dialog $0
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 10% 20u 80% 12u "Hello World"
Pop $MyTextbox
nsDialogs::Show
FunctionEnd
Function configLocationDialogLeave
${NSD_GetText} $MyTextbox $0
MessageBox mb_ok $0
FunctionEnd
Section -Main SEC0000
SectionEnd
The NSISEclipse plugin generates a lot of code, including a set of macros that execute the various pages that are displayed:
# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
The trick is to inject the page call at the right step.
# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup
Page custom configLocationDialog configLocationDialogLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

NSIS: Use selected language for MessageBox in .onInit (MUI2)

I try to get a localized message box in the .onInit method which fails with the following code:
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "German"
LangString Message ${LANG_ENGLISH} "This is a message."
LangString Message ${LANG_GERMAN} "Dies ist eine Nachricht"
Function .onInit
!insertmacro MUI_LANGDLL_DISPLAY
MessageBox MB_OK "$(Message)"
FunctionEnd
The MessageBox always shows the same language string.
The Problem is, that the language is processed after the .onInit method.
A workaround for this could be to put the custom code from the .onInit method to the .onGUIInit method.
With MUI2 this is done as follows:
!define MUI_CUSTOMFUNCTION_GUIINIT myGuiInit
!include "MUI2.nsh"
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "German"
LangString Message ${LANG_ENGLISH} "This is a message."
LangString Message ${LANG_GERMAN} "Dies ist eine Nachricht"
Function .onInit
!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd
Function myGuiInit
MessageBox MB_OK "$(Message)"
FunctionEnd
Now the MessageBox should show the correctly localized message.

Resources