]> git.decadent.org.uk Git - ion3.git/blob - mod_menu/mod_menu.lua
[svn-inject] Installing original source of ion3
[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-2006.
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 l=reg:managed_list()
33         for i, r in pairs(l) do
34             if obj_is(r, "WMenu") then
35                 return
36             end
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 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 function menu_bigmenu(m, s, menu)
68       return mod_menu.do_menu(m, s, menu, {big=true, initial=initial})
69     end
70     return menu_(mplex, sub, menu_or_name, menu_bigmenu, true)
71 end
72
73 --DOC
74 -- This function is similar to \fnref{mod_menu.menu}, but input
75 -- is grabbed and the key used to active the menu can be used to
76 -- cycle through menu entries.
77 function mod_menu.grabmenu(mplex, sub, menu_or_name, param) 
78     local function menu_grabmenu(m, s, menu)
79         return mod_menu.do_grabmenu(m, s, menu, param)
80     end
81     return menu_(mplex, sub, menu_or_name, menu_grabmenu, true)
82 end
83
84 -- Compatibility
85 function mod_menu.biggrabmenu(mplex, sub, menu_or_name, key, initial) 
86     local function menu_biggrabmenu(m, s, menu)
87         return mod_menu.do_grabmenu(m, s, menu, true, key, initial or 0)
88     end
89     return menu_(mplex, sub, menu_or_name, menu_biggrabmenu, true, initial)
90 end
91
92 --DOC
93 -- This function displays a drop-down menu and should only
94 -- be called from a mouse press handler. The parameters are
95 -- similar to those of \fnref{mod_menu.menu}.
96 function mod_menu.pmenu(win, sub, menu_or_name) 
97     return menu_(win, sub, menu_or_name, mod_menu.do_pmenu)
98 end
99
100 -- }}}
101
102
103 -- Mark ourselves loaded.
104 package.loaded["mod_menu"]=true
105
106
107 -- Load configuration file
108 dopath('cfg_menu', true)