'====================================================================
' Nested modeless dialog demo, modeless dialog as main.
'====================================================================

$ filename "NestedModeless.exe"
'uses rtl32
'uses rtl64

'#define review

uses dialogs


sys g_hNestedDlg

function NestedDialogProc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
  select case uMsg

    case WM_COMMAND
      if loword(wParam) = IDCANCEL then
        DestroyWindow( hDlg )
      end if

    case WM_CLOSE
      DestroyWindow( hDlg )

      'Reset so main can determine if nested dialog is open.
      g_hNestedDlg = 0

  end select

  return 0
end function

'====================================================================

function MainDialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as sys callback
  select case uMsg

    case WM_COMMAND

      select case loword(wParam)
        case 100
          'Open nested dialog only if not already open.
          if IsWindow( g_hNestedDlg ) = 0 then
            Dialog( 0, 0, 120, 90, "Modeless Nested Dialog",
                         WS_OVERLAPPED or WS_SYSMENU or DS_CENTER or WS_VISIBLE )
            DefPushButton( "Close", IDCANCEL, -1, 60, 40, 12 )
            g_hNestedDlg = CreateModelessDialog( hDlg, @NestedDialogProc, 0 )
          end if

        case IDCANCEL
          DestroyWindow( hDlg )

      end select

    case WM_CLOSE
      DestroyWindow( hDlg )

    case WM_DESTROY
      PostQuitMessage( null )

  end select

  return 0
end function

'====================================================================

sys hDlg
MSG wMsg

Dialog( 0, 0, 150, 100, "Main Dialog",
        WS_OVERLAPPED or WS_SYSMENU or DS_CENTER or WS_VISIBLE )

DefPushButton(  "Go", 100, 30, 70, 40, 12 )
PushButton( "Close", IDCANCEL, 80, 70, 40, 12 )

hDlg = CreateModelessDialog( 0, @MainDialogProc, 0 )

while GetMessage( @wMsg, null, 0, 0 ) <> 0
  if IsDialogMessage( hDlg,  @wMsg ) = 0 then
    if IsDialogMessage( g_hNestedDlg,  @wMsg ) = 0 then
      TranslateMessage( @wMsg )
      DispatchMessage( @wMsg )
    end if
  end if
wend

'====================================================================