]> code.delx.au - gnu-emacs/blob - src/xmenu.c
(Flength): Doc fix.
[gnu-emacs] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 *
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
25 *
26 */
27
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
30
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32
33 /* On 4.3 this loses if it comes after xterm.h. */
34 #include <signal.h>
35 #include <config.h>
36
37 #include <stdio.h>
38 #include "lisp.h"
39 #include "termhooks.h"
40 #include "frame.h"
41 #include "window.h"
42 #include "keyboard.h"
43 #include "blockinput.h"
44 #include "buffer.h"
45
46 #ifdef MSDOS
47 #include "msdos.h"
48 #endif
49
50 #ifdef HAVE_X_WINDOWS
51 /* This may include sys/types.h, and that somehow loses
52 if this is not done before the other system files. */
53 #include "xterm.h"
54 #endif
55
56 /* Load sys/types.h if not already loaded.
57 In some systems loading it twice is suicidal. */
58 #ifndef makedev
59 #include <sys/types.h>
60 #endif
61
62 #include "dispextern.h"
63
64 #ifdef HAVE_X_WINDOWS
65 #ifdef USE_X_TOOLKIT
66 #include <X11/Xlib.h>
67 #include <X11/IntrinsicP.h>
68 #include <X11/CoreP.h>
69 #include <X11/StringDefs.h>
70 #include <X11/Shell.h>
71 #ifdef USE_LUCID
72 #include <X11/Xaw/Paned.h>
73 #endif /* USE_LUCID */
74 #include "../lwlib/lwlib.h"
75 #else /* not USE_X_TOOLKIT */
76 #include "../oldXMenu/XMenu.h"
77 #endif /* not USE_X_TOOLKIT */
78 #endif /* HAVE_X_WINDOWS */
79
80 #define min(x,y) (((x) < (y)) ? (x) : (y))
81 #define max(x,y) (((x) > (y)) ? (x) : (y))
82
83 #ifndef TRUE
84 #define TRUE 1
85 #define FALSE 0
86 #endif /* no TRUE */
87
88 Lisp_Object Vmenu_updating_frame;
89
90 Lisp_Object Qdebug_on_next_call;
91
92 extern Lisp_Object Qmenu_bar;
93 extern Lisp_Object Qmouse_click, Qevent_kind;
94
95 extern Lisp_Object QCtoggle, QCradio;
96
97 extern Lisp_Object Voverriding_local_map;
98 extern Lisp_Object Voverriding_local_map_menu_flag;
99
100 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
101
102 extern Lisp_Object Qmenu_bar_update_hook;
103
104 #ifdef USE_X_TOOLKIT
105 extern void set_frame_menubar ();
106 extern void process_expose_from_menu ();
107 extern XtAppContext Xt_app_con;
108
109 static Lisp_Object xdialog_show ();
110 void popup_get_selection ();
111 #endif
112
113 static Lisp_Object xmenu_show ();
114 static void keymap_panes ();
115 static void single_keymap_panes ();
116 static void single_menu_item ();
117 static void list_of_panes ();
118 static void list_of_items ();
119 \f
120 /* This holds a Lisp vector that holds the results of decoding
121 the keymaps or alist-of-alists that specify a menu.
122
123 It describes the panes and items within the panes.
124
125 Each pane is described by 3 elements in the vector:
126 t, the pane name, the pane's prefix key.
127 Then follow the pane's items, with 5 elements per item:
128 the item string, the enable flag, the item's value,
129 the definition, and the equivalent keyboard key's description string.
130
131 In some cases, multiple levels of menus may be described.
132 A single vector slot containing nil indicates the start of a submenu.
133 A single vector slot containing lambda indicates the end of a submenu.
134 The submenu follows a menu item which is the way to reach the submenu.
135
136 A single vector slot containing quote indicates that the
137 following items should appear on the right of a dialog box.
138
139 Using a Lisp vector to hold this information while we decode it
140 takes care of protecting all the data from GC. */
141
142 #define MENU_ITEMS_PANE_NAME 1
143 #define MENU_ITEMS_PANE_PREFIX 2
144 #define MENU_ITEMS_PANE_LENGTH 3
145
146 #define MENU_ITEMS_ITEM_NAME 0
147 #define MENU_ITEMS_ITEM_ENABLE 1
148 #define MENU_ITEMS_ITEM_VALUE 2
149 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
150 #define MENU_ITEMS_ITEM_DEFINITION 4
151 #define MENU_ITEMS_ITEM_LENGTH 5
152
153 static Lisp_Object menu_items;
154
155 /* Number of slots currently allocated in menu_items. */
156 static int menu_items_allocated;
157
158 /* This is the index in menu_items of the first empty slot. */
159 static int menu_items_used;
160
161 /* The number of panes currently recorded in menu_items,
162 excluding those within submenus. */
163 static int menu_items_n_panes;
164
165 /* Current depth within submenus. */
166 static int menu_items_submenu_depth;
167
168 /* Flag which when set indicates a dialog or menu has been posted by
169 Xt on behalf of one of the widget sets. */
170 static int popup_activated_flag;
171
172 static int next_menubar_widget_id;
173
174 /* This is set nonzero after the user activates the menu bar, and set
175 to zero again after the menu bars are redisplayed by prepare_menu_bar.
176 While it is nonzero, all calls to set_frame_menubar go deep.
177
178 I don't understand why this is needed, but it does seem to be
179 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
180
181 int pending_menu_activation;
182 \f
183 #ifdef USE_X_TOOLKIT
184
185 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
186
187 static struct frame *
188 menubar_id_to_frame (id)
189 LWLIB_ID id;
190 {
191 Lisp_Object tail, frame;
192 FRAME_PTR f;
193
194 for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
195 {
196 frame = XCONS (tail)->car;
197 if (!GC_FRAMEP (frame))
198 continue;
199 f = XFRAME (frame);
200 if (f->output_data.nothing == 1)
201 continue;
202 if (f->output_data.x->id == id)
203 return f;
204 }
205 return 0;
206 }
207
208 #endif
209 \f
210 /* Initialize the menu_items structure if we haven't already done so.
211 Also mark it as currently empty. */
212
213 static void
214 init_menu_items ()
215 {
216 if (NILP (menu_items))
217 {
218 menu_items_allocated = 60;
219 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
220 }
221
222 menu_items_used = 0;
223 menu_items_n_panes = 0;
224 menu_items_submenu_depth = 0;
225 }
226
227 /* Call at the end of generating the data in menu_items.
228 This fills in the number of items in the last pane. */
229
230 static void
231 finish_menu_items ()
232 {
233 }
234
235 /* Call when finished using the data for the current menu
236 in menu_items. */
237
238 static void
239 discard_menu_items ()
240 {
241 /* Free the structure if it is especially large.
242 Otherwise, hold on to it, to save time. */
243 if (menu_items_allocated > 200)
244 {
245 menu_items = Qnil;
246 menu_items_allocated = 0;
247 }
248 }
249
250 /* Make the menu_items vector twice as large. */
251
252 static void
253 grow_menu_items ()
254 {
255 Lisp_Object old;
256 int old_size = menu_items_allocated;
257 old = menu_items;
258
259 menu_items_allocated *= 2;
260 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
261 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
262 old_size * sizeof (Lisp_Object));
263 }
264
265 /* Begin a submenu. */
266
267 static void
268 push_submenu_start ()
269 {
270 if (menu_items_used + 1 > menu_items_allocated)
271 grow_menu_items ();
272
273 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
274 menu_items_submenu_depth++;
275 }
276
277 /* End a submenu. */
278
279 static void
280 push_submenu_end ()
281 {
282 if (menu_items_used + 1 > menu_items_allocated)
283 grow_menu_items ();
284
285 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
286 menu_items_submenu_depth--;
287 }
288
289 /* Indicate boundary between left and right. */
290
291 static void
292 push_left_right_boundary ()
293 {
294 if (menu_items_used + 1 > menu_items_allocated)
295 grow_menu_items ();
296
297 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
298 }
299
300 /* Start a new menu pane in menu_items..
301 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
302
303 static void
304 push_menu_pane (name, prefix_vec)
305 Lisp_Object name, prefix_vec;
306 {
307 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
308 grow_menu_items ();
309
310 if (menu_items_submenu_depth == 0)
311 menu_items_n_panes++;
312 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
313 XVECTOR (menu_items)->contents[menu_items_used++] = name;
314 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
315 }
316
317 /* Push one menu item into the current pane.
318 NAME is the string to display. ENABLE if non-nil means
319 this item can be selected. KEY is the key generated by
320 choosing this item, or nil if this item doesn't really have a definition.
321 DEF is the definition of this item.
322 EQUIV is the textual description of the keyboard equivalent for
323 this item (or nil if none). */
324
325 static void
326 push_menu_item (name, enable, key, def, equiv)
327 Lisp_Object name, enable, key, def, equiv;
328 {
329 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
330 grow_menu_items ();
331
332 XVECTOR (menu_items)->contents[menu_items_used++] = name;
333 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
334 XVECTOR (menu_items)->contents[menu_items_used++] = key;
335 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
336 XVECTOR (menu_items)->contents[menu_items_used++] = def;
337 }
338 \f
339 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
340 and generate menu panes for them in menu_items.
341 If NOTREAL is nonzero,
342 don't bother really computing whether an item is enabled. */
343
344 static void
345 keymap_panes (keymaps, nmaps, notreal)
346 Lisp_Object *keymaps;
347 int nmaps;
348 int notreal;
349 {
350 int mapno;
351
352 init_menu_items ();
353
354 /* Loop over the given keymaps, making a pane for each map.
355 But don't make a pane that is empty--ignore that map instead.
356 P is the number of panes we have made so far. */
357 for (mapno = 0; mapno < nmaps; mapno++)
358 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal, 10);
359
360 finish_menu_items ();
361 }
362
363 /* This is a recursive subroutine of keymap_panes.
364 It handles one keymap, KEYMAP.
365 The other arguments are passed along
366 or point to local variables of the previous function.
367 If NOTREAL is nonzero, only check for equivalent key bindings, don't
368 evaluate expressions in menu items and don't make any menu.
369
370 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
371
372 static void
373 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
374 Lisp_Object keymap;
375 Lisp_Object pane_name;
376 Lisp_Object prefix;
377 int notreal;
378 int maxdepth;
379 {
380 Lisp_Object pending_maps = Qnil;
381 Lisp_Object tail, item;
382 struct gcpro gcpro1, gcpro2;
383 int notbuttons = 0;
384
385 if (maxdepth <= 0)
386 return;
387
388 push_menu_pane (pane_name, prefix);
389
390 #ifndef HAVE_BOXES
391 /* Remember index for first item in this pane so we can go back and
392 add a prefix when (if) we see the first button. After that, notbuttons
393 is set to 0, to mark that we have seen a button and all non button
394 items need a prefix. */
395 notbuttons = menu_items_used;
396 #endif
397
398 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
399 {
400 GCPRO2 (keymap, pending_maps);
401 /* Look at each key binding, and if it is a menu item add it
402 to this menu. */
403 item = XCONS (tail)->car;
404 if (CONSP (item))
405 single_menu_item (XCONS (item)->car, XCONS (item)->cdr,
406 &pending_maps, notreal, maxdepth, &notbuttons);
407 else if (VECTORP (item))
408 {
409 /* Loop over the char values represented in the vector. */
410 int len = XVECTOR (item)->size;
411 int c;
412 for (c = 0; c < len; c++)
413 {
414 Lisp_Object character;
415 XSETFASTINT (character, c);
416 single_menu_item (character, XVECTOR (item)->contents[c],
417 &pending_maps, notreal, maxdepth, &notbuttons);
418 }
419 }
420 UNGCPRO;
421 }
422
423 /* Process now any submenus which want to be panes at this level. */
424 while (!NILP (pending_maps))
425 {
426 Lisp_Object elt, eltcdr, string;
427 elt = Fcar (pending_maps);
428 eltcdr = XCONS (elt)->cdr;
429 string = XCONS (eltcdr)->car;
430 /* We no longer discard the @ from the beginning of the string here.
431 Instead, we do this in xmenu_show. */
432 single_keymap_panes (Fcar (elt), string,
433 XCONS (eltcdr)->cdr, notreal, maxdepth - 1);
434 pending_maps = Fcdr (pending_maps);
435 }
436 }
437 \f
438 /* This is a subroutine of single_keymap_panes that handles one
439 keymap entry.
440 KEY is a key in a keymap and ITEM is its binding.
441 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
442 separate panes.
443 If NOTREAL is nonzero, only check for equivalent key bindings, don't
444 evaluate expressions in menu items and don't make any menu.
445 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
446 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
447 buttons. It points to variable notbuttons in single_keymap_panes,
448 which keeps track of if we have seen a button in this menu or not. */
449
450 static void
451 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
452 notbuttons_ptr)
453 Lisp_Object key, item;
454 Lisp_Object *pending_maps_ptr;
455 int maxdepth, notreal;
456 int *notbuttons_ptr;
457 {
458 Lisp_Object def, map, item_string, enabled;
459 struct gcpro gcpro1, gcpro2;
460 int res;
461
462 /* Parse the menu item and leave the result in item_properties. */
463 GCPRO2 (key, item);
464 res = parse_menu_item (item, notreal, 0);
465 UNGCPRO;
466 if (!res)
467 return; /* Not a menu item. */
468
469 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
470
471 if (notreal)
472 {
473 /* We don't want to make a menu, just traverse the keymaps to
474 precompute equivalent key bindings. */
475 if (!NILP (map))
476 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
477 return;
478 }
479
480 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
481 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
482
483 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
484 {
485 if (!NILP (enabled))
486 /* An enabled separate pane. Remember this to handle it later. */
487 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
488 *pending_maps_ptr);
489 return;
490 }
491
492 #ifndef HAVE_BOXES
493 /* Simulate radio buttons and toggle boxes by putting a prefix in
494 front of them. */
495 {
496 Lisp_Object prefix = Qnil;
497 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
498 if (!NILP (type))
499 {
500 Lisp_Object selected
501 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
502
503 if (*notbuttons_ptr)
504 /* The first button. Line up previous items in this menu. */
505 {
506 int index = *notbuttons_ptr; /* Index for first item this menu. */
507 int submenu = 0;
508 Lisp_Object tem;
509 while (index < menu_items_used)
510 {
511 tem
512 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
513 if (NILP (tem))
514 {
515 index++;
516 submenu++; /* Skip sub menu. */
517 }
518 else if (EQ (tem, Qlambda))
519 {
520 index++;
521 submenu--; /* End sub menu. */
522 }
523 else if (EQ (tem, Qt))
524 index += 3; /* Skip new pane marker. */
525 else if (EQ (tem, Qquote))
526 index++; /* Skip a left, right divider. */
527 else
528 {
529 if (!submenu && XSTRING (tem)->data[0] != '\0'
530 && XSTRING (tem)->data[0] != '-')
531 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
532 = concat2 (build_string (" "), tem);
533 index += MENU_ITEMS_ITEM_LENGTH;
534 }
535 }
536 *notbuttons_ptr = 0;
537 }
538
539 /* Calculate prefix, if any, for this item. */
540 if (EQ (type, QCtoggle))
541 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
542 else if (EQ (type, QCradio))
543 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
544 }
545 /* Not a button. If we have earlier buttons, then we need a prefix. */
546 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
547 && XSTRING (item_string)->data[0] != '-')
548 prefix = build_string (" ");
549
550 if (!NILP (prefix))
551 item_string = concat2 (prefix, item_string);
552 }
553 #endif /* not HAVE_BOXES */
554
555 #ifndef USE_X_TOOLKIT
556 if (!NILP(map))
557 /* Indicate visually that this is a submenu. */
558 item_string = concat2 (item_string, build_string (" >"));
559 #endif
560
561 push_menu_item (item_string, enabled, key,
562 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
563 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]);
564
565 #ifdef USE_X_TOOLKIT
566 /* Display a submenu using the toolkit. */
567 if (! (NILP (map) || NILP (enabled)))
568 {
569 push_submenu_start ();
570 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
571 push_submenu_end ();
572 }
573 #endif
574 }
575 \f
576 /* Push all the panes and items of a menu described by the
577 alist-of-alists MENU.
578 This handles old-fashioned calls to x-popup-menu. */
579
580 static void
581 list_of_panes (menu)
582 Lisp_Object menu;
583 {
584 Lisp_Object tail;
585
586 init_menu_items ();
587
588 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
589 {
590 Lisp_Object elt, pane_name, pane_data;
591 elt = Fcar (tail);
592 pane_name = Fcar (elt);
593 CHECK_STRING (pane_name, 0);
594 push_menu_pane (pane_name, Qnil);
595 pane_data = Fcdr (elt);
596 CHECK_CONS (pane_data, 0);
597 list_of_items (pane_data);
598 }
599
600 finish_menu_items ();
601 }
602
603 /* Push the items in a single pane defined by the alist PANE. */
604
605 static void
606 list_of_items (pane)
607 Lisp_Object pane;
608 {
609 Lisp_Object tail, item, item1;
610
611 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
612 {
613 item = Fcar (tail);
614 if (STRINGP (item))
615 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
616 else if (NILP (item))
617 push_left_right_boundary ();
618 else
619 {
620 CHECK_CONS (item, 0);
621 item1 = Fcar (item);
622 CHECK_STRING (item1, 1);
623 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
624 }
625 }
626 }
627 \f
628 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
629 "Pop up a deck-of-cards menu and return user's selection.\n\
630 POSITION is a position specification. This is either a mouse button event\n\
631 or a list ((XOFFSET YOFFSET) WINDOW)\n\
632 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
633 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
634 This controls the position of the center of the first line\n\
635 in the first pane of the menu, not the top left of the menu as a whole.\n\
636 If POSITION is t, it means to use the current mouse position.\n\
637 \n\
638 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
639 The menu items come from key bindings that have a menu string as well as\n\
640 a definition; actually, the \"definition\" in such a key binding looks like\n\
641 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
642 the keymap as a top-level element.\n\n\
643 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
644 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
645 \n\
646 You can also use a list of keymaps as MENU.\n\
647 Then each keymap makes a separate pane.\n\
648 When MENU is a keymap or a list of keymaps, the return value\n\
649 is a list of events.\n\n\
650 \n\
651 Alternatively, you can specify a menu of multiple panes\n\
652 with a list of the form (TITLE PANE1 PANE2...),\n\
653 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
654 Each ITEM is normally a cons cell (STRING . VALUE);\n\
655 but a string can appear as an item--that makes a nonselectable line\n\
656 in the menu.\n\
657 With this form of menu, the return value is VALUE from the chosen item.\n\
658 \n\
659 If POSITION is nil, don't display the menu at all, just precalculate the\n\
660 cached information about equivalent key sequences.")
661 (position, menu)
662 Lisp_Object position, menu;
663 {
664 int number_of_panes, panes;
665 Lisp_Object keymap, tem;
666 int xpos, ypos;
667 Lisp_Object title;
668 char *error_name;
669 Lisp_Object selection;
670 int i, j;
671 FRAME_PTR f;
672 Lisp_Object x, y, window;
673 int keymaps = 0;
674 int for_click = 0;
675 struct gcpro gcpro1;
676
677 #ifdef HAVE_MENUS
678 if (! NILP (position))
679 {
680 check_x ();
681
682 /* Decode the first argument: find the window and the coordinates. */
683 if (EQ (position, Qt)
684 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
685 {
686 /* Use the mouse's current position. */
687 FRAME_PTR new_f = selected_frame;
688 Lisp_Object bar_window;
689 int part;
690 unsigned long time;
691
692 if (mouse_position_hook)
693 (*mouse_position_hook) (&new_f, 1, &bar_window,
694 &part, &x, &y, &time);
695 if (new_f != 0)
696 XSETFRAME (window, new_f);
697 else
698 {
699 window = selected_window;
700 XSETFASTINT (x, 0);
701 XSETFASTINT (y, 0);
702 }
703 }
704 else
705 {
706 tem = Fcar (position);
707 if (CONSP (tem))
708 {
709 window = Fcar (Fcdr (position));
710 x = Fcar (tem);
711 y = Fcar (Fcdr (tem));
712 }
713 else
714 {
715 for_click = 1;
716 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
717 window = Fcar (tem); /* POSN_WINDOW (tem) */
718 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
719 x = Fcar (tem);
720 y = Fcdr (tem);
721 }
722 }
723
724 CHECK_NUMBER (x, 0);
725 CHECK_NUMBER (y, 0);
726
727 /* Decode where to put the menu. */
728
729 if (FRAMEP (window))
730 {
731 f = XFRAME (window);
732 xpos = 0;
733 ypos = 0;
734 }
735 else if (WINDOWP (window))
736 {
737 CHECK_LIVE_WINDOW (window, 0);
738 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
739
740 xpos = (FONT_WIDTH (f->output_data.x->font)
741 * XFASTINT (XWINDOW (window)->left));
742 ypos = (f->output_data.x->line_height
743 * XFASTINT (XWINDOW (window)->top));
744 }
745 else
746 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
747 but I don't want to make one now. */
748 CHECK_WINDOW (window, 0);
749
750 xpos += XINT (x);
751 ypos += XINT (y);
752
753 XSETFRAME (Vmenu_updating_frame, f);
754 }
755 Vmenu_updating_frame = Qnil;
756 #endif /* HAVE_MENUS */
757
758 title = Qnil;
759 GCPRO1 (title);
760
761 /* Decode the menu items from what was specified. */
762
763 keymap = Fkeymapp (menu);
764 tem = Qnil;
765 if (CONSP (menu))
766 tem = Fkeymapp (Fcar (menu));
767 if (!NILP (keymap))
768 {
769 /* We were given a keymap. Extract menu info from the keymap. */
770 Lisp_Object prompt;
771 keymap = get_keymap (menu);
772
773 /* Extract the detailed info to make one pane. */
774 keymap_panes (&menu, 1, NILP (position));
775
776 /* Search for a string appearing directly as an element of the keymap.
777 That string is the title of the menu. */
778 prompt = map_prompt (keymap);
779 if (NILP (title) && !NILP (prompt))
780 title = prompt;
781
782 /* Make that be the pane title of the first pane. */
783 if (!NILP (prompt) && menu_items_n_panes >= 0)
784 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
785
786 keymaps = 1;
787 }
788 else if (!NILP (tem))
789 {
790 /* We were given a list of keymaps. */
791 int nmaps = XFASTINT (Flength (menu));
792 Lisp_Object *maps
793 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
794 int i;
795
796 title = Qnil;
797
798 /* The first keymap that has a prompt string
799 supplies the menu title. */
800 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
801 {
802 Lisp_Object prompt;
803
804 maps[i++] = keymap = get_keymap (Fcar (tem));
805
806 prompt = map_prompt (keymap);
807 if (NILP (title) && !NILP (prompt))
808 title = prompt;
809 }
810
811 /* Extract the detailed info to make one pane. */
812 keymap_panes (maps, nmaps, NILP (position));
813
814 /* Make the title be the pane title of the first pane. */
815 if (!NILP (title) && menu_items_n_panes >= 0)
816 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
817
818 keymaps = 1;
819 }
820 else
821 {
822 /* We were given an old-fashioned menu. */
823 title = Fcar (menu);
824 CHECK_STRING (title, 1);
825
826 list_of_panes (Fcdr (menu));
827
828 keymaps = 0;
829 }
830
831 if (NILP (position))
832 {
833 discard_menu_items ();
834 UNGCPRO;
835 return Qnil;
836 }
837
838 #ifdef HAVE_MENUS
839 /* Display them in a menu. */
840 BLOCK_INPUT;
841
842 selection = xmenu_show (f, xpos, ypos, for_click,
843 keymaps, title, &error_name);
844 UNBLOCK_INPUT;
845
846 discard_menu_items ();
847
848 UNGCPRO;
849 #endif /* HAVE_MENUS */
850
851 if (error_name) error (error_name);
852 return selection;
853 }
854
855 #ifdef HAVE_MENUS
856
857 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
858 "Pop up a dialog box and return user's selection.\n\
859 POSITION specifies which frame to use.\n\
860 This is normally a mouse button event or a window or frame.\n\
861 If POSITION is t, it means to use the frame the mouse is on.\n\
862 The dialog box appears in the middle of the specified frame.\n\
863 \n\
864 CONTENTS specifies the alternatives to display in the dialog box.\n\
865 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
866 Each ITEM is a cons cell (STRING . VALUE).\n\
867 The return value is VALUE from the chosen item.\n\n\
868 An ITEM may also be just a string--that makes a nonselectable item.\n\
869 An ITEM may also be nil--that means to put all preceding items\n\
870 on the left of the dialog box and all following items on the right.\n\
871 \(By default, approximately half appear on each side.)")
872 (position, contents)
873 Lisp_Object position, contents;
874 {
875 FRAME_PTR f;
876 Lisp_Object window;
877
878 check_x ();
879
880 /* Decode the first argument: find the window or frame to use. */
881 if (EQ (position, Qt)
882 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
883 {
884 #if 0 /* Using the frame the mouse is on may not be right. */
885 /* Use the mouse's current position. */
886 FRAME_PTR new_f = selected_frame;
887 Lisp_Object bar_window;
888 int part;
889 unsigned long time;
890 Lisp_Object x, y;
891
892 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
893
894 if (new_f != 0)
895 XSETFRAME (window, new_f);
896 else
897 window = selected_window;
898 #endif
899 window = selected_window;
900 }
901 else if (CONSP (position))
902 {
903 Lisp_Object tem;
904 tem = Fcar (position);
905 if (CONSP (tem))
906 window = Fcar (Fcdr (position));
907 else
908 {
909 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
910 window = Fcar (tem); /* POSN_WINDOW (tem) */
911 }
912 }
913 else if (WINDOWP (position) || FRAMEP (position))
914 window = position;
915 else
916 window = Qnil;
917
918 /* Decode where to put the menu. */
919
920 if (FRAMEP (window))
921 f = XFRAME (window);
922 else if (WINDOWP (window))
923 {
924 CHECK_LIVE_WINDOW (window, 0);
925 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
926 }
927 else
928 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
929 but I don't want to make one now. */
930 CHECK_WINDOW (window, 0);
931
932 #ifndef USE_X_TOOLKIT
933 /* Display a menu with these alternatives
934 in the middle of frame F. */
935 {
936 Lisp_Object x, y, frame, newpos;
937 XSETFRAME (frame, f);
938 XSETINT (x, x_pixel_width (f) / 2);
939 XSETINT (y, x_pixel_height (f) / 2);
940 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
941
942 return Fx_popup_menu (newpos,
943 Fcons (Fcar (contents), Fcons (contents, Qnil)));
944 }
945 #else
946 {
947 Lisp_Object title;
948 char *error_name;
949 Lisp_Object selection;
950
951 /* Decode the dialog items from what was specified. */
952 title = Fcar (contents);
953 CHECK_STRING (title, 1);
954
955 list_of_panes (Fcons (contents, Qnil));
956
957 /* Display them in a dialog box. */
958 BLOCK_INPUT;
959 selection = xdialog_show (f, 0, title, &error_name);
960 UNBLOCK_INPUT;
961
962 discard_menu_items ();
963
964 if (error_name) error (error_name);
965 return selection;
966 }
967 #endif
968 }
969 \f
970 #ifdef USE_X_TOOLKIT
971
972 /* Loop in Xt until the menu pulldown or dialog popup has been
973 popped down (deactivated). This is used for x-popup-menu
974 and x-popup-dialog; it is not used for the menu bar any more.
975
976 NOTE: All calls to popup_get_selection should be protected
977 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
978
979 void
980 popup_get_selection (initial_event, dpyinfo, id)
981 XEvent *initial_event;
982 struct x_display_info *dpyinfo;
983 LWLIB_ID id;
984 {
985 XEvent event;
986
987 /* Define a queue to save up for later unreading
988 all X events that don't pertain to the menu. */
989 struct event_queue
990 {
991 XEvent event;
992 struct event_queue *next;
993 };
994
995 struct event_queue *queue = NULL;
996 struct event_queue *queue_tmp;
997
998 if (initial_event)
999 event = *initial_event;
1000 else
1001 XtAppNextEvent (Xt_app_con, &event);
1002
1003 while (1)
1004 {
1005 /* Handle expose events for editor frames right away. */
1006 if (event.type == Expose)
1007 process_expose_from_menu (event);
1008 /* Make sure we don't consider buttons grabbed after menu goes.
1009 And make sure to deactivate for any ButtonRelease,
1010 even if XtDispatchEvent doesn't do that. */
1011 else if (event.type == ButtonRelease
1012 && dpyinfo->display == event.xbutton.display)
1013 {
1014 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1015 popup_activated_flag = 0;
1016 #ifdef USE_MOTIF /* Pretending that the event came from a
1017 Btn1Down seems the only way to convince Motif to
1018 activate its callbacks; setting the XmNmenuPost
1019 isn't working. --marcus@sysc.pdx.edu. */
1020 event.xbutton.button = 1;
1021 #endif
1022 }
1023 /* If the user presses a key, deactivate the menu.
1024 The user is likely to do that if we get wedged. */
1025 else if (event.type == KeyPress
1026 && dpyinfo->display == event.xbutton.display)
1027 {
1028 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1029 if (!IsModifierKey (keysym))
1030 {
1031 popup_activated_flag = 0;
1032 break;
1033 }
1034 }
1035 /* Button presses outside the menu also pop it down. */
1036 else if (event.type == ButtonPress
1037 && event.xany.display == dpyinfo->display
1038 && x_any_window_to_frame (dpyinfo, event.xany.window))
1039 {
1040 popup_activated_flag = 0;
1041 break;
1042 }
1043
1044 /* Queue all events not for this popup,
1045 except for Expose, which we've already handled, and ButtonRelease.
1046 Note that the X window is associated with the frame if this
1047 is a menu bar popup, but not if it's a dialog box. So we use
1048 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1049 if (event.type != Expose
1050 && !(event.type == ButtonRelease
1051 && dpyinfo->display == event.xbutton.display)
1052 && (event.xany.display != dpyinfo->display
1053 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1054 {
1055 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1056
1057 if (queue_tmp != NULL)
1058 {
1059 queue_tmp->event = event;
1060 queue_tmp->next = queue;
1061 queue = queue_tmp;
1062 }
1063 }
1064 else
1065 XtDispatchEvent (&event);
1066
1067 if (!popup_activated ())
1068 break;
1069 XtAppNextEvent (Xt_app_con, &event);
1070 }
1071
1072 /* Unread any events that we got but did not handle. */
1073 while (queue != NULL)
1074 {
1075 queue_tmp = queue;
1076 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1077 queue = queue_tmp->next;
1078 free ((char *)queue_tmp);
1079 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1080 interrupt_input_pending = 1;
1081 }
1082 }
1083
1084 /* Activate the menu bar of frame F.
1085 This is called from keyboard.c when it gets the
1086 menu_bar_activate_event out of the Emacs event queue.
1087
1088 To activate the menu bar, we use the X button-press event
1089 that was saved in saved_menu_event.
1090 That makes the toolkit do its thing.
1091
1092 But first we recompute the menu bar contents (the whole tree).
1093
1094 The reason for saving the button event until here, instead of
1095 passing it to the toolkit right away, is that we can safely
1096 execute Lisp code. */
1097
1098 x_activate_menubar (f)
1099 FRAME_PTR f;
1100 {
1101 if (!f->output_data.x->saved_menu_event->type)
1102 return;
1103
1104 set_frame_menubar (f, 0, 1);
1105 BLOCK_INPUT;
1106 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1107 UNBLOCK_INPUT;
1108 #ifdef USE_MOTIF
1109 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1110 pending_menu_activation = 1;
1111 #endif
1112
1113 /* Ignore this if we get it a second time. */
1114 f->output_data.x->saved_menu_event->type = 0;
1115 }
1116
1117 /* Detect if a dialog or menu has been posted. */
1118
1119 int
1120 popup_activated ()
1121 {
1122 return popup_activated_flag;
1123 }
1124
1125
1126 /* This callback is invoked when the user selects a menubar cascade
1127 pushbutton, but before the pulldown menu is posted. */
1128
1129 static void
1130 popup_activate_callback (widget, id, client_data)
1131 Widget widget;
1132 LWLIB_ID id;
1133 XtPointer client_data;
1134 {
1135 popup_activated_flag = 1;
1136 }
1137
1138 /* This callback is called from the menu bar pulldown menu
1139 when the user makes a selection.
1140 Figure out what the user chose
1141 and put the appropriate events into the keyboard buffer. */
1142
1143 static void
1144 menubar_selection_callback (widget, id, client_data)
1145 Widget widget;
1146 LWLIB_ID id;
1147 XtPointer client_data;
1148 {
1149 Lisp_Object prefix, entry;
1150 FRAME_PTR f = menubar_id_to_frame (id);
1151 Lisp_Object vector;
1152 Lisp_Object *subprefix_stack;
1153 int submenu_depth = 0;
1154 int i;
1155
1156 if (!f)
1157 return;
1158 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1159 vector = f->menu_bar_vector;
1160 prefix = Qnil;
1161 i = 0;
1162 while (i < f->menu_bar_items_used)
1163 {
1164 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1165 {
1166 subprefix_stack[submenu_depth++] = prefix;
1167 prefix = entry;
1168 i++;
1169 }
1170 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1171 {
1172 prefix = subprefix_stack[--submenu_depth];
1173 i++;
1174 }
1175 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1176 {
1177 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1178 i += MENU_ITEMS_PANE_LENGTH;
1179 }
1180 else
1181 {
1182 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1183 /* The EMACS_INT cast avoids a warning. There's no problem
1184 as long as pointers have enough bits to hold small integers. */
1185 if ((int) (EMACS_INT) client_data == i)
1186 {
1187 int j;
1188 struct input_event buf;
1189 Lisp_Object frame;
1190
1191 XSETFRAME (frame, f);
1192 buf.kind = menu_bar_event;
1193 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1194 kbd_buffer_store_event (&buf);
1195
1196 for (j = 0; j < submenu_depth; j++)
1197 if (!NILP (subprefix_stack[j]))
1198 {
1199 buf.kind = menu_bar_event;
1200 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1201 kbd_buffer_store_event (&buf);
1202 }
1203
1204 if (!NILP (prefix))
1205 {
1206 buf.kind = menu_bar_event;
1207 buf.frame_or_window = Fcons (frame, prefix);
1208 kbd_buffer_store_event (&buf);
1209 }
1210
1211 buf.kind = menu_bar_event;
1212 buf.frame_or_window = Fcons (frame, entry);
1213 kbd_buffer_store_event (&buf);
1214
1215 return;
1216 }
1217 i += MENU_ITEMS_ITEM_LENGTH;
1218 }
1219 }
1220 }
1221
1222 /* This callback is invoked when a dialog or menu is finished being
1223 used and has been unposted. */
1224
1225 static void
1226 popup_deactivate_callback (widget, id, client_data)
1227 Widget widget;
1228 LWLIB_ID id;
1229 XtPointer client_data;
1230 {
1231 popup_activated_flag = 0;
1232 }
1233
1234 /* Allocate a widget_value, blocking input. */
1235
1236 widget_value *
1237 xmalloc_widget_value ()
1238 {
1239 widget_value *value;
1240
1241 BLOCK_INPUT;
1242 value = malloc_widget_value ();
1243 UNBLOCK_INPUT;
1244
1245 return value;
1246 }
1247
1248 /* This recursively calls free_widget_value on the tree of widgets.
1249 It must free all data that was malloc'ed for these widget_values.
1250 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1251 must be left alone. */
1252
1253 void
1254 free_menubar_widget_value_tree (wv)
1255 widget_value *wv;
1256 {
1257 if (! wv) return;
1258
1259 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1260
1261 if (wv->contents && (wv->contents != (widget_value*)1))
1262 {
1263 free_menubar_widget_value_tree (wv->contents);
1264 wv->contents = (widget_value *) 0xDEADBEEF;
1265 }
1266 if (wv->next)
1267 {
1268 free_menubar_widget_value_tree (wv->next);
1269 wv->next = (widget_value *) 0xDEADBEEF;
1270 }
1271 BLOCK_INPUT;
1272 free_widget_value (wv);
1273 UNBLOCK_INPUT;
1274 }
1275 \f
1276 /* Return a tree of widget_value structures for a menu bar item
1277 whose event type is ITEM_KEY (with string ITEM_NAME)
1278 and whose contents come from the list of keymaps MAPS. */
1279
1280 static widget_value *
1281 single_submenu (item_key, item_name, maps)
1282 Lisp_Object item_key, item_name, maps;
1283 {
1284 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1285 int i;
1286 int submenu_depth = 0;
1287 Lisp_Object length;
1288 int len;
1289 Lisp_Object *mapvec;
1290 widget_value **submenu_stack;
1291 int mapno;
1292 int previous_items = menu_items_used;
1293 int top_level_items = 0;
1294
1295 length = Flength (maps);
1296 len = XINT (length);
1297
1298 /* Convert the list MAPS into a vector MAPVEC. */
1299 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1300 for (i = 0; i < len; i++)
1301 {
1302 mapvec[i] = Fcar (maps);
1303 maps = Fcdr (maps);
1304 }
1305
1306 menu_items_n_panes = 0;
1307
1308 /* Loop over the given keymaps, making a pane for each map.
1309 But don't make a pane that is empty--ignore that map instead. */
1310 for (i = 0; i < len; i++)
1311 {
1312 if (SYMBOLP (mapvec[i])
1313 || (CONSP (mapvec[i])
1314 && NILP (Fkeymapp (mapvec[i]))))
1315 {
1316 /* Here we have a command at top level in the menu bar
1317 as opposed to a submenu. */
1318 top_level_items = 1;
1319 push_menu_pane (Qnil, Qnil);
1320 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1321 }
1322 else
1323 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1324 }
1325
1326 /* Create a tree of widget_value objects
1327 representing the panes and their items. */
1328
1329 submenu_stack
1330 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1331 wv = xmalloc_widget_value ();
1332 wv->name = "menu";
1333 wv->value = 0;
1334 wv->enabled = 1;
1335 first_wv = wv;
1336 save_wv = 0;
1337 prev_wv = 0;
1338
1339 /* Loop over all panes and items made during this call
1340 and construct a tree of widget_value objects.
1341 Ignore the panes and items made by previous calls to
1342 single_submenu, even though those are also in menu_items. */
1343 i = previous_items;
1344 while (i < menu_items_used)
1345 {
1346 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1347 {
1348 submenu_stack[submenu_depth++] = save_wv;
1349 save_wv = prev_wv;
1350 prev_wv = 0;
1351 i++;
1352 }
1353 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1354 {
1355 prev_wv = save_wv;
1356 save_wv = submenu_stack[--submenu_depth];
1357 i++;
1358 }
1359 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1360 && submenu_depth != 0)
1361 i += MENU_ITEMS_PANE_LENGTH;
1362 /* Ignore a nil in the item list.
1363 It's meaningful only for dialog boxes. */
1364 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1365 i += 1;
1366 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1367 {
1368 /* Create a new pane. */
1369 Lisp_Object pane_name, prefix;
1370 char *pane_string;
1371 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1372 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1373 pane_string = (NILP (pane_name)
1374 ? "" : (char *) XSTRING (pane_name)->data);
1375 /* If there is just one top-level pane, put all its items directly
1376 under the top-level menu. */
1377 if (menu_items_n_panes == 1)
1378 pane_string = "";
1379
1380 /* If the pane has a meaningful name,
1381 make the pane a top-level menu item
1382 with its items as a submenu beneath it. */
1383 if (strcmp (pane_string, ""))
1384 {
1385 wv = xmalloc_widget_value ();
1386 if (save_wv)
1387 save_wv->next = wv;
1388 else
1389 first_wv->contents = wv;
1390 wv->name = pane_string;
1391 /* Ignore the @ that means "separate pane".
1392 This is a kludge, but this isn't worth more time. */
1393 if (!NILP (prefix) && wv->name[0] == '@')
1394 wv->name++;
1395 wv->value = 0;
1396 wv->enabled = 1;
1397 }
1398 save_wv = wv;
1399 prev_wv = 0;
1400 i += MENU_ITEMS_PANE_LENGTH;
1401 }
1402 else
1403 {
1404 /* Create a new item within current pane. */
1405 Lisp_Object item_name, enable, descrip, def;
1406 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1407 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1408 descrip
1409 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1410 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1411
1412 wv = xmalloc_widget_value ();
1413 if (prev_wv)
1414 prev_wv->next = wv;
1415 else
1416 save_wv->contents = wv;
1417
1418 wv->name = (char *) XSTRING (item_name)->data;
1419 if (!NILP (descrip))
1420 wv->key = (char *) XSTRING (descrip)->data;
1421 wv->value = 0;
1422 /* The EMACS_INT cast avoids a warning. There's no problem
1423 as long as pointers have enough bits to hold small integers. */
1424 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1425 wv->enabled = !NILP (enable);
1426 prev_wv = wv;
1427
1428 i += MENU_ITEMS_ITEM_LENGTH;
1429 }
1430 }
1431
1432 /* If we have just one "menu item"
1433 that was originally a button, return it by itself. */
1434 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1435 {
1436 wv = first_wv->contents;
1437 free_widget_value (first_wv);
1438 return wv;
1439 }
1440
1441 return first_wv;
1442 }
1443 \f
1444 extern void EmacsFrameSetCharSize ();
1445
1446 /* Recompute all the widgets of frame F, when the menu bar
1447 has been changed. */
1448
1449 static void
1450 update_frame_menubar (f)
1451 FRAME_PTR f;
1452 {
1453 struct x_output *x = f->output_data.x;
1454 int columns, rows;
1455 int menubar_changed;
1456
1457 Dimension shell_height;
1458
1459 /* We assume the menubar contents has changed if the global flag is set,
1460 or if the current buffer has changed, or if the menubar has never
1461 been updated before.
1462 */
1463 menubar_changed = (x->menubar_widget
1464 && !XtIsManaged (x->menubar_widget));
1465
1466 if (! (menubar_changed))
1467 return;
1468
1469 BLOCK_INPUT;
1470 /* Save the size of the frame because the pane widget doesn't accept to
1471 resize itself. So force it. */
1472 columns = f->width;
1473 rows = f->height;
1474
1475 /* Do the voodoo which means "I'm changing lots of things, don't try to
1476 refigure sizes until I'm done." */
1477 lw_refigure_widget (x->column_widget, False);
1478
1479 /* the order in which children are managed is the top to
1480 bottom order in which they are displayed in the paned window.
1481 First, remove the text-area widget.
1482 */
1483 XtUnmanageChild (x->edit_widget);
1484
1485 /* remove the menubar that is there now, and put up the menubar that
1486 should be there.
1487 */
1488 if (menubar_changed)
1489 {
1490 XtManageChild (x->menubar_widget);
1491 XtMapWidget (x->menubar_widget);
1492 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1493 }
1494
1495 /* Re-manage the text-area widget, and then thrash the sizes. */
1496 XtManageChild (x->edit_widget);
1497 lw_refigure_widget (x->column_widget, True);
1498
1499 /* Force the pane widget to resize itself with the right values. */
1500 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1501
1502 UNBLOCK_INPUT;
1503 }
1504
1505 /* Set the contents of the menubar widgets of frame F.
1506 The argument FIRST_TIME is currently ignored;
1507 it is set the first time this is called, from initialize_frame_menubar. */
1508
1509 void
1510 set_frame_menubar (f, first_time, deep_p)
1511 FRAME_PTR f;
1512 int first_time;
1513 int deep_p;
1514 {
1515 Widget menubar_widget = f->output_data.x->menubar_widget;
1516 Lisp_Object tail, items, frame;
1517 widget_value *wv, *first_wv, *prev_wv = 0;
1518 int i;
1519 LWLIB_ID id;
1520
1521 XSETFRAME (Vmenu_updating_frame, f);
1522
1523 if (f->output_data.x->id == 0)
1524 f->output_data.x->id = next_menubar_widget_id++;
1525 id = f->output_data.x->id;
1526
1527 if (! menubar_widget)
1528 deep_p = 1;
1529 else if (pending_menu_activation && !deep_p)
1530 deep_p = 1;
1531 /* Make the first call for any given frame always go deep. */
1532 else if (!f->output_data.x->saved_menu_event && !deep_p)
1533 {
1534 deep_p = 1;
1535 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1536 f->output_data.x->saved_menu_event->type = 0;
1537 }
1538
1539 wv = xmalloc_widget_value ();
1540 wv->name = "menubar";
1541 wv->value = 0;
1542 wv->enabled = 1;
1543 first_wv = wv;
1544
1545 if (deep_p)
1546 {
1547 /* Make a widget-value tree representing the entire menu trees. */
1548
1549 struct buffer *prev = current_buffer;
1550 Lisp_Object buffer;
1551 int specpdl_count = specpdl_ptr - specpdl;
1552 int previous_menu_items_used = f->menu_bar_items_used;
1553 Lisp_Object *previous_items
1554 = (Lisp_Object *) alloca (previous_menu_items_used
1555 * sizeof (Lisp_Object));
1556
1557 /* If we are making a new widget, its contents are empty,
1558 do always reinitialize them. */
1559 if (! menubar_widget)
1560 previous_menu_items_used = 0;
1561
1562 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1563 specbind (Qinhibit_quit, Qt);
1564 /* Don't let the debugger step into this code
1565 because it is not reentrant. */
1566 specbind (Qdebug_on_next_call, Qnil);
1567
1568 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1569 if (NILP (Voverriding_local_map_menu_flag))
1570 {
1571 specbind (Qoverriding_terminal_local_map, Qnil);
1572 specbind (Qoverriding_local_map, Qnil);
1573 }
1574
1575 set_buffer_internal_1 (XBUFFER (buffer));
1576
1577 /* Run the Lucid hook. */
1578 call1 (Vrun_hooks, Qactivate_menubar_hook);
1579 /* If it has changed current-menubar from previous value,
1580 really recompute the menubar from the value. */
1581 if (! NILP (Vlucid_menu_bar_dirty_flag))
1582 call0 (Qrecompute_lucid_menubar);
1583 safe_run_hooks (Qmenu_bar_update_hook);
1584 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1585
1586 items = FRAME_MENU_BAR_ITEMS (f);
1587
1588 inhibit_garbage_collection ();
1589
1590 /* Save the frame's previous menu bar contents data. */
1591 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1592 previous_menu_items_used * sizeof (Lisp_Object));
1593
1594 /* Fill in the current menu bar contents. */
1595 menu_items = f->menu_bar_vector;
1596 menu_items_allocated = XVECTOR (menu_items)->size;
1597 init_menu_items ();
1598 for (i = 0; i < XVECTOR (items)->size; i += 4)
1599 {
1600 Lisp_Object key, string, maps;
1601
1602 key = XVECTOR (items)->contents[i];
1603 string = XVECTOR (items)->contents[i + 1];
1604 maps = XVECTOR (items)->contents[i + 2];
1605 if (NILP (string))
1606 break;
1607
1608 wv = single_submenu (key, string, maps);
1609 if (prev_wv)
1610 prev_wv->next = wv;
1611 else
1612 first_wv->contents = wv;
1613 /* Don't set wv->name here; GC during the loop might relocate it. */
1614 wv->enabled = 1;
1615 prev_wv = wv;
1616 }
1617
1618 finish_menu_items ();
1619
1620 set_buffer_internal_1 (prev);
1621 unbind_to (specpdl_count, Qnil);
1622
1623 /* If there has been no change in the Lisp-level contents
1624 of the menu bar, skip redisplaying it. Just exit. */
1625
1626 for (i = 0; i < previous_menu_items_used; i++)
1627 if (menu_items_used == i
1628 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1629 break;
1630 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1631 {
1632 free_menubar_widget_value_tree (first_wv);
1633 menu_items = Qnil;
1634
1635 return;
1636 }
1637
1638 /* Now GC cannot happen during the lifetime of the widget_value,
1639 so it's safe to store data from a Lisp_String. */
1640 wv = first_wv->contents;
1641 for (i = 0; i < XVECTOR (items)->size; i += 4)
1642 {
1643 Lisp_Object string;
1644 string = XVECTOR (items)->contents[i + 1];
1645 if (NILP (string))
1646 break;
1647 wv->name = (char *) XSTRING (string)->data;
1648 wv = wv->next;
1649 }
1650
1651 f->menu_bar_vector = menu_items;
1652 f->menu_bar_items_used = menu_items_used;
1653 menu_items = Qnil;
1654 }
1655 else
1656 {
1657 /* Make a widget-value tree containing
1658 just the top level menu bar strings. */
1659
1660 items = FRAME_MENU_BAR_ITEMS (f);
1661 for (i = 0; i < XVECTOR (items)->size; i += 4)
1662 {
1663 Lisp_Object string;
1664
1665 string = XVECTOR (items)->contents[i + 1];
1666 if (NILP (string))
1667 break;
1668
1669 wv = xmalloc_widget_value ();
1670 wv->name = (char *) XSTRING (string)->data;
1671 wv->value = 0;
1672 wv->enabled = 1;
1673 /* This prevents lwlib from assuming this
1674 menu item is really supposed to be empty. */
1675 /* The EMACS_INT cast avoids a warning.
1676 This value just has to be different from small integers. */
1677 wv->call_data = (void *) (EMACS_INT) (-1);
1678
1679 if (prev_wv)
1680 prev_wv->next = wv;
1681 else
1682 first_wv->contents = wv;
1683 prev_wv = wv;
1684 }
1685
1686 /* Forget what we thought we knew about what is in the
1687 detailed contents of the menu bar menus.
1688 Changing the top level always destroys the contents. */
1689 f->menu_bar_items_used = 0;
1690 }
1691
1692 /* Create or update the menu bar widget. */
1693
1694 BLOCK_INPUT;
1695
1696 if (menubar_widget)
1697 {
1698 /* Disable resizing (done for Motif!) */
1699 lw_allow_resizing (f->output_data.x->widget, False);
1700
1701 /* The third arg is DEEP_P, which says to consider the entire
1702 menu trees we supply, rather than just the menu bar item names. */
1703 lw_modify_all_widgets (id, first_wv, deep_p);
1704
1705 /* Re-enable the edit widget to resize. */
1706 lw_allow_resizing (f->output_data.x->widget, True);
1707 }
1708 else
1709 {
1710 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1711 f->output_data.x->column_widget,
1712 0,
1713 popup_activate_callback,
1714 menubar_selection_callback,
1715 popup_deactivate_callback);
1716 f->output_data.x->menubar_widget = menubar_widget;
1717 }
1718
1719 {
1720 int menubar_size
1721 = (f->output_data.x->menubar_widget
1722 ? (f->output_data.x->menubar_widget->core.height
1723 + f->output_data.x->menubar_widget->core.border_width)
1724 : 0);
1725
1726 #if 0 /* Experimentally, we now get the right results
1727 for -geometry -0-0 without this. 24 Aug 96, rms. */
1728 #ifdef USE_LUCID
1729 if (FRAME_EXTERNAL_MENU_BAR (f))
1730 {
1731 Dimension ibw = 0;
1732 XtVaGetValues (f->output_data.x->column_widget,
1733 XtNinternalBorderWidth, &ibw, NULL);
1734 menubar_size += ibw;
1735 }
1736 #endif /* USE_LUCID */
1737 #endif /* 0 */
1738
1739 f->output_data.x->menubar_height = menubar_size;
1740 }
1741
1742 free_menubar_widget_value_tree (first_wv);
1743
1744 update_frame_menubar (f);
1745
1746 UNBLOCK_INPUT;
1747 }
1748
1749 /* Called from Fx_create_frame to create the initial menubar of a frame
1750 before it is mapped, so that the window is mapped with the menubar already
1751 there instead of us tacking it on later and thrashing the window after it
1752 is visible. */
1753
1754 void
1755 initialize_frame_menubar (f)
1756 FRAME_PTR f;
1757 {
1758 /* This function is called before the first chance to redisplay
1759 the frame. It has to be, so the frame will have the right size. */
1760 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1761 set_frame_menubar (f, 1, 1);
1762 }
1763
1764 /* Get rid of the menu bar of frame F, and free its storage.
1765 This is used when deleting a frame, and when turning off the menu bar. */
1766
1767 void
1768 free_frame_menubar (f)
1769 FRAME_PTR f;
1770 {
1771 Widget menubar_widget;
1772 int id;
1773
1774 menubar_widget = f->output_data.x->menubar_widget;
1775
1776 f->output_data.x->menubar_height = 0;
1777
1778 if (menubar_widget)
1779 {
1780 BLOCK_INPUT;
1781 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1782 UNBLOCK_INPUT;
1783 }
1784 }
1785
1786 #endif /* USE_X_TOOLKIT */
1787 \f
1788 /* xmenu_show actually displays a menu using the panes and items in menu_items
1789 and returns the value selected from it.
1790 There are two versions of xmenu_show, one for Xt and one for Xlib.
1791 Both assume input is blocked by the caller. */
1792
1793 /* F is the frame the menu is for.
1794 X and Y are the frame-relative specified position,
1795 relative to the inside upper left corner of the frame F.
1796 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1797 KEYMAPS is 1 if this menu was specified with keymaps;
1798 in that case, we return a list containing the chosen item's value
1799 and perhaps also the pane's prefix.
1800 TITLE is the specified menu title.
1801 ERROR is a place to store an error message string in case of failure.
1802 (We return nil on failure, but the value doesn't actually matter.) */
1803
1804 #ifdef USE_X_TOOLKIT
1805
1806 /* We need a unique id for each widget handled by the Lucid Widget
1807 library.
1808
1809 For the main windows, and popup menus, we use this counter,
1810 which we increment each time after use. This starts from 1<<16.
1811
1812 For menu bars, we use numbers starting at 0, counted in
1813 next_menubar_widget_id. */
1814 LWLIB_ID widget_id_tick;
1815
1816 #ifdef __STDC__
1817 static Lisp_Object *volatile menu_item_selection;
1818 #else
1819 static Lisp_Object *menu_item_selection;
1820 #endif
1821
1822 static void
1823 popup_selection_callback (widget, id, client_data)
1824 Widget widget;
1825 LWLIB_ID id;
1826 XtPointer client_data;
1827 {
1828 menu_item_selection = (Lisp_Object *) client_data;
1829 }
1830
1831 static Lisp_Object
1832 xmenu_show (f, x, y, for_click, keymaps, title, error)
1833 FRAME_PTR f;
1834 int x;
1835 int y;
1836 int for_click;
1837 int keymaps;
1838 Lisp_Object title;
1839 char **error;
1840 {
1841 int i;
1842 LWLIB_ID menu_id;
1843 Widget menu;
1844 Arg av[2];
1845 int ac = 0;
1846 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1847 widget_value **submenu_stack
1848 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1849 Lisp_Object *subprefix_stack
1850 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1851 int submenu_depth = 0;
1852 XButtonPressedEvent dummy;
1853
1854 int first_pane;
1855 int next_release_must_exit = 0;
1856
1857 *error = NULL;
1858
1859 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1860 {
1861 *error = "Empty menu";
1862 return Qnil;
1863 }
1864
1865 /* Create a tree of widget_value objects
1866 representing the panes and their items. */
1867 wv = xmalloc_widget_value ();
1868 wv->name = "menu";
1869 wv->value = 0;
1870 wv->enabled = 1;
1871 first_wv = wv;
1872 first_pane = 1;
1873
1874 /* Loop over all panes and items, filling in the tree. */
1875 i = 0;
1876 while (i < menu_items_used)
1877 {
1878 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1879 {
1880 submenu_stack[submenu_depth++] = save_wv;
1881 save_wv = prev_wv;
1882 prev_wv = 0;
1883 first_pane = 1;
1884 i++;
1885 }
1886 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1887 {
1888 prev_wv = save_wv;
1889 save_wv = submenu_stack[--submenu_depth];
1890 first_pane = 0;
1891 i++;
1892 }
1893 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1894 && submenu_depth != 0)
1895 i += MENU_ITEMS_PANE_LENGTH;
1896 /* Ignore a nil in the item list.
1897 It's meaningful only for dialog boxes. */
1898 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1899 i += 1;
1900 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1901 {
1902 /* Create a new pane. */
1903 Lisp_Object pane_name, prefix;
1904 char *pane_string;
1905 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1906 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1907 pane_string = (NILP (pane_name)
1908 ? "" : (char *) XSTRING (pane_name)->data);
1909 /* If there is just one top-level pane, put all its items directly
1910 under the top-level menu. */
1911 if (menu_items_n_panes == 1)
1912 pane_string = "";
1913
1914 /* If the pane has a meaningful name,
1915 make the pane a top-level menu item
1916 with its items as a submenu beneath it. */
1917 if (!keymaps && strcmp (pane_string, ""))
1918 {
1919 wv = xmalloc_widget_value ();
1920 if (save_wv)
1921 save_wv->next = wv;
1922 else
1923 first_wv->contents = wv;
1924 wv->name = pane_string;
1925 if (keymaps && !NILP (prefix))
1926 wv->name++;
1927 wv->value = 0;
1928 wv->enabled = 1;
1929 save_wv = wv;
1930 prev_wv = 0;
1931 }
1932 else if (first_pane)
1933 {
1934 save_wv = wv;
1935 prev_wv = 0;
1936 }
1937 first_pane = 0;
1938 i += MENU_ITEMS_PANE_LENGTH;
1939 }
1940 else
1941 {
1942 /* Create a new item within current pane. */
1943 Lisp_Object item_name, enable, descrip, def;
1944 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1945 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1946 descrip
1947 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1948 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1949
1950 wv = xmalloc_widget_value ();
1951 if (prev_wv)
1952 prev_wv->next = wv;
1953 else
1954 save_wv->contents = wv;
1955 wv->name = (char *) XSTRING (item_name)->data;
1956 if (!NILP (descrip))
1957 wv->key = (char *) XSTRING (descrip)->data;
1958 wv->value = 0;
1959 /* If this item has a null value,
1960 make the call_data null so that it won't display a box
1961 when the mouse is on it. */
1962 wv->call_data
1963 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
1964 wv->enabled = !NILP (enable);
1965 prev_wv = wv;
1966
1967 i += MENU_ITEMS_ITEM_LENGTH;
1968 }
1969 }
1970
1971 /* Deal with the title, if it is non-nil. */
1972 if (!NILP (title))
1973 {
1974 widget_value *wv_title = xmalloc_widget_value ();
1975 widget_value *wv_sep1 = xmalloc_widget_value ();
1976 widget_value *wv_sep2 = xmalloc_widget_value ();
1977
1978 wv_sep2->name = "--";
1979 wv_sep2->next = first_wv->contents;
1980
1981 wv_sep1->name = "--";
1982 wv_sep1->next = wv_sep2;
1983
1984 wv_title->name = (char *) XSTRING (title)->data;
1985 wv_title->enabled = True;
1986 wv_title->next = wv_sep1;
1987 first_wv->contents = wv_title;
1988 }
1989
1990 /* Actually create the menu. */
1991 menu_id = widget_id_tick++;
1992 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1993 f->output_data.x->widget, 1, 0,
1994 popup_selection_callback,
1995 popup_deactivate_callback);
1996
1997 /* Adjust coordinates to relative to the outer (window manager) window. */
1998 {
1999 Window child;
2000 int win_x = 0, win_y = 0;
2001
2002 /* Find the position of the outside upper-left corner of
2003 the inner window, with respect to the outer window. */
2004 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2005 {
2006 BLOCK_INPUT;
2007 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2008
2009 /* From-window, to-window. */
2010 f->output_data.x->window_desc,
2011 f->output_data.x->parent_desc,
2012
2013 /* From-position, to-position. */
2014 0, 0, &win_x, &win_y,
2015
2016 /* Child of window. */
2017 &child);
2018 UNBLOCK_INPUT;
2019 x += win_x;
2020 y += win_y;
2021 }
2022 }
2023
2024 /* Adjust coordinates to be root-window-relative. */
2025 x += f->output_data.x->left_pos;
2026 y += f->output_data.x->top_pos;
2027
2028 dummy.type = ButtonPress;
2029 dummy.serial = 0;
2030 dummy.send_event = 0;
2031 dummy.display = FRAME_X_DISPLAY (f);
2032 dummy.time = CurrentTime;
2033 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2034 dummy.window = dummy.root;
2035 dummy.subwindow = dummy.root;
2036 dummy.x_root = x;
2037 dummy.y_root = y;
2038 dummy.x = x;
2039 dummy.y = y;
2040 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2041 dummy.button = 0;
2042 for (i = 0; i < 5; i++)
2043 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2044 dummy.button = i;
2045
2046 /* Don't allow any geometry request from the user. */
2047 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2048 XtSetValues (menu, av, ac);
2049
2050 /* Free the widget_value objects we used to specify the contents. */
2051 free_menubar_widget_value_tree (first_wv);
2052
2053 /* No selection has been chosen yet. */
2054 menu_item_selection = 0;
2055
2056 /* Display the menu. */
2057 lw_popup_menu (menu, &dummy);
2058 popup_activated_flag = 1;
2059
2060 /* Process events that apply to the menu. */
2061 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2062
2063 /* fp turned off the following statement and wrote a comment
2064 that it is unnecessary--that the menu has already disappeared.
2065 Nowadays the menu disappears ok, all right, but
2066 we need to delete the widgets or multiple ones will pile up. */
2067 lw_destroy_all_widgets (menu_id);
2068
2069 /* Find the selected item, and its pane, to return
2070 the proper value. */
2071 if (menu_item_selection != 0)
2072 {
2073 Lisp_Object prefix, entry;
2074
2075 prefix = Qnil;
2076 i = 0;
2077 while (i < menu_items_used)
2078 {
2079 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2080 {
2081 subprefix_stack[submenu_depth++] = prefix;
2082 prefix = entry;
2083 i++;
2084 }
2085 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2086 {
2087 prefix = subprefix_stack[--submenu_depth];
2088 i++;
2089 }
2090 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2091 {
2092 prefix
2093 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2094 i += MENU_ITEMS_PANE_LENGTH;
2095 }
2096 /* Ignore a nil in the item list.
2097 It's meaningful only for dialog boxes. */
2098 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2099 i += 1;
2100 else
2101 {
2102 entry
2103 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2104 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2105 {
2106 if (keymaps != 0)
2107 {
2108 int j;
2109
2110 entry = Fcons (entry, Qnil);
2111 if (!NILP (prefix))
2112 entry = Fcons (prefix, entry);
2113 for (j = submenu_depth - 1; j >= 0; j--)
2114 if (!NILP (subprefix_stack[j]))
2115 entry = Fcons (subprefix_stack[j], entry);
2116 }
2117 return entry;
2118 }
2119 i += MENU_ITEMS_ITEM_LENGTH;
2120 }
2121 }
2122 }
2123
2124 return Qnil;
2125 }
2126 \f
2127 static void
2128 dialog_selection_callback (widget, id, client_data)
2129 Widget widget;
2130 LWLIB_ID id;
2131 XtPointer client_data;
2132 {
2133 /* The EMACS_INT cast avoids a warning. There's no problem
2134 as long as pointers have enough bits to hold small integers. */
2135 if ((int) (EMACS_INT) client_data != -1)
2136 menu_item_selection = (Lisp_Object *) client_data;
2137 BLOCK_INPUT;
2138 lw_destroy_all_widgets (id);
2139 UNBLOCK_INPUT;
2140 popup_activated_flag = 0;
2141 }
2142
2143 static char * button_names [] = {
2144 "button1", "button2", "button3", "button4", "button5",
2145 "button6", "button7", "button8", "button9", "button10" };
2146
2147 static Lisp_Object
2148 xdialog_show (f, keymaps, title, error)
2149 FRAME_PTR f;
2150 int keymaps;
2151 Lisp_Object title;
2152 char **error;
2153 {
2154 int i, nb_buttons=0;
2155 LWLIB_ID dialog_id;
2156 Widget menu;
2157 char dialog_name[6];
2158
2159 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2160
2161 /* Number of elements seen so far, before boundary. */
2162 int left_count = 0;
2163 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2164 int boundary_seen = 0;
2165
2166 *error = NULL;
2167
2168 if (menu_items_n_panes > 1)
2169 {
2170 *error = "Multiple panes in dialog box";
2171 return Qnil;
2172 }
2173
2174 /* Create a tree of widget_value objects
2175 representing the text label and buttons. */
2176 {
2177 Lisp_Object pane_name, prefix;
2178 char *pane_string;
2179 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2180 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2181 pane_string = (NILP (pane_name)
2182 ? "" : (char *) XSTRING (pane_name)->data);
2183 prev_wv = xmalloc_widget_value ();
2184 prev_wv->value = pane_string;
2185 if (keymaps && !NILP (prefix))
2186 prev_wv->name++;
2187 prev_wv->enabled = 1;
2188 prev_wv->name = "message";
2189 first_wv = prev_wv;
2190
2191 /* Loop over all panes and items, filling in the tree. */
2192 i = MENU_ITEMS_PANE_LENGTH;
2193 while (i < menu_items_used)
2194 {
2195
2196 /* Create a new item within current pane. */
2197 Lisp_Object item_name, enable, descrip;
2198 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2199 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2200 descrip
2201 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2202
2203 if (NILP (item_name))
2204 {
2205 free_menubar_widget_value_tree (first_wv);
2206 *error = "Submenu in dialog items";
2207 return Qnil;
2208 }
2209 if (EQ (item_name, Qquote))
2210 {
2211 /* This is the boundary between left-side elts
2212 and right-side elts. Stop incrementing right_count. */
2213 boundary_seen = 1;
2214 i++;
2215 continue;
2216 }
2217 if (nb_buttons >= 9)
2218 {
2219 free_menubar_widget_value_tree (first_wv);
2220 *error = "Too many dialog items";
2221 return Qnil;
2222 }
2223
2224 wv = xmalloc_widget_value ();
2225 prev_wv->next = wv;
2226 wv->name = (char *) button_names[nb_buttons];
2227 if (!NILP (descrip))
2228 wv->key = (char *) XSTRING (descrip)->data;
2229 wv->value = (char *) XSTRING (item_name)->data;
2230 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2231 wv->enabled = !NILP (enable);
2232 prev_wv = wv;
2233
2234 if (! boundary_seen)
2235 left_count++;
2236
2237 nb_buttons++;
2238 i += MENU_ITEMS_ITEM_LENGTH;
2239 }
2240
2241 /* If the boundary was not specified,
2242 by default put half on the left and half on the right. */
2243 if (! boundary_seen)
2244 left_count = nb_buttons - nb_buttons / 2;
2245
2246 wv = xmalloc_widget_value ();
2247 wv->name = dialog_name;
2248
2249 /* Dialog boxes use a really stupid name encoding
2250 which specifies how many buttons to use
2251 and how many buttons are on the right.
2252 The Q means something also. */
2253 dialog_name[0] = 'Q';
2254 dialog_name[1] = '0' + nb_buttons;
2255 dialog_name[2] = 'B';
2256 dialog_name[3] = 'R';
2257 /* Number of buttons to put on the right. */
2258 dialog_name[4] = '0' + nb_buttons - left_count;
2259 dialog_name[5] = 0;
2260 wv->contents = first_wv;
2261 first_wv = wv;
2262 }
2263
2264 /* Actually create the dialog. */
2265 dialog_id = widget_id_tick++;
2266 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2267 f->output_data.x->widget, 1, 0,
2268 dialog_selection_callback, 0);
2269 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2270 /* Free the widget_value objects we used to specify the contents. */
2271 free_menubar_widget_value_tree (first_wv);
2272
2273 /* No selection has been chosen yet. */
2274 menu_item_selection = 0;
2275
2276 /* Display the menu. */
2277 lw_pop_up_all_widgets (dialog_id);
2278 popup_activated_flag = 1;
2279
2280 /* Process events that apply to the menu. */
2281 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2282
2283 lw_destroy_all_widgets (dialog_id);
2284
2285 /* Find the selected item, and its pane, to return
2286 the proper value. */
2287 if (menu_item_selection != 0)
2288 {
2289 Lisp_Object prefix;
2290
2291 prefix = Qnil;
2292 i = 0;
2293 while (i < menu_items_used)
2294 {
2295 Lisp_Object entry;
2296
2297 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2298 {
2299 prefix
2300 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2301 i += MENU_ITEMS_PANE_LENGTH;
2302 }
2303 else
2304 {
2305 entry
2306 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2307 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2308 {
2309 if (keymaps != 0)
2310 {
2311 entry = Fcons (entry, Qnil);
2312 if (!NILP (prefix))
2313 entry = Fcons (prefix, entry);
2314 }
2315 return entry;
2316 }
2317 i += MENU_ITEMS_ITEM_LENGTH;
2318 }
2319 }
2320 }
2321
2322 return Qnil;
2323 }
2324 #else /* not USE_X_TOOLKIT */
2325
2326 static Lisp_Object
2327 xmenu_show (f, x, y, for_click, keymaps, title, error)
2328 FRAME_PTR f;
2329 int x, y;
2330 int for_click;
2331 int keymaps;
2332 Lisp_Object title;
2333 char **error;
2334 {
2335 Window root;
2336 XMenu *menu;
2337 int pane, selidx, lpane, status;
2338 Lisp_Object entry, pane_prefix;
2339 char *datap;
2340 int ulx, uly, width, height;
2341 int dispwidth, dispheight;
2342 int i, j;
2343 int maxwidth;
2344 int dummy_int;
2345 unsigned int dummy_uint;
2346
2347 *error = 0;
2348 if (menu_items_n_panes == 0)
2349 return Qnil;
2350
2351 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2352 {
2353 *error = "Empty menu";
2354 return Qnil;
2355 }
2356
2357 /* Figure out which root window F is on. */
2358 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2359 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2360 &dummy_uint, &dummy_uint);
2361
2362 /* Make the menu on that window. */
2363 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2364 if (menu == NULL)
2365 {
2366 *error = "Can't create menu";
2367 return Qnil;
2368 }
2369
2370 #ifdef HAVE_X_WINDOWS
2371 /* Adjust coordinates to relative to the outer (window manager) window. */
2372 {
2373 Window child;
2374 int win_x = 0, win_y = 0;
2375
2376 /* Find the position of the outside upper-left corner of
2377 the inner window, with respect to the outer window. */
2378 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2379 {
2380 BLOCK_INPUT;
2381 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2382
2383 /* From-window, to-window. */
2384 f->output_data.x->window_desc,
2385 f->output_data.x->parent_desc,
2386
2387 /* From-position, to-position. */
2388 0, 0, &win_x, &win_y,
2389
2390 /* Child of window. */
2391 &child);
2392 UNBLOCK_INPUT;
2393 x += win_x;
2394 y += win_y;
2395 }
2396 }
2397 #endif /* HAVE_X_WINDOWS */
2398
2399 /* Adjust coordinates to be root-window-relative. */
2400 x += f->output_data.x->left_pos;
2401 y += f->output_data.x->top_pos;
2402
2403 /* Create all the necessary panes and their items. */
2404 i = 0;
2405 while (i < menu_items_used)
2406 {
2407 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2408 {
2409 /* Create a new pane. */
2410 Lisp_Object pane_name, prefix;
2411 char *pane_string;
2412
2413 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2414 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2415 pane_string = (NILP (pane_name)
2416 ? "" : (char *) XSTRING (pane_name)->data);
2417 if (keymaps && !NILP (prefix))
2418 pane_string++;
2419
2420 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2421 if (lpane == XM_FAILURE)
2422 {
2423 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2424 *error = "Can't create pane";
2425 return Qnil;
2426 }
2427 i += MENU_ITEMS_PANE_LENGTH;
2428
2429 /* Find the width of the widest item in this pane. */
2430 maxwidth = 0;
2431 j = i;
2432 while (j < menu_items_used)
2433 {
2434 Lisp_Object item;
2435 item = XVECTOR (menu_items)->contents[j];
2436 if (EQ (item, Qt))
2437 break;
2438 if (NILP (item))
2439 {
2440 j++;
2441 continue;
2442 }
2443 width = STRING_BYTES (XSTRING (item));
2444 if (width > maxwidth)
2445 maxwidth = width;
2446
2447 j += MENU_ITEMS_ITEM_LENGTH;
2448 }
2449 }
2450 /* Ignore a nil in the item list.
2451 It's meaningful only for dialog boxes. */
2452 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2453 i += 1;
2454 else
2455 {
2456 /* Create a new item within current pane. */
2457 Lisp_Object item_name, enable, descrip;
2458 unsigned char *item_data;
2459
2460 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2461 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2462 descrip
2463 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2464 if (!NILP (descrip))
2465 {
2466 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2467 #ifdef C_ALLOCA
2468 Lisp_Object spacer;
2469 spacer = Fmake_string (make_number (gap), make_number (' '));
2470 item_name = concat2 (item_name, spacer);
2471 item_name = concat2 (item_name, descrip);
2472 item_data = XSTRING (item_name)->data;
2473 #else
2474 /* if alloca is fast, use that to make the space,
2475 to reduce gc needs. */
2476 item_data
2477 = (unsigned char *) alloca (maxwidth
2478 + STRING_BYTES (XSTRING (descrip)) + 1);
2479 bcopy (XSTRING (item_name)->data, item_data,
2480 STRING_BYTES (XSTRING (item_name)));
2481 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2482 item_data[j] = ' ';
2483 bcopy (XSTRING (descrip)->data, item_data + j,
2484 STRING_BYTES (XSTRING (descrip)));
2485 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2486 #endif
2487 }
2488 else
2489 item_data = XSTRING (item_name)->data;
2490
2491 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2492 menu, lpane, 0, item_data,
2493 !NILP (enable))
2494 == XM_FAILURE)
2495 {
2496 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2497 *error = "Can't add selection to menu";
2498 return Qnil;
2499 }
2500 i += MENU_ITEMS_ITEM_LENGTH;
2501 }
2502 }
2503
2504 /* All set and ready to fly. */
2505 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2506 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2507 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2508 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2509 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2510 x = min (x, dispwidth);
2511 y = min (y, dispheight);
2512 x = max (x, 1);
2513 y = max (y, 1);
2514 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2515 &ulx, &uly, &width, &height);
2516 if (ulx+width > dispwidth)
2517 {
2518 x -= (ulx + width) - dispwidth;
2519 ulx = dispwidth - width;
2520 }
2521 if (uly+height > dispheight)
2522 {
2523 y -= (uly + height) - dispheight;
2524 uly = dispheight - height;
2525 }
2526 if (ulx < 0) x -= ulx;
2527 if (uly < 0) y -= uly;
2528
2529 XMenuSetAEQ (menu, TRUE);
2530 XMenuSetFreeze (menu, TRUE);
2531 pane = selidx = 0;
2532
2533 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2534 x, y, ButtonReleaseMask, &datap);
2535
2536
2537 #ifdef HAVE_X_WINDOWS
2538 /* Assume the mouse has moved out of the X window.
2539 If it has actually moved in, we will get an EnterNotify. */
2540 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2541 #endif
2542
2543 switch (status)
2544 {
2545 case XM_SUCCESS:
2546 #ifdef XDEBUG
2547 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2548 #endif
2549
2550 /* Find the item number SELIDX in pane number PANE. */
2551 i = 0;
2552 while (i < menu_items_used)
2553 {
2554 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2555 {
2556 if (pane == 0)
2557 pane_prefix
2558 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2559 pane--;
2560 i += MENU_ITEMS_PANE_LENGTH;
2561 }
2562 else
2563 {
2564 if (pane == -1)
2565 {
2566 if (selidx == 0)
2567 {
2568 entry
2569 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2570 if (keymaps != 0)
2571 {
2572 entry = Fcons (entry, Qnil);
2573 if (!NILP (pane_prefix))
2574 entry = Fcons (pane_prefix, entry);
2575 }
2576 break;
2577 }
2578 selidx--;
2579 }
2580 i += MENU_ITEMS_ITEM_LENGTH;
2581 }
2582 }
2583 break;
2584
2585 case XM_FAILURE:
2586 *error = "Can't activate menu";
2587 case XM_IA_SELECT:
2588 case XM_NO_SELECT:
2589 entry = Qnil;
2590 break;
2591 }
2592 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2593
2594 #ifdef HAVE_X_WINDOWS
2595 /* State that no mouse buttons are now held.
2596 (The oldXMenu code doesn't track this info for us.)
2597 That is not necessarily true, but the fiction leads to reasonable
2598 results, and it is a pain to ask which are actually held now. */
2599 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2600 #endif
2601
2602 return entry;
2603 }
2604
2605 #endif /* not USE_X_TOOLKIT */
2606
2607 #endif /* HAVE_MENUS */
2608 \f
2609 syms_of_xmenu ()
2610 {
2611 staticpro (&menu_items);
2612 menu_items = Qnil;
2613
2614 Qdebug_on_next_call = intern ("debug-on-next-call");
2615 staticpro (&Qdebug_on_next_call);
2616
2617 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2618 "Frame for which we are updating a menu.\n\
2619 The enable predicate for a menu command should check this variable.");
2620 Vmenu_updating_frame = Qnil;
2621
2622 #ifdef USE_X_TOOLKIT
2623 widget_id_tick = (1<<16);
2624 next_menubar_widget_id = 1;
2625 #endif
2626
2627 defsubr (&Sx_popup_menu);
2628 #ifdef HAVE_MENUS
2629 defsubr (&Sx_popup_dialog);
2630 #endif
2631 }