program temper
use dialogm
implicit none
include 'temp.fd'
type (dialog) dlg
logical return
return = dlginit(idd_temp,dlg)
call dodialog()
end program


subroutine dodialog()
use dialogm
implicit none
include 'temp.fd'
integer retint
logical retlog
type (dialog) dlg
external updatetemp
! 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_range)
  retlog = dlgset(dlg,idc_edit_celsius,"100")
! altera o título de Celsius para new celsius
  retlog=dlgset(dlg,idc_text_celsius,'new celsius',dlg_title)
  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)
! activate the dialog
  retint = dlgmodal(dlg)
! release dialog resources.
  call dlguninit(dlg)
  end if
end subroutine dodialog


subroutine updatetemp(dlg,control_name,callbacktype)
use dialogm
implicit none
type (dialog) dlg
integer control_name
integer callbacktype
include 'temp.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 scrollbar 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 scrollbar 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



