summaryrefslogtreecommitdiff
path: root/iup/srclua5/tree.lua
blob: bd8daf73b977deac463ecdd1d18a47f01bec1830 (plain)
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
------------------------------------------------------------------------------
-- Tree class 
------------------------------------------------------------------------------
local ctrl = {
  nick = "tree",
  parent = WIDGET,
  creation = "",
  callback = {
    selection_cb = "nn",
    multiselection_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
    rename_cb = "ns",
    showrename_cb = "n",
    rightclick_cb = "n",
    dragdrop_cb = "nnnn",
  },
  extrafuncs = 1,
}

local function TreeSetAttributeHandle(handle, name, value)
   if iupGetClass(value) == "iup handle" then value = ihandle_setname(value) end
   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
end

function TreeSetValueRec(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)
      end
    else
      if node then
        SetAttribute(handle, "ADDLEAF"..id, node)
      end
    end
    cont = cont - 1
   end
end

function TreeSetValue(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)
  end
  TreeSetValueRec(handle, t, id)
end

function ctrl.createElement(class, arg)
  return Tree()
end

iupRegisterWidget(ctrl)
iupSetClass(ctrl, "iup widget")