NSIS nsDialogs Plug-in

Next generation of user interface design

Table of Contents

Introduction

nsDialogs allows creation of custom pages in the installer. On top of the built-in pages, nsDialogs can create pages with any type of controls in any order and arrangement. It can create everything from a simple page with one label to form which reacts to user's actions. Modern UI 2, for example, uses nsDialogs to create the welcome and finish pages.

nsDialogs is a new NSIS plug-in, introduced in version 2.29 as a replacement for InstallOptions. nsDialogs doesn't use INI files, so it's way faster than InstallOptions. Integration with the script is tighter and more natural - creating controls is done using plug-in functions and notification is done by directly calling a function in the script. Unlike InstallOptions, there isn't a predefined set of available control type and by providing a lower level access to Windows API, every type of control can be created and pages can be more customizable.

The same thing that makes nsDialogs more flexible can also make it more complicated for users with no knowledge of Win32 API. This is solved by creating a library of predefined functions, defined in script, that allow creation and handling of controls. This way, novices get easy access to the flexibility, while advanced users still get access to the core functionality by modifying the library or simply avoid using it.

Step-by-Step Tutorial

Basic Script

Before using nsDialogs, lets first create a basic script as our skeleton.

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Page instfiles

Section

	DetailPrint "hello world"

SectionEnd

Custom Page

Next, we'll add a custom page where we can use nsDialogs. nsDialogs cannot be used in sections or any other function but a custom page's function.

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Page custom nsDialogsPage
Page instfiles

Function nsDialogsPage
FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Creating Page

Now that the foundations are laid, it's time to use nsDialogs. The first call must always be to nsDialogs::Create. It will create a new dialog in the page and return its HWND on the stack. The result must be popped from the stack to prevent stack corruption. If the result is error, the dialog couldn't be created.

nsDialogs::Create, like every other nsDialogs function but nsDialogs::Show, must always be called with /NOUNLOAD.

nsDialogs::Create accepts one parameter. It has a very specific function, but to keep things simple for this tutorial, it must always be 1018.

HWND is a number that uniquely identifies the dialog and can be used with SendMessage, SetCtlColors and Win32 API.

!include LogicLib.nsh

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Var Dialog

Page custom nsDialogsPage
Page instfiles

Function nsDialogsPage

	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog

	${If} $Dialog == error
		Abort
	${EndIf}

FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Showing Page

Now that the page is created, it's time to show it. This is done with nsDialogs::Show. This function will not return until the user clicks Next, Back or Cancel.

!include LogicLib.nsh

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Var Dialog

Page custom nsDialogsPage
Page instfiles

Function nsDialogsPage
	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog

	${If} $Dialog == error
		Abort
	${EndIf}

	nsDialogs::Show

FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Adding Controls

Compiling the last script and running it results in an empty page which is not very useful. So now we'll add some controls to it to. To do so, we'll use ${NSD_Create*} macros from nsDialogs.nsh. Each of those macros takes 5 parameters - x, y, width, height and text. Each macro also returns one value on the stack, which is the new control's HWND. Like the dialogs HWND, it must be popped from the stack and saved.

Each of the measurements that the macros take can use one of three unit types - pixels, dialog units or percentage of the dialog's size. It can also be negative to indicate it should be measured from the end. To use dialog units, the measurement must be suffixed with the letter u. To use percentage, the measurement must be suffixed with the percentage sign - %. Any other suffix, or no suffix, means pixels.

Dialog units allow creation of dialogs that scale well when different fonts or DPI is used. Its size in pixels is determined on runtime based on the font and the DPI. For example, standard pages in the classic NSIS user interface are 266 dialog units wide and 130 dialog units high. Pages in Modern UI are 300 dialogs units wide and 140 dialog units high. In different resolutions, using different fonts or DPI settings, the dialogs will always have the same size in dialog units, but different size in pixels.

!include nsDialogs.nsh
!include LogicLib.nsh

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Var Dialog
Var Label
Var Text

Page custom nsDialogsPage
Page instfiles

Function nsDialogsPage

	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog

	${If} $Dialog == error
		Abort
	${EndIf}

	${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
	Pop $Label

	${NSD_CreateText} 0 13u 100% -13u "Type something here..."
	Pop $Text

	nsDialogs::Show

FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Available control types that can be created with ${NSD_Create*} are:

Control State

Now that we have some controls that the user can interact with, it's time to see what the user actually does with them. For that, we'll first add a leave callback function to our page. In that function, we'll query the state of the text control we've created and display it to the user. To do so, we'll use the ${NSD_GetText} macro.

Note that not all controls support ${NSD_GetText} and some require special handling with specific messages defined in WinMessages.nsh. For example, the ListBox control requires usage of LB_GETCURSEL and LB_GETTEXT. With time, the library of macros in nsDialogs.nsh will fill with more and more macros that'll handle more cases like this.

!include nsDialogs.nsh
!include LogicLib.nsh

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Var Dialog
Var Label
Var Text

Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles

Function nsDialogsPage

	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog

	${If} $Dialog == error
		Abort
	${EndIf}

	${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
	Pop $Label

	${NSD_CreateText} 0 13u 100% -13u "Type something here..."
	Pop $Text

	nsDialogs::Show

FunctionEnd

Function nsDialogsPageLeave

	${NSD_GetText} $Text $0
	MessageBox MB_OK "You typed:$\n$\n$0"

FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Real-time Notification

One of the more exciting new features of nsDialogs is callback function notification of changes to the dialog. nsDialogs can call a function defined in a script in response to a user action such as changing of a text field or click of a button. To make nsDialogs notify us of events, we'll use ${NSD_OnClick} and ${NSD_OnChange}. Not every control supports both of the events. For example, there's nothing to notify about labels.

When the callback function is called, the control's HWND will be waiting on the stack and must be popped to prevent stack corruption. In this simple example, this is not so useful. But in case of a bigger script where several controls are associated with the same callback function, the HWND can shed some light on which control originated the event.

The new example will respond to the user type hello in the text box.

!include nsDialogs.nsh
!include LogicLib.nsh

Name nsDialogs
OutFile nsDialogs.exe

XPStyle on

Var Dialog
Var Label
Var Text

Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles

Function nsDialogsPage

	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog

	${If} $Dialog == error
		Abort
	${EndIf}

	${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
	Pop $Label

	${NSD_CreateText} 0 13u 100% -13u "Type something here..."
	Pop $Text
	${NSD_OnChange} $Text nsDialogsPageTextChange

	nsDialogs::Show

FunctionEnd

Function nsDialogsPageLeave

	${NSD_GetText} $Text $0
	MessageBox MB_OK "You typed:$\n$\n$0"

FunctionEnd

Function nsDialogsPageTextChange

	Pop $1 # $1 == $ Text

	${NSD_GetText} $Text $0

	${If} $0 == "hello"

		MessageBox MB_OK "right back at ya!"

	${EndIf}

FunctionEnd

Section

	DetailPrint "hello world"

SectionEnd

Function Reference

Create

nsDialogs::Create /NOUNLOAD rect

Creates a new dialog. rect specific the identifier of the control whose location will be mimiced. This should usually be 1018, which is control mimiced for creation of built-in pages. The Modern UI also has control 1040 for the welcome and the finish page.

Returns the new dialog's HWND on the stack or error.

CreateControl

nsDialogs::CreateControl /NOUNLOAD class style extended_style x y width height text

Create a new control in the current dialog. A dialog must exist for this to work, so nsDialogs::Create must be called prior to this function.

Returns the new dialog's HWND on the stack or error.

Show

nsDialogs::Show

Displays the page. Call this once finished with nsDialogs::Create, nsDialogs::CreateControl and the rest.

Returns nothing.

SelectFileDialog

nsDialogs::SelectFileDialog /NOUNLOAD mode initial_selection filter

Displays a file selection dialog to the user. If mode is set to save, displays a file save dialog. If mode is set to open, displays a file open dialog. filter is a list of available file filters separated by pipes. If an empty string is passed, the default is used - All Files|*.*.

Returns the selected file on the stack or an empty string if the user canceled the operation.

SelectFolderDialog

nsDialogs::SelectFolderDialog /NOUNLOAD title initial_selection

Displays a directory selection dialog to the user.

Returns the selected directory on the stack or an empty string if the user canceled the operation.

SetRTL

nsDialogs::SetRTL /NOUNLOAD rtl_setting

Sets right-to-left mode on or off. If rtl_setting is 0, it's set to off. If rtl_setting is 1, it's set to on. This function must be called before any calls to nsDialogs::CreateControl.

Returns nothing.

GetUserData

nsDialogs::GetUserData /NOUNLOAD control_HWND

Returns user data associated with the control on the stack. Use nsDialogs::SetUserData to set this data.

SetUserData

nsDialogs::SetUserData /NOUNLOAD control_HWND data

Associates data with the control. Use nsDialogs::GetUserData to get this data.

Returns nothing.

OnBack

nsDialogs::OnBack /NOUNLOAD function_address

Sets the callback function for the Back button. This function will be called when the user clicks the back button. Call Abort in this function to prevent the user from going back to the last page.

Use GetFunctionAddress to get the address of the desired callback function.

Returns nothing.

OnChange

nsDialogs::OnChange /NOUNLOAD control_HWND function_address

Sets a change notification callback function for the given control. Whenever the control changes, the function will be called and the control's HWND will be waiting on its stack.

Use GetFunctionAddress to get the address of the desired callback function.

Returns nothing.

OnClick

nsDialogs::OnClick /NOUNLOAD control_HWND function_address

Sets a click notification callback function for the given control. Whenever the control is clicked, the function will be called and the control's HWND will be waiting on its stack.

Use GetFunctionAddress to get the address of the desired callback function.

Returns nothing.

OnNotify

nsDialogs::OnNotify /NOUNLOAD control_HWND function_address

Sets a notification callback function for the given control. Whenever the control receives the WM_NOTIFY message, the function will be called and the control's HWND, notification code and a pointer to the MNHDR structure will be waiting on its stack.

Use GetFunctionAddress to get the address of the desired callback function.

Returns nothing.

FAQ