NsDialogs CreateIPaddress

From NSIS Wiki
Jump to navigationJump to search

Description

This header is an extension to the existing nsDialogs.nsh header file, and adds the ability to create IP address controls via a new ${NSD_CreateIPaddress} command. IP address controls look like a standard edit (text) control, but separated logically by 3 dots, where each of the 4 parts only accepts numbers within the range 0 .. 255. An IP address control is the logical choice when requiting IP address input. ofer

Attribution

This header is the result of the forum discussion nsDialogs IP Address Control, and credit goes largely to Xokar and Anders for the correct system calls and to the nsDialogs header team for the conventions laid out therein.

A fix for a font corruption bug stems from the forum discussion Problem of NSD_CreateIPAddress.nsh

Header


MD5: 23e8d124fadf7f9af7ea865fe495675a | Filesize: 1023B

Creating

Although typically the various internet controls may already be initialized on your system, it is a good idea to make sure they are indeed initialized. For the IPaddress control, use ${NSD_InitIPaddress} to initialize the control. If used, this should only be needed once (e.g. in .onGuiInit).

Creating the IPaddress control is as simple as creating any other control, using '${NSD_CreateIPaddress} top left width height ""'. Note that the IP address control does not take any label text and is thus left blank.

Writing

You can set the IP address using the existing '${NSD_SetText}' function. For example: '${NSD_SetText} <control> "192.168.0.0"'. The accepted text must be a valid IP address of format #.#.#.# .

Callbacks

The IPaddress control only triggers the '${NSD_OnNotify}' callback. This callback is triggered in two cases:

  * twice after one part of an IP address is entered (once for the entered text, once again for
the next part receiving focus). To separate the two events, set a temporary variable. * once after the IP address control loses focus (user tabs out, clicks out, etc.)

If you want to do some type of validation on the IP address entered once the control loses focus you will have to get the focus of the current control and compare that to the four hwnds of each of the IP address fields.

You can get each of the fields using:

FindWindow $Field4 "" "" $IPAddressControl
FindWindow $Field3 "" "" $IPAddressControl $Field4
FindWindow $Field2 "" "" $IPAddressControl $Field3
FindWindow $Field1 "" "" $IPAddressControl $Field2

You can get the hwnd of the control that currently has focus using:

System::Call "user32::GetFocus(v) i.r0" ; $0 now contains the hwnd

Using this information you can check if, after a notification, one of the 4 IP address fields has focus, or whether focus was lost from all the IP address fields; using LogicLib:

${Select} $0
	${Case} $Field1
	${Case} $Field2
	${Case} $Field3
	${Case} $Field4
	${Default}
		; process here
${EndSelect}

Reading

To read the IP address entered, you can use the existing '${NSD_GetText} <control> <outvar>' convention. The IP address is returned as a single string of format #.#.#.# . If you need to check pieces of the IP address, you will have to manually separate it into the appropriate pieces using existing string manipulation functions.

Example

outfile 'nsdialogs_createIPaddress.exe'
 
!include 'nsdialogs.nsh'
!include 'nsdialogs_createIPaddress.nsh'
 
Page Custom CustomPre
 
Function CustomPre
	nsDialogs::Create 1018
	Pop $R0
 
	${If} $R0 == error
		Abort
	${EndIf}
 
	; This would more appropriately be called in .onGUIInit
	${NSD_InitIPaddress}
	Pop $0
	IntCmp $0 0 0 +3 +3
	MessageBox MB_OK "Something went wrong while initializing the IPaddress control"
	Abort
 
	${NSD_CreateIPaddress} 5% 90% 30% 12u ""
	Pop $0
 
	nsDialogs::Show
FunctionEnd
 
Section
SectionEnd

Example get data

outfile 'test_install.exe'
 
!include 'nsdialogs.nsh'
!include 'nsdialogs_createIPaddress.nsh'
 
Page Custom MyDialogePre MyDialogeAfter
Page Custom MyNextPage
 
XPStyle on
 
Var IPAddressControl
Var IPADR
 
Function MyDialogePre
  nsDialogs::Create 1018
  Pop $R0
 
  ${If} $R0 == error
    Abort
  ${EndIf}
 
  ; This would more appropriately be called in .onGUIInit
  ${NSD_InitIPaddress}
  Pop $0
 
  IntCmp $0 0 0 +3 +3
  MessageBox MB_OK "Something went wrong while initializing the IPaddress control"
  Abort
 
  ${NSD_CreateLabel} 0u 0 50% 10% "Input IP address:"
  Pop $0
  ${NSD_CreateIPaddress} 5% 90% 30% 12u ""
  Pop $IPAddressControl
  ${NSD_SetText} $IPAddressControl "192.168.1.1"
  Pop $IPAddressControl
  
  nsDialogs::Show
FunctionEnd
 
Function MyDialogeAfter
  ${NSD_GetText} $IPAddressControl $IPADR
  MessageBox MB_OK "IP=$IPADR: Here you could save IP-address to file..."
FunctionEnd
 
Function MyNextPage
  MessageBox MB_OK "(IP=$IPADR): Do some more stuff here..."
FunctionEnd
 
Section
SectionEnd

Styling

Note that due to the nature of the control, you can only apply limited styling. For example, changing the background color of the control will result in an incorrectly painted control, while attempts to change the background color of any of the 4 fields will result in a crash.