Search code examples
windows-installernsis

Disable next button until nsDialogs filled


I am trying to create a datasource file in installation folder. I am using nsDialogs. Until the username and password are entered, Install/Next button shouldn't be enabled.

!include "MUI2.nsh"

Name database
OutFile database.exe

InstallDir $DESKTOP

!insertmacro MUI_PAGE_COMPONENTS
Page custom pgPageCreate pgPageLeave
!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Var Dialog
Var TextUser
Var TextPass

Section /o "Data Source" datasource
    CreateDirectory "$INSTDIR\datasource"
    FileOpen $3 "$INSTDIR\datasource\datasource.properties" w
    FileWrite $3 username=$1$\n
    FileWrite $3 password=$2$\n
    FileClose $3
SectionEnd

Function pgPageCreate
    ${IfNot} ${SectionIsSelected} ${datasource}
        Abort
    ${EndIf}

    !insertmacro MUI_HEADER_TEXT "Database Settings" "Provide MySQL config details."

    nsDialogs::Create 1018
    Pop $Dialog

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

    ${NSD_CreateLabel} 20% 26u 20% 10u "Username:"
    Pop $0

    ${NSD_CreateText} 40% 24u 40% 12u ""
    Pop $TextUser

    ${NSD_CreateLabel} 20% 62u 20% 10u "Password:"
    Pop $0

    ${NSD_CreatePassword} 40% 60u 40% 12u ""
    Pop $TextPass

    nsDialogs::Show
FunctionEnd

Function PgPageLeave
    ${NSD_GetText} $TextUser $1
    ${NSD_GetText} $TextPass $2
FunctionEnd

enter image description here

How to disable this Install button. I couldn't find any solution. Thanks in advance.


Solution

  • Use the NSD_OnChange callback:

    !include LogicLib.nsh
    !include nsDialogs.nsh
    #!include MUI2.nsh
    
    Page Custom pgPageCreate pgPageLeave
    Page InstFiles
    
    Var hTextUser
    Var hTextPass
    Var User
    Var Pass
    
    Function OnLoginInfoChanged
    Pop $0 ; Throw away handle we don't need
    ${NSD_GetText} $hTextUser $1
    ${NSD_GetText} $hTextPass $2
    GetDlgItem $0 $hWndParent 1 ; Get button handle
    ${If} "$1" == ""
    ${OrIf} "$2" == ""
        EnableWindow $0 0
    ${Else}
        EnableWindow $0 1
    ${EndIf}
    FunctionEnd
    
    Function pgPageCreate
    nsDialogs::Create 1018
    Pop $0
    
    ${NSD_CreateText} 40% 24u 40% 12u "$User"
    Pop $hTextUser
    ${NSD_OnChange} $hTextUser OnLoginInfoChanged
    
    ${NSD_CreatePassword} 40% 60u 40% 12u "$Pass"
    Pop $hTextPass
    ${NSD_OnChange} $hTextPass OnLoginInfoChanged
    
    Push ""
    Call OnLoginInfoChanged ; Simulate change event to initialize the button
    nsDialogs::Show
    FunctionEnd
    
    Function PgPageLeave
    ${NSD_GetText} $hTextUser $User
    ${NSD_GetText} $hTextPass $Pass
    FunctionEnd
    
    Section
    DetailPrint $User:$Pass
    SectionEnd