Microsoft Small Basic

Program Listing: NVD371-2
' Small Wiki Editor
' Version 0.2a
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-07-27
' Program ID NVD371-2
'
title = "Small Wiki Editor 0.2a"
GraphicsWindow.Title = title
SB_Workaround()
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
LT = "<"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "#333333"
GraphicsWindow.FontName = "Segoe UI"
GraphicsWindow.FontBold = "False"
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "#666666"
posX = "1=5;2=105;3=205;4=305;"
item = "1=Wiki;2=HTML;3=Preview;"
GraphicsWindow.DrawLine(posX[1], 5, posX[4], 5)
For i = 1 To 3
menu[i] = Shapes.AddText(item[i])
Shapes.Move(menu[i], posX[i] + 5, 10)
GraphicsWindow.DrawLine(posX[i], 5, posX[i], 30)
hline[i] = Shapes.AddLine(posX[i], 30, posX[i + 1], 30)
EndFor
iLast = 1
Shapes.HideShape(hline[iLast])
GraphicsWindow.DrawLine(posX[4], 5, posX[4], 30)
GraphicsWindow.DrawLine(posX[4], 30, gw - 5, 30)
tbox = Controls.AddMultiLineTextBox(posX[1], 35)
Controls.SetSize(tbox, gw - 10, gh - 40)
LoadWikiText()
LoadHTMLText()
Controls.SetTextBoxText(tbox, wikiText)
GraphicsWindow.MouseDown = OnMouseDown
While "True"
If mouseDown Then
If 5 <= my And my < 30 Then
For i = 1 To 3
If posX[i] <= mx And mx < posX[i + 1] Then
If i <> iLast Then
Shapes.ShowShape(hline[iLast])
Shapes.HideShape(hline[i])
If i = 1 Or i = 2 Then
If iLast = 3 Then
Shapes_Remove()
EndIf
Controls.ShowControl(tbox)
If i = 1 Then
Controls.SetTextBoxText(tbox, wikiText)
ElseIf i = 2 Then
Controls.SetTextBoxText(tbox, htmlText)
EndIf
ElseIf i = 3 Then
Controls.HideControl(tbox)
' initialize shapes
If shape = "" Then
Shapes_Init()
EndIf
' add shapes
scale = 1
angle = 0
iMin = 1
iMax = Array.GetItemCount(shape)
Shapes_Add()
EndIf
iLast = i
EndIf
EndIf
EndFor
EndIf
mouseDown = "False"
EndIf
EndWhile
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
mouseDown = "True"
EndSub
Sub LoadWikiText
wikiText = "*bold*" + CRLF
wikiText = wikiText + "_italics_" + CRLF
wikiText = wikiText + "+underline+" + CRLF
wikiText = wikiText + "! Heading 1" + CRLF
wikiText = wikiText + "!! Heading 2" + CRLF
wikiText = wikiText + "!!! Heading 3" + CRLF
wikiText = wikiText + "* Bullet List" + CRLF
wikiText = wikiText + "** Bullet List 2" + CRLF
wikiText = wikiText + "# Number List" + CRLF
wikiText = wikiText + "## Number List 2" + CRLF
wikiText = wikiText + "[image:Turtle.png]" + CRLF
wikiText = wikiText + "||Table Heading 1||Table Heading 2||" + CRLF
wikiText = wikiText + "|Row 1 - Cell 1|Row 1 - Cell 2|" + CRLF
wikiText = wikiText + "|Row 2 - Cell 1|Row 2 - Cell 2|" + CRLF
wikiText = wikiText + "----"
EndSub
Sub LoadHTMLText
htmlText = "" + CRLF
htmlText = htmlText + LT + "body>" + CRLF
htmlText = htmlText + LT + "p>" + CRLF
htmlText = htmlText + LT + "strong>bold" + LT + "/strong>" + LT + "br>" + CRLF
htmlText = htmlText + LT + "em>italics" + LT + "/em>" + LT + "br>" + CRLF
htmlText = htmlText + LT + "span style='text-decoration:underline;'>underline" + LT + "/span>" + CRLF
htmlText = htmlText + LT + "/p>" + CRLF
htmlText = htmlText + LT + "h1>Heading 1" + LT + "/h1>" + CRLF
htmlText = htmlText + LT + "h2>Heading 2" + LT + "/h2>" + CRLF
htmlText = htmlText + LT + "h3>Heading 3" + LT + "/h3>" + CRLF
htmlText = htmlText + LT + "ul>" + CRLF
htmlText = htmlText + LT + "li>Bullet List" + LT + "/li>" + LT + "br>" + CRLF
htmlText = htmlText + LT + "ul>" + CRLF
htmlText = htmlText + LT + "li>Bullet List 2" + LT + "/li>" + CRLF
htmlText = htmlText + LT + "/ul>" + CRLF
htmlText = htmlText + LT + "/ul>" + CRLF
htmlText = htmlText + LT + "ol>" + CRLF
htmlText = htmlText + LT + "li>Number List" + LT + "/li>" + LT + "br>" + CRLF
htmlText = htmlText + LT + "ol>" + CRLF
htmlText = htmlText + LT + "li>Number List 2" + LT + "/li>" + CRLF
htmlText = htmlText + LT + "/ol>" + CRLF
htmlText = htmlText + LT + "/ol>" + CRLF
htmlText = htmlText + LT + "p>" + LT + "img src='http://www.nonkit.com/smallbasic.files/Turtle.png'>" + LT + "/p>" + CRLF
htmlText = htmlText + LT + "table border=1>" + CRLF
htmlText = htmlText + LT + "tr>" + LT + "th>Table Heading 1" + LT + "/th>" + LT + "th>Table Heading 2" + LT + "/th>" + LT + "/tr>" + CRLF
htmlText = htmlText + LT + "tr>" + LT + "td>Row 1-Cell 1" + LT + "/td>" + LT + "td>Row 1-Cell 2" + LT + "/td>" + LT + "/tr>" + CRLF
htmlText = htmlText + LT + "tr>" + LT + "td>Row 2-Cell 1" + LT + "/td>" + LT + "td>Row 2-Cell 2" + LT + "/td>" + LT + "/tr>" + CRLF
htmlText = htmlText + LT + "/table>" + CRLF
htmlText = htmlText + LT + "hr>" + CRLF
htmlText = htmlText + LT + "/body>" + CRLF
htmlText = htmlText + LT + "/html>"
EndSub
Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 10 ' x offset
shY = 35 ' y offset
shape = ""
shape[1] = "func=text;x=0;y=0;fn=Segoe UI;fs=12;fb=True;text=bold;bc=#000000;"
shape[2] = "func=text;x=0;y=16;fn=Segoe UI;fs=12;fi=True;text=italics;bc=#000000;"
shape[3] = "func=text;x=0;y=32;fn=Segoe UI;fs=12;text=underline;bc=#000000;"
shape[4] = "func=rect;x=0;y=46;width=50;height=1;pc=#000000;pw=1;"
shape[5] = "func=text;x=0;y=58;fn=Segoe UI;fs=36;fb=True;text=Heading 1;bc=#000000;"
shape[6] = "func=text;x=0;y=98;fn=Segoe UI;fs=26;fb=True;text=Heading 2;bc=#000000;"
shape[7] = "func=text;x=0;y=128;fn=Segoe UI;fs=16;fb=True;text=Heading 3;bc=#000000;"
shape[8] = "func=ell;x=20;y=154;x2=50;y2=54;width=5;height=5;bc=#000000;pw=0;"
shape[9] = "func=text;x=30;y=148;fn=Segoe UI;fs=12;text=Bullet List;bc=#000000;"
shape[10] = "func=ell;x=40;y=170;x2=50;y2=54;width=5;height=5;bc=#FFFFFF;pc=#000000;pw=1;"
shape[11] = "func=text;x=50;y=164;fn=Segoe UI;fs=12;text=Bullet List 2;bc=#000000;"
shape[12] = "func=text;x=18;y=180;fn=Segoe UI;fs=12;text=1. Number List;bc=#000000;"
shape[13] = "func=text;x=38;y=196;fn=Segoe UI;fs=12;text=1. Number List 2;bc=#000000;"
shape[14] = "func=img;x=0;y=212;src=http://www.nonkit.com/smallbasic.files/Turtle.png;"
shape[15] = "func=rect;x=0;y=464;width=200;height=54;bc=#FFFFFF;pc=#666666;pw=1;"
shape[16] = "func=rect;x=0;y=482;width=200;height=1;pc=#666666;pw=1;"
shape[17] = "func=rect;x=0;y=500;width=200;height=1;pc=#666666;pw=1;"
shape[18] = "func=rect;x=100;y=464;width=1;height=54;pc=#666666;pw=1;"
shape[19] = "func=text;x=2;y=464;fn=Segoe UI;fs=12;fb=True;text=Table Heading 1;bc=#000000;"
shape[20] = "func=text;x=102;y=464;fn=Segoe UI;fs=12;fb=True;text=Table Heading 2;bc=#000000;"
shape[21] = "func=text;x=2;y=482;fn=Segoe UI;fs=12;text=Row 1-Cell 1;bc=#000000;"
shape[22] = "func=text;x=102;y=482;fn=Segoe UI;fs=12;text=Row 1-Cell 2;bc=#000000;"
shape[23] = "func=text;x=2;y=500;fn=Segoe UI;fs=12;text=Row 2-Cell 1;bc=#000000;"
shape[24] = "func=text;x=102;y=500;fn=Segoe UI;fs=12;text=Row 2-Cell 2;bc=#000000;"
shape[25] = "func=rect;x=0;y=522;width=578;height=1;pc=#666666;pw=1;"
EndSub
Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
EndSub
Sub SB_RotateWorkaround
' Small Basic | Rotate workaround for Silverlight
' param shp - current shape
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shp["func"] = "tri" Then
x1 = -Math.Floor(shp["x3"] / 2)
y1 = -Math.Floor(shp["y3"] / 2)
ElseIf shp["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shp["x1"] - shp["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shp["y1"] - shp["y2"]) / 2)
EndIf
ox = x - x1
oy = y - y1
x = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + ox
y = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + oy
EndSub
Sub SB_Workaround
' Small Basic | Workaround for Silverlight
' returns silverlight - "True" if in remote
color = GraphicsWindow.GetPixel(0, 0)
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
EndIf
EndSub
Sub Shapes_Add
' Shapes | add shapes as shapes data
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - 1 if same scale
' return shWidth, shHeight - total size of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
s = scale
For i = iMin To iMax
shp = shape[i]
GraphicsWindow.PenWidth = shp["pw"] * s
If shp["pw"] > 0 Then
GraphicsWindow.PenColor = shp["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
EndIf
If shp["func"] = "rect" Then
shp["obj"] = Shapes.AddRectangle(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "ell" Then
shp["obj"] = Shapes.AddEllipse(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "tri" Then
shp["obj"] = Shapes.AddTriangle(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s, shp["x3"] * s, shp["y3"] * s)
ElseIf shp["func"] = "line" Then
shp["obj"] = Shapes.AddLine(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s)
ElseIf shp["func"] = "text" Then
fs = shp["fs"]
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
GraphicsWindow.FontBold = shp["fb"]
GraphicsWindow.FontItalic = shp["fi"]
shp["obj"] = Shapes.AddText(shp["text"])
ElseIf shp["func"] = "img" Then
shp["obj"] = Shapes.AddImage(shp["src"])
Shapes.Move(shp["obj"], shp["x"], shp["y"])
EndIf
x = shp["x"]
y = shp["y"]
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(shp["angle"])
SB_RotateWorkaround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
If Text.IsSubText("rect|ell|tri|text", shp["func"]) And (shp["angle"] <> 0) And (shp["angle"] <> "") Then
Shapes.Rotate(shp["obj"], shp["angle"])
EndIf
shape[i] = shp
EndFor
shAngle = 0
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub
Sub Shapes_CalcRotatePos
' Shapes | Calculate position for rotated shape
' param["x"], param["y"] - position of a shape
' param["width"], param["height"] - size of a shape
' param ["cx"], param["cy"] - center of rotation
' param ["angle"] - rotate angle
' return x, y - rotated position of a shape
_cx = param["x"] + param["width"] / 2
_cy = param["y"] + param["height"] / 2
x = _cx - param["cx"]
y = _cy - param["cy"]
Math_CartesianToPolar()
a = a + param["angle"]
x = r * Math.Cos(a * Math.Pi / 180)
y = r * Math.Sin(a * Math.Pi / 180)
_cx = x + param["cx"]
_cy = y + param["cy"]
x = _cx - param["width"] / 2
y = _cy - param["height"] / 2
EndSub
Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' param iMin, iMax - shape indices to add
' return shWidth, shHeight - total size of shapes
For i = iMin To iMax
shp = shape[i]
If shp["func"] = "tri" Or shp["func"] = "line" Then
xmin = shp["x1"]
xmax = shp["x1"]
ymin = shp["y1"]
ymax = shp["y1"]
If shp["x2"] < xmin Then
xmin = shp["x2"]
EndIf
If xmax < shp["x2"] Then
xmax = shp["x2"]
EndIf
If shp["y2"] < ymin Then
ymin = shp["y2"]
EndIf
If ymax < shp["y2"] Then
ymax = shp["y2"]
EndIf
If shp["func"] = "tri" Then
If shp["x3"] < xmin Then
xmin = shp["x3"]
EndIf
If xmax < shp["x3"] Then
xmax = shp["x3"]
EndIf
If shp["y3"] < ymin Then
ymin = shp["y3"]
EndIf
If ymax < shp["y3"] Then
ymax = shp["y3"]
EndIf
EndIf
shp["width"] = xmax - xmin
shp["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shp["x"] + shp["width"]
shHeight = shp["y"] + shp["height"]
Else
If shWidth < shp["x"] + shp["width"] Then
shWidth = shp["x"] + shp["width"]
EndIf
If shHeight < shp["y"] + shp["height"] Then
shHeight = shp["y"] + shp["height"]
EndIf
EndIf
shape[i] = shp
EndFor
EndSub
Sub Shapes_Move
' Shapes | Move shapes
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - to zoom
' param x, y - position to move
' return shX, shY - new position of shapes
Stack.PushValue("local", i)
s = scale
shX = x
shY = y
For i = iMin To iMax
shp = shape[i]
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
_x = shp["wx"]
_y = shp["wy"]
Else
_x = shp["rx"]
_y = shp["ry"]
EndIf
Shapes.Move(shp["obj"], shX + _x * s, shY + _y * s)
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Remove
' Shapes | Remove shapes
' param iMin, iMax - shapes indices to remove
' param shape - array of shapes
Stack.PushValue("local", i)
For i = iMin To iMax
shp = shape[i]
Shapes.Remove(shp["obj"])
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Rotate
' Shapes | Rotate shapes
' param iMin, iMax - shapes indices to rotate
' param shape - array of shapes
' param cx, cy - rotation center
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
s = scale
param["angle"] = angle
If cx <> "" Then
param["cx"] = cx
Else
cx = "" ' to avoid syntax error
param["cx"] = shWidth / 2
EndIf
If cy <> "" Then
param["cy"] = cy
Else
cy = "" ' to avoid syntax error
param["cy"] = shHeight / 2
EndIf
For i = iMin To iMax
shp = shape[i]
param["x"] = shp["x"]
param["y"] = shp["y"]
param["width"] = shp["width"]
param["height"] = shp["height"]
Shapes_CalcRotatePos()
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(angle + shp["angle"])
SB_RotateWorkAround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shp["obj"], angle + shp["angle"])
shape[i] = shp
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub