xmenu.c 71.7 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* X Communication module for terminals which understand the X protocol.
Richard M. Stallman's avatar
Richard M. Stallman committed
2
   Copyright (C) 1986, 1988, 1993, 1994, 1996 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
9 10 11 12 13 14 15 16 17
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
20 21 22 23 24 25 26 27

/* X pop-up deck-of-cards menu facility for gnuemacs.
 *
 * Written by Jon Arnold and Roman Budzianowski
 * Mods and rewrite by Robert Krawitz
 *
 */

28 29 30
/* Modified by Fred Pierresteguy on December 93
   to make the popup menus and menubar use the Xt.  */

31 32
/* Rewritten for clarity and GC protection by rms in Feb 94.  */

Jim Blandy's avatar
Jim Blandy committed
33 34
/* On 4.3 this loses if it comes after xterm.h.  */
#include <signal.h>
35
#include <config.h>
36 37

#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
38
#include "lisp.h"
39
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
40
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
41
#include "window.h"
Jim Blandy's avatar
Jim Blandy committed
42
#include "keyboard.h"
43
#include "blockinput.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
44
#include "buffer.h"
Jim Blandy's avatar
Jim Blandy committed
45

46 47 48 49
#ifdef MSDOS
#include "msdos.h"
#endif

Morten Welinder's avatar
Morten Welinder committed
50
#ifdef HAVE_X_WINDOWS
Jim Blandy's avatar
Jim Blandy committed
51 52 53
/* This may include sys/types.h, and that somehow loses
   if this is not done before the other system files.  */
#include "xterm.h"
Morten Welinder's avatar
Morten Welinder committed
54
#endif
Jim Blandy's avatar
Jim Blandy committed
55 56 57 58 59 60 61 62 63

/* Load sys/types.h if not already loaded.
   In some systems loading it twice is suicidal.  */
#ifndef makedev
#include <sys/types.h>
#endif

#include "dispextern.h"

Morten Welinder's avatar
Morten Welinder committed
64
#ifdef HAVE_X_WINDOWS
65 66 67 68 69
#ifdef USE_X_TOOLKIT
#include <X11/Xlib.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <X11/StringDefs.h>
70
#include <X11/Shell.h>
71
#ifdef USE_LUCID
72
#include <X11/Xaw/Paned.h>
73
#endif /* USE_LUCID */
74
#include "../lwlib/lwlib.h"
75 76 77 78
#else /* not USE_X_TOOLKIT */
#include "../oldXMenu/XMenu.h"
#endif /* not USE_X_TOOLKIT */
#endif /* HAVE_X_WINDOWS */
79

Jim Blandy's avatar
Jim Blandy committed
80 81 82 83 84 85
#define min(x,y) (((x) < (y)) ? (x) : (y))
#define max(x,y) (((x) > (y)) ? (x) : (y))

#ifndef TRUE
#define TRUE 1
#define FALSE 0
86
#endif /* no TRUE */
Jim Blandy's avatar
Jim Blandy committed
87

88 89
Lisp_Object Vmenu_updating_frame;

90 91
Lisp_Object Qdebug_on_next_call;

92
extern Lisp_Object Qmenu_bar;
93
extern Lisp_Object Qmouse_click, Qevent_kind;
94

95
extern Lisp_Object QCtoggle, QCradio;
96

Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99 100 101 102 103
extern Lisp_Object Voverriding_local_map;
extern Lisp_Object Voverriding_local_map_menu_flag;

extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;

extern Lisp_Object Qmenu_bar_update_hook;

104
#ifdef USE_X_TOOLKIT
Richard M. Stallman's avatar
Richard M. Stallman committed
105
extern void set_frame_menubar ();
106 107 108
extern void process_expose_from_menu ();
extern XtAppContext Xt_app_con;

109
static Lisp_Object xdialog_show ();
110
void popup_get_selection ();
111 112
#endif

113 114 115
static Lisp_Object xmenu_show ();
static void keymap_panes ();
static void single_keymap_panes ();
116
static void single_menu_item ();
117 118 119 120 121
static void list_of_panes ();
static void list_of_items ();

/* This holds a Lisp vector that holds the results of decoding
   the keymaps or alist-of-alists that specify a menu.
Jim Blandy's avatar
Jim Blandy committed
122

123
   It describes the panes and items within the panes.
Jim Blandy's avatar
Jim Blandy committed
124

125 126
   Each pane is described by 3 elements in the vector:
   t, the pane name, the pane's prefix key.
127
   Then follow the pane's items, with 5 elements per item:
128
   the item string, the enable flag, the item's value,
129
   the definition, and the equivalent keyboard key's description string.
Jim Blandy's avatar
Jim Blandy committed
130

131 132 133 134 135
   In some cases, multiple levels of menus may be described.
   A single vector slot containing nil indicates the start of a submenu.
   A single vector slot containing lambda indicates the end of a submenu.
   The submenu follows a menu item which is the way to reach the submenu.

136 137 138
   A single vector slot containing quote indicates that the
   following items should appear on the right of a dialog box.

139 140
   Using a Lisp vector to hold this information while we decode it
   takes care of protecting all the data from GC.  */
Jim Blandy's avatar
Jim Blandy committed
141

142 143 144
#define MENU_ITEMS_PANE_NAME 1
#define MENU_ITEMS_PANE_PREFIX 2
#define MENU_ITEMS_PANE_LENGTH 3
145

146 147 148 149
#define MENU_ITEMS_ITEM_NAME 0
#define MENU_ITEMS_ITEM_ENABLE 1
#define MENU_ITEMS_ITEM_VALUE 2
#define MENU_ITEMS_ITEM_EQUIV_KEY 3
150 151
#define MENU_ITEMS_ITEM_DEFINITION 4
#define MENU_ITEMS_ITEM_LENGTH 5
152

153
static Lisp_Object menu_items;
154

155 156
/* Number of slots currently allocated in menu_items.  */
static int menu_items_allocated;
157

158 159
/* This is the index in menu_items of the first empty slot.  */
static int menu_items_used;
160

161 162
/* The number of panes currently recorded in menu_items,
   excluding those within submenus.  */
163
static int menu_items_n_panes;
164

165 166 167
/* Current depth within submenus.  */
static int menu_items_submenu_depth;

168
/* Flag which when set indicates a dialog or menu has been posted by
169
   Xt on behalf of one of the widget sets.  */
170 171
static int popup_activated_flag;

Richard M. Stallman's avatar
Richard M. Stallman committed
172
static int next_menubar_widget_id;
173

174 175 176 177 178 179 180 181
/* This is set nonzero after the user activates the menu bar, and set
   to zero again after the menu bars are redisplayed by prepare_menu_bar.
   While it is nonzero, all calls to set_frame_menubar go deep.

   I don't understand why this is needed, but it does seem to be
   needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>.  */

int pending_menu_activation;
182

Richard M. Stallman's avatar
Richard M. Stallman committed
183
#ifdef USE_X_TOOLKIT
184

185
/* Return the frame whose ->output_data.x->id equals ID, or 0 if none.  */
186

Richard M. Stallman's avatar
Richard M. Stallman committed
187 188 189 190 191 192
static struct frame *
menubar_id_to_frame (id)
     LWLIB_ID id;
{
  Lisp_Object tail, frame;
  FRAME_PTR f;
193

Richard M. Stallman's avatar
Richard M. Stallman committed
194
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
195
    {
Richard M. Stallman's avatar
Richard M. Stallman committed
196 197 198 199
      frame = XCONS (tail)->car;
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
Karl Heuer's avatar
Karl Heuer committed
200
      if (f->output_data.nothing == 1)
Richard M. Stallman's avatar
Richard M. Stallman committed
201
	continue;
202
      if (f->output_data.x->id == id)
Richard M. Stallman's avatar
Richard M. Stallman committed
203
	return f;
204
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
205
  return 0;
206
}
Richard M. Stallman's avatar
Richard M. Stallman committed
207 208

#endif
209

210 211 212 213 214 215 216 217 218 219
/* Initialize the menu_items structure if we haven't already done so.
   Also mark it as currently empty.  */

static void
init_menu_items ()
{
  if (NILP (menu_items))
    {
      menu_items_allocated = 60;
      menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
220 221
    }

222 223
  menu_items_used = 0;
  menu_items_n_panes = 0;
224
  menu_items_submenu_depth = 0;
225
}
226

227 228
/* Call at the end of generating the data in menu_items.
   This fills in the number of items in the last pane.  */
229

230 231 232 233
static void
finish_menu_items ()
{
}
234

235 236
/* Call when finished using the data for the current menu
   in menu_items.  */
237

238 239 240 241 242 243 244 245 246 247 248
static void
discard_menu_items ()
{
  /* Free the structure if it is especially large.
     Otherwise, hold on to it, to save time.  */
  if (menu_items_allocated > 200)
    {
      menu_items = Qnil;
      menu_items_allocated = 0;
    }
}
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
/* Make the menu_items vector twice as large.  */

static void
grow_menu_items ()
{
  Lisp_Object old;
  int old_size = menu_items_allocated;
  old = menu_items;

  menu_items_allocated *= 2;
  menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
  bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
	 old_size * sizeof (Lisp_Object));
}

/* Begin a submenu.  */

static void
push_submenu_start ()
{
  if (menu_items_used + 1 > menu_items_allocated)
    grow_menu_items ();

  XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
  menu_items_submenu_depth++;
}

/* End a submenu.  */

static void
push_submenu_end ()
{
  if (menu_items_used + 1 > menu_items_allocated)
    grow_menu_items ();

  XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
  menu_items_submenu_depth--;
}

289 290 291 292 293 294 295 296 297 298 299
/* Indicate boundary between left and right.  */

static void
push_left_right_boundary ()
{
  if (menu_items_used + 1 > menu_items_allocated)
    grow_menu_items ();

  XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
}

300 301
/* Start a new menu pane in menu_items..
   NAME is the pane name.  PREFIX_VEC is a prefix key for this pane.  */
302

303 304 305 306 307
static void
push_menu_pane (name, prefix_vec)
     Lisp_Object name, prefix_vec;
{
  if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
308
    grow_menu_items ();
Jim Blandy's avatar
Jim Blandy committed
309

310 311
  if (menu_items_submenu_depth == 0)
    menu_items_n_panes++;
312 313 314 315
  XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
  XVECTOR (menu_items)->contents[menu_items_used++] = name;
  XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
}
Jim Blandy's avatar
Jim Blandy committed
316

317 318 319
/* Push one menu item into the current pane.
   NAME is the string to display.  ENABLE if non-nil means
   this item can be selected.  KEY is the key generated by
320 321 322 323
   choosing this item, or nil if this item doesn't really have a definition.
   DEF is the definition of this item.
   EQUIV is the textual description of the keyboard equivalent for
   this item (or nil if none).  */
324

325
static void
326 327
push_menu_item (name, enable, key, def, equiv)
     Lisp_Object name, enable, key, def, equiv;
328 329
{
  if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
330
    grow_menu_items ();
331

332 333 334 335
  XVECTOR (menu_items)->contents[menu_items_used++] = name;
  XVECTOR (menu_items)->contents[menu_items_used++] = enable;
  XVECTOR (menu_items)->contents[menu_items_used++] = key;
  XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
336
  XVECTOR (menu_items)->contents[menu_items_used++] = def;
337 338 339
}

/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
340 341 342
   and generate menu panes for them in menu_items.
   If NOTREAL is nonzero,
   don't bother really computing whether an item is enabled.  */
343

344
static void
345
keymap_panes (keymaps, nmaps, notreal)
346 347
     Lisp_Object *keymaps;
     int nmaps;
348
     int notreal;
349
{
350
  int mapno;
351

352
  init_menu_items ();
353

354 355 356 357
  /* Loop over the given keymaps, making a pane for each map.
     But don't make a pane that is empty--ignore that map instead.
     P is the number of panes we have made so far.  */
  for (mapno = 0; mapno < nmaps; mapno++)
358
    single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal, 10);
359 360 361 362 363 364 365

  finish_menu_items ();
}

/* This is a recursive subroutine of keymap_panes.
   It handles one keymap, KEYMAP.
   The other arguments are passed along
366
   or point to local variables of the previous function.
367 368
   If NOTREAL is nonzero, only check for equivalent key bindings, don't
   evaluate expressions in menu items and don't make any menu.
369 370

   If we encounter submenus deeper than MAXDEPTH levels, ignore them.  */
371 372

static void
373
single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
374 375 376
     Lisp_Object keymap;
     Lisp_Object pane_name;
     Lisp_Object prefix;
377
     int notreal;
378
     int maxdepth;
379
{
380 381 382 383
  Lisp_Object pending_maps = Qnil;
  Lisp_Object tail, item;
  struct gcpro gcpro1, gcpro2;
  int notbuttons = 0;
384

385 386 387
  if (maxdepth <= 0)
    return;

388 389
  push_menu_pane (pane_name, prefix);

390 391 392 393 394 395 396 397
#ifndef HAVE_BOXES
  /* Remember index for first item in this pane so we can go back and
     add a prefix when (if) we see the first button.  After that, notbuttons
     is set to 0, to mark that we have seen a button and all non button
     items need a prefix.  */
  notbuttons = menu_items_used;
#endif

398
  for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
399
    {
400 401 402
      GCPRO2 (keymap, pending_maps);
      /* Look at each key binding, and if it is a menu item add it
	 to this menu.  */
403
      item = XCONS (tail)->car;
404
      if (CONSP (item))
405 406
	single_menu_item (XCONS (item)->car, XCONS (item)->cdr,
			  &pending_maps, notreal, maxdepth, &notbuttons);
407
      else if (VECTORP (item))
408 409 410 411 412 413 414
	{
	  /* Loop over the char values represented in the vector.  */
	  int len = XVECTOR (item)->size;
	  int c;
	  for (c = 0; c < len; c++)
	    {
	      Lisp_Object character;
415
	      XSETFASTINT (character, c);
416 417
	      single_menu_item (character, XVECTOR (item)->contents[c],
				&pending_maps, notreal, maxdepth, &notbuttons);
418
	    }
419
	}
420
      UNGCPRO;
421
    }
422 423 424 425

  /* Process now any submenus which want to be panes at this level.  */
  while (!NILP (pending_maps))
    {
426
      Lisp_Object elt, eltcdr, string;
427 428
      elt = Fcar (pending_maps);
      eltcdr = XCONS (elt)->cdr;
429 430 431 432
      string = XCONS (eltcdr)->car;
      /* We no longer discard the @ from the beginning of the string here.
	 Instead, we do this in xmenu_show.  */
      single_keymap_panes (Fcar (elt), string,
433
			   XCONS (eltcdr)->cdr, notreal, maxdepth - 1);
434 435
      pending_maps = Fcdr (pending_maps);
    }
436
}
437

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/* This is a subroutine of single_keymap_panes that handles one
   keymap entry.
   KEY is a key in a keymap and ITEM is its binding. 
   PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
   separate panes.
   If NOTREAL is nonzero, only check for equivalent key bindings, don't
   evaluate expressions in menu items and don't make any menu.
   If we encounter submenus deeper than MAXDEPTH levels, ignore them.
   NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
   buttons.  It points to variable notbuttons in single_keymap_panes,
   which keeps track of if we have seen a button in this menu or not.  */

static void
single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
		  notbuttons_ptr)
     Lisp_Object key, item;
     Lisp_Object *pending_maps_ptr;
     int maxdepth, notreal;
     int *notbuttons_ptr;
{
  Lisp_Object def, map, item_string, enabled;
  struct gcpro gcpro1, gcpro2;
  int res;
  
  /* Parse the menu item and leave the result in item_properties.  */
  GCPRO2 (key, item);
  res = parse_menu_item (item, notreal, 0);
  UNGCPRO;
  if (!res)
    return;			/* Not a menu item.  */

  map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
  
  if (notreal)
    {
      /* We don't want to make a menu, just traverse the keymaps to
	 precompute equivalent key bindings.  */
      if (!NILP (map))
	single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
      return;
    }

  enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
  item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]; 

  if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
    {
      if (!NILP (enabled))
	/* An enabled separate pane. Remember this to handle it later.  */
	*pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
				   *pending_maps_ptr);
      return;
    }

#ifndef HAVE_BOXES
  /* Simulate radio buttons and toggle boxes by putting a prefix in
     front of them.  */
  {
    Lisp_Object prefix = Qnil;
    Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
    if (!NILP (type))
      {
	Lisp_Object selected
	  = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];

	if (*notbuttons_ptr)
	  /* The first button. Line up previous items in this menu.  */
	  {
	    int index = *notbuttons_ptr; /* Index for first item this menu.  */
	    int submenu = 0;
	    Lisp_Object tem;
	    while (index < menu_items_used)
	      {
		tem
		  = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
		if (NILP (tem))
		  {
		    index++;
		    submenu++;		/* Skip sub menu.  */
		  }
		else if (EQ (tem, Qlambda))
		  {
		    index++;
		    submenu--;		/* End sub menu.  */
		  }
		else if (EQ (tem, Qt))
		  index += 3;		/* Skip new pane marker. */
		else if (EQ (tem, Qquote))
		  index++;		/* Skip a left, right divider. */
		else
		  {
		    if (!submenu && XSTRING (tem)->data[0] != '\0'
			&& XSTRING (tem)->data[0] != '-')
		      XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
			= concat2 (build_string ("    "), tem);
		    index += MENU_ITEMS_ITEM_LENGTH;
		  }
	      }
	    *notbuttons_ptr = 0;
	  }

	/* Calculate prefix, if any, for this item.  */
	if (EQ (type, QCtoggle))
	  prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
	else if (EQ (type, QCradio))
	  prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
      }
    /* Not a button. If we have earlier buttons, then we need a prefix.  */
    else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
	     && XSTRING (item_string)->data[0] != '-')
      prefix = build_string ("    ");

    if (!NILP (prefix))
      item_string = concat2 (prefix, item_string);
  }
#endif /* not HAVE_BOXES */
 
#ifndef USE_X_TOOLKIT
  if (!NILP(map))
    /* Indicate visually that this is a submenu.  */
    item_string = concat2 (item_string, build_string (" >"));
#endif

  push_menu_item (item_string, enabled, key,
		  XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
		  XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]);

#ifdef USE_X_TOOLKIT
  /* Display a submenu using the toolkit.  */
  if (! (NILP (map) || NILP (enabled)))
    {
      push_submenu_start ();
      single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
      push_submenu_end ();
    }
#endif
}

Karl Heuer's avatar
Karl Heuer committed
576
/* Push all the panes and items of a menu described by the
577 578
   alist-of-alists MENU.
   This handles old-fashioned calls to x-popup-menu.  */
579

580 581
static void
list_of_panes (menu)
582 583
     Lisp_Object menu;
{
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  Lisp_Object tail;

  init_menu_items ();

  for (tail = menu; !NILP (tail); tail = Fcdr (tail))
    {
      Lisp_Object elt, pane_name, pane_data;
      elt = Fcar (tail);
      pane_name = Fcar (elt);
      CHECK_STRING (pane_name, 0);
      push_menu_pane (pane_name, Qnil);
      pane_data = Fcdr (elt);
      CHECK_CONS (pane_data, 0);
      list_of_items (pane_data);
    }

  finish_menu_items ();
}

/* Push the items in a single pane defined by the alist PANE.  */

static void
list_of_items (pane)
     Lisp_Object pane;
{
  Lisp_Object tail, item, item1;

  for (tail = pane; !NILP (tail); tail = Fcdr (tail))
    {
      item = Fcar (tail);
      if (STRINGP (item))
615
	push_menu_item (item, Qnil, Qnil, Qt, Qnil);
616 617
      else if (NILP (item))
	push_left_right_boundary ();
618 619 620 621 622
      else
	{
	  CHECK_CONS (item, 0);
	  item1 = Fcar (item);
	  CHECK_STRING (item1, 1);
623
	  push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
624 625 626 627
	}
    }
}

628
DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
629 630 631
  "Pop up a deck-of-cards menu and return user's selection.\n\
POSITION is a position specification.  This is either a mouse button event\n\
or a list ((XOFFSET YOFFSET) WINDOW)\n\
632
where XOFFSET and YOFFSET are positions in pixels from the top left\n\
633 634 635 636 637 638 639 640 641 642
corner of WINDOW's frame.  (WINDOW may be a frame object instead of a window.)\n\
This controls the position of the center of the first line\n\
in the first pane of the menu, not the top left of the menu as a whole.\n\
If POSITION is t, it means to use the current mouse position.\n\
\n\
MENU is a specifier for a menu.  For the simplest case, MENU is a keymap.\n\
The menu items come from key bindings that have a menu string as well as\n\
a definition; actually, the \"definition\" in such a key binding looks like\n\
\(STRING . REAL-DEFINITION).  To give the menu a title, put a string into\n\
the keymap as a top-level element.\n\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
643 644 645
If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
\n\
646 647 648 649
You can also use a list of keymaps as MENU.\n\
  Then each keymap makes a separate pane.\n\
When MENU is a keymap or a list of keymaps, the return value\n\
is a list of events.\n\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
650
\n\
651 652 653 654 655 656 657 658 659 660 661 662 663 664
Alternatively, you can specify a menu of multiple panes\n\
  with a list of the form (TITLE PANE1 PANE2...),\n\
where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
Each ITEM is normally a cons cell (STRING . VALUE);\n\
but a string can appear as an item--that makes a nonselectable line\n\
in the menu.\n\
With this form of menu, the return value is VALUE from the chosen item.\n\
\n\
If POSITION is nil, don't display the menu at all, just precalculate the\n\
cached information about equivalent key sequences.")
  (position, menu)
     Lisp_Object position, menu;
{
  int number_of_panes, panes;
665
  Lisp_Object keymap, tem;
666 667 668 669
  int xpos, ypos;
  Lisp_Object title;
  char *error_name;
  Lisp_Object selection;
670
  int i, j;
671 672 673
  FRAME_PTR f;
  Lisp_Object x, y, window;
  int keymaps = 0;
674
  int for_click = 0;
675 676
  struct gcpro gcpro1;

677
#ifdef HAVE_MENUS
678 679
  if (! NILP (position))
    {
680 681
      check_x ();

682
      /* Decode the first argument: find the window and the coordinates.  */
683 684
      if (EQ (position, Qt)
	  || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
685 686
	{
	  /* Use the mouse's current position.  */
687
	  FRAME_PTR new_f = selected_frame;
688 689 690 691
	  Lisp_Object bar_window;
	  int part;
	  unsigned long time;

692
	  if (mouse_position_hook)
693 694
	    (*mouse_position_hook) (&new_f, 1, &bar_window,
				    &part, &x, &y, &time);
695
	  if (new_f != 0)
696
	    XSETFRAME (window, new_f);
697 698 699
	  else
	    {
	      window = selected_window;
700 701
	      XSETFASTINT (x, 0);
	      XSETFASTINT (y, 0);
702
	    }
703 704 705 706
	}
      else
	{
	  tem = Fcar (position);
707
	  if (CONSP (tem))
708 709 710 711 712 713 714
	    {
	      window = Fcar (Fcdr (position));
	      x = Fcar (tem);
	      y = Fcar (Fcdr (tem));
	    }
	  else
	    {
715
	      for_click = 1;
716 717 718 719 720 721 722 723 724 725 726 727 728
	      tem = Fcar (Fcdr (position));  /* EVENT_START (position) */
	      window = Fcar (tem);	     /* POSN_WINDOW (tem) */
	      tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
	      x = Fcar (tem);
	      y = Fcdr (tem);
	    }
	}

      CHECK_NUMBER (x, 0);
      CHECK_NUMBER (y, 0);

      /* Decode where to put the menu.  */

729
      if (FRAMEP (window))
730 731 732 733 734
	{
	  f = XFRAME (window);
	  xpos = 0;
	  ypos = 0;
	}
735
      else if (WINDOWP (window))
736 737 738 739
	{
	  CHECK_LIVE_WINDOW (window, 0);
	  f = XFRAME (WINDOW_FRAME (XWINDOW (window)));

740 741 742 743
	  xpos = (FONT_WIDTH (f->output_data.x->font)
		  * XFASTINT (XWINDOW (window)->left));
	  ypos = (f->output_data.x->line_height
		  * XFASTINT (XWINDOW (window)->top));
744 745 746 747 748 749 750 751
	}
      else
	/* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
	   but I don't want to make one now.  */
	CHECK_WINDOW (window, 0);

      xpos += XINT (x);
      ypos += XINT (y);
752 753

      XSETFRAME (Vmenu_updating_frame, f);
754
    }
755
  Vmenu_updating_frame = Qnil;
756
#endif /* HAVE_MENUS */
757 758 759 760 761

  title = Qnil;
  GCPRO1 (title);

  /* Decode the menu items from what was specified.  */
762 763 764

  keymap = Fkeymapp (menu);
  tem = Qnil;
765
  if (CONSP (menu))
766 767 768 769 770 771 772
    tem = Fkeymapp (Fcar (menu));
  if (!NILP (keymap))
    {
      /* We were given a keymap.  Extract menu info from the keymap.  */
      Lisp_Object prompt;
      keymap = get_keymap (menu);

773
      /* Extract the detailed info to make one pane.  */
774
      keymap_panes (&menu, 1, NILP (position));
775

776 777 778
      /* Search for a string appearing directly as an element of the keymap.
	 That string is the title of the menu.  */
      prompt = map_prompt (keymap);
779 780
      if (NILP (title) && !NILP (prompt))
	title = prompt;
781

782 783 784 785 786
      /* Make that be the pane title of the first pane.  */
      if (!NILP (prompt) && menu_items_n_panes >= 0)
	XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;

      keymaps = 1;
787 788 789 790 791 792 793 794
    }
  else if (!NILP (tem))
    {
      /* We were given a list of keymaps.  */
      int nmaps = XFASTINT (Flength (menu));
      Lisp_Object *maps
	= (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
      int i;
795 796

      title = Qnil;
797 798 799

      /* The first keymap that has a prompt string
	 supplies the menu title.  */
800
      for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
801
	{
802 803
	  Lisp_Object prompt;

804 805 806
	  maps[i++] = keymap = get_keymap (Fcar (tem));

	  prompt = map_prompt (keymap);
807 808
	  if (NILP (title) && !NILP (prompt))
	    title = prompt;
809 810 811
	}

      /* Extract the detailed info to make one pane.  */
812
      keymap_panes (maps, nmaps, NILP (position));
813 814 815 816 817 818

      /* Make the title be the pane title of the first pane.  */
      if (!NILP (title) && menu_items_n_panes >= 0)
	XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;

      keymaps = 1;
819 820 821 822
    }
  else
    {
      /* We were given an old-fashioned menu.  */
823 824
      title = Fcar (menu);
      CHECK_STRING (title, 1);
825

826
      list_of_panes (Fcdr (menu));
827

828 829
      keymaps = 0;
    }
830
  
831
  if (NILP (position))
832
    {
833 834 835
      discard_menu_items ();
      UNGCPRO;
      return Qnil;
836 837
    }

838
#ifdef HAVE_MENUS
839 840
  /* Display them in a menu.  */
  BLOCK_INPUT;
841

842
  selection = xmenu_show (f, xpos, ypos, for_click,
843 844
			  keymaps, title, &error_name);
  UNBLOCK_INPUT;
845

846
  discard_menu_items ();
847

848
  UNGCPRO;
849
#endif /* HAVE_MENUS */
850

851 852
  if (error_name) error (error_name);
  return selection;
853
}
854

855 856
#ifdef HAVE_MENUS

857
DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
858
  "Pop up a dialog box and return user's selection.\n\
859 860 861 862
POSITION specifies which frame to use.\n\
This is normally a mouse button event or a window or frame.\n\
If POSITION is t, it means to use the frame the mouse is on.\n\
The dialog box appears in the middle of the specified frame.\n\
863
\n\
864 865 866
CONTENTS specifies the alternatives to display in the dialog box.\n\
It is a list of the form (TITLE ITEM1 ITEM2...).\n\
Each ITEM is a cons cell (STRING . VALUE).\n\
867 868 869 870 871
The return value is VALUE from the chosen item.\n\n\
An ITEM may also be just a string--that makes a nonselectable item.\n\
An ITEM may also be nil--that means to put all preceding items\n\
on the left of the dialog box and all following items on the right.\n\
\(By default, approximately half appear on each side.)")
872 873
  (position, contents)
     Lisp_Object position, contents;
874 875
{
  FRAME_PTR f;
876
  Lisp_Object window;
877 878 879

  check_x ();

880
  /* Decode the first argument: find the window or frame to use.  */
881 882
  if (EQ (position, Qt)
      || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
883
    {
884
#if 0 /* Using the frame the mouse is on may not be right.  */
885
      /* Use the mouse's current position.  */
886
      FRAME_PTR new_f = selected_frame;
887 888 889 890
      Lisp_Object bar_window;
      int part;
      unsigned long time;
      Lisp_Object x, y;
891

892
      (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
893

894
      if (new_f != 0)
895
	XSETFRAME (window, new_f);
896 897
      else
	window = selected_window;
898
#endif
899
      window = selected_window;
900 901 902 903 904
    }
  else if (CONSP (position))
    {
      Lisp_Object tem;
      tem = Fcar (position);
905
      if (CONSP (tem))
906
	window = Fcar (Fcdr (position));
907 908
      else
	{
909 910
	  tem = Fcar (Fcdr (position));  /* EVENT_START (position) */
	  window = Fcar (tem);	     /* POSN_WINDOW (tem) */
911 912
	}
    }
913 914
  else if (WINDOWP (position) || FRAMEP (position))
    window = position;
915 916
  else
    window = Qnil;
917

918
  /* Decode where to put the menu.  */
919

920
  if (FRAMEP (window))
921
    f = XFRAME (window);
922
  else if (WINDOWP (window))
923
    {
924 925
      CHECK_LIVE_WINDOW (window, 0);
      f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
926
    }
927 928 929 930
  else
    /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
       but I don't want to make one now.  */
    CHECK_WINDOW (window, 0);
931

932 933 934 935 936
#ifndef USE_X_TOOLKIT
  /* Display a menu with these alternatives
     in the middle of frame F.  */
  {
    Lisp_Object x, y, frame, newpos;
937 938 939
    XSETFRAME (frame, f);
    XSETINT (x, x_pixel_width (f) / 2);
    XSETINT (y, x_pixel_height (f) / 2);
940 941 942 943 944 945 946 947 948 949
    newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));

    return Fx_popup_menu (newpos,
			  Fcons (Fcar (contents), Fcons (contents, Qnil)));
  }
#else
  {
    Lisp_Object title;
    char *error_name;
    Lisp_Object selection;
950

951 952 953
    /* Decode the dialog items from what was specified.  */
    title = Fcar (contents);
    CHECK_STRING (title, 1);
954

955
    list_of_panes (Fcons (contents, Qnil));
956

957 958
    /* Display them in a dialog box.  */
    BLOCK_INPUT;
959
    selection = xdialog_show (f, 0, title, &error_name);
960
    UNBLOCK_INPUT;
961

962 963 964 965 966
    discard_menu_items ();

    if (error_name) error (error_name);
    return selection;
  }
967
#endif
968
}
969 970

#ifdef USE_X_TOOLKIT
971

972
/* Loop in Xt until the menu pulldown or dialog popup has been
Richard M. Stallman's avatar
Richard M. Stallman committed
973 974
   popped down (deactivated).  This is used for x-popup-menu
   and x-popup-dialog; it is not used for the menu bar any more.
975

976
   NOTE: All calls to popup_get_selection should be protected
977
   with BLOCK_INPUT, UNBLOCK_INPUT wrappers.  */
978

979
void
980
popup_get_selection (initial_event, dpyinfo, id)
981
     XEvent *initial_event;
982
     struct x_display_info *dpyinfo;
983
     LWLIB_ID id;
984
{
985
  XEvent event;
986

987 988 989 990 991 992 993 994 995 996 997
  /* Define a queue to save up for later unreading
     all X events that don't pertain to the menu.  */
  struct event_queue
    {