Archive: Need Help on Buttons in custom pages


Need Help on Buttons in custom pages
Hi all,

I am as new as you can get to NSIS, so excuse my ignorance.

I have created a custom page with a button. I want to call a function when that button is clicked. The function, in turn, will run some commands based on selections made on the custom page.

What works:
1. got a page with 2 radio buttons, text field and Add button
2. i read the values of the fields in my custom page function

Need help:
1. how to call a function from my button, yet remain on the same page
2. the function being called needs to use values read in the custom page function

Is this possible? and if yes, how would I do it?

Any help is GREATLY appreciated.


Use Abort to stay on the current page.

You can store values in variables or on the stack.


Thanks for a quick response....

But how do I call a function from my button?

I have searched the forum and the docs, all I could find was that I can put a command that can be executed with Exec in the state of the button, and clicking the button will Exec that code.

What I need is to click the button and call a method in my script.

Thanks again.


See the InstallOptions documentation and examples about the NOTIFY flag.


Let me check that out....


Great !!!! Thanks, it works, BUT I am encountering another issue: can't get out of the window even if I click close. Here is my code:

Page custom AddUserPage test
.
.
Function AddUserPage ;FunctionName defined with Page command
!insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "sshUser.ini"

!insertmacro MUI_INSTALLOPTIONS_READ $domain_radio "sshUser.ini" "Field 2" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $local_radio "sshUser.ini" "Field 3" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $user_text "sshUser.ini" "Field 4" "State"

FunctionEnd


Function test
MessageBox MB_OK "add clicked"
Abort
FunctionEnd

I know there has to be a way to close this, my guess would be an error/completion code in the stack?!?! But how would I get it, what do I compare it too? Just need close button to close without calling test again.

Can't thank enough the people responding...


You should read the status from the INI file, see docs.


I appologize if it sounds like I don't want to do searching myself, its definetly not the case. I just can not find it in docs or the forum post related to my question.

Dear Joost, after reading the forum for about 5 continous hours I see that your contribution to both the forum and development is quite large.

I would greatly appreciate a pointer to an example or maybe the section in the docs (a link) to solve my problem

Again, thanks for taking the time to answer my questions


If you put only the Abort command inside a leave_function, you won't ever close the program by normal ways. You have to give it some conditions to don't execute the Abort command. You can use StrCmp for comparing strings.

Ok, this is the first step, now you have to have a string to compare with. This can be:

!insertmacro MUI_INSTALLOPTIONS_READ $0 "sshUser.ini" "Settings" "State"

Where the result will be a control number if you selected one, or an empty string if not.

The result code will be as this below:

Function test

!insertmacro MUI_INSTALLOPTIONS_READ $0 "sshUser.ini" "Settings" "State"
StrCmp $0 2 +3
StrCmp $0 3 +2
StrCmp $0 4 0 +2
Abort

FunctionEnd


Try it and say if it worked.

Hi all,

1. Thanks a whole lot for the response. REALLY REALLY appreciate it.
2. What you are suggesting is very clear to me. Here is my problem:
I have 3 buttons on the screen, Cancel, Close, Add User (custom page button). Cancel works fine, does what is expected. Add User also behaves normally. Close calls the same method as Add User (I am using notify flag), but in the method I would like to be able to know that the method was called as a result of Close button being clicked not Add User. Is there a way to do that?

So I still have a problem.....

Thanks for your responses.


Instead of using !insertmacro MUI_INSTALLOPTIONS_DISPLAY you can use !insertmacro MUI_INSTALLOPTIONS_DISPLAY_RETURN and pop the return value. The value can be one of these:

success - The user has pressed the Next button

back - The user has pressed the Back button

cancel - The user has pressed the Cancel button

error - An error has occurred, the dialog cannot be displayed.


Thanks for the info, I have tried it with interesting results...still does not work.

How can I attach my file? I appologize in advance for pasting the code...

Please take a look, maybe I am doing something exceptionally stupid.


# NOTE: this .NSI script is designed for NSIS v1.8+
!define ALL_USERS
!include MUI.nsh

Name "SSH Users"
OutFile "configureUsers.exe"


InstallDir "C:\CP_WORK\TEST_AREA\test"

LangString TEXT_IO_TITLE ${LANG_ENGLISH} "SSH User configuration"
LangString TEXT_IO_SUBTITLE ${LANG_ENGLISH} "Configure users"

BrandingText /TRIMRIGHT "SSH User Configurator"

Page custom AddUserPage addUser

# declare variables
Var domain_radio
Var local_radio
Var user_text
var path_to_ssh
var clean_path

# begin install settings/section
Section "install" # (default section)
SetOutPath "$INSTDIR"

# write out uninstaller
WriteUninstaller "$INSTDIR\uninst.exe"
SectionEnd ; end of default section


# begin uninstall settings/section
UninstallText "This will uninstall SiteAlive from your system"

Section Uninstall
#Set the context to all users
SetShellVarContext all

SectionEnd # end of uninstall section

# FUNCTIONS
Function .onInit
Call findOpenSSHInstallDir
!insertmacro MUI_INSTALLOPTIONS_EXTRACT "sshUser.ini"
FunctionEnd

Function AddUserPage ;FunctionName defined with Page command

!insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
!insertmacro MUI_INSTALLOPTIONS_DISPLAY_RETURN "sshUser.ini"

FunctionEnd
.
.
.
Function addUser ;FunctionName defined with Page command
Pop $R1
StrCmp $R1 cancel c1
StrCmp $R1 success c2
goto process

c1:
MessageBox MB_OK "Canceled"
goto done

c2:
MessageBox MB_OK "Closed"
goto done

process:
!insertmacro MUI_INSTALLOPTIONS_READ $domain_radio "sshUser.ini" "Field 2" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $local_radio "sshUser.ini" "Field 3" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $user_text "sshUser.ini" "Field 4" "State"

StrCmp $domain_radio "1" domainuser localuser

domainuser:
StrLen $4 $user_text
StrCmp $4 "0" samePage
Exec '"$clean_path\bin\configDomainUser.bat" $path_to_ssh $user_text'
MessageBox MB_OK "User Added $clean_path\bin\configDomainUser.bat $\r$\n $path_to_ssh $\r$\n $user_text"
goto samePage

localuser:
StrLen $4 $user_text
StrCmp $4 "0" samePage
Exec '"$clean_path\bin\configLocalUser.bat" $path_to_ssh $user_text'
MessageBox MB_OK "User Added $clean_path\bin\configLocalUser.bat $\r$\n $path_to_ssh $\r$\n $user_text"
goto samePage

samePage:
Abort

done:
FunctionEnd

# eof


Ahh, so "Close" button is the "Next" button...

Replace (Field number of "Add User" button) as it says.

c2:
MessageBox MB_OK "Closed"
goto done

!insertmacro MUI_INSTALLOPTIONS_READ $0 "sshUser.ini" "Settings" "State"

StrCmp $0 '(Field number of "Add User" button)' 0 EndAddUser

MessageBox MB_OK '"Add User" button executed!'

EndAddUser:

process:

TADA!!!!

Cannot thank you enough!!!!

Worked like a charm. Thanks again deguix and everybody.