'interface version 3
'--- from Vroby (0404xx or earlier)
'--- more implementation trying from vroby (040502)
'--- more implementation trying from Paulo Silva (040429,040512)
'-------------------------------------
common _interface[256,8]

'-- properties
common _state=0
  common destroy=0,drawing=1,operative=2,clicked=3,activated=4
common _type=1
  common window=0,frame=1,label=2,menubutton=3
  common button=4,textentry=5,textarea=6,check=7
  common radio=8,list=9,combo=10,slider=11,staticbox=12
  common gauge=13
common _parent=2,_value=7
  common _x=3,_y=4,_w=5,_h=6

'-- colors
common _back_col=0xAAAAAA,_light=8^8-1,_dark=0

'-- reset the array
for i=0 to 256:_interface[i,0]=0:next

'-- GET / SET
'-------------------------------------
' getparam(n,param) : return the value of param
function getparam(n,param)
    return _interface[n,param]
end function
'-------------------------------------
' setparam(n,param,value) : set a param with value:and force redrawing
sub setparam(n,param,value)
    _interface[n,param]=value
    _interface[n,_state]=drawing
end sub
'-------------------------------------
' setnew :set a new interaface element
sub setnew(n,type,parent,x,y,w,h,value)
    _interface[n,_state]=1 'state default is 1 -1 =destroy
    _interface[n,_type]=type
    _interface[n,2]=parent
    _interface[n,3]=x:_interface[n,4]=y
    _interface[n,5]=w:_interface[n,6]=h
    _interface[n,7]=value
end sub
'-------------------------------------
'-- DRAWING SYSTEM
' drawing system use many routines that provides to draw respective element
' in a specific state drawobject set the relative coordinate of parent object
' and select the appropriate draw routine draw write all elements
' refresh rewrite all elements in current state
'-------------------------------------
sub draw_window(state,x,y,w,h,value)
   if state=1 then
	ink(_back_col): bar(x,y-15,x+w,y+h)
	ink(_light): box(x,y-15,x+w,y+h)
	ink(_light): bar(x,y-15,x+w,y)
	ink(_dark): text(x+10,y-15,12,value)
	state=2
   end if
end sub
'-------------------------------------
sub draw_frame(state,x,y,w,h,value)
     draw_staticbox(state,x,y,w,h,value)
end sub
'-------------------------------------
sub draw_label(state,x,y,w,h,value)
   if state=1 then
       ink(_back_col): bar(x,y,x+w,y+h)
       ink(_dark): text(x+2,y,12,value)
	state=2
   end if

end sub
'-------------------------------------
sub draw_menubutton(state,x,y,w,h,value)
 end sub
'-------------------------------------
sub draw_button(state,x,y,w,h,value)
   if state=1 then
       ink(_back_col): bar(x,y,x+w,y+h)
       ink(_light): line(x,y,x+w,y):line(x,y,x,y+h)
       ink(_dark):  line(x+w,y+h,x+w,y):line(x+w,y+h,x,y+h)
       ink(_dark): text(x+2,y,12,value)
	state=2
   end if
   if state=3 then
       ink(_back_col): bar(x,y,x+w,y+h)
       ink(_dark): line(x,y,x+w,y):line(x,y,x,y+h)
       ink(_light):  line(x+w,y+h,x+w,y):line(x+w,y+h,x,y+h)
       ink(_dark): text(x+2,y,12,value)
	state=4
   end if
 end sub
'-------------------------------------
sub draw_textentry(state,x,y,w,h,value)
   if state=1 then
       ink(_light): bar(x,y,x+w,y+h)
       ink(_dark): box(x,y,x+w,y+h)
       ink(_dark): text(x+2,y,12,value)
	state=2
    end if
 end sub
'-------------------------------------
sub draw_textarea(state,x,y,w,h,value)
       ink(_light): bar(x,y,x+w,y+h)
       ink(_dark): line(x,y,x+w,y):line(x,y,x,y+h)
       ink(_dark):  line(x+w,y+h,x+w,y):line(x+w,y+h,x,y+h)
       ink(_dark): text(x+2,y,12,value)
 end sub
'-------------------------------------
sub draw_check(state,x,y,w,h,value)
    if state=1 then
       ink(_back_col): bar(x,y,x+w,y+h)
       ink(_light)
         line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark)
         line(x,y,x+12,y):line(x,y,x,y+12)
         text(x+20,y-1,12,value)
           state=2
     end if
    if state=3 then
       ink(_back_col): bar(x,y,x+w,y+h)
       ink(_light)
         line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark)
         line(x,y,x+12,y):line(x,y,x,y+12)
         line(x+3,y+6,x+6,y+10):line(x+6,y+9,x+10,y+3)
         line(x+2,y+6,x+5,y+10):line(x+5,y+9,x+9,y+3)
         text(x+20,y-1,12,value)
           state=4
     end if
 end sub
'-------------------------------------
sub draw_radio(state,x,y,w,h,value)
   if state=1 then
      ink(_back_col)
        bar(x,y,x+w,y+h)
      ink(_light)
        line(x+12,y+9,x+12,y+3):line(x+9,y+12,x+3,y+12)
        line(x+12,y+9,x+9,y+12)
      ink(_dark)
        line(x+3,y,x+9,y):line(x,y+3,x,y+9)
        dot(x+1,y+2):dot(x+2,y+1)
        text(x+20,y-1,12,value)
          state=2
    end if
   if state=3 then
      ink(_back_col)
        bar(x,y,x+w,y+h)
      ink(_light)
        line(x+12,y+9,x+12,y+3):line(x+9,y+12,x+3,y+12)
        line(x+12,y+9,x+9,y+12)
      ink(_dark)
        line(x+3,y,x+9,y):line(x,y+3,x,y+9)
        dot(x+1,y+2):dot(x+2,y+1)
        bar(x+4,y+5,x+9,y+8):bar(x+5,y+4,x+8,y+9)
        text(x+20,y-1,12,value)
          state=4
    end if
 end sub
'-------------------------------------
sub draw_list(state,x,y,w,h,value)
 end sub
'-------------------------------------
sub draw_combo(state,x,y,w,h,value)
 end sub
'-------------------------------------
sub draw_slider(state,x,y,w,h,value)
   ink(_back_col): bar(x,y,x+w,y+8)
    ink(_dark): line(x,y+4,x+w,y+4):line(x,y+4,x,y+8)
    ink(_light):  line(x+w,y+4,x+w,y+8):line(x,y+8,x+w,y+8)
   ink(_back_col): bar(x+value,y,x+value+4,y+12)
    ink(_light):line(x+value,y,x+value+4,y):line(x+value,y+0,x+value,y+12)
    ink(_dark): line(x+value,y+12,x+value+4,y+12):line(x+value+4,y+0,x+value+4,y+12)
 end sub
'-------------------------------------
sub draw_staticbox(state,x,y,w,h,value)
      ink(_dark): box(x,y+8,x+w,y+h)
       ink(_light):box(x+1,y+9,x+w+1,y+h+1)
      ink(_back_col):vl=len(value)*7: bar(x+4,y,x+vl+4,y+12)
       ink(_dark): text(x+8,y,12,value)
  end sub
'-------------------------------------
sub draw_gauge(state,x,y,w,h,value)
    ink(_back_col):bar(x,y,x+w,y+12)
    ink(_light):   line(x,y+12,x+w,y+12):line(x+w,y,x+w,y+12)
    ink(_dark)
      line(x,y,x+w,y):line(x,y,x,y+12)
      bar(x+2,y+2,x+2+((w*value)/100),y+12-2)
  end sub
'-------------------------------------
'-------------------------------------
sub drawobject(i)
    if _interface[i,_state]<>2 then
    'and _interface[i,_state]<>0 then

	'SETPARENT
	if Parent<>-1 and parent <> i then
	    x=_interface[i,_x]+_interface[parent,_x]
	    y=_interface[i,_y]+_interface[parent,_y]
	    w=_interface[i,_w]
	    h=_interface[i,_h]
	    value=_interface[i,_value]
	else
	    x=_interface[i,_x]
	    y=_interface[i,_y]
	    w=_interface[i,_w]
	    h=_interface[i,_h]
	    value=_interface[i,_value]
	end if

	'WINDOW
	if _interface[i,_type]=window then
	    draw_window(_interface[i,_state],x,y,w,h,value)
	end if

	'BUTTON
	if _interface[i,_type]=button then
	    draw_button(_interface[i,_state],x,y,w,h,value)
	end if

	'LABEL
	if _interface[i,_type]=label then
	    draw_label(_interface[i,_state],x,y,w,h,value)
	end if

	'CHECK
	if _interface[i,_type]=check then
	    draw_check(_interface[i,_state],x,y,w,h,value)
	end if

	'RADIO
	if _interface[i,_type]=radio then
	    draw_radio(_interface[i,_state],x,y,w,h,value)
	end if

	'SLIDER
	if _interface[i,_type]=slider then
	    draw_slider(_interface[i,_state],x,y,w,h,value)
	end if

	'TEXTAREA
	if _interface[i,_type]=textarea then
	    draw_textarea(_interface[i,_state],x,y,w,h,value)
	end if

	'STATICBOX
	if _interface[i,_type]=staticbox then
	    draw_staticbox(_interface[i,_state],x,y,w,h,value)
	end if

	'GAUGE
	if _interface[i,_type]=gauge then
	    draw_gauge(_interface[i,_state],x,y,w,h,value)
	end if


    end if
end sub
'-------------------------------------
sub draw
    for i =0 to 256:drawobject(i): next
 end sub
'-------------------------------------
sub refresh
    for i =0 to 256
	if _interface[i,_state]=4 then
	    _interface[i,_state]=3
	else
	    if _interface[i,_state]<>0 then
		 _interface[i,_state]=1
	    end if
	end if
	drawobject(i)
    next
 end sub

'-------------------------------------
'-- EVENT SYSTEM
' Event system provide to verify all object event
' At this time not work well and doesn't optimizes routine
' The logic is similar to drawing system
'-------------------------------------

function event_window(i,state,x,y,w,h,value)
    if state=2 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
		state=3
		drawobject(i)
		return 3
		exit function
	    end if
	end if
    end if

    return 0
end function
'-------------------------------------

function event_button(i,state,x,y,w,h,value)
    if state=4 then
	state=1
	drawobject(i)
    end if

    if state=2 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
		state=3
		drawobject(i)
		return 3
		exit function
	    end if
	end if
    end if
    return 0
end function
'-------------------------------------
function event_label(i,state,x,y,w,h,value)

    if state=4 then
	state=1
	drawobject(i)
    end if

    if state=2 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
	       state=3
	       drawobject(i)
	       return 3
	       exit function
	    end if
	end if
    end if
    return 0
end function
'-------------------------------------
function event_check(i,state,x,y,w,h,value)
    if state=2 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
		state=3
		drawobject(i)
		return 3
		exit function
	    end if
	end if
    end if

    if state=4 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
		state=1
		drawobject(i)
		return 1
		exit function
	    end if
	end if
    end if
    return 0
end function
'-------------------------------------

function event_radio(i,state,x,y,w,h,value)
    if state=2 then
	if mousezone(x,y,w,h)then
	    if bmouse=1 then
		state=3
		drawobject(i)
		for i1 =0 to 256
		    if _interface[i1,_type]=radio and _interface[i1,_state]=activated and i1<>i then
			_interface[i1,_state]=1:drawobject(i1)
		    end if
		next
		return 3
		exit function
	    end if
	end if
    end if
    return 0
end function
'-------------------------------------

function eventobject(i)
      	'SETPARENT
	if Parent<>-1 and parent <> i then
	    x=_interface[i,_x]+_interface[parent,_x]
	    y=_interface[i,_y]+_interface[parent,_y]
	    w=_interface[i,_w]
	    h=_interface[i,_h]
	    value=_interface[i,_value]
	else
	    x=_interface[i,_x]
	    y=_interface[i,_y]
	    w=_interface[i,_w]
	    h=_interface[i,_h]
	    value=_interface[i,_value]
	end if

	'WINDOW
	if _interface[i,_type]=window then
	    return event_window(i,_interface[i,_state],x,y,w,h,value)
	    exit function
	end if

	'BUTTON
	if _interface[i,_type]=button then
	    return event_button(i,_interface[i,_state],x,y,w,h,value)
	    exit function
	end if

	'LABEL
	if _interface[i,_type]=label then
	    return event_label(i,_interface[i,_state],x,y,w,h,value)
	    exit function
	end if

	'CHECK
	if _interface[i,_type]=check then
	    return event_check(i,_interface[i,_state],x,y,w,h,value)
	    exit function
	end if

	'RADIO
	if _interface[i,_type]=radio then
	    return event_radio(i,_interface[i,_state],x,y,w,h,value)
	    exit function
	end if

    return 0
end function

'-------------------------------------
function event
    x=0
    for i =0 to 256
	x=x+eventobject(i)
    next
    waitvbl
    return x
 end function
'-------------------------------------
sub waitevent(t)
    'refresh
    while 0=0
	if event() <> 0 then :exit while:end if
	t-=1
	if t=0 then :exit while:end if
    end while
end sub
'-------------------------------------
