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 Emchar elt;
|
|
196 int expecting_underscore = 0;
|
197
|
197
|
195
|
198 CHECK_STRING (name);
|
197
|
199
|
195
|
200 n = XSTRING (name);
|
|
201 end = string_char_length (n);
|
|
202 name_data = string_data (n);
|
197
|
203
|
195
|
204 string_result = (Bufbyte *) alloca (end * MAX_EMCHAR_LEN);
|
|
205 string_result_ptr = string_result;
|
|
206 for (i = 0; i < end ; i++)
|
|
207 {
|
|
208 elt = charptr_emchar_n (name_data, i);
|
|
209 elt = DOWNCASE (buf, elt);
|
|
210 if (elt == '%')
|
|
211 expecting_underscore = 1;
|
|
212 else if (expecting_underscore)
|
|
213 {
|
|
214 expecting_underscore = 0;
|
|
215 if (elt != '_')
|
|
216 {
|
|
217 string_result_ptr += set_charptr_emchar (string_result_ptr, '%');
|
|
218 string_result_ptr += set_charptr_emchar (string_result_ptr, elt);
|
|
219 }
|
|
220 }
|
|
221 else
|
|
222 string_result_ptr += set_charptr_emchar (string_result_ptr, elt);
|
|
223 }
|
|
224
|
|
225 return make_string (string_result, string_result_ptr - string_result);
|
|
226 }
|
|
227
|
0
|
228 void
|
|
229 syms_of_menubar (void)
|
|
230 {
|
|
231 defsymbol (&Qcurrent_menubar, "current-menubar");
|
20
|
232 DEFSUBR (Fpopup_menu);
|
195
|
233 DEFSUBR (Fnormalize_menu_item_name);
|
0
|
234 }
|
|
235
|
|
236 void
|
|
237 vars_of_menubar (void)
|
|
238 {
|
|
239 {
|
|
240 /* put in Vblank_menubar a menubar value which has no visible
|
|
241 * items. This is a bit tricky due to various quirks. We
|
|
242 * could use '(["" nil nil]), but this is apparently equivalent
|
|
243 * to '(nil), and a new frame created with this menubar will
|
|
244 * get a vertically-squished menubar. If we use " " as the
|
|
245 * button title instead of "", we get an etched button border.
|
|
246 * So we use
|
|
247 * '(("No active menubar" ["" nil nil]))
|
|
248 * which creates a menu whose title is "No active menubar",
|
|
249 * and this works fine.
|
|
250 */
|
|
251
|
|
252 Lisp_Object menu_item[3];
|
|
253 static CONST char *blank_msg = "No active menubar";
|
|
254
|
|
255 menu_item[0] = build_string ("");
|
|
256 menu_item[1] = Qnil;
|
|
257 menu_item[2] = Qnil;
|
185
|
258 Vblank_menubar = Fcons (Fcons (build_string (blank_msg),
|
|
259 Fcons (Fvector (3, &menu_item[0]),
|
0
|
260 Qnil)),
|
|
261 Qnil);
|
|
262 Vblank_menubar = Fpurecopy (Vblank_menubar);
|
|
263 staticpro (&Vblank_menubar);
|
|
264 }
|
|
265
|
|
266 DEFVAR_BOOL ("popup-menu-titles", &popup_menu_titles /*
|
|
267 If true, popup menus will have title bars at the top.
|
|
268 */ );
|
|
269 popup_menu_titles = 1;
|
|
270
|
|
271 /* #### Replace current menubar with a specifier. */
|
|
272
|
|
273 /* All C code must access the menubar via Qcurrent_menubar
|
|
274 because it can be buffer-local. Note that Vcurrent_menubar
|
|
275 doesn't need to exist at all, except for the magic function. */
|
|
276
|
|
277 DEFVAR_LISP_MAGIC ("current-menubar", &Vcurrent_menubar /*
|
|
278 The current menubar. This may be buffer-local.
|
|
279
|
|
280 When the menubar is changed, the function `set-menubar-dirty-flag' has to
|
|
281 be called for the menubar to be updated on the frame. See `set-menubar'
|
|
282 and `set-buffer-menubar'.
|
|
283
|
|
284 A menubar is a list of menus and menu-items.
|
|
285 A menu is a list of menu items, keyword-value pairs, strings, and submenus.
|
|
286
|
|
287 The first element of a menu must be a string, which is the name of the menu.
|
|
288 This is the string that will be displayed in the parent menu, if any. For
|
|
289 toplevel menus, it is ignored. This string is not displayed in the menu
|
|
290 itself.
|
|
291
|
|
292 Immediately following the name string of the menu, any of three
|
|
293 optional keyword-value pairs is permitted.
|
|
294
|
|
295 If an element of a menu (or menubar) is a string, then that string will be
|
|
296 presented as unselectable text.
|
|
297
|
|
298 If an element of a menu is a string consisting solely of hyphens, then that
|
|
299 item will be presented as a solid horizontal line.
|
|
300
|
|
301 If an element of a menu is a list, it is treated as a submenu. The name of
|
|
302 that submenu (the first element in the list) will be used as the name of the
|
|
303 item representing this menu on the parent.
|
|
304
|
|
305 If an element of a menubar is `nil', then it is used to represent the
|
|
306 division between the set of menubar-items which are flushleft and those
|
|
307 which are flushright.
|
|
308
|
|
309 Otherwise, the element must be a vector, which describes a menu item.
|
|
310 A menu item can have any of the following forms:
|
|
311
|
185
|
312 [ "name" callback <active-p> ]
|
|
313 [ "name" callback <active-p> "suffix" ]
|
|
314 [ "name" callback :<keyword> <value> :<keyword> <value> ... ]
|
0
|
315
|
|
316 The name is the string to display on the menu; it is filtered through the
|
|
317 resource database, so it is possible for resources to override what string
|
|
318 is actually displayed.
|
|
319
|
|
320 If the `callback' of a menu item is a symbol, then it must name a command.
|
|
321 It will be invoked with `call-interactively'. If it is a list, then it is
|
|
322 evaluated with `eval'.
|
|
323
|
|
324 The possible keywords are this:
|
|
325
|
|
326 :active <form> Same as <active-p> in the first two forms: the
|
|
327 expression is evaluated just before the menu is
|
|
328 displayed, and the menu will be selectable only if
|
|
329 the result is non-nil.
|
|
330
|
185
|
331 :suffix "string" Same as "suffix" in the second form: the suffix is
|
0
|
332 appended to the displayed name, providing a convenient
|
|
333 way of adding the name of a command's ``argument'' to
|
|
334 the menu, like ``Kill Buffer NAME''.
|
|
335
|
185
|
336 :keys "string" Normally, the keyboard equivalents of commands in
|
0
|
337 menus are displayed when the `callback' is a symbol.
|
|
338 This can be used to specify keys for more complex menu
|
|
339 items. It is passed through `substitute-command-keys'
|
|
340 first.
|
|
341
|
|
342 :style <style> Specifies what kind of object this menu item is:
|
|
343
|
|
344 nil A normal menu item.
|
|
345 toggle A toggle button.
|
|
346 radio A radio button.
|
|
347 button A menubar button.
|
|
348
|
|
349 The only difference between toggle and radio buttons is
|
|
350 how they are displayed. But for consistency, a toggle
|
|
351 button should be used when there is one option whose
|
|
352 value can be turned on or off, and radio buttons should
|
|
353 be used when there is a set of mutually exclusive
|
|
354 options. When using a group of radio buttons, you
|
|
355 should arrange for no more than one to be marked as
|
|
356 selected at a time.
|
|
357
|
|
358 :selected <form> Meaningful only when STYLE is `toggle', `radio' or
|
|
359 `button'. This specifies whether the button will be in
|
|
360 the selected or unselected state.
|
|
361
|
|
362 :included <form> This can be used to control the visibility of a menu or
|
|
363 menu item. The form is evaluated and the menu or menu
|
|
364 item is only displayed if the result is non-nil.
|
|
365
|
|
366 :config <symbol> This is an efficient shorthand for
|
|
367 :included (memq symbol menubar-configuration)
|
|
368 See the variable `menubar-configuration'.
|
|
369
|
|
370 :filter <function> A menu filter can only be used in a menu item list.
|
|
371 (i.e.: not in a menu item itself). It is used to
|
|
372 sensitize or incrementally create a submenu only when
|
|
373 it is selected by the user and not every time the
|
|
374 menubar is activated. The filter function is passed
|
|
375 the list of menu items in the submenu and must return a
|
|
376 list of menu items to be used for the menu. It is
|
|
377 called only when the menu is about to be displayed, so
|
|
378 other menus may already be displayed. Vile and
|
|
379 terrible things will happen if a menu filter function
|
|
380 changes the current buffer, window, or frame. It
|
|
381 also should not raise, lower, or iconify any frames.
|
|
382 Basically, the filter function should have no
|
|
383 side-effects.
|
|
384
|
|
385 For example:
|
|
386
|
185
|
387 ("File"
|
0
|
388 :filter file-menu-filter ; file-menu-filter is a function that takes
|
|
389 ; one argument (a list of menu items) and
|
|
390 ; returns a list of menu items
|
185
|
391 [ "Save As..." write-file t ]
|
|
392 [ "Revert Buffer" revert-buffer (buffer-modified-p) ]
|
|
393 [ "Read Only" toggle-read-only :style toggle
|
0
|
394 :selected buffer-read-only ]
|
|
395 )
|
|
396
|
|
397 See x-menubar.el for many more examples.
|
|
398
|
|
399 After the menubar is clicked upon, but before any menus are popped up,
|
|
400 the functions on the `activate-menubar-hook' are invoked to make top-level
|
|
401 changes to the menus and menubar. Note, however, that the use of menu
|
|
402 filters (using the :filter keyword) is usually a more efficient way to
|
|
403 dynamically alter or sensitize menus.
|
|
404 */, menubar_variable_changed);
|
|
405
|
|
406 Vcurrent_menubar = Qnil;
|
|
407
|
|
408 DEFVAR_LISP ("activate-menubar-hook", &Vactivate_menubar_hook /*
|
|
409 Function or functions called before a menubar menu is pulled down.
|
|
410 These functions are called with no arguments, and should interrogate and
|
|
411 modify the value of `current-menubar' as desired.
|
|
412
|
|
413 The functions on this hook are invoked after the mouse goes down, but before
|
|
414 the menu is mapped, and may be used to activate, deactivate, add, or delete
|
|
415 items from the menus. However, it is probably the case that using a :filter
|
|
416 keyword in a submenu would be a more efficient way of updating menus. See
|
|
417 the documentation of `current-menubar'.
|
|
418
|
|
419 These functions may return the symbol `t' to assert that they have made
|
|
420 no changes to the menubar. If any other value is returned, the menubar is
|
|
421 recomputed. If `t' is returned but the menubar has been changed, then the
|
|
422 changes may not show up right away. Returning `nil' when the menubar has
|
|
423 not changed is not so bad; more computation will be done, but redisplay of
|
|
424 the menubar will still be performed optimally.
|
|
425 */ );
|
|
426 Vactivate_menubar_hook = Qnil;
|
|
427 defsymbol (&Qactivate_menubar_hook, "activate-menubar-hook");
|
|
428
|
|
429 DEFVAR_BOOL ("menubar-show-keybindings", &menubar_show_keybindings /*
|
|
430 If true, the menubar will display keyboard equivalents.
|
|
431 If false, only the command names will be displayed.
|
|
432 */ );
|
|
433 menubar_show_keybindings = 1;
|
|
434
|
|
435 DEFVAR_LISP_MAGIC ("menubar-configuration", &Vmenubar_configuration /*
|
|
436 A list of symbols, against which the value of the :config tag for each
|
|
437 menubar item will be compared. If a menubar item has a :config tag, then
|
|
438 it is omitted from the menubar if that tag is not a member of the
|
|
439 `menubar-configuration' list.
|
|
440 */ , menubar_variable_changed);
|
|
441 Vmenubar_configuration = Qnil;
|
|
442
|
|
443 DEFVAR_LISP ("menubar-pointer-glyph", &Vmenubar_pointer_glyph /*
|
|
444 *The shape of the mouse-pointer when over the menubar.
|
|
445 This is a glyph; use `set-glyph-image' to change it.
|
|
446 If unspecified in a particular domain, the window-system-provided
|
|
447 default pointer is used.
|
|
448 */ );
|
|
449
|
|
450 Fprovide (intern ("menubar"));
|
|
451 }
|
|
452
|
|
453 void
|
|
454 specifier_vars_of_menubar (void)
|
|
455 {
|
|
456 DEFVAR_SPECIFIER ("menubar-visible-p", &Vmenubar_visible_p /*
|
|
457 *Whether the menubar is visible.
|
|
458 This is a specifier; use `set-specifier' to change it.
|
|
459 */ );
|
|
460 Vmenubar_visible_p = Fmake_specifier (Qboolean);
|
|
461
|
|
462 set_specifier_fallback (Vmenubar_visible_p, list1 (Fcons (Qnil, Qt)));
|
|
463 set_specifier_caching (Vmenubar_visible_p,
|
|
464 slot_offset (struct window,
|
|
465 menubar_visible_p),
|
|
466 menubar_visible_p_changed,
|
|
467 slot_offset (struct frame,
|
|
468 menubar_visible_p),
|
|
469 menubar_visible_p_changed_in_frame);
|
|
470 }
|
|
471
|
|
472 void
|
|
473 complex_vars_of_menubar (void)
|
|
474 {
|
|
475 Vmenubar_pointer_glyph = Fmake_glyph_internal (Qpointer);
|
|
476 }
|