program main
use appgraphics
use children, only: child_ids, child_create_time, redraw_children
implicit none

    integer::myscreen
    integer::create_child_button
    integer::sc_ticks
    integer::sc_tick_rate
    real::time_now
    
    myscreen = initwindow(200, 130, closeflag=.TRUE.)
    
    ! Add a button for creating children
    create_child_button = createbutton(60, 80, 80, 40, "Create Child", &
                                       handle_create_child)
    
    ! Initialize the child index arrays
    child_ids = -1
    child_create_time = 0.0

    ! Label this as the main window    
    call settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 60)
    call outtextxy(15, 5, "Master")
    
    call system_clock(count_rate=sc_tick_rate)
    
    do while(.true.)
    
        ! Calculate the system time now in seconds
        call system_clock(count=sc_ticks)
        time_now = real(sc_ticks)/real(sc_tick_rate)

        ! Redraw all children and provide them with the system time
        call redraw_children(time_now)

        ! Set the master to the current window and idle 1 second
        call setcurrentwindow(myscreen)
        call startidle(1000)
                
    end do
    
    call closewindow(myscreen)
    
    contains
    
    subroutine handle_create_child()
    use children, only: create_child
    implicit none
    
        call create_child()
    
    end subroutine handle_create_child
    
end program main
