forked from nim-lang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.nim
529 lines (399 loc) · 15.6 KB
/
ui.nim
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
523
524
525
526
527
528
529
import ui/rawui
type
Widget* = ref object of RootRef ## abstract Widget base class.
proc init*() =
var o: rawui.InitOptions
var err: cstring
err = rawui.init(addr(o))
if err != nil:
let msg = $err
freeInitError(err)
raise newException(ValueError, msg)
proc quit*() = rawui.quit()
proc mainLoop*() =
rawui.main()
rawui.uninit()
proc pollingMainLoop*(poll: proc(timeout: int); timeout: int) =
## Can be used to merge an async event loop with UI's event loop.
## Implemented using timeouts and polling because that's the only
## thing that truely composes.
rawui.mainSteps()
while true:
poll(timeout)
discard rawui.mainStep(0)# != 0: break
rawui.uninit()
template newFinal(result) =
#proc finalize(x: type(result)) {.nimcall.} =
# controlDestroy(x.impl)
new(result) #, finalize)
template voidCallback(name, supertyp, basetyp, on) {.dirty.} =
proc name(w: ptr rawui.supertyp; data: pointer) {.cdecl.} =
let widget = cast[basetyp](data)
if widget.on != nil: widget.on()
template intCallback(name, supertyp, basetyp, on) {.dirty.} =
proc name(w: ptr rawui.supertyp; data: pointer) {.cdecl.} =
let widget = cast[basetyp](data)
if widget.on != nil: widget.on(widget.value)
# ------------------- Button --------------------------------------
type
Button* = ref object of Widget
impl*: ptr rawui.Button
onclick*: proc () {.closure.}
voidCallback(wrapOnClick, Button, Button, onclick)
proc text*(b: Button): string =
## Gets the button's text.
$buttonText(b.impl)
proc `text=`*(b: Button; text: string) =
## Sets the button's text.
buttonSetText(b.impl, text)
proc newButton*(text: string; onclick: proc() = nil): Button =
newFinal(result)
result.impl = rawui.newButton(text)
result.impl.buttonOnClicked(wrapOnClick, cast[pointer](result))
result.onclick = onclick
# ------------------------ RadioButtons ----------------------------
type
RadioButtons* = ref object of Widget
impl*: ptr rawui.RadioButtons
onRadioButtonClick*: proc() {.closure.}
voidCallback(wrapOnRadioButtonClick, RadioButtons, RadioButtons, onRadioButtonClick)
proc add*(r: RadioButtons; text: string) =
radioButtonsAppend(r.impl, text)
proc radioButtonsSelected*(r: RadioButtons): int =
radioButtonsSelected(r.impl)
proc newRadioButtons*(onclick: proc() = nil): RadioButtons =
newFinal(result)
result.impl = rawui.newRadioButtons()
result.impl.radioButtonsOnSelected(wrapOnRadioButtonClick, cast[pointer](result))
result.onRadioButtonClick = onclick
# ----------------- Window -------------------------------------------
type
Window* = ref object of Widget
impl*: ptr rawui.Window
onclosing*: proc (): bool
child: Widget
proc title*(w: Window): string =
## Gets the window's title.
$windowTitle(w.impl)
proc `title=`*(w: Window; text: string) =
## Sets the window's title.
windowSetTitle(w.impl, text)
proc destroy*(w: Window) =
## this needs to be called if the callback passed to addQuitItem returns
## true. Don't ask...
controlDestroy(w.impl)
proc onclosingWrapper(rw: ptr rawui.Window; data: pointer): cint {.cdecl.} =
let w = cast[Window](data)
if w.onclosing != nil:
if w.onclosing():
controlDestroy(w.impl)
rawui.quit()
system.quit()
proc newWindow*(title: string; width, height: int; hasMenubar: bool): Window =
newFinal(result)
result.impl = rawui.newWindow(title, cint width, cint height,
cint hasMenubar)
windowOnClosing(result.impl, onClosingWrapper, cast[pointer](result))
proc margined*(w: Window): bool = windowMargined(w.impl) != 0
proc `margined=`*(w: Window; x: bool) = windowSetMargined(w.impl, cint(x))
proc setChild*[SomeWidget: Widget](w: Window; child: SomeWidget) =
windowSetChild(w.impl, child.impl)
w.child = child
proc openFile*(parent: Window): string =
let x = openFile(parent.impl)
result = $x
if x != nil: freeText(x)
proc saveFile*(parent: Window): string =
let x = saveFile(parent.impl)
result = $x
if x != nil: freeText(x)
proc msgBox*(parent: Window; title, desc: string) =
msgBox(parent.impl, title, desc)
proc msgBoxError*(parent: Window; title, desc: string) =
msgBoxError(parent.impl, title, desc)
# ------------------------- Box ------------------------------------------
type
Box* = ref object of Widget
impl*: ptr rawui.Box
children*: seq[Widget]
proc add*[SomeWidget: Widget](b: Box; child: SomeWidget; stretchy=false) =
boxAppend(b.impl, child.impl, cint(stretchy))
b.children.add child
proc delete*(b: Box; index: int) = boxDelete(b.impl, index.cint)
proc padded*(b: Box): bool = boxPadded(b.impl) != 0
proc `padded=`*(b: Box; x: bool) = boxSetPadded(b.impl, x.cint)
proc newHorizontalBox*(padded = false): Box =
newFinal(result)
result.impl = rawui.newHorizontalBox()
result.children = @[]
boxSetPadded(result.impl, padded.cint)
proc newVerticalBox*(padded = false): Box =
newFinal(result)
result.impl = rawui.newVerticalBox()
result.children = @[]
boxSetPadded(result.impl, padded.cint)
# -------------------- Checkbox ----------------------------------
type
Checkbox* = ref object of Widget
impl*: ptr rawui.Checkbox
ontoggled*: proc ()
proc text*(c: Checkbox): string = $checkboxText(c.impl)
proc `text=`*(c: Checkbox; text: string) = checkboxSetText(c.impl, text)
voidCallback(wrapOntoggled, Checkbox, Checkbox, ontoggled)
proc checked*(c: Checkbox): bool = checkboxChecked(c.impl) != 0
proc `checked=`*(c: Checkbox; x: bool) =
checkboxSetChecked(c.impl, cint(x))
proc newCheckbox*(text: string; ontoggled: proc() = nil): Checkbox =
newFinal(result)
result.impl = rawui.newCheckbox(text)
result.ontoggled = ontoggled
checkboxOnToggled(result.impl, wrapOntoggled, cast[pointer](result))
# ------------------ Entry ---------------------------------------
type
Entry* = ref object of Widget
impl*: ptr rawui.Entry
onchanged*: proc ()
proc text*(e: Entry): string = $entryText(e.impl)
proc `text=`*(e: Entry; text: string) = entrySetText(e.impl, text)
voidCallback(wrapOnchanged, Entry, Entry, onchanged)
proc readOnly*(e: Entry): bool = entryReadOnly(e.impl) != 0
proc `readOnly=`*(e: Entry; x: bool) =
entrySetReadOnly(e.impl, cint(x))
proc newEntry*(text: string; onchanged: proc() = nil): Entry =
newFinal(result)
result.impl = rawui.newEntry()
result.impl.entryOnChanged(wrapOnchanged, cast[pointer](result))
result.onchanged = onchanged
entrySetText(result.impl, text)
# ----------------- Label ----------------------------------------
type
Label* = ref object of Widget
impl*: ptr rawui.Label
proc text*(L: Label): string = $labelText(L.impl)
proc `text=`*(L: Label; text: string) = labelSetText(L.impl, text)
proc newLabel*(text: string): Label =
newFinal(result)
result.impl = rawui.newLabel(text)
# ---------------- Tab --------------------------------------------
type
Tab* = ref object of Widget
impl*: ptr rawui.Tab
children*: seq[Widget]
proc add*[SomeWidget: Widget](t: Tab; name: string; c: SomeWidget) =
tabAppend t.impl, name, c.impl
t.children.add c
proc insertAt*[SomeWidget: Widget](t: Tab; name: string; at: int; c: SomeWidget) =
tabInsertAt(t.impl, name, at.uint64, c.impl)
t.children.insert(c, at)
proc delete*(t: Tab; index: int) =
tabDelete(t.impl, index.cint)
t.children.delete(index)
proc numPages*(t: Tab): int = tabNumPages(t.impl).int
proc margined*(t: Tab; page: int): bool =
tabMargined(t.impl, page.cint) != 0
proc `margined=`*(t: Tab; page: int; x: bool) =
tabSetMargined(t.impl, page.cint, cint(x))
proc newTab*(): Tab =
newFinal result
result.impl = rawui.newTab()
result.children = @[]
# ------------- Group --------------------------------------------------
type
Group* = ref object of Widget
impl*: ptr rawui.Group
child: Widget
proc title*(g: Group): string = $groupTitle(g.impl)
proc `title=`*(g: Group; title: string) =
groupSetTitle(g.impl, title)
proc `child=`*[SomeWidget: Widget](g: Group; c: SomeWidget) =
groupSetChild(g.impl, c.impl)
g.child = c
proc margined*(g: Group): bool = groupMargined(g.impl) != 0
proc `margined=`*(g: Group; x: bool) =
groupSetMargined(g.impl, x.cint)
proc newGroup*(title: string; margined=false): Group =
newFinal result
result.impl = rawui.newGroup(title)
groupSetMargined(result.impl, margined.cint)
# ----------------------- Spinbox ---------------------------------------
type
Spinbox* = ref object of Widget
impl*: ptr rawui.Spinbox
onchanged*: proc(newvalue: int)
proc value*(s: Spinbox): int = spinboxValue(s.impl)
proc `value=`*(s: Spinbox; value: int) = spinboxSetValue(s.impl, value.cint)
intCallback wrapsbOnChanged, Spinbox, Spinbox, onchanged
proc newSpinbox*(min, max: int; onchanged: proc (newvalue: int) = nil): Spinbox =
newFinal result
result.impl = rawui.newSpinbox(cint min, cint max)
spinboxOnChanged result.impl, wrapsbOnChanged, cast[pointer](result)
result.onchanged = onchanged
# ---------------------- Slider ---------------------------------------
type
Slider* = ref object of Widget
impl*: ptr rawui.Slider
onchanged*: proc(newvalue: int)
proc value*(s: Slider): int = sliderValue(s.impl)
proc `value=`*(s: Slider; value: int) = sliderSetValue(s.impl, cint value)
intCallback wrapslOnChanged, Slider, Slider, onchanged
proc newSlider*(min, max: int; onchanged: proc (newvalue: int) = nil): Slider =
newFinal result
result.impl = rawui.newSlider(cint min, cint max)
sliderOnChanged result.impl, wrapslOnChanged, cast[pointer](result)
result.onchanged = onchanged
# ------------------- Progressbar ---------------------------------
type
ProgressBar* = ref object of Widget
impl*: ptr rawui.ProgressBar
proc `value=`*(p: ProgressBar; n: int) =
progressBarSetValue p.impl, n.cint
proc newProgressBar*(): ProgressBar =
newFinal result
result.impl = rawui.newProgressBar()
# ------------------------- Separator ----------------------------
type
Separator* = ref object of Widget
impl*: ptr rawui.Separator
proc newVerticalSeparator*(): Separator =
newFinal result
result.impl = rawui.newVerticalSeparator()
proc newHorizontalSeparator*(): Separator =
newFinal result
result.impl = rawui.newHorizontalSeparator()
# ------------------------ Combobox ------------------------------
type
Combobox* = ref object of Widget
impl*: ptr rawui.Combobox
onselected*: proc ()
proc add*(c: Combobox; text: string) =
c.impl.comboboxAppend text
proc selected*(c: Combobox): int = comboboxSelected(c.impl)
proc `selected=`*(c: Combobox; n: int) =
comboboxSetSelected c.impl, cint n
voidCallback wrapbbOnSelected, Combobox, Combobox, onselected
proc newCombobox*(onSelected: proc() = nil): Combobox =
newFinal result
result.impl = rawui.newCombobox()
result.onSelected = onSelected
comboboxOnSelected(result.impl, wrapbbOnSelected, cast[pointer](result))
# ----------------------- EditableCombobox ----------------------
type
EditableCombobox* = ref object of Widget
impl*: ptr rawui.EditableCombobox
onchanged*: proc ()
proc add*(c: EditableCombobox; text: string) =
editableComboboxAppend(c.impl, text)
proc text*(c: EditableCombobox): string =
$editableComboboxText(c.impl)
proc `text=`*(c: EditableCombobox; text: string) =
editableComboboxSetText(c.impl, text)
voidCallback wrapecbOnchanged, EditableCombobox, EditableCombobox, onchanged
proc newEditableCombobox*(onchanged: proc () = nil): EditableCombobox =
newFinal result
result.impl = rawui.newEditableCombobox()
result.onchanged = onchanged
editableComboboxOnChanged result.impl, wrapecbOnchanged, cast[pointer](result)
# ------------------------ MultilineEntry ------------------------------
type
MultilineEntry* = ref object of Widget
impl*: ptr rawui.MultilineEntry
onchanged*: proc ()
proc text*(e: MultilineEntry): string =
$multilineEntryText(e.impl)
proc `text=`*(e: MultilineEntry; text: string) =
multilineEntrySetText(e.impl, text)
proc add*(e: MultilineEntry; text: string) =
multilineEntryAppend(e.impl, text)
voidCallback wrapmeOnchanged, MultilineEntry, MultilineEntry, onchanged
proc readonly*(e: MultilineEntry): bool =
multilineEntryReadOnly(e.impl) != 0
proc `readonly=`*(e: MultilineEntry; x: bool) =
multilineEntrySetReadOnly(e.impl, cint(x))
proc newMultilineEntry*(): MultilineEntry =
newFinal result
result.impl = rawui.newMultilineEntry()
multilineEntryOnChanged(result.impl, wrapmeOnchanged, cast[pointer](result))
proc newNonWrappingMultilineEntry*(): MultilineEntry =
newFinal result
result.impl = rawui.newNonWrappingMultilineEntry()
multilineEntryOnChanged(result.impl, wrapmeOnchanged, cast[pointer](result))
# ---------------------- MenuItem ---------------------------------------
type
MenuItem* = ref object of Widget
impl*: ptr rawui.MenuItem
onclicked*: proc ()
proc enable*(m: MenuItem) = menuItemEnable(m.impl)
proc disable*(m: MenuItem) = menuItemDisable(m.impl)
proc wrapmeOnclicked(sender: ptr rawui.MenuItem;
window: ptr rawui.Window; data: pointer) {.cdecl.} =
let m = cast[MenuItem](data)
if m.onclicked != nil: m.onclicked()
proc checked*(m: MenuItem): bool = menuItemChecked(m.impl) != 0
proc `checked=`*(m: MenuItem; x: bool) = menuItemSetChecked(m.impl, cint(x))
# -------------------- Menu ---------------------------------------------
type
Menu* = ref object of Widget
impl*: ptr rawui.Menu
children*: seq[MenuItem]
template addMenuItemImpl(ex) =
newFinal result
result.impl = ex
menuItemOnClicked(result.impl, wrapmeOnclicked, cast[pointer](result))
m.children.add result
proc addItem*(m: Menu; name: string, onclicked: proc()): MenuItem {.discardable.} =
addMenuItemImpl(menuAppendItem(m.impl, name))
result.onclicked = onclicked
proc addCheckItem*(m: Menu; name: string, onclicked: proc()): MenuItem {.discardable.} =
addMenuItemImpl(menuAppendCheckItem(m.impl, name))
result.onclicked = onclicked
type
ShouldQuitClosure = ref object
fn: proc(): bool
proc wrapOnShouldQuit(data: pointer): cint {.cdecl.} =
let c = cast[ShouldQuitClosure](data)
result = cint(c.fn())
if result == 1:
GC_unref c
proc addQuitItem*(m: Menu, shouldQuit: proc(): bool): MenuItem {.discardable.} =
newFinal result
result.impl = menuAppendQuitItem(m.impl)
m.children.add result
var cl = ShouldQuitClosure(fn: shouldQuit)
GC_ref cl
onShouldQuit(wrapOnShouldQuit, cast[pointer](cl))
proc addPreferencesItem*(m: Menu, onclicked: proc()): MenuItem {.discardable.} =
addMenuItemImpl(menuAppendPreferencesItem(m.impl))
result.onclicked = onclicked
proc addAboutItem*(m: Menu, onclicked: proc()): MenuItem {.discardable.} =
addMenuItemImpl(menuAppendAboutItem(m.impl))
result.onclicked = onclicked
proc addSeparator*(m: Menu) =
menuAppendSeparator m.impl
proc newMenu*(name: string): Menu =
newFinal result
result.impl = rawui.newMenu(name)
result.children = @[]
# -------------------- Generics ------------------------------------
proc show*[W: Widget](w: W) =
rawui.controlShow(w.impl)
proc hide*[W: Widget](w: W) =
rawui.controlHide(w.impl)
proc enable*[W: Widget](w: W) =
rawui.controlEnable(w.impl)
proc disable*[W: Widget](w: W) =
rawui.controlDisable(w.impl)
# -------------------- DateTimePicker ------------------------------
type
DateTimePicker* = ref object of Widget
impl*: ptr rawui.DateTimePicker
proc selectedDateTime*(dt: DateTimePicker): DateTime =
result = rawui.dateTimeSelected(dt.impl)
proc newDateTimePicker*(): DateTimePicker =
newFinal result
result.impl = rawui.newDateTimePicker()
proc newDatePicker*(): DateTimePicker =
newFinal result
result.impl = rawui.newDatePicker()
proc newTimePicker*(): DateTimePicker =
newFinal result
result.impl = rawui.newTimePicker()