!  LessonA37.f90 
!
!  FUNCTIONS:
!   WinMain()      - Entry point for the application;
!                    displays the main window; processes the message loop
!   LessonA37Sub()  - Callback routine for the main dialog box
!   LessonA37Apply()- 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 LessonA37Globals

    implicit none

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

    include 'resource.fd'

    external LessonA37Sub
    external LessonA37Apply

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


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

    lret = DlgInit(IDD_LESSONA37_DIALOG, gdlg)
    if (lret == .FALSE.) goto 99999
    lret = DlgSetSub(gdlg, IDD_LESSONA37_DIALOG, LessonA37Sub)
    lret = DlgSetSub(gdlg, IDM_APPLY, LessonA37Apply)
    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 LessonA37"C, &
                     "Error"C, MB_OK)
    WinMain = 0

end 

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

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

  use user32
  use dflogm

  implicit none

  include 'resource.fd'
    interface
	subroutine CreateChart()
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?CreateChart@Classless@@YAXXZ'::CreateChart
	end subroutine CreateChart
	end interface

	interface
	subroutine DestroyChart()
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?DestroyChart@Classless@@YAXXZ'::DestroyChart
	end subroutine DestroyChart
	end interface

	interface
	integer function AttachDlg(hDlg, nCtrlID, nType)
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?AttachDlg@Classless@@YAHPAUHWND__@@IH@Z'::AttachDlg
		!DEC$ATTRIBUTES VALUE::hDlg, nCtrlID, nType
		integer hDlg, nCtrlID, nType
	end function AttachDlg
	end interface

	interface
	integer function AddStems(pData, nLen, title)
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?AddStems@Classless@@YAHPANHPAD@Z'::AddStems
		!DEC$ATTRIBUTES VALUE::pData, nLen, title
		integer pData, nLen, title
	end function AddStems
	end interface

	interface
	subroutine SetStemLabel(label, which)
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetStemLabel@Classless@@YAXPBDH@Z'::SetStemLabel
		!DEC$ATTRIBUTES VALUE::label, which
		integer label, which
	end subroutine SetStemLabel
	end interface

	interface
	subroutine SetTitle(title)
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetTitle@Classless@@YAXPBD@Z'::SetTitle
		!DEC$ATTRIBUTES VALUE::title
		integer title
	end subroutine SetTitle
	end interface

	interface
	subroutine SetLegendShow(bShow, nPlotIndex)
		!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetLegendShow@Classless@@YAX_NH@Z'::SetLegendShow
		!DEC$ATTRIBUTES VALUE::bShow, nPlotIndex
		integer*1 bShow
		integer nPlotIndex
	end subroutine SetLegendShow
	end interface

  type (dialog) dlg
  integer id, callbacktype

  double precision pData(5)

  if (callbacktype == dlg_init) then
    call CreateChart()
	call AttachDlg(dlg%hwnd, IDC_CANVAS, 2)
	pData(1) = 7
	pData(2) = 6
	pData(3) = 1
	pData(4) = 2
	pData(5) = 4
	call AddStems(LOC(pData), 5, LOC("序列1"C))
	pData(1) = 3
	pData(2) = 7
	pData(3) = 5
	pData(4) = 4
	pData(5) = 2
	call AddStems(LOC(pData), 5, LOC("序列2"C))

	call SetStemLabel(LOC("张"C), 0)
	call SetStemLabel(LOC("王"C), 1)
	call SetStemLabel(LOC("李"C), 2)
	call SetStemLabel(LOC("赵"C), 3)
	call SetStemLabel(LOC("陈"C), 4)

	call SetLegendShow(1, 0)
	call SetTitle(LOC("Fortran对话框标题"))
  endif

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

  END SUBROUTINE LessonA37Sub

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

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

  use dflogm

  implicit none

  type (dialog) dlg
  integer id, callbacktype

  if (callbacktype == dlg_clicked) then
    ! TO DO; Add your APPLY logic here
  endif

  END SUBROUTINE LessonA37Apply


