'interface version 3
'--- from Vroby (0404xx or earlier)
'--- more implementation trying from Paulo Silva (040429)
'--- more implementation trying from vroby (040502)
'-------------------------------------
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
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 rutines that provides to draw rispective element in a specific state
'drawobject set the relative coordinate of parent object and select the appropiate draw rutine
'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)
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(_light): text(x+2,y,12,value)
	state=4
   end if
 end sub
'-------------------------------------
sub draw_textentry(state,x,y,w,h,value)
 end sub
'-------------------------------------
sub draw_textarea(state,x,y,w,h,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(_dark): line(x,y,x+12,y):line(x,y,x,y+12)
       ink(_light): line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark): text(x+2+20,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+12,y):line(x,y,x,y+12)
       ink(_light): line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark): text(x+2+20,y,12,value)
       ink(_light): line(x+2,y+6,x+6,y+10):line(x+6,y+10,x+10,y+2)
	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(_dark): line(x,y,x+12,y):line(x,y,x,y+12)
       ink(_light): line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark): text(x+2+20,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+12,y):line(x,y,x,y+12)
       ink(_light): line(x+12,y+12,x+12,y):line(x+12,y+12,x,y+12)
       ink(_dark): text(x+2+20,y,12,value)
       ink(_light): bar(x+3,y+3,x+9,y+9)
	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 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

    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 obiect event
'At this time not work well and isn't optimize rutine
'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
'-------------------------------------
