(makefile-mode): make-local-variable misspelled as make-local-file.
[emacs.git] / src / xmenu.c
blob18d510549347b7528a2f0f7e5ff8afe3a26c9158
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 #include <stdio.h>
34 /* On 4.3 this loses if it comes after xterm.h. */
35 #include <signal.h>
36 #include <config.h>
37 #include "lisp.h"
38 #include "termhooks.h"
39 #include "frame.h"
40 #include "window.h"
41 #include "keyboard.h"
42 #include "blockinput.h"
44 /* This may include sys/types.h, and that somehow loses
45 if this is not done before the other system files. */
46 #include "xterm.h"
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif
54 #include "dispextern.h"
56 #ifdef HAVE_X11
57 #include "../oldXMenu/XMenu.h"
58 #else
59 #include <X/XMenu.h>
60 #endif
62 #ifdef USE_X_TOOLKIT
63 #include <X11/Xlib.h>
64 #include <X11/IntrinsicP.h>
65 #include <X11/CoreP.h>
66 #include <X11/StringDefs.h>
67 #include <X11/Xaw/Paned.h>
68 #include "../lwlib/lwlib.h"
69 #include "../lwlib/xlwmenuP.h"
70 #endif /* USE_X_TOOLKIT */
72 #define min(x,y) (((x) < (y)) ? (x) : (y))
73 #define max(x,y) (((x) > (y)) ? (x) : (y))
75 #ifndef TRUE
76 #define TRUE 1
77 #define FALSE 0
78 #endif /* no TRUE */
80 #ifdef HAVE_X11
81 extern Display *x_current_display;
82 #else
83 #define ButtonReleaseMask ButtonReleased
84 #endif /* not HAVE_X11 */
86 /* We need a unique id for each popup menu and dialog box. */
87 static unsigned int popup_id_tick;
89 extern Lisp_Object Qmenu_enable;
90 extern Lisp_Object Qmenu_bar;
92 #ifdef USE_X_TOOLKIT
93 extern void process_expose_from_menu ();
94 extern XtAppContext Xt_app_con;
96 static int string_width ();
97 static Lisp_Object xdialog_show ();
98 #endif
100 static Lisp_Object xmenu_show ();
101 static void keymap_panes ();
102 static void single_keymap_panes ();
103 static void list_of_panes ();
104 static void list_of_items ();
106 /* This holds a Lisp vector that holds the results of decoding
107 the keymaps or alist-of-alists that specify a menu.
109 It describes the panes and items within the panes.
111 Each pane is described by 3 elements in the vector:
112 t, the pane name, the pane's prefix key.
113 Then follow the pane's items, with 4 elements per item:
114 the item string, the enable flag, the item's value,
115 and the equivalent keyboard key's description string.
117 In some cases, multiple levels of menus may be described.
118 A single vector slot containing nil indicates the start of a submenu.
119 A single vector slot containing lambda indicates the end of a submenu.
120 The submenu follows a menu item which is the way to reach the submenu.
122 A single vector slot containing quote indicates that the
123 following items should appear on the right of a dialog box.
125 Using a Lisp vector to hold this information while we decode it
126 takes care of protecting all the data from GC. */
128 #define MENU_ITEMS_PANE_NAME 1
129 #define MENU_ITEMS_PANE_PREFIX 2
130 #define MENU_ITEMS_PANE_LENGTH 3
132 #define MENU_ITEMS_ITEM_NAME 0
133 #define MENU_ITEMS_ITEM_ENABLE 1
134 #define MENU_ITEMS_ITEM_VALUE 2
135 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
136 #define MENU_ITEMS_ITEM_LENGTH 4
138 static Lisp_Object menu_items;
140 /* Number of slots currently allocated in menu_items. */
141 static int menu_items_allocated;
143 /* This is the index in menu_items of the first empty slot. */
144 static int menu_items_used;
146 /* The number of panes currently recorded in menu_items,
147 excluding those within submenus. */
148 static int menu_items_n_panes;
150 /* Current depth within submenus. */
151 static int menu_items_submenu_depth;
153 /* Initialize the menu_items structure if we haven't already done so.
154 Also mark it as currently empty. */
156 static void
157 init_menu_items ()
159 if (NILP (menu_items))
161 menu_items_allocated = 60;
162 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
165 menu_items_used = 0;
166 menu_items_n_panes = 0;
167 menu_items_submenu_depth = 0;
170 /* Call at the end of generating the data in menu_items.
171 This fills in the number of items in the last pane. */
173 static void
174 finish_menu_items ()
178 /* Call when finished using the data for the current menu
179 in menu_items. */
181 static void
182 discard_menu_items ()
184 /* Free the structure if it is especially large.
185 Otherwise, hold on to it, to save time. */
186 if (menu_items_allocated > 200)
188 menu_items = Qnil;
189 menu_items_allocated = 0;
193 /* Make the menu_items vector twice as large. */
195 static void
196 grow_menu_items ()
198 Lisp_Object old;
199 int old_size = menu_items_allocated;
200 old = menu_items;
202 menu_items_allocated *= 2;
203 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
204 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
205 old_size * sizeof (Lisp_Object));
208 /* Begin a submenu. */
210 static void
211 push_submenu_start ()
213 if (menu_items_used + 1 > menu_items_allocated)
214 grow_menu_items ();
216 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
217 menu_items_submenu_depth++;
220 /* End a submenu. */
222 static void
223 push_submenu_end ()
225 if (menu_items_used + 1 > menu_items_allocated)
226 grow_menu_items ();
228 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
229 menu_items_submenu_depth--;
232 /* Indicate boundary between left and right. */
234 static void
235 push_left_right_boundary ()
237 if (menu_items_used + 1 > menu_items_allocated)
238 grow_menu_items ();
240 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
243 /* Start a new menu pane in menu_items..
244 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
246 static void
247 push_menu_pane (name, prefix_vec)
248 Lisp_Object name, prefix_vec;
250 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
251 grow_menu_items ();
253 if (menu_items_submenu_depth == 0)
254 menu_items_n_panes++;
255 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
256 XVECTOR (menu_items)->contents[menu_items_used++] = name;
257 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
260 /* Push one menu item into the current pane.
261 NAME is the string to display. ENABLE if non-nil means
262 this item can be selected. KEY is the key generated by
263 choosing this item. EQUIV is the textual description
264 of the keyboard equivalent for this item (or nil if none). */
266 static void
267 push_menu_item (name, enable, key, equiv)
268 Lisp_Object name, enable, key, equiv;
270 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
271 grow_menu_items ();
273 XVECTOR (menu_items)->contents[menu_items_used++] = name;
274 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
275 XVECTOR (menu_items)->contents[menu_items_used++] = key;
276 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
279 /* Figure out the current keyboard equivalent of a menu item ITEM1.
280 The item string for menu display should be ITEM_STRING.
281 Store the equivalent keyboard key sequence's
282 textual description into *DESCRIP_PTR.
283 Also cache them in the item itself.
284 Return the real definition to execute. */
286 static Lisp_Object
287 menu_item_equiv_key (item_string, item1, descrip_ptr)
288 Lisp_Object item_string;
289 Lisp_Object item1;
290 Lisp_Object *descrip_ptr;
292 /* This is the real definition--the function to run. */
293 Lisp_Object def;
294 /* This is the sublist that records cached equiv key data
295 so we can save time. */
296 Lisp_Object cachelist;
297 /* These are the saved equivalent keyboard key sequence
298 and its key-description. */
299 Lisp_Object savedkey, descrip;
300 Lisp_Object def1;
301 int changed = 0;
303 /* If a help string follows the item string, skip it. */
304 if (CONSP (XCONS (item1)->cdr)
305 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
306 item1 = XCONS (item1)->cdr;
308 def = Fcdr (item1);
310 /* Get out the saved equivalent-keyboard-key info. */
311 cachelist = savedkey = descrip = Qnil;
312 if (CONSP (def) && CONSP (XCONS (def)->car)
313 && (NILP (XCONS (XCONS (def)->car)->car)
314 || VECTORP (XCONS (XCONS (def)->car)->car)))
316 cachelist = XCONS (def)->car;
317 def = XCONS (def)->cdr;
318 savedkey = XCONS (cachelist)->car;
319 descrip = XCONS (cachelist)->cdr;
322 /* Is it still valid? */
323 def1 = Qnil;
324 if (!NILP (savedkey))
325 def1 = Fkey_binding (savedkey, Qnil);
326 /* If not, update it. */
327 if (! EQ (def1, def)
328 /* If something had no key binding before, don't recheck it--
329 doing that takes too much time and makes menus too slow. */
330 && !(!NILP (cachelist) && NILP (savedkey)))
332 changed = 1;
333 descrip = Qnil;
334 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
335 if (VECTORP (savedkey)
336 && EQ (XVECTOR (savedkey)->contents[0], Qmenu_bar))
337 savedkey = Qnil;
338 if (!NILP (savedkey))
340 descrip = Fkey_description (savedkey);
341 descrip = concat2 (make_string (" (", 3), descrip);
342 descrip = concat2 (descrip, make_string (")", 1));
346 /* Cache the data we just got in a sublist of the menu binding. */
347 if (NILP (cachelist))
348 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
349 else if (changed)
351 XCONS (cachelist)->car = savedkey;
352 XCONS (cachelist)->cdr = descrip;
355 *descrip_ptr = descrip;
356 return def;
359 /* This is used as the handler when calling internal_condition_case_1. */
361 static Lisp_Object
362 menu_item_enabled_p_1 (arg)
363 Lisp_Object arg;
365 return Qnil;
368 /* Return non-nil if the command DEF is enabled when used as a menu item.
369 This is based on looking for a menu-enable property.
370 If NOTREAL is set, don't bother really computing this. */
372 static Lisp_Object
373 menu_item_enabled_p (def, notreal)
374 Lisp_Object def;
376 Lisp_Object enabled, tem;
378 enabled = Qt;
379 if (notreal)
380 return enabled;
381 if (XTYPE (def) == Lisp_Symbol)
383 /* No property, or nil, means enable.
384 Otherwise, enable if value is not nil. */
385 tem = Fget (def, Qmenu_enable);
386 if (!NILP (tem))
387 /* (condition-case nil (eval tem)
388 (error nil)) */
389 enabled = internal_condition_case_1 (Feval, tem, Qerror,
390 menu_item_enabled_p_1);
392 return enabled;
395 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
396 and generate menu panes for them in menu_items.
397 If NOTREAL is nonzero,
398 don't bother really computing whether an item is enabled. */
400 static void
401 keymap_panes (keymaps, nmaps, notreal)
402 Lisp_Object *keymaps;
403 int nmaps;
404 int notreal;
406 int mapno;
408 init_menu_items ();
410 /* Loop over the given keymaps, making a pane for each map.
411 But don't make a pane that is empty--ignore that map instead.
412 P is the number of panes we have made so far. */
413 for (mapno = 0; mapno < nmaps; mapno++)
414 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
416 finish_menu_items ();
419 /* This is a recursive subroutine of keymap_panes.
420 It handles one keymap, KEYMAP.
421 The other arguments are passed along
422 or point to local variables of the previous function.
423 If NOTREAL is nonzero,
424 don't bother really computing whether an item is enabled. */
426 static void
427 single_keymap_panes (keymap, pane_name, prefix, notreal)
428 Lisp_Object keymap;
429 Lisp_Object pane_name;
430 Lisp_Object prefix;
431 int notreal;
433 Lisp_Object pending_maps;
434 Lisp_Object tail, item, item1, item_string, table;
435 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
437 pending_maps = Qnil;
439 push_menu_pane (pane_name, prefix);
441 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
443 /* Look at each key binding, and if it has a menu string,
444 make a menu item from it. */
445 item = XCONS (tail)->car;
446 if (XTYPE (item) == Lisp_Cons)
448 item1 = XCONS (item)->cdr;
449 if (XTYPE (item1) == Lisp_Cons)
451 item_string = XCONS (item1)->car;
452 if (XTYPE (item_string) == Lisp_String)
454 /* This is the real definition--the function to run. */
455 Lisp_Object def;
456 /* These are the saved equivalent keyboard key sequence
457 and its key-description. */
458 Lisp_Object descrip;
459 Lisp_Object tem, enabled;
461 def = menu_item_equiv_key (item_string, item1, &descrip);
463 /* GCPRO because we will call eval.
464 Protecting KEYMAP preserves everything we use;
465 aside from that, must protect whatever might be
466 a string. Since there's no GCPRO5, we refetch
467 item_string instead of protecting it. */
468 GCPRO4 (keymap, pending_maps, def, descrip);
469 enabled = menu_item_enabled_p (def, notreal);
471 UNGCPRO;
473 item_string = XCONS (item1)->car;
475 tem = Fkeymapp (def);
476 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
477 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
478 pending_maps);
479 else
481 Lisp_Object submap;
482 submap = get_keymap_1 (def, 0, 1);
483 #ifndef USE_X_TOOLKIT
484 /* Indicate visually that this is a submenu. */
485 if (!NILP (submap))
486 item_string = concat2 (item_string,
487 build_string (" >"));
488 #endif
489 push_menu_item (item_string, enabled, XCONS (item)->car,
490 descrip);
491 #ifdef USE_X_TOOLKIT
492 /* Display a submenu using the toolkit. */
493 if (! NILP (submap))
495 push_submenu_start ();
496 single_keymap_panes (submap, Qnil,
497 XCONS (item)->car, notreal);
498 push_submenu_end ();
500 #endif
505 else if (XTYPE (item) == Lisp_Vector)
507 /* Loop over the char values represented in the vector. */
508 int len = XVECTOR (item)->size;
509 int c;
510 for (c = 0; c < len; c++)
512 Lisp_Object character;
513 XFASTINT (character) = c;
514 item1 = XVECTOR (item)->contents[c];
515 if (XTYPE (item1) == Lisp_Cons)
517 item_string = XCONS (item1)->car;
518 if (XTYPE (item_string) == Lisp_String)
520 Lisp_Object def;
522 /* These are the saved equivalent keyboard key sequence
523 and its key-description. */
524 Lisp_Object descrip;
525 Lisp_Object tem, enabled;
527 def = menu_item_equiv_key (item_string, item1, &descrip);
529 /* GCPRO because we will call eval.
530 Protecting KEYMAP preserves everything we use;
531 aside from that, must protect whatever might be
532 a string. Since there's no GCPRO5, we refetch
533 item_string instead of protecting it. */
534 GCPRO4 (keymap, pending_maps, def, descrip);
535 enabled = menu_item_enabled_p (def, notreal);
536 UNGCPRO;
538 item_string = XCONS (item1)->car;
540 tem = Fkeymapp (def);
541 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
542 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
543 pending_maps);
544 else
546 Lisp_Object submap;
547 submap = get_keymap_1 (def, 0, 1);
548 #ifndef USE_X_TOOLKIT
549 if (!NILP (submap))
550 item_string = concat2 (item_string,
551 build_string (" >"));
552 #endif
553 push_menu_item (item_string, enabled, character,
554 descrip);
555 #ifdef USE_X_TOOLKIT
556 if (! NILP (submap))
558 push_submenu_start ();
559 single_keymap_panes (submap, Qnil,
560 character, notreal);
561 push_submenu_end ();
563 #endif
571 /* Process now any submenus which want to be panes at this level. */
572 while (!NILP (pending_maps))
574 Lisp_Object elt, eltcdr, string;
575 elt = Fcar (pending_maps);
576 eltcdr = XCONS (elt)->cdr;
577 string = XCONS (eltcdr)->car;
578 /* We no longer discard the @ from the beginning of the string here.
579 Instead, we do this in xmenu_show. */
580 single_keymap_panes (Fcar (elt), string,
581 XCONS (eltcdr)->cdr, notreal);
582 pending_maps = Fcdr (pending_maps);
586 /* Push all the panes and items of a menu decsribed by the
587 alist-of-alists MENU.
588 This handles old-fashioned calls to x-popup-menu. */
590 static void
591 list_of_panes (menu)
592 Lisp_Object menu;
594 Lisp_Object tail;
596 init_menu_items ();
598 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
600 Lisp_Object elt, pane_name, pane_data;
601 elt = Fcar (tail);
602 pane_name = Fcar (elt);
603 CHECK_STRING (pane_name, 0);
604 push_menu_pane (pane_name, Qnil);
605 pane_data = Fcdr (elt);
606 CHECK_CONS (pane_data, 0);
607 list_of_items (pane_data);
610 finish_menu_items ();
613 /* Push the items in a single pane defined by the alist PANE. */
615 static void
616 list_of_items (pane)
617 Lisp_Object pane;
619 Lisp_Object tail, item, item1;
621 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
623 item = Fcar (tail);
624 if (STRINGP (item))
625 push_menu_item (item, Qnil, Qnil, Qnil);
626 else if (NILP (item))
627 push_left_right_boundary ();
628 else
630 CHECK_CONS (item, 0);
631 item1 = Fcar (item);
632 CHECK_STRING (item1, 1);
633 push_menu_item (item1, Qt, Fcdr (item), Qnil);
638 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
639 "Pop up a deck-of-cards menu and return user's selection.\n\
640 POSITION is a position specification. This is either a mouse button event\n\
641 or a list ((XOFFSET YOFFSET) WINDOW)\n\
642 where XOFFSET and YOFFSET are positions in characters from the top left\n\
643 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
644 This controls the position of the center of the first line\n\
645 in the first pane of the menu, not the top left of the menu as a whole.\n\
646 If POSITION is t, it means to use the current mouse position.\n\
648 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
649 The menu items come from key bindings that have a menu string as well as\n\
650 a definition; actually, the \"definition\" in such a key binding looks like\n\
651 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
652 the keymap as a top-level element.\n\n\
653 You can also use a list of keymaps as MENU.\n\
654 Then each keymap makes a separate pane.\n\
655 When MENU is a keymap or a list of keymaps, the return value\n\
656 is a list of events.\n\n\
657 Alternatively, you can specify a menu of multiple panes\n\
658 with a list of the form (TITLE PANE1 PANE2...),\n\
659 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
660 Each ITEM is normally a cons cell (STRING . VALUE);\n\
661 but a string can appear as an item--that makes a nonselectable line\n\
662 in the menu.\n\
663 With this form of menu, the return value is VALUE from the chosen item.\n\
665 If POSITION is nil, don't display the menu at all, just precalculate the\n\
666 cached information about equivalent key sequences.")
667 (position, menu)
668 Lisp_Object position, menu;
670 int number_of_panes, panes;
671 Lisp_Object keymap, tem;
672 int xpos, ypos;
673 Lisp_Object title;
674 char *error_name;
675 Lisp_Object selection;
676 int i, j;
677 FRAME_PTR f;
678 Lisp_Object x, y, window;
679 int keymaps = 0;
680 int menubarp = 0;
681 struct gcpro gcpro1;
683 if (! NILP (position))
685 check_x ();
687 /* Decode the first argument: find the window and the coordinates. */
688 if (EQ (position, Qt))
690 /* Use the mouse's current position. */
691 FRAME_PTR new_f = 0;
692 Lisp_Object bar_window;
693 int part;
694 unsigned long time;
696 if (new_f != 0)
697 XSET (window, Lisp_Frame, new_f);
698 else
700 window = selected_window;
701 XFASTINT (x) = 0;
702 XFASTINT (y) = 0;
705 else
707 tem = Fcar (position);
708 if (XTYPE (tem) == Lisp_Cons)
710 window = Fcar (Fcdr (position));
711 x = Fcar (tem);
712 y = Fcar (Fcdr (tem));
714 else
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);
722 /* Determine whether this menu is handling a menu bar click. */
723 tem = Fcar (Fcdr (Fcar (Fcdr (position))));
724 if (XTYPE (Fcar (position)) != Lisp_Cons
725 && CONSP (tem)
726 && EQ (Fcar (tem), Qmenu_bar))
727 menubarp = 1;
731 CHECK_NUMBER (x, 0);
732 CHECK_NUMBER (y, 0);
734 /* Decode where to put the menu. */
736 if (XTYPE (window) == Lisp_Frame)
738 f = XFRAME (window);
740 xpos = 0;
741 ypos = 0;
743 else if (XTYPE (window) == Lisp_Window)
745 CHECK_LIVE_WINDOW (window, 0);
746 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
748 xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left);
749 ypos = (f->display.x->line_height * XWINDOW (window)->top);
751 else
752 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
753 but I don't want to make one now. */
754 CHECK_WINDOW (window, 0);
756 xpos += XINT (x);
757 ypos += XINT (y);
760 title = Qnil;
761 GCPRO1 (title);
763 /* Decode the menu items from what was specified. */
765 keymap = Fkeymapp (menu);
766 tem = Qnil;
767 if (XTYPE (menu) == Lisp_Cons)
768 tem = Fkeymapp (Fcar (menu));
769 if (!NILP (keymap))
771 /* We were given a keymap. Extract menu info from the keymap. */
772 Lisp_Object prompt;
773 keymap = get_keymap (menu);
775 /* Extract the detailed info to make one pane. */
776 keymap_panes (&menu, 1, NILP (position));
778 /* Search for a string appearing directly as an element of the keymap.
779 That string is the title of the menu. */
780 prompt = map_prompt (keymap);
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;
786 keymaps = 1;
788 else if (!NILP (tem))
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;
796 title = Qnil;
798 /* The first keymap that has a prompt string
799 supplies the menu title. */
800 for (tem = menu, i = 0; XTYPE (tem) == Lisp_Cons; tem = Fcdr (tem))
802 Lisp_Object prompt;
804 maps[i++] = keymap = get_keymap (Fcar (tem));
806 prompt = map_prompt (keymap);
807 if (NILP (title) && !NILP (prompt))
808 title = prompt;
811 /* Extract the detailed info to make one pane. */
812 keymap_panes (maps, nmaps, NILP (position));
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;
818 keymaps = 1;
820 else
822 /* We were given an old-fashioned menu. */
823 title = Fcar (menu);
824 CHECK_STRING (title, 1);
826 list_of_panes (Fcdr (menu));
828 keymaps = 0;
831 if (NILP (position))
833 discard_menu_items ();
834 UNGCPRO;
835 return Qnil;
838 /* Display them in a menu. */
839 BLOCK_INPUT;
841 selection = xmenu_show (f, xpos, ypos, menubarp,
842 keymaps, title, &error_name);
843 UNBLOCK_INPUT;
845 discard_menu_items ();
847 UNGCPRO;
849 if (error_name) error (error_name);
850 return selection;
853 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
854 "Pop up a dialog box and return user's selection.\n\
855 POSITION specifies which frame to use.\n\
856 This is normally a mouse button event or a window or frame.\n\
857 If POSITION is t, it means to use the frame the mouse is on.\n\
858 The dialog box appears in the middle of the specified frame.\n\
860 CONTENTS specifies the alternatives to display in the dialog box.\n\
861 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
862 Each ITEM is a cons cell (STRING . VALUE).\n\
863 The return value is VALUE from the chosen item.\n\n\
864 An ITEM may also be just a string--that makes a nonselectable item.\n\
865 An ITEM may also be nil--that means to put all preceding items\n\
866 on the left of the dialog box and all following items on the right.\n\
867 \(By default, approximately half appear on each side.)")
868 (position, contents)
869 Lisp_Object position, contents;
871 FRAME_PTR f;
872 Lisp_Object window;
874 check_x ();
876 /* Decode the first argument: find the window or frame to use. */
877 if (EQ (position, Qt))
879 #if 0 /* Using the frame the mouse is on may not be right. */
880 /* Use the mouse's current position. */
881 FRAME_PTR new_f = 0;
882 Lisp_Object bar_window;
883 int part;
884 unsigned long time;
885 Lisp_Object x, y;
887 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
889 if (new_f != 0)
890 XSET (window, Lisp_Frame, new_f);
891 else
892 window = selected_window;
893 #endif
894 /* Decode the first argument: find the window and the coordinates. */
895 if (EQ (position, Qt))
896 window = selected_window;
898 else if (CONSP (position))
900 Lisp_Object tem;
901 tem = Fcar (position);
902 if (XTYPE (tem) == Lisp_Cons)
903 window = Fcar (Fcdr (position));
904 else
906 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
907 window = Fcar (tem); /* POSN_WINDOW (tem) */
910 else if (WINDOWP (position) || FRAMEP (position))
911 window = position;
913 /* Decode where to put the menu. */
915 if (XTYPE (window) == Lisp_Frame)
916 f = XFRAME (window);
917 else if (XTYPE (window) == Lisp_Window)
919 CHECK_LIVE_WINDOW (window, 0);
920 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
922 else
923 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
924 but I don't want to make one now. */
925 CHECK_WINDOW (window, 0);
927 #ifndef USE_X_TOOLKIT
928 /* Display a menu with these alternatives
929 in the middle of frame F. */
931 Lisp_Object x, y, frame, newpos;
932 XSET (frame, Lisp_Frame, f);
933 XSET (x, Lisp_Int, x_pixel_width (f) / 2);
934 XSET (y, Lisp_Int, x_pixel_height (f) / 2);
935 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
937 return Fx_popup_menu (newpos,
938 Fcons (Fcar (contents), Fcons (contents, Qnil)));
940 #else
942 Lisp_Object title;
943 char *error_name;
944 Lisp_Object selection;
946 /* Decode the dialog items from what was specified. */
947 title = Fcar (contents);
948 CHECK_STRING (title, 1);
950 list_of_panes (Fcons (contents, Qnil));
952 /* Display them in a dialog box. */
953 BLOCK_INPUT;
954 selection = xdialog_show (f, 0, 0, title, &error_name);
955 UNBLOCK_INPUT;
957 discard_menu_items ();
959 if (error_name) error (error_name);
960 return selection;
962 #endif
965 #ifdef USE_X_TOOLKIT
967 static void
968 dispatch_dummy_expose (w, x, y)
969 Widget w;
970 int x;
971 int y;
973 XExposeEvent dummy;
975 dummy.type = Expose;
976 dummy.window = XtWindow (w);
977 dummy.count = 0;
978 dummy.serial = 0;
979 dummy.send_event = 0;
980 dummy.display = XtDisplay (w);
981 dummy.x = x;
982 dummy.y = y;
984 XtDispatchEvent (&dummy);
987 static int
988 string_width (mw, s)
989 XlwMenuWidget mw;
990 char* s;
992 XCharStruct xcs;
993 int drop;
995 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
996 return xcs.width;
999 static int
1000 event_is_in_menu_item (mw, event, name, string_w)
1001 XlwMenuWidget mw;
1002 struct input_event *event;
1003 char *name;
1004 int *string_w;
1006 *string_w += (string_width (mw, name)
1007 + 2 * (mw->menu.horizontal_spacing
1008 + mw->menu.shadow_thickness));
1009 return XINT (event->x) < *string_w;
1013 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1015 Lisp_Object
1016 map_event_to_object (event, f)
1017 struct input_event *event;
1018 FRAME_PTR f;
1020 int i,j, string_w;
1021 window_state* ws;
1022 XlwMenuWidget mw = (XlwMenuWidget) f->display.x->menubar_widget;
1023 widget_value *val;
1026 string_w = 0;
1027 /* Find the window */
1028 for (val = mw->menu.old_stack [0]->contents; val; val = val->next)
1030 ws = &mw->menu.windows [0];
1031 if (ws && event_is_in_menu_item (mw, event, val->name, &string_w))
1033 Lisp_Object items;
1034 int i;
1036 items = FRAME_MENU_BAR_ITEMS (f);
1038 for (i = 0; i < XVECTOR (items)->size; i += 3)
1040 Lisp_Object pos, string, item;
1041 item = XVECTOR (items)->contents[i];
1042 string = XVECTOR (items)->contents[i + 1];
1043 pos = XVECTOR (items)->contents[i + 2];
1044 if (NILP (string))
1045 break;
1047 if (!strcmp (val->name, XSTRING (string)->data))
1048 return item;
1052 return Qnil;
1055 static Lisp_Object *menu_item_selection;
1057 static void
1058 popup_selection_callback (widget, id, client_data)
1059 Widget widget;
1060 LWLIB_ID id;
1061 XtPointer client_data;
1063 menu_item_selection = (Lisp_Object *) client_data;
1066 static void
1067 popup_down_callback (widget, id, client_data)
1068 Widget widget;
1069 LWLIB_ID id;
1070 XtPointer client_data;
1072 BLOCK_INPUT;
1073 lw_destroy_all_widgets (id);
1074 UNBLOCK_INPUT;
1077 static void
1078 dialog_selection_callback (widget, id, client_data)
1079 Widget widget;
1080 LWLIB_ID id;
1081 XtPointer client_data;
1083 if ((int)client_data != -1)
1084 menu_item_selection = (Lisp_Object *) client_data;
1085 BLOCK_INPUT;
1086 lw_destroy_all_widgets (id);
1087 UNBLOCK_INPUT;
1090 /* This recursively calls free_widget_value() on the tree of widgets.
1091 It must free all data that was malloc'ed for these widget_values.
1092 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1093 must be left alone. */
1095 void
1096 free_menubar_widget_value_tree (wv)
1097 widget_value *wv;
1099 if (! wv) return;
1101 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1103 if (wv->contents && (wv->contents != (widget_value*)1))
1105 free_menubar_widget_value_tree (wv->contents);
1106 wv->contents = (widget_value *) 0xDEADBEEF;
1108 if (wv->next)
1110 free_menubar_widget_value_tree (wv->next);
1111 wv->next = (widget_value *) 0xDEADBEEF;
1113 BLOCK_INPUT;
1114 free_widget_value (wv);
1115 UNBLOCK_INPUT;
1118 extern void EmacsFrameSetCharSize ();
1120 static void
1121 update_frame_menubar (f)
1122 FRAME_PTR f;
1124 struct x_display *x = f->display.x;
1125 int columns, rows;
1126 int menubar_changed;
1128 menubar_changed = (x->menubar_widget
1129 && !XtIsManaged (x->menubar_widget));
1131 if (! (menubar_changed))
1132 return;
1134 BLOCK_INPUT;
1135 /* Save the size of the frame because the pane widget doesn't accept to
1136 resize itself. So force it. */
1137 columns = f->width;
1138 rows = f->height;
1141 XawPanedSetRefigureMode (x->column_widget, 0);
1143 /* the order in which children are managed is the top to
1144 bottom order in which they are displayed in the paned window.
1145 First, remove the text-area widget.
1147 XtUnmanageChild (x->edit_widget);
1149 /* remove the menubar that is there now, and put up the menubar that
1150 should be there.
1152 if (menubar_changed)
1154 XtManageChild (x->menubar_widget);
1155 XtMapWidget (x->menubar_widget);
1156 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1160 /* Re-manage the text-area widget */
1161 XtManageChild (x->edit_widget);
1163 /* and now thrash the sizes */
1164 XawPanedSetRefigureMode (x->column_widget, 1);
1166 /* Force the pane widget to resize itself with the right values. */
1167 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1169 UNBLOCK_INPUT;
1172 void
1173 set_frame_menubar (f, first_time)
1174 FRAME_PTR f;
1175 int first_time;
1177 Widget menubar_widget = f->display.x->menubar_widget;
1178 int id = (int) f;
1179 Lisp_Object tail, items;
1180 widget_value *wv, *save_wv, *first_wv, *prev_wv = 0;
1181 int i;
1183 BLOCK_INPUT;
1185 wv = malloc_widget_value ();
1186 wv->name = "menubar";
1187 wv->value = 0;
1188 wv->enabled = 1;
1189 save_wv = first_wv = wv;
1191 if (NILP (items = FRAME_MENU_BAR_ITEMS (f)))
1192 items = FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1194 for (i = 0; i < XVECTOR (items)->size; i += 3)
1196 Lisp_Object string;
1198 string = XVECTOR (items)->contents[i + 1];
1199 if (NILP (string))
1200 break;
1202 wv = malloc_widget_value ();
1203 if (prev_wv)
1204 prev_wv->next = wv;
1205 else
1206 save_wv->contents = wv;
1207 wv->name = XSTRING (string)->data;
1208 wv->value = 0;
1209 wv->enabled = 1;
1210 prev_wv = wv;
1213 if (menubar_widget)
1214 lw_modify_all_widgets (id, first_wv, False);
1215 else
1217 menubar_widget = lw_create_widget ("menubar", "menubar",
1218 id, first_wv,
1219 f->display.x->column_widget,
1220 0, 0,
1221 0, 0);
1222 f->display.x->menubar_widget = menubar_widget;
1223 XtVaSetValues (menubar_widget,
1224 XtNshowGrip, 0,
1225 XtNresizeToPreferred, 1,
1226 XtNallowResize, 1,
1230 free_menubar_widget_value_tree (first_wv);
1232 /* Don't update the menubar the first time it is created via x_window. */
1233 if (!first_time)
1234 update_frame_menubar (f);
1236 UNBLOCK_INPUT;
1239 void
1240 free_frame_menubar (f)
1241 FRAME_PTR f;
1243 Widget menubar_widget;
1244 int id;
1246 menubar_widget = f->display.x->menubar_widget;
1247 id = (int) f;
1249 if (menubar_widget)
1251 BLOCK_INPUT;
1252 lw_destroy_all_widgets (id);
1253 UNBLOCK_INPUT;
1256 /* Called from Fx_create_frame to create the inital menubar of a frame
1257 before it is mapped, so that the window is mapped with the menubar already
1258 there instead of us tacking it on later and thrashing the window after it
1259 is visible. */
1260 void
1261 initialize_frame_menubar (f)
1262 FRAME_PTR f;
1264 set_frame_menubar (f, 1);
1267 /* Nonzero if position X, Y relative to inside of frame F
1268 is in some other menu bar item. */
1270 static int this_menu_bar_item_beg;
1271 static int this_menu_bar_item_end;
1273 static int
1274 other_menu_bar_item_p (f, x, y)
1275 FRAME_PTR f;
1276 int x, y;
1278 return (y >= 0
1279 && y < f->display.x->menubar_widget->core.height
1280 && x >= 0
1281 && x < f->display.x->menubar_widget->core.width
1282 && (x >= this_menu_bar_item_end
1283 || x < this_menu_bar_item_beg));
1286 /* Unread a button-press event in the menu bar of frame F
1287 at x position XPOS relative to the inside of the frame. */
1289 static void
1290 unread_menu_bar_button (f, xpos)
1291 FRAME_PTR f;
1292 int xpos;
1294 XEvent event;
1296 event.type = ButtonPress;
1297 event.xbutton.display = x_current_display;
1298 event.xbutton.serial = 0;
1299 event.xbutton.send_event = 0;
1300 event.xbutton.time = CurrentTime;
1301 event.xbutton.button = Button1;
1302 event.xbutton.window = XtWindow (f->display.x->menubar_widget);
1303 event.xbutton.x = xpos;
1304 XPutBackEvent (XDISPLAY &event);
1307 /* If the mouse has moved to another menu bar item,
1308 return 1 and unread a button press event for that item.
1309 Otherwise return 0. */
1311 static int
1312 check_mouse_other_menu_bar (f)
1313 FRAME_PTR f;
1315 FRAME_PTR new_f;
1316 Lisp_Object bar_window;
1317 int part;
1318 Lisp_Object x, y;
1319 unsigned long time;
1321 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
1323 if (f == new_f && other_menu_bar_item_p (f, x, y))
1325 unread_menu_bar_button (f, x);
1326 return 1;
1329 return 0;
1331 #endif /* USE_X_TOOLKIT */
1333 /* xmenu_show actually displays a menu using the panes and items in menu_items
1334 and returns the value selected from it.
1335 There are two versions of xmenu_show, one for Xt and one for Xlib.
1336 Both assume input is blocked by the caller. */
1338 /* F is the frame the menu is for.
1339 X and Y are the frame-relative specified position,
1340 relative to the inside upper left corner of the frame F.
1341 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1342 KEYMAPS is 1 if this menu was specified with keymaps;
1343 in that case, we return a list containing the chosen item's value
1344 and perhaps also the pane's prefix.
1345 TITLE is the specified menu title.
1346 ERROR is a place to store an error message string in case of failure.
1347 (We return nil on failure, but the value doesn't actually matter.) */
1349 #ifdef USE_X_TOOLKIT
1351 extern unsigned int x_mouse_grabbed;
1352 extern Lisp_Object Vmouse_depressed;
1354 static Lisp_Object
1355 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1356 FRAME_PTR f;
1357 int x;
1358 int y;
1359 int menubarp;
1360 int keymaps;
1361 Lisp_Object title;
1362 char **error;
1364 int i;
1365 int menu_id;
1366 Widget menu;
1367 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1369 /* This is the menu bar item (if any) that led to this menu. */
1370 widget_value *menubar_item = 0;
1372 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1373 widget_value **submenu_stack
1374 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1375 Lisp_Object *subprefix_stack
1376 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1377 int submenu_depth = 0;
1379 /* Define a queue to save up for later unreading
1380 all X events that don't pertain to the menu. */
1381 struct event_queue
1383 XEvent event;
1384 struct event_queue *next;
1387 struct event_queue *queue = NULL;
1388 struct event_queue *queue_tmp;
1390 *error = NULL;
1392 this_menu_bar_item_beg = -1;
1393 this_menu_bar_item_end = -1;
1395 /* Figure out which menu bar item, if any, this menu is for. */
1396 if (menubarp)
1398 int xbeg;
1399 int xend = 0;
1401 for (menubar_item = menubar->menu.old_stack[0]->contents;
1402 menubar_item;
1403 menubar_item = menubar_item->next)
1405 xbeg = xend;
1406 xend += (string_width (menubar, menubar_item->name)
1407 + 2 * (menubar->menu.horizontal_spacing
1408 + menubar->menu.shadow_thickness));
1409 if (x < xend)
1411 x = xbeg + 4;
1412 y = 0;
1413 /* Arrange to show a different menu if we move in the menu bar
1414 to a different item. */
1415 this_menu_bar_item_beg = xbeg;
1416 this_menu_bar_item_end = xend;
1417 break;
1421 if (menubar_item == 0)
1422 menubarp = 0;
1424 /* Offset the coordinates to root-relative. */
1425 x += (f->display.x->widget->core.x
1426 + f->display.x->widget->core.border_width);
1427 y += (f->display.x->widget->core.y
1428 + f->display.x->widget->core.border_width
1429 + f->display.x->menubar_widget->core.height);
1431 /* Create a tree of widget_value objects
1432 representing the panes and their items. */
1433 wv = malloc_widget_value ();
1434 wv->name = "menu";
1435 wv->value = 0;
1436 wv->enabled = 1;
1437 first_wv = wv;
1439 /* Loop over all panes and items, filling in the tree. */
1440 i = 0;
1441 while (i < menu_items_used)
1443 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1445 submenu_stack[submenu_depth++] = save_wv;
1446 save_wv = prev_wv;
1447 prev_wv = 0;
1448 i++;
1450 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1452 prev_wv = save_wv;
1453 save_wv = submenu_stack[--submenu_depth];
1454 i++;
1456 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1457 && submenu_depth != 0)
1458 i += MENU_ITEMS_PANE_LENGTH;
1459 /* Ignore a nil in the item list.
1460 It's meaningful only for dialog boxes. */
1461 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1462 i += 1;
1463 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1465 /* Create a new pane. */
1466 Lisp_Object pane_name, prefix;
1467 char *pane_string;
1468 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1469 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1470 pane_string = (NILP (pane_name)
1471 ? "" : (char *) XSTRING (pane_name)->data);
1472 /* If there is just one top-level pane, put all its items directly
1473 under the top-level menu. */
1474 if (menu_items_n_panes == 1)
1475 pane_string = "";
1477 /* If the pane has a meaningful name,
1478 make the pane a top-level menu item
1479 with its items as a submenu beneath it. */
1480 if (strcmp (pane_string, ""))
1482 wv = malloc_widget_value ();
1483 if (save_wv)
1484 save_wv->next = wv;
1485 else
1486 first_wv->contents = wv;
1487 wv->name = pane_string;
1488 if (keymaps && !NILP (prefix))
1489 wv->name++;
1490 wv->value = 0;
1491 wv->enabled = 1;
1493 save_wv = wv;
1494 prev_wv = 0;
1495 i += MENU_ITEMS_PANE_LENGTH;
1497 else
1499 /* Create a new item within current pane. */
1500 Lisp_Object item_name, enable, descrip;
1501 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1502 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1503 descrip
1504 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1506 wv = malloc_widget_value ();
1507 if (prev_wv)
1508 prev_wv->next = wv;
1509 else
1510 save_wv->contents = wv;
1511 wv->name = XSTRING (item_name)->data;
1512 if (!NILP (descrip))
1513 wv->key = XSTRING (descrip)->data;
1514 wv->value = 0;
1515 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1516 wv->enabled = !NILP (enable);
1517 prev_wv = wv;
1519 i += MENU_ITEMS_ITEM_LENGTH;
1523 /* Actually create the menu. */
1524 menu_id = ++popup_id_tick;
1525 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1526 f->display.x->widget, 1, 0,
1527 popup_selection_callback, popup_down_callback);
1528 /* Free the widget_value objects we used to specify the contents. */
1529 free_menubar_widget_value_tree (first_wv);
1531 /* No selection has been chosen yet. */
1532 menu_item_selection = 0;
1534 /* If the mouse moves out of the menu before we show the menu,
1535 don't show it at all. */
1536 if (check_mouse_other_menu_bar (f))
1538 lw_destroy_all_widgets (menu_id);
1539 return Qnil;
1543 /* Highlight the menu bar item (if any) that led to this menu. */
1544 if (menubarp)
1546 menubar_item->call_data = (XtPointer) 1;
1547 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1550 /* Display the menu. */
1552 XButtonPressedEvent dummy;
1553 XlwMenuWidget mw;
1555 mw = (XlwMenuWidget) ((CompositeWidget)menu)->composite.children[0];
1557 dummy.type = ButtonPress;
1558 dummy.serial = 0;
1559 dummy.send_event = 0;
1560 dummy.display = XtDisplay (menu);
1561 dummy.window = XtWindow (XtParent (menu));
1562 dummy.time = CurrentTime;
1563 dummy.button = 0;
1564 dummy.x_root = x;
1565 dummy.y_root = y;
1567 /* We activate directly the lucid implementation. */
1568 pop_up_menu (mw, &dummy);
1571 /* No need to check a second time since this is done in the XEvent loop.
1572 This slows done the execution. */
1573 #if 0
1574 /* Check again whether the mouse has moved to another menu bar item. */
1575 if (check_mouse_other_menu_bar (f))
1577 /* The mouse moved into a different menu bar item.
1578 We should bring up that item's menu instead.
1579 First pop down this menu. */
1580 XtUngrabPointer ((Widget)
1581 ((XlwMenuWidget)
1582 ((CompositeWidget)menu)->composite.children[0]),
1583 CurrentTime);
1584 lw_destroy_all_widgets (menu_id);
1585 goto pop_down;
1587 #endif
1589 /* Process events that apply to the menu. */
1590 while (1)
1592 XEvent event;
1594 XtAppNextEvent (Xt_app_con, &event);
1595 if (event.type == ButtonRelease)
1597 XtDispatchEvent (&event);
1598 if (! menubarp)
1600 /* Do the work of construct_mouse_click since it can't
1601 be called. Initially, the popup menu has been called
1602 from a ButtonPress in the edit_widget. Then the mouse
1603 has been set to grabbed. Reset it now. */
1604 x_mouse_grabbed &= ~(1 << event.xbutton.button);
1605 if (!x_mouse_grabbed)
1606 Vmouse_depressed = Qnil;
1608 break;
1610 else if (event.type == Expose)
1611 process_expose_from_menu (event);
1612 else if (event.type == MotionNotify)
1614 int event_x = (event.xmotion.x_root
1615 - (f->display.x->widget->core.x
1616 + f->display.x->widget->core.border_width));
1617 int event_y = (event.xmotion.y_root
1618 - (f->display.x->widget->core.y
1619 + f->display.x->widget->core.border_width));
1621 if (other_menu_bar_item_p (f, event_x, event_y))
1623 /* The mouse moved into a different menu bar item.
1624 We should bring up that item's menu instead.
1625 First pop down this menu. */
1626 XtUngrabPointer ((Widget)
1627 ((XlwMenuWidget)
1628 ((CompositeWidget)menu)->composite.children[0]),
1629 event.xbutton.time);
1630 lw_destroy_all_widgets (menu_id);
1632 /* Put back an event that will bring up the other item's menu. */
1633 unread_menu_bar_button (f, event_x);
1634 /* Don't let us select anything in this case. */
1635 menu_item_selection = 0;
1636 break;
1640 XtDispatchEvent (&event);
1641 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1643 queue_tmp
1644 = (struct event_queue *) malloc (sizeof (struct event_queue));
1646 if (queue_tmp != NULL)
1648 queue_tmp->event = event;
1649 queue_tmp->next = queue;
1650 queue = queue_tmp;
1655 pop_down:
1656 /* Unhighlight the menu bar item (if any) that led to this menu. */
1657 if (menubarp)
1659 menubar_item->call_data = (XtPointer) 0;
1660 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1663 /* fp turned off the following statement and wrote a comment
1664 that it is unnecessary--that the menu has already disappeared.
1665 I observer that is not so. -- rms. */
1666 /* Make sure the menu disappears. */
1667 lw_destroy_all_widgets (menu_id);
1669 /* Unread any events that we got but did not handle. */
1670 while (queue != NULL)
1672 queue_tmp = queue;
1673 XPutBackEvent (XDISPLAY &queue_tmp->event);
1674 queue = queue_tmp->next;
1675 free ((char *)queue_tmp);
1678 /* Find the selected item, and its pane, to return
1679 the proper value. */
1680 if (menu_item_selection != 0)
1682 Lisp_Object prefix;
1684 prefix = Qnil;
1685 i = 0;
1686 while (i < menu_items_used)
1688 Lisp_Object entry;
1690 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1692 subprefix_stack[submenu_depth++] = prefix;
1693 prefix = entry;
1694 i++;
1696 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1698 prefix = subprefix_stack[--submenu_depth];
1699 i++;
1701 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1703 prefix
1704 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1705 i += MENU_ITEMS_PANE_LENGTH;
1707 else
1709 entry
1710 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1711 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1713 if (keymaps != 0)
1715 int j;
1717 entry = Fcons (entry, Qnil);
1718 if (!NILP (prefix))
1719 entry = Fcons (prefix, entry);
1720 for (j = submenu_depth - 1; j >= 0; j--)
1721 entry = Fcons (subprefix_stack[j], entry);
1723 return entry;
1725 i += MENU_ITEMS_ITEM_LENGTH;
1730 return Qnil;
1733 static char * button_names [] = {
1734 "button1", "button2", "button3", "button4", "button5",
1735 "button6", "button7", "button8", "button9", "button10" };
1737 static Lisp_Object
1738 xdialog_show (f, menubarp, keymaps, title, error)
1739 FRAME_PTR f;
1740 int menubarp;
1741 int keymaps;
1742 Lisp_Object title;
1743 char **error;
1745 int i, nb_buttons=0;
1746 int dialog_id;
1747 Widget menu;
1748 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1749 char dialog_name[6];
1751 /* This is the menu bar item (if any) that led to this menu. */
1752 widget_value *menubar_item = 0;
1754 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1756 /* Define a queue to save up for later unreading
1757 all X events that don't pertain to the menu. */
1758 struct event_queue
1760 XEvent event;
1761 struct event_queue *next;
1764 struct event_queue *queue = NULL;
1765 struct event_queue *queue_tmp;
1767 /* Number of elements seen so far, before boundary. */
1768 int left_count = 0;
1769 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1770 int boundary_seen = 0;
1772 *error = NULL;
1774 if (menu_items_n_panes > 1)
1776 *error = "Multiple panes in dialog box";
1777 return Qnil;
1780 /* Create a tree of widget_value objects
1781 representing the text label and buttons. */
1783 Lisp_Object pane_name, prefix;
1784 char *pane_string;
1785 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1786 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1787 pane_string = (NILP (pane_name)
1788 ? "" : (char *) XSTRING (pane_name)->data);
1789 prev_wv = malloc_widget_value ();
1790 prev_wv->value = pane_string;
1791 if (keymaps && !NILP (prefix))
1792 prev_wv->name++;
1793 prev_wv->enabled = 1;
1794 prev_wv->name = "message";
1795 first_wv = prev_wv;
1797 /* Loop over all panes and items, filling in the tree. */
1798 i = MENU_ITEMS_PANE_LENGTH;
1799 while (i < menu_items_used)
1802 /* Create a new item within current pane. */
1803 Lisp_Object item_name, enable, descrip;
1804 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1805 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1806 descrip
1807 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1809 if (NILP (item_name))
1811 free_menubar_widget_value_tree (first_wv);
1812 *error = "Submenu in dialog items";
1813 return Qnil;
1815 if (EQ (item_name, Qquote))
1817 /* This is the boundary between left-side elts
1818 and right-side elts. Stop incrementing right_count. */
1819 boundary_seen = 1;
1820 i++;
1821 continue;
1823 if (nb_buttons >= 10)
1825 free_menubar_widget_value_tree (first_wv);
1826 *error = "Too many dialog items";
1827 return Qnil;
1830 wv = malloc_widget_value ();
1831 prev_wv->next = wv;
1832 wv->name = (char *) button_names[nb_buttons];
1833 if (!NILP (descrip))
1834 wv->key = XSTRING (descrip)->data;
1835 wv->value = XSTRING (item_name)->data;
1836 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1837 wv->enabled = !NILP (enable);
1838 prev_wv = wv;
1840 if (! boundary_seen)
1841 left_count++;
1843 nb_buttons++;
1844 i += MENU_ITEMS_ITEM_LENGTH;
1847 /* If the boundary was not specified,
1848 by default put half on the left and half on the right. */
1849 if (! boundary_seen)
1850 left_count = nb_buttons - nb_buttons / 2;
1852 wv = malloc_widget_value ();
1853 wv->name = dialog_name;
1855 /* Dialog boxes use a really stupid name encoding
1856 which specifies how many buttons to use
1857 and how many buttons are on the right.
1858 The Q means something also. */
1859 dialog_name[0] = 'Q';
1860 dialog_name[1] = '0' + nb_buttons;
1861 dialog_name[2] = 'B';
1862 dialog_name[3] = 'R';
1863 /* Number of buttons to put on the right. */
1864 dialog_name[4] = '0' + nb_buttons - left_count;
1865 dialog_name[5] = 0;
1866 wv->contents = first_wv;
1867 first_wv = wv;
1870 /* Actually create the dialog. */
1871 dialog_id = ++popup_id_tick;
1872 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1873 f->display.x->widget, 1, 0,
1874 dialog_selection_callback, 0);
1875 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1876 lw_modify_all_widgets (dialog_id, first_wv, True);
1877 #endif
1878 lw_modify_all_widgets (dialog_id, first_wv->contents->next, True);
1879 /* Free the widget_value objects we used to specify the contents. */
1880 free_menubar_widget_value_tree (first_wv);
1882 /* No selection has been chosen yet. */
1883 menu_item_selection = 0;
1885 /* Display the menu. */
1886 lw_pop_up_all_widgets (dialog_id);
1888 /* Process events that apply to the menu. */
1889 while (1)
1891 XEvent event;
1893 XtAppNextEvent (Xt_app_con, &event);
1894 if (event.type == ButtonRelease)
1896 XtDispatchEvent (&event);
1897 break;
1899 else if (event.type == Expose)
1900 process_expose_from_menu (event);
1901 XtDispatchEvent (&event);
1902 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1904 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1906 if (queue_tmp != NULL)
1908 queue_tmp->event = event;
1909 queue_tmp->next = queue;
1910 queue = queue_tmp;
1914 pop_down:
1916 /* Unread any events that we got but did not handle. */
1917 while (queue != NULL)
1919 queue_tmp = queue;
1920 XPutBackEvent (XDISPLAY &queue_tmp->event);
1921 queue = queue_tmp->next;
1922 free ((char *)queue_tmp);
1925 /* Find the selected item, and its pane, to return
1926 the proper value. */
1927 if (menu_item_selection != 0)
1929 Lisp_Object prefix;
1931 prefix = Qnil;
1932 i = 0;
1933 while (i < menu_items_used)
1935 Lisp_Object entry;
1937 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1939 prefix
1940 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1941 i += MENU_ITEMS_PANE_LENGTH;
1943 else
1945 entry
1946 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1947 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1949 if (keymaps != 0)
1951 entry = Fcons (entry, Qnil);
1952 if (!NILP (prefix))
1953 entry = Fcons (prefix, entry);
1955 return entry;
1957 i += MENU_ITEMS_ITEM_LENGTH;
1962 return Qnil;
1964 #else /* not USE_X_TOOLKIT */
1966 static Lisp_Object
1967 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1968 FRAME_PTR f;
1969 int x, y;
1970 int keymaps;
1971 int menubarp;
1972 Lisp_Object title;
1973 char **error;
1975 Window root;
1976 XMenu *menu;
1977 int pane, selidx, lpane, status;
1978 Lisp_Object entry, pane_prefix;
1979 char *datap;
1980 int ulx, uly, width, height;
1981 int dispwidth, dispheight;
1982 int i;
1983 int dummy_int;
1984 unsigned int dummy_uint;
1986 *error = 0;
1987 if (menu_items_n_panes == 0)
1988 return Qnil;
1990 /* Figure out which root window F is on. */
1991 XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root,
1992 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
1993 &dummy_uint, &dummy_uint);
1995 /* Make the menu on that window. */
1996 menu = XMenuCreate (XDISPLAY root, "emacs");
1997 if (menu == NULL)
1999 *error = "Can't create menu";
2000 return Qnil;
2003 /* Adjust coordinates to relative to the outer (window manager) window. */
2004 #ifdef HAVE_X11
2006 Window child;
2007 int win_x = 0, win_y = 0;
2009 /* Find the position of the outside upper-left corner of
2010 the inner window, with respect to the outer window. */
2011 if (f->display.x->parent_desc != ROOT_WINDOW)
2013 BLOCK_INPUT;
2014 XTranslateCoordinates (x_current_display,
2016 /* From-window, to-window. */
2017 f->display.x->window_desc,
2018 f->display.x->parent_desc,
2020 /* From-position, to-position. */
2021 0, 0, &win_x, &win_y,
2023 /* Child of window. */
2024 &child);
2025 UNBLOCK_INPUT;
2026 x += win_x;
2027 y += win_y;
2030 #endif /* HAVE_X11 */
2032 /* Adjust coordinates to be root-window-relative. */
2033 x += f->display.x->left_pos;
2034 y += f->display.x->top_pos;
2036 /* Create all the necessary panes and their items. */
2037 i = 0;
2038 while (i < menu_items_used)
2040 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2042 /* Create a new pane. */
2043 Lisp_Object pane_name, prefix;
2044 char *pane_string;
2046 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2047 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2048 pane_string = (NILP (pane_name)
2049 ? "" : (char *) XSTRING (pane_name)->data);
2050 if (keymaps && !NILP (prefix))
2051 pane_string++;
2053 lpane = XMenuAddPane (XDISPLAY menu, pane_string, TRUE);
2054 if (lpane == XM_FAILURE)
2056 XMenuDestroy (XDISPLAY menu);
2057 *error = "Can't create pane";
2058 return Qnil;
2060 i += MENU_ITEMS_PANE_LENGTH;
2062 /* Ignore a nil in the item list.
2063 It's meaningful only for dialog boxes. */
2064 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2065 i += 1;
2066 else
2068 /* Create a new item within current pane. */
2069 Lisp_Object item_name, enable, descrip;
2071 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2072 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2073 descrip
2074 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2075 if (!NILP (descrip))
2076 item_name = concat2 (item_name, descrip);
2078 if (XMenuAddSelection (XDISPLAY menu, lpane, 0,
2079 XSTRING (item_name)->data,
2080 !NILP (enable))
2081 == XM_FAILURE)
2083 XMenuDestroy (XDISPLAY menu);
2084 *error = "Can't add selection to menu";
2085 return Qnil;
2087 i += MENU_ITEMS_ITEM_LENGTH;
2091 /* All set and ready to fly. */
2092 XMenuRecompute (XDISPLAY menu);
2093 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
2094 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
2095 x = min (x, dispwidth);
2096 y = min (y, dispheight);
2097 x = max (x, 1);
2098 y = max (y, 1);
2099 XMenuLocate (XDISPLAY menu, 0, 0, x, y,
2100 &ulx, &uly, &width, &height);
2101 if (ulx+width > dispwidth)
2103 x -= (ulx + width) - dispwidth;
2104 ulx = dispwidth - width;
2106 if (uly+height > dispheight)
2108 y -= (uly + height) - dispheight;
2109 uly = dispheight - height;
2111 if (ulx < 0) x -= ulx;
2112 if (uly < 0) y -= uly;
2114 XMenuSetAEQ (menu, TRUE);
2115 XMenuSetFreeze (menu, TRUE);
2116 pane = selidx = 0;
2118 status = XMenuActivate (XDISPLAY menu, &pane, &selidx,
2119 x, y, ButtonReleaseMask, &datap);
2120 switch (status)
2122 case XM_SUCCESS:
2123 #ifdef XDEBUG
2124 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2125 #endif
2127 /* Find the item number SELIDX in pane number PANE. */
2128 i = 0;
2129 while (i < menu_items_used)
2131 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2133 if (pane == 0)
2134 pane_prefix
2135 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2136 pane--;
2137 i += MENU_ITEMS_PANE_LENGTH;
2139 else
2141 if (pane == -1)
2143 if (selidx == 0)
2145 entry
2146 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2147 if (keymaps != 0)
2149 entry = Fcons (entry, Qnil);
2150 if (!NILP (pane_prefix))
2151 entry = Fcons (pane_prefix, entry);
2153 break;
2155 selidx--;
2157 i += MENU_ITEMS_ITEM_LENGTH;
2160 break;
2162 case XM_FAILURE:
2163 XMenuDestroy (XDISPLAY menu);
2164 *error = "Can't activate menu";
2165 case XM_IA_SELECT:
2166 case XM_NO_SELECT:
2167 entry = Qnil;
2168 break;
2170 XMenuDestroy (XDISPLAY menu);
2171 return entry;
2173 #endif /* not USE_X_TOOLKIT */
2175 syms_of_xmenu ()
2177 staticpro (&menu_items);
2178 menu_items = Qnil;
2180 popup_id_tick = (1<<16);
2181 defsubr (&Sx_popup_menu);
2182 defsubr (&Sx_popup_dialog);