program convert
use appgraphics
implicit none

    integer::myscreen
    integer::original, converted
    integer, dimension(4)::src, dest
    integer::ignore
    
    character(16), dimension(4), parameter:: units =(/ 'Fahrenheit',  &
                                                       'Celsius   ',  &
                                                       'Kelvin    ',  &
                                                       'Rankine   ' /)
    
    myscreen = initwindow(150, 255, title="TempConvert", closeflag=.TRUE.)
    call setcolor(BLACK)
    call setbkcolor(systemcolor(COLOR_WINDOW_BKGD))
    call settextstyle(WINDOWS_FONT, HORIZ_DIR, 10)
    call clearviewport()
    
    original = createtextbox(5, 5, 140, 20)
    call settextboxcontents(original, "32.0")
    
    call outtextxy(5, 30, "Original:")
    call beginradiogroup()
    src(1) = createradiobutton(50, 30, 95, 20, units(1))
    src(2) = createradiobutton(50, 50, 95, 20, units(2))
    src(3) = createradiobutton(50, 70, 95, 20, units(3))
    src(4) = createradiobutton(50, 90, 95, 20, units(4))
    call radiobuttonsetchecked(src(1), logical(.TRUE., 1))
    
    call line(5, 112, 140, 112)
    
    call outtextxy(5, 115, "Desired:")
    call beginradiogroup()
    dest(1) = createradiobutton(50, 115, 95, 20, units(1))
    dest(2) = createradiobutton(50, 135, 95, 20, units(2))
    dest(3) = createradiobutton(50, 155, 95, 20, units(3))
    dest(4) = createradiobutton(50, 175, 95, 20, units(4))
    call radiobuttonsetchecked(dest(2), logical(.TRUE., 1))
    
    converted = createtextbox(5, 200, 140, 20)
    call settextboxcontents(converted, "0.0")
    
    ignore = createbutton(40, 230, 50, 20, "Convert", convert_temp)
    ignore = createbutton(95, 230, 50, 20, "Close", quit)
    
    call loop()
    
    call closewindow(myscreen)

contains

    subroutine quit()
    implicit none
        
        call stopidle()
        
    end subroutine quit
    
    subroutine convert_temp()
    implicit none
    
    character(40)::source_text, dest_text
    real::source_number, dest_number
    integer::i
    
        i = gettextboxcontents(original, source_text)
        Read(source_text, *) source_number
        
        ! Check if we have kelvin or rankine and correct to
        ! celsius or fahrenheit
        if(radiobuttonischecked(src(3))) then
            source_number = source_number - 273.15
        elseif(radiobuttonischecked(src(4))) then
            source_number = source_number - 460.67
        end if
        
        dest_number = source_number
        
        ! fahrenheit conversion
        if(radiobuttonischecked(src(1)) .OR. &
           radiobuttonischecked(src(4))) then
               
            if(radiobuttonischecked(dest(2)) .OR. &
               radiobuttonischecked(dest(3))) then
                   
                dest_number = farenheit_to_celsius(source_number)
                
                if(radiobuttonischecked(dest(3))) then
                    dest_number = dest_number + 273.15
                end if
            elseif(radiobuttonischecked(dest(4))) then
                dest_number = dest_number + 460.67
            end if
            
        elseif(radiobuttonischecked(src(2)) .OR. &
               radiobuttonischecked(src(3))) then
        
            if(radiobuttonischecked(dest(1)) .OR. &
               radiobuttonischecked(dest(4))) then
                   
                dest_number = celsius_to_farenheit(source_number)
                
                if(radiobuttonischecked(dest(4))) then
                    dest_number = dest_number + 460.67
                end if
            elseif(radiobuttonischecked(dest(3))) then
                dest_number = dest_number + 273.15
            end if
        end if
    
        Write(dest_text, '(F10.2)') dest_number
        call settextboxcontents(converted, trim(adjustl(dest_text)))
    
    end subroutine convert_temp
    
    function celsius_to_farenheit(c)
    implicit none
    
        real, intent(in)::c
        real::celsius_to_farenheit
        
        celsius_to_farenheit = 9.0*c/5.0 + 32.0
    
    end function celsius_to_farenheit
    
    function farenheit_to_celsius(f)
    implicit none
    
        real, intent(in)::f
        real::farenheit_to_celsius
        
        farenheit_to_celsius = 5.0*(f-32.0)/9.0
    
    end function farenheit_to_celsius

end program convert
