'====================================================================
' Create a Main Window in Winmain() and call Modeless Dialog
'====================================================================

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

'% review

uses dialogs

sys hInstance = GetModuleHandle(0)

% ID_BUTTON1=100
% ID_BUTTON2=101

string g_szClassName = "WinDemo"

sys hButton1, hButton2
sys g_hDialog

function DialogDlgProc(sys hwnd, Message, wParam, lParam) as bool callback

   select Message   
        case WM_COMMAND

            select loword(wParam)        
                case IDOK
                    MessageBox(hwnd, "Oh yeah!", "This feels good!", 
                        MB_OK or MB_ICONASTERISK)
                
                case IDCANCEL
                    MessageBox(hwnd, "Outch!", "This hurts!", 
                        MB_OK or MB_ICONEXCLAMATION)                
            end select
        
        case else
            return FALSE

    end select

    return TRUE
end function    


function WndProc(sys hwnd, Message, wParam, lParam) as sys callback

    select Message    
        case WM_COMMAND
            select loword(wParam)
                            
                case ID_BUTTON1                
                    ShowWindow(g_hDialog, SW_SHOW)
                
                case ID_BUTTON2                
                    ShowWindow(g_hDialog, SW_HIDE)               
            end select
        
        case WM_CLOSE
            DestroyWindow(hwnd)
        
        case WM_DESTROY
            DestroyWindow(g_hDialog)
            PostQuitMessage(0)
        
        case else
            return DefWindowProc(hwnd, Message, wParam, lParam)
    
    end select
    
    return 0
end function


function WinMain(sys nCmdShow) as sys

    WNDCLASSEX wc
    MSG Msg

    sys hwnd

    wc.cbSize        = sizeof(WNDCLASSEX)
    wc.style         = 0
    wc.lpfnWndProc   = @WndProc
    wc.cbClsExtra    = 0
    wc.cbWndExtra    = 0
    wc.hInstance     = hInstance
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION)
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW)
    wc.hbrBackground = COLOR_WINDOW+1
    wc.lpszMenuName  = NULL
    wc.lpszClassName = strptr(g_szClassName)
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION)

    if not RegisterClassEx(&wc) then
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION or MB_OK)
        return 0
    end if

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Call dynamic Dialog",
        WS_OVERLAPPEDWINDOW,
        300, 300, 450, 140,
        NULL, 
        NULL, 
        hInstance, NULL)

    if hwnd = NULL then
        MessageBox(NULL, "Window Creation Failed!", "Error!",
                   MB_ICONEXCLAMATION or MB_OK)
        return 0
    end if        

    hButton1 = CreateWindowEx (0,"button", "Show Dialog",
                               WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
                               40, 20, 100, 24, hwnd, ID_BUTTON1,
                               hInstance, NULL)
    hButton2 = CreateWindowEx (0,"button", "Hide Dialog",
                               WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
                               40, 50, 100, 24, hwnd, ID_BUTTON2,
                               hInstance, NULL)

    Dialog( 100, 0, 98, 52, "Simple Dialog",  WS_CHILD)
    PushButton( "Press this button", IDOK, 7, 7, 84, 14 )
    PushButton( "Don't press this button", IDCANCEL, 7,31, 84, 14 )

    g_hDialog = CreateModelessDialog( hwnd, @DialogDlgProc, 0)
    if g_hDialog != null  then          
      ShowWindow(g_hDialog, SW_SHOW)            
    else            
      MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",    
                 MB_OK or MB_ICONINFORMATION)
    end if
                          
    ShowWindow(hwnd, nCmdShow)
    UpdateWindow(hwnd)

    sys bRet

    do while bRet := GetMessage(&Msg, NULL, 0, 0)
      if bRet = -1 then
        'show an error message
        print "Error in Message Loop"
        end        
      else
          if not IsDialogMessage(g_hDialog, &Msg) then    
            TranslateMessage(&Msg)
            DispatchMessage(&Msg)
        end if
      end if        
    wend
    
    return Msg.wParam
end function

'WINDOWS START
'=============

WinMain(SW_NORMAL)