Microsoft Small Basic

Program Listing: KMG391-0
' Combo Box Sample
' Version 0.1b
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-10-24
' Program ID KMG391-0
'
GraphicsWindow.Title = "Combo Box Sample 0.1b"
GraphicsWindow.BackgroundColor = "LightGray"
Controls_Init()
item = "1=\=;2=>;3=<;4=>\=;5=<\=;6=<>;"
left = 10
top = 10
Controls_AddComboBox()
item = "1=And;2=Or;"
left = 10
top = 40
Controls_AddComboBox()
While "True"
If Controls_triggered Then
Controls_Proc()
Else
Program.Delay(200)
EndIf
EndWhile
Sub Controls_AddComboBox
' param item - array of items
' param left - the x co-ordinate of the combo box
' param top - the y co-ordinate of the combo box
' return cbox - the combo box
' version 0.1a
nCBox = nCBox + 1
fs = GraphicsWindow.FontSize
cbProp[nCBox]["height"] = fs * 1.8
nItem = Array.GetItemCount(item)
cbProp[nCBox]["nItem"] = nItem
cbProp[nCBox]["item"] = item
widthMax = 0
For i = 1 To nItem
len = Text.GetLength(item[i])
width = 0
For j = 1 To len
width = width + Math.Floor(luw[Text.GetSubText(item[i], j, 1)] * fs / 100)
EndFor
If widthMax < width Then
widthMax = width
EndIf
EndFor
tboxWidth = widthMax + fs
cbProp[nCBox]["width"] = tboxWidth
cbProp[nCBox]["x"] = left
cbProp[nCBox]["y"] = top
cbProp[nCBox]["tbox"] = Controls.AddTextBox(left, top)
Controls.SetSize(cbProp[nCBox]["tbox"], tboxWidth, cbProp[nCBox]["height"])
cbProp[nCBox]["btn"] = Controls.AddButton("▾", left + tboxWidth - 1, top)
Controls.SetSize(cbProp[nCBox]["btn"], fs * 1.8, cbProp[nCBox]["height"])
cbox = "ComboBox" + nCBox
Controls.ButtonClicked = Controls_OnButtonClicked
GraphicsWindow.MouseMove = Controls_OnMouseMove
GraphicsWindow.MouseDown = Controls_OnMouseDown
EndSub
Sub Controls_Proc
' version 0.1a
For i = 1 To nCBox
If Controls.LastClickedButton = cbProp[i]["btn"] Then
GraphicsWindow.PenColor = "LightGray"
GraphicsWindow.PenWidth = 1
GraphicsWindow.BrushColor = "White"
rect = Shapes.AddRectangle(cbProp[i]["width"], cbProp[i]["height"] * cbProp[i]["nItem"])
Shapes.Move(rect, cbProp[i]["x"], cbProp[i]["y"] + cbProp[i]["height"] - 1)
GraphicsWindow.BrushColor = "Black"
For j = 1 To cbProp[i]["nItem"]
it[j] = Shapes.AddText(cbProp[i]["item"][j])
Shapes.Move(it[j], cbProp[i]["x"] + cbProp[i]["height"] * 0.3, cbProp[i]["y"] + (j + 0.2) * cbProp[i]["height"])
EndFor
Controls_mouseDown = "False"
While Not[Controls_mouseDown]
If Controls_mouseMove Then
Controls_Select()
Controls_mouseMove = "False"
Else
Program.Delay(200)
EndIf
EndWhile
Controls_Select()
Shapes.Remove(cbSelect)
cbSelect = ""
For j = 1 To cbProp[i]["nItem"]
Shapes.Remove(it[j])
EndFor
Shapes.Remove(rect)
If selected <> 0 Then
Controls.SetTextBoxText(cbProp[i]["tbox"],cbProp[i]["item"][selected])
EndIf
EndIf
EndFor
Controls_triggered = "False"
EndSub
Sub Controls_Init
' version 0.1a
Not = "False=True;True=False;"
GraphicsWindow.FontName = "Lucida UI"
GraphicsWindow.BrushColor = "Black"
WQ = Text.GetCharacter(34)
' Lucida UI font width [px] table while the size (height) 100 [px]
luw = " =60;!=65;" + WQ + "=82;#=92;$=90;%=119;&=117;'=62;(=69;)=69;"
luw = luw + "*=78;+=103;,=59;-=73;.=59;/=77;0=90;1=90;2=90;3=90;4=90;"
luw = luw + "5=90;6=90;7=90;8=90;9=90;:=59;\;=59;<=103;\==103;>=103;"
luw = luw + "?=76;@=128;A=86;B=94;C=80;D=94;E=86;F=71;G=94;H=93;I=61;"
luw = luw + "J=61;K=88;L=61;M=124;N=93;O=94;P=94;Q=94;R=72;S=76;T=71;"
luw = luw + "U=93;V=87;W=112;X=88;Y=86;Z=80;[=69;\\=76;]=69;^=103;_=74;"
luw = luw + "`=64;{=69;|=65;}=69;~=103; =60;¡=65;¢=90;£=90;¤=88;¥=90;"
luw = luw + "¦=65;§=81;¨=79;©=120;ª=73;«=90;¬=103;­=32;®=120;¯=74;°=70;"
luw = luw + "±=103;²=73;³=73;´=63;µ=94;¶=83;·=59;¸=54;¹=72;º=78;»=90;"
luw = luw + "¼=128;½=129;¾=130;¿=76;À=86;Á=86;Â=86;Ã=86;Ä=86;Å=86;Æ=115;"
luw = luw + "Ç=80;È=86;É=86;Ê=86;Ë=86;Ì=61;Í=61;Î=61;Ï=61;Ð=92;Ñ=93;Ò=93;"
luw = luw + "Ó=93;Ô=93;Õ=93;Ö=93;×=103;Ø=94;Ù=93;Ú=93;Û=93;Ü=93;Ý=86;"
luw = luw + "Þ=94;ß=95;÷=103;ÿ=86;"
Controls_triggered = "False"
EndSub
Sub Controls_OnButtonClicked
' version 0.1a
Controls_triggered = "True"
EndSub
Sub Controls_OnMouseMove
' version 0.1a
Controls_mx = GraphicsWindow.MouseX
Controls_my = GraphicsWindow.MouseY
Controls_mouseMove = "True"
EndSub
Sub Controls_OnMouseDown
' version 0.1a
Controls_mx = GraphicsWindow.MouseX
Controls_my = GraphicsWindow.MouseY
Controls_mouseDown = "True"
EndSub
Sub Controls_Select
' version 0.1a
selected = 0
If cbProp[i]["x"] <= Controls_mx And Controls_mx <= cbProp[i]["x"] + cbProp[i]["width"] Then
For j = 1 To cbProp[i]["nItem"]
y1 = cbProp[i]["y"] + j * cbProp[i]["height"]
y2 = y1 + cbProp[i]["height"]
If y1 <= Controls_my And Controls_my <= y2 Then
GraphicsWindow.BrushColor = "#663399FF"
GraphicsWindow.PenWidth = 0
If cbSelect = "" Then
cbSelect = Shapes.AddRectangle(cbProp[i]["width"], cbProp[i]["height"])
EndIf
Shapes.Move(cbSelect, cbProp[i]["x"], y1 - 1)
selected = j
EndIf
EndFor
EndIf
EndSub