Merge from emacs-23
[bpt/emacs.git] / src / widget.c
index 6659640..d4fb341 100644 (file)
@@ -1,13 +1,13 @@
 /* The emacs frame widget.
    Copyright (C) 1992, 1993, 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
+                 2005, 2006, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,9 +15,7 @@ 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/>.  */
 
 /* Emacs 19 face widget ported by Fred Pierresteguy */
 
@@ -33,6 +31,7 @@ Boston, MA 02110-1301, USA.  */
 
 #include <config.h>
 #include <stdio.h>
+#include <setjmp.h>
 #include "lisp.h"
 #include "xterm.h"
 
@@ -55,6 +54,9 @@ Boston, MA 02110-1301, USA.  */
 #include <signal.h>
 #include "syssignal.h"
 
+#include "character.h"
+#include "font.h"
+
 /* This sucks: this is the first default that x-faces.el tries.  This won't
    be used unless neither the "Emacs.EmacsFrame" resource nor the
    "Emacs.EmacsFrame" resource is set; the frame
@@ -74,14 +76,12 @@ Boston, MA 02110-1301, USA.  */
 #define DEFAULT_FACE_FONT "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-*"
 
 
-static void EmacsFrameInitialize (/*Widget, Widget, ArgList, Cardinal * */);
-static void EmacsFrameDestroy (/* Widget */);
-static void EmacsFrameRealize (/* Widget, XtValueMask*, XSetWindowAttributes* */);
-void EmacsFrameResize (/* Widget widget */);
-static Boolean EmacsFrameSetValues (/* Widget, Widget, Widget,
-                                    ArgList, Cardinal * */);
-static XtGeometryResult EmacsFrameQueryGeometry (/* Widget, XtWidgetGeometry*,
-                                                 XtWidgetGeometry* */);
+static void EmacsFrameInitialize (Widget request, Widget new, ArgList dum1, Cardinal *dum2);
+static void EmacsFrameDestroy (Widget widget);
+static void EmacsFrameRealize (Widget widget, XtValueMask *mask, XSetWindowAttributes *attrs);
+void EmacsFrameResize (Widget widget);
+static Boolean EmacsFrameSetValues (Widget cur_widget, Widget req_widget, Widget new_widget, ArgList dum1, Cardinal *dum2);
+static XtGeometryResult EmacsFrameQueryGeometry (Widget widget, XtWidgetGeometry *request, XtWidgetGeometry *result);
 
 
 #undef XtOffset
@@ -106,7 +106,7 @@ static XtResource resources[] = {
      offset (internal_border_width), XtRImmediate, (XtPointer)4},
   {XtNinterline, XtCInterline, XtRInt, sizeof (int),
      offset (interline), XtRImmediate, (XtPointer)0},
-  {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
+  {XtNfont,  XtCFont, XtRFontStruct, sizeof(struct font *),
      offset(font),XtRString, DEFAULT_FACE_FONT},
   {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
      offset(foreground_pixel), XtRString, "XtDefaultForeground"},
@@ -178,10 +178,7 @@ EmacsFrameClassRec emacsFrameClassRec = {
 WidgetClass emacsFrameClass = (WidgetClass) &emacsFrameClassRec;
 
 static void
-get_default_char_pixel_size (ew, pixel_width, pixel_height)
-     EmacsFrame ew;
-     int* pixel_width;
-     int* pixel_height;
+get_default_char_pixel_size (EmacsFrame ew, int *pixel_width, int *pixel_height)
 {
   struct frame* f = ew->emacs_frame.frame;
   *pixel_width = FRAME_COLUMN_WIDTH (f);
@@ -189,12 +186,7 @@ get_default_char_pixel_size (ew, pixel_width, pixel_height)
 }
 
 static void
-pixel_to_char_size (ew, pixel_width, pixel_height, char_width, char_height)
-     EmacsFrame ew;
-     Dimension pixel_width;
-     Dimension pixel_height;
-     int* char_width;
-     int* char_height;
+pixel_to_char_size (EmacsFrame ew, Dimension pixel_width, Dimension pixel_height, int *char_width, int *char_height)
 {
   struct frame* f = ew->emacs_frame.frame;
   *char_width = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, (int) pixel_width);
@@ -202,12 +194,7 @@ pixel_to_char_size (ew, pixel_width, pixel_height, char_width, char_height)
 }
 
 static void
-char_to_pixel_size (ew, char_width, char_height, pixel_width, pixel_height)
-     EmacsFrame ew;
-     int char_width;
-     int char_height;
-     Dimension* pixel_width;
-     Dimension* pixel_height;
+char_to_pixel_size (EmacsFrame ew, int char_width, int char_height, Dimension *pixel_width, Dimension *pixel_height)
 {
   struct frame* f = ew->emacs_frame.frame;
   *pixel_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, char_width);
@@ -215,12 +202,7 @@ char_to_pixel_size (ew, char_width, char_height, pixel_width, pixel_height)
 }
 
 static void
-round_size_to_char (ew, in_width, in_height, out_width, out_height)
-     EmacsFrame ew;
-     Dimension in_width;
-     Dimension in_height;
-     Dimension* out_width;
-     Dimension* out_height;
+round_size_to_char (EmacsFrame ew, Dimension in_width, Dimension in_height, Dimension *out_width, Dimension *out_height)
 {
   int char_width;
   int char_height;
@@ -229,8 +211,7 @@ round_size_to_char (ew, in_width, in_height, out_width, out_height)
 }
 
 static Widget
-get_wm_shell (w)
-     Widget w;
+get_wm_shell (Widget w)
 {
   Widget wmshell;
 
@@ -267,8 +248,7 @@ static Boolean first_frame_p = True;
 #endif
 
 static void
-set_frame_size (ew)
-     EmacsFrame ew;
+set_frame_size (EmacsFrame ew)
 {
   /* The widget hierarchy is
 
@@ -489,8 +469,7 @@ set_frame_size (ew)
 int update_hints_inhibit;
 
 static void
-update_wm_hints (ew)
-     EmacsFrame ew;
+update_wm_hints (EmacsFrame ew)
 {
   Widget wmshell = get_wm_shell ((Widget)ew);
   int cw;
@@ -568,12 +547,27 @@ static char setup_frame_cursor_bits[] =
 };
 
 static void
-setup_frame_gcs (ew)
-     EmacsFrame ew;
+setup_frame_gcs (EmacsFrame ew)
 {
   XGCValues gc_values;
   struct frame* s = ew->emacs_frame.frame;
   Pixmap blank_stipple, blank_tile;
+  unsigned long valuemask = (GCForeground | GCBackground | GCGraphicsExposures
+                            | GCStipple | GCTile);
+  Lisp_Object font;
+
+  XSETFONT (font, ew->emacs_frame.font);
+  font = Ffont_xlfd_name (font, Qnil);
+  if (STRINGP (font))
+    {
+      XFontStruct *xfont = XLoadQueryFont (FRAME_X_DISPLAY_INFO (s)->display,
+                                          SDATA (font));
+      if (xfont)
+       {
+         gc_values.font = xfont->fid;
+         valuemask |= GCFont;
+       }
+    }
 
   /* We have to initialize all of our GCs to have a stipple/tile, otherwise
      XGetGCValues returns uninitialized data when we query the stipple
@@ -600,31 +594,24 @@ setup_frame_gcs (ew)
                                   ew->core.depth);
 
   /* Normal video */
-  gc_values.font = ew->emacs_frame.font->fid;
   gc_values.foreground = ew->emacs_frame.foreground_pixel;
   gc_values.background = ew->core.background_pixel;
   gc_values.graphics_exposures = False;
   gc_values.stipple = blank_stipple;
   gc_values.tile = blank_tile;
   XChangeGC (XtDisplay (ew), s->output_data.x->normal_gc,
-            (GCFont | GCForeground | GCBackground | GCGraphicsExposures
-             | GCStipple | GCTile),
-            &gc_values);
+            valuemask, &gc_values);
 
   /* Reverse video style. */
-  gc_values.font = ew->emacs_frame.font->fid;
   gc_values.foreground = ew->core.background_pixel;
   gc_values.background = ew->emacs_frame.foreground_pixel;
   gc_values.graphics_exposures = False;
   gc_values.stipple = blank_stipple;
   gc_values.tile = blank_tile;
   XChangeGC (XtDisplay (ew), s->output_data.x->reverse_gc,
-            (GCFont | GCForeground | GCBackground | GCGraphicsExposures
-             | GCStipple | GCTile),
-            &gc_values);
+            valuemask, &gc_values);
 
   /* Cursor has to have an empty stipple. */
-  gc_values.font = ew->emacs_frame.font->fid;
   gc_values.foreground = ew->core.background_pixel;
   gc_values.background = ew->emacs_frame.cursor_color;
   gc_values.graphics_exposures = False;
@@ -634,14 +621,11 @@ setup_frame_gcs (ew)
                             RootWindowOfScreen (XtScreen (ew)),
                             setup_frame_cursor_bits, 16, 16);
   XChangeGC (XtDisplay (ew), s->output_data.x->cursor_gc,
-            (GCFont | GCForeground | GCBackground | GCGraphicsExposures
-             | GCStipple | GCTile),
-            &gc_values);
+            valuemask, &gc_values);
 }
 
 static void
-update_various_frame_slots (ew)
-     EmacsFrame ew;
+update_various_frame_slots (EmacsFrame ew)
 {
   struct frame *f = ew->emacs_frame.frame;
   struct x_output *x = f->output_data.x;
@@ -652,8 +636,7 @@ update_various_frame_slots (ew)
 }
 
 static void
-update_from_various_frame_slots (ew)
-     EmacsFrame ew;
+update_from_various_frame_slots (EmacsFrame ew)
 {
   struct frame *f = ew->emacs_frame.frame;
   struct x_output *x = f->output_data.x;
@@ -668,11 +651,7 @@ update_from_various_frame_slots (ew)
 }
 
 static void
-EmacsFrameInitialize (request, new, dum1, dum2)
-     Widget request;
-     Widget new;
-     ArgList dum1;
-     Cardinal *dum2;
+EmacsFrameInitialize (Widget request, Widget new, ArgList dum1, Cardinal *dum2)
 {
   EmacsFrame ew = (EmacsFrame)new;
 
@@ -683,56 +662,13 @@ EmacsFrameInitialize (request, new, dum1, dum2)
       exit (1);
     }
 
-#if 0 /* done in xfns.c */
-  /* If the "Emacs.EmacsFrame.{default,Face}.{attributeFont,AttributeFont}"
-     resource is set, then it always overrides "Emacs.EmacsFrame.{font,Font}".
-     It's unfortunate that we have to do this, but we need to know the font
-     size for frame-sizing purposes before the faces get initialized.  If
-     the "default.attributeFont" isn't set, then we use the font of this
-     EmacsFrame itself, defaulting to XtDefaultFont.  Up in the lisp code,
-     the "default" face will use the frame's font if its own is not set,
-     so everything stays in sync -- it's not possible for the frame's font
-     and the default face's font to be different.
-   */
-  {
-    XFontStruct *f = 0;
-    XtResource face_res;
-    face_res.resource_name = "attributeFont";
-    face_res.resource_class = "AttributeFont";
-    face_res.resource_type = XtRFontStruct;
-    face_res.resource_size = sizeof (XFontStruct *);
-    face_res.resource_offset = 0;
-    face_res.default_type = XtRImmediate;
-    face_res.default_addr = 0;
-    XtGetSubresources ((Widget) ew, (XtPointer) &f, "default", "Face",
-                      &face_res, 1, NULL, 0);
-
-    if (f)
-       ew->emacs_frame.font = f;
-    else if (! ew->emacs_frame.font)
-      {
-       fprintf (stderr, "emacs frame widget could not load a font\n");
-       exit (1);
-      }
-  }
-
-/* Update the font field in frame */
-  FRAME_FONT (ew->emacs_frame.frame) = ew->emacs_frame.font;
-#endif
-
   update_from_various_frame_slots (ew);
   set_frame_size (ew);
-/*create_frame_gcs (ew);
-  setup_frame_gcs (ew);
-  update_various_frame_slots (ew); */
 }
 
 
 static void
-EmacsFrameRealize (widget, mask, attrs)
-     Widget widget;
-     XtValueMask *mask;
-     XSetWindowAttributes *attrs;
+EmacsFrameRealize (Widget widget, XtValueMask *mask, XSetWindowAttributes *attrs)
 {
   EmacsFrame ew = (EmacsFrame)widget;
 
@@ -748,11 +684,10 @@ EmacsFrameRealize (widget, mask, attrs)
   update_wm_hints (ew);
 }
 
-extern void free_frame_faces (/* struct frame * */);
+extern void free_frame_faces (struct frame *);
 
 static void
-EmacsFrameDestroy (widget)
-     Widget widget;
+EmacsFrameDestroy (Widget widget)
 {
   EmacsFrame ew = (EmacsFrame) widget;
   struct frame* s = ew->emacs_frame.frame;
@@ -770,8 +705,7 @@ EmacsFrameDestroy (widget)
 }
 
 void
-EmacsFrameResize (widget)
-     Widget widget;
+EmacsFrameResize (Widget widget)
 {
   EmacsFrame ew = (EmacsFrame)widget;
   struct frame *f = ew->emacs_frame.frame;
@@ -787,12 +721,7 @@ EmacsFrameResize (widget)
 }
 
 static Boolean
-EmacsFrameSetValues (cur_widget, req_widget, new_widget, dum1, dum2)
-     Widget cur_widget;
-     Widget req_widget;
-     Widget new_widget;
-     ArgList dum1;
-     Cardinal *dum2;
+EmacsFrameSetValues (Widget cur_widget, Widget req_widget, Widget new_widget, ArgList dum1, Cardinal *dum2)
 {
   EmacsFrame cur = (EmacsFrame)cur_widget;
   EmacsFrame new = (EmacsFrame)new_widget;
@@ -806,6 +735,7 @@ EmacsFrameSetValues (cur_widget, req_widget, new_widget, dum1, dum2)
   Dimension pixel_width;
   Dimension pixel_height;
 
+  /* AFAIK, this function is never called. -- Jan D, Oct 2009.  */
   has_to_recompute_gcs = (cur->emacs_frame.font != new->emacs_frame.font
                          || (cur->emacs_frame.foreground_pixel
                              != new->emacs_frame.foreground_pixel)
@@ -864,10 +794,7 @@ EmacsFrameSetValues (cur_widget, req_widget, new_widget, dum1, dum2)
 }
 
 static XtGeometryResult
-EmacsFrameQueryGeometry (widget, request, result)
-     Widget widget;
-     XtWidgetGeometry* request;
-     XtWidgetGeometry* result;
+EmacsFrameQueryGeometry (Widget widget, XtWidgetGeometry *request, XtWidgetGeometry *result)
 {
   EmacsFrame ew = (EmacsFrame)widget;
 
@@ -897,124 +824,17 @@ EmacsFrameQueryGeometry (widget, request, result)
 
 /* Special entrypoints */
 void
-EmacsFrameSetCharSize (widget, columns, rows)
-     Widget widget;
-     int columns;
-     int rows;
+EmacsFrameSetCharSize (Widget widget, int columns, int rows)
 {
   EmacsFrame ew = (EmacsFrame) widget;
-  Dimension pixel_width, pixel_height;
   struct frame *f = ew->emacs_frame.frame;
 
-  if (columns < 3) columns = 3;  /* no way buddy */
-
-  check_frame_size (f, &rows, &columns);
-  f->scroll_bar_actual_width
-    = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
-
-  compute_fringe_widths (f, 0);
-
-  char_to_pixel_size (ew, columns, rows, &pixel_width, &pixel_height);
-
-#if 0  /* This doesn't seem to be right.  The frame gets too wide. --gerd.  */
-  /* Something is really strange here wrt to the border width:
-     Apparently, XtNwidth and XtNheight include the border, so we have
-     to add it here.  But the XtNborderWidth set for the widgets has
-     no similarity to what f->border_width is set to.  */
-  XtVaGetValues (widget, XtNborderWidth, &border_width, NULL);
-  pixel_height += 2 * border_width;
-  pixel_width += 2 * border_width;
-#endif
-
-  /* Manually change the height and width of all our widgets,
-     adjusting each widget by the same increments.  */
-  if (ew->core.width != pixel_width
-      || ew->core.height != pixel_height)
-    {
-      int hdelta = pixel_height - ew->core.height;
-      int wdelta = pixel_width - ew->core.width;
-      int column_widget_height = f->output_data.x->column_widget->core.height;
-      int column_widget_width = f->output_data.x->column_widget->core.width;
-      int outer_widget_height = f->output_data.x->widget->core.height;
-      int outer_widget_width = f->output_data.x->widget->core.width;
-      int old_left = f->output_data.x->widget->core.x;
-      int old_top = f->output_data.x->widget->core.y;
-
-      /* Input is blocked here, and Xt waits for some event to
-         occur.  */
-
-      lw_refigure_widget (f->output_data.x->column_widget, False);
-      update_hints_inhibit = 1;
-
-      /* Xt waits for a ConfigureNotify event from the window manager
-        in EmacsFrameSetCharSize when the shell widget is resized.
-        For some window managers like fvwm2 2.2.5 and KDE 2.1 this
-        event doesn't arrive for an unknown reason and Emacs hangs in
-        Xt when the default font is changed.  Tell Xt not to wait,
-        depending on the value of the frame parameter
-        `wait-for-wm'.  */
-      x_catch_errors (FRAME_X_DISPLAY (f));
-      XtVaSetValues (f->output_data.x->widget,
-                    XtNwaitForWm, (XtArgVal) f->output_data.x->wait_for_wm,
-                    NULL);
-      x_uncatch_errors ();
-
-      /* Workaround: When a SIGIO or SIGALRM occurs while Xt is
-        waiting for a ConfigureNotify event (see above), this leads
-        to Xt waiting indefinitely instead of using its default
-        timeout (5 seconds).  */
-      turn_on_atimers (0);
-#ifdef SIGIO
-      sigblock (sigmask (SIGIO));
-#endif
-
-      /* Do parents first, otherwise LessTif's geometry management
-        enters an infinite loop (as of 2000-01-15).  This is fixed in
-        later versions of LessTif (as of 2001-03-13); I'll leave it
-        as is because I think it can't do any harm.  */
-      /* In April 2002, simon.marshall@misys.com reports the problem
-        seems not to occur any longer.  */
-      x_catch_errors (FRAME_X_DISPLAY (f));
-      XtVaSetValues (f->output_data.x->widget,
-                    XtNheight, (XtArgVal) (outer_widget_height + hdelta),
-                    XtNwidth, (XtArgVal) (outer_widget_width + wdelta),
-                    NULL);
-      XtVaSetValues (f->output_data.x->column_widget,
-                    XtNheight, (XtArgVal) (column_widget_height + hdelta),
-                    XtNwidth, (XtArgVal) column_widget_width + wdelta,
-                    NULL);
-      XtVaSetValues ((Widget) ew,
-                     XtNheight, (XtArgVal) pixel_height,
-                    XtNwidth, (XtArgVal) pixel_width,
-                    NULL);
-      x_uncatch_errors ();
-
-#ifdef SIGIO
-      sigunblock (sigmask (SIGIO));
-#endif
-      turn_on_atimers (1);
-
-      lw_refigure_widget (f->output_data.x->column_widget, True);
-
-      update_hints_inhibit = 0;
-      update_wm_hints (ew);
-
-      /* These seem to get clobbered.  I don't know why. - rms.  */
-      f->output_data.x->widget->core.x = old_left;
-      f->output_data.x->widget->core.y = old_top;
-    }
-
-  /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
-     receive in the ConfigureNotify event; if we get what we asked
-     for, then the event won't cause the screen to become garbaged, so
-     we have to make sure to do it here.  */
-  SET_FRAME_GARBAGED (f);
+  x_set_window_size (f, 0, columns, rows);
 }
 
 \f
 void
-widget_store_internal_border (widget)
-     Widget widget;
+widget_store_internal_border (Widget widget)
 {
   EmacsFrame ew = (EmacsFrame) widget;
   FRAME_PTR f = ew->emacs_frame.frame;