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