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