0
|
1 /* Implements an elisp-programmable menubar.
|
|
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* #### There ain't much here because menubars have not been
|
|
25 properly abstracted yet. */
|
|
26
|
|
27 #include <config.h>
|
|
28 #include "lisp.h"
|
|
29
|
195
|
30 #include "buffer.h"
|
0
|
31 #include "device.h"
|
|
32 #include "frame.h"
|
|
33 #include "menubar.h"
|
|
34 #include "redisplay.h"
|
|
35 #include "window.h"
|
|
36
|
|
37 int menubar_show_keybindings;
|
|
38 Lisp_Object Vmenubar_configuration;
|
|
39
|
|
40 Lisp_Object Qcurrent_menubar;
|
|
41
|
|
42 Lisp_Object Qactivate_menubar_hook, Vactivate_menubar_hook;
|
|
43
|
|
44 Lisp_Object Vmenubar_visible_p;
|
|
45
|
|
46 static Lisp_Object Vcurrent_menubar; /* DO NOT ever reference this.
|
|
47 Always go through Qcurrent_menubar.
|
|
48 See below. */
|
175
|
49
|
0
|
50 Lisp_Object Vblank_menubar;
|
|
51
|
|
52 int popup_menu_titles;
|
|
53
|
|
54 Lisp_Object Vmenubar_pointer_glyph;
|
|
55
|
|
56 static int
|
|
57 menubar_variable_changed (Lisp_Object sym, Lisp_Object *val,
|
|
58 Lisp_Object in_object, int flags)
|
|
59 {
|
|
60 MARK_MENUBAR_CHANGED;
|
|
61 return 0;
|
|
62 }
|
|
63
|
|
64 void
|
|
65 update_frame_menubars (struct frame *f)
|
|
66 {
|
|
67 if (f->menubar_changed || f->windows_changed)
|
|
68 MAYBE_FRAMEMETH (f, update_frame_menubars, (f));
|
|
69
|
|
70 f->menubar_changed = 0;
|
|
71 }
|
|
72
|
|
73 void
|
|
74 free_frame_menubars (struct frame *f)
|
|
75 {
|
|
76 /* If we had directly allocated any memory for the menubars instead
|
|
77 of using all Lisp_Objects this is where we would now free it. */
|
|
78
|
|
79 MAYBE_FRAMEMETH (f, free_frame_menubars, (f));
|
|
80 }
|
|
81
|
|
82 static void
|
|
83 menubar_visible_p_changed (Lisp_Object specifier, struct window *w,
|
|
84 Lisp_Object oldval)
|
|
85 {
|
|
86 MARK_MENUBAR_CHANGED;
|
|
87 }
|
|
88
|
|
89 static void
|
|
90 menubar_visible_p_changed_in_frame (Lisp_Object specifier, struct frame *f,
|
|
91 Lisp_Object oldval)
|
|
92 {
|
|
93 update_frame_menubars (f);
|
|
94 }
|
|
95
|
20
|
96 DEFUN ("popup-menu", Fpopup_menu, 1, 2, 0, /*
|
0
|
97 Pop up the given menu.
|
|
98 A menu description is a list of menu items, strings, and submenus.
|
|
99
|
|
100 The first element of a menu must be a string, which is the name of the menu.
|
|
101 This is the string that will be displayed in the parent menu, if any. For
|
|
102 toplevel menus, it is ignored. This string is not displayed in the menu
|
|
103 itself.
|
|
104
|
|
105 If an element of a menu is a string, then that string will be presented in
|
|
106 the menu as unselectable text.
|
|
107
|
|
108 If an element of a menu is a string consisting solely of hyphens, then that
|
|
109 item will be presented as a solid horizontal line.
|
|
110
|
|
111 If an element of a menu is a list, it is treated as a submenu. The name of
|
|
112 that submenu (the first element in the list) will be used as the name of the
|
|
113 item representing this menu on the parent.
|
|
114
|
|
115 Otherwise, the element must be a vector, which describes a menu item.
|
|
116 A menu item can have any of the following forms:
|
|
117
|
185
|
118 [ "name" callback <active-p> ]
|
|
119 [ "name" callback <active-p> "suffix" ]
|
|
120 [ "name" callback :<keyword> <value> :<keyword> <value> ... ]
|
0
|
121
|
|
122 The name is the string to display on the menu; it is filtered through the
|
|
123 resource database, so it is possible for resources to override what string
|
|
124 is actually displayed.
|
|
125
|
|
126 If the `callback' of a menu item is a symbol, then it must name a command.
|
|
127 It will be invoked with `call-interactively'. If it is a list, then it is
|
|
128 evaluated with `eval'.
|
|
129
|
|
130 The possible keywords are this:
|
|
131
|
|
132 :active <form> Same as <active-p> in the first two forms: the
|
|
133 expression is evaluated just before the menu is
|
|
134 displayed, and the menu will be selectable only if
|
|
135 the result is non-nil.
|
|
136
|
185
|
137 :suffix "string" Same as "suffix" in the second form: the suffix is
|
0
|
138 appended to the displayed name, providing a convenient
|
|
139 way of adding the name of a command's ``argument'' to
|
|
140 the menu, like ``Kill Buffer NAME''.
|
|
141
|
185
|
142 :keys "string" Normally, the keyboard equivalents of commands in
|
0
|
143 menus are displayed when the `callback' is a symbol.
|
|
144 This can be used to specify keys for more complex menu
|
|
145 items. It is passed through `substitute-command-keys'
|
|
146 first.
|
|
147
|
|
148 :style <style> Specifies what kind of object this menu item is:
|
|
149
|
|
150 nil A normal menu item.
|
|
151 toggle A toggle button.
|
|
152 radio A radio button.
|
|
153
|
|
154 The only difference between toggle and radio buttons is
|
|
155 how they are displayed. But for consistency, a toggle
|
|
156 button should be used when there is one option whose
|
|
157 value can be turned on or off, and radio buttons should
|
|
158 be used when there is a set of mutually exclusive
|
|
159 options. When using a group of radio buttons, you
|
|
160 should arrange for no more than one to be marked as
|
|
161 selected at a time.
|
|
162
|
|
163 :selected <form> Meaningful only when STYLE is `toggle' or `radio'.
|
|
164 This specifies whether the button will be in the
|
|
165 selected or unselected state.
|
|
166
|
|
167 For example:
|
|
168
|
185
|
169 [ "Save As..." write-file t ]
|
|
170 [ "Revert Buffer" revert-buffer (buffer-modified-p) ]
|
|
171 [ "Read Only" toggle-read-only :style toggle :selected buffer-read-only ]
|
0
|
172
|
|
173 See menubar.el for many more examples.
|
20
|
174 */
|
|
175 (menu_desc, event))
|
0
|
176 {
|
|
177 struct frame *f = decode_frame(Qnil);
|
|
178 MAYBE_FRAMEMETH (f, popup_menu, (menu_desc,event));
|
|
179 return Qnil;
|
|
180 }
|
|
181
|
195
|
182 DEFUN ("normalize-menu-item-name", Fnormalize_menu_item_name, 1, 2, 0, /*
|
|
183 Convert a menu item name string into normal form. Returns a new string.
|
|
184 Menu item names should be converted to normal form before being compared.
|
|
185 */
|
|
186 (name, buffer))
|
|
187 {
|
|
188 struct buffer *buf = decode_buffer (buffer, 0);
|
|
189 struct Lisp_String *n;
|
|
190 Charcount end;
|
|
191 int i;
|
|
192 Bufbyte *name_data;
|
|
193 Bufbyte *string_result;
|
|
194 Bufbyte *string_result_ptr;
|
|
195 Lisp_Object res;
|
|
196 Emchar elt;
|
|
197 int expecting_underscore = 0;
|
|
198
|
|
199 CHECK_STRING (name);
|
|
200
|
|
201 n = XSTRING (name);
|
|
202 end = string_char_length (n);
|
|
203 name_data = string_data (n);
|
|
204
|
|
205 string_result = (Bufbyte *) alloca (end * MAX_EMCHAR_LEN);
|
|
206 string_result_ptr = string_result;
|
|
207 for (i = 0; i < end ; i++)
|
|
208 {
|
|
209 elt = charptr_emchar_n (name_data, i);
|
|
210 elt = DOWNCASE (buf, elt);
|
|
211 if (elt == '%')
|
|
212 expecting_underscore = 1;
|
|
213 else if (expecting_underscore)
|
|
214 {
|
|
215 expecting_underscore = 0;
|
|
216 if (elt != '_')
|
|
217 {
|
|
218 string_result_ptr += set_charptr_emchar (string_result_ptr, '%');
|
|
219 string_result_ptr += set_charptr_emchar (string_result_ptr, elt);
|
|
220 }
|
|
221 }
|
|
222 else
|
|
223 string_result_ptr += set_charptr_emchar (string_result_ptr, elt);
|
|
224 }
|
|
225
|
|
226 return make_string (string_result, string_result_ptr - string_result);
|
|
227 }
|
|
228
|
0
|
229 void
|
|
230 syms_of_menubar (void)
|
|
231 {
|
|
232 defsymbol (&Qcurrent_menubar, "current-menubar");
|
20
|
233 DEFSUBR (Fpopup_menu);
|
195
|
234 DEFSUBR (Fnormalize_menu_item_name);
|
0
|
235 }
|
|
236
|
|
237 void
|
|
238 vars_of_menubar (void)
|
|
239 {
|
|
240 {
|
|
241 /* put in Vblank_menubar a menubar value which has no visible
|
|
242 * items. This is a bit tricky due to various quirks. We
|
|
243 * could use '(["" nil nil]), but this is apparently equivalent
|
|
244 * to '(nil), and a new frame created with this menubar will
|
|
245 * get a vertically-squished menubar. If we use " " as the
|
|
246 * button title instead of "", we get an etched button border.
|
|
247 * So we use
|
|
248 * '(("No active menubar" ["" nil nil]))
|
|
249 * which creates a menu whose title is "No active menubar",
|
|
250 * and this works fine.
|
|
251 */
|
|
252
|
|
253 Lisp_Object menu_item[3];
|
|
254 static CONST char *blank_msg = "No active menubar";
|
|
255
|
|
256 menu_item[0] = build_string ("");
|
|
257 menu_item[1] = Qnil;
|
|
258 menu_item[2] = Qnil;
|
185
|
259 Vblank_menubar = Fcons (Fcons (build_string (blank_msg),
|
|
260 Fcons (Fvector (3, &menu_item[0]),
|
0
|
261 Qnil)),
|
|
262 Qnil);
|
|
263 Vblank_menubar = Fpurecopy (Vblank_menubar);
|
|
264 staticpro (&Vblank_menubar);
|
|
265 }
|
|
266
|
|
267 DEFVAR_BOOL ("popup-menu-titles", &popup_menu_titles /*
|
|
268 If true, popup menus will have title bars at the top.
|
|
269 */ );
|
|
270 popup_menu_titles = 1;
|
|
271
|
|
272 /* #### Replace current menubar with a specifier. */
|
|
273
|
|
274 /* All C code must access the menubar via Qcurrent_menubar
|
|
275 because it can be buffer-local. Note that Vcurrent_menubar
|
|
276 doesn't need to exist at all, except for the magic function. */
|
|
277
|
|
278 DEFVAR_LISP_MAGIC ("current-menubar", &Vcurrent_menubar /*
|
|
279 The current menubar. This may be buffer-local.
|
|
280
|
|
281 When the menubar is changed, the function `set-menubar-dirty-flag' has to
|
|
282 be called for the menubar to be updated on the frame. See `set-menubar'
|
|
283 and `set-buffer-menubar'.
|
|
284
|
|
285 A menubar is a list of menus and menu-items.
|
|
286 A menu is a list of menu items, keyword-value pairs, strings, and submenus.
|
|
287
|
|
288 The first element of a menu must be a string, which is the name of the menu.
|
|
289 This is the string that will be displayed in the parent menu, if any. For
|
|
290 toplevel menus, it is ignored. This string is not displayed in the menu
|
|
291 itself.
|
|
292
|
|
293 Immediately following the name string of the menu, any of three
|
|
294 optional keyword-value pairs is permitted.
|
|
295
|
|
296 If an element of a menu (or menubar) is a string, then that string will be
|
|
297 presented as unselectable text.
|
|
298
|
|
299 If an element of a menu is a string consisting solely of hyphens, then that
|
|
300 item will be presented as a solid horizontal line.
|
|
301
|
|
302 If an element of a menu is a list, it is treated as a submenu. The name of
|
|
303 that submenu (the first element in the list) will be used as the name of the
|
|
304 item representing this menu on the parent.
|
|
305
|
|
306 If an element of a menubar is `nil', then it is used to represent the
|
|
307 division between the set of menubar-items which are flushleft and those
|
|
308 which are flushright.
|
|
309
|
|
310 Otherwise, the element must be a vector, which describes a menu item.
|
|
311 A menu item can have any of the following forms:
|
|
312
|
185
|
313 [ "name" callback <active-p> ]
|
|
314 [ "name" callback <active-p> "suffix" ]
|
|
315 [ "name" callback :<keyword> <value> :<keyword> <value> ... ]
|
0
|
316
|
|
317 The name is the string to display on the menu; it is filtered through the
|
|
318 resource database, so it is possible for resources to override what string
|
|
319 is actually displayed.
|
|
320
|
|
321 If the `callback' of a menu item is a symbol, then it must name a command.
|
|
322 It will be invoked with `call-interactively'. If it is a list, then it is
|
|
323 evaluated with `eval'.
|
|
324
|
|
325 The possible keywords are this:
|
|
326
|
|
327 :active <form> Same as <active-p> in the first two forms: the
|
|
328 expression is evaluated just before the menu is
|
|
329 displayed, and the menu will be selectable only if
|
|
330 the result is non-nil.
|
|
331
|
185
|
332 :suffix "string" Same as "suffix" in the second form: the suffix is
|
0
|
333 appended to the displayed name, providing a convenient
|
|
334 way of adding the name of a command's ``argument'' to
|
|
335 the menu, like ``Kill Buffer NAME''.
|
|
336
|
185
|
337 :keys "string" Normally, the keyboard equivalents of commands in
|
0
|
338 menus are displayed when the `callback' is a symbol.
|
|
339 This can be used to specify keys for more complex menu
|
|
340 items. It is passed through `substitute-command-keys'
|
|
341 first.
|
|
342
|
|
343 :style <style> Specifies what kind of object this menu item is:
|
|
344
|
|
345 nil A normal menu item.
|
|
346 toggle A toggle button.
|
|
347 radio A radio button.
|
|
348 button A menubar button.
|
|
349
|
|
350 The only difference between toggle and radio buttons is
|
|
351 how they are displayed. But for consistency, a toggle
|
|
352 button should be used when there is one option whose
|
|
353 value can be turned on or off, and radio buttons should
|
|
354 be used when there is a set of mutually exclusive
|
|
355 options. When using a group of radio buttons, you
|
|
356 should arrange for no more than one to be marked as
|
|
357 selected at a time.
|
|
358
|
|
359 :selected <form> Meaningful only when STYLE is `toggle', `radio' or
|
|
360 `button'. This specifies whether the button will be in
|
|
361 the selected or unselected state.
|
|
362
|
|
363 :included <form> This can be used to control the visibility of a menu or
|
|
364 menu item. The form is evaluated and the menu or menu
|
|
365 item is only displayed if the result is non-nil.
|
|
366
|
|
367 :config <symbol> This is an efficient shorthand for
|
|
368 :included (memq symbol menubar-configuration)
|
|
369 See the variable `menubar-configuration'.
|
|
370
|
|
371 :filter <function> A menu filter can only be used in a menu item list.
|
|
372 (i.e.: not in a menu item itself). It is used to
|
|
373 sensitize or incrementally create a submenu only when
|
|
374 it is selected by the user and not every time the
|
|
375 menubar is activated. The filter function is passed
|
|
376 the list of menu items in the submenu and must return a
|
|
377 list of menu items to be used for the menu. It is
|
|
378 called only when the menu is about to be displayed, so
|
|
379 other menus may already be displayed. Vile and
|
|
380 terrible things will happen if a menu filter function
|
|
381 changes the current buffer, window, or frame. It
|
|
382 also should not raise, lower, or iconify any frames.
|
|
383 Basically, the filter function should have no
|
|
384 side-effects.
|
|
385
|
|
386 For example:
|
|
387
|
185
|
388 ("File"
|
0
|
389 :filter file-menu-filter ; file-menu-filter is a function that takes
|
|
390 ; one argument (a list of menu items) and
|
|
391 ; returns a list of menu items
|
185
|
392 [ "Save As..." write-file t ]
|
|
393 [ "Revert Buffer" revert-buffer (buffer-modified-p) ]
|
|
394 [ "Read Only" toggle-read-only :style toggle
|
0
|
395 :selected buffer-read-only ]
|
|
396 )
|
|
397
|
|
398 See x-menubar.el for many more examples.
|
|
399
|
|
400 After the menubar is clicked upon, but before any menus are popped up,
|
|
401 the functions on the `activate-menubar-hook' are invoked to make top-level
|
|
402 changes to the menus and menubar. Note, however, that the use of menu
|
|
403 filters (using the :filter keyword) is usually a more efficient way to
|
|
404 dynamically alter or sensitize menus.
|
|
405 */, menubar_variable_changed);
|
|
406
|
|
407 Vcurrent_menubar = Qnil;
|
|
408
|
|
409 DEFVAR_LISP ("activate-menubar-hook", &Vactivate_menubar_hook /*
|
|
410 Function or functions called before a menubar menu is pulled down.
|
|
411 These functions are called with no arguments, and should interrogate and
|
|
412 modify the value of `current-menubar' as desired.
|
|
413
|
|
414 The functions on this hook are invoked after the mouse goes down, but before
|
|
415 the menu is mapped, and may be used to activate, deactivate, add, or delete
|
|
416 items from the menus. However, it is probably the case that using a :filter
|
|
417 keyword in a submenu would be a more efficient way of updating menus. See
|
|
418 the documentation of `current-menubar'.
|
|
419
|
|
420 These functions may return the symbol `t' to assert that they have made
|
|
421 no changes to the menubar. If any other value is returned, the menubar is
|
|
422 recomputed. If `t' is returned but the menubar has been changed, then the
|
|
423 changes may not show up right away. Returning `nil' when the menubar has
|
|
424 not changed is not so bad; more computation will be done, but redisplay of
|
|
425 the menubar will still be performed optimally.
|
|
426 */ );
|
|
427 Vactivate_menubar_hook = Qnil;
|
|
428 defsymbol (&Qactivate_menubar_hook, "activate-menubar-hook");
|
|
429
|
|
430 DEFVAR_BOOL ("menubar-show-keybindings", &menubar_show_keybindings /*
|
|
431 If true, the menubar will display keyboard equivalents.
|
|
432 If false, only the command names will be displayed.
|
|
433 */ );
|
|
434 menubar_show_keybindings = 1;
|
|
435
|
|
436 DEFVAR_LISP_MAGIC ("menubar-configuration", &Vmenubar_configuration /*
|
|
437 A list of symbols, against which the value of the :config tag for each
|
|
438 menubar item will be compared. If a menubar item has a :config tag, then
|
|
439 it is omitted from the menubar if that tag is not a member of the
|
|
440 `menubar-configuration' list.
|
|
441 */ , menubar_variable_changed);
|
|
442 Vmenubar_configuration = Qnil;
|
|
443
|
|
444 DEFVAR_LISP ("menubar-pointer-glyph", &Vmenubar_pointer_glyph /*
|
|
445 *The shape of the mouse-pointer when over the menubar.
|
|
446 This is a glyph; use `set-glyph-image' to change it.
|
|
447 If unspecified in a particular domain, the window-system-provided
|
|
448 default pointer is used.
|
|
449 */ );
|
|
450
|
|
451 Fprovide (intern ("menubar"));
|
|
452 }
|
|
453
|
|
454 void
|
|
455 specifier_vars_of_menubar (void)
|
|
456 {
|
|
457 DEFVAR_SPECIFIER ("menubar-visible-p", &Vmenubar_visible_p /*
|
|
458 *Whether the menubar is visible.
|
|
459 This is a specifier; use `set-specifier' to change it.
|
|
460 */ );
|
|
461 Vmenubar_visible_p = Fmake_specifier (Qboolean);
|
|
462
|
|
463 set_specifier_fallback (Vmenubar_visible_p, list1 (Fcons (Qnil, Qt)));
|
|
464 set_specifier_caching (Vmenubar_visible_p,
|
|
465 slot_offset (struct window,
|
|
466 menubar_visible_p),
|
|
467 menubar_visible_p_changed,
|
|
468 slot_offset (struct frame,
|
|
469 menubar_visible_p),
|
|
470 menubar_visible_p_changed_in_frame);
|
|
471 }
|
|
472
|
|
473 void
|
|
474 complex_vars_of_menubar (void)
|
|
475 {
|
|
476 Vmenubar_pointer_glyph = Fmake_glyph_internal (Qpointer);
|
|
477 }
|