This commit was generated by cvs2svn to compensate for changes in r4969,
[chise/xemacs-chise.git.1] / src / redisplay-msw.c
index b9288e7..17394f4 100644 (file)
@@ -41,7 +41,6 @@ Boston, MA 02111-1307, USA.  */
 #include "faces.h"
 #include "frame.h"
 #include "glyphs-msw.h"
-#include "gutter.h"
 #include "redisplay.h"
 #include "sysdep.h"
 #include "window.h"
@@ -55,17 +54,29 @@ Boston, MA 02111-1307, USA.  */
 #define MSWINDOWS_EOL_CURSOR_WIDTH     5
 
 /*
- * Random forward declarations
+ * Random forward delarations
  */
 static void mswindows_update_dc (HDC hdc, Lisp_Object font, Lisp_Object fg,
                                 Lisp_Object bg, Lisp_Object bg_pmap);
+static void mswindows_clear_region (Lisp_Object locale, face_index findex,
+                             int x, int y, int width, int height);
 static void mswindows_output_vertical_divider (struct window *w, int clear);
 static void mswindows_redraw_exposed_windows (Lisp_Object window, int x,
                                        int y, int width, int height);
 static void mswindows_output_dibitmap (struct frame *f, 
                                       struct Lisp_Image_Instance *p,
-                                      struct display_box* db,
-                                      struct display_glyph_area* dga);
+                                      int x, int y, 
+                                      int clip_x, int clip_y, 
+                                      int clip_width, int clip_height, 
+                                      int width, int height,
+                                      int pixmap_offset,
+                                      int offset_bitmap);
+static void mswindows_output_pixmap (struct window *w, struct display_line *dl,
+                                    Lisp_Object image_instance, int xpos,
+                                    int xoffset, int start_pixpos, int width,
+                                    face_index findex, int cursor_start, 
+                                    int cursor_width, int cursor_height,
+                                    int offset_bitmap);
 
 typedef struct textual_run
 {
@@ -156,7 +167,7 @@ separate_textual_runs (unsigned char *text_storage,
          char_converter.reg[1] = byte1;
          char_converter.reg[2] = byte2;
          char_converter.ic = 0; /* start at beginning each time */
-         ccl_driver (&char_converter, 0, 0, 0, 0, CCL_MODE_ENCODING);
+         ccl_driver (&char_converter, 0, 0, 0, 0);
          byte1 = char_converter.reg[1];
          byte2 = char_converter.reg[2];
        }
@@ -293,37 +304,28 @@ mswindows_output_hline (struct window *w, struct display_line *dl, struct rune *
  of its face.
  ****************************************************************************/
 static void
-mswindows_output_blank (struct window *w, struct display_line *dl, 
-                       struct rune *rb, int start_pixpos)
+mswindows_output_blank (struct window *w, struct display_line *dl, struct rune *rb, int start_pixpos)
 {
   struct frame *f = XFRAME (w->frame);
-  RECT rect = { rb->xpos, DISPLAY_LINE_YPOS (dl),
-               rb->xpos+rb->width, 
-               DISPLAY_LINE_YEND (dl) };
+  RECT rect = { rb->xpos, dl->ypos-dl->ascent,
+               rb->xpos+rb->width, dl->ypos+dl->descent-dl->clip };
   struct face_cachel *cachel = WINDOW_FACE_CACHEL (w, rb->findex);
 
   Lisp_Object bg_pmap = WINDOW_FACE_CACHEL_BACKGROUND_PIXMAP (w, rb->findex);
-
-  /* Unmap all subwindows in the area we are going to blank. */
-  redisplay_unmap_subwindows_maybe (f, rb->xpos, DISPLAY_LINE_YPOS (dl),
-                                   rb->width, DISPLAY_LINE_HEIGHT (dl));
-
+  
   if (!IMAGE_INSTANCEP (bg_pmap)
       || !IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (bg_pmap)))
     bg_pmap = Qnil;
 
   if (!NILP(bg_pmap))
     {
-      struct display_box db;
-      struct display_glyph_area dga;
-      redisplay_calculate_display_boxes (dl, rb->xpos, 
-                                        /*rb->object.dglyph.xoffset*/ 0,
-                                        start_pixpos, rb->width,
-                                        &db, &dga);
       /* blank the background in the appropriate color */
       mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, cachel->foreground,
                           cachel->background, Qnil);
-      redisplay_output_pixmap (w, bg_pmap, &db, &dga, rb->findex,
+
+      mswindows_output_pixmap (w, dl, bg_pmap, 
+                              rb->xpos, 0 /*rb->object.dglyph.xoffset*/,
+                              start_pixpos, rb->width, rb->findex,
                               0, 0, 0, TRUE);
     }
   else 
@@ -349,7 +351,7 @@ mswindows_output_cursor (struct window *w, struct display_line *dl, int xpos,
 {
   struct frame *f = XFRAME (w->frame);
   struct device *d = XDEVICE (f->device);
-  struct face_cachel *cachel=0;
+  struct face_cachel *cachel;
   Lisp_Object font = Qnil;
   int focus = EQ (w->frame, DEVICE_FRAME_WITH_FOCUS_REAL (d));
   HDC hdc = FRAME_MSWINDOWS_DC (f);
@@ -357,19 +359,15 @@ mswindows_output_cursor (struct window *w, struct display_line *dl, int xpos,
   char *p_char = NULL;
   int n_char = 0;
   RECT rect = { xpos,
-               DISPLAY_LINE_YPOS (dl),
+               dl->ypos - dl->ascent,
                xpos + width,
-               DISPLAY_LINE_YEND (dl) };
+               dl->ypos + dl->descent - dl->clip};
   Lisp_Object bar = symbol_value_in_buffer (Qbar_cursor,
                                            WINDOW_BUFFER (w));
   int bar_p = image_p || !NILP (bar);
   int cursor_p = !NILP (w->text_cursor_visible_p);
   int real_char_p = ch != 0;
 
-  /* Unmap all subwindows in the area we are going to blank. */
-  redisplay_unmap_subwindows_maybe (f, xpos, DISPLAY_LINE_YPOS (dl),
-                                   width, DISPLAY_LINE_HEIGHT (dl));
-
   if (real_char_p)
     {
       /* Use the font from the underlying character */
@@ -468,10 +466,8 @@ mswindows_output_cursor (struct window *w, struct display_line *dl, int xpos,
  ****************************************************************************/
 void
 mswindows_output_string (struct window *w, struct display_line *dl,
-                        Emchar_dynarr *buf, int xpos, int xoffset, int clip_start,
-                        int width, face_index findex,
-                        int cursor, int cursor_start, int cursor_width,
-                        int cursor_height)
+                  Emchar_dynarr *buf, int xpos, int xoffset, int clip_start,
+                  int width, face_index findex)
 {
   struct frame *f = XFRAME (w->frame);
   /* struct device *d = XDEVICE (f->device);*/
@@ -508,15 +504,11 @@ mswindows_output_string (struct window *w, struct display_line *dl,
   xpos -= xoffset;
 
   /* sort out the destination rectangle */
-  height = DISPLAY_LINE_HEIGHT (dl);
+  height = dl->ascent + dl->descent - dl->clip;
   rect.left = clip_start;
-  rect.top  = DISPLAY_LINE_YPOS (dl);
+  rect.top  = dl->ypos - dl->ascent;
   rect.right = clip_end;
-  rect.bottom = rect.top + height;
-
-  /* make sure the area we are about to display is subwindow free. */
-  redisplay_unmap_subwindows_maybe (f, clip_start, DISPLAY_LINE_YPOS (dl),
-                                   clip_end - clip_start, DISPLAY_LINE_HEIGHT (dl));
+  rect.bottom = height + dl->ypos - dl->ascent;
 
   /* output the background pixmap if there is one */
   bg_pmap = cachel->background_pixmap;
@@ -526,14 +518,13 @@ mswindows_output_string (struct window *w, struct display_line *dl,
 
   if (!NILP(bg_pmap))
     {
-      struct display_box db;
-      struct display_glyph_area dga;
-      redisplay_calculate_display_boxes (dl, xpos + xoffset, 0,
-                                        clip_start, width, &db, &dga);
       /* blank the background in the appropriate color */
       mswindows_update_dc (hdc, Qnil, cachel->foreground,
                           cachel->background, Qnil);
-      redisplay_output_pixmap (w, bg_pmap, &db, &dga, findex,
+
+      mswindows_output_pixmap (w, dl, bg_pmap, 
+                              xpos, xoffset,
+                              clip_start, width, findex,
                               0, 0, 0, TRUE);
       /* output pixmap calls this so we have to recall to get correct
          references */
@@ -558,14 +549,14 @@ mswindows_output_string (struct window *w, struct display_line *dl,
       this_width = mswindows_text_width_single_run (hdc, cachel, runs + i);
       
       /* cope with fonts taller than lines */
-      if ((int) fi->height < (int) (height + dl->clip + dl->top_clip))
+      if ((int) fi->height < (int) (height + dl->clip))
        {
          int clear_start = max (xpos, clip_start);
          int clear_end = min (xpos + this_width, clip_end);
          
          {
-           redisplay_clear_region (window, findex, clear_start,
-                                   DISPLAY_LINE_YPOS (dl), 
+           mswindows_clear_region (window, findex, clear_start,
+                                   dl->ypos - dl->ascent, 
                                    clear_end - clear_start,
                                    height);
            /* output pixmap calls this so we have to recall to get correct
@@ -590,12 +581,30 @@ mswindows_output_string (struct window *w, struct display_line *dl,
 
 static void
 mswindows_output_dibitmap (struct frame *f, struct Lisp_Image_Instance *p,
-                          struct display_box* db,
-                          struct display_glyph_area* dga)
+                          int x, int y, 
+                          int clip_x, int clip_y, 
+                          int clip_width, int clip_height, 
+                          int width, int height, int pixmap_offset,
+                          int offset_bitmap)
 {
   HDC hdc = FRAME_MSWINDOWS_DC (f);
   HGDIOBJ old=NULL;
   COLORREF bgcolor = GetBkColor (hdc);
+  int need_clipping = (clip_x || clip_y);
+  int yoffset=0;
+  int xoffset=0;
+  /* do we need to offset the pixmap vertically? this is necessary
+     for background pixmaps. */
+  if (offset_bitmap)
+    {
+      yoffset = y % IMAGE_INSTANCE_PIXMAP_HEIGHT (p);
+      xoffset = x % IMAGE_INSTANCE_PIXMAP_WIDTH (p);
+      /* the width is handled by mswindows_output_pixmap_region */
+    }
+
+  if (need_clipping)
+    {
+    }
 
   /* first blt the mask */
   if (IMAGE_INSTANCE_MSWINDOWS_MASK (p))
@@ -612,94 +621,93 @@ mswindows_output_dibitmap (struct frame *f, struct Lisp_Image_Instance *p,
       SetDIBColorTable (FRAME_MSWINDOWS_CDC (f), 1, 1, &col);
 
       BitBlt (hdc, 
-             db->xpos, db->ypos,
-             dga->width, dga->height, 
+             x,y,
+             width, height, 
              FRAME_MSWINDOWS_CDC (f),
-             dga->xoffset, dga->yoffset, 
+             xoffset,yoffset, 
              SRCCOPY);                  
 
       SelectObject (FRAME_MSWINDOWS_CDC (f), old);
     }
   
-  /* Now blt the bitmap itself, or one of its slices. */
+  /* now blt the bitmap itself. */
   old = SelectObject (FRAME_MSWINDOWS_CDC (f),
-                     IMAGE_INSTANCE_MSWINDOWS_BITMAP_SLICE 
-                     (p, IMAGE_INSTANCE_PIXMAP_SLICE (p)));
+                     IMAGE_INSTANCE_MSWINDOWS_BITMAP (p));
 
   BitBlt (hdc, 
-         db->xpos, db->ypos,
-         dga->width, dga->height,
+         x,y,
+         width, height, 
          FRAME_MSWINDOWS_CDC (f),
-         dga->xoffset, dga->yoffset, 
+         xoffset, yoffset, 
          IMAGE_INSTANCE_MSWINDOWS_MASK (p) ? SRCINVERT : SRCCOPY);
 
   SelectObject (FRAME_MSWINDOWS_CDC (f),old);
+
+  if (need_clipping)
+    {
+    }
 }
 
-/* X gc's have this nice property that setting the bg pixmap will
+/*
+ * X gc's have this nice property that setting the bg pixmap will
  * output it offset relative to the window. Windows doesn't have this
- * feature so we have to emulate this by outputting multiple pixmaps.
- * This is only used for background pixmaps. Normal pixmaps are
- * outputted once and are scrollable */
+ * feature so we have to emulate this by outputting multiple pixmaps 
+ */
 static void
 mswindows_output_dibitmap_region (struct frame *f, 
                                  struct Lisp_Image_Instance *p,
-                                 struct display_box *db,
-                                 struct display_glyph_area *dga)
+                                 int x, int y, 
+                                 int clip_x, int clip_y, 
+                                 int clip_width, int clip_height, 
+                                 int width, int height, int pixmap_offset,
+                                 int offset_bitmap)
 {
-  struct display_box xdb = { db->xpos, db->ypos, db->width, db->height };
-  struct display_glyph_area xdga
-    = { 0, 0, IMAGE_INSTANCE_PIXMAP_WIDTH (p),
-       IMAGE_INSTANCE_PIXMAP_HEIGHT (p) };
+  int pwidth = min (width, IMAGE_INSTANCE_PIXMAP_WIDTH (p));
+  int pheight = min (height, IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
   int pxoffset = 0, pyoffset = 0;
 
-  if (dga)
-    {  
-      xdga.width = dga->width;
-      xdga.height = dga->height;
-    }
-  else if (!redisplay_normalize_glyph_area (&xdb, &xdga))
-    return;
-
   /* when doing a bg pixmap do a partial pixmap first so that we
      blt whole pixmaps thereafter */
-  xdga.height = min (xdga.height, IMAGE_INSTANCE_PIXMAP_HEIGHT (p) -
-                     db->ypos % IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
 
-  while (xdga.height > 0)
+  if (offset_bitmap)
     {
-      xdga.width = min (min (db->width, IMAGE_INSTANCE_PIXMAP_WIDTH (p)),
+      pheight = min (pheight, IMAGE_INSTANCE_PIXMAP_HEIGHT (p) -
+                    y % IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
+    }
+  
+  while (pheight > 0)
+    {
+      if (offset_bitmap)
+       {
+         pwidth = min (min (width, IMAGE_INSTANCE_PIXMAP_WIDTH (p)),
                        IMAGE_INSTANCE_PIXMAP_WIDTH (p) -
-                       db->xpos % IMAGE_INSTANCE_PIXMAP_WIDTH (p));
-      pxoffset = 0;
-      while (xdga.width > 0)
+                       x % IMAGE_INSTANCE_PIXMAP_WIDTH (p));
+         pxoffset = 0;
+       }
+      while (pwidth > 0)
        {
-         xdb.xpos = db->xpos + pxoffset;
-         xdb.ypos = db->ypos + pyoffset;
-           /* do we need to offset the pixmap vertically? this is necessary
-              for background pixmaps. */
-         xdga.yoffset = xdb.ypos % IMAGE_INSTANCE_PIXMAP_HEIGHT (p);
-         xdga.xoffset = xdb.xpos % IMAGE_INSTANCE_PIXMAP_WIDTH (p);
-         /* the width is handled by mswindows_output_pixmap_region */
-         mswindows_output_dibitmap (f, p, &xdb, &xdga);
-         pxoffset += xdga.width;
-         xdga.width = min ((db->width - pxoffset),
-                           IMAGE_INSTANCE_PIXMAP_WIDTH (p));
+         mswindows_output_dibitmap (f, p,
+                                    x + pxoffset, y + pyoffset, 
+                                    clip_x, clip_y, 
+                                    clip_width, clip_height, 
+                                    pwidth, pheight, pixmap_offset,
+                                    offset_bitmap);
+         pxoffset += pwidth;
+         pwidth = min ((width-pxoffset), 
+                       IMAGE_INSTANCE_PIXMAP_WIDTH (p));
        }
-      pyoffset += xdga.height;
-      xdga.height = min ((db->height - pyoffset), 
-                        IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
+      pyoffset += pheight;
+      pheight = min ((height-pyoffset), 
+                    IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
     }
 }
 
-/* Output a pixmap at the desired location. 
-   DB          normalized display_box.
-   DGA         normalized display_glyph_area. */
 static void
-mswindows_output_pixmap (struct window *w, Lisp_Object image_instance,
-                        struct display_box *db, struct display_glyph_area *dga,
-                        face_index findex, int cursor_start, int cursor_width,
-                        int cursor_height, int bg_pixmap)
+mswindows_output_pixmap (struct window *w, struct display_line *dl,
+                        Lisp_Object image_instance, int xpos, int xoffset,
+                        int start_pixpos, int width, face_index findex,
+                        int cursor_start, int cursor_width, int cursor_height,
+                        int offset_bitmap)
 {
   struct frame *f = XFRAME (w->frame);
   HDC hdc = FRAME_MSWINDOWS_DC (f);
@@ -707,18 +715,100 @@ mswindows_output_pixmap (struct window *w, Lisp_Object image_instance,
   struct Lisp_Image_Instance *p = XIMAGE_INSTANCE (image_instance);
   Lisp_Object window;
 
+  int lheight = dl->ascent + dl->descent - dl->clip;
+  int pheight = ((int) IMAGE_INSTANCE_PIXMAP_HEIGHT (p) > lheight ? lheight :
+                IMAGE_INSTANCE_PIXMAP_HEIGHT (p));
+  int clip_x, clip_y, clip_width, clip_height;
+
+  /* The pixmap_offset is used to center the pixmap on lines which are
+     shorter than it is.  This results in odd effects when scrolling
+     pixmaps off of the bottom.  Let's try not using it. */
+#if 0
+  int pixmap_offset = (int) (IMAGE_INSTANCE_PIXMAP_HEIGHT (p) - lheight) / 2;
+#else
+  int pixmap_offset = 0;
+#endif
+
   XSETWINDOW (window, w);
 
+  if ((start_pixpos >= 0 && start_pixpos > xpos) || xoffset)
+    {
+      if (start_pixpos > xpos && start_pixpos > xpos + width)
+       return;
+
+      clip_x = xoffset;
+      clip_width = width;
+      if (start_pixpos > xpos)
+       {
+         clip_x += (start_pixpos - xpos);
+         clip_width -= (start_pixpos - xpos);
+       }
+    }
+  else
+    {
+      clip_x = 0;
+      clip_width = 0;
+    }
+
+  /* Place markers for possible future functionality (clipping the top
+     half instead of the bottom half; think pixel scrolling). */
+  clip_y = 0;
+  clip_height = pheight;
+
+  /* Clear the area the pixmap is going into.  The pixmap itself will
+     always take care of the full width.  We don't want to clear where
+     it is going to go in order to avoid flicker.  So, all we have to
+     take care of is any area above or below the pixmap. */
+  /* #### We take a shortcut for now.  We know that since we have
+     pixmap_offset hardwired to 0 that the pixmap is against the top
+     edge so all we have to worry about is below it. */
+  /* #### Unless the pixmap has a mask in which case we have to clear
+     the whole damn thing since we can't yet clear just the area not
+     included in the mask. */
+  if (((int) (dl->ypos - dl->ascent + pheight) <
+       (int) (dl->ypos + dl->descent - dl->clip))
+      || IMAGE_INSTANCE_MSWINDOWS_MASK (p))
+    {
+      int clear_x, clear_y, clear_width, clear_height;
+
+      if (IMAGE_INSTANCE_MSWINDOWS_MASK (p))
+       {
+         clear_y = dl->ypos - dl->ascent;
+         clear_height = lheight;
+       }
+      else
+       {
+         clear_y = dl->ypos - dl->ascent + pheight;
+         clear_height = lheight - pheight;
+       }
+
+      if (start_pixpos >= 0 && start_pixpos > xpos)
+       {
+         clear_x = start_pixpos;
+         clear_width = xpos + width - start_pixpos;
+       }
+      else
+       {
+         clear_x = xpos;
+         clear_width = width;
+       }
+
+      if (!offset_bitmap)      /* i.e. not a bg pixmap */
+       mswindows_clear_region (window, findex, clear_x, clear_y,
+                               clear_width, clear_height);
+    }
+
   /* Output the pixmap. Have to do this as many times as is required
    to fill the given area */
   mswindows_update_dc (hdc, Qnil,
                       WINDOW_FACE_CACHEL_FOREGROUND (w, findex),
                       WINDOW_FACE_CACHEL_BACKGROUND (w, findex), Qnil);
 
-  if (bg_pixmap)
-    mswindows_output_dibitmap_region (f, p, db, dga);
-  else
-    mswindows_output_dibitmap (f, p, db, dga);
+  mswindows_output_dibitmap_region (f, p, xpos - xoffset,
+                                   dl->ypos - dl->ascent,
+                                   clip_x, clip_y, clip_width, clip_height,
+                                   width + xoffset, pheight, pixmap_offset,
+                                   offset_bitmap);
 }
 
 #ifdef HAVE_SCROLLBARS
@@ -810,11 +900,12 @@ mswindows_redraw_exposed_window (struct window *w, int x, int y, int width,
   for (line = 0; line < Dynarr_length (cdla); line++)
     {
       struct display_line *cdl = Dynarr_atp (cdla, line);
+      int top_y = cdl->ypos - cdl->ascent;
+      int bottom_y = cdl->ypos + cdl->descent;
 
-      if (DISPLAY_LINE_YPOS (cdl) + DISPLAY_LINE_HEIGHT (cdl)
-         >= rect_draw.top)
+      if (bottom_y >= rect_draw.top)
        {
-         if (DISPLAY_LINE_YPOS (cdl) > rect_draw.bottom)
+         if (top_y > rect_draw.bottom)
            {
              if (line == 0)
                continue;
@@ -875,7 +966,6 @@ mswindows_redraw_exposed_area (struct frame *f, int x, int y, int width, int hei
      redraw anyhow. */
   MAYBE_FRAMEMETH (f, redraw_exposed_toolbars, (f, x, y, width, height));
 #endif
-  redraw_exposed_gutters (f, x, y, width, height);
 
   if (!f->window_face_cache_reset)
        {
@@ -888,54 +978,38 @@ mswindows_redraw_exposed_area (struct frame *f, int x, int y, int width, int hei
 
 
 /*****************************************************************************
- mswindows_bevel_area
+ mswindows_bevel_modeline
 
- Draw a 3d border around the specified area on window W.
+ Draw a 3d border around the modeline on window W.
  ****************************************************************************/
 static void
-mswindows_bevel_area (struct window *w, face_index findex, int x, int y, 
-                     int width, int height, int thickness,
-                     int edges, enum edge_style style)
+mswindows_bevel_modeline (struct window *w, struct display_line *dl)
 {
   struct frame *f = XFRAME (w->frame);
+  Lisp_Object color;
+  int shadow_width = MODELINE_SHADOW_THICKNESS (w);
+  RECT rect = {        WINDOW_MODELINE_LEFT (w), 
+               dl->ypos - dl->ascent - shadow_width,
+               WINDOW_MODELINE_RIGHT (w),
+               dl->ypos + dl->descent + shadow_width};
   UINT edge;
-  UINT border = 0;
 
-  if (style == EDGE_ETCHED_IN)
-    edge = EDGE_ETCHED;
-  else if (style == EDGE_ETCHED_OUT)
-    edge = EDGE_BUMP;
-  else if (style == EDGE_BEVEL_IN)
-    {
-      if (thickness == 1)
-       edge = BDR_SUNKENINNER;
-      else
-       edge = EDGE_SUNKEN;
-    }
-  else                         /* EDGE_BEVEL_OUT */
-    {
-      if (thickness == 1)
-       edge = BDR_RAISEDINNER;
-      else
-       edge = EDGE_RAISED;
-    }
+  color = WINDOW_FACE_CACHEL_BACKGROUND (w, MODELINE_INDEX);
+  mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, Qnil, color, Qnil);
 
-  if (edges & EDGE_TOP)
-    border |= BF_TOP;
-  if (edges & EDGE_LEFT)
-    border |= BF_LEFT;
-  if (edges & EDGE_BOTTOM)
-    border |= BF_BOTTOM;
-  if (edges & EDGE_RIGHT)
-    border |= BF_RIGHT;
-
-  {
-    RECT rect = { x, y, x + width, y + height };
-    Lisp_Object color = WINDOW_FACE_CACHEL_BACKGROUND (w, findex);
-    mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, Qnil, color, Qnil);
-
-    DrawEdge (FRAME_MSWINDOWS_DC (f), &rect, edge, border);
-  }
+  if (XINT (w->modeline_shadow_thickness) < 0)
+    shadow_width = -shadow_width;
+
+  if (shadow_width < -1)
+    edge = EDGE_SUNKEN;
+  else if (shadow_width < 0)
+    edge = BDR_SUNKENINNER;
+  else if (shadow_width == 1)
+    edge = BDR_RAISEDINNER;
+  else
+    edge = EDGE_RAISED;
+    
+  DrawEdge (FRAME_MSWINDOWS_DC (f), &rect, edge, BF_RECT);
 }
 
 \f
@@ -1013,7 +1087,7 @@ mswindows_ring_bell (struct device *d, int volume, int pitch, int duration)
 
  Given a display line, a block number for that start line, output all
  runes between start and end in the specified display block.
- Ripped off with minimal thought from the corresponding X routine.
+ Ripped off with mininmal thought from the corresponding X routine.
  ****************************************************************************/
 static void
 mswindows_output_display_block (struct window *w, struct display_line *dl, int block,
@@ -1037,14 +1111,18 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
   rb = Dynarr_atp (rba, start);
 
   if (!rb)
+    {
       /* Nothing to do so don't do anything. */
       return;
-
-  findex = rb->findex;
-  xpos = rb->xpos;
-  width = 0;
-  if (rb->type == RUNE_CHAR)
-    charset = CHAR_CHARSET (rb->object.chr.ch);
+    }
+  else
+    {
+      findex = rb->findex;
+      xpos = rb->xpos;
+      width = 0;
+      if (rb->type == RUNE_CHAR)
+       charset = CHAR_CHARSET (rb->object.chr.ch);
+    }
 
   if (end < 0)
     end = Dynarr_length (rba);
@@ -1067,7 +1145,7 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
          if (Dynarr_length (buf))
            {
              mswindows_output_string (w, dl, buf, xpos, 0, start_pixpos, width,
-                                findex, 0, 0, 0, 0);
+                                findex);
              xpos = rb->xpos;
              width = 0;
            }
@@ -1101,9 +1179,10 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
              else if (rb->object.chr.ch == '\n')
                {
                  /* Clear in case a cursor was formerly here. */
-                 redisplay_clear_region (window, findex, xpos, 
-                                         DISPLAY_LINE_YPOS (dl),
-                                         rb->width, DISPLAY_LINE_HEIGHT (dl));
+                 int height = dl->ascent + dl->descent - dl->clip;
+
+                 mswindows_clear_region (window, findex, xpos, dl->ypos - dl->ascent,
+                                   rb->width, height);
                  elt++;
                }
            }
@@ -1139,11 +1218,6 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
          else if (rb->type == RUNE_DGLYPH)
            {
              Lisp_Object instance;
-             struct display_box db;
-             struct display_glyph_area dga;
-             redisplay_calculate_display_boxes (dl, rb->xpos, rb->object.dglyph.xoffset,
-                                                start_pixpos, rb->width,
-                                                &db, &dga);
 
              XSETWINDOW (window, w);
              instance = glyph_image_instance (rb->object.dglyph.glyph,
@@ -1168,17 +1242,17 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
                      else /* #### redisplay-x passes -1 as the width: why ? */
                        mswindows_output_string (w, dl, buf, xpos,
                                           rb->object.dglyph.xoffset,
-                                          start_pixpos, rb->width, findex,
-                                                0, 0, 0, 0);
+                                          start_pixpos, rb->width, findex);
                      Dynarr_reset (buf);
                    }
                    break;
 
                  case IMAGE_MONO_PIXMAP:
                  case IMAGE_COLOR_PIXMAP:
-                   redisplay_output_pixmap (w, instance, &db, &dga, findex,
-                                            cursor_start, cursor_width,
-                                            cursor_height, 0);
+                   mswindows_output_pixmap (w, dl, instance, xpos,
+                                    rb->object.dglyph.xoffset, start_pixpos,
+                                    rb->width, findex, cursor_start,
+                                    cursor_width, cursor_height, 0);
                    if (rb->cursor_type == CURSOR_ON)
                      mswindows_output_cursor (w, dl, xpos, cursor_width,
                                               findex, 0, 1);
@@ -1188,22 +1262,7 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
                    abort ();
 
                  case IMAGE_SUBWINDOW:
-                 case IMAGE_WIDGET:
-                   redisplay_output_subwindow (w, instance, &db, &dga, findex,
-                                               cursor_start, cursor_width,
-                                               cursor_height);
-                   if (rb->cursor_type == CURSOR_ON)
-                     mswindows_output_cursor (w, dl, xpos, cursor_width,
-                                              findex, 0, 1);
-                   break;
-
-                 case IMAGE_LAYOUT:
-                   redisplay_output_layout (w, instance, &db, &dga, findex,
-                                            cursor_start, cursor_width,
-                                            cursor_height);
-                   if (rb->cursor_type == CURSOR_ON)
-                     mswindows_output_cursor (w, dl, xpos, cursor_width,
-                                              findex, 0, 1);
+                   /* #### implement me */
                    break;
 
                  case IMAGE_NOTHING:
@@ -1223,15 +1282,14 @@ mswindows_output_display_block (struct window *w, struct display_line *dl, int b
     }
 
   if (Dynarr_length (buf))
-    mswindows_output_string (w, dl, buf, xpos, 0, start_pixpos, width, findex,
-                            0, 0, 0, 0);
+    mswindows_output_string (w, dl, buf, xpos, 0, start_pixpos, width, findex);
 
   if (dl->modeline
       && !EQ (Qzero, w->modeline_shadow_thickness)
       && (f->clear
          || f->windows_structure_changed
          || w->shadow_thickness_changed))
-    bevel_modeline (w, dl);
+    mswindows_bevel_modeline (w, dl);
 
   Dynarr_free (buf);
 }
@@ -1252,14 +1310,12 @@ mswindows_output_vertical_divider (struct window *w, int clear_unused)
   int abs_shadow = abs (shadow);
   int line_width = XINT (w->vertical_divider_line_width);
   int div_left = WINDOW_RIGHT (w) - window_divider_width (w);
-  int y1 = WINDOW_TOP (w) + FRAME_TOP_GUTTER_BOUNDS (f);
-  int y2 = WINDOW_BOTTOM (w) + FRAME_BOTTOM_GUTTER_BOUNDS (f);
 
   /* Clear left and right spacing areas */
   if (spacing)
     {
-      rect.top = y1;
-      rect.bottom = y2;
+      rect.top = WINDOW_TOP (w);
+      rect.bottom = WINDOW_BOTTOM (w);
       mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, Qnil,
                   WINDOW_FACE_CACHEL_BACKGROUND (w, DEFAULT_INDEX), Qnil);
       rect.right = WINDOW_RIGHT (w);
@@ -1273,8 +1329,8 @@ mswindows_output_vertical_divider (struct window *w, int clear_unused)
     }
   
   /* Clear divider face */
-  rect.top = y1 + abs_shadow;
-  rect.bottom = y2 - abs_shadow;
+  rect.top = WINDOW_TOP (w) + abs_shadow;
+  rect.bottom = WINDOW_BOTTOM (w) - abs_shadow;
   rect.left = div_left + spacing + abs_shadow;
   rect.right = rect.left + line_width;
   if (rect.left < rect.right)
@@ -1290,7 +1346,7 @@ mswindows_output_vertical_divider (struct window *w, int clear_unused)
   /* Draw a shadow around the divider */
   if (shadow != 0)
     {
-      /* #### This will be fixed to support arbitrary thickness */
+      /* #### This will be fixed to support arbitrary thichkness */
       InflateRect (&rect, abs_shadow, abs_shadow);
       DrawEdge (FRAME_MSWINDOWS_DC (f), &rect,
                shadow > 0 ? EDGE_RAISED : EDGE_SUNKEN, BF_RECT);
@@ -1330,34 +1386,140 @@ mswindows_text_width (struct frame *f, struct face_cachel *cachel,
  given face.
  ****************************************************************************/
 static void
-mswindows_clear_region (Lisp_Object locale, struct device* d, struct frame* f, 
-                       face_index findex, int x, int y,
-                       int width, int height, Lisp_Object fcolor, Lisp_Object bcolor,
-                       Lisp_Object background_pixmap)
+mswindows_clear_region (Lisp_Object locale, face_index findex, int x, int y,
+                 int width, int height)
 {
+  struct window *w;
+  struct frame *f;
+  Lisp_Object background_pixmap = Qunbound;
+  Lisp_Object temp;
   RECT rect = { x, y, x+width, y+height };
 
-  if (!NILP (background_pixmap))
+  if (!(width && height))   /* We often seem to get called with width==0 */
+    return;
+
+  if (WINDOWP (locale))
+    {
+      w = XWINDOW (locale);
+      f = XFRAME (w->frame);
+    }
+  else if (FRAMEP (locale))
+    {
+      w = NULL;
+      f = XFRAME (locale);
+    }
+  else
+    abort ();
+  
+  if (w)
+    {
+      temp = WINDOW_FACE_CACHEL_BACKGROUND_PIXMAP (w, findex);
+
+      if (IMAGE_INSTANCEP (temp)
+         && IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (temp)))
+       {
+         /* #### maybe we could implement such that a string
+            can be a background pixmap? */
+         background_pixmap = temp;
+       }
+    }
+  else
+    {
+      temp = FACE_BACKGROUND_PIXMAP (Vdefault_face, locale);
+
+      if (IMAGE_INSTANCEP (temp)
+         && IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (temp)))
+       {
+         background_pixmap = temp;
+       }
+    }
+
+  if (!UNBOUNDP (background_pixmap))
     {
-      struct display_box db = { x, y, width, height };
+      Lisp_Object fcolor, bcolor;
+      
+      if (w)
+       {
+         fcolor = WINDOW_FACE_CACHEL_FOREGROUND (w, findex);
+         bcolor = WINDOW_FACE_CACHEL_BACKGROUND (w, findex);
+       }
+      else
+       {
+         fcolor = FACE_FOREGROUND (Vdefault_face, locale);
+         bcolor = FACE_BACKGROUND (Vdefault_face, locale);
+       }
+
       mswindows_update_dc (FRAME_MSWINDOWS_DC (f),
                           Qnil, fcolor, bcolor, background_pixmap);
+
       mswindows_output_dibitmap_region 
-       ( f, XIMAGE_INSTANCE (background_pixmap), &db, 0);
+       ( f, XIMAGE_INSTANCE (background_pixmap),
+         x, y, 0, 0, 0, 0, width, height, 0, TRUE);
     }
   else
     {
-      mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, Qnil, fcolor, Qnil);
-      ExtTextOut (FRAME_MSWINDOWS_DC (f), 0, 0, ETO_OPAQUE, 
-                 &rect, NULL, 0, NULL);
+      Lisp_Object color = (w ? WINDOW_FACE_CACHEL_BACKGROUND (w, findex) :
+                          FACE_BACKGROUND (Vdefault_face, locale));
+      mswindows_update_dc (FRAME_MSWINDOWS_DC (f), Qnil, Qnil, color, Qnil);
+      ExtTextOut (FRAME_MSWINDOWS_DC (f), 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
     }
 
 #ifdef HAVE_SCROLLBARS
   if (WINDOWP (locale))
-    mswindows_redisplay_deadbox_maybe (XWINDOW (locale), &rect);
+    mswindows_redisplay_deadbox_maybe (w, &rect);
 #endif
 }
 
+/*****************************************************************************
+ mswindows_clear_to_window_end
+
+ Clear the area between ypos1 and ypos2.  Each margin area and the
+ text area is handled separately since they may each have their own
+ background color.
+ ****************************************************************************/
+static void
+mswindows_clear_to_window_end (struct window *w, int ypos1, int ypos2)
+{
+  int height = ypos2 - ypos1;
+
+  if (height)
+    {
+      struct frame *f = XFRAME (w->frame);
+      Lisp_Object window;
+      int bflag = (window_needs_vertical_divider (w) ? 0 : 1);
+      layout_bounds bounds;
+
+      bounds = calculate_display_line_boundaries (w, bflag);
+      XSETWINDOW (window, w);
+
+      if (window_is_leftmost (w))
+       mswindows_clear_region (window, DEFAULT_INDEX, FRAME_LEFT_BORDER_START (f),
+                         ypos1, FRAME_BORDER_WIDTH (f), height);
+
+      if (bounds.left_in - bounds.left_out > 0)
+       mswindows_clear_region (window,
+                         get_builtin_face_cache_index (w, Vleft_margin_face),
+                         bounds.left_out, ypos1,
+                         bounds.left_in - bounds.left_out, height);
+
+      if (bounds.right_in - bounds.left_in > 0)
+       mswindows_clear_region (window, DEFAULT_INDEX, bounds.left_in, ypos1,
+                         bounds.right_in - bounds.left_in, height);
+
+      if (bounds.right_out - bounds.right_in > 0)
+       mswindows_clear_region (window,
+                         get_builtin_face_cache_index (w, Vright_margin_face),
+                         bounds.right_in, ypos1,
+                         bounds.right_out - bounds.right_in, height);
+
+      if (window_is_rightmost (w))
+       mswindows_clear_region (window, DEFAULT_INDEX, FRAME_RIGHT_BORDER_START (f),
+                         ypos1, FRAME_BORDER_WIDTH (f), height);
+    }
+
+}
+
+
 /* XXX Implement me! */
 static void
 mswindows_clear_frame (struct frame *f)
@@ -1380,13 +1542,11 @@ console_type_create_redisplay_mswindows (void)
   CONSOLE_HAS_METHOD (mswindows, divider_height);
   CONSOLE_HAS_METHOD (mswindows, eol_cursor_width);
   CONSOLE_HAS_METHOD (mswindows, output_vertical_divider);
+  CONSOLE_HAS_METHOD (mswindows, clear_to_window_end);
   CONSOLE_HAS_METHOD (mswindows, clear_region);
   CONSOLE_HAS_METHOD (mswindows, clear_frame);
   CONSOLE_HAS_METHOD (mswindows, output_begin);
   CONSOLE_HAS_METHOD (mswindows, output_end);
   CONSOLE_HAS_METHOD (mswindows, flash);
   CONSOLE_HAS_METHOD (mswindows, ring_bell);
-  CONSOLE_HAS_METHOD (mswindows, bevel_area);
-  CONSOLE_HAS_METHOD (mswindows, output_string);
-  CONSOLE_HAS_METHOD (mswindows, output_pixmap);
 }