* lisp/emacs-lisp/byte-run.el (defun): Set name after definition.
[bpt/emacs.git] / lwlib / xlwmenu.c
index 065d81e..6f4db56 100644 (file)
@@ -1,7 +1,7 @@
 /* Implements a lightweight menubar widget.
 
 Copyright (C) 1992 Lucid, Inc.
-Copyright (C) 1994-1995, 1997, 1999-201 Free Software Foundation, Inc.
+Copyright (C) 1994-1995, 1997, 1999-2014 Free Software Foundation, Inc.
 
 This file is part of the Lucid Widget Library.
 
@@ -16,21 +16,16 @@ 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, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Created by devin@lucid.com */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
 
 #include <setjmp.h>
 #include <lisp.h>
 
 #include <stdio.h>
-#include <ctype.h>
 
 #include <sys/types.h>
 #if (defined __sun) && !(defined SUNOS41)
@@ -49,30 +44,18 @@ Boston, MA 02110-1301, USA.  */
 
 #ifdef emacs
 
-/* Defined in xfns.c.  When config.h defines `static' as empty, we get
-   redefinition errors when gray_bitmap is included more than once, so
-   we're referring to the one include in xfns.c here.  */
-
-extern int gray_bitmap_width;
-extern int gray_bitmap_height;
-extern char *gray_bitmap_bits;
-
 #include <xterm.h>
+#include "bitmaps/gray.xbm"
 
 #else /* not emacs */
 
 #include <X11/bitmaps/gray>
-#define gray_bitmap_width      gray_width
-#define gray_bitmap_height     gray_height
-#define gray_bitmap_bits       gray_bits
 
 #endif /* not emacs */
 
 static int pointer_grabbed;
 static XEvent menu_post_event;
 
-static XFontStruct *xlwmenu_default_font;
-
 static char
 xlwMenuTranslations [] =
 "<BtnDown>:      start()\n\
@@ -115,7 +98,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 +114,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),
@@ -204,7 +185,6 @@ 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 [] =
@@ -287,7 +267,7 @@ ungrab_all (Widget w, Time ungrabtime)
 
 /* Like abort, but remove grabs from widget W before.  */
 
-static void
+static _Noreturn void
 abort_gracefully (Widget w)
 {
   if (XtIsShell (XtParent (w)))
@@ -1224,9 +1204,9 @@ display_menu (XlwMenuWidget mw,
        {
          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,
@@ -1352,6 +1332,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.  */
@@ -1429,7 +1410,7 @@ fit_to_screen (XlwMenuWidget mw,
 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;
@@ -1489,7 +1470,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);
@@ -1534,10 +1515,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);
     }
 
@@ -1593,9 +1576,9 @@ map_event_to_widget_value (XlwMenuWidget mw,
        }
     }
 
-  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;
@@ -1613,14 +1596,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;
@@ -1691,8 +1677,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)
@@ -1847,37 +1835,40 @@ 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)
     {
       int screen = XScreenNumberOfScreen (mw->core.screen);
       int len = strlen (fname), i = len-1;
       /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9.  */
-      while (i > 0 && isdigit (fname[i]))
+      while (i > 0 && '0' <= fname[i] && fname[i] <= '9')
         --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) 
-        {
-          fprintf (stderr, "Can't find font '%s'\n", fname);
-          mw->menu.xft_font = XftFontOpenName (XtDisplay (mw), screen,
-                                               DEFAULT_FACENAME);
-        }
+      if (!mw->menu.xft_font)
+       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;
 }
@@ -1891,21 +1882,12 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
   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.  */
-  memcpy (tem, mw->menu.contents, 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_bitmap_bits,
-                                  gray_bitmap_width, gray_bitmap_height,
+    = XCreatePixmapFromBitmapData (display, window, gray_bits,
+                                  gray_width, gray_height,
                                   (unsigned long)1, (unsigned long)0, 1);
 
 #ifdef HAVE_XFT
@@ -1913,16 +1895,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 ();
+            }
         }
     }
 
@@ -1930,7 +1912,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);
 
@@ -1966,7 +1948,6 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args)
 static void
 XlwMenuClassInitialize (void)
 {
-  xlwmenu_default_font = 0;
 }
 
 static void
@@ -2077,19 +2058,15 @@ 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. */
   if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
   if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
 
-  /* Remember, you can't free anything that came from the resource
+  /* Original comment was:
+
+     Remember, you can't free anything that came from the resource
      database.  This includes:
          mw->menu.cursor
          mw->menu.top_shadow_pixmap
@@ -2098,7 +2075,11 @@ XlwMenuDestroy (Widget w)
      Also the color cells of top_shadow_color, bottom_shadow_color,
      foreground, and button_foreground will never be freed until this
      client exits.  Nice, eh?
-   */
+
+     But now I can free font without any visible glitches.  */
+
+  if (mw->menu.font)
+    XFreeFont (XtDisplay (mw), mw->menu.font);
 
 #ifdef HAVE_XFT
   if (mw->menu.windows [0].xft_draw)
@@ -2107,12 +2088,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)
@@ -2126,13 +2107,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
 
@@ -2142,23 +2123,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
@@ -2168,6 +2148,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new,
 #endif
       )
     {
+      int i;
       release_drawing_gcs (newmw);
       make_drawing_gcs (newmw);
 
@@ -2177,7 +2158,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.  */
@@ -2193,7 +2174,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);
@@ -2218,12 +2199,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