(single_keymap_panes): Put menu_item_equiv_key in the gcpro.
[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 static Lisp_Object *menu_item_selection;
1068
1069 static void
1070 popup_selection_callback (widget, id, client_data)
1071 Widget widget;
1072 LWLIB_ID id;
1073 XtPointer client_data;
1074 {
1075 menu_item_selection = (Lisp_Object *) client_data;
1076 }
1077
1078 static void
1079 popup_down_callback (widget, id, client_data)
1080 Widget widget;
1081 LWLIB_ID id;
1082 XtPointer client_data;
1083 {
1084 BLOCK_INPUT;
1085 lw_destroy_all_widgets (id);
1086 UNBLOCK_INPUT;
1087 }
1088
1089 static void
1090 dialog_selection_callback (widget, id, client_data)
1091 Widget widget;
1092 LWLIB_ID id;
1093 XtPointer client_data;
1094 {
1095 if ((int)client_data != -1)
1096 menu_item_selection = (Lisp_Object *) client_data;
1097 BLOCK_INPUT;
1098 lw_destroy_all_widgets (id);
1099 UNBLOCK_INPUT;
1100 }
1101
1102 /* This recursively calls free_widget_value() on the tree of widgets.
1103 It must free all data that was malloc'ed for these widget_values.
1104 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1105 must be left alone. */
1106
1107 void
1108 free_menubar_widget_value_tree (wv)
1109 widget_value *wv;
1110 {
1111 if (! wv) return;
1112
1113 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1114
1115 if (wv->contents && (wv->contents != (widget_value*)1))
1116 {
1117 free_menubar_widget_value_tree (wv->contents);
1118 wv->contents = (widget_value *) 0xDEADBEEF;
1119 }
1120 if (wv->next)
1121 {
1122 free_menubar_widget_value_tree (wv->next);
1123 wv->next = (widget_value *) 0xDEADBEEF;
1124 }
1125 BLOCK_INPUT;
1126 free_widget_value (wv);
1127 UNBLOCK_INPUT;
1128 }
1129
1130 extern void EmacsFrameSetCharSize ();
1131
1132 static void
1133 update_frame_menubar (f)
1134 FRAME_PTR f;
1135 {
1136 struct x_display *x = f->display.x;
1137 int columns, rows;
1138 int menubar_changed;
1139
1140 menubar_changed = (x->menubar_widget
1141 && !XtIsManaged (x->menubar_widget));
1142
1143 if (! (menubar_changed))
1144 return;
1145
1146 BLOCK_INPUT;
1147 /* Save the size of the frame because the pane widget doesn't accept to
1148 resize itself. So force it. */
1149 columns = f->width;
1150 rows = f->height;
1151
1152
1153 XawPanedSetRefigureMode (x->column_widget, 0);
1154
1155 /* the order in which children are managed is the top to
1156 bottom order in which they are displayed in the paned window.
1157 First, remove the text-area widget.
1158 */
1159 XtUnmanageChild (x->edit_widget);
1160
1161 /* remove the menubar that is there now, and put up the menubar that
1162 should be there.
1163 */
1164 if (menubar_changed)
1165 {
1166 XtManageChild (x->menubar_widget);
1167 XtMapWidget (x->menubar_widget);
1168 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1169 }
1170
1171
1172 /* Re-manage the text-area widget */
1173 XtManageChild (x->edit_widget);
1174
1175 /* and now thrash the sizes */
1176 XawPanedSetRefigureMode (x->column_widget, 1);
1177
1178 /* Force the pane widget to resize itself with the right values. */
1179 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1180
1181 UNBLOCK_INPUT;
1182 }
1183
1184 void
1185 set_frame_menubar (f, first_time)
1186 FRAME_PTR f;
1187 int first_time;
1188 {
1189 Widget menubar_widget = f->display.x->menubar_widget;
1190 int id = (int) f;
1191 Lisp_Object tail, items;
1192 widget_value *wv, *save_wv, *first_wv, *prev_wv = 0;
1193 int i;
1194
1195 BLOCK_INPUT;
1196
1197 wv = malloc_widget_value ();
1198 wv->name = "menubar";
1199 wv->value = 0;
1200 wv->enabled = 1;
1201 save_wv = first_wv = wv;
1202
1203 if (NILP (items = FRAME_MENU_BAR_ITEMS (f)))
1204 items = FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1205
1206 for (i = 0; i < XVECTOR (items)->size; i += 3)
1207 {
1208 Lisp_Object string;
1209
1210 string = XVECTOR (items)->contents[i + 1];
1211 if (NILP (string))
1212 break;
1213
1214 wv = malloc_widget_value ();
1215 if (prev_wv)
1216 prev_wv->next = wv;
1217 else
1218 save_wv->contents = wv;
1219 wv->name = (char *) XSTRING (string)->data;
1220 wv->value = 0;
1221 wv->enabled = 1;
1222 prev_wv = wv;
1223 }
1224
1225 if (menubar_widget)
1226 lw_modify_all_widgets (id, first_wv, False);
1227 else
1228 {
1229 menubar_widget = lw_create_widget ("menubar", "menubar",
1230 id, first_wv,
1231 f->display.x->column_widget,
1232 0, 0,
1233 0, 0);
1234 f->display.x->menubar_widget = menubar_widget;
1235 XtVaSetValues (menubar_widget,
1236 XtNshowGrip, 0,
1237 XtNresizeToPreferred, 1,
1238 XtNallowResize, 1,
1239 0);
1240 }
1241
1242 free_menubar_widget_value_tree (first_wv);
1243
1244 /* Don't update the menubar the first time it is created via x_window. */
1245 if (!first_time)
1246 update_frame_menubar (f);
1247
1248 UNBLOCK_INPUT;
1249 }
1250
1251 void
1252 free_frame_menubar (f)
1253 FRAME_PTR f;
1254 {
1255 Widget menubar_widget;
1256 int id;
1257
1258 menubar_widget = f->display.x->menubar_widget;
1259 id = (int) f;
1260
1261 if (menubar_widget)
1262 {
1263 BLOCK_INPUT;
1264 lw_destroy_all_widgets (id);
1265 UNBLOCK_INPUT;
1266 }
1267 }
1268 /* Called from Fx_create_frame to create the inital menubar of a frame
1269 before it is mapped, so that the window is mapped with the menubar already
1270 there instead of us tacking it on later and thrashing the window after it
1271 is visible. */
1272 void
1273 initialize_frame_menubar (f)
1274 FRAME_PTR f;
1275 {
1276 set_frame_menubar (f, 1);
1277 }
1278 \f
1279 /* Horizontal bounds of the current menu bar item. */
1280
1281 static int this_menu_bar_item_beg;
1282 static int this_menu_bar_item_end;
1283
1284 /* Horizontal position of the end of the last menu bar item. */
1285
1286 static int last_menu_bar_item_end;
1287
1288 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1289 but not in the current menu bar item. */
1290
1291 static int
1292 other_menu_bar_item_p (f, x, y)
1293 FRAME_PTR f;
1294 int x, y;
1295 {
1296 return (y >= 0
1297 && f->display.x->menubar_widget != 0
1298 && y < f->display.x->menubar_widget->core.height
1299 && x >= 0
1300 && x < last_menu_bar_item_end
1301 && (x >= this_menu_bar_item_end
1302 || x < this_menu_bar_item_beg));
1303 }
1304
1305 /* Unread a button-press event in the menu bar of frame F
1306 at x position XPOS relative to the inside of the frame. */
1307
1308 static void
1309 unread_menu_bar_button (f, xpos)
1310 FRAME_PTR f;
1311 int xpos;
1312 {
1313 XEvent event;
1314
1315 event.type = ButtonPress;
1316 event.xbutton.display = x_current_display;
1317 event.xbutton.serial = 0;
1318 event.xbutton.send_event = 0;
1319 event.xbutton.time = CurrentTime;
1320 event.xbutton.button = Button1;
1321 event.xbutton.window = XtWindow (f->display.x->menubar_widget);
1322 event.xbutton.x = xpos;
1323 XPutBackEvent (XDISPLAY &event);
1324 }
1325
1326 /* If the mouse has moved to another menu bar item,
1327 return 1 and unread a button press event for that item.
1328 Otherwise return 0. */
1329
1330 static int
1331 check_mouse_other_menu_bar (f)
1332 FRAME_PTR f;
1333 {
1334 FRAME_PTR new_f;
1335 Lisp_Object bar_window;
1336 int part;
1337 Lisp_Object x, y;
1338 unsigned long time;
1339
1340 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
1341
1342 if (f == new_f && other_menu_bar_item_p (f, x, y))
1343 {
1344 unread_menu_bar_button (f, x);
1345 return 1;
1346 }
1347
1348 return 0;
1349 }
1350 #endif /* USE_X_TOOLKIT */
1351 \f
1352 /* xmenu_show actually displays a menu using the panes and items in menu_items
1353 and returns the value selected from it.
1354 There are two versions of xmenu_show, one for Xt and one for Xlib.
1355 Both assume input is blocked by the caller. */
1356
1357 /* F is the frame the menu is for.
1358 X and Y are the frame-relative specified position,
1359 relative to the inside upper left corner of the frame F.
1360 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1361 KEYMAPS is 1 if this menu was specified with keymaps;
1362 in that case, we return a list containing the chosen item's value
1363 and perhaps also the pane's prefix.
1364 TITLE is the specified menu title.
1365 ERROR is a place to store an error message string in case of failure.
1366 (We return nil on failure, but the value doesn't actually matter.) */
1367
1368 #ifdef USE_X_TOOLKIT
1369
1370 extern unsigned int x_mouse_grabbed;
1371 extern Lisp_Object Vmouse_depressed;
1372
1373 static Lisp_Object
1374 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1375 FRAME_PTR f;
1376 int x;
1377 int y;
1378 int menubarp;
1379 int keymaps;
1380 Lisp_Object title;
1381 char **error;
1382 {
1383 int i;
1384 int menu_id;
1385 Widget menu;
1386 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1387
1388 /* This is the menu bar item (if any) that led to this menu. */
1389 widget_value *menubar_item = 0;
1390
1391 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1392 widget_value **submenu_stack
1393 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1394 Lisp_Object *subprefix_stack
1395 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1396 int submenu_depth = 0;
1397
1398 /* Define a queue to save up for later unreading
1399 all X events that don't pertain to the menu. */
1400 struct event_queue
1401 {
1402 XEvent event;
1403 struct event_queue *next;
1404 };
1405
1406 struct event_queue *queue = NULL;
1407 struct event_queue *queue_tmp;
1408
1409 Position root_x, root_y;
1410
1411 int first_pane;
1412
1413 *error = NULL;
1414
1415 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1416 {
1417 *error = "Empty menu";
1418 return Qnil;
1419 }
1420 this_menu_bar_item_beg = -1;
1421 this_menu_bar_item_end = -1;
1422 last_menu_bar_item_end = -1;
1423
1424 /* Figure out which menu bar item, if any, this menu is for. */
1425 if (menubarp)
1426 {
1427 int xbeg;
1428 int xend = 0;
1429 widget_value *mb_item = 0;
1430
1431 for (mb_item = menubar->menu.old_stack[0]->contents;
1432 mb_item;
1433 mb_item = mb_item->next)
1434 {
1435 xbeg = xend;
1436 xend += (string_width (menubar, mb_item->name)
1437 + 2 * (menubar->menu.horizontal_spacing
1438 + menubar->menu.shadow_thickness));
1439 if (x >= xbeg && x < xend)
1440 {
1441 x = xbeg + 4;
1442 y = 0;
1443 menubar_item = mb_item;
1444 /* Arrange to show a different menu if we move in the menu bar
1445 to a different item. */
1446 this_menu_bar_item_beg = xbeg;
1447 this_menu_bar_item_end = xend;
1448 }
1449 }
1450 last_menu_bar_item_end = xend;
1451 }
1452 if (menubar_item == 0)
1453 menubarp = 0;
1454
1455 /* Offset the coordinates to root-relative. */
1456 if (f->display.x->menubar_widget != 0)
1457 y += f->display.x->menubar_widget->core.height;
1458 XtTranslateCoords (f->display.x->widget,
1459 x, y, &root_x, &root_y);
1460 x = root_x;
1461 y = root_y;
1462
1463 /* Create a tree of widget_value objects
1464 representing the panes and their items. */
1465 wv = malloc_widget_value ();
1466 wv->name = "menu";
1467 wv->value = 0;
1468 wv->enabled = 1;
1469 first_wv = wv;
1470 first_pane = 1;
1471
1472 /* Loop over all panes and items, filling in the tree. */
1473 i = 0;
1474 while (i < menu_items_used)
1475 {
1476 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1477 {
1478 submenu_stack[submenu_depth++] = save_wv;
1479 save_wv = prev_wv;
1480 prev_wv = 0;
1481 first_pane = 1;
1482 i++;
1483 }
1484 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1485 {
1486 prev_wv = save_wv;
1487 save_wv = submenu_stack[--submenu_depth];
1488 first_pane = 0;
1489 i++;
1490 }
1491 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1492 && submenu_depth != 0)
1493 i += MENU_ITEMS_PANE_LENGTH;
1494 /* Ignore a nil in the item list.
1495 It's meaningful only for dialog boxes. */
1496 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1497 i += 1;
1498 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1499 {
1500 /* Create a new pane. */
1501 Lisp_Object pane_name, prefix;
1502 char *pane_string;
1503 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1504 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1505 pane_string = (NILP (pane_name)
1506 ? "" : (char *) XSTRING (pane_name)->data);
1507 /* If there is just one top-level pane, put all its items directly
1508 under the top-level menu. */
1509 if (menu_items_n_panes == 1)
1510 pane_string = "";
1511
1512 /* If the pane has a meaningful name,
1513 make the pane a top-level menu item
1514 with its items as a submenu beneath it. */
1515 if (!keymaps && strcmp (pane_string, ""))
1516 {
1517 wv = malloc_widget_value ();
1518 if (save_wv)
1519 save_wv->next = wv;
1520 else
1521 first_wv->contents = wv;
1522 wv->name = pane_string;
1523 if (keymaps && !NILP (prefix))
1524 wv->name++;
1525 wv->value = 0;
1526 wv->enabled = 1;
1527 save_wv = wv;
1528 prev_wv = 0;
1529 }
1530 else if (first_pane)
1531 {
1532 save_wv = wv;
1533 prev_wv = 0;
1534 }
1535 first_pane = 0;
1536 i += MENU_ITEMS_PANE_LENGTH;
1537 }
1538 else
1539 {
1540 /* Create a new item within current pane. */
1541 Lisp_Object item_name, enable, descrip;
1542 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1543 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1544 descrip
1545 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1546
1547 wv = malloc_widget_value ();
1548 if (prev_wv)
1549 prev_wv->next = wv;
1550 else
1551 save_wv->contents = wv;
1552 wv->name = (char *) XSTRING (item_name)->data;
1553 if (!NILP (descrip))
1554 wv->key = (char *) XSTRING (descrip)->data;
1555 wv->value = 0;
1556 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1557 wv->enabled = !NILP (enable);
1558 prev_wv = wv;
1559
1560 i += MENU_ITEMS_ITEM_LENGTH;
1561 }
1562 }
1563
1564 /* Actually create the menu. */
1565 menu_id = ++popup_id_tick;
1566 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1567 f->display.x->widget, 1, 0,
1568 popup_selection_callback, popup_down_callback);
1569 /* Free the widget_value objects we used to specify the contents. */
1570 free_menubar_widget_value_tree (first_wv);
1571
1572 /* No selection has been chosen yet. */
1573 menu_item_selection = 0;
1574
1575 /* If the mouse moves out of the menu before we show the menu,
1576 don't show it at all. */
1577 if (check_mouse_other_menu_bar (f))
1578 {
1579 lw_destroy_all_widgets (menu_id);
1580 return Qnil;
1581 }
1582
1583
1584 /* Highlight the menu bar item (if any) that led to this menu. */
1585 if (menubarp)
1586 {
1587 menubar_item->call_data = (XtPointer) 1;
1588 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1589 }
1590
1591 /* Display the menu. */
1592 {
1593 XButtonPressedEvent dummy;
1594 XlwMenuWidget mw;
1595
1596 mw = (XlwMenuWidget) ((CompositeWidget)menu)->composite.children[0];
1597
1598 dummy.type = ButtonPress;
1599 dummy.serial = 0;
1600 dummy.send_event = 0;
1601 dummy.display = XtDisplay (menu);
1602 dummy.window = XtWindow (XtParent (menu));
1603 dummy.time = CurrentTime;
1604 dummy.button = 0;
1605 dummy.x_root = x;
1606 dummy.y_root = y;
1607
1608 /* We activate directly the lucid implementation. */
1609 pop_up_menu (mw, &dummy);
1610 }
1611
1612 /* No need to check a second time since this is done in the XEvent loop.
1613 This slows done the execution. */
1614 #if 0
1615 /* Check again whether the mouse has moved to another menu bar item. */
1616 if (check_mouse_other_menu_bar (f))
1617 {
1618 /* The mouse moved into a different menu bar item.
1619 We should bring up that item's menu instead.
1620 First pop down this menu. */
1621 XtUngrabPointer ((Widget)
1622 ((XlwMenuWidget)
1623 ((CompositeWidget)menu)->composite.children[0]),
1624 CurrentTime);
1625 lw_destroy_all_widgets (menu_id);
1626 goto pop_down;
1627 }
1628 #endif
1629
1630 /* Process events that apply to the menu. */
1631 while (1)
1632 {
1633 XEvent event;
1634
1635 XtAppNextEvent (Xt_app_con, &event);
1636 if (event.type == ButtonRelease)
1637 {
1638 XtDispatchEvent (&event);
1639 if (! menubarp)
1640 {
1641 /* Do the work of construct_mouse_click since it can't
1642 be called. Initially, the popup menu has been called
1643 from a ButtonPress in the edit_widget. Then the mouse
1644 has been set to grabbed. Reset it now. */
1645 x_mouse_grabbed &= ~(1 << event.xbutton.button);
1646 if (!x_mouse_grabbed)
1647 Vmouse_depressed = Qnil;
1648 }
1649 break;
1650 }
1651 else if (event.type == Expose)
1652 process_expose_from_menu (event);
1653 else if (event.type == MotionNotify)
1654 {
1655 int event_x = (event.xmotion.x_root
1656 - (f->display.x->widget->core.x
1657 + f->display.x->widget->core.border_width));
1658 int event_y = (event.xmotion.y_root
1659 - (f->display.x->widget->core.y
1660 + f->display.x->widget->core.border_width));
1661
1662 if (other_menu_bar_item_p (f, event_x, event_y))
1663 {
1664 /* The mouse moved into a different menu bar item.
1665 We should bring up that item's menu instead.
1666 First pop down this menu. */
1667 XtUngrabPointer ((Widget)
1668 ((XlwMenuWidget)
1669 ((CompositeWidget)menu)->composite.children[0]),
1670 event.xbutton.time);
1671 lw_destroy_all_widgets (menu_id);
1672
1673 /* Put back an event that will bring up the other item's menu. */
1674 unread_menu_bar_button (f, event_x);
1675 /* Don't let us select anything in this case. */
1676 menu_item_selection = 0;
1677 break;
1678 }
1679 }
1680
1681 XtDispatchEvent (&event);
1682 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1683 {
1684 queue_tmp
1685 = (struct event_queue *) malloc (sizeof (struct event_queue));
1686
1687 if (queue_tmp != NULL)
1688 {
1689 queue_tmp->event = event;
1690 queue_tmp->next = queue;
1691 queue = queue_tmp;
1692 }
1693 }
1694 }
1695
1696 pop_down:
1697 /* Unhighlight the menu bar item (if any) that led to this menu. */
1698 if (menubarp)
1699 {
1700 menubar_item->call_data = (XtPointer) 0;
1701 dispatch_dummy_expose (f->display.x->menubar_widget, x, y);
1702 }
1703
1704 /* fp turned off the following statement and wrote a comment
1705 that it is unnecessary--that the menu has already disappeared.
1706 I observer that is not so. -- rms. */
1707 /* Make sure the menu disappears. */
1708 lw_destroy_all_widgets (menu_id);
1709
1710 /* Unread any events that we got but did not handle. */
1711 while (queue != NULL)
1712 {
1713 queue_tmp = queue;
1714 XPutBackEvent (XDISPLAY &queue_tmp->event);
1715 queue = queue_tmp->next;
1716 free ((char *)queue_tmp);
1717 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1718 interrupt_input_pending = 1;
1719 }
1720
1721 /* Find the selected item, and its pane, to return
1722 the proper value. */
1723 if (menu_item_selection != 0)
1724 {
1725 Lisp_Object prefix;
1726
1727 prefix = Qnil;
1728 i = 0;
1729 while (i < menu_items_used)
1730 {
1731 Lisp_Object entry;
1732
1733 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1734 {
1735 subprefix_stack[submenu_depth++] = prefix;
1736 prefix = entry;
1737 i++;
1738 }
1739 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1740 {
1741 prefix = subprefix_stack[--submenu_depth];
1742 i++;
1743 }
1744 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1745 {
1746 prefix
1747 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1748 i += MENU_ITEMS_PANE_LENGTH;
1749 }
1750 else
1751 {
1752 entry
1753 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1754 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1755 {
1756 if (keymaps != 0)
1757 {
1758 int j;
1759
1760 entry = Fcons (entry, Qnil);
1761 if (!NILP (prefix))
1762 entry = Fcons (prefix, entry);
1763 for (j = submenu_depth - 1; j >= 0; j--)
1764 if (!NILP (subprefix_stack[j]))
1765 entry = Fcons (subprefix_stack[j], entry);
1766 }
1767 return entry;
1768 }
1769 i += MENU_ITEMS_ITEM_LENGTH;
1770 }
1771 }
1772 }
1773
1774 return Qnil;
1775 }
1776
1777 static char * button_names [] = {
1778 "button1", "button2", "button3", "button4", "button5",
1779 "button6", "button7", "button8", "button9", "button10" };
1780
1781 static Lisp_Object
1782 xdialog_show (f, menubarp, keymaps, title, error)
1783 FRAME_PTR f;
1784 int menubarp;
1785 int keymaps;
1786 Lisp_Object title;
1787 char **error;
1788 {
1789 int i, nb_buttons=0;
1790 int dialog_id;
1791 Widget menu;
1792 XlwMenuWidget menubar = (XlwMenuWidget) f->display.x->menubar_widget;
1793 char dialog_name[6];
1794
1795 /* This is the menu bar item (if any) that led to this menu. */
1796 widget_value *menubar_item = 0;
1797
1798 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1799
1800 /* Define a queue to save up for later unreading
1801 all X events that don't pertain to the menu. */
1802 struct event_queue
1803 {
1804 XEvent event;
1805 struct event_queue *next;
1806 };
1807
1808 struct event_queue *queue = NULL;
1809 struct event_queue *queue_tmp;
1810
1811 /* Number of elements seen so far, before boundary. */
1812 int left_count = 0;
1813 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1814 int boundary_seen = 0;
1815
1816 *error = NULL;
1817
1818 if (menu_items_n_panes > 1)
1819 {
1820 *error = "Multiple panes in dialog box";
1821 return Qnil;
1822 }
1823
1824 /* Create a tree of widget_value objects
1825 representing the text label and buttons. */
1826 {
1827 Lisp_Object pane_name, prefix;
1828 char *pane_string;
1829 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1830 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1831 pane_string = (NILP (pane_name)
1832 ? "" : (char *) XSTRING (pane_name)->data);
1833 prev_wv = malloc_widget_value ();
1834 prev_wv->value = pane_string;
1835 if (keymaps && !NILP (prefix))
1836 prev_wv->name++;
1837 prev_wv->enabled = 1;
1838 prev_wv->name = "message";
1839 first_wv = prev_wv;
1840
1841 /* Loop over all panes and items, filling in the tree. */
1842 i = MENU_ITEMS_PANE_LENGTH;
1843 while (i < menu_items_used)
1844 {
1845
1846 /* Create a new item within current pane. */
1847 Lisp_Object item_name, enable, descrip;
1848 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1849 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1850 descrip
1851 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1852
1853 if (NILP (item_name))
1854 {
1855 free_menubar_widget_value_tree (first_wv);
1856 *error = "Submenu in dialog items";
1857 return Qnil;
1858 }
1859 if (EQ (item_name, Qquote))
1860 {
1861 /* This is the boundary between left-side elts
1862 and right-side elts. Stop incrementing right_count. */
1863 boundary_seen = 1;
1864 i++;
1865 continue;
1866 }
1867 if (nb_buttons >= 10)
1868 {
1869 free_menubar_widget_value_tree (first_wv);
1870 *error = "Too many dialog items";
1871 return Qnil;
1872 }
1873
1874 wv = malloc_widget_value ();
1875 prev_wv->next = wv;
1876 wv->name = (char *) button_names[nb_buttons];
1877 if (!NILP (descrip))
1878 wv->key = (char *) XSTRING (descrip)->data;
1879 wv->value = (char *) XSTRING (item_name)->data;
1880 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1881 wv->enabled = !NILP (enable);
1882 prev_wv = wv;
1883
1884 if (! boundary_seen)
1885 left_count++;
1886
1887 nb_buttons++;
1888 i += MENU_ITEMS_ITEM_LENGTH;
1889 }
1890
1891 /* If the boundary was not specified,
1892 by default put half on the left and half on the right. */
1893 if (! boundary_seen)
1894 left_count = nb_buttons - nb_buttons / 2;
1895
1896 wv = malloc_widget_value ();
1897 wv->name = dialog_name;
1898
1899 /* Dialog boxes use a really stupid name encoding
1900 which specifies how many buttons to use
1901 and how many buttons are on the right.
1902 The Q means something also. */
1903 dialog_name[0] = 'Q';
1904 dialog_name[1] = '0' + nb_buttons;
1905 dialog_name[2] = 'B';
1906 dialog_name[3] = 'R';
1907 /* Number of buttons to put on the right. */
1908 dialog_name[4] = '0' + nb_buttons - left_count;
1909 dialog_name[5] = 0;
1910 wv->contents = first_wv;
1911 first_wv = wv;
1912 }
1913
1914 /* Actually create the dialog. */
1915 dialog_id = ++popup_id_tick;
1916 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1917 f->display.x->widget, 1, 0,
1918 dialog_selection_callback, 0);
1919 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1920 lw_modify_all_widgets (dialog_id, first_wv, True);
1921 #endif
1922 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1923 /* Free the widget_value objects we used to specify the contents. */
1924 free_menubar_widget_value_tree (first_wv);
1925
1926 /* No selection has been chosen yet. */
1927 menu_item_selection = 0;
1928
1929 /* Display the menu. */
1930 lw_pop_up_all_widgets (dialog_id);
1931
1932 /* Process events that apply to the menu. */
1933 while (1)
1934 {
1935 XEvent event;
1936
1937 XtAppNextEvent (Xt_app_con, &event);
1938 if (event.type == ButtonRelease)
1939 {
1940 XtDispatchEvent (&event);
1941 break;
1942 }
1943 else if (event.type == Expose)
1944 process_expose_from_menu (event);
1945 XtDispatchEvent (&event);
1946 if (XtWindowToWidget(XDISPLAY event.xany.window) != menu)
1947 {
1948 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1949
1950 if (queue_tmp != NULL)
1951 {
1952 queue_tmp->event = event;
1953 queue_tmp->next = queue;
1954 queue = queue_tmp;
1955 }
1956 }
1957 }
1958 pop_down:
1959
1960 /* State that no mouse buttons are now held.
1961 That is not necessarily true, but the fiction leads to reasonable
1962 results, and it is a pain to ask which are actually held now
1963 or track this in the loop above. */
1964 x_mouse_grabbed = 0;
1965
1966 /* Unread any events that we got but did not handle. */
1967 while (queue != NULL)
1968 {
1969 queue_tmp = queue;
1970 XPutBackEvent (XDISPLAY &queue_tmp->event);
1971 queue = queue_tmp->next;
1972 free ((char *)queue_tmp);
1973 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1974 interrupt_input_pending = 1;
1975 }
1976
1977 /* Find the selected item, and its pane, to return
1978 the proper value. */
1979 if (menu_item_selection != 0)
1980 {
1981 Lisp_Object prefix;
1982
1983 prefix = Qnil;
1984 i = 0;
1985 while (i < menu_items_used)
1986 {
1987 Lisp_Object entry;
1988
1989 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1990 {
1991 prefix
1992 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1993 i += MENU_ITEMS_PANE_LENGTH;
1994 }
1995 else
1996 {
1997 entry
1998 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1999 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2000 {
2001 if (keymaps != 0)
2002 {
2003 entry = Fcons (entry, Qnil);
2004 if (!NILP (prefix))
2005 entry = Fcons (prefix, entry);
2006 }
2007 return entry;
2008 }
2009 i += MENU_ITEMS_ITEM_LENGTH;
2010 }
2011 }
2012 }
2013
2014 return Qnil;
2015 }
2016 #else /* not USE_X_TOOLKIT */
2017
2018 static Lisp_Object
2019 xmenu_show (f, x, y, menubarp, keymaps, title, error)
2020 FRAME_PTR f;
2021 int x, y;
2022 int keymaps;
2023 int menubarp;
2024 Lisp_Object title;
2025 char **error;
2026 {
2027 Window root;
2028 XMenu *menu;
2029 int pane, selidx, lpane, status;
2030 Lisp_Object entry, pane_prefix;
2031 char *datap;
2032 int ulx, uly, width, height;
2033 int dispwidth, dispheight;
2034 int i, j;
2035 int maxwidth;
2036 int dummy_int;
2037 unsigned int dummy_uint;
2038
2039 *error = 0;
2040 if (menu_items_n_panes == 0)
2041 return Qnil;
2042
2043 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2044 {
2045 *error = "Empty menu";
2046 return Qnil;
2047 }
2048
2049 /* Figure out which root window F is on. */
2050 XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root,
2051 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2052 &dummy_uint, &dummy_uint);
2053
2054 /* Make the menu on that window. */
2055 menu = XMenuCreate (XDISPLAY root, "emacs");
2056 if (menu == NULL)
2057 {
2058 *error = "Can't create menu";
2059 return Qnil;
2060 }
2061
2062 /* Adjust coordinates to relative to the outer (window manager) window. */
2063 #ifdef HAVE_X11
2064 {
2065 Window child;
2066 int win_x = 0, win_y = 0;
2067
2068 /* Find the position of the outside upper-left corner of
2069 the inner window, with respect to the outer window. */
2070 if (f->display.x->parent_desc != ROOT_WINDOW)
2071 {
2072 BLOCK_INPUT;
2073 XTranslateCoordinates (x_current_display,
2074
2075 /* From-window, to-window. */
2076 f->display.x->window_desc,
2077 f->display.x->parent_desc,
2078
2079 /* From-position, to-position. */
2080 0, 0, &win_x, &win_y,
2081
2082 /* Child of window. */
2083 &child);
2084 UNBLOCK_INPUT;
2085 x += win_x;
2086 y += win_y;
2087 }
2088 }
2089 #endif /* HAVE_X11 */
2090
2091 /* Adjust coordinates to be root-window-relative. */
2092 x += f->display.x->left_pos;
2093 y += f->display.x->top_pos;
2094
2095 /* Create all the necessary panes and their items. */
2096 i = 0;
2097 while (i < menu_items_used)
2098 {
2099 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2100 {
2101 /* Create a new pane. */
2102 Lisp_Object pane_name, prefix;
2103 char *pane_string;
2104
2105 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2106 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2107 pane_string = (NILP (pane_name)
2108 ? "" : (char *) XSTRING (pane_name)->data);
2109 if (keymaps && !NILP (prefix))
2110 pane_string++;
2111
2112 lpane = XMenuAddPane (XDISPLAY menu, pane_string, TRUE);
2113 if (lpane == XM_FAILURE)
2114 {
2115 XMenuDestroy (XDISPLAY menu);
2116 *error = "Can't create pane";
2117 return Qnil;
2118 }
2119 i += MENU_ITEMS_PANE_LENGTH;
2120
2121 /* Find the width of the widest item in this pane. */
2122 maxwidth = 0;
2123 j = i;
2124 while (j < menu_items_used)
2125 {
2126 Lisp_Object item;
2127 item = XVECTOR (menu_items)->contents[j];
2128 if (EQ (item, Qt))
2129 break;
2130 if (NILP (item))
2131 {
2132 j++;
2133 continue;
2134 }
2135 width = XSTRING (item)->size;
2136 if (width > maxwidth)
2137 maxwidth = width;
2138
2139 j += MENU_ITEMS_ITEM_LENGTH;
2140 }
2141 }
2142 /* Ignore a nil in the item list.
2143 It's meaningful only for dialog boxes. */
2144 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2145 i += 1;
2146 else
2147 {
2148 /* Create a new item within current pane. */
2149 Lisp_Object item_name, enable, descrip;
2150 unsigned char *item_data;
2151
2152 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2153 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2154 descrip
2155 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2156 if (!NILP (descrip))
2157 {
2158 int gap = maxwidth - XSTRING (item_name)->size;
2159 #ifdef C_ALLOCA
2160 Lisp_Object spacer;
2161 spacer = Fmake_string (make_number (gap), make_number (' '));
2162 item_name = concat2 (item_name, spacer);
2163 item_name = concat2 (item_name, descrip);
2164 item_data = XSTRING (item_name)->data;
2165 #else
2166 /* if alloca is fast, use that to make the space,
2167 to reduce gc needs. */
2168 item_data
2169 = (unsigned char *) alloca (maxwidth
2170 + XSTRING (descrip)->size + 1);
2171 bcopy (XSTRING (item_name)->data, item_data,
2172 XSTRING (item_name)->size);
2173 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2174 item_data[j] = ' ';
2175 bcopy (XSTRING (descrip)->data, item_data + j,
2176 XSTRING (descrip)->size);
2177 item_data[j + XSTRING (descrip)->size] = 0;
2178 #endif
2179 }
2180 else
2181 item_data = XSTRING (item_name)->data;
2182
2183 if (XMenuAddSelection (XDISPLAY menu, lpane, 0, item_data,
2184 !NILP (enable))
2185 == XM_FAILURE)
2186 {
2187 XMenuDestroy (XDISPLAY menu);
2188 *error = "Can't add selection to menu";
2189 return Qnil;
2190 }
2191 i += MENU_ITEMS_ITEM_LENGTH;
2192 }
2193 }
2194
2195 /* All set and ready to fly. */
2196 XMenuRecompute (XDISPLAY menu);
2197 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
2198 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
2199 x = min (x, dispwidth);
2200 y = min (y, dispheight);
2201 x = max (x, 1);
2202 y = max (y, 1);
2203 XMenuLocate (XDISPLAY menu, 0, 0, x, y,
2204 &ulx, &uly, &width, &height);
2205 if (ulx+width > dispwidth)
2206 {
2207 x -= (ulx + width) - dispwidth;
2208 ulx = dispwidth - width;
2209 }
2210 if (uly+height > dispheight)
2211 {
2212 y -= (uly + height) - dispheight;
2213 uly = dispheight - height;
2214 }
2215 if (ulx < 0) x -= ulx;
2216 if (uly < 0) y -= uly;
2217
2218 XMenuSetAEQ (menu, TRUE);
2219 XMenuSetFreeze (menu, TRUE);
2220 pane = selidx = 0;
2221
2222 status = XMenuActivate (XDISPLAY menu, &pane, &selidx,
2223 x, y, ButtonReleaseMask, &datap);
2224 switch (status)
2225 {
2226 case XM_SUCCESS:
2227 #ifdef XDEBUG
2228 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2229 #endif
2230
2231 /* Find the item number SELIDX in pane number PANE. */
2232 i = 0;
2233 while (i < menu_items_used)
2234 {
2235 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2236 {
2237 if (pane == 0)
2238 pane_prefix
2239 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2240 pane--;
2241 i += MENU_ITEMS_PANE_LENGTH;
2242 }
2243 else
2244 {
2245 if (pane == -1)
2246 {
2247 if (selidx == 0)
2248 {
2249 entry
2250 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2251 if (keymaps != 0)
2252 {
2253 entry = Fcons (entry, Qnil);
2254 if (!NILP (pane_prefix))
2255 entry = Fcons (pane_prefix, entry);
2256 }
2257 break;
2258 }
2259 selidx--;
2260 }
2261 i += MENU_ITEMS_ITEM_LENGTH;
2262 }
2263 }
2264 break;
2265
2266 case XM_FAILURE:
2267 *error = "Can't activate menu";
2268 case XM_IA_SELECT:
2269 case XM_NO_SELECT:
2270 entry = Qnil;
2271 break;
2272 }
2273 XMenuDestroy (XDISPLAY menu);
2274
2275 /* State that no mouse buttons are now held.
2276 (The oldXMenu code doesn't track this info for us.)
2277 That is not necessarily true, but the fiction leads to reasonable
2278 results, and it is a pain to ask which are actually held now. */
2279 x_mouse_grabbed = 0;
2280
2281 return entry;
2282 }
2283 #endif /* not USE_X_TOOLKIT */
2284 \f
2285 syms_of_xmenu ()
2286 {
2287 staticpro (&menu_items);
2288 menu_items = Qnil;
2289
2290 popup_id_tick = (1<<16);
2291 defsubr (&Sx_popup_menu);
2292 defsubr (&Sx_popup_dialog);
2293 }