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