xmenu.c 75.1 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>
Dave Love's avatar
Dave Love committed
36 37 38
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
39 40

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

49 50 51 52
#ifdef MSDOS
#include "msdos.h"
#endif

Morten Welinder's avatar
Morten Welinder committed
53
#ifdef HAVE_X_WINDOWS
Jim Blandy's avatar
Jim Blandy committed
54 55 56
/* 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
57
#endif
Jim Blandy's avatar
Jim Blandy committed
58 59 60 61 62 63 64 65 66

/* 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
67
#ifdef HAVE_X_WINDOWS
68
#undef HAVE_MULTILINGUAL_MENU
69 70 71 72 73
#ifdef USE_X_TOOLKIT
#include <X11/Xlib.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <X11/StringDefs.h>
74
#include <X11/Shell.h>
75
#ifdef USE_LUCID
76
#include <X11/Xaw/Paned.h>
77
#endif /* USE_LUCID */
78
#include "../lwlib/lwlib.h"
79 80 81 82
#else /* not USE_X_TOOLKIT */
#include "../oldXMenu/XMenu.h"
#endif /* not USE_X_TOOLKIT */
#endif /* HAVE_X_WINDOWS */
83

Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
84 85 86 87
#ifdef USE_MOTIF
#include <Xm/Xm.h>		/* for LESSTIF_VERSION */
#endif

Jim Blandy's avatar
Jim Blandy committed
88 89 90 91 92 93
#define min(x,y) (((x) < (y)) ? (x) : (y))
#define max(x,y) (((x) > (y)) ? (x) : (y))

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

96 97
Lisp_Object Vmenu_updating_frame;

98 99
Lisp_Object Qdebug_on_next_call;

100
extern Lisp_Object Qmenu_bar;
101
extern Lisp_Object Qmouse_click, Qevent_kind;
102

103
extern Lisp_Object QCtoggle, QCradio;
104

Richard M. Stallman's avatar
Richard M. Stallman committed
105 106 107 108 109 110 111
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;

112
#ifdef USE_X_TOOLKIT
Richard M. Stallman's avatar
Richard M. Stallman committed
113
extern void set_frame_menubar ();
114 115 116
extern void process_expose_from_menu ();
extern XtAppContext Xt_app_con;

117
static Lisp_Object xdialog_show ();
118
void popup_get_selection ();
119 120
#endif

Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
121 122 123 124 125 126 127 128 129 130
#ifdef USE_X_TOOLKIT

/* Define HAVE_BOXES if meus can handle radio and toggle buttons.  */

#define HAVE_BOXES 1
#endif

static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
				Lisp_Object, Lisp_Object, Lisp_Object,
				Lisp_Object));
131 132 133
static Lisp_Object xmenu_show ();
static void keymap_panes ();
static void single_keymap_panes ();
134
static void single_menu_item ();
135 136 137 138 139
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
140

141
   It describes the panes and items within the panes.
Jim Blandy's avatar
Jim Blandy committed
142

143 144
   Each pane is described by 3 elements in the vector:
   t, the pane name, the pane's prefix key.
145
   Then follow the pane's items, with 5 elements per item:
146
   the item string, the enable flag, the item's value,
147
   the definition, and the equivalent keyboard key's description string.
Jim Blandy's avatar
Jim Blandy committed
148

149 150 151 152 153
   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.

154 155 156
   A single vector slot containing quote indicates that the
   following items should appear on the right of a dialog box.

157 158
   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
159

160 161 162
#define MENU_ITEMS_PANE_NAME 1
#define MENU_ITEMS_PANE_PREFIX 2
#define MENU_ITEMS_PANE_LENGTH 3
163

164 165 166 167
#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
168
#define MENU_ITEMS_ITEM_DEFINITION 4
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
169 170 171
#define MENU_ITEMS_ITEM_TYPE 5
#define MENU_ITEMS_ITEM_SELECTED 6
#define MENU_ITEMS_ITEM_LENGTH 7
172

173
static Lisp_Object menu_items;
174

175 176
/* Number of slots currently allocated in menu_items.  */
static int menu_items_allocated;
177

178 179
/* This is the index in menu_items of the first empty slot.  */
static int menu_items_used;
180

181 182
/* The number of panes currently recorded in menu_items,
   excluding those within submenus.  */
183
static int menu_items_n_panes;
184

185 186 187
/* Current depth within submenus.  */
static int menu_items_submenu_depth;

188
/* Flag which when set indicates a dialog or menu has been posted by
189
   Xt on behalf of one of the widget sets.  */
190 191
static int popup_activated_flag;

Richard M. Stallman's avatar
Richard M. Stallman committed
192
static int next_menubar_widget_id;
193

194 195 196 197 198 199 200 201
/* 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;
202

Richard M. Stallman's avatar
Richard M. Stallman committed
203
#ifdef USE_X_TOOLKIT
204

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

Richard M. Stallman's avatar
Richard M. Stallman committed
207 208 209 210 211 212
static struct frame *
menubar_id_to_frame (id)
     LWLIB_ID id;
{
  Lisp_Object tail, frame;
  FRAME_PTR f;
213

214
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
215
    {
216
      frame = XCAR (tail);
Richard M. Stallman's avatar
Richard M. Stallman committed
217 218 219
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
Karl Heuer's avatar
Karl Heuer committed
220
      if (f->output_data.nothing == 1)
Richard M. Stallman's avatar
Richard M. Stallman committed
221
	continue;
222
      if (f->output_data.x->id == id)
Richard M. Stallman's avatar
Richard M. Stallman committed
223
	return f;
224
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
225
  return 0;
226
}
Richard M. Stallman's avatar
Richard M. Stallman committed
227 228

#endif
229

230 231 232 233 234 235 236 237 238 239
/* 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);
240 241
    }

242 243
  menu_items_used = 0;
  menu_items_n_panes = 0;
244
  menu_items_submenu_depth = 0;
245
}
246

247 248
/* Call at the end of generating the data in menu_items.
   This fills in the number of items in the last pane.  */
249

250 251 252 253
static void
finish_menu_items ()
{
}
254

255 256
/* Call when finished using the data for the current menu
   in menu_items.  */
257

258 259 260 261 262 263 264 265 266 267 268
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;
    }
}
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/* 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--;
}

309 310 311 312 313 314 315 316 317 318 319
/* 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;
}

320 321
/* Start a new menu pane in menu_items..
   NAME is the pane name.  PREFIX_VEC is a prefix key for this pane.  */
322

323 324 325 326 327
static void
push_menu_pane (name, prefix_vec)
     Lisp_Object name, prefix_vec;
{
  if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
328
    grow_menu_items ();
Jim Blandy's avatar
Jim Blandy committed
329

330 331
  if (menu_items_submenu_depth == 0)
    menu_items_n_panes++;
332 333 334 335
  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
336

Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
337 338 339 340 341 342 343
/* 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 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).  TYPE is the type of this menu
   item, one of nil, `toggle' or `radio'. */
344

345
static void
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
346 347
push_menu_item (name, enable, key, def, equiv, type, selected)
     Lisp_Object name, enable, key, def, equiv, type, selected;
348 349
{
  if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
350
    grow_menu_items ();
351

352 353 354 355
  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;
356
  XVECTOR (menu_items)->contents[menu_items_used++] = def;
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
357 358
  XVECTOR (menu_items)->contents[menu_items_used++] = type;
  XVECTOR (menu_items)->contents[menu_items_used++] = selected;
359 360 361
}

/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
362 363 364
   and generate menu panes for them in menu_items.
   If NOTREAL is nonzero,
   don't bother really computing whether an item is enabled.  */
365

366
static void
367
keymap_panes (keymaps, nmaps, notreal)
368 369
     Lisp_Object *keymaps;
     int nmaps;
370
     int notreal;
371
{
372
  int mapno;
373

374
  init_menu_items ();
375

376 377 378 379
  /* 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++)
380
    single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal, 10);
381 382 383 384 385 386 387

  finish_menu_items ();
}

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

   If we encounter submenus deeper than MAXDEPTH levels, ignore them.  */
393 394

static void
395
single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
396 397 398
     Lisp_Object keymap;
     Lisp_Object pane_name;
     Lisp_Object prefix;
399
     int notreal;
400
     int maxdepth;
401
{
402 403 404 405
  Lisp_Object pending_maps = Qnil;
  Lisp_Object tail, item;
  struct gcpro gcpro1, gcpro2;
  int notbuttons = 0;
406

407 408 409
  if (maxdepth <= 0)
    return;

410 411
  push_menu_pane (pane_name, prefix);

412 413 414 415 416 417 418 419
#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

420
  for (tail = keymap; CONSP (tail); tail = XCDR (tail))
421
    {
422 423 424
      GCPRO2 (keymap, pending_maps);
      /* Look at each key binding, and if it is a menu item add it
	 to this menu.  */
425
      item = XCAR (tail);
426
      if (CONSP (item))
427
	single_menu_item (XCAR (item), XCDR (item),
428
			  &pending_maps, notreal, maxdepth, &notbuttons);
429
      else if (VECTORP (item))
430 431 432 433 434 435 436
	{
	  /* 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;
437
	      XSETFASTINT (character, c);
438 439
	      single_menu_item (character, XVECTOR (item)->contents[c],
				&pending_maps, notreal, maxdepth, &notbuttons);
440
	    }
441
	}
442
      UNGCPRO;
443
    }
444 445 446 447

  /* Process now any submenus which want to be panes at this level.  */
  while (!NILP (pending_maps))
    {
448
      Lisp_Object elt, eltcdr, string;
449
      elt = Fcar (pending_maps);
450 451
      eltcdr = XCDR (elt);
      string = XCAR (eltcdr);
452 453 454
      /* 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,
455
			   XCDR (eltcdr), notreal, maxdepth - 1);
456 457
      pending_maps = Fcdr (pending_maps);
    }
458
}
459

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/* 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;
{
480
  Lisp_Object map, item_string, enabled;
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 576 577 578 579 580 581 582 583 584
  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],
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
585 586 587
		  XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
		  XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
		  XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]);
588 589 590 591 592 593 594 595 596 597 598 599

#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
600
/* Push all the panes and items of a menu described by the
601 602
   alist-of-alists MENU.
   This handles old-fashioned calls to x-popup-menu.  */
603

604 605
static void
list_of_panes (menu)
606 607
     Lisp_Object menu;
{
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
  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))
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
639
	push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil);
640 641
      else if (NILP (item))
	push_left_right_boundary ();
642 643 644 645 646
      else
	{
	  CHECK_CONS (item, 0);
	  item1 = Fcar (item);
	  CHECK_STRING (item1, 1);
Gerd Moellmann's avatar
Ditto.  
Gerd Moellmann committed
647
	  push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil);
648 649 650 651
	}
    }
}

652
DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
653 654 655
  "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\
656
where XOFFSET and YOFFSET are positions in pixels from the top left\n\
657 658 659 660 661 662 663 664 665 666
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
667 668 669
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\
670 671 672 673
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
674
\n\
675 676 677 678 679 680 681 682 683 684 685 686 687
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;
{
688
  Lisp_Object keymap, tem;
689 690 691 692 693 694 695
  int xpos, ypos;
  Lisp_Object title;
  char *error_name;
  Lisp_Object selection;
  FRAME_PTR f;
  Lisp_Object x, y, window;
  int keymaps = 0;
696
  int for_click = 0;
697 698
  struct gcpro gcpro1;

699
#ifdef HAVE_MENUS
700 701
  if (! NILP (position))
    {
702 703
      check_x ();

704
      /* Decode the first argument: find the window and the coordinates.  */
705
      if (EQ (position, Qt)
706
	  || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
707 708
	{
	  /* Use the mouse's current position.  */
709
	  FRAME_PTR new_f = SELECTED_FRAME ();
710
	  Lisp_Object bar_window;
Andreas Schwab's avatar
Andreas Schwab committed
711
	  enum scroll_bar_part part;
712 713
	  unsigned long time;

714
	  if (mouse_position_hook)
715 716
	    (*mouse_position_hook) (&new_f, 1, &bar_window,
				    &part, &x, &y, &time);
717
	  if (new_f != 0)
718
	    XSETFRAME (window, new_f);
719 720 721
	  else
	    {
	      window = selected_window;
722 723
	      XSETFASTINT (x, 0);
	      XSETFASTINT (y, 0);
724
	    }
725 726 727 728
	}
      else
	{
	  tem = Fcar (position);
729
	  if (CONSP (tem))
730 731 732 733 734 735 736
	    {
	      window = Fcar (Fcdr (position));
	      x = Fcar (tem);
	      y = Fcar (Fcdr (tem));
	    }
	  else
	    {
737
	      for_click = 1;
738 739 740 741 742 743 744 745 746 747 748 749 750
	      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.  */

751
      if (FRAMEP (window))
752 753 754 755 756
	{
	  f = XFRAME (window);
	  xpos = 0;
	  ypos = 0;
	}
757
      else if (WINDOWP (window))
758 759 760 761
	{
	  CHECK_LIVE_WINDOW (window, 0);
	  f = XFRAME (WINDOW_FRAME (XWINDOW (window)));

762 763 764 765
	  xpos = (FONT_WIDTH (f->output_data.x->font)
		  * XFASTINT (XWINDOW (window)->left));
	  ypos = (f->output_data.x->line_height
		  * XFASTINT (XWINDOW (window)->top));
766 767 768 769 770 771 772 773
	}
      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);
774 775

      XSETFRAME (Vmenu_updating_frame, f);
776
    }
777
  Vmenu_updating_frame = Qnil;
778
#endif /* HAVE_MENUS */
779 780 781 782 783

  title = Qnil;
  GCPRO1 (title);

  /* Decode the menu items from what was specified.  */
784 785 786

  keymap = Fkeymapp (menu);
  tem = Qnil;
787
  if (CONSP (menu))
788 789 790 791 792 793 794
    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);

795
      /* Extract the detailed info to make one pane.  */
796
      keymap_panes (&menu, 1, NILP (position));
797

798 799 800
      /* Search for a string appearing directly as an element of the keymap.
	 That string is the title of the menu.  */
      prompt = map_prompt (keymap);
801 802
      if (NILP (title) && !NILP (prompt))
	title = prompt;
803

804 805 806 807 808
      /* 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;
809 810 811 812 813 814 815 816
    }
  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;
817 818

      title = Qnil;
819 820 821

      /* The first keymap that has a prompt string
	 supplies the menu title.  */
822
      for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
823
	{
824 825
	  Lisp_Object prompt;

826 827 828
	  maps[i++] = keymap = get_keymap (Fcar (tem));

	  prompt = map_prompt (keymap);
829 830
	  if (NILP (title) && !NILP (prompt))
	    title = prompt;
831 832 833
	}

      /* Extract the detailed info to make one pane.  */
834
      keymap_panes (maps, nmaps, NILP (position));
835 836 837 838 839 840

      /* 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;
841 842 843 844
    }
  else
    {
      /* We were given an old-fashioned menu.  */
845 846
      title = Fcar (menu);
      CHECK_STRING (title, 1);
847

848
      list_of_panes (Fcdr (menu));
849

850 851
      keymaps = 0;
    }
852
  
853
  if (NILP (position))
854
    {
855 856 857
      discard_menu_items ();
      UNGCPRO;
      return Qnil;
858 859
    }

860
#ifdef HAVE_MENUS
861 862
  /* Display them in a menu.  */
  BLOCK_INPUT;
863

864
  selection = xmenu_show (f, xpos, ypos, for_click,
865 866
			  keymaps, title, &error_name);
  UNBLOCK_INPUT;
867

868
  discard_menu_items ();
869

870
  UNGCPRO;
871
#endif /* HAVE_MENUS */
872

873 874
  if (error_name) error (error_name);
  return selection;
875
}
876

877 878
#ifdef HAVE_MENUS

879
DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
880
  "Pop up a dialog box and return user's selection.\n\
881 882 883 884
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\
885
\n\
886 887 888
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\
889 890 891 892 893
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.)")
894 895
  (position, contents)
     Lisp_Object position, contents;
896 897
{
  FRAME_PTR f;
898
  Lisp_Object window;
899 900 901

  check_x ();

902
  /* Decode the first argument: find the window or frame to use.  */
903
  if (EQ (position, Qt)
904
      || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
905
    {
906
#if 0 /* Using the frame the mouse is on may not be right.  */
907
      /* Use the mouse's current position.  */
908
      FRAME_PTR new_f = SELECTED_FRAME ();
909 910 911 912
      Lisp_Object bar_window;
      int part;
      unsigned long time;
      Lisp_Object x, y;
913

914
      (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
915

916
      if (new_f != 0)
917
	XSETFRAME (window, new_f);
918 919
      else
	window = selected_window;
920
#endif
921
      window = selected_window;
922 923 924 925 926
    }
  else if (CONSP (position))
    {
      Lisp_Object tem;
      tem = Fcar (position);
927
      if (CONSP (tem))
928
	window = Fcar (Fcdr (position));
929 930
      else
	{
931 932
	  tem = Fcar (Fcdr (position));  /* EVENT_START (position) */
	  window = Fcar (tem);	     /* POSN_WINDOW (tem) */
933 934
	}
    }
935 936
  else if (WINDOWP (position) || FRAMEP (position))
    window = position;
937 938
  else
    window = Qnil;
939

940
  /* Decode where to put the menu.  */
941

942
  if (FRAMEP (window))
943
    f = XFRAME (window);
944
  else if (WINDOWP (window))
945
    {
946 947
      CHECK_LIVE_WINDOW (window, 0);
      f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
948
    }
949 950 951 952
  else
    /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
       but I don't want to make one now.  */
    CHECK_WINDOW (window, 0);
953

954 955 956 957 958
#ifndef USE_X_TOOLKIT
  /* Display a menu with these alternatives
     in the middle of frame F.  */
  {
    Lisp_Object x, y, frame, newpos;
959 960 961
    XSETFRAME (frame, f);
    XSETINT (x, x_pixel_width (f) / 2);
    XSETINT (y, x_pixel_height (f) / 2);
962 963 964 965 966 967 968 969 970 971
    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;
972

973 974 975
    /* Decode the dialog items from what was specified.  */
    title = Fcar (contents);
    CHECK_STRING (title, 1);
976

977
    list_of_panes (Fcons (contents, Qnil));
978

979 980
    /* Display them in a dialog box.  */
    BLOCK_INPUT;
981
    selection = xdialog_show (f, 0, title, &error_name);
982
    UNBLOCK_INPUT;
983

984 985 986 987 988
    discard_menu_items ();

    if (error_name) error (error_name);
    return selection;
  }
989
#endif
990
}
991 992

#ifdef USE_X_TOOLKIT
993

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

998
   NOTE: All calls to popup_get_selection should be protected
999
   with BLOCK_INPUT, UNBLOCK_INPUT wrappers.  */
1000

1001
void
1002
popup_get_selection (initial_event, dpyinfo, id)
1003
     XEvent *initial_event;
1004
     struct x_display_info *dpyinfo;
1005
     LWLIB_ID id;
1006
{
1007
  XEvent event;
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  /* Define a queue to save up for later unreading
     all X events that don't pertain to the menu.  */
  struct event_queue
    {
      XEvent event;
      struct event_queue *next;
    };
  
  struct event_queue *queue = NULL;
  struct event_queue *queue_tmp;

1020 1021 1022 1023
  if (initial_event)
    event = *initial_event;
  else
    XtAppNextEvent (Xt_app_con, &event);
1024

1025
  while (1)
1026
    {
1027 1028 1029
      /* Handle expose events for editor frames right away.  */
      if (event.type == Expose)
	process_expose_from_menu (event);
1030 1031 1032
      /* Make sure we don't consider buttons grabbed after menu goes.
	 And make sure to deactivate for any ButtonRelease,
	 even if XtDispatchEvent doesn't do that.  */
1033 1034
      else if (event.type == ButtonRelease
	       && dpyinfo->display == event.xbutton.display)
1035 1036 1037
        {
          dpyinfo->grabbed &= ~(1 << event.xbutton.button);
          popup_activated_flag = 0;
1038 1039 1040 1041 1042 1043
#ifdef USE_MOTIF /* Pretending that the event came from a 
		    Btn1Down seems the only way to convince Motif to
		    activate its callbacks; setting the XmNmenuPost
		    isn't working. --marcus@sysc.pdx.edu.  */
	  event.xbutton.button = 1;
#endif
1044
        }
1045 1046 1047 1048 1049
      /* If the user presses a key, deactivate the menu.
	 The user is likely to do that if we get wedged.  */
      else if (event.type == KeyPress
	       && dpyinfo->display == event.xbutton.display)
	{
1050 1051 1052 1053 1054 1055
	  KeySym keysym = XLookupKeysym (&event.xkey, 0);
	  if (!IsModifierKey (keysym))
	    {
	      popup_activated_flag = 0;
	      break;
	    }
1056
	}
1057 1058 1059 1060 1061 1062 1063 1064
      /* Button presses outside the menu also pop it down.  */
      else if (event.type == ButtonPress
	       && event.xany.display == dpyinfo->display
	       && x_any_window_to_frame (dpyinfo, event.xany.window))
	{
	  popup_activated_flag = 0;
	  break;
	}
1065 1066

      /* Queue all events not for this popup,
1067
	 except for Expose, which we've already handled, and ButtonRelease.
1068 1069 1070
	 Note that the X window is associated with the frame if this
	 is a menu bar popup, but not if it's a dialog box.  So we use
	 x_non_menubar_window_to_frame, not x_any_window_to_frame.  */
1071
      if (event.type != Expose