I figured out the problem of why the nodes of my tree were appearing
in reverse order. It has to do with how addleaf, insertleaf,
addbranch, insertbranch work. If adding just leaves with one For loop,
the fix is simple because you just need to add tree.value = i - 1 to
the loop in order to have the previous node be selected instead of the
current one before the next leaf is created:
require("iuplua")
local tree = iup.flattree{rastersize="400x500"}
tree["addbranch-1"] = "COLORS"
local tlist =
{"Black","Grey","White","Red","Orange","Yellow","Green","Blue","Purple","Pink","Brown"}
for i,v in ipairs(tlist) do
tree.value = i - 1
tree.addleaf = v
end
local testdlg = iup.dialog{iup.vbox{tree; ncmargin = "10 x 10"}; title
= "Test Window"}
testdlg:showxy(iup.CENTER, iup.CENTER)
if (iup.MainLoopLevel()==0) then
iup.MainLoop()
end
Things get more complicated with adding branches. I wanted my branches
all at the same depth, but addbranch kept adding +1 depth. I couldn't
make the "insert" commands work either with the nested loops in the
actual code giving me problems. I had to ditch those commands and
create a table of tables to use TreeAddNodes on.
This is what the code looks like now, and what it does is take info
about an app's modules and the category names that they're grouped
under, alphabetizes the categories then module names, arranges them in
a tree, and when the user clicks on a module name, it executes a
function in that module's Lua file to make its dialog appear:
-- MODULE TREE -------------------------------------------------------------
local modtree = iup.flattree{rastersize = "400x500"}
modtree["addbranch-1"] = "MODULES" --unlike iup.tree, add root node
for iup.flattree like this
modtree:SetNodeAttributes(0,{titlefontstyle = "Bold"}) --make root node bold
modtree.addexpanded = "YES"
local ay_cats = {} --array to alphabetize categories
for cat in pairs(cats) do --iterate thru all mod categories
table.insert(ay_cats, cat) --assign each category to the array
end
table.sort(ay_cats, function(a, b) return a:lower() < b:lower() end)
--alphabetize categories (which are strings)
--Using add/insert branch/leaf commands in these nested loops will
give the wrong depth/layout,
--so a table of tables must be built (treebuild) and TreeAddNodes
called after.
local treebuild = {}
local ay_modtree = {} --array to collect alphabetized
categories/mods in same order as they should appear in tree
for _,v in ipairs(ay_cats) do --iterate thru alphabetized category array
local catmods = {branchname = v} --adds a branch with category name
table.insert(ay_modtree, v) --a placeholder so leaves will
have indices that match their node IDs
local sorter = {} --temporary array for sorting mods
for _,module in pairs(cats[v]) do --iterate thru each
category, which is a key to a "module" table
table.insert(sorter,module) --add module (a table) to temp table
end
table.sort(sorter, function(mod1, mod2) return
mod1.label:lower() < mod2.label:lower() end) --alphabetize mods by
module.label strings
for _,val in ipairs(sorter) do --iterate thru alphabetized mods
table.insert(catmods, val.label) --add leaf with mod's name
table.insert(ay_modtree, val) --put alphabetized mods into
tree table
function modtree:executeleaf_cb(id) --callback for
clicking on leaf. Gives the leaf's id.
--Unlike the original button.action cb, attempting to
assign a different function to each leaf didn't work since a node is
part of
--the tree, not an independent object. As a
workaround, I had to list the categories/mods in an array mirroring
their node order so
--each leaf id (the default is a # corresponding to
its order in the tree) matches an array index containing its mod's
function.
ay_modtree[id].bindsettings(t) --use leaf's id as
index to call its module.bindsettings function from array
end
end
table.insert(treebuild,catmods)
end
iup.TreeAddNodes(modtree, treebuild)
_______________________________________________
Iup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/iup-users