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