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