summaryrefslogtreecommitdiff
path: root/iup/srclua5/menu.lua
blob: 750494ff15b3933bf082d98ba6c36175c9e330ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
------------------------------------------------------------------------------
-- Menu class 
------------------------------------------------------------------------------
local ctrl = {
  nick = "menu",
  parent = iup.BOX,  -- iup.Append will be automatically called after createElement
  creation = "-",
  callback = {
    open_cb = "",
    menuclose_cb = "",
  }
}

function ctrl.popup(handle, x, y)
  iup.Popup(handle, x, y)
end

function ctrl.append(handle, elem)
  iup.Append(handle, elem)
end

function ctrl.getargs(menu_param)
  local itemarg = {}
  -- copy the named attributes to the element parameters
  for u,v in pairs(menu_param) do
    if type(u) ~= "number" then
      itemarg[u] = v
    end
  end
  return itemarg
end

function ctrl.createElement(class, param)
  local n = #param
  for i=1,n do
    local menu_param = param[i]
    if type(menu_param) == "table" then 
      -- replace param[i], so it will be used by iup.Append after createElement
      -- other elements already created can also be used
      if type(menu_param[1]) == nil then
        param[i] = iup.separator()
      elseif type(menu_param[1]) == "string" then
        local itemarg = ctrl.getargs(menu_param)
        if type(menu_param[2]) == "userdata" then
          itemarg[1] = menu_param[2]
          itemarg.title = menu_param[1]
          param[i] = iup.submenu(itemarg)
        else
          itemarg.title = menu_param[1]
          itemarg.action = menu_param[2]
          param[i] = iup.item(itemarg)
        end
      end
    end
  end
  return iup.Menu()
end

function ctrl.showxy(handle, x, y)
  return iup.ShowXY(handle, x, y)
end

function ctrl.destroy(handle)
  return iup.Destroy(handle)
end

iup.RegisterWidget(ctrl)
iup.SetClass(ctrl, "iup widget")