diff options
Diffstat (limited to 'iup/srclua5/tree.lua')
-rwxr-xr-x | iup/srclua5/tree.lua | 107 |
1 files changed, 73 insertions, 34 deletions
diff --git a/iup/srclua5/tree.lua b/iup/srclua5/tree.lua index bd8daf7..5bd21c4 100755 --- a/iup/srclua5/tree.lua +++ b/iup/srclua5/tree.lua @@ -3,16 +3,16 @@ ------------------------------------------------------------------------------ local ctrl = { nick = "tree", - parent = WIDGET, + parent = iup.WIDGET, creation = "", callback = { selection_cb = "nn", multiselection_cb = "nn", -- fake definition to be replaced by treefuncs module + multiunselection_cb = "nn", -- fake definition to be replaced by treefuncs module branchopen_cb = "n", branchclose_cb = "n", executeleaf_cb = "n", - renamenode_cb = "ns", - noderemoved_cb = "ns", -- fake definition to be replaced by treefuncs module + noderemoved_cb = "s", -- fake definition to be replaced by treefuncs module rename_cb = "ns", showrename_cb = "n", rightclick_cb = "n", @@ -21,56 +21,95 @@ local ctrl = { extrafuncs = 1, } -local function TreeSetAttributeHandle(handle, name, value) - if iupGetClass(value) == "iup handle" then value = ihandle_setname(value) end - SetAttribute(handle, name, value) +function iup.TreeSetNodeAttributes(handle, id, attrs) + for attr, val in pairs(attrs) do + handle[attr..id] = val + end +end + +function iup.TreeSetAncestorsAttributes(handle, ini, attrs) + ini = handle["parent"..ini] + local stack = {} + while ini do + table.insert(stack, 1, ini) + ini = handle["parent"..ini] + end + for i = 1, #stack do + iup.TreeSetNodeAttributes(handle, stack[i], attrs) + end +end + +function iup.TreeSetDescentsAttributes(handle, ini, attrs) + local id = ini + for i = 1, handle["childcount"..ini] do + id = id+1 + iup.TreeSetNodeAttributes(handle, id, attrs) + if handle["kind"..id] == "BRANCH" then + id = iup.TreeSetDescentsAttributes(handle, id, attrs) + end + end + return id +end + +function iup.TreeSetAttributeHandle(handle, name, value) + if iup.GetClass(value) == "iup handle" then value = iup.SetHandleName(value) end + iup.SetAttribute(handle, name, value) end -function TreeSetNodeAttrib(handle, node, id) - if node.color then SetAttribute(handle, "COLOR"..id, node.color) end - if node.state then SetAttribute(handle, "STATE"..id, node.state) end - if node.titlefont then SetAttribute(handle, "TITLEFONT"..id, node.titlefont) end - if node.marked then SetAttribute(handle, "MARKED"..id, node.marked) end - if node.image then TreeSetAttributeHandle(handle, "IMAGE"..id, node.image) end - if node.imageexpanded then TreeSetAttributeHandle(handle, "IMAGEEXPANDED"..id, node.imageexpanded) end - if node.userid then TreeSetUserId(handle, id, node.userid) end +-- must be after the branch has nodes +function iup.TreeSetState(handle, tnode, id) + if tnode.state then iup.SetAttribute(handle, "STATE"..id, tnode.state) end end -function TreeSetValueRec(handle, t, id) +function iup.TreeSetNodeAttrib(handle, tnode, id) + if tnode.color then iup.SetAttribute(handle, "COLOR"..id, tnode.color) end + if tnode.titlefont then iup.SetAttribute(handle, "TITLEFONT"..id, tnode.titlefont) end + if tnode.marked then iup.SetAttribute(handle, "MARKED"..id, tnode.marked) end + if tnode.image then iup.TreeSetAttributeHandle(handle, "IMAGE"..id, tnode.image) end + if tnode.imageexpanded then iup.TreeSetAttributeHandle(handle, "IMAGEEXPANDED"..id, tnode.imageexpanded) end + if tnode.userid then iup.TreeSetUserId(handle, id, tnode.userid) end +end + +function iup.TreeAddNodesRec(handle, t, id) if t == nil then return end local cont = #t while cont >= 0 do - local node = t[cont] - if type(node) == "table" then - if node.branchname then - SetAttribute(handle, "ADDBRANCH"..id, node.branchname) - TreeSetNodeAttrib(handle, node, id+1) - TreeSetValueRec(handle, node, id+1) - elseif node.leafname then - SetAttribute(handle, "ADDLEAF"..id, node.leafname) - TreeSetNodeAttrib(handle, node, id+1) + local tnode = t[cont] + if type(tnode) == "table" then + if tnode.branchname then + iup.SetAttribute(handle, "ADDBRANCH"..id, tnode.branchname) + iup.TreeSetNodeAttrib(handle, tnode, id+1) + iup.TreeAddNodesRec(handle, tnode, id+1) + iup.TreeSetState(handle, tnode, id+1) + elseif tnode.leafname then + iup.SetAttribute(handle, "ADDLEAF"..id, tnode.leafname) + iup.TreeSetNodeAttrib(handle, tnode, id+1) end else - if node then - SetAttribute(handle, "ADDLEAF"..id, node) + if tnode then + iup.SetAttribute(handle, "ADDLEAF"..id, tnode) end end cont = cont - 1 end end -function TreeSetValue(handle, t, id) +function iup.TreeAddNodes(handle, t, id) if (not id) then id = 0 -- default is the root - if t.branchname then SetAttribute(handle, "TITLE0", t.branchname) end - TreeSetNodeAttrib(handle, t, 0) + if t.branchname then iup.SetAttribute(handle, "TITLE0", t.branchname) end + iup.TreeSetNodeAttrib(handle, t, 0) end - TreeSetValueRec(handle, t, id) + iup.TreeAddNodesRec(handle, t, id) + if (id == 0) then iup.TreeSetState(handle, t, 0) end end -function ctrl.createElement(class, arg) - return Tree() +-- backward compatibility +iup.TreeSetValue = iup.TreeAddNodes + +function ctrl.createElement(class, param) + return iup.Tree() end -iupRegisterWidget(ctrl) -iupSetClass(ctrl, "iup widget") +iup.RegisterWidget(ctrl) +iup.SetClass(ctrl, "iup widget") |