Murga-Projects Forums

Full Version: collapsing browser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There has again been a slight lull in postings here (seems to jump when new murgaLua versions are released and subside a couple weeks later), and I'm too excited about this new discovery, so I'm gonna post. This script will be included in the next release of the widgets demo. It demonstrates a way to collapse and expand sections of a browser widget.

-- b = browser name, included to make it easier
-- to plug this function into other scripts.
function toggle(b)
local count,state,one,two=1
-- selected line and its label
local me,label=b:value(),b:text(b:value())
if string.find(label,"%+%s*") then
  state,one,two="expanded","%+  ","%-  "
  state,one,two="collapsed","%-  ","%+  "
b:text(me,string.gsub(label,one,two)) -- swap + and -
-- optional output; cut off sep and the following + or -
o:value("You "..state.." the "..string.gsub(label,sep..".%s*",""))
-- toggle all lines until reaching the next section or the browser end
while b:text(me+count) and not string.match(b:text(me+count),sep) do
  if state=="collapsed" then b:hide(me+count)
  else b:show(me+count)

ww=500; wh=200; f=10 -- window width, height, border
w=fltk:Fl_Window(ww,wh,"collapsible browser")
h=fltk:Fl_Box(ww/2+f,f,ww/2-f*2,wh/2-f*2) -- help
h:align(149) -- align text inside top left
h:label([[Click the titles or press "Enter" to expand or collapse a section.

Press Alt+number to jump to the corresponding section.]])

function smove(me)
  local n=string.sub(me:label(),2) -- strip off "&"
  list:value(cat[tonumber(n)]) -- select the separator with index n
  local label=list:text(list:value()) -- get the text of selected line
  o:value("You jumped to the "..string.gsub(label,sep..".%s*",""))
sbox=fltk:Fl_Box(ww/2+f,wh-f*8,f*3,f*2,"Move to section:")
sbox:align(20) -- inside left
-- buttons used to select section headers
s1=fltk:Fl_Button(ww-f*10,wh-f*8,f*3,f*2,"&1"); s1:callback(smove)
s2=fltk:Fl_Button(ww-f*7,wh-f*8,f*3,f*2,"&2"); s2:callback(smove)
s3=fltk:Fl_Button(ww-f*4,wh-f*8,f*3,f*2,"&3"); s3:callback(smove)
o=fltk:Fl_Output(ww/2+f,wh-f*4,ww/2-f*2,f*3) -- output

-- separator/subsection, prefix for the separator labels
-- format: bold text, gray background, underlined
t="    item:  " -- indentation

-- table to store the index of each separator

-- contents of sections: label, item1, item2, ...
{sep.."-  first section","one-A","one-B","one-C"},
{sep.."-  second section","two A","to be","two C","two D"},
{sep.."-  third section","three-A","three-B","three-C","3-D","three-E","three-F"}

for k,v in ipairs(categories) do -- do each sub-table
  for i,s in ipairs(v) do
    if string.find(s,sep) then list:add(s); cat[k]=list:size() -- find the index of the separator
    else list:add(t..s) end -- add the string to the browser with optional indentation
-- to start all collapsed, using the cat table to select the right lines
for k in ipairs(cat) do list:value(cat[k]); toggle(list) end
-- to start a specific section collapsed
--list:value(cat[3]); toggle(list)
o:value("squishy browser demo")

list:callback( function()
if list:value()>0 then -- clicking a blank space can be fatal without this check
if Fl:event() == fltk.FL_RELEASE or Fl:event_key()==fltk.FL_Enter then -- on click or Enter
  if string.find(list:text(list:value()),sep) then toggle(list) -- if it's a separator
  else o:value([[You poked "]]..string.gsub(list:text(list:value()),t,"")..[["]]) -- print label to output, remove the indent
-- offscreen button that allows the list to respond to Enter key
enter:callback(function() list:do_callback() end)


Very nice.

I'll see if I can hack folders and files into such a list.
Then figure out how to get our documentation into one too.

Reference URL's