Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function list3(params)
- params = params or {}
- params.control = 'YES'
- --params.expand = 'YES'
- local controls, items = {}, {}
- local colors, num_colors = {}, 0
- local actioncb, createitemfunc, sortfunc
- local list = iup.stationsublist(params)
- local function create_subdlg(ctrl)
- assert(type(ctrl) == 'userdata' and iup.GetType(ctrl):match('box'))
- ctrl = ctrl or iup.hbox{}
- local dlg = iup.dialog{
- ctrl,
- border = 'NO', menubox = 'NO', resize = 'NO', active = 'NO'
- }
- return dlg
- end
- -- Custom methods
- function list:AddItems(itemstbl)
- assert(type(itemstbl) == 'table')
- items = itemstbl
- end
- function list:ClearItems()
- items = {}
- end
- function list:ClearList()
- self[1] = nil -- why?
- for i, subdlg in ipairs(controls) do
- iup.Detach(subdlg)
- iup.Destroy(subdlg)
- controls[i] = nil
- end
- end
- function list:PopulateList()
- self:ClearList()
- --if (sortfunc) then table.sort(controls, sortfunc) end
- for i, item in ipairs(items) do
- if (createitemfunc) then item = createitemfunc(item) end
- local subdlg = create_subdlg(item)
- subdlg:map()
- table.insert(controls, subdlg)
- end
- for i, subdlg in ipairs(controls) do
- if (num_colors > 0) then subdlg.bgcolor = colors[(i % num_colors) + 1] end
- iup.Append(self, subdlg)
- end
- self:map()
- self[1] = 1 -- why?
- end
- function list:SetActionCB(func)
- -- Argument should be a function that will be executed after appropriate color changes have been applied
- -- this function should receive one argument, the selected index
- if (func) then assert(type(func) == 'function') end
- actioncb = func
- end
- function list:SetCreateItemFunc(func)
- -- Function should receive an argument identifying the item and return a single box that will be put into a sub-dialog template
- if (func) then assert(type(func) == 'function') end
- createitemfunc = func
- end
- function list:SetColors(colortbl)
- -- Function should receive a table with numeric indexes. [0] will be used to indicate the color of the selected item.
- assert(type(colortbl) == 'table')
- if (not colortbl[0] and colortbl[1]) then
- -- if no select color, invert the first color
- local mask = bitlib.bnot(255)
- local selcolorparts = {}
- for v in colortbl[1]:gmatch('%S+') do
- local num = tonumber(v)
- if (v) then num = (bitlib.bnot(v) - mask) else num = v end
- table.insert(selcolorparts, num)
- end
- colortbl[0] = table.concat(selcolorparts, ' ')
- end
- num_colors = #colortbl
- colors = colortbl
- printtable(colors)
- --test.print(num_colors)
- end
- function list:SetSelected(index)
- if (index) then
- assert(type(index) == 'number')
- self:action(nil, index, 1)
- end
- if (self.vo_listsel) then self:action(nil, tonumber(self.vo_listsel), 0) end
- self.vo_listsel = index
- end
- -- function list:SetSortFunc(func)
- -- if (func) then assert(type(func) == 'function') end
- -- sortfunc = func
- -- end
- -- Callbacks
- function list:action(text, index, state)
- --test.varprint(' / ', 'text', text, 'index', index, 'state', state, 'vo_listsel', self.vo_listsel)
- if (state == 1) then
- if (index ~= tonumber(self.vo_listsel)) then
- controls[index].bgcolor = colors[0]
- self.selected = index
- if (actioncb) then return actioncb(index) end
- else
- self.selected = nil
- end
- else
- controls[index].bgcolor = colors[(index % num_colors) + 1]
- end
- end
- function list:multiselect_cb(value)
- --test.varprint('', 'value', value)
- end
- function list:edit_cb(c, after)
- --test.varprint(' / ', 'char_code', c, 'after', after)
- end
- function list:caret_cb(row, col)
- --test.varprint(' / ', 'row', row, 'col', col)
- end
- return list
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement