This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
uibuilder.nim
310 lines (271 loc) · 9.13 KB
/
uibuilder.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
import ui, os, streams, strutils, tables, strtabs, strformat, os, xml, xml/selector
import uibuilderpkg/[builder, helpers, uihelpers, types, uitypes]
type
Builder* = ref object of BaseBuilder
widgetById: TableRef[string, Widget]
proc newBuilder*(): Builder =
new result
result.widgetById = newTable[string, Widget]()
result.adjustmentById = newTable[string, Adjustment]()
result.textBufferById = newStringTable()
result.hasMenuBar = false
proc getWidgetById*(builder: Builder, id: string): Widget =
if builder.widgetById.hasKey(id):
result = builder.widgetById[id]
else:
raise newException(ValueError, "no widget with id " & id & " found")
proc run*(builder: Builder) =
mainLoop()
proc build(builder: Builder, ui: BuilderWidget, parent: var Widget) =
var widget: Widget
case ui.kind
of UiWindow:
widget = makeWindow(ui, builder.hasMenuBar)
of UiBox:
widget = makeBox(ui)
parent.addChild((Box)widget)
of UiGroup:
widget = newGroup(ui.groupTitle, true)
parent.addChild((Group)widget)
of UiButton:
widget = newButton(ui.buttonText)
parent.addChild((Button)widget)
of UICheckbox:
widget = newCheckbox(ui.checkboxText)
parent.addChild((Checkbox)widget)
of UIEntry:
widget = newEntry(ui.entryText)
parent.addChild((Entry)widget)
of UILabel:
widget = newLabel(ui.label)
parent.addChild((Label)widget)
of UISpinbox:
var adj: Adjustment
if ui.adjustmentId.len > 0 and builder.adjustmentById.hasKey(ui.adjustmentId):
adj = builder.adjustmentById[ui.adjustmentId]
var spinbox = newSpinbox(adj.lower, adj.upper)
if ui.value != 0:
spinbox.value = ui.value
else:
spinbox.value = adj.value
parent.addChild(spinbox)
widget = spinbox
of UiProgressBar:
widget = newProgressBar()
((ProgressBar)widget).value = 50
parent.addChild((ProgressBar)widget)
of UICombobox:
widget = newCombobox()
parent.addChild((Combobox)widget)
of UIEditableCombobox:
var ec = newEditableCombobox()
for item in ui.items:
ec.add(item)
parent.addChild(ec)
widget = ec
of UISeparator:
widget = newHorizontalSeparator()
parent.addChild((Separator)widget)
of UISlider:
var adj: Adjustment
if ui.sliderAdjustmentId.len > 0 and builder.adjustmentById.hasKey(ui.sliderAdjustmentId):
adj = builder.adjustmentById[ui.sliderAdjustmentId]
widget = newSlider(adj.lower, adj.upper)
((Slider)widget).value = adj.value
parent.addChild((Slider)widget)
of UIRadioButtons:
widget = newRadioButtons()
for button in ui.buttons:
((RadioButtons)widget).add(button)
parent.addChild((RadioButtons)widget)
of UiTab:
var tab = newTab()
for i in 0..<ui.labels.len:
var panel: Widget
builder.build(ui.children[i], panel)
if panel of Box:
tab.add(ui.labels[i], (Box)panel)
else:
raise newException(ValueError, "Tab " & ui.labels[i] & "'s panel must be a box")
parent.addChild(tab)
if ui.id.len > 0:
builder.widgetById[ui.id] = tab
# don't add children directly like other container
return
of UiMultilineEntry:
if ui.wrapText:
widget = newMultilineEntry()
else:
widget = newNonWrappingMultilineEntry()
((MultilineEntry)widget).text = ui.text
parent.addChild((MultilineEntry)widget)
else:
discard
for child in ui.children:
builder.build(child, widget)
if parent.isNil:
parent = widget
if ui.id.len > 0:
builder.widgetById[ui.id] = widget
proc makeMenu(menuBar: XmlNode) =
var
menu: Menu
menuItem: MenuItem
for m in menuBar.select("> child > object.GtkMenuItem"):
var properties = m.getProperties()
menu = newMenu(properties.getOrDefault("label", ""))
if properties.getOrDefault("visible", "True") != "True":
menu.hide()
for item in m.select("> child > object.GtkMenu > child > object"):
properties = item.getProperties()
case item.attr("class")
of "GtkMenuItem":
menuItem = menu.addItem(properties.getOrDefault("label", ""), proc() = discard)
of "GtkCheckMenuItem":
menuItem = menu.addCheckItem(properties.getOrDefault("label", ""), proc() = discard)
else:
{.warning: "only GtkMenuItem and GtkCheckMenuItem is supported".}
if properties.getOrDefault("visible", "True") != "True":
menuItem.hide()
# what is the property for disabled menu item??
#if properties.getOrDefault("can_focus", "True") != "True":
# menuItem.disable()
proc load*(builder: Builder, path: string) =
init()
var root = parseXml(readFile(path))
if root.name != "interface":
raise newException(IOError, "invalid glade file")
# search for GtkMenuBar and init it first
for node in root.children:
if node.name == "object" and node.attr("class") == "GtkMenuBar":
builder.hasMenuBar = true
makeMenu(node)
for node in root.children:
if node.name == "object" and node.attr("class") != "GtkMenuBar":
var
rootBuilderWidget: BuilderWidget
rootWidget: Widget
builder.parseXml(node, rootBuilderWidget)
builder.build(rootBuilderWidget, rootWidget)
proc gen*(builder: Builder, f: File, ui: BuilderWidget, ids: var seq[string], parent: BuilderWidget, parentName = ""): string {.discardable.} =
if ui.kind == None:
return
var name = getId(ui, ids)
result = name
case ui.kind
of UiWindow:
f.write &"""var {name} = newWindow("{ui.name}", {ui.width}, {ui.height}, {builder.hasMenuBar})
{name}.margined = true
{name}.onClosing = (proc (): bool = return true)
{name}.show()
"""
of UiBox:
if ui.orientation == HORIZONTAL:
f.write &"var {name} = newHorizontalBox()\n"
else:
f.write &"var {name} = newVerticalBox()\n"
of UiGroup:
f.write &"var {name} = newGroup(\"{ui.groupTitle}\", true)\n"
of UiButton:
f.write &"var {name} = newButton(\"{ui.buttonText}\")\n"
of UICheckbox:
f.write &"var {name} = newCheckbox(\"{ui.checkboxText}\")\n"
of UIEntry:
f.write &"var {name} = newEntry(\"{ui.entryText}\")\n"
of UILabel:
f.write &"var {name} = newLabel(\"{ui.label}\")\n"
of UISpinbox:
var adj: Adjustment
if ui.adjustmentId.len > 0 and builder.adjustmentById.hasKey(ui.adjustmentId):
adj = builder.adjustmentById[ui.adjustmentId]
f.write &"""var {name} = newSpinBox({adj.lower}, {adj.upper})
{name}.value = {adj.value}
"""
of UiProgressBar:
f.write &"var {name} = newProgressBar()\n"
of UICombobox:
f.write &"var {name} = newCombobox()\n"
of UIEditableCombobox:
f.write &"var {name} = newEditableCombobox()\n"
for item in ui.items:
f.write &"{name}.add(\"{item}\")\n"
of UISeparator:
f.write &"var {name} = newHorizontalSeparator()\n"
of UISlider:
var adj: Adjustment
if ui.sliderAdjustmentId.len > 0 and builder.adjustmentById.hasKey(ui.sliderAdjustmentId):
adj = builder.adjustmentById[ui.sliderAdjustmentId]
f.write &"""var {name} = newSlider({adj.lower}, {adj.upper})
{name}.value = {adj.value}
"""
of UIRadioButtons:
f.write &"var {name} = newRadioButtons()\n"
for button in ui.buttons:
f.write &"{name}.add(\"{button}\")\n"
of UiTab:
f.write &"var {name} = newTab()\n"
for i in 0..<ui.labels.len:
var panelName = builder.gen(f, ui.children[i], ids, ui, name)
f.write &"{name}.add(\"{ui.labels[i]}\", {panelName})\n"
of UiMultilineEntry:
if ui.wrapText:
f.write &"var {name} = newMultilineEntry()\n"
else:
f.write &"var {name} = newNonWrappingMultilineEntry()\n"
f.write &"{name}.text = \"\"\"{ui.text}\"\"\"\n"
else:
discard
if not ui.visible:
f.write &"{name}.hide()\n"
if parent.kind != None and parentName.len != 0:
f.write genAddStmt(parent.kind, parentName, ui.kind, name)
f.write "\n"
if ui.kind != UiTab:
for child in ui.children:
builder.gen(f, child, ids, ui, name)
proc codegen*(builder: Builder, path: string) =
var root = parseXml(readFile(path))
if root.name != "interface":
raise newException(IOError, "invalid glade file")
var
lastDot = path.rfind('.')
outputPath = path
outputPath[lastDot..<path.len] = ".nim"
var output = open(outputPath, fmWrite)
output.write """import ui
when isMainModule:
init()
"""
var ids: seq[string] = @[]
for node in root.children:
if node.name == "object":
var rootBuilderWidget: BuilderWidget
builder.parseXml(node, rootBuilderWidget)
builder.gen(output, rootBuilderWidget, ids, rootBuilderWidget)
if ids.len > 0:
output.write "\nexport " & ids.join(", ")
output.write """
when isMainModule:
mainLoop()
"""
output.close()
echo &"""Nim code saved at: {outputPath}
Run command bellow to see the result:
# nim c -r {outputPath}
"""
when isMainModule:
if paramCount() < 1 or paramCount() > 2:
quit(&"Usage: {paramStr(0)} -c <glade file>")
if paramCount() == 1:
var path = paramStr(1)
if not path.fileExists:
quit("Glade file {path} not found")
var b = newBuilder()
b.load(path)
b.run()
if paramCount() == 2 and paramStr(1) == "-c":
var path = paramStr(2)
if not path.fileExists:
quit("Glade file {path} not found")
var b = newBuilder()
b.codegen(path)