summaryrefslogtreecommitdiff
path: root/iupe-tview.lua
blob: 4048b563cbcb41d117c5175de5d5cbf08dbf06a7 (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
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
loadmodule "luaiup"

if not iupe then iupe = {} end
if not iupep then iupep = {} end

iupep.tableview = {
    build_branches_r = function (t, branch)
        for k, v in pairs(t) do
            local isstr, ks = pcall(function() return k .. "" end)
            if type(v) == "table" and isstr then
                local subbranch = { branchname = ks, userid = v }
                iupep.tableview.build_branches_r(v, subbranch)
                table.insert(branch, subbranch)
            end
        end
    end,
    
    updatetable = function (self, t, name)
        if type(t) ~= "table" then error "You need a table to display something in the tableview." end

        local ttreeview = self.ttreeview

        ttreeview.DELNODE0 = "CHILDREN"

        local branches = { branchname = name, userid = t }
        iupep.tableview.build_branches_r(t, branches)
        iup.TreeSetValue(ttreeview, branches)
        ttreeview.value = 0
        ttreeview:selection_cb(0, 1)
        self.table = t
        self.tablename = name
    end,
    
    selection_cb = function (self, id, status)
        if status == 0 then return end
        local val = iup.TreeGetUserId(self, id)
        local attributes = self.attributes
        attributes.numlin = #val
        attributes.numlin_visible = #val
        local l = 1
        local isstr, ks, vs
        for k, v in pairs(val) do
            isstr, ks = pcall(function() return k .. "" end)
            if not isstr then
                ks = "<" .. type(k) .. ">"
            end
            attributes:setcell(l, 1, ks)
            isstr, vs = pcall(function() return v .. "" end)
            if type(v) == "function" then
                vs = "--function(...) ... end"
            elseif not isstr then
                vs = "<" .. type(v) .. ">"
            end
            attributes:setcell(l, 2, vs)
            l = l + 1
        end
        attributes.redraw = "all"
    end,
    
    map_cb = function (self)
        iup.TreeSetValue(self, { branchname = "nil" })
    end,

    create = function (tab)
        local ttreeview = iup.tree {}
        local attributes = iup.matrix { readonly = "Yes", numcol = 2, numcol_visible = 2, resizematrix = "Yes", markmode = "Single" }
        ttreeview.selection_cb = iupep.tableview.selection_cb
        ttreeview.attributes = attributes
        ttreeview.map_cb = iupep.tableview.map_cb
        attributes:setcell(0, 1, "Name")
        attributes:setcell(0, 2, "Value")
        attributes.alignment1 = "ALEFT"
        attributes.alignment2 = "ALEFT"
        
        local refresh_btn = iup.button { title = "Refresh" }

        local r = iup.frame {
            iup.vbox {
                iup.hbox {
                    iup.sbox {
                        ttreeview,
                    },
                    iup.frame {
                        attributes,
                    }
                },
                iup.hbox {
                    iup.fill {},
                    refresh_btn,
                },
            },
            unpack(tab),
        }
        
        function refresh_btn:action()
            r:updatetable(r.table, r.tablename)
        end

        r.ttreeview = ttreeview
        r.updatetable = iupep.tableview.updatetable
        r.table = {}
        r.tablename = "nil"
        
        return r
    end,
}

iupe.tableview = iupep.tableview.create