Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
using new xml parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Aug 16, 2018
1 parent 1446c86 commit 1ec0d7e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
3 changes: 2 additions & 1 deletion examples/controlgallery.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ui, ../uibuilder, random, posix
from os import getAppDir

randomize()

var builder = newBuilder()
builder.load("basic_controls.glade")
builder.load(getAppDir() & "/basic_controls.glade")

var
button = (Button)builder.getWidgetById("button1")
Expand Down
54 changes: 28 additions & 26 deletions uibuilder.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ui, os, streams, xmlparser, xmltree, strutils, tables, strtabs, q, strformat, os
import ui, os, streams, strutils, tables, strtabs, strformat, os, xml, xml/selector
import uibuilderpkg/[helpers, types]

{.deadCodeElim: on.}
Expand Down Expand Up @@ -44,7 +44,6 @@ proc parseXml(builder: Builder, node: XmlNode, parent: var BuilderWidget, level
kind = UiRadioButtons

var widget = initUiWidget(kind, node)

if node.attr("id").len > 0:
widget.id = node.attr("id")

Expand All @@ -65,6 +64,7 @@ proc parseXml(builder: Builder, node: XmlNode, parent: var BuilderWidget, level
builder.adjustmentById[widget.id] = adj
return
of "GtkTextBuffer":
echo widget.id , " ", props.getOrDefault("text")
builder.textBufferById[widget.id] = props.getOrDefault("text", "")
return
else:
Expand Down Expand Up @@ -109,7 +109,7 @@ proc parseXml(builder: Builder, node: XmlNode, parent: var BuilderWidget, level
of UiEditableCombobox:
widget.items = @[]
for item in node.select("item"):
widget.items.add(item.innerText)
widget.items.add(item.text)
of UiRadioButtons:
widget.buttons = @[]
children = node.select("> child > object.GtkRadioButton")
Expand Down Expand Up @@ -277,24 +277,26 @@ proc makeMenu(menuBar: XmlNode) =

proc load*(builder: Builder, path: string) =
init()
var root = loadXml(path)
if root.tag != "interface":
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.items:
if node.tag == "object" and node.attr("class") == "GtkMenuBar":
builder.hasMenuBar = true
makeMenu(node)

for node in root.items:
if node.tag == "object" and node.attr("class") != "GtkMenuBar":
var
rootBuilderWidget: BuilderWidget
rootWidget: Widget

builder.parseXml(node, rootBuilderWidget)
builder.build(rootBuilderWidget, rootWidget)
if not root.children.isNil:
for node in root.children:
if node.name == "object" and node.attr("class") == "GtkMenuBar":
builder.hasMenuBar = true
makeMenu(node)

if not root.children.isNil:
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:
Expand Down Expand Up @@ -379,8 +381,8 @@ proc gen*(builder: Builder, f: File, ui: BuilderWidget, ids: var seq[string], pa
builder.gen(f, child, ids, ui, name)

proc codegen*(builder: Builder, path: string) =
var root = loadXml(path)
if root.tag != "interface":
var root = parseXml(readFile(path))
if root.name != "interface":
raise newException(IOError, "invalid glade file")
var
lastDot = path.rfind('.')
Expand All @@ -394,13 +396,13 @@ when isMainModule:
"""

var ids: seq[string] = @[]
for node in root.items:
if node.tag == "object":
var
rootBuilderWidget: BuilderWidget
if not root.children.isNil:
for node in root.children:
if node.name == "object":
var rootBuilderWidget: BuilderWidget

builder.parseXml(node, rootBuilderWidget)
builder.gen(output, rootBuilderWidget, ids, rootBuilderWidget)
builder.parseXml(node, rootBuilderWidget)
builder.gen(output, rootBuilderWidget, ids, rootBuilderWidget)

if ids.len > 0:
output.write "\nexport " & ids.join(", ")
Expand Down
5 changes: 3 additions & 2 deletions uibuilder.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.0"
version = "0.1.1"
author = "Huy Doan"
description = "UI building with Gnome\'s Glade"
license = "MIT"
Expand All @@ -10,4 +10,5 @@ bin = @["uibuilder"]

requires "nim >= 0.18.1"
requires "ui >= 0.9.2"
requires "q >= 0.0.6"
requires "q >= 0.0.6"
requires "https://github.com/ba0f3/xml.nim >= 0.1.1"
6 changes: 3 additions & 3 deletions uibuilderpkg/helpers.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import ui, strutils, strtabs, types, xmltree, q, random, strformat
import ui, strutils, strtabs, types, random, strformat, xml, xml/selector

randomize()

proc getProperties*(node: XmlNode): StringTableRef =
result = newStringTable(modeCaseInsensitive)
for prop in node.select("> property"):
result[prop.attr("name")] = prop.innerText
result[prop.attr("name")] = prop.text

proc getLabel*(node: XmlNode): string =
for prop in node.select("> property"):
if prop.attr("name") == "label":
result = prop.innerText
result = prop.text

proc getLabel*(node: seq[XmlNode]): string {.inline.} =
if not node.isNil and node.len > 0:
Expand Down
2 changes: 1 addition & 1 deletion uibuilderpkg/types.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ui, strtabs, xmltree
import ui, strtabs, xml

type
Orientation* = enum
Expand Down

0 comments on commit 1ec0d7e

Please sign in to comment.