]> Pileus Git - ~andy/gtk/blob - gdk/gdkwindow.c
Cleanups
[~andy/gtk] / gdk / gdkwindow.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include "gdkwindow.h"
29 #include "gdkinternals.h"
30 #include "gdk.h"                /* For gdk_rectangle_union() */
31 #include "gdkpixmap.h"
32 #include "gdkdrawable.h"
33 #include "gdkscreen.h"
34 #include "gdkalias.h"
35
36 #define USE_BACKING_STORE       /* Appears to work on Win32, too, now. */
37
38 typedef struct _GdkWindowPaint GdkWindowPaint;
39
40 struct _GdkWindowPaint
41 {
42   GdkRegion *region;
43   GdkPixmap *pixmap;
44   gint x_offset;
45   gint y_offset;
46   cairo_surface_t *surface;
47 };
48
49 static GdkGC *gdk_window_create_gc      (GdkDrawable     *drawable,
50                                          GdkGCValues     *values,
51                                          GdkGCValuesMask  mask);
52 static void   gdk_window_draw_rectangle (GdkDrawable     *drawable,
53                                          GdkGC           *gc,
54                                          gboolean         filled,
55                                          gint             x,
56                                          gint             y,
57                                          gint             width,
58                                          gint             height);
59 static void   gdk_window_draw_arc       (GdkDrawable     *drawable,
60                                          GdkGC           *gc,
61                                          gboolean         filled,
62                                          gint             x,
63                                          gint             y,
64                                          gint             width,
65                                          gint             height,
66                                          gint             angle1,
67                                          gint             angle2);
68 static void   gdk_window_draw_polygon   (GdkDrawable     *drawable,
69                                          GdkGC           *gc,
70                                          gboolean         filled,
71                                          GdkPoint        *points,
72                                          gint             npoints);
73 static void   gdk_window_draw_text      (GdkDrawable     *drawable,
74                                          GdkFont         *font,
75                                          GdkGC           *gc,
76                                          gint             x,
77                                          gint             y,
78                                          const gchar     *text,
79                                          gint             text_length);
80 static void   gdk_window_draw_text_wc   (GdkDrawable     *drawable,
81                                          GdkFont         *font,
82                                          GdkGC           *gc,
83                                          gint             x,
84                                          gint             y,
85                                          const GdkWChar  *text,
86                                          gint             text_length);
87 static void   gdk_window_draw_drawable  (GdkDrawable     *drawable,
88                                          GdkGC           *gc,
89                                          GdkPixmap       *src,
90                                          gint             xsrc,
91                                          gint             ysrc,
92                                          gint             xdest,
93                                          gint             ydest,
94                                          gint             width,
95                                          gint             height);
96 static void   gdk_window_draw_points    (GdkDrawable     *drawable,
97                                          GdkGC           *gc,
98                                          GdkPoint        *points,
99                                          gint             npoints);
100 static void   gdk_window_draw_segments  (GdkDrawable     *drawable,
101                                          GdkGC           *gc,
102                                          GdkSegment      *segs,
103                                          gint             nsegs);
104 static void   gdk_window_draw_lines     (GdkDrawable     *drawable,
105                                          GdkGC           *gc,
106                                          GdkPoint        *points,
107                                          gint             npoints);
108
109 static void gdk_window_draw_glyphs             (GdkDrawable      *drawable,
110                                                 GdkGC            *gc,
111                                                 PangoFont        *font,
112                                                 gint              x,
113                                                 gint              y,
114                                                 PangoGlyphString *glyphs);
115 static void gdk_window_draw_glyphs_transformed (GdkDrawable      *drawable,
116                                                 GdkGC            *gc,
117                                                 PangoMatrix      *matrix,
118                                                 PangoFont        *font,
119                                                 gint              x,
120                                                 gint              y,
121                                                 PangoGlyphString *glyphs);
122
123 static void   gdk_window_draw_image     (GdkDrawable     *drawable,
124                                          GdkGC           *gc,
125                                          GdkImage        *image,
126                                          gint             xsrc,
127                                          gint             ysrc,
128                                          gint             xdest,
129                                          gint             ydest,
130                                          gint             width,
131                                          gint             height);
132
133 static void gdk_window_draw_pixbuf (GdkDrawable     *drawable,
134                                     GdkGC           *gc,
135                                     GdkPixbuf       *pixbuf,
136                                     gint             src_x,
137                                     gint             src_y,
138                                     gint             dest_x,
139                                     gint             dest_y,
140                                     gint             width,
141                                     gint             height,
142                                     GdkRgbDither     dither,
143                                     gint             x_dither,
144                                     gint             y_dither);
145
146 static void gdk_window_draw_trapezoids (GdkDrawable   *drawable,
147                                         GdkGC         *gc,
148                                         GdkTrapezoid  *trapezoids,
149                                         gint           n_trapezoids);
150
151 static GdkImage* gdk_window_copy_to_image (GdkDrawable *drawable,
152                                            GdkImage    *image,
153                                            gint         src_x,
154                                            gint         src_y,
155                                            gint         dest_x,
156                                            gint         dest_y,
157                                            gint         width,
158                                            gint         height);
159
160 static cairo_surface_t *gdk_window_ref_cairo_surface (GdkDrawable *drawable);
161
162 static void   gdk_window_real_get_size  (GdkDrawable     *drawable,
163                                          gint            *width,
164                                          gint            *height);
165
166 static GdkVisual*   gdk_window_real_get_visual   (GdkDrawable *drawable);
167 static gint         gdk_window_real_get_depth    (GdkDrawable *drawable);
168 static GdkScreen*   gdk_window_real_get_screen   (GdkDrawable *drawable);
169 static void         gdk_window_real_set_colormap (GdkDrawable *drawable,
170                                                   GdkColormap *cmap);
171 static GdkColormap* gdk_window_real_get_colormap (GdkDrawable *drawable);
172
173 static GdkDrawable* gdk_window_get_composite_drawable (GdkDrawable *drawable,
174                                                        gint         x,
175                                                        gint         y,
176                                                        gint         width,
177                                                        gint         height,
178                                                        gint        *composite_x_offset,
179                                                        gint        *composite_y_offset);
180 static GdkRegion*   gdk_window_get_clip_region        (GdkDrawable *drawable);
181 static GdkRegion*   gdk_window_get_visible_region     (GdkDrawable *drawable);
182
183 static void gdk_window_free_paint_stack (GdkWindow *window);
184
185 static void gdk_window_init       (GdkWindowObject      *window);
186 static void gdk_window_class_init (GdkWindowObjectClass *klass);
187 static void gdk_window_finalize   (GObject              *object);
188 static void gdk_window_clear_backing_rect (GdkWindow *window,
189                                            gint       x,
190                                            gint       y,
191                                            gint       width,
192                                            gint       height);
193
194 static gpointer parent_class = NULL;
195
196 GType
197 gdk_window_object_get_type (void)
198 {
199   static GType object_type = 0;
200
201   if (!object_type)
202     object_type = g_type_register_static_simple (GDK_TYPE_DRAWABLE,
203                                                  "GdkWindow",
204                                                  sizeof (GdkWindowObjectClass),
205                                                  (GClassInitFunc) gdk_window_class_init,
206                                                  sizeof (GdkWindowObject),
207                                                  (GInstanceInitFunc) gdk_window_init,
208                                                  0);
209   
210   return object_type;
211 }
212
213 GType
214 _gdk_paintable_get_type (void)
215 {
216   static GType paintable_type = 0;
217
218   if (!paintable_type)
219     {
220       const GTypeInfo paintable_info =
221       {
222         sizeof (GdkPaintableIface),  /* class_size */
223         NULL,                        /* base_init */
224         NULL,                        /* base_finalize */
225       };
226
227       paintable_type = g_type_register_static (G_TYPE_INTERFACE,
228                                                g_intern_static_string ("GdkPaintable"),
229                                                &paintable_info, 0);
230
231       g_type_interface_add_prerequisite (paintable_type, G_TYPE_OBJECT);
232     }
233
234   return paintable_type;
235 }
236
237 static void
238 gdk_window_init (GdkWindowObject *window)
239 {
240   /* 0-initialization is good for all other fields. */
241
242   window->window_type = GDK_WINDOW_CHILD;
243
244   window->state = GDK_WINDOW_STATE_WITHDRAWN;
245   
246   window->impl = g_object_new (_gdk_window_impl_get_type (), NULL);
247 }
248
249 static void
250 gdk_window_class_init (GdkWindowObjectClass *klass)
251 {
252   GObjectClass *object_class = G_OBJECT_CLASS (klass);
253   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
254   
255   parent_class = g_type_class_peek_parent (klass);
256
257   object_class->finalize = gdk_window_finalize;
258
259   drawable_class->create_gc = gdk_window_create_gc;
260   drawable_class->draw_rectangle = gdk_window_draw_rectangle;
261   drawable_class->draw_arc = gdk_window_draw_arc;
262   drawable_class->draw_polygon = gdk_window_draw_polygon;
263   drawable_class->draw_text = gdk_window_draw_text;
264   drawable_class->draw_text_wc = gdk_window_draw_text_wc;
265   drawable_class->draw_drawable = gdk_window_draw_drawable;
266   drawable_class->draw_points = gdk_window_draw_points;
267   drawable_class->draw_segments = gdk_window_draw_segments;
268   drawable_class->draw_lines = gdk_window_draw_lines;
269   drawable_class->draw_glyphs = gdk_window_draw_glyphs;
270   drawable_class->draw_glyphs_transformed = gdk_window_draw_glyphs_transformed;
271   drawable_class->draw_image = gdk_window_draw_image;
272   drawable_class->draw_pixbuf = gdk_window_draw_pixbuf;
273   drawable_class->draw_trapezoids = gdk_window_draw_trapezoids;
274   drawable_class->get_depth = gdk_window_real_get_depth;
275   drawable_class->get_screen = gdk_window_real_get_screen;
276   drawable_class->get_size = gdk_window_real_get_size;
277   drawable_class->set_colormap = gdk_window_real_set_colormap;
278   drawable_class->get_colormap = gdk_window_real_get_colormap;
279   drawable_class->get_visual = gdk_window_real_get_visual;
280   drawable_class->_copy_to_image = gdk_window_copy_to_image;
281   drawable_class->ref_cairo_surface = gdk_window_ref_cairo_surface;
282   drawable_class->get_clip_region = gdk_window_get_clip_region;
283   drawable_class->get_visible_region = gdk_window_get_visible_region;
284   drawable_class->get_composite_drawable = gdk_window_get_composite_drawable;
285 }
286
287 static void
288 gdk_window_finalize (GObject *object)
289 {
290   GdkWindow *window = GDK_WINDOW (object);
291   GdkWindowObject *obj = (GdkWindowObject *) object;
292   
293   if (!GDK_WINDOW_DESTROYED (window))
294     {
295       if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
296         {
297           g_warning ("losing last reference to undestroyed window\n");
298           _gdk_window_destroy (window, FALSE);
299         }
300       else
301         /* We use TRUE here, to keep us from actually calling
302          * XDestroyWindow() on the window
303          */
304         _gdk_window_destroy (window, TRUE);
305     }
306
307   g_object_unref (obj->impl);
308   obj->impl = NULL;
309   
310   G_OBJECT_CLASS (parent_class)->finalize (object);
311 }
312
313 static void
314 window_remove_filters (GdkWindow *window)
315 {
316   GdkWindowObject *obj = (GdkWindowObject*) window;
317
318   if (obj->filters)
319     {
320       GList *tmp_list;
321       
322       for (tmp_list = obj->filters; tmp_list; tmp_list = tmp_list->next)
323         g_free (tmp_list->data);
324     
325       g_list_free (obj->filters);
326       obj->filters = NULL;
327     }
328 }
329
330 /**
331  * _gdk_window_destroy_hierarchy:
332  * @window: a #GdkWindow
333  * @recursing: If TRUE, then this is being called because a parent
334  *            was destroyed. This generally means that the call to the 
335  *            windowing system to destroy the window can be omitted, since
336  *            it will be destroyed as a result of the parent being destroyed.
337  *            Unless @foreign_destroy.           
338  * @foreign_destroy: If TRUE, the window or a parent was destroyed by some 
339  *            external agency. The window has already been destroyed and no 
340  *            windowing system calls should be made. (This may never happen
341  *            for some windowing systems.)
342  *
343  * Internal function to destroy a window. Like gdk_window_destroy(),
344  * but does not drop the reference count created by gdk_window_new().
345  **/
346 static void
347 _gdk_window_destroy_hierarchy (GdkWindow *window,
348                                gboolean   recursing,
349                                gboolean   foreign_destroy)
350 {
351   GdkWindowObject *private;
352   GdkWindowObject *temp_private;
353   GdkWindow *temp_window;
354   GdkScreen *screen;
355   GList *children;
356   GList *tmp;
357   
358   g_return_if_fail (window != NULL);
359   
360   private = (GdkWindowObject*) window;
361   
362   if (GDK_WINDOW_DESTROYED (window))
363     return;
364     
365   switch (GDK_WINDOW_TYPE (window))
366     {
367     case GDK_WINDOW_ROOT:
368       screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
369       if (!screen->closed)
370         {
371           g_error ("attempted to destroy root window");
372           break;
373         }
374       /* else fall thru */
375     case GDK_WINDOW_TOPLEVEL:
376     case GDK_WINDOW_CHILD:
377     case GDK_WINDOW_DIALOG:
378     case GDK_WINDOW_TEMP:
379     case GDK_WINDOW_FOREIGN:
380       if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_FOREIGN && !foreign_destroy)
381         {
382           /* Logically, it probably makes more sense to send
383            * a "destroy yourself" message to the foreign window
384            * whether or not it's in our heirarchy; but for historical
385            * reasons, we only send "destroy yourself" messages to
386            * foreign windows in our heirarchy.
387            */
388           if (private->parent)
389             _gdk_windowing_window_destroy_foreign (window);
390
391           /* Also for historical reasons, we remove any filters
392            * on a foreign window when it or a parent is destroyed;
393            * this likely causes problems if two separate portions
394            * of code are maintaining filter lists on a foreign window.
395            */
396           window_remove_filters (window);
397         }
398       else
399         {
400           private->state |= GDK_WINDOW_STATE_WITHDRAWN;
401           
402           if (private->parent)
403             {
404               GdkWindowObject *parent_private = (GdkWindowObject *)private->parent;
405               if (parent_private->children)
406                 parent_private->children = g_list_remove (parent_private->children, window);
407             }
408
409           _gdk_window_clear_update_area (window);
410           gdk_window_free_paint_stack (window);
411           
412           if (private->bg_pixmap &&
413               private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
414               private->bg_pixmap != GDK_NO_BG)
415             {
416               g_object_unref (private->bg_pixmap);
417               private->bg_pixmap = NULL;
418             }
419           
420           if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_FOREIGN)
421             g_assert (private->children == NULL);
422           else
423             {
424               children = tmp = private->children;
425               private->children = NULL;
426               
427               while (tmp)
428                 {
429                   temp_window = tmp->data;
430                   tmp = tmp->next;
431                   
432                   temp_private = (GdkWindowObject*) temp_window;
433                   if (temp_private)
434                     _gdk_window_destroy_hierarchy (temp_window,
435                                                    TRUE, foreign_destroy);
436                 }
437               
438               g_list_free (children);
439             }
440           
441           _gdk_windowing_window_destroy (window, recursing, foreign_destroy);
442           private->parent = NULL;
443           private->destroyed = TRUE;
444
445           window_remove_filters (window);
446
447           gdk_drawable_set_colormap (GDK_DRAWABLE (window), NULL);
448         }
449       break;
450     }
451 }
452
453 /**
454  * _gdk_window_destroy:
455  * @window: a #GdkWindow
456  * @foreign_destroy: If TRUE, the window or a parent was destroyed by some
457  *            external agency. The window has already been destroyed and no
458  *            windowing system calls should be made. (This may never happen
459  *            for some windowing systems.)
460  *
461  * Internal function to destroy a window. Like gdk_window_destroy(),
462  * but does not drop the reference count created by gdk_window_new().
463  **/
464 void
465 _gdk_window_destroy (GdkWindow *window,
466                      gboolean   foreign_destroy)
467 {
468   _gdk_window_destroy_hierarchy (window, FALSE, foreign_destroy);
469 }
470
471 /**
472  * gdk_window_destroy:
473  * @window: a #GdkWindow
474  *
475  * Destroys the window system resources associated with @window and decrements @window's
476  * reference count. The window system resources for all children of @window are also
477  * destroyed, but the children's reference counts are not decremented.
478  *
479  * Note that a window will not be destroyed automatically when its reference count
480  * reaches zero. You must call this function yourself before that happens.
481  *
482  **/
483 void
484 gdk_window_destroy (GdkWindow *window)
485 {
486   _gdk_window_destroy_hierarchy (window, FALSE, FALSE);
487   g_object_unref (window);
488 }
489
490 /**
491  * gdk_window_set_user_data:
492  * @window: a #GdkWindow
493  * @user_data: user data
494  *
495  * For most purposes this function is deprecated in favor of
496  * g_object_set_data(). However, for historical reasons GTK+ stores
497  * the #GtkWidget that owns a #GdkWindow as user data on the
498  * #GdkWindow. So, custom widget implementations should use
499  * this function for that. If GTK+ receives an event for a #GdkWindow,
500  * and the user data for the window is non-%NULL, GTK+ will assume the
501  * user data is a #GtkWidget, and forward the event to that widget.
502  * 
503  **/
504 void
505 gdk_window_set_user_data (GdkWindow *window,
506                           gpointer   user_data)
507 {
508   g_return_if_fail (window != NULL);
509   
510   ((GdkWindowObject*)window)->user_data = user_data;
511 }
512
513 /**
514  * gdk_window_get_user_data:
515  * @window: a #GdkWindow
516  * @data: return location for user data
517  *
518  * Retrieves the user data for @window, which is normally the widget
519  * that @window belongs to. See gdk_window_set_user_data().
520  * 
521  **/
522 void
523 gdk_window_get_user_data (GdkWindow *window,
524                           gpointer  *data)
525 {
526   g_return_if_fail (window != NULL);
527   
528   *data = ((GdkWindowObject*)window)->user_data;
529 }
530
531 /**
532  * gdk_window_get_window_type:
533  * @window: a #GdkWindow
534  * 
535  * Gets the type of the window. See #GdkWindowType.
536  * 
537  * Return value: type of window
538  **/
539 GdkWindowType
540 gdk_window_get_window_type (GdkWindow *window)
541 {
542   g_return_val_if_fail (GDK_IS_WINDOW (window), (GdkWindowType) -1);
543   
544   return GDK_WINDOW_TYPE (window);
545 }
546
547 /**
548  * gdk_window_get_position:
549  * @window: a #GdkWindow
550  * @x: X coordinate of window
551  * @y: Y coordinate of window
552  *
553  * Obtains the position of the window as reported in the
554  * most-recently-processed #GdkEventConfigure. Contrast with
555  * gdk_window_get_geometry() which queries the X server for the
556  * current window position, regardless of which events have been
557  * received or processed.
558  *
559  * The position coordinates are relative to the window's parent window.
560  * 
561  **/
562 void
563 gdk_window_get_position (GdkWindow *window,
564                          gint      *x,
565                          gint      *y)
566 {
567   GdkWindowObject *obj;
568   
569   g_return_if_fail (GDK_IS_WINDOW (window));
570   
571   obj = (GdkWindowObject*) window;
572   
573   if (x)
574     *x = obj->x;
575   if (y)
576     *y = obj->y;
577 }
578
579 /**
580  * gdk_window_get_parent:
581  * @window: a #GdkWindow
582  * 
583  * Obtains the parent of @window, as known to GDK. Does not query the
584  * X server; thus this returns the parent as passed to gdk_window_new(),
585  * not the actual parent. This should never matter unless you're using
586  * Xlib calls mixed with GDK calls on the X11 platform. It may also
587  * matter for toplevel windows, because the window manager may choose
588  * to reparent them.
589  * 
590  * Return value: parent of @window
591  **/
592 GdkWindow*
593 gdk_window_get_parent (GdkWindow *window)
594 {
595   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
596   
597   return (GdkWindow*) ((GdkWindowObject*) window)->parent;
598 }
599
600 /**
601  * gdk_window_get_toplevel:
602  * @window: a #GdkWindow
603  * 
604  * Gets the toplevel window that's an ancestor of @window.
605  * 
606  * Return value: the toplevel window containing @window
607  **/
608 GdkWindow*
609 gdk_window_get_toplevel (GdkWindow *window)
610 {
611   GdkWindowObject *obj;
612   
613   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
614
615   obj = (GdkWindowObject *)window;
616   while (GDK_WINDOW_TYPE (obj) == GDK_WINDOW_CHILD)
617     obj = (GdkWindowObject *)obj->parent;
618   
619   return GDK_WINDOW (obj);
620 }
621
622 /**
623  * gdk_window_get_children:
624  * @window: a #GdkWindow
625  * 
626  * Gets the list of children of @window known to GDK.
627  * This function only returns children created via GDK,
628  * so for example it's useless when used with the root window;
629  * it only returns windows an application created itself.
630  *
631  * The returned list must be freed, but the elements in the
632  * list need not be.
633  * 
634  * Return value: list of child windows inside @window
635  **/
636 GList*
637 gdk_window_get_children (GdkWindow *window)
638 {
639   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
640
641   if (GDK_WINDOW_DESTROYED (window))
642     return NULL;
643
644   return g_list_copy (GDK_WINDOW_OBJECT (window)->children);
645 }
646
647 /**
648  * gdk_window_peek_children:
649  * @window: a #GdkWindow
650  * 
651  * Like gdk_window_get_children(), but does not copy the list of
652  * children, so the list does not need to be freed.
653  * 
654  * Return value: a reference to the list of child windows in @window
655  **/
656 GList *
657 gdk_window_peek_children (GdkWindow *window)
658 {
659   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
660
661   if (GDK_WINDOW_DESTROYED (window))
662     return NULL;
663
664   return GDK_WINDOW_OBJECT (window)->children;
665 }
666
667 /**
668  * gdk_window_add_filter:
669  * @window: a #GdkWindow
670  * @function: filter callback
671  * @data: data to pass to filter callback
672  *
673  * Adds an event filter to @window, allowing you to intercept events
674  * before they reach GDK. This is a low-level operation and makes it
675  * easy to break GDK and/or GTK+, so you have to know what you're
676  * doing. Pass %NULL for @window to get all events for all windows,
677  * instead of events for a specific window.
678  * 
679  **/
680 void          
681 gdk_window_add_filter (GdkWindow     *window,
682                        GdkFilterFunc  function,
683                        gpointer       data)
684 {
685   GdkWindowObject *private;
686   GList *tmp_list;
687   GdkEventFilter *filter;
688   
689   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
690
691   private = (GdkWindowObject*) window;
692   if (private && GDK_WINDOW_DESTROYED (window))
693     return;
694   
695   if (private)
696     tmp_list = private->filters;
697   else
698     tmp_list = _gdk_default_filters;
699   
700   while (tmp_list)
701     {
702       filter = (GdkEventFilter *)tmp_list->data;
703       if ((filter->function == function) && (filter->data == data))
704         return;
705       tmp_list = tmp_list->next;
706     }
707   
708   filter = g_new (GdkEventFilter, 1);
709   filter->function = function;
710   filter->data = data;
711   
712   if (private)
713     private->filters = g_list_append (private->filters, filter);
714   else
715     _gdk_default_filters = g_list_append (_gdk_default_filters, filter);
716 }
717
718 /**
719  * gdk_window_remove_filter:
720  * @window: a #GdkWindow
721  * @function: previously-added filter function
722  * @data: user data for previously-added filter function
723  *
724  * Remove a filter previously added with gdk_window_add_filter().
725  * 
726  **/
727 void
728 gdk_window_remove_filter (GdkWindow     *window,
729                           GdkFilterFunc  function,
730                           gpointer       data)
731 {
732   GdkWindowObject *private;
733   GList *tmp_list, *node;
734   GdkEventFilter *filter;
735   
736   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
737
738   private = (GdkWindowObject*) window;
739   
740   if (private)
741     tmp_list = private->filters;
742   else
743     tmp_list = _gdk_default_filters;
744   
745   while (tmp_list)
746     {
747       filter = (GdkEventFilter *)tmp_list->data;
748       node = tmp_list;
749       tmp_list = tmp_list->next;
750       
751       if ((filter->function == function) && (filter->data == data))
752         {
753           if (private)
754             private->filters = g_list_remove_link (private->filters, node);
755           else
756             _gdk_default_filters = g_list_remove_link (_gdk_default_filters, node);
757           g_list_free_1 (node);
758           g_free (filter);
759           
760           return;
761         }
762     }
763 }
764
765 /**
766  * gdk_screen_get_toplevel_windows:
767  * @screen: The #GdkScreen where the toplevels are located.
768  * 
769  * Obtains a list of all toplevel windows known to GDK on the screen @screen.
770  * A toplevel window is a child of the root window (see
771  * gdk_get_default_root_window()).
772  *
773  * The returned list should be freed with g_list_free(), but
774  * its elements need not be freed.
775  * 
776  * Return value: list of toplevel windows, free with g_list_free()
777  *
778  * Since: 2.2
779  **/
780 GList *
781 gdk_screen_get_toplevel_windows (GdkScreen *screen)
782 {
783   GdkWindow * root_window;
784   GList *new_list = NULL;
785   GList *tmp_list;
786   
787   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
788   
789   root_window = gdk_screen_get_root_window (screen);
790
791   tmp_list = ((GdkWindowObject *)root_window)->children;
792   while (tmp_list)
793     {
794       if (GDK_WINDOW_TYPE (tmp_list->data) != GDK_WINDOW_FOREIGN)
795         new_list = g_list_prepend (new_list, tmp_list->data);
796       tmp_list = tmp_list->next;
797     }
798   
799   return new_list;
800 }
801
802 /**
803  * gdk_window_get_toplevels:
804  * 
805  * Obtains a list of all toplevel windows known to GDK on the default
806  * screen (see gdk_screen_get_toplevel_windows()).
807  * A toplevel window is a child of the root window (see
808  * gdk_get_default_root_window()).
809  *
810  * The returned list should be freed with g_list_free(), but
811  * its elements need not be freed.
812  * 
813  * Return value: list of toplevel windows, free with g_list_free()
814  **/
815 GList *
816 gdk_window_get_toplevels (void)
817 {
818   return gdk_screen_get_toplevel_windows (gdk_screen_get_default ());
819 }
820
821 /**
822  * gdk_window_is_visible:
823  * @window: a #GdkWindow
824  * 
825  * Checks whether the window has been mapped (with gdk_window_show() or
826  * gdk_window_show_unraised()).
827  * 
828  * Return value: %TRUE if the window is mapped
829  **/
830 gboolean 
831 gdk_window_is_visible (GdkWindow *window)
832 {
833   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
834   
835   return GDK_WINDOW_IS_MAPPED (window);
836 }
837
838 /**
839  * gdk_window_is_viewable:
840  * @window: a #GdkWindow
841  * 
842  * Check if the window and all ancestors of the window are
843  * mapped. (This is not necessarily "viewable" in the X sense, since
844  * we only check as far as we have GDK window parents, not to the root
845  * window.)
846  *
847  * Return value: %TRUE if the window is viewable
848  **/
849 gboolean 
850 gdk_window_is_viewable (GdkWindow *window)
851 {
852   GdkWindowObject *private = (GdkWindowObject *)window;
853   GdkScreen *screen;
854   GdkWindow *root_window;
855   
856   g_return_val_if_fail (window != NULL, FALSE);
857   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
858
859   screen = gdk_drawable_get_screen (window);
860   root_window = gdk_screen_get_root_window (screen);
861   
862   while (private && 
863          (private != (GdkWindowObject *)root_window) &&
864          (GDK_WINDOW_TYPE (private) != GDK_WINDOW_FOREIGN))
865     {
866       if (GDK_WINDOW_DESTROYED (private) || !GDK_WINDOW_IS_MAPPED (private))
867         return FALSE;
868       
869       private = (GdkWindowObject *)private->parent;
870     }
871   
872   return TRUE;
873 }
874
875 /**
876  * gdk_window_get_state:
877  * @window: a #GdkWindow
878  * 
879  * Gets the bitwise OR of the currently active window state flags,
880  * from the #GdkWindowState enumeration.
881  * 
882  * Return value: window state bitfield
883  **/
884 GdkWindowState
885 gdk_window_get_state (GdkWindow *window)
886 {
887   GdkWindowObject *private = (GdkWindowObject *)window;
888   
889   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
890   
891   return private->state;
892 }
893
894 /**
895  * gdk_window_begin_paint_rect:
896  * @window: a #GdkWindow
897  * @rectangle: rectangle you intend to draw to
898  *
899  * A convenience wrapper around gdk_window_begin_paint_region() which
900  * creates a rectangular region for you. See
901  * gdk_window_begin_paint_region() for details.
902  * 
903  **/
904 void
905 gdk_window_begin_paint_rect (GdkWindow    *window,
906                              GdkRectangle *rectangle)
907 {
908   GdkRegion *region;
909
910   g_return_if_fail (window != NULL);
911   g_return_if_fail (GDK_IS_WINDOW (window));
912
913   region = gdk_region_rectangle (rectangle);
914   gdk_window_begin_paint_region (window, region);
915   gdk_region_destroy (region);
916 }
917
918 #ifdef GDK_WINDOWING_X11
919 #include "x11/gdkx.h"
920 #endif
921
922 /**
923  * gdk_window_begin_paint_region:
924  * @window: a #GdkWindow
925  * @region: region you intend to draw to
926  *
927  * Indicates that you are beginning the process of redrawing @region.
928  * A backing store (offscreen buffer) large enough to contain @region
929  * will be created. The backing store will be initialized with the
930  * background color or background pixmap for @window. Then, all
931  * drawing operations performed on @window will be diverted to the
932  * backing store.  When you call gdk_window_end_paint(), the backing
933  * store will be copied to @window, making it visible onscreen. Only
934  * the part of @window contained in @region will be modified; that is,
935  * drawing operations are clipped to @region.
936  *
937  * The net result of all this is to remove flicker, because the user
938  * sees the finished product appear all at once when you call
939  * gdk_window_end_paint(). If you draw to @window directly without
940  * calling gdk_window_begin_paint_region(), the user may see flicker
941  * as individual drawing operations are performed in sequence.  The
942  * clipping and background-initializing features of
943  * gdk_window_begin_paint_region() are conveniences for the
944  * programmer, so you can avoid doing that work yourself.
945  *
946  * When using GTK+, the widget system automatically places calls to
947  * gdk_window_begin_paint_region() and gdk_window_end_paint() around
948  * emissions of the expose_event signal. That is, if you're writing an
949  * expose event handler, you can assume that the exposed area in
950  * #GdkEventExpose has already been cleared to the window background,
951  * is already set as the clip region, and already has a backing store.
952  * Therefore in most cases, application code need not call
953  * gdk_window_begin_paint_region(). (You can disable the automatic
954  * calls around expose events on a widget-by-widget basis by calling
955  * gtk_widget_set_double_buffered().)
956  *
957  * If you call this function multiple times before calling the
958  * matching gdk_window_end_paint(), the backing stores are pushed onto
959  * a stack. gdk_window_end_paint() copies the topmost backing store
960  * onscreen, subtracts the topmost region from all other regions in
961  * the stack, and pops the stack. All drawing operations affect only
962  * the topmost backing store in the stack. One matching call to
963  * gdk_window_end_paint() is required for each call to
964  * gdk_window_begin_paint_region().
965  * 
966  **/
967 void          
968 gdk_window_begin_paint_region (GdkWindow *window,
969                                GdkRegion *region)
970 {
971 #ifdef USE_BACKING_STORE
972   GdkWindowObject *private = (GdkWindowObject *)window;
973   GdkRectangle clip_box;
974   GdkWindowPaint *paint;
975   GSList *list;
976   
977   g_return_if_fail (window != NULL);
978   g_return_if_fail (GDK_IS_WINDOW (window));
979
980   if (GDK_WINDOW_DESTROYED (window))
981     return;
982
983   if (GDK_IS_PAINTABLE (private->impl) &&
984       GDK_PAINTABLE_GET_IFACE (private->impl)->begin_paint_region) 
985     {
986       GDK_PAINTABLE_GET_IFACE (private->impl)->begin_paint_region (GDK_PAINTABLE (private->impl), region);
987       return;
988     }
989
990   gdk_region_get_clipbox (region, &clip_box);
991
992   paint = g_new (GdkWindowPaint, 1);
993   paint->region = gdk_region_copy (region);
994   paint->x_offset = clip_box.x;
995   paint->y_offset = clip_box.y;
996   paint->pixmap =
997     gdk_pixmap_new (window,
998                     MAX (clip_box.width, 1), MAX (clip_box.height, 1), -1);
999
1000   paint->surface = _gdk_drawable_ref_cairo_surface (paint->pixmap);
1001   cairo_surface_set_device_offset (paint->surface,
1002                                    - paint->x_offset, - paint->y_offset);
1003   
1004   for (list = private->paint_stack; list != NULL; list = list->next)
1005     {
1006       GdkWindowPaint *tmp_paint = list->data;
1007
1008       gdk_region_subtract (tmp_paint->region, paint->region);
1009     }
1010   
1011   private->paint_stack = g_slist_prepend (private->paint_stack, paint);
1012
1013   if (!gdk_region_empty (region))
1014     {
1015       gdk_window_clear_backing_rect (window,
1016                                      clip_box.x, clip_box.y,
1017                                      clip_box.width, clip_box.height);
1018     }
1019 #endif /* USE_BACKING_STORE */
1020 }
1021
1022 /**
1023  * gdk_window_end_paint:
1024  * @window: a #GdkWindow
1025  *
1026  * Indicates that the backing store created by the most recent call to
1027  * gdk_window_begin_paint_region() should be copied onscreen and
1028  * deleted, leaving the next-most-recent backing store or no backing
1029  * store at all as the active paint region. See
1030  * gdk_window_begin_paint_region() for full details. It is an error to
1031  * call this function without a matching
1032  * gdk_window_begin_paint_region() first.
1033  * 
1034  **/
1035 void
1036 gdk_window_end_paint (GdkWindow *window)
1037 {
1038 #ifdef USE_BACKING_STORE
1039   GdkWindowObject *private = (GdkWindowObject *)window;
1040   GdkWindowPaint *paint;
1041   GdkGC *tmp_gc;
1042   GdkRectangle clip_box;
1043   gint x_offset, y_offset;
1044
1045   g_return_if_fail (window != NULL);
1046   g_return_if_fail (GDK_IS_WINDOW (window));
1047
1048   if (GDK_WINDOW_DESTROYED (window))
1049     return;
1050
1051   if (GDK_IS_PAINTABLE (private->impl) &&
1052       GDK_PAINTABLE_GET_IFACE (private->impl)->end_paint) 
1053     {
1054       GDK_PAINTABLE_GET_IFACE (private->impl)->end_paint (GDK_PAINTABLE (private->impl));
1055       return;
1056     }
1057
1058   if (private->paint_stack == NULL)
1059     {
1060       g_warning (G_STRLOC": no preceding call to gdk_window_begin_paint_region(), see documentation");
1061       return;
1062     }
1063
1064   paint = private->paint_stack->data;
1065   private->paint_stack = g_slist_delete_link (private->paint_stack, 
1066                                               private->paint_stack);
1067
1068   gdk_region_get_clipbox (paint->region, &clip_box);
1069
1070   tmp_gc = _gdk_drawable_get_scratch_gc (window, FALSE);
1071
1072   _gdk_windowing_window_get_offsets (window, &x_offset, &y_offset);
1073
1074   gdk_gc_set_clip_region (tmp_gc, paint->region);
1075   gdk_gc_set_clip_origin (tmp_gc, - x_offset, - y_offset);
1076
1077   gdk_draw_drawable (private->impl, tmp_gc, paint->pixmap,
1078                      clip_box.x - paint->x_offset,
1079                      clip_box.y - paint->y_offset,
1080                      clip_box.x - x_offset, clip_box.y - y_offset,
1081                      clip_box.width, clip_box.height);
1082
1083   /* Reset clip region of the cached GdkGC */
1084   gdk_gc_set_clip_region (tmp_gc, NULL);
1085
1086   cairo_surface_destroy (paint->surface);
1087   g_object_unref (paint->pixmap);
1088   gdk_region_destroy (paint->region);
1089   g_free (paint);
1090 #endif /* USE_BACKING_STORE */
1091 }
1092
1093 static void
1094 gdk_window_free_paint_stack (GdkWindow *window)
1095 {
1096   GdkWindowObject *private = (GdkWindowObject *)window;
1097   
1098   if (private->paint_stack)
1099     {
1100       GSList *tmp_list = private->paint_stack;
1101
1102       while (tmp_list)
1103         {
1104           GdkWindowPaint *paint = tmp_list->data;
1105
1106           if (tmp_list == private->paint_stack)
1107             g_object_unref (paint->pixmap);
1108                   
1109           gdk_region_destroy (paint->region);
1110           g_free (paint);
1111
1112           tmp_list = tmp_list->next;
1113         }
1114
1115       g_slist_free (private->paint_stack);
1116       private->paint_stack = NULL;
1117     }
1118 }
1119
1120 static void
1121 gdk_window_get_offsets (GdkWindow *window,
1122                         gint      *x_offset,
1123                         gint      *y_offset)
1124 {
1125   GdkWindowObject *private = (GdkWindowObject *)window;
1126   
1127   if (private->paint_stack)
1128     {
1129       GdkWindowPaint *paint = private->paint_stack->data;
1130       *x_offset = paint->x_offset;
1131       *y_offset = paint->y_offset;
1132     }
1133   else
1134     _gdk_windowing_window_get_offsets (window, x_offset, y_offset);
1135 }
1136
1137 /**
1138  * gdk_window_get_internal_paint_info:
1139  * @window: a #GdkWindow
1140  * @real_drawable: location to store the drawable to which drawing should be 
1141  *            done.
1142  * @x_offset: location to store the X offset between coordinates in @window,
1143  *            and the underlying window system primitive coordinates for 
1144  *            *@real_drawable.
1145  * @y_offset: location to store the Y offset between coordinates in @window,
1146  *            and the underlying window system primitive coordinates for
1147  *            *@real_drawable.
1148  * 
1149  * If you bypass the GDK layer and use windowing system primitives to
1150  * draw directly onto a #GdkWindow, then you need to deal with two
1151  * details: there may be an offset between GDK coordinates and windowing
1152  * system coordinates, and GDK may have redirected drawing to a offscreen
1153  * pixmap as the result of a gdk_window_begin_paint_region() calls.
1154  * This function allows retrieving the information you need to compensate
1155  * for these effects.
1156  *
1157  * This function exposes details of the GDK implementation, and is thus
1158  * likely to change in future releases of GDK.
1159  **/
1160 void
1161 gdk_window_get_internal_paint_info (GdkWindow    *window,
1162                                     GdkDrawable **real_drawable,
1163                                     gint         *x_offset,
1164                                     gint         *y_offset)
1165 {
1166   gint x_off, y_off;
1167   
1168   GdkWindowObject *private;
1169
1170   g_return_if_fail (GDK_IS_WINDOW (window));
1171
1172   private = (GdkWindowObject *)window;
1173
1174   if (real_drawable)
1175     {
1176       if (private->paint_stack)
1177         {
1178           GdkWindowPaint *paint = private->paint_stack->data;
1179           *real_drawable = paint->pixmap;
1180         }
1181       else
1182         *real_drawable = window;
1183     }
1184
1185   gdk_window_get_offsets (window, &x_off, &y_off);
1186
1187   if (x_offset)
1188     *x_offset = x_off;
1189   if (y_offset)
1190     *y_offset = y_off;
1191 }
1192
1193 #define OFFSET_GC(gc)                                         \
1194     gint x_offset, y_offset;                                  \
1195     gint old_clip_x = gc->clip_x_origin;    \
1196     gint old_clip_y = gc->clip_y_origin;    \
1197     gint old_ts_x = gc->ts_x_origin;        \
1198     gint old_ts_y = gc->ts_y_origin;        \
1199     gdk_window_get_offsets (drawable, &x_offset, &y_offset);  \
1200     if (x_offset != 0 || y_offset != 0)                       \
1201       {                                                       \
1202         gdk_gc_set_clip_origin (gc, old_clip_x - x_offset,    \
1203                                 old_clip_y - y_offset);       \
1204         gdk_gc_set_ts_origin (gc, old_ts_x - x_offset,        \
1205                               old_ts_y - y_offset);           \
1206       }
1207
1208 #define RESTORE_GC(gc)                                      \
1209     if (x_offset != 0 || y_offset != 0)                     \
1210      {                                                      \
1211        gdk_gc_set_clip_origin (gc, old_clip_x, old_clip_y); \
1212        gdk_gc_set_ts_origin (gc, old_ts_x, old_ts_y);       \
1213      }
1214
1215 static GdkGC *
1216 gdk_window_create_gc (GdkDrawable     *drawable,
1217                       GdkGCValues     *values,
1218                       GdkGCValuesMask  mask)
1219 {
1220   g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);
1221   
1222   if (GDK_WINDOW_DESTROYED (drawable))
1223     return NULL;
1224
1225   return gdk_gc_new_with_values (((GdkWindowObject *) drawable)->impl,
1226                                  values, mask);
1227 }
1228
1229 static void
1230 gdk_window_draw_rectangle (GdkDrawable *drawable,
1231                            GdkGC       *gc,
1232                            gboolean     filled,
1233                            gint         x,
1234                            gint         y,
1235                            gint         width,
1236                            gint         height)
1237 {
1238   GdkWindowObject *private = (GdkWindowObject *)drawable;
1239   OFFSET_GC (gc);
1240
1241   if (GDK_WINDOW_DESTROYED (drawable))
1242     return;
1243   
1244   if (private->paint_stack)
1245     {
1246       GdkWindowPaint *paint = private->paint_stack->data;
1247       gdk_draw_rectangle (paint->pixmap, gc, filled,
1248                           x - x_offset, y - y_offset, width, height);
1249     }
1250   else
1251     gdk_draw_rectangle (private->impl, gc, filled,
1252                         x - x_offset, y - y_offset, width, height);
1253
1254   RESTORE_GC (gc);
1255 }
1256
1257 static void
1258 gdk_window_draw_arc (GdkDrawable *drawable,
1259                      GdkGC       *gc,
1260                      gboolean     filled,
1261                      gint         x,
1262                      gint         y,
1263                      gint         width,
1264                      gint         height,
1265                      gint         angle1,
1266                      gint         angle2)
1267 {
1268   GdkWindowObject *private = (GdkWindowObject *)drawable;
1269   OFFSET_GC (gc);
1270
1271   if (GDK_WINDOW_DESTROYED (drawable))
1272     return;
1273   
1274   if (private->paint_stack)
1275     {
1276       GdkWindowPaint *paint = private->paint_stack->data;
1277       gdk_draw_arc (paint->pixmap, gc, filled,
1278                     x - x_offset, y - y_offset,
1279                     width, height, angle1, angle2);
1280     }
1281   else
1282     gdk_draw_arc (private->impl, gc, filled,
1283                   x - x_offset, y - y_offset,
1284                   width, height, angle1, angle2);
1285   RESTORE_GC (gc);
1286 }
1287
1288 static void
1289 gdk_window_draw_polygon (GdkDrawable *drawable,
1290                          GdkGC       *gc,
1291                          gboolean     filled,
1292                          GdkPoint    *points,
1293                          gint         npoints)
1294 {
1295   GdkWindowObject *private = (GdkWindowObject *)drawable;
1296   GdkPoint *new_points;
1297   
1298   OFFSET_GC (gc);
1299
1300   if (GDK_WINDOW_DESTROYED (drawable))
1301     return;
1302   
1303   if (x_offset != 0 || y_offset != 0)
1304     {
1305       int i;
1306       
1307       new_points = g_new (GdkPoint, npoints);
1308       for (i=0; i<npoints; i++)
1309         {
1310           new_points[i].x = points[i].x - x_offset;
1311           new_points[i].y = points[i].y - y_offset;
1312         }
1313     }
1314   else
1315     new_points = points;
1316
1317   if (private->paint_stack)
1318     {
1319       GdkWindowPaint *paint = private->paint_stack->data;
1320       gdk_draw_polygon (paint->pixmap, gc, filled, new_points, npoints);
1321
1322     }
1323   else
1324     gdk_draw_polygon (private->impl, gc, filled, new_points, npoints);
1325   
1326   if (new_points != points)
1327     g_free (new_points);
1328
1329   RESTORE_GC (gc);
1330 }
1331
1332 static void
1333 gdk_window_draw_text (GdkDrawable *drawable,
1334                       GdkFont     *font,
1335                       GdkGC       *gc,
1336                       gint         x,
1337                       gint         y,
1338                       const gchar *text,
1339                       gint         text_length)
1340 {
1341   GdkWindowObject *private = (GdkWindowObject *)drawable;
1342   OFFSET_GC (gc);
1343
1344   if (GDK_WINDOW_DESTROYED (drawable))
1345     return;
1346   
1347   if (private->paint_stack)
1348     {
1349       GdkWindowPaint *paint = private->paint_stack->data;
1350       gdk_draw_text (paint->pixmap, font, gc, 
1351                      x - x_offset, y - y_offset, text, text_length);
1352
1353     }
1354   else
1355     gdk_draw_text (private->impl, font, gc,
1356                    x - x_offset, y - y_offset, text, text_length);
1357
1358   RESTORE_GC (gc);
1359 }
1360
1361 static void
1362 gdk_window_draw_text_wc (GdkDrawable    *drawable,
1363                          GdkFont        *font,
1364                          GdkGC          *gc,
1365                          gint            x,
1366                          gint            y,
1367                          const GdkWChar *text,
1368                          gint            text_length)
1369 {
1370   GdkWindowObject *private = (GdkWindowObject *)drawable;
1371   OFFSET_GC (gc);
1372
1373   if (GDK_WINDOW_DESTROYED (drawable))
1374     return;
1375   
1376   if (private->paint_stack)
1377     {
1378       GdkWindowPaint *paint = private->paint_stack->data;
1379       gdk_draw_text_wc (paint->pixmap, font, gc, 
1380                         x - x_offset, y - y_offset, text, text_length);
1381     }
1382   else
1383     gdk_draw_text_wc (private->impl, font, gc,
1384                       x - x_offset, y - y_offset, text, text_length);
1385   
1386   RESTORE_GC (gc);
1387 }
1388
1389 static GdkDrawable*
1390 gdk_window_get_composite_drawable (GdkDrawable *drawable,
1391                                    gint         x,
1392                                    gint         y,
1393                                    gint         width,
1394                                    gint         height,
1395                                    gint        *composite_x_offset,
1396                                    gint        *composite_y_offset)
1397 {
1398   GdkWindowObject *private = (GdkWindowObject *)drawable;
1399   GSList *list;
1400   GdkPixmap *tmp_pixmap;
1401   GdkRectangle rect;
1402   GdkGC *tmp_gc;
1403   gboolean overlap_buffer;
1404
1405   _gdk_windowing_window_get_offsets (drawable,
1406                                      composite_x_offset,
1407                                      composite_y_offset);
1408   
1409   if ((GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
1410       || private->paint_stack == NULL)
1411     {
1412       /* No backing store */
1413       return g_object_ref (drawable);
1414     }
1415
1416   /* See if the buffered part is overlapping the part we want
1417    * to get
1418    */
1419   rect.x = x;
1420   rect.y = y;
1421   rect.width = width;
1422   rect.height = height;
1423
1424   overlap_buffer = FALSE;
1425   
1426   for (list = private->paint_stack; list != NULL; list = list->next)
1427     {
1428       GdkWindowPaint *paint = list->data;
1429       GdkOverlapType overlap;
1430
1431       overlap = gdk_region_rect_in (paint->region, &rect);
1432
1433       if (overlap == GDK_OVERLAP_RECTANGLE_IN)
1434         {
1435           *composite_x_offset = paint->x_offset;
1436           *composite_y_offset = paint->y_offset;
1437           
1438           return g_object_ref (paint->pixmap);
1439         }
1440       else if (overlap == GDK_OVERLAP_RECTANGLE_PART)
1441         {
1442           overlap_buffer = TRUE;
1443           break;
1444         }
1445     }
1446
1447   if (!overlap_buffer)
1448     return g_object_ref (drawable);
1449
1450   tmp_pixmap = gdk_pixmap_new (drawable, width, height, -1);
1451   tmp_gc = _gdk_drawable_get_scratch_gc (tmp_pixmap, FALSE);
1452
1453   /* Copy the current window contents */
1454   gdk_draw_drawable (tmp_pixmap,
1455                      tmp_gc,
1456                      private->impl,
1457                      x - *composite_x_offset,
1458                      y - *composite_y_offset,
1459                      0, 0,
1460                      width, height);
1461
1462   /* paint the backing stores */
1463   for (list = private->paint_stack; list != NULL; list = list->next)
1464     {
1465       GdkWindowPaint *paint = list->data;
1466
1467       gdk_gc_set_clip_region (tmp_gc, paint->region);
1468       gdk_gc_set_clip_origin (tmp_gc, -x, -y);
1469       
1470       gdk_draw_drawable (tmp_pixmap, tmp_gc, paint->pixmap,
1471                          x - paint->x_offset,
1472                          y - paint->y_offset,
1473                          0, 0, width, height);
1474     }
1475
1476   /* Reset clip region of the cached GdkGC */
1477   gdk_gc_set_clip_region (tmp_gc, NULL);
1478
1479   /* Set these to location of tmp_pixmap within the window */
1480   *composite_x_offset = x;
1481   *composite_y_offset = y;
1482
1483   return tmp_pixmap;
1484 }
1485
1486 static GdkRegion*
1487 gdk_window_get_clip_region (GdkDrawable *drawable)
1488 {
1489   GdkWindowObject *private = (GdkWindowObject *)drawable;
1490   GdkRegion *result;
1491
1492   result = gdk_drawable_get_clip_region (private->impl);
1493
1494   if (private->paint_stack)
1495     {
1496       GdkRegion *paint_region = gdk_region_new ();
1497       GSList *tmp_list = private->paint_stack;
1498
1499       while (tmp_list)
1500         {
1501           GdkWindowPaint *paint = tmp_list->data;
1502           
1503           gdk_region_union (paint_region, paint->region);
1504
1505           tmp_list = tmp_list->next;
1506         }
1507
1508       gdk_region_intersect (result, paint_region);
1509       gdk_region_destroy (paint_region);
1510     }
1511
1512   return result;
1513 }
1514
1515 static GdkRegion*
1516 gdk_window_get_visible_region (GdkDrawable *drawable)
1517 {
1518   GdkWindowObject *private = (GdkWindowObject*) drawable;
1519   
1520   return gdk_drawable_get_visible_region (private->impl);
1521 }
1522
1523 static void
1524 gdk_window_draw_drawable (GdkDrawable *drawable,
1525                           GdkGC       *gc,
1526                           GdkPixmap   *src,
1527                           gint         xsrc,
1528                           gint         ysrc,
1529                           gint         xdest,
1530                           gint         ydest,
1531                           gint         width,
1532                           gint         height)
1533 {
1534   GdkWindowObject *private = (GdkWindowObject *)drawable;
1535   OFFSET_GC (gc);
1536   
1537   if (GDK_WINDOW_DESTROYED (drawable))
1538     return;
1539
1540   /* If we have a backing pixmap draw to that */
1541   if (private->paint_stack)
1542     {
1543       GdkWindowPaint *paint = private->paint_stack->data;
1544       gdk_draw_drawable (paint->pixmap, gc,
1545                          src, xsrc, ysrc,
1546                          xdest - x_offset, ydest - y_offset, width, height);
1547
1548     }
1549   else
1550     gdk_draw_drawable (private->impl, gc,
1551                        src, xsrc, ysrc,
1552                        xdest - x_offset, ydest - y_offset,
1553                        width, height);
1554
1555   RESTORE_GC (gc);
1556 }
1557
1558 static void
1559 gdk_window_draw_points (GdkDrawable *drawable,
1560                         GdkGC       *gc,
1561                         GdkPoint    *points,
1562                         gint         npoints)
1563 {
1564   GdkWindowObject *private = (GdkWindowObject *)drawable;
1565   GdkPoint *new_points;
1566   
1567   OFFSET_GC (gc);
1568
1569   if (GDK_WINDOW_DESTROYED (drawable))
1570     return;
1571   
1572   if (x_offset != 0 || y_offset != 0)
1573     {
1574       gint i;
1575
1576       new_points = g_new (GdkPoint, npoints);
1577       for (i=0; i<npoints; i++)
1578         {
1579           new_points[i].x = points[i].x - x_offset;
1580           new_points[i].y = points[i].y - y_offset;
1581         }
1582     }
1583   else
1584     new_points = points;
1585
1586   if (private->paint_stack)
1587     {
1588       GdkWindowPaint *paint = private->paint_stack->data;
1589       gdk_draw_points (paint->pixmap, gc, new_points, npoints);
1590     }
1591   else
1592     gdk_draw_points (private->impl, gc, points, npoints);
1593
1594   if (new_points != points)
1595     g_free (new_points);
1596
1597   RESTORE_GC (gc);
1598 }
1599
1600 static void
1601 gdk_window_draw_segments (GdkDrawable *drawable,
1602                           GdkGC       *gc,
1603                           GdkSegment  *segs,
1604                           gint         nsegs)
1605 {
1606   GdkWindowObject *private = (GdkWindowObject *)drawable;
1607   GdkSegment *new_segs;
1608
1609   OFFSET_GC (gc);
1610
1611   if (GDK_WINDOW_DESTROYED (drawable))
1612     return;
1613   
1614   if (x_offset != 0 || y_offset != 0)
1615     {
1616       gint i;
1617
1618       new_segs = g_new (GdkSegment, nsegs);
1619       for (i=0; i<nsegs; i++)
1620         {
1621           new_segs[i].x1 = segs[i].x1 - x_offset;
1622           new_segs[i].y1 = segs[i].y1 - y_offset;
1623           new_segs[i].x2 = segs[i].x2 - x_offset;
1624           new_segs[i].y2 = segs[i].y2 - y_offset;
1625         }
1626     }
1627   else
1628     new_segs = segs;
1629
1630   if (private->paint_stack)
1631     {
1632       GdkWindowPaint *paint = private->paint_stack->data;
1633       gdk_draw_segments (paint->pixmap, gc, new_segs, nsegs);
1634     }
1635   else
1636     gdk_draw_segments (private->impl, gc, new_segs, nsegs);
1637   
1638   if (new_segs != segs)
1639     g_free (new_segs);
1640
1641   RESTORE_GC (gc);
1642 }
1643
1644 static void
1645 gdk_window_draw_lines (GdkDrawable *drawable,
1646                        GdkGC       *gc,
1647                        GdkPoint    *points,
1648                        gint         npoints)
1649 {
1650   GdkWindowObject *private = (GdkWindowObject *)drawable;
1651   GdkPoint *new_points;
1652
1653   OFFSET_GC (gc);
1654
1655   if (GDK_WINDOW_DESTROYED (drawable))
1656     return;
1657   
1658   if (x_offset != 0 || y_offset != 0)
1659     {
1660       gint i;
1661
1662       new_points = g_new (GdkPoint, npoints);
1663       for (i=0; i<npoints; i++)
1664         {
1665           new_points[i].x = points[i].x - x_offset;
1666           new_points[i].y = points[i].y - y_offset;
1667         }
1668     }
1669   else
1670     new_points = points;
1671
1672   if (private->paint_stack)
1673     {
1674       GdkWindowPaint *paint = private->paint_stack->data;
1675       gdk_draw_lines (paint->pixmap, gc, new_points, npoints);
1676     }
1677   else
1678     gdk_draw_lines (private->impl, gc, new_points, npoints);
1679
1680   if (new_points != points)
1681     g_free (new_points);
1682
1683   RESTORE_GC (gc);
1684 }
1685
1686 static void
1687 gdk_window_draw_glyphs (GdkDrawable      *drawable,
1688                         GdkGC            *gc,
1689                         PangoFont        *font,
1690                         gint              x,
1691                         gint              y,
1692                         PangoGlyphString *glyphs)
1693 {
1694   GdkWindowObject *private = (GdkWindowObject *)drawable;
1695
1696   OFFSET_GC (gc);
1697
1698   if (GDK_WINDOW_DESTROYED (drawable))
1699     return;
1700   
1701   if (private->paint_stack)
1702     {
1703       GdkWindowPaint *paint = private->paint_stack->data;
1704
1705       gdk_draw_glyphs (paint->pixmap, gc, font, x - x_offset, y - y_offset, glyphs);
1706     }
1707   else
1708     gdk_draw_glyphs (private->impl, gc, font,
1709                      x - x_offset, y - y_offset, glyphs);
1710
1711   RESTORE_GC (gc);
1712 }
1713
1714 static void
1715 gdk_window_draw_glyphs_transformed (GdkDrawable      *drawable,
1716                                     GdkGC            *gc,
1717                                     PangoMatrix      *matrix,
1718                                     PangoFont        *font,
1719                                     gint              x,
1720                                     gint              y,
1721                                     PangoGlyphString *glyphs)
1722 {
1723   GdkWindowObject *private = (GdkWindowObject *)drawable;
1724   PangoMatrix tmp_matrix;
1725
1726   OFFSET_GC (gc);
1727
1728   if (GDK_WINDOW_DESTROYED (drawable))
1729     return;
1730
1731   if (x_offset != 0 || y_offset != 0)
1732     {
1733       if (matrix)
1734         {
1735           tmp_matrix = *matrix;
1736           tmp_matrix.x0 -= x_offset;
1737           tmp_matrix.y0 -= y_offset;
1738           matrix = &tmp_matrix;
1739         }
1740       else
1741         {
1742           x -= x_offset * PANGO_SCALE;
1743           y -= y_offset * PANGO_SCALE;
1744         }
1745     }
1746   
1747   if (private->paint_stack)
1748     {
1749       GdkWindowPaint *paint = private->paint_stack->data;
1750
1751       gdk_draw_glyphs_transformed (paint->pixmap, gc, matrix, font, x, y, glyphs);
1752     }
1753   else
1754     gdk_draw_glyphs_transformed (private->impl, gc, matrix, font, x, y, glyphs);
1755
1756   RESTORE_GC (gc);
1757 }
1758
1759 typedef struct {
1760   cairo_t *cr; /* if non-null, it means use this cairo context */
1761   GdkGC *gc;   /* if non-null, it means use this GC instead */
1762 } BackingRectMethod;
1763
1764 static void
1765 setup_backing_rect_method (BackingRectMethod *method, GdkWindow *window, GdkWindowPaint *paint, int x_offset_cairo, int y_offset_cairo)
1766 {
1767   GdkWindowObject *private = (GdkWindowObject *)window;
1768
1769   if (private->bg_pixmap == GDK_PARENT_RELATIVE_BG && private->parent)
1770     {
1771       GdkWindowPaint tmp_paint;
1772
1773       tmp_paint = *paint;
1774       tmp_paint.x_offset += private->x;
1775       tmp_paint.y_offset += private->y;
1776
1777       x_offset_cairo += private->x;
1778       y_offset_cairo += private->y;
1779
1780       setup_backing_rect_method (method, GDK_WINDOW (private->parent), &tmp_paint, x_offset_cairo, y_offset_cairo);
1781     }
1782   else if (private->bg_pixmap &&
1783            private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
1784            private->bg_pixmap != GDK_NO_BG)
1785     {
1786 /* This is a workaround for https://bugs.freedesktop.org/show_bug.cgi?id=4320.
1787  * In it, using a pixmap as a repeating pattern in Cairo, and painting it to a
1788  * pixmap destination surface, can be very slow (on the order of seconds for a
1789  * whole-screen copy).  The workaround is to use pretty much the same code that
1790  * we used in GTK+ 2.6 (pre-Cairo), which clears the double-buffer pixmap with
1791  * a tiled GC XFillRectangle().
1792  */
1793
1794 /* Actually computing this flag is left as an exercise for the reader */
1795 #if defined (G_OS_UNIX)
1796 #  define GDK_CAIRO_REPEAT_IS_FAST 0
1797 #else
1798 #  define GDK_CAIRO_REPEAT_IS_FAST 1
1799 #endif
1800
1801 #if GDK_CAIRO_REPEAT_IS_FAST
1802       cairo_surface_t *surface = _gdk_drawable_ref_cairo_surface (private->bg_pixmap);
1803       cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);
1804       cairo_surface_destroy (surface);
1805
1806       if (x_offset_cairo != 0 || y_offset_cairo != 0)
1807         {
1808           cairo_matrix_t matrix;
1809           cairo_matrix_init_translate (&matrix, x_offset_cairo, y_offset_cairo);
1810           cairo_pattern_set_matrix (pattern, &matrix);
1811         }
1812
1813       cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1814
1815       method->cr = cairo_create (paint->surface);
1816       method->gc = NULL;
1817
1818       cairo_set_source (method->cr, pattern);
1819       cairo_pattern_destroy (pattern);
1820 #else
1821       guint gc_mask;
1822       GdkGCValues gc_values;
1823
1824       gc_values.fill = GDK_TILED;
1825       gc_values.tile = private->bg_pixmap;
1826       gc_values.ts_x_origin = -x_offset_cairo;
1827       gc_values.ts_y_origin = -y_offset_cairo;
1828
1829       gc_mask = GDK_GC_FILL | GDK_GC_TILE | GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN;
1830
1831       method->gc = gdk_gc_new_with_values (paint->pixmap, &gc_values, gc_mask);
1832 #endif
1833     }
1834   else
1835     {
1836       method->cr = cairo_create (paint->surface);
1837
1838       gdk_cairo_set_source_color (method->cr, &private->bg_color);
1839     }
1840 }
1841
1842 static void
1843 gdk_window_clear_backing_rect (GdkWindow *window,
1844                                gint       x,
1845                                gint       y,
1846                                gint       width,
1847                                gint       height)
1848 {
1849   GdkWindowObject *private = (GdkWindowObject *)window;
1850   GdkWindowPaint *paint = private->paint_stack->data;
1851   BackingRectMethod method;
1852 #if 0
1853   GTimer *timer;
1854   double elapsed;
1855 #endif
1856
1857   if (GDK_WINDOW_DESTROYED (window))
1858     return;
1859
1860 #if 0
1861   timer = g_timer_new ();
1862 #endif
1863
1864   method.cr = NULL;
1865   method.gc = NULL;
1866   setup_backing_rect_method (&method, window, paint, 0, 0);
1867
1868   if (method.cr)
1869     {
1870       g_assert (method.gc == NULL);
1871
1872       cairo_rectangle (method.cr, x, y, width, height);
1873       cairo_clip (method.cr);
1874
1875       gdk_cairo_region (method.cr, paint->region);
1876       cairo_fill (method.cr);
1877
1878       cairo_destroy (method.cr);
1879 #if 0
1880       elapsed = g_timer_elapsed (timer, NULL);
1881       g_print ("Draw the background with Cairo: %fs\n", elapsed);
1882 #endif
1883     }
1884   else
1885     {
1886       g_assert (method.gc != NULL);
1887
1888       gdk_gc_set_clip_region (method.gc, paint->region);
1889       gdk_draw_rectangle (window, method.gc, TRUE, x, y, width, height);
1890       g_object_unref (method.gc);
1891
1892 #if 0
1893       elapsed = g_timer_elapsed (timer, NULL);
1894       g_print ("Draw the background with GDK: %fs\n", elapsed);
1895 #endif
1896     }
1897
1898 #if 0
1899   g_timer_destroy (timer);
1900 #endif
1901 }
1902
1903 /**
1904  * gdk_window_clear:
1905  * @window: a #GdkWindow
1906  * 
1907  * Clears an entire @window to the background color or background pixmap.
1908  **/
1909 void
1910 gdk_window_clear (GdkWindow *window)
1911 {
1912   gint width, height;
1913   
1914   g_return_if_fail (window != NULL);
1915   g_return_if_fail (GDK_IS_WINDOW (window));
1916
1917   gdk_drawable_get_size (GDK_DRAWABLE (window), &width, &height);
1918   
1919   gdk_window_clear_area (window, 0, 0,
1920                          width, height);
1921 }
1922
1923 /**
1924  * gdk_window_clear_area:
1925  * @window: a #GdkWindow
1926  * @x: x coordinate of rectangle to clear
1927  * @y: y coordinate of rectangle to clear
1928  * @width: width of rectangle to clear
1929  * @height: height of rectangle to clear
1930  *
1931  * Clears an area of @window to the background color or background pixmap.
1932  * 
1933  **/
1934 void
1935 gdk_window_clear_area (GdkWindow *window,
1936                        gint       x,
1937                        gint       y,
1938                        gint       width,
1939                        gint       height)
1940 {
1941   GdkWindowObject *private = (GdkWindowObject *)window;
1942
1943   g_return_if_fail (window != NULL);
1944   g_return_if_fail (GDK_IS_WINDOW (window));
1945   
1946   if (private->paint_stack)
1947     gdk_window_clear_backing_rect (window, x, y, width, height);
1948   else
1949     _gdk_windowing_window_clear_area (window, x, y, width, height);
1950 }
1951
1952 /**
1953  * gdk_window_clear_area_e:
1954  * @window: a #GdkWindow
1955  * @x: x coordinate of rectangle to clear
1956  * @y: y coordinate of rectangle to clear
1957  * @width: width of rectangle to clear
1958  * @height: height of rectangle to clear
1959  *
1960  * Like gdk_window_clear_area(), but also generates an expose event for
1961  * the cleared area.
1962  *
1963  * This function has a stupid name because it dates back to the mists
1964  * time, pre-GDK-1.0.
1965  * 
1966  **/
1967 void
1968 gdk_window_clear_area_e (GdkWindow *window,
1969                          gint       x,
1970                          gint       y,
1971                          gint       width,
1972                          gint       height)
1973 {
1974   GdkWindowObject *private = (GdkWindowObject *)window;
1975
1976   g_return_if_fail (window != NULL);
1977   g_return_if_fail (GDK_IS_WINDOW (window));
1978   
1979   if (private->paint_stack)
1980     gdk_window_clear_backing_rect (window, x, y, width, height);
1981
1982   _gdk_windowing_window_clear_area_e (window, x, y, width, height);
1983 }
1984
1985 static void
1986 gdk_window_draw_image (GdkDrawable *drawable,
1987                        GdkGC       *gc,
1988                        GdkImage    *image,
1989                        gint         xsrc,
1990                        gint         ysrc,
1991                        gint         xdest,
1992                        gint         ydest,
1993                        gint         width,
1994                        gint         height)
1995 {
1996   GdkWindowObject *private = (GdkWindowObject *)drawable;
1997
1998   OFFSET_GC (gc);
1999
2000   if (GDK_WINDOW_DESTROYED (drawable))
2001     return;
2002   
2003   if (private->paint_stack)
2004     {
2005       GdkWindowPaint *paint = private->paint_stack->data;
2006       gdk_draw_image (paint->pixmap, gc, image, xsrc, ysrc,
2007                       xdest - x_offset, ydest - y_offset,
2008                       width, height);
2009
2010     }
2011   else
2012     gdk_draw_image (private->impl, gc, image, xsrc, ysrc,
2013                     xdest - x_offset, ydest - y_offset,
2014                     width, height);
2015
2016   RESTORE_GC (gc);
2017 }
2018
2019 static void
2020 gdk_window_draw_pixbuf (GdkDrawable     *drawable,
2021                         GdkGC           *gc,
2022                         GdkPixbuf       *pixbuf,
2023                         gint             src_x,
2024                         gint             src_y,
2025                         gint             dest_x,
2026                         gint             dest_y,
2027                         gint             width,
2028                         gint             height,
2029                         GdkRgbDither     dither,
2030                         gint             x_dither,
2031                         gint             y_dither)
2032 {
2033   GdkWindowObject *private = (GdkWindowObject *)drawable;
2034
2035   if (GDK_WINDOW_DESTROYED (drawable))
2036     return;
2037   
2038   if (gc)
2039     {
2040       OFFSET_GC (gc);
2041   
2042       if (private->paint_stack)
2043         {
2044           GdkWindowPaint *paint = private->paint_stack->data;
2045           gdk_draw_pixbuf (paint->pixmap, gc, pixbuf, src_x, src_y,
2046                            dest_x - x_offset, dest_y - y_offset,
2047                            width, height,
2048                            dither, x_dither - x_offset, y_dither - y_offset);
2049         }
2050       else
2051         gdk_draw_pixbuf (private->impl, gc, pixbuf, src_x, src_y,
2052                          dest_x - x_offset, dest_y - y_offset,
2053                          width, height,
2054                          dither, x_dither, y_dither);
2055       
2056       RESTORE_GC (gc);
2057     }
2058   else
2059     {
2060       gint x_offset, y_offset;
2061       gdk_window_get_offsets (drawable, &x_offset, &y_offset);
2062       
2063       if (private->paint_stack)
2064         {
2065           GdkWindowPaint *paint = private->paint_stack->data;
2066           gdk_draw_pixbuf (paint->pixmap, gc, pixbuf, src_x, src_y,
2067                            dest_x - x_offset, dest_y - y_offset,
2068                            width, height,
2069                             dither, x_dither - x_offset, y_dither - y_offset);
2070         }
2071       else
2072         gdk_draw_pixbuf (private->impl, gc, pixbuf, src_x, src_y,
2073                          dest_x - x_offset, dest_y - y_offset,
2074                          width, height,
2075                          dither, x_dither, y_dither);
2076     }
2077 }
2078
2079 static void
2080 gdk_window_draw_trapezoids (GdkDrawable   *drawable,
2081                             GdkGC         *gc,
2082                             GdkTrapezoid  *trapezoids,
2083                             gint           n_trapezoids)
2084 {
2085   GdkWindowObject *private = (GdkWindowObject *)drawable;
2086   GdkTrapezoid *new_trapezoids = NULL;
2087
2088   OFFSET_GC (gc);
2089
2090   if (GDK_WINDOW_DESTROYED (drawable))
2091     return;
2092   
2093   if (x_offset != 0 || y_offset != 0)
2094     {
2095       gint i;
2096
2097       new_trapezoids = g_new (GdkTrapezoid, n_trapezoids);
2098       for (i=0; i < n_trapezoids; i++)
2099         {
2100           new_trapezoids[i].y1 = trapezoids[i].y1 - y_offset;
2101           new_trapezoids[i].x11 = trapezoids[i].x11 - x_offset;
2102           new_trapezoids[i].x21 = trapezoids[i].x21 - x_offset;
2103           new_trapezoids[i].y2 = trapezoids[i].y2 - y_offset;
2104           new_trapezoids[i].x12 = trapezoids[i].x12 - x_offset;
2105           new_trapezoids[i].x22 = trapezoids[i].x22 - x_offset;
2106         }
2107
2108       trapezoids = new_trapezoids;
2109     }
2110
2111   if (private->paint_stack)
2112     {
2113       GdkWindowPaint *paint = private->paint_stack->data;
2114       gdk_draw_trapezoids (paint->pixmap, gc, trapezoids, n_trapezoids);
2115     }
2116   else
2117     gdk_draw_trapezoids (private->impl, gc, trapezoids, n_trapezoids);
2118   
2119   if (new_trapezoids)
2120     g_free (new_trapezoids);
2121
2122   RESTORE_GC (gc);
2123 }
2124
2125 static void
2126 gdk_window_real_get_size (GdkDrawable *drawable,
2127                           gint *width,
2128                           gint *height)
2129 {
2130   g_return_if_fail (GDK_IS_WINDOW (drawable));
2131
2132   gdk_drawable_get_size (GDK_WINDOW_OBJECT (drawable)->impl,
2133                          width, height);
2134 }
2135
2136 static GdkVisual*
2137 gdk_window_real_get_visual (GdkDrawable *drawable)
2138 {
2139   GdkColormap *colormap;
2140
2141   g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);
2142
2143   colormap = gdk_drawable_get_colormap (drawable);
2144   return colormap ? gdk_colormap_get_visual (colormap) : NULL;
2145 }
2146
2147 static gint
2148 gdk_window_real_get_depth (GdkDrawable *drawable)
2149 {
2150   g_return_val_if_fail (GDK_IS_WINDOW (drawable), 0);
2151
2152   return ((GdkWindowObject *)GDK_WINDOW (drawable))->depth;
2153 }
2154
2155 static GdkScreen*
2156 gdk_window_real_get_screen (GdkDrawable *drawable)
2157 {
2158   return gdk_drawable_get_screen (GDK_WINDOW_OBJECT (drawable)->impl);
2159 }
2160
2161 static void
2162 gdk_window_real_set_colormap (GdkDrawable *drawable,
2163                               GdkColormap *cmap)
2164 {
2165   g_return_if_fail (GDK_IS_WINDOW (drawable));  
2166
2167   if (GDK_WINDOW_DESTROYED (drawable))
2168     return;
2169   
2170   gdk_drawable_set_colormap (((GdkWindowObject*)drawable)->impl, cmap);
2171 }
2172
2173 static GdkColormap*
2174 gdk_window_real_get_colormap (GdkDrawable *drawable)
2175 {
2176   g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);
2177
2178   if (GDK_WINDOW_DESTROYED (drawable))
2179     return NULL;
2180   
2181   return gdk_drawable_get_colormap (((GdkWindowObject*)drawable)->impl);
2182 }
2183                       
2184 static GdkImage*
2185 gdk_window_copy_to_image (GdkDrawable     *drawable,
2186                           GdkImage        *image,
2187                           gint             src_x,
2188                           gint             src_y,
2189                           gint             dest_x,
2190                           gint             dest_y,
2191                           gint             width,
2192                           gint             height)
2193 {
2194   gint x_offset, y_offset;
2195   
2196   g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);
2197   
2198   if (GDK_WINDOW_DESTROYED (drawable))
2199     return NULL;
2200
2201   /* If we're here, a composite image was not necessary, so
2202    * we can ignore the paint stack.
2203    */
2204   
2205   _gdk_windowing_window_get_offsets (drawable, &x_offset, &y_offset);
2206   
2207   return gdk_drawable_copy_to_image (((GdkWindowObject*)drawable)->impl,
2208                                      image,
2209                                      src_x - x_offset,
2210                                      src_y - y_offset,
2211                                      dest_x, dest_y,
2212                                      width, height);
2213 }
2214
2215 static cairo_surface_t *
2216 gdk_window_ref_cairo_surface (GdkDrawable *drawable)
2217 {
2218   GdkWindowObject *private = (GdkWindowObject*) drawable;
2219   cairo_surface_t *surface;
2220
2221   if (private->paint_stack)
2222     {
2223       GdkWindowPaint *paint = private->paint_stack->data;
2224
2225       surface = paint->surface;
2226       cairo_surface_reference (surface);
2227     }
2228   else
2229     surface = _gdk_drawable_ref_cairo_surface (private->impl);
2230
2231   return surface;
2232 }
2233
2234 /* Code for dirty-region queueing
2235  */
2236 static GSList *update_windows = NULL;
2237 static guint update_idle = 0;
2238 static gboolean debug_updates = FALSE;
2239
2240 static gboolean
2241 gdk_window_update_idle (gpointer data)
2242 {
2243   GDK_THREADS_ENTER ();
2244   gdk_window_process_all_updates ();
2245   GDK_THREADS_LEAVE ();
2246   
2247   return FALSE;
2248 }
2249
2250 static void
2251 gdk_window_schedule_update (GdkWindow *window)
2252 {
2253   if (window && GDK_WINDOW_OBJECT (window)->update_freeze_count)
2254     return;
2255
2256   if (!update_idle)
2257     {
2258       update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW,
2259                                      gdk_window_update_idle, NULL, NULL);
2260     }
2261 }
2262
2263 static void
2264 gdk_window_process_updates_internal (GdkWindow *window)
2265 {
2266   GdkWindowObject *private = (GdkWindowObject *)window;
2267   gboolean save_region = FALSE;
2268
2269   /* If an update got queued during update processing, we can get a
2270    * window in the update queue that has an empty update_area.
2271    * just ignore it.
2272    */
2273   if (private->update_area)
2274     {
2275       GdkRegion *update_area = private->update_area;
2276       private->update_area = NULL;
2277       
2278       if (_gdk_event_func && gdk_window_is_viewable (window))
2279         {
2280           GdkRectangle window_rect;
2281           GdkRegion *expose_region;
2282           GdkRegion *window_region;
2283           gint width, height;
2284
2285           if (debug_updates)
2286             {
2287               /* Make sure we see the red invalid area before redrawing. */
2288               gdk_display_sync (gdk_drawable_get_display (window));
2289               g_usleep (70000);
2290             }
2291           
2292           save_region = _gdk_windowing_window_queue_antiexpose (window, update_area);
2293
2294           if (save_region)
2295             expose_region = gdk_region_copy (update_area);
2296           else
2297             expose_region = update_area;
2298           
2299           gdk_drawable_get_size (GDK_DRAWABLE (private), &width, &height);
2300
2301           window_rect.x = 0;
2302           window_rect.y = 0;
2303           window_rect.width = width;
2304           window_rect.height = height;
2305
2306           window_region = gdk_region_rectangle (&window_rect);
2307           gdk_region_intersect (expose_region,
2308                                 window_region);
2309           gdk_region_destroy (window_region);
2310           
2311           if (!gdk_region_empty (expose_region) &&
2312               (private->event_mask & GDK_EXPOSURE_MASK))
2313             {
2314               GdkEvent event;
2315               
2316               event.expose.type = GDK_EXPOSE;
2317               event.expose.window = g_object_ref (window);
2318               event.expose.send_event = FALSE;
2319               event.expose.count = 0;
2320               event.expose.region = expose_region;
2321               gdk_region_get_clipbox (expose_region, &event.expose.area);
2322               
2323               (*_gdk_event_func) (&event, _gdk_event_data);
2324               
2325               g_object_unref (window);
2326             }
2327
2328           if (expose_region != update_area)
2329             gdk_region_destroy (expose_region);
2330         }
2331       if (!save_region)
2332         gdk_region_destroy (update_area);
2333     }
2334 }
2335
2336 static void
2337 flush_all_displays (void)
2338 {
2339   GSList *displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2340   GSList *tmp_list;
2341
2342   for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
2343     gdk_display_flush (tmp_list->data);
2344
2345   g_slist_free (displays);
2346 }
2347
2348 /* Currently it is not possible to override
2349  * gdk_window_process_all_updates in the same manner as
2350  * gdk_window_process_updates and gdk_window_invalidate_maybe_recurse
2351  * by implementing the GdkPaintable interface.  If in the future a
2352  * backend would need this, the right solution would be to add a
2353  * method to GdkDisplay that can be optionally
2354  * NULL. gdk_window_process_all_updates can then walk the list of open
2355  * displays and call the mehod.
2356  */
2357
2358 /**
2359  * gdk_window_process_all_updates:
2360  *
2361  * Calls gdk_window_process_updates() for all windows (see #GdkWindow)
2362  * in the application.
2363  * 
2364  **/
2365 void
2366 gdk_window_process_all_updates (void)
2367 {
2368   GSList *old_update_windows = update_windows;
2369   GSList *tmp_list = update_windows;
2370
2371   if (update_idle)
2372     g_source_remove (update_idle);
2373   
2374   update_windows = NULL;
2375   update_idle = 0;
2376
2377   g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
2378   
2379   while (tmp_list)
2380     {
2381       GdkWindowObject *private = (GdkWindowObject *)tmp_list->data;
2382       
2383       if (private->update_freeze_count)
2384         update_windows = g_slist_prepend (update_windows, private);
2385       else
2386         gdk_window_process_updates_internal (tmp_list->data);
2387       
2388       g_object_unref (tmp_list->data);
2389       tmp_list = tmp_list->next;
2390     }
2391
2392   g_slist_free (old_update_windows);
2393
2394   flush_all_displays ();
2395 }
2396
2397 /**
2398  * gdk_window_process_updates:
2399  * @window: a #GdkWindow
2400  * @update_children: whether to also process updates for child windows
2401  *
2402  * Sends one or more expose events to @window. The areas in each 
2403  * expose event will cover the entire update area for the window (see
2404  * gdk_window_invalidate_region() for details). Normally GDK calls
2405  * gdk_window_process_all_updates() on your behalf, so there's no
2406  * need to call this function unless you want to force expose events
2407  * to be delivered immediately and synchronously (vs. the usual
2408  * case, where GDK delivers them in an idle handler). Occasionally
2409  * this is useful to produce nicer scrolling behavior, for example.
2410  * 
2411  **/
2412 void
2413 gdk_window_process_updates (GdkWindow *window,
2414                             gboolean   update_children)
2415 {
2416   GdkWindowObject *private = (GdkWindowObject *)window;
2417
2418   g_return_if_fail (window != NULL);
2419   g_return_if_fail (GDK_IS_WINDOW (window));
2420   
2421   if (GDK_IS_PAINTABLE (private->impl) &&
2422       GDK_PAINTABLE_GET_IFACE (private->impl)->process_updates)
2423     {
2424       GDK_PAINTABLE_GET_IFACE (private->impl)->process_updates (GDK_PAINTABLE (private->impl), update_children);
2425       return;
2426     }
2427   
2428   if (private->update_area && !private->update_freeze_count)
2429     {      
2430       gdk_window_process_updates_internal (window);
2431       update_windows = g_slist_remove (update_windows, window);
2432     }
2433
2434   if (update_children)
2435     {
2436       GList *tmp_list = private->children;
2437       while (tmp_list)
2438         {
2439           gdk_window_process_updates (tmp_list->data, TRUE);
2440           tmp_list = tmp_list->next;
2441         }
2442     }
2443 }
2444
2445 /**
2446  * gdk_window_invalidate_rect:
2447  * @window: a #GdkWindow
2448  * @rect: rectangle to invalidate
2449  * @invalidate_children: whether to also invalidate child windows
2450  *
2451  * A convenience wrapper around gdk_window_invalidate_region() which
2452  * invalidates a rectangular region. See
2453  * gdk_window_invalidate_region() for details.
2454  * 
2455  **/
2456 void
2457 gdk_window_invalidate_rect   (GdkWindow    *window,
2458                               GdkRectangle *rect,
2459                               gboolean      invalidate_children)
2460 {
2461   GdkRectangle window_rect;
2462   GdkRegion *region;
2463   GdkWindowObject *private = (GdkWindowObject *)window;
2464
2465   g_return_if_fail (window != NULL);
2466   g_return_if_fail (GDK_IS_WINDOW (window));
2467
2468   if (GDK_WINDOW_DESTROYED (window))
2469     return;
2470   
2471   if (private->input_only || !GDK_WINDOW_IS_MAPPED (window))
2472     return;
2473
2474   if (!rect)
2475     {
2476       window_rect.x = 0;
2477       window_rect.y = 0;
2478       gdk_drawable_get_size (GDK_DRAWABLE (window),
2479                              &window_rect.width,
2480                              &window_rect.height);
2481       rect = &window_rect;
2482     }
2483
2484   region = gdk_region_rectangle (rect);
2485   gdk_window_invalidate_region (window, region, invalidate_children);
2486   gdk_region_destroy (region);
2487 }
2488
2489 static void
2490 draw_ugly_color (GdkWindow *window,
2491                 GdkRegion *region)
2492 {
2493   /* Draw ugly color all over the newly-invalid region */
2494   GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2495   GdkGC *ugly_gc;
2496   GdkRectangle clipbox;
2497     
2498   ugly_gc = gdk_gc_new (window);
2499   gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2500   gdk_gc_set_clip_region (ugly_gc, region);
2501
2502   gdk_region_get_clipbox (region, &clipbox);
2503   
2504   gdk_draw_rectangle (window,
2505                       ugly_gc,
2506                       TRUE,
2507                       clipbox.x, clipbox.y,
2508                       clipbox.width, clipbox.height);
2509   
2510   g_object_unref (ugly_gc);
2511 }
2512
2513 /**
2514  * gdk_window_invalidate_maybe_recurse:
2515  * @window: a #GdkWindow
2516  * @region: a #GdkRegion
2517  * @child_func: function to use to decide if to recurse to a child,
2518  *              %NULL means never recurse.
2519  * @user_data: data passed to @child_func
2520  *
2521  * Adds @region to the update area for @window. The update area is the
2522  * region that needs to be redrawn, or "dirty region." The call
2523  * gdk_window_process_updates() sends one or more expose events to the
2524  * window, which together cover the entire update area. An
2525  * application would normally redraw the contents of @window in
2526  * response to those expose events.
2527  *
2528  * GDK will call gdk_window_process_all_updates() on your behalf
2529  * whenever your program returns to the main loop and becomes idle, so
2530  * normally there's no need to do that manually, you just need to
2531  * invalidate regions that you know should be redrawn.
2532  *
2533  * The @child_func parameter controls whether the region of
2534  * each child window that intersects @region will also be invalidated.
2535  * Only children for which @child_func returns TRUE will have the area
2536  * invalidated.
2537  **/
2538 void
2539 gdk_window_invalidate_maybe_recurse (GdkWindow *window,
2540                                      GdkRegion *region,
2541                                      gboolean (*child_func) (GdkWindow *, gpointer),
2542                                      gpointer   user_data)
2543 {
2544   GdkWindowObject *private = (GdkWindowObject *)window;
2545   GdkRegion *visible_region;
2546   GList *tmp_list;
2547
2548   g_return_if_fail (window != NULL);
2549   g_return_if_fail (GDK_IS_WINDOW (window));
2550
2551   if (GDK_WINDOW_DESTROYED (window))
2552     return;
2553   
2554   if (private->input_only || !GDK_WINDOW_IS_MAPPED (window))
2555     return;
2556
2557   if (GDK_IS_PAINTABLE (private->impl) &&
2558       GDK_PAINTABLE_GET_IFACE (private->impl)->invalidate_maybe_recurse)
2559     {
2560       GDK_PAINTABLE_GET_IFACE (private->impl)->invalidate_maybe_recurse (GDK_PAINTABLE (private->impl), region,
2561                                                                          child_func, user_data);
2562       return;
2563     }
2564
2565   visible_region = gdk_drawable_get_visible_region (window);
2566   gdk_region_intersect (visible_region, region);
2567
2568   tmp_list = private->children;
2569   while (tmp_list)
2570     {
2571       GdkWindowObject *child = tmp_list->data;
2572       
2573       if (!child->input_only)
2574         {
2575           GdkRegion *child_region;
2576           GdkRectangle child_rect;
2577           
2578           gdk_window_get_position ((GdkWindow *)child,
2579                                    &child_rect.x, &child_rect.y);
2580           gdk_drawable_get_size ((GdkDrawable *)child,
2581                                  &child_rect.width, &child_rect.height);
2582
2583           child_region = gdk_region_rectangle (&child_rect);
2584           
2585           /* remove child area from the invalid area of the parent */
2586           if (GDK_WINDOW_IS_MAPPED (child) && !child->shaped)
2587             gdk_region_subtract (visible_region, child_region);
2588           
2589           if (child_func && (*child_func) ((GdkWindow *)child, user_data))
2590             {
2591               gdk_region_offset (region, - child_rect.x, - child_rect.y);
2592               gdk_region_offset (child_region, - child_rect.x, - child_rect.y);
2593               gdk_region_intersect (child_region, region);
2594               
2595               gdk_window_invalidate_maybe_recurse ((GdkWindow *)child,
2596                                                    child_region, child_func, user_data);
2597               
2598               gdk_region_offset (region, child_rect.x, child_rect.y);
2599             }
2600
2601           gdk_region_destroy (child_region);
2602         }
2603
2604       tmp_list = tmp_list->next;
2605     }
2606   
2607   if (!gdk_region_empty (visible_region))
2608     {
2609       if (debug_updates)
2610         draw_ugly_color (window, region);
2611       
2612       if (private->update_area)
2613         {
2614           gdk_region_union (private->update_area, visible_region);
2615         }
2616       else
2617         {
2618           update_windows = g_slist_prepend (update_windows, window);
2619           private->update_area = gdk_region_copy (visible_region);
2620           
2621           gdk_window_schedule_update (window);
2622         }
2623     }
2624   
2625   gdk_region_destroy (visible_region);
2626 }
2627
2628 static gboolean
2629 true_predicate (GdkWindow *window,
2630                 gpointer   user_data)
2631 {
2632   return TRUE;
2633 }
2634
2635 /**
2636  * gdk_window_invalidate_region:
2637  * @window: a #GdkWindow
2638  * @region: a #GdkRegion
2639  * @invalidate_children: %TRUE to also invalidate child windows 
2640  *
2641  * Adds @region to the update area for @window. The update area is the
2642  * region that needs to be redrawn, or "dirty region." The call
2643  * gdk_window_process_updates() sends one or more expose events to the
2644  * window, which together cover the entire update area. An
2645  * application would normally redraw the contents of @window in
2646  * response to those expose events.
2647  *
2648  * GDK will call gdk_window_process_all_updates() on your behalf
2649  * whenever your program returns to the main loop and becomes idle, so
2650  * normally there's no need to do that manually, you just need to
2651  * invalidate regions that you know should be redrawn.
2652  *
2653  * The @invalidate_children parameter controls whether the region of
2654  * each child window that intersects @region will also be invalidated.
2655  * If %FALSE, then the update area for child windows will remain
2656  * unaffected. See gdk_window_invalidate_maybe_recurse if you need
2657  * fine grained control over which children are invalidated.
2658  **/
2659 void
2660 gdk_window_invalidate_region (GdkWindow *window,
2661                               GdkRegion *region,
2662                               gboolean   invalidate_children)
2663 {
2664   gdk_window_invalidate_maybe_recurse (window, region,
2665                                        invalidate_children ?
2666                                          true_predicate : (gboolean (*) (GdkWindow *, gpointer))NULL,
2667                                        NULL);
2668 }
2669
2670 /**
2671  * gdk_window_get_update_area:
2672  * @window: a #GdkWindow
2673  * 
2674  * Transfers ownership of the update area from @window to the caller
2675  * of the function. That is, after calling this function, @window will
2676  * no longer have an invalid/dirty region; the update area is removed
2677  * from @window and handed to you. If a window has no update area,
2678  * gdk_window_get_update_area() returns %NULL. You are responsible for
2679  * calling gdk_region_destroy() on the returned region if it's non-%NULL.
2680  * 
2681  * Return value: the update area for @window
2682  **/
2683 GdkRegion *
2684 gdk_window_get_update_area (GdkWindow *window)
2685 {
2686   GdkWindowObject *private = (GdkWindowObject *)window;
2687   GdkRegion *tmp_region;
2688
2689   g_return_val_if_fail (window != NULL, NULL);
2690   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2691
2692   if (private->update_area)
2693     {
2694       tmp_region = private->update_area;
2695       private->update_area = NULL;
2696
2697       update_windows = g_slist_remove (update_windows, window);
2698       
2699       return tmp_region;
2700     }
2701   else
2702     return NULL;
2703 }
2704
2705 /**
2706  * _gdk_window_clear_update_area:
2707  * @window: a #GdkWindow.
2708  * 
2709  * Internal function to clear the update area for a window. This
2710  * is called when the window is hidden or destroyed.
2711  **/
2712 void
2713 _gdk_window_clear_update_area (GdkWindow *window)
2714 {
2715   GdkWindowObject *private = (GdkWindowObject *)window;
2716
2717   g_return_if_fail (window != NULL);
2718   g_return_if_fail (GDK_IS_WINDOW (window));
2719
2720   if (private->update_area)
2721     {
2722       update_windows = g_slist_remove (update_windows, window);
2723       
2724       gdk_region_destroy (private->update_area);
2725       private->update_area = NULL;
2726     }
2727 }
2728
2729 /**
2730  * gdk_window_freeze_updates:
2731  * @window: a #GdkWindow
2732  * 
2733  * Temporarily freezes a window such that it won't receive expose
2734  * events.  The window will begin receiving expose events again when
2735  * gdk_window_thaw_updates() is called. If gdk_window_freeze_updates()
2736  * has been called more than once, gdk_window_thaw_updates() must be called
2737  * an equal number of times to begin processing exposes.
2738  **/
2739 void
2740 gdk_window_freeze_updates (GdkWindow *window)
2741 {
2742   GdkWindowObject *private = (GdkWindowObject *)window;
2743
2744   g_return_if_fail (window != NULL);
2745   g_return_if_fail (GDK_IS_WINDOW (window));
2746
2747   private->update_freeze_count++;
2748 }
2749
2750 /**
2751  * gdk_window_thaw_updates:
2752  * @window: a #GdkWindow
2753  * 
2754  * Thaws a window frozen with gdk_window_freeze_updates().
2755  **/
2756 void
2757 gdk_window_thaw_updates (GdkWindow *window)
2758 {
2759   GdkWindowObject *private = (GdkWindowObject *)window;
2760
2761   g_return_if_fail (window != NULL);
2762   g_return_if_fail (GDK_IS_WINDOW (window));
2763   g_return_if_fail (private->update_freeze_count > 0);
2764
2765   if (--private->update_freeze_count == 0)
2766     gdk_window_schedule_update (window);
2767 }
2768
2769 /**
2770  * gdk_window_set_debug_updates:
2771  * @setting: %TRUE to turn on update debugging
2772  *
2773  * With update debugging enabled, calls to
2774  * gdk_window_invalidate_region() clear the invalidated region of the
2775  * screen to a noticeable color, and GDK pauses for a short time
2776  * before sending exposes to windows during
2777  * gdk_window_process_updates().  The net effect is that you can see
2778  * the invalid region for each window and watch redraws as they
2779  * occur. This allows you to diagnose inefficiencies in your application.
2780  *
2781  * In essence, because the GDK rendering model prevents all flicker,
2782  * if you are redrawing the same region 400 times you may never
2783  * notice, aside from noticing a speed problem. Enabling update
2784  * debugging causes GTK to flicker slowly and noticeably, so you can
2785  * see exactly what's being redrawn when, in what order.
2786  *
2787  * The --gtk-debug=updates command line option passed to GTK+ programs
2788  * enables this debug option at application startup time. That's
2789  * usually more useful than calling gdk_window_set_debug_updates()
2790  * yourself, though you might want to use this function to enable
2791  * updates sometime after application startup time.
2792  * 
2793  **/
2794 void
2795 gdk_window_set_debug_updates (gboolean setting)
2796 {
2797   debug_updates = setting;
2798 }
2799
2800 /**
2801  * gdk_window_constrain_size:
2802  * @geometry: a #GdkGeometry structure
2803  * @flags: a mask indicating what portions of @geometry are set
2804  * @width: desired width of window
2805  * @height: desired height of the window
2806  * @new_width: location to store resulting width
2807  * @new_height: location to store resulting height
2808  * 
2809  * Constrains a desired width and height according to a 
2810  * set of geometry hints (such as minimum and maximum size).
2811  */
2812 void
2813 gdk_window_constrain_size (GdkGeometry *geometry,
2814                            guint        flags,
2815                            gint         width,
2816                            gint         height,
2817                            gint        *new_width,
2818                            gint        *new_height)
2819 {
2820   /* This routine is partially borrowed from fvwm.
2821    *
2822    * Copyright 1993, Robert Nation
2823    *     You may use this code for any purpose, as long as the original
2824    *     copyright remains in the source code and all documentation
2825    *
2826    * which in turn borrows parts of the algorithm from uwm
2827    */
2828   gint min_width = 0;
2829   gint min_height = 0;
2830   gint base_width = 0;
2831   gint base_height = 0;
2832   gint xinc = 1;
2833   gint yinc = 1;
2834   gint max_width = G_MAXINT;
2835   gint max_height = G_MAXINT;
2836   
2837 #define FLOOR(value, base)      ( ((gint) ((value) / (base))) * (base) )
2838
2839   if ((flags & GDK_HINT_BASE_SIZE) && (flags & GDK_HINT_MIN_SIZE))
2840     {
2841       base_width = geometry->base_width;
2842       base_height = geometry->base_height;
2843       min_width = geometry->min_width;
2844       min_height = geometry->min_height;
2845     }
2846   else if (flags & GDK_HINT_BASE_SIZE)
2847     {
2848       base_width = geometry->base_width;
2849       base_height = geometry->base_height;
2850       min_width = geometry->base_width;
2851       min_height = geometry->base_height;
2852     }
2853   else if (flags & GDK_HINT_MIN_SIZE)
2854     {
2855       base_width = geometry->min_width;
2856       base_height = geometry->min_height;
2857       min_width = geometry->min_width;
2858       min_height = geometry->min_height;
2859     }
2860
2861   if (flags & GDK_HINT_MAX_SIZE)
2862     {
2863       max_width = geometry->max_width ;
2864       max_height = geometry->max_height;
2865     }
2866
2867   if (flags & GDK_HINT_RESIZE_INC)
2868     {
2869       xinc = MAX (xinc, geometry->width_inc);
2870       yinc = MAX (yinc, geometry->height_inc);
2871     }
2872   
2873   /* clamp width and height to min and max values
2874    */
2875   width = CLAMP (width, min_width, max_width);
2876   height = CLAMP (height, min_height, max_height);
2877   
2878   /* shrink to base + N * inc
2879    */
2880   width = base_width + FLOOR (width - base_width, xinc);
2881   height = base_height + FLOOR (height - base_height, yinc);
2882
2883   /* constrain aspect ratio, according to:
2884    *
2885    *                width     
2886    * min_aspect <= -------- <= max_aspect
2887    *                height    
2888    */
2889   
2890   if (flags & GDK_HINT_ASPECT &&
2891       geometry->min_aspect > 0 &&
2892       geometry->max_aspect > 0)
2893     {
2894       gint delta;
2895
2896       if (geometry->min_aspect * height > width)
2897         {
2898           delta = FLOOR (height - width / geometry->min_aspect, yinc);
2899           if (height - delta >= min_height)
2900             height -= delta;
2901           else
2902             { 
2903               delta = FLOOR (height * geometry->min_aspect - width, xinc);
2904               if (width + delta <= max_width) 
2905                 width += delta;
2906             }
2907         }
2908       
2909       if (geometry->max_aspect * height < width)
2910         {
2911           delta = FLOOR (width - height * geometry->max_aspect, xinc);
2912           if (width - delta >= min_width) 
2913             width -= delta;
2914           else
2915             {
2916               delta = FLOOR (width / geometry->max_aspect - height, yinc);
2917               if (height + delta <= max_height)
2918                 height += delta;
2919             }
2920         }
2921     }
2922
2923 #undef FLOOR
2924   
2925   *new_width = width;
2926   *new_height = height;
2927 }
2928
2929 /**
2930  * gdk_window_get_pointer:
2931  * @window: a #GdkWindow
2932  * @x: return location for X coordinate of pointer
2933  * @y: return location for Y coordinate of pointer
2934  * @mask: return location for modifier mask
2935  *
2936  * Obtains the current pointer position and modifier state.
2937  * The position is given in coordinates relative to the upper left 
2938  * corner of @window.
2939  * 
2940  * Return value: the window containing the pointer (as with
2941  * gdk_window_at_pointer()), or %NULL if the window containing the
2942  * pointer isn't known to GDK
2943  **/
2944 GdkWindow*
2945 gdk_window_get_pointer (GdkWindow         *window,
2946                         gint              *x,
2947                         gint              *y,
2948                         GdkModifierType   *mask)
2949 {
2950   GdkDisplay *display;
2951   gint tmp_x, tmp_y;
2952   GdkModifierType tmp_mask;
2953   GdkWindow *child;
2954   
2955   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
2956
2957   if (window)
2958     {
2959       display = gdk_drawable_get_display (window);
2960     }
2961   else
2962     {
2963       GdkScreen *screen = gdk_screen_get_default ();
2964
2965       display = gdk_screen_get_display (screen);
2966       window = gdk_screen_get_root_window (screen);
2967       
2968       GDK_NOTE (MULTIHEAD,
2969                 g_message ("Passing NULL for window to gdk_window_get_pointer()\n"
2970                            "is not multihead safe"));
2971     }
2972
2973   child = display->pointer_hooks->window_get_pointer (display, window, &tmp_x, &tmp_y, &tmp_mask);
2974
2975   if (x)
2976     *x = tmp_x;
2977   if (y)
2978     *y = tmp_y;
2979   if (mask)
2980     *mask = tmp_mask;
2981
2982   return child;
2983 }
2984
2985 /**
2986  * gdk_window_at_pointer:
2987  * @win_x: return location for origin of the window under the pointer
2988  * @win_y: return location for origin of the window under the pointer
2989  * 
2990  * Obtains the window underneath the mouse pointer, returning the
2991  * location of that window in @win_x, @win_y. Returns %NULL if the
2992  * window under the mouse pointer is not known to GDK (if the window
2993  * belongs to another application and a #GdkWindow hasn't been created
2994  * for it with gdk_window_foreign_new())
2995  *
2996  * NOTE: For multihead-aware widgets or applications use
2997  * gdk_display_get_window_at_pointer() instead.
2998  * 
2999  * Return value: window under the mouse pointer
3000  **/
3001 GdkWindow*
3002 gdk_window_at_pointer (gint *win_x,
3003                        gint *win_y)
3004 {
3005   return gdk_display_get_window_at_pointer (gdk_display_get_default (), win_x, win_y);
3006 }
3007
3008 /**
3009  * gdk_get_default_root_window:
3010  * 
3011  * Obtains the root window (parent all other windows are inside)
3012  * for the default display and screen.
3013  * 
3014  * Return value: the default root window
3015  **/
3016 GdkWindow *
3017 gdk_get_default_root_window (void)
3018 {
3019   return gdk_screen_get_root_window (gdk_screen_get_default ());
3020 }
3021
3022 /**
3023  * gdk_window_foreign_new:
3024  * @anid: a native window handle.
3025  * 
3026  * Wraps a native window for the default display in a #GdkWindow.
3027  * This may fail if the window has been destroyed.
3028  *
3029  * For example in the X backend, a native window handle is an Xlib
3030  * <type>XID</type>.
3031  * 
3032  * Return value: the newly-created #GdkWindow wrapper for the 
3033  *    native window or %NULL if the window has been destroyed.
3034  **/
3035 GdkWindow *
3036 gdk_window_foreign_new (GdkNativeWindow anid)
3037 {
3038   return gdk_window_foreign_new_for_display (gdk_display_get_default (), anid);
3039 }
3040
3041 #define __GDK_WINDOW_C__
3042 #include "gdkaliasdef.c"