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