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