Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Originally written by slime (c) 2007
- -- Heavily modified by draugath (c) 2010.
- test.table_viewer = {}
- test.table_viewer.ui = {}
- local tv = test.table_viewer
- tv.failed = false
- tv.history_size = gkini.ReadInt('DevKit', 'tableviewer.history_size', 10)
- tv.history = unspickle(gkini.ReadString('DevKit', 'tableviewer.history', '{}'))
- tv.treetable = {}
- tv.table_ref = {}
- tv.breadcrumbs = {}
- tv.breadcrumbs.list = {}
- local ui = tv.ui
- ui.dialog = {}
- local function build_table_name(tbl)
- tbl = tbl or {}
- local table_ref = tv.table_ref
- local tree = ui.tree
- local curid = tonumber(tree.curid)
- local function strip_desc(id)
- local name, str = tree['NAME'..id]
- if (type(table_ref[id]) == 'table') then
- return (name:gsub('%(.*%)%s', ''))
- elseif (ui.readiup.value == 'ON' and type(table_ref[id]) == 'userdata') then
- str = name:gsub('%(.*%)%s', '') or name:gsub(':.*', '')
- if (str == name) then return nil else return str end
- else
- return (name:gsub(':.*', ''))
- end
- end
- while (curid > 0) do
- table.insert(tbl, 1, strip_desc(curid))
- if (tbl[1] == nil) then return end
- curid = tonumber(tree['PARENT'..curid])
- end
- for i, v in ipairs(test.splittable(tree['NAME0'])) do
- table.insert(tbl, i, v)
- end
- return test.jointable(tbl)
- end
- function tv.breadcrumbs:New(name, path)
- local button = iup.stationbutton{title=name, path=path}
- function button:action()
- ui.textbox.value = self.path
- ui.search:action()
- end
- return button
- end
- function tv.ConvertTable(tbl_string)
- local string_good, tbl = test.exists(tbl_string)
- if (not string_good or not type(tbl)) then tv.failed = true; return false end
- tv.treetable = {}
- tv.table_ref = {}
- local function iup_recurse(in_control, insert_table)
- local controls = {}
- local cur_elem = in_control
- if (cur_elem == ui.breadcrumbbox_inner_contents) then return end
- repeat
- table.insert(controls, cur_elem)
- cur_elem = iup.GetBrother(cur_elem)
- until (not cur_elem)
- for _,v in ipairs(controls) do
- local iupType = iup.GetType(v)
- table.insert(tv.table_ref, v)
- if (test.IsIupContainer(v) or (iupType == 'list' and v['control'] == 'YES')) then
- table.insert(insert_table, {branchname=iupType})
- cur_elem = iup.GetNextChild(v)
- if (iupType == 'list') then test.print('list.nextchild='..tostring(pcall(iup.GetType, cur_elem))) end
- if (cur_elem) then iup_recurse(cur_elem, insert_table[#insert_table]) end
- else
- table.insert(insert_table, iupType)
- end
- end
- end
- local function recurse(intable, insert_table)
- if (intable == tv.treetable or intable == tv.table_ref) then return end
- local keys = {}
- if (ui.readiup.value == 'ON' and
- #tv.treetable == 0 and
- type(intable) == 'userdata') then
- iup_recurse(intable, insert_table)
- elseif (type(intable) == 'table') then
- for k in pairs(intable) do table.insert(keys, k) end
- table.sort(keys, function(a,b) return gkmisc.strnatcasecmp(a,b) < 0 end)
- for _,v in ipairs(keys) do
- if (ui[type(intable[v])].value == 'ON') then
- table.insert(tv.table_ref, intable[v])
- if type(intable[v]) == "table" then
- if (type(v) == 'number') then
- table.insert(insert_table, {branchname='(table) ['..tostring(v)..']'})
- else
- table.insert(insert_table, {branchname='(table) '..tostring(v)})
- end
- recurse(intable[v], insert_table[#insert_table])
- elseif (ui.readiup.value == 'ON' and
- type(intable[v]) == 'userdata' and
- test.IsIupControl(intable[v])) then
- table.insert(insert_table, {branchname="(iup."..iup.GetType(intable[v])..") "..tostring(v)})
- iup_recurse(intable[v], insert_table[#insert_table])
- else
- local val = intable[v]
- local str
- if type(val) == "string" then val = string.format("%q", val) else val = tostring(val) end
- if (type(v) == 'number') then
- str = '['..tostring(v)..']: '..val
- else
- str = tostring(v)..": "..val
- end
- table.insert(insert_table, str)
- end
- end
- end
- else
- local val = intable
- if type(val) == "string" then val = string.format("%q", val) else val = tostring(val) end
- local str = val
- table.insert(insert_table, str)
- end
- end
- recurse(tbl, tv.treetable)
- tv.treetable.branchname = tbl_string
- return true
- end
- function tv.breadcrumbs.create(tbl_string)
- local ibox = ui.breadcrumbbox_inner_contents
- if (tv.breadcrumbs.list[1] == nil) then
- tv.breadcrumbs.list = test.splittable(ui.textbox.value)
- end
- local bc = tv.breadcrumbs.list
- iup.Detach(ibox)
- iup.Destroy(ibox)
- ibox = iup.hbox{gap = 5,}
- for i = 1, (#bc - 1) do
- iup.Append(ibox, tv.breadcrumbs:New(bc[i], test.jointable(bc, 1, i)))
- end
- iup.Append(ui.breadcrumbbox_inner, ibox)
- ui.breadcrumbbox_inner_contents = ibox
- ui.breadcrumbbox_inner:map()
- iup.Refresh(ui.breadcrumbbox_inner)
- end
- -- Top line controls --------
- ui.entry, ui.textbox, ui.dropdown = test.list2{expand='HORIZONTAL', list = tv.history}
- ui.readiup = iup.stationtoggle{title = 'Read IUP', value = 'OFF', action = function(self)
- ui.search.action()
- end
- }
- ui.search = iup.stationbutton{title='Go', size=120, action=function(self, fromOpen)
- local tbl
- -- if (tv.failed) then
- -- tbl = ui.tree["NAME0"]
- -- else
- tbl = ui.textbox.value
- -- end
- if tbl == '' then return end
- if (fromOpen == nil) then tv.breadcrumbs.list = {} end
- if (test.disable_iup_read[tbl]) then ui.readiup.value = 'OFF' end
- if (tv.ConvertTable(tbl)) then
- ui.tree.value = 'ROOT'
- ui.tree.delnode = 'CHILDREN'
- iup.TreeSetValue(ui.tree, tv.treetable)
- ui.tree.redraw = 'YES'
- tv.breadcrumbs.create(tbl)
- tv.failed = false
- local inHistory = false
- for _, v in ipairs(tv.history) do if (v == tbl) then inHistory = true; break end end
- if (not inHistory) then
- table.insert(tv.history, 1, tbl)
- local size = tv.history_size
- if (#tv.history == (size + 1)) then tv.history[size + 1] = nil end
- do
- local h = {}
- for i = 1, size do h[i] = tv.history[i] end
- gkini.WriteString('DevKit', 'tableviewer.history', spickle(h))
- end
- end
- for i, v in ipairs(tv.history) do ui.dropdown[i] = v end
- end
- end
- }
- -- --------------------------
- -- Filter Box definition ----
- ui.filterbox = iup.hbox{gap = 10, iup.label{title = 'Filters:'}}
- for _, v in ipairs({'boolean', 'number', 'string', 'function', 'userdata', 'thread', 'table'}) do
- ui[v] = iup.stationtoggle{title = v, value = 'on', action = function(self)
- ui.search.action()
- end}
- iup.Append(ui.filterbox, ui[v])
- end
- -- --------------------------
- -- Breadcrumb Box definition
- ui.open = iup.stationbutton{title = 'Open', active = 'NO', action = function(self)
- tv.breadcrumbs.list = {}
- local bc = tv.breadcrumbs.list
- local str = build_table_name()
- if (test.dont_read[str]) then return end
- ui.textbox.value = str
- ui.search:action(true)
- end
- }
- ui.breadcrumbbox_inner_contents = iup.hbox{gap = 5,}
- ui.breadcrumbbox_inner = iup.hbox{ui.breadcrumbbox_inner_contents}
- -- --------------------------
- -- Other controls
- ui.selected = iup.label{title = '', expand = 'HORIZONTAL'}
- ui.multiline = iup.stationsubmultiline{size = '350', expand = 'yes', readonly = 'yes'}
- ui.tree = iup.stationsubsubtree{expand='YES', addexpanded='NO', renamenode_cb=function(self, id, name)
- if self['KIND'..id] == 'BRANCH' then
- local state = self['STATE'..id]
- if state == 'EXPANDED' then
- self:setstate(id, 'COLLAPSED')
- else
- self:setstate(id, 'EXPANDED')
- end
- end
- end, selection_cb = function(self, id, status)
- if (ui.textbox.value ~= nil and ui.textbox.value == '') then return iup.IGNORE end
- local tbl, txt
- if (id == 0) then
- ui.open.active = 'NO'
- else
- ui.open.active = 'YES'
- tbl = tv.table_ref[id]
- end
- ui.tree.curid = id -- type(id) == 'number', but it is getting converted to 'string' on assignment
- if (ui.readiup.value == 'ON' and test.IsIupControl(tbl)) then
- txt = 'Name='..test.nz(iup.GetName(tbl)).."\n"..string.gsub(iup.GetAttributes(tbl),",", "\n")
- else
- txt = tostring(tbl)
- end
- ui.multiline.value = txt
- ui.selected.title = build_table_name()
- end
- }
- ui.options = iup.stationbutton{title='Options', size = 100, action = function(self)
- local opt = tv.options
- opt.build_dialog()
- opt.ui.dialog:popup(iup.CENTER, iup.CENTER)
- opt.ui.dialog:destroy()
- end}
- ui.close = iup.stationbutton{title='Close', size = 100, action = function(self)
- HideDialog(ui.dialog)
- end
- }
- -- -----------------------------
- function tv.build_dialog()
- ui.main = iup.pdarootframe{
- iup.pdarootframebg{
- iup.vbox{
- iup.hbox{
- iup.label{title='Enter A Table:'},
- ui.entry,
- ui.readiup,
- ui.search,
- gap=5, alignment='ACENTER',
- },
- ui.filterbox,
- iup.hbox{
- ui.breadcrumbbox_inner,
- iup.fill{},
- ui.open,
- },
- iup.hbox{iup.label{title = 'Currently selected: '}, ui.selected},
- iup.hbox{
- ui.tree,
- ui.multiline,
- gap=5,
- },
- iup.hbox{iup.fill{expand = 'YES'}, ui.options, ui.close, gap = 5},
- gap=5,
- },
- },
- size="TWOTHIRDxTWOTHIRD", expand="NO",
- }
- ui.dialog = iup.dialog{
- iup.vbox{
- iup.fill{},
- iup.hbox{
- iup.fill{},
- ui.main,
- iup.fill{},
- },
- iup.fill{},
- },
- defaultesc=ui.close,
- defaultenter=ui.search,
- bgcolor="0 0 0 128 *",
- -- fullscreen="YES",
- title = 'Table Viewer [v'..test.version..']',
- border="NO",
- resize="NO",
- menubox="YES",
- topmost="YES",
- }
- function ui.dialog:Show(str)
- local autoset = false
- if (str ~= nil and type(str) == 'string') then ui.textbox.value = str; autoset = true end
- ShowDialog(self, iup.CENTER, iup.CENTER)
- if (autoset) then ui.search:action() end
- end
- ui.dialog:map()
- end
- dofile('table_viewer_options.lua')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement