]> Pileus Git - ~andy/gtk/blob - gdk/gdkwindow.c
gdk: Add GDK_WINDOW_STATE_FOCUSED to GdkWindowState
[~andy/gtk] / gdk / gdkwindow.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
3  * Josh MacDonald, Ryan Lortie
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26  */
27
28 #include "config.h"
29
30 #include <cairo-gobject.h>
31
32 #include "gdkwindow.h"
33
34 #include "gdkrectangle.h"
35 #include "gdkinternals.h"
36 #include "gdkintl.h"
37 #include "gdkscreenprivate.h"
38 #include "gdkdisplayprivate.h"
39 #include "gdkdeviceprivate.h"
40 #include "gdkvisualprivate.h"
41 #include "gdkmarshalers.h"
42 #include "gdkwindowimpl.h"
43
44 #include <math.h>
45
46 #undef DEBUG_WINDOW_PRINTING
47
48
49 /**
50  * SECTION:windows
51  * @Short_description: Onscreen display areas in the target window system
52  * @Title: Windows
53  *
54  * A #GdkWindow is a (usually) rectangular region on the screen.
55  * It's a low-level object, used to implement high-level objects such as
56  * #GtkWidget and #GtkWindow on the GTK+ level. A #GtkWindow is a toplevel
57  * window, the thing a user might think of as a "window" with a titlebar and
58  * so on; a #GtkWindow may contain many #GdkWindows. For example, each
59  * #GtkButton has a #GdkWindow associated with it.
60  *
61  * <refsect2 id="COMPOSITED-WINDOWS">
62  * <title>Composited Windows</title>
63  * <para>
64  * Normally, the windowing system takes care of rendering the contents of a
65  * child window onto its parent window. This mechanism can be intercepted by
66  * calling gdk_window_set_composited() on the child window. For a
67  * <firstterm>composited</firstterm> window it is the responsibility of the
68  * application to render the window contents at the right spot.
69  * </para>
70  * <example id="composited-window-example">
71  * <title>Composited windows</title>
72  * <programlisting>
73  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/gdk/composited-window-example.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>
74  * </programlisting></example>
75  * <para>
76  * In the example <xref linkend="composited-window-example"/>, a button is
77  * placed inside of an event box inside of a window. The event box is set as
78  * composited and therefore is no longer automatically drawn to the screen.
79  *
80  * When the contents of the event box change, an expose event is generated on
81  * its parent window (which, in this case, belongs to the toplevel #GtkWindow).
82  * The expose handler for this widget is responsible for merging the changes
83  * back on the screen in the way that it wishes.
84  *
85  * In our case, we merge the contents with a 50% transparency. We also set the
86  * background colour of the window to red. The effect is that the background
87  * shows through the button.
88  * </para>
89  * </refsect2>
90  * <refsect2 id="OFFSCREEN-WINDOWS">
91  * <title>Offscreen Windows</title>
92  * <para>
93  * Offscreen windows are more general than composited windows, since they allow
94  * not only to modify the rendering of the child window onto its parent, but
95  * also to apply coordinate transformations.
96  *
97  * To integrate an offscreen window into a window hierarchy, one has to call
98  * gdk_offscreen_window_set_embedder() and handle a number of signals. The
99  * #GdkWindow::pick-embedded-child signal on the embedder window is used to
100  * select an offscreen child at given coordinates, and the
101  * #GdkWindow::to-embedder and #GdkWindow::from-embedder signals on the
102  * offscreen window are used to translate coordinates between the embedder and
103  * the offscreen window.
104  *
105  * For rendering an offscreen window onto its embedder, the contents of the
106  * offscreen window are available as a surface, via
107  * gdk_offscreen_window_get_surface().
108  * </para>
109  * </refsect2>
110  */
111
112
113 /* Historically a GdkWindow always matches a platform native window,
114  * be it a toplevel window or a child window. In this setup the
115  * GdkWindow (and other GdkDrawables) were platform independent classes,
116  * and the actual platform specific implementation was in a delegate
117  * object availible as "impl" in the window object.
118  *
119  * With the addition of client side windows and offscreen windows this
120  * changes a bit. The application-visible GdkWindow object behaves as
121  * it did before, but not all such windows now have a corresponding native
122  * window. Instead windows that are "client side" are emulated by the gdk
123  * code such that clipping, drawing, moving, events etc work as expected.
124  *
125  * For GdkWindows that have a native window the "impl" object is the
126  * same as before. However, for all client side windows the impl object
127  * is shared with its parent (i.e. all client windows descendants of one
128  * native window has the same impl.
129  *
130  * Additionally there is a new type of platform independent impl object,
131  * GdkOffscreenWindow. All windows of type GDK_WINDOW_OFFSCREEN get an impl
132  * of this type (while their children are generally GDK_WINDOW_CHILD virtual
133  * windows). Such windows work by allocating a #cairo_surface_t as the backing
134  * store for drawing operations, which is resized with the window.
135  *
136  * GdkWindows have a pointer to the "impl window" they are in, i.e.
137  * the topmost GdkWindow which have the same "impl" value. This is stored
138  * in impl_window, which is different from the window itself only for client
139  * side windows.
140  * All GdkWindows (native or not) track the position of the window in the parent
141  * (x, y), the size of the window (width, height), the position of the window
142  * with respect to the impl window (abs_x, abs_y). We also track the clip
143  * region of the window wrt parent windows and siblings, in window-relative
144  * coordinates with and without child windows included (clip_region,
145  * clip_region_with_children).
146  *
147  * All toplevel windows are native windows, but also child windows can be
148  * native (although not children of offscreens). We always listen to
149  * a basic set of events (see get_native_event_mask) for these windows
150  * so that we can emulate events for any client side children.
151  *
152  * For native windows we apply the calculated clip region as a window shape
153  * so that eg. client side siblings that overlap the native child properly
154  * draws over the native child window.
155  *
156  * In order to minimize flicker and for performance we use a couple of cacheing
157  * tricks. First of all, every time we do a window to window copy area, for instance
158  * when moving a client side window or when scrolling/moving a region in a window
159  * we store this in outstanding_moves instead of applying immediately. We then
160  * delay this move until we really need it (because something depends on being
161  * able to read it), or until we're handing a redraw from an expose/invalidation
162  * (actually we delay it past redraw, but before blitting the double buffer
163  * to the window). This gives us two advantages. First of all it minimizes the time
164  * from the window is moved to the exposes related to that move, secondly it allows
165  * us to be smart about how to do the copy. We combine multiple moves into one (when
166  * possible) and we don't actually do copies to anything that is or will be
167  * invalidated and exposed anyway.
168  *
169  * Secondly, we use something called a "implicit paint" during repaint handling.
170  * An implicit paint is similar to a regular paint for the paint stack, but it is
171  * not put on the stack. Instead, it is set on the impl window, and later when
172  * regular gdk_window_begin_paint_region()  happen on a window of this impl window
173  * we reuse the surface from the implicit paint. During repaint we create and at the
174  * end flush an implicit paint, which means we can collect all the paints on
175  * multiple client side windows in the same backing store.
176  */
177
178 #define USE_BACKING_STORE       /* Appears to work on Win32, too, now. */
179
180 /* This adds a local value to the GdkVisibilityState enum */
181 #define GDK_VISIBILITY_NOT_VIEWABLE 3
182
183 enum {
184   PICK_EMBEDDED_CHILD, /* only called if children are embedded */
185   TO_EMBEDDER,
186   FROM_EMBEDDER,
187   CREATE_SURFACE,
188   LAST_SIGNAL
189 };
190
191 enum {
192   PROP_0,
193   PROP_CURSOR
194 };
195
196 typedef enum {
197   CLEAR_BG_NONE,
198   CLEAR_BG_WINCLEARED, /* Clear backgrounds except those that the window system clears */
199   CLEAR_BG_ALL
200 } ClearBg;
201
202 struct _GdkWindowPaint
203 {
204   cairo_region_t *region;
205   cairo_surface_t *surface;
206   guint uses_implicit : 1;
207   guint flushed : 1;
208   guint32 region_tag;
209 };
210
211 typedef struct {
212   cairo_region_t *dest_region; /* The destination region */
213   int dx, dy; /* The amount that the source was moved to reach dest_region */
214 } GdkWindowRegionMove;
215
216 /* Global info */
217
218 static void             gdk_window_drop_cairo_surface (GdkWindow *private);
219
220 static void gdk_window_free_paint_stack (GdkWindow *window);
221
222 static void gdk_window_finalize   (GObject              *object);
223
224 static void gdk_window_set_property (GObject      *object,
225                                      guint         prop_id,
226                                      const GValue *value,
227                                      GParamSpec   *pspec);
228 static void gdk_window_get_property (GObject      *object,
229                                      guint         prop_id,
230                                      GValue       *value,
231                                      GParamSpec   *pspec);
232
233 static void gdk_window_clear_backing_region (GdkWindow *window,
234                                              cairo_region_t *region);
235
236 static void recompute_visible_regions   (GdkWindow *private,
237                                          gboolean recalculate_siblings,
238                                          gboolean recalculate_children);
239 static void gdk_window_flush_outstanding_moves (GdkWindow *window);
240 static void gdk_window_flush_recursive  (GdkWindow *window);
241 static void do_move_region_bits_on_impl (GdkWindow *window,
242                                          cairo_region_t *region, /* In impl window coords */
243                                          int dx, int dy);
244 static void gdk_window_invalidate_in_parent (GdkWindow *private);
245 static void move_native_children        (GdkWindow *private);
246 static void update_cursor               (GdkDisplay *display,
247                                          GdkDevice  *device);
248 static void impl_window_add_update_area (GdkWindow *impl_window,
249                                          cairo_region_t *region);
250 static void gdk_window_region_move_free (GdkWindowRegionMove *move);
251 static void gdk_window_invalidate_region_full (GdkWindow       *window,
252                                                const cairo_region_t *region,
253                                                gboolean         invalidate_children,
254                                                ClearBg          clear_bg);
255 static void gdk_window_invalidate_rect_full (GdkWindow          *window,
256                                              const GdkRectangle *rect,
257                                              gboolean            invalidate_children,
258                                              ClearBg             clear_bg);
259
260 static guint signals[LAST_SIGNAL] = { 0 };
261
262 static gpointer parent_class = NULL;
263
264 static const cairo_user_data_key_t gdk_window_cairo_key;
265
266 static guint32
267 new_region_tag (void)
268 {
269   static guint32 tag = 0;
270
271   return ++tag;
272 }
273
274 G_DEFINE_ABSTRACT_TYPE (GdkWindow, gdk_window, G_TYPE_OBJECT)
275
276 GType
277 _gdk_paintable_get_type (void)
278 {
279   static GType paintable_type = 0;
280
281   if (!paintable_type)
282     {
283       const GTypeInfo paintable_info =
284       {
285         sizeof (GdkPaintableIface),  /* class_size */
286         NULL,                        /* base_init */
287         NULL,                        /* base_finalize */
288       };
289
290       paintable_type = g_type_register_static (G_TYPE_INTERFACE,
291                                                g_intern_static_string ("GdkPaintable"),
292                                                &paintable_info, 0);
293
294       g_type_interface_add_prerequisite (paintable_type, G_TYPE_OBJECT);
295     }
296
297   return paintable_type;
298 }
299
300 static void
301 gdk_window_init (GdkWindow *window)
302 {
303   /* 0-initialization is good for all other fields. */
304
305   window->window_type = GDK_WINDOW_CHILD;
306
307   window->state = GDK_WINDOW_STATE_WITHDRAWN;
308   window->width = 1;
309   window->height = 1;
310   window->toplevel_window_type = -1;
311   /* starts hidden */
312   window->effective_visibility = GDK_VISIBILITY_NOT_VIEWABLE;
313   window->visibility = GDK_VISIBILITY_FULLY_OBSCURED;
314   /* Default to unobscured since some backends don't send visibility events */
315   window->native_visibility = GDK_VISIBILITY_UNOBSCURED;
316
317   window->device_cursor = g_hash_table_new_full (NULL, NULL,
318                                                  NULL, g_object_unref);
319 }
320
321 /* Stop and return on the first non-NULL parent */
322 static gboolean
323 accumulate_get_window (GSignalInvocationHint *ihint,
324                        GValue                  *return_accu,
325                        const GValue            *handler_return,
326                        gpointer               data)
327 {
328   g_value_copy (handler_return, return_accu);
329   /* Continue while returning NULL */
330   return g_value_get_object (handler_return) == NULL;
331 }
332
333 static gboolean
334 create_surface_accumulator (GSignalInvocationHint *ihint,
335                             GValue                *return_accu,
336                             const GValue          *handler_return,
337                             gpointer               data)
338 {
339   g_value_copy (handler_return, return_accu);
340
341   /* Stop on the first non-NULL return value */
342   return g_value_get_boxed (handler_return) == NULL;
343 }
344
345 static GQuark quark_pointer_window = 0;
346
347 static void
348 gdk_window_class_init (GdkWindowClass *klass)
349 {
350   GObjectClass *object_class = G_OBJECT_CLASS (klass);
351
352   parent_class = g_type_class_peek_parent (klass);
353
354   object_class->finalize = gdk_window_finalize;
355   object_class->set_property = gdk_window_set_property;
356   object_class->get_property = gdk_window_get_property;
357
358   klass->create_surface = _gdk_offscreen_window_create_surface;
359
360   quark_pointer_window = g_quark_from_static_string ("gtk-pointer-window");
361
362
363   /* Properties */
364
365   /**
366    * GdkWindow:cursor:
367    *
368    * The mouse pointer for a #GdkWindow. See gdk_window_set_cursor() and
369    * gdk_window_get_cursor() for details.
370    *
371    * Since: 2.18
372    */
373   g_object_class_install_property (object_class,
374                                    PROP_CURSOR,
375                                    g_param_spec_object ("cursor",
376                                                         P_("Cursor"),
377                                                         P_("Cursor"),
378                                                         GDK_TYPE_CURSOR,
379                                                         G_PARAM_READWRITE));
380
381   /**
382    * GdkWindow::pick-embedded-child:
383    * @window: the window on which the signal is emitted
384    * @x: x coordinate in the window
385    * @y: y coordinate in the window
386    *
387    * The ::pick-embedded-child signal is emitted to find an embedded
388    * child at the given position.
389    *
390    * Returns: (transfer none): the #GdkWindow of the embedded child at
391    *     @x, @y, or %NULL
392    *
393    * Since: 2.18
394    */
395   signals[PICK_EMBEDDED_CHILD] =
396     g_signal_new (g_intern_static_string ("pick-embedded-child"),
397                   G_OBJECT_CLASS_TYPE (object_class),
398                   G_SIGNAL_RUN_LAST,
399                   G_STRUCT_OFFSET (GdkWindowClass, pick_embedded_child),
400                   accumulate_get_window, NULL,
401                   _gdk_marshal_OBJECT__DOUBLE_DOUBLE,
402                   GDK_TYPE_WINDOW,
403                   2,
404                   G_TYPE_DOUBLE,
405                   G_TYPE_DOUBLE);
406
407   /**
408    * GdkWindow::to-embedder:
409    * @window: the offscreen window on which the signal is emitted
410    * @offscreen-x: x coordinate in the offscreen window
411    * @offscreen-y: y coordinate in the offscreen window
412    * @embedder-x: (out) (type double): return location for the x
413    *     coordinate in the embedder window
414    * @embedder-y: (out) (type double): return location for the y
415    *     coordinate in the embedder window
416    *
417    * The ::to-embedder signal is emitted to translate coordinates
418    * in an offscreen window to its embedder.
419    *
420    * See also #GtkWindow::from-embedder.
421    *
422    * Since: 2.18
423    */
424   signals[TO_EMBEDDER] =
425     g_signal_new (g_intern_static_string ("to-embedder"),
426                   G_OBJECT_CLASS_TYPE (object_class),
427                   G_SIGNAL_RUN_LAST,
428                   G_STRUCT_OFFSET (GdkWindowClass, to_embedder),
429                   NULL, NULL,
430                   _gdk_marshal_VOID__DOUBLE_DOUBLE_POINTER_POINTER,
431                   G_TYPE_NONE,
432                   4,
433                   G_TYPE_DOUBLE,
434                   G_TYPE_DOUBLE,
435                   G_TYPE_POINTER,
436                   G_TYPE_POINTER);
437
438   /**
439    * GdkWindow::from-embedder:
440    * @window: the offscreen window on which the signal is emitted
441    * @embedder-x: x coordinate in the embedder window
442    * @embedder-y: y coordinate in the embedder window
443    * @offscreen-x: (out) (type double): return location for the x
444    *     coordinate in the offscreen window
445    * @offscreen-y: (out) (type double): return location for the y
446    *     coordinate in the offscreen window
447    *
448    * The ::from-embedder signal is emitted to translate coordinates
449    * in the embedder of an offscreen window to the offscreen window.
450    *
451    * See also #GtkWindow::to-embedder.
452    *
453    * Since: 2.18
454    */
455   signals[FROM_EMBEDDER] =
456     g_signal_new (g_intern_static_string ("from-embedder"),
457                   G_OBJECT_CLASS_TYPE (object_class),
458                   G_SIGNAL_RUN_LAST,
459                   G_STRUCT_OFFSET (GdkWindowClass, from_embedder),
460                   NULL, NULL,
461                   _gdk_marshal_VOID__DOUBLE_DOUBLE_POINTER_POINTER,
462                   G_TYPE_NONE,
463                   4,
464                   G_TYPE_DOUBLE,
465                   G_TYPE_DOUBLE,
466                   G_TYPE_POINTER,
467                   G_TYPE_POINTER);
468
469   /**
470    * GdkWindow::create-surface:
471    * @window: the offscreen window on which the signal is emitted
472    * @width: the width of the offscreen surface to create
473    * @height: the height of the offscreen surface to create
474    *
475    * The ::create-surface signal is emitted when an offscreen window
476    * needs its surface (re)created, which happens either when the the
477    * window is first drawn to, or when the window is being
478    * resized. The first signal handler that returns a non-%NULL
479    * surface will stop any further signal emission, and its surface
480    * will be used.
481    *
482    * Note that it is not possible to access the window's previous
483    * surface from within any callback of this signal. Calling
484    * gdk_offscreen_window_get_surface() will lead to a crash.
485    *
486    * Returns: the newly created #cairo_surface_t for the offscreen window
487    *
488    * Since: 3.0
489    */
490   signals[CREATE_SURFACE] =
491     g_signal_new (g_intern_static_string ("create-surface"),
492                   G_OBJECT_CLASS_TYPE (object_class),
493                   G_SIGNAL_RUN_LAST,
494                   G_STRUCT_OFFSET (GdkWindowClass, create_surface),
495                   create_surface_accumulator, NULL,
496                   _gdk_marshal_BOXED__INT_INT,
497                   CAIRO_GOBJECT_TYPE_SURFACE,
498                   2,
499                   G_TYPE_INT,
500                   G_TYPE_INT);
501 }
502
503 static void
504 device_removed_cb (GdkDeviceManager *device_manager,
505                    GdkDevice        *device,
506                    GdkWindow        *window)
507 {
508   window->devices_inside = g_list_remove (window->devices_inside, device);
509   g_hash_table_remove (window->device_cursor, device);
510
511   if (window->device_events)
512     g_hash_table_remove (window->device_events, device);
513 }
514
515 static void
516 gdk_window_finalize (GObject *object)
517 {
518   GdkWindow *window = GDK_WINDOW (object);
519   GdkDeviceManager *device_manager;
520
521   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
522   g_signal_handlers_disconnect_by_func (device_manager, device_removed_cb, window);
523
524   if (!GDK_WINDOW_DESTROYED (window))
525     {
526       if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
527         {
528           g_warning ("losing last reference to undestroyed window\n");
529           _gdk_window_destroy (window, FALSE);
530         }
531       else
532         /* We use TRUE here, to keep us from actually calling
533          * XDestroyWindow() on the window
534          */
535         _gdk_window_destroy (window, TRUE);
536     }
537
538   gdk_window_drop_cairo_surface (window);
539
540   if (window->impl)
541     {
542       g_object_unref (window->impl);
543       window->impl = NULL;
544     }
545
546   if (window->impl_window != window)
547     {
548       g_object_unref (window->impl_window);
549       window->impl_window = NULL;
550     }
551
552   if (window->shape)
553     cairo_region_destroy (window->shape);
554
555   if (window->input_shape)
556     cairo_region_destroy (window->input_shape);
557
558   if (window->cursor)
559     g_object_unref (window->cursor);
560
561   if (window->device_cursor)
562     g_hash_table_destroy (window->device_cursor);
563
564   if (window->device_events)
565     g_hash_table_destroy (window->device_events);
566
567   if (window->source_event_masks)
568     g_hash_table_destroy (window->source_event_masks);
569
570   if (window->devices_inside)
571     g_list_free (window->devices_inside);
572
573   G_OBJECT_CLASS (parent_class)->finalize (object);
574 }
575
576 static void
577 gdk_window_set_property (GObject      *object,
578                          guint         prop_id,
579                          const GValue *value,
580                          GParamSpec   *pspec)
581 {
582   GdkWindow *window = (GdkWindow *)object;
583
584   switch (prop_id)
585     {
586     case PROP_CURSOR:
587       gdk_window_set_cursor (window, g_value_get_object (value));
588       break;
589
590     default:
591       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
592       break;
593     }
594 }
595
596 static void
597 gdk_window_get_property (GObject    *object,
598                          guint       prop_id,
599                          GValue     *value,
600                          GParamSpec *pspec)
601 {
602   GdkWindow *window = (GdkWindow *) object;
603
604   switch (prop_id)
605     {
606     case PROP_CURSOR:
607       g_value_set_object (value, gdk_window_get_cursor (window));
608       break;
609
610     default:
611       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612       break;
613     }
614 }
615
616 static gboolean
617 gdk_window_is_offscreen (GdkWindow *window)
618 {
619   return window->window_type == GDK_WINDOW_OFFSCREEN;
620 }
621
622 static GdkWindow *
623 gdk_window_get_impl_window (GdkWindow *window)
624 {
625   return window->impl_window;
626 }
627
628 GdkWindow *
629 _gdk_window_get_impl_window (GdkWindow *window)
630 {
631   return gdk_window_get_impl_window (window);
632 }
633
634 static gboolean
635 gdk_window_has_impl (GdkWindow *window)
636 {
637   return window->impl_window == window;
638 }
639
640 static gboolean
641 gdk_window_is_toplevel (GdkWindow *window)
642 {
643   return
644     window->parent == NULL ||
645     window->parent->window_type == GDK_WINDOW_ROOT;
646 }
647
648 gboolean
649 _gdk_window_has_impl (GdkWindow *window)
650 {
651   return gdk_window_has_impl (window);
652 }
653
654 static gboolean
655 gdk_window_has_no_impl (GdkWindow *window)
656 {
657   return window->impl_window != window;
658 }
659
660 static void
661 remove_child_area (GdkWindow *private,
662                    GdkWindow *until,
663                    gboolean for_input,
664                    cairo_region_t *region)
665 {
666   GdkWindow *child;
667   cairo_region_t *child_region;
668   GdkRectangle r;
669   GList *l;
670   cairo_region_t *shape;
671
672   for (l = private->children; l; l = l->next)
673     {
674       child = l->data;
675
676       if (child == until)
677         break;
678
679       /* If region is empty already, no need to do
680          anything potentially costly */
681       if (cairo_region_is_empty (region))
682         break;
683
684       if (!GDK_WINDOW_IS_MAPPED (child) || child->input_only || child->composited)
685         continue;
686
687       /* Ignore offscreen children, as they don't draw in their parent and
688        * don't take part in the clipping */
689       if (gdk_window_is_offscreen (child))
690         continue;
691
692       r.x = child->x;
693       r.y = child->y;
694       r.width = child->width;
695       r.height = child->height;
696
697       /* Bail early if child totally outside region */
698       if (cairo_region_contains_rectangle (region, &r) == CAIRO_REGION_OVERLAP_OUT)
699         continue;
700
701       child_region = cairo_region_create_rectangle (&r);
702
703       if (child->shape)
704         {
705           /* Adjust shape region to parent window coords */
706           cairo_region_translate (child->shape, child->x, child->y);
707           cairo_region_intersect (child_region, child->shape);
708           cairo_region_translate (child->shape, -child->x, -child->y);
709         }
710       else if (private->window_type == GDK_WINDOW_FOREIGN)
711         {
712           shape = GDK_WINDOW_IMPL_GET_CLASS (child)->get_shape (child);
713           if (shape)
714             {
715               cairo_region_intersect (child_region, shape);
716               cairo_region_destroy (shape);
717             }
718         }
719
720       if (for_input)
721         {
722           if (child->input_shape)
723             cairo_region_intersect (child_region, child->input_shape);
724           else if (private->window_type == GDK_WINDOW_FOREIGN)
725             {
726               shape = GDK_WINDOW_IMPL_GET_CLASS (child)->get_input_shape (child);
727               if (shape)
728                 {
729                   cairo_region_intersect (child_region, shape);
730                   cairo_region_destroy (shape);
731                 }
732             }
733         }
734
735       cairo_region_subtract (region, child_region);
736       cairo_region_destroy (child_region);
737
738     }
739 }
740
741 static GdkVisibilityState
742 effective_visibility (GdkWindow *window)
743 {
744   GdkVisibilityState native;
745
746   if (!gdk_window_is_viewable (window))
747     return GDK_VISIBILITY_NOT_VIEWABLE;
748
749   native = window->impl_window->native_visibility;
750
751   if (native == GDK_VISIBILITY_FULLY_OBSCURED ||
752       window->visibility == GDK_VISIBILITY_FULLY_OBSCURED)
753     return GDK_VISIBILITY_FULLY_OBSCURED;
754   else if (native == GDK_VISIBILITY_UNOBSCURED)
755     return window->visibility;
756   else /* native PARTIAL, private partial or unobscured  */
757     return GDK_VISIBILITY_PARTIAL;
758 }
759
760 static void
761 gdk_window_update_visibility (GdkWindow *window)
762 {
763   GdkVisibilityState new_visibility;
764   GdkEvent *event;
765
766   new_visibility = effective_visibility (window);
767
768   if (new_visibility != window->effective_visibility)
769     {
770       window->effective_visibility = new_visibility;
771
772       if (new_visibility != GDK_VISIBILITY_NOT_VIEWABLE &&
773           window->event_mask & GDK_VISIBILITY_NOTIFY)
774         {
775           event = _gdk_make_event (window, GDK_VISIBILITY_NOTIFY,
776                                    NULL, FALSE);
777           event->visibility.state = new_visibility;
778         }
779     }
780 }
781
782 static void
783 gdk_window_update_visibility_recursively (GdkWindow *window,
784                                           GdkWindow *only_for_impl)
785 {
786   GdkWindow *child;
787   GList *l;
788
789   gdk_window_update_visibility (window);
790   for (l = window->children; l != NULL; l = l->next)
791     {
792       child = l->data;
793       if ((only_for_impl == NULL) ||
794           (only_for_impl == child->impl_window))
795         gdk_window_update_visibility_recursively (child, only_for_impl);
796     }
797 }
798
799 static gboolean
800 should_apply_clip_as_shape (GdkWindow *window)
801 {
802   return
803     gdk_window_has_impl (window) &&
804     /* Not for offscreens */
805     !gdk_window_is_offscreen (window) &&
806     /* or for toplevels */
807     !gdk_window_is_toplevel (window) &&
808     /* or for foreign windows */
809     window->window_type != GDK_WINDOW_FOREIGN &&
810     /* or for the root window */
811     window->window_type != GDK_WINDOW_ROOT;
812 }
813
814 static void
815 apply_shape (GdkWindow *window,
816              cairo_region_t *region)
817 {
818   GdkWindowImplClass *impl_class;
819
820   /* We trash whether we applied a shape so that
821      we can avoid unsetting it many times, which
822      could happen in e.g. apply_clip_as_shape as
823      windows get resized */
824   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
825   if (region)
826     impl_class->shape_combine_region (window,
827                                       region, 0, 0);
828   else if (window->applied_shape)
829     impl_class->shape_combine_region (window,
830                                       NULL, 0, 0);
831
832   window->applied_shape = region != NULL;
833 }
834
835 static gboolean
836 region_rect_equal (const cairo_region_t *region,
837                    const GdkRectangle *rect)
838 {
839     GdkRectangle extents;
840
841     if (cairo_region_num_rectangles (region) != 1)
842         return FALSE;
843
844     cairo_region_get_extents (region, &extents);
845
846     return extents.x == rect->x &&
847         extents.y == rect->y &&
848         extents.width == rect->width &&
849         extents.height == rect->height;
850 }
851
852 static void
853 apply_clip_as_shape (GdkWindow *window)
854 {
855   GdkRectangle r;
856
857   r.x = r.y = 0;
858   r.width = window->width;
859   r.height = window->height;
860
861   /* We only apply the clip region if would differ
862      from the actual clip region implied by the size
863      of the window. This is to avoid unneccessarily
864      adding meaningless shapes to all native subwindows */
865   if (!region_rect_equal (window->clip_region, &r))
866     apply_shape (window, window->clip_region);
867   else
868     apply_shape (window, NULL);
869 }
870
871 static void
872 recompute_visible_regions_internal (GdkWindow *private,
873                                     gboolean   recalculate_clip,
874                                     gboolean   recalculate_siblings,
875                                     gboolean   recalculate_children)
876 {
877   GdkRectangle r;
878   GList *l;
879   GdkWindow *child;
880   cairo_region_t *new_clip, *old_clip_region_with_children;
881   gboolean clip_region_changed;
882   gboolean abs_pos_changed;
883   int old_abs_x, old_abs_y;
884
885   old_abs_x = private->abs_x;
886   old_abs_y = private->abs_y;
887
888   /* Update absolute position */
889   if (gdk_window_has_impl (private))
890     {
891       /* Native window starts here */
892       private->abs_x = 0;
893       private->abs_y = 0;
894     }
895   else
896     {
897       private->abs_x = private->parent->abs_x + private->x;
898       private->abs_y = private->parent->abs_y + private->y;
899     }
900
901   abs_pos_changed =
902     private->abs_x != old_abs_x ||
903     private->abs_y != old_abs_y;
904
905   /* Update clip region based on:
906    * parent clip
907    * window size
908    * siblings in parents above window
909    */
910   clip_region_changed = FALSE;
911   if (recalculate_clip)
912     {
913       if (private->viewable)
914         {
915           /* Calculate visible region (sans children) in parent window coords */
916           r.x = private->x;
917           r.y = private->y;
918           r.width = private->width;
919           r.height = private->height;
920           new_clip = cairo_region_create_rectangle (&r);
921
922           if (!gdk_window_is_toplevel (private))
923             {
924               cairo_region_intersect (new_clip, private->parent->clip_region);
925
926               /* Remove all overlapping children from parent. */
927               remove_child_area (private->parent, private, FALSE, new_clip);
928             }
929
930           /* Convert from parent coords to window coords */
931           cairo_region_translate (new_clip, -private->x, -private->y);
932
933           if (private->shape)
934             cairo_region_intersect (new_clip, private->shape);
935         }
936       else
937         new_clip = cairo_region_create ();
938
939       if (private->clip_region == NULL ||
940           !cairo_region_equal (private->clip_region, new_clip))
941         clip_region_changed = TRUE;
942
943       if (private->clip_region)
944         cairo_region_destroy (private->clip_region);
945       private->clip_region = new_clip;
946
947       old_clip_region_with_children = private->clip_region_with_children;
948       private->clip_region_with_children = cairo_region_copy (private->clip_region);
949       if (private->window_type != GDK_WINDOW_ROOT)
950         remove_child_area (private, NULL, FALSE, private->clip_region_with_children);
951
952       if (clip_region_changed ||
953           !cairo_region_equal (private->clip_region_with_children, old_clip_region_with_children))
954           private->clip_tag = new_region_tag ();
955
956       if (old_clip_region_with_children)
957         cairo_region_destroy (old_clip_region_with_children);
958     }
959
960   if (clip_region_changed)
961     {
962       GdkVisibilityState visibility;
963       gboolean fully_visible;
964
965       if (cairo_region_is_empty (private->clip_region))
966         visibility = GDK_VISIBILITY_FULLY_OBSCURED;
967       else
968         {
969           if (private->shape)
970             {
971               fully_visible = cairo_region_equal (private->clip_region,
972                                                 private->shape);
973             }
974           else
975             {
976               r.x = 0;
977               r.y = 0;
978               r.width = private->width;
979               r.height = private->height;
980               fully_visible = region_rect_equal (private->clip_region, &r);
981             }
982
983           if (fully_visible)
984             visibility = GDK_VISIBILITY_UNOBSCURED;
985           else
986             visibility = GDK_VISIBILITY_PARTIAL;
987         }
988
989       if (private->visibility != visibility)
990         {
991           private->visibility = visibility;
992           gdk_window_update_visibility (private);
993         }
994     }
995
996   /* Update all children, recursively (except for root, where children are not exact). */
997   if ((abs_pos_changed || clip_region_changed || recalculate_children) &&
998       private->window_type != GDK_WINDOW_ROOT)
999     {
1000       for (l = private->children; l; l = l->next)
1001         {
1002           child = l->data;
1003           /* Only recalculate clip if the the clip region changed, otherwise
1004            * there is no way the child clip region could change (its has not e.g. moved)
1005            * Except if recalculate_children is set to force child updates
1006            */
1007           recompute_visible_regions_internal (child,
1008                                               recalculate_clip && (clip_region_changed || recalculate_children),
1009                                               FALSE, FALSE);
1010         }
1011     }
1012
1013   if (clip_region_changed &&
1014       should_apply_clip_as_shape (private))
1015     apply_clip_as_shape (private);
1016
1017   if (recalculate_siblings &&
1018       !gdk_window_is_toplevel (private))
1019     {
1020       /* If we moved a child window in parent or changed the stacking order, then we
1021        * need to recompute the visible area of all the other children in the parent
1022        */
1023       for (l = private->parent->children; l; l = l->next)
1024         {
1025           child = l->data;
1026
1027           if (child != private)
1028             recompute_visible_regions_internal (child, TRUE, FALSE, FALSE);
1029         }
1030
1031       /* We also need to recompute the _with_children clip for the parent */
1032       recompute_visible_regions_internal (private->parent, TRUE, FALSE, FALSE);
1033     }
1034
1035   if (private->cairo_surface && gdk_window_has_impl (private))
1036     {
1037       GdkWindowImplClass *iface = GDK_WINDOW_IMPL_GET_CLASS (private->impl);
1038
1039       private->cairo_surface = iface->resize_cairo_surface (private,
1040                                                             private->cairo_surface,
1041                                                             private->width,
1042                                                             private->height);
1043     }
1044   else if (private->cairo_surface)
1045     gdk_window_drop_cairo_surface (private);
1046 }
1047
1048 /* Call this when private has changed in one or more of these ways:
1049  *  size changed
1050  *  window moved
1051  *  new window added
1052  *  stacking order of window changed
1053  *  child deleted
1054  *
1055  * It will recalculate abs_x/y and the clip regions
1056  *
1057  * Unless the window didn't change stacking order or size/pos, pass in TRUE
1058  * for recalculate_siblings. (Mostly used internally for the recursion)
1059  *
1060  * If a child window was removed (and you can't use that child for
1061  * recompute_visible_regions), pass in TRUE for recalculate_children on the parent
1062  */
1063 static void
1064 recompute_visible_regions (GdkWindow *private,
1065                            gboolean recalculate_siblings,
1066                            gboolean recalculate_children)
1067 {
1068   recompute_visible_regions_internal (private,
1069                                       TRUE,
1070                                       recalculate_siblings,
1071                                       recalculate_children);
1072 }
1073
1074 void
1075 _gdk_window_update_size (GdkWindow *window)
1076 {
1077   recompute_visible_regions (window, TRUE, FALSE);
1078 }
1079
1080 /* Find the native window that would be just above "child"
1081  * in the native stacking order if "child" was a native window
1082  * (it doesn't have to be native). If there is no such native
1083  * window inside this native parent then NULL is returned.
1084  * If child is NULL, find lowest native window in parent.
1085  */
1086 static GdkWindow *
1087 find_native_sibling_above_helper (GdkWindow *parent,
1088                                   GdkWindow *child)
1089 {
1090   GdkWindow *w;
1091   GList *l;
1092
1093   if (child)
1094     {
1095       l = g_list_find (parent->children, child);
1096       g_assert (l != NULL); /* Better be a child of its parent... */
1097       l = l->prev; /* Start looking at the one above the child */
1098     }
1099   else
1100     l = g_list_last (parent->children);
1101
1102   for (; l != NULL; l = l->prev)
1103     {
1104       w = l->data;
1105
1106       if (gdk_window_has_impl (w))
1107         return w;
1108
1109       g_assert (parent != w);
1110       w = find_native_sibling_above_helper (w, NULL);
1111       if (w)
1112         return w;
1113     }
1114
1115   return NULL;
1116 }
1117
1118
1119 static GdkWindow *
1120 find_native_sibling_above (GdkWindow *parent,
1121                            GdkWindow *child)
1122 {
1123   GdkWindow *w;
1124
1125   w = find_native_sibling_above_helper (parent, child);
1126   if (w)
1127     return w;
1128
1129   if (gdk_window_has_impl (parent))
1130     return NULL;
1131   else
1132     return find_native_sibling_above (parent->parent, parent);
1133 }
1134
1135 static GdkEventMask
1136 get_native_device_event_mask (GdkWindow *private,
1137                               GdkDevice *device)
1138 {
1139   GdkEventMask event_mask;
1140
1141   if (device)
1142     event_mask = GPOINTER_TO_INT (g_hash_table_lookup (private->device_events, device));
1143   else
1144     event_mask = private->event_mask;
1145
1146   if (private->window_type == GDK_WINDOW_ROOT ||
1147       private->window_type == GDK_WINDOW_FOREIGN)
1148     return event_mask;
1149   else
1150     {
1151       GdkEventMask mask;
1152
1153       /* Do whatever the app asks to, since the app
1154        * may be asking for weird things for native windows,
1155        * but don't use motion hints as that may affect non-native
1156        * child windows that don't want it. Also, we need to
1157        * set all the app-specified masks since they will be picked
1158        * up by any implicit grabs (i.e. if they were not set as
1159        * native we would not get the events we need). */
1160       mask = private->event_mask & ~GDK_POINTER_MOTION_HINT_MASK;
1161
1162       /* We need thse for all native windows so we can
1163          emulate events on children: */
1164       mask |=
1165         GDK_EXPOSURE_MASK |
1166         GDK_VISIBILITY_NOTIFY_MASK |
1167         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK;
1168
1169       /* Additionally we select for pointer and button events
1170        * for toplevels as we need to get these to emulate
1171        * them for non-native subwindows. Even though we don't
1172        * select on them for all native windows we will get them
1173        * as the events are propagated out to the first window
1174        * that select for them.
1175        * Not selecting for button press on all windows is an
1176        * important thing, because in X only one client can do
1177        * so, and we don't want to unexpectedly prevent another
1178        * client from doing it.
1179        *
1180        * We also need to do the same if the app selects for button presses
1181        * because then we will get implicit grabs for this window, and the
1182        * event mask used for that grab is based on the rest of the mask
1183        * for the window, but we might need more events than this window
1184        * lists due to some non-native child window.
1185        */
1186       if (gdk_window_is_toplevel (private) ||
1187           mask & GDK_BUTTON_PRESS_MASK)
1188         mask |=
1189           GDK_POINTER_MOTION_MASK |
1190           GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1191           GDK_SCROLL_MASK;
1192
1193       return mask;
1194     }
1195 }
1196
1197 static GdkEventMask
1198 get_native_grab_event_mask (GdkEventMask grab_mask)
1199 {
1200   /* Similar to the above but for pointer events only */
1201   return
1202     GDK_POINTER_MOTION_MASK |
1203     GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1204     GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1205     GDK_SCROLL_MASK |
1206     (grab_mask &
1207      ~GDK_POINTER_MOTION_HINT_MASK);
1208 }
1209
1210 static GdkEventMask
1211 get_native_event_mask (GdkWindow *private)
1212 {
1213   return get_native_device_event_mask (private, NULL);
1214 }
1215
1216 /* Puts the native window in the right order wrt the other native windows
1217  * in the hierarchy, given the position it has in the client side data.
1218  * This is useful if some operation changed the stacking order.
1219  * This calls assumes the native window is now topmost in its native parent.
1220  */
1221 static void
1222 sync_native_window_stack_position (GdkWindow *window)
1223 {
1224   GdkWindow *above;
1225   GdkWindowImplClass *impl_class;
1226   GList listhead = {0};
1227
1228   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
1229
1230   above = find_native_sibling_above (window->parent, window);
1231   if (above)
1232     {
1233       listhead.data = window;
1234       impl_class->restack_under (above, &listhead);
1235     }
1236 }
1237
1238 /**
1239  * gdk_window_new: (constructor)
1240  * @parent: (allow-none): a #GdkWindow, or %NULL to create the window as a child of
1241  *   the default root window for the default display.
1242  * @attributes: attributes of the new window
1243  * @attributes_mask: mask indicating which fields in @attributes are valid
1244  *
1245  * Creates a new #GdkWindow using the attributes from
1246  * @attributes. See #GdkWindowAttr and #GdkWindowAttributesType for
1247  * more details.  Note: to use this on displays other than the default
1248  * display, @parent must be specified.
1249  *
1250  * Return value: (transfer full): the new #GdkWindow
1251  **/
1252 GdkWindow*
1253 gdk_window_new (GdkWindow     *parent,
1254                 GdkWindowAttr *attributes,
1255                 gint           attributes_mask)
1256 {
1257   GdkWindow *window;
1258   GdkScreen *screen;
1259   GdkDisplay *display;
1260   int x, y;
1261   gboolean native;
1262   GdkEventMask event_mask;
1263   GdkWindow *real_parent;
1264   GdkDeviceManager *device_manager;
1265
1266   g_return_val_if_fail (attributes != NULL, NULL);
1267
1268   if (!parent)
1269     {
1270       GDK_NOTE (MULTIHEAD,
1271                 g_warning ("gdk_window_new(): no parent specified reverting to parent = default root window"));
1272
1273       screen = gdk_screen_get_default ();
1274       parent = gdk_screen_get_root_window (screen);
1275     }
1276   else
1277     screen = gdk_window_get_screen (parent);
1278
1279   g_return_val_if_fail (GDK_IS_WINDOW (parent), NULL);
1280
1281   if (GDK_WINDOW_DESTROYED (parent))
1282     {
1283       g_warning ("gdk_window_new(): parent is destroyed\n");
1284       return NULL;
1285     }
1286
1287   display = gdk_screen_get_display (screen);
1288
1289   window = _gdk_display_create_window (display);
1290
1291   /* Windows with a foreign parent are treated as if they are children
1292    * of the root window, except for actual creation.
1293    */
1294   real_parent = parent;
1295   if (GDK_WINDOW_TYPE (parent) == GDK_WINDOW_FOREIGN)
1296     parent = gdk_screen_get_root_window (screen);
1297
1298   window->parent = parent;
1299
1300   window->accept_focus = TRUE;
1301   window->focus_on_map = TRUE;
1302
1303   if (attributes_mask & GDK_WA_X)
1304     x = attributes->x;
1305   else
1306     x = 0;
1307
1308   if (attributes_mask & GDK_WA_Y)
1309     y = attributes->y;
1310   else
1311     y = 0;
1312
1313   window->x = x;
1314   window->y = y;
1315   window->width = (attributes->width > 1) ? (attributes->width) : (1);
1316   window->height = (attributes->height > 1) ? (attributes->height) : (1);
1317
1318   if (attributes->wclass == GDK_INPUT_ONLY)
1319     {
1320       /* Backwards compatiblity - we've always ignored
1321        * attributes->window_type for input-only windows
1322        * before
1323        */
1324       if (GDK_WINDOW_TYPE (parent) == GDK_WINDOW_ROOT)
1325         window->window_type = GDK_WINDOW_TEMP;
1326       else
1327         window->window_type = GDK_WINDOW_CHILD;
1328     }
1329   else
1330     window->window_type = attributes->window_type;
1331
1332   /* Sanity checks */
1333   switch (window->window_type)
1334     {
1335     case GDK_WINDOW_TOPLEVEL:
1336     case GDK_WINDOW_TEMP:
1337     case GDK_WINDOW_OFFSCREEN:
1338       if (GDK_WINDOW_TYPE (parent) != GDK_WINDOW_ROOT)
1339         g_warning (G_STRLOC "Toplevel windows must be created as children of\n"
1340                    "of a window of type GDK_WINDOW_ROOT or GDK_WINDOW_FOREIGN");
1341     case GDK_WINDOW_CHILD:
1342       break;
1343       break;
1344     default:
1345       g_warning (G_STRLOC "cannot make windows of type %d", window->window_type);
1346       return NULL;
1347     }
1348
1349   if (attributes_mask & GDK_WA_VISUAL)
1350     window->visual = attributes->visual;
1351   else
1352     window->visual = gdk_screen_get_system_visual (screen);
1353
1354   window->event_mask = attributes->event_mask;
1355
1356   if (attributes->wclass == GDK_INPUT_OUTPUT)
1357     {
1358       window->input_only = FALSE;
1359       window->depth = window->visual->depth;
1360
1361       /* XXX: Cache this somehow? */
1362       window->background = cairo_pattern_create_rgb (0, 0, 0);
1363     }
1364   else
1365     {
1366       window->depth = 0;
1367       window->input_only = TRUE;
1368     }
1369
1370   if (window->parent)
1371     window->parent->children = g_list_prepend (window->parent->children, window);
1372
1373   native = FALSE;
1374   if (window->parent->window_type == GDK_WINDOW_ROOT)
1375     native = TRUE; /* Always use native windows for toplevels */
1376   else if (!window->input_only &&
1377            (attributes_mask & GDK_WA_VISUAL &&
1378             attributes->visual != gdk_window_get_visual (window->parent)))
1379     native = TRUE; /* InputOutput window with different visual than parent, needs native window */
1380
1381   if (gdk_window_is_offscreen (window))
1382     {
1383       _gdk_offscreen_window_new (window, attributes, attributes_mask);
1384       window->impl_window = window;
1385     }
1386   else if (native)
1387     {
1388       event_mask = get_native_event_mask (window);
1389
1390       /* Create the impl */
1391       _gdk_display_create_window_impl (display, window, real_parent, screen, event_mask, attributes, attributes_mask);
1392       window->impl_window = window;
1393
1394       /* This will put the native window topmost in the native parent, which may
1395        * be wrong wrt other native windows in the non-native hierarchy, so restack */
1396       if (!_gdk_window_has_impl (real_parent))
1397         sync_native_window_stack_position (window);
1398     }
1399   else
1400     {
1401       window->impl_window = g_object_ref (window->parent->impl_window);
1402       window->impl = g_object_ref (window->impl_window->impl);
1403     }
1404
1405   recompute_visible_regions (window, TRUE, FALSE);
1406
1407   gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
1408                                   (attributes->cursor) :
1409                                   NULL));
1410
1411   device_manager = gdk_display_get_device_manager (gdk_window_get_display (parent));
1412   g_signal_connect (device_manager, "device-removed",
1413                     G_CALLBACK (device_removed_cb), window);
1414
1415   return window;
1416 }
1417
1418 static gboolean
1419 is_parent_of (GdkWindow *parent,
1420               GdkWindow *child)
1421 {
1422   GdkWindow *w;
1423
1424   w = child;
1425   while (w != NULL)
1426     {
1427       if (w == parent)
1428         return TRUE;
1429
1430       w = gdk_window_get_parent (w);
1431     }
1432
1433   return FALSE;
1434 }
1435
1436 static void
1437 change_impl (GdkWindow *private,
1438              GdkWindow *impl_window,
1439              GdkWindowImpl *new)
1440 {
1441   GList *l;
1442   GdkWindow *child;
1443   GdkWindowImpl *old_impl;
1444   GdkWindow *old_impl_window;
1445
1446   old_impl = private->impl;
1447   old_impl_window = private->impl_window;
1448   if (private != impl_window)
1449     private->impl_window = g_object_ref (impl_window);
1450   else
1451     private->impl_window = private;
1452   private->impl = g_object_ref (new);
1453   if (old_impl_window != private)
1454     g_object_unref (old_impl_window);
1455   g_object_unref (old_impl);
1456
1457   for (l = private->children; l != NULL; l = l->next)
1458     {
1459       child = l->data;
1460
1461       if (child->impl == old_impl)
1462         change_impl (child, impl_window, new);
1463     }
1464 }
1465
1466 static void
1467 reparent_to_impl (GdkWindow *private)
1468 {
1469   GList *l;
1470   GdkWindow *child;
1471   gboolean show;
1472   GdkWindowImplClass *impl_class;
1473
1474   impl_class = GDK_WINDOW_IMPL_GET_CLASS (private->impl);
1475
1476   /* Enumerate in reverse order so we get the right order for the native
1477      windows (first in childrens list is topmost, and reparent places on top) */
1478   for (l = g_list_last (private->children); l != NULL; l = l->prev)
1479     {
1480       child = l->data;
1481
1482       if (child->impl == private->impl)
1483         reparent_to_impl (child);
1484       else
1485         {
1486           show = impl_class->reparent ((GdkWindow *)child,
1487                                        (GdkWindow *)private,
1488                                        child->x, child->y);
1489           if (show)
1490             gdk_window_show_unraised ((GdkWindow *)child);
1491         }
1492     }
1493 }
1494
1495
1496 /**
1497  * gdk_window_reparent:
1498  * @window: a #GdkWindow
1499  * @new_parent: new parent to move @window into
1500  * @x: X location inside the new parent
1501  * @y: Y location inside the new parent
1502  *
1503  * Reparents @window into the given @new_parent. The window being
1504  * reparented will be unmapped as a side effect.
1505  *
1506  **/
1507 void
1508 gdk_window_reparent (GdkWindow *window,
1509                      GdkWindow *new_parent,
1510                      gint       x,
1511                      gint       y)
1512 {
1513   GdkWindow *old_parent;
1514   GdkScreen *screen;
1515   gboolean show, was_mapped, applied_clip_as_shape;
1516   gboolean do_reparent_to_impl;
1517   GdkEventMask old_native_event_mask;
1518   GdkWindowImplClass *impl_class;
1519
1520   g_return_if_fail (GDK_IS_WINDOW (window));
1521   g_return_if_fail (new_parent == NULL || GDK_IS_WINDOW (new_parent));
1522   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_ROOT);
1523
1524   if (GDK_WINDOW_DESTROYED (window) ||
1525       (new_parent && GDK_WINDOW_DESTROYED (new_parent)))
1526     return;
1527
1528   screen = gdk_window_get_screen (window);
1529   if (!new_parent)
1530     new_parent = gdk_screen_get_root_window (screen);
1531
1532   /* No input-output children of input-only windows */
1533   if (new_parent->input_only && !window->input_only)
1534     return;
1535
1536   /* Don't create loops in hierarchy */
1537   if (is_parent_of (window, new_parent))
1538     return;
1539
1540   /* This might be wrong in the new parent, e.g. for non-native surfaces.
1541      To make sure we're ok, just wipe it. */
1542   gdk_window_drop_cairo_surface (window);
1543
1544   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
1545   old_parent = window->parent;
1546
1547   was_mapped = GDK_WINDOW_IS_MAPPED (window);
1548   show = FALSE;
1549
1550   /* Reparenting to toplevel. Ensure we have a native window so this can work */
1551   if (new_parent->window_type == GDK_WINDOW_ROOT ||
1552       new_parent->window_type == GDK_WINDOW_FOREIGN)
1553     gdk_window_ensure_native (window);
1554
1555   applied_clip_as_shape = should_apply_clip_as_shape (window);
1556
1557   old_native_event_mask = 0;
1558   do_reparent_to_impl = FALSE;
1559   if (gdk_window_has_impl (window))
1560     {
1561       old_native_event_mask = get_native_event_mask (window);
1562       /* Native window */
1563       show = impl_class->reparent (window, new_parent, x, y);
1564     }
1565   else
1566     {
1567       /* This shouldn't happen, as we created a native in this case, check anyway to see if that ever fails */
1568       g_assert (new_parent->window_type != GDK_WINDOW_ROOT &&
1569                 new_parent->window_type != GDK_WINDOW_FOREIGN);
1570
1571       show = was_mapped;
1572       gdk_window_hide (window);
1573
1574       do_reparent_to_impl = TRUE;
1575       change_impl (window,
1576                    new_parent->impl_window,
1577                    new_parent->impl);
1578     }
1579
1580   /* From here on, we treat parents of type GDK_WINDOW_FOREIGN like
1581    * the root window
1582    */
1583   if (GDK_WINDOW_TYPE (new_parent) == GDK_WINDOW_FOREIGN)
1584     {
1585       new_parent = gdk_screen_get_root_window (screen);
1586     }
1587
1588   if (old_parent)
1589     old_parent->children = g_list_remove (old_parent->children, window);
1590
1591   window->parent = new_parent;
1592   window->x = x;
1593   window->y = y;
1594
1595   new_parent->children = g_list_prepend (new_parent->children, window);
1596
1597   /* Switch the window type as appropriate */
1598
1599   switch (GDK_WINDOW_TYPE (new_parent))
1600     {
1601     case GDK_WINDOW_ROOT:
1602     case GDK_WINDOW_FOREIGN:
1603       if (window->toplevel_window_type != -1)
1604         GDK_WINDOW_TYPE (window) = window->toplevel_window_type;
1605       else if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1606         GDK_WINDOW_TYPE (window) = GDK_WINDOW_TOPLEVEL;
1607       break;
1608     case GDK_WINDOW_OFFSCREEN:
1609     case GDK_WINDOW_TOPLEVEL:
1610     case GDK_WINDOW_CHILD:
1611     case GDK_WINDOW_TEMP:
1612       if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD && \
1613           GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
1614         {
1615           /* Save the original window type so we can restore it if the
1616            * window is reparented back to be a toplevel
1617            */
1618           window->toplevel_window_type = GDK_WINDOW_TYPE (window);
1619           GDK_WINDOW_TYPE (window) = GDK_WINDOW_CHILD;
1620         }
1621     }
1622
1623   /* We might have changed window type for a native windows, so we
1624      need to change the event mask too. */
1625   if (gdk_window_has_impl (window))
1626     {
1627       GdkEventMask native_event_mask = get_native_event_mask (window);
1628
1629       if (native_event_mask != old_native_event_mask)
1630         impl_class->set_events (window, native_event_mask);
1631     }
1632
1633   _gdk_window_update_viewable (window);
1634
1635   recompute_visible_regions (window, TRUE, FALSE);
1636   if (old_parent && GDK_WINDOW_TYPE (old_parent) != GDK_WINDOW_ROOT)
1637     recompute_visible_regions (old_parent, FALSE, TRUE);
1638
1639   /* We used to apply the clip as the shape, but no more.
1640      Reset this to the real shape */
1641   if (gdk_window_has_impl (window) &&
1642       applied_clip_as_shape &&
1643       !should_apply_clip_as_shape (window))
1644     apply_shape (window, window->shape);
1645
1646   if (do_reparent_to_impl)
1647     reparent_to_impl (window);
1648   else
1649     {
1650       /* The reparent will have put the native window topmost in the native parent,
1651        * which may be wrong wrt other native windows in the non-native hierarchy,
1652        * so restack */
1653       if (!gdk_window_has_impl (new_parent))
1654         sync_native_window_stack_position (window);
1655     }
1656
1657   if (show)
1658     gdk_window_show_unraised (window);
1659   else
1660     _gdk_synthesize_crossing_events_for_geometry_change (window);
1661 }
1662
1663 /**
1664  * gdk_window_ensure_native:
1665  * @window: a #GdkWindow
1666  *
1667  * Tries to ensure that there is a window-system native window for this
1668  * GdkWindow. This may fail in some situations, returning %FALSE.
1669  *
1670  * Offscreen window and children of them can never have native windows.
1671  *
1672  * Some backends may not support native child windows.
1673  *
1674  * Returns: %TRUE if the window has a native window, %FALSE otherwise
1675  *
1676  * Since: 2.18
1677  */
1678 gboolean
1679 gdk_window_ensure_native (GdkWindow *window)
1680 {
1681   GdkWindow *impl_window;
1682   GdkWindowImpl *new_impl, *old_impl;
1683   GdkDisplay *display;
1684   GdkScreen *screen;
1685   GdkWindow *above;
1686   GList listhead;
1687   GdkWindowImplClass *impl_class;
1688
1689   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
1690
1691   if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT ||
1692       GDK_WINDOW_DESTROYED (window))
1693     return FALSE;
1694
1695   impl_window = gdk_window_get_impl_window (window);
1696
1697   if (gdk_window_is_offscreen (impl_window))
1698     return FALSE; /* native in offscreens not supported */
1699
1700   if (impl_window == window)
1701     /* Already has an impl, and its not offscreen . */
1702     return TRUE;
1703
1704   /* Need to create a native window */
1705
1706   gdk_window_drop_cairo_surface (window);
1707
1708   screen = gdk_window_get_screen (window);
1709   display = gdk_screen_get_display (screen);
1710
1711   old_impl = window->impl;
1712   _gdk_display_create_window_impl (display,
1713                                    window, window->parent,
1714                                    screen,
1715                                    get_native_event_mask (window),
1716                                    NULL, 0);
1717   new_impl = window->impl;
1718
1719   window->impl = old_impl;
1720   change_impl (window, window, new_impl);
1721
1722   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
1723
1724   /* Native window creation will put the native window topmost in the
1725    * native parent, which may be wrong wrt the position of the previous
1726    * non-native window wrt to the other non-native children, so correct this.
1727    */
1728   above = find_native_sibling_above (window->parent, window);
1729   if (above)
1730     {
1731       listhead.data = window;
1732       listhead.prev = NULL;
1733       listhead.next = NULL;
1734       impl_class->restack_under ((GdkWindow *)above, &listhead);
1735     }
1736
1737   recompute_visible_regions (window, FALSE, FALSE);
1738
1739   /* The shape may not have been set, as the clip region doesn't actually
1740      change, so do it here manually */
1741   if (should_apply_clip_as_shape (window))
1742     apply_clip_as_shape (window);
1743
1744   reparent_to_impl (window);
1745
1746   if (!window->input_only)
1747     impl_class->set_background (window, window->background);
1748
1749   impl_class->input_shape_combine_region (window,
1750                                           window->input_shape,
1751                                           0, 0);
1752
1753   if (gdk_window_is_viewable (window))
1754     impl_class->show (window, FALSE);
1755
1756   return TRUE;
1757 }
1758
1759 /**
1760  * _gdk_event_filter_unref:
1761  * @window: (allow-none): A #GdkWindow, or %NULL to be the global window
1762  * @filter: A window filter
1763  *
1764  * Release a reference to @filter.  Note this function may
1765  * mutate the list storage, so you need to handle this
1766  * if iterating over a list of filters.
1767  */
1768 void
1769 _gdk_event_filter_unref (GdkWindow       *window,
1770                          GdkEventFilter  *filter)
1771 {
1772   GList **filters;
1773   GList *tmp_list;
1774
1775   if (window == NULL)
1776     filters = &_gdk_default_filters;
1777   else
1778     filters = &window->filters;
1779
1780   tmp_list = *filters;
1781   while (tmp_list)
1782     {
1783       GdkEventFilter *iter_filter = tmp_list->data;
1784       GList *node;
1785
1786       node = tmp_list;
1787       tmp_list = tmp_list->next;
1788
1789       if (iter_filter != filter)
1790         continue;
1791
1792       g_assert (iter_filter->ref_count > 0);
1793
1794       filter->ref_count--;
1795       if (filter->ref_count != 0)
1796         continue;
1797
1798       *filters = g_list_remove_link (*filters, node);
1799       g_free (filter);
1800       g_list_free_1 (node);
1801     }
1802 }
1803
1804 static void
1805 window_remove_filters (GdkWindow *window)
1806 {
1807   while (window->filters)
1808     _gdk_event_filter_unref (window, window->filters->data);
1809 }
1810
1811 static void
1812 update_pointer_info_foreach (GdkDisplay           *display,
1813                              GdkDevice            *device,
1814                              GdkPointerWindowInfo *pointer_info,
1815                              gpointer              user_data)
1816 {
1817   GdkWindow *window = user_data;
1818
1819   if (pointer_info->toplevel_under_pointer == window)
1820     {
1821       g_object_unref (pointer_info->toplevel_under_pointer);
1822       pointer_info->toplevel_under_pointer = NULL;
1823     }
1824 }
1825
1826 static void
1827 window_remove_from_pointer_info (GdkWindow  *window,
1828                                  GdkDisplay *display)
1829 {
1830   _gdk_display_pointer_info_foreach (display,
1831                                      update_pointer_info_foreach,
1832                                      window);
1833 }
1834
1835 /**
1836  * _gdk_window_destroy_hierarchy:
1837  * @window: a #GdkWindow
1838  * @recursing: If TRUE, then this is being called because a parent
1839  *            was destroyed.
1840  * @recursing_native: If TRUE, then this is being called because a native parent
1841  *            was destroyed. This generally means that the call to the
1842  *            windowing system to destroy the window can be omitted, since
1843  *            it will be destroyed as a result of the parent being destroyed.
1844  *            Unless @foreign_destroy.
1845  * @foreign_destroy: If TRUE, the window or a parent was destroyed by some
1846  *            external agency. The window has already been destroyed and no
1847  *            windowing system calls should be made. (This may never happen
1848  *            for some windowing systems.)
1849  *
1850  * Internal function to destroy a window. Like gdk_window_destroy(),
1851  * but does not drop the reference count created by gdk_window_new().
1852  **/
1853 static void
1854 _gdk_window_destroy_hierarchy (GdkWindow *window,
1855                                gboolean   recursing,
1856                                gboolean   recursing_native,
1857                                gboolean   foreign_destroy)
1858 {
1859   GdkWindowImplClass *impl_class;
1860   GdkWindow *temp_window;
1861   GdkScreen *screen;
1862   GdkDisplay *display;
1863   GList *children;
1864   GList *tmp;
1865
1866   g_return_if_fail (GDK_IS_WINDOW (window));
1867
1868   if (GDK_WINDOW_DESTROYED (window))
1869     return;
1870
1871   display = gdk_window_get_display (window);
1872   screen = gdk_window_get_screen (window);
1873   temp_window = g_object_get_qdata (G_OBJECT (screen), quark_pointer_window);
1874   if (temp_window == window)
1875     g_object_set_qdata (G_OBJECT (screen), quark_pointer_window, NULL);
1876
1877
1878   switch (window->window_type)
1879     {
1880     case GDK_WINDOW_ROOT:
1881       if (!screen->closed)
1882         {
1883           g_error ("attempted to destroy root window");
1884           break;
1885         }
1886       /* else fall thru */
1887     case GDK_WINDOW_TOPLEVEL:
1888     case GDK_WINDOW_CHILD:
1889     case GDK_WINDOW_TEMP:
1890     case GDK_WINDOW_FOREIGN:
1891     case GDK_WINDOW_OFFSCREEN:
1892       if (window->window_type == GDK_WINDOW_FOREIGN && !foreign_destroy)
1893         {
1894           /* Logically, it probably makes more sense to send
1895            * a "destroy yourself" message to the foreign window
1896            * whether or not it's in our hierarchy; but for historical
1897            * reasons, we only send "destroy yourself" messages to
1898            * foreign windows in our hierarchy.
1899            */
1900           if (window->parent)
1901             {
1902               impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
1903
1904               if (gdk_window_has_impl (window))
1905                 impl_class->destroy_foreign (window);
1906             }
1907
1908           /* Also for historical reasons, we remove any filters
1909            * on a foreign window when it or a parent is destroyed;
1910            * this likely causes problems if two separate portions
1911            * of code are maintaining filter lists on a foreign window.
1912            */
1913           window_remove_filters (window);
1914         }
1915       else
1916         {
1917           if (window->parent)
1918             {
1919               if (window->parent->children)
1920                 window->parent->children = g_list_remove (window->parent->children, window);
1921
1922               if (!recursing &&
1923                   GDK_WINDOW_IS_MAPPED (window))
1924                 {
1925                   recompute_visible_regions (window, TRUE, FALSE);
1926                   gdk_window_invalidate_in_parent (window);
1927                 }
1928             }
1929
1930           gdk_window_free_paint_stack (window);
1931
1932           if (window->background)
1933             {
1934               cairo_pattern_destroy (window->background);
1935               window->background = NULL;
1936             }
1937
1938           if (window->window_type == GDK_WINDOW_FOREIGN)
1939             g_assert (window->children == NULL);
1940           else
1941             {
1942               children = tmp = window->children;
1943               window->children = NULL;
1944
1945               while (tmp)
1946                 {
1947                   temp_window = tmp->data;
1948                   tmp = tmp->next;
1949
1950                   if (temp_window)
1951                     _gdk_window_destroy_hierarchy (temp_window,
1952                                                    TRUE,
1953                                                    recursing_native || gdk_window_has_impl (window),
1954                                                    foreign_destroy);
1955                 }
1956
1957               g_list_free (children);
1958             }
1959
1960           _gdk_window_clear_update_area (window);
1961
1962           gdk_window_drop_cairo_surface (window);
1963
1964           impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
1965
1966           if (gdk_window_has_impl (window))
1967             impl_class->destroy (window, recursing_native,
1968                                  foreign_destroy);
1969           else
1970             {
1971               /* hide to make sure we repaint and break grabs */
1972               gdk_window_hide (window);
1973             }
1974
1975           window->state |= GDK_WINDOW_STATE_WITHDRAWN;
1976           window->parent = NULL;
1977           window->destroyed = TRUE;
1978
1979           window_remove_filters (window);
1980
1981           window_remove_from_pointer_info (window, display);
1982
1983           if (window->clip_region)
1984             {
1985               cairo_region_destroy (window->clip_region);
1986               window->clip_region = NULL;
1987             }
1988
1989           if (window->clip_region_with_children)
1990             {
1991               cairo_region_destroy (window->clip_region_with_children);
1992               window->clip_region_with_children = NULL;
1993             }
1994
1995           if (window->outstanding_moves)
1996             {
1997               g_list_foreach (window->outstanding_moves, (GFunc)gdk_window_region_move_free, NULL);
1998               g_list_free (window->outstanding_moves);
1999               window->outstanding_moves = NULL;
2000             }
2001         }
2002       break;
2003     }
2004 }
2005
2006 /**
2007  * _gdk_window_destroy:
2008  * @window: a #GdkWindow
2009  * @foreign_destroy: If TRUE, the window or a parent was destroyed by some
2010  *            external agency. The window has already been destroyed and no
2011  *            windowing system calls should be made. (This may never happen
2012  *            for some windowing systems.)
2013  *
2014  * Internal function to destroy a window. Like gdk_window_destroy(),
2015  * but does not drop the reference count created by gdk_window_new().
2016  **/
2017 void
2018 _gdk_window_destroy (GdkWindow *window,
2019                      gboolean   foreign_destroy)
2020 {
2021   _gdk_window_destroy_hierarchy (window, FALSE, FALSE, foreign_destroy);
2022 }
2023
2024 /**
2025  * gdk_window_destroy:
2026  * @window: a #GdkWindow
2027  *
2028  * Destroys the window system resources associated with @window and decrements @window's
2029  * reference count. The window system resources for all children of @window are also
2030  * destroyed, but the children's reference counts are not decremented.
2031  *
2032  * Note that a window will not be destroyed automatically when its reference count
2033  * reaches zero. You must call this function yourself before that happens.
2034  *
2035  **/
2036 void
2037 gdk_window_destroy (GdkWindow *window)
2038 {
2039   _gdk_window_destroy_hierarchy (window, FALSE, FALSE, FALSE);
2040   g_object_unref (window);
2041 }
2042
2043 /**
2044  * gdk_window_set_user_data:
2045  * @window: a #GdkWindow
2046  * @user_data: (allow-none) (type GObject.Object): user data
2047  *
2048  * For most purposes this function is deprecated in favor of
2049  * g_object_set_data(). However, for historical reasons GTK+ stores
2050  * the #GtkWidget that owns a #GdkWindow as user data on the
2051  * #GdkWindow. So, custom widget implementations should use
2052  * this function for that. If GTK+ receives an event for a #GdkWindow,
2053  * and the user data for the window is non-%NULL, GTK+ will assume the
2054  * user data is a #GtkWidget, and forward the event to that widget.
2055  *
2056  **/
2057 void
2058 gdk_window_set_user_data (GdkWindow *window,
2059                           gpointer   user_data)
2060 {
2061   g_return_if_fail (GDK_IS_WINDOW (window));
2062
2063   window->user_data = user_data;
2064 }
2065
2066 /**
2067  * gdk_window_get_user_data:
2068  * @window: a #GdkWindow
2069  * @data: (out): return location for user data
2070  *
2071  * Retrieves the user data for @window, which is normally the widget
2072  * that @window belongs to. See gdk_window_set_user_data().
2073  *
2074  **/
2075 void
2076 gdk_window_get_user_data (GdkWindow *window,
2077                           gpointer  *data)
2078 {
2079   g_return_if_fail (GDK_IS_WINDOW (window));
2080
2081   *data = window->user_data;
2082 }
2083
2084 /**
2085  * gdk_window_get_window_type:
2086  * @window: a #GdkWindow
2087  *
2088  * Gets the type of the window. See #GdkWindowType.
2089  *
2090  * Return value: type of window
2091  **/
2092 GdkWindowType
2093 gdk_window_get_window_type (GdkWindow *window)
2094 {
2095   g_return_val_if_fail (GDK_IS_WINDOW (window), (GdkWindowType) -1);
2096
2097   return GDK_WINDOW_TYPE (window);
2098 }
2099
2100 /**
2101  * gdk_window_get_visual:
2102  * @window: a #GdkWindow
2103  * 
2104  * Gets the #GdkVisual describing the pixel format of @window.
2105  * 
2106  * Return value: (transfer none): a #GdkVisual
2107  *
2108  * Since: 2.24
2109  **/
2110 GdkVisual*
2111 gdk_window_get_visual (GdkWindow *window)
2112 {
2113   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2114   
2115   return window->visual;
2116 }
2117
2118 /**
2119  * gdk_window_get_screen:
2120  * @window: a #GdkWindow
2121  * 
2122  * Gets the #GdkScreen associated with a #GdkWindow.
2123  * 
2124  * Return value: (transfer none): the #GdkScreen associated with @window
2125  *
2126  * Since: 2.24
2127  **/
2128 GdkScreen*
2129 gdk_window_get_screen (GdkWindow *window)
2130 {
2131   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2132
2133   return gdk_visual_get_screen (window->visual);
2134 }
2135
2136 /**
2137  * gdk_window_get_display:
2138  * @window: a #GdkWindow
2139  * 
2140  * Gets the #GdkDisplay associated with a #GdkWindow.
2141  * 
2142  * Return value: (transfer none): the #GdkDisplay associated with @window
2143  *
2144  * Since: 2.24
2145  **/
2146 GdkDisplay *
2147 gdk_window_get_display (GdkWindow *window)
2148 {
2149   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2150
2151   return gdk_screen_get_display (gdk_visual_get_screen (window->visual));
2152 }
2153 /**
2154  * gdk_window_is_destroyed:
2155  * @window: a #GdkWindow
2156  *
2157  * Check to see if a window is destroyed..
2158  *
2159  * Return value: %TRUE if the window is destroyed
2160  *
2161  * Since: 2.18
2162  **/
2163 gboolean
2164 gdk_window_is_destroyed (GdkWindow *window)
2165 {
2166   return GDK_WINDOW_DESTROYED (window);
2167 }
2168
2169 static void
2170 to_embedder (GdkWindow *window,
2171              gdouble    offscreen_x,
2172              gdouble    offscreen_y,
2173              gdouble   *embedder_x,
2174              gdouble   *embedder_y)
2175 {
2176   g_signal_emit (window, signals[TO_EMBEDDER], 0,
2177                  offscreen_x, offscreen_y,
2178                  embedder_x, embedder_y);
2179 }
2180
2181 static void
2182 from_embedder (GdkWindow *window,
2183                gdouble    embedder_x,
2184                gdouble    embedder_y,
2185                gdouble   *offscreen_x,
2186                gdouble   *offscreen_y)
2187 {
2188   g_signal_emit (window, signals[FROM_EMBEDDER], 0,
2189                  embedder_x, embedder_y,
2190                  offscreen_x, offscreen_y);
2191 }
2192
2193 /**
2194  * gdk_window_has_native:
2195  * @window: a #GdkWindow
2196  *
2197  * Checks whether the window has a native window or not. Note that
2198  * you can use gdk_window_ensure_native() if a native window is needed.
2199  *
2200  * Returns: %TRUE if the %window has a native window, %FALSE otherwise.
2201  *
2202  * Since: 2.22
2203  */
2204 gboolean
2205 gdk_window_has_native (GdkWindow *window)
2206 {
2207   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2208
2209   return window->parent == NULL || window->parent->impl != window->impl;
2210 }
2211
2212 /**
2213  * gdk_window_get_position:
2214  * @window: a #GdkWindow
2215  * @x: (out) (allow-none): X coordinate of window
2216  * @y: (out) (allow-none): Y coordinate of window
2217  *
2218  * Obtains the position of the window as reported in the
2219  * most-recently-processed #GdkEventConfigure. Contrast with
2220  * gdk_window_get_geometry() which queries the X server for the
2221  * current window position, regardless of which events have been
2222  * received or processed.
2223  *
2224  * The position coordinates are relative to the window's parent window.
2225  *
2226  **/
2227 void
2228 gdk_window_get_position (GdkWindow *window,
2229                          gint      *x,
2230                          gint      *y)
2231 {
2232   g_return_if_fail (GDK_IS_WINDOW (window));
2233
2234   if (x)
2235     *x = window->x;
2236   if (y)
2237     *y = window->y;
2238 }
2239
2240 /**
2241  * gdk_window_get_parent:
2242  * @window: a #GdkWindow
2243  *
2244  * Obtains the parent of @window, as known to GDK. Does not query the
2245  * X server; thus this returns the parent as passed to gdk_window_new(),
2246  * not the actual parent. This should never matter unless you're using
2247  * Xlib calls mixed with GDK calls on the X11 platform. It may also
2248  * matter for toplevel windows, because the window manager may choose
2249  * to reparent them.
2250  *
2251  * Note that you should use gdk_window_get_effective_parent() when
2252  * writing generic code that walks up a window hierarchy, because
2253  * gdk_window_get_parent() will most likely not do what you expect if
2254  * there are offscreen windows in the hierarchy.
2255  *
2256  * Return value: (transfer none): parent of @window
2257  **/
2258 GdkWindow*
2259 gdk_window_get_parent (GdkWindow *window)
2260 {
2261   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2262
2263   return window->parent;
2264 }
2265
2266 /**
2267  * gdk_window_get_effective_parent:
2268  * @window: a #GdkWindow
2269  *
2270  * Obtains the parent of @window, as known to GDK. Works like
2271  * gdk_window_get_parent() for normal windows, but returns the
2272  * window's embedder for offscreen windows.
2273  *
2274  * See also: gdk_offscreen_window_get_embedder()
2275  *
2276  * Return value: (transfer none): effective parent of @window
2277  *
2278  * Since: 2.22
2279  **/
2280 GdkWindow *
2281 gdk_window_get_effective_parent (GdkWindow *window)
2282 {
2283   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2284
2285   if (gdk_window_is_offscreen (window))
2286     return gdk_offscreen_window_get_embedder (window);
2287   else
2288     return window->parent;
2289 }
2290
2291 /**
2292  * gdk_window_get_toplevel:
2293  * @window: a #GdkWindow
2294  *
2295  * Gets the toplevel window that's an ancestor of @window.
2296  *
2297  * Any window type but %GDK_WINDOW_CHILD is considered a
2298  * toplevel window, as is a %GDK_WINDOW_CHILD window that
2299  * has a root window as parent.
2300  *
2301  * Note that you should use gdk_window_get_effective_toplevel() when
2302  * you want to get to a window's toplevel as seen on screen, because
2303  * gdk_window_get_toplevel() will most likely not do what you expect
2304  * if there are offscreen windows in the hierarchy.
2305  *
2306  * Return value: (transfer none): the toplevel window containing @window
2307  **/
2308 GdkWindow *
2309 gdk_window_get_toplevel (GdkWindow *window)
2310 {
2311   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2312
2313   while (window->window_type == GDK_WINDOW_CHILD)
2314     {
2315       if (gdk_window_is_toplevel (window))
2316         break;
2317       window = window->parent;
2318     }
2319
2320   return window;
2321 }
2322
2323 /**
2324  * gdk_window_get_effective_toplevel:
2325  * @window: a #GdkWindow
2326  *
2327  * Gets the toplevel window that's an ancestor of @window.
2328  *
2329  * Works like gdk_window_get_toplevel(), but treats an offscreen window's
2330  * embedder as its parent, using gdk_window_get_effective_parent().
2331  *
2332  * See also: gdk_offscreen_window_get_embedder()
2333  *
2334  * Return value: (transfer none): the effective toplevel window containing @window
2335  *
2336  * Since: 2.22
2337  **/
2338 GdkWindow *
2339 gdk_window_get_effective_toplevel (GdkWindow *window)
2340 {
2341   GdkWindow *parent;
2342
2343   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2344
2345   while ((parent = gdk_window_get_effective_parent (window)) != NULL &&
2346          (gdk_window_get_window_type (parent) != GDK_WINDOW_ROOT))
2347     window = parent;
2348
2349   return window;
2350 }
2351
2352 /**
2353  * gdk_window_get_children:
2354  * @window: a #GdkWindow
2355  *
2356  * Gets the list of children of @window known to GDK.
2357  * This function only returns children created via GDK,
2358  * so for example it's useless when used with the root window;
2359  * it only returns windows an application created itself.
2360  *
2361  * The returned list must be freed, but the elements in the
2362  * list need not be.
2363  *
2364  * Return value: (transfer container) (element-type GdkWindow):
2365  *     list of child windows inside @window
2366  **/
2367 GList*
2368 gdk_window_get_children (GdkWindow *window)
2369 {
2370   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2371
2372   if (GDK_WINDOW_DESTROYED (window))
2373     return NULL;
2374
2375   return g_list_copy (window->children);
2376 }
2377
2378 /**
2379  * gdk_window_peek_children:
2380  * @window: a #GdkWindow
2381  *
2382  * Like gdk_window_get_children(), but does not copy the list of
2383  * children, so the list does not need to be freed.
2384  *
2385  * Return value: (transfer none) (element-type GdkWindow):
2386  *     a reference to the list of child windows in @window
2387  **/
2388 GList *
2389 gdk_window_peek_children (GdkWindow *window)
2390 {
2391   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2392
2393   if (GDK_WINDOW_DESTROYED (window))
2394     return NULL;
2395
2396   return window->children;
2397 }
2398
2399 /**
2400  * gdk_window_add_filter: (skip)
2401  * @window: (allow-none): a #GdkWindow
2402  * @function: filter callback
2403  * @data: data to pass to filter callback
2404  *
2405  * Adds an event filter to @window, allowing you to intercept events
2406  * before they reach GDK. This is a low-level operation and makes it
2407  * easy to break GDK and/or GTK+, so you have to know what you're
2408  * doing. Pass %NULL for @window to get all events for all windows,
2409  * instead of events for a specific window.
2410  *
2411  * If you are interested in X GenericEvents, bear in mind that
2412  * XGetEventData() has been already called on the event, and
2413  * XFreeEventData() must not be called within @function.
2414  **/
2415 void
2416 gdk_window_add_filter (GdkWindow     *window,
2417                        GdkFilterFunc  function,
2418                        gpointer       data)
2419 {
2420   GList *tmp_list;
2421   GdkEventFilter *filter;
2422
2423   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
2424
2425   if (window && GDK_WINDOW_DESTROYED (window))
2426     return;
2427
2428   /* Filters are for the native events on the native window, so
2429      ensure there is a native window. */
2430   if (window)
2431     gdk_window_ensure_native (window);
2432
2433   if (window)
2434     tmp_list = window->filters;
2435   else
2436     tmp_list = _gdk_default_filters;
2437
2438   while (tmp_list)
2439     {
2440       filter = (GdkEventFilter *)tmp_list->data;
2441       if ((filter->function == function) && (filter->data == data))
2442         {
2443           filter->ref_count++;
2444           return;
2445         }
2446       tmp_list = tmp_list->next;
2447     }
2448
2449   filter = g_new (GdkEventFilter, 1);
2450   filter->function = function;
2451   filter->data = data;
2452   filter->ref_count = 1;
2453   filter->flags = 0;
2454
2455   if (window)
2456     window->filters = g_list_append (window->filters, filter);
2457   else
2458     _gdk_default_filters = g_list_append (_gdk_default_filters, filter);
2459 }
2460
2461 /**
2462  * gdk_window_remove_filter: (skip)
2463  * @window: a #GdkWindow
2464  * @function: previously-added filter function
2465  * @data: user data for previously-added filter function
2466  *
2467  * Remove a filter previously added with gdk_window_add_filter().
2468  */
2469 void
2470 gdk_window_remove_filter (GdkWindow     *window,
2471                           GdkFilterFunc  function,
2472                           gpointer       data)
2473 {
2474   GList *tmp_list;
2475   GdkEventFilter *filter;
2476
2477   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
2478
2479   if (window)
2480     tmp_list = window->filters;
2481   else
2482     tmp_list = _gdk_default_filters;
2483
2484   while (tmp_list)
2485     {
2486       filter = (GdkEventFilter *)tmp_list->data;
2487       tmp_list = tmp_list->next;
2488
2489       if ((filter->function == function) && (filter->data == data))
2490         {
2491           filter->flags |= GDK_EVENT_FILTER_REMOVED;
2492
2493           _gdk_event_filter_unref (window, filter);
2494
2495           return;
2496         }
2497     }
2498 }
2499
2500 /**
2501  * gdk_screen_get_toplevel_windows:
2502  * @screen: The #GdkScreen where the toplevels are located.
2503  *
2504  * Obtains a list of all toplevel windows known to GDK on the screen @screen.
2505  * A toplevel window is a child of the root window (see
2506  * gdk_get_default_root_window()).
2507  *
2508  * The returned list should be freed with g_list_free(), but
2509  * its elements need not be freed.
2510  *
2511  * Return value: (transfer container) (element-type GdkWindow):
2512  *     list of toplevel windows, free with g_list_free()
2513  *
2514  * Since: 2.2
2515  **/
2516 GList *
2517 gdk_screen_get_toplevel_windows (GdkScreen *screen)
2518 {
2519   GdkWindow * root_window;
2520   GList *new_list = NULL;
2521   GList *tmp_list;
2522
2523   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
2524
2525   root_window = gdk_screen_get_root_window (screen);
2526
2527   tmp_list = root_window->children;
2528   while (tmp_list)
2529     {
2530       GdkWindow *w = tmp_list->data;
2531
2532       if (w->window_type != GDK_WINDOW_FOREIGN)
2533         new_list = g_list_prepend (new_list, w);
2534       tmp_list = tmp_list->next;
2535     }
2536
2537   return new_list;
2538 }
2539
2540 /**
2541  * gdk_window_is_visible:
2542  * @window: a #GdkWindow
2543  *
2544  * Checks whether the window has been mapped (with gdk_window_show() or
2545  * gdk_window_show_unraised()).
2546  *
2547  * Return value: %TRUE if the window is mapped
2548  **/
2549 gboolean
2550 gdk_window_is_visible (GdkWindow *window)
2551 {
2552   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2553
2554   return GDK_WINDOW_IS_MAPPED (window);
2555 }
2556
2557 /**
2558  * gdk_window_is_viewable:
2559  * @window: a #GdkWindow
2560  *
2561  * Check if the window and all ancestors of the window are
2562  * mapped. (This is not necessarily "viewable" in the X sense, since
2563  * we only check as far as we have GDK window parents, not to the root
2564  * window.)
2565  *
2566  * Return value: %TRUE if the window is viewable
2567  **/
2568 gboolean
2569 gdk_window_is_viewable (GdkWindow *window)
2570 {
2571   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2572
2573   if (window->destroyed)
2574     return FALSE;
2575
2576   return window->viewable;
2577 }
2578
2579 /**
2580  * gdk_window_get_state:
2581  * @window: a #GdkWindow
2582  *
2583  * Gets the bitwise OR of the currently active window state flags,
2584  * from the #GdkWindowState enumeration.
2585  *
2586  * Return value: window state bitfield
2587  **/
2588 GdkWindowState
2589 gdk_window_get_state (GdkWindow *window)
2590 {
2591   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2592
2593   return window->state;
2594 }
2595
2596 static cairo_content_t
2597 gdk_window_get_content (GdkWindow *window)
2598 {
2599   cairo_surface_t *surface;
2600   cairo_content_t content;
2601
2602   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2603
2604   surface = _gdk_window_ref_cairo_surface (window);
2605   content = cairo_surface_get_content (surface);
2606   cairo_surface_destroy (surface);
2607
2608   return content;
2609 }
2610
2611 /* This creates an empty "implicit" paint region for the impl window.
2612  * By itself this does nothing, but real paints to this window
2613  * or children of it can use this surface as backing to avoid allocating
2614  * multiple surfaces for subwindow rendering. When doing so they
2615  * add to the region of the implicit paint region, which will be
2616  * pushed to the window when the implicit paint region is ended.
2617  * Such paints should not copy anything to the window on paint end, but
2618  * should rely on the implicit paint end.
2619  * The implicit paint will be automatically ended if someone draws
2620  * directly to the window or a child window.
2621  */
2622 static gboolean
2623 gdk_window_begin_implicit_paint (GdkWindow *window, GdkRectangle *rect)
2624 {
2625   GdkWindowPaint *paint;
2626
2627   g_assert (gdk_window_has_impl (window));
2628
2629   if (GDK_IS_PAINTABLE (window->impl))
2630     return FALSE; /* Implementation does double buffering */
2631
2632   if (window->paint_stack != NULL ||
2633       window->implicit_paint != NULL)
2634     return FALSE; /* Don't stack implicit paints */
2635
2636   /* Never do implicit paints for foreign windows, they don't need
2637    * double buffer combination since they have no client side children,
2638    * and creating surfaces for them is risky since they could disappear
2639    * at any time
2640    */
2641   if (window->window_type == GDK_WINDOW_FOREIGN)
2642     return FALSE;
2643
2644   paint = g_new (GdkWindowPaint, 1);
2645   paint->region = cairo_region_create (); /* Empty */
2646   paint->uses_implicit = FALSE;
2647   paint->flushed = FALSE;
2648   paint->surface = gdk_window_create_similar_surface (window,
2649                                                       gdk_window_get_content (window),
2650                                                       MAX (rect->width, 1),
2651                                                       MAX (rect->height, 1));
2652   cairo_surface_set_device_offset (paint->surface, -rect->x, -rect->y);
2653
2654   window->implicit_paint = paint;
2655
2656   return TRUE;
2657 }
2658
2659 static cairo_surface_t *
2660 gdk_window_ref_impl_surface (GdkWindow *window)
2661 {
2662   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->ref_cairo_surface (gdk_window_get_impl_window (window));
2663 }
2664
2665 static cairo_t *
2666 gdk_cairo_create_for_impl (GdkWindow *window)
2667 {
2668   cairo_surface_t *surface;
2669   cairo_t *cr;
2670
2671   surface = gdk_window_ref_impl_surface (window);
2672   cr = cairo_create (surface);
2673
2674   cairo_surface_destroy (surface);
2675
2676   return cr;
2677 }
2678
2679 /* Ensure that all content related to this (sub)window is pushed to the
2680    native region. If there is an active paint then that area is not
2681    pushed, in order to not show partially finished double buffers. */
2682 static void
2683 gdk_window_flush_implicit_paint (GdkWindow *window)
2684 {
2685   GdkWindow *impl_window;
2686   GdkWindowPaint *paint;
2687   cairo_region_t *region;
2688   GSList *list;
2689
2690   impl_window = gdk_window_get_impl_window (window);
2691   if (impl_window->implicit_paint == NULL)
2692     return;
2693
2694   paint = impl_window->implicit_paint;
2695   paint->flushed = TRUE;
2696   region = cairo_region_copy (window->clip_region_with_children);
2697
2698   /* Don't flush active double buffers, as that may show partially done
2699    * rendering */
2700   for (list = window->paint_stack; list != NULL; list = list->next)
2701     {
2702       GdkWindowPaint *tmp_paint = list->data;
2703
2704       cairo_region_subtract (region, tmp_paint->region);
2705     }
2706
2707   cairo_region_translate (region, -window->abs_x, -window->abs_y);
2708   cairo_region_intersect (region, paint->region);
2709
2710   if (!GDK_WINDOW_DESTROYED (window) && !cairo_region_is_empty (region))
2711     {
2712       cairo_t *cr;
2713
2714       /* Remove flushed region from the implicit paint */
2715       cairo_region_subtract (paint->region, region);
2716
2717       /* Some regions are valid, push these to window now */
2718       cr = gdk_cairo_create_for_impl (window);
2719       gdk_cairo_region (cr, region);
2720       cairo_clip (cr);
2721       cairo_set_source_surface (cr, paint->surface, 0, 0);
2722       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
2723       cairo_paint (cr);
2724       cairo_destroy (cr);
2725     }
2726   
2727   cairo_region_destroy (region);
2728 }
2729
2730 /* Ends an implicit paint, paired with gdk_window_begin_implicit_paint returning TRUE */
2731 static void
2732 gdk_window_end_implicit_paint (GdkWindow *window)
2733 {
2734   GdkWindowPaint *paint;
2735
2736   g_assert (gdk_window_has_impl (window));
2737
2738   g_assert (window->implicit_paint != NULL);
2739
2740   paint = window->implicit_paint;
2741
2742   window->implicit_paint = NULL;
2743
2744   if (!GDK_WINDOW_DESTROYED (window) && !cairo_region_is_empty (paint->region))
2745     {
2746       cairo_t *cr;
2747
2748       /* Some regions are valid, push these to window now */
2749       cr = gdk_cairo_create_for_impl (window);
2750       gdk_cairo_region (cr, paint->region);
2751       cairo_clip (cr);
2752       cairo_set_source_surface (cr, paint->surface, 0, 0);
2753       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
2754       cairo_paint (cr);
2755       cairo_destroy (cr);
2756     }
2757   
2758   cairo_region_destroy (paint->region);
2759
2760   cairo_surface_destroy (paint->surface);
2761   g_free (paint);
2762 }
2763
2764 /**
2765  * gdk_window_begin_paint_rect:
2766  * @window: a #GdkWindow
2767  * @rectangle: rectangle you intend to draw to
2768  *
2769  * A convenience wrapper around gdk_window_begin_paint_region() which
2770  * creates a rectangular region for you. See
2771  * gdk_window_begin_paint_region() for details.
2772  *
2773  **/
2774 void
2775 gdk_window_begin_paint_rect (GdkWindow          *window,
2776                              const GdkRectangle *rectangle)
2777 {
2778   cairo_region_t *region;
2779
2780   g_return_if_fail (GDK_IS_WINDOW (window));
2781
2782   region = cairo_region_create_rectangle (rectangle);
2783   gdk_window_begin_paint_region (window, region);
2784   cairo_region_destroy (region);
2785 }
2786
2787 /**
2788  * gdk_window_begin_paint_region:
2789  * @window: a #GdkWindow
2790  * @region: region you intend to draw to
2791  *
2792  * Indicates that you are beginning the process of redrawing @region.
2793  * A backing store (offscreen buffer) large enough to contain @region
2794  * will be created. The backing store will be initialized with the
2795  * background color or background surface for @window. Then, all
2796  * drawing operations performed on @window will be diverted to the
2797  * backing store.  When you call gdk_window_end_paint(), the backing
2798  * store will be copied to @window, making it visible onscreen. Only
2799  * the part of @window contained in @region will be modified; that is,
2800  * drawing operations are clipped to @region.
2801  *
2802  * The net result of all this is to remove flicker, because the user
2803  * sees the finished product appear all at once when you call
2804  * gdk_window_end_paint(). If you draw to @window directly without
2805  * calling gdk_window_begin_paint_region(), the user may see flicker
2806  * as individual drawing operations are performed in sequence.  The
2807  * clipping and background-initializing features of
2808  * gdk_window_begin_paint_region() are conveniences for the
2809  * programmer, so you can avoid doing that work yourself.
2810  *
2811  * When using GTK+, the widget system automatically places calls to
2812  * gdk_window_begin_paint_region() and gdk_window_end_paint() around
2813  * emissions of the expose_event signal. That is, if you're writing an
2814  * expose event handler, you can assume that the exposed area in
2815  * #GdkEventExpose has already been cleared to the window background,
2816  * is already set as the clip region, and already has a backing store.
2817  * Therefore in most cases, application code need not call
2818  * gdk_window_begin_paint_region(). (You can disable the automatic
2819  * calls around expose events on a widget-by-widget basis by calling
2820  * gtk_widget_set_double_buffered().)
2821  *
2822  * If you call this function multiple times before calling the
2823  * matching gdk_window_end_paint(), the backing stores are pushed onto
2824  * a stack. gdk_window_end_paint() copies the topmost backing store
2825  * onscreen, subtracts the topmost region from all other regions in
2826  * the stack, and pops the stack. All drawing operations affect only
2827  * the topmost backing store in the stack. One matching call to
2828  * gdk_window_end_paint() is required for each call to
2829  * gdk_window_begin_paint_region().
2830  *
2831  **/
2832 void
2833 gdk_window_begin_paint_region (GdkWindow       *window,
2834                                const cairo_region_t *region)
2835 {
2836 #ifdef USE_BACKING_STORE
2837   GdkRectangle clip_box;
2838   GdkWindowPaint *paint, *implicit_paint;
2839   GdkWindow *impl_window;
2840   GSList *list;
2841
2842   g_return_if_fail (GDK_IS_WINDOW (window));
2843
2844   if (GDK_WINDOW_DESTROYED (window))
2845     return;
2846
2847   if (GDK_IS_PAINTABLE (window->impl))
2848     {
2849       GdkPaintableIface *iface = GDK_PAINTABLE_GET_IFACE (window->impl);
2850
2851       if (iface->begin_paint_region)
2852         iface->begin_paint_region ((GdkPaintable*)window->impl, window, region);
2853
2854       return;
2855     }
2856
2857   impl_window = gdk_window_get_impl_window (window);
2858   implicit_paint = impl_window->implicit_paint;
2859
2860   paint = g_new (GdkWindowPaint, 1);
2861   paint->region = cairo_region_copy (region);
2862   paint->region_tag = new_region_tag ();
2863
2864   cairo_region_intersect (paint->region, window->clip_region_with_children);
2865   cairo_region_get_extents (paint->region, &clip_box);
2866
2867   cairo_region_translate (paint->region, window->abs_x, window->abs_y);
2868
2869   /* Mark the region as valid on the implicit paint */
2870
2871   if (implicit_paint)
2872     cairo_region_union (implicit_paint->region, paint->region);
2873
2874   /* Convert back to normal coords */
2875   cairo_region_translate (paint->region, -window->abs_x, -window->abs_y);
2876
2877   if (implicit_paint)
2878     {
2879       paint->uses_implicit = TRUE;
2880       paint->surface = cairo_surface_create_for_rectangle (implicit_paint->surface,
2881                                                            window->abs_x + clip_box.x,
2882                                                            window->abs_y + clip_box.y,
2883                                                            MAX (clip_box.width, 1),
2884                                                            MAX (clip_box.height, 1));
2885     }
2886   else
2887     {
2888       paint->uses_implicit = FALSE;
2889       paint->surface = gdk_window_create_similar_surface (window,
2890                                                           gdk_window_get_content (window),
2891                                                           MAX (clip_box.width, 1),
2892                                                           MAX (clip_box.height, 1));
2893     }
2894   cairo_surface_set_device_offset (paint->surface, -clip_box.x, -clip_box.y);
2895
2896   for (list = window->paint_stack; list != NULL; list = list->next)
2897     {
2898       GdkWindowPaint *tmp_paint = list->data;
2899
2900       cairo_region_subtract (tmp_paint->region, paint->region);
2901     }
2902
2903   window->paint_stack = g_slist_prepend (window->paint_stack, paint);
2904
2905   if (!cairo_region_is_empty (paint->region))
2906     {
2907       gdk_window_clear_backing_region (window,
2908                                        paint->region);
2909     }
2910
2911 #endif /* USE_BACKING_STORE */
2912 }
2913
2914 /**
2915  * gdk_window_end_paint:
2916  * @window: a #GdkWindow
2917  *
2918  * Indicates that the backing store created by the most recent call to
2919  * gdk_window_begin_paint_region() should be copied onscreen and
2920  * deleted, leaving the next-most-recent backing store or no backing
2921  * store at all as the active paint region. See
2922  * gdk_window_begin_paint_region() for full details. It is an error to
2923  * call this function without a matching
2924  * gdk_window_begin_paint_region() first.
2925  *
2926  **/
2927 void
2928 gdk_window_end_paint (GdkWindow *window)
2929 {
2930 #ifdef USE_BACKING_STORE
2931   GdkWindow *composited;
2932   GdkWindowPaint *paint;
2933   GdkRectangle clip_box;
2934   cairo_region_t *full_clip;
2935
2936   g_return_if_fail (GDK_IS_WINDOW (window));
2937
2938   if (GDK_WINDOW_DESTROYED (window))
2939     return;
2940
2941   if (GDK_IS_PAINTABLE (window->impl))
2942     {
2943       GdkPaintableIface *iface = GDK_PAINTABLE_GET_IFACE (window->impl);
2944
2945       if (iface->end_paint)
2946         iface->end_paint ((GdkPaintable*)window->impl);
2947       return;
2948     }
2949
2950   if (window->paint_stack == NULL)
2951     {
2952       g_warning (G_STRLOC": no preceding call to gdk_window_begin_paint_region(), see documentation");
2953       return;
2954     }
2955
2956   paint = window->paint_stack->data;
2957
2958   window->paint_stack = g_slist_delete_link (window->paint_stack,
2959                                              window->paint_stack);
2960
2961   cairo_region_get_extents (paint->region, &clip_box);
2962
2963   if (!paint->uses_implicit)
2964     {
2965       cairo_t *cr;
2966
2967       gdk_window_flush_outstanding_moves (window);
2968
2969       full_clip = cairo_region_copy (window->clip_region_with_children);
2970       cairo_region_intersect (full_clip, paint->region);
2971
2972       cr = gdk_cairo_create (window);
2973       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
2974       cairo_set_source_surface (cr, paint->surface, 0, 0);
2975       gdk_cairo_region (cr, full_clip);
2976       cairo_fill (cr);
2977
2978       cairo_destroy (cr);
2979       cairo_region_destroy (full_clip);
2980     }
2981
2982   cairo_surface_destroy (paint->surface);
2983   cairo_region_destroy (paint->region);
2984   g_free (paint);
2985
2986   /* find a composited window in our hierarchy to signal its
2987    * parent to redraw, calculating the clip box as we go...
2988    *
2989    * stop if parent becomes NULL since then we'd have nowhere
2990    * to draw (ie: 'composited' will always be non-NULL here).
2991    */
2992   for (composited = window;
2993        composited->parent;
2994        composited = composited->parent)
2995     {
2996       clip_box.x += composited->x;
2997       clip_box.y += composited->y;
2998       clip_box.width = MIN (clip_box.width, composited->parent->width - clip_box.x);
2999       clip_box.height = MIN (clip_box.height, composited->parent->height - clip_box.y);
3000
3001       if (composited->composited)
3002         {
3003           gdk_window_invalidate_rect (GDK_WINDOW (composited->parent),
3004                                       &clip_box, FALSE);
3005           break;
3006         }
3007     }
3008 #endif /* USE_BACKING_STORE */
3009 }
3010
3011 static void
3012 gdk_window_free_paint_stack (GdkWindow *window)
3013 {
3014   if (window->paint_stack)
3015     {
3016       GSList *tmp_list = window->paint_stack;
3017
3018       while (tmp_list)
3019         {
3020           GdkWindowPaint *paint = tmp_list->data;
3021
3022           if (tmp_list == window->paint_stack)
3023             cairo_surface_destroy (paint->surface);
3024
3025           cairo_region_destroy (paint->region);
3026           g_free (paint);
3027
3028           tmp_list = tmp_list->next;
3029         }
3030
3031       g_slist_free (window->paint_stack);
3032       window->paint_stack = NULL;
3033     }
3034 }
3035
3036 static void
3037 do_move_region_bits_on_impl (GdkWindow *impl_window,
3038                              cairo_region_t *dest_region, /* In impl window coords */
3039                              int dx, int dy)
3040 {
3041   GdkWindowImplClass *impl_class;
3042
3043   impl_class = GDK_WINDOW_IMPL_GET_CLASS (impl_window->impl);
3044
3045   impl_class->translate (impl_window, dest_region, dx, dy);
3046 }
3047
3048 static GdkWindowRegionMove *
3049 gdk_window_region_move_new (cairo_region_t *region,
3050                             int dx, int dy)
3051 {
3052   GdkWindowRegionMove *move;
3053
3054   move = g_slice_new (GdkWindowRegionMove);
3055   move->dest_region = cairo_region_copy (region);
3056   move->dx = dx;
3057   move->dy = dy;
3058
3059   return move;
3060 }
3061
3062 static void
3063 gdk_window_region_move_free (GdkWindowRegionMove *move)
3064 {
3065   cairo_region_destroy (move->dest_region);
3066   g_slice_free (GdkWindowRegionMove, move);
3067 }
3068
3069 static void
3070 append_move_region (GdkWindow *impl_window,
3071                     cairo_region_t *new_dest_region,
3072                     int dx, int dy)
3073 {
3074   GdkWindowRegionMove *move, *old_move;
3075   cairo_region_t *new_total_region, *old_total_region;
3076   cairo_region_t *source_overlaps_destination;
3077   cairo_region_t *non_overwritten;
3078   gboolean added_move;
3079   GList *l, *prev;
3080
3081   if (cairo_region_is_empty (new_dest_region))
3082     return;
3083
3084   /* In principle this could just append the move to the list of outstanding
3085      moves that will be replayed before drawing anything when we're handling
3086      exposes. However, we'd like to do a bit better since its commonly the case
3087      that we get multiple copies where A is copied to B and then B is copied
3088      to C, and we'd like to express this as a simple copy A to C operation. */
3089
3090   /* We approach this by taking the new move and pushing it ahead of moves
3091      starting at the end of the list and stopping when its not safe to do so.
3092      It's not safe to push past a move if either the source of the new move
3093      is in the destination of the old move, or if the destination of the new
3094      move is in the source of the new move, or if the destination of the new
3095      move overlaps the destination of the old move. We simplify this by
3096      just comparing the total regions (src + dest) */
3097   new_total_region = cairo_region_copy (new_dest_region);
3098   cairo_region_translate (new_total_region, -dx, -dy);
3099   cairo_region_union (new_total_region, new_dest_region);
3100
3101   added_move = FALSE;
3102   for (l = g_list_last (impl_window->outstanding_moves); l != NULL; l = prev)
3103     {
3104       prev = l->prev;
3105       old_move = l->data;
3106
3107       old_total_region = cairo_region_copy (old_move->dest_region);
3108       cairo_region_translate (old_total_region, -old_move->dx, -old_move->dy);
3109       cairo_region_union (old_total_region, old_move->dest_region);
3110
3111       cairo_region_intersect (old_total_region, new_total_region);
3112       /* If these regions intersect then its not safe to push the
3113          new region before the old one */
3114       if (!cairo_region_is_empty (old_total_region))
3115         {
3116           /* The area where the new moves source overlaps the old ones
3117              destination */
3118           source_overlaps_destination = cairo_region_copy (new_dest_region);
3119           cairo_region_translate (source_overlaps_destination, -dx, -dy);
3120           cairo_region_intersect (source_overlaps_destination, old_move->dest_region);
3121           cairo_region_translate (source_overlaps_destination, dx, dy);
3122
3123           /* We can do all sort of optimizations here, but to do things safely it becomes
3124              quite complicated. However, a very common case is that you copy something first,
3125              then copy all that or a subset of it to a new location (i.e. if you scroll twice
3126              in the same direction). We'd like to detect this case and optimize it to one
3127              copy. */
3128           if (cairo_region_equal (source_overlaps_destination, new_dest_region))
3129             {
3130               /* This means we might be able to replace the old move and the new one
3131                  with the new one read from the old ones source, and a second copy of
3132                  the non-overwritten parts of the old move. However, such a split
3133                  is only valid if the source in the old move isn't overwritten
3134                  by the destination of the new one */
3135
3136               /* the new destination of old move if split is ok: */
3137               non_overwritten = cairo_region_copy (old_move->dest_region);
3138               cairo_region_subtract (non_overwritten, new_dest_region);
3139               /* move to source region */
3140               cairo_region_translate (non_overwritten, -old_move->dx, -old_move->dy);
3141
3142               cairo_region_intersect (non_overwritten, new_dest_region);
3143               if (cairo_region_is_empty (non_overwritten))
3144                 {
3145                   added_move = TRUE;
3146                   move = gdk_window_region_move_new (new_dest_region,
3147                                                      dx + old_move->dx,
3148                                                      dy + old_move->dy);
3149
3150                   impl_window->outstanding_moves =
3151                     g_list_insert_before (impl_window->outstanding_moves,
3152                                           l, move);
3153                   cairo_region_subtract (old_move->dest_region, new_dest_region);
3154                 }
3155               cairo_region_destroy (non_overwritten);
3156             }
3157
3158           cairo_region_destroy (source_overlaps_destination);
3159           cairo_region_destroy (old_total_region);
3160           break;
3161         }
3162       cairo_region_destroy (old_total_region);
3163     }
3164
3165   cairo_region_destroy (new_total_region);
3166
3167   if (!added_move)
3168     {
3169       move = gdk_window_region_move_new (new_dest_region, dx, dy);
3170
3171       if (l == NULL)
3172         impl_window->outstanding_moves =
3173           g_list_prepend (impl_window->outstanding_moves,
3174                           move);
3175       else
3176         impl_window->outstanding_moves =
3177           g_list_insert_before (impl_window->outstanding_moves,
3178                                 l->next, move);
3179     }
3180 }
3181
3182 /* Moves bits and update area by dx/dy in impl window.
3183    Takes ownership of region to avoid copy (because we may change it) */
3184 static void
3185 move_region_on_impl (GdkWindow *impl_window,
3186                      cairo_region_t *region, /* In impl window coords */
3187                      int dx, int dy)
3188 {
3189   if ((dx == 0 && dy == 0) ||
3190       cairo_region_is_empty (region))
3191     {
3192       cairo_region_destroy (region);
3193       return;
3194     }
3195
3196   g_assert (impl_window == gdk_window_get_impl_window (impl_window));
3197
3198   /* Move any old invalid regions in the copy source area by dx/dy */
3199   if (impl_window->update_area)
3200     {
3201       cairo_region_t *update_area;
3202
3203       update_area = cairo_region_copy (region);
3204
3205       /* Convert from target to source */
3206       cairo_region_translate (update_area, -dx, -dy);
3207       cairo_region_intersect (update_area, impl_window->update_area);
3208       /* We only copy the area, so keep the old update area invalid.
3209          It would be safe to remove it too, as code that uses
3210          move_region_on_impl generally also invalidate the source
3211          area. However, it would just use waste cycles. */
3212
3213       /* Convert back */
3214       cairo_region_translate (update_area, dx, dy);
3215       cairo_region_union (impl_window->update_area, update_area);
3216
3217       /* This area of the destination is now invalid,
3218          so no need to copy to it.  */
3219       cairo_region_subtract (region, update_area);
3220
3221       cairo_region_destroy (update_area);
3222     }
3223
3224   /* If we're currently exposing this window, don't copy to this
3225      destination, as it will be overdrawn when the expose is done,
3226      instead invalidate it and repaint later. */
3227   if (impl_window->implicit_paint)
3228     {
3229       GdkWindowPaint *implicit_paint = impl_window->implicit_paint;
3230       cairo_region_t *exposing;
3231
3232       exposing = cairo_region_copy (implicit_paint->region);
3233       cairo_region_intersect (exposing, region);
3234       cairo_region_subtract (region, exposing);
3235
3236       impl_window_add_update_area (impl_window, exposing);
3237       cairo_region_destroy (exposing);
3238     }
3239
3240   append_move_region (impl_window, region, dx, dy);
3241
3242   cairo_region_destroy (region);
3243 }
3244
3245 /* Flushes all outstanding changes to the window, call this
3246  * before drawing directly to the window (i.e. outside a begin/end_paint pair).
3247  */
3248 static void
3249 gdk_window_flush_outstanding_moves (GdkWindow *window)
3250 {
3251   GdkWindow *impl_window;
3252   GList *l, *outstanding;
3253   GdkWindowRegionMove *move;
3254
3255   impl_window = gdk_window_get_impl_window (window);
3256   outstanding = impl_window->outstanding_moves;
3257   impl_window->outstanding_moves = NULL;
3258
3259   for (l = outstanding; l != NULL; l = l->next)
3260     {
3261       move = l->data;
3262
3263       do_move_region_bits_on_impl (impl_window,
3264                                    move->dest_region, move->dx, move->dy);
3265
3266       gdk_window_region_move_free (move);
3267     }
3268
3269   g_list_free (outstanding);
3270 }
3271
3272 /**
3273  * gdk_window_flush:
3274  * @window: a #GdkWindow
3275  *
3276  * Flush all outstanding cached operations on a window, leaving the
3277  * window in a state which reflects all that has been drawn before.
3278  *
3279  * Gdk uses multiple kinds of caching to get better performance and
3280  * nicer drawing. For instance, during exposes all paints to a window
3281  * using double buffered rendering are keep on a surface until the last
3282  * window has been exposed. It also delays window moves/scrolls until
3283  * as long as possible until next update to avoid tearing when moving
3284  * windows.
3285  *
3286  * Normally this should be completely invisible to applications, as
3287  * we automatically flush the windows when required, but this might
3288  * be needed if you for instance mix direct native drawing with
3289  * gdk drawing. For Gtk widgets that don't use double buffering this
3290  * will be called automatically before sending the expose event.
3291  *
3292  * Since: 2.18
3293  **/
3294 void
3295 gdk_window_flush (GdkWindow *window)
3296 {
3297   gdk_window_flush_outstanding_moves (window);
3298   gdk_window_flush_implicit_paint (window);
3299 }
3300
3301 /* If we're about to move/resize or otherwise change the
3302  * hierarchy of a client side window in an impl and we're
3303  * called from an expose event handler then we need to
3304  * flush any already painted parts of the implicit paint
3305  * that are not part of the current paint, as these may
3306  * be used when scrolling or may overdraw the changes
3307  * caused by the hierarchy change.
3308  */
3309 static void
3310 gdk_window_flush_if_exposing (GdkWindow *window)
3311 {
3312   GdkWindow *impl_window;
3313
3314   impl_window = gdk_window_get_impl_window (window);
3315
3316   /* If we're in an implicit paint (i.e. in an expose handler, flush
3317      all the already finished exposes to get things to an uptodate state. */
3318   if (impl_window->implicit_paint)
3319     gdk_window_flush (window);
3320 }
3321
3322
3323 static void
3324 gdk_window_flush_recursive_helper (GdkWindow *window,
3325                                    GdkWindowImpl *impl)
3326 {
3327   GdkWindow *child;
3328   GList *l;
3329
3330   for (l = window->children; l != NULL; l = l->next)
3331     {
3332       child = l->data;
3333
3334       if (child->impl == impl)
3335         /* Same impl, ignore */
3336         gdk_window_flush_recursive_helper (child, impl);
3337       else
3338         gdk_window_flush_recursive (child);
3339     }
3340 }
3341
3342 static void
3343 gdk_window_flush_recursive (GdkWindow *window)
3344 {
3345   gdk_window_flush (window);
3346   gdk_window_flush_recursive_helper (window, window->impl);
3347 }
3348
3349 /**
3350  * gdk_window_get_clip_region:
3351  * @window: a #GdkWindow
3352  * 
3353  * Computes the region of a window that potentially can be written
3354  * to by drawing primitives. This region may not take into account
3355  * other factors such as if the window is obscured by other windows,
3356  * but no area outside of this region will be affected by drawing
3357  * primitives.
3358  * 
3359  * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
3360  *          when you are done.
3361  **/
3362 cairo_region_t*
3363 gdk_window_get_clip_region (GdkWindow *window)
3364 {
3365   cairo_region_t *result;
3366
3367   g_return_val_if_fail (GDK_WINDOW (window), NULL);
3368
3369   result = cairo_region_copy (window->clip_region);
3370
3371   if (window->paint_stack)
3372     {
3373       cairo_region_t *paint_region = cairo_region_create ();
3374       GSList *tmp_list = window->paint_stack;
3375
3376       while (tmp_list)
3377         {
3378           GdkWindowPaint *paint = tmp_list->data;
3379
3380           cairo_region_union (paint_region, paint->region);
3381
3382           tmp_list = tmp_list->next;
3383         }
3384
3385       cairo_region_intersect (result, paint_region);
3386       cairo_region_destroy (paint_region);
3387     }
3388
3389   return result;
3390 }
3391
3392 /**
3393  * gdk_window_get_visible_region:
3394  * @window: a #GdkWindow
3395  * 
3396  * Computes the region of the @window that is potentially visible.
3397  * This does not necessarily take into account if the window is
3398  * obscured by other windows, but no area outside of this region
3399  * is visible.
3400  * 
3401  * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
3402  *          when you are done.
3403  **/
3404 cairo_region_t *
3405 gdk_window_get_visible_region (GdkWindow *window)
3406 {
3407   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3408
3409   return cairo_region_copy (window->clip_region);
3410 }
3411
3412 static cairo_t *
3413 setup_backing_rect (GdkWindow *window, GdkWindowPaint *paint, int x_offset_cairo, int y_offset_cairo)
3414 {
3415   GdkWindow *bg_window;
3416   cairo_pattern_t *pattern = NULL;
3417   int x_offset = 0, y_offset = 0;
3418   cairo_t *cr;
3419
3420   cr = cairo_create (paint->surface);
3421
3422   for (bg_window = window; bg_window; bg_window = bg_window->parent)
3423     {
3424       pattern = gdk_window_get_background_pattern (bg_window);
3425       if (pattern)
3426         break;
3427
3428       x_offset += bg_window->x;
3429       y_offset += bg_window->y;
3430     }
3431
3432   if (pattern)
3433     {
3434       cairo_translate (cr, -x_offset, -y_offset);
3435       cairo_set_source (cr, pattern);
3436       cairo_translate (cr, x_offset, y_offset);
3437     }
3438   else
3439     cairo_set_source_rgb (cr, 0, 0, 0);
3440
3441   return cr;
3442 }
3443
3444 static void
3445 gdk_window_clear_backing_region (GdkWindow *window,
3446                                  cairo_region_t *region)
3447 {
3448   GdkWindowPaint *paint = window->paint_stack->data;
3449   cairo_region_t *clip;
3450   GdkRectangle clipbox;
3451   cairo_t *cr;
3452
3453   if (GDK_WINDOW_DESTROYED (window))
3454     return;
3455
3456   cr = setup_backing_rect (window, paint, 0, 0);
3457
3458   clip = cairo_region_copy (paint->region);
3459   cairo_region_intersect (clip, region);
3460   cairo_region_get_extents (clip, &clipbox);
3461
3462   gdk_cairo_region (cr, clip);
3463   cairo_fill (cr);
3464
3465   cairo_destroy (cr);
3466
3467   cairo_region_destroy (clip);
3468 }
3469
3470 static void
3471 gdk_window_clear_backing_region_direct (GdkWindow *window,
3472                                         cairo_region_t *region)
3473 {
3474   GdkWindowPaint paint;
3475   cairo_region_t *clip;
3476   GdkRectangle clipbox;
3477   cairo_t *cr;
3478
3479   if (GDK_WINDOW_DESTROYED (window))
3480     return;
3481
3482   paint.surface = _gdk_window_ref_cairo_surface (window);
3483
3484   cr = setup_backing_rect (window, &paint, 0, 0);
3485
3486   clip = cairo_region_copy (window->clip_region_with_children);
3487   cairo_region_intersect (clip, region);
3488   cairo_region_get_extents (clip, &clipbox);
3489
3490   gdk_cairo_region (cr, clip);
3491   cairo_fill (cr);
3492
3493   cairo_destroy (cr);
3494
3495   cairo_region_destroy (clip);
3496   cairo_surface_destroy (paint.surface);
3497 }
3498
3499
3500 static void
3501 gdk_window_clear_region_internal (GdkWindow *window,
3502                                   cairo_region_t *region)
3503 {
3504   if (window->paint_stack)
3505     gdk_window_clear_backing_region (window, region);
3506   else
3507     gdk_window_clear_backing_region_direct (window, region);
3508 }
3509
3510 static void
3511 gdk_window_drop_cairo_surface (GdkWindow *window)
3512 {
3513   if (window->cairo_surface)
3514     {
3515       cairo_surface_finish (window->cairo_surface);
3516       cairo_surface_set_user_data (window->cairo_surface, &gdk_window_cairo_key,
3517                                    NULL, NULL);
3518       window->cairo_surface = NULL;
3519     }
3520 }
3521
3522 static void
3523 gdk_window_cairo_surface_destroy (void *data)
3524 {
3525   GdkWindow *window = data;
3526
3527   window->cairo_surface = NULL;
3528 }
3529
3530 static cairo_surface_t *
3531 gdk_window_create_cairo_surface (GdkWindow *window,
3532                                  int width,
3533                                  int height)
3534 {
3535   cairo_surface_t *surface, *subsurface;
3536   
3537   surface = gdk_window_ref_impl_surface (window);
3538   if (gdk_window_has_impl (window))
3539     return surface;
3540
3541   subsurface = cairo_surface_create_for_rectangle (surface,
3542                                                    window->abs_x,
3543                                                    window->abs_y,
3544                                                    width,
3545                                                    height);
3546   cairo_surface_destroy (surface);
3547   return subsurface;
3548 }
3549
3550
3551 cairo_surface_t *
3552 _gdk_window_ref_cairo_surface (GdkWindow *window)
3553 {
3554   cairo_surface_t *surface;
3555
3556   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3557
3558   if (window->paint_stack)
3559     {
3560       GdkWindowPaint *paint = window->paint_stack->data;
3561
3562       surface = paint->surface;
3563       cairo_surface_reference (surface);
3564     }
3565   else
3566     {
3567
3568       /* This will be drawing directly to the window, so flush implicit paint */
3569       gdk_window_flush (window);
3570
3571       if (!window->cairo_surface)
3572         {
3573           window->cairo_surface = gdk_window_create_cairo_surface (window,
3574                                                                    window->width,
3575                                                                    window->height);
3576
3577           if (window->cairo_surface)
3578             {
3579               cairo_surface_set_user_data (window->cairo_surface, &gdk_window_cairo_key,
3580                                            window, gdk_window_cairo_surface_destroy);
3581             }
3582         }
3583       else
3584         cairo_surface_reference (window->cairo_surface);
3585
3586       surface = window->cairo_surface;
3587     }
3588
3589   return surface;
3590 }
3591
3592 /**
3593  * gdk_cairo_create:
3594  * @window: a #GdkWindow
3595  * 
3596  * Creates a Cairo context for drawing to @window.
3597  *
3598  * <note><warning>
3599  * Note that calling cairo_reset_clip() on the resulting #cairo_t will
3600  * produce undefined results, so avoid it at all costs.
3601  * </warning></note>
3602  *
3603  * Return value: A newly created Cairo context. Free with
3604  *  cairo_destroy() when you are done drawing.
3605  * 
3606  * Since: 2.8
3607  **/
3608 cairo_t *
3609 gdk_cairo_create (GdkWindow *window)
3610 {
3611   cairo_surface_t *surface;
3612   cairo_t *cr;
3613     
3614   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3615
3616   surface = _gdk_window_ref_cairo_surface (window);
3617   cr = cairo_create (surface);
3618
3619   if (!window->paint_stack)
3620     {
3621       gdk_cairo_region (cr, window->clip_region_with_children);
3622       cairo_clip (cr);
3623     }
3624   else
3625     {
3626       GdkWindowPaint *paint = window->paint_stack->data;
3627
3628       /* Only needs to clip to region if piggybacking
3629          on an implicit paint */
3630       if (paint->uses_implicit)
3631         {
3632           gdk_cairo_region (cr, paint->region);
3633           cairo_clip (cr);
3634         }
3635     }
3636     
3637   cairo_surface_destroy (surface);
3638
3639   return cr;
3640 }
3641
3642 /* Code for dirty-region queueing
3643  */
3644 static GSList *update_windows = NULL;
3645 static guint update_idle = 0;
3646 static gboolean debug_updates = FALSE;
3647
3648 static inline gboolean
3649 gdk_window_is_ancestor (GdkWindow *window,
3650                         GdkWindow *ancestor)
3651 {
3652   while (window)
3653     {
3654       GdkWindow *parent = window->parent;
3655
3656       if (parent == ancestor)
3657         return TRUE;
3658
3659       window = parent;
3660     }
3661
3662   return FALSE;
3663 }
3664
3665 static void
3666 gdk_window_add_update_window (GdkWindow *window)
3667 {
3668   GSList *tmp;
3669   GSList *prev = NULL;
3670   gboolean has_ancestor_in_list = FALSE;
3671
3672   for (tmp = update_windows; tmp; tmp = tmp->next)
3673     {
3674       GdkWindow *parent = window->parent;
3675
3676       /*  check if tmp is an ancestor of "window"; if it is, set a
3677        *  flag indicating that all following windows are either
3678        *  children of "window" or from a differen hierarchy
3679        */
3680       if (!has_ancestor_in_list && gdk_window_is_ancestor (window, tmp->data))
3681         has_ancestor_in_list = TRUE;
3682
3683       /* insert in reverse stacking order when adding around siblings,
3684        * so processing updates properly paints over lower stacked windows
3685        */
3686       if (parent == GDK_WINDOW (tmp->data)->parent)
3687         {
3688           gint index = g_list_index (parent->children, window);
3689           for (; tmp && parent == GDK_WINDOW (tmp->data)->parent; tmp = tmp->next)
3690             {
3691               gint sibling_index = g_list_index (parent->children, tmp->data);
3692               if (index > sibling_index)
3693                 break;
3694               prev = tmp;
3695             }
3696           /* here, tmp got advanced past all lower stacked siblings */
3697           tmp = g_slist_prepend (tmp, window);
3698           if (prev)
3699             prev->next = tmp;
3700           else
3701             update_windows = tmp;
3702           return;
3703         }
3704
3705       /*  if "window" has an ancestor in the list and tmp is one of
3706        *  "window's" children, insert "window" before tmp
3707        */
3708       if (has_ancestor_in_list && gdk_window_is_ancestor (tmp->data, window))
3709         {
3710           tmp = g_slist_prepend (tmp, window);
3711
3712           if (prev)
3713             prev->next = tmp;
3714           else
3715             update_windows = tmp;
3716           return;
3717         }
3718
3719       /*  if we're at the end of the list and had an ancestor it it,
3720        *  append to the list
3721        */
3722       if (! tmp->next && has_ancestor_in_list)
3723         {
3724           tmp = g_slist_append (tmp, window);
3725           return;
3726         }
3727
3728       prev = tmp;
3729     }
3730
3731   /*  if all above checks failed ("window" is from a different
3732    *  hierarchy than what is already in the list) or the list is
3733    *  empty, prepend
3734    */
3735   update_windows = g_slist_prepend (update_windows, window);
3736 }
3737
3738 static void
3739 gdk_window_remove_update_window (GdkWindow *window)
3740 {
3741   update_windows = g_slist_remove (update_windows, window);
3742 }
3743
3744 static gboolean
3745 gdk_window_update_idle (gpointer data)
3746 {
3747   gdk_window_process_all_updates ();
3748
3749   return FALSE;
3750 }
3751
3752 static gboolean
3753 gdk_window_is_toplevel_frozen (GdkWindow *window)
3754 {
3755   GdkWindow *toplevel;
3756
3757   toplevel = gdk_window_get_toplevel (window);
3758
3759   return toplevel->update_and_descendants_freeze_count > 0;
3760 }
3761
3762 static void
3763 gdk_window_schedule_update (GdkWindow *window)
3764 {
3765   if (window &&
3766       (window->update_freeze_count ||
3767        gdk_window_is_toplevel_frozen (window)))
3768     return;
3769
3770   if (!update_idle)
3771     update_idle =
3772       gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
3773                                  gdk_window_update_idle,
3774                                  NULL, NULL);
3775 }
3776
3777 void
3778 _gdk_window_process_updates_recurse (GdkWindow *window,
3779                                      cairo_region_t *expose_region)
3780 {
3781   GdkWindow *child;
3782   cairo_region_t *child_region;
3783   GdkRectangle r;
3784   GList *l, *children;
3785
3786   if (cairo_region_is_empty (expose_region))
3787     return;
3788
3789   if (gdk_window_is_offscreen (window->impl_window) &&
3790       window == window->impl_window)
3791     _gdk_window_add_damage ((GdkWindow *) window->impl_window, expose_region);
3792
3793   /* Make this reentrancy safe for expose handlers freeing windows */
3794   children = g_list_copy (window->children);
3795   g_list_foreach (children, (GFunc)g_object_ref, NULL);
3796
3797   /* Iterate over children, starting at topmost */
3798   for (l = children; l != NULL; l = l->next)
3799     {
3800       child = l->data;
3801
3802       if (child->destroyed || !GDK_WINDOW_IS_MAPPED (child) || child->input_only || child->composited)
3803         continue;
3804
3805       /* Ignore offscreen children, as they don't draw in their parent and
3806        * don't take part in the clipping */
3807       if (gdk_window_is_offscreen (child))
3808         continue;
3809
3810       r.x = child->x;
3811       r.y = child->y;
3812       r.width = child->width;
3813       r.height = child->height;
3814
3815       child_region = cairo_region_create_rectangle (&r);
3816       if (child->shape)
3817         {
3818           /* Adjust shape region to parent window coords */
3819           cairo_region_translate (child->shape, child->x, child->y);
3820           cairo_region_intersect (child_region, child->shape);
3821           cairo_region_translate (child->shape, -child->x, -child->y);
3822         }
3823
3824       if (child->impl == window->impl)
3825         {
3826           /* Client side child, expose */
3827           cairo_region_intersect (child_region, expose_region);
3828           cairo_region_subtract (expose_region, child_region);
3829           cairo_region_translate (child_region, -child->x, -child->y);
3830           _gdk_window_process_updates_recurse ((GdkWindow *)child, child_region);
3831         }
3832       else
3833         {
3834           /* Native child, just remove area from expose region */
3835           cairo_region_subtract (expose_region, child_region);
3836         }
3837       cairo_region_destroy (child_region);
3838     }
3839
3840   g_list_foreach (children, (GFunc)g_object_unref, NULL);
3841   g_list_free (children);
3842
3843   if (!cairo_region_is_empty (expose_region) &&
3844       !window->destroyed)
3845     {
3846       if (window->event_mask & GDK_EXPOSURE_MASK)
3847         {
3848           GdkEvent event;
3849
3850           event.expose.type = GDK_EXPOSE;
3851           event.expose.window = g_object_ref (window);
3852           event.expose.send_event = FALSE;
3853           event.expose.count = 0;
3854           event.expose.region = expose_region;
3855           cairo_region_get_extents (expose_region, &event.expose.area);
3856
3857           _gdk_event_emit (&event);
3858
3859           g_object_unref (window);
3860         }
3861       else if (window->window_type != GDK_WINDOW_FOREIGN)
3862         {
3863           /* No exposure mask set, so nothing will be drawn, the
3864            * app relies on the background being what it specified
3865            * for the window. So, we need to clear this manually.
3866            *
3867            * For foreign windows if expose is not set that generally
3868            * means some other client paints them, so don't clear
3869            * there.
3870            *
3871            * We use begin/end_paint around the clear so that we can
3872            * piggyback on the implicit paint */
3873
3874           gdk_window_begin_paint_region (window, expose_region);
3875           gdk_window_clear_region_internal (window, expose_region);
3876           gdk_window_end_paint (window);
3877         }
3878     }
3879 }
3880
3881 /* Process and remove any invalid area on the native window by creating
3882  * expose events for the window and all non-native descendants.
3883  * Also processes any outstanding moves on the window before doing
3884  * any drawing. Note that its possible to have outstanding moves without
3885  * any invalid area as we use the update idle mechanism to coalesce
3886  * multiple moves as well as multiple invalidations.
3887  */
3888 static void
3889 gdk_window_process_updates_internal (GdkWindow *window)
3890 {
3891   GdkWindowImplClass *impl_class;
3892   gboolean save_region = FALSE;
3893   GdkRectangle clip_box;
3894
3895   /* Ensure the window lives while updating it */
3896   g_object_ref (window);
3897
3898   /* If an update got queued during update processing, we can get a
3899    * window in the update queue that has an empty update_area.
3900    * just ignore it.
3901    */
3902   if (window->update_area)
3903     {
3904       cairo_region_t *update_area = window->update_area;
3905       window->update_area = NULL;
3906
3907       if (gdk_window_is_viewable (window))
3908         {
3909           cairo_region_t *expose_region;
3910           gboolean end_implicit;
3911
3912           /* Clip to part visible in toplevel */
3913           cairo_region_intersect (update_area, window->clip_region);
3914
3915           if (debug_updates)
3916             {
3917               /* Make sure we see the red invalid area before redrawing. */
3918               gdk_display_sync (gdk_window_get_display (window));
3919               g_usleep (70000);
3920             }
3921
3922           /* At this point we will be completely redrawing all of update_area.
3923            * If we have any outstanding moves that end up moving stuff inside
3924            * this area we don't actually need to move that as that part would
3925            * be overdrawn by the expose anyway. So, in order to copy less data
3926            * we remove these areas from the outstanding moves.
3927            */
3928           if (window->outstanding_moves)
3929             {
3930               GdkWindowRegionMove *move;
3931               cairo_region_t *remove;
3932               GList *l, *prev;
3933
3934               remove = cairo_region_copy (update_area);
3935               /* We iterate backwards, starting from the state that would be
3936                  if we had applied all the moves. */
3937               for (l = g_list_last (window->outstanding_moves); l != NULL; l = prev)
3938                 {
3939                   prev = l->prev;
3940                   move = l->data;
3941
3942                   /* Don't need this area */
3943                   cairo_region_subtract (move->dest_region, remove);
3944
3945                   /* However if any of the destination we do need has a source
3946                      in the updated region we do need that as a destination for
3947                      the earlier moves */
3948                   cairo_region_translate (move->dest_region, -move->dx, -move->dy);
3949                   cairo_region_subtract (remove, move->dest_region);
3950
3951                   if (cairo_region_is_empty (move->dest_region))
3952                     {
3953                       gdk_window_region_move_free (move);
3954                       window->outstanding_moves =
3955                         g_list_delete_link (window->outstanding_moves, l);
3956                     }
3957                   else /* move back */
3958                     cairo_region_translate (move->dest_region, move->dx, move->dy);
3959                 }
3960               cairo_region_destroy (remove);
3961             }
3962
3963           /* By now we a set of window moves that should be applied, and then
3964            * an update region that should be repainted. A trivial implementation
3965            * would just do that in order, however in order to get nicer drawing
3966            * we do some tricks:
3967            *
3968            * First of all, each subwindow expose may be double buffered by
3969            * itself (depending on widget setting) via
3970            * gdk_window_begin/end_paint(). But we also do an "implicit" paint,
3971            * creating a single surface the size of the invalid area on the
3972            * native window which all the individual normal paints will draw
3973            * into. This way in the normal case there will be only one surface
3974            * allocated and only once surface draw done for all the windows
3975            * in this native window.
3976            * There are a couple of reasons this may fail, for instance, some
3977            * backends (like quartz) do its own double buffering, so we disable
3978            * gdk double buffering there. Secondly, some subwindow could be
3979            * non-double buffered and draw directly to the window outside a
3980            * begin/end_paint pair. That will be lead to a gdk_window_flush
3981            * which immediately executes all outstanding moves and paints+removes
3982            * the implicit paint (further paints will allocate their own surfaces).
3983            *
3984            * Secondly, in the case of implicit double buffering we expose all
3985            * the child windows into the implicit surface before we execute
3986            * the outstanding moves. This way we minimize the time between
3987            * doing the moves and rendering the new update area, thus minimizing
3988            * flashing. Of course, if any subwindow is non-double buffered we
3989            * well flush earlier than that.
3990            *
3991            * Thirdly, after having done the outstanding moves we queue an
3992            * "antiexpose" on the area that will be drawn by the expose, which
3993            * means that any invalid region on the native window side before
3994            * the first expose drawing operation will be discarded, as it
3995            * has by then been overdrawn with valid data. This means we can
3996            * avoid doing the unnecessary repaint any outstanding expose events.
3997            */
3998
3999           cairo_region_get_extents (update_area, &clip_box);
4000           end_implicit = gdk_window_begin_implicit_paint (window, &clip_box);
4001           expose_region = cairo_region_copy (update_area);
4002           impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
4003           if (!end_implicit)
4004             {
4005               /* Rendering is not double buffered by gdk, do outstanding
4006                * moves and queue antiexposure immediately. No need to do
4007                * any tricks */
4008               gdk_window_flush_outstanding_moves (window);
4009               save_region = impl_class->queue_antiexpose (window, update_area);
4010             }
4011           /* Render the invalid areas to the implicit paint, by sending exposes.
4012            * May flush if non-double buffered widget draw. */
4013           impl_class->process_updates_recurse (window, expose_region);
4014
4015           if (end_implicit)
4016             {
4017               /* Do moves right before exposes are rendered to the window */
4018               gdk_window_flush_outstanding_moves (window);
4019
4020               /* By this time we know that any outstanding expose for this
4021                * area is invalid and we can avoid it, so queue an antiexpose.
4022                * we have already started drawing to the window, so it would
4023                * be to late to anti-expose now. Since this is merely an
4024                * optimization we just avoid doing it at all in that case.
4025                */
4026               if (window->implicit_paint != NULL && !window->implicit_paint->flushed)
4027                 save_region = impl_class->queue_antiexpose (window, update_area);
4028
4029               gdk_window_end_implicit_paint (window);
4030             }
4031           cairo_region_destroy (expose_region);
4032         }
4033       if (!save_region)
4034         cairo_region_destroy (update_area);
4035     }
4036
4037   if (window->outstanding_moves)
4038     {
4039       /* Flush any outstanding moves, may happen if we moved a window but got
4040          no actual invalid area */
4041       gdk_window_flush_outstanding_moves (window);
4042     }
4043
4044   g_object_unref (window);
4045 }
4046
4047 static void
4048 flush_all_displays (void)
4049 {
4050   GSList *displays, *l;
4051
4052   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4053   for (l = displays; l; l = l->next)
4054     gdk_display_flush (l->data);
4055
4056   g_slist_free (displays);
4057 }
4058
4059 static void
4060 before_process_all_updates (void)
4061 {
4062   GSList *displays, *l;
4063   GdkDisplayClass *display_class;
4064
4065   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4066   display_class = GDK_DISPLAY_GET_CLASS (displays->data);
4067   for (l = displays; l; l = l->next)
4068     display_class->before_process_all_updates (l->data);
4069
4070   g_slist_free (displays);
4071 }
4072
4073 static void
4074 after_process_all_updates (void)
4075 {
4076   GSList *displays, *l;
4077   GdkDisplayClass *display_class;
4078
4079   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4080   display_class = GDK_DISPLAY_GET_CLASS (displays->data);
4081   for (l = displays; l; l = l->next)
4082     display_class->after_process_all_updates (l->data);
4083
4084   g_slist_free (displays);
4085 }
4086
4087 /* Currently it is not possible to override
4088  * gdk_window_process_all_updates in the same manner as
4089  * gdk_window_process_updates and gdk_window_invalidate_maybe_recurse
4090  * by implementing the GdkPaintable interface.  If in the future a
4091  * backend would need this, the right solution would be to add a
4092  * method to GdkDisplay that can be optionally
4093  * NULL. gdk_window_process_all_updates can then walk the list of open
4094  * displays and call the mehod.
4095  */
4096
4097 /**
4098  * gdk_window_process_all_updates:
4099  *
4100  * Calls gdk_window_process_updates() for all windows (see #GdkWindow)
4101  * in the application.
4102  *
4103  **/
4104 void
4105 gdk_window_process_all_updates (void)
4106 {
4107   GSList *old_update_windows = update_windows;
4108   GSList *tmp_list = update_windows;
4109   static gboolean in_process_all_updates = FALSE;
4110   static gboolean got_recursive_update = FALSE;
4111
4112   if (in_process_all_updates)
4113     {
4114       /* We can't do this now since that would recurse, so
4115          delay it until after the recursion is done. */
4116       got_recursive_update = TRUE;
4117       update_idle = 0;
4118       return;
4119     }
4120
4121   in_process_all_updates = TRUE;
4122   got_recursive_update = FALSE;
4123
4124   if (update_idle)
4125     g_source_remove (update_idle);
4126
4127   update_windows = NULL;
4128   update_idle = 0;
4129
4130   before_process_all_updates ();
4131
4132   g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
4133
4134   while (tmp_list)
4135     {
4136       GdkWindow *window = tmp_list->data;
4137
4138       if (!GDK_WINDOW_DESTROYED (window))
4139         {
4140           if (window->update_freeze_count ||
4141               gdk_window_is_toplevel_frozen (window))
4142             gdk_window_add_update_window (window);
4143           else
4144             gdk_window_process_updates_internal (window);
4145         }
4146
4147       g_object_unref (window);
4148       tmp_list = tmp_list->next;
4149     }
4150
4151   g_slist_free (old_update_windows);
4152
4153   flush_all_displays ();
4154
4155   after_process_all_updates ();
4156
4157   in_process_all_updates = FALSE;
4158
4159   /* If we ignored a recursive call, schedule a
4160      redraw now so that it eventually happens,
4161      otherwise we could miss an update if nothing
4162      else schedules an update. */
4163   if (got_recursive_update && !update_idle)
4164     update_idle =
4165       gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
4166                                  gdk_window_update_idle,
4167                                  NULL, NULL);
4168 }
4169
4170 /**
4171  * gdk_window_process_updates:
4172  * @window: a #GdkWindow
4173  * @update_children: whether to also process updates for child windows
4174  *
4175  * Sends one or more expose events to @window. The areas in each
4176  * expose event will cover the entire update area for the window (see
4177  * gdk_window_invalidate_region() for details). Normally GDK calls
4178  * gdk_window_process_all_updates() on your behalf, so there's no
4179  * need to call this function unless you want to force expose events
4180  * to be delivered immediately and synchronously (vs. the usual
4181  * case, where GDK delivers them in an idle handler). Occasionally
4182  * this is useful to produce nicer scrolling behavior, for example.
4183  *
4184  **/
4185 void
4186 gdk_window_process_updates (GdkWindow *window,
4187                             gboolean   update_children)
4188 {
4189   GdkWindow *impl_window;
4190
4191   g_return_if_fail (GDK_IS_WINDOW (window));
4192
4193   if (GDK_WINDOW_DESTROYED (window))
4194     return;
4195
4196   /* Make sure the window lives during the expose callouts */
4197   g_object_ref (window);
4198
4199   impl_window = gdk_window_get_impl_window (window);
4200   if ((impl_window->update_area ||
4201        impl_window->outstanding_moves) &&
4202       !impl_window->update_freeze_count &&
4203       !gdk_window_is_toplevel_frozen (window) &&
4204
4205       /* Don't recurse into process_updates_internal, we'll
4206        * do the update later when idle instead. */
4207       impl_window->implicit_paint == NULL)
4208     {
4209       gdk_window_process_updates_internal ((GdkWindow *)impl_window);
4210       gdk_window_remove_update_window ((GdkWindow *)impl_window);
4211     }
4212
4213   if (update_children)
4214     {
4215       /* process updates in reverse stacking order so composition or
4216        * painting over achieves the desired effect for offscreen windows
4217        */
4218       GList *node, *children;
4219
4220       children = g_list_copy (window->children);
4221       g_list_foreach (children, (GFunc)g_object_ref, NULL);
4222
4223       for (node = g_list_last (children); node; node = node->prev)
4224         {
4225           gdk_window_process_updates (node->data, TRUE);
4226           g_object_unref (node->data);
4227         }
4228
4229       g_list_free (children);
4230     }
4231
4232   g_object_unref (window);
4233 }
4234
4235 static void
4236 gdk_window_invalidate_rect_full (GdkWindow          *window,
4237                                   const GdkRectangle *rect,
4238                                   gboolean            invalidate_children,
4239                                   ClearBg             clear_bg)
4240 {
4241   GdkRectangle window_rect;
4242   cairo_region_t *region;
4243
4244   g_return_if_fail (GDK_IS_WINDOW (window));
4245
4246   if (GDK_WINDOW_DESTROYED (window))
4247     return;
4248
4249   if (window->input_only || !window->viewable)
4250     return;
4251
4252   if (!rect)
4253     {
4254       window_rect.x = 0;
4255       window_rect.y = 0;
4256       window_rect.width = window->width;
4257       window_rect.height = window->height;
4258       rect = &window_rect;
4259     }
4260
4261   region = cairo_region_create_rectangle (rect);
4262   gdk_window_invalidate_region_full (window, region, invalidate_children, clear_bg);
4263   cairo_region_destroy (region);
4264 }
4265
4266 /**
4267  * gdk_window_invalidate_rect:
4268  * @window: a #GdkWindow
4269  * @rect: (allow-none): rectangle to invalidate or %NULL to invalidate the whole
4270  *      window
4271  * @invalidate_children: whether to also invalidate child windows
4272  *
4273  * A convenience wrapper around gdk_window_invalidate_region() which
4274  * invalidates a rectangular region. See
4275  * gdk_window_invalidate_region() for details.
4276  **/
4277 void
4278 gdk_window_invalidate_rect (GdkWindow          *window,
4279                             const GdkRectangle *rect,
4280                             gboolean            invalidate_children)
4281 {
4282   gdk_window_invalidate_rect_full (window, rect, invalidate_children, CLEAR_BG_NONE);
4283 }
4284
4285 static void
4286 draw_ugly_color (GdkWindow       *window,
4287                  const cairo_region_t *region)
4288 {
4289   cairo_t *cr;
4290
4291   cr = gdk_cairo_create (window);
4292   /* Draw ugly color all over the newly-invalid region */
4293   cairo_set_source_rgb (cr, 50000/65535., 10000/65535., 10000/65535.);
4294   gdk_cairo_region (cr, region);
4295   cairo_fill (cr);
4296
4297   cairo_destroy (cr);
4298 }
4299
4300 static void
4301 impl_window_add_update_area (GdkWindow *impl_window,
4302                              cairo_region_t *region)
4303 {
4304   if (impl_window->update_area)
4305     cairo_region_union (impl_window->update_area, region);
4306   else
4307     {
4308       gdk_window_add_update_window (impl_window);
4309       impl_window->update_area = cairo_region_copy (region);
4310       gdk_window_schedule_update (impl_window);
4311     }
4312 }
4313
4314 /* clear_bg controls if the region will be cleared to
4315  * the background pattern if the exposure mask is not
4316  * set for the window, whereas this might not otherwise be
4317  * done (unless necessary to emulate background settings).
4318  * Set this to CLEAR_BG_WINCLEARED or CLEAR_BG_ALL if you
4319  * need to clear the background, such as when exposing the area beneath a
4320  * hidden or moved window, but not when an app requests repaint or when the
4321  * windowing system exposes a newly visible area (because then the windowing
4322  * system has already cleared the area).
4323  */
4324 static void
4325 gdk_window_invalidate_maybe_recurse_full (GdkWindow            *window,
4326                                           const cairo_region_t *region,
4327                                           ClearBg               clear_bg,
4328                                           GdkWindowChildFunc    child_func,
4329                                           gpointer              user_data)
4330 {
4331   GdkWindow *impl_window;
4332   cairo_region_t *visible_region;
4333   GList *tmp_list;
4334
4335   g_return_if_fail (GDK_IS_WINDOW (window));
4336
4337   if (GDK_WINDOW_DESTROYED (window))
4338     return;
4339
4340   if (window->input_only ||
4341       !window->viewable ||
4342       cairo_region_is_empty (region) ||
4343       window->window_type == GDK_WINDOW_ROOT)
4344     return;
4345
4346   visible_region = gdk_window_get_visible_region (window);
4347   cairo_region_intersect (visible_region, region);
4348
4349   tmp_list = window->children;
4350   while (tmp_list)
4351     {
4352       GdkWindow *child = tmp_list->data;
4353
4354       if (!child->input_only)
4355         {
4356           cairo_region_t *child_region;
4357           GdkRectangle child_rect;
4358
4359           child_rect.x = child->x;
4360           child_rect.y = child->y;
4361           child_rect.width = child->width;
4362           child_rect.height = child->height;
4363           child_region = cairo_region_create_rectangle (&child_rect);
4364
4365           /* remove child area from the invalid area of the parent */
4366           if (GDK_WINDOW_IS_MAPPED (child) && !child->shaped &&
4367               !child->composited &&
4368               !gdk_window_is_offscreen (child))
4369             cairo_region_subtract (visible_region, child_region);
4370
4371           if (child_func && (*child_func) ((GdkWindow *)child, user_data))
4372             {
4373               cairo_region_t *tmp = cairo_region_copy (region);
4374
4375               cairo_region_translate (tmp, - child_rect.x, - child_rect.y);
4376               cairo_region_translate (child_region, - child_rect.x, - child_rect.y);
4377               cairo_region_intersect (child_region, tmp);
4378
4379               gdk_window_invalidate_maybe_recurse_full ((GdkWindow *)child,
4380                                                         child_region, clear_bg, child_func, user_data);
4381
4382               cairo_region_destroy (tmp);
4383             }
4384
4385           cairo_region_destroy (child_region);
4386         }
4387
4388       tmp_list = tmp_list->next;
4389     }
4390
4391   impl_window = gdk_window_get_impl_window (window);
4392
4393   if (!cairo_region_is_empty (visible_region)  ||
4394       /* Even if we're not exposing anything, make sure we process
4395          idles for windows with outstanding moves */
4396       (impl_window->outstanding_moves != NULL &&
4397        impl_window->update_area == NULL))
4398     {
4399       if (debug_updates)
4400         draw_ugly_color (window, region);
4401
4402       /* Convert to impl coords */
4403       cairo_region_translate (visible_region, window->abs_x, window->abs_y);
4404
4405       /* Only invalidate area if app requested expose events or if
4406          we need to clear the area (by request or to emulate background
4407          clearing for non-native windows or native windows with no support
4408          for window backgrounds */
4409       if (window->event_mask & GDK_EXPOSURE_MASK ||
4410           clear_bg == CLEAR_BG_ALL ||
4411           clear_bg == CLEAR_BG_WINCLEARED)
4412         impl_window_add_update_area (impl_window, visible_region);
4413     }
4414
4415   cairo_region_destroy (visible_region);
4416 }
4417
4418 /**
4419  * gdk_window_invalidate_maybe_recurse:
4420  * @window: a #GdkWindow
4421  * @region: a #cairo_region_t
4422  * @child_func: (scope call) (allow-none): function to use to decide if to
4423  *     recurse to a child, %NULL means never recurse.
4424  * @user_data: data passed to @child_func
4425  *
4426  * Adds @region to the update area for @window. The update area is the
4427  * region that needs to be redrawn, or "dirty region." The call
4428  * gdk_window_process_updates() sends one or more expose events to the
4429  * window, which together cover the entire update area. An
4430  * application would normally redraw the contents of @window in
4431  * response to those expose events.
4432  *
4433  * GDK will call gdk_window_process_all_updates() on your behalf
4434  * whenever your program returns to the main loop and becomes idle, so
4435  * normally there's no need to do that manually, you just need to
4436  * invalidate regions that you know should be redrawn.
4437  *
4438  * The @child_func parameter controls whether the region of
4439  * each child window that intersects @region will also be invalidated.
4440  * Only children for which @child_func returns TRUE will have the area
4441  * invalidated.
4442  **/
4443 void
4444 gdk_window_invalidate_maybe_recurse (GdkWindow            *window,
4445                                      const cairo_region_t *region,
4446                                      GdkWindowChildFunc    child_func,
4447                                      gpointer              user_data)
4448 {
4449   gdk_window_invalidate_maybe_recurse_full (window, region, CLEAR_BG_NONE,
4450                                             child_func, user_data);
4451 }
4452
4453 static gboolean
4454 true_predicate (GdkWindow *window,
4455                 gpointer   user_data)
4456 {
4457   return TRUE;
4458 }
4459
4460 static void
4461 gdk_window_invalidate_region_full (GdkWindow       *window,
4462                                     const cairo_region_t *region,
4463                                     gboolean         invalidate_children,
4464                                     ClearBg          clear_bg)
4465 {
4466   gdk_window_invalidate_maybe_recurse_full (window, region, clear_bg,
4467                                             invalidate_children ?
4468                                             true_predicate : (gboolean (*) (GdkWindow *, gpointer))NULL,
4469                                        NULL);
4470 }
4471
4472 /**
4473  * gdk_window_invalidate_region:
4474  * @window: a #GdkWindow
4475  * @region: a #cairo_region_t
4476  * @invalidate_children: %TRUE to also invalidate child windows
4477  *
4478  * Adds @region to the update area for @window. The update area is the
4479  * region that needs to be redrawn, or "dirty region." The call
4480  * gdk_window_process_updates() sends one or more expose events to the
4481  * window, which together cover the entire update area. An
4482  * application would normally redraw the contents of @window in
4483  * response to those expose events.
4484  *
4485  * GDK will call gdk_window_process_all_updates() on your behalf
4486  * whenever your program returns to the main loop and becomes idle, so
4487  * normally there's no need to do that manually, you just need to
4488  * invalidate regions that you know should be redrawn.
4489  *
4490  * The @invalidate_children parameter controls whether the region of
4491  * each child window that intersects @region will also be invalidated.
4492  * If %FALSE, then the update area for child windows will remain
4493  * unaffected. See gdk_window_invalidate_maybe_recurse if you need
4494  * fine grained control over which children are invalidated.
4495  **/
4496 void
4497 gdk_window_invalidate_region (GdkWindow       *window,
4498                               const cairo_region_t *region,
4499                               gboolean         invalidate_children)
4500 {
4501   gdk_window_invalidate_maybe_recurse (window, region,
4502                                        invalidate_children ?
4503                                          true_predicate : (gboolean (*) (GdkWindow *, gpointer))NULL,
4504                                        NULL);
4505 }
4506
4507 /**
4508  * _gdk_window_invalidate_for_expose:
4509  * @window: a #GdkWindow
4510  * @region: a #cairo_region_t
4511  *
4512  * Adds @region to the update area for @window. The update area is the
4513  * region that needs to be redrawn, or "dirty region." The call
4514  * gdk_window_process_updates() sends one or more expose events to the
4515  * window, which together cover the entire update area. An
4516  * application would normally redraw the contents of @window in
4517  * response to those expose events.
4518  *
4519  * GDK will call gdk_window_process_all_updates() on your behalf
4520  * whenever your program returns to the main loop and becomes idle, so
4521  * normally there's no need to do that manually, you just need to
4522  * invalidate regions that you know should be redrawn.
4523  *
4524  * This version of invalidation is used when you recieve expose events
4525  * from the native window system. It exposes the native window, plus
4526  * any non-native child windows (but not native child windows, as those would
4527  * have gotten their own expose events).
4528  **/
4529 void
4530 _gdk_window_invalidate_for_expose (GdkWindow       *window,
4531                                    cairo_region_t       *region)
4532 {
4533   GdkWindowRegionMove *move;
4534   cairo_region_t *move_region;
4535   GList *l;
4536
4537   /* Any invalidations comming from the windowing system will
4538      be in areas that may be moved by outstanding moves,
4539      so we need to modify the expose region correspondingly,
4540      otherwise we would expose in the wrong place, as the
4541      outstanding moves will be copied before we draw the
4542      exposes. */
4543   for (l = window->outstanding_moves; l != NULL; l = l->next)
4544     {
4545       move = l->data;
4546
4547       /* covert to move source region */
4548       move_region = cairo_region_copy (move->dest_region);
4549       cairo_region_translate (move_region, -move->dx, -move->dy);
4550
4551       /* Move area of region that intersects with move source
4552          by dx, dy of the move*/
4553       cairo_region_intersect (move_region, region);
4554       cairo_region_subtract (region, move_region);
4555       cairo_region_translate (move_region, move->dx, move->dy);
4556       cairo_region_union (region, move_region);
4557
4558       cairo_region_destroy (move_region);
4559     }
4560
4561   gdk_window_invalidate_maybe_recurse_full (window, region, CLEAR_BG_WINCLEARED,
4562                                             (gboolean (*) (GdkWindow *, gpointer))gdk_window_has_no_impl,
4563                                             NULL);
4564 }
4565
4566
4567 /**
4568  * gdk_window_get_update_area:
4569  * @window: a #GdkWindow
4570  *
4571  * Transfers ownership of the update area from @window to the caller
4572  * of the function. That is, after calling this function, @window will
4573  * no longer have an invalid/dirty region; the update area is removed
4574  * from @window and handed to you. If a window has no update area,
4575  * gdk_window_get_update_area() returns %NULL. You are responsible for
4576  * calling cairo_region_destroy() on the returned region if it's non-%NULL.
4577  *
4578  * Return value: the update area for @window
4579  **/
4580 cairo_region_t *
4581 gdk_window_get_update_area (GdkWindow *window)
4582 {
4583   GdkWindow *impl_window;
4584   cairo_region_t *tmp_region;
4585
4586   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
4587
4588   impl_window = gdk_window_get_impl_window (window);
4589
4590   if (impl_window->update_area)
4591     {
4592       tmp_region = cairo_region_copy (window->clip_region_with_children);
4593       /* Convert to impl coords */
4594       cairo_region_translate (tmp_region, window->abs_x, window->abs_y);
4595       cairo_region_intersect (tmp_region, impl_window->update_area);
4596
4597       if (cairo_region_is_empty (tmp_region))
4598         {
4599           cairo_region_destroy (tmp_region);
4600           return NULL;
4601         }
4602       else
4603         {
4604           cairo_region_subtract (impl_window->update_area, tmp_region);
4605
4606           if (cairo_region_is_empty (impl_window->update_area) &&
4607               impl_window->outstanding_moves == NULL)
4608             {
4609               cairo_region_destroy (impl_window->update_area);
4610               impl_window->update_area = NULL;
4611
4612               gdk_window_remove_update_window ((GdkWindow *)impl_window);
4613             }
4614
4615           /* Convert from impl coords */
4616           cairo_region_translate (tmp_region, -window->abs_x, -window->abs_y);
4617           return tmp_region;
4618
4619         }
4620     }
4621   else
4622     return NULL;
4623 }
4624
4625 /**
4626  * _gdk_window_clear_update_area:
4627  * @window: a #GdkWindow.
4628  *
4629  * Internal function to clear the update area for a window. This
4630  * is called when the window is hidden or destroyed.
4631  **/
4632 void
4633 _gdk_window_clear_update_area (GdkWindow *window)
4634 {
4635   g_return_if_fail (GDK_IS_WINDOW (window));
4636
4637   if (window->update_area)
4638     {
4639       gdk_window_remove_update_window (window);
4640
4641       cairo_region_destroy (window->update_area);
4642       window->update_area = NULL;
4643     }
4644 }
4645
4646 /**
4647  * gdk_window_freeze_updates:
4648  * @window: a #GdkWindow
4649  *
4650  * Temporarily freezes a window such that it won't receive expose
4651  * events.  The window will begin receiving expose events again when
4652  * gdk_window_thaw_updates() is called. If gdk_window_freeze_updates()
4653  * has been called more than once, gdk_window_thaw_updates() must be called
4654  * an equal number of times to begin processing exposes.
4655  **/
4656 void
4657 gdk_window_freeze_updates (GdkWindow *window)
4658 {
4659   GdkWindow *impl_window;
4660
4661   g_return_if_fail (GDK_IS_WINDOW (window));
4662
4663   impl_window = gdk_window_get_impl_window (window);
4664   impl_window->update_freeze_count++;
4665 }
4666
4667 /**
4668  * gdk_window_thaw_updates:
4669  * @window: a #GdkWindow
4670  *
4671  * Thaws a window frozen with gdk_window_freeze_updates().
4672  **/
4673 void
4674 gdk_window_thaw_updates (GdkWindow *window)
4675 {
4676   GdkWindow *impl_window;
4677
4678   g_return_if_fail (GDK_IS_WINDOW (window));
4679
4680   impl_window = gdk_window_get_impl_window (window);
4681
4682   g_return_if_fail (impl_window->update_freeze_count > 0);
4683
4684   if (--impl_window->update_freeze_count == 0)
4685     gdk_window_schedule_update (GDK_WINDOW (impl_window));
4686 }
4687
4688 /**
4689  * gdk_window_freeze_toplevel_updates_libgtk_only:
4690  * @window: a #GdkWindow
4691  *
4692  * Temporarily freezes a window and all its descendants such that it won't
4693  * receive expose events.  The window will begin receiving expose events
4694  * again when gdk_window_thaw_toplevel_updates_libgtk_only() is called. If
4695  * gdk_window_freeze_toplevel_updates_libgtk_only()
4696  * has been called more than once,
4697  * gdk_window_thaw_toplevel_updates_libgtk_only() must be called
4698  * an equal number of times to begin processing exposes.
4699  *
4700  * This function is not part of the GDK public API and is only
4701  * for use by GTK+.
4702  **/
4703 void
4704 gdk_window_freeze_toplevel_updates_libgtk_only (GdkWindow *window)
4705 {
4706   g_return_if_fail (GDK_IS_WINDOW (window));
4707   g_return_if_fail (window->window_type != GDK_WINDOW_CHILD);
4708
4709   window->update_and_descendants_freeze_count++;
4710 }
4711
4712 /**
4713  * gdk_window_thaw_toplevel_updates_libgtk_only:
4714  * @window: a #GdkWindow
4715  *
4716  * Thaws a window frozen with
4717  * gdk_window_freeze_toplevel_updates_libgtk_only().
4718  *
4719  * This function is not part of the GDK public API and is only
4720  * for use by GTK+.
4721  **/
4722 void
4723 gdk_window_thaw_toplevel_updates_libgtk_only (GdkWindow *window)
4724 {
4725   g_return_if_fail (GDK_IS_WINDOW (window));
4726   g_return_if_fail (window->window_type != GDK_WINDOW_CHILD);
4727   g_return_if_fail (window->update_and_descendants_freeze_count > 0);
4728
4729   window->update_and_descendants_freeze_count--;
4730
4731   gdk_window_schedule_update (window);
4732 }
4733
4734 /**
4735  * gdk_window_set_debug_updates:
4736  * @setting: %TRUE to turn on update debugging
4737  *
4738  * With update debugging enabled, calls to
4739  * gdk_window_invalidate_region() clear the invalidated region of the
4740  * screen to a noticeable color, and GDK pauses for a short time
4741  * before sending exposes to windows during
4742  * gdk_window_process_updates().  The net effect is that you can see
4743  * the invalid region for each window and watch redraws as they
4744  * occur. This allows you to diagnose inefficiencies in your application.
4745  *
4746  * In essence, because the GDK rendering model prevents all flicker,
4747  * if you are redrawing the same region 400 times you may never
4748  * notice, aside from noticing a speed problem. Enabling update
4749  * debugging causes GTK to flicker slowly and noticeably, so you can
4750  * see exactly what's being redrawn when, in what order.
4751  *
4752  * The --gtk-debug=updates command line option passed to GTK+ programs
4753  * enables this debug option at application startup time. That's
4754  * usually more useful than calling gdk_window_set_debug_updates()
4755  * yourself, though you might want to use this function to enable
4756  * updates sometime after application startup time.
4757  *
4758  **/
4759 void
4760 gdk_window_set_debug_updates (gboolean setting)
4761 {
4762   debug_updates = setting;
4763 }
4764
4765 /**
4766  * gdk_window_constrain_size:
4767  * @geometry: a #GdkGeometry structure
4768  * @flags: a mask indicating what portions of @geometry are set
4769  * @width: desired width of window
4770  * @height: desired height of the window
4771  * @new_width: (out): location to store resulting width
4772  * @new_height: (out): location to store resulting height
4773  *
4774  * Constrains a desired width and height according to a
4775  * set of geometry hints (such as minimum and maximum size).
4776  */
4777 void
4778 gdk_window_constrain_size (GdkGeometry *geometry,
4779                            guint        flags,
4780                            gint         width,
4781                            gint         height,
4782                            gint        *new_width,
4783                            gint        *new_height)
4784 {
4785   /* This routine is partially borrowed from fvwm.
4786    *
4787    * Copyright 1993, Robert Nation
4788    *     You may use this code for any purpose, as long as the original
4789    *     copyright remains in the source code and all documentation
4790    *
4791    * which in turn borrows parts of the algorithm from uwm
4792    */
4793   gint min_width = 0;
4794   gint min_height = 0;
4795   gint base_width = 0;
4796   gint base_height = 0;
4797   gint xinc = 1;
4798   gint yinc = 1;
4799   gint max_width = G_MAXINT;
4800   gint max_height = G_MAXINT;
4801
4802 #define FLOOR(value, base)      ( ((gint) ((value) / (base))) * (base) )
4803
4804   if ((flags & GDK_HINT_BASE_SIZE) && (flags & GDK_HINT_MIN_SIZE))
4805     {
4806       base_width = geometry->base_width;
4807       base_height = geometry->base_height;
4808       min_width = geometry->min_width;
4809       min_height = geometry->min_height;
4810     }
4811   else if (flags & GDK_HINT_BASE_SIZE)
4812     {
4813       base_width = geometry->base_width;
4814       base_height = geometry->base_height;
4815       min_width = geometry->base_width;
4816       min_height = geometry->base_height;
4817     }
4818   else if (flags & GDK_HINT_MIN_SIZE)
4819     {
4820       base_width = geometry->min_width;
4821       base_height = geometry->min_height;
4822       min_width = geometry->min_width;
4823       min_height = geometry->min_height;
4824     }
4825
4826   if (flags & GDK_HINT_MAX_SIZE)
4827     {
4828       max_width = geometry->max_width ;
4829       max_height = geometry->max_height;
4830     }
4831
4832   if (flags & GDK_HINT_RESIZE_INC)
4833     {
4834       xinc = MAX (xinc, geometry->width_inc);
4835       yinc = MAX (yinc, geometry->height_inc);
4836     }
4837
4838   /* clamp width and height to min and max values
4839    */
4840   width = CLAMP (width, min_width, max_width);
4841   height = CLAMP (height, min_height, max_height);
4842
4843   /* shrink to base + N * inc
4844    */
4845   width = base_width + FLOOR (width - base_width, xinc);
4846   height = base_height + FLOOR (height - base_height, yinc);
4847
4848   /* constrain aspect ratio, according to:
4849    *
4850    *                width
4851    * min_aspect <= -------- <= max_aspect
4852    *                height
4853    */
4854
4855   if (flags & GDK_HINT_ASPECT &&
4856       geometry->min_aspect > 0 &&
4857       geometry->max_aspect > 0)
4858     {
4859       gint delta;
4860
4861       if (geometry->min_aspect * height > width)
4862         {
4863           delta = FLOOR (height - width / geometry->min_aspect, yinc);
4864           if (height - delta >= min_height)
4865             height -= delta;
4866           else
4867             {
4868               delta = FLOOR (height * geometry->min_aspect - width, xinc);
4869               if (width + delta <= max_width)
4870                 width += delta;
4871             }
4872         }
4873
4874       if (geometry->max_aspect * height < width)
4875         {
4876           delta = FLOOR (width - height * geometry->max_aspect, xinc);
4877           if (width - delta >= min_width)
4878             width -= delta;
4879           else
4880             {
4881               delta = FLOOR (width / geometry->max_aspect - height, yinc);
4882               if (height + delta <= max_height)
4883                 height += delta;
4884             }
4885         }
4886     }
4887
4888 #undef FLOOR
4889
4890   *new_width = width;
4891   *new_height = height;
4892 }
4893
4894 /**
4895  * gdk_window_get_pointer:
4896  * @window: a #GdkWindow
4897  * @x: (out) (allow-none): return location for X coordinate of pointer or %NULL to not
4898  *      return the X coordinate
4899  * @y: (out) (allow-none):  return location for Y coordinate of pointer or %NULL to not
4900  *      return the Y coordinate
4901  * @mask: (out) (allow-none): return location for modifier mask or %NULL to not return the
4902  *      modifier mask
4903  *
4904  * Obtains the current pointer position and modifier state.
4905  * The position is given in coordinates relative to the upper left
4906  * corner of @window.
4907  *
4908  * Return value: (transfer none): the window containing the pointer (as with
4909  * gdk_window_at_pointer()), or %NULL if the window containing the
4910  * pointer isn't known to GDK
4911  *
4912  * Deprecated: 3.0: Use gdk_window_get_device_position() instead.
4913  **/
4914 GdkWindow*
4915 gdk_window_get_pointer (GdkWindow         *window,
4916                         gint              *x,
4917                         gint              *y,
4918                         GdkModifierType   *mask)
4919 {
4920   GdkDisplay *display;
4921
4922   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
4923
4924   display = gdk_window_get_display (window);
4925
4926   return gdk_window_get_device_position (window, display->core_pointer, x, y, mask);
4927 }
4928
4929 /**
4930  * gdk_window_get_device_position:
4931  * @window: a #GdkWindow.
4932  * @device: pointer #GdkDevice to query to.
4933  * @x: (out) (allow-none): return location for the X coordinate of @device, or %NULL.
4934  * @y: (out) (allow-none): return location for the Y coordinate of @device, or %NULL.
4935  * @mask: (out) (allow-none): return location for the modifier mask, or %NULL.
4936  *
4937  * Obtains the current device position and modifier state.
4938  * The position is given in coordinates relative to the upper left
4939  * corner of @window.
4940  *
4941  * Return value: (transfer none): The window underneath @device (as with
4942  * gdk_device_get_window_at_position()), or %NULL if the window is not known to GDK.
4943  *
4944  * Since: 3.0
4945  **/
4946 GdkWindow *
4947 gdk_window_get_device_position (GdkWindow       *window,
4948                                 GdkDevice       *device,
4949                                 gint            *x,
4950                                 gint            *y,
4951                                 GdkModifierType *mask)
4952 {
4953   gint tmp_x, tmp_y;
4954   GdkModifierType tmp_mask;
4955   gboolean normal_child;
4956
4957   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
4958   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
4959   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
4960
4961   normal_child = GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_device_state (window,
4962                                                                              device,
4963                                                                              &tmp_x, &tmp_y,
4964                                                                              &tmp_mask);
4965   /* We got the coords on the impl, convert to the window */
4966   tmp_x -= window->abs_x;
4967   tmp_y -= window->abs_y;
4968
4969   if (x)
4970     *x = tmp_x;
4971   if (y)
4972     *y = tmp_y;
4973   if (mask)
4974     *mask = tmp_mask;
4975
4976   _gdk_display_enable_motion_hints (gdk_window_get_display (window), device);
4977
4978   if (normal_child)
4979     return _gdk_window_find_child_at (window, tmp_x, tmp_y);
4980   return NULL;
4981 }
4982
4983 /**
4984  * gdk_get_default_root_window:
4985  *
4986  * Obtains the root window (parent all other windows are inside)
4987  * for the default display and screen.
4988  *
4989  * Return value: (transfer none): the default root window
4990  **/
4991 GdkWindow *
4992 gdk_get_default_root_window (void)
4993 {
4994   return gdk_screen_get_root_window (gdk_screen_get_default ());
4995 }
4996
4997 static void
4998 get_all_native_children (GdkWindow *window,
4999                          GList **native)
5000 {
5001   GdkWindow *child;
5002   GList *l;
5003
5004   for (l = window->children; l != NULL; l = l->next)
5005     {
5006       child = l->data;
5007
5008       if (gdk_window_has_impl (child))
5009         *native = g_list_prepend (*native, child);
5010       else
5011         get_all_native_children (child, native);
5012     }
5013 }
5014
5015
5016 static inline void
5017 gdk_window_raise_internal (GdkWindow *window)
5018 {
5019   GdkWindow *parent = window->parent;
5020   GdkWindow *above;
5021   GList *native_children;
5022   GList *l, listhead;
5023   GdkWindowImplClass *impl_class;
5024
5025   if (parent)
5026     {
5027       parent->children = g_list_remove (parent->children, window);
5028       parent->children = g_list_prepend (parent->children, window);
5029     }
5030
5031   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5032   /* Just do native raise for toplevels */
5033   if (gdk_window_is_toplevel (window) ||
5034       /* The restack_under codepath should work correctly even if the parent
5035          is native, but it relies on the order of ->children to be correct,
5036          and some apps like SWT reorder the x windows without gdks knowledge,
5037          so we use raise directly in order to make these behave as before
5038          when using native windows */
5039       (gdk_window_has_impl (window) && gdk_window_has_impl (parent)))
5040     {
5041       impl_class->raise (window);
5042     }
5043   else if (gdk_window_has_impl (window))
5044     {
5045       above = find_native_sibling_above (parent, window);
5046       if (above)
5047         {
5048           listhead.data = window;
5049           listhead.next = NULL;
5050           listhead.prev = NULL;
5051           impl_class->restack_under ((GdkWindow *)above,
5052                                      &listhead);
5053         }
5054       else
5055         impl_class->raise (window);
5056     }
5057   else
5058     {
5059       native_children = NULL;
5060       get_all_native_children (window, &native_children);
5061       if (native_children != NULL)
5062         {
5063           above = find_native_sibling_above (parent, window);
5064
5065           if (above)
5066             impl_class->restack_under (above, native_children);
5067           else
5068             {
5069               /* Right order, since native_children is bottom-topmost first */
5070               for (l = native_children; l != NULL; l = l->next)
5071                 impl_class->raise (l->data);
5072             }
5073
5074           g_list_free (native_children);
5075         }
5076
5077     }
5078 }
5079
5080 /* Returns TRUE If the native window was mapped or unmapped */
5081 static gboolean
5082 set_viewable (GdkWindow *w,
5083               gboolean val)
5084 {
5085   GdkWindow *child;
5086   GdkWindowImplClass *impl_class;
5087   GList *l;
5088
5089   if (w->viewable == val)
5090     return FALSE;
5091
5092   w->viewable = val;
5093
5094   if (val)
5095     recompute_visible_regions (w, FALSE, FALSE);
5096
5097   for (l = w->children; l != NULL; l = l->next)
5098     {
5099       child = l->data;
5100
5101       if (GDK_WINDOW_IS_MAPPED (child) &&
5102           child->window_type != GDK_WINDOW_FOREIGN)
5103         set_viewable (child, val);
5104     }
5105
5106   if (gdk_window_has_impl (w)  &&
5107       w->window_type != GDK_WINDOW_FOREIGN &&
5108       !gdk_window_is_toplevel (w))
5109     {
5110       /* For most native windows we show/hide them not when they are
5111        * mapped/unmapped, because that may not produce the correct results.
5112        * For instance, if a native window have a non-native parent which is
5113        * hidden, but its native parent is viewable then showing the window
5114        * would make it viewable to X but its not viewable wrt the non-native
5115        * hierarchy. In order to handle this we track the gdk side viewability
5116        * and only map really viewable windows.
5117        *
5118        * There are two exceptions though:
5119        *
5120        * For foreign windows we don't want ever change the mapped state
5121        * except when explicitly done via gdk_window_show/hide, as this may
5122        * cause problems for client owning the foreign window when its window
5123        * is suddenly mapped or unmapped.
5124        *
5125        * For toplevel windows embedded in a foreign window (e.g. a plug)
5126        * we sometimes synthesize a map of a window, but the native
5127        * window is really shown by the embedder, so we don't want to
5128        * do the show ourselves. We can't really tell this case from the normal
5129        * toplevel show as such toplevels are seen by gdk as parents of the
5130        * root window, so we make an exception for all toplevels.
5131        */
5132
5133       impl_class = GDK_WINDOW_IMPL_GET_CLASS (w->impl);
5134       if (val)
5135         impl_class->show ((GdkWindow *)w, FALSE);
5136       else
5137         impl_class->hide ((GdkWindow *)w);
5138
5139       return TRUE;
5140     }
5141
5142   return FALSE;
5143 }
5144
5145 /* Returns TRUE If the native window was mapped or unmapped */
5146 gboolean
5147 _gdk_window_update_viewable (GdkWindow *window)
5148 {
5149   gboolean viewable;
5150
5151   if (window->window_type == GDK_WINDOW_FOREIGN ||
5152       window->window_type == GDK_WINDOW_ROOT)
5153     viewable = TRUE;
5154   else if (gdk_window_is_toplevel (window) ||
5155            window->parent->viewable)
5156     viewable = GDK_WINDOW_IS_MAPPED (window);
5157   else
5158     viewable = FALSE;
5159
5160   return set_viewable (window, viewable);
5161 }
5162
5163 static void
5164 gdk_window_show_internal (GdkWindow *window, gboolean raise)
5165 {
5166   GdkWindowImplClass *impl_class;
5167   gboolean was_mapped, was_viewable;
5168   gboolean did_show;
5169
5170   g_return_if_fail (GDK_IS_WINDOW (window));
5171
5172   if (window->destroyed)
5173     return;
5174
5175   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5176   was_viewable = window->viewable;
5177
5178   if (raise)
5179     /* Keep children in (reverse) stacking order */
5180     gdk_window_raise_internal (window);
5181
5182   if (gdk_window_has_impl (window))
5183     {
5184       if (!was_mapped)
5185         gdk_synthesize_window_state (window,
5186                                      GDK_WINDOW_STATE_WITHDRAWN,
5187                                      GDK_WINDOW_STATE_FOCUSED);
5188     }
5189   else
5190     {
5191       window->state = 0;
5192     }
5193
5194   did_show = _gdk_window_update_viewable (window);
5195
5196   /* If it was already viewable the backend show op won't be called, call it
5197      again to ensure things happen right if the mapped tracking was not right
5198      for e.g. a foreign window.
5199      Dunno if this is strictly needed but its what happened pre-csw.
5200      Also show if not done by gdk_window_update_viewable. */
5201   if (gdk_window_has_impl (window) && (was_viewable || !did_show))
5202     {
5203       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5204       impl_class->show (window, !did_show ? was_mapped : TRUE);
5205     }
5206
5207   if (!was_mapped && !gdk_window_has_impl (window))
5208     {
5209       if (window->event_mask & GDK_STRUCTURE_MASK)
5210         _gdk_make_event (window, GDK_MAP, NULL, FALSE);
5211
5212       if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5213         _gdk_make_event (window, GDK_MAP, NULL, FALSE);
5214     }
5215
5216   if (!was_mapped || raise)
5217     {
5218       recompute_visible_regions (window, TRUE, FALSE);
5219
5220       /* If any decendants became visible we need to send visibility notify */
5221       gdk_window_update_visibility_recursively (window, NULL);
5222
5223       if (gdk_window_is_viewable (window))
5224         {
5225           _gdk_synthesize_crossing_events_for_geometry_change (window);
5226           gdk_window_invalidate_rect_full (window, NULL, TRUE, CLEAR_BG_ALL);
5227         }
5228     }
5229 }
5230
5231 /**
5232  * gdk_window_show_unraised:
5233  * @window: a #GdkWindow
5234  *
5235  * Shows a #GdkWindow onscreen, but does not modify its stacking
5236  * order. In contrast, gdk_window_show() will raise the window
5237  * to the top of the window stack.
5238  *
5239  * On the X11 platform, in Xlib terms, this function calls
5240  * XMapWindow() (it also updates some internal GDK state, which means
5241  * that you can't really use XMapWindow() directly on a GDK window).
5242  */
5243 void
5244 gdk_window_show_unraised (GdkWindow *window)
5245 {
5246   gdk_window_show_internal (window, FALSE);
5247 }
5248
5249 /**
5250  * gdk_window_raise:
5251  * @window: a #GdkWindow
5252  *
5253  * Raises @window to the top of the Z-order (stacking order), so that
5254  * other windows with the same parent window appear below @window.
5255  * This is true whether or not the windows are visible.
5256  *
5257  * If @window is a toplevel, the window manager may choose to deny the
5258  * request to move the window in the Z-order, gdk_window_raise() only
5259  * requests the restack, does not guarantee it.
5260  */
5261 void
5262 gdk_window_raise (GdkWindow *window)
5263 {
5264   cairo_region_t *old_region, *new_region;
5265
5266   g_return_if_fail (GDK_IS_WINDOW (window));
5267
5268   if (window->destroyed)
5269     return;
5270
5271   gdk_window_flush_if_exposing (window);
5272
5273   old_region = NULL;
5274   if (gdk_window_is_viewable (window) &&
5275       !window->input_only)
5276     old_region = cairo_region_copy (window->clip_region);
5277
5278   /* Keep children in (reverse) stacking order */
5279   gdk_window_raise_internal (window);
5280
5281   recompute_visible_regions (window, TRUE, FALSE);
5282
5283   if (old_region)
5284     {
5285       new_region = cairo_region_copy (window->clip_region);
5286
5287       cairo_region_subtract (new_region, old_region);
5288       gdk_window_invalidate_region_full (window, new_region, TRUE, CLEAR_BG_ALL);
5289
5290       cairo_region_destroy (old_region);
5291       cairo_region_destroy (new_region);
5292     }
5293 }
5294
5295 static void
5296 gdk_window_lower_internal (GdkWindow *window)
5297 {
5298   GdkWindow *parent = window->parent;
5299   GdkWindowImplClass *impl_class;
5300   GdkWindow *above;
5301   GList *native_children;
5302   GList *l, listhead;
5303
5304   if (parent)
5305     {
5306       parent->children = g_list_remove (parent->children, window);
5307       parent->children = g_list_append (parent->children, window);
5308     }
5309
5310   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5311   /* Just do native lower for toplevels */
5312   if (gdk_window_is_toplevel (window) ||
5313       /* The restack_under codepath should work correctly even if the parent
5314          is native, but it relies on the order of ->children to be correct,
5315          and some apps like SWT reorder the x windows without gdks knowledge,
5316          so we use lower directly in order to make these behave as before
5317          when using native windows */
5318       (gdk_window_has_impl (window) && gdk_window_has_impl (parent)))
5319     {
5320       impl_class->lower (window);
5321     }
5322   else if (gdk_window_has_impl (window))
5323     {
5324       above = find_native_sibling_above (parent, window);
5325       if (above)
5326         {
5327           listhead.data = window;
5328           listhead.next = NULL;
5329           listhead.prev = NULL;
5330           impl_class->restack_under ((GdkWindow *)above, &listhead);
5331         }
5332       else
5333         impl_class->raise (window);
5334     }
5335   else
5336     {
5337       native_children = NULL;
5338       get_all_native_children (window, &native_children);
5339       if (native_children != NULL)
5340         {
5341           above = find_native_sibling_above (parent, window);
5342
5343           if (above)
5344             impl_class->restack_under ((GdkWindow *)above,
5345                                        native_children);
5346           else
5347             {
5348               /* Right order, since native_children is bottom-topmost first */
5349               for (l = native_children; l != NULL; l = l->next)
5350                 impl_class->raise (l->data);
5351             }
5352
5353           g_list_free (native_children);
5354         }
5355
5356     }
5357 }
5358
5359 static void
5360 gdk_window_invalidate_in_parent (GdkWindow *private)
5361 {
5362   GdkRectangle r, child;
5363
5364   if (gdk_window_is_toplevel (private))
5365     return;
5366
5367   /* get the visible rectangle of the parent */
5368   r.x = r.y = 0;
5369   r.width = private->parent->width;
5370   r.height = private->parent->height;
5371
5372   child.x = private->x;
5373   child.y = private->y;
5374   child.width = private->width;
5375   child.height = private->height;
5376   gdk_rectangle_intersect (&r, &child, &r);
5377
5378   gdk_window_invalidate_rect_full (private->parent, &r, TRUE, CLEAR_BG_ALL);
5379 }
5380
5381
5382 /**
5383  * gdk_window_lower:
5384  * @window: a #GdkWindow
5385  *
5386  * Lowers @window to the bottom of the Z-order (stacking order), so that
5387  * other windows with the same parent window appear above @window.
5388  * This is true whether or not the other windows are visible.
5389  *
5390  * If @window is a toplevel, the window manager may choose to deny the
5391  * request to move the window in the Z-order, gdk_window_lower() only
5392  * requests the restack, does not guarantee it.
5393  *
5394  * Note that gdk_window_show() raises the window again, so don't call this
5395  * function before gdk_window_show(). (Try gdk_window_show_unraised().)
5396  */
5397 void
5398 gdk_window_lower (GdkWindow *window)
5399 {
5400   g_return_if_fail (GDK_IS_WINDOW (window));
5401
5402   if (window->destroyed)
5403     return;
5404
5405   gdk_window_flush_if_exposing (window);
5406
5407   /* Keep children in (reverse) stacking order */
5408   gdk_window_lower_internal (window);
5409
5410   recompute_visible_regions (window, TRUE, FALSE);
5411
5412   _gdk_synthesize_crossing_events_for_geometry_change (window);
5413   gdk_window_invalidate_in_parent (window);
5414 }
5415
5416 /**
5417  * gdk_window_restack:
5418  * @window: a #GdkWindow
5419  * @sibling: (allow-none): a #GdkWindow that is a sibling of @window, or %NULL
5420  * @above: a boolean
5421  *
5422  * Changes the position of  @window in the Z-order (stacking order), so that
5423  * it is above @sibling (if @above is %TRUE) or below @sibling (if @above is
5424  * %FALSE).
5425  *
5426  * If @sibling is %NULL, then this either raises (if @above is %TRUE) or
5427  * lowers the window.
5428  *
5429  * If @window is a toplevel, the window manager may choose to deny the
5430  * request to move the window in the Z-order, gdk_window_restack() only
5431  * requests the restack, does not guarantee it.
5432  *
5433  * Since: 2.18
5434  */
5435 void
5436 gdk_window_restack (GdkWindow     *window,
5437                     GdkWindow     *sibling,
5438                     gboolean       above)
5439 {
5440   GdkWindowImplClass *impl_class;
5441   GdkWindow *parent;
5442   GdkWindow *above_native;
5443   GList *sibling_link;
5444   GList *native_children;
5445   GList *l, listhead;
5446
5447   g_return_if_fail (GDK_IS_WINDOW (window));
5448   g_return_if_fail (sibling == NULL || GDK_IS_WINDOW (sibling));
5449
5450   if (window->destroyed)
5451     return;
5452
5453   if (sibling == NULL)
5454     {
5455       if (above)
5456         gdk_window_raise (window);
5457       else
5458         gdk_window_lower (window);
5459       return;
5460     }
5461
5462   gdk_window_flush_if_exposing (window);
5463
5464   if (gdk_window_is_toplevel (window))
5465     {
5466       g_return_if_fail (gdk_window_is_toplevel (sibling));
5467       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5468       impl_class->restack_toplevel (window, sibling, above);
5469       return;
5470     }
5471
5472   parent = window->parent;
5473   if (parent)
5474     {
5475       sibling_link = g_list_find (parent->children, sibling);
5476       g_return_if_fail (sibling_link != NULL);
5477       if (sibling_link == NULL)
5478         return;
5479
5480       parent->children = g_list_remove (parent->children, window);
5481       if (above)
5482         parent->children = g_list_insert_before (parent->children,
5483                                                  sibling_link,
5484                                                  window);
5485       else
5486         parent->children = g_list_insert_before (parent->children,
5487                                                  sibling_link->next,
5488                                                  window);
5489
5490       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5491       if (gdk_window_has_impl (window))
5492         {
5493           above_native = find_native_sibling_above (parent, window);
5494           if (above_native)
5495             {
5496               listhead.data = window;
5497               listhead.next = NULL;
5498               listhead.prev = NULL;
5499               impl_class->restack_under (above_native, &listhead);
5500             }
5501           else
5502             impl_class->raise (window);
5503         }
5504       else
5505         {
5506           native_children = NULL;
5507           get_all_native_children (window, &native_children);
5508           if (native_children != NULL)
5509             {
5510               above_native = find_native_sibling_above (parent, window);
5511               if (above_native)
5512                 impl_class->restack_under (above_native,
5513                                            native_children);
5514               else
5515                 {
5516                   /* Right order, since native_children is bottom-topmost first */
5517                   for (l = native_children; l != NULL; l = l->next)
5518                     impl_class->raise (l->data);
5519                 }
5520
5521               g_list_free (native_children);
5522             }
5523         }
5524     }
5525
5526   recompute_visible_regions (window, TRUE, FALSE);
5527
5528   _gdk_synthesize_crossing_events_for_geometry_change (window);
5529   gdk_window_invalidate_in_parent (window);
5530 }
5531
5532
5533 /**
5534  * gdk_window_show:
5535  * @window: a #GdkWindow
5536  *
5537  * Like gdk_window_show_unraised(), but also raises the window to the
5538  * top of the window stack (moves the window to the front of the
5539  * Z-order).
5540  *
5541  * This function maps a window so it's visible onscreen. Its opposite
5542  * is gdk_window_hide().
5543  *
5544  * When implementing a #GtkWidget, you should call this function on the widget's
5545  * #GdkWindow as part of the "map" method.
5546  */
5547 void
5548 gdk_window_show (GdkWindow *window)
5549 {
5550   gdk_window_show_internal (window, TRUE);
5551 }
5552
5553 /**
5554  * gdk_window_hide:
5555  * @window: a #GdkWindow
5556  *
5557  * For toplevel windows, withdraws them, so they will no longer be
5558  * known to the window manager; for all windows, unmaps them, so
5559  * they won't be displayed. Normally done automatically as
5560  * part of gtk_widget_hide().
5561  */
5562 void
5563 gdk_window_hide (GdkWindow *window)
5564 {
5565   GdkWindowImplClass *impl_class;
5566   gboolean was_mapped, did_hide;
5567
5568   g_return_if_fail (GDK_IS_WINDOW (window));
5569
5570   if (window->destroyed)
5571     return;
5572
5573   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5574
5575   if (gdk_window_has_impl (window))
5576     {
5577
5578       if (GDK_WINDOW_IS_MAPPED (window))
5579         gdk_synthesize_window_state (window,
5580                                      0,
5581                                      GDK_WINDOW_STATE_WITHDRAWN);
5582     }
5583   else if (was_mapped)
5584     {
5585       GdkDisplay *display;
5586       GdkDeviceManager *device_manager;
5587       GList *devices, *d;
5588
5589       /* May need to break grabs on children */
5590       display = gdk_window_get_display (window);
5591       device_manager = gdk_display_get_device_manager (display);
5592
5593       /* Get all devices */
5594       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
5595       devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
5596       devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
5597
5598       for (d = devices; d; d = d->next)
5599         {
5600           GdkDevice *device = d->data;
5601
5602           if (_gdk_display_end_device_grab (display,
5603                                             device,
5604                                             _gdk_display_get_next_serial (display),
5605                                             window,
5606                                             TRUE))
5607             gdk_device_ungrab (device, GDK_CURRENT_TIME);
5608         }
5609
5610       window->state = GDK_WINDOW_STATE_WITHDRAWN;
5611       g_list_free (devices);
5612     }
5613
5614   did_hide = _gdk_window_update_viewable (window);
5615
5616   /* Hide foreign window as those are not handled by update_viewable. */
5617   if (gdk_window_has_impl (window) && (!did_hide))
5618     {
5619       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5620       impl_class->hide (window);
5621     }
5622
5623   recompute_visible_regions (window, TRUE, FALSE);
5624
5625   /* all decendants became non-visible, we need to send visibility notify */
5626   gdk_window_update_visibility_recursively (window, NULL);
5627
5628   if (was_mapped && !gdk_window_has_impl (window))
5629     {
5630       if (window->event_mask & GDK_STRUCTURE_MASK)
5631         _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5632
5633       if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5634         _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5635
5636       _gdk_synthesize_crossing_events_for_geometry_change (window->parent);
5637     }
5638
5639   /* Invalidate the rect */
5640   if (was_mapped)
5641     gdk_window_invalidate_in_parent (window);
5642 }
5643
5644 /**
5645  * gdk_window_withdraw:
5646  * @window: a toplevel #GdkWindow
5647  *
5648  * Withdraws a window (unmaps it and asks the window manager to forget about it).
5649  * This function is not really useful as gdk_window_hide() automatically
5650  * withdraws toplevel windows before hiding them.
5651  **/
5652 void
5653 gdk_window_withdraw (GdkWindow *window)
5654 {
5655   GdkWindowImplClass *impl_class;
5656   gboolean was_mapped;
5657
5658   g_return_if_fail (GDK_IS_WINDOW (window));
5659
5660   if (window->destroyed)
5661     return;
5662
5663   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5664
5665   if (gdk_window_has_impl (window))
5666     {
5667       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5668       impl_class->withdraw (window);
5669
5670       if (was_mapped)
5671         {
5672           if (window->event_mask & GDK_STRUCTURE_MASK)
5673             _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5674
5675           if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5676             _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5677
5678           _gdk_synthesize_crossing_events_for_geometry_change (window->parent);
5679         }
5680
5681       recompute_visible_regions (window, TRUE, FALSE);
5682     }
5683 }
5684
5685 /**
5686  * gdk_window_set_events:
5687  * @window: a #GdkWindow
5688  * @event_mask: event mask for @window
5689  *
5690  * The event mask for a window determines which events will be reported
5691  * for that window from all master input devices. For example, an event mask
5692  * including #GDK_BUTTON_PRESS_MASK means the window should report button
5693  * press events. The event mask is the bitwise OR of values from the
5694  * #GdkEventMask enumeration.
5695  **/
5696 void
5697 gdk_window_set_events (GdkWindow       *window,
5698                        GdkEventMask     event_mask)
5699 {
5700   GdkWindowImplClass *impl_class;
5701   GdkDisplay *display;
5702
5703   g_return_if_fail (GDK_IS_WINDOW (window));
5704
5705   if (window->destroyed)
5706     return;
5707
5708   /* If motion hint is disabled, enable motion events again */
5709   display = gdk_window_get_display (window);
5710   if ((window->event_mask & GDK_POINTER_MOTION_HINT_MASK) &&
5711       !(event_mask & GDK_POINTER_MOTION_HINT_MASK))
5712     {
5713       GList *devices = window->devices_inside;
5714
5715       while (devices)
5716         {
5717           _gdk_display_enable_motion_hints (display, (GdkDevice *) devices->data);
5718           devices = devices->next;
5719         }
5720     }
5721
5722   window->event_mask = event_mask;
5723
5724   if (gdk_window_has_impl (window))
5725     {
5726       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5727       impl_class->set_events (window,
5728                               get_native_event_mask (window));
5729     }
5730
5731 }
5732
5733 /**
5734  * gdk_window_get_events:
5735  * @window: a #GdkWindow
5736  *
5737  * Gets the event mask for @window for all master input devices. See
5738  * gdk_window_set_events().
5739  *
5740  * Return value: event mask for @window
5741  **/
5742 GdkEventMask
5743 gdk_window_get_events (GdkWindow *window)
5744 {
5745   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
5746
5747   if (window->destroyed)
5748     return 0;
5749
5750   return window->event_mask;
5751 }
5752
5753 /**
5754  * gdk_window_set_device_events:
5755  * @window: a #GdkWindow
5756  * @device: #GdkDevice to enable events for.
5757  * @event_mask: event mask for @window
5758  *
5759  * Sets the event mask for a given device (Normally a floating device, not
5760  * attached to any visible pointer) to @window. For example, an event mask
5761  * including #GDK_BUTTON_PRESS_MASK means the window should report button
5762  * press events. The event mask is the bitwise OR of values from the
5763  * #GdkEventMask enumeration.
5764  *
5765  * Since: 3.0
5766  **/
5767 void
5768 gdk_window_set_device_events (GdkWindow    *window,
5769                               GdkDevice    *device,
5770                               GdkEventMask  event_mask)
5771 {
5772   GdkEventMask device_mask;
5773   GdkDisplay *display;
5774   GdkWindow *native;
5775
5776   g_return_if_fail (GDK_IS_WINDOW (window));
5777   g_return_if_fail (GDK_IS_DEVICE (device));
5778
5779   if (GDK_WINDOW_DESTROYED (window))
5780     return;
5781
5782   /* If motion hint is disabled, enable motion events again */
5783   display = gdk_window_get_display (window);
5784   if ((window->event_mask & GDK_POINTER_MOTION_HINT_MASK) &&
5785       !(event_mask & GDK_POINTER_MOTION_HINT_MASK))
5786     _gdk_display_enable_motion_hints (display, device);
5787
5788   if (G_UNLIKELY (!window->device_events))
5789     window->device_events = g_hash_table_new (NULL, NULL);
5790
5791   if (event_mask == 0)
5792     {
5793       /* FIXME: unsetting events on a master device
5794        * would restore window->event_mask
5795        */
5796       g_hash_table_remove (window->device_events, device);
5797     }
5798   else
5799     g_hash_table_insert (window->device_events, device,
5800                          GINT_TO_POINTER (event_mask));
5801
5802   native = gdk_window_get_toplevel (window);
5803
5804   while (gdk_window_is_offscreen (native))
5805     {
5806       native = gdk_offscreen_window_get_embedder (native);
5807
5808       if (native == NULL ||
5809           (!_gdk_window_has_impl (native) &&
5810            !gdk_window_is_viewable (native)))
5811         return;
5812
5813       native = gdk_window_get_toplevel (native);
5814     }
5815
5816   device_mask = get_native_device_event_mask (window, device);
5817   GDK_DEVICE_GET_CLASS (device)->select_window_events (device, native, device_mask);
5818 }
5819
5820 /**
5821  * gdk_window_get_device_events:
5822  * @window: a #GdkWindow.
5823  * @device: a #GdkDevice.
5824  *
5825  * Returns the event mask for @window corresponding to an specific device.
5826  *
5827  * Returns: device event mask for @window
5828  *
5829  * Since: 3.0
5830  **/
5831 GdkEventMask
5832 gdk_window_get_device_events (GdkWindow *window,
5833                               GdkDevice *device)
5834 {
5835   GdkEventMask mask;
5836
5837   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
5838   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
5839
5840   if (GDK_WINDOW_DESTROYED (window))
5841     return 0;
5842
5843   if (!window->device_events)
5844     return 0;
5845
5846   mask = GPOINTER_TO_INT (g_hash_table_lookup (window->device_events, device));
5847
5848   /* FIXME: device could be controlled by window->event_mask */
5849
5850   return mask;
5851 }
5852
5853 static void
5854 gdk_window_move_resize_toplevel (GdkWindow *window,
5855                                  gboolean   with_move,
5856                                  gint       x,
5857                                  gint       y,
5858                                  gint       width,
5859                                  gint       height)
5860 {
5861   cairo_region_t *old_region, *new_region;
5862   GdkWindowImplClass *impl_class;
5863   gboolean expose;
5864   gboolean is_resize;
5865
5866   expose = FALSE;
5867   old_region = NULL;
5868
5869   is_resize = (width != -1) || (height != -1);
5870
5871   if (gdk_window_is_viewable (window) &&
5872       !window->input_only)
5873     {
5874       expose = TRUE;
5875       old_region = cairo_region_copy (window->clip_region);
5876     }
5877
5878   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5879   impl_class->move_resize (window, with_move, x, y, width, height);
5880
5881   /* Avoid recomputing for pure toplevel moves, for performance reasons */
5882   if (is_resize)
5883     recompute_visible_regions (window, TRUE, FALSE);
5884
5885   if (expose)
5886     {
5887       new_region = cairo_region_copy (window->clip_region);
5888
5889       /* This is the newly exposed area (due to any resize),
5890        * X will expose it, but lets do that without the roundtrip
5891        */
5892       cairo_region_subtract (new_region, old_region);
5893       gdk_window_invalidate_region_full (window, new_region, TRUE, CLEAR_BG_WINCLEARED);
5894
5895       cairo_region_destroy (old_region);
5896       cairo_region_destroy (new_region);
5897     }
5898
5899   _gdk_synthesize_crossing_events_for_geometry_change (window);
5900 }
5901
5902
5903 static void
5904 move_native_children (GdkWindow *private)
5905 {
5906   GList *l;
5907   GdkWindow *child;
5908   GdkWindowImplClass *impl_class;
5909
5910   for (l = private->children; l; l = l->next)
5911     {
5912       child = l->data;
5913
5914       if (child->impl != private->impl)
5915         {
5916           impl_class = GDK_WINDOW_IMPL_GET_CLASS (child->impl);
5917           impl_class->move_resize (child, TRUE,
5918                                    child->x, child->y,
5919                                    child->width, child->height);
5920         }
5921       else
5922         move_native_children  (child);
5923     }
5924 }
5925
5926 static gboolean
5927 collect_native_child_region_helper (GdkWindow *window,
5928                                     GdkWindowImpl *impl,
5929                                     cairo_region_t **region,
5930                                     int x_offset,
5931                                     int y_offset)
5932 {
5933   GdkWindow *child;
5934   cairo_region_t *tmp;
5935   GList *l;
5936
5937   for (l = window->children; l != NULL; l = l->next)
5938     {
5939       child = l->data;
5940
5941       if (!GDK_WINDOW_IS_MAPPED (child) || child->input_only)
5942         continue;
5943
5944       if (child->impl != impl)
5945         {
5946           tmp = cairo_region_copy (child->clip_region);
5947           cairo_region_translate (tmp,
5948                              x_offset + child->x,
5949                              y_offset + child->y);
5950           if (*region == NULL)
5951             *region = tmp;
5952           else
5953             {
5954               cairo_region_union (*region, tmp);
5955               cairo_region_destroy (tmp);
5956             }
5957         }
5958       else
5959         collect_native_child_region_helper (child, impl, region,
5960                                             x_offset + child->x,
5961                                             y_offset + child->y);
5962     }
5963
5964   return FALSE;
5965 }
5966
5967 static cairo_region_t *
5968 collect_native_child_region (GdkWindow *window,
5969                              gboolean include_this)
5970 {
5971   cairo_region_t *region;
5972
5973   if (include_this && gdk_window_has_impl (window) && window->viewable)
5974     return cairo_region_copy (window->clip_region);
5975
5976   region = NULL;
5977
5978   collect_native_child_region_helper (window, window->impl, &region, 0, 0);
5979
5980   return region;
5981 }
5982
5983
5984 static void
5985 gdk_window_move_resize_internal (GdkWindow *window,
5986                                  gboolean   with_move,
5987                                  gint       x,
5988                                  gint       y,
5989                                  gint       width,
5990                                  gint       height)
5991 {
5992   cairo_region_t *old_region, *new_region, *copy_area;
5993   cairo_region_t *old_native_child_region, *new_native_child_region;
5994   GdkWindow *impl_window;
5995   GdkWindowImplClass *impl_class;
5996   gboolean expose;
5997   int old_x, old_y, old_abs_x, old_abs_y;
5998   int dx, dy;
5999
6000   g_return_if_fail (GDK_IS_WINDOW (window));
6001
6002   if (window->destroyed)
6003     return;
6004
6005   if (gdk_window_is_toplevel (window))
6006     {
6007       gdk_window_move_resize_toplevel (window, with_move, x, y, width, height);
6008       return;
6009     }
6010
6011   /* Bail early if no change */
6012   if (window->width == width &&
6013       window->height == height &&
6014       (!with_move ||
6015        (window->x == x &&
6016         window->y == y)))
6017     return;
6018
6019   gdk_window_flush_if_exposing (window);
6020
6021   /* Handle child windows */
6022
6023   expose = FALSE;
6024   old_region = NULL;
6025
6026   impl_window = gdk_window_get_impl_window (window);
6027
6028   old_x = window->x;
6029   old_y = window->y;
6030
6031   old_native_child_region = NULL;
6032   if (gdk_window_is_viewable (window) &&
6033       !window->input_only)
6034     {
6035       expose = TRUE;
6036
6037       old_region = cairo_region_copy (window->clip_region);
6038       /* Adjust region to parent window coords */
6039       cairo_region_translate (old_region, window->x, window->y);
6040
6041       old_native_child_region = collect_native_child_region (window, TRUE);
6042       if (old_native_child_region)
6043         {
6044           /* Adjust region to parent window coords */
6045           cairo_region_translate (old_native_child_region, window->x, window->y);
6046
6047           /* Any native window move will immediately copy stuff to the destination, which may overwrite a
6048            * source or destination for a delayed GdkWindowRegionMove. So, we need
6049            * to flush those here for the parent window and all overlapped subwindows
6050            * of it. And we need to do this before setting the new clips as those will be
6051            * affecting this.
6052            */
6053           gdk_window_flush_recursive (window->parent);
6054         }
6055     }
6056
6057   /* Set the new position and size */
6058   if (with_move)
6059     {
6060       window->x = x;
6061       window->y = y;
6062     }
6063   if (!(width < 0 && height < 0))
6064     {
6065       if (width < 1)
6066         width = 1;
6067       window->width = width;
6068       if (height < 1)
6069         height = 1;
6070       window->height = height;
6071     }
6072
6073   dx = window->x - old_x;
6074   dy = window->y - old_y;
6075
6076   old_abs_x = window->abs_x;
6077   old_abs_y = window->abs_y;
6078
6079   recompute_visible_regions (window, TRUE, FALSE);
6080
6081   new_native_child_region = NULL;
6082   if (old_native_child_region)
6083     {
6084       new_native_child_region = collect_native_child_region (window, TRUE);
6085       /* Adjust region to parent window coords */
6086       cairo_region_translate (new_native_child_region, window->x, window->y);
6087     }
6088
6089   if (gdk_window_has_impl (window))
6090     {
6091       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6092
6093       /* Do the actual move after recomputing things, as this will have set the shape to
6094          the now correct one, thus avoiding copying regions that should not be copied. */
6095       impl_class->move_resize (window, TRUE,
6096                                window->x, window->y,
6097                                window->width, window->height);
6098     }
6099   else if (old_abs_x != window->abs_x ||
6100            old_abs_y != window->abs_y)
6101     move_native_children (window);
6102
6103   if (expose)
6104     {
6105       new_region = cairo_region_copy (window->clip_region);
6106       /* Adjust region to parent window coords */
6107       cairo_region_translate (new_region, window->x, window->y);
6108
6109       /* copy_area:
6110        * Part of the data at the new location can be copied from the
6111        * old location, this area is the intersection of the old region
6112        * moved as the copy will move it and then intersected with
6113        * the new region.
6114        *
6115        * new_region:
6116        * Everything in the old and new regions that is not copied must be
6117        * invalidated (including children) as this is newly exposed
6118        */
6119       copy_area = cairo_region_copy (new_region);
6120
6121       cairo_region_union (new_region, old_region);
6122
6123       if (old_native_child_region)
6124         {
6125           /* Don't copy from inside native children, as this is copied by
6126            * the native window move.
6127            */
6128           cairo_region_subtract (old_region, old_native_child_region);
6129         }
6130       cairo_region_translate (old_region, dx, dy);
6131
6132       cairo_region_intersect (copy_area, old_region);
6133
6134       if (new_native_child_region)
6135         {
6136           /* Don't copy any bits that would cause a read from the moved
6137              native windows, as we can't read that data */
6138           cairo_region_translate (new_native_child_region, dx, dy);
6139           cairo_region_subtract (copy_area, new_native_child_region);
6140           cairo_region_translate (new_native_child_region, -dx, -dy);
6141         }
6142
6143       cairo_region_subtract (new_region, copy_area);
6144
6145       /* Convert old region to impl coords */
6146       cairo_region_translate (old_region, -dx + window->abs_x - window->x, -dy + window->abs_y - window->y);
6147
6148       /* convert from parent coords to impl */
6149       cairo_region_translate (copy_area, window->abs_x - window->x, window->abs_y - window->y);
6150
6151       move_region_on_impl (impl_window, copy_area, dx, dy); /* takes ownership of copy_area */
6152
6153       /* Invalidate affected part in the parent window
6154        *  (no higher window should be affected)
6155        * We also invalidate any children in that area, which could include
6156        * this window if it still overlaps that area.
6157        */
6158       if (old_native_child_region)
6159         {
6160           /* No need to expose the region that the native window move copies */
6161           cairo_region_translate (old_native_child_region, dx, dy);
6162           cairo_region_intersect (old_native_child_region, new_native_child_region);
6163           cairo_region_subtract (new_region, old_native_child_region);
6164         }
6165       gdk_window_invalidate_region_full (window->parent, new_region, TRUE, CLEAR_BG_ALL);
6166
6167       cairo_region_destroy (old_region);
6168       cairo_region_destroy (new_region);
6169     }
6170
6171   if (old_native_child_region)
6172     {
6173       cairo_region_destroy (old_native_child_region);
6174       cairo_region_destroy (new_native_child_region);
6175     }
6176
6177   _gdk_synthesize_crossing_events_for_geometry_change (window);
6178 }
6179
6180
6181
6182 /**
6183  * gdk_window_move:
6184  * @window: a #GdkWindow
6185  * @x: X coordinate relative to window's parent
6186  * @y: Y coordinate relative to window's parent
6187  *
6188  * Repositions a window relative to its parent window.
6189  * For toplevel windows, window managers may ignore or modify the move;
6190  * you should probably use gtk_window_move() on a #GtkWindow widget
6191  * anyway, instead of using GDK functions. For child windows,
6192  * the move will reliably succeed.
6193  *
6194  * If you're also planning to resize the window, use gdk_window_move_resize()
6195  * to both move and resize simultaneously, for a nicer visual effect.
6196  **/
6197 void
6198 gdk_window_move (GdkWindow *window,
6199                  gint       x,
6200                  gint       y)
6201 {
6202   gdk_window_move_resize_internal (window, TRUE, x, y, -1, -1);
6203 }
6204
6205 /**
6206  * gdk_window_resize:
6207  * @window: a #GdkWindow
6208  * @width: new width of the window
6209  * @height: new height of the window
6210  *
6211  * Resizes @window; for toplevel windows, asks the window manager to resize
6212  * the window. The window manager may not allow the resize. When using GTK+,
6213  * use gtk_window_resize() instead of this low-level GDK function.
6214  *
6215  * Windows may not be resized below 1x1.
6216  *
6217  * If you're also planning to move the window, use gdk_window_move_resize()
6218  * to both move and resize simultaneously, for a nicer visual effect.
6219  **/
6220 void
6221 gdk_window_resize (GdkWindow *window,
6222                    gint       width,
6223                    gint       height)
6224 {
6225   gdk_window_move_resize_internal (window, FALSE, 0, 0, width, height);
6226 }
6227
6228
6229 /**
6230  * gdk_window_move_resize:
6231  * @window: a #GdkWindow
6232  * @x: new X position relative to window's parent
6233  * @y: new Y position relative to window's parent
6234  * @width: new width
6235  * @height: new height
6236  *
6237  * Equivalent to calling gdk_window_move() and gdk_window_resize(),
6238  * except that both operations are performed at once, avoiding strange
6239  * visual effects. (i.e. the user may be able to see the window first
6240  * move, then resize, if you don't use gdk_window_move_resize().)
6241  **/
6242 void
6243 gdk_window_move_resize (GdkWindow *window,
6244                         gint       x,
6245                         gint       y,
6246                         gint       width,
6247                         gint       height)
6248 {
6249   gdk_window_move_resize_internal (window, TRUE, x, y, width, height);
6250 }
6251
6252
6253 /**
6254  * gdk_window_scroll:
6255  * @window: a #GdkWindow
6256  * @dx: Amount to scroll in the X direction
6257  * @dy: Amount to scroll in the Y direction
6258  *
6259  * Scroll the contents of @window, both pixels and children, by the
6260  * given amount. @window itself does not move. Portions of the window
6261  * that the scroll operation brings in from offscreen areas are
6262  * invalidated. The invalidated region may be bigger than what would
6263  * strictly be necessary.
6264  *
6265  * For X11, a minimum area will be invalidated if the window has no
6266  * subwindows, or if the edges of the window's parent do not extend
6267  * beyond the edges of the window. In other cases, a multi-step process
6268  * is used to scroll the window which may produce temporary visual
6269  * artifacts and unnecessary invalidations.
6270  **/
6271 void
6272 gdk_window_scroll (GdkWindow *window,
6273                    gint       dx,
6274                    gint       dy)
6275 {
6276   GdkWindow *impl_window;
6277   cairo_region_t *copy_area, *noncopy_area;
6278   cairo_region_t *old_native_child_region, *new_native_child_region;
6279   GList *tmp_list;
6280
6281   g_return_if_fail (GDK_IS_WINDOW (window));
6282
6283   if (dx == 0 && dy == 0)
6284     return;
6285
6286   if (window->destroyed)
6287     return;
6288
6289   gdk_window_flush_if_exposing (window);
6290
6291   old_native_child_region = collect_native_child_region (window, FALSE);
6292   if (old_native_child_region)
6293     {
6294       /* Any native window move will immediately copy stuff to the destination, which may overwrite a
6295        * source or destination for a delayed GdkWindowRegionMove. So, we need
6296        * to flush those here for the window and all overlapped subwindows
6297        * of it. And we need to do this before setting the new clips as those will be
6298        * affecting this.
6299        */
6300       gdk_window_flush_recursive (window);
6301     }
6302
6303
6304   /* First move all child windows, without causing invalidation */
6305
6306   tmp_list = window->children;
6307   while (tmp_list)
6308     {
6309       GdkWindow *child = GDK_WINDOW (tmp_list->data);
6310
6311       /* Just update the positions, the bits will move with the copy */
6312       child->x += dx;
6313       child->y += dy;
6314
6315       tmp_list = tmp_list->next;
6316     }
6317
6318   recompute_visible_regions (window, FALSE, TRUE);
6319
6320   new_native_child_region = NULL;
6321   if (old_native_child_region)
6322     new_native_child_region = collect_native_child_region (window, FALSE);
6323
6324   move_native_children (window);
6325
6326   /* Then copy the actual bits of the window w/ child windows */
6327
6328   impl_window = gdk_window_get_impl_window (window);
6329
6330   /* Calculate the area that can be gotten by copying the old area */
6331   copy_area = cairo_region_copy (window->clip_region);
6332   if (old_native_child_region)
6333     {
6334       /* Don't copy from inside native children, as this is copied by
6335        * the native window move.
6336        */
6337       cairo_region_subtract (copy_area, old_native_child_region);
6338
6339       /* Don't copy any bits that would cause a read from the moved
6340          native windows, as we can't read that data */
6341       cairo_region_subtract (copy_area, new_native_child_region);
6342     }
6343   cairo_region_translate (copy_area, dx, dy);
6344   cairo_region_intersect (copy_area, window->clip_region);
6345
6346   /* And the rest need to be invalidated */
6347   noncopy_area = cairo_region_copy (window->clip_region);
6348   cairo_region_subtract (noncopy_area, copy_area);
6349
6350   /* convert from window coords to impl */
6351   cairo_region_translate (copy_area, window->abs_x, window->abs_y);
6352
6353   move_region_on_impl (impl_window, copy_area, dx, dy); /* takes ownership of copy_area */
6354
6355   /* Invalidate not copied regions */
6356   if (old_native_child_region)
6357     {
6358       /* No need to expose the region that the native window move copies */
6359       cairo_region_translate (old_native_child_region, dx, dy);
6360       cairo_region_intersect (old_native_child_region, new_native_child_region);
6361       cairo_region_subtract (noncopy_area, old_native_child_region);
6362     }
6363   gdk_window_invalidate_region_full (window, noncopy_area, TRUE, CLEAR_BG_ALL);
6364
6365   cairo_region_destroy (noncopy_area);
6366
6367   if (old_native_child_region)
6368     {
6369       cairo_region_destroy (old_native_child_region);
6370       cairo_region_destroy (new_native_child_region);
6371     }
6372
6373   _gdk_synthesize_crossing_events_for_geometry_change (window);
6374 }
6375
6376 /**
6377  * gdk_window_move_region:
6378  * @window: a #GdkWindow
6379  * @region: The #cairo_region_t to move
6380  * @dx: Amount to move in the X direction
6381  * @dy: Amount to move in the Y direction
6382  *
6383  * Move the part of @window indicated by @region by @dy pixels in the Y
6384  * direction and @dx pixels in the X direction. The portions of @region
6385  * that not covered by the new position of @region are invalidated.
6386  *
6387  * Child windows are not moved.
6388  *
6389  * Since: 2.8
6390  */
6391 void
6392 gdk_window_move_region (GdkWindow       *window,
6393                         const cairo_region_t *region,
6394                         gint             dx,
6395                         gint             dy)
6396 {
6397   GdkWindow *impl_window;
6398   cairo_region_t *nocopy_area;
6399   cairo_region_t *copy_area;
6400
6401   g_return_if_fail (GDK_IS_WINDOW (window));
6402   g_return_if_fail (region != NULL);
6403
6404   if (dx == 0 && dy == 0)
6405     return;
6406
6407   if (window->destroyed)
6408     return;
6409
6410   impl_window = gdk_window_get_impl_window (window);
6411
6412   /* compute source regions */
6413   copy_area = cairo_region_copy (region);
6414   cairo_region_intersect (copy_area, window->clip_region_with_children);
6415
6416   /* compute destination regions */
6417   cairo_region_translate (copy_area, dx, dy);
6418   cairo_region_intersect (copy_area, window->clip_region_with_children);
6419
6420   /* Invalidate parts of the region (source and dest) not covered
6421      by the copy */
6422   nocopy_area = cairo_region_copy (region);
6423   cairo_region_translate (nocopy_area, dx, dy);
6424   cairo_region_union (nocopy_area, region);
6425   cairo_region_subtract (nocopy_area, copy_area);
6426
6427   /* convert from window coords to impl */
6428   cairo_region_translate (copy_area, window->abs_x, window->abs_y);
6429   move_region_on_impl (impl_window, copy_area, dx, dy); /* Takes ownership of copy_area */
6430
6431   gdk_window_invalidate_region_full (window, nocopy_area, FALSE, CLEAR_BG_ALL);
6432   cairo_region_destroy (nocopy_area);
6433 }
6434
6435 /**
6436  * gdk_window_set_background:
6437  * @window: a #GdkWindow
6438  * @color: a #GdkColor
6439  *
6440  * Sets the background color of @window. (However, when using GTK+,
6441  * set the background of a widget with gtk_widget_modify_bg() - if
6442  * you're an application - or gtk_style_set_background() - if you're
6443  * implementing a custom widget.)
6444  *
6445  * See also gdk_window_set_background_pattern().
6446  */
6447 void
6448 gdk_window_set_background (GdkWindow      *window,
6449                            const GdkColor *color)
6450 {
6451   cairo_pattern_t *pattern;
6452
6453   g_return_if_fail (GDK_IS_WINDOW (window));
6454
6455   pattern = cairo_pattern_create_rgb (color->red   / 65535.,
6456                                       color->green / 65535.,
6457                                       color->blue  / 65535.);
6458
6459   gdk_window_set_background_pattern (window, pattern);
6460
6461   cairo_pattern_destroy (pattern);
6462 }
6463
6464 /**
6465  * gdk_window_set_background_rgba:
6466  * @window: a #GdkWindow
6467  * @rgba: a #GdkRGBA color
6468  *
6469  * Sets the background color of @window.
6470  *
6471  * See also gdk_window_set_background_pattern().
6472  **/
6473 void
6474 gdk_window_set_background_rgba (GdkWindow *window,
6475                                 GdkRGBA   *rgba)
6476 {
6477   cairo_pattern_t *pattern;
6478
6479   g_return_if_fail (GDK_IS_WINDOW (window));
6480   g_return_if_fail (rgba != NULL);
6481
6482   pattern = cairo_pattern_create_rgba (rgba->red, rgba->green,
6483                                        rgba->blue, rgba->alpha);
6484
6485   gdk_window_set_background_pattern (window, pattern);
6486
6487   cairo_pattern_destroy (pattern);
6488 }
6489
6490 /**
6491  * gdk_window_set_background_pattern:
6492  * @window: a #GdkWindow
6493  * @pattern: (allow-none): a pattern to use, or %NULL
6494  *
6495  * Sets the background of @window.
6496  *
6497  * A background of %NULL means that the window will inherit its
6498  * background form its parent window.
6499  *
6500  * The windowing system will normally fill a window with its background
6501  * when the window is obscured then exposed.
6502  */
6503 void
6504 gdk_window_set_background_pattern (GdkWindow *window,
6505                                    cairo_pattern_t *pattern)
6506 {
6507   g_return_if_fail (GDK_IS_WINDOW (window));
6508
6509   if (window->input_only)
6510     return;
6511
6512   if (pattern)
6513     cairo_pattern_reference (pattern);
6514   if (window->background)
6515     cairo_pattern_destroy (window->background);
6516   window->background = pattern;
6517
6518   if (gdk_window_has_impl (window))
6519     {
6520       GdkWindowImplClass *impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6521       impl_class->set_background (window, pattern);
6522     }
6523   else
6524     gdk_window_invalidate_rect_full (window, NULL, TRUE, CLEAR_BG_ALL);
6525 }
6526
6527 /**
6528  * gdk_window_get_background_pattern:
6529  * @window: a window
6530  *
6531  * Gets the pattern used to clear the background on @window. If @window
6532  * does not have its own background and reuses the parent's, %NULL is
6533  * returned and you'll have to query it yourself.
6534  *
6535  * Returns: (transfer none): The pattern to use for the background or
6536  *     %NULL to use the parent's background.
6537  *
6538  * Since: 2.22
6539  **/
6540 cairo_pattern_t *
6541 gdk_window_get_background_pattern (GdkWindow *window)
6542 {
6543   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6544
6545   return window->background;
6546 }
6547
6548 static void
6549 gdk_window_set_cursor_internal (GdkWindow *window,
6550                                 GdkDevice *device,
6551                                 GdkCursor *cursor)
6552 {
6553   if (GDK_WINDOW_DESTROYED (window))
6554     return;
6555
6556   if (window->window_type == GDK_WINDOW_ROOT ||
6557       window->window_type == GDK_WINDOW_FOREIGN)
6558     GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_device_cursor (window, device, cursor);
6559   else
6560     {
6561       GdkPointerWindowInfo *pointer_info;
6562       GdkDisplay *display;
6563
6564       display = gdk_window_get_display (window);
6565       pointer_info = _gdk_display_get_pointer_info (display, device);
6566
6567       if (_gdk_window_event_parent_of (window, pointer_info->window_under_pointer))
6568         update_cursor (display, device);
6569     }
6570 }
6571
6572 /**
6573  * gdk_window_get_cursor:
6574  * @window: a #GdkWindow
6575  *
6576  * Retrieves a #GdkCursor pointer for the cursor currently set on the
6577  * specified #GdkWindow, or %NULL.  If the return value is %NULL then
6578  * there is no custom cursor set on the specified window, and it is
6579  * using the cursor for its parent window.
6580  *
6581  * Return value: (transfer none): a #GdkCursor, or %NULL. The returned
6582  *   object is owned by the #GdkWindow and should not be unreferenced
6583  *   directly. Use gdk_window_set_cursor() to unset the cursor of the
6584  *   window
6585  *
6586  * Since: 2.18
6587  */
6588 GdkCursor *
6589 gdk_window_get_cursor (GdkWindow *window)
6590 {
6591   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6592
6593   return window->cursor;
6594 }
6595
6596 /**
6597  * gdk_window_set_cursor:
6598  * @window: a #GdkWindow
6599  * @cursor: (allow-none): a cursor
6600  *
6601  * Sets the default mouse pointer for a #GdkWindow. Use gdk_cursor_new_for_display()
6602  * or gdk_cursor_new_from_pixbuf() to create the cursor. To make the cursor
6603  * invisible, use %GDK_BLANK_CURSOR. Passing %NULL for the @cursor argument
6604  * to gdk_window_set_cursor() means that @window will use the cursor of its
6605  * parent window. Most windows should use this default.
6606  */
6607 void
6608 gdk_window_set_cursor (GdkWindow *window,
6609                        GdkCursor *cursor)
6610 {
6611   GdkDisplay *display;
6612
6613   g_return_if_fail (GDK_IS_WINDOW (window));
6614
6615   display = gdk_window_get_display (window);
6616
6617   if (window->cursor)
6618     {
6619       g_object_unref (window->cursor);
6620       window->cursor = NULL;
6621     }
6622
6623   if (!GDK_WINDOW_DESTROYED (window))
6624     {
6625       GdkDeviceManager *device_manager;
6626       GList *devices, *d;
6627
6628       if (cursor)
6629         window->cursor = g_object_ref (cursor);
6630
6631       device_manager = gdk_display_get_device_manager (display);
6632       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
6633
6634       for (d = devices; d; d = d->next)
6635         {
6636           GdkDevice *device;
6637
6638           device = d->data;
6639
6640           if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
6641             continue;
6642
6643           gdk_window_set_cursor_internal (window, device, window->cursor);
6644         }
6645
6646       g_list_free (devices);
6647       g_object_notify (G_OBJECT (window), "cursor");
6648     }
6649 }
6650
6651 /**
6652  * gdk_window_get_device_cursor:
6653  * @window: a #GdkWindow.
6654  * @device: a master, pointer #GdkDevice.
6655  *
6656  * Retrieves a #GdkCursor pointer for the @device currently set on the
6657  * specified #GdkWindow, or %NULL.  If the return value is %NULL then
6658  * there is no custom cursor set on the specified window, and it is
6659  * using the cursor for its parent window.
6660  *
6661  * Returns: (transfer none): a #GdkCursor, or %NULL. The returned
6662  *   object is owned by the #GdkWindow and should not be unreferenced
6663  *   directly. Use gdk_window_set_cursor() to unset the cursor of the
6664  *   window
6665  *
6666  * Since: 3.0
6667  **/
6668 GdkCursor *
6669 gdk_window_get_device_cursor (GdkWindow *window,
6670                               GdkDevice *device)
6671 {
6672   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6673   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
6674   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
6675   g_return_val_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER, NULL);
6676
6677   return g_hash_table_lookup (window->device_cursor, device);
6678 }
6679
6680 /**
6681  * gdk_window_set_device_cursor:
6682  * @window: a #Gdkwindow
6683  * @device: a master, pointer #GdkDevice
6684  * @cursor: a #GdkCursor
6685  *
6686  * Sets a specific #GdkCursor for a given device when it gets inside @window.
6687  * Use gdk_cursor_new_for_display() or gdk_cursor_new_from_pixbuf() to create
6688  * the cursor. To make the cursor invisible, use %GDK_BLANK_CURSOR. Passing
6689  * %NULL for the @cursor argument to gdk_window_set_cursor() means that
6690  * @window will use the cursor of its parent window. Most windows should
6691  * use this default.
6692  *
6693  * Since: 3.0
6694  **/
6695 void
6696 gdk_window_set_device_cursor (GdkWindow *window,
6697                               GdkDevice *device,
6698                               GdkCursor *cursor)
6699 {
6700   g_return_if_fail (GDK_IS_WINDOW (window));
6701   g_return_if_fail (GDK_IS_DEVICE (device));
6702   g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
6703   g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
6704
6705   if (!cursor)
6706     g_hash_table_remove (window->device_cursor, device);
6707   else
6708     g_hash_table_replace (window->device_cursor, device, g_object_ref (cursor));
6709
6710   gdk_window_set_cursor_internal (window, device, cursor);
6711 }
6712
6713 /**
6714  * gdk_window_get_geometry:
6715  * @window: a #GdkWindow
6716  * @x: (out) (allow-none): return location for X coordinate of window (relative to its parent)
6717  * @y: (out) (allow-none): return location for Y coordinate of window (relative to its parent)
6718  * @width: (out) (allow-none): return location for width of window
6719  * @height: (out) (allow-none): return location for height of window
6720  *
6721  * Any of the return location arguments to this function may be %NULL,
6722  * if you aren't interested in getting the value of that field.
6723  *
6724  * The X and Y coordinates returned are relative to the parent window
6725  * of @window, which for toplevels usually means relative to the
6726  * window decorations (titlebar, etc.) rather than relative to the
6727  * root window (screen-size background window).
6728  *
6729  * On the X11 platform, the geometry is obtained from the X server,
6730  * so reflects the latest position of @window; this may be out-of-sync
6731  * with the position of @window delivered in the most-recently-processed
6732  * #GdkEventConfigure. gdk_window_get_position() in contrast gets the
6733  * position from the most recent configure event.
6734  *
6735  * <note>
6736  * If @window is not a toplevel, it is <emphasis>much</emphasis> better
6737  * to call gdk_window_get_position(), gdk_window_get_width() and
6738  * gdk_window_get_height() instead, because it avoids the roundtrip to
6739  * the X server and because these functions support the full 32-bit
6740  * coordinate space, whereas gdk_window_get_geometry() is restricted to
6741  * the 16-bit coordinates of X11.
6742  *</note>
6743  **/
6744 void
6745 gdk_window_get_geometry (GdkWindow *window,
6746                          gint      *x,
6747                          gint      *y,
6748                          gint      *width,
6749                          gint      *height)
6750 {
6751   GdkWindow *parent;
6752   GdkWindowImplClass *impl_class;
6753
6754   if (!window)
6755     {
6756       GDK_NOTE (MULTIHEAD,
6757                 g_message ("gdk_window_get_geometry(): Window needs "
6758                            "to be non-NULL to be multi head safe"));
6759       window = gdk_screen_get_root_window ((gdk_screen_get_default ()));
6760     }
6761
6762   g_return_if_fail (GDK_IS_WINDOW (window));
6763
6764   if (!GDK_WINDOW_DESTROYED (window))
6765     {
6766       if (gdk_window_has_impl (window))
6767         {
6768           impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6769           impl_class->get_geometry (window, x, y,
6770                                     width, height);
6771           /* This reports the position wrt to the native parent, we need to convert
6772              it to be relative to the client side parent */
6773           parent = window->parent;
6774           if (parent && !gdk_window_has_impl (parent))
6775             {
6776               if (x)
6777                 *x -= parent->abs_x;
6778               if (y)
6779                 *y -= parent->abs_y;
6780             }
6781         }
6782       else
6783         {
6784           if (x)
6785             *x = window->x;
6786           if (y)
6787             *y = window->y;
6788           if (width)
6789             *width = window->width;
6790           if (height)
6791             *height = window->height;
6792         }
6793     }
6794 }
6795
6796 /**
6797  * gdk_window_get_width:
6798  * @window: a #GdkWindow
6799  *
6800  * Returns the width of the given @window.
6801  *
6802  * On the X11 platform the returned size is the size reported in the
6803  * most-recently-processed configure event, rather than the current
6804  * size on the X server.
6805  *
6806  * Returns: The width of @window
6807  *
6808  * Since: 2.24
6809  */
6810 int
6811 gdk_window_get_width (GdkWindow *window)
6812 {
6813   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6814
6815   return window->width;
6816 }
6817
6818 /**
6819  * gdk_window_get_height:
6820  * @window: a #GdkWindow
6821  *
6822  * Returns the height of the given @window.
6823  *
6824  * On the X11 platform the returned size is the size reported in the
6825  * most-recently-processed configure event, rather than the current
6826  * size on the X server.
6827  *
6828  * Returns: The height of @window
6829  *
6830  * Since: 2.24
6831  */
6832 int
6833 gdk_window_get_height (GdkWindow *window)
6834 {
6835   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6836
6837   return window->height;
6838 }
6839
6840 /**
6841  * gdk_window_get_origin:
6842  * @window: a #GdkWindow
6843  * @x: (out) (allow-none): return location for X coordinate
6844  * @y: (out) (allow-none): return location for Y coordinate
6845  *
6846  * Obtains the position of a window in root window coordinates.
6847  * (Compare with gdk_window_get_position() and
6848  * gdk_window_get_geometry() which return the position of a window
6849  * relative to its parent window.)
6850  *
6851  * Return value: not meaningful, ignore
6852  */
6853 gint
6854 gdk_window_get_origin (GdkWindow *window,
6855                        gint      *x,
6856                        gint      *y)
6857 {
6858   GdkWindowImplClass *impl_class;
6859
6860   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6861
6862   if (GDK_WINDOW_DESTROYED (window))
6863     {
6864       if (x)
6865         *x = 0;
6866       if (y)
6867         *y = 0;
6868       return 0;
6869     }
6870
6871   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6872   impl_class->get_root_coords (window,
6873                                window->abs_x,
6874                                window->abs_y,
6875                                x, y);
6876
6877   return TRUE;
6878 }
6879
6880 /**
6881  * gdk_window_get_root_coords:
6882  * @window: a #GdkWindow
6883  * @x: X coordinate in window
6884  * @y: Y coordinate in window
6885  * @root_x: (out): return location for X coordinate
6886  * @root_y: (out): return location for Y coordinate
6887  *
6888  * Obtains the position of a window position in root
6889  * window coordinates. This is similar to
6890  * gdk_window_get_origin() but allows you go pass
6891  * in any position in the window, not just the origin.
6892  *
6893  * Since: 2.18
6894  */
6895 void
6896 gdk_window_get_root_coords (GdkWindow *window,
6897                             gint       x,
6898                             gint       y,
6899                             gint      *root_x,
6900                             gint      *root_y)
6901 {
6902   GdkWindowImplClass *impl_class;
6903
6904   g_return_if_fail (GDK_IS_WINDOW (window));
6905
6906   if (GDK_WINDOW_DESTROYED (window))
6907     {
6908       *root_x = 0;
6909       *root_y = 0;
6910       return;
6911     }
6912   
6913   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6914   impl_class->get_root_coords (window,
6915                                x + window->abs_x,
6916                                y + window->abs_y,
6917                                root_x, root_y);
6918 }
6919
6920 /**
6921  * gdk_window_coords_to_parent:
6922  * @window: a child window
6923  * @x: X coordinate in child's coordinate system
6924  * @y: Y coordinate in child's coordinate system
6925  * @parent_x: (out) (allow-none): return location for X coordinate
6926  * in parent's coordinate system, or %NULL
6927  * @parent_y: (out) (allow-none): return location for Y coordinate
6928  * in parent's coordinate system, or %NULL
6929  *
6930  * Transforms window coordinates from a child window to its parent
6931  * window, where the parent window is the normal parent as returned by
6932  * gdk_window_get_parent() for normal windows, and the window's
6933  * embedder as returned by gdk_offscreen_window_get_embedder() for
6934  * offscreen windows.
6935  *
6936  * For normal windows, calling this function is equivalent to adding
6937  * the return values of gdk_window_get_position() to the child coordinates.
6938  * For offscreen windows however (which can be arbitrarily transformed),
6939  * this function calls the GdkWindow::to-embedder: signal to translate
6940  * the coordinates.
6941  *
6942  * You should always use this function when writing generic code that
6943  * walks up a window hierarchy.
6944  *
6945  * See also: gdk_window_coords_from_parent()
6946  *
6947  * Since: 2.22
6948  **/
6949 void
6950 gdk_window_coords_to_parent (GdkWindow *window,
6951                              gdouble    x,
6952                              gdouble    y,
6953                              gdouble   *parent_x,
6954                              gdouble   *parent_y)
6955 {
6956   g_return_if_fail (GDK_IS_WINDOW (window));
6957
6958   if (gdk_window_is_offscreen (window))
6959     {
6960       gdouble px, py;
6961
6962       to_embedder (window, x, y, &px, &py);
6963
6964       if (parent_x)
6965         *parent_x = px;
6966
6967       if (parent_y)
6968         *parent_y = py;
6969     }
6970   else
6971     {
6972       if (parent_x)
6973         *parent_x = x + window->x;
6974
6975       if (parent_y)
6976         *parent_y = y + window->y;
6977     }
6978 }
6979
6980 /**
6981  * gdk_window_coords_from_parent:
6982  * @window: a child window
6983  * @parent_x: X coordinate in parent's coordinate system
6984  * @parent_y: Y coordinate in parent's coordinate system
6985  * @x: (out) (allow-none): return location for X coordinate in child's coordinate system
6986  * @y: (out) (allow-none): return location for Y coordinate in child's coordinate system
6987  *
6988  * Transforms window coordinates from a parent window to a child
6989  * window, where the parent window is the normal parent as returned by
6990  * gdk_window_get_parent() for normal windows, and the window's
6991  * embedder as returned by gdk_offscreen_window_get_embedder() for
6992  * offscreen windows.
6993  *
6994  * For normal windows, calling this function is equivalent to subtracting
6995  * the return values of gdk_window_get_position() from the parent coordinates.
6996  * For offscreen windows however (which can be arbitrarily transformed),
6997  * this function calls the GdkWindow::from-embedder: signal to translate
6998  * the coordinates.
6999  *
7000  * You should always use this function when writing generic code that
7001  * walks down a window hierarchy.
7002  *
7003  * See also: gdk_window_coords_to_parent()
7004  *
7005  * Since: 2.22
7006  **/
7007 void
7008 gdk_window_coords_from_parent (GdkWindow *window,
7009                                gdouble    parent_x,
7010                                gdouble    parent_y,
7011                                gdouble   *x,
7012                                gdouble   *y)
7013 {
7014   g_return_if_fail (GDK_IS_WINDOW (window));
7015
7016   if (gdk_window_is_offscreen (window))
7017     {
7018       gdouble cx, cy;
7019
7020       from_embedder (window, parent_x, parent_y, &cx, &cy);
7021
7022       if (x)
7023         *x = cx;
7024
7025       if (y)
7026         *y = cy;
7027     }
7028   else
7029     {
7030       if (x)
7031         *x = parent_x - window->x;
7032
7033       if (y)
7034         *y = parent_y - window->y;
7035     }
7036 }
7037
7038 /**
7039  * gdk_window_shape_combine_region:
7040  * @window: a #GdkWindow
7041  * @shape_region: (allow-none): region of window to be non-transparent
7042  * @offset_x: X position of @shape_region in @window coordinates
7043  * @offset_y: Y position of @shape_region in @window coordinates
7044  *
7045  * Makes pixels in @window outside @shape_region be transparent,
7046  * so that the window may be nonrectangular.
7047  *
7048  * If @shape_region is %NULL, the shape will be unset, so the whole
7049  * window will be opaque again. @offset_x and @offset_y are ignored
7050  * if @shape_region is %NULL.
7051  *
7052  * On the X11 platform, this uses an X server extension which is
7053  * widely available on most common platforms, but not available on
7054  * very old X servers, and occasionally the implementation will be
7055  * buggy. On servers without the shape extension, this function
7056  * will do nothing.
7057  *
7058  * This function works on both toplevel and child windows.
7059  */
7060 void
7061 gdk_window_shape_combine_region (GdkWindow       *window,
7062                                  const cairo_region_t *shape_region,
7063                                  gint             offset_x,
7064                                  gint             offset_y)
7065 {
7066   cairo_region_t *old_region, *new_region, *diff;
7067
7068   g_return_if_fail (GDK_IS_WINDOW (window));
7069
7070   if (GDK_WINDOW_DESTROYED (window))
7071     return;
7072
7073   if (!window->shape && shape_region == NULL)
7074     return;
7075
7076   window->shaped = (shape_region != NULL);
7077
7078   if (window->shape)
7079     cairo_region_destroy (window->shape);
7080
7081   old_region = NULL;
7082   if (GDK_WINDOW_IS_MAPPED (window))
7083     old_region = cairo_region_copy (window->clip_region);
7084
7085   if (shape_region)
7086     {
7087       window->shape = cairo_region_copy (shape_region);
7088       cairo_region_translate (window->shape, offset_x, offset_y);
7089     }
7090   else
7091     window->shape = NULL;
7092
7093   recompute_visible_regions (window, TRUE, FALSE);
7094
7095   if (gdk_window_has_impl (window) &&
7096       !should_apply_clip_as_shape (window))
7097     apply_shape (window, window->shape);
7098
7099   if (old_region)
7100     {
7101       new_region = cairo_region_copy (window->clip_region);
7102
7103       /* New area in the window, needs invalidation */
7104       diff = cairo_region_copy (new_region);
7105       cairo_region_subtract (diff, old_region);
7106
7107       gdk_window_invalidate_region_full (window, diff, TRUE, CLEAR_BG_ALL);
7108
7109       cairo_region_destroy (diff);
7110
7111       if (!gdk_window_is_toplevel (window))
7112         {
7113           /* New area in the non-root parent window, needs invalidation */
7114           diff = cairo_region_copy (old_region);
7115           cairo_region_subtract (diff, new_region);
7116
7117           /* Adjust region to parent window coords */
7118           cairo_region_translate (diff, window->x, window->y);
7119
7120           gdk_window_invalidate_region_full (window->parent, diff, TRUE, CLEAR_BG_ALL);
7121
7122           cairo_region_destroy (diff);
7123         }
7124
7125       cairo_region_destroy (new_region);
7126       cairo_region_destroy (old_region);
7127     }
7128 }
7129
7130 static void
7131 do_child_shapes (GdkWindow *window,
7132                  gboolean merge)
7133 {
7134   GdkRectangle r;
7135   cairo_region_t *region;
7136
7137   r.x = 0;
7138   r.y = 0;
7139   r.width = window->width;
7140   r.height = window->height;
7141
7142   region = cairo_region_create_rectangle (&r);
7143   remove_child_area (window, NULL, FALSE, region);
7144
7145   if (merge && window->shape)
7146     cairo_region_subtract (region, window->shape);
7147
7148   gdk_window_shape_combine_region (window, region, 0, 0);
7149 }
7150
7151 /**
7152  * gdk_window_set_child_shapes:
7153  * @window: a #GdkWindow
7154  *
7155  * Sets the shape mask of @window to the union of shape masks
7156  * for all children of @window, ignoring the shape mask of @window
7157  * itself. Contrast with gdk_window_merge_child_shapes() which includes
7158  * the shape mask of @window in the masks to be merged.
7159  **/
7160 void
7161 gdk_window_set_child_shapes (GdkWindow *window)
7162 {
7163   g_return_if_fail (GDK_IS_WINDOW (window));
7164
7165   do_child_shapes (window, FALSE);
7166 }
7167
7168 /**
7169  * gdk_window_merge_child_shapes:
7170  * @window: a #GdkWindow
7171  *
7172  * Merges the shape masks for any child windows into the
7173  * shape mask for @window. i.e. the union of all masks
7174  * for @window and its children will become the new mask
7175  * for @window. See gdk_window_shape_combine_region().
7176  *
7177  * This function is distinct from gdk_window_set_child_shapes()
7178  * because it includes @window's shape mask in the set of shapes to
7179  * be merged.
7180  */
7181 void
7182 gdk_window_merge_child_shapes (GdkWindow *window)
7183 {
7184   g_return_if_fail (GDK_IS_WINDOW (window));
7185
7186   do_child_shapes (window, TRUE);
7187 }
7188
7189 /**
7190  * gdk_window_input_shape_combine_region:
7191  * @window: a #GdkWindow
7192  * @shape_region: region of window to be non-transparent
7193  * @offset_x: X position of @shape_region in @window coordinates
7194  * @offset_y: Y position of @shape_region in @window coordinates
7195  *
7196  * Like gdk_window_shape_combine_region(), but the shape applies
7197  * only to event handling. Mouse events which happen while
7198  * the pointer position corresponds to an unset bit in the
7199  * mask will be passed on the window below @window.
7200  *
7201  * An input shape is typically used with RGBA windows.
7202  * The alpha channel of the window defines which pixels are
7203  * invisible and allows for nicely antialiased borders,
7204  * and the input shape controls where the window is
7205  * "clickable".
7206  *
7207  * On the X11 platform, this requires version 1.1 of the
7208  * shape extension.
7209  *
7210  * On the Win32 platform, this functionality is not present and the
7211  * function does nothing.
7212  *
7213  * Since: 2.10
7214  */
7215 void
7216 gdk_window_input_shape_combine_region (GdkWindow       *window,
7217                                        const cairo_region_t *shape_region,
7218                                        gint             offset_x,
7219                                        gint             offset_y)
7220 {
7221   GdkWindowImplClass *impl_class;
7222
7223   g_return_if_fail (GDK_IS_WINDOW (window));
7224
7225   if (GDK_WINDOW_DESTROYED (window))
7226     return;
7227
7228   if (window->input_shape)
7229     cairo_region_destroy (window->input_shape);
7230
7231   if (shape_region)
7232     {
7233       window->input_shape = cairo_region_copy (shape_region);
7234       cairo_region_translate (window->input_shape, offset_x, offset_y);
7235     }
7236   else
7237     window->input_shape = NULL;
7238
7239   if (gdk_window_has_impl (window))
7240     {
7241       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7242       impl_class->input_shape_combine_region (window, window->input_shape, 0, 0);
7243     }
7244
7245   /* Pointer may have e.g. moved outside window due to the input mask change */
7246   _gdk_synthesize_crossing_events_for_geometry_change (window);
7247 }
7248
7249 static void
7250 do_child_input_shapes (GdkWindow *window,
7251                        gboolean merge)
7252 {
7253   GdkRectangle r;
7254   cairo_region_t *region;
7255
7256   r.x = 0;
7257   r.y = 0;
7258   r.width = window->width;
7259   r.height = window->height;
7260
7261   region = cairo_region_create_rectangle (&r);
7262   remove_child_area (window, NULL, TRUE, region);
7263
7264   if (merge && window->shape)
7265     cairo_region_subtract (region, window->shape);
7266   if (merge && window->input_shape)
7267     cairo_region_subtract (region, window->input_shape);
7268
7269   gdk_window_input_shape_combine_region (window, region, 0, 0);
7270 }
7271
7272
7273 /**
7274  * gdk_window_set_child_input_shapes:
7275  * @window: a #GdkWindow
7276  *
7277  * Sets the input shape mask of @window to the union of input shape masks
7278  * for all children of @window, ignoring the input shape mask of @window
7279  * itself. Contrast with gdk_window_merge_child_input_shapes() which includes
7280  * the input shape mask of @window in the masks to be merged.
7281  *
7282  * Since: 2.10
7283  **/
7284 void
7285 gdk_window_set_child_input_shapes (GdkWindow *window)
7286 {
7287   g_return_if_fail (GDK_IS_WINDOW (window));
7288
7289   do_child_input_shapes (window, FALSE);
7290 }
7291
7292 /**
7293  * gdk_window_merge_child_input_shapes:
7294  * @window: a #GdkWindow
7295  *
7296  * Merges the input shape masks for any child windows into the
7297  * input shape mask for @window. i.e. the union of all input masks
7298  * for @window and its children will become the new input mask
7299  * for @window. See gdk_window_input_shape_combine_region().
7300  *
7301  * This function is distinct from gdk_window_set_child_input_shapes()
7302  * because it includes @window's input shape mask in the set of
7303  * shapes to be merged.
7304  *
7305  * Since: 2.10
7306  **/
7307 void
7308 gdk_window_merge_child_input_shapes (GdkWindow *window)
7309 {
7310   g_return_if_fail (GDK_IS_WINDOW (window));
7311
7312   do_child_input_shapes (window, TRUE);
7313 }
7314
7315
7316 /**
7317  * gdk_window_set_static_gravities:
7318  * @window: a #GdkWindow
7319  * @use_static: %TRUE to turn on static gravity
7320  *
7321  * Set the bit gravity of the given window to static, and flag it so
7322  * all children get static subwindow gravity. This is used if you are
7323  * implementing scary features that involve deep knowledge of the
7324  * windowing system. Don't worry about it unless you have to.
7325  *
7326  * Return value: %TRUE if the server supports static gravity
7327  */
7328 gboolean
7329 gdk_window_set_static_gravities (GdkWindow *window,
7330                                  gboolean   use_static)
7331 {
7332   GdkWindowImplClass *impl_class;
7333
7334   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7335
7336   if (gdk_window_has_impl (window))
7337     {
7338       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7339       return impl_class->set_static_gravities (window, use_static);
7340     }
7341
7342   return FALSE;
7343 }
7344
7345 /**
7346  * gdk_window_get_composited:
7347  * @window: a #GdkWindow
7348  *
7349  * Determines whether @window is composited.
7350  *
7351  * See gdk_window_set_composited().
7352  *
7353  * Returns: %TRUE if the window is composited.
7354  *
7355  * Since: 2.22
7356  **/
7357 gboolean
7358 gdk_window_get_composited (GdkWindow *window)
7359 {
7360   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7361
7362   return window->composited;
7363 }
7364
7365 /**
7366  * gdk_window_set_composited:
7367  * @window: a #GdkWindow
7368  * @composited: %TRUE to set the window as composited
7369  *
7370  * Sets a #GdkWindow as composited, or unsets it. Composited
7371  * windows do not automatically have their contents drawn to
7372  * the screen. Drawing is redirected to an offscreen buffer
7373  * and an expose event is emitted on the parent of the composited
7374  * window. It is the responsibility of the parent's expose handler
7375  * to manually merge the off-screen content onto the screen in
7376  * whatever way it sees fit. See <xref linkend="composited-window-example"/>
7377  * for an example.
7378  *
7379  * It only makes sense for child windows to be composited; see
7380  * gdk_window_set_opacity() if you need translucent toplevel
7381  * windows.
7382  *
7383  * An additional effect of this call is that the area of this
7384  * window is no longer clipped from regions marked for
7385  * invalidation on its parent. Draws done on the parent
7386  * window are also no longer clipped by the child.
7387  *
7388  * This call is only supported on some systems (currently,
7389  * only X11 with new enough Xcomposite and Xdamage extensions).
7390  * You must call gdk_display_supports_composite() to check if
7391  * setting a window as composited is supported before
7392  * attempting to do so.
7393  *
7394  * Since: 2.12
7395  */
7396 void
7397 gdk_window_set_composited (GdkWindow *window,
7398                            gboolean   composited)
7399 {
7400   GdkDisplay *display;
7401   GdkWindowImplClass *impl_class;
7402
7403   g_return_if_fail (GDK_IS_WINDOW (window));
7404
7405   composited = composited != FALSE;
7406
7407   if (window->composited == composited)
7408     return;
7409
7410   if (composited)
7411     gdk_window_ensure_native (window);
7412
7413   display = gdk_window_get_display (window);
7414
7415   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7416
7417   if (composited && (!gdk_display_supports_composite (display) || !impl_class->set_composited))
7418     {
7419       g_warning ("gdk_window_set_composited called but "
7420                  "compositing is not supported");
7421       return;
7422     }
7423
7424   impl_class->set_composited (window, composited);
7425
7426   recompute_visible_regions (window, TRUE, FALSE);
7427
7428   if (GDK_WINDOW_IS_MAPPED (window))
7429     gdk_window_invalidate_in_parent (window);
7430
7431   window->composited = composited;
7432 }
7433
7434 /**
7435  * gdk_window_get_modal_hint:
7436  * @window: A toplevel #GdkWindow.
7437  *
7438  * Determines whether or not the window manager is hinted that @window
7439  * has modal behaviour.
7440  *
7441  * Return value: whether or not the window has the modal hint set.
7442  *
7443  * Since: 2.22
7444  */
7445 gboolean
7446 gdk_window_get_modal_hint (GdkWindow *window)
7447 {
7448   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7449
7450   return window->modal_hint;
7451 }
7452
7453 /**
7454  * gdk_window_get_accept_focus:
7455  * @window: a toplevel #GdkWindow.
7456  *
7457  * Determines whether or not the desktop environment shuld be hinted that
7458  * the window does not want to receive input focus.
7459  *
7460  * Return value: whether or not the window should receive input focus.
7461  *
7462  * Since: 2.22
7463  */
7464 gboolean
7465 gdk_window_get_accept_focus (GdkWindow *window)
7466 {
7467   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7468
7469   return window->accept_focus;
7470 }
7471
7472 /**
7473  * gdk_window_get_focus_on_map:
7474  * @window: a toplevel #GdkWindow.
7475  *
7476  * Determines whether or not the desktop environment should be hinted that the
7477  * window does not want to receive input focus when it is mapped.
7478  *
7479  * Return value: whether or not the window wants to receive input focus when
7480  * it is mapped.
7481  *
7482  * Since: 2.22
7483  */
7484 gboolean
7485 gdk_window_get_focus_on_map (GdkWindow *window)
7486 {
7487   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7488
7489   return window->focus_on_map;
7490 }
7491
7492 /**
7493  * gdk_window_is_input_only:
7494  * @window: a toplevel #GdkWindow
7495  *
7496  * Determines whether or not the window is an input only window.
7497  *
7498  * Return value: %TRUE if @window is input only
7499  *
7500  * Since: 2.22
7501  */
7502 gboolean
7503 gdk_window_is_input_only (GdkWindow *window)
7504 {
7505   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7506
7507   return window->input_only;
7508 }
7509
7510 /**
7511  * gdk_window_is_shaped:
7512  * @window: a toplevel #GdkWindow
7513  *
7514  * Determines whether or not the window is shaped.
7515  *
7516  * Return value: %TRUE if @window is shaped
7517  *
7518  * Since: 2.22
7519  */
7520 gboolean
7521 gdk_window_is_shaped (GdkWindow *window)
7522 {
7523   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7524
7525   return window->shaped;
7526 }
7527
7528 static void
7529 window_get_size_rectangle (GdkWindow    *window,
7530                            GdkRectangle *rect)
7531 {
7532   rect->x = rect->y = 0;
7533   rect->width = window->width;
7534   rect->height = window->height;
7535 }
7536
7537 /* Calculates the real clipping region for a window, in window coordinates,
7538  * taking into account other windows, gc clip region and gc clip mask.
7539  */
7540 cairo_region_t *
7541 _gdk_window_calculate_full_clip_region (GdkWindow *window,
7542                                         GdkWindow *base_window,
7543                                         gboolean   do_children,
7544                                         gint      *base_x_offset,
7545                                         gint      *base_y_offset)
7546 {
7547   GdkRectangle visible_rect;
7548   cairo_region_t *real_clip_region;
7549   gint x_offset, y_offset;
7550   GdkWindow *parentwin, *lastwin;
7551
7552   if (base_x_offset)
7553     *base_x_offset = 0;
7554   if (base_y_offset)
7555     *base_y_offset = 0;
7556
7557   if (!window->viewable || window->input_only)
7558     return cairo_region_create ();
7559
7560   window_get_size_rectangle (window, &visible_rect);
7561
7562   /* real_clip_region is in window coordinates */
7563   real_clip_region = cairo_region_create_rectangle (&visible_rect);
7564
7565   x_offset = y_offset = 0;
7566
7567   lastwin = window;
7568   if (do_children)
7569     parentwin = lastwin;
7570   else
7571     parentwin = lastwin->parent;
7572
7573   /* Remove the areas of all overlapping windows above parentwin in the hiearachy */
7574   for (; parentwin != NULL &&
7575          (parentwin == window || lastwin != base_window);
7576        lastwin = parentwin, parentwin = lastwin->parent)
7577     {
7578       GList *cur;
7579       GdkRectangle real_clip_rect;
7580
7581       if (parentwin != window)
7582         {
7583           x_offset += lastwin->x;
7584           y_offset += lastwin->y;
7585         }
7586
7587       /* children is ordered in reverse stack order */
7588       for (cur = parentwin->children;
7589            cur && cur->data != lastwin;
7590            cur = cur->next)
7591         {
7592           GdkWindow *child = cur->data;
7593
7594           if (!GDK_WINDOW_IS_MAPPED (child) || child->input_only)
7595             continue;
7596
7597           /* Ignore offscreen children, as they don't draw in their parent and
7598            * don't take part in the clipping */
7599           if (gdk_window_is_offscreen (child))
7600             continue;
7601
7602           window_get_size_rectangle (child, &visible_rect);
7603
7604           /* Convert rect to "window" coords */
7605           visible_rect.x += child->x - x_offset;
7606           visible_rect.y += child->y - y_offset;
7607
7608           /* This shortcut is really necessary for performance when there are a lot of windows */
7609           cairo_region_get_extents (real_clip_region, &real_clip_rect);
7610           if (visible_rect.x >= real_clip_rect.x + real_clip_rect.width ||
7611               visible_rect.x + visible_rect.width <= real_clip_rect.x ||
7612               visible_rect.y >= real_clip_rect.y + real_clip_rect.height ||
7613               visible_rect.y + visible_rect.height <= real_clip_rect.y)
7614             continue;
7615
7616           cairo_region_subtract_rectangle (real_clip_region, &visible_rect);
7617         }
7618
7619       /* Clip to the parent */
7620       window_get_size_rectangle ((GdkWindow *)parentwin, &visible_rect);
7621       /* Convert rect to "window" coords */
7622       visible_rect.x += - x_offset;
7623       visible_rect.y += - y_offset;
7624
7625       cairo_region_intersect_rectangle (real_clip_region, &visible_rect);
7626     }
7627
7628   if (base_x_offset)
7629     *base_x_offset = x_offset;
7630   if (base_y_offset)
7631     *base_y_offset = y_offset;
7632
7633   return real_clip_region;
7634 }
7635
7636 void
7637 _gdk_window_add_damage (GdkWindow *toplevel,
7638                         cairo_region_t *damaged_region)
7639 {
7640   GdkDisplay *display;
7641   GdkEvent event = { 0, };
7642   event.expose.type = GDK_DAMAGE;
7643   event.expose.window = toplevel;
7644   event.expose.send_event = FALSE;
7645   event.expose.region = damaged_region;
7646   cairo_region_get_extents (event.expose.region, &event.expose.area);
7647   display = gdk_window_get_display (event.expose.window);
7648   _gdk_event_queue_append (display, gdk_event_copy (&event));
7649 }
7650
7651 /* Gets the toplevel for a window as used for events,
7652    i.e. including offscreen parents */
7653 static GdkWindow *
7654 get_event_parent (GdkWindow *window)
7655 {
7656   if (gdk_window_is_offscreen (window))
7657     return gdk_offscreen_window_get_embedder ((GdkWindow *)window);
7658   else
7659     return window->parent;
7660 }
7661
7662 /* Gets the toplevel for a window as used for events,
7663    i.e. including offscreen parents going up to the native
7664    toplevel */
7665 static GdkWindow *
7666 get_event_toplevel (GdkWindow *window)
7667 {
7668   GdkWindow *parent;
7669
7670   while ((parent = get_event_parent (window)) != NULL &&
7671          (parent->window_type != GDK_WINDOW_ROOT))
7672     window = parent;
7673
7674   return window;
7675 }
7676
7677 gboolean
7678 _gdk_window_event_parent_of (GdkWindow *parent,
7679                              GdkWindow *child)
7680 {
7681   GdkWindow *w;
7682
7683   w = child;
7684   while (w != NULL)
7685     {
7686       if (w == parent)
7687         return TRUE;
7688
7689       w = get_event_parent (w);
7690     }
7691
7692   return FALSE;
7693 }
7694
7695 static void
7696 update_cursor (GdkDisplay *display,
7697                GdkDevice  *device)
7698 {
7699   GdkWindow *cursor_window, *parent, *toplevel;
7700   GdkWindow *pointer_window;
7701   GdkWindowImplClass *impl_class;
7702   GdkPointerWindowInfo *pointer_info;
7703   GdkDeviceGrabInfo *grab;
7704   GdkCursor *cursor;
7705
7706   pointer_info = _gdk_display_get_pointer_info (display, device);
7707   pointer_window = pointer_info->window_under_pointer;
7708
7709   /* We ignore the serials here and just pick the last grab
7710      we've sent, as that would shortly be used anyway. */
7711   grab = _gdk_display_get_last_device_grab (display, device);
7712   if (/* have grab */
7713       grab != NULL &&
7714       /* the pointer is not in a descendant of the grab window */
7715       !_gdk_window_event_parent_of (grab->window, pointer_window))
7716     {
7717       /* use the cursor from the grab window */
7718       cursor_window = grab->window;
7719     }
7720   else
7721     {
7722       /* otherwise use the cursor from the pointer window */
7723       cursor_window = pointer_window;
7724     }
7725
7726   /* Find the first window with the cursor actually set, as
7727      the cursor is inherited from the parent */
7728   while (cursor_window->cursor == NULL &&
7729          (parent = get_event_parent (cursor_window)) != NULL &&
7730          parent->window_type != GDK_WINDOW_ROOT)
7731     cursor_window = parent;
7732
7733   cursor = g_hash_table_lookup (cursor_window->device_cursor, device);
7734
7735   if (!cursor)
7736     cursor = cursor_window->cursor;
7737
7738   /* Set all cursors on toplevel, otherwise its tricky to keep track of
7739    * which native window has what cursor set. */
7740   toplevel = get_event_toplevel (pointer_window);
7741   impl_class = GDK_WINDOW_IMPL_GET_CLASS (toplevel->impl);
7742   impl_class->set_device_cursor (toplevel, device, cursor);
7743 }
7744
7745 static gboolean
7746 point_in_window (GdkWindow *window,
7747                  gdouble    x,
7748                  gdouble    y)
7749 {
7750   return
7751     x >= 0 && x < window->width &&
7752     y >= 0 && y < window->height &&
7753     (window->shape == NULL ||
7754      cairo_region_contains_point (window->shape,
7755                           x, y)) &&
7756     (window->input_shape == NULL ||
7757      cairo_region_contains_point (window->input_shape,
7758                           x, y));
7759 }
7760
7761 static GdkWindow *
7762 convert_native_coords_to_toplevel (GdkWindow *window,
7763                                    gdouble    child_x,
7764                                    gdouble    child_y,
7765                                    gdouble   *toplevel_x,
7766                                    gdouble   *toplevel_y)
7767 {
7768   gdouble x, y;
7769
7770   x = child_x;
7771   y = child_y;
7772
7773   while (!gdk_window_is_toplevel (window))
7774     {
7775       x += window->x;
7776       y += window->y;
7777       window = window->parent;
7778     }
7779
7780   *toplevel_x = x;
7781   *toplevel_y = y;
7782
7783   return window;
7784 }
7785
7786 static void
7787 convert_toplevel_coords_to_window (GdkWindow *window,
7788                                    gdouble    toplevel_x,
7789                                    gdouble    toplevel_y,
7790                                    gdouble   *window_x,
7791                                    gdouble   *window_y)
7792 {
7793   GdkWindow *parent;
7794   gdouble x, y;
7795   GList *children, *l;
7796
7797   x = toplevel_x;
7798   y = toplevel_y;
7799
7800   children = NULL;
7801   while ((parent = get_event_parent (window)) != NULL &&
7802          (parent->window_type != GDK_WINDOW_ROOT))
7803     {
7804       children = g_list_prepend (children, window);
7805       window = parent;
7806     }
7807
7808   for (l = children; l != NULL; l = l->next)
7809     gdk_window_coords_from_parent (l->data, x, y, &x, &y);
7810
7811   g_list_free (children);
7812
7813   *window_x = x;
7814   *window_y = y;
7815 }
7816
7817 static GdkWindow *
7818 pick_embedded_child (GdkWindow *window,
7819                      gdouble    x,
7820                      gdouble    y)
7821 {
7822   GdkWindow *res;
7823
7824   res = NULL;
7825   g_signal_emit (window,
7826                  signals[PICK_EMBEDDED_CHILD], 0,
7827                  x, y, &res);
7828
7829   return res;
7830 }
7831
7832 GdkWindow *
7833 _gdk_window_find_child_at (GdkWindow *window,
7834                            int        x,
7835                            int        y)
7836 {
7837   GdkWindow *sub;
7838   double child_x, child_y;
7839   GList *l;
7840
7841   if (point_in_window (window, x, y))
7842     {
7843       /* Children is ordered in reverse stack order, i.e. first is topmost */
7844       for (l = window->children; l != NULL; l = l->next)
7845         {
7846           sub = l->data;
7847
7848           if (!GDK_WINDOW_IS_MAPPED (sub))
7849             continue;
7850
7851           gdk_window_coords_from_parent ((GdkWindow *)sub,
7852                                          x, y,
7853                                          &child_x, &child_y);
7854           if (point_in_window (sub, child_x, child_y))
7855             return (GdkWindow *)sub;
7856         }
7857
7858       if (window->num_offscreen_children > 0)
7859         {
7860           sub = pick_embedded_child (window,
7861                                      x, y);
7862           if (sub)
7863             return (GdkWindow *)sub;
7864         }
7865     }
7866
7867   return NULL;
7868 }
7869
7870 GdkWindow *
7871 _gdk_window_find_descendant_at (GdkWindow *window,
7872                                 gdouble    x,
7873                                 gdouble    y,
7874                                 gdouble   *found_x,
7875                                 gdouble   *found_y)
7876 {
7877   GdkWindow *sub;
7878   gdouble child_x, child_y;
7879   GList *l;
7880   gboolean found;
7881
7882   if (point_in_window (window, x, y))
7883     {
7884       do
7885         {
7886           found = FALSE;
7887           /* Children is ordered in reverse stack order, i.e. first is topmost */
7888           for (l = window->children; l != NULL; l = l->next)
7889             {
7890               sub = l->data;
7891
7892               if (!GDK_WINDOW_IS_MAPPED (sub))
7893                 continue;
7894
7895               gdk_window_coords_from_parent ((GdkWindow *)sub,
7896                                              x, y,
7897                                              &child_x, &child_y);
7898               if (point_in_window (sub, child_x, child_y))
7899                 {
7900                   x = child_x;
7901                   y = child_y;
7902                   window = sub;
7903                   found = TRUE;
7904                   break;
7905                 }
7906             }
7907           if (!found &&
7908               window->num_offscreen_children > 0)
7909             {
7910               sub = pick_embedded_child (window,
7911                                          x, y);
7912               if (sub)
7913                 {
7914                   found = TRUE;
7915                   window = sub;
7916                   from_embedder (sub, x, y, &x, &y);
7917                 }
7918             }
7919         }
7920       while (found);
7921     }
7922   else
7923     {
7924       /* Not in window at all */
7925       window = NULL;
7926     }
7927
7928   if (found_x)
7929     *found_x = x;
7930   if (found_y)
7931     *found_y = y;
7932
7933   return window;
7934 }
7935
7936 /**
7937  * gdk_window_beep:
7938  * @window: a toplevel #GdkWindow
7939  *
7940  * Emits a short beep associated to @window in the appropriate
7941  * display, if supported. Otherwise, emits a short beep on
7942  * the display just as gdk_display_beep().
7943  *
7944  * Since: 2.12
7945  **/
7946 void
7947 gdk_window_beep (GdkWindow *window)
7948 {
7949   GdkDisplay *display;
7950   GdkWindow *toplevel;
7951
7952   g_return_if_fail (GDK_IS_WINDOW (window));
7953
7954   if (GDK_WINDOW_DESTROYED (window))
7955     return;
7956
7957   toplevel = get_event_toplevel (window);
7958   display = gdk_window_get_display (window);
7959
7960   if (toplevel)
7961     {
7962       if (GDK_WINDOW_IMPL_GET_CLASS (toplevel->impl)->beep (toplevel))
7963         return;
7964     }
7965   
7966   /* If windows fail to beep, we beep the display. */
7967   gdk_display_beep (display);
7968 }
7969
7970 /**
7971  * gdk_window_set_support_multidevice:
7972  * @window: a #GdkWindow.
7973  * @support_multidevice: %TRUE to enable multidevice support in @window.
7974  *
7975  * This function will enable multidevice features in @window.
7976  *
7977  * Multidevice aware windows will need to handle properly multiple,
7978  * per device enter/leave events, device grabs and grab ownerships.
7979  *
7980  * Since: 3.0
7981  **/
7982 void
7983 gdk_window_set_support_multidevice (GdkWindow *window,
7984                                     gboolean   support_multidevice)
7985 {
7986   g_return_if_fail (GDK_IS_WINDOW (window));
7987
7988   if (GDK_WINDOW_DESTROYED (window))
7989     return;
7990
7991   if (window->support_multidevice == support_multidevice)
7992     return;
7993
7994   window->support_multidevice = support_multidevice;
7995
7996   /* FIXME: What to do if called when some pointers are inside the window ? */
7997 }
7998
7999 /**
8000  * gdk_window_get_support_multidevice:
8001  * @window: a #GdkWindow.
8002  *
8003  * Returns %TRUE if the window is aware of the existence of multiple
8004  * devices.
8005  *
8006  * Returns: %TRUE if the window handles multidevice features.
8007  *
8008  * Since: 3.0
8009  **/
8010 gboolean
8011 gdk_window_get_support_multidevice (GdkWindow *window)
8012 {
8013   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
8014
8015   if (GDK_WINDOW_DESTROYED (window))
8016     return FALSE;
8017
8018   return window->support_multidevice;
8019 }
8020
8021 static const guint type_masks[] = {
8022   GDK_SUBSTRUCTURE_MASK, /* GDK_DELETE                 = 0  */
8023   GDK_STRUCTURE_MASK, /* GDK_DESTROY                   = 1  */
8024   GDK_EXPOSURE_MASK, /* GDK_EXPOSE                     = 2  */
8025   GDK_POINTER_MOTION_MASK, /* GDK_MOTION_NOTIFY        = 3  */
8026   GDK_BUTTON_PRESS_MASK, /* GDK_BUTTON_PRESS           = 4  */
8027   GDK_BUTTON_PRESS_MASK, /* GDK_2BUTTON_PRESS          = 5  */
8028   GDK_BUTTON_PRESS_MASK, /* GDK_3BUTTON_PRESS          = 6  */
8029   GDK_BUTTON_RELEASE_MASK, /* GDK_BUTTON_RELEASE       = 7  */
8030   GDK_KEY_PRESS_MASK, /* GDK_KEY_PRESS                 = 8  */
8031   GDK_KEY_RELEASE_MASK, /* GDK_KEY_RELEASE             = 9  */
8032   GDK_ENTER_NOTIFY_MASK, /* GDK_ENTER_NOTIFY           = 10 */
8033   GDK_LEAVE_NOTIFY_MASK, /* GDK_LEAVE_NOTIFY           = 11 */
8034   GDK_FOCUS_CHANGE_MASK, /* GDK_FOCUS_CHANGE           = 12 */
8035   GDK_STRUCTURE_MASK, /* GDK_CONFIGURE                 = 13 */
8036   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_MAP               = 14 */
8037   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_UNMAP             = 15 */
8038   GDK_PROPERTY_CHANGE_MASK, /* GDK_PROPERTY_NOTIFY     = 16 */
8039   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_CLEAR     = 17 */
8040   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_REQUEST   = 18 */
8041   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_NOTIFY    = 19 */
8042   GDK_PROXIMITY_IN_MASK, /* GDK_PROXIMITY_IN           = 20 */
8043   GDK_PROXIMITY_OUT_MASK, /* GDK_PROXIMITY_OUT         = 21 */
8044   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_ENTER               = 22 */
8045   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_LEAVE               = 23 */
8046   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_MOTION              = 24 */
8047   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_STATUS              = 25 */
8048   GDK_ALL_EVENTS_MASK, /* GDK_DROP_START               = 26 */
8049   GDK_ALL_EVENTS_MASK, /* GDK_DROP_FINISHED            = 27 */
8050   GDK_ALL_EVENTS_MASK, /* GDK_CLIENT_EVENT             = 28 */
8051   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_VISIBILITY_NOTIFY = 29 */
8052   GDK_EXPOSURE_MASK, /* GDK_NO_EXPOSE                  = 30 */
8053   GDK_SCROLL_MASK | GDK_BUTTON_PRESS_MASK,/* GDK_SCROLL= 31 */
8054   0, /* GDK_WINDOW_STATE = 32 */
8055   0, /* GDK_SETTING = 33 */
8056   0, /* GDK_OWNER_CHANGE = 34 */
8057   0, /* GDK_GRAB_BROKEN = 35 */
8058   0, /* GDK_DAMAGE = 36 */
8059 };
8060 G_STATIC_ASSERT (G_N_ELEMENTS (type_masks) == GDK_EVENT_LAST);
8061
8062 /* send motion events if the right buttons are down */
8063 static guint
8064 update_evmask_for_button_motion (guint           evmask,
8065                                  GdkModifierType mask)
8066 {
8067   if (evmask & GDK_BUTTON_MOTION_MASK &&
8068       mask & (GDK_BUTTON1_MASK |
8069               GDK_BUTTON2_MASK |
8070               GDK_BUTTON3_MASK |
8071               GDK_BUTTON4_MASK |
8072               GDK_BUTTON5_MASK))
8073     evmask |= GDK_POINTER_MOTION_MASK;
8074
8075   if ((evmask & GDK_BUTTON1_MOTION_MASK && mask & GDK_BUTTON1_MASK) ||
8076       (evmask & GDK_BUTTON2_MOTION_MASK && mask & GDK_BUTTON2_MASK) ||
8077       (evmask & GDK_BUTTON3_MOTION_MASK && mask & GDK_BUTTON3_MASK))
8078     evmask |= GDK_POINTER_MOTION_MASK;
8079
8080   return evmask;
8081 }
8082
8083 static gboolean
8084 is_button_type (GdkEventType type)
8085 {
8086   return type == GDK_BUTTON_PRESS ||
8087          type == GDK_2BUTTON_PRESS ||
8088          type == GDK_3BUTTON_PRESS ||
8089          type == GDK_BUTTON_RELEASE ||
8090          type == GDK_SCROLL;
8091 }
8092
8093 static gboolean
8094 is_motion_type (GdkEventType type)
8095 {
8096   return type == GDK_MOTION_NOTIFY ||
8097          type == GDK_ENTER_NOTIFY ||
8098          type == GDK_LEAVE_NOTIFY;
8099 }
8100
8101 static GdkWindow *
8102 find_common_ancestor (GdkWindow *win1,
8103                       GdkWindow *win2)
8104 {
8105   GdkWindow *tmp;
8106   GList *path1 = NULL, *path2 = NULL;
8107   GList *list1, *list2;
8108
8109   tmp = win1;
8110   while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
8111     {
8112       path1 = g_list_prepend (path1, tmp);
8113       tmp = get_event_parent (tmp);
8114     }
8115
8116   tmp = win2;
8117   while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
8118     {
8119       path2 = g_list_prepend (path2, tmp);
8120       tmp = get_event_parent (tmp);
8121     }
8122
8123   list1 = path1;
8124   list2 = path2;
8125   tmp = NULL;
8126   while (list1 && list2 && (list1->data == list2->data))
8127     {
8128       tmp = list1->data;
8129       list1 = g_list_next (list1);
8130       list2 = g_list_next (list2);
8131     }
8132   g_list_free (path1);
8133   g_list_free (path2);
8134
8135   return tmp;
8136 }
8137
8138 GdkEvent *
8139 _gdk_make_event (GdkWindow    *window,
8140                  GdkEventType  type,
8141                  GdkEvent     *event_in_queue,
8142                  gboolean      before_event)
8143 {
8144   GdkEvent *event = gdk_event_new (type);
8145   guint32 the_time;
8146   GdkModifierType the_state;
8147
8148   the_time = gdk_event_get_time (event_in_queue);
8149   gdk_event_get_state (event_in_queue, &the_state);
8150
8151   event->any.window = g_object_ref (window);
8152   event->any.send_event = FALSE;
8153   if (event_in_queue && event_in_queue->any.send_event)
8154     event->any.send_event = TRUE;
8155
8156   switch (type)
8157     {
8158     case GDK_MOTION_NOTIFY:
8159       event->motion.time = the_time;
8160       event->motion.axes = NULL;
8161       event->motion.state = the_state;
8162       break;
8163
8164     case GDK_BUTTON_PRESS:
8165     case GDK_2BUTTON_PRESS:
8166     case GDK_3BUTTON_PRESS:
8167     case GDK_BUTTON_RELEASE:
8168       event->button.time = the_time;
8169       event->button.axes = NULL;
8170       event->button.state = the_state;
8171       break;
8172
8173     case GDK_SCROLL:
8174       event->scroll.time = the_time;
8175       event->scroll.state = the_state;
8176       break;
8177
8178     case GDK_KEY_PRESS:
8179     case GDK_KEY_RELEASE:
8180       event->key.time = the_time;
8181       event->key.state = the_state;
8182       break;
8183
8184     case GDK_ENTER_NOTIFY:
8185     case GDK_LEAVE_NOTIFY:
8186       event->crossing.time = the_time;
8187       event->crossing.state = the_state;
8188       break;
8189
8190     case GDK_PROPERTY_NOTIFY:
8191       event->property.time = the_time;
8192       event->property.state = the_state;
8193       break;
8194
8195     case GDK_SELECTION_CLEAR:
8196     case GDK_SELECTION_REQUEST:
8197     case GDK_SELECTION_NOTIFY:
8198       event->selection.time = the_time;
8199       break;
8200
8201     case GDK_PROXIMITY_IN:
8202     case GDK_PROXIMITY_OUT:
8203       event->proximity.time = the_time;
8204       break;
8205
8206     case GDK_DRAG_ENTER:
8207     case GDK_DRAG_LEAVE:
8208     case GDK_DRAG_MOTION:
8209     case GDK_DRAG_STATUS:
8210     case GDK_DROP_START:
8211     case GDK_DROP_FINISHED:
8212       event->dnd.time = the_time;
8213       break;
8214
8215     case GDK_FOCUS_CHANGE:
8216     case GDK_CONFIGURE:
8217     case GDK_MAP:
8218     case GDK_UNMAP:
8219     case GDK_CLIENT_EVENT:
8220     case GDK_VISIBILITY_NOTIFY:
8221     case GDK_DELETE:
8222     case GDK_DESTROY:
8223     case GDK_EXPOSE:
8224     default:
8225       break;
8226     }
8227
8228   if (event_in_queue)
8229     {
8230     if (before_event)
8231       _gdk_event_queue_insert_before (gdk_window_get_display (window), event_in_queue, event);
8232     else
8233       _gdk_event_queue_insert_after (gdk_window_get_display (window), event_in_queue, event);
8234     }
8235   else
8236     _gdk_event_queue_append (gdk_window_get_display (window), event);
8237
8238   return event;
8239 }
8240
8241 static void
8242 send_crossing_event (GdkDisplay                 *display,
8243                      GdkWindow                  *toplevel,
8244                      GdkWindow                  *window,
8245                      GdkEventType                type,
8246                      GdkCrossingMode             mode,
8247                      GdkNotifyType               notify_type,
8248                      GdkWindow                  *subwindow,
8249                      GdkDevice                  *device,
8250                      GdkDevice                  *source_device,
8251                      gint                        toplevel_x,
8252                      gint                        toplevel_y,
8253                      GdkModifierType             mask,
8254                      guint32                     time_,
8255                      GdkEvent                   *event_in_queue,
8256                      gulong                      serial)
8257 {
8258   GdkEvent *event;
8259   guint32 window_event_mask, type_event_mask;
8260   GdkDeviceGrabInfo *grab;
8261   gboolean block_event = FALSE;
8262
8263   grab = _gdk_display_has_device_grab (display, device, serial);
8264
8265   if (grab != NULL &&
8266       !grab->owner_events)
8267     {
8268       /* !owner_event => only report events wrt grab window, ignore rest */
8269       if ((GdkWindow *)window != grab->window)
8270         return;
8271       window_event_mask = grab->event_mask;
8272     }
8273   else
8274     window_event_mask = window->event_mask;
8275
8276   if (type == GDK_LEAVE_NOTIFY)
8277     {
8278       type_event_mask = GDK_LEAVE_NOTIFY_MASK;
8279       window->devices_inside = g_list_remove (window->devices_inside, device);
8280
8281       if (!window->support_multidevice && window->devices_inside)
8282         {
8283           /* Block leave events unless it's the last pointer */
8284           block_event = TRUE;
8285         }
8286     }
8287   else
8288     {
8289       type_event_mask = GDK_ENTER_NOTIFY_MASK;
8290
8291       if (!window->support_multidevice && window->devices_inside)
8292         {
8293           /* Only emit enter events for the first device */
8294           block_event = TRUE;
8295         }
8296
8297       if (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER &&
8298           gdk_device_get_mode (device) != GDK_MODE_DISABLED &&
8299           !g_list_find (window->devices_inside, device))
8300         window->devices_inside = g_list_prepend (window->devices_inside, device);
8301     }
8302
8303   if (block_event)
8304     return;
8305
8306   if (window_event_mask & type_event_mask)
8307     {
8308       event = _gdk_make_event ((GdkWindow *)window, type, event_in_queue, TRUE);
8309       gdk_event_set_device (event, device);
8310
8311       if (source_device)
8312         gdk_event_set_source_device (event, source_device);
8313
8314       event->crossing.time = time_;
8315       event->crossing.subwindow = subwindow;
8316       if (subwindow)
8317         g_object_ref (subwindow);
8318       convert_toplevel_coords_to_window ((GdkWindow *)window,
8319                                          toplevel_x, toplevel_y,
8320                                          &event->crossing.x, &event->crossing.y);
8321       event->crossing.x_root = toplevel_x + toplevel->x;
8322       event->crossing.y_root = toplevel_y + toplevel->y;
8323       event->crossing.mode = mode;
8324       event->crossing.detail = notify_type;
8325       event->crossing.focus = FALSE;
8326       event->crossing.state = mask;
8327     }
8328 }
8329
8330
8331 /* The coordinates are in the toplevel window that src/dest are in.
8332  * src and dest are always (if != NULL) in the same toplevel, as
8333  * we get a leave-notify and set the window_under_pointer to null
8334  * before crossing to another toplevel.
8335  */
8336 void
8337 _gdk_synthesize_crossing_events (GdkDisplay                 *display,
8338                                  GdkWindow                  *src,
8339                                  GdkWindow                  *dest,
8340                                  GdkDevice                  *device,
8341                                  GdkDevice                  *source_device,
8342                                  GdkCrossingMode             mode,
8343                                  gint                        toplevel_x,
8344                                  gint                        toplevel_y,
8345                                  GdkModifierType             mask,
8346                                  guint32                     time_,
8347                                  GdkEvent                   *event_in_queue,
8348                                  gulong                      serial,
8349                                  gboolean                    non_linear)
8350 {
8351   GdkWindow *c;
8352   GdkWindow *win, *last, *next;
8353   GList *path, *list;
8354   GdkWindow *a;
8355   GdkWindow *b;
8356   GdkWindow *toplevel;
8357   GdkNotifyType notify_type;
8358
8359   /* TODO: Don't send events to toplevel, as we get those from the windowing system */
8360
8361   a = src;
8362   b = dest;
8363   if (src == dest)
8364     return; /* No crossings generated between src and dest */
8365
8366   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
8367     {
8368       if (a && gdk_window_get_device_events (src, device) == 0)
8369         a = NULL;
8370
8371       if (b && gdk_window_get_device_events (dest, device) == 0)
8372         b = NULL;
8373     }
8374
8375   if (!a && !b)
8376     return;
8377
8378   c = find_common_ancestor (a, b);
8379
8380   non_linear |= (c != a) && (c != b);
8381
8382   if (a) /* There might not be a source (i.e. if no previous pointer_in_window) */
8383     {
8384       toplevel = gdk_window_get_toplevel (a);
8385
8386       /* Traverse up from a to (excluding) c sending leave events */
8387       if (non_linear)
8388         notify_type = GDK_NOTIFY_NONLINEAR;
8389       else if (c == a)
8390         notify_type = GDK_NOTIFY_INFERIOR;
8391       else
8392         notify_type = GDK_NOTIFY_ANCESTOR;
8393       send_crossing_event (display, toplevel,
8394                            a, GDK_LEAVE_NOTIFY,
8395                            mode,
8396                            notify_type,
8397                            NULL, device, source_device,
8398                            toplevel_x, toplevel_y,
8399                            mask, time_,
8400                            event_in_queue,
8401                            serial);
8402
8403       if (c != a)
8404         {
8405           if (non_linear)
8406             notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
8407           else
8408             notify_type = GDK_NOTIFY_VIRTUAL;
8409
8410           last = a;
8411           win = get_event_parent (a);
8412           while (win != c && win->window_type != GDK_WINDOW_ROOT)
8413             {
8414               send_crossing_event (display, toplevel,
8415                                    win, GDK_LEAVE_NOTIFY,
8416                                    mode,
8417                                    notify_type,
8418                                    (GdkWindow *)last,
8419                                    device, source_device,
8420                                    toplevel_x, toplevel_y,
8421                                    mask, time_,
8422                                    event_in_queue,
8423                                    serial);
8424
8425               last = win;
8426               win = get_event_parent (win);
8427             }
8428         }
8429     }
8430
8431   if (b) /* Might not be a dest, e.g. if we're moving out of the window */
8432     {
8433       toplevel = gdk_window_get_toplevel ((GdkWindow *)b);
8434
8435       /* Traverse down from c to b */
8436       if (c != b)
8437         {
8438           path = NULL;
8439           win = get_event_parent (b);
8440           while (win != c && win->window_type != GDK_WINDOW_ROOT)
8441             {
8442               path = g_list_prepend (path, win);
8443               win = get_event_parent (win);
8444             }
8445
8446           if (non_linear)
8447             notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
8448           else
8449             notify_type = GDK_NOTIFY_VIRTUAL;
8450
8451           list = path;
8452           while (list)
8453             {
8454               win = list->data;
8455               list = g_list_next (list);
8456               if (list)
8457                 next = list->data;
8458               else
8459                 next = b;
8460
8461               send_crossing_event (display, toplevel,
8462                                    win, GDK_ENTER_NOTIFY,
8463                                    mode,
8464                                    notify_type,
8465                                    (GdkWindow *)next,
8466                                    device, source_device,
8467                                    toplevel_x, toplevel_y,
8468                                    mask, time_,
8469                                    event_in_queue,
8470                                    serial);
8471             }
8472           g_list_free (path);
8473         }
8474
8475
8476       if (non_linear)
8477         notify_type = GDK_NOTIFY_NONLINEAR;
8478       else if (c == a)
8479         notify_type = GDK_NOTIFY_ANCESTOR;
8480       else
8481         notify_type = GDK_NOTIFY_INFERIOR;
8482
8483       send_crossing_event (display, toplevel,
8484                            b, GDK_ENTER_NOTIFY,
8485                            mode,
8486                            notify_type,
8487                            NULL,
8488                            device, source_device,
8489                            toplevel_x, toplevel_y,
8490                            mask, time_,
8491                            event_in_queue,
8492                            serial);
8493     }
8494 }
8495
8496 /* Returns the window inside the event window with the pointer in it
8497  * at the specified coordinates, or NULL if its not in any child of
8498  * the toplevel. It also takes into account !owner_events grabs.
8499  */
8500 static GdkWindow *
8501 get_pointer_window (GdkDisplay *display,
8502                     GdkWindow *event_window,
8503                     GdkDevice *device,
8504                     gdouble toplevel_x,
8505                     gdouble toplevel_y,
8506                     gulong serial)
8507 {
8508   GdkWindow *pointer_window;
8509   GdkDeviceGrabInfo *grab;
8510   GdkPointerWindowInfo *pointer_info;
8511
8512   pointer_info = _gdk_display_get_pointer_info (display, device);
8513
8514   if (event_window == pointer_info->toplevel_under_pointer)
8515     pointer_window =
8516       _gdk_window_find_descendant_at (event_window,
8517                                       toplevel_x, toplevel_y,
8518                                       NULL, NULL);
8519   else
8520     pointer_window = NULL;
8521
8522   grab = _gdk_display_has_device_grab (display, device, serial);
8523   if (grab != NULL &&
8524       !grab->owner_events &&
8525       pointer_window != grab->window)
8526     pointer_window = NULL;
8527
8528   return pointer_window;
8529 }
8530
8531 void
8532 _gdk_display_set_window_under_pointer (GdkDisplay *display,
8533                                        GdkDevice  *device,
8534                                        GdkWindow  *window)
8535 {
8536   GdkPointerWindowInfo *device_info;
8537
8538   device_info = _gdk_display_get_pointer_info (display, device);
8539
8540   if (device_info->window_under_pointer)
8541     g_object_unref (device_info->window_under_pointer);
8542   device_info->window_under_pointer = window;
8543
8544   if (window)
8545     {
8546       g_object_ref (window);
8547       update_cursor (display, device);
8548     }
8549
8550   _gdk_display_enable_motion_hints (display, device);
8551 }
8552
8553 /**
8554  * gdk_pointer_grab:
8555  * @window: the #GdkWindow which will own the grab (the grab window).
8556  * @owner_events: if %FALSE then all pointer events are reported with respect to
8557  *                @window and are only reported if selected by @event_mask. If %TRUE then pointer
8558  *                events for this application are reported as normal, but pointer events outside
8559  *                this application are reported with respect to @window and only if selected by
8560  *                @event_mask. In either mode, unreported events are discarded.
8561  * @event_mask: specifies the event mask, which is used in accordance with
8562  *              @owner_events. Note that only pointer events (i.e. button and motion events)
8563  *              may be selected.
8564  * @confine_to: (allow-none): If non-%NULL, the pointer will be confined to this
8565  *              window during the grab. If the pointer is outside @confine_to, it will
8566  *              automatically be moved to the closest edge of @confine_to and enter
8567  *              and leave events will be generated as necessary.
8568  * @cursor: (allow-none): the cursor to display while the grab is active. If this is %NULL then
8569  *          the normal cursors are used for @window and its descendants, and the cursor
8570  *          for @window is used for all other windows.
8571  * @time_: the timestamp of the event which led to this pointer grab. This usually
8572  *         comes from a #GdkEventButton struct, though %GDK_CURRENT_TIME can be used if
8573  *         the time isn't known.
8574  *
8575  * Grabs the pointer (usually a mouse) so that all events are passed to this
8576  * application until the pointer is ungrabbed with gdk_pointer_ungrab(), or
8577  * the grab window becomes unviewable.
8578  * This overrides any previous pointer grab by this client.
8579  *
8580  * Pointer grabs are used for operations which need complete control over mouse
8581  * events, even if the mouse leaves the application.
8582  * For example in GTK+ it is used for Drag and Drop, for dragging the handle in
8583  * the #GtkHPaned and #GtkVPaned widgets.
8584  *
8585  * Note that if the event mask of an X window has selected both button press and
8586  * button release events, then a button press event will cause an automatic
8587  * pointer grab until the button is released.
8588  * X does this automatically since most applications expect to receive button
8589  * press and release events in pairs.
8590  * It is equivalent to a pointer grab on the window with @owner_events set to
8591  * %TRUE.
8592  *
8593  * If you set up anything at the time you take the grab that needs to be cleaned
8594  * up when the grab ends, you should handle the #GdkEventGrabBroken events that
8595  * are emitted when the grab ends unvoluntarily.
8596  *
8597  * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
8598  *
8599  * Deprecated: 3.0: Use gdk_device_grab() instead.
8600  **/
8601 GdkGrabStatus
8602 gdk_pointer_grab (GdkWindow *     window,
8603                   gboolean        owner_events,
8604                   GdkEventMask    event_mask,
8605                   GdkWindow *     confine_to,
8606                   GdkCursor *     cursor,
8607                   guint32         time)
8608 {
8609   GdkWindow *native;
8610   GdkDisplay *display;
8611   GdkDeviceManager *device_manager;
8612   GdkDevice *device;
8613   GdkGrabStatus res = 0;
8614   gulong serial;
8615   GList *devices, *dev;
8616
8617   g_return_val_if_fail (window != NULL, 0);
8618   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8619   g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
8620
8621   /* We need a native window for confine to to work, ensure we have one */
8622   if (confine_to)
8623     {
8624       if (!gdk_window_ensure_native (confine_to))
8625         {
8626           g_warning ("Can't confine to grabbed window, not native");
8627           confine_to = NULL;
8628         }
8629     }
8630
8631   /* Non-viewable client side window => fail */
8632   if (!_gdk_window_has_impl (window) &&
8633       !gdk_window_is_viewable (window))
8634     return GDK_GRAB_NOT_VIEWABLE;
8635
8636   native = gdk_window_get_toplevel (window);
8637   while (gdk_window_is_offscreen (native))
8638     {
8639       native = gdk_offscreen_window_get_embedder (native);
8640
8641       if (native == NULL ||
8642           (!_gdk_window_has_impl (native) &&
8643            !gdk_window_is_viewable (native)))
8644         return GDK_GRAB_NOT_VIEWABLE;
8645
8646       native = gdk_window_get_toplevel (native);
8647     }
8648
8649   display = gdk_window_get_display (window);
8650
8651   serial = _gdk_display_get_next_serial (display);
8652   device_manager = gdk_display_get_device_manager (display);
8653   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
8654
8655   /* FIXME: Should this be generic to all backends? */
8656   /* FIXME: What happens with extended devices? */
8657   for (dev = devices; dev; dev = dev->next)
8658     {
8659       device = dev->data;
8660
8661       if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
8662         continue;
8663
8664       res = GDK_DEVICE_GET_CLASS (device)->grab (device,
8665                                                  native,
8666                                                  owner_events,
8667                                                  get_native_grab_event_mask (event_mask),
8668                                                  confine_to,
8669                                                  cursor,
8670                                                  time);
8671
8672       if (res == GDK_GRAB_SUCCESS)
8673         _gdk_display_add_device_grab (display,
8674                                       device,
8675                                       window,
8676                                       native,
8677                                       GDK_OWNERSHIP_NONE,
8678                                       owner_events,
8679                                       event_mask,
8680                                       serial,
8681                                       time,
8682                                       FALSE);
8683     }
8684
8685   /* FIXME: handle errors when grabbing */
8686
8687   g_list_free (devices);
8688
8689   return res;
8690 }
8691
8692 /**
8693  * gdk_keyboard_grab:
8694  * @window: the #GdkWindow which will own the grab (the grab window).
8695  * @owner_events: if %FALSE then all keyboard events are reported with respect to
8696  *   @window. If %TRUE then keyboard events for this application are
8697  *   reported as normal, but keyboard events outside this application
8698  *   are reported with respect to @window. Both key press and key
8699  *   release events are always reported, independant of the event mask
8700  *   set by the application.
8701  * @time_: a timestamp from a #GdkEvent, or %GDK_CURRENT_TIME if no timestamp is
8702  *   available.
8703  *
8704  * Grabs the keyboard so that all events are passed to this
8705  * application until the keyboard is ungrabbed with gdk_keyboard_ungrab().
8706  * This overrides any previous keyboard grab by this client.
8707  *
8708  * If you set up anything at the time you take the grab that needs to be cleaned
8709  * up when the grab ends, you should handle the #GdkEventGrabBroken events that
8710  * are emitted when the grab ends unvoluntarily.
8711  *
8712  * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
8713  *
8714  * Deprecated: 3.0: Use gdk_device_grab() instead.
8715  **/
8716 GdkGrabStatus
8717 gdk_keyboard_grab (GdkWindow *window,
8718                    gboolean   owner_events,
8719                    guint32    time)
8720 {
8721   GdkWindow *native;
8722   GdkDisplay *display;
8723   GdkDeviceManager *device_manager;
8724   GdkDevice *device;
8725   GdkGrabStatus res = 0;
8726   gulong serial;
8727   GList *devices, *dev;
8728
8729   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8730
8731   /* Non-viewable client side window => fail */
8732   if (!_gdk_window_has_impl (window) &&
8733       !gdk_window_is_viewable (window))
8734     return GDK_GRAB_NOT_VIEWABLE;
8735
8736   native = gdk_window_get_toplevel (window);
8737
8738   while (gdk_window_is_offscreen (native))
8739     {
8740       native = gdk_offscreen_window_get_embedder (native);
8741
8742       if (native == NULL ||
8743           (!_gdk_window_has_impl (native) &&
8744            !gdk_window_is_viewable (native)))
8745         return GDK_GRAB_NOT_VIEWABLE;
8746
8747       native = gdk_window_get_toplevel (native);
8748     }
8749
8750   display = gdk_window_get_display (window);
8751   serial = _gdk_display_get_next_serial (display);
8752   device_manager = gdk_display_get_device_manager (display);
8753   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
8754
8755   /* FIXME: Should this be generic to all backends? */
8756   /* FIXME: What happens with extended devices? */
8757   for (dev = devices; dev; dev = dev->next)
8758     {
8759       device = dev->data;
8760
8761       if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
8762         continue;
8763
8764       res = GDK_DEVICE_GET_CLASS (device)->grab (device,
8765                                                  native,
8766                                                  owner_events,
8767                                                  GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
8768                                                  NULL,
8769                                                  NULL,
8770                                                  time);
8771
8772       if (res == GDK_GRAB_SUCCESS)
8773         _gdk_display_add_device_grab (display,
8774                                       device,
8775                                       window,
8776                                       native,
8777                                       GDK_OWNERSHIP_NONE,
8778                                       owner_events, 0,
8779                                       serial,
8780                                       time,
8781                                       FALSE);
8782     }
8783
8784   /* FIXME: handle errors when grabbing */
8785
8786   g_list_free (devices);
8787
8788   return res;
8789 }
8790
8791 /**
8792  * gdk_window_geometry_changed:
8793  * @window: an embedded offscreen #GdkWindow
8794  *
8795  * This function informs GDK that the geometry of an embedded
8796  * offscreen window has changed. This is necessary for GDK to keep
8797  * track of which offscreen window the pointer is in.
8798  *
8799  * Since: 2.18
8800  */
8801 void
8802 gdk_window_geometry_changed (GdkWindow *window)
8803 {
8804   _gdk_synthesize_crossing_events_for_geometry_change (window);
8805 }
8806
8807 static void
8808 source_events_device_added (GdkDeviceManager *device_manager,
8809                             GdkDevice        *device,
8810                             gpointer          user_data)
8811 {
8812   GdkWindow *window;
8813   GdkEventMask event_mask;
8814   GdkInputSource source;
8815
8816   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_FLOATING)
8817     return;
8818
8819   window = user_data;
8820   source = gdk_device_get_source (device);
8821
8822   event_mask = GPOINTER_TO_INT (g_hash_table_lookup (window->source_event_masks,
8823                                                      GINT_TO_POINTER (source)));
8824   if (event_mask)
8825     gdk_window_set_device_events (window, device, event_mask);
8826 }
8827
8828 static void
8829 source_events_device_changed (GdkDeviceManager *device_manager,
8830                               GdkDevice        *device,
8831                               gpointer          user_data)
8832 {
8833   GdkDeviceType type;
8834   GdkInputSource source;
8835   GdkEventMask event_mask;
8836   GdkWindow *window;
8837
8838   window = user_data;
8839   type = gdk_device_get_device_type (device);
8840   source = gdk_device_get_source (device);
8841
8842   event_mask = GPOINTER_TO_INT (g_hash_table_lookup (window->source_event_masks,
8843                                                      GINT_TO_POINTER (source)));
8844
8845   if (!event_mask)
8846     return;
8847
8848   if (type == GDK_DEVICE_TYPE_FLOATING)
8849     {
8850       /* The device was just floated, enable its event mask */
8851       gdk_window_set_device_events (window, device, event_mask);
8852     }
8853   else if (type == GDK_DEVICE_TYPE_SLAVE)
8854     gdk_window_set_device_events (window, device, 0);
8855 }
8856
8857 /**
8858  * gdk_window_set_source_events:
8859  * @window: a #GdkWindow
8860  * @source: a #GdkInputSource to define the source class.
8861  * @event_mask: event mask for @window
8862  *
8863  * Sets the event mask for any floating device (i.e. not attached to any
8864  * visible pointer) that has the source defined as @source. This event
8865  * mask will be applied both to currently existing, newly added devices
8866  * after this call, and devices being attached/detached.
8867  *
8868  * Since: 3.0
8869  **/
8870 void
8871 gdk_window_set_source_events (GdkWindow      *window,
8872                               GdkInputSource  source,
8873                               GdkEventMask    event_mask)
8874 {
8875   GdkDeviceManager *device_manager;
8876   GdkDisplay *display;
8877   GList *devices, *d;
8878   guint size;
8879
8880   g_return_if_fail (GDK_IS_WINDOW (window));
8881
8882   display = gdk_window_get_display (window);
8883   device_manager = gdk_display_get_device_manager (display);
8884
8885   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
8886
8887   /* Set event mask for existing devices */
8888   for (d = devices; d; d = d->next)
8889     {
8890       GdkDevice *device = d->data;
8891
8892       if (source == gdk_device_get_source (device))
8893         gdk_window_set_device_events (window, device, event_mask);
8894     }
8895
8896   g_list_free (devices);
8897
8898   /* Update accounting */
8899   if (G_UNLIKELY (!window->source_event_masks))
8900     window->source_event_masks = g_hash_table_new (NULL, NULL);
8901
8902   if (event_mask)
8903     g_hash_table_insert (window->source_event_masks,
8904                          GUINT_TO_POINTER (source),
8905                          GUINT_TO_POINTER (event_mask));
8906   else
8907     g_hash_table_remove (window->source_event_masks,
8908                          GUINT_TO_POINTER (source));
8909
8910   size = g_hash_table_size (window->source_event_masks);
8911
8912   /* Update handler if needed */
8913   if (!window->device_added_handler_id && size > 0)
8914     {
8915       window->device_added_handler_id =
8916         g_signal_connect (device_manager, "device-added",
8917                           G_CALLBACK (source_events_device_added), window);
8918       window->device_changed_handler_id =
8919         g_signal_connect (device_manager, "device-changed",
8920                           G_CALLBACK (source_events_device_changed), window);
8921     }
8922   else if (window->device_added_handler_id && size == 0)
8923     g_signal_handler_disconnect (device_manager, window->device_added_handler_id);
8924 }
8925
8926 /**
8927  * gdk_window_get_source_events:
8928  * @window: a #GdkWindow
8929  * @source: a #GdkInputSource to define the source class.
8930  *
8931  * Returns the event mask for @window corresponding to the device class specified
8932  * by @source.
8933  *
8934  * Returns: source event mask for @window
8935  **/
8936 GdkEventMask
8937 gdk_window_get_source_events (GdkWindow      *window,
8938                               GdkInputSource  source)
8939 {
8940   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8941
8942   return GPOINTER_TO_UINT (g_hash_table_lookup (window->source_event_masks,
8943                                                 GUINT_TO_POINTER (source)));
8944 }
8945
8946 static gboolean
8947 do_synthesize_crossing_event (gpointer data)
8948 {
8949   GdkDisplay *display;
8950   GdkWindow *changed_toplevel;
8951   GHashTableIter iter;
8952   gpointer key, value;
8953   gulong serial;
8954
8955   changed_toplevel = data;
8956
8957   changed_toplevel->synthesize_crossing_event_queued = FALSE;
8958
8959   if (GDK_WINDOW_DESTROYED (changed_toplevel))
8960     return FALSE;
8961
8962   display = gdk_window_get_display (changed_toplevel);
8963   serial = _gdk_display_get_next_serial (display);
8964   g_hash_table_iter_init (&iter, display->pointers_info);
8965
8966   while (g_hash_table_iter_next (&iter, &key, &value))
8967     {
8968       GdkWindow *new_window_under_pointer;
8969       GdkPointerWindowInfo *pointer_info = value;
8970       GdkDevice *device = key;
8971
8972       if (changed_toplevel == pointer_info->toplevel_under_pointer)
8973         {
8974           new_window_under_pointer =
8975             get_pointer_window (display, changed_toplevel,
8976                                 device,
8977                                 pointer_info->toplevel_x,
8978                                 pointer_info->toplevel_y,
8979                                 serial);
8980           if (new_window_under_pointer != pointer_info->window_under_pointer)
8981             {
8982               _gdk_synthesize_crossing_events (display,
8983                                                pointer_info->window_under_pointer,
8984                                                new_window_under_pointer,
8985                                                device, NULL,
8986                                                GDK_CROSSING_NORMAL,
8987                                                pointer_info->toplevel_x,
8988                                                pointer_info->toplevel_y,
8989                                                pointer_info->state,
8990                                                GDK_CURRENT_TIME,
8991                                                NULL,
8992                                                serial,
8993                                                FALSE);
8994               _gdk_display_set_window_under_pointer (display, device, new_window_under_pointer);
8995             }
8996         }
8997     }
8998
8999   return FALSE;
9000 }
9001
9002 void
9003 _gdk_synthesize_crossing_events_for_geometry_change (GdkWindow *changed_window)
9004 {
9005   GdkWindow *toplevel;
9006
9007   toplevel = get_event_toplevel (changed_window);
9008
9009   if (!toplevel->synthesize_crossing_event_queued)
9010     {
9011       toplevel->synthesize_crossing_event_queued = TRUE;
9012
9013       gdk_threads_add_idle_full (GDK_PRIORITY_EVENTS - 1,
9014                                  do_synthesize_crossing_event,
9015                                  g_object_ref (toplevel),
9016                                  g_object_unref);
9017     }
9018 }
9019
9020 /* Don't use for crossing events */
9021 static GdkWindow *
9022 get_event_window (GdkDisplay                 *display,
9023                   GdkDevice                  *device,
9024                   GdkWindow                  *pointer_window,
9025                   GdkEventType                type,
9026                   GdkModifierType             mask,
9027                   guint                      *evmask_out,
9028                   gulong                      serial)
9029 {
9030   guint evmask;
9031   GdkWindow *grab_window;
9032   GdkDeviceGrabInfo *grab;
9033
9034   grab = _gdk_display_has_device_grab (display, device, serial);
9035
9036   if (grab != NULL && !grab->owner_events)
9037     {
9038       evmask = grab->event_mask;
9039       evmask = update_evmask_for_button_motion (evmask, mask);
9040
9041       grab_window = grab->window;
9042
9043       if (evmask & type_masks[type])
9044         {
9045           if (evmask_out)
9046             *evmask_out = evmask;
9047           return grab_window;
9048         }
9049       else
9050         return NULL;
9051     }
9052
9053   while (pointer_window != NULL)
9054     {
9055       evmask = pointer_window->event_mask;
9056       evmask = update_evmask_for_button_motion (evmask, mask);
9057
9058       if (evmask & type_masks[type])
9059         {
9060           if (evmask_out)
9061             *evmask_out = evmask;
9062           return pointer_window;
9063         }
9064
9065       pointer_window = get_event_parent (pointer_window);
9066     }
9067
9068   if (grab != NULL &&
9069       grab->owner_events)
9070     {
9071       evmask = grab->event_mask;
9072       evmask = update_evmask_for_button_motion (evmask, mask);
9073
9074       if (evmask & type_masks[type])
9075         {
9076           if (evmask_out)
9077             *evmask_out = evmask;
9078           return grab->window;
9079         }
9080       else
9081         return NULL;
9082     }
9083
9084   return NULL;
9085 }
9086
9087 static gboolean
9088 proxy_pointer_event (GdkDisplay                 *display,
9089                      GdkEvent                   *source_event,
9090                      gulong                      serial)
9091 {
9092   GdkWindow *toplevel_window, *event_window;
9093   GdkWindow *pointer_window;
9094   GdkPointerWindowInfo *pointer_info;
9095   GdkDevice *device, *source_device;
9096   GdkEvent *event;
9097   guint state;
9098   gdouble toplevel_x, toplevel_y;
9099   guint32 time_;
9100   gboolean non_linear;
9101
9102   event_window = source_event->any.window;
9103   gdk_event_get_coords (source_event, &toplevel_x, &toplevel_y);
9104   gdk_event_get_state (source_event, &state);
9105   time_ = gdk_event_get_time (source_event);
9106   device = gdk_event_get_device (source_event);
9107   source_device = gdk_event_get_source_device (source_event);
9108   pointer_info = _gdk_display_get_pointer_info (display, device);
9109   toplevel_window = convert_native_coords_to_toplevel (event_window,
9110                                                        toplevel_x, toplevel_y,
9111                                                        &toplevel_x, &toplevel_y);
9112
9113   non_linear = FALSE;
9114   if ((source_event->type == GDK_LEAVE_NOTIFY ||
9115        source_event->type == GDK_ENTER_NOTIFY) &&
9116       (source_event->crossing.detail == GDK_NOTIFY_NONLINEAR ||
9117        source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))
9118     non_linear = TRUE;
9119
9120   /* If we get crossing events with subwindow unexpectedly being NULL
9121      that means there is a native subwindow that gdk doesn't know about.
9122      We track these and forward them, with the correct virtual window
9123      events inbetween.
9124      This is important to get right, as metacity uses gdk for the frame
9125      windows, but gdk doesn't know about the client windows reparented
9126      into the frame. */
9127   if (((source_event->type == GDK_LEAVE_NOTIFY &&
9128         source_event->crossing.detail == GDK_NOTIFY_INFERIOR) ||
9129        (source_event->type == GDK_ENTER_NOTIFY &&
9130         (source_event->crossing.detail == GDK_NOTIFY_VIRTUAL ||
9131          source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))) &&
9132       source_event->crossing.subwindow == NULL)
9133     {
9134       /* Left for an unknown (to gdk) subwindow */
9135
9136       /* Send leave events from window under pointer to event window
9137          that will get the subwindow == NULL window */
9138       _gdk_synthesize_crossing_events (display,
9139                                        pointer_info->window_under_pointer,
9140                                        event_window,
9141                                        device, source_device,
9142                                        source_event->crossing.mode,
9143                                        toplevel_x, toplevel_y,
9144                                        state, time_,
9145                                        source_event,
9146                                        serial,
9147                                        non_linear);
9148
9149       /* Send subwindow == NULL event */
9150       send_crossing_event (display,
9151                            toplevel_window,
9152                            event_window,
9153                            source_event->type,
9154                            source_event->crossing.mode,
9155                            source_event->crossing.detail,
9156                            NULL,
9157                            device, source_device,
9158                            toplevel_x, toplevel_y,
9159                            state, time_,
9160                            source_event,
9161                            serial);
9162
9163       _gdk_display_set_window_under_pointer (display, device, NULL);
9164       return TRUE;
9165     }
9166
9167   pointer_window = get_pointer_window (display, toplevel_window, device,
9168                                        toplevel_x, toplevel_y, serial);
9169
9170   if (((source_event->type == GDK_ENTER_NOTIFY &&
9171         source_event->crossing.detail == GDK_NOTIFY_INFERIOR) ||
9172        (source_event->type == GDK_LEAVE_NOTIFY &&
9173         (source_event->crossing.detail == GDK_NOTIFY_VIRTUAL ||
9174          source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))) &&
9175       source_event->crossing.subwindow == NULL)
9176     {
9177       /* Entered from an unknown (to gdk) subwindow */
9178
9179       /* Send subwindow == NULL event */
9180       send_crossing_event (display,
9181                            toplevel_window,
9182                            event_window,
9183                            source_event->type,
9184                            source_event->crossing.mode,
9185                            source_event->crossing.detail,
9186                            NULL,
9187                            device, source_device,
9188                            toplevel_x, toplevel_y,
9189                            state, time_,
9190                            source_event,
9191                            serial);
9192
9193       /* Send enter events from event window to pointer_window */
9194       _gdk_synthesize_crossing_events (display,
9195                                        event_window,
9196                                        pointer_window,
9197                                        device, source_device,
9198                                        source_event->crossing.mode,
9199                                        toplevel_x, toplevel_y,
9200                                        state, time_,
9201                                        source_event,
9202                                        serial, non_linear);
9203       _gdk_display_set_window_under_pointer (display, device, pointer_window);
9204       return TRUE;
9205     }
9206
9207   if (pointer_info->window_under_pointer != pointer_window)
9208     {
9209       /* Either a toplevel crossing notify that ended up inside a child window,
9210          or a motion notify that got into another child window  */
9211
9212       /* Different than last time, send crossing events */
9213       _gdk_synthesize_crossing_events (display,
9214                                        pointer_info->window_under_pointer,
9215                                        pointer_window,
9216                                        device, source_device,
9217                                        GDK_CROSSING_NORMAL,
9218                                        toplevel_x, toplevel_y,
9219                                        state, time_,
9220                                        source_event,
9221                                        serial, non_linear);
9222       _gdk_display_set_window_under_pointer (display, device, pointer_window);
9223     }
9224   else if (source_event->type == GDK_MOTION_NOTIFY)
9225     {
9226       GdkWindow *event_win;
9227       guint evmask;
9228       gboolean is_hint;
9229
9230       event_win = get_event_window (display,
9231                                     device,
9232                                     pointer_window,
9233                                     source_event->type,
9234                                     state,
9235                                     &evmask,
9236                                     serial);
9237
9238       if (event_win &&
9239           gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER &&
9240           gdk_window_get_device_events (event_win, device) == 0)
9241         return TRUE;
9242
9243       is_hint = FALSE;
9244
9245       if (event_win &&
9246           (evmask & GDK_POINTER_MOTION_HINT_MASK))
9247         {
9248           gulong *device_serial;
9249
9250           device_serial = g_hash_table_lookup (display->motion_hint_info, device);
9251
9252           if (!device_serial ||
9253               (*device_serial != 0 &&
9254                serial < *device_serial))
9255             event_win = NULL; /* Ignore event */
9256           else
9257             {
9258               is_hint = TRUE;
9259               *device_serial = G_MAXULONG;
9260             }
9261         }
9262
9263       if (event_win && !display->ignore_core_events)
9264         {
9265           event = _gdk_make_event (event_win, GDK_MOTION_NOTIFY, source_event, FALSE);
9266           event->motion.time = time_;
9267           convert_toplevel_coords_to_window (event_win,
9268                                              toplevel_x, toplevel_y,
9269                                              &event->motion.x, &event->motion.y);
9270           event->motion.x_root = source_event->motion.x_root;
9271           event->motion.y_root = source_event->motion.y_root;
9272           event->motion.state = state;
9273           event->motion.is_hint = is_hint;
9274           event->motion.device = source_event->motion.device;
9275           event->motion.axes = g_memdup (source_event->motion.axes,
9276                                          sizeof (gdouble) * gdk_device_get_n_axes (source_event->motion.device));
9277           gdk_event_set_source_device (event, source_device);
9278         }
9279     }
9280
9281   /* unlink all move events from queue.
9282      We handle our own, including our emulated masks. */
9283   return TRUE;
9284 }
9285
9286 #define GDK_ANY_BUTTON_MASK (GDK_BUTTON1_MASK | \
9287                              GDK_BUTTON2_MASK | \
9288                              GDK_BUTTON3_MASK | \
9289                              GDK_BUTTON4_MASK | \
9290                              GDK_BUTTON5_MASK)
9291
9292 static gboolean
9293 proxy_button_event (GdkEvent *source_event,
9294                     gulong serial)
9295 {
9296   GdkWindow *toplevel_window, *event_window;
9297   GdkWindow *event_win;
9298   GdkWindow *pointer_window;
9299   GdkWindow *parent;
9300   GdkEvent *event;
9301   guint state;
9302   guint32 time_;
9303   GdkEventType type;
9304   gdouble toplevel_x, toplevel_y;
9305   GdkDisplay *display;
9306   GdkWindow *w;
9307   GdkDevice *device, *source_device;
9308
9309   type = source_event->any.type;
9310   event_window = source_event->any.window;
9311   gdk_event_get_coords (source_event, &toplevel_x, &toplevel_y);
9312   gdk_event_get_state (source_event, &state);
9313   time_ = gdk_event_get_time (source_event);
9314   device = gdk_event_get_device (source_event);
9315   source_device = gdk_event_get_source_device (source_event);
9316   display = gdk_window_get_display (source_event->any.window);
9317   toplevel_window = convert_native_coords_to_toplevel (event_window,
9318                                                        toplevel_x, toplevel_y,
9319                                                        &toplevel_x, &toplevel_y);
9320
9321   if (type == GDK_BUTTON_PRESS &&
9322       !source_event->any.send_event &&
9323       _gdk_display_has_device_grab (display, device, serial) == NULL)
9324     {
9325       pointer_window =
9326         _gdk_window_find_descendant_at (toplevel_window,
9327                                         toplevel_x, toplevel_y,
9328                                         NULL, NULL);
9329
9330       /* Find the event window, that gets the grab */
9331       w = pointer_window;
9332       while (w != NULL &&
9333              (parent = get_event_parent (w)) != NULL &&
9334              parent->window_type != GDK_WINDOW_ROOT)
9335         {
9336           if (w->event_mask & GDK_BUTTON_PRESS_MASK)
9337             break;
9338           w = parent;
9339         }
9340       pointer_window = (GdkWindow *)w;
9341
9342       _gdk_display_add_device_grab  (display,
9343                                      device,
9344                                      pointer_window,
9345                                      toplevel_window,
9346                                      GDK_OWNERSHIP_NONE,
9347                                      FALSE,
9348                                      gdk_window_get_events (pointer_window),
9349                                      serial,
9350                                       time_,
9351                                      TRUE);
9352       _gdk_display_device_grab_update (display, device, source_device, serial);
9353     }
9354
9355   pointer_window = get_pointer_window (display, toplevel_window, device,
9356                                        toplevel_x, toplevel_y,
9357                                        serial);
9358
9359   event_win = get_event_window (display,
9360                                 device,
9361                                 pointer_window,
9362                                 type, state,
9363                                 NULL, serial);
9364
9365   if (event_win == NULL || display->ignore_core_events)
9366     return TRUE;
9367
9368   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER &&
9369       gdk_window_get_device_events (event_win, device) == 0)
9370     return TRUE;
9371
9372   event = _gdk_make_event (event_win, type, source_event, FALSE);
9373
9374   switch (type)
9375     {
9376     case GDK_BUTTON_PRESS:
9377     case GDK_BUTTON_RELEASE:
9378       event->button.button = source_event->button.button;
9379       convert_toplevel_coords_to_window (event_win,
9380                                          toplevel_x, toplevel_y,
9381                                          &event->button.x, &event->button.y);
9382       event->button.x_root = source_event->button.x_root;
9383       event->button.y_root = source_event->button.y_root;
9384       event->button.state = state;
9385       event->button.device = source_event->button.device;
9386       event->button.axes = g_memdup (source_event->button.axes,
9387                                      sizeof (gdouble) * gdk_device_get_n_axes (source_event->button.device));
9388
9389       gdk_event_set_source_device (event, source_device);
9390
9391       if (type == GDK_BUTTON_PRESS)
9392         _gdk_event_button_generate (display, event);
9393       return TRUE;
9394
9395     case GDK_SCROLL:
9396       event->scroll.direction = source_event->scroll.direction;
9397       convert_toplevel_coords_to_window (event_win,
9398                                          toplevel_x, toplevel_y,
9399                                          &event->scroll.x, &event->scroll.y);
9400       event->scroll.x_root = source_event->scroll.x_root;
9401       event->scroll.y_root = source_event->scroll.y_root;
9402       event->scroll.state = state;
9403       event->scroll.device = source_event->scroll.device;
9404       gdk_event_set_source_device (event, source_device);
9405       return TRUE;
9406
9407     default:
9408       return FALSE;
9409     }
9410
9411   return TRUE; /* Always unlink original, we want to obey the emulated event mask */
9412 }
9413
9414 #ifdef DEBUG_WINDOW_PRINTING
9415 static void
9416 gdk_window_print (GdkWindow *window,
9417                   int indent)
9418 {
9419   GdkRectangle r;
9420   const char *window_types[] = {
9421     "root",
9422     "toplevel",
9423     "child",
9424     "dialog",
9425     "temp",
9426     "foreign",
9427     "offscreen"
9428   };
9429
9430   g_print ("%*s%p: [%s] %d,%d %dx%d", indent, "", window,
9431            window->user_data ? g_type_name_from_instance (window->user_data) : "no widget",
9432            window->x, window->y,
9433            window->width, window->height
9434            );
9435
9436   if (gdk_window_has_impl (window))
9437     {
9438 #ifdef GDK_WINDOWING_X11
9439       g_print (" impl(0x%lx)", gdk_x11_window_get_xid (window));
9440 #endif
9441     }
9442
9443   if (window->window_type != GDK_WINDOW_CHILD)
9444     g_print (" %s", window_types[window->window_type]);
9445
9446   if (window->input_only)
9447     g_print (" input-only");
9448
9449   if (window->shaped)
9450     g_print (" shaped");
9451
9452   if (!gdk_window_is_visible ((GdkWindow *)window))
9453     g_print (" hidden");
9454
9455   g_print (" abs[%d,%d]",
9456            window->abs_x, window->abs_y);
9457
9458   cairo_region_get_extents (window->clip_region, &r);
9459   if (cairo_region_is_empty (window->clip_region))
9460     g_print (" clipbox[empty]");
9461   else
9462     g_print (" clipbox[%d,%d %dx%d]", r.x, r.y, r.width, r.height);
9463
9464   g_print ("\n");
9465 }
9466
9467
9468 static void
9469 gdk_window_print_tree (GdkWindow *window,
9470                        int indent,
9471                        gboolean include_input_only)
9472 {
9473   GList *l;
9474
9475   if (window->input_only && !include_input_only)
9476     return;
9477
9478   gdk_window_print (window, indent);
9479
9480   for (l = window->children; l != NULL; l = l->next)
9481     gdk_window_print_tree (l->data, indent + 4, include_input_only);
9482 }
9483
9484 #endif /* DEBUG_WINDOW_PRINTING */
9485
9486 void
9487 _gdk_windowing_got_event (GdkDisplay *display,
9488                           GList      *event_link,
9489                           GdkEvent   *event,
9490                           gulong      serial)
9491 {
9492   GdkWindow *event_window;
9493   gdouble x, y;
9494   gboolean unlink_event;
9495   guint old_state, old_button;
9496   GdkDeviceGrabInfo *button_release_grab;
9497   GdkPointerWindowInfo *pointer_info;
9498   GdkDevice *device, *source_device;
9499   gboolean is_toplevel;
9500
9501   if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
9502     display->last_event_time = gdk_event_get_time (event);
9503
9504   device = gdk_event_get_device (event);
9505   source_device = gdk_event_get_source_device (event);
9506
9507   if (device)
9508     {
9509       GdkInputMode mode;
9510
9511       g_object_get (device, "input-mode", &mode, NULL);
9512       _gdk_display_device_grab_update (display, device, source_device, serial);
9513
9514       if (mode == GDK_MODE_DISABLED ||
9515           !_gdk_display_check_grab_ownership (display, device, serial))
9516         {
9517           /* Device events are blocked by another
9518            * device grab, or the device is disabled
9519            */
9520           unlink_event = TRUE;
9521           goto out;
9522         }
9523     }
9524
9525   event_window = event->any.window;
9526   if (!event_window)
9527     return;
9528
9529   pointer_info = _gdk_display_get_pointer_info (display, device);
9530
9531 #ifdef DEBUG_WINDOW_PRINTING
9532   if (event->type == GDK_KEY_PRESS &&
9533       (event->key.keyval == 0xa7 ||
9534        event->key.keyval == 0xbd))
9535     {
9536       gdk_window_print_tree (event_window, 0,
9537                              event->key.keyval == 0xbd);
9538     }
9539 #endif
9540
9541   if (event->type == GDK_VISIBILITY_NOTIFY)
9542     {
9543       event_window->native_visibility = event->visibility.state;
9544       gdk_window_update_visibility_recursively (event_window,
9545                                                 event_window);
9546       return;
9547     }
9548
9549   if (!(is_button_type (event->type) ||
9550         is_motion_type (event->type)) ||
9551       event_window->window_type == GDK_WINDOW_ROOT)
9552     return;
9553
9554   is_toplevel = gdk_window_is_toplevel (event_window);
9555
9556   if ((event->type == GDK_ENTER_NOTIFY ||
9557        event->type == GDK_LEAVE_NOTIFY) &&
9558       (event->crossing.mode == GDK_CROSSING_GRAB ||
9559        event->crossing.mode == GDK_CROSSING_UNGRAB) &&
9560       (_gdk_display_has_device_grab (display, device, serial) ||
9561        event->crossing.detail == GDK_NOTIFY_INFERIOR))
9562     {
9563       /* We synthesize all crossing events due to grabs ourselves,
9564        * so we ignore the native ones caused by our native pointer_grab
9565        * calls. Otherwise we would proxy these crossing event and cause
9566        * multiple copies of crossing events for grabs.
9567        *
9568        * We do want to handle grabs from other clients though, as for
9569        * instance alt-tab in metacity causes grabs like these and
9570        * we want to handle those. Thus the has_pointer_grab check.
9571        *
9572        * Implicit grabs on child windows create some grabbing events
9573        * that are sent before the button press. This means we can't
9574        * detect these with the has_pointer_grab check (as the implicit
9575        * grab is only noticed when we get button press event), so we
9576        * detect these events by checking for INFERIOR enter or leave
9577        * events. These should never be a problem to filter out.
9578        */
9579
9580       /* We ended up in this window after some (perhaps other clients)
9581          grab, so update the toplevel_under_window state */
9582       if (is_toplevel &&
9583           event->type == GDK_ENTER_NOTIFY &&
9584           event->crossing.mode == GDK_CROSSING_UNGRAB)
9585         {
9586           if (pointer_info->toplevel_under_pointer)
9587             g_object_unref (pointer_info->toplevel_under_pointer);
9588           pointer_info->toplevel_under_pointer = g_object_ref (event_window);
9589         }
9590
9591       unlink_event = TRUE;
9592       goto out;
9593     }
9594
9595   /* Track toplevel_under_pointer */
9596   if (is_toplevel)
9597     {
9598       if (event->type == GDK_ENTER_NOTIFY &&
9599           event->crossing.detail != GDK_NOTIFY_INFERIOR)
9600         {
9601           if (pointer_info->toplevel_under_pointer)
9602             g_object_unref (pointer_info->toplevel_under_pointer);
9603           pointer_info->toplevel_under_pointer = g_object_ref (event_window);
9604         }
9605       else if (event->type == GDK_LEAVE_NOTIFY &&
9606                event->crossing.detail != GDK_NOTIFY_INFERIOR &&
9607                pointer_info->toplevel_under_pointer == event_window)
9608         {
9609           if (pointer_info->toplevel_under_pointer)
9610             g_object_unref (pointer_info->toplevel_under_pointer);
9611           pointer_info->toplevel_under_pointer = NULL;
9612         }
9613     }
9614
9615   /* Store last pointer window and position/state */
9616   old_state = pointer_info->state;
9617   old_button = pointer_info->button;
9618
9619   gdk_event_get_coords (event, &x, &y);
9620   convert_native_coords_to_toplevel (event_window, x, y,  &x, &y);
9621   pointer_info->toplevel_x = x;
9622   pointer_info->toplevel_y = y;
9623   gdk_event_get_state (event, &pointer_info->state);
9624   if (event->type == GDK_BUTTON_PRESS ||
9625       event->type == GDK_BUTTON_RELEASE)
9626     pointer_info->button = event->button.button;
9627
9628   if (device &&
9629       (pointer_info->state != old_state ||
9630        pointer_info->button != old_button))
9631     _gdk_display_enable_motion_hints (display, device);
9632
9633   unlink_event = FALSE;
9634   if (is_motion_type (event->type))
9635     unlink_event = proxy_pointer_event (display,
9636                                         event,
9637                                         serial);
9638   else if (is_button_type (event->type))
9639     unlink_event = proxy_button_event (event,
9640                                        serial);
9641
9642   if (event->type == GDK_BUTTON_RELEASE &&
9643       !event->any.send_event)
9644     {
9645       button_release_grab =
9646         _gdk_display_has_device_grab (display, device, serial);
9647       if (button_release_grab &&
9648           button_release_grab->implicit &&
9649           (event->button.state & GDK_ANY_BUTTON_MASK & ~(GDK_BUTTON1_MASK << (event->button.button - 1))) == 0)
9650         {
9651           button_release_grab->serial_end = serial;
9652           button_release_grab->implicit_ungrab = FALSE;
9653           _gdk_display_device_grab_update (display, device, source_device, serial);
9654         }
9655     }
9656
9657  out:
9658   if (unlink_event)
9659     {
9660       _gdk_event_queue_remove_link (display, event_link);
9661       g_list_free_1 (event_link);
9662       gdk_event_free (event);
9663     }
9664 }
9665
9666 /**
9667  * gdk_window_create_similar_surface:
9668  * @window: window to make new surface similar to
9669  * @content: the content for the new surface
9670  * @width: width of the new surface
9671  * @height: height of the new surface
9672  *
9673  * Create a new surface that is as compatible as possible with the
9674  * given @window. For example the new surface will have the same
9675  * fallback resolution and font options as @window. Generally, the new
9676  * surface will also use the same backend as @window, unless that is
9677  * not possible for some reason. The type of the returned surface may
9678  * be examined with cairo_surface_get_type().
9679  *
9680  * Initially the surface contents are all 0 (transparent if contents
9681  * have transparency, black otherwise.)
9682  *
9683  * Returns: a pointer to the newly allocated surface. The caller
9684  * owns the surface and should call cairo_surface_destroy() when done
9685  * with it.
9686  *
9687  * This function always returns a valid pointer, but it will return a
9688  * pointer to a "nil" surface if @other is already in an error state
9689  * or any other error occurs.
9690  *
9691  * Since: 2.22
9692  **/
9693 cairo_surface_t *
9694 gdk_window_create_similar_surface (GdkWindow *     window,
9695                                    cairo_content_t content,
9696                                    int             width,
9697                                    int             height)
9698 {
9699   cairo_surface_t *window_surface, *surface;
9700
9701   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
9702   
9703   window_surface = _gdk_window_ref_cairo_surface (window);
9704
9705   switch (_gdk_rendering_mode)
9706   {
9707     case GDK_RENDERING_MODE_RECORDING:
9708       {
9709         cairo_rectangle_t rect = { 0, 0, width, height };
9710         surface = cairo_recording_surface_create (content, &rect);
9711       }
9712       break;
9713     case GDK_RENDERING_MODE_IMAGE:
9714       surface = cairo_image_surface_create (content == CAIRO_CONTENT_COLOR ? CAIRO_FORMAT_RGB24 :
9715                                             content == CAIRO_CONTENT_ALPHA ? CAIRO_FORMAT_A8 : CAIRO_FORMAT_ARGB32,
9716                                             width, height);
9717       break;
9718     case GDK_RENDERING_MODE_SIMILAR:
9719     default:
9720       surface = cairo_surface_create_similar (window_surface,
9721                                               content,
9722                                               width, height);
9723       break;
9724   }
9725
9726   cairo_surface_destroy (window_surface);
9727
9728   return surface;
9729 }
9730
9731 /**
9732  * gdk_window_focus:
9733  * @window: a #GdkWindow
9734  * @timestamp: timestamp of the event triggering the window focus
9735  *
9736  * Sets keyboard focus to @window. In most cases, gtk_window_present()
9737  * should be used on a #GtkWindow, rather than calling this function.
9738  *
9739  **/
9740 void
9741 gdk_window_focus (GdkWindow *window,
9742                   guint32    timestamp)
9743 {
9744   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->focus (window, timestamp);
9745 }
9746
9747 /**
9748  * gdk_window_set_type_hint:
9749  * @window: A toplevel #GdkWindow
9750  * @hint: A hint of the function this window will have
9751  *
9752  * The application can use this call to provide a hint to the window
9753  * manager about the functionality of a window. The window manager
9754  * can use this information when determining the decoration and behaviour
9755  * of the window.
9756  *
9757  * The hint must be set before the window is mapped.
9758  **/
9759 void
9760 gdk_window_set_type_hint (GdkWindow        *window,
9761                           GdkWindowTypeHint hint)
9762 {
9763   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_type_hint (window, hint);
9764 }
9765
9766 /**
9767  * gdk_window_get_type_hint:
9768  * @window: A toplevel #GdkWindow
9769  *
9770  * This function returns the type hint set for a window.
9771  *
9772  * Return value: The type hint set for @window
9773  *
9774  * Since: 2.10
9775  **/
9776 GdkWindowTypeHint
9777 gdk_window_get_type_hint (GdkWindow *window)
9778 {
9779   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_type_hint (window);
9780 }
9781
9782 /**
9783  * gdk_window_set_modal_hint:
9784  * @window: A toplevel #GdkWindow
9785  * @modal: %TRUE if the window is modal, %FALSE otherwise.
9786  *
9787  * The application can use this hint to tell the window manager
9788  * that a certain window has modal behaviour. The window manager
9789  * can use this information to handle modal windows in a special
9790  * way.
9791  *
9792  * You should only use this on windows for which you have
9793  * previously called gdk_window_set_transient_for()
9794  **/
9795 void
9796 gdk_window_set_modal_hint (GdkWindow *window,
9797                            gboolean   modal)
9798 {
9799   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_modal_hint (window, modal);
9800 }
9801
9802 /**
9803  * gdk_window_set_skip_taskbar_hint:
9804  * @window: a toplevel #GdkWindow
9805  * @skips_taskbar: %TRUE to skip the taskbar
9806  *
9807  * Toggles whether a window should appear in a task list or window
9808  * list. If a window's semantic type as specified with
9809  * gdk_window_set_type_hint() already fully describes the window, this
9810  * function should <emphasis>not</emphasis> be called in addition,
9811  * instead you should allow the window to be treated according to
9812  * standard policy for its semantic type.
9813  *
9814  * Since: 2.2
9815  **/
9816 void
9817 gdk_window_set_skip_taskbar_hint (GdkWindow *window,
9818                                   gboolean   skips_taskbar)
9819 {
9820   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_skip_taskbar_hint (window, skips_taskbar);
9821 }
9822
9823 /**
9824  * gdk_window_set_skip_pager_hint:
9825  * @window: a toplevel #GdkWindow
9826  * @skips_pager: %TRUE to skip the pager
9827  *
9828  * Toggles whether a window should appear in a pager (workspace
9829  * switcher, or other desktop utility program that displays a small
9830  * thumbnail representation of the windows on the desktop). If a
9831  * window's semantic type as specified with gdk_window_set_type_hint()
9832  * already fully describes the window, this function should
9833  * <emphasis>not</emphasis> be called in addition, instead you should
9834  * allow the window to be treated according to standard policy for
9835  * its semantic type.
9836  *
9837  * Since: 2.2
9838  **/
9839 void
9840 gdk_window_set_skip_pager_hint (GdkWindow *window,
9841                                 gboolean   skips_pager)
9842 {
9843   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_skip_pager_hint (window, skips_pager);
9844 }
9845
9846 /**
9847  * gdk_window_set_urgency_hint:
9848  * @window: a toplevel #GdkWindow
9849  * @urgent: %TRUE if the window is urgent
9850  *
9851  * Toggles whether a window needs the user's
9852  * urgent attention.
9853  *
9854  * Since: 2.8
9855  **/
9856 void
9857 gdk_window_set_urgency_hint (GdkWindow *window,
9858                              gboolean   urgent)
9859 {
9860   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_urgency_hint (window, urgent);
9861 }
9862
9863 /**
9864  * gdk_window_set_geometry_hints:
9865  * @window: a toplevel #GdkWindow
9866  * @geometry: geometry hints
9867  * @geom_mask: bitmask indicating fields of @geometry to pay attention to
9868  *
9869  * Sets the geometry hints for @window. Hints flagged in @geom_mask
9870  * are set, hints not flagged in @geom_mask are unset.
9871  * To unset all hints, use a @geom_mask of 0 and a @geometry of %NULL.
9872  *
9873  * This function provides hints to the windowing system about
9874  * acceptable sizes for a toplevel window. The purpose of
9875  * this is to constrain user resizing, but the windowing system
9876  * will typically  (but is not required to) also constrain the
9877  * current size of the window to the provided values and
9878  * constrain programatic resizing via gdk_window_resize() or
9879  * gdk_window_move_resize().
9880  *
9881  * Note that on X11, this effect has no effect on windows
9882  * of type %GDK_WINDOW_TEMP or windows where override redirect
9883  * has been turned on via gdk_window_set_override_redirect()
9884  * since these windows are not resizable by the user.
9885  *
9886  * Since you can't count on the windowing system doing the
9887  * constraints for programmatic resizes, you should generally
9888  * call gdk_window_constrain_size() yourself to determine
9889  * appropriate sizes.
9890  *
9891  **/
9892 void
9893 gdk_window_set_geometry_hints (GdkWindow         *window,
9894                                const GdkGeometry *geometry,
9895                                GdkWindowHints     geom_mask)
9896 {
9897   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_geometry_hints (window, geometry, geom_mask);
9898 }
9899
9900 /**
9901  * gdk_window_set_title:
9902  * @window: a toplevel #GdkWindow
9903  * @title: title of @window
9904  *
9905  * Sets the title of a toplevel window, to be displayed in the titlebar.
9906  * If you haven't explicitly set the icon name for the window
9907  * (using gdk_window_set_icon_name()), the icon name will be set to
9908  * @title as well. @title must be in UTF-8 encoding (as with all
9909  * user-readable strings in GDK/GTK+). @title may not be %NULL.
9910  **/
9911 void
9912 gdk_window_set_title (GdkWindow   *window,
9913                       const gchar *title)
9914 {
9915   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_title (window, title);
9916 }
9917
9918 /**
9919  * gdk_window_set_role:
9920  * @window: a toplevel #GdkWindow
9921  * @role: a string indicating its role
9922  *
9923  * When using GTK+, typically you should use gtk_window_set_role() instead
9924  * of this low-level function.
9925  *
9926  * The window manager and session manager use a window's role to
9927  * distinguish it from other kinds of window in the same application.
9928  * When an application is restarted after being saved in a previous
9929  * session, all windows with the same title and role are treated as
9930  * interchangeable.  So if you have two windows with the same title
9931  * that should be distinguished for session management purposes, you
9932  * should set the role on those windows. It doesn't matter what string
9933  * you use for the role, as long as you have a different role for each
9934  * non-interchangeable kind of window.
9935  *
9936  **/
9937 void
9938 gdk_window_set_role (GdkWindow   *window,
9939                      const gchar *role)
9940 {
9941   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_role (window, role);
9942 }
9943
9944 /**
9945  * gdk_window_set_startup_id:
9946  * @window: a toplevel #GdkWindow
9947  * @startup_id: a string with startup-notification identifier
9948  *
9949  * When using GTK+, typically you should use gtk_window_set_startup_id()
9950  * instead of this low-level function.
9951  *
9952  * Since: 2.12
9953  *
9954  **/
9955 void
9956 gdk_window_set_startup_id (GdkWindow   *window,
9957                            const gchar *startup_id)
9958 {
9959   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_startup_id (window, startup_id);
9960 }
9961
9962 /**
9963  * gdk_window_set_transient_for:
9964  * @window: a toplevel #GdkWindow
9965  * @parent: another toplevel #GdkWindow
9966  *
9967  * Indicates to the window manager that @window is a transient dialog
9968  * associated with the application window @parent. This allows the
9969  * window manager to do things like center @window on @parent and
9970  * keep @window above @parent.
9971  *
9972  * See gtk_window_set_transient_for() if you're using #GtkWindow or
9973  * #GtkDialog.
9974  **/
9975 void
9976 gdk_window_set_transient_for (GdkWindow *window,
9977                               GdkWindow *parent)
9978 {
9979   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_transient_for (window, parent);
9980 }
9981
9982 /**
9983  * gdk_window_get_root_origin:
9984  * @window: a toplevel #GdkWindow
9985  * @x: (out): return location for X position of window frame
9986  * @y: (out): return location for Y position of window frame
9987  *
9988  * Obtains the top-left corner of the window manager frame in root
9989  * window coordinates.
9990  *
9991  **/
9992 void
9993 gdk_window_get_root_origin (GdkWindow *window,
9994                             gint      *x,
9995                             gint      *y)
9996 {
9997   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_root_origin (window, x, y);
9998 }
9999
10000 /**
10001  * gdk_window_get_frame_extents:
10002  * @window: a toplevel #GdkWindow
10003  * @rect: rectangle to fill with bounding box of the window frame
10004  *
10005  * Obtains the bounding box of the window, including window manager
10006  * titlebar/borders if any. The frame position is given in root window
10007  * coordinates. To get the position of the window itself (rather than
10008  * the frame) in root window coordinates, use gdk_window_get_origin().
10009  *
10010  **/
10011 void
10012 gdk_window_get_frame_extents (GdkWindow    *window,
10013                               GdkRectangle *rect)
10014 {
10015   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_frame_extents (window, rect);
10016 }
10017
10018 /**
10019  * gdk_window_set_override_redirect:
10020  * @window: a toplevel #GdkWindow
10021  * @override_redirect: %TRUE if window should be override redirect
10022  *
10023  * An override redirect window is not under the control of the window manager.
10024  * This means it won't have a titlebar, won't be minimizable, etc. - it will
10025  * be entirely under the control of the application. The window manager
10026  * can't see the override redirect window at all.
10027  *
10028  * Override redirect should only be used for short-lived temporary
10029  * windows, such as popup menus. #GtkMenu uses an override redirect
10030  * window in its implementation, for example.
10031  *
10032  **/
10033 void
10034 gdk_window_set_override_redirect (GdkWindow *window,
10035                                   gboolean override_redirect)
10036 {
10037   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_override_redirect (window, override_redirect);
10038 }
10039
10040 /**
10041  * gdk_window_set_accept_focus:
10042  * @window: a toplevel #GdkWindow
10043  * @accept_focus: %TRUE if the window should receive input focus
10044  *
10045  * Setting @accept_focus to %FALSE hints the desktop environment that the
10046  * window doesn't want to receive input focus.
10047  *
10048  * On X, it is the responsibility of the window manager to interpret this
10049  * hint. ICCCM-compliant window manager usually respect it.
10050  *
10051  * Since: 2.4
10052  **/
10053 void
10054 gdk_window_set_accept_focus (GdkWindow *window,
10055                              gboolean accept_focus)
10056 {
10057   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_accept_focus (window, accept_focus);
10058 }
10059
10060 /**
10061  * gdk_window_set_focus_on_map:
10062  * @window: a toplevel #GdkWindow
10063  * @focus_on_map: %TRUE if the window should receive input focus when mapped
10064  *
10065  * Setting @focus_on_map to %FALSE hints the desktop environment that the
10066  * window doesn't want to receive input focus when it is mapped.
10067  * focus_on_map should be turned off for windows that aren't triggered
10068  * interactively (such as popups from network activity).
10069  *
10070  * On X, it is the responsibility of the window manager to interpret
10071  * this hint. Window managers following the freedesktop.org window
10072  * manager extension specification should respect it.
10073  *
10074  * Since: 2.6
10075  **/
10076 void
10077 gdk_window_set_focus_on_map (GdkWindow *window,
10078                              gboolean focus_on_map)
10079 {
10080   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_focus_on_map (window, focus_on_map);
10081 }
10082
10083 /**
10084  * gdk_window_set_icon_list:
10085  * @window: The #GdkWindow toplevel window to set the icon of.
10086  * @pixbufs: (transfer none) (element-type GdkPixbuf):
10087  *     A list of pixbufs, of different sizes.
10088  *
10089  * Sets a list of icons for the window. One of these will be used
10090  * to represent the window when it has been iconified. The icon is
10091  * usually shown in an icon box or some sort of task bar. Which icon
10092  * size is shown depends on the window manager. The window manager
10093  * can scale the icon  but setting several size icons can give better
10094  * image quality since the window manager may only need to scale the
10095  * icon by a small amount or not at all.
10096  *
10097  **/
10098 void
10099 gdk_window_set_icon_list (GdkWindow *window,
10100                           GList     *pixbufs)
10101 {
10102   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_icon_list (window, pixbufs);
10103 }
10104
10105 /**
10106  * gdk_window_set_icon_name:
10107  * @window: a toplevel #GdkWindow
10108  * @name: (allow-none): name of window while iconified (minimized)
10109  *
10110  * Windows may have a name used while minimized, distinct from the
10111  * name they display in their titlebar. Most of the time this is a bad
10112  * idea from a user interface standpoint. But you can set such a name
10113  * with this function, if you like.
10114  *
10115  * After calling this with a non-%NULL @name, calls to gdk_window_set_title()
10116  * will not update the icon title.
10117  *
10118  * Using %NULL for @name unsets the icon title; further calls to
10119  * gdk_window_set_title() will again update the icon title as well.
10120  **/
10121 void
10122 gdk_window_set_icon_name (GdkWindow   *window,
10123                           const gchar *name)
10124 {
10125   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_icon_name (window, name);
10126 }
10127
10128 /**
10129  * gdk_window_iconify:
10130  * @window: a toplevel #GdkWindow
10131  *
10132  * Asks to iconify (minimize) @window. The window manager may choose
10133  * to ignore the request, but normally will honor it. Using
10134  * gtk_window_iconify() is preferred, if you have a #GtkWindow widget.
10135  *
10136  * This function only makes sense when @window is a toplevel window.
10137  *
10138  **/
10139 void
10140 gdk_window_iconify (GdkWindow *window)
10141 {
10142   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->iconify (window);
10143 }
10144
10145 /**
10146  * gdk_window_deiconify:
10147  * @window: a toplevel #GdkWindow
10148  *
10149  * Attempt to deiconify (unminimize) @window. On X11 the window manager may
10150  * choose to ignore the request to deiconify. When using GTK+,
10151  * use gtk_window_deiconify() instead of the #GdkWindow variant. Or better yet,
10152  * you probably want to use gtk_window_present(), which raises the window, focuses it,
10153  * unminimizes it, and puts it on the current desktop.
10154  *
10155  **/
10156 void
10157 gdk_window_deiconify (GdkWindow *window)
10158 {
10159   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->deiconify (window);
10160 }
10161
10162 /**
10163  * gdk_window_stick:
10164  * @window: a toplevel #GdkWindow
10165  *
10166  * "Pins" a window such that it's on all workspaces and does not scroll
10167  * with viewports, for window managers that have scrollable viewports.
10168  * (When using #GtkWindow, gtk_window_stick() may be more useful.)
10169  *
10170  * On the X11 platform, this function depends on window manager
10171  * support, so may have no effect with many window managers. However,
10172  * GDK will do the best it can to convince the window manager to stick
10173  * the window. For window managers that don't support this operation,
10174  * there's nothing you can do to force it to happen.
10175  *
10176  **/
10177 void
10178 gdk_window_stick (GdkWindow *window)
10179 {
10180   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->stick (window);
10181 }
10182
10183 /**
10184  * gdk_window_unstick:
10185  * @window: a toplevel #GdkWindow
10186  *
10187  * Reverse operation for gdk_window_stick(); see gdk_window_stick(),
10188  * and gtk_window_unstick().
10189  *
10190  **/
10191 void
10192 gdk_window_unstick (GdkWindow *window)
10193 {
10194   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unstick (window);
10195 }
10196
10197 /**
10198  * gdk_window_maximize:
10199  * @window: a toplevel #GdkWindow
10200  *
10201  * Maximizes the window. If the window was already maximized, then
10202  * this function does nothing.
10203  *
10204  * On X11, asks the window manager to maximize @window, if the window
10205  * manager supports this operation. Not all window managers support
10206  * this, and some deliberately ignore it or don't have a concept of
10207  * "maximized"; so you can't rely on the maximization actually
10208  * happening. But it will happen with most standard window managers,
10209  * and GDK makes a best effort to get it to happen.
10210  *
10211  * On Windows, reliably maximizes the window.
10212  *
10213  **/
10214 void
10215 gdk_window_maximize (GdkWindow *window)
10216 {
10217   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->maximize (window);
10218 }
10219
10220 /**
10221  * gdk_window_unmaximize:
10222  * @window: a toplevel #GdkWindow
10223  *
10224  * Unmaximizes the window. If the window wasn't maximized, then this
10225  * function does nothing.
10226  *
10227  * On X11, asks the window manager to unmaximize @window, if the
10228  * window manager supports this operation. Not all window managers
10229  * support this, and some deliberately ignore it or don't have a
10230  * concept of "maximized"; so you can't rely on the unmaximization
10231  * actually happening. But it will happen with most standard window
10232  * managers, and GDK makes a best effort to get it to happen.
10233  *
10234  * On Windows, reliably unmaximizes the window.
10235  *
10236  **/
10237 void
10238 gdk_window_unmaximize (GdkWindow *window)
10239 {
10240   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unmaximize (window);
10241 }
10242
10243 /**
10244  * gdk_window_fullscreen:
10245  * @window: a toplevel #GdkWindow
10246  *
10247  * Moves the window into fullscreen mode. This means the
10248  * window covers the entire screen and is above any panels
10249  * or task bars.
10250  *
10251  * If the window was already fullscreen, then this function does nothing.
10252  *
10253  * On X11, asks the window manager to put @window in a fullscreen
10254  * state, if the window manager supports this operation. Not all
10255  * window managers support this, and some deliberately ignore it or
10256  * don't have a concept of "fullscreen"; so you can't rely on the
10257  * fullscreenification actually happening. But it will happen with
10258  * most standard window managers, and GDK makes a best effort to get
10259  * it to happen.
10260  *
10261  * Since: 2.2
10262  **/
10263 void
10264 gdk_window_fullscreen (GdkWindow *window)
10265 {
10266   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->fullscreen (window);
10267 }
10268
10269 /**
10270  * gdk_window_unfullscreen:
10271  * @window: a toplevel #GdkWindow
10272  *
10273  * Moves the window out of fullscreen mode. If the window was not
10274  * fullscreen, does nothing.
10275  *
10276  * On X11, asks the window manager to move @window out of the fullscreen
10277  * state, if the window manager supports this operation. Not all
10278  * window managers support this, and some deliberately ignore it or
10279  * don't have a concept of "fullscreen"; so you can't rely on the
10280  * unfullscreenification actually happening. But it will happen with
10281  * most standard window managers, and GDK makes a best effort to get
10282  * it to happen.
10283  *
10284  * Since: 2.2
10285  **/
10286 void
10287 gdk_window_unfullscreen (GdkWindow *window)
10288 {
10289   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unfullscreen (window);
10290 }
10291
10292 /**
10293  * gdk_window_set_keep_above:
10294  * @window: a toplevel #GdkWindow
10295  * @setting: whether to keep @window above other windows
10296  *
10297  * Set if @window must be kept above other windows. If the
10298  * window was already above, then this function does nothing.
10299  *
10300  * On X11, asks the window manager to keep @window above, if the window
10301  * manager supports this operation. Not all window managers support
10302  * this, and some deliberately ignore it or don't have a concept of
10303  * "keep above"; so you can't rely on the window being kept above.
10304  * But it will happen with most standard window managers,
10305  * and GDK makes a best effort to get it to happen.
10306  *
10307  * Since: 2.4
10308  **/
10309 void
10310 gdk_window_set_keep_above (GdkWindow *window,
10311                            gboolean   setting)
10312 {
10313   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_keep_above (window, setting);
10314 }
10315
10316 /**
10317  * gdk_window_set_keep_below:
10318  * @window: a toplevel #GdkWindow
10319  * @setting: whether to keep @window below other windows
10320  *
10321  * Set if @window must be kept below other windows. If the
10322  * window was already below, then this function does nothing.
10323  *
10324  * On X11, asks the window manager to keep @window below, if the window
10325  * manager supports this operation. Not all window managers support
10326  * this, and some deliberately ignore it or don't have a concept of
10327  * "keep below"; so you can't rely on the window being kept below.
10328  * But it will happen with most standard window managers,
10329  * and GDK makes a best effort to get it to happen.
10330  *
10331  * Since: 2.4
10332  **/
10333 void
10334 gdk_window_set_keep_below (GdkWindow *window, gboolean setting)
10335 {
10336   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_keep_below (window, setting);
10337 }
10338
10339 /**
10340  * gdk_window_get_group:
10341  * @window: a toplevel #GdkWindow
10342  *
10343  * Returns the group leader window for @window. See gdk_window_set_group().
10344  *
10345  * Return value: (transfer none): the group leader window for @window
10346  *
10347  * Since: 2.4
10348  **/
10349 GdkWindow *
10350 gdk_window_get_group (GdkWindow *window)
10351 {
10352   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_group (window);
10353 }
10354
10355 /**
10356  * gdk_window_set_group:
10357  * @window: a toplevel #GdkWindow
10358  * @leader: (allow-none): group leader window, or %NULL to restore the default group leader window
10359  *
10360  * Sets the group leader window for @window. By default,
10361  * GDK sets the group leader for all toplevel windows
10362  * to a global window implicitly created by GDK. With this function
10363  * you can override this default.
10364  *
10365  * The group leader window allows the window manager to distinguish
10366  * all windows that belong to a single application. It may for example
10367  * allow users to minimize/unminimize all windows belonging to an
10368  * application at once. You should only set a non-default group window
10369  * if your application pretends to be multiple applications.
10370  **/
10371 void
10372 gdk_window_set_group (GdkWindow *window,
10373                       GdkWindow *leader)
10374 {
10375   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_group (window, leader);
10376 }
10377
10378 /**
10379  * gdk_window_set_decorations:
10380  * @window: a toplevel #GdkWindow
10381  * @decorations: decoration hint mask
10382  *
10383  * "Decorations" are the features the window manager adds to a toplevel #GdkWindow.
10384  * This function sets the traditional Motif window manager hints that tell the
10385  * window manager which decorations you would like your window to have.
10386  * Usually you should use gtk_window_set_decorated() on a #GtkWindow instead of
10387  * using the GDK function directly.
10388  *
10389  * The @decorations argument is the logical OR of the fields in
10390  * the #GdkWMDecoration enumeration. If #GDK_DECOR_ALL is included in the
10391  * mask, the other bits indicate which decorations should be turned off.
10392  * If #GDK_DECOR_ALL is not included, then the other bits indicate
10393  * which decorations should be turned on.
10394  *
10395  * Most window managers honor a decorations hint of 0 to disable all decorations,
10396  * but very few honor all possible combinations of bits.
10397  *
10398  **/
10399 void
10400 gdk_window_set_decorations (GdkWindow      *window,
10401                             GdkWMDecoration decorations)
10402 {
10403   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_decorations (window, decorations);
10404 }
10405
10406 /**
10407  * gdk_window_get_decorations:
10408  * @window: The toplevel #GdkWindow to get the decorations from
10409  * @decorations: (out): The window decorations will be written here
10410  *
10411  * Returns the decorations set on the GdkWindow with
10412  * gdk_window_set_decorations().
10413  *
10414  * Returns: %TRUE if the window has decorations set, %FALSE otherwise.
10415  **/
10416 gboolean
10417 gdk_window_get_decorations(GdkWindow       *window,
10418                            GdkWMDecoration *decorations)
10419 {
10420   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_decorations (window, decorations);
10421 }
10422
10423 /**
10424  * gdk_window_set_functions:
10425  * @window: a toplevel #GdkWindow
10426  * @functions: bitmask of operations to allow on @window
10427  *
10428  * Sets hints about the window management functions to make available
10429  * via buttons on the window frame.
10430  *
10431  * On the X backend, this function sets the traditional Motif window
10432  * manager hint for this purpose. However, few window managers do
10433  * anything reliable or interesting with this hint. Many ignore it
10434  * entirely.
10435  *
10436  * The @functions argument is the logical OR of values from the
10437  * #GdkWMFunction enumeration. If the bitmask includes #GDK_FUNC_ALL,
10438  * then the other bits indicate which functions to disable; if
10439  * it doesn't include #GDK_FUNC_ALL, it indicates which functions to
10440  * enable.
10441  *
10442  **/
10443 void
10444 gdk_window_set_functions (GdkWindow    *window,
10445                           GdkWMFunction functions)
10446 {
10447   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_functions (window, functions);
10448 }
10449
10450 /**
10451  * gdk_window_begin_resize_drag_for_device:
10452  * @window: a toplevel #GdkWindow
10453  * @edge: the edge or corner from which the drag is started
10454  * @device: the device used for the operation
10455  * @button: the button being used to drag
10456  * @root_x: root window X coordinate of mouse click that began the drag
10457  * @root_y: root window Y coordinate of mouse click that began the drag
10458  * @timestamp: timestamp of mouse click that began the drag (use gdk_event_get_time())
10459  *
10460  * Begins a window resize operation (for a toplevel window).
10461  * You might use this function to implement a "window resize grip," for
10462  * example; in fact #GtkStatusbar uses it. The function works best
10463  * with window managers that support the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended Window Manager Hints</ulink>, but has a
10464  * fallback implementation for other window managers.
10465  *
10466  * Since: 3.4
10467  */
10468 void
10469 gdk_window_begin_resize_drag_for_device (GdkWindow     *window,
10470                                          GdkWindowEdge  edge,
10471                                          GdkDevice     *device,
10472                                          gint           button,
10473                                          gint           root_x,
10474                                          gint           root_y,
10475                                          guint32        timestamp)
10476 {
10477   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->begin_resize_drag (window, edge, device, button, root_x, root_y, timestamp);
10478 }
10479
10480 /**
10481  * gdk_window_begin_resize_drag:
10482  * @window: a toplevel #GdkWindow
10483  * @edge: the edge or corner from which the drag is started
10484  * @button: the button being used to drag
10485  * @root_x: root window X coordinate of mouse click that began the drag
10486  * @root_y: root window Y coordinate of mouse click that began the drag
10487  * @timestamp: timestamp of mouse click that began the drag (use gdk_event_get_time())
10488  *
10489  * Begins a window resize operation (for a toplevel window).
10490  *
10491  * This function assumes that the drag is controlled by the
10492  * client pointer device, use gdk_window_begin_resize_drag_for_device()
10493  * to begin a drag with a different device.
10494  */
10495 void
10496 gdk_window_begin_resize_drag (GdkWindow     *window,
10497                               GdkWindowEdge  edge,
10498                               gint           button,
10499                               gint           root_x,
10500                               gint           root_y,
10501                               guint32        timestamp)
10502 {
10503   GdkDeviceManager *device_manager;
10504   GdkDevice *device;
10505
10506   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
10507   device = gdk_device_manager_get_client_pointer (device_manager);
10508   gdk_window_begin_resize_drag_for_device (window, edge,
10509                                            device, button, root_x, root_y, timestamp);
10510 }
10511
10512 /**
10513  * gdk_window_begin_move_drag_for_device:
10514  * @window: a toplevel #GdkWindow
10515  * @device: the device used for the operation
10516  * @button: the button being used to drag
10517  * @root_x: root window X coordinate of mouse click that began the drag
10518  * @root_y: root window Y coordinate of mouse click that began the drag
10519  * @timestamp: timestamp of mouse click that began the drag
10520  *
10521  * Begins a window move operation (for a toplevel window).
10522  * You might use this function to implement a "window move grip," for
10523  * example. The function works best with window managers that support
10524  * the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
10525  * Window Manager Hints</ulink>, but has a fallback implementation for
10526  * other window managers.
10527  *
10528  * Since: 3.4
10529  */
10530 void
10531 gdk_window_begin_move_drag_for_device (GdkWindow *window,
10532                                        GdkDevice *device,
10533                                        gint       button,
10534                                        gint       root_x,
10535                                        gint       root_y,
10536                                        guint32    timestamp)
10537 {
10538   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->begin_move_drag (window,
10539                                                              device, button, root_x, root_y, timestamp);
10540 }
10541
10542 /**
10543  * gdk_window_begin_move_drag:
10544  * @window: a toplevel #GdkWindow
10545  * @button: the button being used to drag
10546  * @root_x: root window X coordinate of mouse click that began the drag
10547  * @root_y: root window Y coordinate of mouse click that began the drag
10548  * @timestamp: timestamp of mouse click that began the drag
10549  *
10550  * Begins a window move operation (for a toplevel window).
10551  *
10552  * This function assumes that the drag is controlled by the
10553  * client pointer device, use gdk_window_begin_move_drag_for_device()
10554  * to begin a drag with a different device.
10555  */
10556 void
10557 gdk_window_begin_move_drag (GdkWindow *window,
10558                             gint       button,
10559                             gint       root_x,
10560                             gint       root_y,
10561                             guint32    timestamp)
10562 {
10563   GdkDeviceManager *device_manager;
10564   GdkDevice *device;
10565
10566   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
10567   device = gdk_device_manager_get_client_pointer (device_manager);
10568   gdk_window_begin_move_drag_for_device (window, device, button, root_x, root_y, timestamp);
10569 }
10570
10571 /**
10572  * gdk_window_enable_synchronized_configure:
10573  * @window: a toplevel #GdkWindow
10574  *
10575  * Indicates that the application will cooperate with the window
10576  * system in synchronizing the window repaint with the window
10577  * manager during resizing operations. After an application calls
10578  * this function, it must call gdk_window_configure_finished() every
10579  * time it has finished all processing associated with a set of
10580  * Configure events. Toplevel GTK+ windows automatically use this
10581  * protocol.
10582  *
10583  * On X, calling this function makes @window participate in the
10584  * _NET_WM_SYNC_REQUEST window manager protocol.
10585  *
10586  * Since: 2.6
10587  **/
10588 void
10589 gdk_window_enable_synchronized_configure (GdkWindow *window)
10590 {
10591   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->enable_synchronized_configure (window);
10592 }
10593
10594 /**
10595  * gdk_window_configure_finished:
10596  * @window: a toplevel #GdkWindow
10597  * 
10598  * Signal to the window system that the application has finished
10599  * handling Configure events it has received. Window Managers can
10600  * use this to better synchronize the frame repaint with the
10601  * application. GTK+ applications will automatically call this
10602  * function when appropriate.
10603  *
10604  * This function can only be called if gdk_window_enable_synchronized_configure()
10605  * was called previously.
10606  *
10607  * Since: 2.6
10608  **/
10609 void
10610 gdk_window_configure_finished (GdkWindow *window)
10611 {
10612   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->configure_finished (window);
10613 }
10614
10615 /**
10616  * gdk_window_set_opacity:
10617  * @window: a top-level #GdkWindow
10618  * @opacity: opacity
10619  *
10620  * Request the windowing system to make @window partially transparent,
10621  * with opacity 0 being fully transparent and 1 fully opaque. (Values
10622  * of the opacity parameter are clamped to the [0,1] range.) 
10623  *
10624  * On X11, this works only on X screens with a compositing manager 
10625  * running.
10626  *
10627  * For setting up per-pixel alpha, see gdk_screen_get_rgba_visual().
10628  * For making non-toplevel windows translucent, see 
10629  * gdk_window_set_composited().
10630  *
10631  * Since: 2.12
10632  */
10633 void
10634 gdk_window_set_opacity (GdkWindow *window,
10635                         gdouble    opacity)
10636 {
10637   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_opacity (window, opacity);
10638 }
10639
10640 /* This function is called when the XWindow is really gone.
10641  */
10642 void
10643 gdk_window_destroy_notify (GdkWindow *window)
10644 {
10645   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->destroy_notify (window);
10646 }
10647
10648 /**
10649  * gdk_window_register_dnd:
10650  * @window: a #GdkWindow.
10651  *
10652  * Registers a window as a potential drop destination.
10653  */
10654 void
10655 gdk_window_register_dnd (GdkWindow *window)
10656 {
10657   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->register_dnd (window);
10658 }
10659
10660 /**
10661  * gdk_window_get_drag_protocol:
10662  * @window: the destination window
10663  * @target: (out) (allow-none) (transfer full): location of the window
10664  *    where the drop should happen. This may be @window or a proxy window,
10665  *    or %NULL if @window does not support Drag and Drop.
10666  *
10667  * Finds out the DND protocol supported by a window.
10668  *
10669  * Returns: the supported DND protocol.
10670  *
10671  * Since: 3.0
10672  */
10673 GdkDragProtocol
10674 gdk_window_get_drag_protocol (GdkWindow  *window,
10675                               GdkWindow **target)
10676 {
10677   g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_DRAG_PROTO_NONE);
10678
10679   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_drag_protocol (window, target);
10680 }
10681
10682 /**
10683  * gdk_drag_begin:
10684  * @window: the source window for this drag.
10685  * @targets: (transfer none) (element-type GdkAtom): the offered targets,
10686  *     as list of #GdkAtoms
10687  *
10688  * Starts a drag and creates a new drag context for it.
10689  * This function assumes that the drag is controlled by the
10690  * client pointer device, use gdk_drag_begin_for_device() to
10691  * begin a drag with a different device.
10692  *
10693  * This function is called by the drag source.
10694  *
10695  * Return value: (transfer full): a newly created #GdkDragContext
10696  */
10697 GdkDragContext *
10698 gdk_drag_begin (GdkWindow     *window,
10699                 GList         *targets)
10700 {
10701   GdkDeviceManager *device_manager;
10702   GdkDevice *device;
10703
10704   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
10705   device = gdk_device_manager_get_client_pointer (device_manager);
10706
10707   return gdk_drag_begin_for_device (window, device, targets);
10708 }
10709
10710 /**
10711  * gdk_drag_begin_for_device:
10712  * @window: the source window for this drag
10713  * @device: the device that controls this drag
10714  * @targets: (transfer none) (element-type GdkAtom): the offered targets,
10715  *     as list of #GdkAtoms
10716  *
10717  * Starts a drag and creates a new drag context for it.
10718  *
10719  * This function is called by the drag source.
10720  *
10721  * Return value: (transfer full): a newly created #GdkDragContext
10722  */
10723 GdkDragContext *
10724 gdk_drag_begin_for_device (GdkWindow     *window,
10725                            GdkDevice     *device,
10726                            GList         *targets)
10727 {
10728   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->drag_begin (window, device, targets);
10729 }
10730
10731 /**
10732  * gdk_test_render_sync:
10733  * @window: a mapped #GdkWindow
10734  *
10735  * Retrieves a pixel from @window to force the windowing
10736  * system to carry out any pending rendering commands.
10737  *
10738  * This function is intended to be used to synchronize with rendering
10739  * pipelines, to benchmark windowing system rendering operations.
10740  *
10741  * Since: 2.14
10742  **/
10743 void
10744 gdk_test_render_sync (GdkWindow *window)
10745 {
10746   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->sync_rendering (window);
10747 }
10748
10749 /**
10750  * gdk_test_simulate_key:
10751  * @window: a #GdkWindow to simulate a key event for
10752  * @x:      x coordinate within @window for the key event
10753  * @y:      y coordinate within @window for the key event
10754  * @keyval: A GDK keyboard value
10755  * @modifiers: Keyboard modifiers the event is setup with
10756  * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
10757  *
10758  * This function is intended to be used in GTK+ test programs.
10759  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
10760  * the given (@x,@y) coordinates within @window and simulate a
10761  * key press or release event.
10762  *
10763  * When the mouse pointer is warped to the target location, use
10764  * of this function outside of test programs that run in their
10765  * own virtual windowing system (e.g. Xvfb) is not recommended.
10766  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
10767  * be warped and @window origin will be used as mouse pointer
10768  * location for the event.
10769  *
10770  * Also, gdk_test_simulate_key() is a fairly low level function,
10771  * for most testing purposes, gtk_test_widget_send_key() is the
10772  * right function to call which will generate a key press event
10773  * followed by its accompanying key release event.
10774  *
10775  * Returns: whether all actions necessary for a key event simulation
10776  *     were carried out successfully
10777  *
10778  * Since: 2.14
10779  */
10780 gboolean
10781 gdk_test_simulate_key (GdkWindow      *window,
10782                        gint            x,
10783                        gint            y,
10784                        guint           keyval,
10785                        GdkModifierType modifiers,
10786                        GdkEventType    key_pressrelease)
10787 {
10788   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
10789     ->simulate_key (window, x, y, keyval, modifiers, key_pressrelease);
10790 }
10791
10792 /**
10793  * gdk_test_simulate_button:
10794  * @window: a #GdkWindow to simulate a button event for
10795  * @x:      x coordinate within @window for the button event
10796  * @y:      y coordinate within @window for the button event
10797  * @button: Number of the pointer button for the event, usually 1, 2 or 3
10798  * @modifiers: Keyboard modifiers the event is setup with
10799  * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
10800  *
10801  * This function is intended to be used in GTK+ test programs.
10802  * It will warp the mouse pointer to the given (@x,@y) coordinates
10803  * within @window and simulate a button press or release event.
10804  * Because the mouse pointer needs to be warped to the target
10805  * location, use of this function outside of test programs that
10806  * run in their own virtual windowing system (e.g. Xvfb) is not
10807  * recommended.
10808  *
10809 * Also, gdk_test_simulate_button() is a fairly low level function,
10810  * for most testing purposes, gtk_test_widget_click() is the right
10811  * function to call which will generate a button press event followed
10812  * by its accompanying button release event.
10813  *
10814  * Returns: whether all actions necessary for a button event simulation
10815  *     were carried out successfully
10816  *
10817  * Since: 2.14
10818  */
10819 gboolean
10820 gdk_test_simulate_button (GdkWindow      *window,
10821                           gint            x,
10822                           gint            y,
10823                           guint           button, /*1..3*/
10824                           GdkModifierType modifiers,
10825                           GdkEventType    button_pressrelease)
10826 {
10827   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
10828     ->simulate_button (window, x, y, button, modifiers, button_pressrelease);
10829 }
10830
10831 /**
10832  * gdk_property_get:
10833  * @window: a #GdkWindow
10834  * @property: the property to retrieve
10835  * @type: the desired property type, or %GDK_NONE, if any type of data
10836  *   is acceptable. If this does not match the actual
10837  *   type, then @actual_format and @actual_length will
10838  *   be filled in, a warning will be printed to stderr
10839  *   and no data will be returned.
10840  * @offset: the offset into the property at which to begin
10841  *   retrieving data, in 4 byte units.
10842  * @length: the length of the data to retrieve in bytes.  Data is
10843  *   considered to be retrieved in 4 byte chunks, so @length
10844  *   will be rounded up to the next highest 4 byte boundary
10845  *   (so be careful not to pass a value that might overflow
10846  *   when rounded up).
10847  * @pdelete: if %TRUE, delete the property after retrieving the
10848  *   data.
10849  * @actual_property_type: (out) (transfer none): location to store the
10850  *   actual type of the property.
10851  * @actual_format: (out): location to store the actual return format of the
10852  *   data; either 8, 16 or 32 bits.
10853  * @actual_length: location to store the length of the retrieved data, in
10854  *   bytes.  Data returned in the 32 bit format is stored
10855  *   in a long variable, so the actual number of 32 bit
10856  *   elements should be be calculated via
10857  *   @actual_length / sizeof(glong) to ensure portability to
10858  *   64 bit systems.
10859  * @data: (out) (array length=actual_length) (transfer full): location
10860  *   to store a pointer to the data. The retrieved data should be
10861  *   freed with g_free() when you are finished using it.
10862  *
10863  * Retrieves a portion of the contents of a property. If the
10864  * property does not exist, then the function returns %FALSE,
10865  * and %GDK_NONE will be stored in @actual_property_type.
10866  *
10867  * <note>
10868  * <para>
10869  * The XGetWindowProperty() function that gdk_property_get()
10870  * uses has a very confusing and complicated set of semantics.
10871  * Unfortunately, gdk_property_get() makes the situation
10872  * worse instead of better (the semantics should be considered
10873  * undefined), and also prints warnings to stderr in cases where it
10874  * should return a useful error to the program. You are advised to use
10875  * XGetWindowProperty() directly until a replacement function for
10876  * gdk_property_get()
10877  * is provided.
10878  * </para>
10879  * </note>
10880  *
10881  * Returns: %TRUE if data was successfully received and stored
10882  *   in @data, otherwise %FALSE.
10883  */
10884 gboolean
10885 gdk_property_get (GdkWindow  *window,
10886                   GdkAtom     property,
10887                   GdkAtom     type,
10888                   gulong      offset,
10889                   gulong      length,
10890                   gint        pdelete,
10891                   GdkAtom    *actual_property_type,
10892                   gint       *actual_format_type,
10893                   gint       *actual_length,
10894                   guchar    **data)
10895 {
10896   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
10897     ->get_property (window, property, type, offset, length, pdelete,
10898                     actual_property_type, actual_format_type,
10899                     actual_length, data);
10900 }
10901
10902 /**
10903  * gdk_property_change: (skip)
10904  * @window: a #GdkWindow
10905  * @property: the property to change
10906  * @type: the new type for the property. If @mode is
10907  *   %GDK_PROP_MODE_PREPEND or %GDK_PROP_MODE_APPEND, then this
10908  *   must match the existing type or an error will occur.
10909  * @format: the new format for the property. If @mode is
10910  *   %GDK_PROP_MODE_PREPEND or %GDK_PROP_MODE_APPEND, then this
10911  *   must match the existing format or an error will occur.
10912  * @mode: a value describing how the new data is to be combined
10913  *   with the current data.
10914  * @data: the data (a <literal>guchar *</literal>
10915  *   <literal>gushort *</literal>, or <literal>gulong *</literal>,
10916  *   depending on @format), cast to a <literal>guchar *</literal>.
10917  * @nelements: the number of elements of size determined by the format,
10918  *   contained in @data.
10919  *
10920  * Changes the contents of a property on a window.
10921  */
10922 void
10923 gdk_property_change (GdkWindow    *window,
10924                      GdkAtom       property,
10925                      GdkAtom       type,
10926                      gint          format,
10927                      GdkPropMode   mode,
10928                      const guchar *data,
10929                      gint          nelements)
10930 {
10931   GDK_WINDOW_IMPL_GET_CLASS (window->impl)
10932     ->change_property (window, property, type, format, mode, data, nelements);
10933 }
10934
10935 /**
10936  * gdk_property_delete:
10937  * @window: a #GdkWindow
10938  * @property: the property to delete
10939  *
10940  * Deletes a property from a window.
10941  */
10942 void
10943 gdk_property_delete (GdkWindow *window,
10944                      GdkAtom    property)
10945 {
10946   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->delete_property (window, property);
10947 }