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