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