(EMACS_CLASS): Remove. Use generic define.
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
950eaee7
GM
2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000, 2001
3 Free Software Foundation, Inc.
dcfdbac7
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
774910eb 9the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dcfdbac7 21
c9e60620 22/* X pop-up deck-of-cards menu facility for GNU Emacs.
dcfdbac7
JB
23 *
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
26 *
27 */
28
18686d47
RS
29/* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
31
78589e07
RS
32/* Rewritten for clarity and GC protection by rms in Feb 94. */
33
68c45bf0
PE
34#include <config.h>
35
dcfdbac7
JB
36/* On 4.3 this loses if it comes after xterm.h. */
37#include <signal.h>
565620a5
RS
38
39#include <stdio.h>
7ee72033 40
dcfdbac7 41#include "lisp.h"
18686d47 42#include "termhooks.h"
02067692 43#include "keyboard.h"
5ee707b8 44#include "keymap.h"
7708e9bd 45#include "frame.h"
dcfdbac7 46#include "window.h"
9ac0d9e0 47#include "blockinput.h"
88766961 48#include "buffer.h"
cc45fb40
SM
49#include "charset.h"
50#include "coding.h"
dcfdbac7 51
eeee3112
RS
52#ifdef MSDOS
53#include "msdos.h"
54#endif
55
87485d6f 56#ifdef HAVE_X_WINDOWS
dcfdbac7
JB
57/* This may include sys/types.h, and that somehow loses
58 if this is not done before the other system files. */
59#include "xterm.h"
87485d6f 60#endif
dcfdbac7
JB
61
62/* Load sys/types.h if not already loaded.
63 In some systems loading it twice is suicidal. */
64#ifndef makedev
65#include <sys/types.h>
66#endif
67
68#include "dispextern.h"
69
87485d6f 70#ifdef HAVE_X_WINDOWS
703dc2a8 71#undef HAVE_MULTILINGUAL_MENU
18686d47 72#ifdef USE_X_TOOLKIT
991786dd 73#include "widget.h"
18686d47
RS
74#include <X11/Xlib.h>
75#include <X11/IntrinsicP.h>
76#include <X11/CoreP.h>
77#include <X11/StringDefs.h>
60f312e2 78#include <X11/Shell.h>
5ee80bd3 79#ifdef USE_LUCID
1d1c1567 80#include <X11/Xaw/Paned.h>
5ee80bd3 81#endif /* USE_LUCID */
18686d47 82#include "../lwlib/lwlib.h"
a352a815 83#else /* not USE_X_TOOLKIT */
488dd4c4 84#ifndef USE_GTK
a352a815 85#include "../oldXMenu/XMenu.h"
488dd4c4 86#endif
a352a815
RS
87#endif /* not USE_X_TOOLKIT */
88#endif /* HAVE_X_WINDOWS */
18686d47 89
dcfdbac7
JB
90#ifndef TRUE
91#define TRUE 1
92#define FALSE 0
78589e07 93#endif /* no TRUE */
dcfdbac7 94
bfc524bc
RS
95Lisp_Object Vmenu_updating_frame;
96
0314aacb
RS
97Lisp_Object Qdebug_on_next_call;
98
18686d47 99extern Lisp_Object Qmenu_bar;
92280f67 100extern Lisp_Object Qmouse_click, Qevent_kind;
78589e07 101
de57a39c 102extern Lisp_Object QCtoggle, QCradio;
8fbc986d 103
88766961
RS
104extern Lisp_Object Voverriding_local_map;
105extern Lisp_Object Voverriding_local_map_menu_flag;
106
107extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
108
109extern Lisp_Object Qmenu_bar_update_hook;
110
18686d47 111#ifdef USE_X_TOOLKIT
88766961 112extern void set_frame_menubar ();
78589e07
RS
113extern XtAppContext Xt_app_con;
114
165e1749 115static Lisp_Object xdialog_show ();
f02cac82 116static void popup_get_selection ();
3427a3db 117
c9e60620 118/* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
3427a3db
GM
119
120#define HAVE_BOXES 1
488dd4c4
JD
121#endif /* USE_X_TOOLKIT */
122
123#ifdef USE_GTK
124#include "gtkutil.h"
125#define HAVE_BOXES 1
126extern void set_frame_menubar ();
127static Lisp_Object xdialog_show ();
3427a3db
GM
128#endif
129
130static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
131 Lisp_Object, Lisp_Object, Lisp_Object,
850df50b 132 Lisp_Object, Lisp_Object));
f61a541b
GM
133static int update_frame_menubar P_ ((struct frame *));
134static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
135 Lisp_Object, char **));
136static void keymap_panes P_ ((Lisp_Object *, int, int));
137static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
138 int, int));
139static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object *,
140 int, int, int *));
141static void list_of_panes P_ ((Lisp_Object));
142static void list_of_items P_ ((Lisp_Object));
f02cac82
RS
143
144extern EMACS_TIME timer_check P_ ((int));
78589e07
RS
145\f
146/* This holds a Lisp vector that holds the results of decoding
147 the keymaps or alist-of-alists that specify a menu.
dcfdbac7 148
78589e07 149 It describes the panes and items within the panes.
dcfdbac7 150
78589e07
RS
151 Each pane is described by 3 elements in the vector:
152 t, the pane name, the pane's prefix key.
a352a815 153 Then follow the pane's items, with 5 elements per item:
78589e07 154 the item string, the enable flag, the item's value,
a352a815 155 the definition, and the equivalent keyboard key's description string.
dcfdbac7 156
101bb4a5
RS
157 In some cases, multiple levels of menus may be described.
158 A single vector slot containing nil indicates the start of a submenu.
159 A single vector slot containing lambda indicates the end of a submenu.
160 The submenu follows a menu item which is the way to reach the submenu.
161
fcaa7665
RS
162 A single vector slot containing quote indicates that the
163 following items should appear on the right of a dialog box.
164
78589e07
RS
165 Using a Lisp vector to hold this information while we decode it
166 takes care of protecting all the data from GC. */
dcfdbac7 167
78589e07
RS
168#define MENU_ITEMS_PANE_NAME 1
169#define MENU_ITEMS_PANE_PREFIX 2
170#define MENU_ITEMS_PANE_LENGTH 3
088831f6 171
850df50b
GM
172enum menu_item_idx
173{
174 MENU_ITEMS_ITEM_NAME = 0,
175 MENU_ITEMS_ITEM_ENABLE,
176 MENU_ITEMS_ITEM_VALUE,
177 MENU_ITEMS_ITEM_EQUIV_KEY,
178 MENU_ITEMS_ITEM_DEFINITION,
179 MENU_ITEMS_ITEM_TYPE,
180 MENU_ITEMS_ITEM_SELECTED,
181 MENU_ITEMS_ITEM_HELP,
182 MENU_ITEMS_ITEM_LENGTH
183};
7da99777 184
78589e07 185static Lisp_Object menu_items;
18686d47 186
86fad4ec
SM
187/* If non-nil, means that the global vars defined here are already in use.
188 Used to detect cases where we try to re-enter this non-reentrant code. */
189static Lisp_Object menu_items_inuse;
190
78589e07
RS
191/* Number of slots currently allocated in menu_items. */
192static int menu_items_allocated;
18686d47 193
78589e07
RS
194/* This is the index in menu_items of the first empty slot. */
195static int menu_items_used;
18686d47 196
101bb4a5
RS
197/* The number of panes currently recorded in menu_items,
198 excluding those within submenus. */
78589e07 199static int menu_items_n_panes;
18686d47 200
101bb4a5
RS
201/* Current depth within submenus. */
202static int menu_items_submenu_depth;
203
4dedbfe0 204/* Flag which when set indicates a dialog or menu has been posted by
c98fcf4b 205 Xt on behalf of one of the widget sets. */
488dd4c4 206static int popup_activated_flag;
4dedbfe0 207
88766961 208static int next_menubar_widget_id;
745c34fb 209
a9be6839
RS
210/* This is set nonzero after the user activates the menu bar, and set
211 to zero again after the menu bars are redisplayed by prepare_menu_bar.
212 While it is nonzero, all calls to set_frame_menubar go deep.
213
214 I don't understand why this is needed, but it does seem to be
215 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
216
217int pending_menu_activation;
bd3a4da2 218\f
88766961 219#ifdef USE_X_TOOLKIT
bd3a4da2 220
7556890b 221/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
bd3a4da2 222
88766961
RS
223static struct frame *
224menubar_id_to_frame (id)
225 LWLIB_ID id;
226{
227 Lisp_Object tail, frame;
228 FRAME_PTR f;
bd3a4da2 229
8e713be6 230 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
bd3a4da2 231 {
8e713be6 232 frame = XCAR (tail);
88766961
RS
233 if (!GC_FRAMEP (frame))
234 continue;
235 f = XFRAME (frame);
2d764c78 236 if (!FRAME_WINDOW_P (f))
88766961 237 continue;
7556890b 238 if (f->output_data.x->id == id)
88766961 239 return f;
bd3a4da2 240 }
88766961 241 return 0;
bd3a4da2 242}
88766961
RS
243
244#endif
4dedbfe0 245\f
78589e07
RS
246/* Initialize the menu_items structure if we haven't already done so.
247 Also mark it as currently empty. */
248
249static void
250init_menu_items ()
251{
252 if (NILP (menu_items))
253 {
254 menu_items_allocated = 60;
255 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
18686d47
RS
256 }
257
86fad4ec
SM
258 if (!NILP (menu_items_inuse))
259 error ("Trying to use a menu from within a menu-entry");
260 menu_items_inuse = Qt;
78589e07
RS
261 menu_items_used = 0;
262 menu_items_n_panes = 0;
101bb4a5 263 menu_items_submenu_depth = 0;
78589e07 264}
18686d47 265
0b1f4572 266/* Call at the end of generating the data in menu_items. */
1658603c 267
78589e07
RS
268static void
269finish_menu_items ()
270{
271}
1658603c 272
86fad4ec
SM
273static Lisp_Object
274unuse_menu_items (dummy)
7448803e 275 int dummy;
86fad4ec
SM
276{
277 return menu_items_inuse = Qnil;
278}
279
78589e07
RS
280/* Call when finished using the data for the current menu
281 in menu_items. */
1658603c 282
78589e07
RS
283static void
284discard_menu_items ()
285{
286 /* Free the structure if it is especially large.
287 Otherwise, hold on to it, to save time. */
288 if (menu_items_allocated > 200)
289 {
290 menu_items = Qnil;
291 menu_items_allocated = 0;
292 }
86fad4ec 293 xassert (NILP (menu_items_inuse));
78589e07 294}
1658603c 295
101bb4a5
RS
296/* Make the menu_items vector twice as large. */
297
298static void
299grow_menu_items ()
300{
301 Lisp_Object old;
302 int old_size = menu_items_allocated;
303 old = menu_items;
304
305 menu_items_allocated *= 2;
306 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
307 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
308 old_size * sizeof (Lisp_Object));
309}
310
311/* Begin a submenu. */
312
313static void
314push_submenu_start ()
315{
316 if (menu_items_used + 1 > menu_items_allocated)
317 grow_menu_items ();
318
319 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
320 menu_items_submenu_depth++;
321}
322
323/* End a submenu. */
324
325static void
326push_submenu_end ()
327{
328 if (menu_items_used + 1 > menu_items_allocated)
329 grow_menu_items ();
330
331 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
332 menu_items_submenu_depth--;
333}
334
fcaa7665
RS
335/* Indicate boundary between left and right. */
336
337static void
338push_left_right_boundary ()
339{
340 if (menu_items_used + 1 > menu_items_allocated)
341 grow_menu_items ();
342
343 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
344}
345
c9e60620 346/* Start a new menu pane in menu_items.
78589e07 347 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
1658603c 348
78589e07
RS
349static void
350push_menu_pane (name, prefix_vec)
351 Lisp_Object name, prefix_vec;
352{
353 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
101bb4a5 354 grow_menu_items ();
dcfdbac7 355
101bb4a5
RS
356 if (menu_items_submenu_depth == 0)
357 menu_items_n_panes++;
78589e07
RS
358 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
359 XVECTOR (menu_items)->contents[menu_items_used++] = name;
360 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
361}
dcfdbac7 362
3427a3db
GM
363/* Push one menu item into the current pane. NAME is the string to
364 display. ENABLE if non-nil means this item can be selected. KEY
365 is the key generated by choosing this item, or nil if this item
366 doesn't really have a definition. DEF is the definition of this
367 item. EQUIV is the textual description of the keyboard equivalent
368 for this item (or nil if none). TYPE is the type of this menu
369 item, one of nil, `toggle' or `radio'. */
18686d47 370
78589e07 371static void
850df50b
GM
372push_menu_item (name, enable, key, def, equiv, type, selected, help)
373 Lisp_Object name, enable, key, def, equiv, type, selected, help;
78589e07
RS
374{
375 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
101bb4a5 376 grow_menu_items ();
088831f6 377
78589e07
RS
378 XVECTOR (menu_items)->contents[menu_items_used++] = name;
379 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
380 XVECTOR (menu_items)->contents[menu_items_used++] = key;
381 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
a352a815 382 XVECTOR (menu_items)->contents[menu_items_used++] = def;
3427a3db
GM
383 XVECTOR (menu_items)->contents[menu_items_used++] = type;
384 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
850df50b 385 XVECTOR (menu_items)->contents[menu_items_used++] = help;
78589e07
RS
386}
387\f
78589e07 388/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
101bb4a5
RS
389 and generate menu panes for them in menu_items.
390 If NOTREAL is nonzero,
391 don't bother really computing whether an item is enabled. */
18686d47 392
78589e07 393static void
101bb4a5 394keymap_panes (keymaps, nmaps, notreal)
78589e07
RS
395 Lisp_Object *keymaps;
396 int nmaps;
101bb4a5 397 int notreal;
18686d47 398{
78589e07 399 int mapno;
18686d47 400
78589e07 401 init_menu_items ();
18686d47 402
78589e07
RS
403 /* Loop over the given keymaps, making a pane for each map.
404 But don't make a pane that is empty--ignore that map instead.
405 P is the number of panes we have made so far. */
406 for (mapno = 0; mapno < nmaps; mapno++)
488205c0 407 single_keymap_panes (keymaps[mapno],
5ee707b8 408 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
78589e07
RS
409
410 finish_menu_items ();
411}
412
413/* This is a recursive subroutine of keymap_panes.
414 It handles one keymap, KEYMAP.
415 The other arguments are passed along
101bb4a5 416 or point to local variables of the previous function.
de57a39c
RS
417 If NOTREAL is nonzero, only check for equivalent key bindings, don't
418 evaluate expressions in menu items and don't make any menu.
98381e42
RS
419
420 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
78589e07
RS
421
422static void
98381e42 423single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
78589e07
RS
424 Lisp_Object keymap;
425 Lisp_Object pane_name;
426 Lisp_Object prefix;
101bb4a5 427 int notreal;
98381e42 428 int maxdepth;
78589e07 429{
de57a39c
RS
430 Lisp_Object pending_maps = Qnil;
431 Lisp_Object tail, item;
432 struct gcpro gcpro1, gcpro2;
433 int notbuttons = 0;
78589e07 434
98381e42
RS
435 if (maxdepth <= 0)
436 return;
437
78589e07
RS
438 push_menu_pane (pane_name, prefix);
439
de57a39c
RS
440#ifndef HAVE_BOXES
441 /* Remember index for first item in this pane so we can go back and
442 add a prefix when (if) we see the first button. After that, notbuttons
443 is set to 0, to mark that we have seen a button and all non button
444 items need a prefix. */
445 notbuttons = menu_items_used;
446#endif
447
8e713be6 448 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
18686d47 449 {
de57a39c
RS
450 GCPRO2 (keymap, pending_maps);
451 /* Look at each key binding, and if it is a menu item add it
452 to this menu. */
8e713be6 453 item = XCAR (tail);
b5bb2705 454 if (CONSP (item))
8e713be6 455 single_menu_item (XCAR (item), XCDR (item),
de57a39c 456 &pending_maps, notreal, maxdepth, &notbuttons);
b5bb2705 457 else if (VECTORP (item))
78589e07
RS
458 {
459 /* Loop over the char values represented in the vector. */
460 int len = XVECTOR (item)->size;
461 int c;
462 for (c = 0; c < len; c++)
463 {
464 Lisp_Object character;
33b43fa6 465 XSETFASTINT (character, c);
de57a39c
RS
466 single_menu_item (character, XVECTOR (item)->contents[c],
467 &pending_maps, notreal, maxdepth, &notbuttons);
78589e07 468 }
18686d47 469 }
de57a39c 470 UNGCPRO;
18686d47 471 }
78589e07
RS
472
473 /* Process now any submenus which want to be panes at this level. */
474 while (!NILP (pending_maps))
475 {
101bb4a5 476 Lisp_Object elt, eltcdr, string;
78589e07 477 elt = Fcar (pending_maps);
8e713be6
KR
478 eltcdr = XCDR (elt);
479 string = XCAR (eltcdr);
101bb4a5
RS
480 /* We no longer discard the @ from the beginning of the string here.
481 Instead, we do this in xmenu_show. */
482 single_keymap_panes (Fcar (elt), string,
8e713be6 483 XCDR (eltcdr), notreal, maxdepth - 1);
78589e07
RS
484 pending_maps = Fcdr (pending_maps);
485 }
18686d47 486}
78589e07 487\f
de57a39c
RS
488/* This is a subroutine of single_keymap_panes that handles one
489 keymap entry.
177c0ea7 490 KEY is a key in a keymap and ITEM is its binding.
de57a39c
RS
491 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
492 separate panes.
493 If NOTREAL is nonzero, only check for equivalent key bindings, don't
494 evaluate expressions in menu items and don't make any menu.
495 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
496 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
497 buttons. It points to variable notbuttons in single_keymap_panes,
498 which keeps track of if we have seen a button in this menu or not. */
499
500static void
501single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
502 notbuttons_ptr)
503 Lisp_Object key, item;
504 Lisp_Object *pending_maps_ptr;
505 int maxdepth, notreal;
506 int *notbuttons_ptr;
507{
faa935b6 508 Lisp_Object map, item_string, enabled;
de57a39c
RS
509 struct gcpro gcpro1, gcpro2;
510 int res;
177c0ea7 511
de57a39c
RS
512 /* Parse the menu item and leave the result in item_properties. */
513 GCPRO2 (key, item);
514 res = parse_menu_item (item, notreal, 0);
515 UNGCPRO;
516 if (!res)
517 return; /* Not a menu item. */
518
519 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
177c0ea7 520
de57a39c
RS
521 if (notreal)
522 {
523 /* We don't want to make a menu, just traverse the keymaps to
524 precompute equivalent key bindings. */
525 if (!NILP (map))
526 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
527 return;
528 }
529
530 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
177c0ea7 531 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
de57a39c 532
d5db4077 533 if (!NILP (map) && SREF (item_string, 0) == '@')
de57a39c
RS
534 {
535 if (!NILP (enabled))
536 /* An enabled separate pane. Remember this to handle it later. */
537 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
538 *pending_maps_ptr);
539 return;
540 }
541
542#ifndef HAVE_BOXES
543 /* Simulate radio buttons and toggle boxes by putting a prefix in
544 front of them. */
545 {
546 Lisp_Object prefix = Qnil;
547 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
548 if (!NILP (type))
549 {
550 Lisp_Object selected
551 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
552
553 if (*notbuttons_ptr)
554 /* The first button. Line up previous items in this menu. */
555 {
556 int index = *notbuttons_ptr; /* Index for first item this menu. */
557 int submenu = 0;
558 Lisp_Object tem;
559 while (index < menu_items_used)
560 {
561 tem
562 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
563 if (NILP (tem))
564 {
565 index++;
566 submenu++; /* Skip sub menu. */
567 }
568 else if (EQ (tem, Qlambda))
569 {
570 index++;
571 submenu--; /* End sub menu. */
572 }
573 else if (EQ (tem, Qt))
574 index += 3; /* Skip new pane marker. */
575 else if (EQ (tem, Qquote))
576 index++; /* Skip a left, right divider. */
577 else
578 {
d5db4077
KR
579 if (!submenu && SREF (tem, 0) != '\0'
580 && SREF (tem, 0) != '-')
de57a39c
RS
581 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
582 = concat2 (build_string (" "), tem);
583 index += MENU_ITEMS_ITEM_LENGTH;
584 }
585 }
586 *notbuttons_ptr = 0;
587 }
588
589 /* Calculate prefix, if any, for this item. */
590 if (EQ (type, QCtoggle))
591 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
592 else if (EQ (type, QCradio))
593 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
594 }
595 /* Not a button. If we have earlier buttons, then we need a prefix. */
d5db4077
KR
596 else if (!*notbuttons_ptr && SREF (item_string, 0) != '\0'
597 && SREF (item_string, 0) != '-')
de57a39c
RS
598 prefix = build_string (" ");
599
600 if (!NILP (prefix))
601 item_string = concat2 (prefix, item_string);
602 }
603#endif /* not HAVE_BOXES */
177c0ea7 604
488dd4c4 605#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
de57a39c
RS
606 if (!NILP(map))
607 /* Indicate visually that this is a submenu. */
608 item_string = concat2 (item_string, build_string (" >"));
609#endif
610
611 push_menu_item (item_string, enabled, key,
612 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
3427a3db
GM
613 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
614 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
850df50b
GM
615 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
616 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
de57a39c 617
488dd4c4 618#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
de57a39c
RS
619 /* Display a submenu using the toolkit. */
620 if (! (NILP (map) || NILP (enabled)))
621 {
622 push_submenu_start ();
623 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
624 push_submenu_end ();
625 }
626#endif
627}
628\f
8e6208c5 629/* Push all the panes and items of a menu described by the
78589e07
RS
630 alist-of-alists MENU.
631 This handles old-fashioned calls to x-popup-menu. */
18686d47 632
78589e07
RS
633static void
634list_of_panes (menu)
18686d47 635 Lisp_Object menu;
18686d47 636{
78589e07
RS
637 Lisp_Object tail;
638
639 init_menu_items ();
640
641 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
642 {
643 Lisp_Object elt, pane_name, pane_data;
644 elt = Fcar (tail);
645 pane_name = Fcar (elt);
b7826503 646 CHECK_STRING (pane_name);
78589e07
RS
647 push_menu_pane (pane_name, Qnil);
648 pane_data = Fcdr (elt);
b7826503 649 CHECK_CONS (pane_data);
78589e07
RS
650 list_of_items (pane_data);
651 }
652
653 finish_menu_items ();
654}
655
656/* Push the items in a single pane defined by the alist PANE. */
657
658static void
659list_of_items (pane)
660 Lisp_Object pane;
661{
662 Lisp_Object tail, item, item1;
663
664 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
665 {
666 item = Fcar (tail);
667 if (STRINGP (item))
850df50b 668 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
fcaa7665
RS
669 else if (NILP (item))
670 push_left_right_boundary ();
78589e07
RS
671 else
672 {
b7826503 673 CHECK_CONS (item);
78589e07 674 item1 = Fcar (item);
b7826503 675 CHECK_STRING (item1);
850df50b 676 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
78589e07
RS
677 }
678 }
679}
680\f
a894238b
JD
681#ifdef HAVE_X_WINDOWS
682/* Return the mouse position in *X and *Y. The coordinates are window
683 relative for the edit window in frame F.
684 This is for Fx_popup_menu. The mouse_position_hook can not
685 be used for X, as it returns window relative coordinates
686 for the window where the mouse is in. This could be the menu bar,
687 the scroll bar or the edit window. Fx_popup_menu needs to be
688 sure it is the edit window. */
689static void
690mouse_position_for_popup(f, x, y)
691 FRAME_PTR f;
692 int *x;
693 int *y;
694{
695 Window root, dummy_window;
696 int dummy;
697
698 BLOCK_INPUT;
177c0ea7 699
a894238b
JD
700 XQueryPointer (FRAME_X_DISPLAY (f),
701 DefaultRootWindow (FRAME_X_DISPLAY (f)),
702
703 /* The root window which contains the pointer. */
704 &root,
705
706 /* Window pointer is on, not used */
707 &dummy_window,
708
709 /* The position on that root window. */
710 x, y,
711
712 /* x/y in dummy_window coordinates, not used. */
713 &dummy, &dummy,
714
715 /* Modifier keys and pointer buttons, about which
716 we don't care. */
717 (unsigned int *) &dummy);
718
719 UNBLOCK_INPUT;
720
721 /* xmenu_show expects window coordinates, not root window
722 coordinates. Translate. */
723 *x -= f->output_data.x->left_pos
724 + FRAME_OUTER_TO_INNER_DIFF_X (f);
725 *y -= f->output_data.x->top_pos
726 + FRAME_OUTER_TO_INNER_DIFF_Y (f);
727}
728
729#endif /* HAVE_X_WINDOWS */
730
540e52d1 731DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
f6e34773 732 doc: /* Pop up a deck-of-cards menu and return user's selection.
228299fa
GM
733POSITION is a position specification. This is either a mouse button event
734or a list ((XOFFSET YOFFSET) WINDOW)
735where XOFFSET and YOFFSET are positions in pixels from the top left
736corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)
737This controls the position of the center of the first line
738in the first pane of the menu, not the top left of the menu as a whole.
739If POSITION is t, it means to use the current mouse position.
740
741MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
742The menu items come from key bindings that have a menu string as well as
743a definition; actually, the "definition" in such a key binding looks like
744\(STRING . REAL-DEFINITION). To give the menu a title, put a string into
745the keymap as a top-level element.
746
747If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
748Otherwise, REAL-DEFINITION should be a valid key binding definition.
749
750You can also use a list of keymaps as MENU.
751 Then each keymap makes a separate pane.
752When MENU is a keymap or a list of keymaps, the return value
753is a list of events.
754
755Alternatively, you can specify a menu of multiple panes
756 with a list of the form (TITLE PANE1 PANE2...),
757where each pane is a list of form (TITLE ITEM1 ITEM2...).
758Each ITEM is normally a cons cell (STRING . VALUE);
759but a string can appear as an item--that makes a nonselectable line
760in the menu.
761With this form of menu, the return value is VALUE from the chosen item.
762
763If POSITION is nil, don't display the menu at all, just precalculate the
7ee72033
MB
764cached information about equivalent key sequences. */)
765 (position, menu)
78589e07
RS
766 Lisp_Object position, menu;
767{
18686d47 768 Lisp_Object keymap, tem;
6bbd7a29 769 int xpos = 0, ypos = 0;
78589e07
RS
770 Lisp_Object title;
771 char *error_name;
772 Lisp_Object selection;
cc45fb40 773 FRAME_PTR f = NULL;
78589e07
RS
774 Lisp_Object x, y, window;
775 int keymaps = 0;
9685a93f 776 int for_click = 0;
86fad4ec 777 int specpdl_count = SPECPDL_INDEX ();
78589e07
RS
778 struct gcpro gcpro1;
779
1e659e4c 780#ifdef HAVE_MENUS
78589e07
RS
781 if (! NILP (position))
782 {
488dd4c4 783 int get_current_pos_p = 0;
101bb4a5
RS
784 check_x ();
785
78589e07 786 /* Decode the first argument: find the window and the coordinates. */
9572375b 787 if (EQ (position, Qt)
2b06561a
DL
788 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
789 || EQ (XCAR (position), Qtool_bar))))
78589e07 790 {
488dd4c4
JD
791 get_current_pos_p = 1;
792 }
793 else
794 {
795 tem = Fcar (position);
796 if (CONSP (tem))
797 {
798 window = Fcar (Fcdr (position));
799 x = Fcar (tem);
800 y = Fcar (Fcdr (tem));
801 }
802 else
803 {
804 for_click = 1;
805 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
806 window = Fcar (tem); /* POSN_WINDOW (tem) */
807 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
808 x = Fcar (tem);
809 y = Fcdr (tem);
810 }
811
812 /* If a click happens in an external tool bar or a detached
813 tool bar, x and y is NIL. In that case, use the current
814 mouse position. This happens for the help button in the
815 tool bar. Ideally popup-menu should pass NIL to
816 this function, but it doesn't. */
817 if (NILP (x) && NILP (y))
818 get_current_pos_p = 1;
819 }
820
821 if (get_current_pos_p)
822 {
78589e07 823 /* Use the mouse's current position. */
b404828f 824 FRAME_PTR new_f = SELECTED_FRAME ();
a894238b
JD
825#ifdef HAVE_X_WINDOWS
826 /* Can't use mouse_position_hook for X since it returns
827 coordinates relative to the window the mouse is in,
828 we need coordinates relative to the edit widget always. */
829 if (new_f != 0)
830 {
831 int cur_x, cur_y;
832
833 mouse_position_for_popup (new_f, &cur_x, &cur_y);
834 /* cur_x/y may be negative, so use make_number. */
835 x = make_number (cur_x);
836 y = make_number (cur_y);
837 }
177c0ea7 838
a894238b
JD
839#else /* not HAVE_X_WINDOWS */
840 Lisp_Object bar_window;
841 enum scroll_bar_part part;
842 unsigned long time;
843
844 if (mouse_position_hook)
845 (*mouse_position_hook) (&new_f, 1, &bar_window,
846 &part, &x, &y, &time);
847#endif /* not HAVE_X_WINDOWS */
848
849 if (new_f != 0)
850 XSETFRAME (window, new_f);
851 else
852 {
853 window = selected_window;
854 XSETFASTINT (x, 0);
855 XSETFASTINT (y, 0);
856 }
78589e07 857 }
78589e07 858
b7826503
PJ
859 CHECK_NUMBER (x);
860 CHECK_NUMBER (y);
78589e07
RS
861
862 /* Decode where to put the menu. */
863
b5bb2705 864 if (FRAMEP (window))
78589e07
RS
865 {
866 f = XFRAME (window);
78589e07
RS
867 xpos = 0;
868 ypos = 0;
869 }
b5bb2705 870 else if (WINDOWP (window))
78589e07 871 {
b7826503 872 CHECK_LIVE_WINDOW (window);
78589e07
RS
873 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
874
cc45fb40 875 xpos = (FONT_WIDTH (FRAME_FONT (f))
99d3fac7 876 * XFASTINT (XWINDOW (window)->left));
cc45fb40 877 ypos = (FRAME_LINE_HEIGHT (f)
99d3fac7 878 * XFASTINT (XWINDOW (window)->top));
78589e07
RS
879 }
880 else
881 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
882 but I don't want to make one now. */
b7826503 883 CHECK_WINDOW (window);
78589e07
RS
884
885 xpos += XINT (x);
886 ypos += XINT (y);
887 }
bfc524bc 888 Vmenu_updating_frame = Qnil;
1e659e4c 889#endif /* HAVE_MENUS */
78589e07 890
86fad4ec 891 record_unwind_protect (unuse_menu_items, Qnil);
78589e07
RS
892 title = Qnil;
893 GCPRO1 (title);
894
895 /* Decode the menu items from what was specified. */
18686d47 896
02067692
SM
897 keymap = get_keymap (menu, 0, 0);
898 if (CONSP (keymap))
18686d47
RS
899 {
900 /* We were given a keymap. Extract menu info from the keymap. */
901 Lisp_Object prompt;
18686d47 902
78589e07 903 /* Extract the detailed info to make one pane. */
101bb4a5 904 keymap_panes (&menu, 1, NILP (position));
78589e07 905
18686d47
RS
906 /* Search for a string appearing directly as an element of the keymap.
907 That string is the title of the menu. */
5ee707b8 908 prompt = Fkeymap_prompt (keymap);
4f9ad016
RS
909 if (NILP (title) && !NILP (prompt))
910 title = prompt;
18686d47 911
78589e07
RS
912 /* Make that be the pane title of the first pane. */
913 if (!NILP (prompt) && menu_items_n_panes >= 0)
914 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
915
916 keymaps = 1;
18686d47 917 }
02067692 918 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
18686d47
RS
919 {
920 /* We were given a list of keymaps. */
18686d47
RS
921 int nmaps = XFASTINT (Flength (menu));
922 Lisp_Object *maps
923 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
924 int i;
78589e07
RS
925
926 title = Qnil;
18686d47
RS
927
928 /* The first keymap that has a prompt string
929 supplies the menu title. */
b5bb2705 930 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
18686d47 931 {
78589e07
RS
932 Lisp_Object prompt;
933
02067692 934 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
18686d47 935
5ee707b8 936 prompt = Fkeymap_prompt (keymap);
78589e07
RS
937 if (NILP (title) && !NILP (prompt))
938 title = prompt;
18686d47
RS
939 }
940
941 /* Extract the detailed info to make one pane. */
101bb4a5 942 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
943
944 /* Make the title be the pane title of the first pane. */
945 if (!NILP (title) && menu_items_n_panes >= 0)
946 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
947
948 keymaps = 1;
18686d47
RS
949 }
950 else
951 {
952 /* We were given an old-fashioned menu. */
78589e07 953 title = Fcar (menu);
b7826503 954 CHECK_STRING (title);
18686d47 955
78589e07 956 list_of_panes (Fcdr (menu));
18686d47 957
78589e07
RS
958 keymaps = 0;
959 }
177c0ea7 960
86fad4ec
SM
961 unbind_to (specpdl_count, Qnil);
962
78589e07 963 if (NILP (position))
18686d47 964 {
78589e07
RS
965 discard_menu_items ();
966 UNGCPRO;
967 return Qnil;
18686d47
RS
968 }
969
1e659e4c 970#ifdef HAVE_MENUS
78589e07
RS
971 /* Display them in a menu. */
972 BLOCK_INPUT;
18686d47 973
673a6211 974 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
975 keymaps, title, &error_name);
976 UNBLOCK_INPUT;
18686d47 977
78589e07 978 discard_menu_items ();
18686d47 979
78589e07 980 UNGCPRO;
1e659e4c 981#endif /* HAVE_MENUS */
18686d47 982
78589e07
RS
983 if (error_name) error (error_name);
984 return selection;
18686d47 985}
165e1749 986
1e659e4c
RS
987#ifdef HAVE_MENUS
988
540e52d1 989DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
f6e34773 990 doc: /* Pop up a dialog box and return user's selection.
228299fa
GM
991POSITION specifies which frame to use.
992This is normally a mouse button event or a window or frame.
993If POSITION is t, it means to use the frame the mouse is on.
994The dialog box appears in the middle of the specified frame.
995
996CONTENTS specifies the alternatives to display in the dialog box.
997It is a list of the form (TITLE ITEM1 ITEM2...).
998Each ITEM is a cons cell (STRING . VALUE).
999The return value is VALUE from the chosen item.
1000
1001An ITEM may also be just a string--that makes a nonselectable item.
1002An ITEM may also be nil--that means to put all preceding items
1003on the left of the dialog box and all following items on the right.
7ee72033
MB
1004\(By default, approximately half appear on each side.) */)
1005 (position, contents)
99fe880d 1006 Lisp_Object position, contents;
165e1749 1007{
cc45fb40 1008 FRAME_PTR f = NULL;
99fe880d 1009 Lisp_Object window;
165e1749
FP
1010
1011 check_x ();
1012
99fe880d 1013 /* Decode the first argument: find the window or frame to use. */
88d4f6ec 1014 if (EQ (position, Qt)
2b06561a
DL
1015 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1016 || EQ (XCAR (position), Qtool_bar))))
165e1749 1017 {
b14db4d7 1018#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 1019 /* Use the mouse's current position. */
b404828f 1020 FRAME_PTR new_f = SELECTED_FRAME ();
99fe880d 1021 Lisp_Object bar_window;
cc45fb40 1022 enum scroll_bar_part part;
99fe880d
RS
1023 unsigned long time;
1024 Lisp_Object x, y;
165e1749 1025
46b657e2 1026 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 1027
99fe880d 1028 if (new_f != 0)
a39f0477 1029 XSETFRAME (window, new_f);
99fe880d
RS
1030 else
1031 window = selected_window;
b14db4d7 1032#endif
88d4f6ec 1033 window = selected_window;
99fe880d
RS
1034 }
1035 else if (CONSP (position))
1036 {
1037 Lisp_Object tem;
1038 tem = Fcar (position);
b5bb2705 1039 if (CONSP (tem))
99fe880d 1040 window = Fcar (Fcdr (position));
80670155
RS
1041 else
1042 {
99fe880d
RS
1043 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1044 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 1045 }
165e1749 1046 }
99fe880d
RS
1047 else if (WINDOWP (position) || FRAMEP (position))
1048 window = position;
a9be6839
RS
1049 else
1050 window = Qnil;
165e1749 1051
99fe880d 1052 /* Decode where to put the menu. */
165e1749 1053
b5bb2705 1054 if (FRAMEP (window))
99fe880d 1055 f = XFRAME (window);
b5bb2705 1056 else if (WINDOWP (window))
165e1749 1057 {
b7826503 1058 CHECK_LIVE_WINDOW (window);
99fe880d 1059 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 1060 }
99fe880d
RS
1061 else
1062 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1063 but I don't want to make one now. */
b7826503 1064 CHECK_WINDOW (window);
165e1749 1065
488dd4c4 1066#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
99fe880d
RS
1067 /* Display a menu with these alternatives
1068 in the middle of frame F. */
1069 {
1070 Lisp_Object x, y, frame, newpos;
a39f0477
KH
1071 XSETFRAME (frame, f);
1072 XSETINT (x, x_pixel_width (f) / 2);
1073 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
1074 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1075
1076 return Fx_popup_menu (newpos,
1077 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1078 }
1079#else
1080 {
1081 Lisp_Object title;
1082 char *error_name;
1083 Lisp_Object selection;
86fad4ec 1084 int specpdl_count = SPECPDL_INDEX ();
165e1749 1085
99fe880d
RS
1086 /* Decode the dialog items from what was specified. */
1087 title = Fcar (contents);
b7826503 1088 CHECK_STRING (title);
86fad4ec 1089 record_unwind_protect (unuse_menu_items, Qnil);
165e1749 1090
99fe880d 1091 list_of_panes (Fcons (contents, Qnil));
165e1749 1092
99fe880d
RS
1093 /* Display them in a dialog box. */
1094 BLOCK_INPUT;
f8c2630d 1095 selection = xdialog_show (f, 0, title, &error_name);
99fe880d 1096 UNBLOCK_INPUT;
165e1749 1097
86fad4ec 1098 unbind_to (specpdl_count, Qnil);
99fe880d
RS
1099 discard_menu_items ();
1100
1101 if (error_name) error (error_name);
1102 return selection;
1103 }
7464b131 1104#endif
392d3f4b 1105}
78589e07 1106\f
488dd4c4 1107#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18686d47 1108
4dedbfe0 1109/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2 1110 popped down (deactivated). This is used for x-popup-menu
f02cac82
RS
1111 and x-popup-dialog; it is not used for the menu bar.
1112
1113 If DO_TIMERS is nonzero, run timers.
c98fcf4b 1114
2e2b8e22 1115 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1116 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1117
488dd4c4 1118#ifdef USE_X_TOOLKIT
f02cac82
RS
1119static void
1120popup_get_selection (initial_event, dpyinfo, id, do_timers)
4dedbfe0 1121 XEvent *initial_event;
aa669def 1122 struct x_display_info *dpyinfo;
2e2b8e22 1123 LWLIB_ID id;
f02cac82 1124 int do_timers;
78589e07 1125{
4dedbfe0 1126 XEvent event;
f02cac82 1127
8b806a85
JD
1128 while (popup_activated_flag)
1129 {
1130 /* If we have no events to run, consider timers. */
1131 if (do_timers && !XtAppPending (Xt_app_con))
1132 timer_check (1);
aa669def 1133
8b806a85
JD
1134 if (initial_event)
1135 {
1136 event = *initial_event;
1137 initial_event = 0;
1138 }
1139 else
1140 XtAppNextEvent (Xt_app_con, &event);
78589e07 1141
10624005 1142 /* Make sure we don't consider buttons grabbed after menu goes.
8b806a85
JD
1143 And make sure to deactivate for any ButtonRelease,
1144 even if XtDispatchEvent doesn't do that. */
1145 if (event.type == ButtonRelease
1146 && dpyinfo->display == event.xbutton.display)
10624005
KH
1147 {
1148 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
177c0ea7 1149#ifdef USE_MOTIF /* Pretending that the event came from a
8b806a85
JD
1150 Btn1Down seems the only way to convince Motif to
1151 activate its callbacks; setting the XmNmenuPost
1152 isn't working. --marcus@sysc.pdx.edu. */
1153 event.xbutton.button = 1;
1154 /* Motif only pops down menus when no Ctrl, Alt or Mod
1155 key is pressed and the button is released. So reset key state
1156 so Motif thinks this is the case. */
1157 event.xbutton.state = 0;
c8b5aa3d 1158#endif
10624005 1159 }
2e2b8e22 1160 /* If the user presses a key, deactivate the menu.
8b806a85
JD
1161 The user is likely to do that if we get wedged.
1162 This is mostly for Lucid, Motif pops down the menu on ESC. */
2e2b8e22 1163 else if (event.type == KeyPress
10624005 1164 && dpyinfo->display == event.xbutton.display)
8b806a85
JD
1165 {
1166 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1167 if (!IsModifierKey (keysym))
1168 popup_activated_flag = 0;
1169 }
177c0ea7 1170
8b806a85 1171 x_dispatch_event (&event, event.xany.display);
aa669def 1172 }
78589e07
RS
1173}
1174
488dd4c4
JD
1175#endif /* USE_X_TOOLKIT */
1176
1177#ifdef USE_GTK
1178/* Loop util popup_activated_flag is set to zero in a callback.
1179 Used for popup menus and dialogs. */
1180static void
1181popup_widget_loop ()
1182{
1183 ++popup_activated_flag;
1184
1185 /* Process events in the Gtk event loop until done. */
1186 while (popup_activated_flag)
1187 {
1188 gtk_main_iteration ();
1189 }
1190}
1191#endif
1192
88766961
RS
1193/* Activate the menu bar of frame F.
1194 This is called from keyboard.c when it gets the
3b8f9651 1195 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
88766961
RS
1196
1197 To activate the menu bar, we use the X button-press event
ac78b144 1198 that was saved in saved_menu_event.
88766961
RS
1199 That makes the toolkit do its thing.
1200
1201 But first we recompute the menu bar contents (the whole tree).
1202
1203 The reason for saving the button event until here, instead of
1204 passing it to the toolkit right away, is that we can safely
1205 execute Lisp code. */
177c0ea7 1206
dfcf069d 1207void
88766961
RS
1208x_activate_menubar (f)
1209 FRAME_PTR f;
1210{
ac78b144 1211 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1212 return;
1213
177c0ea7 1214#ifdef USE_GTK
488dd4c4
JD
1215 if (! xg_win_to_widget (f->output_data.x->saved_menu_event->xany.window))
1216 return;
1217#endif
177c0ea7 1218
a9be6839 1219 set_frame_menubar (f, 0, 1);
88766961 1220 BLOCK_INPUT;
488dd4c4
JD
1221#ifdef USE_GTK
1222 XPutBackEvent (f->output_data.x->display_info->display,
1223 f->output_data.x->saved_menu_event);
1224 popup_activated_flag = 1;
1225#else
86fad4ec 1226 XtDispatchEvent (f->output_data.x->saved_menu_event);
488dd4c4 1227#endif
88766961 1228 UNBLOCK_INPUT;
a9be6839 1229#ifdef USE_MOTIF
745c34fb
RS
1230 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1231 pending_menu_activation = 1;
a9be6839 1232#endif
177c0ea7 1233
88766961 1234 /* Ignore this if we get it a second time. */
ac78b144 1235 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1236}
1237
c98fcf4b 1238/* Detect if a dialog or menu has been posted. */
aa669def 1239
4dedbfe0
PR
1240int
1241popup_activated ()
1242{
1243 return popup_activated_flag;
1244}
1245
4dedbfe0
PR
1246/* This callback is invoked when the user selects a menubar cascade
1247 pushbutton, but before the pulldown menu is posted. */
78589e07 1248
488dd4c4 1249#ifndef USE_GTK
78589e07 1250static void
4dedbfe0 1251popup_activate_callback (widget, id, client_data)
78589e07
RS
1252 Widget widget;
1253 LWLIB_ID id;
1254 XtPointer client_data;
1255{
4dedbfe0 1256 popup_activated_flag = 1;
7555d825 1257}
488dd4c4 1258#endif
7555d825
GM
1259
1260/* This callback is invoked when a dialog or menu is finished being
1261 used and has been unposted. */
1262
488dd4c4
JD
1263#ifdef USE_GTK
1264static void
1265popup_deactivate_callback (widget, client_data)
1266 GtkWidget *widget;
1267 gpointer client_data;
1268{
1269 popup_activated_flag = 0;
1270}
1271#else
7555d825
GM
1272static void
1273popup_deactivate_callback (widget, id, client_data)
1274 Widget widget;
1275 LWLIB_ID id;
1276 XtPointer client_data;
1277{
7555d825 1278 popup_activated_flag = 0;
78589e07 1279}
488dd4c4 1280#endif
78589e07 1281
850df50b 1282
488dd4c4
JD
1283/* Function that finds the frame for WIDGET and shows the HELP text
1284 for that widget.
1285 F is the frame if known, or NULL if not known. */
1286static void
1287show_help_event (f, widget, help)
1288 FRAME_PTR f;
1289 xt_or_gtk_widget widget;
1290 Lisp_Object help;
850df50b 1291{
488dd4c4 1292 Lisp_Object frame;
850df50b 1293
850df50b 1294 if (f)
9cd50434
GM
1295 {
1296 XSETFRAME (frame, f);
1297 kbd_buffer_store_help_event (frame, help);
1298 }
850df50b
GM
1299 else
1300 {
177c0ea7 1301 /* WIDGET is the popup menu. It's parent is the frame's
850df50b 1302 widget. See which frame that is. */
488dd4c4 1303 xt_or_gtk_widget frame_widget = XtParent (widget);
850df50b
GM
1304 Lisp_Object tail;
1305
1306 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1307 {
1308 frame = XCAR (tail);
1309 if (GC_FRAMEP (frame)
1310 && (f = XFRAME (frame),
1311 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1312 break;
1313 }
850df50b 1314
9cd50434
GM
1315 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1316 }
850df50b
GM
1317}
1318
488dd4c4
JD
1319/* Callback called when menu items are highlighted/unhighlighted
1320 while moving the mouse over them. WIDGET is the menu bar or menu
1321 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1322 the data structure for the menu item, or null in case of
1323 unhighlighting. */
4dedbfe0 1324
488dd4c4
JD
1325#ifdef USE_GTK
1326void
1327menu_highlight_callback (widget, call_data)
1328 GtkWidget *widget;
1329 gpointer call_data;
1330{
1331 xg_menu_item_cb_data *cb_data;
1332 Lisp_Object help;
177c0ea7 1333
488dd4c4
JD
1334 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1335 XG_ITEM_DATA);
1336 if (! cb_data) return;
1337
1338 help = call_data ? cb_data->help : Qnil;
1339
1340 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1341 Don't show help for them, they won't appear before the
1342 popup is popped down. */
1343 if (popup_activated_flag <= 1)
1344 show_help_event (cb_data->cl_data->f, widget, help);
1345}
1346#else
1347void
1348menu_highlight_callback (widget, id, call_data)
78589e07
RS
1349 Widget widget;
1350 LWLIB_ID id;
488dd4c4
JD
1351 void *call_data;
1352{
1353 struct frame *f;
1354 Lisp_Object help;
1355
1356 widget_value *wv = (widget_value *) call_data;
1357
1358 help = wv ? wv->help : Qnil;
177c0ea7 1359
488dd4c4
JD
1360 /* Determine the frame for the help event. */
1361 f = menubar_id_to_frame (id);
1362
1363 show_help_event (f, widget, help);
1364}
1365#endif
1366
1367/* Find the menu selection and store it in the keyboard buffer.
1368 F is the frame the menu is on.
1369 MENU_BAR_ITEMS_USED is the length of VECTOR.
1370 VECTOR is an array of menu events for the whole menu.
1371 */
1372void
1373find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1374 FRAME_PTR f;
1375 int menu_bar_items_used;
1376 Lisp_Object vector;
1377 void *client_data;
78589e07 1378{
c63f6952 1379 Lisp_Object prefix, entry;
4dedbfe0
PR
1380 Lisp_Object *subprefix_stack;
1381 int submenu_depth = 0;
1382 int i;
1383
6bbd7a29 1384 entry = Qnil;
488dd4c4 1385 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
4dedbfe0
PR
1386 prefix = Qnil;
1387 i = 0;
488dd4c4
JD
1388
1389 while (i < menu_bar_items_used)
4dedbfe0 1390 {
4dedbfe0
PR
1391 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1392 {
1393 subprefix_stack[submenu_depth++] = prefix;
1394 prefix = entry;
1395 i++;
1396 }
1397 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1398 {
1399 prefix = subprefix_stack[--submenu_depth];
1400 i++;
1401 }
1402 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1403 {
4cb35c39 1404 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1405 i += MENU_ITEMS_PANE_LENGTH;
1406 }
1407 else
1408 {
4cb35c39 1409 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1410 /* The EMACS_INT cast avoids a warning. There's no problem
1411 as long as pointers have enough bits to hold small integers. */
1412 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1413 {
1414 int j;
1415 struct input_event buf;
4cb35c39 1416 Lisp_Object frame;
4dedbfe0 1417
4cb35c39 1418 XSETFRAME (frame, f);
9979315d
GM
1419 buf.kind = MENU_BAR_EVENT;
1420 buf.frame_or_window = frame;
1421 buf.arg = frame;
4dedbfe0
PR
1422 kbd_buffer_store_event (&buf);
1423
1424 for (j = 0; j < submenu_depth; j++)
1425 if (!NILP (subprefix_stack[j]))
1426 {
9979315d
GM
1427 buf.kind = MENU_BAR_EVENT;
1428 buf.frame_or_window = frame;
1429 buf.arg = subprefix_stack[j];
4dedbfe0
PR
1430 kbd_buffer_store_event (&buf);
1431 }
1432
1433 if (!NILP (prefix))
1434 {
9979315d
GM
1435 buf.kind = MENU_BAR_EVENT;
1436 buf.frame_or_window = frame;
1437 buf.arg = prefix;
4dedbfe0
PR
1438 kbd_buffer_store_event (&buf);
1439 }
1440
9979315d
GM
1441 buf.kind = MENU_BAR_EVENT;
1442 buf.frame_or_window = frame;
1443 buf.arg = entry;
4dedbfe0
PR
1444 kbd_buffer_store_event (&buf);
1445
1446 return;
1447 }
1448 i += MENU_ITEMS_ITEM_LENGTH;
1449 }
1450 }
18686d47
RS
1451}
1452
488dd4c4
JD
1453
1454#ifdef USE_GTK
1455/* Gtk calls callbacks just because we tell it what item should be
1456 selected in a radio group. If this variable is set to a non-zero
1457 value, we are creating menus and don't want callbacks right now.
1458*/
1459static int xg_crazy_callback_abort;
1460
1461/* This callback is called from the menu bar pulldown menu
1462 when the user makes a selection.
1463 Figure out what the user chose
1464 and put the appropriate events into the keyboard buffer. */
1465static void
1466menubar_selection_callback (widget, client_data)
1467 GtkWidget *widget;
1468 gpointer client_data;
1469{
1470 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1471
1472 if (xg_crazy_callback_abort)
1473 return;
1474
1475 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1476 return;
1477
1478 find_and_call_menu_selection (cb_data->cl_data->f,
1479 cb_data->cl_data->menu_bar_items_used,
1480 cb_data->cl_data->menu_bar_vector,
1481 cb_data->call_data);
1482}
1483
1484#else /* not USE_GTK */
1485
1486/* This callback is called from the menu bar pulldown menu
1487 when the user makes a selection.
1488 Figure out what the user chose
1489 and put the appropriate events into the keyboard buffer. */
1490static void
1491menubar_selection_callback (widget, id, client_data)
1492 Widget widget;
1493 LWLIB_ID id;
1494 XtPointer client_data;
1495{
1496 FRAME_PTR f;
1497
1498 f = menubar_id_to_frame (id);
1499 if (!f)
1500 return;
1501 find_and_call_menu_selection (f, f->menu_bar_items_used,
1502 f->menu_bar_vector, client_data);
1503}
1504#endif /* not USE_GTK */
1505
f9655c60
RS
1506/* Allocate a widget_value, blocking input. */
1507
1508widget_value *
1509xmalloc_widget_value ()
1510{
1511 widget_value *value;
1512
1513 BLOCK_INPUT;
1514 value = malloc_widget_value ();
1515 UNBLOCK_INPUT;
1516
1517 return value;
1518}
4dedbfe0
PR
1519
1520/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1521 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1522 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1523 must be left alone. */
1524
18686d47
RS
1525void
1526free_menubar_widget_value_tree (wv)
1527 widget_value *wv;
1528{
1529 if (! wv) return;
18686d47
RS
1530
1531 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1532
1533 if (wv->contents && (wv->contents != (widget_value*)1))
1534 {
1535 free_menubar_widget_value_tree (wv->contents);
1536 wv->contents = (widget_value *) 0xDEADBEEF;
1537 }
1538 if (wv->next)
1539 {
1540 free_menubar_widget_value_tree (wv->next);
1541 wv->next = (widget_value *) 0xDEADBEEF;
1542 }
1543 BLOCK_INPUT;
1544 free_widget_value (wv);
1545 UNBLOCK_INPUT;
1546}
4dedbfe0 1547\f
e6c87a0f 1548/* Set up data in menu_items for a menu bar item
4dedbfe0
PR
1549 whose event type is ITEM_KEY (with string ITEM_NAME)
1550 and whose contents come from the list of keymaps MAPS. */
1551
ced89c24
RS
1552static int
1553parse_single_submenu (item_key, item_name, maps)
4dedbfe0
PR
1554 Lisp_Object item_key, item_name, maps;
1555{
4dedbfe0
PR
1556 Lisp_Object length;
1557 int len;
1558 Lisp_Object *mapvec;
ced89c24 1559 int i;
71dca3e3 1560 int top_level_items = 0;
4dedbfe0
PR
1561
1562 length = Flength (maps);
1563 len = XINT (length);
1564
1565 /* Convert the list MAPS into a vector MAPVEC. */
1566 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1567 for (i = 0; i < len; i++)
1568 {
1569 mapvec[i] = Fcar (maps);
1570 maps = Fcdr (maps);
1571 }
1572
4dedbfe0
PR
1573 /* Loop over the given keymaps, making a pane for each map.
1574 But don't make a pane that is empty--ignore that map instead. */
1575 for (i = 0; i < len; i++)
71dca3e3 1576 {
e6c87a0f 1577 if (!KEYMAPP (mapvec[i]))
71dca3e3 1578 {
846e8c10
RS
1579 /* Here we have a command at top level in the menu bar
1580 as opposed to a submenu. */
71dca3e3
RS
1581 top_level_items = 1;
1582 push_menu_pane (Qnil, Qnil);
850df50b
GM
1583 push_menu_item (item_name, Qt, item_key, mapvec[i],
1584 Qnil, Qnil, Qnil, Qnil);
71dca3e3
RS
1585 }
1586 else
37dc84ff
RS
1587 {
1588 Lisp_Object prompt;
1589 prompt = Fkeymap_prompt (mapvec[i]);
1590 single_keymap_panes (mapvec[i],
1591 !NILP (prompt) ? prompt : item_name,
1592 item_key, 0, 10);
1593 }
71dca3e3 1594 }
4dedbfe0 1595
ced89c24
RS
1596 return top_level_items;
1597}
1598
1599/* Create a tree of widget_value objects
1600 representing the panes and items
1601 in menu_items starting at index START, up to index END. */
1602
1603static widget_value *
1604digest_single_submenu (start, end, top_level_items)
7448803e 1605 int start, end, top_level_items;
ced89c24
RS
1606{
1607 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1608 int i;
1609 int submenu_depth = 0;
1610 widget_value **submenu_stack;
4dedbfe0
PR
1611
1612 submenu_stack
1613 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1614 wv = xmalloc_widget_value ();
4dedbfe0
PR
1615 wv->name = "menu";
1616 wv->value = 0;
1617 wv->enabled = 1;
3427a3db 1618 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1619 wv->help = Qnil;
4dedbfe0
PR
1620 first_wv = wv;
1621 save_wv = 0;
71dca3e3 1622 prev_wv = 0;
177c0ea7 1623
37dc84ff
RS
1624 /* Loop over all panes and items made by the preceding call
1625 to parse_single_submenu and construct a tree of widget_value objects.
1626 Ignore the panes and items used by previous calls to
1627 digest_single_submenu, even though those are also in menu_items. */
ced89c24
RS
1628 i = start;
1629 while (i < end)
4dedbfe0
PR
1630 {
1631 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1632 {
1633 submenu_stack[submenu_depth++] = save_wv;
1634 save_wv = prev_wv;
1635 prev_wv = 0;
1636 i++;
1637 }
1638 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1639 {
1640 prev_wv = save_wv;
1641 save_wv = submenu_stack[--submenu_depth];
1642 i++;
1643 }
1644 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1645 && submenu_depth != 0)
1646 i += MENU_ITEMS_PANE_LENGTH;
1647 /* Ignore a nil in the item list.
1648 It's meaningful only for dialog boxes. */
1649 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1650 i += 1;
1651 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1652 {
1653 /* Create a new pane. */
1654 Lisp_Object pane_name, prefix;
1655 char *pane_string;
177c0ea7 1656
4dedbfe0
PR
1657 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1658 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
177c0ea7 1659
703dc2a8
KH
1660#ifndef HAVE_MULTILINGUAL_MENU
1661 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
4c329aa8 1662 {
cc45fb40 1663 pane_name = ENCODE_SYSTEM (pane_name);
4c329aa8
GM
1664 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1665 }
703dc2a8 1666#endif
4dedbfe0 1667 pane_string = (NILP (pane_name)
d5db4077 1668 ? "" : (char *) SDATA (pane_name));
4dedbfe0
PR
1669 /* If there is just one top-level pane, put all its items directly
1670 under the top-level menu. */
1671 if (menu_items_n_panes == 1)
1672 pane_string = "";
1673
1674 /* If the pane has a meaningful name,
1675 make the pane a top-level menu item
1676 with its items as a submenu beneath it. */
1677 if (strcmp (pane_string, ""))
1678 {
f7fab165 1679 wv = xmalloc_widget_value ();
4dedbfe0
PR
1680 if (save_wv)
1681 save_wv->next = wv;
1682 else
1683 first_wv->contents = wv;
1684 wv->name = pane_string;
ffcb5a51
RS
1685 /* Ignore the @ that means "separate pane".
1686 This is a kludge, but this isn't worth more time. */
1687 if (!NILP (prefix) && wv->name[0] == '@')
4dedbfe0
PR
1688 wv->name++;
1689 wv->value = 0;
1690 wv->enabled = 1;
3427a3db 1691 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1692 wv->help = Qnil;
4dedbfe0
PR
1693 }
1694 save_wv = wv;
1695 prev_wv = 0;
1696 i += MENU_ITEMS_PANE_LENGTH;
1697 }
1698 else
1699 {
1700 /* Create a new item within current pane. */
3427a3db 1701 Lisp_Object item_name, enable, descrip, def, type, selected;
850df50b 1702 Lisp_Object help;
177c0ea7 1703
4c329aa8
GM
1704 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1705 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1706 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1707 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1708 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1709 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1710 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1711
703dc2a8 1712#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1713 if (STRING_MULTIBYTE (item_name))
4c329aa8 1714 {
cc45fb40 1715 item_name = ENCODE_SYSTEM (item_name);
4c329aa8
GM
1716 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1717 }
177c0ea7 1718
3427a3db 1719 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1720 {
cc45fb40 1721 descrip = ENCODE_SYSTEM (descrip);
4c329aa8
GM
1722 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1723 }
1724#endif /* not HAVE_MULTILINGUAL_MENU */
3427a3db 1725
f7fab165 1726 wv = xmalloc_widget_value ();
177c0ea7 1727 if (prev_wv)
4dedbfe0 1728 prev_wv->next = wv;
71dca3e3 1729 else
4dedbfe0 1730 save_wv->contents = wv;
71dca3e3 1731
d5db4077 1732 wv->name = (char *) SDATA (item_name);
4dedbfe0 1733 if (!NILP (descrip))
d5db4077 1734 wv->key = (char *) SDATA (descrip);
4dedbfe0 1735 wv->value = 0;
01d5e892
RS
1736 /* The EMACS_INT cast avoids a warning. There's no problem
1737 as long as pointers have enough bits to hold small integers. */
1738 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0 1739 wv->enabled = !NILP (enable);
177c0ea7 1740
3427a3db
GM
1741 if (NILP (type))
1742 wv->button_type = BUTTON_TYPE_NONE;
1743 else if (EQ (type, QCradio))
1744 wv->button_type = BUTTON_TYPE_RADIO;
1745 else if (EQ (type, QCtoggle))
1746 wv->button_type = BUTTON_TYPE_TOGGLE;
1747 else
1748 abort ();
1749
1750 wv->selected = !NILP (selected);
0b1f4572
RS
1751 if (! STRINGP (help))
1752 help = Qnil;
1753
1754 wv->help = help;
cc45fb40 1755
4dedbfe0
PR
1756 prev_wv = wv;
1757
1758 i += MENU_ITEMS_ITEM_LENGTH;
1759 }
1760 }
1761
71dca3e3
RS
1762 /* If we have just one "menu item"
1763 that was originally a button, return it by itself. */
1764 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1765 {
1766 wv = first_wv->contents;
1767 free_widget_value (first_wv);
1768 return wv;
1769 }
1770
4dedbfe0
PR
1771 return first_wv;
1772}
f61a541b
GM
1773\f
1774/* Recompute all the widgets of frame F, when the menu bar has been
1775 changed. Value is non-zero if widgets were updated. */
1776
1777static int
6af6cbb5 1778update_frame_menubar (f)
18686d47
RS
1779 FRAME_PTR f;
1780{
488dd4c4
JD
1781#ifdef USE_GTK
1782 return xg_update_frame_menubar (f);
1783#else
7556890b 1784 struct x_output *x = f->output_data.x;
cffa74ea 1785 int columns, rows;
177c0ea7 1786
f61a541b
GM
1787 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1788 return 0;
18686d47
RS
1789
1790 BLOCK_INPUT;
f61a541b
GM
1791 /* Save the size of the frame because the pane widget doesn't accept
1792 to resize itself. So force it. */
cffa74ea
FP
1793 columns = f->width;
1794 rows = f->height;
1795
f61a541b
GM
1796 /* Do the voodoo which means "I'm changing lots of things, don't try
1797 to refigure sizes until I'm done." */
4dedbfe0 1798 lw_refigure_widget (x->column_widget, False);
cffa74ea 1799
f61a541b
GM
1800 /* The order in which children are managed is the top to bottom
1801 order in which they are displayed in the paned window. First,
1802 remove the text-area widget. */
18686d47
RS
1803 XtUnmanageChild (x->edit_widget);
1804
f61a541b
GM
1805 /* Remove the menubar that is there now, and put up the menubar that
1806 should be there. */
1807 XtManageChild (x->menubar_widget);
1808 XtMapWidget (x->menubar_widget);
1809 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
18686d47 1810
c98fcf4b 1811 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1812 XtManageChild (x->edit_widget);
4dedbfe0 1813 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1814
1815 /* Force the pane widget to resize itself with the right values. */
1816 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
18686d47 1817 UNBLOCK_INPUT;
488dd4c4 1818#endif
f61a541b 1819 return 1;
18686d47
RS
1820}
1821
4bcdbab1
KH
1822/* Set the contents of the menubar widgets of frame F.
1823 The argument FIRST_TIME is currently ignored;
1824 it is set the first time this is called, from initialize_frame_menubar. */
1825
18686d47 1826void
88766961 1827set_frame_menubar (f, first_time, deep_p)
18686d47 1828 FRAME_PTR f;
706aa2f2 1829 int first_time;
88766961 1830 int deep_p;
18686d47 1831{
488dd4c4
JD
1832 xt_or_gtk_widget menubar_widget = f->output_data.x->menubar_widget;
1833#ifdef USE_X_TOOLKIT
1834 LWLIB_ID id;
1835#endif
faa935b6 1836 Lisp_Object items;
4d19cb8e 1837 widget_value *wv, *first_wv, *prev_wv = 0;
ced89c24
RS
1838 int i, last_i;
1839 int *submenu_start, *submenu_end;
37dc84ff 1840 int *submenu_top_level_items, *submenu_n_panes;
cc45fb40 1841
18686d47 1842
bfc524bc
RS
1843 XSETFRAME (Vmenu_updating_frame, f);
1844
488dd4c4 1845#ifdef USE_X_TOOLKIT
7556890b
RS
1846 if (f->output_data.x->id == 0)
1847 f->output_data.x->id = next_menubar_widget_id++;
1848 id = f->output_data.x->id;
488dd4c4 1849#endif
177c0ea7 1850
88766961
RS
1851 if (! menubar_widget)
1852 deep_p = 1;
745c34fb 1853 else if (pending_menu_activation && !deep_p)
a9be6839
RS
1854 deep_p = 1;
1855 /* Make the first call for any given frame always go deep. */
1856 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
1857 {
1858 deep_p = 1;
a9be6839
RS
1859 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1860 f->output_data.x->saved_menu_event->type = 0;
745c34fb 1861 }
18686d47 1862
88766961 1863 if (deep_p)
18686d47 1864 {
88766961
RS
1865 /* Make a widget-value tree representing the entire menu trees. */
1866
1867 struct buffer *prev = current_buffer;
1868 Lisp_Object buffer;
aed13378 1869 int specpdl_count = SPECPDL_INDEX ();
88766961
RS
1870 int previous_menu_items_used = f->menu_bar_items_used;
1871 Lisp_Object *previous_items
1872 = (Lisp_Object *) alloca (previous_menu_items_used
1873 * sizeof (Lisp_Object));
1874
0b1cf399
RS
1875 /* If we are making a new widget, its contents are empty,
1876 do always reinitialize them. */
1877 if (! menubar_widget)
1878 previous_menu_items_used = 0;
1879
88766961
RS
1880 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1881 specbind (Qinhibit_quit, Qt);
1882 /* Don't let the debugger step into this code
1883 because it is not reentrant. */
1884 specbind (Qdebug_on_next_call, Qnil);
1885
58226364 1886 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
86fad4ec 1887 record_unwind_protect (unuse_menu_items, Qnil);
88766961
RS
1888 if (NILP (Voverriding_local_map_menu_flag))
1889 {
1890 specbind (Qoverriding_terminal_local_map, Qnil);
1891 specbind (Qoverriding_local_map, Qnil);
1892 }
18686d47 1893
88766961 1894 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 1895
88766961 1896 /* Run the Lucid hook. */
950eaee7 1897 safe_run_hooks (Qactivate_menubar_hook);
177c0ea7 1898
88766961
RS
1899 /* If it has changed current-menubar from previous value,
1900 really recompute the menubar from the value. */
1901 if (! NILP (Vlucid_menu_bar_dirty_flag))
1902 call0 (Qrecompute_lucid_menubar);
a57634d4 1903 safe_run_hooks (Qmenu_bar_update_hook);
88766961 1904 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 1905
88766961 1906 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 1907
88766961 1908 /* Save the frame's previous menu bar contents data. */
86c04183
GM
1909 if (previous_menu_items_used)
1910 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1911 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 1912
ced89c24
RS
1913 /* Fill in menu_items with the current menu bar contents.
1914 This can evaluate Lisp code. */
88766961 1915 menu_items = f->menu_bar_vector;
86c04183 1916 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
ced89c24
RS
1917 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1918 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
37dc84ff 1919 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
ced89c24
RS
1920 submenu_top_level_items
1921 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
88766961 1922 init_menu_items ();
f04366cb 1923 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1924 {
1925 Lisp_Object key, string, maps;
1926
ced89c24
RS
1927 last_i = i;
1928
88766961
RS
1929 key = XVECTOR (items)->contents[i];
1930 string = XVECTOR (items)->contents[i + 1];
1931 maps = XVECTOR (items)->contents[i + 2];
1932 if (NILP (string))
1933 break;
1934
ced89c24
RS
1935 submenu_start[i] = menu_items_used;
1936
1937 menu_items_n_panes = 0;
1938 submenu_top_level_items[i]
1939 = parse_single_submenu (key, string, maps);
37dc84ff 1940 submenu_n_panes[i] = menu_items_n_panes;
ced89c24
RS
1941
1942 submenu_end[i] = menu_items_used;
1943 }
1944
1945 finish_menu_items ();
1946
1947 /* Convert menu_items into widget_value trees
1948 to display the menu. This cannot evaluate Lisp code. */
1949
1950 wv = xmalloc_widget_value ();
1951 wv->name = "menubar";
1952 wv->value = 0;
1953 wv->enabled = 1;
1954 wv->button_type = BUTTON_TYPE_NONE;
1955 wv->help = Qnil;
1956 first_wv = wv;
1957
1958 for (i = 0; i < last_i; i += 4)
1959 {
37dc84ff 1960 menu_items_n_panes = submenu_n_panes[i];
ced89c24
RS
1961 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1962 submenu_top_level_items[i]);
177c0ea7 1963 if (prev_wv)
88766961
RS
1964 prev_wv->next = wv;
1965 else
1966 first_wv->contents = wv;
1967 /* Don't set wv->name here; GC during the loop might relocate it. */
1968 wv->enabled = 1;
3427a3db 1969 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
1970 prev_wv = wv;
1971 }
1972
88766961 1973 set_buffer_internal_1 (prev);
8d8a3494 1974 unbind_to (specpdl_count, Qnil);
8d8a3494 1975
88766961
RS
1976 /* If there has been no change in the Lisp-level contents
1977 of the menu bar, skip redisplaying it. Just exit. */
1978
1979 for (i = 0; i < previous_menu_items_used; i++)
1980 if (menu_items_used == i
99d3fac7 1981 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 1982 break;
62555c22 1983 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
1984 {
1985 free_menubar_widget_value_tree (first_wv);
86fad4ec 1986 discard_menu_items ();
88766961
RS
1987
1988 return;
1989 }
1990
1991 /* Now GC cannot happen during the lifetime of the widget_value,
1992 so it's safe to store data from a Lisp_String. */
1993 wv = first_wv->contents;
f04366cb 1994 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1995 {
1996 Lisp_Object string;
1997 string = XVECTOR (items)->contents[i + 1];
1998 if (NILP (string))
1999 break;
d5db4077 2000 wv->name = (char *) SDATA (string);
88766961
RS
2001 wv = wv->next;
2002 }
2003
2004 f->menu_bar_vector = menu_items;
2005 f->menu_bar_items_used = menu_items_used;
86fad4ec 2006 discard_menu_items ();
4d19cb8e 2007 }
88766961
RS
2008 else
2009 {
2010 /* Make a widget-value tree containing
2011 just the top level menu bar strings. */
4d19cb8e 2012
ced89c24
RS
2013 wv = xmalloc_widget_value ();
2014 wv->name = "menubar";
2015 wv->value = 0;
2016 wv->enabled = 1;
2017 wv->button_type = BUTTON_TYPE_NONE;
2018 wv->help = Qnil;
2019 first_wv = wv;
2020
88766961 2021 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 2022 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2023 {
2024 Lisp_Object string;
2025
2026 string = XVECTOR (items)->contents[i + 1];
2027 if (NILP (string))
2028 break;
2029
f7fab165 2030 wv = xmalloc_widget_value ();
d5db4077 2031 wv->name = (char *) SDATA (string);
88766961
RS
2032 wv->value = 0;
2033 wv->enabled = 1;
3427a3db 2034 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2035 wv->help = Qnil;
fe8fa62f
RS
2036 /* This prevents lwlib from assuming this
2037 menu item is really supposed to be empty. */
2038 /* The EMACS_INT cast avoids a warning.
2039 This value just has to be different from small integers. */
2040 wv->call_data = (void *) (EMACS_INT) (-1);
88766961 2041
177c0ea7 2042 if (prev_wv)
88766961
RS
2043 prev_wv->next = wv;
2044 else
2045 first_wv->contents = wv;
2046 prev_wv = wv;
2047 }
62555c22
RS
2048
2049 /* Forget what we thought we knew about what is in the
2050 detailed contents of the menu bar menus.
2051 Changing the top level always destroys the contents. */
2052 f->menu_bar_items_used = 0;
88766961 2053 }
4dedbfe0 2054
88766961 2055 /* Create or update the menu bar widget. */
aa669def
RS
2056
2057 BLOCK_INPUT;
2058
488dd4c4
JD
2059#ifdef USE_GTK
2060 xg_crazy_callback_abort = 1;
2061 if (menubar_widget)
2062 {
2063 /* The third arg is DEEP_P, which says to consider the entire
2064 menu trees we supply, rather than just the menu bar item names. */
2065 xg_modify_menubar_widgets (menubar_widget,
2066 f,
2067 first_wv,
2068 deep_p,
2069 G_CALLBACK (menubar_selection_callback),
2070 G_CALLBACK (popup_deactivate_callback),
2071 G_CALLBACK (menu_highlight_callback));
2072 }
2073 else
2074 {
2075 GtkWidget *wvbox = f->output_data.x->vbox_widget;
177c0ea7 2076
488dd4c4 2077 menubar_widget
177c0ea7 2078 = xg_create_widget ("menubar", "menubar", f, first_wv,
488dd4c4
JD
2079 G_CALLBACK (menubar_selection_callback),
2080 G_CALLBACK (popup_deactivate_callback),
2081 G_CALLBACK (menu_highlight_callback));
2082
2083 f->output_data.x->menubar_widget = menubar_widget;
2084 }
2085
177c0ea7 2086
488dd4c4 2087#else /* not USE_GTK */
18686d47 2088 if (menubar_widget)
4dedbfe0
PR
2089 {
2090 /* Disable resizing (done for Motif!) */
7556890b 2091 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
2092
2093 /* The third arg is DEEP_P, which says to consider the entire
2094 menu trees we supply, rather than just the menu bar item names. */
88766961 2095 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 2096
c98fcf4b 2097 /* Re-enable the edit widget to resize. */
7556890b 2098 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 2099 }
18686d47
RS
2100 else
2101 {
177c0ea7 2102 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 2103 f->output_data.x->column_widget,
4dedbfe0
PR
2104 0,
2105 popup_activate_callback,
2106 menubar_selection_callback,
850df50b
GM
2107 popup_deactivate_callback,
2108 menu_highlight_callback);
7556890b 2109 f->output_data.x->menubar_widget = menubar_widget;
18686d47 2110 }
1d1c1567
KH
2111
2112 {
177c0ea7 2113 int menubar_size
7556890b
RS
2114 = (f->output_data.x->menubar_widget
2115 ? (f->output_data.x->menubar_widget->core.height
2116 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
2117 : 0);
2118
f42aa681
RS
2119#if 0 /* Experimentally, we now get the right results
2120 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 2121#ifdef USE_LUCID
1d1c1567
KH
2122 if (FRAME_EXTERNAL_MENU_BAR (f))
2123 {
2124 Dimension ibw = 0;
7556890b 2125 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
2126 XtNinternalBorderWidth, &ibw, NULL);
2127 menubar_size += ibw;
2128 }
5ee80bd3 2129#endif /* USE_LUCID */
f42aa681 2130#endif /* 0 */
1d1c1567 2131
7556890b 2132 f->output_data.x->menubar_height = menubar_size;
1d1c1567 2133 }
488dd4c4 2134#endif /* not USE_GTK */
177c0ea7 2135
18686d47 2136 free_menubar_widget_value_tree (first_wv);
364cd450 2137 update_frame_menubar (f);
18686d47 2138
488dd4c4
JD
2139#ifdef USE_GTK
2140 xg_crazy_callback_abort = 0;
2141#endif
2142
18686d47
RS
2143 UNBLOCK_INPUT;
2144}
85f487d1 2145
8e6208c5 2146/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
2147 before it is mapped, so that the window is mapped with the menubar already
2148 there instead of us tacking it on later and thrashing the window after it
2149 is visible. */
2150
2151void
2152initialize_frame_menubar (f)
2153 FRAME_PTR f;
2154{
2155 /* This function is called before the first chance to redisplay
2156 the frame. It has to be, so the frame will have the right size. */
2157 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 2158 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
2159}
2160
aba25348 2161
4dedbfe0 2162/* Get rid of the menu bar of frame F, and free its storage.
488dd4c4
JD
2163 This is used when deleting a frame, and when turning off the menu bar.
2164 For GTK this function is in gtkutil.c. */
4dedbfe0 2165
488dd4c4 2166#ifndef USE_GTK
85f487d1
FP
2167void
2168free_frame_menubar (f)
2169 FRAME_PTR f;
2170{
2171 Widget menubar_widget;
85f487d1 2172
7556890b 2173 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
2174
2175 f->output_data.x->menubar_height = 0;
177c0ea7 2176
85f487d1
FP
2177 if (menubar_widget)
2178 {
aba25348
GM
2179#ifdef USE_MOTIF
2180 /* Removing the menu bar magically changes the shell widget's x
2181 and y position of (0, 0) which, when the menu bar is turned
2182 on again, leads to pull-down menuss appearing in strange
2183 positions near the upper-left corner of the display. This
2184 happens only with some window managers like twm and ctwm,
2185 but not with other like Motif's mwm or kwm, because the
2186 latter generate ConfigureNotify events when the menu bar
2187 is switched off, which fixes the shell position. */
2188 Position x0, y0, x1, y1;
2189#endif
177c0ea7 2190
85f487d1 2191 BLOCK_INPUT;
aba25348
GM
2192
2193#ifdef USE_MOTIF
ae556422
GM
2194 if (f->output_data.x->widget)
2195 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
aba25348 2196#endif
177c0ea7 2197
7556890b 2198 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
0c05dcd3 2199 f->output_data.x->menubar_widget = NULL;
aba25348
GM
2200
2201#ifdef USE_MOTIF
ae556422
GM
2202 if (f->output_data.x->widget)
2203 {
2204 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2205 if (x1 == 0 && y1 == 0)
2206 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2207 }
aba25348 2208#endif
177c0ea7 2209
85f487d1
FP
2210 UNBLOCK_INPUT;
2211 }
2212}
488dd4c4 2213#endif /* not USE_GTK */
78589e07 2214
488dd4c4 2215#endif /* USE_X_TOOLKIT || USE_GTK */
78589e07
RS
2216\f
2217/* xmenu_show actually displays a menu using the panes and items in menu_items
2218 and returns the value selected from it.
2219 There are two versions of xmenu_show, one for Xt and one for Xlib.
2220 Both assume input is blocked by the caller. */
2221
2222/* F is the frame the menu is for.
2223 X and Y are the frame-relative specified position,
2224 relative to the inside upper left corner of the frame F.
c8b5aa3d 2225 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
2226 KEYMAPS is 1 if this menu was specified with keymaps;
2227 in that case, we return a list containing the chosen item's value
2228 and perhaps also the pane's prefix.
2229 TITLE is the specified menu title.
2230 ERROR is a place to store an error message string in case of failure.
2231 (We return nil on failure, but the value doesn't actually matter.) */
18686d47 2232
488dd4c4
JD
2233#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2234
2235/* The item selected in the popup menu. */
2236static Lisp_Object *volatile menu_item_selection;
2237
2238#ifdef USE_GTK
2239
2240/* Used when position a popup menu. See menu_position_func and
2241 create_and_show_popup_menu below. */
2242struct next_popup_x_y
2243{
7b76ca1c 2244 FRAME_PTR f;
488dd4c4
JD
2245 int x;
2246 int y;
2247};
2248
2249/* The menu position function to use if we are not putting a popup
2250 menu where the pointer is.
2251 MENU is the menu to pop up.
2252 X and Y shall on exit contain x/y where the menu shall pop up.
2253 PUSH_IN is not documented in the GTK manual.
2254 USER_DATA is any data passed in when calling gtk_menu_popup.
2255 Here it points to a struct next_popup_x_y where the coordinates
7b76ca1c 2256 to store in *X and *Y are as well as the frame for the popup.
488dd4c4
JD
2257
2258 Here only X and Y are used. */
2259static void
2260menu_position_func (menu, x, y, push_in, user_data)
2261 GtkMenu *menu;
2262 gint *x;
2263 gint *y;
2264 gboolean *push_in;
2265 gpointer user_data;
2266{
7b76ca1c
JD
2267 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2268 GtkRequisition req;
2269 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2270 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2271
2272 *x = data->x;
2273 *y = data->y;
2274
2275 /* Check if there is room for the menu. If not, adjust x/y so that
2276 the menu is fully visible. */
2277 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2278 if (data->x + req.width > disp_width)
2279 *x -= data->x + req.width - disp_width;
2280 if (data->y + req.height > disp_height)
2281 *y -= data->y + req.height - disp_height;
488dd4c4
JD
2282}
2283
2284static void
2285popup_selection_callback (widget, client_data)
2286 GtkWidget *widget;
2287 gpointer client_data;
2288{
2289 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2290
2291 if (xg_crazy_callback_abort) return;
2292 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2293}
2294
2295/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2296 menu pops down.
2297 menu_item_selection will be set to the selection. */
2298static void
2299create_and_show_popup_menu (f, first_wv, x, y, for_click)
2300 FRAME_PTR f;
2301 widget_value *first_wv;
2302 int x;
2303 int y;
2304 int for_click;
2305{
2306 int i;
2307 GtkWidget *menu;
2308 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2309 struct next_popup_x_y popup_x_y;
2310
2311 xg_crazy_callback_abort = 1;
2312 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2313 G_CALLBACK (popup_selection_callback),
2314 G_CALLBACK (popup_deactivate_callback),
2315 G_CALLBACK (menu_highlight_callback));
2316 xg_crazy_callback_abort = 0;
177c0ea7 2317
488dd4c4
JD
2318 for (i = 0; i < 5; i++)
2319 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2320 break;
2321
2322 if (! for_click)
2323 {
2324 /* Not invoked by a click. pop up at x/y. */
2325 pos_func = menu_position_func;
2326
2327 /* Adjust coordinates to be root-window-relative. */
2328 x += f->output_data.x->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2329 y += f->output_data.x->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2330
2331 popup_x_y.x = x;
2332 popup_x_y.y = y;
7b76ca1c 2333 popup_x_y.f = f;
488dd4c4
JD
2334 }
2335
2336 /* Display the menu. */
2337 gtk_widget_show_all (menu);
2338 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
177c0ea7 2339
488dd4c4
JD
2340 xg_did_tearoff = 0;
2341 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2342 two. show_help_echo uses this to detect popup menus. */
2343 popup_activated_flag = 1;
2344 /* Process events that apply to the menu. */
2345 popup_widget_loop ();
2346
2347 if (xg_did_tearoff)
2348 xg_keep_popup (menu, xg_did_tearoff);
2349 else
2350 gtk_widget_destroy (menu);
177c0ea7 2351
488dd4c4
JD
2352 /* Must reset this manually because the button release event is not passed
2353 to Emacs event loop. */
2354 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2355}
2356
2357#else /* not USE_GTK */
18686d47 2358
8ed87156 2359/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
2360 library.
2361
2362 For the main windows, and popup menus, we use this counter,
88766961 2363 which we increment each time after use. This starts from 1<<16.
cc17e9bf 2364
88766961
RS
2365 For menu bars, we use numbers starting at 0, counted in
2366 next_menubar_widget_id. */
8ed87156 2367LWLIB_ID widget_id_tick;
165e1749 2368
4dedbfe0
PR
2369static void
2370popup_selection_callback (widget, id, client_data)
2371 Widget widget;
2372 LWLIB_ID id;
2373 XtPointer client_data;
2374{
2375 menu_item_selection = (Lisp_Object *) client_data;
2376}
2377
488dd4c4
JD
2378/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2379 menu pops down.
2380 menu_item_selection will be set to the selection. */
2381static void
2382create_and_show_popup_menu (f, first_wv, x, y, for_click)
2383 FRAME_PTR f;
2384 widget_value *first_wv;
2385 int x;
2386 int y;
2387 int for_click;
2388{
2389 int i;
2390 Arg av[2];
2391 int ac = 0;
2392 XButtonPressedEvent dummy;
2393 LWLIB_ID menu_id;
2394 Widget menu;
2395 Window child;
2396
2397 menu_id = widget_id_tick++;
2398 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2399 f->output_data.x->widget, 1, 0,
2400 popup_selection_callback,
2401 popup_deactivate_callback,
2402 menu_highlight_callback);
2403
2404 dummy.type = ButtonPress;
2405 dummy.serial = 0;
2406 dummy.send_event = 0;
2407 dummy.display = FRAME_X_DISPLAY (f);
2408 dummy.time = CurrentTime;
2409 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2410 dummy.window = dummy.root;
2411 dummy.subwindow = dummy.root;
2412 dummy.x = x;
2413 dummy.y = y;
2414
2415 /* Adjust coordinates to be root-window-relative. */
2416 x += f->output_data.x->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2417 y += f->output_data.x->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2418
2419 dummy.x_root = x;
2420 dummy.y_root = y;
2421
2422 dummy.state = 0;
2423 dummy.button = 0;
2424 for (i = 0; i < 5; i++)
2425 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2426 dummy.button = i;
2427
2428 /* Don't allow any geometry request from the user. */
2429 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2430 XtSetValues (menu, av, ac);
2431
2432 /* Display the menu. */
2433 lw_popup_menu (menu, (XEvent *) &dummy);
2434 popup_activated_flag = 1;
2435
2436 /* Process events that apply to the menu. */
2437 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 0);
2438
2439 /* fp turned off the following statement and wrote a comment
2440 that it is unnecessary--that the menu has already disappeared.
2441 Nowadays the menu disappears ok, all right, but
2442 we need to delete the widgets or multiple ones will pile up. */
177c0ea7 2443 lw_destroy_all_widgets (menu_id);
488dd4c4
JD
2444}
2445
2446#endif /* not USE_GTK */
2447
78589e07 2448static Lisp_Object
673a6211 2449xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 2450 FRAME_PTR f;
18686d47
RS
2451 int x;
2452 int y;
9685a93f 2453 int for_click;
78589e07
RS
2454 int keymaps;
2455 Lisp_Object title;
2456 char **error;
18686d47 2457{
78589e07 2458 int i;
78589e07 2459 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
2460 widget_value **submenu_stack
2461 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2462 Lisp_Object *subprefix_stack
2463 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2464 int submenu_depth = 0;
4e8d3549 2465
78c8278d
RS
2466 int first_pane;
2467
78589e07
RS
2468 *error = NULL;
2469
742f715d
KH
2470 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2471 {
2472 *error = "Empty menu";
2473 return Qnil;
2474 }
63c414df 2475
78589e07
RS
2476 /* Create a tree of widget_value objects
2477 representing the panes and their items. */
f7fab165 2478 wv = xmalloc_widget_value ();
78589e07
RS
2479 wv->name = "menu";
2480 wv->value = 0;
2481 wv->enabled = 1;
3427a3db 2482 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2483 wv->help =Qnil;
78589e07 2484 first_wv = wv;
78c8278d 2485 first_pane = 1;
177c0ea7 2486
78589e07
RS
2487 /* Loop over all panes and items, filling in the tree. */
2488 i = 0;
2489 while (i < menu_items_used)
2490 {
101bb4a5
RS
2491 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2492 {
2493 submenu_stack[submenu_depth++] = save_wv;
2494 save_wv = prev_wv;
2495 prev_wv = 0;
78c8278d 2496 first_pane = 1;
101bb4a5
RS
2497 i++;
2498 }
2499 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2500 {
2501 prev_wv = save_wv;
2502 save_wv = submenu_stack[--submenu_depth];
78c8278d 2503 first_pane = 0;
101bb4a5
RS
2504 i++;
2505 }
2506 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2507 && submenu_depth != 0)
2508 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2509 /* Ignore a nil in the item list.
2510 It's meaningful only for dialog boxes. */
2511 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2512 i += 1;
101bb4a5 2513 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2514 {
2515 /* Create a new pane. */
2516 Lisp_Object pane_name, prefix;
2517 char *pane_string;
177c0ea7 2518
4c329aa8
GM
2519 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2520 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 2521
703dc2a8 2522#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
2523 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2524 {
cc45fb40 2525 pane_name = ENCODE_SYSTEM (pane_name);
4c329aa8
GM
2526 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2527 }
703dc2a8 2528#endif
78589e07 2529 pane_string = (NILP (pane_name)
d5db4077 2530 ? "" : (char *) SDATA (pane_name));
101bb4a5 2531 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2532 under the top-level menu. */
2533 if (menu_items_n_panes == 1)
2534 pane_string = "";
2535
2536 /* If the pane has a meaningful name,
2537 make the pane a top-level menu item
2538 with its items as a submenu beneath it. */
78c8278d 2539 if (!keymaps && strcmp (pane_string, ""))
78589e07 2540 {
f7fab165 2541 wv = xmalloc_widget_value ();
78589e07
RS
2542 if (save_wv)
2543 save_wv->next = wv;
2544 else
2545 first_wv->contents = wv;
2546 wv->name = pane_string;
2547 if (keymaps && !NILP (prefix))
2548 wv->name++;
2549 wv->value = 0;
2550 wv->enabled = 1;
3427a3db 2551 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2552 wv->help = Qnil;
78c8278d
RS
2553 save_wv = wv;
2554 prev_wv = 0;
78589e07 2555 }
78c8278d
RS
2556 else if (first_pane)
2557 {
2558 save_wv = wv;
2559 prev_wv = 0;
2560 }
2561 first_pane = 0;
78589e07
RS
2562 i += MENU_ITEMS_PANE_LENGTH;
2563 }
2564 else
2565 {
2566 /* Create a new item within current pane. */
9cd50434 2567 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
2568 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2569 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2570 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2571 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2572 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2573 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2574 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 2575
703dc2a8 2576#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 2577 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 2578 {
cc45fb40 2579 item_name = ENCODE_SYSTEM (item_name);
4c329aa8
GM
2580 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2581 }
177c0ea7 2582
3427a3db 2583 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 2584 {
cc45fb40 2585 descrip = ENCODE_SYSTEM (descrip);
4c329aa8
GM
2586 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2587 }
2588#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 2589
f7fab165 2590 wv = xmalloc_widget_value ();
177c0ea7 2591 if (prev_wv)
78589e07 2592 prev_wv->next = wv;
177c0ea7 2593 else
78589e07 2594 save_wv->contents = wv;
d5db4077 2595 wv->name = (char *) SDATA (item_name);
78589e07 2596 if (!NILP (descrip))
d5db4077 2597 wv->key = (char *) SDATA (descrip);
78589e07 2598 wv->value = 0;
a352a815
RS
2599 /* If this item has a null value,
2600 make the call_data null so that it won't display a box
2601 when the mouse is on it. */
2602 wv->call_data
2603 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07 2604 wv->enabled = !NILP (enable);
3427a3db
GM
2605
2606 if (NILP (type))
2607 wv->button_type = BUTTON_TYPE_NONE;
2608 else if (EQ (type, QCtoggle))
2609 wv->button_type = BUTTON_TYPE_TOGGLE;
2610 else if (EQ (type, QCradio))
2611 wv->button_type = BUTTON_TYPE_RADIO;
2612 else
2613 abort ();
2614
2615 wv->selected = !NILP (selected);
cc45fb40 2616
0b1f4572
RS
2617 if (! STRINGP (help))
2618 help = Qnil;
2619
2620 wv->help = help;
cc45fb40 2621
78589e07
RS
2622 prev_wv = wv;
2623
2624 i += MENU_ITEMS_ITEM_LENGTH;
2625 }
2626 }
2627
c98fcf4b 2628 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2629 if (!NILP (title))
2630 {
f7fab165
RS
2631 widget_value *wv_title = xmalloc_widget_value ();
2632 widget_value *wv_sep1 = xmalloc_widget_value ();
2633 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2634
2635 wv_sep2->name = "--";
2636 wv_sep2->next = first_wv->contents;
27ad7b52 2637 wv_sep2->help = Qnil;
4dedbfe0
PR
2638
2639 wv_sep1->name = "--";
2640 wv_sep1->next = wv_sep2;
27ad7b52 2641 wv_sep1->help = Qnil;
4dedbfe0 2642
703dc2a8
KH
2643#ifndef HAVE_MULTILINGUAL_MENU
2644 if (STRING_MULTIBYTE (title))
cc45fb40 2645 title = ENCODE_SYSTEM (title);
703dc2a8 2646#endif
177c0ea7 2647
d5db4077 2648 wv_title->name = (char *) SDATA (title);
cc45fb40 2649 wv_title->enabled = TRUE;
3427a3db 2650 wv_title->button_type = BUTTON_TYPE_NONE;
4dedbfe0 2651 wv_title->next = wv_sep1;
27ad7b52 2652 wv_title->help = Qnil;
4dedbfe0
PR
2653 first_wv->contents = wv_title;
2654 }
2655
78589e07
RS
2656 /* No selection has been chosen yet. */
2657 menu_item_selection = 0;
2658
488dd4c4
JD
2659 /* Actually create and show the menu until popped down. */
2660 create_and_show_popup_menu (f, first_wv, x, y, for_click);
18686d47 2661
488dd4c4
JD
2662 /* Free the widget_value objects we used to specify the contents. */
2663 free_menubar_widget_value_tree (first_wv);
18686d47 2664
78589e07
RS
2665 /* Find the selected item, and its pane, to return
2666 the proper value. */
2667 if (menu_item_selection != 0)
2668 {
c63f6952 2669 Lisp_Object prefix, entry;
78589e07 2670
6bbd7a29 2671 prefix = entry = Qnil;
78589e07
RS
2672 i = 0;
2673 while (i < menu_items_used)
2674 {
101bb4a5
RS
2675 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2676 {
2677 subprefix_stack[submenu_depth++] = prefix;
2678 prefix = entry;
2679 i++;
2680 }
2681 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2682 {
2683 prefix = subprefix_stack[--submenu_depth];
2684 i++;
2685 }
2686 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2687 {
2688 prefix
2689 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2690 i += MENU_ITEMS_PANE_LENGTH;
2691 }
d31d42cc
RS
2692 /* Ignore a nil in the item list.
2693 It's meaningful only for dialog boxes. */
2694 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2695 i += 1;
78589e07
RS
2696 else
2697 {
2698 entry
2699 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2700 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2701 {
2702 if (keymaps != 0)
2703 {
101bb4a5
RS
2704 int j;
2705
78589e07
RS
2706 entry = Fcons (entry, Qnil);
2707 if (!NILP (prefix))
2708 entry = Fcons (prefix, entry);
101bb4a5 2709 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2710 if (!NILP (subprefix_stack[j]))
5964e450 2711 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2712 }
2713 return entry;
2714 }
2715 i += MENU_ITEMS_ITEM_LENGTH;
2716 }
2717 }
2718 }
2719
2720 return Qnil;
18686d47 2721}
4dedbfe0 2722\f
488dd4c4
JD
2723#ifdef USE_GTK
2724static void
2725dialog_selection_callback (widget, client_data)
2726 GtkWidget *widget;
2727 gpointer client_data;
2728{
2729 /* The EMACS_INT cast avoids a warning. There's no problem
2730 as long as pointers have enough bits to hold small integers. */
2731 if ((int) (EMACS_INT) client_data != -1)
2732 menu_item_selection = (Lisp_Object *) client_data;
2733
2734 popup_activated_flag = 0;
2735}
2736
2737/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2738 dialog pops down.
2739 menu_item_selection will be set to the selection. */
2740static void
2741create_and_show_dialog (f, first_wv)
2742 FRAME_PTR f;
2743 widget_value *first_wv;
2744{
2745 GtkWidget *menu;
2746
2747 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2748 G_CALLBACK (dialog_selection_callback),
2749 G_CALLBACK (popup_deactivate_callback),
2750 0);
2751
2752 if (menu)
2753 {
2754 /* Display the menu. */
2755 gtk_widget_show_all (menu);
2756
2757 /* Process events that apply to the menu. */
2758 popup_widget_loop ();
177c0ea7 2759
488dd4c4
JD
2760 gtk_widget_destroy (menu);
2761 }
2762}
2763
2764#else /* not USE_GTK */
4dedbfe0
PR
2765static void
2766dialog_selection_callback (widget, id, client_data)
2767 Widget widget;
2768 LWLIB_ID id;
2769 XtPointer client_data;
2770{
01d5e892
RS
2771 /* The EMACS_INT cast avoids a warning. There's no problem
2772 as long as pointers have enough bits to hold small integers. */
2773 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0 2774 menu_item_selection = (Lisp_Object *) client_data;
488dd4c4 2775
4dedbfe0
PR
2776 BLOCK_INPUT;
2777 lw_destroy_all_widgets (id);
2778 UNBLOCK_INPUT;
9572375b 2779 popup_activated_flag = 0;
4dedbfe0 2780}
18686d47 2781
488dd4c4 2782
f02cac82
RS
2783/* ARG is the LWLIB ID of the dialog box, represented
2784 as a Lisp object as (HIGHPART . LOWPART). */
2785
2786Lisp_Object
2787xdialog_show_unwind (arg)
2788 Lisp_Object arg;
2789{
2790 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2791 | XINT (XCDR (arg)));
2792 BLOCK_INPUT;
2793 lw_destroy_all_widgets (id);
2794 UNBLOCK_INPUT;
2795 popup_activated_flag = 0;
2796 return Qnil;
2797}
2798
488dd4c4
JD
2799
2800/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2801 dialog pops down.
2802 menu_item_selection will be set to the selection. */
2803static void
2804create_and_show_dialog (f, first_wv)
2805 FRAME_PTR f;
2806 widget_value *first_wv;
2807{
2808 LWLIB_ID dialog_id;
2809
2810 dialog_id = widget_id_tick++;
2811 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2812 f->output_data.x->widget, 1, 0,
2813 dialog_selection_callback, 0, 0);
2814 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2815
2816 /* Display the dialog box. */
2817 lw_pop_up_all_widgets (dialog_id);
2818 popup_activated_flag = 1;
2819
2820 /* Process events that apply to the dialog box.
2821 Also handle timers. */
2822 {
2823 int count = SPECPDL_INDEX ();
2824 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 2825
488dd4c4
JD
2826 /* xdialog_show_unwind is responsible for popping the dialog box down. */
2827 record_unwind_protect (xdialog_show_unwind,
2828 Fcons (make_number (dialog_id >> (fact)),
2829 make_number (dialog_id & ~(-1 << (fact)))));
2830
2831 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id, 1);
2832
2833 unbind_to (count, Qnil);
2834 }
2835}
2836
2837#endif /* not USE_GTK */
2838
165e1749
FP
2839static char * button_names [] = {
2840 "button1", "button2", "button3", "button4", "button5",
2841 "button6", "button7", "button8", "button9", "button10" };
2842
2843static Lisp_Object
673a6211 2844xdialog_show (f, keymaps, title, error)
165e1749 2845 FRAME_PTR f;
165e1749
FP
2846 int keymaps;
2847 Lisp_Object title;
2848 char **error;
2849{
2850 int i, nb_buttons=0;
80670155 2851 char dialog_name[6];
165e1749 2852
faa935b6 2853 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 2854
fcaa7665
RS
2855 /* Number of elements seen so far, before boundary. */
2856 int left_count = 0;
2857 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2858 int boundary_seen = 0;
2859
165e1749
FP
2860 *error = NULL;
2861
80670155
RS
2862 if (menu_items_n_panes > 1)
2863 {
2864 *error = "Multiple panes in dialog box";
2865 return Qnil;
2866 }
2867
165e1749
FP
2868 /* Create a tree of widget_value objects
2869 representing the text label and buttons. */
2870 {
2871 Lisp_Object pane_name, prefix;
2872 char *pane_string;
2873 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2874 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2875 pane_string = (NILP (pane_name)
177c0ea7 2876 ? "" : (char *) SDATA (pane_name));
f7fab165 2877 prev_wv = xmalloc_widget_value ();
165e1749
FP
2878 prev_wv->value = pane_string;
2879 if (keymaps && !NILP (prefix))
2880 prev_wv->name++;
2881 prev_wv->enabled = 1;
2882 prev_wv->name = "message";
27ad7b52 2883 prev_wv->help = Qnil;
165e1749 2884 first_wv = prev_wv;
177c0ea7 2885
165e1749
FP
2886 /* Loop over all panes and items, filling in the tree. */
2887 i = MENU_ITEMS_PANE_LENGTH;
2888 while (i < menu_items_used)
2889 {
177c0ea7 2890
165e1749
FP
2891 /* Create a new item within current pane. */
2892 Lisp_Object item_name, enable, descrip;
2893 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2894 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2895 descrip
2896 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
177c0ea7 2897
80670155
RS
2898 if (NILP (item_name))
2899 {
2900 free_menubar_widget_value_tree (first_wv);
2901 *error = "Submenu in dialog items";
2902 return Qnil;
2903 }
fcaa7665
RS
2904 if (EQ (item_name, Qquote))
2905 {
2906 /* This is the boundary between left-side elts
2907 and right-side elts. Stop incrementing right_count. */
2908 boundary_seen = 1;
2909 i++;
2910 continue;
2911 }
86e71abf 2912 if (nb_buttons >= 9)
80670155
RS
2913 {
2914 free_menubar_widget_value_tree (first_wv);
2915 *error = "Too many dialog items";
2916 return Qnil;
2917 }
2918
f7fab165 2919 wv = xmalloc_widget_value ();
165e1749 2920 prev_wv->next = wv;
80670155 2921 wv->name = (char *) button_names[nb_buttons];
165e1749 2922 if (!NILP (descrip))
d5db4077
KR
2923 wv->key = (char *) SDATA (descrip);
2924 wv->value = (char *) SDATA (item_name);
165e1749
FP
2925 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2926 wv->enabled = !NILP (enable);
27ad7b52 2927 wv->help = Qnil;
165e1749
FP
2928 prev_wv = wv;
2929
fcaa7665
RS
2930 if (! boundary_seen)
2931 left_count++;
2932
165e1749
FP
2933 nb_buttons++;
2934 i += MENU_ITEMS_ITEM_LENGTH;
2935 }
2936
fcaa7665
RS
2937 /* If the boundary was not specified,
2938 by default put half on the left and half on the right. */
2939 if (! boundary_seen)
2940 left_count = nb_buttons - nb_buttons / 2;
2941
f7fab165 2942 wv = xmalloc_widget_value ();
80670155 2943 wv->name = dialog_name;
27ad7b52 2944 wv->help = Qnil;
80670155
RS
2945 /* Dialog boxes use a really stupid name encoding
2946 which specifies how many buttons to use
2947 and how many buttons are on the right.
2948 The Q means something also. */
2949 dialog_name[0] = 'Q';
2950 dialog_name[1] = '0' + nb_buttons;
2951 dialog_name[2] = 'B';
2952 dialog_name[3] = 'R';
fcaa7665
RS
2953 /* Number of buttons to put on the right. */
2954 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 2955 dialog_name[5] = 0;
165e1749
FP
2956 wv->contents = first_wv;
2957 first_wv = wv;
165e1749
FP
2958 }
2959
165e1749
FP
2960 /* No selection has been chosen yet. */
2961 menu_item_selection = 0;
2962
488dd4c4
JD
2963 /* Actually create and show the dialog. */
2964 create_and_show_dialog (f, first_wv);
f02cac82 2965
488dd4c4
JD
2966 /* Free the widget_value objects we used to specify the contents. */
2967 free_menubar_widget_value_tree (first_wv);
177c0ea7 2968
488dd4c4
JD
2969 /* Find the selected item, and its pane, to return
2970 the proper value. */
165e1749
FP
2971 if (menu_item_selection != 0)
2972 {
2973 Lisp_Object prefix;
2974
2975 prefix = Qnil;
2976 i = 0;
2977 while (i < menu_items_used)
2978 {
2979 Lisp_Object entry;
2980
2981 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2982 {
2983 prefix
2984 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2985 i += MENU_ITEMS_PANE_LENGTH;
2986 }
85996cfb
GM
2987 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2988 {
2989 /* This is the boundary between left-side elts and
2990 right-side elts. */
2991 ++i;
2992 }
165e1749
FP
2993 else
2994 {
2995 entry
2996 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2997 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2998 {
2999 if (keymaps != 0)
3000 {
3001 entry = Fcons (entry, Qnil);
3002 if (!NILP (prefix))
3003 entry = Fcons (prefix, entry);
3004 }
3005 return entry;
3006 }
3007 i += MENU_ITEMS_ITEM_LENGTH;
3008 }
3009 }
3010 }
3011
3012 return Qnil;
3013}
ba461919 3014
488dd4c4 3015#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 3016
3e703b25
GM
3017/* The frame of the last activated non-toolkit menu bar.
3018 Used to generate menu help events. */
3019
3020static struct frame *menu_help_frame;
3021
3022
62145073
GM
3023/* Show help HELP_STRING, or clear help if HELP_STRING is null.
3024
3025 PANE is the pane number, and ITEM is the menu item number in
3026 the menu (currently not used).
177c0ea7 3027
62145073
GM
3028 This cannot be done with generating a HELP_EVENT because
3029 XMenuActivate contains a loop that doesn't let Emacs process
3030 keyboard events. */
3e703b25
GM
3031
3032static void
62145073 3033menu_help_callback (help_string, pane, item)
3e703b25 3034 char *help_string;
62145073 3035 int pane, item;
3e703b25 3036{
62145073
GM
3037 extern Lisp_Object Qmenu_item;
3038 Lisp_Object *first_item;
3039 Lisp_Object pane_name;
3040 Lisp_Object menu_object;
177c0ea7 3041
62145073
GM
3042 first_item = XVECTOR (menu_items)->contents;
3043 if (EQ (first_item[0], Qt))
3044 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3045 else if (EQ (first_item[0], Qquote))
3046 /* This shouldn't happen, see xmenu_show. */
cc45fb40 3047 pane_name = empty_string;
62145073
GM
3048 else
3049 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 3050
62145073
GM
3051 /* (menu-item MENU-NAME PANE-NUMBER) */
3052 menu_object = Fcons (Qmenu_item,
3053 Fcons (pane_name,
3054 Fcons (make_number (pane), Qnil)));
ba461919 3055 show_help_echo (help_string ? build_string (help_string) : Qnil,
62145073 3056 Qnil, menu_object, make_number (item), 1);
3e703b25 3057}
177c0ea7 3058
3e703b25 3059
78589e07 3060static Lisp_Object
673a6211 3061xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
3062 FRAME_PTR f;
3063 int x, y;
9685a93f
RS
3064 int for_click;
3065 int keymaps;
78589e07
RS
3066 Lisp_Object title;
3067 char **error;
dcfdbac7 3068{
177c0ea7 3069 Window root;
78589e07
RS
3070 XMenu *menu;
3071 int pane, selidx, lpane, status;
3072 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
3073 char *datap;
3074 int ulx, uly, width, height;
3075 int dispwidth, dispheight;
4e8d3549
RS
3076 int i, j;
3077 int maxwidth;
78589e07
RS
3078 int dummy_int;
3079 unsigned int dummy_uint;
088831f6 3080
07a675b7 3081 *error = 0;
78589e07
RS
3082 if (menu_items_n_panes == 0)
3083 return Qnil;
088831f6 3084
742f715d
KH
3085 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3086 {
3087 *error = "Empty menu";
3088 return Qnil;
3089 }
3090
78589e07 3091 /* Figure out which root window F is on. */
92280f67 3092 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
3093 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3094 &dummy_uint, &dummy_uint);
18686d47 3095
78589e07 3096 /* Make the menu on that window. */
92280f67 3097 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 3098 if (menu == NULL)
dcfdbac7
JB
3099 {
3100 *error = "Can't create menu";
78589e07 3101 return Qnil;
dcfdbac7 3102 }
78589e07 3103
87485d6f 3104#ifdef HAVE_X_WINDOWS
78589e07 3105 /* Adjust coordinates to relative to the outer (window manager) window. */
78589e07
RS
3106 {
3107 Window child;
3108 int win_x = 0, win_y = 0;
3109
3110 /* Find the position of the outside upper-left corner of
3111 the inner window, with respect to the outer window. */
7556890b 3112 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
78589e07
RS
3113 {
3114 BLOCK_INPUT;
92280f67 3115 XTranslateCoordinates (FRAME_X_DISPLAY (f),
78589e07
RS
3116
3117 /* From-window, to-window. */
7556890b
RS
3118 f->output_data.x->window_desc,
3119 f->output_data.x->parent_desc,
78589e07
RS
3120
3121 /* From-position, to-position. */
3122 0, 0, &win_x, &win_y,
3123
3124 /* Child of window. */
3125 &child);
3126 UNBLOCK_INPUT;
3127 x += win_x;
3128 y += win_y;
3129 }
3130 }
87485d6f 3131#endif /* HAVE_X_WINDOWS */
78589e07
RS
3132
3133 /* Adjust coordinates to be root-window-relative. */
7556890b
RS
3134 x += f->output_data.x->left_pos;
3135 y += f->output_data.x->top_pos;
177c0ea7 3136
78589e07
RS
3137 /* Create all the necessary panes and their items. */
3138 i = 0;
3139 while (i < menu_items_used)
dcfdbac7 3140 {
78589e07 3141 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 3142 {
78589e07
RS
3143 /* Create a new pane. */
3144 Lisp_Object pane_name, prefix;
3145 char *pane_string;
3146
3147 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3148 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3149 pane_string = (NILP (pane_name)
d5db4077 3150 ? "" : (char *) SDATA (pane_name));
78589e07
RS
3151 if (keymaps && !NILP (prefix))
3152 pane_string++;
3153
92280f67 3154 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
3155 if (lpane == XM_FAILURE)
3156 {
92280f67 3157 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
3158 *error = "Can't create pane";
3159 return Qnil;
3160 }
3161 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
3162
3163 /* Find the width of the widest item in this pane. */
3164 maxwidth = 0;
3165 j = i;
3166 while (j < menu_items_used)
3167 {
3168 Lisp_Object item;
3169 item = XVECTOR (menu_items)->contents[j];
3170 if (EQ (item, Qt))
3171 break;
3172 if (NILP (item))
3173 {
3174 j++;
3175 continue;
3176 }
d5db4077 3177 width = SBYTES (item);
4e8d3549
RS
3178 if (width > maxwidth)
3179 maxwidth = width;
3180
3181 j += MENU_ITEMS_ITEM_LENGTH;
3182 }
dcfdbac7 3183 }
fcaa7665
RS
3184 /* Ignore a nil in the item list.
3185 It's meaningful only for dialog boxes. */
3186 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3187 i += 1;
78589e07 3188 else
dcfdbac7 3189 {
78589e07 3190 /* Create a new item within current pane. */
3e703b25 3191 Lisp_Object item_name, enable, descrip, help;
4e8d3549 3192 unsigned char *item_data;
3e703b25 3193 char *help_string;
78589e07
RS
3194
3195 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3196 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3197 descrip
3198 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3e703b25 3199 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
d5db4077 3200 help_string = STRINGP (help) ? SDATA (help) : NULL;
177c0ea7 3201
78589e07 3202 if (!NILP (descrip))
4e8d3549 3203 {
d5db4077 3204 int gap = maxwidth - SBYTES (item_name);
4e8d3549
RS
3205#ifdef C_ALLOCA
3206 Lisp_Object spacer;
3207 spacer = Fmake_string (make_number (gap), make_number (' '));
3208 item_name = concat2 (item_name, spacer);
3209 item_name = concat2 (item_name, descrip);
d5db4077 3210 item_data = SDATA (item_name);
4e8d3549
RS
3211#else
3212 /* if alloca is fast, use that to make the space,
3213 to reduce gc needs. */
3214 item_data
3215 = (unsigned char *) alloca (maxwidth
d5db4077
KR
3216 + SBYTES (descrip) + 1);
3217 bcopy (SDATA (item_name), item_data,
3218 SBYTES (item_name));
3219 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 3220 item_data[j] = ' ';
d5db4077
KR
3221 bcopy (SDATA (descrip), item_data + j,
3222 SBYTES (descrip));
3223 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
3224#endif
3225 }
3226 else
d5db4077 3227 item_data = SDATA (item_name);
78589e07 3228
92280f67
RS
3229 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3230 menu, lpane, 0, item_data,
3e703b25 3231 !NILP (enable), help_string)
dcfdbac7
JB
3232 == XM_FAILURE)
3233 {
92280f67 3234 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 3235 *error = "Can't add selection to menu";
78589e07 3236 return Qnil;
dcfdbac7 3237 }
78589e07 3238 i += MENU_ITEMS_ITEM_LENGTH;
dcfdbac7
JB
3239 }
3240 }
4e8d3549 3241
78589e07 3242 /* All set and ready to fly. */
92280f67 3243 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
3244 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3245 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
3246 x = min (x, dispwidth);
3247 y = min (y, dispheight);
3248 x = max (x, 1);
3249 y = max (y, 1);
92280f67 3250 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
3251 &ulx, &uly, &width, &height);
3252 if (ulx+width > dispwidth)
3253 {
78589e07 3254 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
3255 ulx = dispwidth - width;
3256 }
3257 if (uly+height > dispheight)
3258 {
78589e07 3259 y -= (uly + height) - dispheight;
dcfdbac7
JB
3260 uly = dispheight - height;
3261 }
78589e07
RS
3262 if (ulx < 0) x -= ulx;
3263 if (uly < 0) y -= uly;
121e4555
KH
3264
3265 XMenuSetAEQ (menu, TRUE);
78589e07
RS
3266 XMenuSetFreeze (menu, TRUE);
3267 pane = selidx = 0;
3e703b25
GM
3268
3269 /* Help display under X won't work because XMenuActivate contains
3270 a loop that doesn't give Emacs a chance to process it. */
3271 menu_help_frame = f;
92280f67 3272 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3e703b25
GM
3273 x, y, ButtonReleaseMask, &datap,
3274 menu_help_callback);
a352a815
RS
3275
3276
f1df80a8 3277#ifdef HAVE_X_WINDOWS
a352a815
RS
3278 /* Assume the mouse has moved out of the X window.
3279 If it has actually moved in, we will get an EnterNotify. */
29e460bd 3280 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
f1df80a8 3281#endif
a352a815 3282
dcfdbac7
JB
3283 switch (status)
3284 {
3285 case XM_SUCCESS:
3286#ifdef XDEBUG
3287 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3288#endif
fa6d54d9 3289
78589e07
RS
3290 /* Find the item number SELIDX in pane number PANE. */
3291 i = 0;
3292 while (i < menu_items_used)
fa6d54d9 3293 {
78589e07 3294 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 3295 {
78589e07
RS
3296 if (pane == 0)
3297 pane_prefix
3298 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3299 pane--;
3300 i += MENU_ITEMS_PANE_LENGTH;
088831f6 3301 }
78589e07 3302 else
ab6ee1a0 3303 {
78589e07 3304 if (pane == -1)
ab6ee1a0 3305 {
78589e07 3306 if (selidx == 0)
ab6ee1a0 3307 {
78589e07
RS
3308 entry
3309 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3310 if (keymaps != 0)
ab6ee1a0 3311 {
78589e07
RS
3312 entry = Fcons (entry, Qnil);
3313 if (!NILP (pane_prefix))
3314 entry = Fcons (pane_prefix, entry);
ab6ee1a0 3315 }
78589e07 3316 break;
ab6ee1a0 3317 }
78589e07 3318 selidx--;
ab6ee1a0 3319 }
78589e07 3320 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
3321 }
3322 }
78589e07 3323 break;
dcfdbac7 3324
78589e07 3325 case XM_FAILURE:
78589e07
RS
3326 *error = "Can't activate menu";
3327 case XM_IA_SELECT:
3328 case XM_NO_SELECT:
3329 entry = Qnil;
3330 break;
dcfdbac7 3331 }
92280f67 3332 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
a5285df3 3333
87485d6f 3334#ifdef HAVE_X_WINDOWS
a5285df3
RS
3335 /* State that no mouse buttons are now held.
3336 (The oldXMenu code doesn't track this info for us.)
3337 That is not necessarily true, but the fiction leads to reasonable
3338 results, and it is a pain to ask which are actually held now. */
e9a79fb2 3339 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
87485d6f 3340#endif
a5285df3 3341
78589e07 3342 return entry;
dcfdbac7 3343}
4dedbfe0 3344
78589e07 3345#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
3346
3347#endif /* HAVE_MENUS */
088831f6 3348\f
dfcf069d 3349void
78589e07 3350syms_of_xmenu ()
dcfdbac7 3351{
78589e07
RS
3352 staticpro (&menu_items);
3353 menu_items = Qnil;
86fad4ec 3354 menu_items_inuse = Qnil;
dcfdbac7 3355
0314aacb
RS
3356 Qdebug_on_next_call = intern ("debug-on-next-call");
3357 staticpro (&Qdebug_on_next_call);
3358
7ee72033
MB
3359 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3360 doc: /* Frame for which we are updating a menu.
228299fa 3361The enable predicate for a menu command should check this variable. */);
bfc524bc
RS
3362 Vmenu_updating_frame = Qnil;
3363
8ed87156 3364#ifdef USE_X_TOOLKIT
177c0ea7 3365 widget_id_tick = (1<<16);
88766961 3366 next_menubar_widget_id = 1;
8ed87156
RS
3367#endif
3368
78589e07 3369 defsubr (&Sx_popup_menu);
1e659e4c 3370#ifdef HAVE_MENUS
165e1749 3371 defsubr (&Sx_popup_dialog);
1e659e4c 3372#endif
dcfdbac7 3373}