!  Win_Dialog.f90 
!
!  FUNCTIONS:
!   WinMain()      - Entry point for the application;
!                    displays the main window; processes the message loop
!   Win_DialogSub()  - Callback routine for the main dialog box
!   Win_DialogApply()- Callback routine for the APPLY button
!

!****************************************************************************
!
!  FUNCTION: WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
!
!  PURPOSE:  Entry point for the application
!
!  COMMENTS: Displays the main window and processes the message loop
!
!****************************************************************************

integer*4 function WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
!DEC$ IF DEFINED(_X86_)
!DEC$ ATTRIBUTES STDCALL, ALIAS : '_WinMain@16' :: WinMain
!DEC$ ELSE
!DEC$ ATTRIBUTES STDCALL, ALIAS : 'WinMain' :: WinMain
!DEC$ ENDIF

    use user32
    use kernel32
    use dflogm
    use Win_DialogGlobals
use icon_mod

    implicit none

    integer*4 hInstance
    integer*4 hPrevInstance
    integer*4 lpszCmdLine
    integer*4 nCmdShow

    include 'resource.fd'

    external Win_DialogSub
    external Win_DialogApply

    ! Variables
    type (T_MSG)            mesg
    integer*4               ret
    logical*4               lret


    ghInstance = hInstance
    ghModule = GetModuleHandle(NULL)
    ghwndMain = NULL

!hIcon%Icon1 =LoadImage(ghInstance, IDI_ICON1, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)
hIcon%Icon1 =LoadImage(ghInstance, "Win_Dialog"C, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)

hIcon%Notepad =LoadImage(ghInstance, "Notepad.ico"C, IMAGE_ICON, &
32, 32, LR_LOADFROMFILE)

    lret = DlgInit(IDD_WIN_DIALOG_DIALOG, gdlg)
    if (lret == .FALSE.) goto 99999
    lret = DlgSetSub(gdlg, IDD_WIN_DIALOG_DIALOG, Win_DialogSub)
    lret = DlgSetSub(gdlg, IDM_APPLY, Win_DialogApply)

lret = DlgSetSub(gdlg, IDC_Button_Notepad, Win_DialogApply)

    lret = DlgModeless(gdlg, nCmdShow)
    if (lret == .FALSE.) goto 99999


    ! Read and process messsages
    do while( GetMessage (mesg, NULL, 0, 0) ) 
       if ( DlgIsDlgMessage(mesg) .EQV. .FALSE. ) then
           lret = TranslateMessage( mesg )
           ret  = DispatchMessage( mesg )
       end if
    end do
    call DlgUninit(gdlg)

    WinMain = mesg.wParam
    return

99999 &

    ret = MessageBox(ghwndMain, "Error initializing application Win_Dialog"C, &
                     "Error"C, MB_OK)
    WinMain = 0

end 

!****************************************************************************
!
!  FUNCTION: Win_DialogSub ( dlg, id, callbacktype )
!
!  PURPOSE:  Dialog box callback for initialization and destroy
!
!  COMMENTS:
!
!****************************************************************************

SUBROUTINE Win_DialogSub( dlg, id, callbacktype )
!DEC$ ATTRIBUTES DEFAULT :: Win_DialogSub

  use user32
  use dflogm
use icon_mod

  implicit none

  type (dialog) dlg
  integer id, callbacktype

integer wparam, lparam
integer iret

    include 'resource.fd'


if (callbacktype == dlg_init) then

wparam =ICON_SMALL
lparam =hIcon%Icon1
iret =SendMessage(dlg%hWnd, WM_SETICON, wparam, lparam)

wParam =image_icon
lParam =hIcon%Notepad
iret =SendDlgItemMessage(dlg%hwnd, idc_Button_Notepad, BM_SETIMAGE, wParam, lParam)

else if (callbacktype == dlg_destroy) then
    call PostQuitMessage(0)
  endif

  END SUBROUTINE Win_DialogSub

!****************************************************************************
!
!  FUNCTION: Win_DialogApply ( dlg, id, callbacktype )
!
!  PURPOSE:  Dialog box callback for APPLY button
!
!  COMMENTS:
!
!****************************************************************************

SUBROUTINE Win_DialogApply( dlg, id, callbacktype )
!DEC$ ATTRIBUTES DEFAULT :: Win_DialogApply

  use dflogm
use dflib

  implicit none

  type (dialog) dlg
  integer id, callbacktype

include 'resource.fd'
integer ir

!  if (callbacktype == dlg_clicked) then

SELECT CASE(id)

CASE(IDM_APPLY)
    ! TO DO; Add your APPLY logic here

CASE(idc_Button_Notepad)
  
ir =runqq('Notepad.exe', ' ')

END SELECT

!  endif

  END SUBROUTINE Win_DialogApply


