]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkwindow.c
Update to changed cairo interface.
[~andy/gtk] / gdk / gdkwindow.c
index e171edc2fa4a1f5eef6e981b23d43e0a6cd33eaf..5e1c7dc3f2239adc008997c6b468a1e391428d11 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
+#include <config.h>
 #include "gdkwindow.h"
 #include "gdkinternals.h"
 #include "gdk.h"               /* For gdk_rectangle_union() */
 #include "gdkpixmap.h"
 #include "gdkdrawable.h"
 #include "gdkpixmap.h"
+#include "gdkscreen.h"
+#include "gdkalias.h"
 
 #define USE_BACKING_STORE      /* Appears to work on Win32, too, now. */
 
@@ -41,28 +44,22 @@ struct _GdkWindowPaint
   GdkPixmap *pixmap;
   gint x_offset;
   gint y_offset;
+  cairo_surface_t *surface;
 };
 
-static const GdkPointerHooks default_pointer_hooks = {
-  _gdk_windowing_window_get_pointer,
-  _gdk_windowing_window_at_pointer
-};
-
-static const GdkPointerHooks *current_pointer_hooks = &default_pointer_hooks;
-
 static GdkGC *gdk_window_create_gc      (GdkDrawable     *drawable,
                                          GdkGCValues     *values,
                                          GdkGCValuesMask  mask);
 static void   gdk_window_draw_rectangle (GdkDrawable     *drawable,
                                          GdkGC           *gc,
-                                         gint             filled,
+                                         gboolean         filled,
                                          gint             x,
                                          gint             y,
                                          gint             width,
                                          gint             height);
 static void   gdk_window_draw_arc       (GdkDrawable     *drawable,
                                          GdkGC           *gc,
-                                         gint             filled,
+                                         gboolean         filled,
                                          gint             x,
                                          gint             y,
                                          gint             width,
@@ -71,7 +68,7 @@ static void   gdk_window_draw_arc       (GdkDrawable     *drawable,
                                          gint             angle2);
 static void   gdk_window_draw_polygon   (GdkDrawable     *drawable,
                                          GdkGC           *gc,
-                                         gint             filled,
+                                         gboolean         filled,
                                          GdkPoint        *points,
                                          gint             npoints);
 static void   gdk_window_draw_text      (GdkDrawable     *drawable,
@@ -109,12 +106,20 @@ static void   gdk_window_draw_lines     (GdkDrawable     *drawable,
                                         GdkGC           *gc,
                                         GdkPoint        *points,
                                         gint             npoints);
-static void   gdk_window_draw_glyphs    (GdkDrawable      *drawable,
-                                        GdkGC            *gc,
-                                        PangoFont        *font,
-                                        gint              x,
-                                        gint              y,
-                                        PangoGlyphString *glyphs);
+
+static void gdk_window_draw_glyphs             (GdkDrawable      *drawable,
+                                               GdkGC            *gc,
+                                               PangoFont        *font,
+                                               gint              x,
+                                               gint              y,
+                                               PangoGlyphString *glyphs);
+static void gdk_window_draw_glyphs_transformed (GdkDrawable      *drawable,
+                                               GdkGC            *gc,
+                                               PangoMatrix      *matrix,
+                                               PangoFont        *font,
+                                               gint              x,
+                                               gint              y,
+                                               PangoGlyphString *glyphs);
 
 static void   gdk_window_draw_image     (GdkDrawable     *drawable,
                                          GdkGC           *gc,
@@ -126,12 +131,34 @@ static void   gdk_window_draw_image     (GdkDrawable     *drawable,
                                          gint             width,
                                          gint             height);
 
-static GdkImage*  gdk_window_get_image  (GdkDrawable     *drawable,
-                                         gint             x,
-                                         gint             y,
-                                         gint             width,
-                                         gint             height);
-
+static void gdk_window_draw_pixbuf (GdkDrawable     *drawable,
+                                   GdkGC           *gc,
+                                   GdkPixbuf       *pixbuf,
+                                   gint             src_x,
+                                   gint             src_y,
+                                   gint             dest_x,
+                                   gint             dest_y,
+                                   gint             width,
+                                   gint             height,
+                                   GdkRgbDither     dither,
+                                   gint             x_dither,
+                                   gint             y_dither);
+
+static void gdk_window_draw_trapezoids (GdkDrawable   *drawable,
+                                       GdkGC         *gc,
+                                       GdkTrapezoid  *trapezoids,
+                                       gint           n_trapezoids);
+
+static GdkImage* gdk_window_copy_to_image (GdkDrawable *drawable,
+                                          GdkImage    *image,
+                                          gint         src_x,
+                                          gint         src_y,
+                                          gint         dest_x,
+                                          gint         dest_y,
+                                          gint         width,
+                                          gint         height);
+
+static cairo_surface_t *gdk_window_ref_cairo_surface (GdkDrawable *drawable);
 
 static void   gdk_window_real_get_size  (GdkDrawable     *drawable,
                                          gint            *width,
@@ -139,8 +166,9 @@ static void   gdk_window_real_get_size  (GdkDrawable     *drawable,
 
 static GdkVisual*   gdk_window_real_get_visual   (GdkDrawable *drawable);
 static gint         gdk_window_real_get_depth    (GdkDrawable *drawable);
+static GdkScreen*   gdk_window_real_get_screen   (GdkDrawable *drawable);
 static void         gdk_window_real_set_colormap (GdkDrawable *drawable,
-                                             GdkColormap *cmap);
+                                                 GdkColormap *cmap);
 static GdkColormap* gdk_window_real_get_colormap (GdkDrawable *drawable);
 
 static GdkDrawable* gdk_window_get_composite_drawable (GdkDrawable *drawable,
@@ -158,6 +186,11 @@ static void gdk_window_free_paint_stack (GdkWindow *window);
 static void gdk_window_init       (GdkWindowObject      *window);
 static void gdk_window_class_init (GdkWindowObjectClass *klass);
 static void gdk_window_finalize   (GObject              *object);
+static void gdk_window_clear_backing_rect (GdkWindow *window,
+                                          gint       x,
+                                          gint       y,
+                                          gint       width,
+                                          gint       height);
 
 static gpointer parent_class = NULL;
 
@@ -222,13 +255,18 @@ gdk_window_class_init (GdkWindowObjectClass *klass)
   drawable_class->draw_segments = gdk_window_draw_segments;
   drawable_class->draw_lines = gdk_window_draw_lines;
   drawable_class->draw_glyphs = gdk_window_draw_glyphs;
+  drawable_class->draw_glyphs_transformed = gdk_window_draw_glyphs_transformed;
   drawable_class->draw_image = gdk_window_draw_image;
+  drawable_class->draw_pixbuf = gdk_window_draw_pixbuf;
+  drawable_class->draw_trapezoids = gdk_window_draw_trapezoids;
   drawable_class->get_depth = gdk_window_real_get_depth;
+  drawable_class->get_screen = gdk_window_real_get_screen;
   drawable_class->get_size = gdk_window_real_get_size;
   drawable_class->set_colormap = gdk_window_real_set_colormap;
   drawable_class->get_colormap = gdk_window_real_get_colormap;
   drawable_class->get_visual = gdk_window_real_get_visual;
-  drawable_class->get_image = gdk_window_get_image;
+  drawable_class->_copy_to_image = gdk_window_copy_to_image;
+  drawable_class->ref_cairo_surface = gdk_window_ref_cairo_surface;
   drawable_class->get_clip_region = gdk_window_get_clip_region;
   drawable_class->get_visible_region = gdk_window_get_visible_region;
   drawable_class->get_composite_drawable = gdk_window_get_composite_drawable;
@@ -254,27 +292,44 @@ gdk_window_finalize (GObject *object)
        _gdk_window_destroy (window, TRUE);
     }
 
-  g_object_unref (G_OBJECT (obj->impl));
+  g_object_unref (obj->impl);
   obj->impl = NULL;
   
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+static void
+window_remove_filters (GdkWindow *window)
+{
+  GdkWindowObject *obj = (GdkWindowObject*) window;
+
+  if (obj->filters)
+    {
+      GList *tmp_list;
+      
+      for (tmp_list = obj->filters; tmp_list; tmp_list = tmp_list->next)
+       g_free (tmp_list->data);
+    
+      g_list_free (obj->filters);
+      obj->filters = NULL;
+    }
+}
+
 /**
  * _gdk_window_destroy_hierarchy:
  * @window: a #GdkWindow
  * @recursing: If TRUE, then this is being called because a parent
- *            was destroyed. This generally means that the call to the windowing system
- *            to destroy the window can be omitted, since it will be destroyed as a result
- *            of the parent being destroyed. Unless @foreign_destroy
- *            
- * foreign_destroy: If TRUE, the window or a parent was destroyed by some external 
- *            agency. The window has already been destroyed and no windowing
- *            system calls should be made. (This may never happen for some
- *            windowing systems.)
+ *            was destroyed. This generally means that the call to the 
+ *            windowing system to destroy the window can be omitted, since
+ *            it will be destroyed as a result of the parent being destroyed.
+ *            Unless @foreign_destroy.           
+ * @foreign_destroy: If TRUE, the window or a parent was destroyed by some 
+ *            external agency. The window has already been destroyed and no 
+ *            windowing system calls should be made. (This may never happen
+ *            for some windowing systems.)
  *
- * Internal function to destroy a window. Like gdk_window_destroy(), but does not
- * drop the reference count created by gdk_window_new().
+ * Internal function to destroy a window. Like gdk_window_destroy(),
+ * but does not drop the reference count created by gdk_window_new().
  **/
 static void
 _gdk_window_destroy_hierarchy (GdkWindow *window,
@@ -291,6 +346,9 @@ _gdk_window_destroy_hierarchy (GdkWindow *window,
   
   private = (GdkWindowObject*) window;
   
+  if (GDK_WINDOW_DESTROYED (window))
+    return;
+    
   switch (GDK_WINDOW_TYPE (window))
     {
     case GDK_WINDOW_TOPLEVEL:
@@ -298,7 +356,25 @@ _gdk_window_destroy_hierarchy (GdkWindow *window,
     case GDK_WINDOW_DIALOG:
     case GDK_WINDOW_TEMP:
     case GDK_WINDOW_FOREIGN:
-      if (!GDK_WINDOW_DESTROYED (window))
+      if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_FOREIGN && !foreign_destroy)
+       {
+         /* Logically, it probably makes more sense to send
+          * a "destroy yourself" message to the foreign window
+          * whether or not it's in our heirarchy; but for historical
+          * reasons, we only send "destroy yourself" messages to
+          * foreign windows in our heirarchy.
+          */
+         if (private->parent)
+           _gdk_windowing_window_destroy_foreign (window);
+
+         /* Also for historical reasons, we remove any filters
+          * on a foreign window when it or a parent is destroyed;
+          * this likely causes problems if two separate portions
+          * of code are maintaining filter lists on a foreign window.
+          */
+         window_remove_filters (window);
+       }
+      else
        {
          private->state |= GDK_WINDOW_STATE_WITHDRAWN;
          
@@ -316,7 +392,7 @@ _gdk_window_destroy_hierarchy (GdkWindow *window,
               private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
               private->bg_pixmap != GDK_NO_BG)
            {
-             gdk_pixmap_unref (private->bg_pixmap);
+             g_object_unref (private->bg_pixmap);
              private->bg_pixmap = NULL;
            }
          
@@ -334,28 +410,18 @@ _gdk_window_destroy_hierarchy (GdkWindow *window,
                  
                  temp_private = (GdkWindowObject*) temp_window;
                  if (temp_private)
-                   _gdk_window_destroy_hierarchy (temp_window, TRUE, foreign_destroy);
+                   _gdk_window_destroy_hierarchy (temp_window,
+                                                   TRUE, foreign_destroy);
                }
              
              g_list_free (children);
            }
          
          _gdk_windowing_window_destroy (window, recursing, foreign_destroy);
+         private->parent = NULL;
          private->destroyed = TRUE;
 
-         if (private->filters)
-           {
-             tmp = private->filters;
-             
-             while (tmp)
-               {
-                 g_free (tmp->data);
-                 tmp = tmp->next;
-               }
-             
-             g_list_free (private->filters);
-             private->filters = NULL;
-           }
+         window_remove_filters (window);
 
           gdk_drawable_set_colormap (GDK_DRAWABLE (window), NULL);
        }
@@ -370,13 +436,13 @@ _gdk_window_destroy_hierarchy (GdkWindow *window,
 /**
  * _gdk_window_destroy:
  * @window: a #GdkWindow
- * foreign_destroy: If TRUE, the window or a parent was destroyed by some external 
- *            agency. The window has already been destroyed and no windowing
- *            system calls should be made. (This may never happen for some
- *            windowing systems.)
+ * @foreign_destroy: If TRUE, the window or a parent was destroyed by some
+ *            external agency. The window has already been destroyed and no
+ *            windowing system calls should be made. (This may never happen
+ *            for some windowing systems.)
  *
- * Internal function to destroy a window. Like gdk_window_destroy(), but does not
- * drop the reference count created by gdk_window_new().
+ * Internal function to destroy a window. Like gdk_window_destroy(),
+ * but does not drop the reference count created by gdk_window_new().
  **/
 void
 _gdk_window_destroy (GdkWindow *window,
@@ -389,19 +455,19 @@ _gdk_window_destroy (GdkWindow *window,
  * gdk_window_destroy:
  * @window: a #GdkWindow
  *
- * Destroys @window (destroys the server-side resource associated with @window).
- * Memory allocated for @window may not be freed until all references
- * to @window are dropped. All children of @window are also destroyed.
+ * Destroys the window system resources associated with @window and decrements @window's
+ * reference count. The window system resources for all children of @window are also
+ * destroyed, but the children's reference counts are not decremented.
+ *
+ * Note that a window will not be destroyed automatically when its reference count
+ * reaches zero. You must call this function yourself before that happens.
  *
- * There's normally no need to use this function, window are automatically
- * destroyed when their reference count reaches 0.
- * 
  **/
 void
 gdk_window_destroy (GdkWindow *window)
 {
   _gdk_window_destroy_hierarchy (window, FALSE, FALSE);
-  gdk_drawable_unref (window);
+  g_object_unref (window);
 }
 
 /**
@@ -467,10 +533,11 @@ gdk_window_get_window_type (GdkWindow *window)
  * @x: X coordinate of window
  * @y: Y coordinate of window
  *
- * Obtains the position of the window as reported in the most-recently-processed
- * #GdkEventConfigure. Contrast with gdk_window_get_geometry() which
- * queries the X server for the current window position, regardless of which
- * events have been received or processed.
+ * Obtains the position of the window as reported in the
+ * most-recently-processed #GdkEventConfigure. Contrast with
+ * gdk_window_get_geometry() which queries the X server for the
+ * current window position, regardless of which events have been
+ * received or processed.
  *
  * The position coordinates are relative to the window's parent window.
  * 
@@ -587,8 +654,10 @@ gdk_window_peek_children (GdkWindow *window)
  * @data: data to pass to filter callback
  *
  * Adds an event filter to @window, allowing you to intercept events
- * before they reach GDK. This is a low-level operation and makes it easy to break
- * GDK and/or GTK+, so you have to know what you're doing.
+ * before they reach GDK. This is a low-level operation and makes it
+ * easy to break GDK and/or GTK+, so you have to know what you're
+ * doing. Pass %NULL for @window to get all events for all windows,
+ * instead of events for a specific window.
  * 
  **/
 void          
@@ -677,9 +746,10 @@ gdk_window_remove_filter (GdkWindow     *window,
 }
 
 /**
- * gdk_window_get_toplevels:
+ * gdk_screen_get_toplevel_windows:
+ * @screen: The #GdkScreen where the toplevels are located.
  * 
- * Obtains a list of all toplevel windows known to GDK.
+ * Obtains a list of all toplevel windows known to GDK on the screen @screen.
  * A toplevel window is a child of the root window (see
  * gdk_get_default_root_window()).
  *
@@ -687,14 +757,21 @@ gdk_window_remove_filter (GdkWindow     *window,
  * its elements need not be freed.
  * 
  * Return value: list of toplevel windows, free with g_list_free()
+ *
+ * Since: 2.2
  **/
 GList *
-gdk_window_get_toplevels (void)
+gdk_screen_get_toplevel_windows (GdkScreen *screen)
 {
+  GdkWindow * root_window;
   GList *new_list = NULL;
   GList *tmp_list;
   
-  tmp_list = ((GdkWindowObject *)_gdk_parent_root)->children;
+  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
+  
+  root_window = gdk_screen_get_root_window (screen);
+
+  tmp_list = ((GdkWindowObject *)root_window)->children;
   while (tmp_list)
     {
       if (GDK_WINDOW_TYPE (tmp_list->data) != GDK_WINDOW_FOREIGN)
@@ -705,6 +782,25 @@ gdk_window_get_toplevels (void)
   return new_list;
 }
 
+/**
+ * gdk_window_get_toplevels:
+ * 
+ * Obtains a list of all toplevel windows known to GDK on the default
+ * screen (see gdk_window_get_toplevels_for_screen()).
+ * A toplevel window is a child of the root window (see
+ * gdk_get_default_root_window()).
+ *
+ * The returned list should be freed with g_list_free(), but
+ * its elements need not be freed.
+ * 
+ * Return value: list of toplevel windows, free with g_list_free()
+ **/
+GList *
+gdk_window_get_toplevels (void)
+{
+  return gdk_screen_get_toplevel_windows (gdk_screen_get_default ());
+}
+
 /**
  * gdk_window_is_visible:
  * @window: a #GdkWindow
@@ -729,25 +825,28 @@ gdk_window_is_visible (GdkWindow *window)
  * Check if the window and all ancestors of the window are
  * mapped. (This is not necessarily "viewable" in the X sense, since
  * we only check as far as we have GDK window parents, not to the root
- * window)
- * 
- * 
- * 
+ * window.)
+ *
  * Return value: %TRUE if the window is viewable
  **/
 gboolean 
 gdk_window_is_viewable (GdkWindow *window)
 {
   GdkWindowObject *private = (GdkWindowObject *)window;
+  GdkScreen *screen;
+  GdkWindow *root_window;
   
   g_return_val_if_fail (window != NULL, FALSE);
   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
+
+  screen = gdk_drawable_get_screen (window);
+  root_window = gdk_screen_get_root_window (screen);
   
   while (private && 
-        (private != (GdkWindowObject *)_gdk_parent_root) &&
+        (private != (GdkWindowObject *)root_window) &&
         (GDK_WINDOW_TYPE (private) != GDK_WINDOW_FOREIGN))
     {
-      if (!GDK_WINDOW_IS_MAPPED (window))
+      if (GDK_WINDOW_DESTROYED (private) || !GDK_WINDOW_IS_MAPPED (private))
        return FALSE;
       
       private = (GdkWindowObject *)private->parent;
@@ -799,58 +898,6 @@ gdk_window_begin_paint_rect (GdkWindow    *window,
   gdk_region_destroy (region);
 }
 
-static GdkGC *
-gdk_window_get_bg_gc (GdkWindow *window, GdkWindowPaint *paint)
-{
-  GdkWindowObject *private = (GdkWindowObject *)window;
-
-  guint gc_mask = 0;
-  GdkGCValues gc_values;
-
-  if (private->bg_pixmap == GDK_PARENT_RELATIVE_BG && private->parent)
-    {
-      GdkWindowPaint tmp_paint = *paint;
-      tmp_paint.x_offset += private->x;
-      tmp_paint.y_offset += private->y;
-      
-      return gdk_window_get_bg_gc (GDK_WINDOW (private->parent), &tmp_paint);
-    }
-  else if (private->bg_pixmap && private->bg_pixmap != GDK_PARENT_RELATIVE_BG && private->bg_pixmap != GDK_NO_BG)
-    {
-      gc_values.fill = GDK_TILED;
-      gc_values.tile = private->bg_pixmap;
-      gc_values.ts_x_origin = - paint->x_offset;
-      gc_values.ts_y_origin = - paint->y_offset;
-      
-      gc_mask = GDK_GC_FILL | GDK_GC_TILE | GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN;
-    }
-  else
-    {
-      gc_values.foreground = private->bg_color;
-      gc_mask = GDK_GC_FOREGROUND;
-    }
-
-  return gdk_gc_new_with_values (paint->pixmap, &gc_values, gc_mask);
-}
-
-static void
-gdk_window_paint_init_bg (GdkWindow      *window,
-                         GdkWindowPaint *paint,
-                         GdkRegion      *init_region)
-{
-  GdkGC *tmp_gc;
-  
-  tmp_gc = gdk_window_get_bg_gc (window, paint);
-
-  gdk_region_offset (init_region,
-                     - paint->x_offset,
-                     - paint->y_offset);
-  gdk_gc_set_clip_region (tmp_gc, init_region);
-
-  gdk_draw_rectangle (paint->pixmap, tmp_gc, TRUE, 0, 0, -1, -1);
-  gdk_gc_unref (tmp_gc);
-}
-
 #ifdef GDK_WINDOWING_X11
 #include "x11/gdkx.h"
 #endif
@@ -908,94 +955,43 @@ gdk_window_begin_paint_region (GdkWindow *window,
   GdkWindowObject *private = (GdkWindowObject *)window;
   GdkRectangle clip_box;
   GdkWindowPaint *paint;
-  GdkRegion *init_region;
-  GdkGC *tmp_gc;
+  GSList *list;
   
   g_return_if_fail (window != NULL);
   g_return_if_fail (GDK_IS_WINDOW (window));
 
   if (GDK_WINDOW_DESTROYED (window))
     return;
-  
-  paint = g_new (GdkWindowPaint, 1);
 
-  paint->region = gdk_region_copy (region);
+  gdk_region_get_clipbox (region, &clip_box);
 
-  init_region = gdk_region_copy (region);
-  gdk_region_get_clipbox (paint->region, &clip_box);
+  paint = g_new (GdkWindowPaint, 1);
+  paint->region = gdk_region_copy (region);
+  paint->x_offset = clip_box.x;
+  paint->y_offset = clip_box.y;
+  paint->pixmap =
+    gdk_pixmap_new (window,
+                   MAX (clip_box.width, 1), MAX (clip_box.height, 1), -1);
 
-  if (private->paint_stack)
+  paint->surface = _gdk_drawable_ref_cairo_surface (paint->pixmap);
+  cairo_surface_set_device_offset (paint->surface,
+                                  - paint->x_offset, - paint->y_offset);
+  
+  for (list = private->paint_stack; list != NULL; list = list->next)
     {
-      gint old_width, old_height;
-      GdkWindowPaint *tmp_paint = private->paint_stack->data;
-      GdkRectangle old_rect, new_rect;
-      GSList *tmp_list;
-
-      gdk_drawable_get_size (tmp_paint->pixmap, &old_width, &old_height);
-      old_rect.x = tmp_paint->x_offset;
-      old_rect.y = tmp_paint->y_offset;
-      old_rect.width = old_width;
-      old_rect.height = old_height;
+      GdkWindowPaint *tmp_paint = list->data;
 
-      gdk_rectangle_union (&clip_box, &old_rect, &new_rect);
-
-      if (new_rect.width > old_rect.width || new_rect.height > old_rect.height)
-       {
-         paint->pixmap = gdk_pixmap_new (window,
-                                          new_rect.width, new_rect.height, -1);
-          tmp_gc = gdk_gc_new (paint->pixmap);
-         gdk_draw_drawable (paint->pixmap, tmp_gc, tmp_paint->pixmap,
-                            0, 0,
-                            old_rect.x - new_rect.x, old_rect.y - new_rect.y,
-                             old_rect.width, old_rect.height);
-          gdk_gc_unref (tmp_gc);
-         gdk_drawable_unref (tmp_paint->pixmap);
-
-         paint->x_offset = new_rect.x;
-         paint->y_offset = new_rect.y;
-         
-         tmp_list = private->paint_stack;
-         while (tmp_list)
-           {
-             tmp_paint = tmp_list->data;
-             gdk_region_subtract (init_region, tmp_paint->region);
-
-             tmp_paint->pixmap = paint->pixmap;
-             tmp_paint->x_offset = paint->x_offset;
-             tmp_paint->y_offset = paint->y_offset;
-              
-             tmp_list = tmp_list->next;
-           }
-       }
-      else
-       {
-         paint->x_offset = tmp_paint->x_offset;
-         paint->y_offset = tmp_paint->y_offset;
-         paint->pixmap = tmp_paint->pixmap;
-
-         tmp_list = private->paint_stack;
-         while (tmp_list)
-           {
-             tmp_paint = tmp_list->data;
-             gdk_region_subtract (init_region, tmp_paint->region);
-
-             tmp_list = tmp_list->next;
-           }
-       }
+      gdk_region_subtract (tmp_paint->region, paint->region);
     }
-  else
-    {
-      paint->x_offset = clip_box.x;
-      paint->y_offset = clip_box.y;
-      paint->pixmap = gdk_pixmap_new (window, clip_box.width, clip_box.height, -1);
-    }
-
-  if (!gdk_region_empty (init_region))
-    gdk_window_paint_init_bg (window, paint, init_region);
-  
-  gdk_region_destroy (init_region);
   
   private->paint_stack = g_slist_prepend (private->paint_stack, paint);
+
+  if (!gdk_region_empty (region))
+    {
+      gdk_window_clear_backing_rect (window,
+                                    clip_box.x, clip_box.y,
+                                    clip_box.width, clip_box.height);
+    }
 #endif /* USE_BACKING_STORE */
 }
 
@@ -1035,38 +1031,29 @@ gdk_window_end_paint (GdkWindow *window)
     }
 
   paint = private->paint_stack->data;
-  private->paint_stack = g_slist_delete_link (private->paint_stack, private->paint_stack);
+  private->paint_stack = g_slist_delete_link (private->paint_stack, 
+                                              private->paint_stack);
 
   gdk_region_get_clipbox (paint->region, &clip_box);
 
-  tmp_gc = gdk_gc_new (window);
+  tmp_gc = _gdk_drawable_get_scratch_gc (window, FALSE);
 
   _gdk_windowing_window_get_offsets (window, &x_offset, &y_offset);
 
   gdk_gc_set_clip_region (tmp_gc, paint->region);
-  gdk_gc_set_clip_origin (tmp_gc, -x_offset, -y_offset);
+  gdk_gc_set_clip_origin (tmp_gc, - x_offset, - y_offset);
 
   gdk_draw_drawable (private->impl, tmp_gc, paint->pixmap,
                      clip_box.x - paint->x_offset,
                      clip_box.y - paint->y_offset,
                      clip_box.x - x_offset, clip_box.y - y_offset,
                      clip_box.width, clip_box.height);
-  gdk_gc_unref (tmp_gc);
 
-  if (private->paint_stack)
-    {
-      GSList *tmp_list = private->paint_stack;
-      while (tmp_list)
-       {
-         GdkWindowPaint *tmp_paint = tmp_list->data;
-         gdk_region_subtract (tmp_paint->region, paint->region);
-          
-         tmp_list = tmp_list->next;
-       }
-    }
-  else
-    gdk_drawable_unref (paint->pixmap);
+  /* Reset clip region of the cached GdkGC */
+  gdk_gc_set_clip_region (tmp_gc, NULL);
 
+  cairo_surface_destroy (paint->surface);
+  g_object_unref (paint->pixmap);
   gdk_region_destroy (paint->region);
   g_free (paint);
 #endif /* USE_BACKING_STORE */
@@ -1086,7 +1073,7 @@ gdk_window_free_paint_stack (GdkWindow *window)
          GdkWindowPaint *paint = tmp_list->data;
 
          if (tmp_list == private->paint_stack)
-           gdk_drawable_unref (paint->pixmap);
+           g_object_unref (paint->pixmap);
                  
          gdk_region_destroy (paint->region);
          g_free (paint);
@@ -1119,11 +1106,14 @@ gdk_window_get_offsets (GdkWindow *window,
 /**
  * gdk_window_get_internal_paint_info:
  * @window: a #GdkWindow
- * @real_drawable: location to store the drawable to which drawing should be done.
- * @x_offset: location to store the X offset between coordinates in @window, and
- *            the underlying window system primitive coordinates for *@real_drawable.
- * @y_offset: location to store the Y offset between coordinates in @window, and
- *            the underlying window system primitive coordinates for *@real_drawable.
+ * @real_drawable: location to store the drawable to which drawing should be 
+ *            done.
+ * @x_offset: location to store the X offset between coordinates in @window,
+ *            and the underlying window system primitive coordinates for 
+ *            *@real_drawable.
+ * @y_offset: location to store the Y offset between coordinates in @window,
+ *            and the underlying window system primitive coordinates for
+ *            *@real_drawable.
  * 
  * If you bypass the GDK layer and use windowing system primitives to
  * draw directly onto a #GdkWindow, then you need to deal with two
@@ -1208,7 +1198,7 @@ gdk_window_create_gc (GdkDrawable     *drawable,
 static void
 gdk_window_draw_rectangle (GdkDrawable *drawable,
                           GdkGC       *gc,
-                          gint         filled,
+                          gboolean     filled,
                           gint         x,
                           gint         y,
                           gint         width,
@@ -1236,7 +1226,7 @@ gdk_window_draw_rectangle (GdkDrawable *drawable,
 static void
 gdk_window_draw_arc (GdkDrawable *drawable,
                     GdkGC       *gc,
-                    gint         filled,
+                    gboolean     filled,
                     gint         x,
                     gint         y,
                     gint         width,
@@ -1267,7 +1257,7 @@ gdk_window_draw_arc (GdkDrawable *drawable,
 static void
 gdk_window_draw_polygon (GdkDrawable *drawable,
                         GdkGC       *gc,
-                        gint         filled,
+                        gboolean     filled,
                         GdkPoint    *points,
                         gint         npoints)
 {
@@ -1366,7 +1356,7 @@ gdk_window_draw_text_wc (GdkDrawable    *drawable,
 }
 
 static GdkDrawable*
-gdk_window_get_composite_drawable (GdkDrawable *window,
+gdk_window_get_composite_drawable (GdkDrawable *drawable,
                                    gint         x,
                                    gint         y,
                                    gint         width,
@@ -1374,51 +1364,22 @@ gdk_window_get_composite_drawable (GdkDrawable *window,
                                    gint        *composite_x_offset,
                                    gint        *composite_y_offset)
 {
-  GdkWindowObject *private = (GdkWindowObject *)window;
-  GdkWindowPaint *paint;
-  GdkRegion *buffered_region;
-  GSList *tmp_list;
-  GdkPixmap *buffer;
+  GdkWindowObject *private = (GdkWindowObject *)drawable;
+  GSList *list;
   GdkPixmap *tmp_pixmap;
   GdkRectangle rect;
-  GdkRegion *rect_region;
   GdkGC *tmp_gc;
-  gint windowing_x_offset, windowing_y_offset;
-  gint buffer_x_offset, buffer_y_offset;
-
-  if (GDK_WINDOW_DESTROYED (window) || private->paint_stack == NULL)
-    {
-      /* No backing store */
-      _gdk_windowing_window_get_offsets (window,
-                                         composite_x_offset,
-                                         composite_y_offset);
-      
-      return GDK_DRAWABLE (g_object_ref (G_OBJECT (window)));
-    }
-  
-  buffered_region = NULL;
-  buffer = NULL;
+  gboolean overlap_buffer;
 
-  /* All GtkWindowPaint structs have the same pixmap and offsets, just
-   * get the first one. (should probably be cleaned up so that the
-   * pixmap is stored in the window)
-   */
-  paint = private->paint_stack->data;
-  buffer = paint->pixmap;
-  buffer_x_offset = paint->x_offset;
-  buffer_y_offset = paint->y_offset;
+  _gdk_windowing_window_get_offsets (drawable,
+                                    composite_x_offset,
+                                    composite_y_offset);
   
-  tmp_list = private->paint_stack;
-  while (tmp_list != NULL)
+  if ((GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
+      || private->paint_stack == NULL)
     {
-      paint = tmp_list->data;
-      
-      if (buffered_region == NULL)
-        buffered_region = gdk_region_copy (paint->region);
-      else
-        gdk_region_union (buffered_region, paint->region);
-
-      tmp_list = g_slist_next (tmp_list);
+      /* No backing store */
+      return g_object_ref (drawable);
     }
 
   /* See if the buffered part is overlapping the part we want
@@ -1429,71 +1390,65 @@ gdk_window_get_composite_drawable (GdkDrawable *window,
   rect.width = width;
   rect.height = height;
 
-  rect_region = gdk_region_rectangle (&rect);
+  overlap_buffer = FALSE;
   
-  gdk_region_intersect (buffered_region, rect_region);
-
-  gdk_region_destroy (rect_region);
-
-  if (gdk_region_empty (buffered_region))
+  for (list = private->paint_stack; list != NULL; list = list->next)
     {
-      gdk_region_destroy (buffered_region);
+      GdkWindowPaint *paint = list->data;
+      GdkOverlapType overlap;
 
-      _gdk_windowing_window_get_offsets (window,
-                                         composite_x_offset,
-                                         composite_y_offset);
+      overlap = gdk_region_rect_in (paint->region, &rect);
 
-      return GDK_DRAWABLE (g_object_ref (G_OBJECT (window)));
+      if (overlap == GDK_OVERLAP_RECTANGLE_IN)
+       {
+         *composite_x_offset = paint->x_offset;
+         *composite_y_offset = paint->y_offset;
+         
+         return g_object_ref (paint->pixmap);
+       }
+      else if (overlap == GDK_OVERLAP_RECTANGLE_PART)
+       {
+         overlap_buffer = TRUE;
+         break;
+       }
     }
-  
-  tmp_pixmap = gdk_pixmap_new (window,
-                               width, height,
-                               -1);
 
-  tmp_gc = gdk_gc_new (tmp_pixmap);
+  if (!overlap_buffer)
+    return g_object_ref (drawable);
+
+  tmp_pixmap = gdk_pixmap_new (drawable, width, height, -1);
+  tmp_gc = _gdk_drawable_get_scratch_gc (tmp_pixmap, FALSE);
 
-  _gdk_windowing_window_get_offsets (window,
-                                     &windowing_x_offset,
-                                     &windowing_y_offset);
-  
   /* Copy the current window contents */
   gdk_draw_drawable (tmp_pixmap,
                      tmp_gc,
                      private->impl,
-                     x - windowing_x_offset,
-                     y - windowing_y_offset,
+                     x - *composite_x_offset,
+                     y - *composite_y_offset,
                      0, 0,
                      width, height);
 
-  /* Make buffered_region relative to the tmp_pixmap */
-  gdk_region_offset (buffered_region,
-                     - x,
-                     - y);
-  
-  /* Set the clip mask to avoid drawing over non-buffered areas of
-   * tmp_pixmap. 
-   */
-  
-  gdk_gc_set_clip_region (tmp_gc, buffered_region);
-  gdk_region_destroy (buffered_region);
-  
-  /* Draw backing pixmap onto the tmp_pixmap, offsetting
-   * appropriately.
-   */
-  gdk_draw_drawable (tmp_pixmap,
-                     tmp_gc,
-                     buffer,
-                     x - buffer_x_offset,
-                     y - buffer_y_offset,
-                     0, 0,
-                     width, height);
-  
+  /* paint the backing stores */
+  for (list = private->paint_stack; list != NULL; list = list->next)
+    {
+      GdkWindowPaint *paint = list->data;
+
+      gdk_gc_set_clip_region (tmp_gc, paint->region);
+      gdk_gc_set_clip_origin (tmp_gc, -x, -y);
+      
+      gdk_draw_drawable (tmp_pixmap, tmp_gc, paint->pixmap,
+                        x - paint->x_offset,
+                        y - paint->y_offset,
+                        0, 0, width, height);
+    }
+
+  /* Reset clip region of the cached GdkGC */
+  gdk_gc_set_clip_region (tmp_gc, NULL);
+
   /* Set these to location of tmp_pixmap within the window */
   *composite_x_offset = x;
   *composite_y_offset = y;
 
-  g_object_unref (G_OBJECT (tmp_gc));
-  
   return tmp_pixmap;
 }
 
@@ -1725,7 +1680,91 @@ gdk_window_draw_glyphs (GdkDrawable      *drawable,
   RESTORE_GC (gc);
 }
 
-/* Fixme - this is just like gdk_window_paint_init_bg */
+static void
+gdk_window_draw_glyphs_transformed (GdkDrawable      *drawable,
+                                   GdkGC            *gc,
+                                   PangoMatrix      *matrix,
+                                   PangoFont        *font,
+                                   gint              x,
+                                   gint              y,
+                                   PangoGlyphString *glyphs)
+{
+  GdkWindowObject *private = (GdkWindowObject *)drawable;
+  PangoMatrix tmp_matrix;
+
+  OFFSET_GC (gc);
+
+  if (GDK_WINDOW_DESTROYED (drawable))
+    return;
+
+  if (x_offset != 0 || y_offset != 0)
+    {
+      if (matrix)
+       {
+         tmp_matrix = *matrix;
+         tmp_matrix.x0 -= x_offset;
+         tmp_matrix.y0 -= y_offset;
+         matrix = &tmp_matrix;
+       }
+      else
+       {
+         x -= x_offset * PANGO_SCALE;
+         y -= y_offset * PANGO_SCALE;
+       }
+    }
+  
+  if (private->paint_stack)
+    {
+      GdkWindowPaint *paint = private->paint_stack->data;
+
+      gdk_draw_glyphs_transformed (paint->pixmap, gc, matrix, font, x, y, glyphs);
+    }
+  else
+    gdk_draw_glyphs_transformed (private->impl, gc, matrix, font, x, y, glyphs);
+
+  RESTORE_GC (gc);
+}
+
+static void
+gdk_window_set_bg_pattern (GdkWindow      *window,
+                          cairo_t        *cr,
+                          int             x_offset,
+                          int             y_offset)
+{
+  GdkWindowObject *private = (GdkWindowObject *)window;
+
+  if (private->bg_pixmap == GDK_PARENT_RELATIVE_BG && private->parent)
+    {
+      x_offset += private->x;
+      y_offset += private->y;
+      gdk_window_set_bg_pattern (GDK_WINDOW (private->parent), cr,
+                                x_offset, y_offset);
+    }
+  else if (private->bg_pixmap && 
+           private->bg_pixmap != GDK_PARENT_RELATIVE_BG && 
+           private->bg_pixmap != GDK_NO_BG)
+    {
+      cairo_surface_t *surface = _gdk_drawable_ref_cairo_surface (private->bg_pixmap);
+      cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);
+      cairo_surface_destroy (surface);
+
+      if (x_offset != 0 || y_offset != 0)
+       {
+         cairo_matrix_t matrix;
+         cairo_matrix_init_translate (&matrix, x_offset, y_offset);
+         cairo_pattern_set_matrix (pattern, &matrix);
+       }
+
+      cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
+      cairo_set_source (cr, pattern);
+      cairo_pattern_destroy (pattern);
+    }
+  else
+    {
+      gdk_cairo_set_source_color (cr, &private->bg_color);
+    }
+}
+
 static void
 gdk_window_clear_backing_rect (GdkWindow *window,
                               gint       x,
@@ -1735,15 +1774,22 @@ gdk_window_clear_backing_rect (GdkWindow *window,
 {
   GdkWindowObject *private = (GdkWindowObject *)window;
   GdkWindowPaint *paint = private->paint_stack->data;
-  GdkGC *tmp_gc;
+  cairo_t *cr;
 
   if (GDK_WINDOW_DESTROYED (window))
     return;
-  
-  tmp_gc = gdk_window_get_bg_gc (window, paint);
-  gdk_draw_rectangle (paint->pixmap, tmp_gc, TRUE,
-                     x - paint->x_offset, y - paint->y_offset, width, height);
-  gdk_gc_unref (tmp_gc);
+
+  cr = cairo_create (paint->surface);
+
+  gdk_window_set_bg_pattern (window, cr, 0, 0);
+
+  cairo_rectangle (cr, x, y, width, height);
+  cairo_clip (cr);
+
+  gdk_cairo_region (cr, paint->region);
+  cairo_fill (cr);
+
+  cairo_destroy (cr);
 }
 
 /**
@@ -1862,6 +1908,111 @@ gdk_window_draw_image (GdkDrawable *drawable,
   RESTORE_GC (gc);
 }
 
+static void
+gdk_window_draw_pixbuf (GdkDrawable     *drawable,
+                       GdkGC           *gc,
+                       GdkPixbuf       *pixbuf,
+                       gint             src_x,
+                       gint             src_y,
+                       gint             dest_x,
+                       gint             dest_y,
+                       gint             width,
+                       gint             height,
+                       GdkRgbDither     dither,
+                       gint             x_dither,
+                       gint             y_dither)
+{
+  GdkWindowObject *private = (GdkWindowObject *)drawable;
+
+  if (GDK_WINDOW_DESTROYED (drawable))
+    return;
+  
+  if (gc)
+    {
+      OFFSET_GC (gc);
+  
+      if (private->paint_stack)
+       {
+         GdkWindowPaint *paint = private->paint_stack->data;
+         gdk_draw_pixbuf (paint->pixmap, gc, pixbuf, src_x, src_y,
+                          dest_x - x_offset, dest_y - y_offset,
+                          width, height,
+                          dither, x_dither - x_offset, y_dither - y_offset);
+       }
+      else
+       gdk_draw_pixbuf (private->impl, gc, pixbuf, src_x, src_y,
+                        dest_x - x_offset, dest_y - y_offset,
+                        width, height,
+                        dither, x_dither, y_dither);
+      
+      RESTORE_GC (gc);
+    }
+  else
+    {
+      gint x_offset, y_offset;
+      gdk_window_get_offsets (drawable, &x_offset, &y_offset);
+      
+      if (private->paint_stack)
+       {
+         GdkWindowPaint *paint = private->paint_stack->data;
+         gdk_draw_pixbuf (paint->pixmap, gc, pixbuf, src_x, src_y,
+                          dest_x - x_offset, dest_y - y_offset,
+                          width, height,
+                           dither, x_dither - x_offset, y_dither - y_offset);
+       }
+      else
+       gdk_draw_pixbuf (private->impl, gc, pixbuf, src_x, src_y,
+                        dest_x - x_offset, dest_y - y_offset,
+                        width, height,
+                        dither, x_dither, y_dither);
+    }
+}
+
+static void
+gdk_window_draw_trapezoids (GdkDrawable   *drawable,
+                           GdkGC         *gc,
+                           GdkTrapezoid  *trapezoids,
+                           gint           n_trapezoids)
+{
+  GdkWindowObject *private = (GdkWindowObject *)drawable;
+  GdkTrapezoid *new_trapezoids = NULL;
+
+  OFFSET_GC (gc);
+
+  if (GDK_WINDOW_DESTROYED (drawable))
+    return;
+  
+  if (x_offset != 0 || y_offset != 0)
+    {
+      gint i;
+
+      new_trapezoids = g_new (GdkTrapezoid, n_trapezoids);
+      for (i=0; i < n_trapezoids; i++)
+       {
+         new_trapezoids[i].y1 = trapezoids[i].y1 - y_offset;
+         new_trapezoids[i].x11 = trapezoids[i].x11 - x_offset;
+         new_trapezoids[i].x21 = trapezoids[i].x21 - x_offset;
+         new_trapezoids[i].y2 = trapezoids[i].y2 - y_offset;
+         new_trapezoids[i].x12 = trapezoids[i].x12 - x_offset;
+         new_trapezoids[i].x22 = trapezoids[i].x22 - x_offset;
+       }
+
+      trapezoids = new_trapezoids;
+    }
+
+  if (private->paint_stack)
+    {
+      GdkWindowPaint *paint = private->paint_stack->data;
+      gdk_draw_trapezoids (paint->pixmap, gc, trapezoids, n_trapezoids);
+    }
+  else
+    gdk_draw_trapezoids (private->impl, gc, trapezoids, n_trapezoids);
+  
+  if (new_trapezoids)
+    g_free (new_trapezoids);
+
+  RESTORE_GC (gc);
+}
 
 static void
 gdk_window_real_get_size (GdkDrawable *drawable,
@@ -1888,19 +2039,15 @@ gdk_window_real_get_visual (GdkDrawable *drawable)
 static gint
 gdk_window_real_get_depth (GdkDrawable *drawable)
 {
-  gint depth;
-  
   g_return_val_if_fail (GDK_IS_WINDOW (drawable), 0);
 
-  depth = ((GdkWindowObject *)GDK_WINDOW (drawable))->depth;
-
-  if (depth == 0)
-    {
-      g_print ("0 depth for type %s\n", g_type_name (G_OBJECT_TYPE (drawable)));
-      G_BREAKPOINT ();
-    }
+  return ((GdkWindowObject *)GDK_WINDOW (drawable))->depth;
+}
 
-  return depth;
+static GdkScreen*
+gdk_window_real_get_screen (GdkDrawable *drawable)
+{
+  return gdk_drawable_get_screen (GDK_WINDOW_OBJECT (drawable)->impl);
 }
 
 static void
@@ -1927,11 +2074,14 @@ gdk_window_real_get_colormap (GdkDrawable *drawable)
 }
                       
 static GdkImage*
-gdk_window_get_image (GdkDrawable *drawable,
-                      gint         x,
-                      gint         y,
-                      gint         width,
-                      gint         height)
+gdk_window_copy_to_image (GdkDrawable     *drawable,
+                         GdkImage        *image,
+                         gint             src_x,
+                         gint             src_y,
+                         gint             dest_x,
+                         gint             dest_y,
+                         gint             width,
+                         gint             height)
 {
   gint x_offset, y_offset;
   
@@ -1946,19 +2096,65 @@ gdk_window_get_image (GdkDrawable *drawable,
   
   _gdk_windowing_window_get_offsets (drawable, &x_offset, &y_offset);
   
-  return gdk_drawable_get_image (((GdkWindowObject*)drawable)->impl,
-                                 x - x_offset,
-                                 y - y_offset,
-                                 width, height);
+  return gdk_drawable_copy_to_image (((GdkWindowObject*)drawable)->impl,
+                                    image,
+                                    src_x - x_offset,
+                                    src_y - y_offset,
+                                    dest_x, dest_y,
+                                    width, height);
+}
+
+static cairo_surface_t *
+gdk_window_ref_cairo_surface (GdkDrawable *drawable)
+{
+  GdkWindowObject *private = (GdkWindowObject*) drawable;
+  cairo_surface_t *surface;
+  gint x_offset, y_offset;
+  
+  gdk_window_get_offsets (GDK_WINDOW (drawable), &x_offset, &y_offset);
+
+  if (private->paint_stack)
+    {
+      GdkWindowPaint *paint = private->paint_stack->data;
+
+      surface = paint->surface;
+      cairo_surface_reference (surface);
+    }
+  else
+    surface = _gdk_drawable_ref_cairo_surface (private->impl);
+
+  return surface;
 }
 
 /* Code for dirty-region queueing
  */
-
 static GSList *update_windows = NULL;
 static guint update_idle = 0;
 static gboolean debug_updates = FALSE;
 
+static gboolean
+gdk_window_update_idle (gpointer data)
+{
+  GDK_THREADS_ENTER ();
+  gdk_window_process_all_updates ();
+  GDK_THREADS_LEAVE ();
+  
+  return FALSE;
+}
+
+static void
+gdk_window_schedule_update (GdkWindow *window)
+{
+  if (window && GDK_WINDOW_OBJECT (window)->update_freeze_count)
+    return;
+
+  if (!update_idle)
+    {
+      update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW,
+                                    gdk_window_update_idle, NULL, NULL);
+    }
+}
+
 static void
 gdk_window_process_updates_internal (GdkWindow *window)
 {
@@ -1976,7 +2172,6 @@ gdk_window_process_updates_internal (GdkWindow *window)
       
       if (_gdk_event_func && gdk_window_is_viewable (window))
        {
-         GdkEvent event;
          GdkRectangle window_rect;
          GdkRegion *expose_region;
          GdkRegion *window_region;
@@ -1985,49 +2180,65 @@ gdk_window_process_updates_internal (GdkWindow *window)
           if (debug_updates)
             {
               /* Make sure we see the red invalid area before redrawing. */
-              gdk_flush ();
+              gdk_display_sync (gdk_drawable_get_display (window));
               g_usleep (70000);
             }
           
-          gdk_drawable_get_size (GDK_DRAWABLE (private), &width, &height);
-          
-         window_rect.x = 0;
-         window_rect.y = 0;
-         window_rect.width = width;
-         window_rect.height = height;
-
          save_region = _gdk_windowing_window_queue_antiexpose (window, update_area);
-      
-         event.expose.type = GDK_EXPOSE;
-         event.expose.window = gdk_window_ref (window);
-         event.expose.count = 0;
 
          if (save_region)
            expose_region = gdk_region_copy (update_area);
          else
            expose_region = update_area;
+         
+          gdk_drawable_get_size (GDK_DRAWABLE (private), &width, &height);
+
+         window_rect.x = 0;
+         window_rect.y = 0;
+         window_rect.width = width;
+         window_rect.height = height;
+
          window_region = gdk_region_rectangle (&window_rect);
          gdk_region_intersect (expose_region,
                                window_region);
          gdk_region_destroy (window_region);
          
-         event.expose.region = expose_region;
-         gdk_region_get_clipbox (expose_region, &event.expose.area);
-         
-         if (!gdk_region_empty (expose_region))
+         if (!gdk_region_empty (expose_region) &&
+             (private->event_mask & GDK_EXPOSURE_MASK))
            {
+             GdkEvent event;
+             
+             event.expose.type = GDK_EXPOSE;
+             event.expose.window = g_object_ref (window);
+             event.expose.count = 0;
+             event.expose.region = expose_region;
+             gdk_region_get_clipbox (expose_region, &event.expose.area);
+             
              (*_gdk_event_func) (&event, _gdk_event_data);
+             
+             g_object_unref (window);
            }
 
          if (expose_region != update_area)
            gdk_region_destroy (expose_region);
-         gdk_window_unref (window);
        }
       if (!save_region)
        gdk_region_destroy (update_area);
     }
 }
 
+static void
+flush_all_displays (void)
+{
+  GSList *displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
+  GSList *tmp_list;
+
+  for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
+    gdk_display_flush (tmp_list->data);
+
+  g_slist_free (displays);
+}
+
 /**
  * gdk_window_process_all_updates:
  *
@@ -2047,23 +2258,24 @@ gdk_window_process_all_updates (void)
   update_windows = NULL;
   update_idle = 0;
 
+  g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
+  
   while (tmp_list)
     {
-      gdk_window_process_updates_internal (tmp_list->data);
+      GdkWindowObject *private = (GdkWindowObject *)tmp_list->data;
+      
+      if (private->update_freeze_count)
+       update_windows = g_slist_prepend (update_windows, private);
+      else
+       gdk_window_process_updates_internal (tmp_list->data);
+      
+      g_object_unref (tmp_list->data);
       tmp_list = tmp_list->next;
     }
 
   g_slist_free (old_update_windows);
 
-  gdk_flush();
-}
-
-static gboolean
-gdk_window_update_idle (gpointer data)
-{
-  gdk_window_process_all_updates ();
-  
-  return FALSE;
+  flush_all_displays ();
 }
 
 /**
@@ -2090,7 +2302,7 @@ gdk_window_process_updates (GdkWindow *window,
   g_return_if_fail (window != NULL);
   g_return_if_fail (GDK_IS_WINDOW (window));
   
-  if (private->update_area)
+  if (private->update_area && !private->update_freeze_count)
     {      
       gdk_window_process_updates_internal (window);
       update_windows = g_slist_remove (update_windows, window);
@@ -2151,13 +2363,37 @@ gdk_window_invalidate_rect   (GdkWindow    *window,
   gdk_region_destroy (region);
 }
 
+static void
+draw_ugly_color (GdkWindow *window,
+                GdkRegion *region)
+{
+  /* Draw ugly color all over the newly-invalid region */
+  GdkColor ugly_color = { 0, 50000, 10000, 10000 };
+  GdkGC *ugly_gc;
+  GdkRectangle clipbox;
+    
+  ugly_gc = gdk_gc_new (window);
+  gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
+  gdk_gc_set_clip_region (ugly_gc, region);
+
+  gdk_region_get_clipbox (region, &clipbox);
+  
+  gdk_draw_rectangle (window,
+                     ugly_gc,
+                     TRUE,
+                     clipbox.x, clipbox.y,
+                     clipbox.width, clipbox.height);
+  
+  g_object_unref (ugly_gc);
+}
+
 /**
  * gdk_window_invalidate_maybe_recurse:
  * @window: a #GdkWindow
  * @region: a #GdkRegion
  * @child_func: function to use to decide if to recurse to a child,
  *              %NULL means never recurse.
- * @child_func_data: data passed to @child_func
+ * @user_data: data passed to @child_func
  *
  * Adds @region to the update area for @window. The update area is the
  * region that needs to be redrawn, or "dirty region." The call
@@ -2173,7 +2409,7 @@ gdk_window_invalidate_rect   (GdkWindow    *window,
  *
  * The @child_func parameter controls whether the region of
  * each child window that intersects @region will also be invalidated.
- * Only children for whic @child_func returns TRUE will have the area
+ * Only children for which @child_func returns TRUE will have the area
  * invalidated.
  **/
 void
@@ -2184,6 +2420,7 @@ gdk_window_invalidate_maybe_recurse (GdkWindow *window,
 {
   GdkWindowObject *private = (GdkWindowObject *)window;
   GdkRegion *visible_region;
+  GList *tmp_list;
 
   g_return_if_fail (window != NULL);
   g_return_if_fail (GDK_IS_WINDOW (window));
@@ -2197,29 +2434,49 @@ gdk_window_invalidate_maybe_recurse (GdkWindow *window,
   visible_region = gdk_drawable_get_visible_region (window);
   gdk_region_intersect (visible_region, region);
 
-  if (!gdk_region_empty (visible_region))
+  tmp_list = private->children;
+  while (tmp_list)
     {
-      if (debug_updates)
-        {
-          /* Draw ugly color all over the newly-invalid region */
-          GdkRectangle ugly_rect;
-          GdkGC *ugly_gc;
-          GdkColor ugly_color = { 0, 60000, 10000, 10000 };
-          
-          ugly_gc = gdk_gc_new (window);
+      GdkWindowObject *child = tmp_list->data;
+      
+      if (!child->input_only)
+       {
+         GdkRegion *child_region;
+         GdkRectangle child_rect;
+         
+         gdk_window_get_position ((GdkWindow *)child,
+                                  &child_rect.x, &child_rect.y);
+         gdk_drawable_get_size ((GdkDrawable *)child,
+                                &child_rect.width, &child_rect.height);
 
-          gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
-          
-         gdk_region_get_clipbox (visible_region, &ugly_rect);
+         child_region = gdk_region_rectangle (&child_rect);
+         
+         /* remove child area from the invalid area of the parent */
+         if (GDK_WINDOW_IS_MAPPED (child))
+           gdk_region_subtract (visible_region, child_region);
+         
+         if (child_func && (*child_func) ((GdkWindow *)child, user_data))
+           {
+             gdk_region_offset (region, - child_rect.x, - child_rect.y);
+             gdk_region_offset (child_region, - child_rect.x, - child_rect.y);
+             gdk_region_intersect (child_region, region);
+             
+             gdk_window_invalidate_maybe_recurse ((GdkWindow *)child,
+                                                  child_region, child_func, user_data);
+             
+             gdk_region_offset (region, child_rect.x, child_rect.y);
+           }
 
-          gdk_draw_rectangle (window,
-                              ugly_gc,
-                              TRUE,
-                              ugly_rect.x, ugly_rect.y,
-                              ugly_rect.width, ugly_rect.height);
-          
-          g_object_unref (G_OBJECT (ugly_gc));
-        }
+         gdk_region_destroy (child_region);
+       }
+
+      tmp_list = tmp_list->next;
+    }
+  
+  if (!gdk_region_empty (visible_region))
+    {
+      if (debug_updates)
+        draw_ugly_color (window, region);
       
       if (private->update_area)
        {
@@ -2230,37 +2487,7 @@ gdk_window_invalidate_maybe_recurse (GdkWindow *window,
          update_windows = g_slist_prepend (update_windows, window);
          private->update_area = gdk_region_copy (visible_region);
          
-         if (!private->update_freeze_count && !update_idle)
-           update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW,
-                                          gdk_window_update_idle, NULL, NULL);
-       }
-      
-      if (child_func)
-       {
-         GList *tmp_list;
-         
-         tmp_list = private->children;
-         while (tmp_list)
-           {
-             GdkWindowObject *child = tmp_list->data;
-             tmp_list = tmp_list->next;
-
-             if (!child->input_only && (*child_func) ((GdkWindow *)child, user_data))
-               {
-                 GdkRegion *child_region;
-                 gint x, y;
-
-                 gdk_window_get_position ((GdkWindow *)child, &x, &y);
-
-                 /* This copy could be saved with a little more complexity */
-                 child_region = gdk_region_copy (visible_region);
-                 gdk_region_offset (child_region, -x, -y);
-                 
-                 gdk_window_invalidate_region ((GdkWindow *)child, child_region, TRUE);
-                 
-                 gdk_region_destroy (child_region);
-               }
-           }
+         gdk_window_schedule_update (window);
        }
     }
   
@@ -2404,10 +2631,8 @@ gdk_window_thaw_updates (GdkWindow *window)
   g_return_if_fail (GDK_IS_WINDOW (window));
   g_return_if_fail (private->update_freeze_count > 0);
 
-  private->update_freeze_count--;
-  if (!private->update_freeze_count && private->update_area && !update_idle)
-    update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW,
-                                  gdk_window_update_idle, NULL, NULL);
+  if (--private->update_freeze_count == 0)
+    gdk_window_schedule_update (window);
 }
 
 /**
@@ -2539,7 +2764,7 @@ gdk_window_constrain_size (GdkGeometry *geometry,
 
       if (geometry->min_aspect * height > width)
        {
-         delta = FLOOR (height - width * geometry->min_aspect, yinc);
+         delta = FLOOR (height - width / geometry->min_aspect, yinc);
          if (height - delta >= min_height)
            height -= delta;
          else
@@ -2570,33 +2795,6 @@ gdk_window_constrain_size (GdkGeometry *geometry,
   *new_height = height;
 }
 
-/**
- * gdk_set_pointer_hooks:
- * @new_hooks: a table of pointer to functions for getting
- *   quantities related to the current pointer position,
- *   or %NULL to restore the default table.
- * 
- * This function allows for hooking into the operation
- * of getting the current location of the pointer. This
- * is only useful for such low-level tools as an
- * event recorder. Applications should never have any
- * reason to use this facility
- * 
- * Return value: the previous pointer hook table
- **/
-GdkPointerHooks *
-gdk_set_pointer_hooks (const GdkPointerHooks *new_hooks)
-{
-  const GdkPointerHooks *result = current_pointer_hooks;
-
-  if (new_hooks)
-    current_pointer_hooks = new_hooks;
-  else
-    current_pointer_hooks = &default_pointer_hooks;
-
-  return (GdkPointerHooks *)result;
-}
-
 /**
  * gdk_window_get_pointer:
  * @window: a #GdkWindow
@@ -2607,18 +2805,49 @@ gdk_set_pointer_hooks (const GdkPointerHooks *new_hooks)
  * Obtains the current pointer position and modifier state.
  * The position is given in coordinates relative to @window.
  * 
- * Return value: the window containing the pointer (as with gdk_window_at_pointer()), or %NULL
- *               if the window containing the pointer isn't known to GDK
+ * Return value: the window containing the pointer (as with
+ * gdk_window_at_pointer()), or %NULL if the window containing the
+ * pointer isn't known to GDK
  **/
 GdkWindow*
 gdk_window_get_pointer (GdkWindow        *window,
                        gint              *x,
                        gint              *y,
-                       GdkModifierType *mask)
+                       GdkModifierType   *mask)
 {
-  g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
+  GdkDisplay *display;
+  gint tmp_x, tmp_y;
+  GdkModifierType tmp_mask;
+  GdkWindow *child;
   
-  return current_pointer_hooks->get_pointer (window, x, y, mask); 
+  g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
+
+  if (window)
+    {
+      display = gdk_drawable_get_display (window);
+    }
+  else
+    {
+      GdkScreen *screen = gdk_screen_get_default ();
+
+      display = gdk_screen_get_display (screen);
+      window = gdk_screen_get_root_window (screen);
+      
+      GDK_NOTE (MULTIHEAD,
+               g_message ("Passing NULL for window to gdk_window_get_pointer()\n"
+                          "is not multihead safe"));
+    }
+
+  child = display->pointer_hooks->window_get_pointer (display, window, &tmp_x, &tmp_y, &tmp_mask);
+
+  if (x)
+    *x = tmp_x;
+  if (y)
+    *y = tmp_y;
+  if (mask)
+    *mask = tmp_mask;
+
+  return child;
 }
 
 /**
@@ -2626,10 +2855,14 @@ gdk_window_get_pointer (GdkWindow         *window,
  * @win_x: return location for origin of the window under the pointer
  * @win_y: return location for origin of the window under the pointer
  * 
- * Obtains the window underneath the mouse pointer, returning the location
- * of that window in @win_x, @win_y. Returns %NULL if the window under
- * the mouse pointer is not known to GDK (for example, belongs to
- * another application).
+ * Obtains the window underneath the mouse pointer, returning the
+ * location of that window in @win_x, @win_y. Returns %NULL if the
+ * window under the mouse pointer is not known to GDK (if the window
+ * belongs to another application and a #GdkWindow hasn't been created
+ * for it with gdk_window_foreign_new())
+ *
+ * NOTE: For multihead-aware widgets or applications use
+ * gdk_display_get_window_at_pointer() instead.
  * 
  * Return value: window under the mouse pointer
  **/
@@ -2637,7 +2870,7 @@ GdkWindow*
 gdk_window_at_pointer (gint *win_x,
                       gint *win_y)
 {
-  return current_pointer_hooks->window_at_pointer (win_x, win_y);
+  return gdk_display_get_window_at_pointer (gdk_display_get_default (), win_x, win_y);
 }
 
 /**
@@ -2651,6 +2884,27 @@ gdk_window_at_pointer (gint *win_x,
 GdkWindow *
 gdk_get_default_root_window (void)
 {
-  return _gdk_parent_root;
+  return gdk_screen_get_root_window (gdk_screen_get_default ());
+}
+
+/**
+ * gdk_window_foreign_new:
+ * @anid: a native window handle.
+ * 
+ * Wraps a native window for the default display in a #GdkWindow.
+ * This may fail if the window has been destroyed.
+ *
+ * For example in the X backend, a native window handle is an Xlib
+ * <type>XID</type>.
+ * 
+ * Return value: the newly-created #GdkWindow wrapper for the 
+ *    native window or %NULL if the window has been destroyed.
+ **/
+GdkWindow *
+gdk_window_foreign_new (GdkNativeWindow anid)
+{
+  return gdk_window_foreign_new_for_display (gdk_display_get_default (), anid);
 }
 
+#define __GDK_WINDOW_C__
+#include "gdkaliasdef.c"