-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path525.html
More file actions
522 lines (484 loc) · 12.8 KB
/
Copy path525.html
File metadata and controls
522 lines (484 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<h1 id="form">FORM</h1>
<blockquote>
<p>f = FORM(formMap)</p>
</blockquote>
<p>Creates a form object <code>f</code> from a MAP variable
<code>formMap</code>. Form object <code>f</code> gives access to the
following GUI elements:</p>
<ul>
<li>Push button</li>
<li>Label</li>
<li>Hyperlinked text</li>
<li>Listbox</li>
<li>Dropdown listbox</li>
<li>Single or multi-line text input</li>
<li>Image button</li>
</ul>
<p>In listboxes and dropdown listboxes press and hold mouse button to
scroll through the items. Alternatively arrow keys can be used. Press
return or space for selecting the highlighted item.</p>
<h3 id="the-form-object">The form object</h3>
<p><code>f = form(formMAP)</code> provides access to the following
sub-commands of the form object <code>f</code>:</p>
<table>
<colgroup>
<col style="width: 29%" />
<col style="width: 70%" />
</colgroup>
<thead>
<tr class="header">
<th>Sub-command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>doEvents()</td>
<td>Process system events for mouse and keyboard handling.</td>
</tr>
<tr class="even">
<td>close()</td>
<td>Closes the active FORM.</td>
</tr>
<tr class="odd">
<td>refresh(n)</td>
<td><code>n = 1</code> copy the UI state into the FORM input variables
<code>f.inputs</code>. <code>n = 0</code> update the UI state using the
FORM input variables <code>f.inputs</code>.</td>
</tr>
</tbody>
</table>
<p>The form object <code>f</code> may contain the following
properties:</p>
<table>
<thead>
<tr class="header">
<th>Property</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>value</td>
<td>The value from the active input field.</td>
</tr>
<tr class="even">
<td><strong>inputs</strong></td>
<td>Array of inputs.</td>
</tr>
<tr class="odd">
<td>focus</td>
<td>Index to the focused input.</td>
</tr>
</tbody>
</table>
<h3 id="defining-the-input-fields">Defining the input fields</h3>
<p><code>inputs</code> is an array of type MAP, each element may contain
the following attributes:</p>
<table>
<colgroup>
<col style="width: 53%" />
<col style="width: 46%" />
</colgroup>
<thead>
<tr class="header">
<th>Attribute</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>x</td>
<td>X coordinate.</td>
</tr>
<tr class="even">
<td>y</td>
<td>Y coordinate.</td>
</tr>
<tr class="odd">
<td>width</td>
<td>Width.</td>
</tr>
<tr class="even">
<td>height</td>
<td>Height.</td>
</tr>
<tr class="odd">
<td>value</td>
<td>The internal value associated with the input.</td>
</tr>
<tr class="even">
<td>label</td>
<td>The display label for the input.</td>
</tr>
<tr class="odd">
<td>name</td>
<td>The name of the input.</td>
</tr>
<tr class="even">
<td><strong>type</strong></td>
<td>The type of input, see below.</td>
</tr>
<tr class="odd">
<td>help</td>
<td>Listbox or single line text input help text.</td>
</tr>
<tr class="even">
<td>backgroundColor</td>
<td>Background color.</td>
</tr>
<tr class="odd">
<td>color</td>
<td>Forground color.</td>
</tr>
<tr class="even">
<td>isExit</td>
<td>Whether clicking the input exits the current program.</td>
</tr>
<tr class="odd">
<td>isExternal</td>
<td>Whether the <code>link</code> field opens in an external
browser.</td>
</tr>
<tr class="even">
<td>resizable</td>
<td>Whether the field can be resized.</td>
</tr>
<tr class="odd">
<td>visible</td>
<td>Whether the input field is visible.</td>
</tr>
<tr class="even">
<td>selectedIndex</td>
<td>The selected item in a listbox or choice.</td>
</tr>
<tr class="odd">
<td>length</td>
<td>Length of a TEXT input field in number of characters.</td>
</tr>
<tr class="even">
<td>noFocus</td>
<td>The input cannot receive focus.</td>
</tr>
<tr class="odd">
<td>onclick</td>
<td>SUB to invoke when clicked.</td>
</tr>
</tbody>
</table>
<p>The type attribute can be one of the following:</p>
<table>
<thead>
<tr class="header">
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>“button”</td>
<td>Push button.</td>
</tr>
<tr class="even">
<td>“label”</td>
<td>Display label.</td>
</tr>
<tr class="odd">
<td>“link”</td>
<td>Hyperlinked text.</td>
</tr>
<tr class="even">
<td>“listbox”</td>
<td>Listbox.</td>
</tr>
<tr class="odd">
<td>“choice”</td>
<td>Dropdown listbox.</td>
</tr>
<tr class="even">
<td>“text”</td>
<td>Single or multi-line text input.</td>
</tr>
<tr class="odd">
<td>“image”</td>
<td>Image button.</td>
</tr>
</tbody>
</table>
<h3 id="change-or-read-the-gui-elements">Change or read the GUI
elements</h3>
<p>Once the GUI elements are rendered on screen they can be changed for
example to respond to user input. By calling <code>f.refresh(1)</code>
the current state of the GUI elements will be copied to <code>f</code>.
The content of <code>f</code> is rendered to screen, when calling
<code>f.refresh(0)</code>. <code>f.inputs</code> is an array of MAP
variables. The first element of that array is the first GUI element
added to <code>formMap.inputs</code>. For example the color of the first
element can be changed:</p>
<pre><code>f.inputs[0].color = 12
f.refresh(0)</code></pre>
<p>To get a list of all available elements of the n-th GUI element, you
can simply use <code>print f.inputs[n]</code>.</p>
<h3
id="example-1-creating-a-push-button-using-callback-function">Example 1:
Creating a push button using callback function</h3>
<p>When using buttons in combination with callback functions, the
<code>doEvents()</code> function is not necessary. Without this function
the execution of the program will not be blocked. In the following
example a number will be incremented in a while loop and printed to the
screen. When the button is pressed, the callback function is executed
and will block the program until the callback function ends.</p>
<pre><code>button.type = "button"
button.x = 120
button.y = 120
button.label = "Button"
button.backgroundcolor = rgb(60, 60, 60)
button.onclick = @ButtonClicked ' Callback function, definition see below
formMAP.inputs << button
f = form(formMAP)
while 1
ii++
locate 0,0: print ii
delay(100)
wend
f.close()
sub ButtonClicked()
locate 1,0
Clicked++
print "Button clicked " + Clicked + " times"
end
</code></pre>
<h3 id="example-2-creating-a-push-button-using-doevents-result">Example
2: Creating a push button using doEvents result</h3>
<pre><code>button.type = "button"
button.x = 120
button.y = 120
button.label = "Button"
button.backgroundcolor = rgb(60, 60, 60)
button.value = 1 ' unique value to identify the button
formMAP.inputs << button
f = form(formMAP)
while 1
f.doEvents()
if(f.value == 1) then
ii++
at 0,0
print "Button cklicked " + ii + " times."
endif
wend
f.close()</code></pre>
<h3 id="example-3-creating-a-label">Example 3: Creating a label</h3>
<pre><code>l.type = "label"
l.x = 120
l.y = 120
l.label = "Label"
l.color = rgb(255, 60, 60)
f.inputs << l
f = form(f)
while 1
f.doEvents()
wend
f.close()</code></pre>
<h3 id="example-4-creating-a-link-to-an-external-website">Example 4:
Creating a link to an external website</h3>
<pre><code>l.type = "link"
l.x = 120
l.y = 120
l.label = "Link to SmallBASIC website"
l.value = "https://smallbasic.github.io"
l.isExternal = true
l.color = rgb(100, 100, 255)
f.inputs << l
f = form(f)
while 1
f.doEvents()
wend
f.close()</code></pre>
<h3 id="example-5-creating-a-listbox">Example 5: Creating a listbox</h3>
<pre><code>l.type = "listbox"
l.x = 120
l.y = 120
l.height = 200
l.width = 100
l.value = "cats|dogs|fish|birds|insects"
' alternative way is to pass an array:
' l.value = ["cats", "dogs", "fish", "birds", "insects"]
l.color = rgb(255, 255, 255)
l.backgroundColor = rgb(100, 100, 100)
f.inputs << l
f = form(f)
while 1
f.doEvents()
' Check for value of the active input field
if (len(f.value) > 0) then
at 0,0
print f.value; " "
end if
wend
f.close()</code></pre>
<h3 id="example-6-creating-a-dropdown-listbox">Example 6: Creating a
dropdown listbox</h3>
<pre><code>l.type = "choice"
l.x = 120
l.y = 120
l.width = 100
l.value = "cats|dogs|fish|birds|insects"
' alternative way is to pass an array:
' l.value = ["cats", "dogs", "fish", "birds", "insects"]
l.color = rgb(255, 255, 255)
l.backgroundColor = rgb(100, 100, 100)
f.inputs << l
f = form(f)
while 1
f.doEvents()
' Check for value of the active input field
if (len(f.value) > 0) then
at 0,0
print f.value; " "
end if
wend
f.close()</code></pre>
<h3 id="example-7-creating-a-text-input-field">Example 7: Creating a
text input field</h3>
<pre><code>t.type = "text"
t.x = 120
t.y = 120
t.width = 300
t.value = "Add more text"
' t.height = 100 ' if hight is not defined, single line field
t.color = rgb(255, 255, 255)
t.backgroundColor = rgb(100, 100, 100)
t.length = 50 ' number of characters
' create additionally an OK-Button to update the form object
' otherwise the text field blocks loop
b.type = "button"
b.x = 120 + t.width + 10
b.y = 120
b.label = "OK"
b.backgroundcolor = rgb(60, 60, 60)
b.onclick = @OKButtonClicked
f.inputs << t
f.inputs << b
f = form(f)
while 1
f.doEvents()
wend
f.close()
sub OKButtonClicked()
f.refresh(1) ' Update the form object
at 0,0
print f.inputs[0].value ' Text field is the first element added to the formMAP
end</code></pre>
<h3 id="example-8-image-button-using-callback-function">Example 8: Image
button using callback function</h3>
<p>In this example a callback function will be used. If you want to use
the doEvents result instead, have a look at example 2.</p>
<pre><code>' Create a simple button and save it as png
' If you have already an image for you button,
' this part is not necessary.
circle 100,100,100 color 15 filled
ButtonImage = image(0,0,200,200)
ButtonImage.save("button.png")
cls
' Define image button
button.type = "image"
button.name = "button.png"
button.x = 120
button.y = 120
button.onclick = @ButtonClicked ' Callback function, definition see below.
formMAP.inputs << button
f = form(formMAP)
while 1
f.doEvents()
wend
f.close()
sub ButtonClicked()
at 0,0
Clicked++
print "Button clicked " + Clicked + " times"
end</code></pre>
<h3 id="example-9-one-more-example">Example 9: One more example</h3>
<pre><code>f.handleKeys = 0
' create some buttons
button1.y = 120
button1.label = "Button1"
button1.value = "valueofButton1"
button1.backgroundcolor = rgb(255,0,0)
button1.onclick = @HelloWorld 'this prints on load
button2.x = -1
button2.y = 120
button2.label = "Button2"
button2.value = "valueofButton2"
button3.value = "cats"
button3.x = -1
button3.y = 120
button3.background = 223344
b4.type = "choice"
b4.value = "cats|dogs|Fish|cats|dogs|Fish|cats|dogs|Fish|cats|dogs|Fish"
b4.selectedIndex = 2
b4.x = -1
b4.y = 120
b5.type = "list"
b5.value = "cats|dogs|Fish|cats|dogs|Fish|cats|dogs|Fish|cats|dogs|Fish|end|lol"
b5.x = -1
b5.y = 120
b5.height = 120
b6.type = "text"
b6.value = "cats"
b6.length=30
b6.x = -1
b6.y = 120
b6.width = 50
b6.noFocus = 0
' add buttons to the form
f.inputs << button1
f.inputs << button2
f.inputs << button3
f.inputs << b4
f.inputs << b5
f.inputs << b6
f.backgroundcolor = "green"
' at this stage 'f' is just a plain user defined structure (map)
' after calling FORM, 'f' becomes a system form object with three special functions
' doEvents, close and refresh (see sokoban.bas for refresh)
f = form(f)
' the string version of the form is JSON (see https://en.wikipedia.org/wiki/JSON)
TSAVE "f.frm", f
while 1
' pump the system event queue
f.doEvents()
' process the event
in$ = inkey
at 0,0
if len(in$)>1 then
flag= asc(left(in$,1))
keychar =right(in$,1)
n= asc(keychar)
if (flag == 1)
? "Ctrl : ";keychar ; " "
else if (flag == 2)
? "Alt : "; keychar
else if (flag == 3)
? "Ctrl+Alt: "; keychar ; " "
else
? "Arrow: "; n ; " "
end if
else
vkey = asc(in$)
if (vkey == 8) then
? "backspace!"
else if (vkey == 127) then
? "delete !"
else
? " key= "; in$; " "; vkey
endif
endif
if b6.value <> "cats" then ? b6.value
if (len(f.value) > 0) then
print f.value; " "
end if
wend
f.close()
func HelloWorld
? "hello world"
end</code></pre>