home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / xmenu.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  93KB  |  3,270 lines

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