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