2 -- ion/mod_menu/mod_menu.lua -- Menu opening helper routines.
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
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.
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
17 if not ioncore.load_module("mod_menu") then
21 local mod_menu=_G["mod_menu"]
22 local menudb=_G["ioncore"]
24 assert(mod_menu and menudb)
29 local function menu_(reg, sub, menu_or_name, fn, check)
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")
40 menu=menudb.evalmenu(menu_or_name, reg, sub)
42 return fn(reg, function(e) e.func(reg, sub) end, menu)
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))
62 return menu_(mplex, sub, menu_or_name, menu_stdmenu, true)
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)
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)
79 return menu_(mplex, sub, menu_or_name, menu_grabmenu, true)
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)
87 return menu_(mplex, sub, menu_or_name, menu_biggrabmenu, true, initial)
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)
101 -- Mark ourselves loaded.
102 package.loaded["mod_menu"]=true
105 -- Load configuration file
106 dopath('cfg_menu', true)