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