("Korean"): Doc fix.
[bpt/emacs.git] / lwlib / xlwmenu.c
index 4d4efd4..f6805e8 100644 (file)
@@ -14,30 +14,63 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+along with GNU Emacs; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 /* Created by devin@lucid.com */
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
 #include <stdio.h>
 
 #include <sys/types.h>
 #include <X11/Xos.h>
 #include <X11/IntrinsicP.h>
+#include <X11/ObjectP.h>
 #include <X11/StringDefs.h>
 #include <X11/cursorfont.h>
 #include <X11/bitmaps/gray>
 #include "xlwmenuP.h"
-#include <string.h>
+
+static int pointer_grabbed;
+static XEvent menu_post_event;
+
+XFontStruct *xlwmenu_default_font;
 
 static char 
 xlwMenuTranslations [] = 
-"<BtnDown>:    start()\n\
-<BtnMotion>:   drag()\n\
-<BtnUp>:       select()\n\
+"<BtnDown>:      start()\n\
+<Motion>:        drag()\n\
+<BtnUp>:         select()\n\
+<Key>Shift_L:     nothing()\n\
+<Key>Shift_R:     nothing()\n\
+<Key>Meta_L:      nothing()\n\
+<Key>Meta_R:      nothing()\n\
+<Key>Control_L:   nothing()\n\
+<Key>Control_R:   nothing()\n\
+<Key>Hyper_L:     nothing()\n\
+<Key>Hyper_R:     nothing()\n\
+<Key>Super_L:     nothing()\n\
+<Key>Super_R:     nothing()\n\
+<Key>Alt_L:       nothing()\n\
+<Key>Alt_R:       nothing()\n\
+<Key>Caps_Lock:   nothing()\n\
+<Key>Shift_Lock:  nothing()\n\
+<KeyUp>Shift_L:   nothing()\n\
+<KeyUp>Shift_R:   nothing()\n\
+<KeyUp>Meta_L:    nothing()\n\
+<KeyUp>Meta_R:    nothing()\n\
+<KeyUp>Control_L: nothing()\n\
+<KeyUp>Control_R: nothing()\n\
+<KeyUp>Hyper_L:   nothing()\n\
+<KeyUp>Hyper_R:   nothing()\n\
+<KeyUp>Super_L:   nothing()\n\
+<KeyUp>Super_R:   nothing()\n\
+<KeyUp>Alt_L:     nothing()\n\
+<KeyUp>Alt_R:     nothing()\n\
+<KeyUp>Caps_Lock: nothing()\n\
+<KeyUp>Shift_Lock:nothing()\n\
+<Key>:            key()\n\
+<KeyUp>:          key()\n\
 ";
 
 #define offset(field) XtOffset(XlwMenuWidget, field)
@@ -59,7 +92,7 @@ xlwMenuResources[] =
   {XtNarrowSpacing, XtCMargin, XtRDimension,  sizeof(Dimension),
      offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
 
-  {XmNshadowThickness, XmCShadowThickness, XmRHorizontalDimension,
+  {XmNshadowThickness, XmCShadowThickness, XtRDimension,
      sizeof (Dimension), offset (menu.shadow_thickness),
      XtRImmediate, (XtPointer) 2},
   {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
@@ -95,6 +128,8 @@ static void XlwMenuClassInitialize();
 static void Start();
 static void Drag();
 static void Select();
+static void Key();
+static void Nothing();
 
 static XtActionsRec 
 xlwMenuActionsList [] =
@@ -102,6 +137,8 @@ xlwMenuActionsList [] =
   {"start",            Start},
   {"drag",             Drag},
   {"select",           Select},
+  {"key",              Key},
+  {"nothing",          Nothing},
 };
 
 #define SuperClass ((CoreWidgetClass)&coreClassRec)
@@ -149,9 +186,15 @@ XlwMenuClassRec xlwMenuClassRec =
 
 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
 
+int submenu_destroyed;
+
+static int next_release_must_exit;
+
 \f/* Utilities */
 static void
-push_new_stack (XlwMenuWidget mw, widget_value* val)
+push_new_stack (mw, val)
+     XlwMenuWidget mw;
+     widget_value* val;
 {
   if (!mw->menu.new_stack)
     {
@@ -171,7 +214,8 @@ push_new_stack (XlwMenuWidget mw, widget_value* val)
 }
 
 static void
-pop_new_stack_if_no_contents (XlwMenuWidget mw)
+pop_new_stack_if_no_contents (mw)
+     XlwMenuWidget mw;
 {
   if (mw->menu.new_depth)
     {
@@ -181,7 +225,9 @@ pop_new_stack_if_no_contents (XlwMenuWidget mw)
 }
 
 static void
-make_old_stack_space (XlwMenuWidget mw, int n)
+make_old_stack_space (mw, n)
+     XlwMenuWidget mw;
+     int n;
 {
   if (!mw->menu.old_stack)
     {
@@ -201,15 +247,18 @@ make_old_stack_space (XlwMenuWidget mw, int n)
 
 \f/* Size code */
 static Boolean
-all_dashes_p (char* s)
+all_dashes_p (s)
+     char *s;
 {
   char* p;
   for (p = s; *p == '-'; p++);
   return !*p;
 }
 
-static int
-string_width (XlwMenuWidget mw, char* s)
+int
+string_width (mw, s)
+     XlwMenuWidget mw;
+     char *s;
 {
   XCharStruct xcs;
   int drop;
@@ -219,7 +268,8 @@ string_width (XlwMenuWidget mw, char* s)
 }
 
 static int
-arrow_width (XlwMenuWidget mw)
+arrow_width (mw)
+     XlwMenuWidget mw;
 {
   return mw->menu.font->ascent / 2 | 1;
 }
@@ -232,7 +282,9 @@ nameResource[] =
 };
 
 static char*
-resource_widget_value (XlwMenuWidget mw, widget_value* val)
+resource_widget_value (mw, val)
+     XlwMenuWidget mw;
+     widget_value *val;
 {
   if (!val->toolkit_data)
     {
@@ -245,7 +297,10 @@ resource_widget_value (XlwMenuWidget mw, widget_value* val)
       if (!resourced_name)
        resourced_name = val->name;
       if (!val->value)
-       complete_name = (char *) strdup (resourced_name);
+       {
+         complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
+         strcpy (complete_name, resourced_name);
+       }
       else
        {
          int complete_length =
@@ -265,8 +320,13 @@ resource_widget_value (XlwMenuWidget mw, widget_value* val)
 
 /* Returns the sizes of an item */
 static void
-size_menu_item (XlwMenuWidget mw, widget_value* val, int horizontal_p,
-               int* label_width, int* rest_width, int* height)
+size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
+     XlwMenuWidget mw;
+     widget_value* val;
+     int horizontal_p;
+     int* label_width;
+     int* rest_width;
+     int* height;
 {
   if (all_dashes_p (val->name))
     {
@@ -297,12 +357,14 @@ size_menu_item (XlwMenuWidget mw, widget_value* val, int horizontal_p,
 }
 
 static void
-size_menu (XlwMenuWidget mw, int level)
+size_menu (mw, level)
+     XlwMenuWidget mw;
+     int level;
 {
-  int          label_width = 0;
+  unsigned int  label_width = 0;
   int          rest_width = 0;
   int          max_rest_width = 0;
-  int          height = 0;
+  unsigned int  height = 0;
   int          horizontal_p = mw->menu.horizontal && (level == 0);
   widget_value*        val;
   window_state*        ws;
@@ -347,7 +409,13 @@ size_menu (XlwMenuWidget mw, int level)
 
 \f/* Display code */
 static void
-draw_arrow (XlwMenuWidget mw, Window window, GC gc, int x, int y, int width)
+draw_arrow (mw, window, gc, x, y, width)
+     XlwMenuWidget mw;
+     Window window;
+     GC gc;
+     int x;
+     int y;
+     int width;
 {
   XPoint points [3];
   points [0].x = x;
@@ -362,8 +430,14 @@ draw_arrow (XlwMenuWidget mw, Window window, GC gc, int x, int y, int width)
 }
 
 static void
-draw_shadow_rectangle (XlwMenuWidget mw, Window window,
-                     int x, int y, int width, int height, int erase_p)
+draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
+     XlwMenuWidget mw;
+     Window window;
+     int x;
+     int y;
+     int width;
+     int height;
+     int erase_p;
 {
   Display *dpy = XtDisplay (mw);
   GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
@@ -413,9 +487,14 @@ draw_shadow_rectangle (XlwMenuWidget mw, Window window,
 ** the menu item was. 
 */
 static void
-display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
-                  XPoint* where, Boolean highlighted_p, Boolean horizontal_p,
-                  Boolean just_compute_p)
+display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
+     XlwMenuWidget mw;
+     widget_value* val;
+     window_state* ws;
+     XPoint* where;
+     Boolean highlighted_p;
+     Boolean horizontal_p;
+     Boolean just_compute_p;
 {
   GC deco_gc;
   GC text_gc;
@@ -442,8 +521,11 @@ display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
       width = ws->width - 2 * shadow;
     }
 
+#if 0
   /* see if it should be a button in the menubar */
   button_p = horizontal_p && val->call_data;
+#endif
+  button_p = 0;
 
   /* Only highlight an enabled item that has a callback. */
   if (highlighted_p)
@@ -474,10 +556,19 @@ display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
        }
       else 
        {
+         int x_offset = x + h_spacing + shadow;
          char* display_string = resource_widget_value (mw, val);
          draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
-         XDrawString (XtDisplay (mw), ws->window, text_gc,
-                      x + h_spacing + shadow,
+
+         /* Deal with centering a menu title. */
+         if (!horizontal_p && !val->contents && !val->call_data)
+           {
+             int l = string_width (mw, display_string);
+
+             if (width > l)
+               x_offset = (width - l) >> 1;
+           }
+          XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
                       y + v_spacing + shadow + font_ascent,
                       display_string, strlen (display_string));
          
@@ -487,7 +578,9 @@ display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
                {
                  int a_w = arrow_width (mw);
                  draw_arrow (mw, ws->window, deco_gc,
-                             x + label_width + mw->menu.arrow_spacing,
+                             x + width - arrow_width (mw)
+                             - mw->menu.horizontal_spacing 
+                             - mw->menu.shadow_thickness,
                              y + v_spacing + shadow, a_w);
                }
              else if (val->key)
@@ -506,10 +599,22 @@ display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
                              x + shadow, y + shadow,
                              label_width + h_spacing - 1,
                              font_ascent + font_descent + 2 * v_spacing - 1);
+             draw_shadow_rectangle (mw, ws->window, x, y, width, height,
+                                    False);
 #else
              highlighted_p = True;
 #endif
            }
+         else
+           {
+             XDrawRectangle (XtDisplay (mw), ws->window, 
+                             mw->menu.background_gc,
+                             x + shadow, y + shadow,
+                             label_width + h_spacing - 1,
+                             font_ascent + font_descent + 2 * v_spacing - 1);
+             draw_shadow_rectangle (mw, ws->window, x, y, width, height,
+                                    True);
+           }
 
          if (highlighted_p)
            draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
@@ -521,9 +626,16 @@ display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws,
 }
 
 static void
-display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p,
-             XPoint* highlighted_pos, XPoint* hit, widget_value** hit_return,
-             widget_value* this, widget_value* that)
+display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
+             this, that)
+     XlwMenuWidget mw;
+     int level;
+     Boolean just_compute_p;
+     XPoint* highlighted_pos;
+     XPoint* hit;
+     widget_value** hit_return;
+     widget_value* this;
+     widget_value* that;
 {
   widget_value*        val;
   widget_value* following_item;
@@ -532,6 +644,9 @@ display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p,
   int horizontal_p = mw->menu.horizontal && (level == 0);
   int highlighted_p;
   int just_compute_this_one_p;
+  /* This is set nonzero if the element containing HIGHLIGHTED_POS
+     is disabled, so that we do not return any subsequent element either.  */
+  int no_return = 0;
 
   if (level >= mw->menu.old_depth)
     abort ();
@@ -576,8 +691,14 @@ display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p,
       if (hit
          && !*hit_return
          && (horizontal_p ? hit->x < where.x : hit->y < where.y)
-         && !all_dashes_p (val->name))
-       *hit_return = val;
+         && !all_dashes_p (val->name)
+         && !no_return)
+       {
+         if (val->enabled)
+           *hit_return = val;
+         else
+           no_return = 1;
+       }
 
       if (horizontal_p)
        where.y = 0;
@@ -591,7 +712,10 @@ display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p,
 
 \f/* Motion code */
 static void
-set_new_state (XlwMenuWidget mw, widget_value* val, int level)
+set_new_state (mw, val, level)
+     XlwMenuWidget mw;
+     widget_value* val;
+     int level;
 {
   int i;
   
@@ -602,7 +726,9 @@ set_new_state (XlwMenuWidget mw, widget_value* val, int level)
 }
 
 static void
-make_windows_if_needed (XlwMenuWidget mw, int n)
+make_windows_if_needed (mw, n)
+     XlwMenuWidget mw;
+     int n;
 {
   int i;
   int start_at;
@@ -619,7 +745,7 @@ make_windows_if_needed (XlwMenuWidget mw, int n)
   xswa.background_pixel = mw->core.background_pixel;
   xswa.border_pixel = mw->core.border_pixel;
   xswa.event_mask =
-    ExposureMask | ButtonMotionMask | PointerMotionHintMask
+    ExposureMask | PointerMotionMask | PointerMotionHintMask
       | ButtonReleaseMask | ButtonPressMask;
   xswa.cursor = mw->menu.cursor_shape;
   mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
@@ -656,11 +782,17 @@ make_windows_if_needed (XlwMenuWidget mw, int n)
 
 /* Make the window fit in the screen */
 static void
-fit_to_screen (XlwMenuWidget mw, window_state* ws, window_state* previous_ws,
-              Boolean horizontal_p)
+fit_to_screen (mw, ws, previous_ws, horizontal_p)
+     XlwMenuWidget mw;
+     window_state* ws;
+     window_state* previous_ws;
+     Boolean horizontal_p;
 {
-  int screen_width = WidthOfScreen (XtScreen (mw));
-  int screen_height = HeightOfScreen (XtScreen (mw));
+  unsigned int screen_width = WidthOfScreen (XtScreen (mw));
+  unsigned int screen_height = HeightOfScreen (XtScreen (mw));
+  /* 1 if we are unable to avoid an overlap between
+     this menu and the parent menu in the X dimension.  */
+  int horizontal_overlap = 0;
 
   if (ws->x < 0)
     ws->x = 0;
@@ -670,7 +802,25 @@ fit_to_screen (XlwMenuWidget mw, window_state* ws, window_state* previous_ws,
        ws->x = previous_ws->x - ws->width;
       else
        ws->x = screen_width - ws->width;
+      if (ws->x < 0)
+       {
+         ws->x = 0;
+         horizontal_overlap = 1;
+       }
+    }
+  /* If we overlap in X, try to avoid overlap in Y.  */
+  if (horizontal_overlap
+      && ws->y < previous_ws->y + previous_ws->height
+      && previous_ws->y < ws->y + ws->height)
+    {
+      /* Put this menu right below or right above PREVIOUS_WS
+        if there's room.  */
+      if (previous_ws->y + previous_ws->height + ws->height < screen_height)
+       ws->y = previous_ws->y + previous_ws->height;
+      else if (previous_ws->y - ws->height > 0)
+       ws->y = previous_ws->y - ws->height;
     }
+
   if (ws->y < 0)
     ws->y = 0;
   else if (ws->y + ws->height > screen_height)
@@ -679,12 +829,15 @@ fit_to_screen (XlwMenuWidget mw, window_state* ws, window_state* previous_ws,
        ws->y = previous_ws->y - ws->height;
       else
        ws->y = screen_height - ws->height;
+      if (ws->y < 0) 
+        ws->y = 0;
     }
 }
 
 /* Updates old_stack from new_stack and redisplays. */
 static void
-remap_menubar (XlwMenuWidget mw)
+remap_menubar (mw)
+     XlwMenuWidget mw;
 {
   int i;
   int last_same;
@@ -724,24 +877,24 @@ remap_menubar (XlwMenuWidget mw)
     old_stack [i] = new_stack [i];
   mw->menu.old_depth = new_depth;
 
-  /* refresh the last seletion */
+  /* refresh the last selection */
   selection_position.x = 0;
   selection_position.y = 0;
   display_menu (mw, last_same, new_selection == old_selection,
                &selection_position, NULL, NULL, old_selection, new_selection);
 
-  /* Now popup the new menus */
-  for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
+  /* Now place the new menus.  */
+  for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
     {
-      window_state* previous_ws = &windows [i - 1];
-      window_state* ws = &windows [i];
+      window_state *previous_ws = &windows[i - 1];
+      window_state *ws = &windows[i];
 
-      ws->x =
-       previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
+      ws->x
+       previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
       if (!mw->menu.horizontal || i > 1)
        ws->x += mw->menu.shadow_thickness;
-      ws->y =
-       previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
+      ws->y
+       previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
 
       size_menu (mw, i);
 
@@ -756,26 +909,33 @@ remap_menubar (XlwMenuWidget mw)
 
   /* unmap the menus that popped down */
   for (i = new_depth - 1; i < old_depth; i++)
-    if (i >= new_depth || !new_stack [i]->contents)
-      XUnmapWindow (XtDisplay (mw), windows [i].window);
+    if (i >= new_depth || !new_stack[i]->contents)
+      XUnmapWindow (XtDisplay (mw), windows[i].window);
 }
 
 static Boolean
-motion_event_is_in_menu (XlwMenuWidget mw, XMotionEvent* ev, int level,
-                        XPoint* relative_pos)
+motion_event_is_in_menu (mw, ev, level, relative_pos)
+     XlwMenuWidget mw;
+     XMotionEvent* ev;
+     int level;
+     XPoint* relative_pos;
 {
   window_state* ws = &mw->menu.windows [level];
-  int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
-  int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
+  int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
+  int x = ws->x + shadow;
+  int y = ws->y + shadow;
   relative_pos->x = ev->x_root - x;
   relative_pos->y = ev->y_root - y;
-  return (x < ev->x_root && ev->x_root < x + ws->width
-         && y < ev->y_root && ev->y_root < y + ws->height);
+  return (x - shadow < ev->x_root && ev->x_root < x + ws->width
+         && y - shadow < ev->y_root && ev->y_root < y + ws->height);
 }
 
 static Boolean
-map_event_to_widget_value (XlwMenuWidget mw, XMotionEvent* ev,
-                          widget_value** val, int* level)
+map_event_to_widget_value (mw, ev, val, level)
+     XlwMenuWidget mw;
+     XMotionEvent* ev;
+     widget_value** val;
+     int* level;
 {
   int          i;
   XPoint       relative_pos;
@@ -803,7 +963,8 @@ map_event_to_widget_value (XlwMenuWidget mw, XMotionEvent* ev,
 
 \f/* Procedures */
 static void
-make_drawing_gcs (XlwMenuWidget mw)
+make_drawing_gcs (mw)
+     XlwMenuWidget mw;
 {
   XGCValues xgcv;
 
@@ -848,7 +1009,8 @@ make_drawing_gcs (XlwMenuWidget mw)
 }
 
 static void
-release_drawing_gcs (XlwMenuWidget mw)
+release_drawing_gcs (mw)
+     XlwMenuWidget mw;
 {
   XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
   XtReleaseGC ((Widget) mw, mw->menu.button_gc);
@@ -867,7 +1029,8 @@ release_drawing_gcs (XlwMenuWidget mw)
                   ? ((unsigned long) (x)) : ((unsigned long) (y)))
 
 static void
-make_shadow_gcs (XlwMenuWidget mw)
+make_shadow_gcs (mw)
+     XlwMenuWidget mw;
 {
   XGCValues xgcv;
   unsigned long pm = 0;
@@ -957,32 +1120,50 @@ make_shadow_gcs (XlwMenuWidget mw)
 
 
 static void
-release_shadow_gcs (XlwMenuWidget mw)
+release_shadow_gcs (mw)
+     XlwMenuWidget mw;
 {
   XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
   XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
 }
 
 static void
-XlwMenuInitialize (Widget request, Widget new, ArgList args,
-                  Cardinal *num_args)
+XlwMenuInitialize (request, mw, args, num_args)
+     Widget request;
+     XlwMenuWidget mw;
+     ArgList args;
+     Cardinal *num_args;
 {
   /* Get the GCs and the widget size */
-  XlwMenuWidget mw = (XlwMenuWidget)new;
-  
   XSetWindowAttributes xswa;
   int mask;
   
   Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
   Display* display = XtDisplay (mw);
   
+#if 0
+  widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
+
+  /* _XtCreate is freeing the object that was passed to us,
+     so make a copy that we will actually keep.  */
+  lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
+  mw->menu.contents = tem;
+#endif
+
 /*  mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
   mw->menu.cursor = mw->menu.cursor_shape;
   
-  mw->menu.gray_pixmap = XCreatePixmapFromBitmapData (display, window,
-                                                     gray_bits, gray_width,
-                                                     gray_height, 1, 0, 1);
+  mw->menu.gray_pixmap
+    = XCreatePixmapFromBitmapData (display, window, gray_bits,
+                                  gray_width, gray_height,
+                                  (unsigned long)1, (unsigned long)0, 1);
   
+  /* I don't understand why this ends up 0 sometimes,
+     but it does.  This kludge works around it.
+     Can anyone find a real fix?   -- rms.  */
+  if (mw->menu.font == 0)
+    mw->menu.font = xlwmenu_default_font;
+
   make_drawing_gcs (mw);
   make_shadow_gcs (mw);
   
@@ -1020,7 +1201,10 @@ XlwMenuClassInitialize ()
 }
 
 static void
-XlwMenuRealize (Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
+XlwMenuRealize (w, valueMask, attributes)
+     Widget w;
+     Mask *valueMask;
+     XSetWindowAttributes *attributes;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
   XSetWindowAttributes xswa;
@@ -1045,21 +1229,39 @@ XlwMenuRealize (Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
    receives expose events through Xt.  So we repaint all the other panes
    when receiving an Expose event. */
 static void 
-XlwMenuRedisplay (Widget w, XEvent* ev, Region region)
+XlwMenuRedisplay (w, ev, region)
+     Widget w;
+     XEvent* ev;
+     Region region;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
   int i;
 
+  /* If we have a depth beyond 1, it's because a submenu was displayed.
+     If the submenu has been destroyed, set the depth back to 1.  */
+  if (submenu_destroyed)
+    {
+      mw->menu.old_depth = 1;
+      submenu_destroyed = 0;
+    }
+
   for (i = 0; i < mw->menu.old_depth; i++)
     display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
 }
 
 static void 
-XlwMenuDestroy (Widget w)
+XlwMenuDestroy (w)
+     Widget w;
 {
   int i;
   XlwMenuWidget mw = (XlwMenuWidget) w;
 
+  if (pointer_grabbed)
+    XtUngrabPointer ((Widget)w, CurrentTime);
+  pointer_grabbed = 0;
+
+  submenu_destroyed = 1;
+
   release_drawing_gcs (mw);
   release_shadow_gcs (mw);
 
@@ -1068,6 +1270,12 @@ XlwMenuDestroy (Widget w)
   XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
   mw->menu.gray_pixmap = (Pixmap) -1;
 
+#if 0
+  /* Do free mw->menu.contents because nowadays we copy it
+     during initialization.  */
+  XtFree (mw->menu.contents);
+#endif
+
   /* Don't free mw->menu.contents because that comes from our creator.
      The `*_stack' elements are just pointers into `contents' so leave
      that alone too.  But free the stacks themselves. */
@@ -1093,7 +1301,10 @@ XlwMenuDestroy (Widget w)
 }
 
 static Boolean 
-XlwMenuSetValues (Widget current, Widget request, Widget new)
+XlwMenuSetValues (current, request, new)
+     Widget current;
+     Widget request;
+     Widget new;
 {
   XlwMenuWidget oldmw = (XlwMenuWidget)current;
   XlwMenuWidget newmw = (XlwMenuWidget)new;
@@ -1104,9 +1315,15 @@ XlwMenuSetValues (Widget current, Widget request, Widget new)
       && newmw->menu.contents->contents
       && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
     redisplay = True;
+  /* Do redisplay if the contents are entirely eliminated.  */
+  if (newmw->menu.contents
+      && newmw->menu.contents->contents == 0
+      && newmw->menu.contents->change >= VISIBLE_CHANGE)
+    redisplay = True;
 
   if (newmw->core.background_pixel != oldmw->core.background_pixel
-      || newmw->menu.foreground != oldmw->menu.foreground)
+      || newmw->menu.foreground != oldmw->menu.foreground
+      || newmw->menu.font != oldmw->menu.font)
     {
       release_drawing_gcs (newmw);
       make_drawing_gcs (newmw);
@@ -1127,17 +1344,31 @@ XlwMenuSetValues (Widget current, Widget request, Widget new)
 }
 
 static void 
-XlwMenuResize (Widget w)
+XlwMenuResize (w)
+     Widget w;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
 
-  mw->menu.windows [0].width = mw->core.width;
-  mw->menu.windows [0].height = mw->core.height;
+  if (mw->menu.popped_up)
+    {
+      /* Don't allow the popup menu to resize itself.  */
+      mw->core.width = mw->menu.windows [0].width;
+      mw->core.height = mw->menu.windows [0].height;
+      mw->core.parent->core.width = mw->core.width ;
+      mw->core.parent->core.height = mw->core.height ;
+    }
+  else
+    {
+      mw->menu.windows [0].width = mw->core.width;
+      mw->menu.windows [0].height = mw->core.height;
+    }
 }
 
 \f/* Action procedures */
 static void
-handle_single_motion_event (XlwMenuWidget mw, XMotionEvent* ev)
+handle_single_motion_event (mw, ev)
+     XlwMenuWidget mw;
+     XMotionEvent* ev;
 {
   widget_value*        val;
   int          level;
@@ -1153,7 +1384,9 @@ handle_single_motion_event (XlwMenuWidget mw, XMotionEvent* ev)
 }
 
 static void
-handle_motion_event (XlwMenuWidget mw, XMotionEvent* ev)
+handle_motion_event (mw, ev)
+     XlwMenuWidget mw;
+     XMotionEvent* ev;
 {
   int x = ev->x_root;
   int y = ev->y_root;
@@ -1174,34 +1407,110 @@ handle_motion_event (XlwMenuWidget mw, XMotionEvent* ev)
 }
 
 static void 
-Start (Widget w, XEvent *ev, String *params, Cardinal *num_params)
+Start (w, ev, params, num_params)
+     Widget w;
+     XEvent *ev;
+     String *params;
+     Cardinal *num_params;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
 
-  XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
-  
-  /* notes the absolute position of the menubar window */
-  mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
-  mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
+  if (!mw->menu.popped_up)
+    {
+      menu_post_event = *ev;
+      pop_up_menu (mw, ev);
+    }
+  else
+    {
+      /* If we push a button while the menu is posted semipermanently,
+        releasing the button should always pop the menu down.  */
+      next_release_must_exit = 1;
 
-  /* handles the down like a move, slots are compatible */
-  handle_motion_event (mw, &ev->xmotion);
+      /* notes the absolute position of the menubar window */
+      mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
+      mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
+
+      /* handles the down like a move, slots are compatible */
+      handle_motion_event (mw, &ev->xmotion);
+    }
 }
 
 static void 
-Drag (Widget w, XEvent *ev, String *params, Cardinal *num_params)
+Drag (w, ev, params, num_params)
+     Widget w;
+     XEvent *ev;
+     String *params;
+     Cardinal *num_params;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
-  handle_motion_event (mw, &ev->xmotion);
+  if (mw->menu.popped_up)
+    handle_motion_event (mw, &ev->xmotion);
 }
 
-static void 
-Select (Widget w, XEvent *ev, String *params, Cardinal *num_params)
+/* Do nothing.
+   This is how we handle presses and releases of modifier keys.  */
+static void
+Nothing (w, ev, params, num_params)
+     Widget w;
+     XEvent *ev;
+     String *params;
+     Cardinal *num_params;
+{
+}
+
+/* Handle key press and release events while menu is popped up.
+   Our action is to get rid of the menu.  */
+static void
+Key (w, ev, params, num_params)
+     Widget w;
+     XEvent *ev;
+     String *params;
+     Cardinal *num_params;
+{
+  XlwMenuWidget mw = (XlwMenuWidget)w;
+
+  /* Pop down everything.  */
+  mw->menu.new_depth = 1;
+  remap_menubar (mw);
+
+  if (mw->menu.popped_up)
+    {
+      mw->menu.popped_up = False;
+      XtUngrabPointer ((Widget)mw, ev->xmotion.time);
+      if (XtIsShell (XtParent ((Widget) mw)))
+       XtPopdown (XtParent ((Widget) mw));
+      else
+       {
+         XtRemoveGrab ((Widget) mw);
+         display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
+       }
+    }
+
+  /* callback */
+  XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
+}
+
+static void
+Select (w, ev, params, num_params)
+     Widget w;
+     XEvent *ev;
+     String *params;
+     Cardinal *num_params;
 {
   XlwMenuWidget mw = (XlwMenuWidget)w;
   widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
   
-  /* pop down everything */
+  /* If user releases the button quickly, without selecting anything,
+     after the initial down-click that brought the menu up,
+     do nothing.  */
+  if ((selected_item == 0
+       || ((widget_value *) selected_item)->call_data == 0)
+      && !next_release_must_exit
+      && (ev->xbutton.time - menu_post_event.xbutton.time
+         < XtGetMultiClickTime (XtDisplay (w))))
+    return;
+
+  /* pop down everything.  */
   mw->menu.new_depth = 1;
   remap_menubar (mw);
 
@@ -1209,18 +1518,25 @@ Select (Widget w, XEvent *ev, String *params, Cardinal *num_params)
     {
       mw->menu.popped_up = False;
       XtUngrabPointer ((Widget)mw, ev->xmotion.time);
-      XtPopdown (XtParent (mw));
+      if (XtIsShell (XtParent ((Widget) mw)))
+       XtPopdown (XtParent ((Widget) mw));
+      else
+       {
+         XtRemoveGrab ((Widget) mw);
+         display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
+       }
     }
 
   /* callback */
   XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
-  
 }
 
 
 \f/* Special code to pop-up a menu */
 void
-pop_up_menu (XlwMenuWidget mw, XButtonPressedEvent* event)
+pop_up_menu (mw, event)
+     XlwMenuWidget mw;
+     XButtonPressedEvent* event;
 {
   int          x = event->x_root;
   int          y = event->y_root;
@@ -1228,10 +1544,15 @@ pop_up_menu (XlwMenuWidget mw, XButtonPressedEvent* event)
   int          h;
   int          borderwidth = mw->menu.shadow_thickness;
   Screen*      screen = XtScreen (mw);
+  Display       *display = XtDisplay (mw);
+  int count;
+
+  next_release_must_exit = 0;
 
   XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
 
-  size_menu (mw, 0);
+  if (XtIsShell (XtParent ((Widget)mw)))
+    size_menu (mw, 0);
 
   w = mw->menu.windows [0].width;
   h = mw->menu.windows [0].height;
@@ -1248,18 +1569,46 @@ pop_up_menu (XlwMenuWidget mw, XButtonPressedEvent* event)
     y = HeightOfScreen (screen) - h - 2 * borderwidth;
 
   mw->menu.popped_up = True;
-  XtConfigureWidget (XtParent (mw), x, y, w, h,
-                    XtParent (mw)->core.border_width);
-  XtPopup (XtParent (mw), XtGrabExclusive);
-  display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
+  if (XtIsShell (XtParent ((Widget)mw)))
+    {
+      XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
+                        XtParent ((Widget)mw)->core.border_width);
+      XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
+      display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
+      mw->menu.windows [0].x = x + borderwidth;
+      mw->menu.windows [0].y = y + borderwidth;
+    }
+  else
+    {
+      XEvent *ev = (XEvent *) event;
+
+      XtAddGrab ((Widget) mw, True, True);
+
+      /* notes the absolute position of the menubar window */
+      mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
+      mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
+    }
+
+#ifdef emacs
+  count = x_catch_errors (display);
+#endif
   XtGrabPointer ((Widget)mw, False,
-                (ButtonMotionMask | PointerMotionHintMask | ButtonReleaseMask
+                (PointerMotionMask
+                 | PointerMotionHintMask
+                 | ButtonReleaseMask
                  | ButtonPressMask),
-                GrabModeAsync, GrabModeAsync, None, mw->menu.cursor_shape,
+                GrabModeAsync, GrabModeAsync, None,
+                mw->menu.cursor_shape,
                 event->time);
-
-  mw->menu.windows [0].x = x + borderwidth;
-  mw->menu.windows [0].y = y + borderwidth;
+  pointer_grabbed = 1;
+#ifdef emacs
+  if (x_had_errors_p (display))
+    {
+      pointer_grabbed = 0;
+      XtUngrabPointer ((Widget)mw, event->time);
+    }
+  x_uncatch_errors (display, count);
+#endif
 
   handle_motion_event (mw, (XMotionEvent*)event);
 }