]> git.decadent.org.uk Git - ion3.git/blob - mod_menu/mod_menu.lua
Imported Upstream version 20090110
[ion3.git] / mod_menu / mod_menu.lua
1 --
2 -- ion/mod_menu/mod_menu.lua -- Menu opening helper routines.
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2009.
5 --
6 -- See the included file LICENSE for details.
7 --
8
9 -- This is a slight abuse of the package.loaded variable perhaps, but
10 -- library-like packages should handle checking if they're loaded instead of
11 -- confusing the user with require/include differences.
12 if package.loaded["mod_menu"] then return end
13
14 if not ioncore.load_module("mod_menu") then
15     return
16 end
17
18 local mod_menu=_G["mod_menu"]
19 local menudb=_G["ioncore"]
20
21 assert(mod_menu and menudb)
22
23
24 -- Menu commands {{{
25
26 local function menu_(reg, sub, menu_or_name, fn, check)
27     if check then
28         -- Check that no other menus are open in reg.
29         local ok=reg:managed_i(function(r) 
30                                    return not obj_is(r, "WMenu") 
31                                end)
32         if not ok then
33             return
34         end
35     end
36
37     menu=menudb.evalmenu(menu_or_name, reg, sub)
38     
39     return fn(reg, function(e) e.func(reg, sub) end, menu)
40 end
41
42
43 --DOC
44 -- Display a menu in the lower-left corner of \var{mplex}.
45 -- The variable \var{menu_or_name} is either the name of a menu
46 -- defined with \fnref{mod_menu.defmenu} or directly a table similar
47 -- to ones passesd to this function. When this function is
48 -- called from a binding handler, \var{sub} should be set to
49 -- the second argument of to the binding handler (\var{_sub})
50 -- so that the menu handler will get the same parameters as the
51 -- binding handler. Extra options can be passed in the table
52 -- \var{param}. The initial entry can be specified as the field
53 -- \var{initial} as an integer starting from 1. Menus can be made
54 -- to use a bigger style by setting the field \var{big} to \code{true}.
55 function mod_menu.menu(mplex, sub, menu_or_name, param) 
56    local function menu_stdmenu(m, s, menu)
57         return ioncore.unsqueeze(mod_menu.do_menu(m, s, menu, param))
58    end
59    return menu_(mplex, sub, menu_or_name, menu_stdmenu, true)
60 end
61
62 -- Compatibility
63 function mod_menu.bigmenu(mplex, sub, menu_or_name, initial) 
64     local param={big=true, initial=initial}
65     return mod_menu.menu(mplex, sub, menu_or_name, param)
66 end
67
68 --DOC
69 -- This function is similar to \fnref{mod_menu.menu}, but input
70 -- is grabbed and the key used to active the menu can be used to
71 -- cycle through menu entries.
72 function mod_menu.grabmenu(mplex, sub, menu_or_name, param) 
73     local function menu_grabmenu(m, s, menu)
74         return mod_menu.do_grabmenu(m, s, menu, param)
75     end
76     return menu_(mplex, sub, menu_or_name, menu_grabmenu, true)
77 end
78
79 -- Compatibility
80 function mod_menu.biggrabmenu(mplex, sub, menu_or_name, key, initial) 
81     local function menu_biggrabmenu(m, s, menu)
82         return mod_menu.do_grabmenu(m, s, menu, true, key, initial or 0)
83     end
84     return menu_(mplex, sub, menu_or_name, menu_biggrabmenu, true, initial)
85 end
86
87 --DOC
88 -- This function displays a drop-down menu and should only
89 -- be called from a mouse press handler. The parameters are
90 -- similar to those of \fnref{mod_menu.menu}.
91 function mod_menu.pmenu(win, sub, menu_or_name) 
92     return menu_(win, sub, menu_or_name, mod_menu.do_pmenu)
93 end
94
95 -- }}}
96
97
98 -- Mark ourselves loaded.
99 package.loaded["mod_menu"]=true
100
101
102 -- Load configuration file
103 dopath('cfg_menu', true)