(x_window): Don't add ibw to menubar_size.
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
faf8a408 2 Copyright (C) 1986, 1988, 1993, 1994, 1996 Free Software Foundation, Inc.
dcfdbac7
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
774910eb 8the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
dcfdbac7
JB
20
21/* X pop-up deck-of-cards menu facility for gnuemacs.
22 *
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
25 *
26 */
27
18686d47
RS
28/* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
30
78589e07
RS
31/* Rewritten for clarity and GC protection by rms in Feb 94. */
32
dcfdbac7
JB
33/* On 4.3 this loses if it comes after xterm.h. */
34#include <signal.h>
18160b98 35#include <config.h>
565620a5
RS
36
37#include <stdio.h>
dcfdbac7 38#include "lisp.h"
18686d47 39#include "termhooks.h"
7708e9bd 40#include "frame.h"
dcfdbac7 41#include "window.h"
031b0e31 42#include "keyboard.h"
9ac0d9e0 43#include "blockinput.h"
62010a73 44#include "puresize.h"
88766961 45#include "buffer.h"
dcfdbac7 46
eeee3112
RS
47#ifdef MSDOS
48#include "msdos.h"
49#endif
50
87485d6f 51#ifdef HAVE_X_WINDOWS
dcfdbac7
JB
52/* This may include sys/types.h, and that somehow loses
53 if this is not done before the other system files. */
54#include "xterm.h"
87485d6f 55#endif
dcfdbac7
JB
56
57/* Load sys/types.h if not already loaded.
58 In some systems loading it twice is suicidal. */
59#ifndef makedev
60#include <sys/types.h>
61#endif
62
63#include "dispextern.h"
64
87485d6f 65#ifdef HAVE_X_WINDOWS
18686d47
RS
66#ifdef USE_X_TOOLKIT
67#include <X11/Xlib.h>
68#include <X11/IntrinsicP.h>
69#include <X11/CoreP.h>
70#include <X11/StringDefs.h>
60f312e2 71#include <X11/Shell.h>
5ee80bd3 72#ifdef USE_LUCID
1d1c1567 73#include <X11/Xaw/Paned.h>
5ee80bd3 74#endif /* USE_LUCID */
18686d47 75#include "../lwlib/lwlib.h"
a352a815
RS
76#else /* not USE_X_TOOLKIT */
77#include "../oldXMenu/XMenu.h"
78#endif /* not USE_X_TOOLKIT */
79#endif /* HAVE_X_WINDOWS */
18686d47 80
dcfdbac7
JB
81#define min(x,y) (((x) < (y)) ? (x) : (y))
82#define max(x,y) (((x) > (y)) ? (x) : (y))
83
dcfdbac7
JB
84#ifndef TRUE
85#define TRUE 1
86#define FALSE 0
78589e07 87#endif /* no TRUE */
dcfdbac7 88
0314aacb
RS
89Lisp_Object Qdebug_on_next_call;
90
67385caa
RS
91Lisp_Object Qmenu_alias;
92
6904bdcd 93extern Lisp_Object Qmenu_enable;
18686d47 94extern Lisp_Object Qmenu_bar;
92280f67 95extern Lisp_Object Qmouse_click, Qevent_kind;
78589e07 96
8fbc986d
RS
97extern Lisp_Object Vdefine_key_rebound_commands;
98
88766961
RS
99extern Lisp_Object Voverriding_local_map;
100extern Lisp_Object Voverriding_local_map_menu_flag;
101
102extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
103
104extern Lisp_Object Qmenu_bar_update_hook;
105
18686d47 106#ifdef USE_X_TOOLKIT
88766961 107extern void set_frame_menubar ();
78589e07
RS
108extern void process_expose_from_menu ();
109extern XtAppContext Xt_app_con;
110
165e1749 111static Lisp_Object xdialog_show ();
4dedbfe0 112void popup_get_selection ();
18686d47
RS
113#endif
114
78589e07
RS
115static Lisp_Object xmenu_show ();
116static void keymap_panes ();
117static void single_keymap_panes ();
118static void list_of_panes ();
119static void list_of_items ();
120\f
121/* This holds a Lisp vector that holds the results of decoding
122 the keymaps or alist-of-alists that specify a menu.
dcfdbac7 123
78589e07 124 It describes the panes and items within the panes.
dcfdbac7 125
78589e07
RS
126 Each pane is described by 3 elements in the vector:
127 t, the pane name, the pane's prefix key.
a352a815 128 Then follow the pane's items, with 5 elements per item:
78589e07 129 the item string, the enable flag, the item's value,
a352a815 130 the definition, and the equivalent keyboard key's description string.
dcfdbac7 131
101bb4a5
RS
132 In some cases, multiple levels of menus may be described.
133 A single vector slot containing nil indicates the start of a submenu.
134 A single vector slot containing lambda indicates the end of a submenu.
135 The submenu follows a menu item which is the way to reach the submenu.
136
fcaa7665
RS
137 A single vector slot containing quote indicates that the
138 following items should appear on the right of a dialog box.
139
78589e07
RS
140 Using a Lisp vector to hold this information while we decode it
141 takes care of protecting all the data from GC. */
dcfdbac7 142
78589e07
RS
143#define MENU_ITEMS_PANE_NAME 1
144#define MENU_ITEMS_PANE_PREFIX 2
145#define MENU_ITEMS_PANE_LENGTH 3
088831f6 146
78589e07
RS
147#define MENU_ITEMS_ITEM_NAME 0
148#define MENU_ITEMS_ITEM_ENABLE 1
149#define MENU_ITEMS_ITEM_VALUE 2
150#define MENU_ITEMS_ITEM_EQUIV_KEY 3
a352a815
RS
151#define MENU_ITEMS_ITEM_DEFINITION 4
152#define MENU_ITEMS_ITEM_LENGTH 5
7da99777 153
78589e07 154static Lisp_Object menu_items;
18686d47 155
78589e07
RS
156/* Number of slots currently allocated in menu_items. */
157static int menu_items_allocated;
18686d47 158
78589e07
RS
159/* This is the index in menu_items of the first empty slot. */
160static int menu_items_used;
18686d47 161
101bb4a5
RS
162/* The number of panes currently recorded in menu_items,
163 excluding those within submenus. */
78589e07 164static int menu_items_n_panes;
18686d47 165
101bb4a5
RS
166/* Current depth within submenus. */
167static int menu_items_submenu_depth;
168
4dedbfe0 169/* Flag which when set indicates a dialog or menu has been posted by
c98fcf4b 170 Xt on behalf of one of the widget sets. */
4dedbfe0
PR
171static int popup_activated_flag;
172
88766961 173static int next_menubar_widget_id;
745c34fb 174
a9be6839
RS
175/* This is set nonzero after the user activates the menu bar, and set
176 to zero again after the menu bars are redisplayed by prepare_menu_bar.
177 While it is nonzero, all calls to set_frame_menubar go deep.
178
179 I don't understand why this is needed, but it does seem to be
180 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
181
182int pending_menu_activation;
bd3a4da2 183\f
88766961 184#ifdef USE_X_TOOLKIT
bd3a4da2 185
7556890b 186/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
bd3a4da2 187
88766961
RS
188static struct frame *
189menubar_id_to_frame (id)
190 LWLIB_ID id;
191{
192 Lisp_Object tail, frame;
193 FRAME_PTR f;
bd3a4da2 194
88766961 195 for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
bd3a4da2 196 {
88766961
RS
197 frame = XCONS (tail)->car;
198 if (!GC_FRAMEP (frame))
199 continue;
200 f = XFRAME (frame);
54e9e953 201 if (f->output_data.nothing == 1)
88766961 202 continue;
7556890b 203 if (f->output_data.x->id == id)
88766961 204 return f;
bd3a4da2 205 }
88766961 206 return 0;
bd3a4da2 207}
88766961
RS
208
209#endif
4dedbfe0 210\f
78589e07
RS
211/* Initialize the menu_items structure if we haven't already done so.
212 Also mark it as currently empty. */
213
214static void
215init_menu_items ()
216{
217 if (NILP (menu_items))
218 {
219 menu_items_allocated = 60;
220 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
18686d47
RS
221 }
222
78589e07
RS
223 menu_items_used = 0;
224 menu_items_n_panes = 0;
101bb4a5 225 menu_items_submenu_depth = 0;
78589e07 226}
18686d47 227
78589e07
RS
228/* Call at the end of generating the data in menu_items.
229 This fills in the number of items in the last pane. */
1658603c 230
78589e07
RS
231static void
232finish_menu_items ()
233{
234}
1658603c 235
78589e07
RS
236/* Call when finished using the data for the current menu
237 in menu_items. */
1658603c 238
78589e07
RS
239static void
240discard_menu_items ()
241{
242 /* Free the structure if it is especially large.
243 Otherwise, hold on to it, to save time. */
244 if (menu_items_allocated > 200)
245 {
246 menu_items = Qnil;
247 menu_items_allocated = 0;
248 }
249}
1658603c 250
101bb4a5
RS
251/* Make the menu_items vector twice as large. */
252
253static void
254grow_menu_items ()
255{
256 Lisp_Object old;
257 int old_size = menu_items_allocated;
258 old = menu_items;
259
260 menu_items_allocated *= 2;
261 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
262 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
263 old_size * sizeof (Lisp_Object));
264}
265
266/* Begin a submenu. */
267
268static void
269push_submenu_start ()
270{
271 if (menu_items_used + 1 > menu_items_allocated)
272 grow_menu_items ();
273
274 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
275 menu_items_submenu_depth++;
276}
277
278/* End a submenu. */
279
280static void
281push_submenu_end ()
282{
283 if (menu_items_used + 1 > menu_items_allocated)
284 grow_menu_items ();
285
286 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
287 menu_items_submenu_depth--;
288}
289
fcaa7665
RS
290/* Indicate boundary between left and right. */
291
292static void
293push_left_right_boundary ()
294{
295 if (menu_items_used + 1 > menu_items_allocated)
296 grow_menu_items ();
297
298 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
299}
300
78589e07
RS
301/* Start a new menu pane in menu_items..
302 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
1658603c 303
78589e07
RS
304static void
305push_menu_pane (name, prefix_vec)
306 Lisp_Object name, prefix_vec;
307{
308 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
101bb4a5 309 grow_menu_items ();
dcfdbac7 310
101bb4a5
RS
311 if (menu_items_submenu_depth == 0)
312 menu_items_n_panes++;
78589e07
RS
313 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
314 XVECTOR (menu_items)->contents[menu_items_used++] = name;
315 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
316}
dcfdbac7 317
78589e07
RS
318/* Push one menu item into the current pane.
319 NAME is the string to display. ENABLE if non-nil means
320 this item can be selected. KEY is the key generated by
a352a815
RS
321 choosing this item, or nil if this item doesn't really have a definition.
322 DEF is the definition of this item.
323 EQUIV is the textual description of the keyboard equivalent for
324 this item (or nil if none). */
18686d47 325
78589e07 326static void
a352a815
RS
327push_menu_item (name, enable, key, def, equiv)
328 Lisp_Object name, enable, key, def, equiv;
78589e07
RS
329{
330 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
101bb4a5 331 grow_menu_items ();
088831f6 332
78589e07
RS
333 XVECTOR (menu_items)->contents[menu_items_used++] = name;
334 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
335 XVECTOR (menu_items)->contents[menu_items_used++] = key;
336 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
a352a815 337 XVECTOR (menu_items)->contents[menu_items_used++] = def;
78589e07
RS
338}
339\f
340/* Figure out the current keyboard equivalent of a menu item ITEM1.
341 The item string for menu display should be ITEM_STRING.
342 Store the equivalent keyboard key sequence's
343 textual description into *DESCRIP_PTR.
344 Also cache them in the item itself.
345 Return the real definition to execute. */
088831f6 346
78589e07
RS
347static Lisp_Object
348menu_item_equiv_key (item_string, item1, descrip_ptr)
349 Lisp_Object item_string;
350 Lisp_Object item1;
351 Lisp_Object *descrip_ptr;
352{
353 /* This is the real definition--the function to run. */
354 Lisp_Object def;
355 /* This is the sublist that records cached equiv key data
356 so we can save time. */
357 Lisp_Object cachelist;
358 /* These are the saved equivalent keyboard key sequence
359 and its key-description. */
360 Lisp_Object savedkey, descrip;
361 Lisp_Object def1;
362 int changed = 0;
8c512fcb 363 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
088831f6 364
78589e07
RS
365 /* If a help string follows the item string, skip it. */
366 if (CONSP (XCONS (item1)->cdr)
367 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
368 item1 = XCONS (item1)->cdr;
088831f6 369
78589e07 370 def = Fcdr (item1);
088831f6 371
78589e07
RS
372 /* Get out the saved equivalent-keyboard-key info. */
373 cachelist = savedkey = descrip = Qnil;
374 if (CONSP (def) && CONSP (XCONS (def)->car)
375 && (NILP (XCONS (XCONS (def)->car)->car)
376 || VECTORP (XCONS (XCONS (def)->car)->car)))
088831f6 377 {
78589e07
RS
378 cachelist = XCONS (def)->car;
379 def = XCONS (def)->cdr;
380 savedkey = XCONS (cachelist)->car;
381 descrip = XCONS (cachelist)->cdr;
088831f6 382 }
78589e07 383
8c512fcb
RS
384 GCPRO4 (def, def1, savedkey, descrip);
385
78589e07
RS
386 /* Is it still valid? */
387 def1 = Qnil;
388 if (!NILP (savedkey))
389 def1 = Fkey_binding (savedkey, Qnil);
390 /* If not, update it. */
391 if (! EQ (def1, def)
cc1032b3
RS
392 /* If the command is an alias for another
393 (such as easymenu.el and lmenu.el set it up),
c98fcf4b 394 check if the original command matches the cached command. */
cc1032b3
RS
395 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
396 && EQ (def1, XSYMBOL (def)->function))
8fbc986d
RS
397 /* If something had no key binding before, don't recheck it
398 because that is too slow--except if we have a list of rebound
399 commands in Vdefine_key_rebound_commands, do recheck any command
400 that appears in that list. */
401 && (NILP (cachelist) || !NILP (savedkey)
402 || (! EQ (Qt, Vdefine_key_rebound_commands)
403 && !NILP (Fmemq (def, Vdefine_key_rebound_commands)))))
088831f6 404 {
78589e07
RS
405 changed = 1;
406 descrip = Qnil;
10bba266
RS
407 /* If the command is an alias for another
408 (such as easymenu.el and lmenu.el set it up),
409 see if the original command name has equivalent keys. */
67385caa
RS
410 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
411 && ! NILP (Fget (def, Qmenu_alias)))
10bba266
RS
412 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
413 Qnil, Qt, Qnil);
86e00a8a
RS
414 else
415 /* Otherwise look up the specified command itself.
416 We don't try both, because that makes easymenu menus slow. */
417 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
418
78589e07
RS
419 if (!NILP (savedkey))
420 {
421 descrip = Fkey_description (savedkey);
422 descrip = concat2 (make_string (" (", 3), descrip);
423 descrip = concat2 (descrip, make_string (")", 1));
424 }
dcfdbac7 425 }
18686d47 426
78589e07
RS
427 /* Cache the data we just got in a sublist of the menu binding. */
428 if (NILP (cachelist))
62010a73
RS
429 {
430 CHECK_IMPURE (item1);
431 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
432 }
78589e07 433 else if (changed)
dcfdbac7 434 {
78589e07
RS
435 XCONS (cachelist)->car = savedkey;
436 XCONS (cachelist)->cdr = descrip;
dcfdbac7 437 }
18686d47 438
8c512fcb 439 UNGCPRO;
78589e07
RS
440 *descrip_ptr = descrip;
441 return def;
18686d47
RS
442}
443
78589e07
RS
444/* This is used as the handler when calling internal_condition_case_1. */
445
446static Lisp_Object
447menu_item_enabled_p_1 (arg)
448 Lisp_Object arg;
18686d47 449{
37a98547
RS
450 /* If we got a quit from within the menu computation,
451 quit all the way out of it. This takes care of C-] in the debugger. */
452 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
453 Fsignal (Qquit, Qnil);
454
78589e07 455 return Qnil;
dcfdbac7
JB
456}
457
78589e07 458/* Return non-nil if the command DEF is enabled when used as a menu item.
101bb4a5
RS
459 This is based on looking for a menu-enable property.
460 If NOTREAL is set, don't bother really computing this. */
78589e07
RS
461
462static Lisp_Object
101bb4a5 463menu_item_enabled_p (def, notreal)
78589e07 464 Lisp_Object def;
0f5e911d 465 int notreal;
18686d47 466{
78589e07 467 Lisp_Object enabled, tem;
18686d47 468
78589e07 469 enabled = Qt;
101bb4a5
RS
470 if (notreal)
471 return enabled;
b5bb2705 472 if (SYMBOLP (def))
78589e07
RS
473 {
474 /* No property, or nil, means enable.
475 Otherwise, enable if value is not nil. */
476 tem = Fget (def, Qmenu_enable);
477 if (!NILP (tem))
478 /* (condition-case nil (eval tem)
479 (error nil)) */
480 enabled = internal_condition_case_1 (Feval, tem, Qerror,
481 menu_item_enabled_p_1);
482 }
483 return enabled;
484}
485\f
486/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
101bb4a5
RS
487 and generate menu panes for them in menu_items.
488 If NOTREAL is nonzero,
489 don't bother really computing whether an item is enabled. */
18686d47 490
78589e07 491static void
101bb4a5 492keymap_panes (keymaps, nmaps, notreal)
78589e07
RS
493 Lisp_Object *keymaps;
494 int nmaps;
101bb4a5 495 int notreal;
18686d47 496{
78589e07 497 int mapno;
18686d47 498
78589e07 499 init_menu_items ();
18686d47 500
78589e07
RS
501 /* Loop over the given keymaps, making a pane for each map.
502 But don't make a pane that is empty--ignore that map instead.
503 P is the number of panes we have made so far. */
504 for (mapno = 0; mapno < nmaps; mapno++)
101bb4a5 505 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
78589e07
RS
506
507 finish_menu_items ();
508}
509
510/* This is a recursive subroutine of keymap_panes.
511 It handles one keymap, KEYMAP.
512 The other arguments are passed along
101bb4a5
RS
513 or point to local variables of the previous function.
514 If NOTREAL is nonzero,
515 don't bother really computing whether an item is enabled. */
78589e07
RS
516
517static void
101bb4a5 518single_keymap_panes (keymap, pane_name, prefix, notreal)
78589e07
RS
519 Lisp_Object keymap;
520 Lisp_Object pane_name;
521 Lisp_Object prefix;
101bb4a5 522 int notreal;
78589e07
RS
523{
524 Lisp_Object pending_maps;
525 Lisp_Object tail, item, item1, item_string, table;
526 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
527
528 pending_maps = Qnil;
529
530 push_menu_pane (pane_name, prefix);
531
b5bb2705 532 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
18686d47 533 {
78589e07
RS
534 /* Look at each key binding, and if it has a menu string,
535 make a menu item from it. */
536 item = XCONS (tail)->car;
b5bb2705 537 if (CONSP (item))
18686d47 538 {
78589e07 539 item1 = XCONS (item)->cdr;
b5bb2705 540 if (CONSP (item1))
78589e07
RS
541 {
542 item_string = XCONS (item1)->car;
b5bb2705 543 if (STRINGP (item_string))
78589e07
RS
544 {
545 /* This is the real definition--the function to run. */
546 Lisp_Object def;
547 /* These are the saved equivalent keyboard key sequence
548 and its key-description. */
549 Lisp_Object descrip;
550 Lisp_Object tem, enabled;
551
8c512fcb
RS
552 /* GCPRO because ...enabled_p will call eval
553 and ..._equiv_key may autoload something.
78589e07
RS
554 Protecting KEYMAP preserves everything we use;
555 aside from that, must protect whatever might be
556 a string. Since there's no GCPRO5, we refetch
557 item_string instead of protecting it. */
8c512fcb 558 descrip = def = Qnil;
78589e07 559 GCPRO4 (keymap, pending_maps, def, descrip);
8c512fcb
RS
560
561 def = menu_item_equiv_key (item_string, item1, &descrip);
101bb4a5
RS
562 enabled = menu_item_enabled_p (def, notreal);
563
78589e07
RS
564 UNGCPRO;
565
566 item_string = XCONS (item1)->car;
567
568 tem = Fkeymapp (def);
569 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
570 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
571 pending_maps);
572 else
101bb4a5
RS
573 {
574 Lisp_Object submap;
bc28c440 575 GCPRO4 (keymap, pending_maps, descrip, item_string);
101bb4a5 576 submap = get_keymap_1 (def, 0, 1);
bc28c440 577 UNGCPRO;
101bb4a5
RS
578#ifndef USE_X_TOOLKIT
579 /* Indicate visually that this is a submenu. */
580 if (!NILP (submap))
581 item_string = concat2 (item_string,
582 build_string (" >"));
583#endif
a352a815
RS
584 /* If definition is nil, pass nil as the key. */
585 push_menu_item (item_string, enabled,
586 XCONS (item)->car, def,
101bb4a5
RS
587 descrip);
588#ifdef USE_X_TOOLKIT
589 /* Display a submenu using the toolkit. */
590 if (! NILP (submap))
591 {
592 push_submenu_start ();
593 single_keymap_panes (submap, Qnil,
594 XCONS (item)->car, notreal);
595 push_submenu_end ();
596 }
597#endif
598 }
78589e07
RS
599 }
600 }
601 }
b5bb2705 602 else if (VECTORP (item))
78589e07
RS
603 {
604 /* Loop over the char values represented in the vector. */
605 int len = XVECTOR (item)->size;
606 int c;
607 for (c = 0; c < len; c++)
608 {
609 Lisp_Object character;
33b43fa6 610 XSETFASTINT (character, c);
78589e07 611 item1 = XVECTOR (item)->contents[c];
b5bb2705 612 if (CONSP (item1))
78589e07
RS
613 {
614 item_string = XCONS (item1)->car;
b5bb2705 615 if (STRINGP (item_string))
78589e07
RS
616 {
617 Lisp_Object def;
618
619 /* These are the saved equivalent keyboard key sequence
620 and its key-description. */
621 Lisp_Object descrip;
622 Lisp_Object tem, enabled;
623
8c512fcb
RS
624 /* GCPRO because ...enabled_p will call eval
625 and ..._equiv_key may autoload something.
78589e07
RS
626 Protecting KEYMAP preserves everything we use;
627 aside from that, must protect whatever might be
628 a string. Since there's no GCPRO5, we refetch
629 item_string instead of protecting it. */
630 GCPRO4 (keymap, pending_maps, def, descrip);
8c512fcb
RS
631 descrip = def = Qnil;
632
633 def = menu_item_equiv_key (item_string, item1, &descrip);
101bb4a5 634 enabled = menu_item_enabled_p (def, notreal);
8c512fcb 635
78589e07
RS
636 UNGCPRO;
637
638 item_string = XCONS (item1)->car;
639
640 tem = Fkeymapp (def);
641 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
642 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
643 pending_maps);
644 else
101bb4a5
RS
645 {
646 Lisp_Object submap;
bc28c440 647 GCPRO4 (keymap, pending_maps, descrip, item_string);
101bb4a5 648 submap = get_keymap_1 (def, 0, 1);
bc28c440 649 UNGCPRO;
101bb4a5
RS
650#ifndef USE_X_TOOLKIT
651 if (!NILP (submap))
652 item_string = concat2 (item_string,
653 build_string (" >"));
654#endif
a352a815 655 /* If definition is nil, pass nil as the key. */
101bb4a5 656 push_menu_item (item_string, enabled, character,
a352a815 657 def, descrip);
101bb4a5
RS
658#ifdef USE_X_TOOLKIT
659 if (! NILP (submap))
660 {
661 push_submenu_start ();
662 single_keymap_panes (submap, Qnil,
663 character, notreal);
664 push_submenu_end ();
665 }
666#endif
667 }
78589e07
RS
668 }
669 }
670 }
18686d47
RS
671 }
672 }
78589e07
RS
673
674 /* Process now any submenus which want to be panes at this level. */
675 while (!NILP (pending_maps))
676 {
101bb4a5 677 Lisp_Object elt, eltcdr, string;
78589e07
RS
678 elt = Fcar (pending_maps);
679 eltcdr = XCONS (elt)->cdr;
101bb4a5
RS
680 string = XCONS (eltcdr)->car;
681 /* We no longer discard the @ from the beginning of the string here.
682 Instead, we do this in xmenu_show. */
683 single_keymap_panes (Fcar (elt), string,
684 XCONS (eltcdr)->cdr, notreal);
78589e07
RS
685 pending_maps = Fcdr (pending_maps);
686 }
18686d47 687}
78589e07 688\f
8e6208c5 689/* Push all the panes and items of a menu described by the
78589e07
RS
690 alist-of-alists MENU.
691 This handles old-fashioned calls to x-popup-menu. */
18686d47 692
78589e07
RS
693static void
694list_of_panes (menu)
18686d47 695 Lisp_Object menu;
18686d47 696{
78589e07
RS
697 Lisp_Object tail;
698
699 init_menu_items ();
700
701 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
702 {
703 Lisp_Object elt, pane_name, pane_data;
704 elt = Fcar (tail);
705 pane_name = Fcar (elt);
706 CHECK_STRING (pane_name, 0);
707 push_menu_pane (pane_name, Qnil);
708 pane_data = Fcdr (elt);
709 CHECK_CONS (pane_data, 0);
710 list_of_items (pane_data);
711 }
712
713 finish_menu_items ();
714}
715
716/* Push the items in a single pane defined by the alist PANE. */
717
718static void
719list_of_items (pane)
720 Lisp_Object pane;
721{
722 Lisp_Object tail, item, item1;
723
724 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
725 {
726 item = Fcar (tail);
727 if (STRINGP (item))
a352a815 728 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
fcaa7665
RS
729 else if (NILP (item))
730 push_left_right_boundary ();
78589e07
RS
731 else
732 {
733 CHECK_CONS (item, 0);
734 item1 = Fcar (item);
735 CHECK_STRING (item1, 1);
a352a815 736 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
78589e07
RS
737 }
738 }
739}
740\f
540e52d1 741DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
78589e07
RS
742 "Pop up a deck-of-cards menu and return user's selection.\n\
743POSITION is a position specification. This is either a mouse button event\n\
744or a list ((XOFFSET YOFFSET) WINDOW)\n\
748a0e1f 745where XOFFSET and YOFFSET are positions in pixels from the top left\n\
78589e07
RS
746corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
747This controls the position of the center of the first line\n\
748in the first pane of the menu, not the top left of the menu as a whole.\n\
749If POSITION is t, it means to use the current mouse position.\n\
750\n\
751MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
752The menu items come from key bindings that have a menu string as well as\n\
753a definition; actually, the \"definition\" in such a key binding looks like\n\
754\(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
755the keymap as a top-level element.\n\n\
756You can also use a list of keymaps as MENU.\n\
757 Then each keymap makes a separate pane.\n\
758When MENU is a keymap or a list of keymaps, the return value\n\
759is a list of events.\n\n\
760Alternatively, you can specify a menu of multiple panes\n\
761 with a list of the form (TITLE PANE1 PANE2...),\n\
762where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
763Each ITEM is normally a cons cell (STRING . VALUE);\n\
764but a string can appear as an item--that makes a nonselectable line\n\
765in the menu.\n\
766With this form of menu, the return value is VALUE from the chosen item.\n\
767\n\
768If POSITION is nil, don't display the menu at all, just precalculate the\n\
769cached information about equivalent key sequences.")
770 (position, menu)
771 Lisp_Object position, menu;
772{
773 int number_of_panes, panes;
18686d47 774 Lisp_Object keymap, tem;
78589e07
RS
775 int xpos, ypos;
776 Lisp_Object title;
777 char *error_name;
778 Lisp_Object selection;
18686d47 779 int i, j;
78589e07
RS
780 FRAME_PTR f;
781 Lisp_Object x, y, window;
782 int keymaps = 0;
9685a93f 783 int for_click = 0;
78589e07
RS
784 struct gcpro gcpro1;
785
1e659e4c 786#ifdef HAVE_MENUS
78589e07
RS
787 if (! NILP (position))
788 {
101bb4a5
RS
789 check_x ();
790
78589e07 791 /* Decode the first argument: find the window and the coordinates. */
9572375b
KH
792 if (EQ (position, Qt)
793 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
78589e07
RS
794 {
795 /* Use the mouse's current position. */
ac8f8f7d 796 FRAME_PTR new_f = selected_frame;
78589e07
RS
797 Lisp_Object bar_window;
798 int part;
799 unsigned long time;
800
b137c582 801 if (mouse_position_hook)
46b657e2
RS
802 (*mouse_position_hook) (&new_f, 1, &bar_window,
803 &part, &x, &y, &time);
5ca2ef64 804 if (new_f != 0)
a39f0477 805 XSETFRAME (window, new_f);
5ca2ef64
RS
806 else
807 {
808 window = selected_window;
33b43fa6
KH
809 XSETFASTINT (x, 0);
810 XSETFASTINT (y, 0);
5ca2ef64 811 }
78589e07
RS
812 }
813 else
814 {
815 tem = Fcar (position);
b5bb2705 816 if (CONSP (tem))
78589e07
RS
817 {
818 window = Fcar (Fcdr (position));
819 x = Fcar (tem);
820 y = Fcar (Fcdr (tem));
821 }
822 else
823 {
9685a93f 824 for_click = 1;
78589e07
RS
825 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
826 window = Fcar (tem); /* POSN_WINDOW (tem) */
827 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
828 x = Fcar (tem);
829 y = Fcdr (tem);
78589e07
RS
830 }
831 }
832
833 CHECK_NUMBER (x, 0);
834 CHECK_NUMBER (y, 0);
835
836 /* Decode where to put the menu. */
837
b5bb2705 838 if (FRAMEP (window))
78589e07
RS
839 {
840 f = XFRAME (window);
78589e07
RS
841 xpos = 0;
842 ypos = 0;
843 }
b5bb2705 844 else if (WINDOWP (window))
78589e07
RS
845 {
846 CHECK_LIVE_WINDOW (window, 0);
847 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
848
7556890b
RS
849 xpos = (FONT_WIDTH (f->output_data.x->font) * XWINDOW (window)->left);
850 ypos = (f->output_data.x->line_height * XWINDOW (window)->top);
78589e07
RS
851 }
852 else
853 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
854 but I don't want to make one now. */
855 CHECK_WINDOW (window, 0);
856
857 xpos += XINT (x);
858 ypos += XINT (y);
859 }
1e659e4c 860#endif /* HAVE_MENUS */
78589e07
RS
861
862 title = Qnil;
863 GCPRO1 (title);
864
865 /* Decode the menu items from what was specified. */
18686d47
RS
866
867 keymap = Fkeymapp (menu);
868 tem = Qnil;
b5bb2705 869 if (CONSP (menu))
18686d47
RS
870 tem = Fkeymapp (Fcar (menu));
871 if (!NILP (keymap))
872 {
873 /* We were given a keymap. Extract menu info from the keymap. */
874 Lisp_Object prompt;
875 keymap = get_keymap (menu);
876
78589e07 877 /* Extract the detailed info to make one pane. */
101bb4a5 878 keymap_panes (&menu, 1, NILP (position));
78589e07 879
18686d47
RS
880 /* Search for a string appearing directly as an element of the keymap.
881 That string is the title of the menu. */
882 prompt = map_prompt (keymap);
18686d47 883
78589e07
RS
884 /* Make that be the pane title of the first pane. */
885 if (!NILP (prompt) && menu_items_n_panes >= 0)
886 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
887
888 keymaps = 1;
18686d47
RS
889 }
890 else if (!NILP (tem))
891 {
892 /* We were given a list of keymaps. */
18686d47
RS
893 int nmaps = XFASTINT (Flength (menu));
894 Lisp_Object *maps
895 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
896 int i;
78589e07
RS
897
898 title = Qnil;
18686d47
RS
899
900 /* The first keymap that has a prompt string
901 supplies the menu title. */
b5bb2705 902 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
18686d47 903 {
78589e07
RS
904 Lisp_Object prompt;
905
18686d47
RS
906 maps[i++] = keymap = get_keymap (Fcar (tem));
907
908 prompt = map_prompt (keymap);
78589e07
RS
909 if (NILP (title) && !NILP (prompt))
910 title = prompt;
18686d47
RS
911 }
912
913 /* Extract the detailed info to make one pane. */
101bb4a5 914 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
915
916 /* Make the title be the pane title of the first pane. */
917 if (!NILP (title) && menu_items_n_panes >= 0)
918 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
919
920 keymaps = 1;
18686d47
RS
921 }
922 else
923 {
924 /* We were given an old-fashioned menu. */
78589e07
RS
925 title = Fcar (menu);
926 CHECK_STRING (title, 1);
18686d47 927
78589e07 928 list_of_panes (Fcdr (menu));
18686d47 929
78589e07
RS
930 keymaps = 0;
931 }
18686d47 932
78589e07 933 if (NILP (position))
18686d47 934 {
78589e07
RS
935 discard_menu_items ();
936 UNGCPRO;
937 return Qnil;
18686d47
RS
938 }
939
1e659e4c 940#ifdef HAVE_MENUS
78589e07
RS
941 /* Display them in a menu. */
942 BLOCK_INPUT;
18686d47 943
673a6211 944 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
945 keymaps, title, &error_name);
946 UNBLOCK_INPUT;
18686d47 947
78589e07 948 discard_menu_items ();
18686d47 949
78589e07 950 UNGCPRO;
1e659e4c 951#endif /* HAVE_MENUS */
18686d47 952
78589e07
RS
953 if (error_name) error (error_name);
954 return selection;
18686d47 955}
165e1749 956
1e659e4c
RS
957#ifdef HAVE_MENUS
958
540e52d1 959DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
165e1749 960 "Pop up a dialog box and return user's selection.\n\
99fe880d
RS
961POSITION specifies which frame to use.\n\
962This is normally a mouse button event or a window or frame.\n\
963If POSITION is t, it means to use the frame the mouse is on.\n\
964The dialog box appears in the middle of the specified frame.\n\
165e1749 965\n\
99fe880d
RS
966CONTENTS specifies the alternatives to display in the dialog box.\n\
967It is a list of the form (TITLE ITEM1 ITEM2...).\n\
968Each ITEM is a cons cell (STRING . VALUE).\n\
fcaa7665
RS
969The return value is VALUE from the chosen item.\n\n\
970An ITEM may also be just a string--that makes a nonselectable item.\n\
971An ITEM may also be nil--that means to put all preceding items\n\
972on the left of the dialog box and all following items on the right.\n\
973\(By default, approximately half appear on each side.)")
99fe880d
RS
974 (position, contents)
975 Lisp_Object position, contents;
165e1749 976{
165e1749 977 FRAME_PTR f;
99fe880d 978 Lisp_Object window;
165e1749
FP
979
980 check_x ();
981
99fe880d 982 /* Decode the first argument: find the window or frame to use. */
88d4f6ec
KH
983 if (EQ (position, Qt)
984 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
165e1749 985 {
b14db4d7 986#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 987 /* Use the mouse's current position. */
ac8f8f7d 988 FRAME_PTR new_f = selected_frame;
99fe880d
RS
989 Lisp_Object bar_window;
990 int part;
991 unsigned long time;
992 Lisp_Object x, y;
165e1749 993
46b657e2 994 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 995
99fe880d 996 if (new_f != 0)
a39f0477 997 XSETFRAME (window, new_f);
99fe880d
RS
998 else
999 window = selected_window;
b14db4d7 1000#endif
88d4f6ec 1001 window = selected_window;
99fe880d
RS
1002 }
1003 else if (CONSP (position))
1004 {
1005 Lisp_Object tem;
1006 tem = Fcar (position);
b5bb2705 1007 if (CONSP (tem))
99fe880d 1008 window = Fcar (Fcdr (position));
80670155
RS
1009 else
1010 {
99fe880d
RS
1011 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1012 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 1013 }
165e1749 1014 }
99fe880d
RS
1015 else if (WINDOWP (position) || FRAMEP (position))
1016 window = position;
a9be6839
RS
1017 else
1018 window = Qnil;
165e1749 1019
99fe880d 1020 /* Decode where to put the menu. */
165e1749 1021
b5bb2705 1022 if (FRAMEP (window))
99fe880d 1023 f = XFRAME (window);
b5bb2705 1024 else if (WINDOWP (window))
165e1749 1025 {
99fe880d
RS
1026 CHECK_LIVE_WINDOW (window, 0);
1027 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 1028 }
99fe880d
RS
1029 else
1030 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1031 but I don't want to make one now. */
1032 CHECK_WINDOW (window, 0);
165e1749 1033
99fe880d
RS
1034#ifndef USE_X_TOOLKIT
1035 /* Display a menu with these alternatives
1036 in the middle of frame F. */
1037 {
1038 Lisp_Object x, y, frame, newpos;
a39f0477
KH
1039 XSETFRAME (frame, f);
1040 XSETINT (x, x_pixel_width (f) / 2);
1041 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
1042 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1043
1044 return Fx_popup_menu (newpos,
1045 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1046 }
1047#else
1048 {
1049 Lisp_Object title;
1050 char *error_name;
1051 Lisp_Object selection;
165e1749 1052
99fe880d
RS
1053 /* Decode the dialog items from what was specified. */
1054 title = Fcar (contents);
1055 CHECK_STRING (title, 1);
165e1749 1056
99fe880d 1057 list_of_panes (Fcons (contents, Qnil));
165e1749 1058
99fe880d
RS
1059 /* Display them in a dialog box. */
1060 BLOCK_INPUT;
f8c2630d 1061 selection = xdialog_show (f, 0, title, &error_name);
99fe880d 1062 UNBLOCK_INPUT;
165e1749 1063
99fe880d
RS
1064 discard_menu_items ();
1065
1066 if (error_name) error (error_name);
1067 return selection;
1068 }
7464b131 1069#endif
392d3f4b 1070}
78589e07
RS
1071\f
1072#ifdef USE_X_TOOLKIT
18686d47 1073
4dedbfe0 1074/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2
RS
1075 popped down (deactivated). This is used for x-popup-menu
1076 and x-popup-dialog; it is not used for the menu bar any more.
c98fcf4b 1077
2e2b8e22 1078 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1079 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1080
4dedbfe0 1081void
2e2b8e22 1082popup_get_selection (initial_event, dpyinfo, id)
4dedbfe0 1083 XEvent *initial_event;
aa669def 1084 struct x_display_info *dpyinfo;
2e2b8e22 1085 LWLIB_ID id;
78589e07 1086{
4dedbfe0 1087 XEvent event;
78589e07 1088
aa669def
RS
1089 /* Define a queue to save up for later unreading
1090 all X events that don't pertain to the menu. */
1091 struct event_queue
1092 {
1093 XEvent event;
1094 struct event_queue *next;
1095 };
1096
1097 struct event_queue *queue = NULL;
1098 struct event_queue *queue_tmp;
1099
4dedbfe0
PR
1100 if (initial_event)
1101 event = *initial_event;
1102 else
1103 XtAppNextEvent (Xt_app_con, &event);
78589e07 1104
4dedbfe0 1105 while (1)
78589e07 1106 {
aa669def
RS
1107 /* Handle expose events for editor frames right away. */
1108 if (event.type == Expose)
1109 process_expose_from_menu (event);
10624005
KH
1110 /* Make sure we don't consider buttons grabbed after menu goes.
1111 And make sure to deactivate for any ButtonRelease,
1112 even if XtDispatchEvent doesn't do that. */
aa669def
RS
1113 else if (event.type == ButtonRelease
1114 && dpyinfo->display == event.xbutton.display)
10624005
KH
1115 {
1116 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1117 popup_activated_flag = 0;
c8b5aa3d
RS
1118#ifdef USE_MOTIF /* Pretending that the event came from a
1119 Btn1Down seems the only way to convince Motif to
1120 activate its callbacks; setting the XmNmenuPost
1121 isn't working. --marcus@sysc.pdx.edu. */
1122 event.xbutton.button = 1;
1123#endif
10624005 1124 }
2e2b8e22
KH
1125 /* If the user presses a key, deactivate the menu.
1126 The user is likely to do that if we get wedged. */
1127 else if (event.type == KeyPress
1128 && dpyinfo->display == event.xbutton.display)
1129 {
2e2b8e22
KH
1130 popup_activated_flag = 0;
1131 break;
1132 }
d31d42cc
RS
1133 /* Button presses outside the menu also pop it down. */
1134 else if (event.type == ButtonPress
1135 && event.xany.display == dpyinfo->display
1136 && x_any_window_to_frame (dpyinfo, event.xany.window))
1137 {
1138 popup_activated_flag = 0;
1139 break;
1140 }
aa669def
RS
1141
1142 /* Queue all events not for this popup,
10624005 1143 except for Expose, which we've already handled, and ButtonRelease.
9572375b
KH
1144 Note that the X window is associated with the frame if this
1145 is a menu bar popup, but not if it's a dialog box. So we use
1146 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
aa669def 1147 if (event.type != Expose
10624005
KH
1148 && !(event.type == ButtonRelease
1149 && dpyinfo->display == event.xbutton.display)
aa669def 1150 && (event.xany.display != dpyinfo->display
9572375b 1151 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
aa669def 1152 {
aa669def
RS
1153 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1154
1155 if (queue_tmp != NULL)
1156 {
1157 queue_tmp->event = event;
1158 queue_tmp->next = queue;
1159 queue = queue_tmp;
1160 }
1161 }
9572375b
KH
1162 else
1163 XtDispatchEvent (&event);
aa669def
RS
1164
1165 if (!popup_activated ())
4dedbfe0
PR
1166 break;
1167 XtAppNextEvent (Xt_app_con, &event);
78589e07 1168 }
aa669def
RS
1169
1170 /* Unread any events that we got but did not handle. */
1171 while (queue != NULL)
1172 {
1173 queue_tmp = queue;
1174 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1175 queue = queue_tmp->next;
1176 free ((char *)queue_tmp);
1177 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1178 interrupt_input_pending = 1;
1179 }
78589e07
RS
1180}
1181
88766961
RS
1182/* Activate the menu bar of frame F.
1183 This is called from keyboard.c when it gets the
1184 menu_bar_activate_event out of the Emacs event queue.
1185
1186 To activate the menu bar, we use the X button-press event
ac78b144 1187 that was saved in saved_menu_event.
88766961
RS
1188 That makes the toolkit do its thing.
1189
1190 But first we recompute the menu bar contents (the whole tree).
1191
1192 The reason for saving the button event until here, instead of
1193 passing it to the toolkit right away, is that we can safely
1194 execute Lisp code. */
1195
1196x_activate_menubar (f)
1197 FRAME_PTR f;
1198{
ac78b144 1199 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1200 return;
1201
a9be6839 1202 set_frame_menubar (f, 0, 1);
88766961 1203 BLOCK_INPUT;
ac78b144 1204 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
88766961 1205 UNBLOCK_INPUT;
a9be6839 1206#ifdef USE_MOTIF
745c34fb
RS
1207 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1208 pending_menu_activation = 1;
a9be6839 1209#endif
745c34fb 1210
88766961 1211 /* Ignore this if we get it a second time. */
ac78b144 1212 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1213}
1214
c98fcf4b 1215/* Detect if a dialog or menu has been posted. */
aa669def 1216
4dedbfe0
PR
1217int
1218popup_activated ()
1219{
1220 return popup_activated_flag;
1221}
1222
1223
1224/* This callback is invoked when the user selects a menubar cascade
1225 pushbutton, but before the pulldown menu is posted. */
78589e07
RS
1226
1227static void
4dedbfe0 1228popup_activate_callback (widget, id, client_data)
78589e07
RS
1229 Widget widget;
1230 LWLIB_ID id;
1231 XtPointer client_data;
1232{
4dedbfe0 1233 popup_activated_flag = 1;
78589e07
RS
1234}
1235
4dedbfe0
PR
1236/* This callback is called from the menu bar pulldown menu
1237 when the user makes a selection.
1238 Figure out what the user chose
1239 and put the appropriate events into the keyboard buffer. */
1240
78589e07 1241static void
4dedbfe0 1242menubar_selection_callback (widget, id, client_data)
78589e07
RS
1243 Widget widget;
1244 LWLIB_ID id;
1245 XtPointer client_data;
1246{
c63f6952 1247 Lisp_Object prefix, entry;
88766961 1248 FRAME_PTR f = menubar_id_to_frame (id);
4dedbfe0
PR
1249 Lisp_Object vector;
1250 Lisp_Object *subprefix_stack;
1251 int submenu_depth = 0;
1252 int i;
1253
1254 if (!f)
1255 return;
1256 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1257 vector = f->menu_bar_vector;
1258 prefix = Qnil;
1259 i = 0;
1260 while (i < f->menu_bar_items_used)
1261 {
4dedbfe0
PR
1262 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1263 {
1264 subprefix_stack[submenu_depth++] = prefix;
1265 prefix = entry;
1266 i++;
1267 }
1268 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1269 {
1270 prefix = subprefix_stack[--submenu_depth];
1271 i++;
1272 }
1273 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1274 {
4cb35c39 1275 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1276 i += MENU_ITEMS_PANE_LENGTH;
1277 }
1278 else
1279 {
4cb35c39 1280 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1281 /* The EMACS_INT cast avoids a warning. There's no problem
1282 as long as pointers have enough bits to hold small integers. */
1283 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1284 {
1285 int j;
1286 struct input_event buf;
4cb35c39 1287 Lisp_Object frame;
4dedbfe0 1288
4cb35c39 1289 XSETFRAME (frame, f);
4dedbfe0 1290 buf.kind = menu_bar_event;
9572375b 1291 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
4dedbfe0
PR
1292 kbd_buffer_store_event (&buf);
1293
1294 for (j = 0; j < submenu_depth; j++)
1295 if (!NILP (subprefix_stack[j]))
1296 {
1297 buf.kind = menu_bar_event;
4cb35c39 1298 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
4dedbfe0
PR
1299 kbd_buffer_store_event (&buf);
1300 }
1301
1302 if (!NILP (prefix))
1303 {
1304 buf.kind = menu_bar_event;
4cb35c39 1305 buf.frame_or_window = Fcons (frame, prefix);
4dedbfe0
PR
1306 kbd_buffer_store_event (&buf);
1307 }
1308
1309 buf.kind = menu_bar_event;
4cb35c39 1310 buf.frame_or_window = Fcons (frame, entry);
4dedbfe0
PR
1311 kbd_buffer_store_event (&buf);
1312
1313 return;
1314 }
1315 i += MENU_ITEMS_ITEM_LENGTH;
1316 }
1317 }
18686d47
RS
1318}
1319
4dedbfe0 1320/* This callback is invoked when a dialog or menu is finished being
c98fcf4b 1321 used and has been unposted. */
4dedbfe0 1322
165e1749 1323static void
4dedbfe0 1324popup_deactivate_callback (widget, id, client_data)
165e1749
FP
1325 Widget widget;
1326 LWLIB_ID id;
1327 XtPointer client_data;
1328{
4dedbfe0 1329 popup_activated_flag = 0;
165e1749
FP
1330}
1331
f9655c60
RS
1332/* Allocate a widget_value, blocking input. */
1333
1334widget_value *
1335xmalloc_widget_value ()
1336{
1337 widget_value *value;
1338
1339 BLOCK_INPUT;
1340 value = malloc_widget_value ();
1341 UNBLOCK_INPUT;
1342
1343 return value;
1344}
4dedbfe0
PR
1345
1346/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1347 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1348 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1349 must be left alone. */
1350
18686d47
RS
1351void
1352free_menubar_widget_value_tree (wv)
1353 widget_value *wv;
1354{
1355 if (! wv) return;
18686d47
RS
1356
1357 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1358
1359 if (wv->contents && (wv->contents != (widget_value*)1))
1360 {
1361 free_menubar_widget_value_tree (wv->contents);
1362 wv->contents = (widget_value *) 0xDEADBEEF;
1363 }
1364 if (wv->next)
1365 {
1366 free_menubar_widget_value_tree (wv->next);
1367 wv->next = (widget_value *) 0xDEADBEEF;
1368 }
1369 BLOCK_INPUT;
1370 free_widget_value (wv);
1371 UNBLOCK_INPUT;
1372}
4dedbfe0
PR
1373\f
1374/* Return a tree of widget_value structures for a menu bar item
1375 whose event type is ITEM_KEY (with string ITEM_NAME)
1376 and whose contents come from the list of keymaps MAPS. */
1377
1378static widget_value *
1379single_submenu (item_key, item_name, maps)
1380 Lisp_Object item_key, item_name, maps;
1381{
1382 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1383 int i;
1384 int submenu_depth = 0;
1385 Lisp_Object length;
1386 int len;
1387 Lisp_Object *mapvec;
1388 widget_value **submenu_stack;
1389 int mapno;
1390 int previous_items = menu_items_used;
71dca3e3 1391 int top_level_items = 0;
4dedbfe0
PR
1392
1393 length = Flength (maps);
1394 len = XINT (length);
1395
1396 /* Convert the list MAPS into a vector MAPVEC. */
1397 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1398 for (i = 0; i < len; i++)
1399 {
1400 mapvec[i] = Fcar (maps);
1401 maps = Fcdr (maps);
1402 }
1403
1404 menu_items_n_panes = 0;
1405
1406 /* Loop over the given keymaps, making a pane for each map.
1407 But don't make a pane that is empty--ignore that map instead. */
1408 for (i = 0; i < len; i++)
71dca3e3 1409 {
846e8c10
RS
1410 if (SYMBOLP (mapvec[i])
1411 || (CONSP (mapvec[i])
1412 && NILP (Fkeymapp (mapvec[i]))))
71dca3e3 1413 {
846e8c10
RS
1414 /* Here we have a command at top level in the menu bar
1415 as opposed to a submenu. */
71dca3e3
RS
1416 top_level_items = 1;
1417 push_menu_pane (Qnil, Qnil);
1418 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1419 }
1420 else
1421 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1422 }
4dedbfe0
PR
1423
1424 /* Create a tree of widget_value objects
1425 representing the panes and their items. */
1426
1427 submenu_stack
1428 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1429 wv = xmalloc_widget_value ();
4dedbfe0
PR
1430 wv->name = "menu";
1431 wv->value = 0;
1432 wv->enabled = 1;
1433 first_wv = wv;
1434 save_wv = 0;
71dca3e3 1435 prev_wv = 0;
4dedbfe0
PR
1436
1437 /* Loop over all panes and items made during this call
1438 and construct a tree of widget_value objects.
1439 Ignore the panes and items made by previous calls to
1440 single_submenu, even though those are also in menu_items. */
1441 i = previous_items;
1442 while (i < menu_items_used)
1443 {
1444 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1445 {
1446 submenu_stack[submenu_depth++] = save_wv;
1447 save_wv = prev_wv;
1448 prev_wv = 0;
1449 i++;
1450 }
1451 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1452 {
1453 prev_wv = save_wv;
1454 save_wv = submenu_stack[--submenu_depth];
1455 i++;
1456 }
1457 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1458 && submenu_depth != 0)
1459 i += MENU_ITEMS_PANE_LENGTH;
1460 /* Ignore a nil in the item list.
1461 It's meaningful only for dialog boxes. */
1462 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1463 i += 1;
1464 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1465 {
1466 /* Create a new pane. */
1467 Lisp_Object pane_name, prefix;
1468 char *pane_string;
1469 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1470 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1471 pane_string = (NILP (pane_name)
1472 ? "" : (char *) XSTRING (pane_name)->data);
1473 /* If there is just one top-level pane, put all its items directly
1474 under the top-level menu. */
1475 if (menu_items_n_panes == 1)
1476 pane_string = "";
1477
1478 /* If the pane has a meaningful name,
1479 make the pane a top-level menu item
1480 with its items as a submenu beneath it. */
1481 if (strcmp (pane_string, ""))
1482 {
f7fab165 1483 wv = xmalloc_widget_value ();
4dedbfe0
PR
1484 if (save_wv)
1485 save_wv->next = wv;
1486 else
1487 first_wv->contents = wv;
1488 wv->name = pane_string;
ffcb5a51
RS
1489 /* Ignore the @ that means "separate pane".
1490 This is a kludge, but this isn't worth more time. */
1491 if (!NILP (prefix) && wv->name[0] == '@')
4dedbfe0
PR
1492 wv->name++;
1493 wv->value = 0;
1494 wv->enabled = 1;
1495 }
1496 save_wv = wv;
1497 prev_wv = 0;
1498 i += MENU_ITEMS_PANE_LENGTH;
1499 }
1500 else
1501 {
1502 /* Create a new item within current pane. */
a352a815 1503 Lisp_Object item_name, enable, descrip, def;
4dedbfe0
PR
1504 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1505 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1506 descrip
1507 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 1508 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
18686d47 1509
f7fab165 1510 wv = xmalloc_widget_value ();
4dedbfe0
PR
1511 if (prev_wv)
1512 prev_wv->next = wv;
71dca3e3 1513 else
4dedbfe0 1514 save_wv->contents = wv;
71dca3e3 1515
4dedbfe0
PR
1516 wv->name = (char *) XSTRING (item_name)->data;
1517 if (!NILP (descrip))
1518 wv->key = (char *) XSTRING (descrip)->data;
1519 wv->value = 0;
01d5e892
RS
1520 /* The EMACS_INT cast avoids a warning. There's no problem
1521 as long as pointers have enough bits to hold small integers. */
1522 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0
PR
1523 wv->enabled = !NILP (enable);
1524 prev_wv = wv;
1525
1526 i += MENU_ITEMS_ITEM_LENGTH;
1527 }
1528 }
1529
71dca3e3
RS
1530 /* If we have just one "menu item"
1531 that was originally a button, return it by itself. */
1532 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1533 {
1534 wv = first_wv->contents;
1535 free_widget_value (first_wv);
1536 return wv;
1537 }
1538
4dedbfe0
PR
1539 return first_wv;
1540}
1541\f
cffa74ea
FP
1542extern void EmacsFrameSetCharSize ();
1543
4bcdbab1
KH
1544/* Recompute all the widgets of frame F, when the menu bar
1545 has been changed. */
4dedbfe0 1546
18686d47 1547static void
6af6cbb5 1548update_frame_menubar (f)
18686d47
RS
1549 FRAME_PTR f;
1550{
7556890b 1551 struct x_output *x = f->output_data.x;
cffa74ea 1552 int columns, rows;
18686d47
RS
1553 int menubar_changed;
1554
4dedbfe0
PR
1555 Dimension shell_height;
1556
1557 /* We assume the menubar contents has changed if the global flag is set,
1558 or if the current buffer has changed, or if the menubar has never
1559 been updated before.
1560 */
18686d47
RS
1561 menubar_changed = (x->menubar_widget
1562 && !XtIsManaged (x->menubar_widget));
1563
1564 if (! (menubar_changed))
1565 return;
1566
1567 BLOCK_INPUT;
cffa74ea
FP
1568 /* Save the size of the frame because the pane widget doesn't accept to
1569 resize itself. So force it. */
1570 columns = f->width;
1571 rows = f->height;
1572
4dedbfe0
PR
1573 /* Do the voodoo which means "I'm changing lots of things, don't try to
1574 refigure sizes until I'm done." */
1575 lw_refigure_widget (x->column_widget, False);
cffa74ea 1576
18686d47
RS
1577 /* the order in which children are managed is the top to
1578 bottom order in which they are displayed in the paned window.
1579 First, remove the text-area widget.
1580 */
1581 XtUnmanageChild (x->edit_widget);
1582
1583 /* remove the menubar that is there now, and put up the menubar that
1584 should be there.
1585 */
1586 if (menubar_changed)
1587 {
1588 XtManageChild (x->menubar_widget);
1589 XtMapWidget (x->menubar_widget);
1590 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1591 }
1592
c98fcf4b 1593 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1594 XtManageChild (x->edit_widget);
4dedbfe0 1595 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1596
1597 /* Force the pane widget to resize itself with the right values. */
1598 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1599
18686d47
RS
1600 UNBLOCK_INPUT;
1601}
1602
4bcdbab1
KH
1603/* Set the contents of the menubar widgets of frame F.
1604 The argument FIRST_TIME is currently ignored;
1605 it is set the first time this is called, from initialize_frame_menubar. */
1606
18686d47 1607void
88766961 1608set_frame_menubar (f, first_time, deep_p)
18686d47 1609 FRAME_PTR f;
706aa2f2 1610 int first_time;
88766961 1611 int deep_p;
18686d47 1612{
7556890b 1613 Widget menubar_widget = f->output_data.x->menubar_widget;
bd3a4da2 1614 Lisp_Object tail, items, frame;
4d19cb8e 1615 widget_value *wv, *first_wv, *prev_wv = 0;
5b3557df 1616 int i;
88766961 1617 LWLIB_ID id;
18686d47 1618
7556890b
RS
1619 if (f->output_data.x->id == 0)
1620 f->output_data.x->id = next_menubar_widget_id++;
1621 id = f->output_data.x->id;
37a98547 1622
88766961
RS
1623 if (! menubar_widget)
1624 deep_p = 1;
745c34fb 1625 else if (pending_menu_activation && !deep_p)
a9be6839
RS
1626 deep_p = 1;
1627 /* Make the first call for any given frame always go deep. */
1628 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
1629 {
1630 deep_p = 1;
a9be6839
RS
1631 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1632 f->output_data.x->saved_menu_event->type = 0;
745c34fb 1633 }
18686d47 1634
f7fab165 1635 wv = xmalloc_widget_value ();
18686d47
RS
1636 wv->name = "menubar";
1637 wv->value = 0;
1638 wv->enabled = 1;
4d19cb8e 1639 first_wv = wv;
8d8a3494 1640
88766961 1641 if (deep_p)
18686d47 1642 {
88766961
RS
1643 /* Make a widget-value tree representing the entire menu trees. */
1644
1645 struct buffer *prev = current_buffer;
1646 Lisp_Object buffer;
1647 int specpdl_count = specpdl_ptr - specpdl;
1648 int previous_menu_items_used = f->menu_bar_items_used;
1649 Lisp_Object *previous_items
1650 = (Lisp_Object *) alloca (previous_menu_items_used
1651 * sizeof (Lisp_Object));
1652
1653 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1654 specbind (Qinhibit_quit, Qt);
1655 /* Don't let the debugger step into this code
1656 because it is not reentrant. */
1657 specbind (Qdebug_on_next_call, Qnil);
1658
1659 record_unwind_protect (Fstore_match_data, Fmatch_data ());
1660 if (NILP (Voverriding_local_map_menu_flag))
1661 {
1662 specbind (Qoverriding_terminal_local_map, Qnil);
1663 specbind (Qoverriding_local_map, Qnil);
1664 }
18686d47 1665
88766961 1666 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 1667
88766961
RS
1668 /* Run the Lucid hook. */
1669 call1 (Vrun_hooks, Qactivate_menubar_hook);
1670 /* If it has changed current-menubar from previous value,
1671 really recompute the menubar from the value. */
1672 if (! NILP (Vlucid_menu_bar_dirty_flag))
1673 call0 (Qrecompute_lucid_menubar);
a57634d4 1674 safe_run_hooks (Qmenu_bar_update_hook);
88766961 1675 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 1676
88766961 1677 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 1678
88766961 1679 inhibit_garbage_collection ();
8d8a3494 1680
88766961
RS
1681 /* Save the frame's previous menu bar contents data. */
1682 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1683 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 1684
88766961
RS
1685 /* Fill in the current menu bar contents. */
1686 menu_items = f->menu_bar_vector;
1687 menu_items_allocated = XVECTOR (menu_items)->size;
1688 init_menu_items ();
f04366cb 1689 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1690 {
1691 Lisp_Object key, string, maps;
1692
1693 key = XVECTOR (items)->contents[i];
1694 string = XVECTOR (items)->contents[i + 1];
1695 maps = XVECTOR (items)->contents[i + 2];
1696 if (NILP (string))
1697 break;
1698
1699 wv = single_submenu (key, string, maps);
1700 if (prev_wv)
1701 prev_wv->next = wv;
1702 else
1703 first_wv->contents = wv;
1704 /* Don't set wv->name here; GC during the loop might relocate it. */
1705 wv->enabled = 1;
1706 prev_wv = wv;
1707 }
1708
1709 finish_menu_items ();
1710
1711 set_buffer_internal_1 (prev);
8d8a3494 1712 unbind_to (specpdl_count, Qnil);
8d8a3494 1713
88766961
RS
1714 /* If there has been no change in the Lisp-level contents
1715 of the menu bar, skip redisplaying it. Just exit. */
1716
1717 for (i = 0; i < previous_menu_items_used; i++)
1718 if (menu_items_used == i
1719 || (previous_items[i] != XVECTOR (menu_items)->contents[i]))
1720 break;
62555c22 1721 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
1722 {
1723 free_menubar_widget_value_tree (first_wv);
1724 menu_items = Qnil;
1725
1726 return;
1727 }
1728
1729 /* Now GC cannot happen during the lifetime of the widget_value,
1730 so it's safe to store data from a Lisp_String. */
1731 wv = first_wv->contents;
f04366cb 1732 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1733 {
1734 Lisp_Object string;
1735 string = XVECTOR (items)->contents[i + 1];
1736 if (NILP (string))
1737 break;
1738 wv->name = (char *) XSTRING (string)->data;
1739 wv = wv->next;
1740 }
1741
1742 f->menu_bar_vector = menu_items;
1743 f->menu_bar_items_used = menu_items_used;
1744 menu_items = Qnil;
4d19cb8e 1745 }
88766961
RS
1746 else
1747 {
1748 /* Make a widget-value tree containing
1749 just the top level menu bar strings. */
4d19cb8e 1750
88766961 1751 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 1752 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1753 {
1754 Lisp_Object string;
1755
1756 string = XVECTOR (items)->contents[i + 1];
1757 if (NILP (string))
1758 break;
1759
f7fab165 1760 wv = xmalloc_widget_value ();
88766961
RS
1761 wv->name = (char *) XSTRING (string)->data;
1762 wv->value = 0;
1763 wv->enabled = 1;
fe8fa62f
RS
1764 /* This prevents lwlib from assuming this
1765 menu item is really supposed to be empty. */
1766 /* The EMACS_INT cast avoids a warning.
1767 This value just has to be different from small integers. */
1768 wv->call_data = (void *) (EMACS_INT) (-1);
88766961
RS
1769
1770 if (prev_wv)
1771 prev_wv->next = wv;
1772 else
1773 first_wv->contents = wv;
1774 prev_wv = wv;
1775 }
62555c22
RS
1776
1777 /* Forget what we thought we knew about what is in the
1778 detailed contents of the menu bar menus.
1779 Changing the top level always destroys the contents. */
1780 f->menu_bar_items_used = 0;
88766961 1781 }
4dedbfe0 1782
88766961 1783 /* Create or update the menu bar widget. */
aa669def
RS
1784
1785 BLOCK_INPUT;
1786
18686d47 1787 if (menubar_widget)
4dedbfe0
PR
1788 {
1789 /* Disable resizing (done for Motif!) */
7556890b 1790 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
1791
1792 /* The third arg is DEEP_P, which says to consider the entire
1793 menu trees we supply, rather than just the menu bar item names. */
88766961 1794 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 1795
c98fcf4b 1796 /* Re-enable the edit widget to resize. */
7556890b 1797 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 1798 }
18686d47
RS
1799 else
1800 {
88766961 1801 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 1802 f->output_data.x->column_widget,
4dedbfe0
PR
1803 0,
1804 popup_activate_callback,
1805 menubar_selection_callback,
1806 popup_deactivate_callback);
7556890b 1807 f->output_data.x->menubar_widget = menubar_widget;
18686d47 1808 }
1d1c1567
KH
1809
1810 {
1811 int menubar_size
7556890b
RS
1812 = (f->output_data.x->menubar_widget
1813 ? (f->output_data.x->menubar_widget->core.height
1814 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
1815 : 0);
1816
5ee80bd3 1817#ifdef USE_LUCID
1d1c1567
KH
1818 if (FRAME_EXTERNAL_MENU_BAR (f))
1819 {
1820 Dimension ibw = 0;
7556890b 1821 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
1822 XtNinternalBorderWidth, &ibw, NULL);
1823 menubar_size += ibw;
1824 }
5ee80bd3 1825#endif /* USE_LUCID */
1d1c1567 1826
7556890b 1827 f->output_data.x->menubar_height = menubar_size;
1d1c1567 1828 }
18686d47
RS
1829
1830 free_menubar_widget_value_tree (first_wv);
1831
4bcdbab1 1832 update_frame_menubar (f);
18686d47
RS
1833
1834 UNBLOCK_INPUT;
1835}
85f487d1 1836
8e6208c5 1837/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
1838 before it is mapped, so that the window is mapped with the menubar already
1839 there instead of us tacking it on later and thrashing the window after it
1840 is visible. */
1841
1842void
1843initialize_frame_menubar (f)
1844 FRAME_PTR f;
1845{
1846 /* This function is called before the first chance to redisplay
1847 the frame. It has to be, so the frame will have the right size. */
1848 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 1849 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
1850}
1851
1852/* Get rid of the menu bar of frame F, and free its storage.
1853 This is used when deleting a frame, and when turning off the menu bar. */
1854
85f487d1
FP
1855void
1856free_frame_menubar (f)
1857 FRAME_PTR f;
1858{
1859 Widget menubar_widget;
1860 int id;
1861
7556890b 1862 menubar_widget = f->output_data.x->menubar_widget;
85f487d1
FP
1863
1864 if (menubar_widget)
1865 {
1866 BLOCK_INPUT;
7556890b 1867 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
85f487d1
FP
1868 UNBLOCK_INPUT;
1869 }
1870}
78589e07 1871
78589e07
RS
1872#endif /* USE_X_TOOLKIT */
1873\f
1874/* xmenu_show actually displays a menu using the panes and items in menu_items
1875 and returns the value selected from it.
1876 There are two versions of xmenu_show, one for Xt and one for Xlib.
1877 Both assume input is blocked by the caller. */
1878
1879/* F is the frame the menu is for.
1880 X and Y are the frame-relative specified position,
1881 relative to the inside upper left corner of the frame F.
c8b5aa3d 1882 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
1883 KEYMAPS is 1 if this menu was specified with keymaps;
1884 in that case, we return a list containing the chosen item's value
1885 and perhaps also the pane's prefix.
1886 TITLE is the specified menu title.
1887 ERROR is a place to store an error message string in case of failure.
1888 (We return nil on failure, but the value doesn't actually matter.) */
18686d47
RS
1889
1890#ifdef USE_X_TOOLKIT
18686d47 1891
8ed87156 1892/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1893 library.
1894
1895 For the main windows, and popup menus, we use this counter,
88766961 1896 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1897
88766961
RS
1898 For menu bars, we use numbers starting at 0, counted in
1899 next_menubar_widget_id. */
8ed87156 1900LWLIB_ID widget_id_tick;
165e1749 1901
4dedbfe0
PR
1902#ifdef __STDC__
1903static Lisp_Object *volatile menu_item_selection;
1904#else
1905static Lisp_Object *menu_item_selection;
1906#endif
1907
1908static void
1909popup_selection_callback (widget, id, client_data)
1910 Widget widget;
1911 LWLIB_ID id;
1912 XtPointer client_data;
1913{
1914 menu_item_selection = (Lisp_Object *) client_data;
1915}
1916
78589e07 1917static Lisp_Object
673a6211 1918xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 1919 FRAME_PTR f;
18686d47
RS
1920 int x;
1921 int y;
9685a93f 1922 int for_click;
78589e07
RS
1923 int keymaps;
1924 Lisp_Object title;
1925 char **error;
18686d47 1926{
78589e07 1927 int i;
cc17e9bf 1928 LWLIB_ID menu_id;
18686d47 1929 Widget menu;
ffcb5a51 1930 Arg av[2];
60f312e2 1931 int ac = 0;
78589e07 1932 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
1933 widget_value **submenu_stack
1934 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1935 Lisp_Object *subprefix_stack
1936 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1937 int submenu_depth = 0;
ffcb5a51 1938 XButtonPressedEvent dummy;
4e8d3549 1939
78c8278d 1940 int first_pane;
a51b963c 1941 int next_release_must_exit = 0;
78c8278d 1942
78589e07
RS
1943 *error = NULL;
1944
742f715d
KH
1945 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1946 {
1947 *error = "Empty menu";
1948 return Qnil;
1949 }
63c414df 1950
78589e07
RS
1951 /* Create a tree of widget_value objects
1952 representing the panes and their items. */
f7fab165 1953 wv = xmalloc_widget_value ();
78589e07
RS
1954 wv->name = "menu";
1955 wv->value = 0;
1956 wv->enabled = 1;
1957 first_wv = wv;
78c8278d 1958 first_pane = 1;
78589e07
RS
1959
1960 /* Loop over all panes and items, filling in the tree. */
1961 i = 0;
1962 while (i < menu_items_used)
1963 {
101bb4a5
RS
1964 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1965 {
1966 submenu_stack[submenu_depth++] = save_wv;
1967 save_wv = prev_wv;
1968 prev_wv = 0;
78c8278d 1969 first_pane = 1;
101bb4a5
RS
1970 i++;
1971 }
1972 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1973 {
1974 prev_wv = save_wv;
1975 save_wv = submenu_stack[--submenu_depth];
78c8278d 1976 first_pane = 0;
101bb4a5
RS
1977 i++;
1978 }
1979 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1980 && submenu_depth != 0)
1981 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
1982 /* Ignore a nil in the item list.
1983 It's meaningful only for dialog boxes. */
1984 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1985 i += 1;
101bb4a5 1986 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
1987 {
1988 /* Create a new pane. */
1989 Lisp_Object pane_name, prefix;
1990 char *pane_string;
1991 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1992 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1993 pane_string = (NILP (pane_name)
1994 ? "" : (char *) XSTRING (pane_name)->data);
101bb4a5 1995 /* If there is just one top-level pane, put all its items directly
78589e07
RS
1996 under the top-level menu. */
1997 if (menu_items_n_panes == 1)
1998 pane_string = "";
1999
2000 /* If the pane has a meaningful name,
2001 make the pane a top-level menu item
2002 with its items as a submenu beneath it. */
78c8278d 2003 if (!keymaps && strcmp (pane_string, ""))
78589e07 2004 {
f7fab165 2005 wv = xmalloc_widget_value ();
78589e07
RS
2006 if (save_wv)
2007 save_wv->next = wv;
2008 else
2009 first_wv->contents = wv;
2010 wv->name = pane_string;
2011 if (keymaps && !NILP (prefix))
2012 wv->name++;
2013 wv->value = 0;
2014 wv->enabled = 1;
78c8278d
RS
2015 save_wv = wv;
2016 prev_wv = 0;
78589e07 2017 }
78c8278d
RS
2018 else if (first_pane)
2019 {
2020 save_wv = wv;
2021 prev_wv = 0;
2022 }
2023 first_pane = 0;
78589e07
RS
2024 i += MENU_ITEMS_PANE_LENGTH;
2025 }
2026 else
2027 {
2028 /* Create a new item within current pane. */
a352a815 2029 Lisp_Object item_name, enable, descrip, def;
78589e07
RS
2030 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2031 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2032 descrip
2033 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 2034 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
78589e07 2035
f7fab165 2036 wv = xmalloc_widget_value ();
78589e07
RS
2037 if (prev_wv)
2038 prev_wv->next = wv;
2039 else
2040 save_wv->contents = wv;
4e8d3549 2041 wv->name = (char *) XSTRING (item_name)->data;
78589e07 2042 if (!NILP (descrip))
4e8d3549 2043 wv->key = (char *) XSTRING (descrip)->data;
78589e07 2044 wv->value = 0;
a352a815
RS
2045 /* If this item has a null value,
2046 make the call_data null so that it won't display a box
2047 when the mouse is on it. */
2048 wv->call_data
2049 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07
RS
2050 wv->enabled = !NILP (enable);
2051 prev_wv = wv;
2052
2053 i += MENU_ITEMS_ITEM_LENGTH;
2054 }
2055 }
2056
c98fcf4b 2057 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2058 if (!NILP (title))
2059 {
f7fab165
RS
2060 widget_value *wv_title = xmalloc_widget_value ();
2061 widget_value *wv_sep1 = xmalloc_widget_value ();
2062 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2063
2064 wv_sep2->name = "--";
2065 wv_sep2->next = first_wv->contents;
2066
2067 wv_sep1->name = "--";
2068 wv_sep1->next = wv_sep2;
2069
2070 wv_title->name = (char *) XSTRING (title)->data;
2071 wv_title->enabled = True;
2072 wv_title->next = wv_sep1;
2073 first_wv->contents = wv_title;
2074 }
2075
78589e07 2076 /* Actually create the menu. */
cc17e9bf 2077 menu_id = widget_id_tick++;
78589e07 2078 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
7556890b 2079 f->output_data.x->widget, 1, 0,
4dedbfe0
PR
2080 popup_selection_callback,
2081 popup_deactivate_callback);
60f312e2 2082
ffcb5a51
RS
2083 /* Adjust coordinates to relative to the outer (window manager) window. */
2084 {
2085 Window child;
2086 int win_x = 0, win_y = 0;
2087
2088 /* Find the position of the outside upper-left corner of
2089 the inner window, with respect to the outer window. */
2090 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2091 {
2092 BLOCK_INPUT;
2093 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2094
2095 /* From-window, to-window. */
2096 f->output_data.x->window_desc,
2097 f->output_data.x->parent_desc,
2098
2099 /* From-position, to-position. */
2100 0, 0, &win_x, &win_y,
2101
2102 /* Child of window. */
2103 &child);
2104 UNBLOCK_INPUT;
2105 x += win_x;
2106 y += win_y;
2107 }
2108 }
2109
2110 /* Adjust coordinates to be root-window-relative. */
2111 x += f->output_data.x->left_pos;
2112 y += f->output_data.x->top_pos;
2113
2114 dummy.type = ButtonPress;
2115 dummy.serial = 0;
2116 dummy.send_event = 0;
2117 dummy.display = FRAME_X_DISPLAY (f);
2118 dummy.time = CurrentTime;
ffcb5a51
RS
2119 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2120 dummy.window = dummy.root;
2121 dummy.subwindow = dummy.root;
2122 dummy.x_root = x;
2123 dummy.y_root = y;
2124 dummy.x = x;
2125 dummy.y = y;
c8b5aa3d
RS
2126 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2127 dummy.button = 0;
2128 for (i = 0; i < 5; i++)
2129 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2130 dummy.button = i;
ffcb5a51 2131
60f312e2
RS
2132 /* Don't allow any geometry request from the user. */
2133 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2134 XtSetValues (menu, av, ac);
2135
78589e07
RS
2136 /* Free the widget_value objects we used to specify the contents. */
2137 free_menubar_widget_value_tree (first_wv);
2138
2139 /* No selection has been chosen yet. */
2140 menu_item_selection = 0;
2141
78589e07 2142 /* Display the menu. */
ffcb5a51 2143 lw_popup_menu (menu, &dummy);
4dedbfe0 2144 popup_activated_flag = 1;
18686d47 2145
78589e07 2146 /* Process events that apply to the menu. */
2e2b8e22 2147 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
78589e07 2148
a9c90b7c
RS
2149 /* fp turned off the following statement and wrote a comment
2150 that it is unnecessary--that the menu has already disappeared.
21af8a68
KH
2151 Nowadays the menu disappears ok, all right, but
2152 we need to delete the widgets or multiple ones will pile up. */
78589e07 2153 lw_destroy_all_widgets (menu_id);
18686d47 2154
78589e07
RS
2155 /* Find the selected item, and its pane, to return
2156 the proper value. */
2157 if (menu_item_selection != 0)
2158 {
c63f6952 2159 Lisp_Object prefix, entry;
78589e07
RS
2160
2161 prefix = Qnil;
2162 i = 0;
2163 while (i < menu_items_used)
2164 {
101bb4a5
RS
2165 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2166 {
2167 subprefix_stack[submenu_depth++] = prefix;
2168 prefix = entry;
2169 i++;
2170 }
2171 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2172 {
2173 prefix = subprefix_stack[--submenu_depth];
2174 i++;
2175 }
2176 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2177 {
2178 prefix
2179 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2180 i += MENU_ITEMS_PANE_LENGTH;
2181 }
d31d42cc
RS
2182 /* Ignore a nil in the item list.
2183 It's meaningful only for dialog boxes. */
2184 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2185 i += 1;
78589e07
RS
2186 else
2187 {
2188 entry
2189 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2190 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2191 {
2192 if (keymaps != 0)
2193 {
101bb4a5
RS
2194 int j;
2195
78589e07
RS
2196 entry = Fcons (entry, Qnil);
2197 if (!NILP (prefix))
2198 entry = Fcons (prefix, entry);
101bb4a5 2199 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2200 if (!NILP (subprefix_stack[j]))
5964e450 2201 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2202 }
2203 return entry;
2204 }
2205 i += MENU_ITEMS_ITEM_LENGTH;
2206 }
2207 }
2208 }
2209
2210 return Qnil;
18686d47 2211}
4dedbfe0
PR
2212\f
2213static void
2214dialog_selection_callback (widget, id, client_data)
2215 Widget widget;
2216 LWLIB_ID id;
2217 XtPointer client_data;
2218{
01d5e892
RS
2219 /* The EMACS_INT cast avoids a warning. There's no problem
2220 as long as pointers have enough bits to hold small integers. */
2221 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0
PR
2222 menu_item_selection = (Lisp_Object *) client_data;
2223 BLOCK_INPUT;
2224 lw_destroy_all_widgets (id);
2225 UNBLOCK_INPUT;
9572375b 2226 popup_activated_flag = 0;
4dedbfe0 2227}
18686d47 2228
165e1749
FP
2229static char * button_names [] = {
2230 "button1", "button2", "button3", "button4", "button5",
2231 "button6", "button7", "button8", "button9", "button10" };
2232
2233static Lisp_Object
673a6211 2234xdialog_show (f, keymaps, title, error)
165e1749 2235 FRAME_PTR f;
165e1749
FP
2236 int keymaps;
2237 Lisp_Object title;
2238 char **error;
2239{
2240 int i, nb_buttons=0;
cc17e9bf 2241 LWLIB_ID dialog_id;
165e1749 2242 Widget menu;
80670155 2243 char dialog_name[6];
165e1749 2244
165e1749
FP
2245 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2246
fcaa7665
RS
2247 /* Number of elements seen so far, before boundary. */
2248 int left_count = 0;
2249 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2250 int boundary_seen = 0;
2251
165e1749
FP
2252 *error = NULL;
2253
80670155
RS
2254 if (menu_items_n_panes > 1)
2255 {
2256 *error = "Multiple panes in dialog box";
2257 return Qnil;
2258 }
2259
165e1749
FP
2260 /* Create a tree of widget_value objects
2261 representing the text label and buttons. */
2262 {
2263 Lisp_Object pane_name, prefix;
2264 char *pane_string;
2265 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2266 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2267 pane_string = (NILP (pane_name)
2268 ? "" : (char *) XSTRING (pane_name)->data);
f7fab165 2269 prev_wv = xmalloc_widget_value ();
165e1749
FP
2270 prev_wv->value = pane_string;
2271 if (keymaps && !NILP (prefix))
2272 prev_wv->name++;
2273 prev_wv->enabled = 1;
2274 prev_wv->name = "message";
2275 first_wv = prev_wv;
2276
2277 /* Loop over all panes and items, filling in the tree. */
2278 i = MENU_ITEMS_PANE_LENGTH;
2279 while (i < menu_items_used)
2280 {
2281
2282 /* Create a new item within current pane. */
2283 Lisp_Object item_name, enable, descrip;
2284 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2285 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2286 descrip
2287 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2288
80670155
RS
2289 if (NILP (item_name))
2290 {
2291 free_menubar_widget_value_tree (first_wv);
2292 *error = "Submenu in dialog items";
2293 return Qnil;
2294 }
fcaa7665
RS
2295 if (EQ (item_name, Qquote))
2296 {
2297 /* This is the boundary between left-side elts
2298 and right-side elts. Stop incrementing right_count. */
2299 boundary_seen = 1;
2300 i++;
2301 continue;
2302 }
86e71abf 2303 if (nb_buttons >= 9)
80670155
RS
2304 {
2305 free_menubar_widget_value_tree (first_wv);
2306 *error = "Too many dialog items";
2307 return Qnil;
2308 }
2309
f7fab165 2310 wv = xmalloc_widget_value ();
165e1749 2311 prev_wv->next = wv;
80670155 2312 wv->name = (char *) button_names[nb_buttons];
165e1749 2313 if (!NILP (descrip))
4e8d3549
RS
2314 wv->key = (char *) XSTRING (descrip)->data;
2315 wv->value = (char *) XSTRING (item_name)->data;
165e1749
FP
2316 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2317 wv->enabled = !NILP (enable);
2318 prev_wv = wv;
2319
fcaa7665
RS
2320 if (! boundary_seen)
2321 left_count++;
2322
165e1749
FP
2323 nb_buttons++;
2324 i += MENU_ITEMS_ITEM_LENGTH;
2325 }
2326
fcaa7665
RS
2327 /* If the boundary was not specified,
2328 by default put half on the left and half on the right. */
2329 if (! boundary_seen)
2330 left_count = nb_buttons - nb_buttons / 2;
2331
f7fab165 2332 wv = xmalloc_widget_value ();
80670155
RS
2333 wv->name = dialog_name;
2334
2335 /* Dialog boxes use a really stupid name encoding
2336 which specifies how many buttons to use
2337 and how many buttons are on the right.
2338 The Q means something also. */
2339 dialog_name[0] = 'Q';
2340 dialog_name[1] = '0' + nb_buttons;
2341 dialog_name[2] = 'B';
2342 dialog_name[3] = 'R';
fcaa7665
RS
2343 /* Number of buttons to put on the right. */
2344 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 2345 dialog_name[5] = 0;
165e1749
FP
2346 wv->contents = first_wv;
2347 first_wv = wv;
165e1749
FP
2348 }
2349
2350 /* Actually create the dialog. */
cc17e9bf 2351 dialog_id = widget_id_tick++;
165e1749 2352 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
7556890b 2353 f->output_data.x->widget, 1, 0,
165e1749 2354 dialog_selection_callback, 0);
b5587215 2355 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
165e1749
FP
2356 /* Free the widget_value objects we used to specify the contents. */
2357 free_menubar_widget_value_tree (first_wv);
2358
2359 /* No selection has been chosen yet. */
2360 menu_item_selection = 0;
2361
165e1749
FP
2362 /* Display the menu. */
2363 lw_pop_up_all_widgets (dialog_id);
aa669def 2364 popup_activated_flag = 1;
165e1749
FP
2365
2366 /* Process events that apply to the menu. */
2e2b8e22 2367 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
165e1749 2368
21af8a68
KH
2369 lw_destroy_all_widgets (dialog_id);
2370
165e1749
FP
2371 /* Find the selected item, and its pane, to return
2372 the proper value. */
2373 if (menu_item_selection != 0)
2374 {
2375 Lisp_Object prefix;
2376
2377 prefix = Qnil;
2378 i = 0;
2379 while (i < menu_items_used)
2380 {
2381 Lisp_Object entry;
2382
2383 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2384 {
2385 prefix
2386 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2387 i += MENU_ITEMS_PANE_LENGTH;
2388 }
2389 else
2390 {
2391 entry
2392 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2393 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2394 {
2395 if (keymaps != 0)
2396 {
2397 entry = Fcons (entry, Qnil);
2398 if (!NILP (prefix))
2399 entry = Fcons (prefix, entry);
2400 }
2401 return entry;
2402 }
2403 i += MENU_ITEMS_ITEM_LENGTH;
2404 }
2405 }
2406 }
2407
2408 return Qnil;
2409}
18686d47 2410#else /* not USE_X_TOOLKIT */
78589e07
RS
2411
2412static Lisp_Object
673a6211 2413xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
2414 FRAME_PTR f;
2415 int x, y;
9685a93f
RS
2416 int for_click;
2417 int keymaps;
78589e07
RS
2418 Lisp_Object title;
2419 char **error;
dcfdbac7 2420{
78589e07
RS
2421 Window root;
2422 XMenu *menu;
2423 int pane, selidx, lpane, status;
2424 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2425 char *datap;
2426 int ulx, uly, width, height;
2427 int dispwidth, dispheight;
4e8d3549
RS
2428 int i, j;
2429 int maxwidth;
78589e07
RS
2430 int dummy_int;
2431 unsigned int dummy_uint;
088831f6 2432
07a675b7 2433 *error = 0;
78589e07
RS
2434 if (menu_items_n_panes == 0)
2435 return Qnil;
088831f6 2436
742f715d
KH
2437 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2438 {
2439 *error = "Empty menu";
2440 return Qnil;
2441 }
2442
78589e07 2443 /* Figure out which root window F is on. */
92280f67 2444 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2445 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2446 &dummy_uint, &dummy_uint);
18686d47 2447
78589e07 2448 /* Make the menu on that window. */
92280f67 2449 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2450 if (menu == NULL)
dcfdbac7
JB
2451 {
2452 *error = "Can't create menu";
78589e07 2453 return Qnil;
dcfdbac7 2454 }
78589e07 2455
87485d6f 2456#ifdef HAVE_X_WINDOWS
78589e07 2457 /* Adjust coordinates to relative to the outer (window manager) window. */
78589e07
RS
2458 {
2459 Window child;
2460 int win_x = 0, win_y = 0;
2461
2462 /* Find the position of the outside upper-left corner of
2463 the inner window, with respect to the outer window. */
7556890b 2464 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
78589e07
RS
2465 {
2466 BLOCK_INPUT;
92280f67 2467 XTranslateCoordinates (FRAME_X_DISPLAY (f),
78589e07
RS
2468
2469 /* From-window, to-window. */
7556890b
RS
2470 f->output_data.x->window_desc,
2471 f->output_data.x->parent_desc,
78589e07
RS
2472
2473 /* From-position, to-position. */
2474 0, 0, &win_x, &win_y,
2475
2476 /* Child of window. */
2477 &child);
2478 UNBLOCK_INPUT;
2479 x += win_x;
2480 y += win_y;
2481 }
2482 }
87485d6f 2483#endif /* HAVE_X_WINDOWS */
78589e07
RS
2484
2485 /* Adjust coordinates to be root-window-relative. */
7556890b
RS
2486 x += f->output_data.x->left_pos;
2487 y += f->output_data.x->top_pos;
18686d47 2488
78589e07
RS
2489 /* Create all the necessary panes and their items. */
2490 i = 0;
2491 while (i < menu_items_used)
dcfdbac7 2492 {
78589e07 2493 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 2494 {
78589e07
RS
2495 /* Create a new pane. */
2496 Lisp_Object pane_name, prefix;
2497 char *pane_string;
2498
2499 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2500 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2501 pane_string = (NILP (pane_name)
2502 ? "" : (char *) XSTRING (pane_name)->data);
2503 if (keymaps && !NILP (prefix))
2504 pane_string++;
2505
92280f67 2506 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2507 if (lpane == XM_FAILURE)
2508 {
92280f67 2509 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
2510 *error = "Can't create pane";
2511 return Qnil;
2512 }
2513 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2514
2515 /* Find the width of the widest item in this pane. */
2516 maxwidth = 0;
2517 j = i;
2518 while (j < menu_items_used)
2519 {
2520 Lisp_Object item;
2521 item = XVECTOR (menu_items)->contents[j];
2522 if (EQ (item, Qt))
2523 break;
2524 if (NILP (item))
2525 {
2526 j++;
2527 continue;
2528 }
2529 width = XSTRING (item)->size;
2530 if (width > maxwidth)
2531 maxwidth = width;
2532
2533 j += MENU_ITEMS_ITEM_LENGTH;
2534 }
dcfdbac7 2535 }
fcaa7665
RS
2536 /* Ignore a nil in the item list.
2537 It's meaningful only for dialog boxes. */
2538 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2539 i += 1;
78589e07 2540 else
dcfdbac7 2541 {
78589e07
RS
2542 /* Create a new item within current pane. */
2543 Lisp_Object item_name, enable, descrip;
4e8d3549 2544 unsigned char *item_data;
78589e07
RS
2545
2546 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2547 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2548 descrip
2549 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2550 if (!NILP (descrip))
4e8d3549
RS
2551 {
2552 int gap = maxwidth - XSTRING (item_name)->size;
2553#ifdef C_ALLOCA
2554 Lisp_Object spacer;
2555 spacer = Fmake_string (make_number (gap), make_number (' '));
2556 item_name = concat2 (item_name, spacer);
2557 item_name = concat2 (item_name, descrip);
2558 item_data = XSTRING (item_name)->data;
2559#else
2560 /* if alloca is fast, use that to make the space,
2561 to reduce gc needs. */
2562 item_data
2563 = (unsigned char *) alloca (maxwidth
2564 + XSTRING (descrip)->size + 1);
2565 bcopy (XSTRING (item_name)->data, item_data,
2566 XSTRING (item_name)->size);
2567 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2568 item_data[j] = ' ';
2569 bcopy (XSTRING (descrip)->data, item_data + j,
2570 XSTRING (descrip)->size);
2571 item_data[j + XSTRING (descrip)->size] = 0;
2572#endif
2573 }
2574 else
2575 item_data = XSTRING (item_name)->data;
78589e07 2576
92280f67
RS
2577 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2578 menu, lpane, 0, item_data,
78589e07 2579 !NILP (enable))
dcfdbac7
JB
2580 == XM_FAILURE)
2581 {
92280f67 2582 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 2583 *error = "Can't add selection to menu";
78589e07 2584 return Qnil;
dcfdbac7 2585 }
78589e07 2586 i += MENU_ITEMS_ITEM_LENGTH;
dcfdbac7
JB
2587 }
2588 }
4e8d3549 2589
78589e07 2590 /* All set and ready to fly. */
92280f67
RS
2591 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2592 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
f1847de3 2593 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
92280f67 2594 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
f1847de3 2595 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
78589e07
RS
2596 x = min (x, dispwidth);
2597 y = min (y, dispheight);
2598 x = max (x, 1);
2599 y = max (y, 1);
92280f67 2600 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2601 &ulx, &uly, &width, &height);
2602 if (ulx+width > dispwidth)
2603 {
78589e07 2604 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2605 ulx = dispwidth - width;
2606 }
2607 if (uly+height > dispheight)
2608 {
78589e07 2609 y -= (uly + height) - dispheight;
dcfdbac7
JB
2610 uly = dispheight - height;
2611 }
78589e07
RS
2612 if (ulx < 0) x -= ulx;
2613 if (uly < 0) y -= uly;
121e4555
KH
2614
2615 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2616 XMenuSetFreeze (menu, TRUE);
2617 pane = selidx = 0;
dcfdbac7 2618
92280f67 2619 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
78589e07 2620 x, y, ButtonReleaseMask, &datap);
a352a815
RS
2621
2622
f1df80a8 2623#ifdef HAVE_X_WINDOWS
a352a815
RS
2624 /* Assume the mouse has moved out of the X window.
2625 If it has actually moved in, we will get an EnterNotify. */
29e460bd 2626 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
f1df80a8 2627#endif
a352a815 2628
dcfdbac7
JB
2629 switch (status)
2630 {
2631 case XM_SUCCESS:
2632#ifdef XDEBUG
2633 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2634#endif
fa6d54d9 2635
78589e07
RS
2636 /* Find the item number SELIDX in pane number PANE. */
2637 i = 0;
2638 while (i < menu_items_used)
fa6d54d9 2639 {
78589e07 2640 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 2641 {
78589e07
RS
2642 if (pane == 0)
2643 pane_prefix
2644 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2645 pane--;
2646 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2647 }
78589e07 2648 else
ab6ee1a0 2649 {
78589e07 2650 if (pane == -1)
ab6ee1a0 2651 {
78589e07 2652 if (selidx == 0)
ab6ee1a0 2653 {
78589e07
RS
2654 entry
2655 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2656 if (keymaps != 0)
ab6ee1a0 2657 {
78589e07
RS
2658 entry = Fcons (entry, Qnil);
2659 if (!NILP (pane_prefix))
2660 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2661 }
78589e07 2662 break;
ab6ee1a0 2663 }
78589e07 2664 selidx--;
ab6ee1a0 2665 }
78589e07 2666 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2667 }
2668 }
78589e07 2669 break;
dcfdbac7 2670
78589e07 2671 case XM_FAILURE:
78589e07
RS
2672 *error = "Can't activate menu";
2673 case XM_IA_SELECT:
2674 case XM_NO_SELECT:
2675 entry = Qnil;
2676 break;
dcfdbac7 2677 }
92280f67 2678 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
a5285df3 2679
87485d6f 2680#ifdef HAVE_X_WINDOWS
a5285df3
RS
2681 /* State that no mouse buttons are now held.
2682 (The oldXMenu code doesn't track this info for us.)
2683 That is not necessarily true, but the fiction leads to reasonable
2684 results, and it is a pain to ask which are actually held now. */
e9a79fb2 2685 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
87485d6f 2686#endif
a5285df3 2687
78589e07 2688 return entry;
dcfdbac7 2689}
4dedbfe0 2690
78589e07 2691#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
2692
2693#endif /* HAVE_MENUS */
088831f6 2694\f
78589e07 2695syms_of_xmenu ()
dcfdbac7 2696{
78589e07
RS
2697 staticpro (&menu_items);
2698 menu_items = Qnil;
dcfdbac7 2699
67385caa
RS
2700 Qmenu_alias = intern ("menu-alias");
2701 staticpro (&Qmenu_alias);
2702
0314aacb
RS
2703 Qdebug_on_next_call = intern ("debug-on-next-call");
2704 staticpro (&Qdebug_on_next_call);
2705
8ed87156 2706#ifdef USE_X_TOOLKIT
4dedbfe0 2707 widget_id_tick = (1<<16);
88766961 2708 next_menubar_widget_id = 1;
8ed87156
RS
2709#endif
2710
78589e07 2711 defsubr (&Sx_popup_menu);
1e659e4c 2712#ifdef HAVE_MENUS
165e1749 2713 defsubr (&Sx_popup_dialog);
1e659e4c 2714#endif
dcfdbac7 2715}