Microsoft Small Basic

Program Listing: QRT907-3
'add an inputField
Object[1]["type"]="Field"
Object[1]["left"]=150
Object[1]["top"]=10
Object[1]["width"]=100
Object[1]["height"]=20
Object[1]["text"]=""

'add another inputField
Object[2]["type"]="Field"
Object[2]["left"]=150
Object[2]["top"]=40
Object[2]["width"]=100
Object[2]["height"]=20
Object[2]["text"]=""

'add a Button
Object[3]["type"]="Button"
Object[3]["left"]=10
Object[3]["top"]=70
Object[3]["width"]=50
Object[3]["height"]=20
Object[3]["text"]="Close"

'add a Label
Object[4]["type"]="Label"
Object[4]["left"]=10
Object[4]["top"]=10
Object[4]["width"]=120
Object[4]["height"]=20
Object[4]["text"]="Enter text here:"

'add another Label
Object[5]["type"]="Label"
Object[5]["left"]=10
Object[5]["top"]=40
Object[5]["width"]=120
Object[5]["height"]=20
Object[5]["text"]="mouse position:"

'add another inputField
count=5
For x=0 to 4
For y=0 To 4
count=count+1
Object[count]["type"]="Field"
Object[count]["left"]=20+x*100
Object[count]["top"]=100+y*20
Object[count]["width"]=100
Object[count]["height"]=19
Object[count]["text"]=x+","+y
EndFor
endfor


'Clear table button
Object[31]["type"]="Button"
Object[31]["left"]=100
Object[31]["top"]=70
Object[31]["width"]=100
Object[31]["height"]=20
Object[31]["text"]="Clear Table"


OverObject=0
CurrentObject=3

drawObjects()

GraphicsWindow.MouseMove=OnHandleMouseMove
MouseMoveEvent = 0
Sub OnHandleMouseMove
MouseMoveEvent = 1
EndSub
GraphicsWindow.MouseDown=OnhandleMouseDown
MouseDownEvent = 0
Sub OnhandleMouseDown
If (MouseDownEvent = 0) Then
MouseDownEvent = 1
EndIf
EndSub
GraphicsWindow.MouseUp=OnhandleMouseUp
Sub OnhandleMouseUp
MouseDownEvent = 0
EndSub
GraphicsWindow.KeyDown=OnhandleKeyDown
KeyDownEvent = 0
Sub OnhandleKeyDown
KeyDownEvent = 1
EndSub

While ("True")
If (MouseMoveEvent = 1) Then
HandleMouseMove()
MouseMoveEvent = 0
EndIf
If (MouseDownEvent = 1) Then
handleMouseDown()
MouseDownEvent = 2 ' Wait for mouse up before looking for next mouse down
EndIf
If (KeyDownEvent = 1) Then
handleKeyDown()
KeyDownEvent = 0
EndIf
Program.Delay(100)
EndWhile

sub drawObjects
ClearObjects()

For i=1 To Array.GetItemCount(Object)
GraphicsWindow.PenWidth=1

If Object[i]["type"]="Field" then
If i=overObject then
GraphicsWindow.penWidth=3
Else
GraphicsWindow.PenWidth=1
endif
GraphicsWindow.BrushColor="White"
GraphicsWindow.FillRectangle(Object[i]["left"],Object[i]["top"],Object[i]["width"],Object[i]["height"])
GraphicsWindow.DrawRectangle(Object[i]["left"],Object[i]["top"],Object[i]["width"],Object[i]["height"])
GraphicsWindow.BrushColor="Blue"
GraphicsWindow.DrawBoundText(Object[i]["left"]+2,Object[i]["top"]+2,object[i]["width"],Object[i]["text"])

elseif Object[i]["type"]="Button" then
'experimetn with mouseOver colourchange
if i=currentObject then
GraphicsWindow.BrushColor="Yellow"
else
GraphicsWindow.BrushColor="White"
endif
GraphicsWindow.FillRectangle(Object[i]["left"],Object[i]["top"],Object[i]["width"],Object[i]["height"])
GraphicsWindow.DrawRectangle(Object[i]["left"],Object[i]["top"],Object[i]["width"],Object[i]["height"])
GraphicsWindow.BrushColor="Blue"

GraphicsWindow.DrawBoundText(Object[i]["left"]+2,Object[i]["top"]+2,object[i]["width"],Object[i]["text"])

elseif Object[i]["type"]="Label" then
GraphicsWindow.BrushColor="White"
GraphicsWindow.BrushColor="Blue"

GraphicsWindow.DrawBoundText(Object[i]["left"]+2,Object[i]["top"]+2,object[i]["width"],Object[i]["text"])

endif

EndFor
endsub

Sub HandleMouseMove
OverObject=0
For i=1 To Array.GetItemCount(Object)
If (Object[i]["left"]<=GraphicsWindow.MouseX) and (Object[i]["left"]+Object[i]["width"]>=GraphicsWindow.MouseX) and (Object[i]["top"]<=GraphicsWindow.MouseY) and (Object[i]["top"]+Object[i]["height"]>=GraphicsWindow.MouseY) then
OverObject=i
endif

EndFor


'Object[1]["text"]=OverObject+","+CurrentObject

Object[2]["text"]=GraphicsWindow.MouseX+"," +GraphicsWindow.mouseY
'if overObject<>0 then
drawObjects()
'endif
EndSub

Sub handleKeyDown
If Object[CurrentObject]["type"]="Button" then
key=graphicsWindow.lastKey
If key="Return" then
ButtonPressed()
endif
ElseIf Object[CurrentObject]["type"]="Field" then
key=GraphicsWindow.Lastkey
'DEBUG: GraphicsWindow.DrawBoundText(20,100,200,key)
If key="Back" then
s=Object[currentObject]["text"]
Object[currentObject]["text"]=Text.GetSubText(s,1,text.getlength(s)-1)
elseif (Text.GetLength(key)<2) then 'lousy way to determine if it was a letter
s=Object[currentObject]["text"]
Object[currentObject]["text"]=s+key
elseif (Text.StartsWith(key,"D") and (Text.GetLength(key)=2)) then 'it's a digit
s=Object[currentObject]["text"]
Object[currentObject]["text"]=s+text.GetSubText(key,2,1)
else
GraphicsWindow.ShowMessage(key+" pressed, only letters and digits can currently be processed","Oeps")
endif
endif
drawObjects()
endsub

Sub HandleMouseDown
If OverObject<>0 Then
CurrentObject=OverObject
EndIf

drawObjects()

If CurrentObject=1 Then

ElseIf CurrentObject=2 Then

ElseIf (CurrentObject=3) or (CurrentObject=31)Then
Sound.PlayClick()
ButtonPressed()
Endif
EndSub

Sub ClearObjects
GraphicsWindow.Clear()
endsub

Sub ButtonPressed
'this code is executed when the button is pressed with the mouse
If CurrentObject=3 then
Program.End()
ElseIf CurrentObject=31 then
For u=6 to 30
object[u]["text"]=""
endfor
endif
endsub