Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain data on custom pages when back button is pressed

Tags:

nsis

Incase of custom pages in NSIS script is there any way to retain the data entered by user when back button is pressed (when the installer is running)?

like image 948
Pia Avatar asked Jan 15 '10 06:01

Pia


1 Answers

There are a couple of ways to do this. Either way you need to store your data in globals.

1) Use a "Leave" function.

Page custom EnterCustom LeaveCustom

; Create two variables for each value/widget pair
Var Text
Var TextWidget
Var Check
Var CheckWidget

Function EnterCustom
  nsDialogs::Create 1018
  Pop $0

  ${NSD_CreateText} 0 0 80u 12u $Text
  Pop $TextWidget

  ${NSD_CreateCheckBox} 0 26u 80u 12u "Check this box"
  Pop $CheckWidget
  ${NSD_SetState} $CheckWidget $Check

  nsDialogs::Show
FunctionEnd

Function LeaveCustom
  ${NSD_GetText} $TextWidget $Text
  ${NSD_GetState} $CheckWidget $Check
FunctionEnd

The only problem with this method is that LeaveCustom only gets called if you hit the next button. So if you edit the fields then click the Back button your changes are lost. The changes are however saved if you go forward then come back.

2) Use the OnChange callback.

This is a little more complicated but solves the problem with the previous method.

Page custom EnterCustom

Var Initialized
; Create two variables for each value/widget pair
Var Text
Var TextWidget
Var Check
Var CheckWidget

Function EnterCustom
  nsDialogs::Create 1018
  Pop $0

  ${If} $Initialized != "True"
    ; Set defaults for all your values here
    StrCpy $Text "Initial Value"
    StrCpy $Check ${BST_UNCHECKED}
    StrCpy $Initialized "True"
  ${EndIf}

  ; Create and configure all of your widgets
  ${NSD_CreateText} 0 0 80u 12u $Text
  Pop $TextWidget
  ${NSD_OnChange} $TextWidget OnTextChange

  ${NSD_CreateCheckBox} 0 26u 80u 12u "Check this box"
  Pop $CheckWidget
  ${NSD_SetState} $CheckWidget $Check
  ${NSD_OnClick} $CheckWidget OnCheckClick

  nsDialogs::Show
FunctionEnd

; Create a callback function for each Widget
Function OnTextChange
  Pop $0 ; Widget handle is on stack
  ${NSD_GetText} $TextWidget $Text
FunctionEnd

Function OnCheckClick
  Pop $0 ; Widget handle is on stack
  ${NSD_GetState} $CheckWidget $Check
FunctionEnd

Some widgets, e.g. RadioButtons and CheckBoxes, use the OnClick function instead. Also the ComboBox does not work well with this method. However, a DropList, which does not seem to be documented, can usually replace it and works fine.

Radio buttons are also a little tricky because only the click callback for the selected button is called. I solved this by updating all of the radio button values in each radio button click callback.

Messy/tedious but it works.

like image 157
jcoffland Avatar answered Sep 21 '22 11:09

jcoffland