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%nbuttons /global
  define integer buttons%clicked /global
  let buttons%clicked -1
  let buttons%nbuttons 0
  gtv search <greg<buttons
  if (.not.gtv%exist) create directory <greg<buttons
  clear directory <greg<buttons
  @ window_tools
  @ button-prepare
end procedure button-reset
!
begin procedure button-init
   change directory <greg<buttons
   @button-prepare
   define integer nb color
   define real pos[4]
   define char*20 text bname
   for iinp 1 to 4
      let pos[iinp] 'pro%arg[iinp]'
   next
   let bname &5   
   let text &6
   if (pro%narg.eq.7) then
      let color &7
   else
      let color 0
   end if
   ! 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
   pen /c 'color' /w 3
   g\draw relocate 'pos[1]' 'pos[2]' /u
   g\draw line 'pos[1]+pos[3]' 'pos[2]' /u
   g\draw line 'pos[1]+pos[3]' 'pos[2]+pos[4]' /u
   g\draw line 'pos[1]' 'pos[2]+pos[4]' /u
   g\draw line 'pos[1]' 'pos[2]' /u
   g\draw text 'pos[1]+pos[3]/2.0' 'pos[2]+pos[4]/2.0' 'text' 5 0 /u
   pen /def
   change directory <greg
end procedure button-init
!
begin procedure button-click
   define logical getclick
   let getclick yes
   if (pro%narg.eq.1) then
      let getclick &1
   endif
   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
      return
   end if
   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
         return
      endif
   next
   change directory <greg   
end procedure button-click
!
begin procedure button-delete
   if (exist(buttons)) then
      delete /var buttons
   end if
   destroy directory <GREG<BUTTONS
end procedure button-delete
!
begin procedure button-prepare
   @ window_xy 1 1 1
   g\limits 0 10 0 10
end procedure button-prepare
!
