(xmenu_show): Disallow empty menus.
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
21 *
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
24 *
25 */
26
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
29
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
31
32 #include <stdio.h>
33
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"
43
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"
47
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
53
54 #include "dispextern.h"
55
56 #ifdef HAVE_X11
57 #include "../oldXMenu/XMenu.h"
58 #else
59 #include <X/XMenu.h>
60 #endif
61
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 */
71
72 #define min(x,y) (((x) < (y)) ? (x) : (y))
73 #define max(x,y) (((x) > (y)) ? (x) : (y))
74
75 #ifndef TRUE
76 #define TRUE 1
77 #define FALSE 0
78 #endif /* no TRUE */
79
80 #ifdef HAVE_X11
81 extern Display *x_current_display;
82 #else
83 #define ButtonReleaseMask ButtonReleased
84 #endif /* not HAVE_X11 */
85
86 /* We need a unique id for each popup menu and dialog box. */
87 static unsigned int popup_id_tick;
88
89 extern Lisp_Object Qmenu_enable;
90 extern Lisp_Object Qmenu_bar;
91
92 #ifdef USE_X_TOOLKIT
93 extern void process_expose_from_menu ();
94 extern XtAppContext Xt_app_con;
95
96 static int string_width ();
97 static Lisp_Object xdialog_show ();
98 #endif
99
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 ();
105 \f
106 /* This holds a Lisp vector that holds the results of decoding
107 the keymaps or alist-of-alists that specify a menu.
108
109 It describes the panes and items within the panes.
110
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.
116
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.
121
122 A single vector slot containing quote indicates that the
123 following items should appear on the right of a dialog box.
124
125 Using a Lisp vector to hold this information while we decode it
126 takes care of protecting all the data from GC. */
127
128 #define MENU_ITEMS_PANE_NAME 1
129 #define MENU_ITEMS_PANE_PREFIX 2
130 #define MENU_ITEMS_PANE_LENGTH 3
131
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
137
138 static Lisp_Object menu_items;
139
140 /* Number of slots currently allocated in menu_items. */
141 static int menu_items_allocated;
142
143 /* This is the index in menu_items of the first empty slot. */
144 static int menu_items_used;
145
146 /* The number of panes currently recorded in menu_items,
147 excluding those within submenus. */
148 static int menu_items_n_panes;
149
150 /* Current depth within submenus. */
151 static int menu_items_submenu_depth;
152
153 /* Initialize the menu_items structure if we haven't already done so.
154 Also mark it as currently empty. */
155
156 static void
157 init_menu_items ()
158 {
159 if (NILP (menu_items))
160 {
161 menu_items_allocated = 60;
162 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
163 }
164
165 menu_items_used = 0;
166 menu_items_n_panes = 0;
167 menu_items_submenu_depth = 0;
168 }
169
170 /* Call at the end of generating the data in menu_items.
171 This fills in the number of items in the last pane. */
172
173 static void
174 finish_menu_items ()
175 {
176 }
177
178 /* Call when finished using the data for the current menu
179 in menu_items. */
180
181 static void
182 discard_menu_items ()
183 {
184 /* Free the structure if it is especially large.
185 Otherwise, hold on to it, to save time. */
186 if (menu_items_allocated > 200)
187 {
188 menu_items = Qnil;
189 menu_items_allocated = 0;
190 }
191 }
192
193 /* Make the menu_items vector twice as large. */
194
195 static void
196 grow_menu_items ()
197 {
198 Lisp_Object old;
199 int old_size = menu_items_allocated;
200 old = menu_items;
201
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));
206 }
207
208 /* Begin a submenu. */
209
210 static void
211 push_submenu_start ()
212 {
213 if (menu_items_used + 1 > menu_items_allocated)
214 grow_menu_items ();
215
216 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
217 menu_items_submenu_depth++;
218 }
219
220 /* End a submenu. */
221
222 static void
223 push_submenu_end ()
224 {
225 if (menu_items_used + 1 > menu_items_allocated)
226 grow_menu_items ();
227
228 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
229 menu_items_submenu_depth--;
230 }
231
232 /* Indicate boundary between left and right. */
233
234 static void
235 push_left_right_boundary ()
236 {
237 if (menu_items_used + 1 > menu_items_allocated)
238 grow_menu_items ();
239
240 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
241 }
242
243 /* Start a new menu pane in menu_items..
244 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
245
246 static void
247 push_menu_pane (name, prefix_vec)
248 Lisp_Object name, prefix_vec;
249 {
250 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
251 grow_menu_items ();
252
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;
258 }
259
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). */
265
266 static void
267 push_menu_item (name, enable, key, equiv)
268 Lisp_Object name, enable, key, equiv;
269 {
270 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
271 grow_menu_items ();
272
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;
277 }
278 \f
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. */
285
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;
291 {
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;
302
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;
307
308 def = Fcdr (item1);
309
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)))
315 {
316 cachelist = XCONS (def)->car;
317 def = XCONS (def)->cdr;
318 savedkey = XCONS (cachelist)->car;
319 descrip = XCONS (cachelist)->cdr;
320 }
321
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)))
331 {
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))
339 {
340 descrip = Fkey_description (savedkey);
341 descrip = concat2 (make_string (" (", 3), descrip);
342 descrip = concat2 (descrip, make_string (")", 1));
343 }
344 }
345
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)
350 {
351 XCONS (cachelist)->car = savedkey;
352 XCONS (cachelist)->cdr = descrip;
353 }
354
355 *descrip_ptr = descrip;
356 return def;
357 }
358
359 /* This is used as the handler when calling internal_condition_case_1. */
360
361 static Lisp_Object
362 menu_item_enabled_p_1 (arg)
363 Lisp_Object arg;
364 {
365 return Qnil;
366 }
367
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. */
371
372 static Lisp_Object
373 menu_item_enabled_p (def, notreal)
374 Lisp_Object def;
375 {
376 Lisp_Object enabled, tem;
377
378 enabled = Qt;
379 if (notreal)
380 return enabled;
381 if (XTYPE (def) == Lisp_Symbol)
382 {
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);
391 }
392 return enabled;
393 }
394 \f
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. */
399
400 static void
401 keymap_panes (keymaps, nmaps, notreal)
402 Lisp_Object *keymaps;
403 int nmaps;
404 int notreal;
405 {
406 int mapno;
407
408 init_menu_items ();
409
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);
415
416 finish_menu_items ();
417 }
418
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. */
425
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;
432 {
433 Lisp_Object pending_maps;
434 Lisp_Object tail, item, item1, item_string, table;
435 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
436
437 pending_maps = Qnil;
438
439 push_menu_pane (pane_name, prefix);
440
441 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
442 {
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)
447 {
448 item1 = XCONS (item)->cdr;
449 if (XTYPE (item1) == Lisp_Cons)
450 {
451 item_string = XCONS (item1)->car;
452 if (XTYPE (item_string) == Lisp_String)
453 {
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;
460
461 def = menu_item_equiv_key (item_string, item1, &descrip);
462
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);
470
471 UNGCPRO;
472
473 item_string = XCONS (item1)->car;
474
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
480 {
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))
494 {
495 push_submenu_start ();
496 single_keymap_panes (submap, Qnil,
497 XCONS (item)->car, notreal);
498 push_submenu_end ();
499 }
500 #endif
501 }
502 }
503 }
504 }
505 else if (XTYPE (item) == Lisp_Vector)
506 {
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++)
511 {
512 Lisp_Object character;
513 XFASTINT (character) = c;
514 item1 = XVECTOR (item)->contents[c];
515 if (XTYPE (item1) == Lisp_Cons)
516 {
517 item_string = XCONS (item1)->car;
518 if (XTYPE (item_string) == Lisp_String)
519 {
520 Lisp_Object def;
521
522 /* These are the saved equivalent keyboard key sequence
523 and its key-description. */
524 Lisp_Object descrip;
525 Lisp_Object tem, enabled;
526
527 def = menu_item_equiv_key (item_string, item1, &descrip);
528
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;
537
538 item_string = XCONS (item1)->car;
539
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
545 {
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))
557 {
558 push_submenu_start ();
559 single_keymap_panes (submap, Qnil,
560 character, notreal);
561 push_submenu_end ();
562 }
563 #endif
564 }
565 }
566 }
567 }
568 }
569 }
570
571 /* Process now any submenus which want to be panes at this level. */
572 while (!NILP (pending_maps))
573 {
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);
583 }
584 }
585 \f
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. */
589
590 static void
591 list_of_panes (menu)
592 Lisp_Object menu;
593 {
594 Lisp_Object tail;
595
596 init_menu_items ();
597
598 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
599 {
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);
608 }
609
610 finish_menu_items ();
611 }
612
613 /* Push the items in a single pane defined by the alist PANE. */
614
615 static void
616 list_of_items (pane)
617 Lisp_Object pane;
618 {
619 Lisp_Object tail, item, item1;
620
621 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
622 {
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
629 {
630 CHECK_CONS (item, 0);
631 item1 = Fcar (item);
632 CHECK_STRING (item1, 1);
633 push_menu_item (item1, Qt, Fcdr (item), Qnil);
634 }
635 }
636 }
637 \f
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 pixels 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\
647 \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\
664 \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;
669 {
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;
682
683 if (! NILP (position))
684 {
685 check_x ();
686
687 /* Decode the first argument: find the window and the coordinates. */
688 if (EQ (position, Qt))
689 {
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;
695
696 if (mouse_position_hook)
697 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
698 if (new_f != 0)
699 XSET (window, Lisp_Frame, new_f);
700 else
701 {
702 window = selected_window;
703 XFASTINT (x) = 0;
704 XFASTINT (y) = 0;
705 }
706 }
707 else
708 {
709 tem = Fcar (position);
710 if (XTYPE (tem) == Lisp_Cons)
711 {
712 window = Fcar (Fcdr (position));
713 x = Fcar (tem);
714 y = Fcar (Fcdr (tem));
715 }
716 else
717 {
718 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
719 window = Fcar (tem); /* POSN_WINDOW (tem) */
720 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
721 x = Fcar (tem);
722 y = Fcdr (tem);
723
724 /* Determine whether this menu is handling a menu bar click. */
725 tem = Fcar (Fcdr (Fcar (Fcdr (position))));
726 if (XTYPE (Fcar (position)) != Lisp_Cons
727 && CONSP (tem)
728 && EQ (Fcar (tem), Qmenu_bar))
729 menubarp = 1;
730 }
731 }
732
733 CHECK_NUMBER (x, 0);
734 CHECK_NUMBER (y, 0);
735
736 /* Decode where to put the menu. */
737
738 if (XTYPE (window) == Lisp_Frame)
739 {
740 f = XFRAME (window);
741
742 xpos = 0;
743 ypos = 0;
744 }
745 else if (XTYPE (window) == Lisp_Window)
746 {
747 CHECK_LIVE_WINDOW (window, 0);
748 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
749
750 xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left);
751 ypos = (f->display.x->line_height * XWINDOW (window)->top);
752 }
753 else
754 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
755 but I don't want to make one now. */
756 CHECK_WINDOW (window, 0);
757
758 xpos += XINT (x);
759 ypos += XINT (y);
760 }
761
762 title = Qnil;
763 GCPRO1 (title);
764
765 /* Decode the menu items from what was specified. */
766
767 keymap = Fkeymapp (menu);
768 tem = Qnil;
769 if (XTYPE (menu) == Lisp_Cons)
770 tem = Fkeymapp (Fcar (menu));
771 if (!NILP (keymap))
772 {
773 /* We were given a keymap. Extract menu info from the keymap. */
774 Lisp_Object prompt;
775 keymap = get_keymap (menu);
776
777 /* Extract the detailed info to make one pane. */
778 keymap_panes (&menu, 1, NILP (position));
779
780 /* Search for a string appearing directly as an element of the keymap.
781 That string is the title of the menu. */
782 prompt = map_prompt (keymap);
783
784 /* Make that be the pane title of the first pane. */
785 if (!NILP (prompt) && menu_items_n_panes >= 0)
786 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
787
788 keymaps = 1;
789 }
790 else if (!NILP (tem))
791 {
792 /* We were given a list of keymaps. */
793 int nmaps = XFASTINT (Flength (menu));
794 Lisp_Object *maps
795 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
796 int i;
797
798 title = Qnil;
799
800 /* The first keymap that has a prompt string
801 supplies the menu title. */
802 for (tem = menu, i = 0; XTYPE (tem) == Lisp_Cons; tem = Fcdr (tem))
803 {
804 Lisp_Object prompt;
805
806 maps[i++] = keymap = get_keymap (Fcar (tem));
807
808 prompt = map_prompt (keymap);
809 if (NILP (title) && !NILP (prompt))
810 title = prompt;
811 }
812
813 /* Extract the detailed info to make one pane. */
814 keymap_panes (maps, nmaps, NILP (position));
815
816 /* Make the title be the pane title of the first pane. */
817 if (!NILP (title) && menu_items_n_panes >= 0)
818 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
819
820 keymaps = 1;
821 }
822 else
823 {
824 /* We were given an old-fashioned menu. */
825 title = Fcar (menu);
826 CHECK_STRING (title, 1);
827
828 list_of_panes (Fcdr (menu));
829
830 keymaps = 0;
831 }
832
833 if (NILP (position))
834 {
835 discard_menu_items ();
836 UNGCPRO;
837 return Qnil;
838 }
839
840 /* Display them in a menu. */
841 BLOCK_INPUT;
842
843 selection = xmenu_show (f, xpos, ypos, menubarp,
844 keymaps, title, &error_name);
845 UNBLOCK_INPUT;
846
847 discard_menu_items ();
848
849 UNGCPRO;
850
851 if (error_name) error (error_name);
852 return selection;
853 }
854
855 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
856 "Pop up a dialog box and return user's selection.\n\
857 POSITION specifies which frame to use.\n\
858 This is normally a mouse button event or a window or frame.\n\
859 If POSITION is t, it means to use the frame the mouse is on.\n\
860 The dialog box appears in the middle of the specified frame.\n\
861 \n\
862 CONTENTS specifies the alternatives to display in the dialog box.\n\
863 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
864 Each ITEM is a cons cell (STRING . VALUE).\n\
865 The return value is VALUE from the chosen item.\n\n\
866 An ITEM may also be just a string--that makes a nonselectable item.\n\
867 An ITEM may also be nil--that means to put all preceding items\n\
868 on the left of the dialog box and all following items on the right.\n\
869 \(By default, approximately half appear on each side.)")
870 (position, contents)
871 Lisp_Object position, contents;
872 {
873 FRAME_PTR f;
874 Lisp_Object window;
875
876 check_x ();
877
878 /* Decode the first argument: find the window or frame to use. */
879 if (EQ (position, Qt))
880 {
881 #if 0 /* Using the frame the mouse is on may not be right. */
882 /* Use the mouse's current position. */
883 FRAME_PTR new_f = 0;
884 Lisp_Object bar_window;
885 int part;
886 unsigned long time;
887 Lisp_Object x, y;
888
889 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
890
891 if (new_f != 0)
892 XSET (window, Lisp_Frame, new_f);
893 else
894 window = selected_window;
895 #endif
896 /* Decode the first argument: find the window and the coordinates. */
897 if (EQ (position, Qt))
898 window = selected_window;
899 }
900 else if (CONSP (position))
901 {
902 Lisp_Object tem;
903 tem = Fcar (position);
904 if (XTYPE (tem) == Lisp_Cons)
905 window = Fcar (Fcdr (position));
906 else
907 {
908 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
909 window = Fcar (tem); /* POSN_WINDOW (tem) */
910 }
911 }
912 else if (WINDOWP (position) || FRAMEP (position))
913 window = position;
914
915 /* Decode where to put the menu. */
916
917 if (XTYPE (window) == Lisp_Frame)
918 f = XFRAME (window);
919 else if (XTYPE (window) == Lisp_Window)
920 {
921 CHECK_LIVE_WINDOW (window, 0);
922 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
923 }
924 else
925 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
926 but I don't want to make one now. */
927 CHECK_WINDOW (window, 0);
928
929 #ifndef USE_X_TOOLKIT
930 /* Display a menu with these alternatives
931 in the middle of frame F. */
932 {
933 Lisp_Object x, y, frame, newpos;
934 XSET (frame, Lisp_Frame, f);
935 XSET (x, Lisp_Int, x_pixel_width (f) / 2);
936 XSET (y, Lisp_Int, x_pixel_height (f) / 2);
937 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
938
939 return Fx_popup_menu (newpos,
940 Fcons (Fcar (contents), Fcons (contents, Qnil)));
941 }
942 #else
943 {
944 Lisp_Object title;
945 char *error_name;
946 Lisp_Object selection;
947
948 /* Decode the dialog items from what was specified. */
949 title = Fcar (contents);
950 CHECK_STRING (title, 1);
951
952 list_of_panes (Fcons (contents, Qnil));
953
954 /* Display them in a dialog box. */
955 BLOCK_INPUT;
956 selection = xdialog_show (f, 0, 0, title, &error_name);
957 UNBLOCK_INPUT;
958
959 discard_menu_items ();
960
961 if (error_name) error (error_name);
962 return selection;
963 }
964 #endif
965 }
966 \f
967 #ifdef USE_X_TOOLKIT
968
969 static void
970 dispatch_dummy_expose (w, x, y)
971 Widget w;
972 int x;
973 int y;
974 {
975 XExposeEvent dummy;
976
977 dummy.type = Expose;
978 dummy.window = XtWindow (w);
979 dummy.count = 0;
980 dummy.serial = 0;
981 dummy.send_event = 0;
982 dummy.display = XtDisplay (w);
983 dummy.x = x;
984 dummy.y = y;
985
986 XtDispatchEvent ((XEvent *) &dummy);
987 }
988
989 static int
990 string_width (mw, s)
991 XlwMenuWidget mw;
992 char* s;
993 {
994 XCharStruct xcs;
995 int drop;
996
997 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
998 return xcs.width;
999 }
1000
1001 static int
1002 event_is_in_menu_item (mw, event, name, string_w)
1003 XlwMenuWidget mw;
1004 struct input_event *event;
1005 char *name;
1006 int *string_w;
1007 {
1008 *string_w += (string_width (mw, name)
1009 + 2 * (mw->menu.horizontal_spacing
1010 + mw->menu.shadow_thickness));
1011 return XINT (event->x) < *string_w;
1012 }
1013
1014
1015 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1016
1017 Lisp_Object
1018 map_event_to_object (event, f)
1019 struct input_event *event;
1020 FRAME_PTR f;
1021 {
1022 int i,j, string_w;
1023 window_state* ws;
1024 XlwMenuWidget mw = (XlwMenuWidget) f->display.x->menubar_widget;
1025 widget_value *val;
1026
1027
1028 string_w = 0;
1029 /* Find the window */
1030 for (val = mw->menu.old_stack [0]->contents; val; val = val->next)
1031 {
1032 ws = &mw->menu.windows [0];
1033 if (ws && event_is_in_menu_item (mw, event, val->name, &string_w))
1034 {
1035 Lisp_Object items;
1036 int i;
1037
1038 items = FRAME_MENU_BAR_ITEMS (f);
1039
1040 for (i = 0; i < XVECTOR (items)->size; i += 3)
1041 {
1042 Lisp_Object pos, string, item;
1043 item = XVECTOR (items)->contents[i];
1044 string = XVECTOR (items)->contents[i + 1];
1045 pos = XVECTOR (items)->contents[i + 2];
1046 if (NILP (string))
1047 break;
1048
1049 if (!strcmp (val->name, XSTRING (string)->data))
1050 return item;
1051 }
1052 }
1053 }
1054 return Qnil;
1055 }
1056
1057 static Lisp_Object *menu_item_selection;
1058
1059 static void
1060 popup_selection_callback (widget, id, client_data)
1061 Widget widget;
1062 LWLIB_ID id;
1063 XtPointer client_data;
1064 {
1065 menu_item_selection = (Lisp_Object *) client_data;
1066 }
1067
1068 static void
1069 popup_down_callback (widget, id, client_data)
1070 Widget widget;
1071 LWLIB_ID id;
1072 XtPointer client_data;
1073 {
1074 BLOCK_INPUT;
1075 lw_destroy_all_widgets (id);
1076 UNBLOCK_INPUT;
1077 }
1078
1079 static void
1080 dialog_selection_callback (widget, id, client_data)
1081 Widget widget;
1082 LWLIB_ID id;
1083 XtPointer client_data;
1084 {
1085 if ((int)client_data != -1)
1086 menu_item_selection = (Lisp_Object *) client_data;
1087 BLOCK_INPUT;
1088 lw_destroy_all_widgets (id);
1089 UNBLOCK_INPUT;
1090 }
1091
1092 /* This recursively calls free_widget_value() on the tree of widgets.
1093 It must free all data that was malloc'ed for these widget_values.
1094 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1095 must be left alone. */
1096
1097 void
1098 free_menubar_widget_value_tree (wv)
1099 widget_value *wv;
1100 {
1101 if (! wv) return;
1102
1103 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1104
1105 if (wv->contents && (wv->contents != (widget_value*)1))
1106 {
1107 free_menubar_widget_value_tree (wv->contents);
1108 wv->contents = (widget_value *) 0xDEADBEEF;
1109 }
1110 if (wv->next)
1111 {
1112 free_menubar_widget_value_tree (wv->next);
1113 wv->next = (widget_value *) 0xDEADBEEF;
1114 }
1115 BLOCK_INPUT;
1116 free_widget_value (wv);
1117 UNBLOCK_INPUT;
1118 }
1119
1120 extern void EmacsFrameSetCharSize ();
1121
1122 static void
1123 update_frame_menubar (f)
1124 FRAME_PTR f;
1125 {
1126 struct x_display *x = f->display.x;
1127 int columns, rows;
1128 int menubar_changed;
1129
1130 menubar_changed = (x->menubar_widget
1131 && !XtIsManaged (x->menubar_widget));
1132
1133 if (! (menubar_changed))
1134 return;
1135
1136 BLOCK_INPUT;
1137 /* Save the size of the frame because the pane widget doesn't accept to
1138 resize itself. So force it. */
1139 columns = f->width;
1140 rows = f->height;
1141
1142
1143 XawPanedSetRefigureMode (x->column_widget, 0);
1144
1145 /* the order in which children are managed is the top to
1146 bottom order in which they are displayed in the paned window.
1147 First, remove the text-area widget.
1148 */
1149 XtUnmanageChild (x->edit_widget);
1150
1151 /* remove the menubar that is there now, and put up the menubar that
1152 should be there.
1153 */
1154 if (menubar_changed)
1155 {
1156 XtManageChild (x->menubar_widget);
1157 XtMapWidget (x->menubar_widget);
1158 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1159 }
1160
1161
1162 /* Re-manage the text-area widget */
1163 XtManageChild (x->edit_widget);
1164
1165 /* and now thrash the sizes */
1166 XawPanedSetRefigureMode (x->column_widget, 1);
1167
1168 /* Force the pane widget to resize itself with the right values. */
1169 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1170
1171 UNBLOCK_INPUT;
1172 }
1173
1174 void
1175 set_frame_menubar (f, first_time)
1176 FRAME_PTR f;
1177 int first_time;
1178 {
1179 Widget menubar_widget = f->display.x->menubar_widget;
1180 int id = (int) f;
1181 Lisp_Object tail, items;
1182 widget_value *wv, *save_wv, *first_wv, *prev_wv = 0;
1183 int i;
1184
1185 BLOCK_INPUT;
1186
1187 wv = malloc_widget_value ();
1188 wv->name = "menubar";
1189 wv->value = 0;
1190 wv->enabled = 1;
1191 save_wv = first_wv = wv;
1192
1193 if (NILP (items = FRAME_MENU_BAR_ITEMS (f)))
1194 items = FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1195
1196 for (i = 0; i < XVECTOR (items)->size; i += 3)
1197 {
1198 Lisp_Object string;
1199
1200 string = XVECTOR (items)->contents[i + 1];
1201 if (NILP (string))
1202 break;
1203
1204 wv = malloc_widget_value ();
1205 if (prev_wv)
1206 prev_wv->next = wv;
1207 else
1208 save_wv->contents = wv;
1209 wv->name = (char *) XSTRING (string)->data;
1210 wv->value = 0;
1211 wv->enabled = 1;
1212 prev_wv = wv;
1213 }
1214
1215 if (menubar_widget)
1216 lw_modify_all_widgets (id, first_wv, False);
1217 else
1218 {
1219 menubar_widget = lw_create_widget ("menubar", "menubar",
1220 id, first_wv,
1221 f->display.x->column_widget,
1222 0, 0,
1223 0, 0);
1224 f->display.x->menubar_widget = menubar_widget;
1225 XtVaSetValues (menubar_widget,
1226 XtNshowGrip, 0,
1227 XtNresizeToPreferred, 1,
1228 XtNallowResize, 1,
1229 0);
1230 }
1231
1232 free_menubar_widget_value_tree (first_wv);
1233
1234 /* Don't update the menubar the first time it is created via x_window. */
1235 if (!first_time)
1236 update_frame_menubar (f);
1237
1238 UNBLOCK_INPUT;
1239 }
1240
1241 void
1242 free_frame_menubar (f)
1243 FRAME_PTR f;
1244 {
1245 Widget menubar_widget;
1246 int id;
1247
1248 menubar_widget = f->display.x->menubar_widget;
1249 id = (int) f;
1250
1251 if (menubar_widget)
1252 {
1253 BLOCK_INPUT;
1254 lw_destroy_all_widgets (id);
1255 UNBLOCK_INPUT;
1256 }
1257 }
1258 /* Called from Fx_create_frame to create the inital menubar of a frame
1259 before it is mapped, so that the window is mapped with the menubar already
1260 there instead of us tacking it on later and thrashing the window after it
1261 is visible. */
1262 void
1263 initialize_frame_menubar (f)
1264 FRAME_PTR f;
1265 {
1266 set_frame_menubar (f, 1);
1267 }
1268 \f
1269 /* Horizontal bounds of the current menu bar item. */
1270
1271 static int this_menu_bar_item_beg;
1272 static int this_menu_bar_item_end;
1273
1274 /* Horizontal position of the end of the last menu bar item. */
1275
1276 static int last_menu_bar_item_end;
1277
1278 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1279 but not in the current menu bar item. */
1280
1281 static int
1282 other_menu_bar_item_p (f, x, y)
1283 FRAME_PTR f;
1284 int x, y;
1285 {
1286 return (y >= 0
1287 && y < f->display.x->menubar_widget->core.height
1288 && x >= 0
1289 && x < last_menu_bar_item_end
1290 && (x >= this_menu_bar_item_end
1291 || x < this_menu_bar_item_beg));
1292 }
1293
1294 /* Unread a button-press event in the menu bar of frame F
1295 at x position XPOS relative to the inside of the frame. */
1296
1297 static void
1298 unread_menu_bar_button (f, xpos)
1299 FRAME_PTR f;
1300 int xpos;
1301 {
1302 XEvent event;
1303
1304 event.type = ButtonPress;
1305 event.xbutton.display = x_current_display;
1306 event.xbutton.serial = 0;
1307 event.xbutton.send_event = 0;
1308 event.xbutton.time = CurrentTime;
1309 event.xbutton.button = Button1;
1310 event.xbutton.window = XtWindow (f->display.x->menubar_widget);
1311 event.xbutton.x = xpos;
1312 XPutBackEvent (XDISPLAY &event);
1313 }
1314
1315 /* If the mouse has moved to another menu bar item,
1316 return 1 and unread a button press event for that item.
1317 Otherwise return 0. */
1318
1319 static int
1320 check_mouse_other_menu_bar (f)
1321 FRAME_PTR f;
1322 {
1323 FRAME_PTR new_f;
1324 Lisp_Object bar_window;
1325 int part;
1326 Lisp_Object x, y;
1327 unsigned long time;
1328
1329 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
1330
1331 if (f == new_f && other_menu_bar_item_p (f, x, y))
1332 {
1333 unread_menu_bar_button (f, x);
1334 return 1;
1335 }
1336
1337 return 0;
1338 }
1339 #endif /* USE_X_TOOLKIT */
1340 \f
1341 /* xmenu_show actually displays a menu using the panes and items in menu_items
1342 and returns the value selected from it.
1343 There are two versions of xmenu_show, one for Xt and one for Xlib.
1344 Both assume input is blocked by the caller. */
1345
1346 /* F is the frame the menu is for.
1347 X and Y are the frame-relative specified position,
1348 relative to the inside upper left corner of the frame F.
1349 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1350 KEYMAPS is 1 if this menu was specified with keymaps;
1351 in that case, we return a list containing the chosen item's value
1352 and perhaps also the pane's prefix.
1353 TITLE is the specified menu title.
1354 ERROR is a place to store an error message string in case of failure.
1355 (We return nil on failure, but the value doesn't actually matter.) */
1356
1357 #ifdef USE_X_TOOLKIT
1358
1359 extern unsigned int x_mouse_grabbed;
1360 extern Lisp_Object Vmouse_depressed;
1361
1362 static Lisp_Object
1363 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1364 FRAME_PTR f;
1365 int x;
1366 int y;
1367 int menubarp;
1368 int keymaps;
1369 Lisp_Object title;
1370 char **error;
1371 {
1372 int i;
1373 int menu_id;
1374 Widget menu;
1375 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1376
1377 /* This is the menu bar item (if any) that led to this menu. */
1378 widget_value *menubar_item = 0;
1379
1380 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1381 widget_value **submenu_stack
1382 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1383 Lisp_Object *subprefix_stack
1384 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1385 int submenu_depth = 0;
1386
1387 /* Define a queue to save up for later unreading
1388 all X events that don't pertain to the menu. */
1389 struct event_queue
1390 {
1391 XEvent event;
1392 struct event_queue *next;
1393 };
1394
1395 struct event_queue *queue = NULL;
1396 struct event_queue *queue_tmp;
1397
1398 Position root_x, root_y;
1399
1400 *error = NULL;
1401
1402 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1403 {
1404 *error = "Empty menu";
1405 return Qnil;
1406 }
1407 this_menu_bar_item_beg = -1;
1408 this_menu_bar_item_end = -1;
1409 last_menu_bar_item_end = -1;
1410
1411 /* Figure out which menu bar item, if any, this menu is for. */
1412 if (menubarp)
1413 {
1414 int xbeg;
1415 int xend = 0;
1416
1417 for (menubar_item = menubar->menu.old_stack[0]->contents;
1418 menubar_item;
1419 menubar_item = menubar_item->next)
1420 {
1421 xbeg = xend;
1422 xend += (string_width (menubar, menubar_item->name)
1423 + 2 * (menubar->menu.horizontal_spacing
1424 + menubar->menu.shadow_thickness));
1425 if (x >= xbeg && x < xend)
1426 {
1427 x = xbeg + 4;
1428 y = 0;
1429 /* Arrange to show a different menu if we move in the menu bar
1430 to a different item. */
1431 this_menu_bar_item_beg = xbeg;
1432 this_menu_bar_item_end = xend;
1433 }
1434 }
1435 last_menu_bar_item_end = xend;
1436 }
1437 if (menubar_item == 0)
1438 menubarp = 0;
1439
1440 /* Offset the coordinates to root-relative. */
1441 XtTranslateCoords (f->display.x->widget,
1442 x, y + f->display.x->menubar_widget->core.height,
1443 &root_x, &root_y);
1444 x = root_x;
1445 y = root_y;
1446
1447 /* Create a tree of widget_value objects
1448 representing the panes and their items. */
1449 wv = malloc_widget_value ();
1450 wv->name = "menu";
1451 wv->value = 0;
1452 wv->enabled = 1;
1453 first_wv = wv;
1454
1455 /* Loop over all panes and items, filling in the tree. */
1456 i = 0;
1457 while (i < menu_items_used)
1458 {
1459 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1460 {
1461 submenu_stack[submenu_depth++] = save_wv;
1462 save_wv = prev_wv;
1463 prev_wv = 0;
1464 i++;
1465 }
1466 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1467 {
1468 prev_wv = save_wv;
1469 save_wv = submenu_stack[--submenu_depth];
1470 i++;
1471 }
1472 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1473 && submenu_depth != 0)
1474 i += MENU_ITEMS_PANE_LENGTH;
1475 /* Ignore a nil in the item list.
1476 It's meaningful only for dialog boxes. */
1477 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1478 i += 1;
1479 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1480 {
1481 /* Create a new pane. */
1482 Lisp_Object pane_name, prefix;
1483 char *pane_string;
1484 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1485 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1486 pane_string = (NILP (pane_name)
1487 ? "" : (char *) XSTRING (pane_name)->data);
1488 /* If there is just one top-level pane, put all its items directly
1489 under the top-level menu. */
1490 if (menu_items_n_panes == 1)
1491 pane_string = "";
1492
1493 /* If the pane has a meaningful name,
1494 make the pane a top-level menu item
1495 with its items as a submenu beneath it. */
1496 if (strcmp (pane_string, ""))
1497 {
1498 wv = malloc_widget_value ();
1499 if (save_wv)
1500 save_wv->next = wv;
1501 else
1502 first_wv->contents = wv;
1503 wv->name = pane_string;
1504 if (keymaps && !NILP (prefix))
1505 wv->name++;
1506 wv->value = 0;
1507 wv->enabled = 1;
1508 }
1509 save_wv = wv;
1510 prev_wv = 0;
1511 i += MENU_ITEMS_PANE_LENGTH;
1512 }
1513 else
1514 {
1515 /* Create a new item within current pane. */
1516 Lisp_Object item_name, enable, descrip;
1517 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1518 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1519 descrip
1520 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1521
1522 wv = malloc_widget_value ();
1523 if (prev_wv)
1524 prev_wv->next = wv;
1525 else
1526 save_wv->contents = wv;
1527 wv->name = (char *) XSTRING (item_name)->data;
1528 if (!NILP (descrip))
1529 wv->key = (char *) XSTRING (descrip)->data;
1530 wv->value = 0;
1531 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1532 wv->enabled = !NILP (enable);
1533 prev_wv = wv;
1534
1535 i += MENU_ITEMS_ITEM_LENGTH;
1536 }
1537 }
1538
1539 /* Actually create the menu. */
1540 menu_id = ++popup_id_tick;
1541 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1542 f->display.x->widget, 1, 0,
1543 popup_selection_callback, popup_down_callback);
1544 /* Free the widget_value objects we used to specify the contents. */
1545 free_menubar_widget_value_tree (first_wv);
1546
1547 /* No selection has been chosen yet. */
1548 menu_item_selection = 0;
1549
1550 /* If the mouse moves out of the menu before we show the menu,
1551 don't show it at all. */
1552 if (check_mouse_other_menu_bar (f))
1553 {
1554 lw_destroy_all_widgets (menu_id);
1555 return Qnil;
1556 }
1557
1558
1559 /* Highlight the menu bar item (if any) that led to this menu. */
1560 if (menubarp)
1561 {
1562 menubar_item->call_data = (XtPointer) 1;
1563 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1564 }
1565
1566 /* Display the menu. */
1567 {
1568 XButtonPressedEvent dummy;
1569 XlwMenuWidget mw;
1570
1571 mw = (XlwMenuWidget) ((CompositeWidget)menu)->composite.children[0];
1572
1573 dummy.type = ButtonPress;
1574 dummy.serial = 0;
1575 dummy.send_event = 0;
1576 dummy.display = XtDisplay (menu);
1577 dummy.window = XtWindow (XtParent (menu));
1578 dummy.time = CurrentTime;
1579 dummy.button = 0;
1580 dummy.x_root = x;
1581 dummy.y_root = y;
1582
1583 /* We activate directly the lucid implementation. */
1584 pop_up_menu (mw, &dummy);
1585 }
1586
1587 /* No need to check a second time since this is done in the XEvent loop.
1588 This slows done the execution. */
1589 #if 0
1590 /* Check again whether the mouse has moved to another menu bar item. */
1591 if (check_mouse_other_menu_bar (f))
1592 {
1593 /* The mouse moved into a different menu bar item.
1594 We should bring up that item's menu instead.
1595 First pop down this menu. */
1596 XtUngrabPointer ((Widget)
1597 ((XlwMenuWidget)
1598 ((CompositeWidget)menu)->composite.children[0]),
1599 CurrentTime);
1600 lw_destroy_all_widgets (menu_id);
1601 goto pop_down;
1602 }
1603 #endif
1604
1605 /* Process events that apply to the menu. */
1606 while (1)
1607 {
1608 XEvent event;
1609
1610 XtAppNextEvent (Xt_app_con, &event);
1611 if (event.type == ButtonRelease)
1612 {
1613 XtDispatchEvent (&event);
1614 if (! menubarp)
1615 {
1616 /* Do the work of construct_mouse_click since it can't
1617 be called. Initially, the popup menu has been called
1618 from a ButtonPress in the edit_widget. Then the mouse
1619 has been set to grabbed. Reset it now. */
1620 x_mouse_grabbed &= ~(1 << event.xbutton.button);
1621 if (!x_mouse_grabbed)
1622 Vmouse_depressed = Qnil;
1623 }
1624 break;
1625 }
1626 else if (event.type == Expose)
1627 process_expose_from_menu (event);
1628 else if (event.type == MotionNotify)
1629 {
1630 int event_x = (event.xmotion.x_root
1631 - (f->display.x->widget->core.x
1632 + f->display.x->widget->core.border_width));
1633 int event_y = (event.xmotion.y_root
1634 - (f->display.x->widget->core.y
1635 + f->display.x->widget->core.border_width));
1636
1637 if (other_menu_bar_item_p (f, event_x, event_y))
1638 {
1639 /* The mouse moved into a different menu bar item.
1640 We should bring up that item's menu instead.
1641 First pop down this menu. */
1642 XtUngrabPointer ((Widget)
1643 ((XlwMenuWidget)
1644 ((CompositeWidget)menu)->composite.children[0]),
1645 event.xbutton.time);
1646 lw_destroy_all_widgets (menu_id);
1647
1648 /* Put back an event that will bring up the other item's menu. */
1649 unread_menu_bar_button (f, event_x);
1650 /* Don't let us select anything in this case. */
1651 menu_item_selection = 0;
1652 break;
1653 }
1654 }
1655
1656 XtDispatchEvent (&event);
1657 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1658 {
1659 queue_tmp
1660 = (struct event_queue *) malloc (sizeof (struct event_queue));
1661
1662 if (queue_tmp != NULL)
1663 {
1664 queue_tmp->event = event;
1665 queue_tmp->next = queue;
1666 queue = queue_tmp;
1667 }
1668 }
1669 }
1670
1671 pop_down:
1672 /* Unhighlight the menu bar item (if any) that led to this menu. */
1673 if (menubarp)
1674 {
1675 menubar_item->call_data = (XtPointer) 0;
1676 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1677 }
1678
1679 /* fp turned off the following statement and wrote a comment
1680 that it is unnecessary--that the menu has already disappeared.
1681 I observer that is not so. -- rms. */
1682 /* Make sure the menu disappears. */
1683 lw_destroy_all_widgets (menu_id);
1684
1685 /* Unread any events that we got but did not handle. */
1686 while (queue != NULL)
1687 {
1688 queue_tmp = queue;
1689 XPutBackEvent (XDISPLAY &queue_tmp->event);
1690 queue = queue_tmp->next;
1691 free ((char *)queue_tmp);
1692 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1693 interrupt_input_pending = 1;
1694 }
1695
1696 /* Find the selected item, and its pane, to return
1697 the proper value. */
1698 if (menu_item_selection != 0)
1699 {
1700 Lisp_Object prefix;
1701
1702 prefix = Qnil;
1703 i = 0;
1704 while (i < menu_items_used)
1705 {
1706 Lisp_Object entry;
1707
1708 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1709 {
1710 subprefix_stack[submenu_depth++] = prefix;
1711 prefix = entry;
1712 i++;
1713 }
1714 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1715 {
1716 prefix = subprefix_stack[--submenu_depth];
1717 i++;
1718 }
1719 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1720 {
1721 prefix
1722 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1723 i += MENU_ITEMS_PANE_LENGTH;
1724 }
1725 else
1726 {
1727 entry
1728 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1729 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1730 {
1731 if (keymaps != 0)
1732 {
1733 int j;
1734
1735 entry = Fcons (entry, Qnil);
1736 if (!NILP (prefix))
1737 entry = Fcons (prefix, entry);
1738 for (j = submenu_depth - 1; j >= 0; j--)
1739 if (!NILP (subprefix_stack[j]))
1740 entry = Fcons (subprefix_stack[j], entry);
1741 }
1742 return entry;
1743 }
1744 i += MENU_ITEMS_ITEM_LENGTH;
1745 }
1746 }
1747 }
1748
1749 return Qnil;
1750 }
1751
1752 static char * button_names [] = {
1753 "button1", "button2", "button3", "button4", "button5",
1754 "button6", "button7", "button8", "button9", "button10" };
1755
1756 static Lisp_Object
1757 xdialog_show (f, menubarp, keymaps, title, error)
1758 FRAME_PTR f;
1759 int menubarp;
1760 int keymaps;
1761 Lisp_Object title;
1762 char **error;
1763 {
1764 int i, nb_buttons=0;
1765 int dialog_id;
1766 Widget menu;
1767 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1768 char dialog_name[6];
1769
1770 /* This is the menu bar item (if any) that led to this menu. */
1771 widget_value *menubar_item = 0;
1772
1773 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1774
1775 /* Define a queue to save up for later unreading
1776 all X events that don't pertain to the menu. */
1777 struct event_queue
1778 {
1779 XEvent event;
1780 struct event_queue *next;
1781 };
1782
1783 struct event_queue *queue = NULL;
1784 struct event_queue *queue_tmp;
1785
1786 /* Number of elements seen so far, before boundary. */
1787 int left_count = 0;
1788 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1789 int boundary_seen = 0;
1790
1791 *error = NULL;
1792
1793 if (menu_items_n_panes > 1)
1794 {
1795 *error = "Multiple panes in dialog box";
1796 return Qnil;
1797 }
1798
1799 /* Create a tree of widget_value objects
1800 representing the text label and buttons. */
1801 {
1802 Lisp_Object pane_name, prefix;
1803 char *pane_string;
1804 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1805 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1806 pane_string = (NILP (pane_name)
1807 ? "" : (char *) XSTRING (pane_name)->data);
1808 prev_wv = malloc_widget_value ();
1809 prev_wv->value = pane_string;
1810 if (keymaps && !NILP (prefix))
1811 prev_wv->name++;
1812 prev_wv->enabled = 1;
1813 prev_wv->name = "message";
1814 first_wv = prev_wv;
1815
1816 /* Loop over all panes and items, filling in the tree. */
1817 i = MENU_ITEMS_PANE_LENGTH;
1818 while (i < menu_items_used)
1819 {
1820
1821 /* Create a new item within current pane. */
1822 Lisp_Object item_name, enable, descrip;
1823 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1824 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1825 descrip
1826 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1827
1828 if (NILP (item_name))
1829 {
1830 free_menubar_widget_value_tree (first_wv);
1831 *error = "Submenu in dialog items";
1832 return Qnil;
1833 }
1834 if (EQ (item_name, Qquote))
1835 {
1836 /* This is the boundary between left-side elts
1837 and right-side elts. Stop incrementing right_count. */
1838 boundary_seen = 1;
1839 i++;
1840 continue;
1841 }
1842 if (nb_buttons >= 10)
1843 {
1844 free_menubar_widget_value_tree (first_wv);
1845 *error = "Too many dialog items";
1846 return Qnil;
1847 }
1848
1849 wv = malloc_widget_value ();
1850 prev_wv->next = wv;
1851 wv->name = (char *) button_names[nb_buttons];
1852 if (!NILP (descrip))
1853 wv->key = (char *) XSTRING (descrip)->data;
1854 wv->value = (char *) XSTRING (item_name)->data;
1855 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1856 wv->enabled = !NILP (enable);
1857 prev_wv = wv;
1858
1859 if (! boundary_seen)
1860 left_count++;
1861
1862 nb_buttons++;
1863 i += MENU_ITEMS_ITEM_LENGTH;
1864 }
1865
1866 /* If the boundary was not specified,
1867 by default put half on the left and half on the right. */
1868 if (! boundary_seen)
1869 left_count = nb_buttons - nb_buttons / 2;
1870
1871 wv = malloc_widget_value ();
1872 wv->name = dialog_name;
1873
1874 /* Dialog boxes use a really stupid name encoding
1875 which specifies how many buttons to use
1876 and how many buttons are on the right.
1877 The Q means something also. */
1878 dialog_name[0] = 'Q';
1879 dialog_name[1] = '0' + nb_buttons;
1880 dialog_name[2] = 'B';
1881 dialog_name[3] = 'R';
1882 /* Number of buttons to put on the right. */
1883 dialog_name[4] = '0' + nb_buttons - left_count;
1884 dialog_name[5] = 0;
1885 wv->contents = first_wv;
1886 first_wv = wv;
1887 }
1888
1889 /* Actually create the dialog. */
1890 dialog_id = ++popup_id_tick;
1891 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1892 f->display.x->widget, 1, 0,
1893 dialog_selection_callback, 0);
1894 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1895 lw_modify_all_widgets (dialog_id, first_wv, True);
1896 #endif
1897 lw_modify_all_widgets (dialog_id, first_wv->contents->next, True);
1898 /* Free the widget_value objects we used to specify the contents. */
1899 free_menubar_widget_value_tree (first_wv);
1900
1901 /* No selection has been chosen yet. */
1902 menu_item_selection = 0;
1903
1904 /* Display the menu. */
1905 lw_pop_up_all_widgets (dialog_id);
1906
1907 /* Process events that apply to the menu. */
1908 while (1)
1909 {
1910 XEvent event;
1911
1912 XtAppNextEvent (Xt_app_con, &event);
1913 if (event.type == ButtonRelease)
1914 {
1915 XtDispatchEvent (&event);
1916 break;
1917 }
1918 else if (event.type == Expose)
1919 process_expose_from_menu (event);
1920 XtDispatchEvent (&event);
1921 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1922 {
1923 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1924
1925 if (queue_tmp != NULL)
1926 {
1927 queue_tmp->event = event;
1928 queue_tmp->next = queue;
1929 queue = queue_tmp;
1930 }
1931 }
1932 }
1933 pop_down:
1934
1935 /* State that no mouse buttons are now held.
1936 That is not necessarily true, but the fiction leads to reasonable
1937 results, and it is a pain to ask which are actually held now
1938 or track this in the loop above. */
1939 x_mouse_grabbed = 0;
1940
1941 /* Unread any events that we got but did not handle. */
1942 while (queue != NULL)
1943 {
1944 queue_tmp = queue;
1945 XPutBackEvent (XDISPLAY &queue_tmp->event);
1946 queue = queue_tmp->next;
1947 free ((char *)queue_tmp);
1948 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1949 interrupt_input_pending = 1;
1950 }
1951
1952 /* Find the selected item, and its pane, to return
1953 the proper value. */
1954 if (menu_item_selection != 0)
1955 {
1956 Lisp_Object prefix;
1957
1958 prefix = Qnil;
1959 i = 0;
1960 while (i < menu_items_used)
1961 {
1962 Lisp_Object entry;
1963
1964 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1965 {
1966 prefix
1967 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1968 i += MENU_ITEMS_PANE_LENGTH;
1969 }
1970 else
1971 {
1972 entry
1973 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1974 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1975 {
1976 if (keymaps != 0)
1977 {
1978 entry = Fcons (entry, Qnil);
1979 if (!NILP (prefix))
1980 entry = Fcons (prefix, entry);
1981 }
1982 return entry;
1983 }
1984 i += MENU_ITEMS_ITEM_LENGTH;
1985 }
1986 }
1987 }
1988
1989 return Qnil;
1990 }
1991 #else /* not USE_X_TOOLKIT */
1992
1993 static Lisp_Object
1994 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1995 FRAME_PTR f;
1996 int x, y;
1997 int keymaps;
1998 int menubarp;
1999 Lisp_Object title;
2000 char **error;
2001 {
2002 Window root;
2003 XMenu *menu;
2004 int pane, selidx, lpane, status;
2005 Lisp_Object entry, pane_prefix;
2006 char *datap;
2007 int ulx, uly, width, height;
2008 int dispwidth, dispheight;
2009 int i, j;
2010 int maxwidth;
2011 int dummy_int;
2012 unsigned int dummy_uint;
2013
2014 *error = 0;
2015 if (menu_items_n_panes == 0)
2016 return Qnil;
2017
2018 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2019 {
2020 *error = "Empty menu";
2021 return Qnil;
2022 }
2023
2024 /* Figure out which root window F is on. */
2025 XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root,
2026 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2027 &dummy_uint, &dummy_uint);
2028
2029 /* Make the menu on that window. */
2030 menu = XMenuCreate (XDISPLAY root, "emacs");
2031 if (menu == NULL)
2032 {
2033 *error = "Can't create menu";
2034 return Qnil;
2035 }
2036
2037 /* Adjust coordinates to relative to the outer (window manager) window. */
2038 #ifdef HAVE_X11
2039 {
2040 Window child;
2041 int win_x = 0, win_y = 0;
2042
2043 /* Find the position of the outside upper-left corner of
2044 the inner window, with respect to the outer window. */
2045 if (f->display.x->parent_desc != ROOT_WINDOW)
2046 {
2047 BLOCK_INPUT;
2048 XTranslateCoordinates (x_current_display,
2049
2050 /* From-window, to-window. */
2051 f->display.x->window_desc,
2052 f->display.x->parent_desc,
2053
2054 /* From-position, to-position. */
2055 0, 0, &win_x, &win_y,
2056
2057 /* Child of window. */
2058 &child);
2059 UNBLOCK_INPUT;
2060 x += win_x;
2061 y += win_y;
2062 }
2063 }
2064 #endif /* HAVE_X11 */
2065
2066 /* Adjust coordinates to be root-window-relative. */
2067 x += f->display.x->left_pos;
2068 y += f->display.x->top_pos;
2069
2070 /* Create all the necessary panes and their items. */
2071 i = 0;
2072 while (i < menu_items_used)
2073 {
2074 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2075 {
2076 /* Create a new pane. */
2077 Lisp_Object pane_name, prefix;
2078 char *pane_string;
2079
2080 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2081 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2082 pane_string = (NILP (pane_name)
2083 ? "" : (char *) XSTRING (pane_name)->data);
2084 if (keymaps && !NILP (prefix))
2085 pane_string++;
2086
2087 lpane = XMenuAddPane (XDISPLAY menu, pane_string, TRUE);
2088 if (lpane == XM_FAILURE)
2089 {
2090 XMenuDestroy (XDISPLAY menu);
2091 *error = "Can't create pane";
2092 return Qnil;
2093 }
2094 i += MENU_ITEMS_PANE_LENGTH;
2095
2096 /* Find the width of the widest item in this pane. */
2097 maxwidth = 0;
2098 j = i;
2099 while (j < menu_items_used)
2100 {
2101 Lisp_Object item;
2102 item = XVECTOR (menu_items)->contents[j];
2103 if (EQ (item, Qt))
2104 break;
2105 if (NILP (item))
2106 {
2107 j++;
2108 continue;
2109 }
2110 width = XSTRING (item)->size;
2111 if (width > maxwidth)
2112 maxwidth = width;
2113
2114 j += MENU_ITEMS_ITEM_LENGTH;
2115 }
2116 }
2117 /* Ignore a nil in the item list.
2118 It's meaningful only for dialog boxes. */
2119 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2120 i += 1;
2121 else
2122 {
2123 /* Create a new item within current pane. */
2124 Lisp_Object item_name, enable, descrip;
2125 unsigned char *item_data;
2126
2127 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2128 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2129 descrip
2130 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2131 if (!NILP (descrip))
2132 {
2133 int gap = maxwidth - XSTRING (item_name)->size;
2134 #ifdef C_ALLOCA
2135 Lisp_Object spacer;
2136 spacer = Fmake_string (make_number (gap), make_number (' '));
2137 item_name = concat2 (item_name, spacer);
2138 item_name = concat2 (item_name, descrip);
2139 item_data = XSTRING (item_name)->data;
2140 #else
2141 /* if alloca is fast, use that to make the space,
2142 to reduce gc needs. */
2143 item_data
2144 = (unsigned char *) alloca (maxwidth
2145 + XSTRING (descrip)->size + 1);
2146 bcopy (XSTRING (item_name)->data, item_data,
2147 XSTRING (item_name)->size);
2148 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2149 item_data[j] = ' ';
2150 bcopy (XSTRING (descrip)->data, item_data + j,
2151 XSTRING (descrip)->size);
2152 item_data[j + XSTRING (descrip)->size] = 0;
2153 #endif
2154 }
2155 else
2156 item_data = XSTRING (item_name)->data;
2157
2158 if (XMenuAddSelection (XDISPLAY menu, lpane, 0, item_data,
2159 !NILP (enable))
2160 == XM_FAILURE)
2161 {
2162 XMenuDestroy (XDISPLAY menu);
2163 *error = "Can't add selection to menu";
2164 return Qnil;
2165 }
2166 i += MENU_ITEMS_ITEM_LENGTH;
2167 }
2168 }
2169
2170 /* All set and ready to fly. */
2171 XMenuRecompute (XDISPLAY menu);
2172 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
2173 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
2174 x = min (x, dispwidth);
2175 y = min (y, dispheight);
2176 x = max (x, 1);
2177 y = max (y, 1);
2178 XMenuLocate (XDISPLAY menu, 0, 0, x, y,
2179 &ulx, &uly, &width, &height);
2180 if (ulx+width > dispwidth)
2181 {
2182 x -= (ulx + width) - dispwidth;
2183 ulx = dispwidth - width;
2184 }
2185 if (uly+height > dispheight)
2186 {
2187 y -= (uly + height) - dispheight;
2188 uly = dispheight - height;
2189 }
2190 if (ulx < 0) x -= ulx;
2191 if (uly < 0) y -= uly;
2192
2193 XMenuSetAEQ (menu, TRUE);
2194 XMenuSetFreeze (menu, TRUE);
2195 pane = selidx = 0;
2196
2197 status = XMenuActivate (XDISPLAY menu, &pane, &selidx,
2198 x, y, ButtonReleaseMask, &datap);
2199 switch (status)
2200 {
2201 case XM_SUCCESS:
2202 #ifdef XDEBUG
2203 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2204 #endif
2205
2206 /* Find the item number SELIDX in pane number PANE. */
2207 i = 0;
2208 while (i < menu_items_used)
2209 {
2210 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2211 {
2212 if (pane == 0)
2213 pane_prefix
2214 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2215 pane--;
2216 i += MENU_ITEMS_PANE_LENGTH;
2217 }
2218 else
2219 {
2220 if (pane == -1)
2221 {
2222 if (selidx == 0)
2223 {
2224 entry
2225 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2226 if (keymaps != 0)
2227 {
2228 entry = Fcons (entry, Qnil);
2229 if (!NILP (pane_prefix))
2230 entry = Fcons (pane_prefix, entry);
2231 }
2232 break;
2233 }
2234 selidx--;
2235 }
2236 i += MENU_ITEMS_ITEM_LENGTH;
2237 }
2238 }
2239 break;
2240
2241 case XM_FAILURE:
2242 XMenuDestroy (XDISPLAY menu);
2243 *error = "Can't activate menu";
2244 case XM_IA_SELECT:
2245 case XM_NO_SELECT:
2246 entry = Qnil;
2247 break;
2248 }
2249 XMenuDestroy (XDISPLAY menu);
2250
2251 /* State that no mouse buttons are now held.
2252 (The oldXMenu code doesn't track this info for us.)
2253 That is not necessarily true, but the fiction leads to reasonable
2254 results, and it is a pain to ask which are actually held now. */
2255 x_mouse_grabbed = 0;
2256
2257 return entry;
2258 }
2259 #endif /* not USE_X_TOOLKIT */
2260 \f
2261 syms_of_xmenu ()
2262 {
2263 staticpro (&menu_items);
2264 menu_items = Qnil;
2265
2266 popup_id_tick = (1<<16);
2267 defsubr (&Sx_popup_menu);
2268 defsubr (&Sx_popup_dialog);
2269 }