!
! Tools to Create buttons in a GreG sub-directory
!
begin procedure button-reset
  if (exist(buttons)) then
     delete /var buttons
  end if
  define structure buttons /global
  define structure buttons%ids /global
  define integer buttons%ids%abort buttons%ids%help /global
  let buttons%ids%abort -1
  let buttons%ids%help -2
  define integer buttons%nbuttons /global
  define integer buttons%clicked /global
  let buttons%clicked -1
  let buttons%nbuttons 0
  gtvl\gtv search <greg<buttons
  if (.not.gtv%exist) create directory <greg<buttons
  gtvl\clear directory <greg<buttons
  @ window_tools
  @ button-prepare
end procedure button-reset
!
begin procedure button-init
  !
  ! Init a New Button
  !  @ button-init Xblc Xsize Yblc Ysize Name Text [Color]
  !
  gtvl\change directory <greg<buttons
  @ button-prepare
  define integer nb color
  define real pos[4]
  define char*20 text bname
  !
  ! Define parameters
  let pos &1 &2 &3 &4
  let bname &5
  let text &6
  let color &7+0
  ! Include it in mother structure
  let buttons%nbuttons buttons%nbuttons+1
  let nb buttons%nbuttons
  define structure buttons%button'nb' /global
  define real buttons%button'nb'%pos[4] /global
  define char*20 buttons%button'nb'%text /global
  define integer buttons%ids%'bname' /global
  let buttons%ids%'bname' 'nb'
  let buttons%button'nb'%pos pos
  ! Draw it
  greg1\pencil /c 'color' /w 3
  greg1\draw relocate 'pos[1]' 'pos[2]' /u
  greg1\draw line 'pos[1]+pos[3]' 'pos[2]' /u
  greg1\draw line 'pos[1]+pos[3]' 'pos[2]+pos[4]' /u
  greg1\draw line 'pos[1]' 'pos[2]+pos[4]' /u
  greg1\draw line 'pos[1]' 'pos[2]' /u
  greg1\draw text 'pos[1]+pos[3]/2.0' 'pos[2]+pos[4]/2.0' 'text' 5 0 /u
  greg1\pencil /def
  gtvl\change directory <greg
end procedure button-init
!
begin procedure button-click
  !
  ! @ button-click [Yes|No]
  !   Yes (default) : ask for cursor
  !   No            : use current cursor coordinates
  define logical getclick
  let getclick yes
  if (pro%narg.eq.1) then
    let getclick &1
  endif
  gtvl\change directory <greg<buttons
  @ button-prepare
  define real click[2] pos[4]
  define logical inside i2
  if (getclick) g\draw relo
  let click use_curs
  if (cursor_code.eq."E") then
    let buttons%clicked 0
    gtvl\change directory <greg
    return
  else if (cursor_code.eq."H") then
    let buttons%clicked buttons%ids%help
    gtvl\change directory <greg
    return
  end if
  !
  ! Scan in which button there was a Click
  for ib 1 to buttons%nbuttons
    let pos buttons%button'ib'%pos
    let inside click[1].ge.pos[1]
    let inside inside.and.click[1].le.(pos[1]+pos[3])
    let inside inside.and.click[2].ge.pos[2]
    let inside inside.and.click[2].le.(pos[2]+pos[4])
    if (inside) then
      let buttons%clicked ib
      gtvl\change directory <greg
      return
    endif
  next
  gtvl\change directory <greg
end procedure button-click
!
begin procedure button-delete
  if (exist(buttons)) then
    delete /var buttons
  end if
  gtvl\destroy directory <GREG<BUTTONS
end procedure button-delete
!
begin procedure button-prepare
  @ window_xy 1 1 1
  greg1\limits 0 10 0 10
end procedure button-prepare
!
