Advertisement
draugath

Untitled

Jan 18th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. local function list3(params)
  2.     params = params or {}
  3.     params.control = 'YES'
  4.     --params.expand = 'YES'
  5.    
  6.     local controls, items = {}, {}
  7.     local colors, num_colors = {}, 0
  8.     local actioncb, createitemfunc, sortfunc
  9.  
  10.     local list = iup.stationsublist(params)
  11.  
  12.     local function create_subdlg(ctrl)
  13.         assert(type(ctrl) == 'userdata' and iup.GetType(ctrl):match('box'))
  14.         ctrl = ctrl or iup.hbox{}
  15.         local dlg = iup.dialog{
  16.             ctrl,
  17.             border = 'NO', menubox = 'NO', resize = 'NO', active = 'NO'
  18.         }
  19.         return dlg
  20.     end
  21.    
  22.     -- Custom methods
  23.     function list:AddItems(itemstbl)
  24.         assert(type(itemstbl) == 'table')
  25.         items = itemstbl
  26.     end
  27.  
  28.     function list:ClearItems()
  29.         items = {}
  30.     end
  31.    
  32.     function list:ClearList()
  33.         self[1] = nil -- why?
  34.         for i, subdlg in ipairs(controls) do
  35.             iup.Detach(subdlg)
  36.             iup.Destroy(subdlg)
  37.             controls[i] = nil
  38.         end
  39.     end
  40.    
  41.     function list:PopulateList()
  42.         self:ClearList()
  43.        
  44.         --if (sortfunc) then table.sort(controls, sortfunc) end
  45.  
  46.         for i, item in ipairs(items) do
  47.             if (createitemfunc) then item = createitemfunc(item) end
  48.  
  49.             local subdlg = create_subdlg(item)
  50.             subdlg:map()
  51.             table.insert(controls, subdlg)
  52.         end
  53.  
  54.         for i, subdlg in ipairs(controls) do
  55.             if (num_colors > 0) then subdlg.bgcolor = colors[(i % num_colors) + 1] end
  56.             iup.Append(self, subdlg)
  57.         end
  58.  
  59.         self:map()
  60.  
  61.         self[1] = 1 -- why?
  62.     end
  63.    
  64.     function list:SetActionCB(func)
  65.         -- Argument should be a function that will be executed after appropriate color changes have been applied
  66.         -- this function should receive one argument, the selected index
  67.         if (func) then assert(type(func) == 'function') end
  68.         actioncb = func
  69.     end
  70.    
  71.     function list:SetCreateItemFunc(func)
  72.         -- Function should receive an argument identifying the item and return a single box that will be put into a sub-dialog template
  73.         if (func) then assert(type(func) == 'function') end
  74.         createitemfunc = func
  75.     end
  76.    
  77.     function list:SetColors(colortbl)
  78.         -- Function should receive a table with numeric indexes. [0] will be used to indicate the color of the selected item.
  79.         assert(type(colortbl) == 'table')
  80.         if (not colortbl[0] and colortbl[1]) then
  81.             -- if no select color, invert the first color
  82.             local mask = bitlib.bnot(255)
  83.             local selcolorparts = {}
  84.             for v in colortbl[1]:gmatch('%S+') do
  85.                 local num = tonumber(v)
  86.                 if (v) then num = (bitlib.bnot(v) - mask) else num = v end
  87.                 table.insert(selcolorparts, num)
  88.             end
  89.             colortbl[0] = table.concat(selcolorparts, ' ')
  90.         end
  91.         num_colors = #colortbl
  92.         colors = colortbl
  93.         printtable(colors)
  94.         --test.print(num_colors)
  95.     end
  96.  
  97.     function list:SetSelected(index)
  98.         if (index) then
  99.             assert(type(index) == 'number')
  100.             self:action(nil, index, 1)
  101.         end
  102.         if (self.vo_listsel) then self:action(nil, tonumber(self.vo_listsel), 0) end
  103.         self.vo_listsel = index
  104.     end
  105.    
  106. --  function list:SetSortFunc(func)
  107. --      if (func) then assert(type(func) == 'function') end
  108. --      sortfunc = func
  109. --  end
  110.  
  111.     -- Callbacks
  112.     function list:action(text, index, state)
  113.         --test.varprint(' / ', 'text', text, 'index', index, 'state', state, 'vo_listsel', self.vo_listsel)
  114.         if (state == 1) then
  115.             if (index ~= tonumber(self.vo_listsel)) then
  116.                 controls[index].bgcolor = colors[0]
  117.                 self.selected = index
  118.                 if (actioncb) then return actioncb(index) end
  119.             else
  120.                 self.selected = nil
  121.             end
  122.  
  123.         else
  124.             controls[index].bgcolor = colors[(index % num_colors) + 1]
  125.  
  126.         end
  127.     end
  128.    
  129.     function list:multiselect_cb(value)
  130.         --test.varprint('', 'value', value)
  131.     end
  132.    
  133.     function list:edit_cb(c, after)
  134.         --test.varprint(' / ', 'char_code', c, 'after', after)
  135.     end
  136.    
  137.     function list:caret_cb(row, col)
  138.         --test.varprint(' / ', 'row', row, 'col', col)
  139.     end
  140.    
  141.     return list
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement