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