Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lwlib / xlwmenu.c
index 242719a..0a328f0 100644 (file)
@@ -1,7 +1,7 @@
 /* Implements a lightweight menubar widget.
+
 Copyright (C) 1992 Lucid, Inc.
-Copyright (C) 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
-  2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
+Copyright (C) 1994-1995, 1997, 1999-2012  Free Software Foundation, Inc.
 
 This file is part of the Lucid Widget Library.
 
@@ -27,7 +27,7 @@ Boston, MA 02110-1301, USA.  */
 #endif
 
 #include <setjmp.h>
-#include "lisp.h"
+#include <lisp.h>
 
 #include <stdio.h>
 #include <ctype.h>
@@ -57,7 +57,7 @@ extern int gray_bitmap_width;
 extern int gray_bitmap_height;
 extern char *gray_bitmap_bits;
 
-#include "xterm.h"
+#include <xterm.h>
 
 #else /* not emacs */
 
@@ -71,8 +71,6 @@ extern char *gray_bitmap_bits;
 static int pointer_grabbed;
 static XEvent menu_post_event;
 
-static XFontStruct *xlwmenu_default_font;
-
 static char
 xlwMenuTranslations [] =
 "<BtnDown>:      start()\n\
@@ -115,7 +113,7 @@ xlwMenuTranslations [] =
 <KeyUp>:          key()\n\
 ";
 
-/* FIXME: Space should toggle toggleable menu item but not remove the menu
+/* FIXME: Space should toggle togglable menu item but not remove the menu
    so you can toggle the next one without entering the menu again.  */
 
 /* FIXME: Should ESC close one level of menu structure or the complete menu?  */
@@ -131,14 +129,12 @@ xlwMenuResources[] =
      offset(menu.fontSet), XtRFontSet, NULL},
 #endif
 #ifdef HAVE_XFT
-#define DEFAULT_FACENAME "Sans-10"
-  {XtNfaceName,  XtCFaceName, XtRString, sizeof(String),
-   offset(menu.faceName), XtRString, DEFAULT_FACENAME},
-  {XtNdefaultFace,  XtCDefaultFace, XtRInt, sizeof(int),
-   offset(menu.default_face), XtRImmediate, (XtPointer)1},
+#define DEFAULT_FONTNAME "Sans-10"
+#else
+#define DEFAULT_FONTNAME "XtDefaultFont"
 #endif
-  {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
-     offset(menu.font), XtRString, "XtDefaultFont"},
+  {XtNfont,  XtCFont, XtRString, sizeof(String),
+     offset(menu.fontName), XtRString, DEFAULT_FONTNAME },
   {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
      offset(menu.foreground), XtRString, "XtDefaultForeground"},
   {XtNdisabledForeground, XtCDisabledForeground, XtRPixel, sizeof(Pixel),
@@ -187,9 +183,9 @@ xlwMenuResources[] =
 
 static Boolean XlwMenuSetValues(Widget current, Widget request, Widget new,
                                 ArgList args, Cardinal *num_args);
-static void XlwMenuRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes);
+static void XlwMenuRealize(Widget, Mask *, XSetWindowAttributes *);
 static void XlwMenuResize(Widget w);
-static void XlwMenuInitialize(Widget request, Widget mw, ArgList args, Cardinal *num_args);
+static void XlwMenuInitialize(Widget, Widget, ArgList, Cardinal *);
 static void XlwMenuRedisplay(Widget w, XEvent *ev, Region region);
 static void XlwMenuDestroy(Widget w);
 static void XlwMenuClassInitialize(void);
@@ -204,7 +200,7 @@ static void Key(Widget w, XEvent *ev, String *params, Cardinal *num_params);
 static void Nothing(Widget w, XEvent *ev, String *params, Cardinal *num_params);
 static int separator_height (enum menu_separator);
 static void pop_up_menu (XlwMenuWidget, XButtonPressedEvent *);
-
+static void abort_gracefully (Widget w) NO_RETURN;
 
 static XtActionsRec
 xlwMenuActionsList [] =
@@ -473,15 +469,13 @@ resource_widget_value (XlwMenuWidget mw, widget_value *val)
 
 /* Returns the sizes of an item */
 static void
-size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
-               height)
-     XlwMenuWidget mw;
-     widget_value* val;
-     int horizontal_p;
-     int* label_width;
-     int* rest_width;
-     int* button_width;
-     int* height;
+size_menu_item (XlwMenuWidget mw,
+                widget_value* val,
+                int horizontal_p,
+                int* label_width,
+                int* rest_width,
+                int* button_width,
+                int* height)
 {
   enum menu_separator separator;
 
@@ -525,12 +519,12 @@ size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
 static void
 size_menu (XlwMenuWidget mw, int level)
 {
-  unsigned int  label_width = 0;
+  int           label_width = 0;
   int          rest_width = 0;
   int          button_width = 0;
   int          max_rest_width = 0;
   int          max_button_width = 0;
-  unsigned int  height = 0;
+  int           height = 0;
   int          horizontal_p = mw->menu.horizontal && (level == 0);
   widget_value*        val;
   window_state*        ws;
@@ -589,7 +583,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, int down_p)
+draw_arrow (XlwMenuWidget mw,
+            Window window,
+            GC gc,
+            int x,
+            int y,
+            int width,
+            int down_p)
 {
   Display *dpy = XtDisplay (mw);
   GC top_gc = mw->menu.shadow_top_gc;
@@ -646,7 +646,14 @@ draw_arrow (XlwMenuWidget mw, Window window, GC gc, int x, int y, int width, int
 
 
 static void
-draw_shadow_rectangle (XlwMenuWidget mw, Window window, int x, int y, int width, int height, int erase_p, int down_p)
+draw_shadow_rectangle (XlwMenuWidget mw,
+                       Window window,
+                       int x,
+                       int y,
+                       int width,
+                       int height,
+                       int erase_p,
+                       int down_p)
 {
   Display *dpy = XtDisplay (mw);
   GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
@@ -702,7 +709,14 @@ draw_shadow_rectangle (XlwMenuWidget mw, Window window, int x, int y, int width,
 
 
 static void
-draw_shadow_rhombus (XlwMenuWidget mw, Window window, int x, int y, int width, int height, int erase_p, int down_p)
+draw_shadow_rhombus (XlwMenuWidget mw,
+                     Window window,
+                     int x,
+                     int y,
+                     int width,
+                     int height,
+                     int erase_p,
+                     int down_p)
 {
   Display *dpy = XtDisplay (mw);
   GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
@@ -796,7 +810,12 @@ draw_radio (XlwMenuWidget mw, Window window, int x, int y, int selected_p)
    separator to draw.  TYPE is the separator type.  */
 
 static void
-draw_separator (XlwMenuWidget mw, Window window, int x, int y, int width, enum menu_separator type)
+draw_separator (XlwMenuWidget mw,
+                Window window,
+                int x,
+                int y,
+                int width,
+                enum menu_separator type)
 {
   Display *dpy = XtDisplay (mw);
   XGCValues xgcv;
@@ -946,15 +965,13 @@ separator_height (enum menu_separator separator)
    the menu item was.  */
 
 static void
-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;
+display_menu_item (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;
@@ -1137,7 +1154,12 @@ display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p,
 }
 
 static void
-display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p, XPoint *highlighted_pos, XPoint *hit, widget_value **hit_return)
+display_menu (XlwMenuWidget mw,
+              int level,
+              Boolean just_compute_p,
+              XPoint *highlighted_pos,
+              XPoint *hit,
+              widget_value **hit_return)
 {
   widget_value*        val;
   widget_value* following_item;
@@ -1198,9 +1220,9 @@ display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p, XPoint *highl
        {
          if (val->enabled)
            *hit_return = val;
-         else 
+         else
             no_return = 1;
-          if (mw->menu.inside_entry != val) 
+          if (mw->menu.inside_entry != val)
             {
               if (mw->menu.inside_entry)
                 XtCallCallbackList ((Widget)mw, mw->menu.leave,
@@ -1326,6 +1348,7 @@ make_windows_if_needed (XlwMenuWidget mw, int n)
 #endif
      set_window_type (windows [i].w, mw);
    }
+  XFlush (XtDisplay (mw));
 }
 
 /* Value is non-zero if WINDOW is part of menu bar widget W.  */
@@ -1345,7 +1368,10 @@ xlwmenu_window_p (Widget w, Window window)
 
 /* 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 (XlwMenuWidget mw,
+               window_state *ws,
+               window_state *previous_ws,
+               Boolean horizontal_p)
 {
   unsigned int screen_width = WidthOfScreen (XtScreen (mw));
   unsigned int screen_height = HeightOfScreen (XtScreen (mw));
@@ -1400,7 +1426,7 @@ fit_to_screen (XlwMenuWidget mw, window_state *ws, window_state *previous_ws, Bo
 static void
 create_pixmap_for_menu (window_state* ws, XlwMenuWidget mw)
 {
-  if (ws->pixmap != None) 
+  if (ws->pixmap != None)
     {
       XFreePixmap (XtDisplay (ws->w), ws->pixmap);
       ws->pixmap = None;
@@ -1460,7 +1486,7 @@ remap_menubar (XlwMenuWidget mw)
   if (new_selection && !new_selection->enabled)
     new_selection = NULL;
 
-  /* Call callback when the hightlighted item changes.  */
+  /* Call callback when the highlighted item changes.  */
   if (old_selection || new_selection)
     XtCallCallbackList ((Widget)mw, mw->menu.highlight,
                        (XtPointer) new_selection);
@@ -1505,10 +1531,12 @@ remap_menubar (XlwMenuWidget mw)
 
       fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
 
-      XtVaSetValues (ws->w, XtNwidth, ws->width, XtNheight, ws->height,
-                     XtNx, ws->x, XtNy, ws->y, NULL);
       create_pixmap_for_menu (ws, mw);
+      XtMoveWidget (ws->w, ws->x, ws->y);
       XtPopup (ws->w, XtGrabNone);
+      XtResizeWidget (ws->w, ws->width, ws->height,
+                      mw->core.border_width);
+      XtResizeWindow (ws->w);
       display_menu (mw, i, False, &selection_position, NULL, NULL);
     }
 
@@ -1519,7 +1547,10 @@ remap_menubar (XlwMenuWidget mw)
 }
 
 static Boolean
-motion_event_is_in_menu (XlwMenuWidget mw, XMotionEvent *ev, int level, XPoint *relative_pos)
+motion_event_is_in_menu (XlwMenuWidget mw,
+                         XMotionEvent *ev,
+                         int level,
+                         XPoint *relative_pos)
 {
   window_state* ws = &mw->menu.windows [level];
   int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
@@ -1532,7 +1563,10 @@ motion_event_is_in_menu (XlwMenuWidget mw, XMotionEvent *ev, int level, XPoint *
 }
 
 static Boolean
-map_event_to_widget_value (XlwMenuWidget mw, XMotionEvent *ev, widget_value **val, int *level)
+map_event_to_widget_value (XlwMenuWidget mw,
+                           XMotionEvent *ev,
+                           widget_value **val,
+                           int *level)
 {
   int          i;
   XPoint       relative_pos;
@@ -1558,9 +1592,9 @@ map_event_to_widget_value (XlwMenuWidget mw, XMotionEvent *ev, widget_value **va
        }
     }
 
-  if (!inside) 
+  if (!inside)
     {
-      if (mw->menu.inside_entry != NULL) 
+      if (mw->menu.inside_entry != NULL)
         XtCallCallbackList ((Widget)mw, mw->menu.leave,
                             (XtPointer) mw->menu.inside_entry);
       mw->menu.inside_entry = NULL;
@@ -1578,14 +1612,17 @@ make_drawing_gcs (XlwMenuWidget mw)
   XtGCMask mask = GCForeground | GCBackground;
 
 #ifdef HAVE_X_I18N
-  if (!mw->menu.fontSet)
+  if (!mw->menu.fontSet && mw->menu.font)
     {
       xgcv.font = mw->menu.font->fid;
       mask |= GCFont;
     }
 #else
-  xgcv.font = mw->menu.font->fid;
-  mask |= GCFont;
+  if (mw->menu.font)
+    {
+      xgcv.font = mw->menu.font->fid;
+      mask |= GCFont;
+    }
 #endif
   xgcv.foreground = mw->menu.foreground;
   xgcv.background = mw->core.background_pixel;
@@ -1656,8 +1693,10 @@ release_drawing_gcs (XlwMenuWidget mw)
   mw->menu.background_gc = (GC) -1;
 }
 
+#ifndef emacs
 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
                   ? ((unsigned long) (x)) : ((unsigned long) (y)))
+#endif
 
 static void
 make_shadow_gcs (XlwMenuWidget mw)
@@ -1812,13 +1851,20 @@ release_shadow_gcs (XlwMenuWidget mw)
 }
 
 #ifdef HAVE_XFT
+static XftFont *
+getDefaultXftFont (XlwMenuWidget mw)
+{
+  int screen = XScreenNumberOfScreen (mw->core.screen);
+  return XftFontOpenName (XtDisplay (mw), screen, DEFAULT_FONTNAME);
+}
+
 static int
 openXftFont (XlwMenuWidget mw)
 {
-  char *fname = mw->menu.faceName;
+  char *fname = mw->menu.fontName;
 
   mw->menu.xft_font = 0;
-  mw->menu.default_face = fname && strcmp (fname, DEFAULT_FACENAME) == 0;
+  mw->menu.default_face = fname && strcmp (fname, DEFAULT_FONTNAME) == 0;
 
   if (fname && strcmp (fname, "none") != 0)
     {
@@ -1829,20 +1875,23 @@ openXftFont (XlwMenuWidget mw)
         --i;
       if (fname[i] == ' ')
         {
-          fname = xstrdup (mw->menu.faceName);
+          fname = xstrdup (mw->menu.fontName);
           fname[i] = '-';
         }
 
-      mw->menu.xft_font = XftFontOpenName (XtDisplay (mw), screen, fname);
-      if (!mw->menu.xft_font) 
+      mw->menu.font = XLoadQueryFont (XtDisplay (mw), fname);
+      if (!mw->menu.font)
         {
-          fprintf (stderr, "Can't find font '%s'\n", fname);
-          mw->menu.xft_font = XftFontOpenName (XtDisplay (mw), screen,
-                                               DEFAULT_FACENAME);
+          mw->menu.xft_font = XftFontOpenName (XtDisplay (mw), screen, fname);
+          if (!mw->menu.xft_font)
+            {
+              fprintf (stderr, "Can't find font '%s'\n", fname);
+              mw->menu.xft_font = getDefaultXftFont (mw);
+            }
         }
     }
 
-  if (fname != mw->menu.faceName) free (fname);
+  if (fname != mw->menu.fontName) xfree (fname);
 
   return mw->menu.xft_font != 0;
 }
@@ -1861,7 +1910,7 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
 
   /* _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));
+  memcpy (tem, mw->menu.contents, sizeof (widget_value));
   mw->menu.contents = tem;
 #endif
 
@@ -1878,16 +1927,16 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
     ;
   else
 #endif
-  
-  if (!mw->menu.font)
     {
-      if (!xlwmenu_default_font)
-        xlwmenu_default_font = XLoadQueryFont (display, "fixed");
-      mw->menu.font = xlwmenu_default_font;
-      if (!mw->menu.font) 
+      mw->menu.font = XLoadQueryFont (display, mw->menu.fontName);
+      if (!mw->menu.font)
         {
-          fprintf (stderr, "Menu font fixed not found, can't continue.\n");
-          abort ();
+          mw->menu.font = XLoadQueryFont (display, "fixed");
+          if (!mw->menu.font)
+            {
+              fprintf (stderr, "Menu font fixed not found, can't continue.\n");
+              abort ();
+            }
         }
     }
 
@@ -1895,7 +1944,7 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
   if (mw->menu.fontSet)
     mw->menu.font_extents = XExtentsOfFontSet (mw->menu.fontSet);
 #endif
-      
+
   make_drawing_gcs (mw);
   make_shadow_gcs (mw);
 
@@ -1931,7 +1980,6 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
 static void
 XlwMenuClassInitialize (void)
 {
-  xlwmenu_default_font = 0;
 }
 
 static void
@@ -2072,12 +2120,12 @@ XlwMenuDestroy (Widget w)
     XftFontClose (XtDisplay (mw), mw->menu.xft_font);
 #endif
 
-  if (mw->menu.windows [0].pixmap != None) 
+  if (mw->menu.windows [0].pixmap != None)
     XFreePixmap (XtDisplay (mw), mw->menu.windows [0].pixmap);
   /* start from 1 because the one in slot 0 is w->core.window */
   for (i = 1; i < mw->menu.windows_length; i++)
     {
-      if (mw->menu.windows [i].pixmap != None) 
+      if (mw->menu.windows [i].pixmap != None)
         XFreePixmap (XtDisplay (mw), mw->menu.windows [i].pixmap);
 #ifdef HAVE_XFT
       if (mw->menu.windows [i].xft_draw)
@@ -2091,13 +2139,13 @@ XlwMenuDestroy (Widget w)
 
 #ifdef HAVE_XFT
 static int
-facename_changed (XlwMenuWidget newmw,
+fontname_changed (XlwMenuWidget newmw,
                   XlwMenuWidget oldmw)
 {
-  /* This will fore a new XftFont even if the same string is set.
+  /* This will force a new XftFont even if the same string is set.
      This is good, as rendering parameters may have changed and
      we just want to do a redisplay.  */
-  return newmw->menu.faceName != oldmw->menu.faceName;
+  return newmw->menu.fontName != oldmw->menu.fontName;
 }
 #endif
 
@@ -2107,23 +2155,22 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
 {
   XlwMenuWidget oldmw = (XlwMenuWidget)current;
   XlwMenuWidget newmw = (XlwMenuWidget)new;
-  Boolean redisplay = False;
-  int i;
+  Boolean do_redisplay = False;
 
   if (newmw->menu.contents
       && newmw->menu.contents->contents
       && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
-    redisplay = True;
+    do_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;
+    do_redisplay = True;
 
   if (newmw->core.background_pixel != oldmw->core.background_pixel
       || newmw->menu.foreground != oldmw->menu.foreground
 #ifdef HAVE_XFT
-      || facename_changed (newmw, oldmw)
+      || fontname_changed (newmw, oldmw)
 #endif
 #ifdef HAVE_X_I18N
       || newmw->menu.fontSet != oldmw->menu.fontSet
@@ -2133,6 +2180,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
 #endif
       )
     {
+      int i;
       release_drawing_gcs (newmw);
       make_drawing_gcs (newmw);
 
@@ -2142,7 +2190,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
       newmw->menu.bottom_shadow_color = -1;
       make_shadow_gcs (newmw);
 
-      redisplay = True;
+      do_redisplay = True;
 
       if (XtIsRealized (current))
        /* If the menu is currently displayed, change the display.  */
@@ -2158,7 +2206,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
     }
 
 #ifdef HAVE_XFT
-  if (facename_changed (newmw, oldmw))
+  if (fontname_changed (newmw, oldmw))
     {
       int i;
       int screen = XScreenNumberOfScreen (newmw->core.screen);
@@ -2183,12 +2231,12 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
 #ifdef HAVE_X_I18N
   if (newmw->menu.fontSet != oldmw->menu.fontSet && newmw->menu.fontSet != NULL)
     {
-      redisplay = True;
+      do_redisplay = True;
       newmw->menu.font_extents = XExtentsOfFontSet (newmw->menu.fontSet);
     }
 #endif
 
-  return redisplay;
+  return do_redisplay;
 }
 
 static void
@@ -2655,6 +2703,3 @@ pop_up_menu (XlwMenuWidget mw, XButtonPressedEvent *event)
   ((XMotionEvent*)event)->is_hint = 0;
   handle_motion_event (mw, (XMotionEvent*)event);
 }
-
-/* arch-tag: 657f43dd-dfd0-4cc9-910c-52935f01176e
-   (do not change this comment) */