(Fchars_in_region): Coerce markers.
[bpt/emacs.git] / lwlib / xlwmenu.c
index 4b5e589..f6805e8 100644 (file)
@@ -14,8 +14,9 @@ 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
-Alongalong 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 */
 
@@ -33,11 +34,43 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 static int pointer_grabbed;
 static XEvent menu_post_event;
 
+XFontStruct *xlwmenu_default_font;
+
 static char 
 xlwMenuTranslations [] = 
-"<BtnDown>:    start()\n\
-<Motion>:      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)
@@ -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)
@@ -324,10 +361,10 @@ 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;
@@ -484,8 +521,11 @@ display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute
       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)
@@ -604,6 +644,9 @@ display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
   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 ();
@@ -648,8 +691,14 @@ display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
       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;
@@ -739,8 +788,11 @@ fit_to_screen (mw, ws, previous_ws, horizontal_p)
      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;
@@ -750,7 +802,25 @@ fit_to_screen (mw, ws, previous_ws, horizontal_p)
        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)
@@ -759,6 +829,8 @@ fit_to_screen (mw, ws, previous_ws, horizontal_p)
        ws->y = previous_ws->y - ws->height;
       else
        ws->y = screen_height - ws->height;
+      if (ws->y < 0) 
+        ws->y = 0;
     }
 }
 
@@ -805,24 +877,24 @@ remap_menubar (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);
 
@@ -837,8 +909,8 @@ remap_menubar (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
@@ -849,12 +921,13 @@ motion_event_is_in_menu (mw, ev, level, relative_pos)
      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
@@ -1055,15 +1128,13 @@ release_shadow_gcs (mw)
 }
 
 static void
-XlwMenuInitialize (request, new, args, num_args)
+XlwMenuInitialize (request, mw, args, num_args)
      Widget request;
-     Widget new;
+     XlwMenuWidget mw;
      ArgList args;
      Cardinal *num_args;
 {
   /* Get the GCs and the widget size */
-  XlwMenuWidget mw = (XlwMenuWidget)new;
-  
   XSetWindowAttributes xswa;
   int mask;
   
@@ -1082,10 +1153,17 @@ XlwMenuInitialize (request, new, args, num_args)
 /*  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);
   
@@ -1237,6 +1315,11 @@ XlwMenuSetValues (current, request, 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
@@ -1360,10 +1443,54 @@ Drag (w, ev, params, num_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 
+/* 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;
@@ -1417,6 +1544,8 @@ pop_up_menu (mw, event)
   int          h;
   int          borderwidth = mw->menu.shadow_thickness;
   Screen*      screen = XtScreen (mw);
+  Display       *display = XtDisplay (mw);
+  int count;
 
   next_release_must_exit = 0;
 
@@ -1461,7 +1590,7 @@ pop_up_menu (mw, event)
     }
 
 #ifdef emacs
-  x_catch_errors ();
+  count = x_catch_errors (display);
 #endif
   XtGrabPointer ((Widget)mw, False,
                 (PointerMotionMask
@@ -1473,12 +1602,12 @@ pop_up_menu (mw, event)
                 event->time);
   pointer_grabbed = 1;
 #ifdef emacs
-  if (x_had_errors_p ())
+  if (x_had_errors_p (display))
     {
       pointer_grabbed = 0;
       XtUngrabPointer ((Widget)mw, event->time);
     }
-  x_uncatch_errors ();
+  x_uncatch_errors (display, count);
 #endif
 
   handle_motion_event (mw, (XMotionEvent*)event);