#!/usr/bin/gtkbasic002

HERE = xwin_getdir(command(2))
HERE &= "/resource" 
xwin_cd(HERE)

//-- load  file and show the window
gins_glade("project.glade")
gins_main()


//-- manipulate a widget, e.g. move the window
gins_set("window1" , "10" , "10" , "<uposition>")


//-- get information about a widget
a = gins_get("button1" , "label" , "<str>")
print a


mem = 0
op = ""
operands = {"+" , "-" , "*" , "/"}


//-- mainloop with eventhandling
while 1

  checkclose()
  widget , event , pdata = gins_event()

  if widget != Nothing then
  '  print widget
  '  print event
  '  print pdata

    if event = "GDK_DELETE" then
      gins_exit()
      end
    end if

    if event = "GDK_BUTTON_RELEASE"  then
   ' print widget
   ' print event
   ' print pdata



    widget = replace(widget , "button" , "")
    widget = val(widget)

      select case widget
      case 0 to 9

        if op = "done" then
          gins_set("entry1" , "text" , "" , "<str>")
          op = ""
          mem = 0
        end if
        if op != "" then
          if anumber = 0 then
          gins_set("entry1" , "text" , "" , "<str>")
          end if
        end if

        n = gins_get("entry1" , "text" , "<str>")
        gins_set("entry1" , "text" , n & widget , "<str>")
        anumber = 1

      case 10
        gins_set("entry1" , "text" , "" , "<str>")
        op = ""


      case 11

        n = gins_get("entry1" , "text" , "<str>")
        if op = "+" then
          gins_set("entry1" , "text" , mem + val(n) , "<str>")
        end if
        if op = "-" then
          gins_set("entry1" , "text" , mem - val(n) , "<str>")
        end if
        if op = "*" then
          gins_set("entry1" , "text" , mem * val(n) , "<str>")
        end if
        if op = "/" then
          gins_set("entry1" , "text" , mem / val(n) , "<str>")
        end if

        mem = 0
        op = "done"
        anumber = 0

      case 12 to 15
        setop(widget)
        anumber=0
      end select

    end if


  end if

  xwin_usleep(10000)

wend

sub setop (button)

  button -= 11
  op = operands[button]
  n = gins_get("entry1" , "text" , "<str>")
  mem = val(n)

end sub

 //------------------------------------------------------------
// this function exits Basic, if the grafical interface crashed
sub checkclose()
	a=gins_get("window1" , "name" , "<str>")
	if a = Nothing then
		print "window was closed, exiting..."
		gins_exit()
		end
	end if
end sub
