Microsoft Small Basic

Program Listing: WMK208-0
' SmallBasic Version 1.1
' Program: ExpanderControl
' Changelog:
' Author: Pappa Lapub
' Website: https://social.msdn.microsoft.com/Forums/en-US/555a3319-d001-41ee-8604-3478a4c336cf/challenge-of-the-month-octobernovember-2015
' ImportURL: http://smallbasic.com/program/?WMK208-1
' Extension: LitDev (2x LDShapes.BrushColour for a nicer optics)
' Comment: http://www.codeproject.com/Articles/248112/Templating-WPF-Expander-Control
' Variables: d (Direction: -1 to close, 1 to open)
' ang (Angle: 180 opened, 0 closed)
' ToDo:
' ================================================================================
isOpen = "" ' start opened ?
y = 20 ' top for both
xL = 20 ' x for shpLeft
wL = 200 ' width for shpLeft
xR = 300 ' x for shpRight
wR = 250 ' width for shpRight
hHead = 16 ' height of expander title
hFull = 200 ' full expander height

dh = (hFull-hHead)/180
'swap = "–=+;+=–;"
swapSign = "-1=+;1=–;"

tab = Text.GetCharacter(9)
lf = Text.GetCharacter(10)

' http://www.blackbeltcoder.com/Articles/strings/implementing-word-wrap-in-email-messages
' txtL = SBText.WordWrap(txtL, wL/8)
txtL = tab+ "Always rrremember" +lf+lf
txtL = txtL+ " Formatted text to show in" +lf+ " opened Expander Control:" +lf
txtL = txtL+ " The voluminous expansion" +lf+ " of the subterrestric tuber" +lf
txtL = txtL+ " plants stands in indirect" +lf+ " proportional relationship" +lf
txtL = txtL+ " to the intellect of the" +lf+ " agronomist." +lf+lf
txtL = txtL+ " anon. 815 a.d. (free trans.)"

txtR = tab+tab+ "So what .." +lf+lf
txtR = txtR+ " Who works hard," +lf+tab+ "makes a lot of mistakes." +lf
txtR = txtR+ " Those who work little," +lf+tab+ "makes few mistakes." +lf
txtR = txtR+ " Who does not work," +lf+tab+ "does not make mistakes." +lf
txtR = txtR+ " Who does not make mistakes," +lf+tab+ "is conveyed!!" +lf+lf
txtR = txtR+tab+ "General proverb (auto trans.)"

BuildGUI()

GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.KeyDown = OnKeyDown

' continue = "True"
While "True" ' continue
If clicked Then
clicked = ""
If isOpen Then
Controls.SetSize(shpRight, wR,hHead)
'Shapes.SetText(shpSign, "+")
isOpen = ""
Else
Controls.SetSize(shpRight, wR,hFull)
'Shapes.SetText(shpSign, "–")
isOpen = "True"
EndIf
sign = -sign
Shapes.SetText(shpSign, swapSign[sign])
RotateCaret()
EndIf

Program.Delay(10)
EndWhile


' ////////// EVENTs \\\\\\\\\\
Sub OnMouseDown ' & checks MousePosition
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
If (mx > xL) And (mx < xL+wL) And (my > y) And (my < y+hHead) Then
clicked = "True" ' shpLeft
EndIf
If (mx > xR) And (mx < xR+wR) And (my > y) And (my < y+hHead) Then
clicked = "True" ' shpRight
EndIf
EndSub

Sub OnKeyDown
lastKey = GraphicsWindow.LastKey
If lastKey = "Escape" Then
Program.End()
ElseIf lastKey = "Return" Then
OnMouseDown() ' checks MousePosition
EndIf
EndSub


' ////////// SUBs \\\\\\\\\\
Sub BuildGUI
GraphicsWindow.Title = "Open|Close Expander by MouseClick or {Return} key with cursor over title and ............."
GraphicsWindow.BackgroundColor = "Peru"
GraphicsWindow.BrushColor = "Black" ' FontColor in Expander

'' Expander field right (immediately)
shpRight = Shapes.AddText(txtR)
Shapes.Move(shpRight, xR,y)
LDShapes.BrushColour(shpRight, "NavajoWhite")

'' Circle/Ring right
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "DeepSkyBlue"
shpCirc = Shapes.AddEllipse(16,16)
Shapes.Move(shpCirc, xR,y)

' Sign (-/+) right, -/+ .. opened/closed
GraphicsWindow.FontSize = 18
GraphicsWindow.BrushColor = "Black"
shpSign = Shapes.AddText("")
Shapes.Move(shpSign, xR+1,y-4)

'' Expander field left (animated)
GraphicsWindow.FontSize = 12
shpLeft = Shapes.AddText(txtL)
Shapes.Move(shpLeft, xL,y)
LDShapes.BrushColour(shpLeft, "Thistle")

'' Circle/Ring left
GraphicsWindow.BrushColor = "Cyan"
shpCirc = Shapes.AddEllipse(16,16)
Shapes.Move(shpCirc, xL,y)

'' Caret ^ left, with peak up/down .. opened/closed
GraphicsWindow.FontSize = 18
GraphicsWindow.BrushColor = "Black"
shpCaret = Shapes.AddText("^")
Shapes.Move(shpCaret, xL+1,y-4)

If isOpen Then
ang = 180
d = -1
h = hFull
dh = -dh
sign = 1 ' "–" after swapped (initially "+" )
Else
d = 1
h = hHead
sign = -1 ' "+" after swapped (initially "-" )
Shapes.Rotate(shpCaret, 180)
EndIf
Controls.SetSize(shpRight, wR,h)
Shapes.SetText(shpSign, swapSign[sign])
Controls.SetSize(shpLeft, wL,h)
EndSub

Sub RotateCaret
While done <> "True"
ang = ang + d
Shapes.Rotate(shpCaret, ang + 180)

h = h + dh
Controls.SetSize(shpLeft, wL, h)

If ang >= 180 Or ang <= 0 Then
d = -d
dh = -dh
done = "True"
EndIf
Program.Delay(3) '5, 10
EndWhile

done = ""
EndSub