Program gui_read
  Use gui
  Implicit None
  Integer, Parameter :: n_buttons = 4;
  Type (guibutton), Dimension(n_buttons) :: buttons
  Integer :: which_button  ! a helper function at end
  Integer :: i,ans   ! counter, temp. var
  Real :: cx, cy  ! cursor position
  Character :: ch ! which button A = left, D = middle, X = right
  Integer, External :: PGOPEN, PGCURS
  Character (len=20):: input = ' '
! Initialize PGPLOT
  If (Pgopen('?') <= 0) Stop
  CALL PGPAP(10.0,0.5)  ! width = 10, aspect = 0.5 (twice as wide)
  CALL PGPAGE   ! start new page
  CALL PGSWIN(0.0,2.0,0.0,1.0) ! aspect ratio 0.5 so x = 2 
  CALL PGSCR(0, 0.4, 0.4, 0.4) ! CI,  CR, CG, CB
     
! set line width pgslw and character size pgsch also from user.
  call pgslw(2)
  call pgsch(1.3)
! Get buttons data from cfg file.
  call init_buttons(buttons, n_buttons, 'guiread.cfg')
  do i = 1, n_buttons
    call draw_button(buttons(i))
  end do

  cx = 0.3 ; cy = 0.3  ! initial cursor position
! process each user click until Exit
  do
     ans = PGCURS(cx, cy, ch) ! wait for user to click
     i = which_button(buttons, n_buttons, cx, cy)
     if (i == 0) Cycle
     Select Case (trim(buttons(i)%name))
        Case ('Input 1','Input 2','Input 3')
           Call ReadString(buttons(i))
           Write (*,*) 'Read the string: ', buttons(i)%value, ' from Button ', i
           Cycle
        Case ('Exit')
           Exit
        Case Default
           Cycle
        End Select
    Exit    
   End do
End Program gui_read

Integer Function which_button(buttons, n_buttons, x, y)
  ! returns which button contains x,y.  0 if none. i.e. outside
  Use gui
  Implicit None
  Integer, Intent(IN) :: n_buttons
  Type (guibutton), Dimension(n_buttons), Intent(IN) :: buttons
  Real, Intent (IN) :: x, y
 
  Integer :: i, ans
  ans = 0
  Do i = 1, n_buttons
     If (within_button(buttons(i),x,y)) Then
        ans = i
        Exit
     End If
  End Do
  which_button = ans
End Function which_button

Subroutine ReadString(button)
  Use Gui
  Implicit None
  Type (guibutton), Intent(INOUT) :: button !value will be set
  Integer :: PGCURS
  !local vars
  Integer :: tmp
  Character (Len=1) :: ch
  Real :: cx, cy
  Logical :: done
  done = .false.  !new input to be read
  cx = button%blx + 0.5 * button%width !start at centre
  cy = button%bly + 0.5 * button%height
  do    ! read chars one by one
    tmp = PGCURS(cx,cy,ch) !read one character
    tmp = ICHAR(CH)        ! get its integer value- for select
    Select Case (tmp)
       Case (8,127)
    !! -- Backspace (ctrl H) or delete removes last character
              If (len(button%value) > 0) Then
         call update_button_value(button, &
                  button%value(1:len(trim(button%value))-1))
              End If
       Case (21)
          !!  -- Ctrl U removes entire string
         call  update_button_value(button, '')
       Case (:7,9:20,22:32)  ! non-Ascii so terminate
          done = .true.
       Case Default  !add the ascii char if possible
         call  update_button_value(button, trim(button%value) // ch)
       End Select
    if (done) EXIT
  end do
End Subroutine readstring
