This commit was generated by cvs2svn to compensate for changes in r5209,
[chise/xemacs-chise.git.1] / src / glyphs-x.c
index 45f1e10..5a71012 100644 (file)
@@ -4,7 +4,7 @@
    Copyright (C) 1995 Tinker Systems
    Copyright (C) 1995, 1996 Ben Wing
    Copyright (C) 1995 Sun Microsystems
    Copyright (C) 1995 Tinker Systems
    Copyright (C) 1995, 1996 Ben Wing
    Copyright (C) 1995 Sun Microsystems
-   Copyright (C) 1999 Andy Piper
+   Copyright (C) 1999, 2000 Andy Piper
 
 This file is part of XEmacs.
 
 
 This file is part of XEmacs.
 
@@ -17,7 +17,6 @@ XEmacs is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 ANY WARRANTY; without even the implied warranty of 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 XEmacs; see the file COPYING.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 You should have received a copy of the GNU General Public License
 along with XEmacs; see the file COPYING.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
@@ -25,6 +24,8 @@ Boston, MA 02111-1307, USA.  */
 
 /* Synched up with: Not in FSF. */
 
 
 /* Synched up with: Not in FSF. */
 
+/* 7-8-00 This file is more or less Mule-ized in my Mule workspace. */
+
 /* Original author: Jamie Zawinski for 19.8
    font-truename stuff added by Jamie Zawinski for 19.10
    subwindow support added by Chuck Thompson
 /* Original author: Jamie Zawinski for 19.8
    font-truename stuff added by Jamie Zawinski for 19.10
    subwindow support added by Chuck Thompson
@@ -40,9 +41,11 @@ Boston, MA 02111-1307, USA.  */
    Many changes for color work and optimizations by Jareth Hein for 21.0
    Switch of GIF/JPEG/PNG to new EImage intermediate code by Jareth Hein for 21.0
    TIFF code by Jareth Hein for 21.0
    Many changes for color work and optimizations by Jareth Hein for 21.0
    Switch of GIF/JPEG/PNG to new EImage intermediate code by Jareth Hein for 21.0
    TIFF code by Jareth Hein for 21.0
-   GIF/JPEG/PNG/TIFF code moved to new glyph-eimage.c for 21.0
+   GIF/JPEG/PNG/TIFF code moved to new glyph-eimage.c by Andy Piper for 21.0
+   Subwindow and Widget support by Andy Piper for 21.2
 
    TODO:
 
    TODO:
+   Support the GrayScale, StaticColor and StaticGray visual classes.
    Convert images.el to C and stick it in here?
  */
 
    Convert images.el to C and stick it in here?
  */
 
@@ -52,7 +55,9 @@ Boston, MA 02111-1307, USA.  */
 #include "console-x.h"
 #include "glyphs-x.h"
 #include "objects-x.h"
 #include "console-x.h"
 #include "glyphs-x.h"
 #include "objects-x.h"
+#ifdef HAVE_WIDGETS
 #include "gui-x.h"
 #include "gui-x.h"
+#endif
 #include "xmu.h"
 
 #include "buffer.h"
 #include "xmu.h"
 
 #include "buffer.h"
@@ -73,7 +78,7 @@ Boston, MA 02111-1307, USA.  */
 #include "file-coding.h"
 #endif
 
 #include "file-coding.h"
 #endif
 
-#ifdef LWLIB_USES_MOTIF
+#ifdef LWLIB_WIDGETS_MOTIF
 #include <Xm/Xm.h>
 #endif
 #include <X11/IntrinsicP.h>
 #include <Xm/Xm.h>
 #endif
 #include <X11/IntrinsicP.h>
@@ -99,13 +104,13 @@ DECLARE_IMAGE_INSTANTIATOR_FORMAT (jpeg);
 #endif
 #ifdef HAVE_TIFF
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (tiff);
 #endif
 #ifdef HAVE_TIFF
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (tiff);
-#endif  
+#endif
 #ifdef HAVE_PNG
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (png);
 #ifdef HAVE_PNG
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (png);
-#endif  
+#endif
 #ifdef HAVE_GIF
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (gif);
 #ifdef HAVE_GIF
 DECLARE_IMAGE_INSTANTIATOR_FORMAT (gif);
-#endif  
+#endif
 #ifdef HAVE_XPM
 DEFINE_DEVICE_IIFORMAT (x, xpm);
 #endif
 #ifdef HAVE_XPM
 DEFINE_DEVICE_IIFORMAT (x, xpm);
 #endif
@@ -122,11 +127,19 @@ DEFINE_IMAGE_INSTANTIATOR_FORMAT (font);
 
 DEFINE_IMAGE_INSTANTIATOR_FORMAT (autodetect);
 
 
 DEFINE_IMAGE_INSTANTIATOR_FORMAT (autodetect);
 
+#ifdef HAVE_WIDGETS
+DECLARE_IMAGE_INSTANTIATOR_FORMAT (layout);
 DEFINE_DEVICE_IIFORMAT (x, widget);
 DEFINE_DEVICE_IIFORMAT (x, widget);
+DEFINE_DEVICE_IIFORMAT (x, native_layout);
 DEFINE_DEVICE_IIFORMAT (x, button);
 DEFINE_DEVICE_IIFORMAT (x, progress_gauge);
 DEFINE_DEVICE_IIFORMAT (x, edit_field);
 DEFINE_DEVICE_IIFORMAT (x, button);
 DEFINE_DEVICE_IIFORMAT (x, progress_gauge);
 DEFINE_DEVICE_IIFORMAT (x, edit_field);
+#if defined (LWLIB_WIDGETS_MOTIF) && XmVERSION > 1
 DEFINE_DEVICE_IIFORMAT (x, combo_box);
 DEFINE_DEVICE_IIFORMAT (x, combo_box);
+#endif
+DEFINE_DEVICE_IIFORMAT (x, tab_control);
+DEFINE_DEVICE_IIFORMAT (x, label);
+#endif
 
 static void cursor_font_instantiate (Lisp_Object image_instance,
                                     Lisp_Object instantiator,
 
 static void cursor_font_instantiate (Lisp_Object image_instance,
                                     Lisp_Object instantiator,
@@ -135,6 +148,17 @@ static void cursor_font_instantiate (Lisp_Object image_instance,
                                     int dest_mask,
                                     Lisp_Object domain);
 
                                     int dest_mask,
                                     Lisp_Object domain);
 
+#ifdef HAVE_WIDGETS
+static void
+update_widget_face (widget_value* wv,
+                   Lisp_Image_Instance* ii, Lisp_Object domain);
+static void
+update_tab_widget_face (widget_value* wv,
+                       Lisp_Image_Instance* ii, Lisp_Object domain);
+#endif
+void
+emacs_Xt_handle_widget_losing_focus (struct frame* f, Widget losing_widget);
+
 #include "bitmaps.h"
 
 \f
 #include "bitmaps.h"
 
 \f
@@ -169,6 +193,13 @@ convert_EImage_to_XImage (Lisp_Object device, int width, int height,
   vis = DEVICE_X_VISUAL (XDEVICE(device));
   depth = DEVICE_X_DEPTH(XDEVICE(device));
 
   vis = DEVICE_X_VISUAL (XDEVICE(device));
   depth = DEVICE_X_DEPTH(XDEVICE(device));
 
+  if (vis->class == GrayScale || vis->class == StaticColor ||
+      vis->class == StaticGray)
+    {
+      /* #### Implement me!!! */
+      return NULL;
+    }
+
   if (vis->class == PseudoColor)
     {
       /* Quantize the image and get a histogram while we're at it.
   if (vis->class == PseudoColor)
     {
       /* Quantize the image and get a histogram while we're at it.
@@ -200,13 +231,14 @@ convert_EImage_to_XImage (Lisp_Object device, int width, int height,
   if (vis->class == PseudoColor)
     {
       unsigned long pixarray[256];
   if (vis->class == PseudoColor)
     {
       unsigned long pixarray[256];
-      int pixcount, n;
+      int pixcount;
+      unsigned int n;
       /* use our quantize table to allocate the colors */
       pixcount = 32;
       *pixtbl = xnew_array (unsigned long, pixcount);
       *npixels = 0;
 
       /* use our quantize table to allocate the colors */
       pixcount = 32;
       *pixtbl = xnew_array (unsigned long, pixcount);
       *npixels = 0;
 
-      /* ### should implement a sort by popularity to assure proper allocation */
+      /* #### should implement a sort by popularity to assure proper allocation */
       n = *npixels;
       for (i = 0; i < qtable->num_active_colors; i++)
        {
       n = *npixels;
       for (i = 0; i < qtable->num_active_colors; i++)
        {
@@ -237,7 +269,7 @@ convert_EImage_to_XImage (Lisp_Object device, int width, int height,
              gr = *ip++;
              bl = *ip++;
              conv.val = pixarray[QUANT_GET_COLOR(qtable,rd,gr,bl)];
              gr = *ip++;
              bl = *ip++;
              conv.val = pixarray[QUANT_GET_COLOR(qtable,rd,gr,bl)];
-#if WORDS_BIGENDIAN
+#ifdef WORDS_BIGENDIAN
              if (outimg->byte_order == MSBFirst)
                for (q = 4-byte_cnt; q < 4; q++) *dp++ = conv.cp[q];
              else
              if (outimg->byte_order == MSBFirst)
                for (q = 4-byte_cnt; q < 4; q++) *dp++ = conv.cp[q];
              else
@@ -312,7 +344,7 @@ convert_EImage_to_XImage (Lisp_Object device, int width, int height,
                bl = *ip++ >> (8 - bbits);
 
              conv.val = (rd << rshift) | (gr << gshift) | (bl << bshift);
                bl = *ip++ >> (8 - bbits);
 
              conv.val = (rd << rshift) | (gr << gshift) | (bl << bshift);
-#if WORDS_BIGENDIAN
+#ifdef WORDS_BIGENDIAN
              if (outimg->byte_order == MSBFirst)
                for (q = 4-byte_cnt; q < 4; q++) *dp++ = conv.cp[q];
              else
              if (outimg->byte_order == MSBFirst)
                for (q = 4-byte_cnt; q < 4; q++) *dp++ = conv.cp[q];
              else
@@ -332,7 +364,7 @@ convert_EImage_to_XImage (Lisp_Object device, int width, int height,
 
 
 static void
 
 
 static void
-x_print_image_instance (struct Lisp_Image_Instance *p,
+x_print_image_instance (Lisp_Image_Instance *p,
                        Lisp_Object printcharfun,
                        int escapeflag)
 {
                        Lisp_Object printcharfun,
                        int escapeflag)
 {
@@ -357,25 +389,42 @@ x_print_image_instance (struct Lisp_Image_Instance *p,
     }
 }
 
     }
 }
 
+#ifdef DEBUG_WIDGETS
+extern int debug_widget_instances;
+#endif
+
 static void
 static void
-x_finalize_image_instance (struct Lisp_Image_Instance *p)
+x_finalize_image_instance (Lisp_Image_Instance *p)
 {
   if (!p->data)
     return;
 
 {
   if (!p->data)
     return;
 
-  if (DEVICE_LIVE_P (XDEVICE (p->device)))
+  if (DEVICE_LIVE_P (XDEVICE (IMAGE_INSTANCE_DEVICE (p))))
     {
     {
-      Display *dpy = DEVICE_X_DISPLAY (XDEVICE (p->device));
-
-      if (IMAGE_INSTANCE_TYPE (p) == IMAGE_WIDGET)
+      Display *dpy = DEVICE_X_DISPLAY
+       (XDEVICE (IMAGE_INSTANCE_DEVICE (p)));
+      if (0)
+       ;
+#ifdef HAVE_WIDGETS
+      else if (IMAGE_INSTANCE_TYPE (p) == IMAGE_WIDGET)
        {
          if (IMAGE_INSTANCE_SUBWINDOW_ID (p))
            {
        {
          if (IMAGE_INSTANCE_SUBWINDOW_ID (p))
            {
-             XtUnmanageChild (IMAGE_INSTANCE_X_WIDGET_ID (p));
-             XtDestroyWidget (IMAGE_INSTANCE_X_WIDGET_ID (p));
-             IMAGE_INSTANCE_SUBWINDOW_ID (p) = 0;
+#ifdef DEBUG_WIDGETS
+             debug_widget_instances--;
+             stderr_out ("widget destroyed, %d left\n", debug_widget_instances);
+#endif
+             lw_destroy_widget (IMAGE_INSTANCE_X_WIDGET_ID (p));
+             lw_destroy_widget (IMAGE_INSTANCE_X_CLIPWIDGET (p));
+
+             /* We can release the callbacks again. */
+             ungcpro_popup_callbacks (IMAGE_INSTANCE_X_WIDGET_LWID (p));
+
+             IMAGE_INSTANCE_X_WIDGET_ID (p) = 0;
+             IMAGE_INSTANCE_X_CLIPWIDGET (p) = 0;
            }
        }
            }
        }
+#endif
       else if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
        {
          if (IMAGE_INSTANCE_SUBWINDOW_ID (p))
       else if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
        {
          if (IMAGE_INSTANCE_SUBWINDOW_ID (p))
@@ -384,20 +433,33 @@ x_finalize_image_instance (struct Lisp_Image_Instance *p)
        }
       else
        {
        }
       else
        {
-         if (IMAGE_INSTANCE_X_PIXMAP (p))
-           XFreePixmap (dpy, IMAGE_INSTANCE_X_PIXMAP (p));
+         unsigned int i;
+         if (IMAGE_INSTANCE_PIXMAP_TIMEOUT (p))
+           disable_glyph_animated_timeout (IMAGE_INSTANCE_PIXMAP_TIMEOUT (p));
+
          if (IMAGE_INSTANCE_X_MASK (p) &&
              IMAGE_INSTANCE_X_MASK (p) != IMAGE_INSTANCE_X_PIXMAP (p))
            XFreePixmap (dpy, IMAGE_INSTANCE_X_MASK (p));
          if (IMAGE_INSTANCE_X_MASK (p) &&
              IMAGE_INSTANCE_X_MASK (p) != IMAGE_INSTANCE_X_PIXMAP (p))
            XFreePixmap (dpy, IMAGE_INSTANCE_X_MASK (p));
-         IMAGE_INSTANCE_X_PIXMAP (p) = 0;
-         IMAGE_INSTANCE_X_MASK (p) = 0;
-         
+         IMAGE_INSTANCE_PIXMAP_MASK (p) = 0;
+
+         if (IMAGE_INSTANCE_X_PIXMAP_SLICES (p))
+           {
+             for (i = 0; i < IMAGE_INSTANCE_PIXMAP_MAXSLICE (p); i++)
+               if (IMAGE_INSTANCE_X_PIXMAP_SLICE (p,i))
+                 {
+                   XFreePixmap (dpy, IMAGE_INSTANCE_X_PIXMAP_SLICE (p,i));
+                   IMAGE_INSTANCE_X_PIXMAP_SLICE (p, i) = 0;
+                 }
+             xfree (IMAGE_INSTANCE_X_PIXMAP_SLICES (p));
+             IMAGE_INSTANCE_X_PIXMAP_SLICES (p) = 0;
+           }
+
          if (IMAGE_INSTANCE_X_CURSOR (p))
            {
              XFreeCursor (dpy, IMAGE_INSTANCE_X_CURSOR (p));
              IMAGE_INSTANCE_X_CURSOR (p) = 0;
            }
          if (IMAGE_INSTANCE_X_CURSOR (p))
            {
              XFreeCursor (dpy, IMAGE_INSTANCE_X_CURSOR (p));
              IMAGE_INSTANCE_X_CURSOR (p) = 0;
            }
-         
+
          if (IMAGE_INSTANCE_X_NPIXELS (p) != 0)
            {
              XFreeColors (dpy,
          if (IMAGE_INSTANCE_X_NPIXELS (p) != 0)
            {
              XFreeColors (dpy,
@@ -408,7 +470,13 @@ x_finalize_image_instance (struct Lisp_Image_Instance *p)
            }
        }
     }
            }
        }
     }
-  if (IMAGE_INSTANCE_X_PIXELS (p))
+  /* You can sometimes have pixels without a live device. I forget
+     why, but that's why we free them here if we have a pixmap type
+     image instance. It probably means that we might also get a memory
+     leak with widgets. */
+  if (IMAGE_INSTANCE_TYPE (p) != IMAGE_WIDGET
+      && IMAGE_INSTANCE_TYPE (p) != IMAGE_SUBWINDOW
+      && IMAGE_INSTANCE_X_PIXELS (p))
     {
       xfree (IMAGE_INSTANCE_X_PIXELS (p));
       IMAGE_INSTANCE_X_PIXELS (p) = 0;
     {
       xfree (IMAGE_INSTANCE_X_PIXELS (p));
       IMAGE_INSTANCE_X_PIXELS (p) = 0;
@@ -419,8 +487,8 @@ x_finalize_image_instance (struct Lisp_Image_Instance *p)
 }
 
 static int
 }
 
 static int
-x_image_instance_equal (struct Lisp_Image_Instance *p1,
-                       struct Lisp_Image_Instance *p2, int depth)
+x_image_instance_equal (Lisp_Image_Instance *p1,
+                       Lisp_Image_Instance *p2, int depth)
 {
   switch (IMAGE_INSTANCE_TYPE (p1))
     {
 {
   switch (IMAGE_INSTANCE_TYPE (p1))
     {
@@ -439,7 +507,7 @@ x_image_instance_equal (struct Lisp_Image_Instance *p1,
 }
 
 static unsigned long
 }
 
 static unsigned long
-x_image_instance_hash (struct Lisp_Image_Instance *p, int depth)
+x_image_instance_hash (Lisp_Image_Instance *p, int depth)
 {
   switch (IMAGE_INSTANCE_TYPE (p))
     {
 {
   switch (IMAGE_INSTANCE_TYPE (p))
     {
@@ -459,10 +527,14 @@ x_image_instance_hash (struct Lisp_Image_Instance *p, int depth)
    methods are called. */
 
 static void
    methods are called. */
 
 static void
-x_initialize_pixmap_image_instance (struct Lisp_Image_Instance *ii,
+x_initialize_pixmap_image_instance (Lisp_Image_Instance *ii,
+                                   int slices,
                                    enum image_instance_type type)
 {
   ii->data = xnew_and_zero (struct x_image_instance_data);
                                    enum image_instance_type type)
 {
   ii->data = xnew_and_zero (struct x_image_instance_data);
+  IMAGE_INSTANCE_PIXMAP_MAXSLICE (ii) = slices;
+  IMAGE_INSTANCE_X_PIXMAP_SLICES (ii) =
+    xnew_array_and_zero (Pixmap, slices);
   IMAGE_INSTANCE_TYPE (ii) = type;
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) = Qnil;
   IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (ii) = Qnil;
   IMAGE_INSTANCE_TYPE (ii) = type;
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) = Qnil;
   IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (ii) = Qnil;
@@ -506,7 +578,7 @@ x_locate_pixmap_file (Lisp_Object name)
         (XSTRING_BYTE (name, 2) == '/')))))
     {
       if (!NILP (Ffile_readable_p (name)))
         (XSTRING_BYTE (name, 2) == '/')))))
     {
       if (!NILP (Ffile_readable_p (name)))
-       return name;
+       return Fexpand_file_name (name, Qnil);
       else
        return Qnil;
     }
       else
        return Qnil;
     }
@@ -632,7 +704,7 @@ write_lisp_string_to_temp_file (Lisp_Object string, char *filename_out)
   /* Get the data while doing the conversion */
   while (1)
     {
   /* Get the data while doing the conversion */
   while (1)
     {
-      int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
+      Lstream_data_count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
       if (!size_in_bytes)
        break;
       /* It does seem the flushes are necessary... */
       if (!size_in_bytes)
        break;
       /* It does seem the flushes are necessary... */
@@ -727,7 +799,7 @@ generate_cursor_fg_bg (Lisp_Object device, Lisp_Object *foreground,
   else
     {
       xbg->pixel = 0;
   else
     {
       xbg->pixel = 0;
-      xbg->red = xbg->green = xbg->blue = ~0;
+      xbg->red = xbg->green = xbg->blue = USHRT_MAX;
     }
 }
 
     }
 }
 
@@ -775,12 +847,13 @@ maybe_recolor_cursor (Lisp_Object image_instance, Lisp_Object foreground,
    Use the same code as for `xpm'. */
 
 static void
    Use the same code as for `xpm'. */
 
 static void
-init_image_instance_from_x_image (struct Lisp_Image_Instance *ii,
+init_image_instance_from_x_image (Lisp_Image_Instance *ii,
                                  XImage *ximage,
                                  int dest_mask,
                                  Colormap cmap,
                                  unsigned long *pixels,
                                  int npixels,
                                  XImage *ximage,
                                  int dest_mask,
                                  Colormap cmap,
                                  unsigned long *pixels,
                                  int npixels,
+                                 int slices,
                                  Lisp_Object instantiator)
 {
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
                                  Lisp_Object instantiator)
 {
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
@@ -816,13 +889,15 @@ init_image_instance_from_x_image (struct Lisp_Image_Instance *ii,
 
   XFreeGC (dpy, gc);
 
 
   XFreeGC (dpy, gc);
 
-  x_initialize_pixmap_image_instance (ii, IMAGE_COLOR_PIXMAP);
+  x_initialize_pixmap_image_instance (ii, slices, IMAGE_COLOR_PIXMAP);
 
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) =
     find_keyword_in_vector (instantiator, Q_file);
 
 
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) =
     find_keyword_in_vector (instantiator, Q_file);
 
+  /* Fixup a set of pixmaps. */
   IMAGE_INSTANCE_X_PIXMAP (ii) = pixmap;
   IMAGE_INSTANCE_X_PIXMAP (ii) = pixmap;
-  IMAGE_INSTANCE_X_MASK (ii) = 0;
+
+  IMAGE_INSTANCE_PIXMAP_MASK (ii) = 0;
   IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = ximage->width;
   IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = ximage->height;
   IMAGE_INSTANCE_PIXMAP_DEPTH (ii) = ximage->depth;
   IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = ximage->width;
   IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = ximage->height;
   IMAGE_INSTANCE_PIXMAP_DEPTH (ii) = ximage->depth;
@@ -832,8 +907,44 @@ init_image_instance_from_x_image (struct Lisp_Image_Instance *ii,
 }
 
 static void
 }
 
 static void
-x_init_image_instance_from_eimage (struct Lisp_Image_Instance *ii,
+image_instance_add_x_image (Lisp_Image_Instance *ii,
+                           XImage *ximage,
+                           int slice,
+                           Lisp_Object instantiator)
+{
+  Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
+  Display *dpy;
+  GC gc;
+  Drawable d;
+  Pixmap pixmap;
+
+  dpy = DEVICE_X_DISPLAY (XDEVICE (device));
+  d = XtWindow(DEVICE_XT_APP_SHELL (XDEVICE (device)));
+
+  pixmap = XCreatePixmap (dpy, d, ximage->width,
+                         ximage->height, ximage->depth);
+  if (!pixmap)
+    signal_simple_error ("Unable to create pixmap", instantiator);
+
+  gc = XCreateGC (dpy, pixmap, 0, NULL);
+  if (!gc)
+    {
+      XFreePixmap (dpy, pixmap);
+      signal_simple_error ("Unable to create GC", instantiator);
+    }
+
+  XPutImage (dpy, pixmap, gc, ximage, 0, 0, 0, 0,
+            ximage->width, ximage->height);
+
+  XFreeGC (dpy, gc);
+
+  IMAGE_INSTANCE_X_PIXMAP_SLICE (ii, slice) = pixmap;
+}
+
+static void
+x_init_image_instance_from_eimage (Lisp_Image_Instance *ii,
                                   int width, int height,
                                   int width, int height,
+                                  int slices,
                                   unsigned char *eimage,
                                   int dest_mask,
                                   Lisp_Object instantiator,
                                   unsigned char *eimage,
                                   int dest_mask,
                                   Lisp_Object instantiator,
@@ -843,33 +954,42 @@ x_init_image_instance_from_eimage (struct Lisp_Image_Instance *ii,
   Colormap cmap = DEVICE_X_COLORMAP (XDEVICE(device));
   unsigned long *pixtbl = NULL;
   int npixels = 0;
   Colormap cmap = DEVICE_X_COLORMAP (XDEVICE(device));
   unsigned long *pixtbl = NULL;
   int npixels = 0;
+  int slice;
   XImage* ximage;
 
   XImage* ximage;
 
-  ximage = convert_EImage_to_XImage (device, width, height, eimage,
-                                    &pixtbl, &npixels);
-  if (!ximage)
+  for (slice = 0; slice < slices; slice++)
     {
     {
-      if (pixtbl) xfree (pixtbl);
-      signal_image_error("EImage to XImage conversion failed", instantiator);
-    }
+      ximage = convert_EImage_to_XImage (device, width, height,
+                                        eimage + (width * height * 3 * slice),
+                                        &pixtbl, &npixels);
+      if (!ximage)
+       {
+         if (pixtbl) xfree (pixtbl);
+         signal_image_error("EImage to XImage conversion failed", instantiator);
+       }
 
 
-  /* Now create the pixmap and set up the image instance */
-  init_image_instance_from_x_image (ii, ximage, dest_mask,
-                                   cmap, pixtbl, npixels,
-                                   instantiator);
+      /* Now create the pixmap and set up the image instance */
+      if (slice == 0)
+       init_image_instance_from_x_image (ii, ximage, dest_mask,
+                                         cmap, pixtbl, npixels, slices,
+                                         instantiator);
+      else
+       image_instance_add_x_image (ii, ximage, slice, instantiator);
 
 
-  if (ximage)
-    {
-      if (ximage->data)
-        {
-         xfree (ximage->data);
-          ximage->data = 0;
-        }
-      XDestroyImage (ximage);
+      if (ximage)
+       {
+         if (ximage->data)
+           {
+             xfree (ximage->data);
+             ximage->data = 0;
+           }
+         XDestroyImage (ximage);
+         ximage = 0;
+       }
     }
 }
 
     }
 }
 
-int read_bitmap_data_from_file (CONST char *filename, unsigned int *width,
+int read_bitmap_data_from_file (const char *filename, unsigned int *width,
                                unsigned int *height, unsigned char **datap,
                                int *x_hot, int *y_hot)
 {
                                unsigned int *height, unsigned char **datap,
                                int *x_hot, int *y_hot)
 {
@@ -883,22 +1003,23 @@ int read_bitmap_data_from_file (CONST char *filename, unsigned int *width,
 static Pixmap
 pixmap_from_xbm_inline (Lisp_Object device, int width, int height,
                        /* Note that data is in ext-format! */
 static Pixmap
 pixmap_from_xbm_inline (Lisp_Object device, int width, int height,
                        /* Note that data is in ext-format! */
-                       CONST Extbyte *bits)
+                       const char *bits)
 {
 {
-  return XCreatePixmapFromBitmapData (DEVICE_X_DISPLAY (XDEVICE(device)),
-                                     XtWindow (DEVICE_XT_APP_SHELL (XDEVICE (device))),
-                                     (char *) bits, width, height,
-                                     1, 0, 1);
+  return XCreatePixmapFromBitmapData
+    (DEVICE_X_DISPLAY (XDEVICE (device)),
+     XtWindow (DEVICE_XT_APP_SHELL (XDEVICE (device))),
+     (char *) bits, width, height,
+     1, 0, 1);
 }
 
 /* Given inline data for a mono pixmap, initialize the given
    image instance accordingly. */
 
 static void
 }
 
 /* Given inline data for a mono pixmap, initialize the given
    image instance accordingly. */
 
 static void
-init_image_instance_from_xbm_inline (struct Lisp_Image_Instance *ii,
+init_image_instance_from_xbm_inline (Lisp_Image_Instance *ii,
                                     int width, int height,
                                     /* Note that data is in ext-format! */
                                     int width, int height,
                                     /* Note that data is in ext-format! */
-                                    CONST char *bits,
+                                    const char *bits,
                                     Lisp_Object instantiator,
                                     Lisp_Object pointer_fg,
                                     Lisp_Object pointer_bg,
                                     Lisp_Object instantiator,
                                     Lisp_Object pointer_fg,
                                     Lisp_Object pointer_bg,
@@ -940,7 +1061,7 @@ init_image_instance_from_xbm_inline (struct Lisp_Image_Instance *ii,
                              IMAGE_MONO_PIXMAP_MASK | IMAGE_COLOR_PIXMAP_MASK
                              | IMAGE_POINTER_MASK);
 
                              IMAGE_MONO_PIXMAP_MASK | IMAGE_COLOR_PIXMAP_MASK
                              | IMAGE_POINTER_MASK);
 
-  x_initialize_pixmap_image_instance (ii, type);
+  x_initialize_pixmap_image_instance (ii, 1, type);
   IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = width;
   IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = height;
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) =
   IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = width;
   IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = height;
   IMAGE_INSTANCE_PIXMAP_FILENAME (ii) =
@@ -1036,24 +1157,22 @@ xbm_instantiate_1 (Lisp_Object image_instance, Lisp_Object instantiator,
                   Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                   int dest_mask, int width, int height,
                   /* Note that data is in ext-format! */
                   Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                   int dest_mask, int width, int height,
                   /* Note that data is in ext-format! */
-                  CONST char *bits)
+                  const char *bits)
 {
   Lisp_Object mask_data = find_keyword_in_vector (instantiator, Q_mask_data);
   Lisp_Object mask_file = find_keyword_in_vector (instantiator, Q_mask_file);
 {
   Lisp_Object mask_data = find_keyword_in_vector (instantiator, Q_mask_data);
   Lisp_Object mask_file = find_keyword_in_vector (instantiator, Q_mask_file);
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Pixmap mask = 0;
   Pixmap mask = 0;
-  CONST char *gcc_may_you_rot_in_hell;
 
   if (!NILP (mask_data))
     {
 
   if (!NILP (mask_data))
     {
-      GET_C_STRING_BINARY_DATA_ALLOCA (XCAR (XCDR (XCDR (mask_data))),
-                                      gcc_may_you_rot_in_hell);
-      mask =
-       pixmap_from_xbm_inline (IMAGE_INSTANCE_DEVICE (ii),
-                               XINT (XCAR (mask_data)),
-                               XINT (XCAR (XCDR (mask_data))),
-                               (CONST unsigned char *)
-                               gcc_may_you_rot_in_hell);
+      const char *ext_data;
+
+      LISP_STRING_TO_EXTERNAL (XCAR (XCDR (XCDR (mask_data))), ext_data, Qbinary);
+      mask = pixmap_from_xbm_inline (IMAGE_INSTANCE_DEVICE (ii),
+                                    XINT (XCAR (mask_data)),
+                                    XINT (XCAR (XCDR (mask_data))),
+                                    ext_data);
     }
 
   init_image_instance_from_xbm_inline (ii, width, height, bits,
     }
 
   init_image_instance_from_xbm_inline (ii, width, height, bits,
@@ -1069,16 +1188,15 @@ x_xbm_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                   int dest_mask, Lisp_Object domain)
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
                   int dest_mask, Lisp_Object domain)
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
-  CONST char *gcc_go_home;
+  const char *ext_data;
 
   assert (!NILP (data));
 
 
   assert (!NILP (data));
 
-  GET_C_STRING_BINARY_DATA_ALLOCA (XCAR (XCDR (XCDR (data))),
-                                  gcc_go_home);
+  LISP_STRING_TO_EXTERNAL (XCAR (XCDR (XCDR (data))), ext_data, Qbinary);
 
   xbm_instantiate_1 (image_instance, instantiator, pointer_fg,
                     pointer_bg, dest_mask, XINT (XCAR (data)),
 
   xbm_instantiate_1 (image_instance, instantiator, pointer_fg,
                     pointer_bg, dest_mask, XINT (XCAR (data)),
-                    XINT (XCAR (XCDR (data))), gcc_go_home);
+                    XINT (XCAR (XCDR (data))), ext_data);
 }
 
 \f
 }
 
 \f
@@ -1182,11 +1300,11 @@ xpm_free (XpmAttributes *xpmattrs)
 
 static void
 x_xpm_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
 
 static void
 x_xpm_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
-                                  Lisp_Object pointer_fg, Lisp_Object pointer_bg,
-                                  int dest_mask, Lisp_Object domain)
+                  Lisp_Object pointer_fg, Lisp_Object pointer_bg,
+                  int dest_mask, Lisp_Object domain)
 {
   /* This function can GC */
 {
   /* This function can GC */
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   Display *dpy;
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   Display *dpy;
@@ -1244,7 +1362,7 @@ x_xpm_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
   visual = DEVICE_X_VISUAL (XDEVICE(device));
 #endif
 
   visual = DEVICE_X_VISUAL (XDEVICE(device));
 #endif
 
-  x_initialize_pixmap_image_instance (ii, type);
+  x_initialize_pixmap_image_instance (ii, 1, type);
 
   assert (!NILP (data));
 
 
   assert (!NILP (data));
 
@@ -1354,7 +1472,7 @@ x_xpm_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
       pixels = NULL;
 
     IMAGE_INSTANCE_X_PIXMAP (ii) = pixmap;
       pixels = NULL;
 
     IMAGE_INSTANCE_X_PIXMAP (ii) = pixmap;
-    IMAGE_INSTANCE_X_MASK (ii) = mask;
+    IMAGE_INSTANCE_PIXMAP_MASK (ii) = (void*)mask;
     IMAGE_INSTANCE_X_COLORMAP (ii) = cmap;
     IMAGE_INSTANCE_X_PIXELS (ii) = pixels;
     IMAGE_INSTANCE_X_NPIXELS (ii) = npixels;
     IMAGE_INSTANCE_X_COLORMAP (ii) = cmap;
     IMAGE_INSTANCE_X_PIXELS (ii) = pixels;
     IMAGE_INSTANCE_X_NPIXELS (ii) = npixels;
@@ -1553,13 +1671,14 @@ x_xface_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   int i, stattis;
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   int i, stattis;
-  char *p, *bits, *bp;
-  CONST char * volatile emsg = 0;
-  CONST char * volatile dstring;
+  char *bits, *bp;
+  const char *p;
+  const char * volatile emsg = 0;
+  const char * volatile dstring;
 
   assert (!NILP (data));
 
 
   assert (!NILP (data));
 
-  GET_C_STRING_BINARY_DATA_ALLOCA (data, dstring);
+  LISP_STRING_TO_EXTERNAL (data, dstring, Qbinary);
 
   if ((p = strchr (dstring, ':')))
     {
 
   if ((p = strchr (dstring, ':')))
     {
@@ -1623,7 +1742,8 @@ autodetect_validate (Lisp_Object instantiator)
 
 static Lisp_Object
 autodetect_normalize (Lisp_Object instantiator,
 
 static Lisp_Object
 autodetect_normalize (Lisp_Object instantiator,
-                               Lisp_Object console_type)
+                     Lisp_Object console_type,
+                     Lisp_Object dest_mask)
 {
   Lisp_Object file = find_keyword_in_vector (instantiator, Q_data);
   Lisp_Object filename = Qnil;
 {
   Lisp_Object file = find_keyword_in_vector (instantiator, Q_data);
   Lisp_Object filename = Qnil;
@@ -1714,10 +1834,10 @@ autodetect_possible_dest_types (void)
 
 static void
 autodetect_instantiate (Lisp_Object image_instance,
 
 static void
 autodetect_instantiate (Lisp_Object image_instance,
-                                 Lisp_Object instantiator,
-                                 Lisp_Object pointer_fg,
-                                 Lisp_Object pointer_bg,
-                                 int dest_mask, Lisp_Object domain)
+                       Lisp_Object instantiator,
+                       Lisp_Object pointer_fg,
+                       Lisp_Object pointer_bg,
+                       int dest_mask, Lisp_Object domain)
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   struct gcpro gcpro1, gcpro2, gcpro3;
 {
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
   struct gcpro gcpro1, gcpro2, gcpro3;
@@ -1730,8 +1850,8 @@ autodetect_instantiate (Lisp_Object image_instance,
   alist = tagged_vector_to_alist (instantiator);
   if (dest_mask & IMAGE_POINTER_MASK)
     {
   alist = tagged_vector_to_alist (instantiator);
   if (dest_mask & IMAGE_POINTER_MASK)
     {
-      CONST char *name_ext;
-      GET_C_STRING_FILENAME_DATA_ALLOCA (data, name_ext);
+      const char *name_ext;
+      LISP_STRING_TO_EXTERNAL (data, name_ext, Qfile_name);
       if (XmuCursorNameToIndex (name_ext) != -1)
         {
           result = alist_to_tagged_vector (Qcursor_font, alist);
       if (XmuCursorNameToIndex (name_ext) != -1)
         {
           result = alist_to_tagged_vector (Qcursor_font, alist);
@@ -1818,7 +1938,7 @@ font_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
 {
   /* This function can GC */
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
 {
   /* This function can GC */
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Display *dpy;
   XColor fg, bg;
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Display *dpy;
   XColor fg, bg;
@@ -1882,7 +2002,7 @@ font_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
 
   /* #### call XQueryTextExtents() and check_pointer_sizes() here. */
 
 
   /* #### call XQueryTextExtents() and check_pointer_sizes() here. */
 
-  x_initialize_pixmap_image_instance (ii, IMAGE_POINTER);
+  x_initialize_pixmap_image_instance (ii, 1, IMAGE_POINTER);
   IMAGE_INSTANCE_X_CURSOR (ii) =
     XCreateGlyphCursor (dpy, source, mask, source_char, mask_char,
                        &fg, &bg);
   IMAGE_INSTANCE_X_CURSOR (ii) =
     XCreateGlyphCursor (dpy, source, mask, source_char, mask_char,
                        &fg, &bg);
@@ -1916,11 +2036,11 @@ cursor_font_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
 {
   /* This function can GC */
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
 {
   /* This function can GC */
   Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Display *dpy;
   int i;
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Display *dpy;
   int i;
-  CONST char *name_ext;
+  const char *name_ext;
   Lisp_Object foreground, background;
 
   if (!DEVICE_X_P (XDEVICE (device)))
   Lisp_Object foreground, background;
 
   if (!DEVICE_X_P (XDEVICE (device)))
@@ -1931,11 +2051,11 @@ cursor_font_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
   if (!(dest_mask & IMAGE_POINTER_MASK))
     incompatible_image_types (instantiator, dest_mask, IMAGE_POINTER_MASK);
 
   if (!(dest_mask & IMAGE_POINTER_MASK))
     incompatible_image_types (instantiator, dest_mask, IMAGE_POINTER_MASK);
 
-  GET_C_STRING_FILENAME_DATA_ALLOCA (data, name_ext);
+  LISP_STRING_TO_EXTERNAL (data, name_ext, Qfile_name);
   if ((i = XmuCursorNameToIndex (name_ext)) == -1)
     signal_simple_error ("Unrecognized cursor-font name", data);
 
   if ((i = XmuCursorNameToIndex (name_ext)) == -1)
     signal_simple_error ("Unrecognized cursor-font name", data);
 
-  x_initialize_pixmap_image_instance (ii, IMAGE_POINTER);
+  x_initialize_pixmap_image_instance (ii, 1, IMAGE_POINTER);
   IMAGE_INSTANCE_X_CURSOR (ii) = XCreateFontCursor (dpy, i);
   foreground = find_keyword_in_vector (instantiator, Q_foreground);
   if (NILP (foreground))
   IMAGE_INSTANCE_X_CURSOR (ii) = XCreateFontCursor (dpy, i);
   foreground = find_keyword_in_vector (instantiator, Q_foreground);
   if (NILP (foreground))
@@ -1950,7 +2070,7 @@ static int
 x_colorize_image_instance (Lisp_Object image_instance,
                           Lisp_Object foreground, Lisp_Object background)
 {
 x_colorize_image_instance (Lisp_Object image_instance,
                           Lisp_Object foreground, Lisp_Object background)
 {
-  struct Lisp_Image_Instance *p;
+  Lisp_Image_Instance *p;
 
   p = XIMAGE_INSTANCE (image_instance);
 
 
   p = XIMAGE_INSTANCE (image_instance);
 
@@ -1960,7 +2080,7 @@ x_colorize_image_instance (Lisp_Object image_instance,
       IMAGE_INSTANCE_TYPE (p) = IMAGE_COLOR_PIXMAP;
       /* Make sure there aren't two pointers to the same mask, causing
         it to get freed twice. */
       IMAGE_INSTANCE_TYPE (p) = IMAGE_COLOR_PIXMAP;
       /* Make sure there aren't two pointers to the same mask, causing
         it to get freed twice. */
-      IMAGE_INSTANCE_X_MASK (p) = 0;
+      IMAGE_INSTANCE_PIXMAP_MASK (p) = 0;
       break;
 
     default:
       break;
 
     default:
@@ -2003,54 +2123,173 @@ x_colorize_image_instance (Lisp_Object image_instance,
 /* unmap the image if it is a widget. This is used by redisplay via
    redisplay_unmap_subwindows */
 static void
 /* unmap the image if it is a widget. This is used by redisplay via
    redisplay_unmap_subwindows */
 static void
-x_unmap_subwindow (struct Lisp_Image_Instance *p)
+x_unmap_subwindow (Lisp_Image_Instance *p)
 {
   if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
     {
 {
   if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
     {
-      XUnmapWindow 
-       (DisplayOfScreen (IMAGE_INSTANCE_X_SUBWINDOW_SCREEN (p)), 
-        IMAGE_INSTANCE_X_SUBWINDOW_ID (p));
+      XUnmapWindow
+       (IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (p),
+        IMAGE_INSTANCE_X_CLIPWINDOW (p));
     }
   else                         /* must be a widget */
     {
     }
   else                         /* must be a widget */
     {
-      XtUnmapWidget (IMAGE_INSTANCE_X_WIDGET_ID (p));
+      /* Since we are being unmapped we want the enclosing frame to
+        get focus. The losing with simple scrolling but is the safest
+        thing to do. */
+      emacs_Xt_handle_widget_losing_focus 
+       ( XFRAME (IMAGE_INSTANCE_FRAME (p)),
+         IMAGE_INSTANCE_X_WIDGET_ID (p));
+      XtUnmapWidget (IMAGE_INSTANCE_X_CLIPWIDGET (p));
     }
 }
 
 /* map the subwindow. This is used by redisplay via
    redisplay_output_subwindow */
 static void
     }
 }
 
 /* map the subwindow. This is used by redisplay via
    redisplay_output_subwindow */
 static void
-x_map_subwindow (struct Lisp_Image_Instance *p, int x, int y)
+x_map_subwindow (Lisp_Image_Instance *p, int x, int y,
+                struct display_glyph_area* dga)
 {
 {
+  assert (dga->width > 0 && dga->height > 0);
   if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
     {
       Window subwindow = IMAGE_INSTANCE_X_SUBWINDOW_ID (p);
   if (IMAGE_INSTANCE_TYPE (p) == IMAGE_SUBWINDOW)
     {
       Window subwindow = IMAGE_INSTANCE_X_SUBWINDOW_ID (p);
-      Screen* screen = IMAGE_INSTANCE_X_SUBWINDOW_SCREEN (p);
-      XMapWindow (DisplayOfScreen (screen), subwindow);
-      XMoveWindow (DisplayOfScreen (screen), subwindow, x, y);
+      XMoveResizeWindow (IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (p),
+                        IMAGE_INSTANCE_X_CLIPWINDOW (p),
+                        x, y, dga->width, dga->height);
+      XMoveWindow (IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (p),
+                  subwindow, -dga->xoffset, -dga->yoffset);
+      if (!IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (p))
+       XMapWindow (IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (p),
+                   IMAGE_INSTANCE_X_CLIPWINDOW (p));
     }
   else                         /* must be a widget */
     {
     }
   else                         /* must be a widget */
     {
-      XtMoveWidget (IMAGE_INSTANCE_X_WIDGET_ID (p), 
-                   x + IMAGE_INSTANCE_X_WIDGET_XOFFSET (p),
-                   y + IMAGE_INSTANCE_X_WIDGET_YOFFSET (p));
-      XtMapWidget (IMAGE_INSTANCE_X_WIDGET_ID (p));
+      XtConfigureWidget (IMAGE_INSTANCE_X_CLIPWIDGET (p),
+                        x + IMAGE_INSTANCE_X_WIDGET_XOFFSET (p),
+                        y + IMAGE_INSTANCE_X_WIDGET_YOFFSET (p),
+                        dga->width, dga->height, 0);
+      XtMoveWidget (IMAGE_INSTANCE_X_WIDGET_ID (p),
+                   -dga->xoffset, -dga->yoffset);
+      if (!IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (p))
+       XtMapWidget (IMAGE_INSTANCE_X_CLIPWIDGET (p));
     }
 }
 
 /* when you click on a widget you may activate another widget this
    needs to be checked and all appropriate widgets updated */
 static void
     }
 }
 
 /* when you click on a widget you may activate another widget this
    needs to be checked and all appropriate widgets updated */
 static void
-x_update_subwindow (struct Lisp_Image_Instance *p)
+x_redisplay_subwindow (Lisp_Image_Instance *p)
+{
+  /* Update the subwindow size if necessary. */
+  if (IMAGE_INSTANCE_SIZE_CHANGED (p))
+    {
+      XResizeWindow (IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (p),
+                    IMAGE_INSTANCE_X_SUBWINDOW_ID (p),
+                    IMAGE_INSTANCE_WIDTH (p),
+                    IMAGE_INSTANCE_HEIGHT (p));
+    }
+}
+
+/* Update all attributes that have changed. Lwlib actually does most
+   of this for us. */
+static void
+x_redisplay_widget (Lisp_Image_Instance *p)
 {
 {
-  if (IMAGE_INSTANCE_TYPE (p) == IMAGE_WIDGET)
+  /* This function can GC if IN_REDISPLAY is false. */
+#ifdef HAVE_WIDGETS
+  widget_value* wv = 0;
+
+  /* First get the items if they have changed since this is a
+     structural change. As such it will nuke all added values so we
+     need to update most other things after the items have changed.*/
+  if (IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (p))
     {
     {
-      widget_value* wv = xmalloc_widget_value ();
-      button_item_to_widget_value (IMAGE_INSTANCE_WIDGET_SINGLE_ITEM (p),
-                                  wv, 1, 1);
-      lw_modify_all_widgets (IMAGE_INSTANCE_X_WIDGET_LWID (p), 
-                            wv, 1);
+      Lisp_Object image_instance;
+
+      XSETIMAGE_INSTANCE (image_instance, p);
+      wv = gui_items_to_widget_values
+       (image_instance, IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (p),
+        /* #### this is not right; we need to keep track of which widgets
+           want accelerators and which don't */ 0);
+      wv->change = STRUCTURAL_CHANGE;
     }
     }
+  else
+    {
+      /* Assume the lotus position, breath deeply and chant to
+        yourself lwlibsux, lwlibsux ... lw_get_all_values returns a
+        reference to the real values rather than a copy thus any
+        changes we make to the values we get back will look like they
+        have already been applied. If we rebuild the widget tree then
+        we may lose properties. */
+      wv = copy_widget_value_tree (lw_get_all_values 
+                                  (IMAGE_INSTANCE_X_WIDGET_LWID (p)),
+                                  NO_CHANGE);
+    }
+
+  /* Possibly update the colors and font */
+  if (IMAGE_INSTANCE_WIDGET_FACE_CHANGED (p)
+      ||
+      /* #### This is not sufficient because it will not cope with widgets
+        that are not currently visible. Once redisplay has done the
+        visible ones it will clear this flag so that when new ones
+        become visible they will not be updated. */
+      XFRAME (IMAGE_INSTANCE_FRAME (p))->faces_changed
+      ||
+      XFRAME (IMAGE_INSTANCE_FRAME (p))->frame_changed
+      ||
+      IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (p))
+    {
+      update_widget_face (wv, p, IMAGE_INSTANCE_FRAME (p));
+    }
+
+  /* Possibly update the text. */
+  if (IMAGE_INSTANCE_TEXT_CHANGED (p))
+    {
+      char* str;
+      Lisp_Object val = IMAGE_INSTANCE_WIDGET_TEXT (p);
+      LISP_STRING_TO_EXTERNAL (val, str, Qnative);
+      wv->value = str;
+    }
+
+  /* Possibly update the size. */
+  if (IMAGE_INSTANCE_SIZE_CHANGED (p)
+      ||
+      IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (p)
+      ||
+      IMAGE_INSTANCE_TEXT_CHANGED (p))
+    {
+      assert (IMAGE_INSTANCE_X_WIDGET_ID (p) &&
+             IMAGE_INSTANCE_X_CLIPWIDGET (p)) ;
+
+      if (IMAGE_INSTANCE_X_WIDGET_ID (p)->core.being_destroyed
+         || !XtIsManaged(IMAGE_INSTANCE_X_WIDGET_ID (p)))
+       {
+         Lisp_Object sw;
+         XSETIMAGE_INSTANCE (sw, p);
+         signal_simple_error ("XEmacs bug: subwindow is deleted", sw);
+       }
+
+      lw_add_widget_value_arg (wv, XtNwidth,
+                              (Dimension)IMAGE_INSTANCE_WIDTH (p));
+      lw_add_widget_value_arg (wv, XtNheight,
+                              (Dimension)IMAGE_INSTANCE_HEIGHT (p));
+    }
+
+  /* Adjust offsets within the frame. */
+  if (XFRAME (IMAGE_INSTANCE_FRAME (p))->size_changed)
+    {
+      Arg al[2];
+      XtSetArg (al [0], XtNx, &IMAGE_INSTANCE_X_WIDGET_XOFFSET (p));
+      XtSetArg (al [1], XtNy, &IMAGE_INSTANCE_X_WIDGET_YOFFSET (p));
+      XtGetValues (FRAME_X_TEXT_WIDGET 
+                  (XFRAME (IMAGE_INSTANCE_FRAME (p))), al, 2);
+    }
+
+  /* now modify the widget */
+  lw_modify_all_widgets (IMAGE_INSTANCE_X_WIDGET_LWID (p),
+                        wv, True);
+  free_widget_value_tree (wv);
+#endif
 }
 
 /* instantiate and x type subwindow */
 }
 
 /* instantiate and x type subwindow */
@@ -2060,17 +2299,17 @@ x_subwindow_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                        int dest_mask, Lisp_Object domain)
 {
   /* This function can GC */
                        int dest_mask, Lisp_Object domain)
 {
   /* This function can GC */
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
-  Lisp_Object frame = FW_FRAME (domain);
+  Lisp_Object frame = DOMAIN_FRAME (domain);
   struct frame* f = XFRAME (frame);
   Display *dpy;
   Screen *xs;
   Window pw, win;
   XSetWindowAttributes xswa;
   Mask valueMask = 0;
   struct frame* f = XFRAME (frame);
   Display *dpy;
   Screen *xs;
   Window pw, win;
   XSetWindowAttributes xswa;
   Mask valueMask = 0;
-  unsigned int w = IMAGE_INSTANCE_SUBWINDOW_WIDTH (ii), 
-    h = IMAGE_INSTANCE_SUBWINDOW_HEIGHT (ii);
+  unsigned int w = IMAGE_INSTANCE_WIDTH (ii),
+    h = IMAGE_INSTANCE_HEIGHT (ii);
 
   if (!DEVICE_X_P (XDEVICE (device)))
     signal_simple_error ("Not an X device", device);
 
   if (!DEVICE_X_P (XDEVICE (device)))
     signal_simple_error ("Not an X device", device);
@@ -2085,17 +2324,25 @@ x_subwindow_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
   ii->data = xnew_and_zero (struct x_subwindow_data);
 
   IMAGE_INSTANCE_X_SUBWINDOW_PARENT (ii) = pw;
   ii->data = xnew_and_zero (struct x_subwindow_data);
 
   IMAGE_INSTANCE_X_SUBWINDOW_PARENT (ii) = pw;
-  IMAGE_INSTANCE_X_SUBWINDOW_SCREEN (ii) = xs;
+  IMAGE_INSTANCE_X_SUBWINDOW_DISPLAY (ii) = DisplayOfScreen (xs);
 
   xswa.backing_store = Always;
   valueMask |= CWBackingStore;
   xswa.colormap = DefaultColormapOfScreen (xs);
   valueMask |= CWColormap;
 
   xswa.backing_store = Always;
   valueMask |= CWBackingStore;
   xswa.colormap = DefaultColormapOfScreen (xs);
   valueMask |= CWColormap;
-  
-  win = XCreateWindow (dpy, pw, 0, 0, w, h, 0, CopyFromParent,
+
+  /* Create a window for clipping */
+  IMAGE_INSTANCE_X_CLIPWINDOW (ii) =
+    XCreateWindow (dpy, pw, 0, 0, w, h, 0, CopyFromParent,
+                  InputOutput, CopyFromParent, valueMask,
+                  &xswa);
+
+  /* Now put the subwindow inside the clip window. */
+  win = XCreateWindow (dpy, IMAGE_INSTANCE_X_CLIPWINDOW (ii),
+                      0, 0, w, h, 0, CopyFromParent,
                       InputOutput, CopyFromParent, valueMask,
                       &xswa);
                       InputOutput, CopyFromParent, valueMask,
                       &xswa);
-  
+
   IMAGE_INSTANCE_SUBWINDOW_ID (ii) = (void*)win;
 }
 
   IMAGE_INSTANCE_SUBWINDOW_ID (ii) = (void*)win;
 }
 
@@ -2108,7 +2355,7 @@ Subwindows are not currently implemented.
        (subwindow, property, data))
 {
   Atom property_atom;
        (subwindow, property, data))
 {
   Atom property_atom;
-  struct Lisp_Subwindow *sw;
+  Lisp_Subwindow *sw;
   Display *dpy;
 
   CHECK_SUBWINDOW (subwindow);
   Display *dpy;
 
   CHECK_SUBWINDOW (subwindow);
@@ -2129,38 +2376,104 @@ Subwindows are not currently implemented.
 }
 #endif
 
 }
 #endif
 
-static void 
-x_resize_subwindow (struct Lisp_Image_Instance* ii, int w, int h)
-{
-  XResizeWindow (DisplayOfScreen (IMAGE_INSTANCE_X_SUBWINDOW_SCREEN (ii)),
-                IMAGE_INSTANCE_X_SUBWINDOW_ID (ii),
-                w, h);
-}
+\f
+#ifdef HAVE_WIDGETS
 
 /************************************************************************/
 /*                            widgets                            */
 /************************************************************************/
 
 static void
 
 /************************************************************************/
 /*                            widgets                            */
 /************************************************************************/
 
 static void
+update_widget_face (widget_value* wv, Lisp_Image_Instance *ii,
+                   Lisp_Object domain)
+{
+#ifdef LWLIB_WIDGETS_MOTIF
+  XmFontList fontList;
+#endif
+  /* Update the foreground. */
+  Lisp_Object pixel = FACE_FOREGROUND
+    (IMAGE_INSTANCE_WIDGET_FACE (ii),
+     domain);
+  XColor fcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel)), bcolor;
+  lw_add_widget_value_arg (wv, XtNforeground, fcolor.pixel);
+
+  /* Update the background. */
+  pixel = FACE_BACKGROUND (IMAGE_INSTANCE_WIDGET_FACE (ii),
+                          domain);
+  bcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
+  lw_add_widget_value_arg (wv, XtNbackground, bcolor.pixel);
+
+#ifdef LWLIB_WIDGETS_MOTIF
+  fontList = XmFontListCreate
+    (FONT_INSTANCE_X_FONT
+     (XFONT_INSTANCE (query_string_font
+                     (IMAGE_INSTANCE_WIDGET_TEXT (ii),
+                      IMAGE_INSTANCE_WIDGET_FACE (ii),
+                      domain))),  XmSTRING_DEFAULT_CHARSET);
+  lw_add_widget_value_arg (wv, XmNfontList, (XtArgVal)fontList);
+#endif
+  lw_add_widget_value_arg
+    (wv, XtNfont, (XtArgVal)FONT_INSTANCE_X_FONT
+     (XFONT_INSTANCE (query_string_font
+                     (IMAGE_INSTANCE_WIDGET_TEXT (ii),
+                      IMAGE_INSTANCE_WIDGET_FACE (ii),
+                      domain))));
+  wv->change = VISIBLE_CHANGE;
+  /* #### Megahack - but its just getting too complicated to do this
+     in the right place. */
+  if (EQ (IMAGE_INSTANCE_WIDGET_TYPE (ii), Qtab_control))
+    update_tab_widget_face (wv, ii, domain);
+}
+
+static void
+update_tab_widget_face (widget_value* wv, Lisp_Image_Instance *ii,
+                       Lisp_Object domain)
+{
+  if (wv->contents)
+    {
+      widget_value* val = wv->contents, *cur;
+
+      /* Give each child label the correct foreground color. */
+      Lisp_Object pixel = FACE_FOREGROUND
+       (IMAGE_INSTANCE_WIDGET_FACE (ii),
+        domain);
+      XColor fcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
+      lw_add_widget_value_arg (val, XtNtabForeground, fcolor.pixel);
+      wv->change = VISIBLE_CHANGE;
+      val->change = VISIBLE_CHANGE;
+
+      for (cur = val->next; cur; cur = cur->next)
+       {
+         cur->change = VISIBLE_CHANGE;
+         if (cur->value)
+           {
+             lw_copy_widget_value_args (val, cur);
+           }
+       }
+    }
+}
+
+static void
 x_widget_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                      int dest_mask, Lisp_Object domain,
 x_widget_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                      int dest_mask, Lisp_Object domain,
-                     CONST char* type, widget_value* wv)
+                     const char* type, widget_value* wv)
 {
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii), pixel;
   struct device* d = XDEVICE (device);
   Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii), pixel;
   struct device* d = XDEVICE (device);
-  Lisp_Object frame = FW_FRAME (domain);
+  Lisp_Object frame = DOMAIN_FRAME (domain);
   struct frame* f = XFRAME (frame);
   struct frame* f = XFRAME (frame);
-  XColor fcolor, bcolor;
-  Extbyte* nm=0;
+  char* nm=0;
   Widget wid;
   Arg al [32];
   int ac = 0;
   int id = new_lwlib_id ();
   Widget wid;
   Arg al [32];
   int ac = 0;
   int id = new_lwlib_id ();
+  widget_value* clip_wv;
+  XColor fcolor, bcolor;
 
   if (!DEVICE_X_P (d))
 
   if (!DEVICE_X_P (d))
-    signal_simple_error ("Not an mswindows device", device);
+    signal_simple_error ("Not an X device", device);
 
   /* have to set the type this late in case there is no device
      instantiation for a widget. But we can go ahead and do it without
 
   /* have to set the type this late in case there is no device
      instantiation for a widget. But we can go ahead and do it without
@@ -2168,93 +2481,107 @@ x_widget_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
   IMAGE_INSTANCE_TYPE (ii) = IMAGE_WIDGET;
 
   if (!NILP (IMAGE_INSTANCE_WIDGET_TEXT (ii)))
   IMAGE_INSTANCE_TYPE (ii) = IMAGE_WIDGET;
 
   if (!NILP (IMAGE_INSTANCE_WIDGET_TEXT (ii)))
-    GET_C_STRING_OS_DATA_ALLOCA (IMAGE_INSTANCE_WIDGET_TEXT (ii), nm);
+    LISP_STRING_TO_EXTERNAL (IMAGE_INSTANCE_WIDGET_TEXT (ii), nm, Qnative);
 
   ii->data = xnew_and_zero (struct x_subwindow_data);
 
 
   ii->data = xnew_and_zero (struct x_subwindow_data);
 
+  /* Create a clip window to contain the subwidget. Incredibly the
+     XEmacs manager seems to be the most appropriate widget for
+     this. Nothing else is simple enough and yet does what is
+     required. */
+  clip_wv = xmalloc_widget_value ();
+
+  lw_add_widget_value_arg (clip_wv, XtNresize, False);
+  lw_add_widget_value_arg (clip_wv, XtNwidth,
+                          (Dimension)IMAGE_INSTANCE_WIDTH (ii));
+  lw_add_widget_value_arg (clip_wv, XtNheight,
+                          (Dimension)IMAGE_INSTANCE_HEIGHT (ii));
+  clip_wv->enabled = True;
+
+  clip_wv->name = xstrdup ("clip-window");
+  clip_wv->value = xstrdup ("clip-window");
+
+  IMAGE_INSTANCE_X_CLIPWIDGET (ii)
+    = lw_create_widget ("clip-window", "clip-window", new_lwlib_id (),
+                       clip_wv, FRAME_X_CONTAINER_WIDGET (f),
+                       False, 0, 0, 0);
+
+  free_widget_value_tree (clip_wv);
+
   /* copy any args we were given */
   /* copy any args we were given */
-  if (wv->nargs)
-    lw_add_value_args_to_args (wv, al, &ac);
+  ac = 0;
+  lw_add_value_args_to_args (wv, al, &ac);
 
 
-  /* add our own arguments */
-  pixel = FACE_FOREGROUND 
+  /* Fixup the colors. We have to do this *before* the widget gets
+     created so that Motif will fix up the shadow colors
+     correctly. Once the widget is created Motif won't do this
+     anymore...*/
+  pixel = FACE_FOREGROUND
     (IMAGE_INSTANCE_WIDGET_FACE (ii),
     (IMAGE_INSTANCE_WIDGET_FACE (ii),
-     IMAGE_INSTANCE_SUBWINDOW_FRAME (ii));
+     IMAGE_INSTANCE_FRAME (ii));
   fcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
   fcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
+
   pixel = FACE_BACKGROUND
     (IMAGE_INSTANCE_WIDGET_FACE (ii),
   pixel = FACE_BACKGROUND
     (IMAGE_INSTANCE_WIDGET_FACE (ii),
-     IMAGE_INSTANCE_SUBWINDOW_FRAME (ii));
+     IMAGE_INSTANCE_FRAME (ii));
   bcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
 
   bcolor = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (pixel));
 
-  XtSetArg (al [ac], XtNbackground, bcolor.pixel);             ac++;
-  XtSetArg (al [ac], XtNforeground, fcolor.pixel);             ac++;
-  XtSetArg (al [ac], XtNfont, (void*)FONT_INSTANCE_X_FONT 
-           (XFONT_INSTANCE (widget_face_font_info 
-                            (domain, 
-                             IMAGE_INSTANCE_WIDGET_FACE (ii),
-                             0, 0))));                 ac++;
-
-  wv->nargs = ac;
-  wv->args = al;
-  
-  wid = lw_create_widget (type, wv->name, id, wv, FRAME_X_CONTAINER_WIDGET (f),
+  lw_add_widget_value_arg (wv, XtNbackground, bcolor.pixel);
+  lw_add_widget_value_arg (wv, XtNforeground, fcolor.pixel);
+  /* we cannot allow widgets to resize themselves */
+  lw_add_widget_value_arg (wv, XtNresize, False);
+  lw_add_widget_value_arg (wv, XtNwidth,
+                          (Dimension)IMAGE_INSTANCE_WIDTH (ii));
+  lw_add_widget_value_arg (wv, XtNheight,
+                          (Dimension)IMAGE_INSTANCE_HEIGHT (ii));
+  /* update the font. */
+  update_widget_face (wv, ii, domain);
+
+  wid = lw_create_widget (type, wv->name, id, wv, IMAGE_INSTANCE_X_CLIPWIDGET (ii),
                          False, 0, popup_selection_callback, 0);
 
                          False, 0, popup_selection_callback, 0);
 
+  IMAGE_INSTANCE_SUBWINDOW_ID (ii) = (void*)wid;
   IMAGE_INSTANCE_X_WIDGET_LWID (ii) = id;
   IMAGE_INSTANCE_X_WIDGET_LWID (ii) = id;
-
   /* because the EmacsManager is the widgets parent we have to
      offset the redisplay of the widget by the amount the text
      widget is inside the manager. */
   ac = 0;
   /* because the EmacsManager is the widgets parent we have to
      offset the redisplay of the widget by the amount the text
      widget is inside the manager. */
   ac = 0;
-  XtSetArg (al [ac], XtNwidth, 
-           (Dimension)IMAGE_INSTANCE_SUBWINDOW_WIDTH (ii)); ac++;
-  XtSetArg (al [ac], XtNheight, 
-           (Dimension)IMAGE_INSTANCE_SUBWINDOW_HEIGHT (ii)); ac++;
-  XtSetValues (wid, al, ac);
-  /* finally get offsets in the frame */
-  ac = 0;
   XtSetArg (al [ac], XtNx, &IMAGE_INSTANCE_X_WIDGET_XOFFSET (ii)); ac++;
   XtSetArg (al [ac], XtNy, &IMAGE_INSTANCE_X_WIDGET_YOFFSET (ii)); ac++;
   XtGetValues (FRAME_X_TEXT_WIDGET (f), al, ac);
 
   XtSetArg (al [ac], XtNx, &IMAGE_INSTANCE_X_WIDGET_XOFFSET (ii)); ac++;
   XtSetArg (al [ac], XtNy, &IMAGE_INSTANCE_X_WIDGET_YOFFSET (ii)); ac++;
   XtGetValues (FRAME_X_TEXT_WIDGET (f), al, ac);
 
-  IMAGE_INSTANCE_SUBWINDOW_ID (ii) = (void*)wid; 
+  XtSetMappedWhenManaged (wid, TRUE);
 
 
-  free_widget_value (wv);
-}
-
-static Lisp_Object
-x_widget_set_property (Lisp_Object image_instance, Lisp_Object prop,
-                      Lisp_Object val)
-{
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
-
-  if (EQ (prop, Q_text))
-    {
-      Extbyte* str=0;
-      widget_value* wv = lw_get_all_values (IMAGE_INSTANCE_X_WIDGET_LWID (ii));
-      CHECK_STRING (val);
-      GET_C_STRING_OS_DATA_ALLOCA (val, str);
-      wv->value = str;
-      lw_modify_all_widgets (IMAGE_INSTANCE_X_WIDGET_LWID (ii), wv, False);
-      return Qt;
-    }
-  return Qunbound;
+  free_widget_value_tree (wv);
+  /* A kludgy but simple way to make sure the callback for a widget
+     doesn't get deleted. */
+  gcpro_popup_callbacks (id);
 }
 
 /* get properties of a control */
 static Lisp_Object
 x_widget_property (Lisp_Object image_instance, Lisp_Object prop)
 {
 }
 
 /* get properties of a control */
 static Lisp_Object
 x_widget_property (Lisp_Object image_instance, Lisp_Object prop)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   /* get the text from a control */
   if (EQ (prop, Q_text))
     {
       widget_value* wv = lw_get_all_values (IMAGE_INSTANCE_X_WIDGET_LWID (ii));
   /* get the text from a control */
   if (EQ (prop, Q_text))
     {
       widget_value* wv = lw_get_all_values (IMAGE_INSTANCE_X_WIDGET_LWID (ii));
-      return build_ext_string (wv->value, FORMAT_OS);
+      return build_ext_string (wv->value, Qnative);
     }
   return Qunbound;
 }
 
     }
   return Qunbound;
 }
 
+/* Instantiate a layout control for putting other widgets in. */
+static void
+x_native_layout_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
+                            Lisp_Object pointer_fg, Lisp_Object pointer_bg,
+                            int dest_mask, Lisp_Object domain)
+{
+  x_widget_instantiate (image_instance, instantiator, pointer_fg,
+                       pointer_bg, dest_mask, domain, "layout", 0);
+}
+
 /* Instantiate a button widget. Unfortunately instantiated widgets are
    particular to a frame since they need to have a parent. It's not
    like images where you just select the image into the context you
 /* Instantiate a button widget. Unfortunately instantiated widgets are
    particular to a frame since they need to have a parent. It's not
    like images where you just select the image into the context you
@@ -2266,12 +2593,10 @@ x_button_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                      int dest_mask, Lisp_Object domain)
 {
                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                      int dest_mask, Lisp_Object domain)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
   Lisp_Object glyph = find_keyword_in_vector (instantiator, Q_image);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
   Lisp_Object glyph = find_keyword_in_vector (instantiator, Q_image);
-  widget_value* wv = xmalloc_widget_value ();
-
-  button_item_to_widget_value (gui, wv, 1, 1);
+  widget_value* wv = gui_items_to_widget_values (image_instance, gui, 1);
 
   if (!NILP (glyph))
     {
 
   if (!NILP (glyph))
     {
@@ -2283,11 +2608,12 @@ x_button_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                        pointer_bg, dest_mask, domain, "button", wv);
 
   /* add the image if one was given */
                        pointer_bg, dest_mask, domain, "button", wv);
 
   /* add the image if one was given */
-  if (!NILP (glyph) && IMAGE_INSTANCEP (glyph))
+  if (!NILP (glyph) && IMAGE_INSTANCEP (glyph)
+      && IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (glyph)))
     {
       Arg al [2];
       int ac =0;
     {
       Arg al [2];
       int ac =0;
-#ifdef LWLIB_USES_MOTIF
+#ifdef LWLIB_WIDGETS_MOTIF
       XtSetArg (al [ac], XmNlabelType, XmPIXMAP);      ac++;
       XtSetArg (al [ac], XmNlabelPixmap, XIMAGE_INSTANCE_X_PIXMAP (glyph));ac++;
 #else
       XtSetArg (al [ac], XmNlabelType, XmPIXMAP);      ac++;
       XtSetArg (al [ac], XmNlabelPixmap, XIMAGE_INSTANCE_X_PIXMAP (glyph));ac++;
 #else
@@ -2297,11 +2623,35 @@ x_button_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
     }
 }
 
     }
 }
 
+/* Update a button's clicked state.
+
+   #### This is overkill, but it works. Right now this causes all
+   button instances to flash for some reason buried deep in lwlib. In
+   theory this should be the Right Thing to do since lwlib should only
+   merge in changed values - and if nothing has changed then nothing
+   should get done. This may be because of the args stuff,
+   i.e. although the arg contents may be the same the args look
+   different and so are re-applied to the widget. */
+static void
+x_button_redisplay (Lisp_Object image_instance)
+{
+  /* This function can GC if IN_REDISPLAY is false. */
+  Lisp_Image_Instance *p = XIMAGE_INSTANCE (image_instance);
+  widget_value* wv =
+    gui_items_to_widget_values (image_instance,
+                               IMAGE_INSTANCE_WIDGET_ITEMS (p), 1);
+
+  /* now modify the widget */
+  lw_modify_all_widgets (IMAGE_INSTANCE_X_WIDGET_LWID (p),
+                        wv, True);
+  free_widget_value_tree (wv);
+}
+
 /* get properties of a button */
 static Lisp_Object
 x_button_property (Lisp_Object image_instance, Lisp_Object prop)
 {
 /* get properties of a button */
 static Lisp_Object
 x_button_property (Lisp_Object image_instance, Lisp_Object prop)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   /* check the state of a button */
   if (EQ (prop, Q_selected))
     {
   /* check the state of a button */
   if (EQ (prop, Q_selected))
     {
@@ -2321,32 +2671,31 @@ x_progress_gauge_instantiate (Lisp_Object image_instance, Lisp_Object instantiat
                        Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                        int dest_mask, Lisp_Object domain)
 {
                        Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                        int dest_mask, Lisp_Object domain)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
-  widget_value* wv = xmalloc_widget_value ();
-
-  button_item_to_widget_value (gui, wv, 1, 1);
+  widget_value* wv = gui_items_to_widget_values (image_instance, gui, 0);
 
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "progress", wv);
 }
 
 
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "progress", wv);
 }
 
-/* set the properties of a progres guage */
-static Lisp_Object
-x_progress_gauge_set_property (Lisp_Object image_instance, Lisp_Object prop,
-                        Lisp_Object val)
+/* set the properties of a progress gauge */
+static void
+x_progress_gauge_redisplay (Lisp_Object image_instance)
 {
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
 
 
-  if (EQ (prop, Q_percent))
+  if (IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (ii))
     {
       Arg al [1];
     {
       Arg al [1];
-      CHECK_INT (val);
+      Lisp_Object val;
+#ifdef ERROR_CHECK_GLYPHS
+      assert (GUI_ITEMP (IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii)));
+#endif
+      val = XGUI_ITEM (IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii))->value;
       XtSetArg (al[0], XtNvalue, XINT (val));
       XtSetValues (IMAGE_INSTANCE_X_WIDGET_ID (ii), al, 1);
       XtSetArg (al[0], XtNvalue, XINT (val));
       XtSetValues (IMAGE_INSTANCE_X_WIDGET_ID (ii), al, 1);
-      return Qt;
     }
     }
-  return Qunbound;
 }
 
 /* instantiate an edit control */
 }
 
 /* instantiate an edit control */
@@ -2355,45 +2704,156 @@ x_edit_field_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                    Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                    int dest_mask, Lisp_Object domain)
 {
                    Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                    int dest_mask, Lisp_Object domain)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
   Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
-  widget_value* wv = xmalloc_widget_value ();
-  
-  button_item_to_widget_value (gui, wv, 1, 1);
-  
+  widget_value* wv = gui_items_to_widget_values (image_instance, gui, 0);
+
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "text-field", wv);
 }
 
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "text-field", wv);
 }
 
+#if defined (LWLIB_WIDGETS_MOTIF) && XmVERSION > 1
 /* instantiate a combo control */
 static void
 x_combo_box_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                     Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                     int dest_mask, Lisp_Object domain)
 {
 /* instantiate a combo control */
 static void
 x_combo_box_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
                     Lisp_Object pointer_fg, Lisp_Object pointer_bg,
                     int dest_mask, Lisp_Object domain)
 {
-  struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
-  Lisp_Object rest;
-  Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
-  widget_value* wv = xmalloc_widget_value ();
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  widget_value * wv = 0;
+  /* This is not done generically because of sizing problems under
+     mswindows. */
+  widget_instantiate (image_instance, instantiator, pointer_fg,
+                     pointer_bg, dest_mask, domain);
+
+  wv = gui_items_to_widget_values (image_instance,
+                                  IMAGE_INSTANCE_WIDGET_ITEMS (ii), 0);
 
 
-  button_item_to_widget_value (gui, wv, 1, 1);
-  
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "combo-box", wv);
   x_widget_instantiate (image_instance, instantiator, pointer_fg,
                        pointer_bg, dest_mask, domain, "combo-box", wv);
-  /* add items to the combo box */
-  LIST_LOOP (rest, Fplist_get (IMAGE_INSTANCE_WIDGET_PROPS (ii), Q_items, Qnil))
-    {
-#if 0
-      Extbyte* str;
-      XmString xmstr;
-      GET_C_STRING_OS_DATA_ALLOCA (XCAR (rest), str);
-      xmstr = XmStringCreate (str, XmSTRING_DEFAULT_CHARSET);
-      XmListAddItem (IMAGE_INSTANCE_X_WIDGET_ID (ii), xmstr, 0);
-      XmStringFree (xmstr);
+}
 #endif
 #endif
+
+static void
+x_tab_control_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
+                          Lisp_Object pointer_fg, Lisp_Object pointer_bg,
+                          int dest_mask, Lisp_Object domain)
+{
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  widget_value * wv =
+    gui_items_to_widget_values (image_instance,
+                               IMAGE_INSTANCE_WIDGET_ITEMS (ii), 0);
+  update_tab_widget_face (wv, ii,
+                         IMAGE_INSTANCE_FRAME (ii));
+  x_widget_instantiate (image_instance, instantiator, pointer_fg,
+                       pointer_bg, dest_mask, domain, "tab-control", wv);
+}
+
+/* Set the properties of a tab control */
+static void
+x_tab_control_redisplay (Lisp_Object image_instance)
+{
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+
+  if (IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (ii)
+      ||
+      IMAGE_INSTANCE_WIDGET_ACTION_OCCURRED (ii))
+    {
+      /* If only the order has changed then simply select the first
+        one of the pending set. This stops horrendous rebuilding -
+        and hence flicker - of the tabs each time you click on
+        one. */
+      if (tab_control_order_only_changed (image_instance))
+       {
+         Lisp_Object rest, selected =
+           gui_item_list_find_selected
+           (NILP (IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii)) ?
+            XCDR (IMAGE_INSTANCE_WIDGET_ITEMS (ii)) :
+            XCDR (IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii)));
+
+         LIST_LOOP (rest, XCDR (IMAGE_INSTANCE_WIDGET_ITEMS (ii)))
+           {
+             if (gui_item_equal_sans_selected (XCAR (rest), selected, 0))
+               {
+                 /* There may be an encapsulated way of doing this,
+                    but I couldn't find it. */
+                 Lisp_Object old_selected =gui_item_list_find_selected
+                   (XCDR (IMAGE_INSTANCE_WIDGET_ITEMS (ii)));
+                 Arg al [2];
+                 char* name;
+                 unsigned int num_children, i;
+                 Widget* children;
+
+                 LISP_STRING_TO_EXTERNAL (XGUI_ITEM (XCAR (rest))->name,
+                                          name, Qnative);
+                 /* The name may contain a `.' which confuses
+                    XtNameToWidget, so we do it ourselves. */
+                 children = XtCompositeChildren (IMAGE_INSTANCE_X_WIDGET_ID (ii),
+                                                 &num_children);
+                 for (i = 0; i < num_children; i++)
+                   {
+                     if (!strcmp (XtName (children [i]), name))
+                       {
+                         XtSetArg (al [0], XtNtopWidget, children [i]);
+                         XtSetArg (al [1], XtNhighlightWidget,
+                                   children [i]);
+                         XtSetValues (IMAGE_INSTANCE_X_WIDGET_ID (ii), al, 2);
+                         break;
+                       }
+                   }
+                 /* Pick up the new selected item. */
+                 XGUI_ITEM (old_selected)->selected =
+                   XGUI_ITEM (XCAR (rest))->selected;
+                 XGUI_ITEM (XCAR (rest))->selected =
+                   XGUI_ITEM (selected)->selected;
+                 /* We're not actually changing the items anymore. */
+                 IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (ii) = 0;
+                 IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii) = Qnil;
+                 break;
+               }
+           }
+       }
+    }
+  /* Possibly update the face. */
+  if (IMAGE_INSTANCE_WIDGET_FACE_CHANGED (ii)
+      ||
+      XFRAME (IMAGE_INSTANCE_FRAME (ii))->faces_changed
+      ||
+      IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (ii))
+    {
+      /* See previous comments on the brokeness of lwlib.
+
+        #### There's actually not much point in doing this here
+        since, colors will have been set appropriately by
+        x_redisplay_widget. */
+      widget_value* wv =copy_widget_value_tree
+       (lw_get_all_values
+        (IMAGE_INSTANCE_X_WIDGET_LWID (ii)),
+        NO_CHANGE);
+
+      update_tab_widget_face (wv, ii,
+                             IMAGE_INSTANCE_FRAME (ii));
+
+      lw_modify_all_widgets (IMAGE_INSTANCE_X_WIDGET_LWID (ii), wv, True);
+      free_widget_value_tree (wv);
     }
 }
 
     }
 }
 
+/* instantiate a static control possible for putting other things in */
+static void
+x_label_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
+                    Lisp_Object pointer_fg, Lisp_Object pointer_bg,
+                    int dest_mask, Lisp_Object domain)
+{
+  Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+  Lisp_Object gui = IMAGE_INSTANCE_WIDGET_ITEM (ii);
+  widget_value* wv = gui_items_to_widget_values (image_instance, gui, 0);
+
+  x_widget_instantiate (image_instance, instantiator, pointer_fg,
+                       pointer_bg, dest_mask, domain, "button", wv);
+}
+#endif /* HAVE_WIDGETS */
+
 \f
 /************************************************************************/
 /*                            initialization                            */
 \f
 /************************************************************************/
 /*                            initialization                            */
@@ -2421,8 +2881,8 @@ console_type_create_glyphs_x (void)
   CONSOLE_HAS_METHOD (x, locate_pixmap_file);
   CONSOLE_HAS_METHOD (x, unmap_subwindow);
   CONSOLE_HAS_METHOD (x, map_subwindow);
   CONSOLE_HAS_METHOD (x, locate_pixmap_file);
   CONSOLE_HAS_METHOD (x, unmap_subwindow);
   CONSOLE_HAS_METHOD (x, map_subwindow);
-  CONSOLE_HAS_METHOD (x, resize_subwindow);
-  CONSOLE_HAS_METHOD (x, update_subwindow);
+  CONSOLE_HAS_METHOD (x, redisplay_widget);
+  CONSOLE_HAS_METHOD (x, redisplay_subwindow);
 }
 
 void
 }
 
 void
@@ -2430,6 +2890,9 @@ image_instantiator_format_create_glyphs_x (void)
 {
   IIFORMAT_VALID_CONSOLE (x, nothing);
   IIFORMAT_VALID_CONSOLE (x, string);
 {
   IIFORMAT_VALID_CONSOLE (x, nothing);
   IIFORMAT_VALID_CONSOLE (x, string);
+#ifdef HAVE_WIDGETS
+  IIFORMAT_VALID_CONSOLE (x, layout);
+#endif
   IIFORMAT_VALID_CONSOLE (x, formatted_string);
   IIFORMAT_VALID_CONSOLE (x, inherit);
 #ifdef HAVE_XPM
   IIFORMAT_VALID_CONSOLE (x, formatted_string);
   IIFORMAT_VALID_CONSOLE (x, inherit);
 #ifdef HAVE_XPM
@@ -2441,38 +2904,51 @@ image_instantiator_format_create_glyphs_x (void)
 #endif
 #ifdef HAVE_TIFF
   IIFORMAT_VALID_CONSOLE (x, tiff);
 #endif
 #ifdef HAVE_TIFF
   IIFORMAT_VALID_CONSOLE (x, tiff);
-#endif  
+#endif
 #ifdef HAVE_PNG
   IIFORMAT_VALID_CONSOLE (x, png);
 #ifdef HAVE_PNG
   IIFORMAT_VALID_CONSOLE (x, png);
-#endif  
+#endif
 #ifdef HAVE_GIF
   IIFORMAT_VALID_CONSOLE (x, gif);
 #ifdef HAVE_GIF
   IIFORMAT_VALID_CONSOLE (x, gif);
-#endif  
+#endif
   INITIALIZE_DEVICE_IIFORMAT (x, xbm);
   IIFORMAT_HAS_DEVMETHOD (x, xbm, instantiate);
 
   INITIALIZE_DEVICE_IIFORMAT (x, subwindow);
   IIFORMAT_HAS_DEVMETHOD (x, subwindow, instantiate);
   INITIALIZE_DEVICE_IIFORMAT (x, xbm);
   IIFORMAT_HAS_DEVMETHOD (x, xbm, instantiate);
 
   INITIALIZE_DEVICE_IIFORMAT (x, subwindow);
   IIFORMAT_HAS_DEVMETHOD (x, subwindow, instantiate);
-
+#ifdef HAVE_WIDGETS
+  /* layout widget */
+  INITIALIZE_DEVICE_IIFORMAT (x, native_layout);
+  IIFORMAT_HAS_DEVMETHOD (x, native_layout, instantiate);
   /* button widget */
   INITIALIZE_DEVICE_IIFORMAT (x, button);
   IIFORMAT_HAS_DEVMETHOD (x, button, property);
   IIFORMAT_HAS_DEVMETHOD (x, button, instantiate);
   /* button widget */
   INITIALIZE_DEVICE_IIFORMAT (x, button);
   IIFORMAT_HAS_DEVMETHOD (x, button, property);
   IIFORMAT_HAS_DEVMETHOD (x, button, instantiate);
-
+  IIFORMAT_HAS_DEVMETHOD (x, button, redisplay);
+  /* general widget methods. */
   INITIALIZE_DEVICE_IIFORMAT (x, widget);
   IIFORMAT_HAS_DEVMETHOD (x, widget, property);
   INITIALIZE_DEVICE_IIFORMAT (x, widget);
   IIFORMAT_HAS_DEVMETHOD (x, widget, property);
-  IIFORMAT_HAS_DEVMETHOD (x, widget, set_property);
   /* progress gauge */
   INITIALIZE_DEVICE_IIFORMAT (x, progress_gauge);
   /* progress gauge */
   INITIALIZE_DEVICE_IIFORMAT (x, progress_gauge);
-  IIFORMAT_HAS_DEVMETHOD (x, progress_gauge, set_property);
+  IIFORMAT_HAS_DEVMETHOD (x, progress_gauge, redisplay);
   IIFORMAT_HAS_DEVMETHOD (x, progress_gauge, instantiate);
   /* text field */
   INITIALIZE_DEVICE_IIFORMAT (x, edit_field);
   IIFORMAT_HAS_DEVMETHOD (x, edit_field, instantiate);
   IIFORMAT_HAS_DEVMETHOD (x, progress_gauge, instantiate);
   /* text field */
   INITIALIZE_DEVICE_IIFORMAT (x, edit_field);
   IIFORMAT_HAS_DEVMETHOD (x, edit_field, instantiate);
+#if defined (LWLIB_WIDGETS_MOTIF) && XmVERSION > 1
   /* combo box */
   INITIALIZE_DEVICE_IIFORMAT (x, combo_box);
   IIFORMAT_HAS_DEVMETHOD (x, combo_box, instantiate);
   /* combo box */
   INITIALIZE_DEVICE_IIFORMAT (x, combo_box);
   IIFORMAT_HAS_DEVMETHOD (x, combo_box, instantiate);
-
+  IIFORMAT_HAS_SHARED_DEVMETHOD (x, combo_box, redisplay, tab_control);
+#endif
+  /* tab control widget */
+  INITIALIZE_DEVICE_IIFORMAT (x, tab_control);
+  IIFORMAT_HAS_DEVMETHOD (x, tab_control, instantiate);
+  IIFORMAT_HAS_DEVMETHOD (x, tab_control, redisplay);
+  /* label */
+  INITIALIZE_DEVICE_IIFORMAT (x, label);
+  IIFORMAT_HAS_DEVMETHOD (x, label, instantiate);
+#endif
   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (cursor_font, "cursor-font");
   IIFORMAT_VALID_CONSOLE (x, cursor_font);
 
   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (cursor_font, "cursor-font");
   IIFORMAT_VALID_CONSOLE (x, cursor_font);
 
@@ -2506,6 +2982,17 @@ image_instantiator_format_create_glyphs_x (void)
   IIFORMAT_HAS_METHOD (autodetect, validate);
   IIFORMAT_HAS_METHOD (autodetect, normalize);
   IIFORMAT_HAS_METHOD (autodetect, possible_dest_types);
   IIFORMAT_HAS_METHOD (autodetect, validate);
   IIFORMAT_HAS_METHOD (autodetect, normalize);
   IIFORMAT_HAS_METHOD (autodetect, possible_dest_types);
+  /* #### autodetect is flawed IMO: 
+  1. It makes the assumption that you can detect whether the user
+  wanted a cursor or a string based on the data, since the data is a
+  string you have to prioritise cursors. Instead we will force users
+  to pick the appropriate image type, this is what we do under
+  MS-Windows anyway.
+  2. It doesn't fit with the new domain model - you cannot tell which
+  domain it needs to be instantiated in until you've actually
+  instantiated it, which mucks up caching.
+  3. It only copes with cursors and strings which seems bogus. */
+  IIFORMAT_HAS_SHARED_METHOD (autodetect, governing_domain, subwindow);
   IIFORMAT_HAS_METHOD (autodetect, instantiate);
   IIFORMAT_VALID_CONSOLE (x, autodetect);
 
   IIFORMAT_HAS_METHOD (autodetect, instantiate);
   IIFORMAT_VALID_CONSOLE (x, autodetect);
 
@@ -2533,9 +3020,9 @@ complex_vars_of_glyphs_x (void)
      vector3 (Qxbm, Q_data,                                    \
              list3 (make_int (name##_width),                   \
                     make_int (name##_height),                  \
      vector3 (Qxbm, Q_data,                                    \
              list3 (make_int (name##_width),                   \
                     make_int (name##_height),                  \
-                    make_ext_string (name##_bits,              \
+                    make_ext_string ((Extbyte *) name##_bits,  \
                                      sizeof (name##_bits),     \
                                      sizeof (name##_bits),     \
-                                     FORMAT_BINARY))),         \
+                                     Qbinary))),               \
      Qglobal, Qx, Qnil)
 
   BUILD_GLYPH_INST (Vtruncation_glyph, truncator);
      Qglobal, Qx, Qnil)
 
   BUILD_GLYPH_INST (Vtruncation_glyph, truncator);