(xmenu_show): Unconditionally update x_mouse_grabbed and Vmouse_depressed.
[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 /* On 4.3 this loses if it comes after xterm.h. */
33 #include <signal.h>
34 #include <config.h>
35
36 #include <stdio.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/Shell.h>
68 #include <X11/Xaw/Paned.h>
69 #include "../lwlib/lwlib.h"
70 #include "../lwlib/xlwmenuP.h"
71 #endif /* USE_X_TOOLKIT */
72
73 #define min(x,y) (((x) < (y)) ? (x) : (y))
74 #define max(x,y) (((x) > (y)) ? (x) : (y))
75
76 #ifndef TRUE
77 #define TRUE 1
78 #define FALSE 0
79 #endif /* no TRUE */
80
81 #ifdef HAVE_X11
82 extern Display *x_current_display;
83 #else
84 #define ButtonReleaseMask ButtonReleased
85 #endif /* not HAVE_X11 */
86
87 /* We need a unique id for each popup menu and dialog box. */
88 static unsigned int popup_id_tick;
89
90 extern Lisp_Object Qmenu_enable;
91 extern Lisp_Object Qmenu_bar;
92
93 #ifdef USE_X_TOOLKIT
94 extern void process_expose_from_menu ();
95 extern XtAppContext Xt_app_con;
96
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 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
303
304 /* If a help string follows the item string, skip it. */
305 if (CONSP (XCONS (item1)->cdr)
306 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
307 item1 = XCONS (item1)->cdr;
308
309 def = Fcdr (item1);
310
311 /* Get out the saved equivalent-keyboard-key info. */
312 cachelist = savedkey = descrip = Qnil;
313 if (CONSP (def) && CONSP (XCONS (def)->car)
314 && (NILP (XCONS (XCONS (def)->car)->car)
315 || VECTORP (XCONS (XCONS (def)->car)->car)))
316 {
317 cachelist = XCONS (def)->car;
318 def = XCONS (def)->cdr;
319 savedkey = XCONS (cachelist)->car;
320 descrip = XCONS (cachelist)->cdr;
321 }
322
323 GCPRO4 (def, def1, savedkey, descrip);
324
325 /* Is it still valid? */
326 def1 = Qnil;
327 if (!NILP (savedkey))
328 def1 = Fkey_binding (savedkey, Qnil);
329 /* If not, update it. */
330 if (! EQ (def1, def)
331 /* If the command is an alias for another
332 (such as easymenu.el and lmenu.el set it up),
333 check if the original command matches the cached command. */
334 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
335 && EQ (def1, XSYMBOL (def)->function))
336 /* If something had no key binding before, don't recheck it--
337 doing that takes too much time and makes menus too slow. */
338 && !(!NILP (cachelist) && NILP (savedkey)))
339 {
340 changed = 1;
341 descrip = Qnil;
342 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
343 /* If the command is an alias for another
344 (such as easymenu.el and lmenu.el set it up),
345 see if the original command name has equivalent keys. */
346 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function))
347 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
348 Qnil, Qt, Qnil);
349
350 if (VECTORP (savedkey)
351 && EQ (XVECTOR (savedkey)->contents[0], Qmenu_bar))
352 savedkey = Qnil;
353 if (!NILP (savedkey))
354 {
355 descrip = Fkey_description (savedkey);
356 descrip = concat2 (make_string (" (", 3), descrip);
357 descrip = concat2 (descrip, make_string (")", 1));
358 }
359 }
360
361 /* Cache the data we just got in a sublist of the menu binding. */
362 if (NILP (cachelist))
363 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
364 else if (changed)
365 {
366 XCONS (cachelist)->car = savedkey;
367 XCONS (cachelist)->cdr = descrip;
368 }
369
370 UNGCPRO;
371 *descrip_ptr = descrip;
372 return def;
373 }
374
375 /* This is used as the handler when calling internal_condition_case_1. */
376
377 static Lisp_Object
378 menu_item_enabled_p_1 (arg)
379 Lisp_Object arg;
380 {
381 return Qnil;
382 }
383
384 /* Return non-nil if the command DEF is enabled when used as a menu item.
385 This is based on looking for a menu-enable property.
386 If NOTREAL is set, don't bother really computing this. */
387
388 static Lisp_Object
389 menu_item_enabled_p (def, notreal)
390 Lisp_Object def;
391 {
392 Lisp_Object enabled, tem;
393
394 enabled = Qt;
395 if (notreal)
396 return enabled;
397 if (XTYPE (def) == Lisp_Symbol)
398 {
399 /* No property, or nil, means enable.
400 Otherwise, enable if value is not nil. */
401 tem = Fget (def, Qmenu_enable);
402 if (!NILP (tem))
403 /* (condition-case nil (eval tem)
404 (error nil)) */
405 enabled = internal_condition_case_1 (Feval, tem, Qerror,
406 menu_item_enabled_p_1);
407 }
408 return enabled;
409 }
410 \f
411 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
412 and generate menu panes for them in menu_items.
413 If NOTREAL is nonzero,
414 don't bother really computing whether an item is enabled. */
415
416 static void
417 keymap_panes (keymaps, nmaps, notreal)
418 Lisp_Object *keymaps;
419 int nmaps;
420 int notreal;
421 {
422 int mapno;
423
424 init_menu_items ();
425
426 /* Loop over the given keymaps, making a pane for each map.
427 But don't make a pane that is empty--ignore that map instead.
428 P is the number of panes we have made so far. */
429 for (mapno = 0; mapno < nmaps; mapno++)
430 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
431
432 finish_menu_items ();
433 }
434
435 /* This is a recursive subroutine of keymap_panes.
436 It handles one keymap, KEYMAP.
437 The other arguments are passed along
438 or point to local variables of the previous function.
439 If NOTREAL is nonzero,
440 don't bother really computing whether an item is enabled. */
441
442 static void
443 single_keymap_panes (keymap, pane_name, prefix, notreal)
444 Lisp_Object keymap;
445 Lisp_Object pane_name;
446 Lisp_Object prefix;
447 int notreal;
448 {
449 Lisp_Object pending_maps;
450 Lisp_Object tail, item, item1, item_string, table;
451 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
452
453 pending_maps = Qnil;
454
455 push_menu_pane (pane_name, prefix);
456
457 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
458 {
459 /* Look at each key binding, and if it has a menu string,
460 make a menu item from it. */
461 item = XCONS (tail)->car;
462 if (XTYPE (item) == Lisp_Cons)
463 {
464 item1 = XCONS (item)->cdr;
465 if (XTYPE (item1) == Lisp_Cons)
466 {
467 item_string = XCONS (item1)->car;
468 if (XTYPE (item_string) == Lisp_String)
469 {
470 /* This is the real definition--the function to run. */
471 Lisp_Object def;
472 /* These are the saved equivalent keyboard key sequence
473 and its key-description. */
474 Lisp_Object descrip;
475 Lisp_Object tem, enabled;
476
477 /* GCPRO because ...enabled_p will call eval
478 and ..._equiv_key may autoload something.
479 Protecting KEYMAP preserves everything we use;
480 aside from that, must protect whatever might be
481 a string. Since there's no GCPRO5, we refetch
482 item_string instead of protecting it. */
483 descrip = def = Qnil;
484 GCPRO4 (keymap, pending_maps, def, descrip);
485
486 def = menu_item_equiv_key (item_string, item1, &descrip);
487 enabled = menu_item_enabled_p (def, notreal);
488
489 UNGCPRO;
490
491 item_string = XCONS (item1)->car;
492
493 tem = Fkeymapp (def);
494 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
495 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
496 pending_maps);
497 else
498 {
499 Lisp_Object submap;
500 GCPRO4 (keymap, pending_maps, descrip, item_string);
501 submap = get_keymap_1 (def, 0, 1);
502 UNGCPRO;
503 #ifndef USE_X_TOOLKIT
504 /* Indicate visually that this is a submenu. */
505 if (!NILP (submap))
506 item_string = concat2 (item_string,
507 build_string (" >"));
508 #endif
509 push_menu_item (item_string, enabled, XCONS (item)->car,
510 descrip);
511 #ifdef USE_X_TOOLKIT
512 /* Display a submenu using the toolkit. */
513 if (! NILP (submap))
514 {
515 push_submenu_start ();
516 single_keymap_panes (submap, Qnil,
517 XCONS (item)->car, notreal);
518 push_submenu_end ();
519 }
520 #endif
521 }
522 }
523 }
524 }
525 else if (XTYPE (item) == Lisp_Vector)
526 {
527 /* Loop over the char values represented in the vector. */
528 int len = XVECTOR (item)->size;
529 int c;
530 for (c = 0; c < len; c++)
531 {
532 Lisp_Object character;
533 XFASTINT (character) = c;
534 item1 = XVECTOR (item)->contents[c];
535 if (XTYPE (item1) == Lisp_Cons)
536 {
537 item_string = XCONS (item1)->car;
538 if (XTYPE (item_string) == Lisp_String)
539 {
540 Lisp_Object def;
541
542 /* These are the saved equivalent keyboard key sequence
543 and its key-description. */
544 Lisp_Object descrip;
545 Lisp_Object tem, enabled;
546
547 /* GCPRO because ...enabled_p will call eval
548 and ..._equiv_key may autoload something.
549 Protecting KEYMAP preserves everything we use;
550 aside from that, must protect whatever might be
551 a string. Since there's no GCPRO5, we refetch
552 item_string instead of protecting it. */
553 GCPRO4 (keymap, pending_maps, def, descrip);
554 descrip = def = Qnil;
555
556 def = menu_item_equiv_key (item_string, item1, &descrip);
557 enabled = menu_item_enabled_p (def, notreal);
558
559 UNGCPRO;
560
561 item_string = XCONS (item1)->car;
562
563 tem = Fkeymapp (def);
564 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
565 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
566 pending_maps);
567 else
568 {
569 Lisp_Object submap;
570 GCPRO4 (keymap, pending_maps, descrip, item_string);
571 submap = get_keymap_1 (def, 0, 1);
572 UNGCPRO;
573 #ifndef USE_X_TOOLKIT
574 if (!NILP (submap))
575 item_string = concat2 (item_string,
576 build_string (" >"));
577 #endif
578 push_menu_item (item_string, enabled, character,
579 descrip);
580 #ifdef USE_X_TOOLKIT
581 if (! NILP (submap))
582 {
583 push_submenu_start ();
584 single_keymap_panes (submap, Qnil,
585 character, notreal);
586 push_submenu_end ();
587 }
588 #endif
589 }
590 }
591 }
592 }
593 }
594 }
595
596 /* Process now any submenus which want to be panes at this level. */
597 while (!NILP (pending_maps))
598 {
599 Lisp_Object elt, eltcdr, string;
600 elt = Fcar (pending_maps);
601 eltcdr = XCONS (elt)->cdr;
602 string = XCONS (eltcdr)->car;
603 /* We no longer discard the @ from the beginning of the string here.
604 Instead, we do this in xmenu_show. */
605 single_keymap_panes (Fcar (elt), string,
606 XCONS (eltcdr)->cdr, notreal);
607 pending_maps = Fcdr (pending_maps);
608 }
609 }
610 \f
611 /* Push all the panes and items of a menu decsribed by the
612 alist-of-alists MENU.
613 This handles old-fashioned calls to x-popup-menu. */
614
615 static void
616 list_of_panes (menu)
617 Lisp_Object menu;
618 {
619 Lisp_Object tail;
620
621 init_menu_items ();
622
623 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
624 {
625 Lisp_Object elt, pane_name, pane_data;
626 elt = Fcar (tail);
627 pane_name = Fcar (elt);
628 CHECK_STRING (pane_name, 0);
629 push_menu_pane (pane_name, Qnil);
630 pane_data = Fcdr (elt);
631 CHECK_CONS (pane_data, 0);
632 list_of_items (pane_data);
633 }
634
635 finish_menu_items ();
636 }
637
638 /* Push the items in a single pane defined by the alist PANE. */
639
640 static void
641 list_of_items (pane)
642 Lisp_Object pane;
643 {
644 Lisp_Object tail, item, item1;
645
646 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
647 {
648 item = Fcar (tail);
649 if (STRINGP (item))
650 push_menu_item (item, Qnil, Qnil, Qnil);
651 else if (NILP (item))
652 push_left_right_boundary ();
653 else
654 {
655 CHECK_CONS (item, 0);
656 item1 = Fcar (item);
657 CHECK_STRING (item1, 1);
658 push_menu_item (item1, Qt, Fcdr (item), Qnil);
659 }
660 }
661 }
662 \f
663 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
664 "Pop up a deck-of-cards menu and return user's selection.\n\
665 POSITION is a position specification. This is either a mouse button event\n\
666 or a list ((XOFFSET YOFFSET) WINDOW)\n\
667 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
668 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
669 This controls the position of the center of the first line\n\
670 in the first pane of the menu, not the top left of the menu as a whole.\n\
671 If POSITION is t, it means to use the current mouse position.\n\
672 \n\
673 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
674 The menu items come from key bindings that have a menu string as well as\n\
675 a definition; actually, the \"definition\" in such a key binding looks like\n\
676 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
677 the keymap as a top-level element.\n\n\
678 You can also use a list of keymaps as MENU.\n\
679 Then each keymap makes a separate pane.\n\
680 When MENU is a keymap or a list of keymaps, the return value\n\
681 is a list of events.\n\n\
682 Alternatively, you can specify a menu of multiple panes\n\
683 with a list of the form (TITLE PANE1 PANE2...),\n\
684 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
685 Each ITEM is normally a cons cell (STRING . VALUE);\n\
686 but a string can appear as an item--that makes a nonselectable line\n\
687 in the menu.\n\
688 With this form of menu, the return value is VALUE from the chosen item.\n\
689 \n\
690 If POSITION is nil, don't display the menu at all, just precalculate the\n\
691 cached information about equivalent key sequences.")
692 (position, menu)
693 Lisp_Object position, menu;
694 {
695 int number_of_panes, panes;
696 Lisp_Object keymap, tem;
697 int xpos, ypos;
698 Lisp_Object title;
699 char *error_name;
700 Lisp_Object selection;
701 int i, j;
702 FRAME_PTR f;
703 Lisp_Object x, y, window;
704 int keymaps = 0;
705 int menubarp = 0;
706 struct gcpro gcpro1;
707
708 if (! NILP (position))
709 {
710 check_x ();
711
712 /* Decode the first argument: find the window and the coordinates. */
713 if (EQ (position, Qt))
714 {
715 /* Use the mouse's current position. */
716 FRAME_PTR new_f = 0;
717 Lisp_Object bar_window;
718 int part;
719 unsigned long time;
720
721 if (mouse_position_hook)
722 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
723 if (new_f != 0)
724 XSET (window, Lisp_Frame, new_f);
725 else
726 {
727 window = selected_window;
728 XFASTINT (x) = 0;
729 XFASTINT (y) = 0;
730 }
731 }
732 else
733 {
734 tem = Fcar (position);
735 if (XTYPE (tem) == Lisp_Cons)
736 {
737 window = Fcar (Fcdr (position));
738 x = Fcar (tem);
739 y = Fcar (Fcdr (tem));
740 }
741 else
742 {
743 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
744 window = Fcar (tem); /* POSN_WINDOW (tem) */
745 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
746 x = Fcar (tem);
747 y = Fcdr (tem);
748
749 /* Determine whether this menu is handling a menu bar click. */
750 tem = Fcar (Fcdr (Fcar (Fcdr (position))));
751 if (CONSP (tem) && EQ (Fcar (tem), Qmenu_bar))
752 menubarp = 1;
753 }
754 }
755
756 CHECK_NUMBER (x, 0);
757 CHECK_NUMBER (y, 0);
758
759 /* Decode where to put the menu. */
760
761 if (XTYPE (window) == Lisp_Frame)
762 {
763 f = XFRAME (window);
764
765 xpos = 0;
766 ypos = 0;
767 }
768 else if (XTYPE (window) == Lisp_Window)
769 {
770 CHECK_LIVE_WINDOW (window, 0);
771 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
772
773 xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left);
774 ypos = (f->display.x->line_height * XWINDOW (window)->top);
775 }
776 else
777 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
778 but I don't want to make one now. */
779 CHECK_WINDOW (window, 0);
780
781 xpos += XINT (x);
782 ypos += XINT (y);
783 }
784
785 title = Qnil;
786 GCPRO1 (title);
787
788 /* Decode the menu items from what was specified. */
789
790 keymap = Fkeymapp (menu);
791 tem = Qnil;
792 if (XTYPE (menu) == Lisp_Cons)
793 tem = Fkeymapp (Fcar (menu));
794 if (!NILP (keymap))
795 {
796 /* We were given a keymap. Extract menu info from the keymap. */
797 Lisp_Object prompt;
798 keymap = get_keymap (menu);
799
800 /* Extract the detailed info to make one pane. */
801 keymap_panes (&menu, 1, NILP (position));
802
803 /* Search for a string appearing directly as an element of the keymap.
804 That string is the title of the menu. */
805 prompt = map_prompt (keymap);
806
807 /* Make that be the pane title of the first pane. */
808 if (!NILP (prompt) && menu_items_n_panes >= 0)
809 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
810
811 keymaps = 1;
812 }
813 else if (!NILP (tem))
814 {
815 /* We were given a list of keymaps. */
816 int nmaps = XFASTINT (Flength (menu));
817 Lisp_Object *maps
818 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
819 int i;
820
821 title = Qnil;
822
823 /* The first keymap that has a prompt string
824 supplies the menu title. */
825 for (tem = menu, i = 0; XTYPE (tem) == Lisp_Cons; tem = Fcdr (tem))
826 {
827 Lisp_Object prompt;
828
829 maps[i++] = keymap = get_keymap (Fcar (tem));
830
831 prompt = map_prompt (keymap);
832 if (NILP (title) && !NILP (prompt))
833 title = prompt;
834 }
835
836 /* Extract the detailed info to make one pane. */
837 keymap_panes (maps, nmaps, NILP (position));
838
839 /* Make the title be the pane title of the first pane. */
840 if (!NILP (title) && menu_items_n_panes >= 0)
841 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
842
843 keymaps = 1;
844 }
845 else
846 {
847 /* We were given an old-fashioned menu. */
848 title = Fcar (menu);
849 CHECK_STRING (title, 1);
850
851 list_of_panes (Fcdr (menu));
852
853 keymaps = 0;
854 }
855
856 if (NILP (position))
857 {
858 discard_menu_items ();
859 UNGCPRO;
860 return Qnil;
861 }
862
863 /* Display them in a menu. */
864 BLOCK_INPUT;
865
866 selection = xmenu_show (f, xpos, ypos, menubarp,
867 keymaps, title, &error_name);
868 UNBLOCK_INPUT;
869
870 discard_menu_items ();
871
872 UNGCPRO;
873
874 if (error_name) error (error_name);
875 return selection;
876 }
877
878 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
879 "Pop up a dialog box and return user's selection.\n\
880 POSITION specifies which frame to use.\n\
881 This is normally a mouse button event or a window or frame.\n\
882 If POSITION is t, it means to use the frame the mouse is on.\n\
883 The dialog box appears in the middle of the specified frame.\n\
884 \n\
885 CONTENTS specifies the alternatives to display in the dialog box.\n\
886 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
887 Each ITEM is a cons cell (STRING . VALUE).\n\
888 The return value is VALUE from the chosen item.\n\n\
889 An ITEM may also be just a string--that makes a nonselectable item.\n\
890 An ITEM may also be nil--that means to put all preceding items\n\
891 on the left of the dialog box and all following items on the right.\n\
892 \(By default, approximately half appear on each side.)")
893 (position, contents)
894 Lisp_Object position, contents;
895 {
896 FRAME_PTR f;
897 Lisp_Object window;
898
899 check_x ();
900
901 /* Decode the first argument: find the window or frame to use. */
902 if (EQ (position, Qt))
903 {
904 #if 0 /* Using the frame the mouse is on may not be right. */
905 /* Use the mouse's current position. */
906 FRAME_PTR new_f = 0;
907 Lisp_Object bar_window;
908 int part;
909 unsigned long time;
910 Lisp_Object x, y;
911
912 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
913
914 if (new_f != 0)
915 XSET (window, Lisp_Frame, new_f);
916 else
917 window = selected_window;
918 #endif
919 /* Decode the first argument: find the window and the coordinates. */
920 if (EQ (position, Qt))
921 window = selected_window;
922 }
923 else if (CONSP (position))
924 {
925 Lisp_Object tem;
926 tem = Fcar (position);
927 if (XTYPE (tem) == Lisp_Cons)
928 window = Fcar (Fcdr (position));
929 else
930 {
931 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
932 window = Fcar (tem); /* POSN_WINDOW (tem) */
933 }
934 }
935 else if (WINDOWP (position) || FRAMEP (position))
936 window = position;
937
938 /* Decode where to put the menu. */
939
940 if (XTYPE (window) == Lisp_Frame)
941 f = XFRAME (window);
942 else if (XTYPE (window) == Lisp_Window)
943 {
944 CHECK_LIVE_WINDOW (window, 0);
945 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
946 }
947 else
948 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
949 but I don't want to make one now. */
950 CHECK_WINDOW (window, 0);
951
952 #ifndef USE_X_TOOLKIT
953 /* Display a menu with these alternatives
954 in the middle of frame F. */
955 {
956 Lisp_Object x, y, frame, newpos;
957 XSET (frame, Lisp_Frame, f);
958 XSET (x, Lisp_Int, x_pixel_width (f) / 2);
959 XSET (y, Lisp_Int, x_pixel_height (f) / 2);
960 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
961
962 return Fx_popup_menu (newpos,
963 Fcons (Fcar (contents), Fcons (contents, Qnil)));
964 }
965 #else
966 {
967 Lisp_Object title;
968 char *error_name;
969 Lisp_Object selection;
970
971 /* Decode the dialog items from what was specified. */
972 title = Fcar (contents);
973 CHECK_STRING (title, 1);
974
975 list_of_panes (Fcons (contents, Qnil));
976
977 /* Display them in a dialog box. */
978 BLOCK_INPUT;
979 selection = xdialog_show (f, 0, 0, title, &error_name);
980 UNBLOCK_INPUT;
981
982 discard_menu_items ();
983
984 if (error_name) error (error_name);
985 return selection;
986 }
987 #endif
988 }
989 \f
990 #ifdef USE_X_TOOLKIT
991
992 static void
993 dispatch_dummy_expose (w, x, y)
994 Widget w;
995 int x;
996 int y;
997 {
998 XExposeEvent dummy;
999
1000 dummy.type = Expose;
1001 dummy.window = XtWindow (w);
1002 dummy.count = 0;
1003 dummy.serial = 0;
1004 dummy.send_event = 0;
1005 dummy.display = XtDisplay (w);
1006 dummy.x = x;
1007 dummy.y = y;
1008
1009 XtDispatchEvent ((XEvent *) &dummy);
1010 }
1011
1012 static int
1013 event_is_in_menu_item (mw, event, name, string_w)
1014 XlwMenuWidget mw;
1015 struct input_event *event;
1016 char *name;
1017 int *string_w;
1018 {
1019 *string_w += (string_width (mw, name)
1020 + 2 * (mw->menu.horizontal_spacing
1021 + mw->menu.shadow_thickness));
1022 return XINT (event->x) < *string_w;
1023 }
1024
1025
1026 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1027
1028 Lisp_Object
1029 map_event_to_object (event, f)
1030 struct input_event *event;
1031 FRAME_PTR f;
1032 {
1033 int i,j, string_w;
1034 window_state* ws;
1035 XlwMenuWidget mw = (XlwMenuWidget) f->display.x->menubar_widget;
1036 widget_value *val;
1037
1038
1039 string_w = 0;
1040 /* Find the window */
1041 for (val = mw->menu.old_stack [0]->contents; val; val = val->next)
1042 {
1043 ws = &mw->menu.windows [0];
1044 if (ws && event_is_in_menu_item (mw, event, val->name, &string_w))
1045 {
1046 Lisp_Object items;
1047 int i;
1048
1049 items = FRAME_MENU_BAR_ITEMS (f);
1050
1051 for (i = 0; i < XVECTOR (items)->size; i += 3)
1052 {
1053 Lisp_Object pos, string, item;
1054 item = XVECTOR (items)->contents[i];
1055 string = XVECTOR (items)->contents[i + 1];
1056 pos = XVECTOR (items)->contents[i + 2];
1057 if (NILP (string))
1058 break;
1059
1060 if (!strcmp (val->name, XSTRING (string)->data))
1061 return item;
1062 }
1063 }
1064 }
1065 return Qnil;
1066 }
1067
1068 #ifdef __STDC__
1069 static Lisp_Object *volatile menu_item_selection;
1070 #else
1071 static Lisp_Object *menu_item_selection;
1072 #endif
1073
1074 static void
1075 popup_selection_callback (widget, id, client_data)
1076 Widget widget;
1077 LWLIB_ID id;
1078 XtPointer client_data;
1079 {
1080 menu_item_selection = (Lisp_Object *) client_data;
1081 }
1082
1083 static void
1084 popup_down_callback (widget, id, client_data)
1085 Widget widget;
1086 LWLIB_ID id;
1087 XtPointer client_data;
1088 {
1089 BLOCK_INPUT;
1090 lw_destroy_all_widgets (id);
1091 UNBLOCK_INPUT;
1092 }
1093
1094 static void
1095 dialog_selection_callback (widget, id, client_data)
1096 Widget widget;
1097 LWLIB_ID id;
1098 XtPointer client_data;
1099 {
1100 if ((int)client_data != -1)
1101 menu_item_selection = (Lisp_Object *) client_data;
1102 BLOCK_INPUT;
1103 lw_destroy_all_widgets (id);
1104 UNBLOCK_INPUT;
1105 }
1106
1107 /* This recursively calls free_widget_value() on the tree of widgets.
1108 It must free all data that was malloc'ed for these widget_values.
1109 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1110 must be left alone. */
1111
1112 void
1113 free_menubar_widget_value_tree (wv)
1114 widget_value *wv;
1115 {
1116 if (! wv) return;
1117
1118 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1119
1120 if (wv->contents && (wv->contents != (widget_value*)1))
1121 {
1122 free_menubar_widget_value_tree (wv->contents);
1123 wv->contents = (widget_value *) 0xDEADBEEF;
1124 }
1125 if (wv->next)
1126 {
1127 free_menubar_widget_value_tree (wv->next);
1128 wv->next = (widget_value *) 0xDEADBEEF;
1129 }
1130 BLOCK_INPUT;
1131 free_widget_value (wv);
1132 UNBLOCK_INPUT;
1133 }
1134
1135 extern void EmacsFrameSetCharSize ();
1136
1137 static void
1138 update_frame_menubar (f)
1139 FRAME_PTR f;
1140 {
1141 struct x_display *x = f->display.x;
1142 int columns, rows;
1143 int menubar_changed;
1144
1145 menubar_changed = (x->menubar_widget
1146 && !XtIsManaged (x->menubar_widget));
1147
1148 if (! (menubar_changed))
1149 return;
1150
1151 BLOCK_INPUT;
1152 /* Save the size of the frame because the pane widget doesn't accept to
1153 resize itself. So force it. */
1154 columns = f->width;
1155 rows = f->height;
1156
1157
1158 XawPanedSetRefigureMode (x->column_widget, 0);
1159
1160 /* the order in which children are managed is the top to
1161 bottom order in which they are displayed in the paned window.
1162 First, remove the text-area widget.
1163 */
1164 XtUnmanageChild (x->edit_widget);
1165
1166 /* remove the menubar that is there now, and put up the menubar that
1167 should be there.
1168 */
1169 if (menubar_changed)
1170 {
1171 XtManageChild (x->menubar_widget);
1172 XtMapWidget (x->menubar_widget);
1173 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1174 }
1175
1176
1177 /* Re-manage the text-area widget */
1178 XtManageChild (x->edit_widget);
1179
1180 /* and now thrash the sizes */
1181 XawPanedSetRefigureMode (x->column_widget, 1);
1182
1183 /* Force the pane widget to resize itself with the right values. */
1184 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1185
1186 UNBLOCK_INPUT;
1187 }
1188
1189 void
1190 set_frame_menubar (f, first_time)
1191 FRAME_PTR f;
1192 int first_time;
1193 {
1194 Widget menubar_widget = f->display.x->menubar_widget;
1195 int id = (int) f;
1196 Lisp_Object tail, items;
1197 widget_value *wv, *save_wv, *first_wv, *prev_wv = 0;
1198 int i;
1199
1200 BLOCK_INPUT;
1201
1202 wv = malloc_widget_value ();
1203 wv->name = "menubar";
1204 wv->value = 0;
1205 wv->enabled = 1;
1206 save_wv = first_wv = wv;
1207
1208 if (NILP (items = FRAME_MENU_BAR_ITEMS (f)))
1209 items = FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1210
1211 for (i = 0; i < XVECTOR (items)->size; i += 3)
1212 {
1213 Lisp_Object string;
1214
1215 string = XVECTOR (items)->contents[i + 1];
1216 if (NILP (string))
1217 break;
1218
1219 wv = malloc_widget_value ();
1220 if (prev_wv)
1221 prev_wv->next = wv;
1222 else
1223 save_wv->contents = wv;
1224 wv->name = (char *) XSTRING (string)->data;
1225 wv->value = 0;
1226 wv->enabled = 1;
1227 prev_wv = wv;
1228 }
1229
1230 if (menubar_widget)
1231 lw_modify_all_widgets (id, first_wv, False);
1232 else
1233 {
1234 menubar_widget = lw_create_widget ("menubar", "menubar",
1235 id, first_wv,
1236 f->display.x->column_widget,
1237 0, 0,
1238 0, 0);
1239 f->display.x->menubar_widget = menubar_widget;
1240 XtVaSetValues (menubar_widget,
1241 XtNshowGrip, 0,
1242 XtNresizeToPreferred, 1,
1243 XtNallowResize, 1,
1244 0);
1245 }
1246
1247 free_menubar_widget_value_tree (first_wv);
1248
1249 /* Don't update the menubar the first time it is created via x_window. */
1250 if (!first_time)
1251 update_frame_menubar (f);
1252
1253 UNBLOCK_INPUT;
1254 }
1255
1256 void
1257 free_frame_menubar (f)
1258 FRAME_PTR f;
1259 {
1260 Widget menubar_widget;
1261 int id;
1262
1263 menubar_widget = f->display.x->menubar_widget;
1264 id = (int) f;
1265
1266 if (menubar_widget)
1267 {
1268 BLOCK_INPUT;
1269 lw_destroy_all_widgets (id);
1270 UNBLOCK_INPUT;
1271 }
1272 }
1273 /* Called from Fx_create_frame to create the inital menubar of a frame
1274 before it is mapped, so that the window is mapped with the menubar already
1275 there instead of us tacking it on later and thrashing the window after it
1276 is visible. */
1277 void
1278 initialize_frame_menubar (f)
1279 FRAME_PTR f;
1280 {
1281 set_frame_menubar (f, 1);
1282 }
1283 \f
1284 /* Horizontal bounds of the current menu bar item. */
1285
1286 static int this_menu_bar_item_beg;
1287 static int this_menu_bar_item_end;
1288
1289 /* Horizontal position of the end of the last menu bar item. */
1290
1291 static int last_menu_bar_item_end;
1292
1293 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1294 but not in the current menu bar item. */
1295
1296 static int
1297 other_menu_bar_item_p (f, x, y)
1298 FRAME_PTR f;
1299 int x, y;
1300 {
1301 return (y >= 0
1302 && f->display.x->menubar_widget != 0
1303 && y < f->display.x->menubar_widget->core.height
1304 && x >= 0
1305 && x < last_menu_bar_item_end
1306 && (x >= this_menu_bar_item_end
1307 || x < this_menu_bar_item_beg));
1308 }
1309
1310 /* Unread a button-press event in the menu bar of frame F
1311 at x position XPOS relative to the inside of the frame. */
1312
1313 static void
1314 unread_menu_bar_button (f, xpos)
1315 FRAME_PTR f;
1316 int xpos;
1317 {
1318 XEvent event;
1319
1320 event.type = ButtonPress;
1321 event.xbutton.display = x_current_display;
1322 event.xbutton.serial = 0;
1323 event.xbutton.send_event = 0;
1324 event.xbutton.time = CurrentTime;
1325 event.xbutton.button = Button1;
1326 event.xbutton.window = XtWindow (f->display.x->menubar_widget);
1327 event.xbutton.x = xpos;
1328 XPutBackEvent (XDISPLAY &event);
1329 }
1330
1331 /* If the mouse has moved to another menu bar item,
1332 return 1 and unread a button press event for that item.
1333 Otherwise return 0. */
1334
1335 static int
1336 check_mouse_other_menu_bar (f)
1337 FRAME_PTR f;
1338 {
1339 FRAME_PTR new_f;
1340 Lisp_Object bar_window;
1341 int part;
1342 Lisp_Object x, y;
1343 unsigned long time;
1344
1345 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
1346
1347 if (f == new_f && other_menu_bar_item_p (f, x, y))
1348 {
1349 unread_menu_bar_button (f, x);
1350 return 1;
1351 }
1352
1353 return 0;
1354 }
1355 #endif /* USE_X_TOOLKIT */
1356 \f
1357 /* xmenu_show actually displays a menu using the panes and items in menu_items
1358 and returns the value selected from it.
1359 There are two versions of xmenu_show, one for Xt and one for Xlib.
1360 Both assume input is blocked by the caller. */
1361
1362 /* F is the frame the menu is for.
1363 X and Y are the frame-relative specified position,
1364 relative to the inside upper left corner of the frame F.
1365 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1366 KEYMAPS is 1 if this menu was specified with keymaps;
1367 in that case, we return a list containing the chosen item's value
1368 and perhaps also the pane's prefix.
1369 TITLE is the specified menu title.
1370 ERROR is a place to store an error message string in case of failure.
1371 (We return nil on failure, but the value doesn't actually matter.) */
1372
1373 #ifdef USE_X_TOOLKIT
1374
1375 extern unsigned last_event_timestamp;
1376 extern Lisp_Object Vdouble_click_time;
1377
1378 extern unsigned int x_mouse_grabbed;
1379 extern Lisp_Object Vmouse_depressed;
1380
1381 static Lisp_Object
1382 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1383 FRAME_PTR f;
1384 int x;
1385 int y;
1386 int menubarp;
1387 int keymaps;
1388 Lisp_Object title;
1389 char **error;
1390 {
1391 int i;
1392 int menu_id;
1393 Widget menu;
1394 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1395 Arg av [2];
1396 int ac = 0;
1397
1398 /* This is the menu bar item (if any) that led to this menu. */
1399 widget_value *menubar_item = 0;
1400
1401 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1402 widget_value **submenu_stack
1403 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1404 Lisp_Object *subprefix_stack
1405 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1406 int submenu_depth = 0;
1407
1408 /* Define a queue to save up for later unreading
1409 all X events that don't pertain to the menu. */
1410 struct event_queue
1411 {
1412 XEvent event;
1413 struct event_queue *next;
1414 };
1415
1416 struct event_queue *queue = NULL;
1417 struct event_queue *queue_tmp;
1418
1419 Position root_x, root_y;
1420
1421 int first_pane;
1422 int next_release_must_exit = 0;
1423
1424 *error = NULL;
1425
1426 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1427 {
1428 *error = "Empty menu";
1429 return Qnil;
1430 }
1431 this_menu_bar_item_beg = -1;
1432 this_menu_bar_item_end = -1;
1433 last_menu_bar_item_end = -1;
1434
1435 /* Figure out which menu bar item, if any, this menu is for. */
1436 if (menubarp)
1437 {
1438 int xbeg;
1439 int xend = 0;
1440 widget_value *mb_item = 0;
1441
1442 for (mb_item = menubar->menu.old_stack[0]->contents;
1443 mb_item;
1444 mb_item = mb_item->next)
1445 {
1446 xbeg = xend;
1447 xend += (string_width (menubar, mb_item->name)
1448 + 2 * (menubar->menu.horizontal_spacing
1449 + menubar->menu.shadow_thickness));
1450 if (x >= xbeg && x < xend)
1451 {
1452 x = xbeg + 4;
1453 y = 0;
1454 menubar_item = mb_item;
1455 /* Arrange to show a different menu if we move in the menu bar
1456 to a different item. */
1457 this_menu_bar_item_beg = xbeg;
1458 this_menu_bar_item_end = xend;
1459 }
1460 }
1461 last_menu_bar_item_end = xend;
1462 }
1463 if (menubar_item == 0)
1464 menubarp = 0;
1465
1466 /* Offset the coordinates to root-relative. */
1467 if (f->display.x->menubar_widget != 0)
1468 y += f->display.x->menubar_widget->core.height;
1469 XtTranslateCoords (f->display.x->widget,
1470 x, y, &root_x, &root_y);
1471 x = root_x;
1472 y = root_y;
1473
1474 /* Create a tree of widget_value objects
1475 representing the panes and their items. */
1476 wv = malloc_widget_value ();
1477 wv->name = "menu";
1478 wv->value = 0;
1479 wv->enabled = 1;
1480 first_wv = wv;
1481 first_pane = 1;
1482
1483 /* Loop over all panes and items, filling in the tree. */
1484 i = 0;
1485 while (i < menu_items_used)
1486 {
1487 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1488 {
1489 submenu_stack[submenu_depth++] = save_wv;
1490 save_wv = prev_wv;
1491 prev_wv = 0;
1492 first_pane = 1;
1493 i++;
1494 }
1495 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1496 {
1497 prev_wv = save_wv;
1498 save_wv = submenu_stack[--submenu_depth];
1499 first_pane = 0;
1500 i++;
1501 }
1502 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1503 && submenu_depth != 0)
1504 i += MENU_ITEMS_PANE_LENGTH;
1505 /* Ignore a nil in the item list.
1506 It's meaningful only for dialog boxes. */
1507 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1508 i += 1;
1509 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1510 {
1511 /* Create a new pane. */
1512 Lisp_Object pane_name, prefix;
1513 char *pane_string;
1514 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1515 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1516 pane_string = (NILP (pane_name)
1517 ? "" : (char *) XSTRING (pane_name)->data);
1518 /* If there is just one top-level pane, put all its items directly
1519 under the top-level menu. */
1520 if (menu_items_n_panes == 1)
1521 pane_string = "";
1522
1523 /* If the pane has a meaningful name,
1524 make the pane a top-level menu item
1525 with its items as a submenu beneath it. */
1526 if (!keymaps && strcmp (pane_string, ""))
1527 {
1528 wv = malloc_widget_value ();
1529 if (save_wv)
1530 save_wv->next = wv;
1531 else
1532 first_wv->contents = wv;
1533 wv->name = pane_string;
1534 if (keymaps && !NILP (prefix))
1535 wv->name++;
1536 wv->value = 0;
1537 wv->enabled = 1;
1538 save_wv = wv;
1539 prev_wv = 0;
1540 }
1541 else if (first_pane)
1542 {
1543 save_wv = wv;
1544 prev_wv = 0;
1545 }
1546 first_pane = 0;
1547 i += MENU_ITEMS_PANE_LENGTH;
1548 }
1549 else
1550 {
1551 /* Create a new item within current pane. */
1552 Lisp_Object item_name, enable, descrip;
1553 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1554 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1555 descrip
1556 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1557
1558 wv = malloc_widget_value ();
1559 if (prev_wv)
1560 prev_wv->next = wv;
1561 else
1562 save_wv->contents = wv;
1563 wv->name = (char *) XSTRING (item_name)->data;
1564 if (!NILP (descrip))
1565 wv->key = (char *) XSTRING (descrip)->data;
1566 wv->value = 0;
1567 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1568 wv->enabled = !NILP (enable);
1569 prev_wv = wv;
1570
1571 i += MENU_ITEMS_ITEM_LENGTH;
1572 }
1573 }
1574
1575 /* Actually create the menu. */
1576 menu_id = ++popup_id_tick;
1577 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1578 f->display.x->widget, 1, 0,
1579 popup_selection_callback, popup_down_callback);
1580
1581 /* Don't allow any geometry request from the user. */
1582 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1583 XtSetValues (menu, av, ac);
1584
1585 /* Free the widget_value objects we used to specify the contents. */
1586 free_menubar_widget_value_tree (first_wv);
1587
1588 /* No selection has been chosen yet. */
1589 menu_item_selection = 0;
1590
1591 /* If the mouse moves out of the menu before we show the menu,
1592 don't show it at all. */
1593 if (check_mouse_other_menu_bar (f))
1594 {
1595 lw_destroy_all_widgets (menu_id);
1596 return Qnil;
1597 }
1598
1599
1600 /* Highlight the menu bar item (if any) that led to this menu. */
1601 if (menubarp)
1602 {
1603 menubar_item->call_data = (XtPointer) 1;
1604 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1605 }
1606
1607 /* Display the menu. */
1608 {
1609 XButtonPressedEvent dummy;
1610 XlwMenuWidget mw;
1611
1612 mw = (XlwMenuWidget) ((CompositeWidget)menu)->composite.children[0];
1613
1614 dummy.type = ButtonPress;
1615 dummy.serial = 0;
1616 dummy.send_event = 0;
1617 dummy.display = XtDisplay (menu);
1618 dummy.window = XtWindow (XtParent (menu));
1619 dummy.time = CurrentTime;
1620 dummy.button = 0;
1621 dummy.x_root = x;
1622 dummy.y_root = y;
1623
1624 /* We activate directly the lucid implementation. */
1625 pop_up_menu (mw, &dummy);
1626 }
1627
1628 /* No need to check a second time since this is done in the XEvent loop.
1629 This slows done the execution. */
1630 #ifdef XMENU_FOO
1631 /* Check again whether the mouse has moved to another menu bar item. */
1632 if (check_mouse_other_menu_bar (f))
1633 {
1634 /* The mouse moved into a different menu bar item.
1635 We should bring up that item's menu instead.
1636 First pop down this menu. */
1637 #if 0 /* xlwmenu.c now does this. */
1638 XtUngrabPointer ((Widget)
1639 ((XlwMenuWidget)
1640 ((CompositeWidget)menu)->composite.children[0]),
1641 CurrentTime);
1642 #endif
1643 lw_destroy_all_widgets (menu_id);
1644 goto pop_down;
1645 }
1646 #endif
1647
1648 /* Process events that apply to the menu. */
1649 while (1)
1650 {
1651 XEvent event;
1652 int queue_and_exit = 0;
1653
1654 XtAppNextEvent (Xt_app_con, &event);
1655 if (event.type == ButtonRelease)
1656 {
1657 XtDispatchEvent (&event);
1658
1659 /* Do the work of construct_mouse_click since it can't
1660 be called. Initially, the popup menu has been called
1661 from a ButtonPress in the edit_widget. Then the mouse
1662 has been set to grabbed. Reset it now. */
1663 x_mouse_grabbed &= ~(1 << event.xbutton.button);
1664 if (!x_mouse_grabbed)
1665 Vmouse_depressed = Qnil;
1666
1667 /* If we release the button soon without selecting anything,
1668 stay in the loop--that is, leave the menu posted.
1669 Otherwise, exit this loop and thus pop down the menu. */
1670 if (! (menu_item_selection == 0
1671 && !next_release_must_exit
1672 && (((XButtonEvent *) (&event))->time - last_event_timestamp
1673 < XINT (Vdouble_click_time))))
1674 break;
1675 /* Don't call XtDispatchEvent again for the same event! */
1676 continue;
1677 }
1678 else if (event.type == ButtonPress)
1679 {
1680 next_release_must_exit = 1;
1681 #if 0
1682 XtDispatchEvent (&event);
1683 /* Any mouse button activity that doesn't select in the menu
1684 should unpost the menu. */
1685 if (menu_item_selection == 0)
1686 break;
1687 #endif
1688 /* Don't call XtDispatchEvent for the down event.
1689 Doing so seems to give strange results
1690 when you click on the menu bar while a menu is posted. */
1691 continue;
1692 }
1693 else if (event.type == KeyPress)
1694 {
1695 /* Exit the loop, but first queue this event for reuse. */
1696 queue_and_exit = 1;
1697 }
1698 else if (event.type == Expose)
1699 process_expose_from_menu (event);
1700 /* If the mouse moves to a different menu bar item, switch to
1701 that item's menu. But only if the button is still held down. */
1702 else if (event.type == MotionNotify
1703 && x_mouse_grabbed)
1704 {
1705 int event_x = (event.xmotion.x_root
1706 - (f->display.x->widget->core.x
1707 + f->display.x->widget->core.border_width));
1708 int event_y = (event.xmotion.y_root
1709 - (f->display.x->widget->core.y
1710 + f->display.x->widget->core.border_width));
1711
1712 if (other_menu_bar_item_p (f, event_x, event_y))
1713 {
1714 /* The mouse moved into a different menu bar item.
1715 We should bring up that item's menu instead.
1716 First pop down this menu. */
1717 #if 0 /* xlwmenu.c now does this. */
1718 XtUngrabPointer ((Widget)
1719 ((XlwMenuWidget)
1720 ((CompositeWidget)menu)->composite.children[0]),
1721 event.xbutton.time);
1722 #endif
1723 lw_destroy_all_widgets (menu_id);
1724
1725 /* Put back an event that will bring up the other item's menu. */
1726 unread_menu_bar_button (f, event_x);
1727 /* Don't let us select anything in this case. */
1728 menu_item_selection = 0;
1729 break;
1730 }
1731 }
1732 else if (event.type == UnmapNotify)
1733 {
1734 /* If the menu disappears, there is no need to stay in the
1735 loop. */
1736 if (event.xunmap.window == menu->core.window)
1737 break;
1738 }
1739
1740 XtDispatchEvent (&event);
1741 if (queue_and_exit
1742 || XtWindowToWidget (XDISPLAY event.xany.window) != menu)
1743 {
1744 queue_tmp
1745 = (struct event_queue *) malloc (sizeof (struct event_queue));
1746
1747 if (queue_tmp != NULL)
1748 {
1749 queue_tmp->event = event;
1750 queue_tmp->next = queue;
1751 queue = queue_tmp;
1752 }
1753 }
1754 if (queue_and_exit)
1755 break;
1756 }
1757
1758 pop_down:
1759 /* Unhighlight the menu bar item (if any) that led to this menu. */
1760 if (menubarp)
1761 {
1762 menubar_item->call_data = (XtPointer) 0;
1763 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1764 }
1765
1766 /* fp turned off the following statement and wrote a comment
1767 that it is unnecessary--that the menu has already disappeared.
1768 I observer that is not so. -- rms. */
1769 /* Make sure the menu disappears. */
1770 lw_destroy_all_widgets (menu_id);
1771
1772 /* Unread any events that we got but did not handle. */
1773 while (queue != NULL)
1774 {
1775 queue_tmp = queue;
1776 XPutBackEvent (XDISPLAY &queue_tmp->event);
1777 queue = queue_tmp->next;
1778 free ((char *)queue_tmp);
1779 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1780 interrupt_input_pending = 1;
1781 }
1782
1783 /* Find the selected item, and its pane, to return
1784 the proper value. */
1785 if (menu_item_selection != 0)
1786 {
1787 Lisp_Object prefix;
1788
1789 prefix = Qnil;
1790 i = 0;
1791 while (i < menu_items_used)
1792 {
1793 Lisp_Object entry;
1794
1795 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1796 {
1797 subprefix_stack[submenu_depth++] = prefix;
1798 prefix = entry;
1799 i++;
1800 }
1801 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1802 {
1803 prefix = subprefix_stack[--submenu_depth];
1804 i++;
1805 }
1806 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1807 {
1808 prefix
1809 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1810 i += MENU_ITEMS_PANE_LENGTH;
1811 }
1812 else
1813 {
1814 entry
1815 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1816 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1817 {
1818 if (keymaps != 0)
1819 {
1820 int j;
1821
1822 entry = Fcons (entry, Qnil);
1823 if (!NILP (prefix))
1824 entry = Fcons (prefix, entry);
1825 for (j = submenu_depth - 1; j >= 0; j--)
1826 if (!NILP (subprefix_stack[j]))
1827 entry = Fcons (subprefix_stack[j], entry);
1828 }
1829 return entry;
1830 }
1831 i += MENU_ITEMS_ITEM_LENGTH;
1832 }
1833 }
1834 }
1835
1836 return Qnil;
1837 }
1838
1839 static char * button_names [] = {
1840 "button1", "button2", "button3", "button4", "button5",
1841 "button6", "button7", "button8", "button9", "button10" };
1842
1843 static Lisp_Object
1844 xdialog_show (f, menubarp, keymaps, title, error)
1845 FRAME_PTR f;
1846 int menubarp;
1847 int keymaps;
1848 Lisp_Object title;
1849 char **error;
1850 {
1851 int i, nb_buttons=0;
1852 int dialog_id;
1853 Widget menu;
1854 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1855 char dialog_name[6];
1856
1857 /* This is the menu bar item (if any) that led to this menu. */
1858 widget_value *menubar_item = 0;
1859
1860 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1861
1862 /* Define a queue to save up for later unreading
1863 all X events that don't pertain to the menu. */
1864 struct event_queue
1865 {
1866 XEvent event;
1867 struct event_queue *next;
1868 };
1869
1870 struct event_queue *queue = NULL;
1871 struct event_queue *queue_tmp;
1872
1873 /* Number of elements seen so far, before boundary. */
1874 int left_count = 0;
1875 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1876 int boundary_seen = 0;
1877
1878 *error = NULL;
1879
1880 if (menu_items_n_panes > 1)
1881 {
1882 *error = "Multiple panes in dialog box";
1883 return Qnil;
1884 }
1885
1886 /* Create a tree of widget_value objects
1887 representing the text label and buttons. */
1888 {
1889 Lisp_Object pane_name, prefix;
1890 char *pane_string;
1891 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1892 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1893 pane_string = (NILP (pane_name)
1894 ? "" : (char *) XSTRING (pane_name)->data);
1895 prev_wv = malloc_widget_value ();
1896 prev_wv->value = pane_string;
1897 if (keymaps && !NILP (prefix))
1898 prev_wv->name++;
1899 prev_wv->enabled = 1;
1900 prev_wv->name = "message";
1901 first_wv = prev_wv;
1902
1903 /* Loop over all panes and items, filling in the tree. */
1904 i = MENU_ITEMS_PANE_LENGTH;
1905 while (i < menu_items_used)
1906 {
1907
1908 /* Create a new item within current pane. */
1909 Lisp_Object item_name, enable, descrip;
1910 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1911 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1912 descrip
1913 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1914
1915 if (NILP (item_name))
1916 {
1917 free_menubar_widget_value_tree (first_wv);
1918 *error = "Submenu in dialog items";
1919 return Qnil;
1920 }
1921 if (EQ (item_name, Qquote))
1922 {
1923 /* This is the boundary between left-side elts
1924 and right-side elts. Stop incrementing right_count. */
1925 boundary_seen = 1;
1926 i++;
1927 continue;
1928 }
1929 if (nb_buttons >= 10)
1930 {
1931 free_menubar_widget_value_tree (first_wv);
1932 *error = "Too many dialog items";
1933 return Qnil;
1934 }
1935
1936 wv = malloc_widget_value ();
1937 prev_wv->next = wv;
1938 wv->name = (char *) button_names[nb_buttons];
1939 if (!NILP (descrip))
1940 wv->key = (char *) XSTRING (descrip)->data;
1941 wv->value = (char *) XSTRING (item_name)->data;
1942 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1943 wv->enabled = !NILP (enable);
1944 prev_wv = wv;
1945
1946 if (! boundary_seen)
1947 left_count++;
1948
1949 nb_buttons++;
1950 i += MENU_ITEMS_ITEM_LENGTH;
1951 }
1952
1953 /* If the boundary was not specified,
1954 by default put half on the left and half on the right. */
1955 if (! boundary_seen)
1956 left_count = nb_buttons - nb_buttons / 2;
1957
1958 wv = malloc_widget_value ();
1959 wv->name = dialog_name;
1960
1961 /* Dialog boxes use a really stupid name encoding
1962 which specifies how many buttons to use
1963 and how many buttons are on the right.
1964 The Q means something also. */
1965 dialog_name[0] = 'Q';
1966 dialog_name[1] = '0' + nb_buttons;
1967 dialog_name[2] = 'B';
1968 dialog_name[3] = 'R';
1969 /* Number of buttons to put on the right. */
1970 dialog_name[4] = '0' + nb_buttons - left_count;
1971 dialog_name[5] = 0;
1972 wv->contents = first_wv;
1973 first_wv = wv;
1974 }
1975
1976 /* Actually create the dialog. */
1977 dialog_id = ++popup_id_tick;
1978 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1979 f->display.x->widget, 1, 0,
1980 dialog_selection_callback, 0);
1981 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1982 lw_modify_all_widgets (dialog_id, first_wv, True);
1983 #endif
1984 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1985 /* Free the widget_value objects we used to specify the contents. */
1986 free_menubar_widget_value_tree (first_wv);
1987
1988 /* No selection has been chosen yet. */
1989 menu_item_selection = 0;
1990
1991 /* Display the menu. */
1992 lw_pop_up_all_widgets (dialog_id);
1993
1994 /* Process events that apply to the menu. */
1995 while (1)
1996 {
1997 XEvent event;
1998
1999 XtAppNextEvent (Xt_app_con, &event);
2000 if (event.type == ButtonRelease)
2001 {
2002 XtDispatchEvent (&event);
2003 break;
2004 }
2005 else if (event.type == Expose)
2006 process_expose_from_menu (event);
2007 XtDispatchEvent (&event);
2008 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
2009 {
2010 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
2011
2012 if (queue_tmp != NULL)
2013 {
2014 queue_tmp->event = event;
2015 queue_tmp->next = queue;
2016 queue = queue_tmp;
2017 }
2018 }
2019 }
2020 pop_down:
2021
2022 /* State that no mouse buttons are now held.
2023 That is not necessarily true, but the fiction leads to reasonable
2024 results, and it is a pain to ask which are actually held now
2025 or track this in the loop above. */
2026 x_mouse_grabbed = 0;
2027
2028 /* Unread any events that we got but did not handle. */
2029 while (queue != NULL)
2030 {
2031 queue_tmp = queue;
2032 XPutBackEvent (XDISPLAY &queue_tmp->event);
2033 queue = queue_tmp->next;
2034 free ((char *)queue_tmp);
2035 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2036 interrupt_input_pending = 1;
2037 }
2038
2039 /* Find the selected item, and its pane, to return
2040 the proper value. */
2041 if (menu_item_selection != 0)
2042 {
2043 Lisp_Object prefix;
2044
2045 prefix = Qnil;
2046 i = 0;
2047 while (i < menu_items_used)
2048 {
2049 Lisp_Object entry;
2050
2051 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2052 {
2053 prefix
2054 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2055 i += MENU_ITEMS_PANE_LENGTH;
2056 }
2057 else
2058 {
2059 entry
2060 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2061 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2062 {
2063 if (keymaps != 0)
2064 {
2065 entry = Fcons (entry, Qnil);
2066 if (!NILP (prefix))
2067 entry = Fcons (prefix, entry);
2068 }
2069 return entry;
2070 }
2071 i += MENU_ITEMS_ITEM_LENGTH;
2072 }
2073 }
2074 }
2075
2076 return Qnil;
2077 }
2078 #else /* not USE_X_TOOLKIT */
2079
2080 static Lisp_Object
2081 xmenu_show (f, x, y, menubarp, keymaps, title, error)
2082 FRAME_PTR f;
2083 int x, y;
2084 int keymaps;
2085 int menubarp;
2086 Lisp_Object title;
2087 char **error;
2088 {
2089 Window root;
2090 XMenu *menu;
2091 int pane, selidx, lpane, status;
2092 Lisp_Object entry, pane_prefix;
2093 char *datap;
2094 int ulx, uly, width, height;
2095 int dispwidth, dispheight;
2096 int i, j;
2097 int maxwidth;
2098 int dummy_int;
2099 unsigned int dummy_uint;
2100
2101 *error = 0;
2102 if (menu_items_n_panes == 0)
2103 return Qnil;
2104
2105 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2106 {
2107 *error = "Empty menu";
2108 return Qnil;
2109 }
2110
2111 /* Figure out which root window F is on. */
2112 XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root,
2113 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2114 &dummy_uint, &dummy_uint);
2115
2116 /* Make the menu on that window. */
2117 menu = XMenuCreate (XDISPLAY root, "emacs");
2118 if (menu == NULL)
2119 {
2120 *error = "Can't create menu";
2121 return Qnil;
2122 }
2123
2124 /* Adjust coordinates to relative to the outer (window manager) window. */
2125 #ifdef HAVE_X11
2126 {
2127 Window child;
2128 int win_x = 0, win_y = 0;
2129
2130 /* Find the position of the outside upper-left corner of
2131 the inner window, with respect to the outer window. */
2132 if (f->display.x->parent_desc != ROOT_WINDOW)
2133 {
2134 BLOCK_INPUT;
2135 XTranslateCoordinates (x_current_display,
2136
2137 /* From-window, to-window. */
2138 f->display.x->window_desc,
2139 f->display.x->parent_desc,
2140
2141 /* From-position, to-position. */
2142 0, 0, &win_x, &win_y,
2143
2144 /* Child of window. */
2145 &child);
2146 UNBLOCK_INPUT;
2147 x += win_x;
2148 y += win_y;
2149 }
2150 }
2151 #endif /* HAVE_X11 */
2152
2153 /* Adjust coordinates to be root-window-relative. */
2154 x += f->display.x->left_pos;
2155 y += f->display.x->top_pos;
2156
2157 /* Create all the necessary panes and their items. */
2158 i = 0;
2159 while (i < menu_items_used)
2160 {
2161 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2162 {
2163 /* Create a new pane. */
2164 Lisp_Object pane_name, prefix;
2165 char *pane_string;
2166
2167 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2168 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2169 pane_string = (NILP (pane_name)
2170 ? "" : (char *) XSTRING (pane_name)->data);
2171 if (keymaps && !NILP (prefix))
2172 pane_string++;
2173
2174 lpane = XMenuAddPane (XDISPLAY menu, pane_string, TRUE);
2175 if (lpane == XM_FAILURE)
2176 {
2177 XMenuDestroy (XDISPLAY menu);
2178 *error = "Can't create pane";
2179 return Qnil;
2180 }
2181 i += MENU_ITEMS_PANE_LENGTH;
2182
2183 /* Find the width of the widest item in this pane. */
2184 maxwidth = 0;
2185 j = i;
2186 while (j < menu_items_used)
2187 {
2188 Lisp_Object item;
2189 item = XVECTOR (menu_items)->contents[j];
2190 if (EQ (item, Qt))
2191 break;
2192 if (NILP (item))
2193 {
2194 j++;
2195 continue;
2196 }
2197 width = XSTRING (item)->size;
2198 if (width > maxwidth)
2199 maxwidth = width;
2200
2201 j += MENU_ITEMS_ITEM_LENGTH;
2202 }
2203 }
2204 /* Ignore a nil in the item list.
2205 It's meaningful only for dialog boxes. */
2206 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2207 i += 1;
2208 else
2209 {
2210 /* Create a new item within current pane. */
2211 Lisp_Object item_name, enable, descrip;
2212 unsigned char *item_data;
2213
2214 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2215 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2216 descrip
2217 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2218 if (!NILP (descrip))
2219 {
2220 int gap = maxwidth - XSTRING (item_name)->size;
2221 #ifdef C_ALLOCA
2222 Lisp_Object spacer;
2223 spacer = Fmake_string (make_number (gap), make_number (' '));
2224 item_name = concat2 (item_name, spacer);
2225 item_name = concat2 (item_name, descrip);
2226 item_data = XSTRING (item_name)->data;
2227 #else
2228 /* if alloca is fast, use that to make the space,
2229 to reduce gc needs. */
2230 item_data
2231 = (unsigned char *) alloca (maxwidth
2232 + XSTRING (descrip)->size + 1);
2233 bcopy (XSTRING (item_name)->data, item_data,
2234 XSTRING (item_name)->size);
2235 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2236 item_data[j] = ' ';
2237 bcopy (XSTRING (descrip)->data, item_data + j,
2238 XSTRING (descrip)->size);
2239 item_data[j + XSTRING (descrip)->size] = 0;
2240 #endif
2241 }
2242 else
2243 item_data = XSTRING (item_name)->data;
2244
2245 if (XMenuAddSelection (XDISPLAY menu, lpane, 0, item_data,
2246 !NILP (enable))
2247 == XM_FAILURE)
2248 {
2249 XMenuDestroy (XDISPLAY menu);
2250 *error = "Can't add selection to menu";
2251 return Qnil;
2252 }
2253 i += MENU_ITEMS_ITEM_LENGTH;
2254 }
2255 }
2256
2257 /* All set and ready to fly. */
2258 XMenuRecompute (XDISPLAY menu);
2259 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
2260 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
2261 x = min (x, dispwidth);
2262 y = min (y, dispheight);
2263 x = max (x, 1);
2264 y = max (y, 1);
2265 XMenuLocate (XDISPLAY menu, 0, 0, x, y,
2266 &ulx, &uly, &width, &height);
2267 if (ulx+width > dispwidth)
2268 {
2269 x -= (ulx + width) - dispwidth;
2270 ulx = dispwidth - width;
2271 }
2272 if (uly+height > dispheight)
2273 {
2274 y -= (uly + height) - dispheight;
2275 uly = dispheight - height;
2276 }
2277 if (ulx < 0) x -= ulx;
2278 if (uly < 0) y -= uly;
2279
2280 XMenuSetAEQ (menu, TRUE);
2281 XMenuSetFreeze (menu, TRUE);
2282 pane = selidx = 0;
2283
2284 status = XMenuActivate (XDISPLAY menu, &pane, &selidx,
2285 x, y, ButtonReleaseMask, &datap);
2286 switch (status)
2287 {
2288 case XM_SUCCESS:
2289 #ifdef XDEBUG
2290 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2291 #endif
2292
2293 /* Find the item number SELIDX in pane number PANE. */
2294 i = 0;
2295 while (i < menu_items_used)
2296 {
2297 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2298 {
2299 if (pane == 0)
2300 pane_prefix
2301 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2302 pane--;
2303 i += MENU_ITEMS_PANE_LENGTH;
2304 }
2305 else
2306 {
2307 if (pane == -1)
2308 {
2309 if (selidx == 0)
2310 {
2311 entry
2312 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2313 if (keymaps != 0)
2314 {
2315 entry = Fcons (entry, Qnil);
2316 if (!NILP (pane_prefix))
2317 entry = Fcons (pane_prefix, entry);
2318 }
2319 break;
2320 }
2321 selidx--;
2322 }
2323 i += MENU_ITEMS_ITEM_LENGTH;
2324 }
2325 }
2326 break;
2327
2328 case XM_FAILURE:
2329 *error = "Can't activate menu";
2330 case XM_IA_SELECT:
2331 case XM_NO_SELECT:
2332 entry = Qnil;
2333 break;
2334 }
2335 XMenuDestroy (XDISPLAY menu);
2336
2337 /* State that no mouse buttons are now held.
2338 (The oldXMenu code doesn't track this info for us.)
2339 That is not necessarily true, but the fiction leads to reasonable
2340 results, and it is a pain to ask which are actually held now. */
2341 x_mouse_grabbed = 0;
2342
2343 return entry;
2344 }
2345 #endif /* not USE_X_TOOLKIT */
2346 \f
2347 syms_of_xmenu ()
2348 {
2349 staticpro (&menu_items);
2350 menu_items = Qnil;
2351
2352 popup_id_tick = (1<<16);
2353 defsubr (&Sx_popup_menu);
2354 defsubr (&Sx_popup_dialog);
2355 }