! Taken from the WHIZZY sample, and then enhanced with a 
! dialog initialization routine, this program demonstrates
! 1)That a console program can use a dialog box.
! 2)That some special customizations on the dialog
!    box may be done by using WIN32 APIs:
!    a) Change the font size in IDD_EDIT_CELSIUS
!    b) Change title of the dialog main window
!    c) Change the position of the dialog main window
!    d) Set a default selection in the combo box.
!    e) Bring the dialog window to the top
! 
!    Compile as a Console application:
!
!    rc /l 0x409 /d "NDEBUG" Celsius.rc                 
!
!    f90 CelsiCon.for Celsius.res
!
!(note: if one line in the initialization routine is changed
! as indicated by the comments, the program may also be
! compiled as a QuickWin application:
!
!    f90 /libs:qwin CelsiCon.for Celsius.res
! ) 
	
	PROGRAM CelsiCon
	USE DFLOGM
	IMPLICIT NONE
	INCLUDE 'RESOURCE.FD'
	CALL DoDialog( )												   
	write(*,*)' At end of program. Hit <return> to exit.'
	read(*,*)					   
*******************************************************************TO***
	END PROGRAM

	SUBROUTINE DoDialog( )  
	USE DFLOGM  
	IMPLICIT NONE  
	INCLUDE 'RESOURCE.FD'
	INTEGER retint  
	LOGICAL retlog  
	TYPE (dialog) dlg  
	EXTERNAL UpdateTemp, UpdateCombo, DoCallBackInit
! Initialize.  
	IF ( .not. DlgInit( idd_temp, dlg ) ) THEN
	WRITE (*,*) 'Error: dialog not found' 
	ELSE
! Set up temperature controls.
	retlog = DlgSet( dlg, IDC_SCROLLBAR_TEMPERATURE, 200,DLG_RANGEMAX)
	retlog = DlgSet( dlg, IDC_EDIT_CELSIUS, "100" )
	CALL UpdateTemp( dlg, IDC_EDIT_CELSIUS, DLG_CHANGE)
	retlog = DlgSetSub( dlg, IDC_EDIT_CELSIUS, UpdateTemp )
	retlog = DlgSetSub( dlg, IDC_EDIT_FAHRENHEIT, UpdateTemp )
	retlog = DlgSetSub( dlg, IDC_SCROLLBAR_TEMPERATURE, UpdateTemp )
       
    	retlog = DlgSetSub( dlg, IDC_COMBO1, UpdateCombo, dlg_selchange )
    	retlog = DlgSetSub( dlg, IDC_COMBO1, UpdateCombo, dlg_update )
        call UpdateCombo( dlg, IDC_COMBO1, dlg_update )


! set up Survey stuff
    	retlog = DlgSet( dlg, IDC_COMBO1, 3 )
    	retlog = DlgSet( dlg, IDC_COMBO1, "useless", 1 )
    	retlog = DlgSet( dlg, IDC_COMBO1, "painful", 2 )
    	retlog = DlgSet( dlg, IDC_COMBO1, "impacted", 3 )

! undocumented feature - set callback for WM_INITDIALOG
  	retlog = DlgSetSub( dlg, IDD_TEMP, DoCallBackInit )

! Activate the modal dialog.    
	retint = DlgModal( dlg )
! Release dialog resources.    
	CALL DlgUninit( dlg )  
	END IF
	END SUBROUTINE DoDialog

  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !! DoInit
  !!
  !! This routine is called just before the dialog box is  
  !! Displayed.
  !!  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
      subroutine DoCallBackInit( dlg, id, callbacktype )
!DEC$ ATTRIBUTES DEFAULT :: DoCallBackInit
      use dflib
	use dflogm
      use dfwin
      implicit none
	external UpdateCombo
      type (dialog) dlg
	integer id, HFONTDLG, HFONT,ires
      integer callbacktype
	integer x1, x2, y1, y2, hi, wi
	integer xfix, yfix
	
      include 'resource.fd'
     
      TYPE(T_LOGFONT) :: LFONT
      TYPE(T_RECT)    :: RECT
      TYPE(T_TEXTMETRIC) tm
      integer :: maxchars = 50
  	
      integer h,w  
      logical retlog, enabled
  
  
! Change the font size in IDD_EDIT_CELSIUS
      Call RtlZeroMemory(loc(lFont), sizeof(lFont))
      lFont%lfPitchAndFamily = (FIXED_PITCH,FF_MODERN)
      lFont%lfWeight = FW_bold
      lFont%lfFaceName=""C ! NULL
      lFont%lfHeight = 20
      HFONTDLG=CreateFontIndirect(lFont)
      HFONT=SendDlgItemMessage(dlg%hwnd,
     &             IDC_EDIT_CELSIUS,WM_SETFONT,HFONTDLG, 1)
  
! Change title of main window
      retlog = SetWindowText(dlg%hwnd, "New Title Text"C)

! Get size of the whole screen (or framewindow)
!   (Note: hard to get console window size and position).
!   Use the following for a Console Application
      h = GetDesktopWindow()
!   Use the following for a QuickWin Application
!      h = GethWndQQ (QWIN$FRAMEWINDOW) 
	retlog = GetWindowRect(h, RECT)
      x1 = rect.left  
      y1 = rect.top 
      x2 = rect.right 
      y2 = rect.bottom 
	hi = y2 - y1 + 1
	wi = x2 - x1 + 1 

! Change the position of the main dialog window
!  to about the center of the screen
      retlog = GetWindowRect(dlg%hwnd, RECT)
      xfix = (wi - (RECT.right-RECT.left+1))/2
	yfix = (hi - (RECT.bottom-RECT.top+1))/2       
      retlog = MoveWindow(dlg%hwnd, x1+xfix,
     &                   y1+yfix,
     &                   RECT.right-RECT.left+1,      
     &	               RECT.bottom-RECT.top+1,       
     &		          .TRUE.)

! Set the combo box text as the default selection
      retlog = SetDlgItemText(dlg%hwnd, IDC_COMBO1, "Default"C)
      call UpdateCombo( dlg, IDC_COMBO1, dlg_update )

! Bring the dialog window to the top
       ires = SetForegroundWindow(dlg%hwnd)  

      end 
 

	SUBROUTINE UpdateTemp( dlg, control_name, callbacktype )
!DEC$ ATTRIBUTES DEFAULT :: UpdateTemp
	USE DFLOGM
	IMPLICIT NONE
	TYPE (dialog) dlg
	INTEGER control_name
	INTEGER callbacktype
	INCLUDE 'RESOURCE.FD'
	CHARACTER(256) text
	INTEGER cel, far, retint
	LOGICAL retlog
! Suppress compiler warnings for unreferenced arguments.
	INTEGER local_callbacktype
	local_callbacktype = callbacktype

	SELECT CASE (control_name)
	CASE (IDC_EDIT_CELSIUS)
    ! Celsius value was modified by the user so
    ! update both Fahrenheit and Scroll bar values.
      retlog = DlgGet( dlg, IDC_EDIT_CELSIUS, text )
      READ (text, *, iostat=retint) cel
      IF ( retint .eq. 0 )THEN
        far = (cel-0.0)*((212.0-32.0)/100.0)+32.0
        WRITE (text,*) far
        retlog = DlgSet( dlg, IDC_EDIT_FAHRENHEIT,TRIM(ADJUSTL(text)) )
        retlog = DlgSet( dlg, IDC_SCROLLBAR_TEMPERATURE, cel,         
     &                    DLG_POSITION )
      END IF
	CASE (IDC_EDIT_FAHRENHEIT)
    ! Fahrenheit value was modified by the user so
    ! update both celsius and Scroll bar values.
       retlog = DlgGet( dlg, IDC_EDIT_FAHRENHEIT, text )
       READ (text, *, iostat=retint) far
       IF ( retint .eq. 0 )THEN
         cel = (far-32.0)*(100.0/(212.0-32.0))+0.0
         WRITE (text,*) cel
         retlog = DlgSet( dlg, IDC_EDIT_CELSIUS, TRIM(ADJUSTL(text)) )
         retlog = DlgSet( dlg, IDC_SCROLLBAR_TEMPERATURE, cel,        
     &                     DLG_POSITION )
       END IF
	CASE (IDC_SCROLLBAR_TEMPERATURE)
    ! Scroll bar value was modified by the user so
    ! update both Celsius and Fahrenheit values.
       retlog = DlgGet( dlg, IDC_SCROLLBAR_TEMPERATURE, cel,          
     &                   DLG_POSITION  )
       far = (cel-0.0)*((212.0-32.0)/100.0)+32.0
       WRITE (text,*) far
       retlog = DlgSet( dlg, IDC_EDIT_FAHRENHEIT,TRIM(ADJUSTL(text)) )
       WRITE (text,*) cel
       retlog = DlgSet( dlg, IDC_EDIT_CELSIUS,TRIM(ADJUSTL(text)) )
	END SELECT
	END SUBROUTINE UpdateTemp


    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! UpdateCombo
    !!
    !! This routine is called when the combobox selection is
    !! modified or when a new value is selected.  The static
    !! text field is then updated.
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
      subroutine UpdateCombo( dlg, id, callbacktype )
!DEC$ ATTRIBUTES DEFAULT :: UpdateCombo
      use dflogm
      implicit none
      type (dialog) dlg
      integer id
      integer callbacktype
    
    	character*256 temp
    	logical retlog
    
        include 'resource.fd'
    
        ! supress compiler warnings for unreferenced arguments
    	integer local_id, local_callbacktype
    	local_id = id
    	local_callbacktype = callbacktype
    
        retlog = DlgGet( dlg, IDC_COMBO1, temp )
    	temp = "Wisdom teeth are " // temp
        retlog = DlgSet( dlg, IDC_STATIC_WISDOM, temp )
    
      end subroutine UpdateCombo
    
