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