1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 /* On 4.3 this loses if it comes after xterm.h. */
38 #include "termhooks.h"
42 #include "blockinput.h"
45 /* This may include sys/types.h, and that somehow loses
46 if this is not done before the other system files. */
49 /* Load sys/types.h if not already loaded.
50 In some systems loading it twice is suicidal. */
52 #include <sys/types.h>
55 #include "dispextern.h"
58 #include "../oldXMenu/XMenu.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 */
74 #define min(x,y) (((x) < (y)) ? (x) : (y))
75 #define max(x,y) (((x) > (y)) ? (x) : (y))
83 extern Display
*x_current_display
;
85 #define ButtonReleaseMask ButtonReleased
86 #endif /* not HAVE_X11 */
88 /* We need a unique id for each popup menu and dialog box. */
89 static unsigned int popup_id_tick
;
91 extern Lisp_Object Qmenu_enable
;
92 extern Lisp_Object Qmenu_bar
;
95 extern void process_expose_from_menu ();
96 extern XtAppContext Xt_app_con
;
98 static Lisp_Object
xdialog_show ();
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 ();
107 /* This holds a Lisp vector that holds the results of decoding
108 the keymaps or alist-of-alists that specify a menu.
110 It describes the panes and items within the panes.
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.
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.
123 A single vector slot containing quote indicates that the
124 following items should appear on the right of a dialog box.
126 Using a Lisp vector to hold this information while we decode it
127 takes care of protecting all the data from GC. */
129 #define MENU_ITEMS_PANE_NAME 1
130 #define MENU_ITEMS_PANE_PREFIX 2
131 #define MENU_ITEMS_PANE_LENGTH 3
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
139 static Lisp_Object menu_items
;
141 /* Number of slots currently allocated in menu_items. */
142 static int menu_items_allocated
;
144 /* This is the index in menu_items of the first empty slot. */
145 static int menu_items_used
;
147 /* The number of panes currently recorded in menu_items,
148 excluding those within submenus. */
149 static int menu_items_n_panes
;
151 /* Current depth within submenus. */
152 static int menu_items_submenu_depth
;
154 /* Initialize the menu_items structure if we haven't already done so.
155 Also mark it as currently empty. */
160 if (NILP (menu_items
))
162 menu_items_allocated
= 60;
163 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
167 menu_items_n_panes
= 0;
168 menu_items_submenu_depth
= 0;
171 /* Call at the end of generating the data in menu_items.
172 This fills in the number of items in the last pane. */
179 /* Call when finished using the data for the current menu
183 discard_menu_items ()
185 /* Free the structure if it is especially large.
186 Otherwise, hold on to it, to save time. */
187 if (menu_items_allocated
> 200)
190 menu_items_allocated
= 0;
194 /* Make the menu_items vector twice as large. */
200 int old_size
= menu_items_allocated
;
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
));
209 /* Begin a submenu. */
212 push_submenu_start ()
214 if (menu_items_used
+ 1 > menu_items_allocated
)
217 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
218 menu_items_submenu_depth
++;
226 if (menu_items_used
+ 1 > menu_items_allocated
)
229 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
230 menu_items_submenu_depth
--;
233 /* Indicate boundary between left and right. */
236 push_left_right_boundary ()
238 if (menu_items_used
+ 1 > menu_items_allocated
)
241 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
244 /* Start a new menu pane in menu_items..
245 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
248 push_menu_pane (name
, prefix_vec
)
249 Lisp_Object name
, prefix_vec
;
251 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
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
;
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). */
268 push_menu_item (name
, enable
, key
, equiv
)
269 Lisp_Object name
, enable
, key
, equiv
;
271 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
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
;
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. */
288 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
289 Lisp_Object item_string
;
291 Lisp_Object
*descrip_ptr
;
293 /* This is the real definition--the function to run. */
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
;
303 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
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
;
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
)))
318 cachelist
= XCONS (def
)->car
;
319 def
= XCONS (def
)->cdr
;
320 savedkey
= XCONS (cachelist
)->car
;
321 descrip
= XCONS (cachelist
)->cdr
;
324 GCPRO4 (def
, def1
, savedkey
, descrip
);
326 /* Is it still valid? */
328 if (!NILP (savedkey
))
329 def1
= Fkey_binding (savedkey
, Qnil
);
330 /* If not, update it. */
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
)))
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
,
351 if (VECTORP (savedkey
)
352 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
354 if (!NILP (savedkey
))
356 descrip
= Fkey_description (savedkey
);
357 descrip
= concat2 (make_string (" (", 3), descrip
);
358 descrip
= concat2 (descrip
, make_string (")", 1));
362 /* Cache the data we just got in a sublist of the menu binding. */
363 if (NILP (cachelist
))
365 CHECK_IMPURE (item1
);
366 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
370 XCONS (cachelist
)->car
= savedkey
;
371 XCONS (cachelist
)->cdr
= descrip
;
375 *descrip_ptr
= descrip
;
379 /* This is used as the handler when calling internal_condition_case_1. */
382 menu_item_enabled_p_1 (arg
)
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. */
393 menu_item_enabled_p (def
, notreal
)
396 Lisp_Object enabled
, tem
;
401 if (XTYPE (def
) == Lisp_Symbol
)
403 /* No property, or nil, means enable.
404 Otherwise, enable if value is not nil. */
405 tem
= Fget (def
, Qmenu_enable
);
407 /* (condition-case nil (eval tem)
409 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
410 menu_item_enabled_p_1
);
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. */
421 keymap_panes (keymaps
, nmaps
, notreal
)
422 Lisp_Object
*keymaps
;
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
);
436 finish_menu_items ();
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. */
447 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
449 Lisp_Object pane_name
;
453 Lisp_Object pending_maps
;
454 Lisp_Object tail
, item
, item1
, item_string
, table
;
455 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
459 push_menu_pane (pane_name
, prefix
);
461 for (tail
= keymap
; XTYPE (tail
) == Lisp_Cons
; tail
= XCONS (tail
)->cdr
)
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
)
468 item1
= XCONS (item
)->cdr
;
469 if (XTYPE (item1
) == Lisp_Cons
)
471 item_string
= XCONS (item1
)->car
;
472 if (XTYPE (item_string
) == Lisp_String
)
474 /* This is the real definition--the function to run. */
476 /* These are the saved equivalent keyboard key sequence
477 and its key-description. */
479 Lisp_Object tem
, enabled
;
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
);
490 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
491 enabled
= menu_item_enabled_p (def
, notreal
);
495 item_string
= XCONS (item1
)->car
;
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
)),
504 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
505 submap
= get_keymap_1 (def
, 0, 1);
507 #ifndef USE_X_TOOLKIT
508 /* Indicate visually that this is a submenu. */
510 item_string
= concat2 (item_string
,
511 build_string (" >"));
513 push_menu_item (item_string
, enabled
, XCONS (item
)->car
,
516 /* Display a submenu using the toolkit. */
519 push_submenu_start ();
520 single_keymap_panes (submap
, Qnil
,
521 XCONS (item
)->car
, notreal
);
529 else if (XTYPE (item
) == Lisp_Vector
)
531 /* Loop over the char values represented in the vector. */
532 int len
= XVECTOR (item
)->size
;
534 for (c
= 0; c
< len
; c
++)
536 Lisp_Object character
;
537 XFASTINT (character
) = c
;
538 item1
= XVECTOR (item
)->contents
[c
];
539 if (XTYPE (item1
) == Lisp_Cons
)
541 item_string
= XCONS (item1
)->car
;
542 if (XTYPE (item_string
) == Lisp_String
)
546 /* These are the saved equivalent keyboard key sequence
547 and its key-description. */
549 Lisp_Object tem
, enabled
;
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
;
560 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
561 enabled
= menu_item_enabled_p (def
, notreal
);
565 item_string
= XCONS (item1
)->car
;
567 tem
= Fkeymapp (def
);
568 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
569 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
574 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
575 submap
= get_keymap_1 (def
, 0, 1);
577 #ifndef USE_X_TOOLKIT
579 item_string
= concat2 (item_string
,
580 build_string (" >"));
582 push_menu_item (item_string
, enabled
, character
,
587 push_submenu_start ();
588 single_keymap_panes (submap
, Qnil
,
600 /* Process now any submenus which want to be panes at this level. */
601 while (!NILP (pending_maps
))
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
);
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. */
627 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
629 Lisp_Object elt
, pane_name
, pane_data
;
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
);
639 finish_menu_items ();
642 /* Push the items in a single pane defined by the alist PANE. */
648 Lisp_Object tail
, item
, item1
;
650 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
654 push_menu_item (item
, Qnil
, Qnil
, Qnil
);
655 else if (NILP (item
))
656 push_left_right_boundary ();
659 CHECK_CONS (item
, 0);
661 CHECK_STRING (item1
, 1);
662 push_menu_item (item1
, Qt
, Fcdr (item
), Qnil
);
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\
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\
692 With this form of menu, the return value is VALUE from the chosen item.\n\
694 If POSITION is nil, don't display the menu at all, just precalculate the\n\
695 cached information about equivalent key sequences.")
697 Lisp_Object position
, menu
;
699 int number_of_panes
, panes
;
700 Lisp_Object keymap
, tem
;
704 Lisp_Object selection
;
707 Lisp_Object x
, y
, window
;
712 if (! NILP (position
))
716 /* Decode the first argument: find the window and the coordinates. */
717 if (EQ (position
, Qt
))
719 /* Use the mouse's current position. */
721 Lisp_Object bar_window
;
725 if (mouse_position_hook
)
726 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
728 XSET (window
, Lisp_Frame
, new_f
);
731 window
= selected_window
;
738 tem
= Fcar (position
);
739 if (XTYPE (tem
) == Lisp_Cons
)
741 window
= Fcar (Fcdr (position
));
743 y
= Fcar (Fcdr (tem
));
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) */
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
))
763 /* Decode where to put the menu. */
765 if (XTYPE (window
) == Lisp_Frame
)
772 else if (XTYPE (window
) == Lisp_Window
)
774 CHECK_LIVE_WINDOW (window
, 0);
775 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
777 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
778 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
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);
792 /* Decode the menu items from what was specified. */
794 keymap
= Fkeymapp (menu
);
796 if (XTYPE (menu
) == Lisp_Cons
)
797 tem
= Fkeymapp (Fcar (menu
));
800 /* We were given a keymap. Extract menu info from the keymap. */
802 keymap
= get_keymap (menu
);
804 /* Extract the detailed info to make one pane. */
805 keymap_panes (&menu
, 1, NILP (position
));
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
);
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
;
817 else if (!NILP (tem
))
819 /* We were given a list of keymaps. */
820 int nmaps
= XFASTINT (Flength (menu
));
822 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
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
))
833 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
835 prompt
= map_prompt (keymap
);
836 if (NILP (title
) && !NILP (prompt
))
840 /* Extract the detailed info to make one pane. */
841 keymap_panes (maps
, nmaps
, NILP (position
));
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
;
851 /* We were given an old-fashioned menu. */
853 CHECK_STRING (title
, 1);
855 list_of_panes (Fcdr (menu
));
862 discard_menu_items ();
867 /* Display them in a menu. */
870 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
871 keymaps
, title
, &error_name
);
874 discard_menu_items ();
878 if (error_name
) error (error_name
);
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\
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.)")
898 Lisp_Object position
, contents
;
905 /* Decode the first argument: find the window or frame to use. */
906 if (EQ (position
, Qt
))
908 #if 0 /* Using the frame the mouse is on may not be right. */
909 /* Use the mouse's current position. */
911 Lisp_Object bar_window
;
916 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
919 XSET (window
, Lisp_Frame
, new_f
);
921 window
= selected_window
;
923 /* Decode the first argument: find the window and the coordinates. */
924 if (EQ (position
, Qt
))
925 window
= selected_window
;
927 else if (CONSP (position
))
930 tem
= Fcar (position
);
931 if (XTYPE (tem
) == Lisp_Cons
)
932 window
= Fcar (Fcdr (position
));
935 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
936 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
939 else if (WINDOWP (position
) || FRAMEP (position
))
942 /* Decode where to put the menu. */
944 if (XTYPE (window
) == Lisp_Frame
)
946 else if (XTYPE (window
) == Lisp_Window
)
948 CHECK_LIVE_WINDOW (window
, 0);
949 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
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);
956 #ifndef USE_X_TOOLKIT
957 /* Display a menu with these alternatives
958 in the middle of frame F. */
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
));
966 return Fx_popup_menu (newpos
,
967 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
973 Lisp_Object selection
;
975 /* Decode the dialog items from what was specified. */
976 title
= Fcar (contents
);
977 CHECK_STRING (title
, 1);
979 list_of_panes (Fcons (contents
, Qnil
));
981 /* Display them in a dialog box. */
983 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
986 discard_menu_items ();
988 if (error_name
) error (error_name
);
997 dispatch_dummy_expose (w
, x
, y
)
1004 dummy
.type
= Expose
;
1005 dummy
.window
= XtWindow (w
);
1008 dummy
.send_event
= 0;
1009 dummy
.display
= XtDisplay (w
);
1013 XtDispatchEvent ((XEvent
*) &dummy
);
1017 event_is_in_menu_item (mw
, event
, name
, string_w
)
1019 struct input_event
*event
;
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
;
1030 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1033 map_event_to_object (event
, f
)
1034 struct input_event
*event
;
1039 XlwMenuWidget mw
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1044 /* Find the window */
1045 for (val
= mw
->menu
.old_stack
[0]->contents
; val
; val
= val
->next
)
1047 ws
= &mw
->menu
.windows
[0];
1048 if (ws
&& event_is_in_menu_item (mw
, event
, val
->name
, &string_w
))
1053 items
= FRAME_MENU_BAR_ITEMS (f
);
1055 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
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];
1064 if (!strcmp (val
->name
, XSTRING (string
)->data
))
1073 static Lisp_Object
*volatile menu_item_selection
;
1075 static Lisp_Object
*menu_item_selection
;
1079 popup_selection_callback (widget
, id
, client_data
)
1082 XtPointer client_data
;
1084 menu_item_selection
= (Lisp_Object
*) client_data
;
1088 popup_down_callback (widget
, id
, client_data
)
1091 XtPointer client_data
;
1094 lw_destroy_all_widgets (id
);
1099 dialog_selection_callback (widget
, id
, client_data
)
1102 XtPointer client_data
;
1104 if ((int)client_data
!= -1)
1105 menu_item_selection
= (Lisp_Object
*) client_data
;
1107 lw_destroy_all_widgets (id
);
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. */
1117 free_menubar_widget_value_tree (wv
)
1122 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1124 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1126 free_menubar_widget_value_tree (wv
->contents
);
1127 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1131 free_menubar_widget_value_tree (wv
->next
);
1132 wv
->next
= (widget_value
*) 0xDEADBEEF;
1135 free_widget_value (wv
);
1139 extern void EmacsFrameSetCharSize ();
1142 update_frame_menubar (f
)
1145 struct x_display
*x
= f
->display
.x
;
1147 int menubar_changed
;
1149 menubar_changed
= (x
->menubar_widget
1150 && !XtIsManaged (x
->menubar_widget
));
1152 if (! (menubar_changed
))
1156 /* Save the size of the frame because the pane widget doesn't accept to
1157 resize itself. So force it. */
1162 XawPanedSetRefigureMode (x
->column_widget
, 0);
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.
1168 XtUnmanageChild (x
->edit_widget
);
1170 /* remove the menubar that is there now, and put up the menubar that
1173 if (menubar_changed
)
1175 XtManageChild (x
->menubar_widget
);
1176 XtMapWidget (x
->menubar_widget
);
1177 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1181 /* Re-manage the text-area widget */
1182 XtManageChild (x
->edit_widget
);
1184 /* and now thrash the sizes */
1185 XawPanedSetRefigureMode (x
->column_widget
, 1);
1187 /* Force the pane widget to resize itself with the right values. */
1188 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1194 set_frame_menubar (f
, first_time
)
1198 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1200 Lisp_Object tail
, items
;
1201 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
1206 wv
= malloc_widget_value ();
1207 wv
->name
= "menubar";
1210 save_wv
= first_wv
= wv
;
1212 if (NILP (items
= FRAME_MENU_BAR_ITEMS (f
)))
1213 items
= FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1215 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1219 string
= XVECTOR (items
)->contents
[i
+ 1];
1223 wv
= malloc_widget_value ();
1227 save_wv
->contents
= wv
;
1228 wv
->name
= (char *) XSTRING (string
)->data
;
1235 lw_modify_all_widgets (id
, first_wv
, False
);
1238 menubar_widget
= lw_create_widget ("menubar", "menubar",
1240 f
->display
.x
->column_widget
,
1243 f
->display
.x
->menubar_widget
= menubar_widget
;
1244 XtVaSetValues (menubar_widget
,
1246 XtNresizeToPreferred
, 1,
1251 free_menubar_widget_value_tree (first_wv
);
1253 /* Don't update the menubar the first time it is created via x_window. */
1255 update_frame_menubar (f
);
1261 free_frame_menubar (f
)
1264 Widget menubar_widget
;
1267 menubar_widget
= f
->display
.x
->menubar_widget
;
1273 lw_destroy_all_widgets (id
);
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
1282 initialize_frame_menubar (f
)
1285 set_frame_menubar (f
, 1);
1288 /* Horizontal bounds of the current menu bar item. */
1290 static int this_menu_bar_item_beg
;
1291 static int this_menu_bar_item_end
;
1293 /* Horizontal position of the end of the last menu bar item. */
1295 static int last_menu_bar_item_end
;
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. */
1301 other_menu_bar_item_p (f
, x
, y
)
1306 && f
->display
.x
->menubar_widget
!= 0
1307 && y
< f
->display
.x
->menubar_widget
->core
.height
1309 && x
< last_menu_bar_item_end
1310 && (x
>= this_menu_bar_item_end
1311 || x
< this_menu_bar_item_beg
));
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. */
1318 unread_menu_bar_button (f
, xpos
)
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
);
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. */
1340 check_mouse_other_menu_bar (f
)
1344 Lisp_Object bar_window
;
1349 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
1351 if (f
== new_f
&& other_menu_bar_item_p (f
, x
, y
))
1353 unread_menu_bar_button (f
, x
);
1359 #endif /* USE_X_TOOLKIT */
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. */
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.) */
1377 #ifdef USE_X_TOOLKIT
1379 extern unsigned last_event_timestamp
;
1380 extern Lisp_Object Vdouble_click_time
;
1382 extern unsigned int x_mouse_grabbed
;
1383 extern Lisp_Object Vmouse_depressed
;
1386 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1398 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1402 /* This is the menu bar item (if any) that led to this menu. */
1403 widget_value
*menubar_item
= 0;
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;
1412 /* Define a queue to save up for later unreading
1413 all X events that don't pertain to the menu. */
1417 struct event_queue
*next
;
1420 struct event_queue
*queue
= NULL
;
1421 struct event_queue
*queue_tmp
;
1423 Position root_x
, root_y
;
1426 int next_release_must_exit
= 0;
1430 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1432 *error
= "Empty menu";
1435 this_menu_bar_item_beg
= -1;
1436 this_menu_bar_item_end
= -1;
1437 last_menu_bar_item_end
= -1;
1439 /* Figure out which menu bar item, if any, this menu is for. */
1444 widget_value
*mb_item
= 0;
1446 for (mb_item
= menubar
->menu
.old_stack
[0]->contents
;
1448 mb_item
= mb_item
->next
)
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
)
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
;
1465 last_menu_bar_item_end
= xend
;
1467 if (menubar_item
== 0)
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
);
1478 /* Create a tree of widget_value objects
1479 representing the panes and their items. */
1480 wv
= malloc_widget_value ();
1487 /* Loop over all panes and items, filling in the tree. */
1489 while (i
< menu_items_used
)
1491 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1493 submenu_stack
[submenu_depth
++] = save_wv
;
1499 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1502 save_wv
= submenu_stack
[--submenu_depth
];
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
))
1513 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1515 /* Create a new pane. */
1516 Lisp_Object pane_name
, prefix
;
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)
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
, ""))
1532 wv
= malloc_widget_value ();
1536 first_wv
->contents
= wv
;
1537 wv
->name
= pane_string
;
1538 if (keymaps
&& !NILP (prefix
))
1545 else if (first_pane
)
1551 i
+= MENU_ITEMS_PANE_LENGTH
;
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
];
1560 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1562 wv
= malloc_widget_value ();
1566 save_wv
->contents
= wv
;
1567 wv
->name
= (char *) XSTRING (item_name
)->data
;
1568 if (!NILP (descrip
))
1569 wv
->key
= (char *) XSTRING (descrip
)->data
;
1571 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1572 wv
->enabled
= !NILP (enable
);
1575 i
+= MENU_ITEMS_ITEM_LENGTH
;
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
);
1585 /* Don't allow any geometry request from the user. */
1586 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1587 XtSetValues (menu
, av
, ac
);
1589 /* Free the widget_value objects we used to specify the contents. */
1590 free_menubar_widget_value_tree (first_wv
);
1592 /* No selection has been chosen yet. */
1593 menu_item_selection
= 0;
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
))
1599 lw_destroy_all_widgets (menu_id
);
1604 /* Highlight the menu bar item (if any) that led to this menu. */
1607 menubar_item
->call_data
= (XtPointer
) 1;
1608 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1611 /* Display the menu. */
1613 XButtonPressedEvent dummy
;
1616 mw
= (XlwMenuWidget
) ((CompositeWidget
)menu
)->composite
.children
[0];
1618 dummy
.type
= ButtonPress
;
1620 dummy
.send_event
= 0;
1621 dummy
.display
= XtDisplay (menu
);
1622 dummy
.window
= XtWindow (XtParent (menu
));
1623 dummy
.time
= CurrentTime
;
1628 /* We activate directly the lucid implementation. */
1629 pop_up_menu (mw
, &dummy
);
1632 /* No need to check a second time since this is done in the XEvent loop.
1633 This slows done the execution. */
1635 /* Check again whether the mouse has moved to another menu bar item. */
1636 if (check_mouse_other_menu_bar (f
))
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
)
1644 ((CompositeWidget
)menu
)->composite
.children
[0]),
1647 lw_destroy_all_widgets (menu_id
);
1652 /* Process events that apply to the menu. */
1656 int queue_and_exit
= 0;
1657 int in_this_menu
= 0, in_menu_bar
= 0;
1660 XtAppNextEvent (Xt_app_con
, &event
);
1662 /* Check whether the event happened in the menu
1663 or any child of it. */
1664 widget
= XtWindowToWidget (XDISPLAY event
.xany
.window
);
1673 if (widget
== f
->display
.x
->menubar_widget
)
1678 widget
= XtParent (widget
);
1681 if (event
.type
== ButtonRelease
)
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
;
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. */
1695 && (next_release_must_exit
1696 || !(((XButtonEvent
*) (&event
))->time
- last_event_timestamp
1697 < XINT (Vdouble_click_time
))))
1700 /* A button press outside the menu => pop it down. */
1701 else if (event
.type
== ButtonPress
&& !in_this_menu
)
1703 else if (event
.type
== ButtonPress
)
1704 next_release_must_exit
= 1;
1705 else if (event
.type
== KeyPress
)
1707 /* Exit the loop, but first queue this event for reuse. */
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
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
));
1724 if (other_menu_bar_item_p (f
, event_x
, event_y
))
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
)
1732 ((CompositeWidget
)menu
)->composite
.children
[0]),
1733 event
.xbutton
.time
);
1735 lw_destroy_all_widgets (menu_id
);
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;
1744 else if (event
.type
== UnmapNotify
)
1746 /* If the menu disappears, there is no need to stay in the
1748 if (event
.xunmap
.window
== menu
->core
.window
)
1752 XtDispatchEvent (&event
);
1754 if (queue_and_exit
|| (!in_this_menu
&& !in_menu_bar
))
1757 = (struct event_queue
*) malloc (sizeof (struct event_queue
));
1759 if (queue_tmp
!= NULL
)
1761 queue_tmp
->event
= event
;
1762 queue_tmp
->next
= queue
;
1771 /* Unhighlight the menu bar item (if any) that led to this menu. */
1774 menubar_item
->call_data
= (XtPointer
) 0;
1775 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
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
);
1784 /* Unread any events that we got but did not handle. */
1785 while (queue
!= NULL
)
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;
1795 /* Find the selected item, and its pane, to return
1796 the proper value. */
1797 if (menu_item_selection
!= 0)
1803 while (i
< menu_items_used
)
1807 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1809 subprefix_stack
[submenu_depth
++] = prefix
;
1813 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1815 prefix
= subprefix_stack
[--submenu_depth
];
1818 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1821 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1822 i
+= MENU_ITEMS_PANE_LENGTH
;
1827 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1828 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1834 entry
= Fcons (entry
, Qnil
);
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
);
1843 i
+= MENU_ITEMS_ITEM_LENGTH
;
1851 static char * button_names
[] = {
1852 "button1", "button2", "button3", "button4", "button5",
1853 "button6", "button7", "button8", "button9", "button10" };
1856 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1863 int i
, nb_buttons
=0;
1866 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1867 char dialog_name
[6];
1869 /* This is the menu bar item (if any) that led to this menu. */
1870 widget_value
*menubar_item
= 0;
1872 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1874 /* Define a queue to save up for later unreading
1875 all X events that don't pertain to the menu. */
1879 struct event_queue
*next
;
1882 struct event_queue
*queue
= NULL
;
1883 struct event_queue
*queue_tmp
;
1885 /* Number of elements seen so far, before boundary. */
1887 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1888 int boundary_seen
= 0;
1892 if (menu_items_n_panes
> 1)
1894 *error
= "Multiple panes in dialog box";
1898 /* Create a tree of widget_value objects
1899 representing the text label and buttons. */
1901 Lisp_Object pane_name
, prefix
;
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
))
1911 prev_wv
->enabled
= 1;
1912 prev_wv
->name
= "message";
1915 /* Loop over all panes and items, filling in the tree. */
1916 i
= MENU_ITEMS_PANE_LENGTH
;
1917 while (i
< menu_items_used
)
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
];
1925 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1927 if (NILP (item_name
))
1929 free_menubar_widget_value_tree (first_wv
);
1930 *error
= "Submenu in dialog items";
1933 if (EQ (item_name
, Qquote
))
1935 /* This is the boundary between left-side elts
1936 and right-side elts. Stop incrementing right_count. */
1941 if (nb_buttons
>= 10)
1943 free_menubar_widget_value_tree (first_wv
);
1944 *error
= "Too many dialog items";
1948 wv
= malloc_widget_value ();
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
);
1958 if (! boundary_seen
)
1962 i
+= MENU_ITEMS_ITEM_LENGTH
;
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;
1970 wv
= malloc_widget_value ();
1971 wv
->name
= dialog_name
;
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
;
1984 wv
->contents
= first_wv
;
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
);
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
);
2000 /* No selection has been chosen yet. */
2001 menu_item_selection
= 0;
2003 /* Display the menu. */
2004 lw_pop_up_all_widgets (dialog_id
);
2006 /* Process events that apply to the menu. */
2011 XtAppNextEvent (Xt_app_con
, &event
);
2012 if (event
.type
== ButtonRelease
)
2014 XtDispatchEvent (&event
);
2017 else if (event
.type
== Expose
)
2018 process_expose_from_menu (event
);
2019 XtDispatchEvent (&event
);
2020 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
2022 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
2024 if (queue_tmp
!= NULL
)
2026 queue_tmp
->event
= event
;
2027 queue_tmp
->next
= queue
;
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;
2040 /* Unread any events that we got but did not handle. */
2041 while (queue
!= NULL
)
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;
2051 /* Find the selected item, and its pane, to return
2052 the proper value. */
2053 if (menu_item_selection
!= 0)
2059 while (i
< menu_items_used
)
2063 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2066 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2067 i
+= MENU_ITEMS_PANE_LENGTH
;
2072 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2073 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2077 entry
= Fcons (entry
, Qnil
);
2079 entry
= Fcons (prefix
, entry
);
2083 i
+= MENU_ITEMS_ITEM_LENGTH
;
2090 #else /* not USE_X_TOOLKIT */
2093 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2103 int pane
, selidx
, lpane
, status
;
2104 Lisp_Object entry
, pane_prefix
;
2106 int ulx
, uly
, width
, height
;
2107 int dispwidth
, dispheight
;
2111 unsigned int dummy_uint
;
2114 if (menu_items_n_panes
== 0)
2117 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2119 *error
= "Empty menu";
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
);
2128 /* Make the menu on that window. */
2129 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2132 *error
= "Can't create menu";
2136 /* Adjust coordinates to relative to the outer (window manager) window. */
2140 int win_x
= 0, win_y
= 0;
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
)
2147 XTranslateCoordinates (x_current_display
,
2149 /* From-window, to-window. */
2150 f
->display
.x
->window_desc
,
2151 f
->display
.x
->parent_desc
,
2153 /* From-position, to-position. */
2154 0, 0, &win_x
, &win_y
,
2156 /* Child of window. */
2163 #endif /* HAVE_X11 */
2165 /* Adjust coordinates to be root-window-relative. */
2166 x
+= f
->display
.x
->left_pos
;
2167 y
+= f
->display
.x
->top_pos
;
2169 /* Create all the necessary panes and their items. */
2171 while (i
< menu_items_used
)
2173 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2175 /* Create a new pane. */
2176 Lisp_Object pane_name
, prefix
;
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
))
2186 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2187 if (lpane
== XM_FAILURE
)
2189 XMenuDestroy (XDISPLAY menu
);
2190 *error
= "Can't create pane";
2193 i
+= MENU_ITEMS_PANE_LENGTH
;
2195 /* Find the width of the widest item in this pane. */
2198 while (j
< menu_items_used
)
2201 item
= XVECTOR (menu_items
)->contents
[j
];
2209 width
= XSTRING (item
)->size
;
2210 if (width
> maxwidth
)
2213 j
+= MENU_ITEMS_ITEM_LENGTH
;
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
))
2222 /* Create a new item within current pane. */
2223 Lisp_Object item_name
, enable
, descrip
;
2224 unsigned char *item_data
;
2226 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2227 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2229 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2230 if (!NILP (descrip
))
2232 int gap
= maxwidth
- XSTRING (item_name
)->size
;
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
;
2240 /* if alloca is fast, use that to make the space,
2241 to reduce gc needs. */
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
++)
2249 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2250 XSTRING (descrip
)->size
);
2251 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2255 item_data
= XSTRING (item_name
)->data
;
2257 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2261 XMenuDestroy (XDISPLAY menu
);
2262 *error
= "Can't add selection to menu";
2265 i
+= MENU_ITEMS_ITEM_LENGTH
;
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
);
2277 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2278 &ulx
, &uly
, &width
, &height
);
2279 if (ulx
+width
> dispwidth
)
2281 x
-= (ulx
+ width
) - dispwidth
;
2282 ulx
= dispwidth
- width
;
2284 if (uly
+height
> dispheight
)
2286 y
-= (uly
+ height
) - dispheight
;
2287 uly
= dispheight
- height
;
2289 if (ulx
< 0) x
-= ulx
;
2290 if (uly
< 0) y
-= uly
;
2292 XMenuSetAEQ (menu
, TRUE
);
2293 XMenuSetFreeze (menu
, TRUE
);
2296 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2297 x
, y
, ButtonReleaseMask
, &datap
);
2302 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2305 /* Find the item number SELIDX in pane number PANE. */
2307 while (i
< menu_items_used
)
2309 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2313 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2315 i
+= MENU_ITEMS_PANE_LENGTH
;
2324 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2327 entry
= Fcons (entry
, Qnil
);
2328 if (!NILP (pane_prefix
))
2329 entry
= Fcons (pane_prefix
, entry
);
2335 i
+= MENU_ITEMS_ITEM_LENGTH
;
2341 *error
= "Can't activate menu";
2347 XMenuDestroy (XDISPLAY menu
);
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;
2357 #endif /* not USE_X_TOOLKIT */
2361 staticpro (&menu_items
);
2364 popup_id_tick
= (1<<16);
2365 defsubr (&Sx_popup_menu
);
2366 defsubr (&Sx_popup_dialog
);