]> Pileus Git - ~andy/gtk/blob - gdk/gdkwindow.c
gdkwindow: Store the implicit paint in a list
[~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 = g_slist_prepend (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->data;
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->data;
2842
2843   window->implicit_paint = g_slist_delete_link (window->implicit_paint,
2844                                                 window->implicit_paint);
2845
2846   if (!GDK_WINDOW_DESTROYED (window) && !cairo_region_is_empty (paint->region))
2847     {
2848       cairo_t *cr;
2849
2850       /* Some regions are valid, push these to window now */
2851       cr = gdk_cairo_create_for_impl (window);
2852       gdk_cairo_region (cr, paint->region);
2853       cairo_clip (cr);
2854       cairo_set_source_surface (cr, paint->surface, 0, 0);
2855       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
2856       cairo_paint (cr);
2857       cairo_destroy (cr);
2858     }
2859   
2860   cairo_region_destroy (paint->region);
2861   if (paint->flushed)
2862     cairo_region_destroy (paint->flushed);
2863   cairo_surface_destroy (paint->surface);
2864   g_free (paint);
2865 }
2866
2867 /**
2868  * gdk_window_begin_paint_rect:
2869  * @window: a #GdkWindow
2870  * @rectangle: rectangle you intend to draw to
2871  *
2872  * A convenience wrapper around gdk_window_begin_paint_region() which
2873  * creates a rectangular region for you. See
2874  * gdk_window_begin_paint_region() for details.
2875  *
2876  **/
2877 void
2878 gdk_window_begin_paint_rect (GdkWindow          *window,
2879                              const GdkRectangle *rectangle)
2880 {
2881   cairo_region_t *region;
2882
2883   g_return_if_fail (GDK_IS_WINDOW (window));
2884
2885   region = cairo_region_create_rectangle (rectangle);
2886   gdk_window_begin_paint_region (window, region);
2887   cairo_region_destroy (region);
2888 }
2889
2890 /**
2891  * gdk_window_begin_paint_region:
2892  * @window: a #GdkWindow
2893  * @region: region you intend to draw to
2894  *
2895  * Indicates that you are beginning the process of redrawing @region.
2896  * A backing store (offscreen buffer) large enough to contain @region
2897  * will be created. The backing store will be initialized with the
2898  * background color or background surface for @window. Then, all
2899  * drawing operations performed on @window will be diverted to the
2900  * backing store.  When you call gdk_window_end_paint(), the backing
2901  * store will be copied to @window, making it visible onscreen. Only
2902  * the part of @window contained in @region will be modified; that is,
2903  * drawing operations are clipped to @region.
2904  *
2905  * The net result of all this is to remove flicker, because the user
2906  * sees the finished product appear all at once when you call
2907  * gdk_window_end_paint(). If you draw to @window directly without
2908  * calling gdk_window_begin_paint_region(), the user may see flicker
2909  * as individual drawing operations are performed in sequence.  The
2910  * clipping and background-initializing features of
2911  * gdk_window_begin_paint_region() are conveniences for the
2912  * programmer, so you can avoid doing that work yourself.
2913  *
2914  * When using GTK+, the widget system automatically places calls to
2915  * gdk_window_begin_paint_region() and gdk_window_end_paint() around
2916  * emissions of the expose_event signal. That is, if you're writing an
2917  * expose event handler, you can assume that the exposed area in
2918  * #GdkEventExpose has already been cleared to the window background,
2919  * is already set as the clip region, and already has a backing store.
2920  * Therefore in most cases, application code need not call
2921  * gdk_window_begin_paint_region(). (You can disable the automatic
2922  * calls around expose events on a widget-by-widget basis by calling
2923  * gtk_widget_set_double_buffered().)
2924  *
2925  * If you call this function multiple times before calling the
2926  * matching gdk_window_end_paint(), the backing stores are pushed onto
2927  * a stack. gdk_window_end_paint() copies the topmost backing store
2928  * onscreen, subtracts the topmost region from all other regions in
2929  * the stack, and pops the stack. All drawing operations affect only
2930  * the topmost backing store in the stack. One matching call to
2931  * gdk_window_end_paint() is required for each call to
2932  * gdk_window_begin_paint_region().
2933  *
2934  **/
2935 void
2936 gdk_window_begin_paint_region (GdkWindow       *window,
2937                                const cairo_region_t *region)
2938 {
2939 #ifdef USE_BACKING_STORE
2940   GdkRectangle clip_box;
2941   GdkWindowPaint *paint, *implicit_paint;
2942   GdkWindow *impl_window;
2943   GSList *list;
2944
2945   g_return_if_fail (GDK_IS_WINDOW (window));
2946
2947   if (GDK_WINDOW_DESTROYED (window))
2948     return;
2949
2950   if (GDK_IS_PAINTABLE (window->impl))
2951     {
2952       GdkPaintableIface *iface = GDK_PAINTABLE_GET_IFACE (window->impl);
2953
2954       if (iface->begin_paint_region)
2955         iface->begin_paint_region ((GdkPaintable*)window->impl, window, region);
2956
2957       return;
2958     }
2959
2960   impl_window = gdk_window_get_impl_window (window);
2961   implicit_paint = impl_window->implicit_paint != NULL ? impl_window->implicit_paint->data : NULL;
2962
2963   paint = g_new (GdkWindowPaint, 1);
2964   paint->region = cairo_region_copy (region);
2965
2966   cairo_region_intersect (paint->region, window->clip_region_with_children);
2967   cairo_region_get_extents (paint->region, &clip_box);
2968
2969   cairo_region_translate (paint->region, window->abs_x, window->abs_y);
2970
2971   /* Mark the region as valid on the implicit paint */
2972
2973   if (implicit_paint)
2974     cairo_region_union (implicit_paint->region, paint->region);
2975
2976   /* Convert back to normal coords */
2977   cairo_region_translate (paint->region, -window->abs_x, -window->abs_y);
2978
2979   if (implicit_paint)
2980     {
2981       paint->uses_implicit = TRUE;
2982       paint->surface = cairo_surface_create_for_rectangle (implicit_paint->surface,
2983                                                            window->abs_x + clip_box.x,
2984                                                            window->abs_y + clip_box.y,
2985                                                            MAX (clip_box.width, 1),
2986                                                            MAX (clip_box.height, 1));
2987     }
2988   else
2989     {
2990       paint->uses_implicit = FALSE;
2991       paint->surface = gdk_window_create_similar_surface (window,
2992                                                           gdk_window_get_content (window),
2993                                                           MAX (clip_box.width, 1),
2994                                                           MAX (clip_box.height, 1));
2995     }
2996
2997   /* Normally alpha backgrounded client side windows are composited on the implicit paint
2998      by being drawn in back to front order. However, if implicit paints are not used, for
2999      instance if it was flushed due to a non-double-buffered paint in the middle of the
3000      expose we need to copy in the existing data here. */
3001   if (gdk_window_has_alpha (window) &&
3002       (!implicit_paint ||
3003        (implicit_paint && implicit_paint->flushed != NULL && !cairo_region_is_empty (implicit_paint->flushed))))
3004     {
3005       cairo_t *cr = cairo_create (paint->surface);
3006       /* We can't use gdk_cairo_set_source_window here, as that might
3007          flush the implicit paint at an unfortunate time, since this
3008          would be detected as a draw during non-expose time */
3009       cairo_surface_t *source_surface  = gdk_window_ref_impl_surface (impl_window);
3010       cairo_set_source_surface (cr, source_surface,
3011                                 - (window->abs_x + clip_box.x),
3012                                 - (window->abs_y + clip_box.y));
3013       cairo_surface_destroy (source_surface);
3014
3015       /* Only read back the flushed area if any */
3016       if (implicit_paint)
3017         {
3018           cairo_region_t *flushed = cairo_region_copy (implicit_paint->flushed);
3019           /* Convert from impl coords */
3020           cairo_region_translate (flushed, -window->abs_x, -window->abs_y);
3021           cairo_region_intersect (flushed, paint->region);
3022           gdk_cairo_region (cr, flushed);
3023           cairo_clip (cr);
3024
3025           /* Convert to impl coords */
3026           cairo_region_translate (flushed, window->abs_x, window->abs_y);
3027           cairo_region_subtract (implicit_paint->flushed, flushed);
3028           cairo_region_destroy (flushed);
3029         }
3030       cairo_paint (cr);
3031       cairo_destroy (cr);
3032     }
3033
3034   cairo_surface_set_device_offset (paint->surface, -clip_box.x, -clip_box.y);
3035
3036   for (list = window->paint_stack; list != NULL; list = list->next)
3037     {
3038       GdkWindowPaint *tmp_paint = list->data;
3039
3040       cairo_region_subtract (tmp_paint->region, paint->region);
3041     }
3042
3043   window->paint_stack = g_slist_prepend (window->paint_stack, paint);
3044
3045   if (!cairo_region_is_empty (paint->region))
3046     {
3047       gdk_window_clear_backing_region (window,
3048                                        paint->region);
3049     }
3050
3051 #endif /* USE_BACKING_STORE */
3052 }
3053
3054 /**
3055  * gdk_window_end_paint:
3056  * @window: a #GdkWindow
3057  *
3058  * Indicates that the backing store created by the most recent call to
3059  * gdk_window_begin_paint_region() should be copied onscreen and
3060  * deleted, leaving the next-most-recent backing store or no backing
3061  * store at all as the active paint region. See
3062  * gdk_window_begin_paint_region() for full details. It is an error to
3063  * call this function without a matching
3064  * gdk_window_begin_paint_region() first.
3065  *
3066  **/
3067 void
3068 gdk_window_end_paint (GdkWindow *window)
3069 {
3070 #ifdef USE_BACKING_STORE
3071   GdkWindow *composited;
3072   GdkWindowPaint *paint;
3073   GdkRectangle clip_box;
3074   cairo_region_t *full_clip;
3075
3076   g_return_if_fail (GDK_IS_WINDOW (window));
3077
3078   if (GDK_WINDOW_DESTROYED (window))
3079     return;
3080
3081   if (GDK_IS_PAINTABLE (window->impl))
3082     {
3083       GdkPaintableIface *iface = GDK_PAINTABLE_GET_IFACE (window->impl);
3084
3085       if (iface->end_paint)
3086         iface->end_paint ((GdkPaintable*)window->impl);
3087       return;
3088     }
3089
3090   if (window->paint_stack == NULL)
3091     {
3092       g_warning (G_STRLOC": no preceding call to gdk_window_begin_paint_region(), see documentation");
3093       return;
3094     }
3095
3096   paint = window->paint_stack->data;
3097
3098   window->paint_stack = g_slist_delete_link (window->paint_stack,
3099                                              window->paint_stack);
3100
3101   cairo_region_get_extents (paint->region, &clip_box);
3102
3103   if (!paint->uses_implicit)
3104     {
3105       cairo_t *cr;
3106
3107       gdk_window_flush_outstanding_moves (window);
3108
3109       full_clip = cairo_region_copy (window->clip_region_with_children);
3110       cairo_region_intersect (full_clip, paint->region);
3111
3112       cr = gdk_cairo_create (window);
3113       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
3114       cairo_set_source_surface (cr, paint->surface, 0, 0);
3115       gdk_cairo_region (cr, full_clip);
3116       cairo_fill (cr);
3117
3118       cairo_destroy (cr);
3119       cairo_region_destroy (full_clip);
3120     }
3121
3122   cairo_surface_destroy (paint->surface);
3123   cairo_region_destroy (paint->region);
3124   g_free (paint);
3125
3126   /* find a composited window in our hierarchy to signal its
3127    * parent to redraw, calculating the clip box as we go...
3128    *
3129    * stop if parent becomes NULL since then we'd have nowhere
3130    * to draw (ie: 'composited' will always be non-NULL here).
3131    */
3132   for (composited = window;
3133        composited->parent;
3134        composited = composited->parent)
3135     {
3136       clip_box.x += composited->x;
3137       clip_box.y += composited->y;
3138       clip_box.width = MIN (clip_box.width, composited->parent->width - clip_box.x);
3139       clip_box.height = MIN (clip_box.height, composited->parent->height - clip_box.y);
3140
3141       if (composited->composited)
3142         {
3143           gdk_window_invalidate_rect (GDK_WINDOW (composited->parent),
3144                                       &clip_box, FALSE);
3145           break;
3146         }
3147     }
3148 #endif /* USE_BACKING_STORE */
3149 }
3150
3151 static void
3152 gdk_window_free_paint_stack (GdkWindow *window)
3153 {
3154   if (window->paint_stack)
3155     {
3156       GSList *tmp_list = window->paint_stack;
3157
3158       while (tmp_list)
3159         {
3160           GdkWindowPaint *paint = tmp_list->data;
3161
3162           if (tmp_list == window->paint_stack)
3163             cairo_surface_destroy (paint->surface);
3164
3165           cairo_region_destroy (paint->region);
3166           g_free (paint);
3167
3168           tmp_list = tmp_list->next;
3169         }
3170
3171       g_slist_free (window->paint_stack);
3172       window->paint_stack = NULL;
3173     }
3174 }
3175
3176 static void
3177 do_move_region_bits_on_impl (GdkWindow *impl_window,
3178                              cairo_region_t *dest_region, /* In impl window coords */
3179                              int dx, int dy)
3180 {
3181   GdkWindowImplClass *impl_class;
3182
3183   impl_class = GDK_WINDOW_IMPL_GET_CLASS (impl_window->impl);
3184
3185   impl_class->translate (impl_window, dest_region, dx, dy);
3186 }
3187
3188 static GdkWindowRegionMove *
3189 gdk_window_region_move_new (cairo_region_t *region,
3190                             int dx, int dy)
3191 {
3192   GdkWindowRegionMove *move;
3193
3194   move = g_slice_new (GdkWindowRegionMove);
3195   move->dest_region = cairo_region_copy (region);
3196   move->dx = dx;
3197   move->dy = dy;
3198
3199   return move;
3200 }
3201
3202 static void
3203 gdk_window_region_move_free (GdkWindowRegionMove *move)
3204 {
3205   cairo_region_destroy (move->dest_region);
3206   g_slice_free (GdkWindowRegionMove, move);
3207 }
3208
3209 static void
3210 append_move_region (GdkWindow *impl_window,
3211                     cairo_region_t *new_dest_region,
3212                     int dx, int dy)
3213 {
3214   GdkWindowRegionMove *move, *old_move;
3215   cairo_region_t *new_total_region, *old_total_region;
3216   cairo_region_t *source_overlaps_destination;
3217   cairo_region_t *non_overwritten;
3218   gboolean added_move;
3219   GList *l, *prev;
3220
3221   if (cairo_region_is_empty (new_dest_region))
3222     return;
3223
3224   /* In principle this could just append the move to the list of outstanding
3225      moves that will be replayed before drawing anything when we're handling
3226      exposes. However, we'd like to do a bit better since its commonly the case
3227      that we get multiple copies where A is copied to B and then B is copied
3228      to C, and we'd like to express this as a simple copy A to C operation. */
3229
3230   /* We approach this by taking the new move and pushing it ahead of moves
3231      starting at the end of the list and stopping when its not safe to do so.
3232      It's not safe to push past a move if either the source of the new move
3233      is in the destination of the old move, or if the destination of the new
3234      move is in the source of the new move, or if the destination of the new
3235      move overlaps the destination of the old move. We simplify this by
3236      just comparing the total regions (src + dest) */
3237   new_total_region = cairo_region_copy (new_dest_region);
3238   cairo_region_translate (new_total_region, -dx, -dy);
3239   cairo_region_union (new_total_region, new_dest_region);
3240
3241   added_move = FALSE;
3242   for (l = g_list_last (impl_window->outstanding_moves); l != NULL; l = prev)
3243     {
3244       prev = l->prev;
3245       old_move = l->data;
3246
3247       old_total_region = cairo_region_copy (old_move->dest_region);
3248       cairo_region_translate (old_total_region, -old_move->dx, -old_move->dy);
3249       cairo_region_union (old_total_region, old_move->dest_region);
3250
3251       cairo_region_intersect (old_total_region, new_total_region);
3252       /* If these regions intersect then its not safe to push the
3253          new region before the old one */
3254       if (!cairo_region_is_empty (old_total_region))
3255         {
3256           /* The area where the new moves source overlaps the old ones
3257              destination */
3258           source_overlaps_destination = cairo_region_copy (new_dest_region);
3259           cairo_region_translate (source_overlaps_destination, -dx, -dy);
3260           cairo_region_intersect (source_overlaps_destination, old_move->dest_region);
3261           cairo_region_translate (source_overlaps_destination, dx, dy);
3262
3263           /* We can do all sort of optimizations here, but to do things safely it becomes
3264              quite complicated. However, a very common case is that you copy something first,
3265              then copy all that or a subset of it to a new location (i.e. if you scroll twice
3266              in the same direction). We'd like to detect this case and optimize it to one
3267              copy. */
3268           if (cairo_region_equal (source_overlaps_destination, new_dest_region))
3269             {
3270               /* This means we might be able to replace the old move and the new one
3271                  with the new one read from the old ones source, and a second copy of
3272                  the non-overwritten parts of the old move. However, such a split
3273                  is only valid if the source in the old move isn't overwritten
3274                  by the destination of the new one */
3275
3276               /* the new destination of old move if split is ok: */
3277               non_overwritten = cairo_region_copy (old_move->dest_region);
3278               cairo_region_subtract (non_overwritten, new_dest_region);
3279               /* move to source region */
3280               cairo_region_translate (non_overwritten, -old_move->dx, -old_move->dy);
3281
3282               cairo_region_intersect (non_overwritten, new_dest_region);
3283               if (cairo_region_is_empty (non_overwritten))
3284                 {
3285                   added_move = TRUE;
3286                   move = gdk_window_region_move_new (new_dest_region,
3287                                                      dx + old_move->dx,
3288                                                      dy + old_move->dy);
3289
3290                   impl_window->outstanding_moves =
3291                     g_list_insert_before (impl_window->outstanding_moves,
3292                                           l, move);
3293                   cairo_region_subtract (old_move->dest_region, new_dest_region);
3294                 }
3295               cairo_region_destroy (non_overwritten);
3296             }
3297
3298           cairo_region_destroy (source_overlaps_destination);
3299           cairo_region_destroy (old_total_region);
3300           break;
3301         }
3302       cairo_region_destroy (old_total_region);
3303     }
3304
3305   cairo_region_destroy (new_total_region);
3306
3307   if (!added_move)
3308     {
3309       move = gdk_window_region_move_new (new_dest_region, dx, dy);
3310
3311       if (l == NULL)
3312         impl_window->outstanding_moves =
3313           g_list_prepend (impl_window->outstanding_moves,
3314                           move);
3315       else
3316         impl_window->outstanding_moves =
3317           g_list_insert_before (impl_window->outstanding_moves,
3318                                 l->next, move);
3319     }
3320 }
3321
3322 /* Moves bits and update area by dx/dy in impl window.
3323    Takes ownership of region to avoid copy (because we may change it) */
3324 static void
3325 move_region_on_impl (GdkWindow *impl_window,
3326                      cairo_region_t *region, /* In impl window coords */
3327                      int dx, int dy)
3328 {
3329   GSList *l;
3330
3331   if ((dx == 0 && dy == 0) ||
3332       cairo_region_is_empty (region))
3333     {
3334       cairo_region_destroy (region);
3335       return;
3336     }
3337
3338   g_assert (impl_window == gdk_window_get_impl_window (impl_window));
3339
3340   /* Move any old invalid regions in the copy source area by dx/dy */
3341   if (impl_window->update_area)
3342     {
3343       cairo_region_t *update_area;
3344
3345       update_area = cairo_region_copy (region);
3346
3347       /* Convert from target to source */
3348       cairo_region_translate (update_area, -dx, -dy);
3349       cairo_region_intersect (update_area, impl_window->update_area);
3350       /* We only copy the area, so keep the old update area invalid.
3351          It would be safe to remove it too, as code that uses
3352          move_region_on_impl generally also invalidate the source
3353          area. However, it would just use waste cycles. */
3354
3355       /* Convert back */
3356       cairo_region_translate (update_area, dx, dy);
3357       cairo_region_union (impl_window->update_area, update_area);
3358
3359       /* This area of the destination is now invalid,
3360          so no need to copy to it.  */
3361       cairo_region_subtract (region, update_area);
3362
3363       cairo_region_destroy (update_area);
3364     }
3365
3366   /* If we're currently exposing this window, don't copy to this
3367      destination, as it will be overdrawn when the expose is done,
3368      instead invalidate it and repaint later. */
3369   for (l = impl_window->implicit_paint; l != NULL; l = l->next)
3370     {
3371       GdkWindowPaint *implicit_paint = l->data;
3372       cairo_region_t *exposing;
3373
3374       exposing = cairo_region_copy (implicit_paint->region);
3375       cairo_region_intersect (exposing, region);
3376       cairo_region_subtract (region, exposing);
3377
3378       impl_window_add_update_area (impl_window, exposing);
3379       cairo_region_destroy (exposing);
3380     }
3381
3382   append_move_region (impl_window, region, dx, dy);
3383
3384   cairo_region_destroy (region);
3385 }
3386
3387 /* Flushes all outstanding changes to the window, call this
3388  * before drawing directly to the window (i.e. outside a begin/end_paint pair).
3389  */
3390 static void
3391 gdk_window_flush_outstanding_moves (GdkWindow *window)
3392 {
3393   GdkWindow *impl_window;
3394   GList *l, *outstanding;
3395   GdkWindowRegionMove *move;
3396
3397   impl_window = gdk_window_get_impl_window (window);
3398   outstanding = impl_window->outstanding_moves;
3399   impl_window->outstanding_moves = NULL;
3400
3401   for (l = outstanding; l != NULL; l = l->next)
3402     {
3403       move = l->data;
3404
3405       do_move_region_bits_on_impl (impl_window,
3406                                    move->dest_region, move->dx, move->dy);
3407
3408       gdk_window_region_move_free (move);
3409     }
3410
3411   g_list_free (outstanding);
3412 }
3413
3414 /**
3415  * gdk_window_flush:
3416  * @window: a #GdkWindow
3417  *
3418  * Flush all outstanding cached operations on a window, leaving the
3419  * window in a state which reflects all that has been drawn before.
3420  *
3421  * Gdk uses multiple kinds of caching to get better performance and
3422  * nicer drawing. For instance, during exposes all paints to a window
3423  * using double buffered rendering are keep on a surface until the last
3424  * window has been exposed. It also delays window moves/scrolls until
3425  * as long as possible until next update to avoid tearing when moving
3426  * windows.
3427  *
3428  * Normally this should be completely invisible to applications, as
3429  * we automatically flush the windows when required, but this might
3430  * be needed if you for instance mix direct native drawing with
3431  * gdk drawing. For Gtk widgets that don't use double buffering this
3432  * will be called automatically before sending the expose event.
3433  *
3434  * Since: 2.18
3435  **/
3436 void
3437 gdk_window_flush (GdkWindow *window)
3438 {
3439   gdk_window_flush_outstanding_moves (window);
3440   gdk_window_flush_implicit_paint (window);
3441 }
3442
3443 static void
3444 gdk_window_flush_recursive_helper (GdkWindow *window,
3445                                    GdkWindowImpl *impl)
3446 {
3447   GdkWindow *child;
3448   GList *l;
3449
3450   for (l = window->children; l != NULL; l = l->next)
3451     {
3452       child = l->data;
3453
3454       if (child->impl == impl)
3455         /* Same impl, ignore */
3456         gdk_window_flush_recursive_helper (child, impl);
3457       else
3458         gdk_window_flush_recursive (child);
3459     }
3460 }
3461
3462 static void
3463 gdk_window_flush_recursive (GdkWindow *window)
3464 {
3465   gdk_window_flush (window);
3466   gdk_window_flush_recursive_helper (window, window->impl);
3467 }
3468
3469 /**
3470  * gdk_window_get_clip_region:
3471  * @window: a #GdkWindow
3472  * 
3473  * Computes the region of a window that potentially can be written
3474  * to by drawing primitives. This region may not take into account
3475  * other factors such as if the window is obscured by other windows,
3476  * but no area outside of this region will be affected by drawing
3477  * primitives.
3478  * 
3479  * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
3480  *          when you are done.
3481  **/
3482 cairo_region_t*
3483 gdk_window_get_clip_region (GdkWindow *window)
3484 {
3485   cairo_region_t *result;
3486
3487   g_return_val_if_fail (GDK_WINDOW (window), NULL);
3488
3489   result = cairo_region_copy (window->clip_region);
3490
3491   if (window->paint_stack)
3492     {
3493       cairo_region_t *paint_region = cairo_region_create ();
3494       GSList *tmp_list = window->paint_stack;
3495
3496       while (tmp_list)
3497         {
3498           GdkWindowPaint *paint = tmp_list->data;
3499
3500           cairo_region_union (paint_region, paint->region);
3501
3502           tmp_list = tmp_list->next;
3503         }
3504
3505       cairo_region_intersect (result, paint_region);
3506       cairo_region_destroy (paint_region);
3507     }
3508
3509   return result;
3510 }
3511
3512 /**
3513  * gdk_window_get_visible_region:
3514  * @window: a #GdkWindow
3515  * 
3516  * Computes the region of the @window that is potentially visible.
3517  * This does not necessarily take into account if the window is
3518  * obscured by other windows, but no area outside of this region
3519  * is visible.
3520  * 
3521  * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
3522  *          when you are done.
3523  **/
3524 cairo_region_t *
3525 gdk_window_get_visible_region (GdkWindow *window)
3526 {
3527   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3528
3529   return cairo_region_copy (window->clip_region);
3530 }
3531
3532 static cairo_t *
3533 setup_backing_rect (GdkWindow *window, GdkWindowPaint *paint)
3534 {
3535   GdkWindow *bg_window;
3536   cairo_pattern_t *pattern = NULL;
3537   int x_offset = 0, y_offset = 0;
3538   cairo_t *cr;
3539
3540   cr = cairo_create (paint->surface);
3541
3542   for (bg_window = window; bg_window; bg_window = bg_window->parent)
3543     {
3544       pattern = gdk_window_get_background_pattern (bg_window);
3545       if (pattern)
3546         break;
3547
3548       x_offset += bg_window->x;
3549       y_offset += bg_window->y;
3550     }
3551
3552   if (pattern)
3553     {
3554       cairo_translate (cr, -x_offset, -y_offset);
3555       cairo_set_source (cr, pattern);
3556       cairo_translate (cr, x_offset, y_offset);
3557     }
3558   else
3559     cairo_set_source_rgb (cr, 0, 0, 0);
3560
3561   return cr;
3562 }
3563
3564 static void
3565 gdk_window_clear_backing_region (GdkWindow *window,
3566                                  cairo_region_t *region)
3567 {
3568   GdkWindowPaint *paint = window->paint_stack->data;
3569   cairo_region_t *clip;
3570   cairo_t *cr;
3571
3572   if (GDK_WINDOW_DESTROYED (window))
3573     return;
3574
3575   cr = setup_backing_rect (window, paint);
3576
3577   clip = cairo_region_copy (paint->region);
3578   cairo_region_intersect (clip, region);
3579
3580   gdk_cairo_region (cr, clip);
3581   cairo_fill (cr);
3582
3583   cairo_destroy (cr);
3584
3585   cairo_region_destroy (clip);
3586 }
3587
3588 static void
3589 gdk_window_drop_cairo_surface (GdkWindow *window)
3590 {
3591   if (window->cairo_surface)
3592     {
3593       cairo_surface_finish (window->cairo_surface);
3594       cairo_surface_set_user_data (window->cairo_surface, &gdk_window_cairo_key,
3595                                    NULL, NULL);
3596       window->cairo_surface = NULL;
3597     }
3598 }
3599
3600 static void
3601 gdk_window_cairo_surface_destroy (void *data)
3602 {
3603   GdkWindow *window = data;
3604
3605   window->cairo_surface = NULL;
3606 }
3607
3608 static cairo_surface_t *
3609 gdk_window_create_cairo_surface (GdkWindow *window,
3610                                  int width,
3611                                  int height)
3612 {
3613   cairo_surface_t *surface, *subsurface;
3614   
3615   surface = gdk_window_ref_impl_surface (window);
3616   if (gdk_window_has_impl (window))
3617     return surface;
3618
3619   subsurface = cairo_surface_create_for_rectangle (surface,
3620                                                    window->abs_x,
3621                                                    window->abs_y,
3622                                                    width,
3623                                                    height);
3624   cairo_surface_destroy (surface);
3625   return subsurface;
3626 }
3627
3628
3629 cairo_surface_t *
3630 _gdk_window_ref_cairo_surface (GdkWindow *window)
3631 {
3632   cairo_surface_t *surface;
3633
3634   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3635
3636   if (window->paint_stack)
3637     {
3638       GdkWindowPaint *paint = window->paint_stack->data;
3639
3640       surface = paint->surface;
3641       cairo_surface_reference (surface);
3642     }
3643   else
3644     {
3645
3646       /* This will be drawing directly to the window, so flush implicit paint */
3647       gdk_window_flush (window);
3648
3649       if (!window->cairo_surface)
3650         {
3651           window->cairo_surface = gdk_window_create_cairo_surface (window,
3652                                                                    window->width,
3653                                                                    window->height);
3654
3655           if (window->cairo_surface)
3656             {
3657               cairo_surface_set_user_data (window->cairo_surface, &gdk_window_cairo_key,
3658                                            window, gdk_window_cairo_surface_destroy);
3659             }
3660         }
3661       else
3662         cairo_surface_reference (window->cairo_surface);
3663
3664       surface = window->cairo_surface;
3665     }
3666
3667   return surface;
3668 }
3669
3670 /**
3671  * gdk_cairo_create:
3672  * @window: a #GdkWindow
3673  * 
3674  * Creates a Cairo context for drawing to @window.
3675  *
3676  * <note><warning>
3677  * Note that calling cairo_reset_clip() on the resulting #cairo_t will
3678  * produce undefined results, so avoid it at all costs.
3679  * </warning></note>
3680  *
3681  * Return value: A newly created Cairo context. Free with
3682  *  cairo_destroy() when you are done drawing.
3683  * 
3684  * Since: 2.8
3685  **/
3686 cairo_t *
3687 gdk_cairo_create (GdkWindow *window)
3688 {
3689   cairo_surface_t *surface;
3690   cairo_t *cr;
3691     
3692   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
3693
3694   surface = _gdk_window_ref_cairo_surface (window);
3695   cr = cairo_create (surface);
3696
3697   if (!window->paint_stack)
3698     {
3699       gdk_cairo_region (cr, window->clip_region_with_children);
3700       cairo_clip (cr);
3701     }
3702   else
3703     {
3704       GdkWindowPaint *paint = window->paint_stack->data;
3705
3706       /* Only needs to clip to region if piggybacking
3707          on an implicit paint */
3708       if (paint->uses_implicit)
3709         {
3710           gdk_cairo_region (cr, paint->region);
3711           cairo_clip (cr);
3712         }
3713     }
3714     
3715   cairo_surface_destroy (surface);
3716
3717   return cr;
3718 }
3719
3720 /* Code for dirty-region queueing
3721  */
3722 static GSList *update_windows = NULL;
3723 static guint update_idle = 0;
3724 static gboolean debug_updates = FALSE;
3725
3726 static inline gboolean
3727 gdk_window_is_ancestor (GdkWindow *window,
3728                         GdkWindow *ancestor)
3729 {
3730   while (window)
3731     {
3732       GdkWindow *parent = window->parent;
3733
3734       if (parent == ancestor)
3735         return TRUE;
3736
3737       window = parent;
3738     }
3739
3740   return FALSE;
3741 }
3742
3743 static void
3744 gdk_window_add_update_window (GdkWindow *window)
3745 {
3746   GSList *tmp;
3747   GSList *prev = NULL;
3748   gboolean has_ancestor_in_list = FALSE;
3749
3750   for (tmp = update_windows; tmp; tmp = tmp->next)
3751     {
3752       GdkWindow *parent = window->parent;
3753
3754       /*  check if tmp is an ancestor of "window"; if it is, set a
3755        *  flag indicating that all following windows are either
3756        *  children of "window" or from a differen hierarchy
3757        */
3758       if (!has_ancestor_in_list && gdk_window_is_ancestor (window, tmp->data))
3759         has_ancestor_in_list = TRUE;
3760
3761       /* insert in reverse stacking order when adding around siblings,
3762        * so processing updates properly paints over lower stacked windows
3763        */
3764       if (parent == GDK_WINDOW (tmp->data)->parent)
3765         {
3766           gint index = g_list_index (parent->children, window);
3767           for (; tmp && parent == GDK_WINDOW (tmp->data)->parent; tmp = tmp->next)
3768             {
3769               gint sibling_index = g_list_index (parent->children, tmp->data);
3770               if (index > sibling_index)
3771                 break;
3772               prev = tmp;
3773             }
3774           /* here, tmp got advanced past all lower stacked siblings */
3775           tmp = g_slist_prepend (tmp, window);
3776           if (prev)
3777             prev->next = tmp;
3778           else
3779             update_windows = tmp;
3780           return;
3781         }
3782
3783       /*  if "window" has an ancestor in the list and tmp is one of
3784        *  "window's" children, insert "window" before tmp
3785        */
3786       if (has_ancestor_in_list && gdk_window_is_ancestor (tmp->data, window))
3787         {
3788           tmp = g_slist_prepend (tmp, window);
3789
3790           if (prev)
3791             prev->next = tmp;
3792           else
3793             update_windows = tmp;
3794           return;
3795         }
3796
3797       /*  if we're at the end of the list and had an ancestor it it,
3798        *  append to the list
3799        */
3800       if (! tmp->next && has_ancestor_in_list)
3801         {
3802           tmp = g_slist_append (tmp, window);
3803           return;
3804         }
3805
3806       prev = tmp;
3807     }
3808
3809   /*  if all above checks failed ("window" is from a different
3810    *  hierarchy than what is already in the list) or the list is
3811    *  empty, prepend
3812    */
3813   update_windows = g_slist_prepend (update_windows, window);
3814 }
3815
3816 static void
3817 gdk_window_remove_update_window (GdkWindow *window)
3818 {
3819   update_windows = g_slist_remove (update_windows, window);
3820 }
3821
3822 static gboolean
3823 gdk_window_update_idle (gpointer data)
3824 {
3825   gdk_window_process_all_updates ();
3826
3827   return FALSE;
3828 }
3829
3830 static gboolean
3831 gdk_window_is_toplevel_frozen (GdkWindow *window)
3832 {
3833   GdkWindow *toplevel;
3834
3835   toplevel = gdk_window_get_toplevel (window);
3836
3837   return toplevel->update_and_descendants_freeze_count > 0;
3838 }
3839
3840 static void
3841 gdk_window_schedule_update (GdkWindow *window)
3842 {
3843   if (window &&
3844       (window->update_freeze_count ||
3845        gdk_window_is_toplevel_frozen (window)))
3846     return;
3847
3848   if (!update_idle)
3849     update_idle =
3850       gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
3851                                  gdk_window_update_idle,
3852                                  NULL, NULL);
3853 }
3854
3855 void
3856 _gdk_window_process_updates_recurse (GdkWindow *window,
3857                                      cairo_region_t *expose_region)
3858 {
3859   GdkWindow *child;
3860   cairo_region_t *clipped_expose_region;
3861   GList *l, *children;
3862
3863   if (cairo_region_is_empty (expose_region))
3864     return;
3865
3866   if (gdk_window_is_offscreen (window->impl_window) &&
3867       gdk_window_has_impl (window))
3868     _gdk_window_add_damage ((GdkWindow *) window->impl_window, expose_region);
3869
3870
3871   /* Paint the window before the children, clipped to the window region
3872      with visible child windows removed */
3873   clipped_expose_region = cairo_region_copy (expose_region);
3874   cairo_region_intersect (clipped_expose_region, window->clip_region_with_children);
3875
3876   if (!cairo_region_is_empty (clipped_expose_region) &&
3877       !window->destroyed)
3878     {
3879       if (window->event_mask & GDK_EXPOSURE_MASK)
3880         {
3881           GdkEvent event;
3882
3883           event.expose.type = GDK_EXPOSE;
3884           event.expose.window = g_object_ref (window);
3885           event.expose.send_event = FALSE;
3886           event.expose.count = 0;
3887           event.expose.region = clipped_expose_region;
3888           cairo_region_get_extents (clipped_expose_region, &event.expose.area);
3889
3890           _gdk_event_emit (&event);
3891
3892           g_object_unref (window);
3893         }
3894       else if (window->window_type != GDK_WINDOW_FOREIGN)
3895         {
3896           /* No exposure mask set, so nothing will be drawn, the
3897            * app relies on the background being what it specified
3898            * for the window. So, we need to clear this manually.
3899            *
3900            * For foreign windows if expose is not set that generally
3901            * means some other client paints them, so don't clear
3902            * there.
3903            *
3904            * We use begin/end_paint around the clear so that we can
3905            * piggyback on the implicit paint */
3906
3907           gdk_window_begin_paint_region (window, clipped_expose_region);
3908           /* The actual clear happens in begin_paint_region */
3909           gdk_window_end_paint (window);
3910         }
3911     }
3912   cairo_region_destroy (clipped_expose_region);
3913
3914   /* Make this reentrancy safe for expose handlers freeing windows */
3915   children = g_list_copy (window->children);
3916   g_list_foreach (children, (GFunc)g_object_ref, NULL);
3917
3918   /* Iterate over children, starting at bottommost */
3919   for (l = g_list_last (children); l != NULL; l = l->prev)
3920     {
3921       child = l->data;
3922
3923       if (child->destroyed || !GDK_WINDOW_IS_MAPPED (child) || child->input_only || child->composited)
3924         continue;
3925
3926       /* Ignore offscreen children, as they don't draw in their parent and
3927        * don't take part in the clipping */
3928       if (gdk_window_is_offscreen (child))
3929         continue;
3930
3931       /* Client side child, expose */
3932       if (child->impl == window->impl)
3933         {
3934           cairo_region_translate (expose_region, -child->x, -child->y);
3935           _gdk_window_process_updates_recurse ((GdkWindow *)child, expose_region);
3936           cairo_region_translate (expose_region, child->x, child->y);
3937         }
3938     }
3939
3940   g_list_free_full (children, g_object_unref);
3941
3942 }
3943
3944 /* Process and remove any invalid area on the native window by creating
3945  * expose events for the window and all non-native descendants.
3946  * Also processes any outstanding moves on the window before doing
3947  * any drawing. Note that its possible to have outstanding moves without
3948  * any invalid area as we use the update idle mechanism to coalesce
3949  * multiple moves as well as multiple invalidations.
3950  */
3951 static void
3952 gdk_window_process_updates_internal (GdkWindow *window)
3953 {
3954   GdkWindowImplClass *impl_class;
3955   gboolean save_region = FALSE;
3956   GdkRectangle clip_box;
3957
3958   /* Ensure the window lives while updating it */
3959   g_object_ref (window);
3960
3961   /* If an update got queued during update processing, we can get a
3962    * window in the update queue that has an empty update_area.
3963    * just ignore it.
3964    */
3965   if (window->update_area)
3966     {
3967       cairo_region_t *update_area = window->update_area;
3968       window->update_area = NULL;
3969
3970       if (gdk_window_is_viewable (window))
3971         {
3972           cairo_region_t *expose_region;
3973           gboolean end_implicit;
3974
3975           /* Clip to part visible in toplevel */
3976           cairo_region_intersect (update_area, window->clip_region);
3977
3978           if (debug_updates)
3979             {
3980               /* Make sure we see the red invalid area before redrawing. */
3981               gdk_display_sync (gdk_window_get_display (window));
3982               g_usleep (70000);
3983             }
3984
3985           /* At this point we will be completely redrawing all of update_area.
3986            * If we have any outstanding moves that end up moving stuff inside
3987            * this area we don't actually need to move that as that part would
3988            * be overdrawn by the expose anyway. So, in order to copy less data
3989            * we remove these areas from the outstanding moves.
3990            */
3991           if (window->outstanding_moves)
3992             {
3993               GdkWindowRegionMove *move;
3994               cairo_region_t *remove;
3995               GList *l, *prev;
3996
3997               remove = cairo_region_copy (update_area);
3998               /* We iterate backwards, starting from the state that would be
3999                  if we had applied all the moves. */
4000               for (l = g_list_last (window->outstanding_moves); l != NULL; l = prev)
4001                 {
4002                   prev = l->prev;
4003                   move = l->data;
4004
4005                   /* Don't need this area */
4006                   cairo_region_subtract (move->dest_region, remove);
4007
4008                   /* However if any of the destination we do need has a source
4009                      in the updated region we do need that as a destination for
4010                      the earlier moves */
4011                   cairo_region_translate (move->dest_region, -move->dx, -move->dy);
4012                   cairo_region_subtract (remove, move->dest_region);
4013
4014                   if (cairo_region_is_empty (move->dest_region))
4015                     {
4016                       gdk_window_region_move_free (move);
4017                       window->outstanding_moves =
4018                         g_list_delete_link (window->outstanding_moves, l);
4019                     }
4020                   else /* move back */
4021                     cairo_region_translate (move->dest_region, move->dx, move->dy);
4022                 }
4023               cairo_region_destroy (remove);
4024             }
4025
4026           /* By now we a set of window moves that should be applied, and then
4027            * an update region that should be repainted. A trivial implementation
4028            * would just do that in order, however in order to get nicer drawing
4029            * we do some tricks:
4030            *
4031            * First of all, each subwindow expose may be double buffered by
4032            * itself (depending on widget setting) via
4033            * gdk_window_begin/end_paint(). But we also do an "implicit" paint,
4034            * creating a single surface the size of the invalid area on the
4035            * native window which all the individual normal paints will draw
4036            * into. This way in the normal case there will be only one surface
4037            * allocated and only once surface draw done for all the windows
4038            * in this native window.
4039            * There are a couple of reasons this may fail, for instance, some
4040            * backends (like quartz) do its own double buffering, so we disable
4041            * gdk double buffering there. Secondly, some subwindow could be
4042            * non-double buffered and draw directly to the window outside a
4043            * begin/end_paint pair. That will be lead to a gdk_window_flush
4044            * which immediately executes all outstanding moves and paints+removes
4045            * the implicit paint (further paints will allocate their own surfaces).
4046            *
4047            * Secondly, in the case of implicit double buffering we expose all
4048            * the child windows into the implicit surface before we execute
4049            * the outstanding moves. This way we minimize the time between
4050            * doing the moves and rendering the new update area, thus minimizing
4051            * flashing. Of course, if any subwindow is non-double buffered we
4052            * well flush earlier than that.
4053            *
4054            * Thirdly, after having done the outstanding moves we queue an
4055            * "antiexpose" on the area that will be drawn by the expose, which
4056            * means that any invalid region on the native window side before
4057            * the first expose drawing operation will be discarded, as it
4058            * has by then been overdrawn with valid data. This means we can
4059            * avoid doing the unnecessary repaint any outstanding expose events.
4060            */
4061
4062           cairo_region_get_extents (update_area, &clip_box);
4063           end_implicit = gdk_window_begin_implicit_paint (window, &clip_box);
4064           expose_region = cairo_region_copy (update_area);
4065           impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
4066           if (!end_implicit)
4067             {
4068               /* Rendering is not double buffered by gdk, do outstanding
4069                * moves and queue antiexposure immediately. No need to do
4070                * any tricks */
4071               gdk_window_flush_outstanding_moves (window);
4072               save_region = impl_class->queue_antiexpose (window, update_area);
4073             }
4074           /* Render the invalid areas to the implicit paint, by sending exposes.
4075            * May flush if non-double buffered widget draw. */
4076           impl_class->process_updates_recurse (window, expose_region);
4077
4078           if (end_implicit)
4079             {
4080               /* Do moves right before exposes are rendered to the window */
4081               gdk_window_flush_outstanding_moves (window);
4082
4083               /* By this time we know that any outstanding expose for this
4084                * area is invalid and we can avoid it, so queue an antiexpose.
4085                * we have already started drawing to the window, so it would
4086                * be to late to anti-expose now. Since this is merely an
4087                * optimization we just avoid doing it at all in that case.
4088                */
4089               if (window->implicit_paint != NULL && !((GdkWindowPaint *)window->implicit_paint->data)->flushed)
4090                 save_region = impl_class->queue_antiexpose (window, update_area);
4091
4092               gdk_window_end_implicit_paint (window);
4093             }
4094           cairo_region_destroy (expose_region);
4095         }
4096       if (!save_region)
4097         cairo_region_destroy (update_area);
4098     }
4099
4100   if (window->outstanding_moves)
4101     {
4102       /* Flush any outstanding moves, may happen if we moved a window but got
4103          no actual invalid area */
4104       gdk_window_flush_outstanding_moves (window);
4105     }
4106
4107   g_object_unref (window);
4108 }
4109
4110 static void
4111 flush_all_displays (void)
4112 {
4113   GSList *displays, *l;
4114
4115   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4116   for (l = displays; l; l = l->next)
4117     gdk_display_flush (l->data);
4118
4119   g_slist_free (displays);
4120 }
4121
4122 static void
4123 before_process_all_updates (void)
4124 {
4125   GSList *displays, *l;
4126   GdkDisplayClass *display_class;
4127
4128   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4129   display_class = GDK_DISPLAY_GET_CLASS (displays->data);
4130   for (l = displays; l; l = l->next)
4131     display_class->before_process_all_updates (l->data);
4132
4133   g_slist_free (displays);
4134 }
4135
4136 static void
4137 after_process_all_updates (void)
4138 {
4139   GSList *displays, *l;
4140   GdkDisplayClass *display_class;
4141
4142   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
4143   display_class = GDK_DISPLAY_GET_CLASS (displays->data);
4144   for (l = displays; l; l = l->next)
4145     display_class->after_process_all_updates (l->data);
4146
4147   g_slist_free (displays);
4148 }
4149
4150 /* Currently it is not possible to override
4151  * gdk_window_process_all_updates in the same manner as
4152  * gdk_window_process_updates and gdk_window_invalidate_maybe_recurse
4153  * by implementing the GdkPaintable interface.  If in the future a
4154  * backend would need this, the right solution would be to add a
4155  * method to GdkDisplay that can be optionally
4156  * NULL. gdk_window_process_all_updates can then walk the list of open
4157  * displays and call the mehod.
4158  */
4159
4160 /**
4161  * gdk_window_process_all_updates:
4162  *
4163  * Calls gdk_window_process_updates() for all windows (see #GdkWindow)
4164  * in the application.
4165  *
4166  **/
4167 void
4168 gdk_window_process_all_updates (void)
4169 {
4170   GSList *old_update_windows = update_windows;
4171   GSList *tmp_list = update_windows;
4172   static gboolean in_process_all_updates = FALSE;
4173   static gboolean got_recursive_update = FALSE;
4174
4175   if (in_process_all_updates)
4176     {
4177       /* We can't do this now since that would recurse, so
4178          delay it until after the recursion is done. */
4179       got_recursive_update = TRUE;
4180       update_idle = 0;
4181       return;
4182     }
4183
4184   in_process_all_updates = TRUE;
4185   got_recursive_update = FALSE;
4186
4187   if (update_idle)
4188     g_source_remove (update_idle);
4189
4190   update_windows = NULL;
4191   update_idle = 0;
4192
4193   before_process_all_updates ();
4194
4195   g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
4196
4197   while (tmp_list)
4198     {
4199       GdkWindow *window = tmp_list->data;
4200
4201       if (!GDK_WINDOW_DESTROYED (window))
4202         {
4203           if (window->update_freeze_count ||
4204               gdk_window_is_toplevel_frozen (window))
4205             gdk_window_add_update_window (window);
4206           else
4207             gdk_window_process_updates_internal (window);
4208         }
4209
4210       g_object_unref (window);
4211       tmp_list = tmp_list->next;
4212     }
4213
4214   g_slist_free (old_update_windows);
4215
4216   flush_all_displays ();
4217
4218   after_process_all_updates ();
4219
4220   in_process_all_updates = FALSE;
4221
4222   /* If we ignored a recursive call, schedule a
4223      redraw now so that it eventually happens,
4224      otherwise we could miss an update if nothing
4225      else schedules an update. */
4226   if (got_recursive_update && !update_idle)
4227     update_idle =
4228       gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
4229                                  gdk_window_update_idle,
4230                                  NULL, NULL);
4231 }
4232
4233 /**
4234  * gdk_window_process_updates:
4235  * @window: a #GdkWindow
4236  * @update_children: whether to also process updates for child windows
4237  *
4238  * Sends one or more expose events to @window. The areas in each
4239  * expose event will cover the entire update area for the window (see
4240  * gdk_window_invalidate_region() for details). Normally GDK calls
4241  * gdk_window_process_all_updates() on your behalf, so there's no
4242  * need to call this function unless you want to force expose events
4243  * to be delivered immediately and synchronously (vs. the usual
4244  * case, where GDK delivers them in an idle handler). Occasionally
4245  * this is useful to produce nicer scrolling behavior, for example.
4246  *
4247  **/
4248 void
4249 gdk_window_process_updates (GdkWindow *window,
4250                             gboolean   update_children)
4251 {
4252   GdkWindow *impl_window;
4253
4254   g_return_if_fail (GDK_IS_WINDOW (window));
4255
4256   if (GDK_WINDOW_DESTROYED (window))
4257     return;
4258
4259   /* Make sure the window lives during the expose callouts */
4260   g_object_ref (window);
4261
4262   impl_window = gdk_window_get_impl_window (window);
4263   if ((impl_window->update_area ||
4264        impl_window->outstanding_moves) &&
4265       !impl_window->update_freeze_count &&
4266       !gdk_window_is_toplevel_frozen (window) &&
4267
4268       /* Don't recurse into process_updates_internal, we'll
4269        * do the update later when idle instead. */
4270       impl_window->implicit_paint == NULL)
4271     {
4272       gdk_window_process_updates_internal ((GdkWindow *)impl_window);
4273       gdk_window_remove_update_window ((GdkWindow *)impl_window);
4274     }
4275
4276   if (update_children)
4277     {
4278       /* process updates in reverse stacking order so composition or
4279        * painting over achieves the desired effect for offscreen windows
4280        */
4281       GList *node, *children;
4282
4283       children = g_list_copy (window->children);
4284       g_list_foreach (children, (GFunc)g_object_ref, NULL);
4285
4286       for (node = g_list_last (children); node; node = node->prev)
4287         {
4288           gdk_window_process_updates (node->data, TRUE);
4289           g_object_unref (node->data);
4290         }
4291
4292       g_list_free (children);
4293     }
4294
4295   g_object_unref (window);
4296 }
4297
4298 static void
4299 gdk_window_invalidate_rect_full (GdkWindow          *window,
4300                                   const GdkRectangle *rect,
4301                                   gboolean            invalidate_children,
4302                                   ClearBg             clear_bg)
4303 {
4304   GdkRectangle window_rect;
4305   cairo_region_t *region;
4306
4307   g_return_if_fail (GDK_IS_WINDOW (window));
4308
4309   if (GDK_WINDOW_DESTROYED (window))
4310     return;
4311
4312   if (window->input_only || !window->viewable)
4313     return;
4314
4315   if (!rect)
4316     {
4317       window_rect.x = 0;
4318       window_rect.y = 0;
4319       window_rect.width = window->width;
4320       window_rect.height = window->height;
4321       rect = &window_rect;
4322     }
4323
4324   region = cairo_region_create_rectangle (rect);
4325   gdk_window_invalidate_region_full (window, region, invalidate_children, clear_bg);
4326   cairo_region_destroy (region);
4327 }
4328
4329 /**
4330  * gdk_window_invalidate_rect:
4331  * @window: a #GdkWindow
4332  * @rect: (allow-none): rectangle to invalidate or %NULL to invalidate the whole
4333  *      window
4334  * @invalidate_children: whether to also invalidate child windows
4335  *
4336  * A convenience wrapper around gdk_window_invalidate_region() which
4337  * invalidates a rectangular region. See
4338  * gdk_window_invalidate_region() for details.
4339  **/
4340 void
4341 gdk_window_invalidate_rect (GdkWindow          *window,
4342                             const GdkRectangle *rect,
4343                             gboolean            invalidate_children)
4344 {
4345   gdk_window_invalidate_rect_full (window, rect, invalidate_children, CLEAR_BG_NONE);
4346 }
4347
4348 static void
4349 draw_ugly_color (GdkWindow       *window,
4350                  const cairo_region_t *region)
4351 {
4352   cairo_t *cr;
4353
4354   cr = gdk_cairo_create (window);
4355   /* Draw ugly color all over the newly-invalid region */
4356   cairo_set_source_rgb (cr, 50000/65535., 10000/65535., 10000/65535.);
4357   gdk_cairo_region (cr, region);
4358   cairo_fill (cr);
4359
4360   cairo_destroy (cr);
4361 }
4362
4363 static void
4364 impl_window_add_update_area (GdkWindow *impl_window,
4365                              cairo_region_t *region)
4366 {
4367   if (impl_window->update_area)
4368     cairo_region_union (impl_window->update_area, region);
4369   else
4370     {
4371       gdk_window_add_update_window (impl_window);
4372       impl_window->update_area = cairo_region_copy (region);
4373       gdk_window_schedule_update (impl_window);
4374     }
4375 }
4376
4377 /* clear_bg controls if the region will be cleared to
4378  * the background pattern if the exposure mask is not
4379  * set for the window, whereas this might not otherwise be
4380  * done (unless necessary to emulate background settings).
4381  * Set this to CLEAR_BG_WINCLEARED or CLEAR_BG_ALL if you
4382  * need to clear the background, such as when exposing the area beneath a
4383  * hidden or moved window, but not when an app requests repaint or when the
4384  * windowing system exposes a newly visible area (because then the windowing
4385  * system has already cleared the area).
4386  */
4387 static void
4388 gdk_window_invalidate_maybe_recurse_full (GdkWindow            *window,
4389                                           const cairo_region_t *region,
4390                                           ClearBg               clear_bg,
4391                                           GdkWindowChildFunc    child_func,
4392                                           gpointer              user_data)
4393 {
4394   GdkWindow *impl_window;
4395   cairo_region_t *visible_region;
4396   GList *tmp_list;
4397
4398   g_return_if_fail (GDK_IS_WINDOW (window));
4399
4400   if (GDK_WINDOW_DESTROYED (window))
4401     return;
4402
4403   if (window->input_only ||
4404       !window->viewable ||
4405       cairo_region_is_empty (region) ||
4406       window->window_type == GDK_WINDOW_ROOT)
4407     return;
4408
4409   visible_region = gdk_window_get_visible_region (window);
4410   cairo_region_intersect (visible_region, region);
4411
4412   tmp_list = window->children;
4413   while (tmp_list)
4414     {
4415       GdkWindow *child = tmp_list->data;
4416
4417       if (!child->input_only)
4418         {
4419           cairo_region_t *child_region;
4420           GdkRectangle child_rect;
4421
4422           child_rect.x = child->x;
4423           child_rect.y = child->y;
4424           child_rect.width = child->width;
4425           child_rect.height = child->height;
4426           child_region = cairo_region_create_rectangle (&child_rect);
4427
4428           /* remove child area from the invalid area of the parent */
4429           if (GDK_WINDOW_IS_MAPPED (child) && !child->shaped &&
4430               !child->composited &&
4431               !gdk_window_is_offscreen (child))
4432             cairo_region_subtract (visible_region, child_region);
4433
4434           if (child_func && (*child_func) ((GdkWindow *)child, user_data))
4435             {
4436               cairo_region_t *tmp = cairo_region_copy (region);
4437
4438               cairo_region_translate (tmp, - child_rect.x, - child_rect.y);
4439               cairo_region_translate (child_region, - child_rect.x, - child_rect.y);
4440               cairo_region_intersect (child_region, tmp);
4441
4442               gdk_window_invalidate_maybe_recurse_full ((GdkWindow *)child,
4443                                                         child_region, clear_bg, child_func, user_data);
4444
4445               cairo_region_destroy (tmp);
4446             }
4447
4448           cairo_region_destroy (child_region);
4449         }
4450
4451       tmp_list = tmp_list->next;
4452     }
4453
4454   impl_window = gdk_window_get_impl_window (window);
4455
4456   if (!cairo_region_is_empty (visible_region)  ||
4457       /* Even if we're not exposing anything, make sure we process
4458          idles for windows with outstanding moves */
4459       (impl_window->outstanding_moves != NULL &&
4460        impl_window->update_area == NULL))
4461     {
4462       if (debug_updates)
4463         draw_ugly_color (window, region);
4464
4465       /* Convert to impl coords */
4466       cairo_region_translate (visible_region, window->abs_x, window->abs_y);
4467
4468       /* Only invalidate area if app requested expose events or if
4469          we need to clear the area (by request or to emulate background
4470          clearing for non-native windows or native windows with no support
4471          for window backgrounds */
4472       if (window->event_mask & GDK_EXPOSURE_MASK ||
4473           clear_bg == CLEAR_BG_ALL ||
4474           clear_bg == CLEAR_BG_WINCLEARED)
4475         impl_window_add_update_area (impl_window, visible_region);
4476     }
4477
4478   cairo_region_destroy (visible_region);
4479 }
4480
4481 /**
4482  * gdk_window_invalidate_maybe_recurse:
4483  * @window: a #GdkWindow
4484  * @region: a #cairo_region_t
4485  * @child_func: (scope call) (allow-none): function to use to decide if to
4486  *     recurse to a child, %NULL means never recurse.
4487  * @user_data: data passed to @child_func
4488  *
4489  * Adds @region to the update area for @window. The update area is the
4490  * region that needs to be redrawn, or "dirty region." The call
4491  * gdk_window_process_updates() sends one or more expose events to the
4492  * window, which together cover the entire update area. An
4493  * application would normally redraw the contents of @window in
4494  * response to those expose events.
4495  *
4496  * GDK will call gdk_window_process_all_updates() on your behalf
4497  * whenever your program returns to the main loop and becomes idle, so
4498  * normally there's no need to do that manually, you just need to
4499  * invalidate regions that you know should be redrawn.
4500  *
4501  * The @child_func parameter controls whether the region of
4502  * each child window that intersects @region will also be invalidated.
4503  * Only children for which @child_func returns TRUE will have the area
4504  * invalidated.
4505  **/
4506 void
4507 gdk_window_invalidate_maybe_recurse (GdkWindow            *window,
4508                                      const cairo_region_t *region,
4509                                      GdkWindowChildFunc    child_func,
4510                                      gpointer              user_data)
4511 {
4512   gdk_window_invalidate_maybe_recurse_full (window, region, CLEAR_BG_NONE,
4513                                             child_func, user_data);
4514 }
4515
4516 static gboolean
4517 true_predicate (GdkWindow *window,
4518                 gpointer   user_data)
4519 {
4520   return TRUE;
4521 }
4522
4523 static void
4524 gdk_window_invalidate_region_full (GdkWindow       *window,
4525                                     const cairo_region_t *region,
4526                                     gboolean         invalidate_children,
4527                                     ClearBg          clear_bg)
4528 {
4529   gdk_window_invalidate_maybe_recurse_full (window, region, clear_bg,
4530                                             invalidate_children ?
4531                                             true_predicate : (gboolean (*) (GdkWindow *, gpointer))NULL,
4532                                        NULL);
4533 }
4534
4535 /**
4536  * gdk_window_invalidate_region:
4537  * @window: a #GdkWindow
4538  * @region: a #cairo_region_t
4539  * @invalidate_children: %TRUE to also invalidate child windows
4540  *
4541  * Adds @region to the update area for @window. The update area is the
4542  * region that needs to be redrawn, or "dirty region." The call
4543  * gdk_window_process_updates() sends one or more expose events to the
4544  * window, which together cover the entire update area. An
4545  * application would normally redraw the contents of @window in
4546  * response to those expose events.
4547  *
4548  * GDK will call gdk_window_process_all_updates() on your behalf
4549  * whenever your program returns to the main loop and becomes idle, so
4550  * normally there's no need to do that manually, you just need to
4551  * invalidate regions that you know should be redrawn.
4552  *
4553  * The @invalidate_children parameter controls whether the region of
4554  * each child window that intersects @region will also be invalidated.
4555  * If %FALSE, then the update area for child windows will remain
4556  * unaffected. See gdk_window_invalidate_maybe_recurse if you need
4557  * fine grained control over which children are invalidated.
4558  **/
4559 void
4560 gdk_window_invalidate_region (GdkWindow       *window,
4561                               const cairo_region_t *region,
4562                               gboolean         invalidate_children)
4563 {
4564   gdk_window_invalidate_maybe_recurse (window, region,
4565                                        invalidate_children ?
4566                                          true_predicate : (gboolean (*) (GdkWindow *, gpointer))NULL,
4567                                        NULL);
4568 }
4569
4570 /**
4571  * _gdk_window_invalidate_for_expose:
4572  * @window: a #GdkWindow
4573  * @region: a #cairo_region_t
4574  *
4575  * Adds @region to the update area for @window. The update area is the
4576  * region that needs to be redrawn, or "dirty region." The call
4577  * gdk_window_process_updates() sends one or more expose events to the
4578  * window, which together cover the entire update area. An
4579  * application would normally redraw the contents of @window in
4580  * response to those expose events.
4581  *
4582  * GDK will call gdk_window_process_all_updates() on your behalf
4583  * whenever your program returns to the main loop and becomes idle, so
4584  * normally there's no need to do that manually, you just need to
4585  * invalidate regions that you know should be redrawn.
4586  *
4587  * This version of invalidation is used when you recieve expose events
4588  * from the native window system. It exposes the native window, plus
4589  * any non-native child windows (but not native child windows, as those would
4590  * have gotten their own expose events).
4591  **/
4592 void
4593 _gdk_window_invalidate_for_expose (GdkWindow       *window,
4594                                    cairo_region_t       *region)
4595 {
4596   GdkWindowRegionMove *move;
4597   cairo_region_t *move_region;
4598   GList *l;
4599
4600   /* Any invalidations comming from the windowing system will
4601      be in areas that may be moved by outstanding moves,
4602      so we need to modify the expose region correspondingly,
4603      otherwise we would expose in the wrong place, as the
4604      outstanding moves will be copied before we draw the
4605      exposes. */
4606   for (l = window->outstanding_moves; l != NULL; l = l->next)
4607     {
4608       move = l->data;
4609
4610       /* covert to move source region */
4611       move_region = cairo_region_copy (move->dest_region);
4612       cairo_region_translate (move_region, -move->dx, -move->dy);
4613
4614       /* Move area of region that intersects with move source
4615          by dx, dy of the move*/
4616       cairo_region_intersect (move_region, region);
4617       cairo_region_subtract (region, move_region);
4618       cairo_region_translate (move_region, move->dx, move->dy);
4619       cairo_region_union (region, move_region);
4620
4621       cairo_region_destroy (move_region);
4622     }
4623
4624   gdk_window_invalidate_maybe_recurse_full (window, region, CLEAR_BG_WINCLEARED,
4625                                             (gboolean (*) (GdkWindow *, gpointer))gdk_window_has_no_impl,
4626                                             NULL);
4627 }
4628
4629
4630 /**
4631  * gdk_window_get_update_area:
4632  * @window: a #GdkWindow
4633  *
4634  * Transfers ownership of the update area from @window to the caller
4635  * of the function. That is, after calling this function, @window will
4636  * no longer have an invalid/dirty region; the update area is removed
4637  * from @window and handed to you. If a window has no update area,
4638  * gdk_window_get_update_area() returns %NULL. You are responsible for
4639  * calling cairo_region_destroy() on the returned region if it's non-%NULL.
4640  *
4641  * Return value: the update area for @window
4642  **/
4643 cairo_region_t *
4644 gdk_window_get_update_area (GdkWindow *window)
4645 {
4646   GdkWindow *impl_window;
4647   cairo_region_t *tmp_region, *to_remove;
4648
4649   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
4650
4651   impl_window = gdk_window_get_impl_window (window);
4652
4653   if (impl_window->update_area)
4654     {
4655       tmp_region = cairo_region_copy (window->clip_region_with_children);
4656       /* Convert to impl coords */
4657       cairo_region_translate (tmp_region, window->abs_x, window->abs_y);
4658       cairo_region_intersect (tmp_region, impl_window->update_area);
4659
4660       if (cairo_region_is_empty (tmp_region))
4661         {
4662           cairo_region_destroy (tmp_region);
4663           return NULL;
4664         }
4665       else
4666         {
4667           /* Convert from impl coords */
4668           cairo_region_translate (tmp_region, -window->abs_x, -window->abs_y);
4669
4670           /* Don't remove any update area that is overlapped by non-opaque windows
4671              (children or siblings) with alpha, as these really need to be repainted
4672              independently of this window. */
4673           to_remove = cairo_region_copy (tmp_region);
4674           cairo_region_subtract (to_remove, window->parent->layered_region);
4675           remove_layered_child_area (window, to_remove);
4676
4677           /* Remove from update_area */
4678           cairo_region_translate (to_remove, window->abs_x, window->abs_y);
4679           cairo_region_subtract (impl_window->update_area, to_remove);
4680
4681           cairo_region_destroy (to_remove);
4682
4683           if (cairo_region_is_empty (impl_window->update_area) &&
4684               impl_window->outstanding_moves == NULL)
4685             {
4686               cairo_region_destroy (impl_window->update_area);
4687               impl_window->update_area = NULL;
4688
4689               gdk_window_remove_update_window ((GdkWindow *)impl_window);
4690             }
4691
4692           return tmp_region;
4693         }
4694     }
4695   else
4696     return NULL;
4697 }
4698
4699 /**
4700  * _gdk_window_clear_update_area:
4701  * @window: a #GdkWindow.
4702  *
4703  * Internal function to clear the update area for a window. This
4704  * is called when the window is hidden or destroyed.
4705  **/
4706 void
4707 _gdk_window_clear_update_area (GdkWindow *window)
4708 {
4709   g_return_if_fail (GDK_IS_WINDOW (window));
4710
4711   if (window->update_area)
4712     {
4713       gdk_window_remove_update_window (window);
4714
4715       cairo_region_destroy (window->update_area);
4716       window->update_area = NULL;
4717     }
4718 }
4719
4720 /**
4721  * gdk_window_freeze_updates:
4722  * @window: a #GdkWindow
4723  *
4724  * Temporarily freezes a window such that it won't receive expose
4725  * events.  The window will begin receiving expose events again when
4726  * gdk_window_thaw_updates() is called. If gdk_window_freeze_updates()
4727  * has been called more than once, gdk_window_thaw_updates() must be called
4728  * an equal number of times to begin processing exposes.
4729  **/
4730 void
4731 gdk_window_freeze_updates (GdkWindow *window)
4732 {
4733   GdkWindow *impl_window;
4734
4735   g_return_if_fail (GDK_IS_WINDOW (window));
4736
4737   impl_window = gdk_window_get_impl_window (window);
4738   impl_window->update_freeze_count++;
4739 }
4740
4741 /**
4742  * gdk_window_thaw_updates:
4743  * @window: a #GdkWindow
4744  *
4745  * Thaws a window frozen with gdk_window_freeze_updates().
4746  **/
4747 void
4748 gdk_window_thaw_updates (GdkWindow *window)
4749 {
4750   GdkWindow *impl_window;
4751
4752   g_return_if_fail (GDK_IS_WINDOW (window));
4753
4754   impl_window = gdk_window_get_impl_window (window);
4755
4756   g_return_if_fail (impl_window->update_freeze_count > 0);
4757
4758   if (--impl_window->update_freeze_count == 0)
4759     gdk_window_schedule_update (GDK_WINDOW (impl_window));
4760 }
4761
4762 /**
4763  * gdk_window_freeze_toplevel_updates_libgtk_only:
4764  * @window: a #GdkWindow
4765  *
4766  * Temporarily freezes a window and all its descendants such that it won't
4767  * receive expose events.  The window will begin receiving expose events
4768  * again when gdk_window_thaw_toplevel_updates_libgtk_only() is called. If
4769  * gdk_window_freeze_toplevel_updates_libgtk_only()
4770  * has been called more than once,
4771  * gdk_window_thaw_toplevel_updates_libgtk_only() must be called
4772  * an equal number of times to begin processing exposes.
4773  *
4774  * This function is not part of the GDK public API and is only
4775  * for use by GTK+.
4776  **/
4777 void
4778 gdk_window_freeze_toplevel_updates_libgtk_only (GdkWindow *window)
4779 {
4780   g_return_if_fail (GDK_IS_WINDOW (window));
4781   g_return_if_fail (window->window_type != GDK_WINDOW_CHILD);
4782
4783   window->update_and_descendants_freeze_count++;
4784 }
4785
4786 /**
4787  * gdk_window_thaw_toplevel_updates_libgtk_only:
4788  * @window: a #GdkWindow
4789  *
4790  * Thaws a window frozen with
4791  * gdk_window_freeze_toplevel_updates_libgtk_only().
4792  *
4793  * This function is not part of the GDK public API and is only
4794  * for use by GTK+.
4795  **/
4796 void
4797 gdk_window_thaw_toplevel_updates_libgtk_only (GdkWindow *window)
4798 {
4799   g_return_if_fail (GDK_IS_WINDOW (window));
4800   g_return_if_fail (window->window_type != GDK_WINDOW_CHILD);
4801   g_return_if_fail (window->update_and_descendants_freeze_count > 0);
4802
4803   window->update_and_descendants_freeze_count--;
4804
4805   gdk_window_schedule_update (window);
4806 }
4807
4808 /**
4809  * gdk_window_set_debug_updates:
4810  * @setting: %TRUE to turn on update debugging
4811  *
4812  * With update debugging enabled, calls to
4813  * gdk_window_invalidate_region() clear the invalidated region of the
4814  * screen to a noticeable color, and GDK pauses for a short time
4815  * before sending exposes to windows during
4816  * gdk_window_process_updates().  The net effect is that you can see
4817  * the invalid region for each window and watch redraws as they
4818  * occur. This allows you to diagnose inefficiencies in your application.
4819  *
4820  * In essence, because the GDK rendering model prevents all flicker,
4821  * if you are redrawing the same region 400 times you may never
4822  * notice, aside from noticing a speed problem. Enabling update
4823  * debugging causes GTK to flicker slowly and noticeably, so you can
4824  * see exactly what's being redrawn when, in what order.
4825  *
4826  * The --gtk-debug=updates command line option passed to GTK+ programs
4827  * enables this debug option at application startup time. That's
4828  * usually more useful than calling gdk_window_set_debug_updates()
4829  * yourself, though you might want to use this function to enable
4830  * updates sometime after application startup time.
4831  *
4832  **/
4833 void
4834 gdk_window_set_debug_updates (gboolean setting)
4835 {
4836   debug_updates = setting;
4837 }
4838
4839 /**
4840  * gdk_window_constrain_size:
4841  * @geometry: a #GdkGeometry structure
4842  * @flags: a mask indicating what portions of @geometry are set
4843  * @width: desired width of window
4844  * @height: desired height of the window
4845  * @new_width: (out): location to store resulting width
4846  * @new_height: (out): location to store resulting height
4847  *
4848  * Constrains a desired width and height according to a
4849  * set of geometry hints (such as minimum and maximum size).
4850  */
4851 void
4852 gdk_window_constrain_size (GdkGeometry *geometry,
4853                            guint        flags,
4854                            gint         width,
4855                            gint         height,
4856                            gint        *new_width,
4857                            gint        *new_height)
4858 {
4859   /* This routine is partially borrowed from fvwm.
4860    *
4861    * Copyright 1993, Robert Nation
4862    *     You may use this code for any purpose, as long as the original
4863    *     copyright remains in the source code and all documentation
4864    *
4865    * which in turn borrows parts of the algorithm from uwm
4866    */
4867   gint min_width = 0;
4868   gint min_height = 0;
4869   gint base_width = 0;
4870   gint base_height = 0;
4871   gint xinc = 1;
4872   gint yinc = 1;
4873   gint max_width = G_MAXINT;
4874   gint max_height = G_MAXINT;
4875
4876 #define FLOOR(value, base)      ( ((gint) ((value) / (base))) * (base) )
4877
4878   if ((flags & GDK_HINT_BASE_SIZE) && (flags & GDK_HINT_MIN_SIZE))
4879     {
4880       base_width = geometry->base_width;
4881       base_height = geometry->base_height;
4882       min_width = geometry->min_width;
4883       min_height = geometry->min_height;
4884     }
4885   else if (flags & GDK_HINT_BASE_SIZE)
4886     {
4887       base_width = geometry->base_width;
4888       base_height = geometry->base_height;
4889       min_width = geometry->base_width;
4890       min_height = geometry->base_height;
4891     }
4892   else if (flags & GDK_HINT_MIN_SIZE)
4893     {
4894       base_width = geometry->min_width;
4895       base_height = geometry->min_height;
4896       min_width = geometry->min_width;
4897       min_height = geometry->min_height;
4898     }
4899
4900   if (flags & GDK_HINT_MAX_SIZE)
4901     {
4902       max_width = geometry->max_width ;
4903       max_height = geometry->max_height;
4904     }
4905
4906   if (flags & GDK_HINT_RESIZE_INC)
4907     {
4908       xinc = MAX (xinc, geometry->width_inc);
4909       yinc = MAX (yinc, geometry->height_inc);
4910     }
4911
4912   /* clamp width and height to min and max values
4913    */
4914   width = CLAMP (width, min_width, max_width);
4915   height = CLAMP (height, min_height, max_height);
4916
4917   /* shrink to base + N * inc
4918    */
4919   width = base_width + FLOOR (width - base_width, xinc);
4920   height = base_height + FLOOR (height - base_height, yinc);
4921
4922   /* constrain aspect ratio, according to:
4923    *
4924    *                width
4925    * min_aspect <= -------- <= max_aspect
4926    *                height
4927    */
4928
4929   if (flags & GDK_HINT_ASPECT &&
4930       geometry->min_aspect > 0 &&
4931       geometry->max_aspect > 0)
4932     {
4933       gint delta;
4934
4935       if (geometry->min_aspect * height > width)
4936         {
4937           delta = FLOOR (height - width / geometry->min_aspect, yinc);
4938           if (height - delta >= min_height)
4939             height -= delta;
4940           else
4941             {
4942               delta = FLOOR (height * geometry->min_aspect - width, xinc);
4943               if (width + delta <= max_width)
4944                 width += delta;
4945             }
4946         }
4947
4948       if (geometry->max_aspect * height < width)
4949         {
4950           delta = FLOOR (width - height * geometry->max_aspect, xinc);
4951           if (width - delta >= min_width)
4952             width -= delta;
4953           else
4954             {
4955               delta = FLOOR (width / geometry->max_aspect - height, yinc);
4956               if (height + delta <= max_height)
4957                 height += delta;
4958             }
4959         }
4960     }
4961
4962 #undef FLOOR
4963
4964   *new_width = width;
4965   *new_height = height;
4966 }
4967
4968 /**
4969  * gdk_window_get_pointer:
4970  * @window: a #GdkWindow
4971  * @x: (out) (allow-none): return location for X coordinate of pointer or %NULL to not
4972  *      return the X coordinate
4973  * @y: (out) (allow-none):  return location for Y coordinate of pointer or %NULL to not
4974  *      return the Y coordinate
4975  * @mask: (out) (allow-none): return location for modifier mask or %NULL to not return the
4976  *      modifier mask
4977  *
4978  * Obtains the current pointer position and modifier state.
4979  * The position is given in coordinates relative to the upper left
4980  * corner of @window.
4981  *
4982  * Return value: (transfer none): the window containing the pointer (as with
4983  * gdk_window_at_pointer()), or %NULL if the window containing the
4984  * pointer isn't known to GDK
4985  *
4986  * Deprecated: 3.0: Use gdk_window_get_device_position() instead.
4987  **/
4988 GdkWindow*
4989 gdk_window_get_pointer (GdkWindow         *window,
4990                         gint              *x,
4991                         gint              *y,
4992                         GdkModifierType   *mask)
4993 {
4994   GdkDisplay *display;
4995
4996   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
4997
4998   display = gdk_window_get_display (window);
4999
5000   return gdk_window_get_device_position (window, display->core_pointer, x, y, mask);
5001 }
5002
5003 /**
5004  * gdk_window_get_device_position:
5005  * @window: a #GdkWindow.
5006  * @device: pointer #GdkDevice to query to.
5007  * @x: (out) (allow-none): return location for the X coordinate of @device, or %NULL.
5008  * @y: (out) (allow-none): return location for the Y coordinate of @device, or %NULL.
5009  * @mask: (out) (allow-none): return location for the modifier mask, or %NULL.
5010  *
5011  * Obtains the current device position and modifier state.
5012  * The position is given in coordinates relative to the upper left
5013  * corner of @window.
5014  *
5015  * Return value: (transfer none): The window underneath @device (as with
5016  * gdk_device_get_window_at_position()), or %NULL if the window is not known to GDK.
5017  *
5018  * Since: 3.0
5019  **/
5020 GdkWindow *
5021 gdk_window_get_device_position (GdkWindow       *window,
5022                                 GdkDevice       *device,
5023                                 gint            *x,
5024                                 gint            *y,
5025                                 GdkModifierType *mask)
5026 {
5027   gint tmp_x, tmp_y;
5028   GdkModifierType tmp_mask;
5029   gboolean normal_child;
5030
5031   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
5032   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
5033   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
5034
5035   normal_child = GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_device_state (window,
5036                                                                              device,
5037                                                                              &tmp_x, &tmp_y,
5038                                                                              &tmp_mask);
5039   /* We got the coords on the impl, convert to the window */
5040   tmp_x -= window->abs_x;
5041   tmp_y -= window->abs_y;
5042
5043   if (x)
5044     *x = tmp_x;
5045   if (y)
5046     *y = tmp_y;
5047   if (mask)
5048     *mask = tmp_mask;
5049
5050   _gdk_display_enable_motion_hints (gdk_window_get_display (window), device);
5051
5052   if (normal_child)
5053     return _gdk_window_find_child_at (window, tmp_x, tmp_y);
5054   return NULL;
5055 }
5056
5057 /**
5058  * gdk_get_default_root_window:
5059  *
5060  * Obtains the root window (parent all other windows are inside)
5061  * for the default display and screen.
5062  *
5063  * Return value: (transfer none): the default root window
5064  **/
5065 GdkWindow *
5066 gdk_get_default_root_window (void)
5067 {
5068   return gdk_screen_get_root_window (gdk_screen_get_default ());
5069 }
5070
5071 static void
5072 get_all_native_children (GdkWindow *window,
5073                          GList **native)
5074 {
5075   GdkWindow *child;
5076   GList *l;
5077
5078   for (l = window->children; l != NULL; l = l->next)
5079     {
5080       child = l->data;
5081
5082       if (gdk_window_has_impl (child))
5083         *native = g_list_prepend (*native, child);
5084       else
5085         get_all_native_children (child, native);
5086     }
5087 }
5088
5089
5090 static inline void
5091 gdk_window_raise_internal (GdkWindow *window)
5092 {
5093   GdkWindow *parent = window->parent;
5094   GdkWindow *above;
5095   GList *native_children;
5096   GList *l, listhead;
5097   GdkWindowImplClass *impl_class;
5098
5099   if (parent)
5100     {
5101       parent->children = g_list_remove (parent->children, window);
5102       parent->children = g_list_prepend (parent->children, window);
5103     }
5104
5105   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5106   /* Just do native raise for toplevels */
5107   if (gdk_window_is_toplevel (window) ||
5108       /* The restack_under codepath should work correctly even if the parent
5109          is native, but it relies on the order of ->children to be correct,
5110          and some apps like SWT reorder the x windows without gdks knowledge,
5111          so we use raise directly in order to make these behave as before
5112          when using native windows */
5113       (gdk_window_has_impl (window) && gdk_window_has_impl (parent)))
5114     {
5115       impl_class->raise (window);
5116     }
5117   else if (gdk_window_has_impl (window))
5118     {
5119       above = find_native_sibling_above (parent, window);
5120       if (above)
5121         {
5122           listhead.data = window;
5123           listhead.next = NULL;
5124           listhead.prev = NULL;
5125           impl_class->restack_under ((GdkWindow *)above,
5126                                      &listhead);
5127         }
5128       else
5129         impl_class->raise (window);
5130     }
5131   else
5132     {
5133       native_children = NULL;
5134       get_all_native_children (window, &native_children);
5135       if (native_children != NULL)
5136         {
5137           above = find_native_sibling_above (parent, window);
5138
5139           if (above)
5140             impl_class->restack_under (above, native_children);
5141           else
5142             {
5143               /* Right order, since native_children is bottom-topmost first */
5144               for (l = native_children; l != NULL; l = l->next)
5145                 impl_class->raise (l->data);
5146             }
5147
5148           g_list_free (native_children);
5149         }
5150
5151     }
5152 }
5153
5154 /* Returns TRUE If the native window was mapped or unmapped */
5155 static gboolean
5156 set_viewable (GdkWindow *w,
5157               gboolean val)
5158 {
5159   GdkWindow *child;
5160   GdkWindowImplClass *impl_class;
5161   GList *l;
5162
5163   if (w->viewable == val)
5164     return FALSE;
5165
5166   w->viewable = val;
5167
5168   if (val)
5169     recompute_visible_regions (w, FALSE, FALSE);
5170
5171   for (l = w->children; l != NULL; l = l->next)
5172     {
5173       child = l->data;
5174
5175       if (GDK_WINDOW_IS_MAPPED (child) &&
5176           child->window_type != GDK_WINDOW_FOREIGN)
5177         set_viewable (child, val);
5178     }
5179
5180   if (gdk_window_has_impl (w)  &&
5181       w->window_type != GDK_WINDOW_FOREIGN &&
5182       !gdk_window_is_toplevel (w))
5183     {
5184       /* For most native windows we show/hide them not when they are
5185        * mapped/unmapped, because that may not produce the correct results.
5186        * For instance, if a native window have a non-native parent which is
5187        * hidden, but its native parent is viewable then showing the window
5188        * would make it viewable to X but its not viewable wrt the non-native
5189        * hierarchy. In order to handle this we track the gdk side viewability
5190        * and only map really viewable windows.
5191        *
5192        * There are two exceptions though:
5193        *
5194        * For foreign windows we don't want ever change the mapped state
5195        * except when explicitly done via gdk_window_show/hide, as this may
5196        * cause problems for client owning the foreign window when its window
5197        * is suddenly mapped or unmapped.
5198        *
5199        * For toplevel windows embedded in a foreign window (e.g. a plug)
5200        * we sometimes synthesize a map of a window, but the native
5201        * window is really shown by the embedder, so we don't want to
5202        * do the show ourselves. We can't really tell this case from the normal
5203        * toplevel show as such toplevels are seen by gdk as parents of the
5204        * root window, so we make an exception for all toplevels.
5205        */
5206
5207       impl_class = GDK_WINDOW_IMPL_GET_CLASS (w->impl);
5208       if (val)
5209         impl_class->show ((GdkWindow *)w, FALSE);
5210       else
5211         impl_class->hide ((GdkWindow *)w);
5212
5213       return TRUE;
5214     }
5215
5216   return FALSE;
5217 }
5218
5219 /* Returns TRUE If the native window was mapped or unmapped */
5220 gboolean
5221 _gdk_window_update_viewable (GdkWindow *window)
5222 {
5223   gboolean viewable;
5224
5225   if (window->window_type == GDK_WINDOW_FOREIGN ||
5226       window->window_type == GDK_WINDOW_ROOT)
5227     viewable = TRUE;
5228   else if (gdk_window_is_toplevel (window) ||
5229            window->parent->viewable)
5230     viewable = GDK_WINDOW_IS_MAPPED (window);
5231   else
5232     viewable = FALSE;
5233
5234   return set_viewable (window, viewable);
5235 }
5236
5237 static void
5238 gdk_window_show_internal (GdkWindow *window, gboolean raise)
5239 {
5240   GdkWindowImplClass *impl_class;
5241   gboolean was_mapped, was_viewable;
5242   gboolean did_show;
5243
5244   g_return_if_fail (GDK_IS_WINDOW (window));
5245
5246   if (window->destroyed)
5247     return;
5248
5249   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5250   was_viewable = window->viewable;
5251
5252   if (raise)
5253     /* Keep children in (reverse) stacking order */
5254     gdk_window_raise_internal (window);
5255
5256   if (gdk_window_has_impl (window))
5257     {
5258       if (!was_mapped)
5259         gdk_synthesize_window_state (window,
5260                                      GDK_WINDOW_STATE_WITHDRAWN,
5261                                      GDK_WINDOW_STATE_FOCUSED);
5262     }
5263   else
5264     {
5265       window->state = 0;
5266     }
5267
5268   did_show = _gdk_window_update_viewable (window);
5269
5270   /* If it was already viewable the backend show op won't be called, call it
5271      again to ensure things happen right if the mapped tracking was not right
5272      for e.g. a foreign window.
5273      Dunno if this is strictly needed but its what happened pre-csw.
5274      Also show if not done by gdk_window_update_viewable. */
5275   if (gdk_window_has_impl (window) && (was_viewable || !did_show))
5276     {
5277       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5278       impl_class->show (window, !did_show ? was_mapped : TRUE);
5279     }
5280
5281   if (!was_mapped && !gdk_window_has_impl (window))
5282     {
5283       if (window->event_mask & GDK_STRUCTURE_MASK)
5284         _gdk_make_event (window, GDK_MAP, NULL, FALSE);
5285
5286       if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5287         _gdk_make_event (window, GDK_MAP, NULL, FALSE);
5288     }
5289
5290   if (!was_mapped || raise)
5291     {
5292       recompute_visible_regions (window, TRUE, FALSE);
5293
5294       /* If any decendants became visible we need to send visibility notify */
5295       gdk_window_update_visibility_recursively (window, NULL);
5296
5297       if (gdk_window_is_viewable (window))
5298         {
5299           _gdk_synthesize_crossing_events_for_geometry_change (window);
5300           gdk_window_invalidate_rect_full (window, NULL, TRUE, CLEAR_BG_ALL);
5301         }
5302     }
5303 }
5304
5305 /**
5306  * gdk_window_show_unraised:
5307  * @window: a #GdkWindow
5308  *
5309  * Shows a #GdkWindow onscreen, but does not modify its stacking
5310  * order. In contrast, gdk_window_show() will raise the window
5311  * to the top of the window stack.
5312  *
5313  * On the X11 platform, in Xlib terms, this function calls
5314  * XMapWindow() (it also updates some internal GDK state, which means
5315  * that you can't really use XMapWindow() directly on a GDK window).
5316  */
5317 void
5318 gdk_window_show_unraised (GdkWindow *window)
5319 {
5320   gdk_window_show_internal (window, FALSE);
5321 }
5322
5323 /**
5324  * gdk_window_raise:
5325  * @window: a #GdkWindow
5326  *
5327  * Raises @window to the top of the Z-order (stacking order), so that
5328  * other windows with the same parent window appear below @window.
5329  * This is true whether or not the windows are visible.
5330  *
5331  * If @window is a toplevel, the window manager may choose to deny the
5332  * request to move the window in the Z-order, gdk_window_raise() only
5333  * requests the restack, does not guarantee it.
5334  */
5335 void
5336 gdk_window_raise (GdkWindow *window)
5337 {
5338   g_return_if_fail (GDK_IS_WINDOW (window));
5339
5340   if (window->destroyed)
5341     return;
5342
5343   /* Keep children in (reverse) stacking order */
5344   gdk_window_raise_internal (window);
5345
5346   recompute_visible_regions (window, TRUE, FALSE);
5347
5348   if (gdk_window_is_viewable (window) &&
5349       !window->input_only)
5350     gdk_window_invalidate_region_full (window, window->clip_region, TRUE, CLEAR_BG_ALL);
5351 }
5352
5353 static void
5354 gdk_window_lower_internal (GdkWindow *window)
5355 {
5356   GdkWindow *parent = window->parent;
5357   GdkWindowImplClass *impl_class;
5358   GdkWindow *above;
5359   GList *native_children;
5360   GList *l, listhead;
5361
5362   if (parent)
5363     {
5364       parent->children = g_list_remove (parent->children, window);
5365       parent->children = g_list_append (parent->children, window);
5366     }
5367
5368   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5369   /* Just do native lower for toplevels */
5370   if (gdk_window_is_toplevel (window) ||
5371       /* The restack_under codepath should work correctly even if the parent
5372          is native, but it relies on the order of ->children to be correct,
5373          and some apps like SWT reorder the x windows without gdks knowledge,
5374          so we use lower directly in order to make these behave as before
5375          when using native windows */
5376       (gdk_window_has_impl (window) && gdk_window_has_impl (parent)))
5377     {
5378       impl_class->lower (window);
5379     }
5380   else if (gdk_window_has_impl (window))
5381     {
5382       above = find_native_sibling_above (parent, window);
5383       if (above)
5384         {
5385           listhead.data = window;
5386           listhead.next = NULL;
5387           listhead.prev = NULL;
5388           impl_class->restack_under ((GdkWindow *)above, &listhead);
5389         }
5390       else
5391         impl_class->raise (window);
5392     }
5393   else
5394     {
5395       native_children = NULL;
5396       get_all_native_children (window, &native_children);
5397       if (native_children != NULL)
5398         {
5399           above = find_native_sibling_above (parent, window);
5400
5401           if (above)
5402             impl_class->restack_under ((GdkWindow *)above,
5403                                        native_children);
5404           else
5405             {
5406               /* Right order, since native_children is bottom-topmost first */
5407               for (l = native_children; l != NULL; l = l->next)
5408                 impl_class->raise (l->data);
5409             }
5410
5411           g_list_free (native_children);
5412         }
5413
5414     }
5415 }
5416
5417 static void
5418 gdk_window_invalidate_in_parent (GdkWindow *private)
5419 {
5420   GdkRectangle r, child;
5421
5422   if (gdk_window_is_toplevel (private))
5423     return;
5424
5425   /* get the visible rectangle of the parent */
5426   r.x = r.y = 0;
5427   r.width = private->parent->width;
5428   r.height = private->parent->height;
5429
5430   child.x = private->x;
5431   child.y = private->y;
5432   child.width = private->width;
5433   child.height = private->height;
5434   gdk_rectangle_intersect (&r, &child, &r);
5435
5436   gdk_window_invalidate_rect_full (private->parent, &r, TRUE, CLEAR_BG_ALL);
5437 }
5438
5439
5440 /**
5441  * gdk_window_lower:
5442  * @window: a #GdkWindow
5443  *
5444  * Lowers @window to the bottom of the Z-order (stacking order), so that
5445  * other windows with the same parent window appear above @window.
5446  * This is true whether or not the other windows are visible.
5447  *
5448  * If @window is a toplevel, the window manager may choose to deny the
5449  * request to move the window in the Z-order, gdk_window_lower() only
5450  * requests the restack, does not guarantee it.
5451  *
5452  * Note that gdk_window_show() raises the window again, so don't call this
5453  * function before gdk_window_show(). (Try gdk_window_show_unraised().)
5454  */
5455 void
5456 gdk_window_lower (GdkWindow *window)
5457 {
5458   g_return_if_fail (GDK_IS_WINDOW (window));
5459
5460   if (window->destroyed)
5461     return;
5462
5463   /* Keep children in (reverse) stacking order */
5464   gdk_window_lower_internal (window);
5465
5466   recompute_visible_regions (window, TRUE, FALSE);
5467
5468   _gdk_synthesize_crossing_events_for_geometry_change (window);
5469   gdk_window_invalidate_in_parent (window);
5470 }
5471
5472 /**
5473  * gdk_window_restack:
5474  * @window: a #GdkWindow
5475  * @sibling: (allow-none): a #GdkWindow that is a sibling of @window, or %NULL
5476  * @above: a boolean
5477  *
5478  * Changes the position of  @window in the Z-order (stacking order), so that
5479  * it is above @sibling (if @above is %TRUE) or below @sibling (if @above is
5480  * %FALSE).
5481  *
5482  * If @sibling is %NULL, then this either raises (if @above is %TRUE) or
5483  * lowers the window.
5484  *
5485  * If @window is a toplevel, the window manager may choose to deny the
5486  * request to move the window in the Z-order, gdk_window_restack() only
5487  * requests the restack, does not guarantee it.
5488  *
5489  * Since: 2.18
5490  */
5491 void
5492 gdk_window_restack (GdkWindow     *window,
5493                     GdkWindow     *sibling,
5494                     gboolean       above)
5495 {
5496   GdkWindowImplClass *impl_class;
5497   GdkWindow *parent;
5498   GdkWindow *above_native;
5499   GList *sibling_link;
5500   GList *native_children;
5501   GList *l, listhead;
5502
5503   g_return_if_fail (GDK_IS_WINDOW (window));
5504   g_return_if_fail (sibling == NULL || GDK_IS_WINDOW (sibling));
5505
5506   if (window->destroyed)
5507     return;
5508
5509   if (sibling == NULL)
5510     {
5511       if (above)
5512         gdk_window_raise (window);
5513       else
5514         gdk_window_lower (window);
5515       return;
5516     }
5517
5518   if (gdk_window_is_toplevel (window))
5519     {
5520       g_return_if_fail (gdk_window_is_toplevel (sibling));
5521       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5522       impl_class->restack_toplevel (window, sibling, above);
5523       return;
5524     }
5525
5526   parent = window->parent;
5527   if (parent)
5528     {
5529       sibling_link = g_list_find (parent->children, sibling);
5530       g_return_if_fail (sibling_link != NULL);
5531       if (sibling_link == NULL)
5532         return;
5533
5534       parent->children = g_list_remove (parent->children, window);
5535       if (above)
5536         parent->children = g_list_insert_before (parent->children,
5537                                                  sibling_link,
5538                                                  window);
5539       else
5540         parent->children = g_list_insert_before (parent->children,
5541                                                  sibling_link->next,
5542                                                  window);
5543
5544       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5545       if (gdk_window_has_impl (window))
5546         {
5547           above_native = find_native_sibling_above (parent, window);
5548           if (above_native)
5549             {
5550               listhead.data = window;
5551               listhead.next = NULL;
5552               listhead.prev = NULL;
5553               impl_class->restack_under (above_native, &listhead);
5554             }
5555           else
5556             impl_class->raise (window);
5557         }
5558       else
5559         {
5560           native_children = NULL;
5561           get_all_native_children (window, &native_children);
5562           if (native_children != NULL)
5563             {
5564               above_native = find_native_sibling_above (parent, window);
5565               if (above_native)
5566                 impl_class->restack_under (above_native,
5567                                            native_children);
5568               else
5569                 {
5570                   /* Right order, since native_children is bottom-topmost first */
5571                   for (l = native_children; l != NULL; l = l->next)
5572                     impl_class->raise (l->data);
5573                 }
5574
5575               g_list_free (native_children);
5576             }
5577         }
5578     }
5579
5580   recompute_visible_regions (window, TRUE, FALSE);
5581
5582   _gdk_synthesize_crossing_events_for_geometry_change (window);
5583   gdk_window_invalidate_in_parent (window);
5584 }
5585
5586
5587 /**
5588  * gdk_window_show:
5589  * @window: a #GdkWindow
5590  *
5591  * Like gdk_window_show_unraised(), but also raises the window to the
5592  * top of the window stack (moves the window to the front of the
5593  * Z-order).
5594  *
5595  * This function maps a window so it's visible onscreen. Its opposite
5596  * is gdk_window_hide().
5597  *
5598  * When implementing a #GtkWidget, you should call this function on the widget's
5599  * #GdkWindow as part of the "map" method.
5600  */
5601 void
5602 gdk_window_show (GdkWindow *window)
5603 {
5604   gdk_window_show_internal (window, TRUE);
5605 }
5606
5607 /**
5608  * gdk_window_hide:
5609  * @window: a #GdkWindow
5610  *
5611  * For toplevel windows, withdraws them, so they will no longer be
5612  * known to the window manager; for all windows, unmaps them, so
5613  * they won't be displayed. Normally done automatically as
5614  * part of gtk_widget_hide().
5615  */
5616 void
5617 gdk_window_hide (GdkWindow *window)
5618 {
5619   GdkWindowImplClass *impl_class;
5620   gboolean was_mapped, did_hide;
5621
5622   g_return_if_fail (GDK_IS_WINDOW (window));
5623
5624   if (window->destroyed)
5625     return;
5626
5627   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5628
5629   if (gdk_window_has_impl (window))
5630     {
5631
5632       if (GDK_WINDOW_IS_MAPPED (window))
5633         gdk_synthesize_window_state (window,
5634                                      0,
5635                                      GDK_WINDOW_STATE_WITHDRAWN);
5636     }
5637   else if (was_mapped)
5638     {
5639       GdkDisplay *display;
5640       GdkDeviceManager *device_manager;
5641       GList *devices, *d;
5642
5643       /* May need to break grabs on children */
5644       display = gdk_window_get_display (window);
5645       device_manager = gdk_display_get_device_manager (display);
5646
5647       /* Get all devices */
5648       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
5649       devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
5650       devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
5651
5652       for (d = devices; d; d = d->next)
5653         {
5654           GdkDevice *device = d->data;
5655
5656           if (_gdk_display_end_device_grab (display,
5657                                             device,
5658                                             _gdk_display_get_next_serial (display),
5659                                             window,
5660                                             TRUE))
5661             gdk_device_ungrab (device, GDK_CURRENT_TIME);
5662         }
5663
5664       window->state = GDK_WINDOW_STATE_WITHDRAWN;
5665       g_list_free (devices);
5666     }
5667
5668   did_hide = _gdk_window_update_viewable (window);
5669
5670   /* Hide foreign window as those are not handled by update_viewable. */
5671   if (gdk_window_has_impl (window) && (!did_hide))
5672     {
5673       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5674       impl_class->hide (window);
5675     }
5676
5677   recompute_visible_regions (window, TRUE, FALSE);
5678
5679   /* all decendants became non-visible, we need to send visibility notify */
5680   gdk_window_update_visibility_recursively (window, NULL);
5681
5682   if (was_mapped && !gdk_window_has_impl (window))
5683     {
5684       if (window->event_mask & GDK_STRUCTURE_MASK)
5685         _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5686
5687       if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5688         _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5689
5690       _gdk_synthesize_crossing_events_for_geometry_change (window->parent);
5691     }
5692
5693   /* Invalidate the rect */
5694   if (was_mapped)
5695     gdk_window_invalidate_in_parent (window);
5696 }
5697
5698 /**
5699  * gdk_window_withdraw:
5700  * @window: a toplevel #GdkWindow
5701  *
5702  * Withdraws a window (unmaps it and asks the window manager to forget about it).
5703  * This function is not really useful as gdk_window_hide() automatically
5704  * withdraws toplevel windows before hiding them.
5705  **/
5706 void
5707 gdk_window_withdraw (GdkWindow *window)
5708 {
5709   GdkWindowImplClass *impl_class;
5710   gboolean was_mapped;
5711
5712   g_return_if_fail (GDK_IS_WINDOW (window));
5713
5714   if (window->destroyed)
5715     return;
5716
5717   was_mapped = GDK_WINDOW_IS_MAPPED (window);
5718
5719   if (gdk_window_has_impl (window))
5720     {
5721       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5722       impl_class->withdraw (window);
5723
5724       if (was_mapped)
5725         {
5726           if (window->event_mask & GDK_STRUCTURE_MASK)
5727             _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5728
5729           if (window->parent && window->parent->event_mask & GDK_SUBSTRUCTURE_MASK)
5730             _gdk_make_event (window, GDK_UNMAP, NULL, FALSE);
5731
5732           _gdk_synthesize_crossing_events_for_geometry_change (window->parent);
5733         }
5734
5735       recompute_visible_regions (window, TRUE, FALSE);
5736     }
5737 }
5738
5739 /**
5740  * gdk_window_set_events:
5741  * @window: a #GdkWindow
5742  * @event_mask: event mask for @window
5743  *
5744  * The event mask for a window determines which events will be reported
5745  * for that window from all master input devices. For example, an event mask
5746  * including #GDK_BUTTON_PRESS_MASK means the window should report button
5747  * press events. The event mask is the bitwise OR of values from the
5748  * #GdkEventMask enumeration.
5749  **/
5750 void
5751 gdk_window_set_events (GdkWindow       *window,
5752                        GdkEventMask     event_mask)
5753 {
5754   GdkWindowImplClass *impl_class;
5755   GdkDisplay *display;
5756
5757   g_return_if_fail (GDK_IS_WINDOW (window));
5758
5759   if (window->destroyed)
5760     return;
5761
5762   /* If motion hint is disabled, enable motion events again */
5763   display = gdk_window_get_display (window);
5764   if ((window->event_mask & GDK_POINTER_MOTION_HINT_MASK) &&
5765       !(event_mask & GDK_POINTER_MOTION_HINT_MASK))
5766     {
5767       GList *devices = window->devices_inside;
5768
5769       while (devices)
5770         {
5771           _gdk_display_enable_motion_hints (display, (GdkDevice *) devices->data);
5772           devices = devices->next;
5773         }
5774     }
5775
5776   window->event_mask = event_mask;
5777
5778   if (gdk_window_has_impl (window))
5779     {
5780       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5781       impl_class->set_events (window,
5782                               get_native_event_mask (window));
5783     }
5784
5785 }
5786
5787 /**
5788  * gdk_window_get_events:
5789  * @window: a #GdkWindow
5790  *
5791  * Gets the event mask for @window for all master input devices. See
5792  * gdk_window_set_events().
5793  *
5794  * Return value: event mask for @window
5795  **/
5796 GdkEventMask
5797 gdk_window_get_events (GdkWindow *window)
5798 {
5799   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
5800
5801   if (window->destroyed)
5802     return 0;
5803
5804   return window->event_mask;
5805 }
5806
5807 /**
5808  * gdk_window_set_device_events:
5809  * @window: a #GdkWindow
5810  * @device: #GdkDevice to enable events for.
5811  * @event_mask: event mask for @window
5812  *
5813  * Sets the event mask for a given device (Normally a floating device, not
5814  * attached to any visible pointer) to @window. For example, an event mask
5815  * including #GDK_BUTTON_PRESS_MASK means the window should report button
5816  * press events. The event mask is the bitwise OR of values from the
5817  * #GdkEventMask enumeration.
5818  *
5819  * Since: 3.0
5820  **/
5821 void
5822 gdk_window_set_device_events (GdkWindow    *window,
5823                               GdkDevice    *device,
5824                               GdkEventMask  event_mask)
5825 {
5826   GdkEventMask device_mask;
5827   GdkDisplay *display;
5828   GdkWindow *native;
5829
5830   g_return_if_fail (GDK_IS_WINDOW (window));
5831   g_return_if_fail (GDK_IS_DEVICE (device));
5832
5833   if (GDK_WINDOW_DESTROYED (window))
5834     return;
5835
5836   /* If motion hint is disabled, enable motion events again */
5837   display = gdk_window_get_display (window);
5838   if ((window->event_mask & GDK_POINTER_MOTION_HINT_MASK) &&
5839       !(event_mask & GDK_POINTER_MOTION_HINT_MASK))
5840     _gdk_display_enable_motion_hints (display, device);
5841
5842   if (G_UNLIKELY (!window->device_events))
5843     window->device_events = g_hash_table_new (NULL, NULL);
5844
5845   if (event_mask == 0)
5846     {
5847       /* FIXME: unsetting events on a master device
5848        * would restore window->event_mask
5849        */
5850       g_hash_table_remove (window->device_events, device);
5851     }
5852   else
5853     g_hash_table_insert (window->device_events, device,
5854                          GINT_TO_POINTER (event_mask));
5855
5856   native = gdk_window_get_toplevel (window);
5857
5858   while (gdk_window_is_offscreen (native))
5859     {
5860       native = gdk_offscreen_window_get_embedder (native);
5861
5862       if (native == NULL ||
5863           (!_gdk_window_has_impl (native) &&
5864            !gdk_window_is_viewable (native)))
5865         return;
5866
5867       native = gdk_window_get_toplevel (native);
5868     }
5869
5870   device_mask = get_native_device_event_mask (window, device);
5871   GDK_DEVICE_GET_CLASS (device)->select_window_events (device, native, device_mask);
5872 }
5873
5874 /**
5875  * gdk_window_get_device_events:
5876  * @window: a #GdkWindow.
5877  * @device: a #GdkDevice.
5878  *
5879  * Returns the event mask for @window corresponding to an specific device.
5880  *
5881  * Returns: device event mask for @window
5882  *
5883  * Since: 3.0
5884  **/
5885 GdkEventMask
5886 gdk_window_get_device_events (GdkWindow *window,
5887                               GdkDevice *device)
5888 {
5889   GdkEventMask mask;
5890
5891   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
5892   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
5893
5894   if (GDK_WINDOW_DESTROYED (window))
5895     return 0;
5896
5897   if (!window->device_events)
5898     return 0;
5899
5900   mask = GPOINTER_TO_INT (g_hash_table_lookup (window->device_events, device));
5901
5902   /* FIXME: device could be controlled by window->event_mask */
5903
5904   return mask;
5905 }
5906
5907 static void
5908 gdk_window_move_resize_toplevel (GdkWindow *window,
5909                                  gboolean   with_move,
5910                                  gint       x,
5911                                  gint       y,
5912                                  gint       width,
5913                                  gint       height)
5914 {
5915   cairo_region_t *old_region, *new_region;
5916   GdkWindowImplClass *impl_class;
5917   gboolean expose;
5918   gboolean is_resize;
5919
5920   expose = FALSE;
5921   old_region = NULL;
5922
5923   is_resize = (width != -1) || (height != -1);
5924
5925   if (gdk_window_is_viewable (window) &&
5926       !window->input_only)
5927     {
5928       expose = TRUE;
5929       old_region = cairo_region_copy (window->clip_region);
5930     }
5931
5932   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
5933   impl_class->move_resize (window, with_move, x, y, width, height);
5934
5935   /* Avoid recomputing for pure toplevel moves, for performance reasons */
5936   if (is_resize)
5937     recompute_visible_regions (window, TRUE, FALSE);
5938
5939   if (expose)
5940     {
5941       new_region = cairo_region_copy (window->clip_region);
5942
5943       /* This is the newly exposed area (due to any resize),
5944        * X will expose it, but lets do that without the roundtrip
5945        */
5946       cairo_region_subtract (new_region, old_region);
5947       gdk_window_invalidate_region_full (window, new_region, TRUE, CLEAR_BG_WINCLEARED);
5948
5949       cairo_region_destroy (old_region);
5950       cairo_region_destroy (new_region);
5951     }
5952
5953   _gdk_synthesize_crossing_events_for_geometry_change (window);
5954 }
5955
5956
5957 static void
5958 move_native_children (GdkWindow *private)
5959 {
5960   GList *l;
5961   GdkWindow *child;
5962   GdkWindowImplClass *impl_class;
5963
5964   for (l = private->children; l; l = l->next)
5965     {
5966       child = l->data;
5967
5968       if (child->impl != private->impl)
5969         {
5970           impl_class = GDK_WINDOW_IMPL_GET_CLASS (child->impl);
5971           impl_class->move_resize (child, TRUE,
5972                                    child->x, child->y,
5973                                    child->width, child->height);
5974         }
5975       else
5976         move_native_children  (child);
5977     }
5978 }
5979
5980 static gboolean
5981 collect_native_child_region_helper (GdkWindow *window,
5982                                     GdkWindowImpl *impl,
5983                                     cairo_region_t **region,
5984                                     int x_offset,
5985                                     int y_offset)
5986 {
5987   GdkWindow *child;
5988   cairo_region_t *tmp;
5989   GList *l;
5990
5991   for (l = window->children; l != NULL; l = l->next)
5992     {
5993       child = l->data;
5994
5995       if (!GDK_WINDOW_IS_MAPPED (child) || child->input_only)
5996         continue;
5997
5998       if (child->impl != impl)
5999         {
6000           tmp = cairo_region_copy (child->clip_region);
6001           cairo_region_translate (tmp,
6002                              x_offset + child->x,
6003                              y_offset + child->y);
6004           if (*region == NULL)
6005             *region = tmp;
6006           else
6007             {
6008               cairo_region_union (*region, tmp);
6009               cairo_region_destroy (tmp);
6010             }
6011         }
6012       else
6013         collect_native_child_region_helper (child, impl, region,
6014                                             x_offset + child->x,
6015                                             y_offset + child->y);
6016     }
6017
6018   return FALSE;
6019 }
6020
6021 static cairo_region_t *
6022 collect_native_child_region (GdkWindow *window,
6023                              gboolean include_this)
6024 {
6025   cairo_region_t *region;
6026
6027   if (include_this && gdk_window_has_impl (window) && window->viewable)
6028     return cairo_region_copy (window->clip_region);
6029
6030   region = NULL;
6031
6032   collect_native_child_region_helper (window, window->impl, &region, 0, 0);
6033
6034   return region;
6035 }
6036
6037
6038 static void
6039 gdk_window_move_resize_internal (GdkWindow *window,
6040                                  gboolean   with_move,
6041                                  gint       x,
6042                                  gint       y,
6043                                  gint       width,
6044                                  gint       height)
6045 {
6046   cairo_region_t *old_region, *old_layered, *new_region, *copy_area;
6047   cairo_region_t *old_native_child_region, *new_native_child_region;
6048   GdkWindow *impl_window;
6049   GdkWindowImplClass *impl_class;
6050   gboolean expose;
6051   int old_x, old_y, old_abs_x, old_abs_y;
6052   int dx, dy;
6053
6054   g_return_if_fail (GDK_IS_WINDOW (window));
6055
6056   if (window->destroyed)
6057     return;
6058
6059   if (gdk_window_is_toplevel (window))
6060     {
6061       gdk_window_move_resize_toplevel (window, with_move, x, y, width, height);
6062       return;
6063     }
6064
6065   /* Bail early if no change */
6066   if (window->width == width &&
6067       window->height == height &&
6068       (!with_move ||
6069        (window->x == x &&
6070         window->y == y)))
6071     return;
6072
6073   /* Handle child windows */
6074
6075   expose = FALSE;
6076   old_region = NULL;
6077   old_layered = NULL;
6078
6079   impl_window = gdk_window_get_impl_window (window);
6080
6081   old_x = window->x;
6082   old_y = window->y;
6083
6084   old_native_child_region = NULL;
6085   if (gdk_window_is_viewable (window) &&
6086       !window->input_only)
6087     {
6088       expose = TRUE;
6089
6090       old_region = cairo_region_copy (window->clip_region);
6091       old_layered = cairo_region_copy (window->layered_region);
6092       /* Adjust regions to parent window coords */
6093       cairo_region_translate (old_region, window->x, window->y);
6094       cairo_region_translate (old_layered, window->x, window->y);
6095
6096       old_native_child_region = collect_native_child_region (window, TRUE);
6097       if (old_native_child_region)
6098         {
6099           /* Adjust region to parent window coords */
6100           cairo_region_translate (old_native_child_region, window->x, window->y);
6101
6102           /* Any native window move will immediately copy stuff to the destination, which may overwrite a
6103            * source or destination for a delayed GdkWindowRegionMove. So, we need
6104            * to flush those here for the parent window and all overlapped subwindows
6105            * of it. And we need to do this before setting the new clips as those will be
6106            * affecting this.
6107            */
6108           gdk_window_flush_recursive (window->parent);
6109         }
6110     }
6111
6112   /* Set the new position and size */
6113   if (with_move)
6114     {
6115       window->x = x;
6116       window->y = y;
6117     }
6118   if (!(width < 0 && height < 0))
6119     {
6120       if (width < 1)
6121         width = 1;
6122       window->width = width;
6123       if (height < 1)
6124         height = 1;
6125       window->height = height;
6126     }
6127
6128   dx = window->x - old_x;
6129   dy = window->y - old_y;
6130
6131   old_abs_x = window->abs_x;
6132   old_abs_y = window->abs_y;
6133
6134   recompute_visible_regions (window, TRUE, FALSE);
6135
6136   new_native_child_region = NULL;
6137   if (old_native_child_region)
6138     {
6139       new_native_child_region = collect_native_child_region (window, TRUE);
6140       /* Adjust region to parent window coords */
6141       cairo_region_translate (new_native_child_region, window->x, window->y);
6142     }
6143
6144   if (gdk_window_has_impl (window))
6145     {
6146       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6147
6148       /* Do the actual move after recomputing things, as this will have set the shape to
6149          the now correct one, thus avoiding copying regions that should not be copied. */
6150       impl_class->move_resize (window, TRUE,
6151                                window->x, window->y,
6152                                window->width, window->height);
6153     }
6154   else if (old_abs_x != window->abs_x ||
6155            old_abs_y != window->abs_y)
6156     move_native_children (window);
6157
6158   if (expose)
6159     {
6160       new_region = cairo_region_copy (window->clip_region);
6161       /* Adjust region to parent window coords */
6162       cairo_region_translate (new_region, window->x, window->y);
6163
6164       /* copy_area:
6165        * Part of the data at the new location can be copied from the
6166        * old location, this area is the intersection of the old region
6167        * moved as the copy will move it and then intersected with
6168        * the new region.
6169        *
6170        * new_region:
6171        * Everything in the old and new regions that is not copied must be
6172        * invalidated (including children) as this is newly exposed
6173        */
6174       if (gdk_window_has_alpha (window))
6175         copy_area = cairo_region_create (); /* Copy nothing for alpha windows */
6176       else
6177         copy_area = cairo_region_copy (new_region);
6178
6179       /* Don't copy from a previously layered region */
6180       cairo_region_translate (old_layered, dx, dy);
6181       cairo_region_subtract (copy_area, old_layered);
6182
6183       /* Don't copy into a layered region */
6184       cairo_region_translate (copy_area, -window->x, -window->y);
6185       cairo_region_subtract (copy_area, window->layered_region);
6186       cairo_region_translate (copy_area, window->x, window->y);
6187
6188       cairo_region_union (new_region, old_region);
6189
6190       if (old_native_child_region)
6191         {
6192           /* Don't copy from inside native children, as this is copied by
6193            * the native window move.
6194            */
6195           cairo_region_subtract (old_region, old_native_child_region);
6196         }
6197       cairo_region_translate (old_region, dx, dy);
6198
6199       cairo_region_intersect (copy_area, old_region);
6200
6201       if (new_native_child_region)
6202         {
6203           /* Don't copy any bits that would cause a read from the moved
6204              native windows, as we can't read that data */
6205           cairo_region_translate (new_native_child_region, dx, dy);
6206           cairo_region_subtract (copy_area, new_native_child_region);
6207           cairo_region_translate (new_native_child_region, -dx, -dy);
6208         }
6209
6210       cairo_region_subtract (new_region, copy_area);
6211
6212       /* Convert old region to impl coords */
6213       cairo_region_translate (old_region, -dx + window->abs_x - window->x, -dy + window->abs_y - window->y);
6214
6215       /* convert from parent coords to impl */
6216       cairo_region_translate (copy_area, window->abs_x - window->x, window->abs_y - window->y);
6217
6218       move_region_on_impl (impl_window, copy_area, dx, dy); /* takes ownership of copy_area */
6219
6220       /* Invalidate affected part in the parent window
6221        *  (no higher window should be affected)
6222        * We also invalidate any children in that area, which could include
6223        * this window if it still overlaps that area.
6224        */
6225       if (old_native_child_region)
6226         {
6227           /* No need to expose the region that the native window move copies */
6228           cairo_region_translate (old_native_child_region, dx, dy);
6229           cairo_region_intersect (old_native_child_region, new_native_child_region);
6230           cairo_region_subtract (new_region, old_native_child_region);
6231         }
6232       gdk_window_invalidate_region_full (window->parent, new_region, TRUE, CLEAR_BG_ALL);
6233
6234       cairo_region_destroy (old_region);
6235       cairo_region_destroy (old_layered);
6236       cairo_region_destroy (new_region);
6237     }
6238
6239   if (old_native_child_region)
6240     {
6241       cairo_region_destroy (old_native_child_region);
6242       cairo_region_destroy (new_native_child_region);
6243     }
6244
6245   _gdk_synthesize_crossing_events_for_geometry_change (window);
6246 }
6247
6248
6249
6250 /**
6251  * gdk_window_move:
6252  * @window: a #GdkWindow
6253  * @x: X coordinate relative to window's parent
6254  * @y: Y coordinate relative to window's parent
6255  *
6256  * Repositions a window relative to its parent window.
6257  * For toplevel windows, window managers may ignore or modify the move;
6258  * you should probably use gtk_window_move() on a #GtkWindow widget
6259  * anyway, instead of using GDK functions. For child windows,
6260  * the move will reliably succeed.
6261  *
6262  * If you're also planning to resize the window, use gdk_window_move_resize()
6263  * to both move and resize simultaneously, for a nicer visual effect.
6264  **/
6265 void
6266 gdk_window_move (GdkWindow *window,
6267                  gint       x,
6268                  gint       y)
6269 {
6270   gdk_window_move_resize_internal (window, TRUE, x, y, -1, -1);
6271 }
6272
6273 /**
6274  * gdk_window_resize:
6275  * @window: a #GdkWindow
6276  * @width: new width of the window
6277  * @height: new height of the window
6278  *
6279  * Resizes @window; for toplevel windows, asks the window manager to resize
6280  * the window. The window manager may not allow the resize. When using GTK+,
6281  * use gtk_window_resize() instead of this low-level GDK function.
6282  *
6283  * Windows may not be resized below 1x1.
6284  *
6285  * If you're also planning to move the window, use gdk_window_move_resize()
6286  * to both move and resize simultaneously, for a nicer visual effect.
6287  **/
6288 void
6289 gdk_window_resize (GdkWindow *window,
6290                    gint       width,
6291                    gint       height)
6292 {
6293   gdk_window_move_resize_internal (window, FALSE, 0, 0, width, height);
6294 }
6295
6296
6297 /**
6298  * gdk_window_move_resize:
6299  * @window: a #GdkWindow
6300  * @x: new X position relative to window's parent
6301  * @y: new Y position relative to window's parent
6302  * @width: new width
6303  * @height: new height
6304  *
6305  * Equivalent to calling gdk_window_move() and gdk_window_resize(),
6306  * except that both operations are performed at once, avoiding strange
6307  * visual effects. (i.e. the user may be able to see the window first
6308  * move, then resize, if you don't use gdk_window_move_resize().)
6309  **/
6310 void
6311 gdk_window_move_resize (GdkWindow *window,
6312                         gint       x,
6313                         gint       y,
6314                         gint       width,
6315                         gint       height)
6316 {
6317   gdk_window_move_resize_internal (window, TRUE, x, y, width, height);
6318 }
6319
6320
6321 /**
6322  * gdk_window_scroll:
6323  * @window: a #GdkWindow
6324  * @dx: Amount to scroll in the X direction
6325  * @dy: Amount to scroll in the Y direction
6326  *
6327  * Scroll the contents of @window, both pixels and children, by the
6328  * given amount. @window itself does not move. Portions of the window
6329  * that the scroll operation brings in from offscreen areas are
6330  * invalidated. The invalidated region may be bigger than what would
6331  * strictly be necessary.
6332  *
6333  * For X11, a minimum area will be invalidated if the window has no
6334  * subwindows, or if the edges of the window's parent do not extend
6335  * beyond the edges of the window. In other cases, a multi-step process
6336  * is used to scroll the window which may produce temporary visual
6337  * artifacts and unnecessary invalidations.
6338  **/
6339 void
6340 gdk_window_scroll (GdkWindow *window,
6341                    gint       dx,
6342                    gint       dy)
6343 {
6344   GdkWindow *impl_window;
6345   cairo_region_t *copy_area, *noncopy_area, *old_layered_area;
6346   cairo_region_t *old_native_child_region, *new_native_child_region;
6347   GList *tmp_list;
6348
6349   g_return_if_fail (GDK_IS_WINDOW (window));
6350
6351   if (dx == 0 && dy == 0)
6352     return;
6353
6354   if (window->destroyed)
6355     return;
6356
6357   old_layered_area = cairo_region_copy (window->layered_region);
6358   old_native_child_region = collect_native_child_region (window, FALSE);
6359   if (old_native_child_region)
6360     {
6361       /* Any native window move will immediately copy stuff to the destination, which may overwrite a
6362        * source or destination for a delayed GdkWindowRegionMove. So, we need
6363        * to flush those here for the window and all overlapped subwindows
6364        * of it. And we need to do this before setting the new clips as those will be
6365        * affecting this.
6366        */
6367       gdk_window_flush_recursive (window);
6368     }
6369
6370
6371   /* First move all child windows, without causing invalidation */
6372
6373   tmp_list = window->children;
6374   while (tmp_list)
6375     {
6376       GdkWindow *child = GDK_WINDOW (tmp_list->data);
6377
6378       /* Just update the positions, the bits will move with the copy */
6379       child->x += dx;
6380       child->y += dy;
6381
6382       tmp_list = tmp_list->next;
6383     }
6384
6385   recompute_visible_regions (window, FALSE, TRUE);
6386
6387   new_native_child_region = NULL;
6388   if (old_native_child_region)
6389     new_native_child_region = collect_native_child_region (window, FALSE);
6390
6391   move_native_children (window);
6392
6393   /* Then copy the actual bits of the window w/ child windows */
6394
6395   impl_window = gdk_window_get_impl_window (window);
6396
6397   /* Calculate the area that can be gotten by copying the old area */
6398   if (gdk_window_has_alpha (window))
6399     copy_area = cairo_region_create (); /* Copy nothing for alpha windows */
6400   else
6401     copy_area = cairo_region_copy (window->clip_region);
6402   cairo_region_subtract (copy_area, old_layered_area);
6403   if (old_native_child_region)
6404     {
6405       /* Don't copy from inside native children, as this is copied by
6406        * the native window move.
6407        */
6408       cairo_region_subtract (copy_area, old_native_child_region);
6409
6410       /* Don't copy any bits that would cause a read from the moved
6411          native windows, as we can't read that data */
6412       cairo_region_subtract (copy_area, new_native_child_region);
6413     }
6414   cairo_region_translate (copy_area, dx, dy);
6415   cairo_region_intersect (copy_area, window->clip_region);
6416   cairo_region_subtract (copy_area, window->layered_region);
6417
6418   /* And the rest need to be invalidated */
6419   noncopy_area = cairo_region_copy (window->clip_region);
6420   cairo_region_subtract (noncopy_area, copy_area);
6421
6422   /* convert from window coords to impl */
6423   cairo_region_translate (copy_area, window->abs_x, window->abs_y);
6424
6425   move_region_on_impl (impl_window, copy_area, dx, dy); /* takes ownership of copy_area */
6426
6427   /* Invalidate not copied regions */
6428   if (old_native_child_region)
6429     {
6430       /* No need to expose the region that the native window move copies */
6431       cairo_region_translate (old_native_child_region, dx, dy);
6432       cairo_region_intersect (old_native_child_region, new_native_child_region);
6433       cairo_region_subtract (noncopy_area, old_native_child_region);
6434     }
6435   gdk_window_invalidate_region_full (window, noncopy_area, TRUE, CLEAR_BG_ALL);
6436
6437   cairo_region_destroy (noncopy_area);
6438   cairo_region_destroy (old_layered_area);
6439
6440   if (old_native_child_region)
6441     {
6442       cairo_region_destroy (old_native_child_region);
6443       cairo_region_destroy (new_native_child_region);
6444     }
6445
6446   _gdk_synthesize_crossing_events_for_geometry_change (window);
6447 }
6448
6449 /**
6450  * gdk_window_move_region:
6451  * @window: a #GdkWindow
6452  * @region: The #cairo_region_t to move
6453  * @dx: Amount to move in the X direction
6454  * @dy: Amount to move in the Y direction
6455  *
6456  * Move the part of @window indicated by @region by @dy pixels in the Y
6457  * direction and @dx pixels in the X direction. The portions of @region
6458  * that not covered by the new position of @region are invalidated.
6459  *
6460  * Child windows are not moved.
6461  *
6462  * Since: 2.8
6463  */
6464 void
6465 gdk_window_move_region (GdkWindow       *window,
6466                         const cairo_region_t *region,
6467                         gint             dx,
6468                         gint             dy)
6469 {
6470   GdkWindow *impl_window;
6471   cairo_region_t *nocopy_area;
6472   cairo_region_t *copy_area;
6473
6474   g_return_if_fail (GDK_IS_WINDOW (window));
6475   g_return_if_fail (region != NULL);
6476
6477   if (dx == 0 && dy == 0)
6478     return;
6479
6480   if (window->destroyed)
6481     return;
6482
6483   impl_window = gdk_window_get_impl_window (window);
6484
6485   /* compute source regions */
6486   if (gdk_window_has_alpha (window))
6487     copy_area = cairo_region_create (); /* Copy nothing for alpha windows */
6488   else
6489     copy_area = cairo_region_copy (region);
6490   cairo_region_intersect (copy_area, window->clip_region_with_children);
6491   cairo_region_subtract (copy_area, window->layered_region);
6492   remove_layered_child_area (window, copy_area);
6493
6494   /* compute destination regions */
6495   cairo_region_translate (copy_area, dx, dy);
6496   cairo_region_intersect (copy_area, window->clip_region_with_children);
6497   cairo_region_subtract (copy_area, window->layered_region);
6498   remove_layered_child_area (window, copy_area);
6499
6500   /* Invalidate parts of the region (source and dest) not covered
6501      by the copy */
6502   nocopy_area = cairo_region_copy (region);
6503   cairo_region_translate (nocopy_area, dx, dy);
6504   cairo_region_union (nocopy_area, region);
6505   cairo_region_subtract (nocopy_area, copy_area);
6506
6507   /* convert from window coords to impl */
6508   cairo_region_translate (copy_area, window->abs_x, window->abs_y);
6509   move_region_on_impl (impl_window, copy_area, dx, dy); /* Takes ownership of copy_area */
6510
6511   gdk_window_invalidate_region_full (window, nocopy_area, FALSE, CLEAR_BG_ALL);
6512   cairo_region_destroy (nocopy_area);
6513 }
6514
6515 /**
6516  * gdk_window_set_background:
6517  * @window: a #GdkWindow
6518  * @color: a #GdkColor
6519  *
6520  * Sets the background color of @window. (However, when using GTK+,
6521  * set the background of a widget with gtk_widget_modify_bg() - if
6522  * you're an application - or gtk_style_set_background() - if you're
6523  * implementing a custom widget.)
6524  *
6525  * See also gdk_window_set_background_pattern().
6526  *
6527  * Deprecated: 3.4: Use gdk_window_set_background_rgba() instead.
6528  */
6529 void
6530 gdk_window_set_background (GdkWindow      *window,
6531                            const GdkColor *color)
6532 {
6533   cairo_pattern_t *pattern;
6534
6535   g_return_if_fail (GDK_IS_WINDOW (window));
6536
6537   pattern = cairo_pattern_create_rgb (color->red   / 65535.,
6538                                       color->green / 65535.,
6539                                       color->blue  / 65535.);
6540
6541   gdk_window_set_background_pattern (window, pattern);
6542
6543   cairo_pattern_destroy (pattern);
6544 }
6545
6546 /**
6547  * gdk_window_set_background_rgba:
6548  * @window: a #GdkWindow
6549  * @rgba: a #GdkRGBA color
6550  *
6551  * Sets the background color of @window.
6552  *
6553  * See also gdk_window_set_background_pattern().
6554  **/
6555 void
6556 gdk_window_set_background_rgba (GdkWindow     *window,
6557                                 const GdkRGBA *rgba)
6558 {
6559   cairo_pattern_t *pattern;
6560
6561   g_return_if_fail (GDK_IS_WINDOW (window));
6562   g_return_if_fail (rgba != NULL);
6563
6564   pattern = cairo_pattern_create_rgba (rgba->red, rgba->green,
6565                                        rgba->blue, rgba->alpha);
6566
6567   gdk_window_set_background_pattern (window, pattern);
6568
6569   cairo_pattern_destroy (pattern);
6570 }
6571
6572
6573 /* Updates has_alpha_background recursively for all child windows
6574    that have parent-relative alpha */
6575 static void
6576 _gdk_window_propagate_has_alpha_background (GdkWindow *window)
6577 {
6578   GdkWindow *child;
6579   GList *l;
6580
6581   for (l = window->children; l; l = l->next)
6582     {
6583       child = l->data;
6584
6585       if (child->background == NULL &&
6586           child->has_alpha_background != window->has_alpha_background)
6587         {
6588           child->has_alpha_background = window->has_alpha_background;
6589           recompute_visible_regions (child, TRUE, FALSE);
6590           _gdk_window_propagate_has_alpha_background (child);
6591         }
6592     }
6593 }
6594
6595 /**
6596  * gdk_window_set_background_pattern:
6597  * @window: a #GdkWindow
6598  * @pattern: (allow-none): a pattern to use, or %NULL
6599  *
6600  * Sets the background of @window.
6601  *
6602  * A background of %NULL means that the window will inherit its
6603  * background form its parent window.
6604  *
6605  * The windowing system will normally fill a window with its background
6606  * when the window is obscured then exposed.
6607  */
6608 void
6609 gdk_window_set_background_pattern (GdkWindow *window,
6610                                    cairo_pattern_t *pattern)
6611 {
6612   gboolean has_alpha;
6613   cairo_pattern_type_t type;
6614   
6615   g_return_if_fail (GDK_IS_WINDOW (window));
6616
6617   if (window->input_only)
6618     return;
6619
6620   if (pattern)
6621     cairo_pattern_reference (pattern);
6622   if (window->background)
6623     cairo_pattern_destroy (window->background);
6624   window->background = pattern;
6625
6626   has_alpha = TRUE;
6627
6628   if (pattern == NULL)
6629     {
6630       /* parent-relative, copy has_alpha from parent */
6631       if (window->parent == NULL ||
6632           window->parent->window_type == GDK_WINDOW_ROOT)
6633         has_alpha = FALSE;
6634       else
6635         has_alpha = window->parent->has_alpha_background;
6636     }
6637   else
6638     {
6639       type = cairo_pattern_get_type (pattern);
6640
6641       if (type == CAIRO_PATTERN_TYPE_SOLID)
6642         {
6643           double alpha;
6644           cairo_pattern_get_rgba (pattern, NULL, NULL, NULL, &alpha);
6645           if (alpha == 1.0)
6646             has_alpha = FALSE;
6647         }
6648       else if (type == CAIRO_PATTERN_TYPE_LINEAR ||
6649                type == CAIRO_PATTERN_TYPE_RADIAL)
6650         {
6651           int i, n;
6652           double alpha;
6653
6654           n = 0;
6655           cairo_pattern_get_color_stop_count (pattern, &n);
6656           has_alpha = FALSE;
6657           for (i = 0; i < n; i++)
6658             {
6659               cairo_pattern_get_color_stop_rgba (pattern, i, NULL,
6660                                                  NULL, NULL, NULL, &alpha);
6661               if (alpha != 1.0)
6662                 {
6663                   has_alpha = TRUE;
6664                   break;
6665                 }
6666             }
6667         }
6668       else if (type == CAIRO_PATTERN_TYPE_SURFACE)
6669         {
6670           cairo_surface_t *surface;
6671           cairo_content_t content;
6672
6673           cairo_pattern_get_surface (pattern, &surface);
6674           content = cairo_surface_get_content (surface);
6675           has_alpha =
6676             (content == CAIRO_CONTENT_ALPHA) ||
6677             (content == CAIRO_CONTENT_COLOR_ALPHA);
6678         }
6679     }
6680
6681   if (has_alpha != window->has_alpha_background)
6682     {
6683       window->has_alpha_background = has_alpha;  
6684       recompute_visible_regions (window, TRUE, FALSE);
6685
6686       _gdk_window_propagate_has_alpha_background (window);
6687     }
6688
6689   if (gdk_window_has_impl (window))
6690     {
6691       GdkWindowImplClass *impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6692       impl_class->set_background (window, pattern);
6693     }
6694   else
6695     gdk_window_invalidate_rect_full (window, NULL, TRUE, CLEAR_BG_ALL);
6696 }
6697
6698 /**
6699  * gdk_window_get_background_pattern:
6700  * @window: a window
6701  *
6702  * Gets the pattern used to clear the background on @window. If @window
6703  * does not have its own background and reuses the parent's, %NULL is
6704  * returned and you'll have to query it yourself.
6705  *
6706  * Returns: (transfer none): The pattern to use for the background or
6707  *     %NULL to use the parent's background.
6708  *
6709  * Since: 2.22
6710  **/
6711 cairo_pattern_t *
6712 gdk_window_get_background_pattern (GdkWindow *window)
6713 {
6714   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6715
6716   return window->background;
6717 }
6718
6719 static void
6720 gdk_window_set_cursor_internal (GdkWindow *window,
6721                                 GdkDevice *device,
6722                                 GdkCursor *cursor)
6723 {
6724   if (GDK_WINDOW_DESTROYED (window))
6725     return;
6726
6727   if (window->window_type == GDK_WINDOW_ROOT ||
6728       window->window_type == GDK_WINDOW_FOREIGN)
6729     GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_device_cursor (window, device, cursor);
6730   else
6731     {
6732       GdkPointerWindowInfo *pointer_info;
6733       GdkDisplay *display;
6734
6735       display = gdk_window_get_display (window);
6736       pointer_info = _gdk_display_get_pointer_info (display, device);
6737
6738       if (_gdk_window_event_parent_of (window, pointer_info->window_under_pointer))
6739         update_cursor (display, device);
6740     }
6741 }
6742
6743 /**
6744  * gdk_window_get_cursor:
6745  * @window: a #GdkWindow
6746  *
6747  * Retrieves a #GdkCursor pointer for the cursor currently set on the
6748  * specified #GdkWindow, or %NULL.  If the return value is %NULL then
6749  * there is no custom cursor set on the specified window, and it is
6750  * using the cursor for its parent window.
6751  *
6752  * Return value: (transfer none): a #GdkCursor, or %NULL. The returned
6753  *   object is owned by the #GdkWindow and should not be unreferenced
6754  *   directly. Use gdk_window_set_cursor() to unset the cursor of the
6755  *   window
6756  *
6757  * Since: 2.18
6758  */
6759 GdkCursor *
6760 gdk_window_get_cursor (GdkWindow *window)
6761 {
6762   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6763
6764   return window->cursor;
6765 }
6766
6767 /**
6768  * gdk_window_set_cursor:
6769  * @window: a #GdkWindow
6770  * @cursor: (allow-none): a cursor
6771  *
6772  * Sets the default mouse pointer for a #GdkWindow. Use gdk_cursor_new_for_display()
6773  * or gdk_cursor_new_from_pixbuf() to create the cursor. To make the cursor
6774  * invisible, use %GDK_BLANK_CURSOR. Passing %NULL for the @cursor argument
6775  * to gdk_window_set_cursor() means that @window will use the cursor of its
6776  * parent window. Most windows should use this default.
6777  */
6778 void
6779 gdk_window_set_cursor (GdkWindow *window,
6780                        GdkCursor *cursor)
6781 {
6782   GdkDisplay *display;
6783
6784   g_return_if_fail (GDK_IS_WINDOW (window));
6785
6786   display = gdk_window_get_display (window);
6787
6788   if (window->cursor)
6789     {
6790       g_object_unref (window->cursor);
6791       window->cursor = NULL;
6792     }
6793
6794   if (!GDK_WINDOW_DESTROYED (window))
6795     {
6796       GdkDeviceManager *device_manager;
6797       GList *devices, *d;
6798
6799       if (cursor)
6800         window->cursor = g_object_ref (cursor);
6801
6802       device_manager = gdk_display_get_device_manager (display);
6803       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
6804
6805       for (d = devices; d; d = d->next)
6806         {
6807           GdkDevice *device;
6808
6809           device = d->data;
6810
6811           if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
6812             continue;
6813
6814           gdk_window_set_cursor_internal (window, device, window->cursor);
6815         }
6816
6817       g_list_free (devices);
6818       g_object_notify (G_OBJECT (window), "cursor");
6819     }
6820 }
6821
6822 /**
6823  * gdk_window_get_device_cursor:
6824  * @window: a #GdkWindow.
6825  * @device: a master, pointer #GdkDevice.
6826  *
6827  * Retrieves a #GdkCursor pointer for the @device currently set on the
6828  * specified #GdkWindow, or %NULL.  If the return value is %NULL then
6829  * there is no custom cursor set on the specified window, and it is
6830  * using the cursor for its parent window.
6831  *
6832  * Returns: (transfer none): a #GdkCursor, or %NULL. The returned
6833  *   object is owned by the #GdkWindow and should not be unreferenced
6834  *   directly. Use gdk_window_set_cursor() to unset the cursor of the
6835  *   window
6836  *
6837  * Since: 3.0
6838  **/
6839 GdkCursor *
6840 gdk_window_get_device_cursor (GdkWindow *window,
6841                               GdkDevice *device)
6842 {
6843   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
6844   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
6845   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
6846   g_return_val_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER, NULL);
6847
6848   return g_hash_table_lookup (window->device_cursor, device);
6849 }
6850
6851 /**
6852  * gdk_window_set_device_cursor:
6853  * @window: a #Gdkwindow
6854  * @device: a master, pointer #GdkDevice
6855  * @cursor: a #GdkCursor
6856  *
6857  * Sets a specific #GdkCursor for a given device when it gets inside @window.
6858  * Use gdk_cursor_new_for_display() or gdk_cursor_new_from_pixbuf() to create
6859  * the cursor. To make the cursor invisible, use %GDK_BLANK_CURSOR. Passing
6860  * %NULL for the @cursor argument to gdk_window_set_cursor() means that
6861  * @window will use the cursor of its parent window. Most windows should
6862  * use this default.
6863  *
6864  * Since: 3.0
6865  **/
6866 void
6867 gdk_window_set_device_cursor (GdkWindow *window,
6868                               GdkDevice *device,
6869                               GdkCursor *cursor)
6870 {
6871   g_return_if_fail (GDK_IS_WINDOW (window));
6872   g_return_if_fail (GDK_IS_DEVICE (device));
6873   g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
6874   g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
6875
6876   if (!cursor)
6877     g_hash_table_remove (window->device_cursor, device);
6878   else
6879     g_hash_table_replace (window->device_cursor, device, g_object_ref (cursor));
6880
6881   gdk_window_set_cursor_internal (window, device, cursor);
6882 }
6883
6884 /**
6885  * gdk_window_get_geometry:
6886  * @window: a #GdkWindow
6887  * @x: (out) (allow-none): return location for X coordinate of window (relative to its parent)
6888  * @y: (out) (allow-none): return location for Y coordinate of window (relative to its parent)
6889  * @width: (out) (allow-none): return location for width of window
6890  * @height: (out) (allow-none): return location for height of window
6891  *
6892  * Any of the return location arguments to this function may be %NULL,
6893  * if you aren't interested in getting the value of that field.
6894  *
6895  * The X and Y coordinates returned are relative to the parent window
6896  * of @window, which for toplevels usually means relative to the
6897  * window decorations (titlebar, etc.) rather than relative to the
6898  * root window (screen-size background window).
6899  *
6900  * On the X11 platform, the geometry is obtained from the X server,
6901  * so reflects the latest position of @window; this may be out-of-sync
6902  * with the position of @window delivered in the most-recently-processed
6903  * #GdkEventConfigure. gdk_window_get_position() in contrast gets the
6904  * position from the most recent configure event.
6905  *
6906  * <note>
6907  * If @window is not a toplevel, it is <emphasis>much</emphasis> better
6908  * to call gdk_window_get_position(), gdk_window_get_width() and
6909  * gdk_window_get_height() instead, because it avoids the roundtrip to
6910  * the X server and because these functions support the full 32-bit
6911  * coordinate space, whereas gdk_window_get_geometry() is restricted to
6912  * the 16-bit coordinates of X11.
6913  *</note>
6914  **/
6915 void
6916 gdk_window_get_geometry (GdkWindow *window,
6917                          gint      *x,
6918                          gint      *y,
6919                          gint      *width,
6920                          gint      *height)
6921 {
6922   GdkWindow *parent;
6923   GdkWindowImplClass *impl_class;
6924
6925   if (!window)
6926     {
6927       GDK_NOTE (MULTIHEAD,
6928                 g_message ("gdk_window_get_geometry(): Window needs "
6929                            "to be non-NULL to be multi head safe"));
6930       window = gdk_screen_get_root_window ((gdk_screen_get_default ()));
6931     }
6932
6933   g_return_if_fail (GDK_IS_WINDOW (window));
6934
6935   if (!GDK_WINDOW_DESTROYED (window))
6936     {
6937       if (gdk_window_has_impl (window))
6938         {
6939           impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
6940           impl_class->get_geometry (window, x, y,
6941                                     width, height);
6942           /* This reports the position wrt to the native parent, we need to convert
6943              it to be relative to the client side parent */
6944           parent = window->parent;
6945           if (parent && !gdk_window_has_impl (parent))
6946             {
6947               if (x)
6948                 *x -= parent->abs_x;
6949               if (y)
6950                 *y -= parent->abs_y;
6951             }
6952         }
6953       else
6954         {
6955           if (x)
6956             *x = window->x;
6957           if (y)
6958             *y = window->y;
6959           if (width)
6960             *width = window->width;
6961           if (height)
6962             *height = window->height;
6963         }
6964     }
6965 }
6966
6967 /**
6968  * gdk_window_get_width:
6969  * @window: a #GdkWindow
6970  *
6971  * Returns the width of the given @window.
6972  *
6973  * On the X11 platform the returned size is the size reported in the
6974  * most-recently-processed configure event, rather than the current
6975  * size on the X server.
6976  *
6977  * Returns: The width of @window
6978  *
6979  * Since: 2.24
6980  */
6981 int
6982 gdk_window_get_width (GdkWindow *window)
6983 {
6984   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6985
6986   return window->width;
6987 }
6988
6989 /**
6990  * gdk_window_get_height:
6991  * @window: a #GdkWindow
6992  *
6993  * Returns the height of the given @window.
6994  *
6995  * On the X11 platform the returned size is the size reported in the
6996  * most-recently-processed configure event, rather than the current
6997  * size on the X server.
6998  *
6999  * Returns: The height of @window
7000  *
7001  * Since: 2.24
7002  */
7003 int
7004 gdk_window_get_height (GdkWindow *window)
7005 {
7006   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
7007
7008   return window->height;
7009 }
7010
7011 /**
7012  * gdk_window_get_origin:
7013  * @window: a #GdkWindow
7014  * @x: (out) (allow-none): return location for X coordinate
7015  * @y: (out) (allow-none): return location for Y coordinate
7016  *
7017  * Obtains the position of a window in root window coordinates.
7018  * (Compare with gdk_window_get_position() and
7019  * gdk_window_get_geometry() which return the position of a window
7020  * relative to its parent window.)
7021  *
7022  * Return value: not meaningful, ignore
7023  */
7024 gint
7025 gdk_window_get_origin (GdkWindow *window,
7026                        gint      *x,
7027                        gint      *y)
7028 {
7029   GdkWindowImplClass *impl_class;
7030
7031   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
7032
7033   if (GDK_WINDOW_DESTROYED (window))
7034     {
7035       if (x)
7036         *x = 0;
7037       if (y)
7038         *y = 0;
7039       return 0;
7040     }
7041
7042   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7043   impl_class->get_root_coords (window,
7044                                window->abs_x,
7045                                window->abs_y,
7046                                x, y);
7047
7048   return TRUE;
7049 }
7050
7051 /**
7052  * gdk_window_get_root_coords:
7053  * @window: a #GdkWindow
7054  * @x: X coordinate in window
7055  * @y: Y coordinate in window
7056  * @root_x: (out): return location for X coordinate
7057  * @root_y: (out): return location for Y coordinate
7058  *
7059  * Obtains the position of a window position in root
7060  * window coordinates. This is similar to
7061  * gdk_window_get_origin() but allows you go pass
7062  * in any position in the window, not just the origin.
7063  *
7064  * Since: 2.18
7065  */
7066 void
7067 gdk_window_get_root_coords (GdkWindow *window,
7068                             gint       x,
7069                             gint       y,
7070                             gint      *root_x,
7071                             gint      *root_y)
7072 {
7073   GdkWindowImplClass *impl_class;
7074
7075   g_return_if_fail (GDK_IS_WINDOW (window));
7076
7077   if (GDK_WINDOW_DESTROYED (window))
7078     {
7079       *root_x = 0;
7080       *root_y = 0;
7081       return;
7082     }
7083   
7084   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7085   impl_class->get_root_coords (window,
7086                                x + window->abs_x,
7087                                y + window->abs_y,
7088                                root_x, root_y);
7089 }
7090
7091 /**
7092  * gdk_window_coords_to_parent:
7093  * @window: a child window
7094  * @x: X coordinate in child's coordinate system
7095  * @y: Y coordinate in child's coordinate system
7096  * @parent_x: (out) (allow-none): return location for X coordinate
7097  * in parent's coordinate system, or %NULL
7098  * @parent_y: (out) (allow-none): return location for Y coordinate
7099  * in parent's coordinate system, or %NULL
7100  *
7101  * Transforms window coordinates from a child window to its parent
7102  * window, where the parent window is the normal parent as returned by
7103  * gdk_window_get_parent() for normal windows, and the window's
7104  * embedder as returned by gdk_offscreen_window_get_embedder() for
7105  * offscreen windows.
7106  *
7107  * For normal windows, calling this function is equivalent to adding
7108  * the return values of gdk_window_get_position() to the child coordinates.
7109  * For offscreen windows however (which can be arbitrarily transformed),
7110  * this function calls the GdkWindow::to-embedder: signal to translate
7111  * the coordinates.
7112  *
7113  * You should always use this function when writing generic code that
7114  * walks up a window hierarchy.
7115  *
7116  * See also: gdk_window_coords_from_parent()
7117  *
7118  * Since: 2.22
7119  **/
7120 void
7121 gdk_window_coords_to_parent (GdkWindow *window,
7122                              gdouble    x,
7123                              gdouble    y,
7124                              gdouble   *parent_x,
7125                              gdouble   *parent_y)
7126 {
7127   g_return_if_fail (GDK_IS_WINDOW (window));
7128
7129   if (gdk_window_is_offscreen (window))
7130     {
7131       gdouble px, py;
7132
7133       to_embedder (window, x, y, &px, &py);
7134
7135       if (parent_x)
7136         *parent_x = px;
7137
7138       if (parent_y)
7139         *parent_y = py;
7140     }
7141   else
7142     {
7143       if (parent_x)
7144         *parent_x = x + window->x;
7145
7146       if (parent_y)
7147         *parent_y = y + window->y;
7148     }
7149 }
7150
7151 /**
7152  * gdk_window_coords_from_parent:
7153  * @window: a child window
7154  * @parent_x: X coordinate in parent's coordinate system
7155  * @parent_y: Y coordinate in parent's coordinate system
7156  * @x: (out) (allow-none): return location for X coordinate in child's coordinate system
7157  * @y: (out) (allow-none): return location for Y coordinate in child's coordinate system
7158  *
7159  * Transforms window coordinates from a parent window to a child
7160  * window, where the parent window is the normal parent as returned by
7161  * gdk_window_get_parent() for normal windows, and the window's
7162  * embedder as returned by gdk_offscreen_window_get_embedder() for
7163  * offscreen windows.
7164  *
7165  * For normal windows, calling this function is equivalent to subtracting
7166  * the return values of gdk_window_get_position() from the parent coordinates.
7167  * For offscreen windows however (which can be arbitrarily transformed),
7168  * this function calls the GdkWindow::from-embedder: signal to translate
7169  * the coordinates.
7170  *
7171  * You should always use this function when writing generic code that
7172  * walks down a window hierarchy.
7173  *
7174  * See also: gdk_window_coords_to_parent()
7175  *
7176  * Since: 2.22
7177  **/
7178 void
7179 gdk_window_coords_from_parent (GdkWindow *window,
7180                                gdouble    parent_x,
7181                                gdouble    parent_y,
7182                                gdouble   *x,
7183                                gdouble   *y)
7184 {
7185   g_return_if_fail (GDK_IS_WINDOW (window));
7186
7187   if (gdk_window_is_offscreen (window))
7188     {
7189       gdouble cx, cy;
7190
7191       from_embedder (window, parent_x, parent_y, &cx, &cy);
7192
7193       if (x)
7194         *x = cx;
7195
7196       if (y)
7197         *y = cy;
7198     }
7199   else
7200     {
7201       if (x)
7202         *x = parent_x - window->x;
7203
7204       if (y)
7205         *y = parent_y - window->y;
7206     }
7207 }
7208
7209 /**
7210  * gdk_window_shape_combine_region:
7211  * @window: a #GdkWindow
7212  * @shape_region: (allow-none): region of window to be non-transparent
7213  * @offset_x: X position of @shape_region in @window coordinates
7214  * @offset_y: Y position of @shape_region in @window coordinates
7215  *
7216  * Makes pixels in @window outside @shape_region be transparent,
7217  * so that the window may be nonrectangular.
7218  *
7219  * If @shape_region is %NULL, the shape will be unset, so the whole
7220  * window will be opaque again. @offset_x and @offset_y are ignored
7221  * if @shape_region is %NULL.
7222  *
7223  * On the X11 platform, this uses an X server extension which is
7224  * widely available on most common platforms, but not available on
7225  * very old X servers, and occasionally the implementation will be
7226  * buggy. On servers without the shape extension, this function
7227  * will do nothing.
7228  *
7229  * This function works on both toplevel and child windows.
7230  */
7231 void
7232 gdk_window_shape_combine_region (GdkWindow       *window,
7233                                  const cairo_region_t *shape_region,
7234                                  gint             offset_x,
7235                                  gint             offset_y)
7236 {
7237   cairo_region_t *old_region, *new_region, *diff;
7238
7239   g_return_if_fail (GDK_IS_WINDOW (window));
7240
7241   if (GDK_WINDOW_DESTROYED (window))
7242     return;
7243
7244   if (!window->shape && shape_region == NULL)
7245     return;
7246
7247   window->shaped = (shape_region != NULL);
7248
7249   if (window->shape)
7250     cairo_region_destroy (window->shape);
7251
7252   old_region = NULL;
7253   if (GDK_WINDOW_IS_MAPPED (window))
7254     old_region = cairo_region_copy (window->clip_region);
7255
7256   if (shape_region)
7257     {
7258       window->shape = cairo_region_copy (shape_region);
7259       cairo_region_translate (window->shape, offset_x, offset_y);
7260     }
7261   else
7262     window->shape = NULL;
7263
7264   recompute_visible_regions (window, TRUE, FALSE);
7265
7266   if (gdk_window_has_impl (window) &&
7267       !should_apply_clip_as_shape (window))
7268     apply_shape (window, window->shape);
7269
7270   if (old_region)
7271     {
7272       new_region = cairo_region_copy (window->clip_region);
7273
7274       /* New area in the window, needs invalidation */
7275       diff = cairo_region_copy (new_region);
7276       cairo_region_subtract (diff, old_region);
7277
7278       gdk_window_invalidate_region_full (window, diff, TRUE, CLEAR_BG_ALL);
7279
7280       cairo_region_destroy (diff);
7281
7282       if (!gdk_window_is_toplevel (window))
7283         {
7284           /* New area in the non-root parent window, needs invalidation */
7285           diff = cairo_region_copy (old_region);
7286           cairo_region_subtract (diff, new_region);
7287
7288           /* Adjust region to parent window coords */
7289           cairo_region_translate (diff, window->x, window->y);
7290
7291           gdk_window_invalidate_region_full (window->parent, diff, TRUE, CLEAR_BG_ALL);
7292
7293           cairo_region_destroy (diff);
7294         }
7295
7296       cairo_region_destroy (new_region);
7297       cairo_region_destroy (old_region);
7298     }
7299 }
7300
7301 static void
7302 do_child_shapes (GdkWindow *window,
7303                  gboolean merge)
7304 {
7305   GdkRectangle r;
7306   cairo_region_t *region;
7307
7308   r.x = 0;
7309   r.y = 0;
7310   r.width = window->width;
7311   r.height = window->height;
7312
7313   region = cairo_region_create_rectangle (&r);
7314   remove_child_area (window, NULL, FALSE, region, NULL);
7315
7316   if (merge && window->shape)
7317     cairo_region_subtract (region, window->shape);
7318
7319   gdk_window_shape_combine_region (window, region, 0, 0);
7320 }
7321
7322 /**
7323  * gdk_window_set_child_shapes:
7324  * @window: a #GdkWindow
7325  *
7326  * Sets the shape mask of @window to the union of shape masks
7327  * for all children of @window, ignoring the shape mask of @window
7328  * itself. Contrast with gdk_window_merge_child_shapes() which includes
7329  * the shape mask of @window in the masks to be merged.
7330  **/
7331 void
7332 gdk_window_set_child_shapes (GdkWindow *window)
7333 {
7334   g_return_if_fail (GDK_IS_WINDOW (window));
7335
7336   do_child_shapes (window, FALSE);
7337 }
7338
7339 /**
7340  * gdk_window_merge_child_shapes:
7341  * @window: a #GdkWindow
7342  *
7343  * Merges the shape masks for any child windows into the
7344  * shape mask for @window. i.e. the union of all masks
7345  * for @window and its children will become the new mask
7346  * for @window. See gdk_window_shape_combine_region().
7347  *
7348  * This function is distinct from gdk_window_set_child_shapes()
7349  * because it includes @window's shape mask in the set of shapes to
7350  * be merged.
7351  */
7352 void
7353 gdk_window_merge_child_shapes (GdkWindow *window)
7354 {
7355   g_return_if_fail (GDK_IS_WINDOW (window));
7356
7357   do_child_shapes (window, TRUE);
7358 }
7359
7360 /**
7361  * gdk_window_input_shape_combine_region:
7362  * @window: a #GdkWindow
7363  * @shape_region: region of window to be non-transparent
7364  * @offset_x: X position of @shape_region in @window coordinates
7365  * @offset_y: Y position of @shape_region in @window coordinates
7366  *
7367  * Like gdk_window_shape_combine_region(), but the shape applies
7368  * only to event handling. Mouse events which happen while
7369  * the pointer position corresponds to an unset bit in the
7370  * mask will be passed on the window below @window.
7371  *
7372  * An input shape is typically used with RGBA windows.
7373  * The alpha channel of the window defines which pixels are
7374  * invisible and allows for nicely antialiased borders,
7375  * and the input shape controls where the window is
7376  * "clickable".
7377  *
7378  * On the X11 platform, this requires version 1.1 of the
7379  * shape extension.
7380  *
7381  * On the Win32 platform, this functionality is not present and the
7382  * function does nothing.
7383  *
7384  * Since: 2.10
7385  */
7386 void
7387 gdk_window_input_shape_combine_region (GdkWindow       *window,
7388                                        const cairo_region_t *shape_region,
7389                                        gint             offset_x,
7390                                        gint             offset_y)
7391 {
7392   GdkWindowImplClass *impl_class;
7393
7394   g_return_if_fail (GDK_IS_WINDOW (window));
7395
7396   if (GDK_WINDOW_DESTROYED (window))
7397     return;
7398
7399   if (window->input_shape)
7400     cairo_region_destroy (window->input_shape);
7401
7402   if (shape_region)
7403     {
7404       window->input_shape = cairo_region_copy (shape_region);
7405       cairo_region_translate (window->input_shape, offset_x, offset_y);
7406     }
7407   else
7408     window->input_shape = NULL;
7409
7410   if (gdk_window_has_impl (window))
7411     {
7412       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7413       impl_class->input_shape_combine_region (window, window->input_shape, 0, 0);
7414     }
7415
7416   /* Pointer may have e.g. moved outside window due to the input mask change */
7417   _gdk_synthesize_crossing_events_for_geometry_change (window);
7418 }
7419
7420 static void
7421 do_child_input_shapes (GdkWindow *window,
7422                        gboolean merge)
7423 {
7424   GdkRectangle r;
7425   cairo_region_t *region;
7426
7427   r.x = 0;
7428   r.y = 0;
7429   r.width = window->width;
7430   r.height = window->height;
7431
7432   region = cairo_region_create_rectangle (&r);
7433   remove_child_area (window, NULL, TRUE, region, NULL);
7434
7435   if (merge && window->shape)
7436     cairo_region_subtract (region, window->shape);
7437   if (merge && window->input_shape)
7438     cairo_region_subtract (region, window->input_shape);
7439
7440   gdk_window_input_shape_combine_region (window, region, 0, 0);
7441 }
7442
7443
7444 /**
7445  * gdk_window_set_child_input_shapes:
7446  * @window: a #GdkWindow
7447  *
7448  * Sets the input shape mask of @window to the union of input shape masks
7449  * for all children of @window, ignoring the input shape mask of @window
7450  * itself. Contrast with gdk_window_merge_child_input_shapes() which includes
7451  * the input shape mask of @window in the masks to be merged.
7452  *
7453  * Since: 2.10
7454  **/
7455 void
7456 gdk_window_set_child_input_shapes (GdkWindow *window)
7457 {
7458   g_return_if_fail (GDK_IS_WINDOW (window));
7459
7460   do_child_input_shapes (window, FALSE);
7461 }
7462
7463 /**
7464  * gdk_window_merge_child_input_shapes:
7465  * @window: a #GdkWindow
7466  *
7467  * Merges the input shape masks for any child windows into the
7468  * input shape mask for @window. i.e. the union of all input masks
7469  * for @window and its children will become the new input mask
7470  * for @window. See gdk_window_input_shape_combine_region().
7471  *
7472  * This function is distinct from gdk_window_set_child_input_shapes()
7473  * because it includes @window's input shape mask in the set of
7474  * shapes to be merged.
7475  *
7476  * Since: 2.10
7477  **/
7478 void
7479 gdk_window_merge_child_input_shapes (GdkWindow *window)
7480 {
7481   g_return_if_fail (GDK_IS_WINDOW (window));
7482
7483   do_child_input_shapes (window, TRUE);
7484 }
7485
7486
7487 /**
7488  * gdk_window_set_static_gravities:
7489  * @window: a #GdkWindow
7490  * @use_static: %TRUE to turn on static gravity
7491  *
7492  * Set the bit gravity of the given window to static, and flag it so
7493  * all children get static subwindow gravity. This is used if you are
7494  * implementing scary features that involve deep knowledge of the
7495  * windowing system. Don't worry about it unless you have to.
7496  *
7497  * Return value: %TRUE if the server supports static gravity
7498  */
7499 gboolean
7500 gdk_window_set_static_gravities (GdkWindow *window,
7501                                  gboolean   use_static)
7502 {
7503   GdkWindowImplClass *impl_class;
7504
7505   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7506
7507   if (gdk_window_has_impl (window))
7508     {
7509       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7510       return impl_class->set_static_gravities (window, use_static);
7511     }
7512
7513   return FALSE;
7514 }
7515
7516 /**
7517  * gdk_window_get_composited:
7518  * @window: a #GdkWindow
7519  *
7520  * Determines whether @window is composited.
7521  *
7522  * See gdk_window_set_composited().
7523  *
7524  * Returns: %TRUE if the window is composited.
7525  *
7526  * Since: 2.22
7527  **/
7528 gboolean
7529 gdk_window_get_composited (GdkWindow *window)
7530 {
7531   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7532
7533   return window->composited;
7534 }
7535
7536 /**
7537  * gdk_window_set_composited:
7538  * @window: a #GdkWindow
7539  * @composited: %TRUE to set the window as composited
7540  *
7541  * Sets a #GdkWindow as composited, or unsets it. Composited
7542  * windows do not automatically have their contents drawn to
7543  * the screen. Drawing is redirected to an offscreen buffer
7544  * and an expose event is emitted on the parent of the composited
7545  * window. It is the responsibility of the parent's expose handler
7546  * to manually merge the off-screen content onto the screen in
7547  * whatever way it sees fit. See <xref linkend="composited-window-example"/>
7548  * for an example.
7549  *
7550  * It only makes sense for child windows to be composited; see
7551  * gdk_window_set_opacity() if you need translucent toplevel
7552  * windows.
7553  *
7554  * An additional effect of this call is that the area of this
7555  * window is no longer clipped from regions marked for
7556  * invalidation on its parent. Draws done on the parent
7557  * window are also no longer clipped by the child.
7558  *
7559  * This call is only supported on some systems (currently,
7560  * only X11 with new enough Xcomposite and Xdamage extensions).
7561  * You must call gdk_display_supports_composite() to check if
7562  * setting a window as composited is supported before
7563  * attempting to do so.
7564  *
7565  * Since: 2.12
7566  */
7567 void
7568 gdk_window_set_composited (GdkWindow *window,
7569                            gboolean   composited)
7570 {
7571   GdkDisplay *display;
7572   GdkWindowImplClass *impl_class;
7573
7574   g_return_if_fail (GDK_IS_WINDOW (window));
7575
7576   composited = composited != FALSE;
7577
7578   if (window->composited == composited)
7579     return;
7580
7581   if (composited)
7582     gdk_window_ensure_native (window);
7583
7584   display = gdk_window_get_display (window);
7585
7586   impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
7587
7588   if (composited && (!gdk_display_supports_composite (display) || !impl_class->set_composited))
7589     {
7590       g_warning ("gdk_window_set_composited called but "
7591                  "compositing is not supported");
7592       return;
7593     }
7594
7595   impl_class->set_composited (window, composited);
7596
7597   recompute_visible_regions (window, TRUE, FALSE);
7598
7599   if (GDK_WINDOW_IS_MAPPED (window))
7600     gdk_window_invalidate_in_parent (window);
7601
7602   window->composited = composited;
7603 }
7604
7605 /**
7606  * gdk_window_get_modal_hint:
7607  * @window: A toplevel #GdkWindow.
7608  *
7609  * Determines whether or not the window manager is hinted that @window
7610  * has modal behaviour.
7611  *
7612  * Return value: whether or not the window has the modal hint set.
7613  *
7614  * Since: 2.22
7615  */
7616 gboolean
7617 gdk_window_get_modal_hint (GdkWindow *window)
7618 {
7619   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7620
7621   return window->modal_hint;
7622 }
7623
7624 /**
7625  * gdk_window_get_accept_focus:
7626  * @window: a toplevel #GdkWindow.
7627  *
7628  * Determines whether or not the desktop environment shuld be hinted that
7629  * the window does not want to receive input focus.
7630  *
7631  * Return value: whether or not the window should receive input focus.
7632  *
7633  * Since: 2.22
7634  */
7635 gboolean
7636 gdk_window_get_accept_focus (GdkWindow *window)
7637 {
7638   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7639
7640   return window->accept_focus;
7641 }
7642
7643 /**
7644  * gdk_window_get_focus_on_map:
7645  * @window: a toplevel #GdkWindow.
7646  *
7647  * Determines whether or not the desktop environment should be hinted that the
7648  * window does not want to receive input focus when it is mapped.
7649  *
7650  * Return value: whether or not the window wants to receive input focus when
7651  * it is mapped.
7652  *
7653  * Since: 2.22
7654  */
7655 gboolean
7656 gdk_window_get_focus_on_map (GdkWindow *window)
7657 {
7658   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7659
7660   return window->focus_on_map;
7661 }
7662
7663 /**
7664  * gdk_window_is_input_only:
7665  * @window: a toplevel #GdkWindow
7666  *
7667  * Determines whether or not the window is an input only window.
7668  *
7669  * Return value: %TRUE if @window is input only
7670  *
7671  * Since: 2.22
7672  */
7673 gboolean
7674 gdk_window_is_input_only (GdkWindow *window)
7675 {
7676   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7677
7678   return window->input_only;
7679 }
7680
7681 /**
7682  * gdk_window_is_shaped:
7683  * @window: a toplevel #GdkWindow
7684  *
7685  * Determines whether or not the window is shaped.
7686  *
7687  * Return value: %TRUE if @window is shaped
7688  *
7689  * Since: 2.22
7690  */
7691 gboolean
7692 gdk_window_is_shaped (GdkWindow *window)
7693 {
7694   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
7695
7696   return window->shaped;
7697 }
7698
7699 void
7700 _gdk_window_add_damage (GdkWindow *toplevel,
7701                         cairo_region_t *damaged_region)
7702 {
7703   GdkDisplay *display;
7704   GdkEvent event = { 0, };
7705   event.expose.type = GDK_DAMAGE;
7706   event.expose.window = toplevel;
7707   event.expose.send_event = FALSE;
7708   event.expose.region = damaged_region;
7709   cairo_region_get_extents (event.expose.region, &event.expose.area);
7710   display = gdk_window_get_display (event.expose.window);
7711   _gdk_event_queue_append (display, gdk_event_copy (&event));
7712 }
7713
7714 /* Gets the toplevel for a window as used for events,
7715    i.e. including offscreen parents */
7716 static GdkWindow *
7717 get_event_parent (GdkWindow *window)
7718 {
7719   if (gdk_window_is_offscreen (window))
7720     return gdk_offscreen_window_get_embedder ((GdkWindow *)window);
7721   else
7722     return window->parent;
7723 }
7724
7725 /* Gets the toplevel for a window as used for events,
7726    i.e. including offscreen parents going up to the native
7727    toplevel */
7728 static GdkWindow *
7729 get_event_toplevel (GdkWindow *window)
7730 {
7731   GdkWindow *parent;
7732
7733   while ((parent = get_event_parent (window)) != NULL &&
7734          (parent->window_type != GDK_WINDOW_ROOT))
7735     window = parent;
7736
7737   return window;
7738 }
7739
7740 gboolean
7741 _gdk_window_event_parent_of (GdkWindow *parent,
7742                              GdkWindow *child)
7743 {
7744   GdkWindow *w;
7745
7746   w = child;
7747   while (w != NULL)
7748     {
7749       if (w == parent)
7750         return TRUE;
7751
7752       w = get_event_parent (w);
7753     }
7754
7755   return FALSE;
7756 }
7757
7758 static void
7759 update_cursor (GdkDisplay *display,
7760                GdkDevice  *device)
7761 {
7762   GdkWindow *cursor_window, *parent, *toplevel;
7763   GdkWindow *pointer_window;
7764   GdkWindowImplClass *impl_class;
7765   GdkPointerWindowInfo *pointer_info;
7766   GdkDeviceGrabInfo *grab;
7767   GdkCursor *cursor;
7768
7769   pointer_info = _gdk_display_get_pointer_info (display, device);
7770   pointer_window = pointer_info->window_under_pointer;
7771
7772   /* We ignore the serials here and just pick the last grab
7773      we've sent, as that would shortly be used anyway. */
7774   grab = _gdk_display_get_last_device_grab (display, device);
7775   if (/* have grab */
7776       grab != NULL &&
7777       /* the pointer is not in a descendant of the grab window */
7778       !_gdk_window_event_parent_of (grab->window, pointer_window))
7779     {
7780       /* use the cursor from the grab window */
7781       cursor_window = grab->window;
7782     }
7783   else
7784     {
7785       /* otherwise use the cursor from the pointer window */
7786       cursor_window = pointer_window;
7787     }
7788
7789   /* Find the first window with the cursor actually set, as
7790      the cursor is inherited from the parent */
7791   while (cursor_window->cursor == NULL &&
7792          (parent = get_event_parent (cursor_window)) != NULL &&
7793          parent->window_type != GDK_WINDOW_ROOT)
7794     cursor_window = parent;
7795
7796   cursor = g_hash_table_lookup (cursor_window->device_cursor, device);
7797
7798   if (!cursor)
7799     cursor = cursor_window->cursor;
7800
7801   /* Set all cursors on toplevel, otherwise its tricky to keep track of
7802    * which native window has what cursor set. */
7803   toplevel = get_event_toplevel (pointer_window);
7804   impl_class = GDK_WINDOW_IMPL_GET_CLASS (toplevel->impl);
7805   impl_class->set_device_cursor (toplevel, device, cursor);
7806 }
7807
7808 static gboolean
7809 point_in_window (GdkWindow *window,
7810                  gdouble    x,
7811                  gdouble    y)
7812 {
7813   return
7814     x >= 0 && x < window->width &&
7815     y >= 0 && y < window->height &&
7816     (window->shape == NULL ||
7817      cairo_region_contains_point (window->shape,
7818                           x, y)) &&
7819     (window->input_shape == NULL ||
7820      cairo_region_contains_point (window->input_shape,
7821                           x, y));
7822 }
7823
7824 static GdkWindow *
7825 convert_native_coords_to_toplevel (GdkWindow *window,
7826                                    gdouble    child_x,
7827                                    gdouble    child_y,
7828                                    gdouble   *toplevel_x,
7829                                    gdouble   *toplevel_y)
7830 {
7831   gdouble x, y;
7832
7833   x = child_x;
7834   y = child_y;
7835
7836   while (!gdk_window_is_toplevel (window))
7837     {
7838       x += window->x;
7839       y += window->y;
7840       window = window->parent;
7841     }
7842
7843   *toplevel_x = x;
7844   *toplevel_y = y;
7845
7846   return window;
7847 }
7848
7849 static void
7850 convert_toplevel_coords_to_window (GdkWindow *window,
7851                                    gdouble    toplevel_x,
7852                                    gdouble    toplevel_y,
7853                                    gdouble   *window_x,
7854                                    gdouble   *window_y)
7855 {
7856   GdkWindow *parent;
7857   gdouble x, y;
7858   GList *children, *l;
7859
7860   x = toplevel_x;
7861   y = toplevel_y;
7862
7863   children = NULL;
7864   while ((parent = get_event_parent (window)) != NULL &&
7865          (parent->window_type != GDK_WINDOW_ROOT))
7866     {
7867       children = g_list_prepend (children, window);
7868       window = parent;
7869     }
7870
7871   for (l = children; l != NULL; l = l->next)
7872     gdk_window_coords_from_parent (l->data, x, y, &x, &y);
7873
7874   g_list_free (children);
7875
7876   *window_x = x;
7877   *window_y = y;
7878 }
7879
7880 static GdkWindow *
7881 pick_embedded_child (GdkWindow *window,
7882                      gdouble    x,
7883                      gdouble    y)
7884 {
7885   GdkWindow *res;
7886
7887   res = NULL;
7888   g_signal_emit (window,
7889                  signals[PICK_EMBEDDED_CHILD], 0,
7890                  x, y, &res);
7891
7892   return res;
7893 }
7894
7895 GdkWindow *
7896 _gdk_window_find_child_at (GdkWindow *window,
7897                            int        x,
7898                            int        y)
7899 {
7900   GdkWindow *sub;
7901   double child_x, child_y;
7902   GList *l;
7903
7904   if (point_in_window (window, x, y))
7905     {
7906       /* Children is ordered in reverse stack order, i.e. first is topmost */
7907       for (l = window->children; l != NULL; l = l->next)
7908         {
7909           sub = l->data;
7910
7911           if (!GDK_WINDOW_IS_MAPPED (sub))
7912             continue;
7913
7914           gdk_window_coords_from_parent ((GdkWindow *)sub,
7915                                          x, y,
7916                                          &child_x, &child_y);
7917           if (point_in_window (sub, child_x, child_y))
7918             return (GdkWindow *)sub;
7919         }
7920
7921       if (window->num_offscreen_children > 0)
7922         {
7923           sub = pick_embedded_child (window,
7924                                      x, y);
7925           if (sub)
7926             return (GdkWindow *)sub;
7927         }
7928     }
7929
7930   return NULL;
7931 }
7932
7933 GdkWindow *
7934 _gdk_window_find_descendant_at (GdkWindow *window,
7935                                 gdouble    x,
7936                                 gdouble    y,
7937                                 gdouble   *found_x,
7938                                 gdouble   *found_y)
7939 {
7940   GdkWindow *sub;
7941   gdouble child_x, child_y;
7942   GList *l;
7943   gboolean found;
7944
7945   if (point_in_window (window, x, y))
7946     {
7947       do
7948         {
7949           found = FALSE;
7950           /* Children is ordered in reverse stack order, i.e. first is topmost */
7951           for (l = window->children; l != NULL; l = l->next)
7952             {
7953               sub = l->data;
7954
7955               if (!GDK_WINDOW_IS_MAPPED (sub))
7956                 continue;
7957
7958               gdk_window_coords_from_parent ((GdkWindow *)sub,
7959                                              x, y,
7960                                              &child_x, &child_y);
7961               if (point_in_window (sub, child_x, child_y))
7962                 {
7963                   x = child_x;
7964                   y = child_y;
7965                   window = sub;
7966                   found = TRUE;
7967                   break;
7968                 }
7969             }
7970           if (!found &&
7971               window->num_offscreen_children > 0)
7972             {
7973               sub = pick_embedded_child (window,
7974                                          x, y);
7975               if (sub)
7976                 {
7977                   found = TRUE;
7978                   window = sub;
7979                   from_embedder (sub, x, y, &x, &y);
7980                 }
7981             }
7982         }
7983       while (found);
7984     }
7985   else
7986     {
7987       /* Not in window at all */
7988       window = NULL;
7989     }
7990
7991   if (found_x)
7992     *found_x = x;
7993   if (found_y)
7994     *found_y = y;
7995
7996   return window;
7997 }
7998
7999 /**
8000  * gdk_window_beep:
8001  * @window: a toplevel #GdkWindow
8002  *
8003  * Emits a short beep associated to @window in the appropriate
8004  * display, if supported. Otherwise, emits a short beep on
8005  * the display just as gdk_display_beep().
8006  *
8007  * Since: 2.12
8008  **/
8009 void
8010 gdk_window_beep (GdkWindow *window)
8011 {
8012   GdkDisplay *display;
8013   GdkWindow *toplevel;
8014
8015   g_return_if_fail (GDK_IS_WINDOW (window));
8016
8017   if (GDK_WINDOW_DESTROYED (window))
8018     return;
8019
8020   toplevel = get_event_toplevel (window);
8021   display = gdk_window_get_display (window);
8022
8023   if (toplevel)
8024     {
8025       if (GDK_WINDOW_IMPL_GET_CLASS (toplevel->impl)->beep (toplevel))
8026         return;
8027     }
8028   
8029   /* If windows fail to beep, we beep the display. */
8030   gdk_display_beep (display);
8031 }
8032
8033 /**
8034  * gdk_window_set_support_multidevice:
8035  * @window: a #GdkWindow.
8036  * @support_multidevice: %TRUE to enable multidevice support in @window.
8037  *
8038  * This function will enable multidevice features in @window.
8039  *
8040  * Multidevice aware windows will need to handle properly multiple,
8041  * per device enter/leave events, device grabs and grab ownerships.
8042  *
8043  * Since: 3.0
8044  **/
8045 void
8046 gdk_window_set_support_multidevice (GdkWindow *window,
8047                                     gboolean   support_multidevice)
8048 {
8049   g_return_if_fail (GDK_IS_WINDOW (window));
8050
8051   if (GDK_WINDOW_DESTROYED (window))
8052     return;
8053
8054   if (window->support_multidevice == support_multidevice)
8055     return;
8056
8057   window->support_multidevice = support_multidevice;
8058
8059   /* FIXME: What to do if called when some pointers are inside the window ? */
8060 }
8061
8062 /**
8063  * gdk_window_get_support_multidevice:
8064  * @window: a #GdkWindow.
8065  *
8066  * Returns %TRUE if the window is aware of the existence of multiple
8067  * devices.
8068  *
8069  * Returns: %TRUE if the window handles multidevice features.
8070  *
8071  * Since: 3.0
8072  **/
8073 gboolean
8074 gdk_window_get_support_multidevice (GdkWindow *window)
8075 {
8076   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
8077
8078   if (GDK_WINDOW_DESTROYED (window))
8079     return FALSE;
8080
8081   return window->support_multidevice;
8082 }
8083
8084 static const guint type_masks[] = {
8085   GDK_SUBSTRUCTURE_MASK, /* GDK_DELETE                 = 0  */
8086   GDK_STRUCTURE_MASK, /* GDK_DESTROY                   = 1  */
8087   GDK_EXPOSURE_MASK, /* GDK_EXPOSE                     = 2  */
8088   GDK_POINTER_MOTION_MASK, /* GDK_MOTION_NOTIFY        = 3  */
8089   GDK_BUTTON_PRESS_MASK, /* GDK_BUTTON_PRESS           = 4  */
8090   GDK_BUTTON_PRESS_MASK, /* GDK_2BUTTON_PRESS          = 5  */
8091   GDK_BUTTON_PRESS_MASK, /* GDK_3BUTTON_PRESS          = 6  */
8092   GDK_BUTTON_RELEASE_MASK, /* GDK_BUTTON_RELEASE       = 7  */
8093   GDK_KEY_PRESS_MASK, /* GDK_KEY_PRESS                 = 8  */
8094   GDK_KEY_RELEASE_MASK, /* GDK_KEY_RELEASE             = 9  */
8095   GDK_ENTER_NOTIFY_MASK, /* GDK_ENTER_NOTIFY           = 10 */
8096   GDK_LEAVE_NOTIFY_MASK, /* GDK_LEAVE_NOTIFY           = 11 */
8097   GDK_FOCUS_CHANGE_MASK, /* GDK_FOCUS_CHANGE           = 12 */
8098   GDK_STRUCTURE_MASK, /* GDK_CONFIGURE                 = 13 */
8099   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_MAP               = 14 */
8100   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_UNMAP             = 15 */
8101   GDK_PROPERTY_CHANGE_MASK, /* GDK_PROPERTY_NOTIFY     = 16 */
8102   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_CLEAR     = 17 */
8103   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_REQUEST   = 18 */
8104   GDK_PROPERTY_CHANGE_MASK, /* GDK_SELECTION_NOTIFY    = 19 */
8105   GDK_PROXIMITY_IN_MASK, /* GDK_PROXIMITY_IN           = 20 */
8106   GDK_PROXIMITY_OUT_MASK, /* GDK_PROXIMITY_OUT         = 21 */
8107   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_ENTER               = 22 */
8108   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_LEAVE               = 23 */
8109   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_MOTION              = 24 */
8110   GDK_ALL_EVENTS_MASK, /* GDK_DRAG_STATUS              = 25 */
8111   GDK_ALL_EVENTS_MASK, /* GDK_DROP_START               = 26 */
8112   GDK_ALL_EVENTS_MASK, /* GDK_DROP_FINISHED            = 27 */
8113   GDK_ALL_EVENTS_MASK, /* GDK_CLIENT_EVENT             = 28 */
8114   GDK_VISIBILITY_NOTIFY_MASK, /* GDK_VISIBILITY_NOTIFY = 29 */
8115   GDK_EXPOSURE_MASK, /* GDK_NO_EXPOSE                  = 30 */
8116   GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK,/* GDK_SCROLL= 31 */
8117   0, /* GDK_WINDOW_STATE = 32 */
8118   0, /* GDK_SETTING = 33 */
8119   0, /* GDK_OWNER_CHANGE = 34 */
8120   0, /* GDK_GRAB_BROKEN = 35 */
8121   0, /* GDK_DAMAGE = 36 */
8122   GDK_TOUCH_MASK, /* GDK_TOUCH_BEGIN = 37 */
8123   GDK_TOUCH_MASK, /* GDK_TOUCH_UPDATE = 38 */
8124   GDK_TOUCH_MASK, /* GDK_TOUCH_END = 39 */
8125   GDK_TOUCH_MASK /* GDK_TOUCH_CANCEL = 40 */
8126 };
8127 G_STATIC_ASSERT (G_N_ELEMENTS (type_masks) == GDK_EVENT_LAST);
8128
8129 /* send motion events if the right buttons are down */
8130 static guint
8131 update_evmask_for_button_motion (guint           evmask,
8132                                  GdkModifierType mask)
8133 {
8134   if (evmask & GDK_BUTTON_MOTION_MASK &&
8135       mask & (GDK_BUTTON1_MASK |
8136               GDK_BUTTON2_MASK |
8137               GDK_BUTTON3_MASK |
8138               GDK_BUTTON4_MASK |
8139               GDK_BUTTON5_MASK))
8140     evmask |= GDK_POINTER_MOTION_MASK;
8141
8142   if ((evmask & GDK_BUTTON1_MOTION_MASK && mask & GDK_BUTTON1_MASK) ||
8143       (evmask & GDK_BUTTON2_MOTION_MASK && mask & GDK_BUTTON2_MASK) ||
8144       (evmask & GDK_BUTTON3_MOTION_MASK && mask & GDK_BUTTON3_MASK))
8145     evmask |= GDK_POINTER_MOTION_MASK;
8146
8147   return evmask;
8148 }
8149
8150 static gboolean
8151 is_button_type (GdkEventType type)
8152 {
8153   return type == GDK_BUTTON_PRESS ||
8154          type == GDK_2BUTTON_PRESS ||
8155          type == GDK_3BUTTON_PRESS ||
8156          type == GDK_BUTTON_RELEASE ||
8157          type == GDK_TOUCH_BEGIN ||
8158          type == GDK_TOUCH_END ||
8159          type == GDK_SCROLL;
8160 }
8161
8162 static gboolean
8163 is_motion_type (GdkEventType type)
8164 {
8165   return type == GDK_MOTION_NOTIFY ||
8166          type == GDK_TOUCH_UPDATE ||
8167          type == GDK_ENTER_NOTIFY ||
8168          type == GDK_LEAVE_NOTIFY;
8169 }
8170
8171 static gboolean
8172 is_touch_type (GdkEventType type)
8173 {
8174   return type == GDK_TOUCH_BEGIN ||
8175          type == GDK_TOUCH_UPDATE ||
8176          type == GDK_TOUCH_END ||
8177          type == GDK_TOUCH_CANCEL;
8178 }
8179
8180 static GdkWindow *
8181 find_common_ancestor (GdkWindow *win1,
8182                       GdkWindow *win2)
8183 {
8184   GdkWindow *tmp;
8185   GList *path1 = NULL, *path2 = NULL;
8186   GList *list1, *list2;
8187
8188   tmp = win1;
8189   while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
8190     {
8191       path1 = g_list_prepend (path1, tmp);
8192       tmp = get_event_parent (tmp);
8193     }
8194
8195   tmp = win2;
8196   while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
8197     {
8198       path2 = g_list_prepend (path2, tmp);
8199       tmp = get_event_parent (tmp);
8200     }
8201
8202   list1 = path1;
8203   list2 = path2;
8204   tmp = NULL;
8205   while (list1 && list2 && (list1->data == list2->data))
8206     {
8207       tmp = list1->data;
8208       list1 = g_list_next (list1);
8209       list2 = g_list_next (list2);
8210     }
8211   g_list_free (path1);
8212   g_list_free (path2);
8213
8214   return tmp;
8215 }
8216
8217 GdkEvent *
8218 _gdk_make_event (GdkWindow    *window,
8219                  GdkEventType  type,
8220                  GdkEvent     *event_in_queue,
8221                  gboolean      before_event)
8222 {
8223   GdkEvent *event = gdk_event_new (type);
8224   guint32 the_time;
8225   GdkModifierType the_state;
8226
8227   the_time = gdk_event_get_time (event_in_queue);
8228   gdk_event_get_state (event_in_queue, &the_state);
8229
8230   event->any.window = g_object_ref (window);
8231   event->any.send_event = FALSE;
8232   if (event_in_queue && event_in_queue->any.send_event)
8233     event->any.send_event = TRUE;
8234
8235   switch (type)
8236     {
8237     case GDK_MOTION_NOTIFY:
8238       event->motion.time = the_time;
8239       event->motion.axes = NULL;
8240       event->motion.state = the_state;
8241       break;
8242
8243     case GDK_BUTTON_PRESS:
8244     case GDK_2BUTTON_PRESS:
8245     case GDK_3BUTTON_PRESS:
8246     case GDK_BUTTON_RELEASE:
8247       event->button.time = the_time;
8248       event->button.axes = NULL;
8249       event->button.state = the_state;
8250       break;
8251
8252     case GDK_TOUCH_BEGIN:
8253     case GDK_TOUCH_UPDATE:
8254     case GDK_TOUCH_END:
8255     case GDK_TOUCH_CANCEL:
8256       event->touch.time = the_time;
8257       event->touch.axes = NULL;
8258       event->touch.state = the_state;
8259       break;
8260
8261     case GDK_SCROLL:
8262       event->scroll.time = the_time;
8263       event->scroll.state = the_state;
8264       break;
8265
8266     case GDK_KEY_PRESS:
8267     case GDK_KEY_RELEASE:
8268       event->key.time = the_time;
8269       event->key.state = the_state;
8270       break;
8271
8272     case GDK_ENTER_NOTIFY:
8273     case GDK_LEAVE_NOTIFY:
8274       event->crossing.time = the_time;
8275       event->crossing.state = the_state;
8276       break;
8277
8278     case GDK_PROPERTY_NOTIFY:
8279       event->property.time = the_time;
8280       event->property.state = the_state;
8281       break;
8282
8283     case GDK_SELECTION_CLEAR:
8284     case GDK_SELECTION_REQUEST:
8285     case GDK_SELECTION_NOTIFY:
8286       event->selection.time = the_time;
8287       break;
8288
8289     case GDK_PROXIMITY_IN:
8290     case GDK_PROXIMITY_OUT:
8291       event->proximity.time = the_time;
8292       break;
8293
8294     case GDK_DRAG_ENTER:
8295     case GDK_DRAG_LEAVE:
8296     case GDK_DRAG_MOTION:
8297     case GDK_DRAG_STATUS:
8298     case GDK_DROP_START:
8299     case GDK_DROP_FINISHED:
8300       event->dnd.time = the_time;
8301       break;
8302
8303     case GDK_FOCUS_CHANGE:
8304     case GDK_CONFIGURE:
8305     case GDK_MAP:
8306     case GDK_UNMAP:
8307     case GDK_CLIENT_EVENT:
8308     case GDK_VISIBILITY_NOTIFY:
8309     case GDK_DELETE:
8310     case GDK_DESTROY:
8311     case GDK_EXPOSE:
8312     default:
8313       break;
8314     }
8315
8316   if (event_in_queue)
8317     {
8318     if (before_event)
8319       _gdk_event_queue_insert_before (gdk_window_get_display (window), event_in_queue, event);
8320     else
8321       _gdk_event_queue_insert_after (gdk_window_get_display (window), event_in_queue, event);
8322     }
8323   else
8324     _gdk_event_queue_append (gdk_window_get_display (window), event);
8325
8326   return event;
8327 }
8328
8329 static void
8330 send_crossing_event (GdkDisplay                 *display,
8331                      GdkWindow                  *toplevel,
8332                      GdkWindow                  *window,
8333                      GdkEventType                type,
8334                      GdkCrossingMode             mode,
8335                      GdkNotifyType               notify_type,
8336                      GdkWindow                  *subwindow,
8337                      GdkDevice                  *device,
8338                      GdkDevice                  *source_device,
8339                      gint                        toplevel_x,
8340                      gint                        toplevel_y,
8341                      GdkModifierType             mask,
8342                      guint32                     time_,
8343                      GdkEvent                   *event_in_queue,
8344                      gulong                      serial)
8345 {
8346   GdkEvent *event;
8347   guint32 window_event_mask, type_event_mask;
8348   GdkDeviceGrabInfo *grab;
8349   GdkTouchGrabInfo *touch_grab = NULL;
8350   GdkPointerWindowInfo *pointer_info;
8351   gboolean block_event = FALSE;
8352   GdkEventSequence *sequence;
8353
8354   grab = _gdk_display_has_device_grab (display, device, serial);
8355   pointer_info = _gdk_display_get_pointer_info (display, device);
8356
8357   sequence = gdk_event_get_event_sequence (event_in_queue);
8358   if (sequence)
8359     touch_grab = _gdk_display_has_touch_grab (display, device, sequence, serial);
8360
8361   if (touch_grab)
8362     {
8363       if (window != touch_grab->window)
8364         return;
8365
8366       window_event_mask = touch_grab->event_mask;
8367     }
8368   else if (grab != NULL &&
8369            !grab->owner_events)
8370     {
8371       /* !owner_event => only report events wrt grab window, ignore rest */
8372       if ((GdkWindow *)window != grab->window)
8373         return;
8374       window_event_mask = grab->event_mask;
8375     }
8376   else
8377     window_event_mask = window->event_mask;
8378
8379   if (type == GDK_ENTER_NOTIFY &&
8380       (pointer_info->need_touch_press_enter ||
8381        gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN) &&
8382       mode != GDK_CROSSING_TOUCH_BEGIN &&
8383       mode != GDK_CROSSING_TOUCH_END)
8384     {
8385       pointer_info->need_touch_press_enter = TRUE;
8386       block_event = TRUE;
8387     }
8388   else if (type == GDK_LEAVE_NOTIFY)
8389     {
8390       type_event_mask = GDK_LEAVE_NOTIFY_MASK;
8391       window->devices_inside = g_list_remove (window->devices_inside, device);
8392
8393       if (!window->support_multidevice && window->devices_inside)
8394         {
8395           /* Block leave events unless it's the last pointer */
8396           block_event = TRUE;
8397         }
8398     }
8399   else
8400     {
8401       type_event_mask = GDK_ENTER_NOTIFY_MASK;
8402
8403       if (!window->support_multidevice && window->devices_inside)
8404         {
8405           /* Only emit enter events for the first device */
8406           block_event = TRUE;
8407         }
8408
8409       if (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER &&
8410           gdk_device_get_mode (device) != GDK_MODE_DISABLED &&
8411           !g_list_find (window->devices_inside, device))
8412         window->devices_inside = g_list_prepend (window->devices_inside, device);
8413     }
8414
8415   if (block_event)
8416     return;
8417
8418   if (window_event_mask & type_event_mask)
8419     {
8420       event = _gdk_make_event ((GdkWindow *)window, type, event_in_queue, TRUE);
8421       gdk_event_set_device (event, device);
8422
8423       if (source_device)
8424         gdk_event_set_source_device (event, source_device);
8425
8426       event->crossing.time = time_;
8427       event->crossing.subwindow = subwindow;
8428       if (subwindow)
8429         g_object_ref (subwindow);
8430       convert_toplevel_coords_to_window ((GdkWindow *)window,
8431                                          toplevel_x, toplevel_y,
8432                                          &event->crossing.x, &event->crossing.y);
8433       event->crossing.x_root = toplevel_x + toplevel->x;
8434       event->crossing.y_root = toplevel_y + toplevel->y;
8435       event->crossing.mode = mode;
8436       event->crossing.detail = notify_type;
8437       event->crossing.focus = FALSE;
8438       event->crossing.state = mask;
8439     }
8440 }
8441
8442
8443 /* The coordinates are in the toplevel window that src/dest are in.
8444  * src and dest are always (if != NULL) in the same toplevel, as
8445  * we get a leave-notify and set the window_under_pointer to null
8446  * before crossing to another toplevel.
8447  */
8448 void
8449 _gdk_synthesize_crossing_events (GdkDisplay                 *display,
8450                                  GdkWindow                  *src,
8451                                  GdkWindow                  *dest,
8452                                  GdkDevice                  *device,
8453                                  GdkDevice                  *source_device,
8454                                  GdkCrossingMode             mode,
8455                                  gint                        toplevel_x,
8456                                  gint                        toplevel_y,
8457                                  GdkModifierType             mask,
8458                                  guint32                     time_,
8459                                  GdkEvent                   *event_in_queue,
8460                                  gulong                      serial,
8461                                  gboolean                    non_linear)
8462 {
8463   GdkWindow *c;
8464   GdkWindow *win, *last, *next;
8465   GList *path, *list;
8466   GdkWindow *a;
8467   GdkWindow *b;
8468   GdkWindow *toplevel;
8469   GdkNotifyType notify_type;
8470
8471   /* TODO: Don't send events to toplevel, as we get those from the windowing system */
8472
8473   a = src;
8474   b = dest;
8475   if (src == dest)
8476     return; /* No crossings generated between src and dest */
8477
8478   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
8479     {
8480       if (a && gdk_window_get_device_events (src, device) == 0)
8481         a = NULL;
8482
8483       if (b && gdk_window_get_device_events (dest, device) == 0)
8484         b = NULL;
8485     }
8486
8487   if (!a && !b)
8488     return;
8489
8490   c = find_common_ancestor (a, b);
8491
8492   non_linear |= (c != a) && (c != b);
8493
8494   if (a) /* There might not be a source (i.e. if no previous pointer_in_window) */
8495     {
8496       toplevel = gdk_window_get_toplevel (a);
8497
8498       /* Traverse up from a to (excluding) c sending leave events */
8499       if (non_linear)
8500         notify_type = GDK_NOTIFY_NONLINEAR;
8501       else if (c == a)
8502         notify_type = GDK_NOTIFY_INFERIOR;
8503       else
8504         notify_type = GDK_NOTIFY_ANCESTOR;
8505       send_crossing_event (display, toplevel,
8506                            a, GDK_LEAVE_NOTIFY,
8507                            mode,
8508                            notify_type,
8509                            NULL, device, source_device,
8510                            toplevel_x, toplevel_y,
8511                            mask, time_,
8512                            event_in_queue,
8513                            serial);
8514
8515       if (c != a)
8516         {
8517           if (non_linear)
8518             notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
8519           else
8520             notify_type = GDK_NOTIFY_VIRTUAL;
8521
8522           last = a;
8523           win = get_event_parent (a);
8524           while (win != c && win->window_type != GDK_WINDOW_ROOT)
8525             {
8526               send_crossing_event (display, toplevel,
8527                                    win, GDK_LEAVE_NOTIFY,
8528                                    mode,
8529                                    notify_type,
8530                                    (GdkWindow *)last,
8531                                    device, source_device,
8532                                    toplevel_x, toplevel_y,
8533                                    mask, time_,
8534                                    event_in_queue,
8535                                    serial);
8536
8537               last = win;
8538               win = get_event_parent (win);
8539             }
8540         }
8541     }
8542
8543   if (b) /* Might not be a dest, e.g. if we're moving out of the window */
8544     {
8545       toplevel = gdk_window_get_toplevel ((GdkWindow *)b);
8546
8547       /* Traverse down from c to b */
8548       if (c != b)
8549         {
8550           path = NULL;
8551           win = get_event_parent (b);
8552           while (win != c && win->window_type != GDK_WINDOW_ROOT)
8553             {
8554               path = g_list_prepend (path, win);
8555               win = get_event_parent (win);
8556             }
8557
8558           if (non_linear)
8559             notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
8560           else
8561             notify_type = GDK_NOTIFY_VIRTUAL;
8562
8563           list = path;
8564           while (list)
8565             {
8566               win = list->data;
8567               list = g_list_next (list);
8568               if (list)
8569                 next = list->data;
8570               else
8571                 next = b;
8572
8573               send_crossing_event (display, toplevel,
8574                                    win, GDK_ENTER_NOTIFY,
8575                                    mode,
8576                                    notify_type,
8577                                    (GdkWindow *)next,
8578                                    device, source_device,
8579                                    toplevel_x, toplevel_y,
8580                                    mask, time_,
8581                                    event_in_queue,
8582                                    serial);
8583             }
8584           g_list_free (path);
8585         }
8586
8587
8588       if (non_linear)
8589         notify_type = GDK_NOTIFY_NONLINEAR;
8590       else if (c == a)
8591         notify_type = GDK_NOTIFY_ANCESTOR;
8592       else
8593         notify_type = GDK_NOTIFY_INFERIOR;
8594
8595       send_crossing_event (display, toplevel,
8596                            b, GDK_ENTER_NOTIFY,
8597                            mode,
8598                            notify_type,
8599                            NULL,
8600                            device, source_device,
8601                            toplevel_x, toplevel_y,
8602                            mask, time_,
8603                            event_in_queue,
8604                            serial);
8605     }
8606 }
8607
8608 /* Returns the window inside the event window with the pointer in it
8609  * at the specified coordinates, or NULL if its not in any child of
8610  * the toplevel. It also takes into account !owner_events grabs.
8611  */
8612 static GdkWindow *
8613 get_pointer_window (GdkDisplay *display,
8614                     GdkWindow *event_window,
8615                     GdkDevice *device,
8616                     gdouble toplevel_x,
8617                     gdouble toplevel_y,
8618                     gulong serial)
8619 {
8620   GdkWindow *pointer_window;
8621   GdkDeviceGrabInfo *grab;
8622   GdkPointerWindowInfo *pointer_info;
8623
8624   pointer_info = _gdk_display_get_pointer_info (display, device);
8625
8626   if (event_window == pointer_info->toplevel_under_pointer)
8627     pointer_window =
8628       _gdk_window_find_descendant_at (event_window,
8629                                       toplevel_x, toplevel_y,
8630                                       NULL, NULL);
8631   else
8632     pointer_window = NULL;
8633
8634   grab = _gdk_display_has_device_grab (display, device, serial);
8635   if (grab != NULL &&
8636       !grab->owner_events &&
8637       pointer_window != grab->window)
8638     pointer_window = NULL;
8639
8640   return pointer_window;
8641 }
8642
8643 void
8644 _gdk_display_set_window_under_pointer (GdkDisplay *display,
8645                                        GdkDevice  *device,
8646                                        GdkWindow  *window)
8647 {
8648   GdkPointerWindowInfo *device_info;
8649
8650   device_info = _gdk_display_get_pointer_info (display, device);
8651
8652   if (device_info->window_under_pointer)
8653     g_object_unref (device_info->window_under_pointer);
8654   device_info->window_under_pointer = window;
8655
8656   if (window)
8657     {
8658       g_object_ref (window);
8659       update_cursor (display, device);
8660     }
8661
8662   _gdk_display_enable_motion_hints (display, device);
8663 }
8664
8665 /**
8666  * gdk_pointer_grab:
8667  * @window: the #GdkWindow which will own the grab (the grab window).
8668  * @owner_events: if %FALSE then all pointer events are reported with respect to
8669  *                @window and are only reported if selected by @event_mask. If %TRUE then pointer
8670  *                events for this application are reported as normal, but pointer events outside
8671  *                this application are reported with respect to @window and only if selected by
8672  *                @event_mask. In either mode, unreported events are discarded.
8673  * @event_mask: specifies the event mask, which is used in accordance with
8674  *              @owner_events. Note that only pointer events (i.e. button and motion events)
8675  *              may be selected.
8676  * @confine_to: (allow-none): If non-%NULL, the pointer will be confined to this
8677  *              window during the grab. If the pointer is outside @confine_to, it will
8678  *              automatically be moved to the closest edge of @confine_to and enter
8679  *              and leave events will be generated as necessary.
8680  * @cursor: (allow-none): the cursor to display while the grab is active. If this is %NULL then
8681  *          the normal cursors are used for @window and its descendants, and the cursor
8682  *          for @window is used for all other windows.
8683  * @time_: the timestamp of the event which led to this pointer grab. This usually
8684  *         comes from a #GdkEventButton struct, though %GDK_CURRENT_TIME can be used if
8685  *         the time isn't known.
8686  *
8687  * Grabs the pointer (usually a mouse) so that all events are passed to this
8688  * application until the pointer is ungrabbed with gdk_pointer_ungrab(), or
8689  * the grab window becomes unviewable.
8690  * This overrides any previous pointer grab by this client.
8691  *
8692  * Pointer grabs are used for operations which need complete control over mouse
8693  * events, even if the mouse leaves the application.
8694  * For example in GTK+ it is used for Drag and Drop, for dragging the handle in
8695  * the #GtkHPaned and #GtkVPaned widgets.
8696  *
8697  * Note that if the event mask of an X window has selected both button press and
8698  * button release events, then a button press event will cause an automatic
8699  * pointer grab until the button is released.
8700  * X does this automatically since most applications expect to receive button
8701  * press and release events in pairs.
8702  * It is equivalent to a pointer grab on the window with @owner_events set to
8703  * %TRUE.
8704  *
8705  * If you set up anything at the time you take the grab that needs to be cleaned
8706  * up when the grab ends, you should handle the #GdkEventGrabBroken events that
8707  * are emitted when the grab ends unvoluntarily.
8708  *
8709  * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
8710  *
8711  * Deprecated: 3.0: Use gdk_device_grab() instead.
8712  **/
8713 GdkGrabStatus
8714 gdk_pointer_grab (GdkWindow *     window,
8715                   gboolean        owner_events,
8716                   GdkEventMask    event_mask,
8717                   GdkWindow *     confine_to,
8718                   GdkCursor *     cursor,
8719                   guint32         time)
8720 {
8721   GdkWindow *native;
8722   GdkDisplay *display;
8723   GdkDeviceManager *device_manager;
8724   GdkDevice *device;
8725   GdkGrabStatus res = 0;
8726   gulong serial;
8727   GList *devices, *dev;
8728
8729   g_return_val_if_fail (window != NULL, 0);
8730   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8731   g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
8732
8733   /* We need a native window for confine to to work, ensure we have one */
8734   if (confine_to)
8735     {
8736       if (!gdk_window_ensure_native (confine_to))
8737         {
8738           g_warning ("Can't confine to grabbed window, not native");
8739           confine_to = NULL;
8740         }
8741     }
8742
8743   /* Non-viewable client side window => fail */
8744   if (!_gdk_window_has_impl (window) &&
8745       !gdk_window_is_viewable (window))
8746     return GDK_GRAB_NOT_VIEWABLE;
8747
8748   native = gdk_window_get_toplevel (window);
8749   while (gdk_window_is_offscreen (native))
8750     {
8751       native = gdk_offscreen_window_get_embedder (native);
8752
8753       if (native == NULL ||
8754           (!_gdk_window_has_impl (native) &&
8755            !gdk_window_is_viewable (native)))
8756         return GDK_GRAB_NOT_VIEWABLE;
8757
8758       native = gdk_window_get_toplevel (native);
8759     }
8760
8761   display = gdk_window_get_display (window);
8762
8763   serial = _gdk_display_get_next_serial (display);
8764   device_manager = gdk_display_get_device_manager (display);
8765   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
8766
8767   /* FIXME: Should this be generic to all backends? */
8768   /* FIXME: What happens with extended devices? */
8769   for (dev = devices; dev; dev = dev->next)
8770     {
8771       device = dev->data;
8772
8773       if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
8774         continue;
8775
8776       res = GDK_DEVICE_GET_CLASS (device)->grab (device,
8777                                                  native,
8778                                                  owner_events,
8779                                                  get_native_grab_event_mask (event_mask),
8780                                                  confine_to,
8781                                                  cursor,
8782                                                  time);
8783
8784       if (res == GDK_GRAB_SUCCESS)
8785         _gdk_display_add_device_grab (display,
8786                                       device,
8787                                       window,
8788                                       native,
8789                                       GDK_OWNERSHIP_NONE,
8790                                       owner_events,
8791                                       event_mask,
8792                                       serial,
8793                                       time,
8794                                       FALSE);
8795     }
8796
8797   /* FIXME: handle errors when grabbing */
8798
8799   g_list_free (devices);
8800
8801   return res;
8802 }
8803
8804 /**
8805  * gdk_keyboard_grab:
8806  * @window: the #GdkWindow which will own the grab (the grab window).
8807  * @owner_events: if %FALSE then all keyboard events are reported with respect to
8808  *   @window. If %TRUE then keyboard events for this application are
8809  *   reported as normal, but keyboard events outside this application
8810  *   are reported with respect to @window. Both key press and key
8811  *   release events are always reported, independant of the event mask
8812  *   set by the application.
8813  * @time_: a timestamp from a #GdkEvent, or %GDK_CURRENT_TIME if no timestamp is
8814  *   available.
8815  *
8816  * Grabs the keyboard so that all events are passed to this
8817  * application until the keyboard is ungrabbed with gdk_keyboard_ungrab().
8818  * This overrides any previous keyboard grab by this client.
8819  *
8820  * If you set up anything at the time you take the grab that needs to be cleaned
8821  * up when the grab ends, you should handle the #GdkEventGrabBroken events that
8822  * are emitted when the grab ends unvoluntarily.
8823  *
8824  * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
8825  *
8826  * Deprecated: 3.0: Use gdk_device_grab() instead.
8827  **/
8828 GdkGrabStatus
8829 gdk_keyboard_grab (GdkWindow *window,
8830                    gboolean   owner_events,
8831                    guint32    time)
8832 {
8833   GdkWindow *native;
8834   GdkDisplay *display;
8835   GdkDeviceManager *device_manager;
8836   GdkDevice *device;
8837   GdkGrabStatus res = 0;
8838   gulong serial;
8839   GList *devices, *dev;
8840
8841   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8842
8843   /* Non-viewable client side window => fail */
8844   if (!_gdk_window_has_impl (window) &&
8845       !gdk_window_is_viewable (window))
8846     return GDK_GRAB_NOT_VIEWABLE;
8847
8848   native = gdk_window_get_toplevel (window);
8849
8850   while (gdk_window_is_offscreen (native))
8851     {
8852       native = gdk_offscreen_window_get_embedder (native);
8853
8854       if (native == NULL ||
8855           (!_gdk_window_has_impl (native) &&
8856            !gdk_window_is_viewable (native)))
8857         return GDK_GRAB_NOT_VIEWABLE;
8858
8859       native = gdk_window_get_toplevel (native);
8860     }
8861
8862   display = gdk_window_get_display (window);
8863   serial = _gdk_display_get_next_serial (display);
8864   device_manager = gdk_display_get_device_manager (display);
8865   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
8866
8867   /* FIXME: Should this be generic to all backends? */
8868   /* FIXME: What happens with extended devices? */
8869   for (dev = devices; dev; dev = dev->next)
8870     {
8871       device = dev->data;
8872
8873       if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
8874         continue;
8875
8876       res = GDK_DEVICE_GET_CLASS (device)->grab (device,
8877                                                  native,
8878                                                  owner_events,
8879                                                  GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
8880                                                  NULL,
8881                                                  NULL,
8882                                                  time);
8883
8884       if (res == GDK_GRAB_SUCCESS)
8885         _gdk_display_add_device_grab (display,
8886                                       device,
8887                                       window,
8888                                       native,
8889                                       GDK_OWNERSHIP_NONE,
8890                                       owner_events, 0,
8891                                       serial,
8892                                       time,
8893                                       FALSE);
8894     }
8895
8896   /* FIXME: handle errors when grabbing */
8897
8898   g_list_free (devices);
8899
8900   return res;
8901 }
8902
8903 /**
8904  * gdk_window_geometry_changed:
8905  * @window: an embedded offscreen #GdkWindow
8906  *
8907  * This function informs GDK that the geometry of an embedded
8908  * offscreen window has changed. This is necessary for GDK to keep
8909  * track of which offscreen window the pointer is in.
8910  *
8911  * Since: 2.18
8912  */
8913 void
8914 gdk_window_geometry_changed (GdkWindow *window)
8915 {
8916   _gdk_synthesize_crossing_events_for_geometry_change (window);
8917 }
8918
8919 static void
8920 source_events_device_added (GdkDeviceManager *device_manager,
8921                             GdkDevice        *device,
8922                             gpointer          user_data)
8923 {
8924   GdkWindow *window;
8925   GdkEventMask event_mask;
8926   GdkInputSource source;
8927
8928   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_FLOATING)
8929     return;
8930
8931   window = user_data;
8932   source = gdk_device_get_source (device);
8933
8934   event_mask = GPOINTER_TO_INT (g_hash_table_lookup (window->source_event_masks,
8935                                                      GINT_TO_POINTER (source)));
8936   if (event_mask)
8937     gdk_window_set_device_events (window, device, event_mask);
8938 }
8939
8940 static void
8941 source_events_device_changed (GdkDeviceManager *device_manager,
8942                               GdkDevice        *device,
8943                               gpointer          user_data)
8944 {
8945   GdkDeviceType type;
8946   GdkInputSource source;
8947   GdkEventMask event_mask;
8948   GdkWindow *window;
8949
8950   window = user_data;
8951   type = gdk_device_get_device_type (device);
8952   source = gdk_device_get_source (device);
8953
8954   event_mask = GPOINTER_TO_INT (g_hash_table_lookup (window->source_event_masks,
8955                                                      GINT_TO_POINTER (source)));
8956
8957   if (!event_mask)
8958     return;
8959
8960   if (type == GDK_DEVICE_TYPE_FLOATING)
8961     {
8962       /* The device was just floated, enable its event mask */
8963       gdk_window_set_device_events (window, device, event_mask);
8964     }
8965   else if (type == GDK_DEVICE_TYPE_SLAVE)
8966     gdk_window_set_device_events (window, device, 0);
8967 }
8968
8969 /**
8970  * gdk_window_set_source_events:
8971  * @window: a #GdkWindow
8972  * @source: a #GdkInputSource to define the source class.
8973  * @event_mask: event mask for @window
8974  *
8975  * Sets the event mask for any floating device (i.e. not attached to any
8976  * visible pointer) that has the source defined as @source. This event
8977  * mask will be applied both to currently existing, newly added devices
8978  * after this call, and devices being attached/detached.
8979  *
8980  * Since: 3.0
8981  **/
8982 void
8983 gdk_window_set_source_events (GdkWindow      *window,
8984                               GdkInputSource  source,
8985                               GdkEventMask    event_mask)
8986 {
8987   GdkDeviceManager *device_manager;
8988   GdkDisplay *display;
8989   GList *devices, *d;
8990   guint size;
8991
8992   g_return_if_fail (GDK_IS_WINDOW (window));
8993
8994   display = gdk_window_get_display (window);
8995   device_manager = gdk_display_get_device_manager (display);
8996
8997   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
8998
8999   /* Set event mask for existing devices */
9000   for (d = devices; d; d = d->next)
9001     {
9002       GdkDevice *device = d->data;
9003
9004       if (source == gdk_device_get_source (device))
9005         gdk_window_set_device_events (window, device, event_mask);
9006     }
9007
9008   g_list_free (devices);
9009
9010   /* Update accounting */
9011   if (G_UNLIKELY (!window->source_event_masks))
9012     window->source_event_masks = g_hash_table_new (NULL, NULL);
9013
9014   if (event_mask)
9015     g_hash_table_insert (window->source_event_masks,
9016                          GUINT_TO_POINTER (source),
9017                          GUINT_TO_POINTER (event_mask));
9018   else
9019     g_hash_table_remove (window->source_event_masks,
9020                          GUINT_TO_POINTER (source));
9021
9022   size = g_hash_table_size (window->source_event_masks);
9023
9024   /* Update handler if needed */
9025   if (!window->device_added_handler_id && size > 0)
9026     {
9027       window->device_added_handler_id =
9028         g_signal_connect (device_manager, "device-added",
9029                           G_CALLBACK (source_events_device_added), window);
9030       window->device_changed_handler_id =
9031         g_signal_connect (device_manager, "device-changed",
9032                           G_CALLBACK (source_events_device_changed), window);
9033     }
9034   else if (window->device_added_handler_id && size == 0)
9035     g_signal_handler_disconnect (device_manager, window->device_added_handler_id);
9036 }
9037
9038 /**
9039  * gdk_window_get_source_events:
9040  * @window: a #GdkWindow
9041  * @source: a #GdkInputSource to define the source class.
9042  *
9043  * Returns the event mask for @window corresponding to the device class specified
9044  * by @source.
9045  *
9046  * Returns: source event mask for @window
9047  **/
9048 GdkEventMask
9049 gdk_window_get_source_events (GdkWindow      *window,
9050                               GdkInputSource  source)
9051 {
9052   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
9053
9054   return GPOINTER_TO_UINT (g_hash_table_lookup (window->source_event_masks,
9055                                                 GUINT_TO_POINTER (source)));
9056 }
9057
9058 static gboolean
9059 do_synthesize_crossing_event (gpointer data)
9060 {
9061   GdkDisplay *display;
9062   GdkWindow *changed_toplevel;
9063   GHashTableIter iter;
9064   gpointer key, value;
9065   gulong serial;
9066
9067   changed_toplevel = data;
9068
9069   changed_toplevel->synthesize_crossing_event_queued = FALSE;
9070
9071   if (GDK_WINDOW_DESTROYED (changed_toplevel))
9072     return FALSE;
9073
9074   display = gdk_window_get_display (changed_toplevel);
9075   serial = _gdk_display_get_next_serial (display);
9076   g_hash_table_iter_init (&iter, display->pointers_info);
9077
9078   while (g_hash_table_iter_next (&iter, &key, &value))
9079     {
9080       GdkWindow *new_window_under_pointer;
9081       GdkPointerWindowInfo *pointer_info = value;
9082       GdkDevice *device = key;
9083
9084       if (changed_toplevel == pointer_info->toplevel_under_pointer)
9085         {
9086           new_window_under_pointer =
9087             get_pointer_window (display, changed_toplevel,
9088                                 device,
9089                                 pointer_info->toplevel_x,
9090                                 pointer_info->toplevel_y,
9091                                 serial);
9092           if (new_window_under_pointer != pointer_info->window_under_pointer)
9093             {
9094               _gdk_synthesize_crossing_events (display,
9095                                                pointer_info->window_under_pointer,
9096                                                new_window_under_pointer,
9097                                                device, pointer_info->last_slave,
9098                                                GDK_CROSSING_NORMAL,
9099                                                pointer_info->toplevel_x,
9100                                                pointer_info->toplevel_y,
9101                                                pointer_info->state,
9102                                                GDK_CURRENT_TIME,
9103                                                NULL,
9104                                                serial,
9105                                                FALSE);
9106               _gdk_display_set_window_under_pointer (display, device, new_window_under_pointer);
9107             }
9108         }
9109     }
9110
9111   return FALSE;
9112 }
9113
9114 void
9115 _gdk_synthesize_crossing_events_for_geometry_change (GdkWindow *changed_window)
9116 {
9117   GdkWindow *toplevel;
9118
9119   toplevel = get_event_toplevel (changed_window);
9120
9121   if (!toplevel->synthesize_crossing_event_queued)
9122     {
9123       toplevel->synthesize_crossing_event_queued = TRUE;
9124
9125       gdk_threads_add_idle_full (GDK_PRIORITY_EVENTS - 1,
9126                                  do_synthesize_crossing_event,
9127                                  g_object_ref (toplevel),
9128                                  g_object_unref);
9129     }
9130 }
9131
9132 /* Don't use for crossing events */
9133 static GdkWindow *
9134 get_event_window (GdkDisplay                 *display,
9135                   GdkDevice                  *device,
9136                   GdkEventSequence           *sequence,
9137                   GdkWindow                  *pointer_window,
9138                   GdkEventType                type,
9139                   GdkModifierType             mask,
9140                   guint                      *evmask_out,
9141                   gboolean                    pointer_emulated,
9142                   gulong                      serial)
9143 {
9144   guint evmask, emulated_mask = 0;
9145   GdkWindow *grab_window;
9146   GdkDeviceGrabInfo *grab;
9147   GdkTouchGrabInfo *touch_grab;
9148
9149   touch_grab = _gdk_display_has_touch_grab (display, device, sequence, serial);
9150   grab = _gdk_display_get_last_device_grab (display, device);
9151
9152   if (is_touch_type (type) && pointer_emulated)
9153     {
9154       switch (type)
9155         {
9156         case GDK_TOUCH_BEGIN:
9157           emulated_mask |= GDK_BUTTON_PRESS_MASK;
9158           break;
9159         case GDK_TOUCH_UPDATE:
9160           emulated_mask |= GDK_POINTER_MOTION_MASK;
9161           break;
9162         case GDK_TOUCH_END:
9163           emulated_mask |= GDK_BUTTON_RELEASE_MASK;
9164         default:
9165           break;
9166         }
9167     }
9168
9169   if (touch_grab != NULL &&
9170       (!grab || grab->implicit || touch_grab->serial >= grab->serial_start))
9171     {
9172       evmask = touch_grab->event_mask;
9173       evmask = update_evmask_for_button_motion (evmask, mask);
9174
9175       if (evmask & (type_masks[type] | emulated_mask))
9176         {
9177           if (evmask_out)
9178             *evmask_out = evmask;
9179           return touch_grab->window;
9180         }
9181       else
9182         return NULL;
9183     }
9184
9185   if (grab != NULL && !grab->owner_events)
9186     {
9187       evmask = grab->event_mask;
9188       evmask = update_evmask_for_button_motion (evmask, mask);
9189
9190       grab_window = grab->window;
9191
9192       if (evmask & (type_masks[type] | emulated_mask))
9193         {
9194           if (evmask_out)
9195             *evmask_out = evmask;
9196           return grab_window;
9197         }
9198       else
9199         return NULL;
9200     }
9201
9202   while (pointer_window != NULL)
9203     {
9204       evmask = pointer_window->event_mask;
9205       evmask = update_evmask_for_button_motion (evmask, mask);
9206
9207       if (evmask & (type_masks[type] | emulated_mask))
9208         {
9209           if (evmask_out)
9210             *evmask_out = evmask;
9211           return pointer_window;
9212         }
9213
9214       pointer_window = get_event_parent (pointer_window);
9215     }
9216
9217   if (grab != NULL &&
9218       grab->owner_events)
9219     {
9220       evmask = grab->event_mask;
9221       evmask = update_evmask_for_button_motion (evmask, mask);
9222
9223       if (evmask & (type_masks[type] | emulated_mask))
9224         {
9225           if (evmask_out)
9226             *evmask_out = evmask;
9227           return grab->window;
9228         }
9229       else
9230         return NULL;
9231     }
9232
9233   return NULL;
9234 }
9235
9236 static gboolean
9237 proxy_pointer_event (GdkDisplay                 *display,
9238                      GdkEvent                   *source_event,
9239                      gulong                      serial)
9240 {
9241   GdkWindow *toplevel_window, *event_window;
9242   GdkWindow *pointer_window;
9243   GdkPointerWindowInfo *pointer_info;
9244   GdkDevice *device, *source_device;
9245   GdkEvent *event;
9246   guint state;
9247   gdouble toplevel_x, toplevel_y;
9248   guint32 time_;
9249   gboolean non_linear, need_synthetic_enter = FALSE;
9250   gint event_type;
9251
9252   event_type = source_event->type;
9253   event_window = source_event->any.window;
9254   gdk_event_get_coords (source_event, &toplevel_x, &toplevel_y);
9255   gdk_event_get_state (source_event, &state);
9256   time_ = gdk_event_get_time (source_event);
9257   device = gdk_event_get_device (source_event);
9258   source_device = gdk_event_get_source_device (source_event);
9259   pointer_info = _gdk_display_get_pointer_info (display, device);
9260   toplevel_window = convert_native_coords_to_toplevel (event_window,
9261                                                        toplevel_x, toplevel_y,
9262                                                        &toplevel_x, &toplevel_y);
9263
9264   non_linear = FALSE;
9265   if ((source_event->type == GDK_LEAVE_NOTIFY ||
9266        source_event->type == GDK_ENTER_NOTIFY) &&
9267       (source_event->crossing.detail == GDK_NOTIFY_NONLINEAR ||
9268        source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))
9269     non_linear = TRUE;
9270
9271   if (pointer_info->need_touch_press_enter &&
9272       gdk_device_get_source (pointer_info->last_slave) != GDK_SOURCE_TOUCHSCREEN &&
9273       (source_event->type != GDK_TOUCH_UPDATE ||
9274        _gdk_event_get_pointer_emulated (source_event)))
9275     {
9276       pointer_info->need_touch_press_enter = FALSE;
9277       need_synthetic_enter = TRUE;
9278     }
9279
9280   /* If we get crossing events with subwindow unexpectedly being NULL
9281      that means there is a native subwindow that gdk doesn't know about.
9282      We track these and forward them, with the correct virtual window
9283      events inbetween.
9284      This is important to get right, as metacity uses gdk for the frame
9285      windows, but gdk doesn't know about the client windows reparented
9286      into the frame. */
9287   if (((source_event->type == GDK_LEAVE_NOTIFY &&
9288         source_event->crossing.detail == GDK_NOTIFY_INFERIOR) ||
9289        (source_event->type == GDK_ENTER_NOTIFY &&
9290         (source_event->crossing.detail == GDK_NOTIFY_VIRTUAL ||
9291          source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))) &&
9292       source_event->crossing.subwindow == NULL)
9293     {
9294       /* Left for an unknown (to gdk) subwindow */
9295
9296       /* Send leave events from window under pointer to event window
9297          that will get the subwindow == NULL window */
9298       _gdk_synthesize_crossing_events (display,
9299                                        pointer_info->window_under_pointer,
9300                                        event_window,
9301                                        device, source_device,
9302                                        source_event->crossing.mode,
9303                                        toplevel_x, toplevel_y,
9304                                        state, time_,
9305                                        source_event,
9306                                        serial,
9307                                        non_linear);
9308
9309       /* Send subwindow == NULL event */
9310       send_crossing_event (display,
9311                            toplevel_window,
9312                            event_window,
9313                            source_event->type,
9314                            source_event->crossing.mode,
9315                            source_event->crossing.detail,
9316                            NULL,
9317                            device, source_device,
9318                            toplevel_x, toplevel_y,
9319                            state, time_,
9320                            source_event,
9321                            serial);
9322
9323       _gdk_display_set_window_under_pointer (display, device, NULL);
9324       return TRUE;
9325     }
9326
9327   pointer_window = get_pointer_window (display, toplevel_window, device,
9328                                        toplevel_x, toplevel_y, serial);
9329
9330   if (((source_event->type == GDK_ENTER_NOTIFY &&
9331         source_event->crossing.detail == GDK_NOTIFY_INFERIOR) ||
9332        (source_event->type == GDK_LEAVE_NOTIFY &&
9333         (source_event->crossing.detail == GDK_NOTIFY_VIRTUAL ||
9334          source_event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL))) &&
9335       source_event->crossing.subwindow == NULL)
9336     {
9337       /* Entered from an unknown (to gdk) subwindow */
9338
9339       /* Send subwindow == NULL event */
9340       send_crossing_event (display,
9341                            toplevel_window,
9342                            event_window,
9343                            source_event->type,
9344                            source_event->crossing.mode,
9345                            source_event->crossing.detail,
9346                            NULL,
9347                            device, source_device,
9348                            toplevel_x, toplevel_y,
9349                            state, time_,
9350                            source_event,
9351                            serial);
9352
9353       /* Send enter events from event window to pointer_window */
9354       _gdk_synthesize_crossing_events (display,
9355                                        event_window,
9356                                        pointer_window,
9357                                        device, source_device,
9358                                        source_event->crossing.mode,
9359                                        toplevel_x, toplevel_y,
9360                                        state, time_,
9361                                        source_event,
9362                                        serial, non_linear);
9363       _gdk_display_set_window_under_pointer (display, device, pointer_window);
9364       return TRUE;
9365     }
9366
9367   if ((source_event->type != GDK_TOUCH_UPDATE ||
9368        _gdk_event_get_pointer_emulated (source_event)) &&
9369       pointer_info->window_under_pointer != pointer_window)
9370     {
9371       /* Either a toplevel crossing notify that ended up inside a child window,
9372          or a motion notify that got into another child window  */
9373
9374       /* Different than last time, send crossing events */
9375       _gdk_synthesize_crossing_events (display,
9376                                        pointer_info->window_under_pointer,
9377                                        pointer_window,
9378                                        device, source_device,
9379                                        GDK_CROSSING_NORMAL,
9380                                        toplevel_x, toplevel_y,
9381                                        state, time_,
9382                                        source_event,
9383                                        serial, non_linear);
9384       _gdk_display_set_window_under_pointer (display, device, pointer_window);
9385     }
9386   else if (source_event->type == GDK_MOTION_NOTIFY ||
9387            source_event->type == GDK_TOUCH_UPDATE)
9388     {
9389       GdkWindow *event_win;
9390       guint evmask;
9391       gboolean is_hint;
9392       GdkEventSequence *sequence;
9393
9394       sequence = gdk_event_get_event_sequence (source_event);
9395
9396       event_win = get_event_window (display,
9397                                     device,
9398                                     sequence,
9399                                     pointer_window,
9400                                     source_event->type,
9401                                     state,
9402                                     &evmask,
9403                                     _gdk_event_get_pointer_emulated (source_event),
9404                                     serial);
9405
9406       if (event_type == GDK_TOUCH_UPDATE)
9407         {
9408           if (_gdk_event_get_pointer_emulated (source_event))
9409             {
9410               /* Touch events emulating pointer events are transformed back
9411                * to pointer events if:
9412                * 1 - The event window doesn't select for touch events
9413                * 2 - There's no touch grab for this sequence, which means
9414                *     it was started as a pointer sequence, but a device
9415                *     grab added touch events afterwards, the sequence must
9416                *     not mutate in this case.
9417                */
9418               if ((evmask & GDK_TOUCH_MASK) == 0 ||
9419                   !_gdk_display_has_touch_grab (display, device, sequence, serial))
9420                 event_type = GDK_MOTION_NOTIFY;
9421             }
9422           else if ((evmask & GDK_TOUCH_MASK) == 0)
9423             return TRUE;
9424         }
9425
9426       if (is_touch_type (source_event->type) && !is_touch_type (event_type))
9427         state |= GDK_BUTTON1_MASK;
9428
9429       if (event_win &&
9430           gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER &&
9431           gdk_window_get_device_events (event_win, device) == 0)
9432         return TRUE;
9433
9434       /* The last device to interact with the window was a touch device,
9435        * which synthesized a leave notify event, so synthesize another enter
9436        * notify to tell the pointer is on the window.
9437        */
9438       if (need_synthetic_enter)
9439         _gdk_synthesize_crossing_events (display,
9440                                          NULL, pointer_window,
9441                                          device, source_device,
9442                                          GDK_CROSSING_DEVICE_SWITCH,
9443                                          toplevel_x, toplevel_y,
9444                                          state, time_, NULL,
9445                                          serial, FALSE);
9446
9447       is_hint = FALSE;
9448
9449       if (event_win &&
9450           event_type == GDK_MOTION_NOTIFY &&
9451           (evmask & GDK_POINTER_MOTION_HINT_MASK))
9452         {
9453           gulong *device_serial;
9454
9455           device_serial = g_hash_table_lookup (display->motion_hint_info, device);
9456
9457           if (!device_serial ||
9458               (*device_serial != 0 &&
9459                serial < *device_serial))
9460             event_win = NULL; /* Ignore event */
9461           else
9462             {
9463               is_hint = TRUE;
9464               *device_serial = G_MAXULONG;
9465             }
9466         }
9467
9468       if (!event_win)
9469         return TRUE;
9470
9471       event = gdk_event_new (event_type);
9472       event->any.window = g_object_ref (event_win);
9473       event->any.send_event = source_event->any.send_event;
9474
9475       gdk_event_set_device (event, gdk_event_get_device (source_event));
9476       gdk_event_set_source_device (event, source_device);
9477
9478       if (event_type == GDK_TOUCH_UPDATE)
9479         {
9480           event->touch.time = time_;
9481           event->touch.state = state | GDK_BUTTON1_MASK;
9482           event->touch.sequence = source_event->touch.sequence;
9483           event->touch.emulating_pointer = source_event->touch.emulating_pointer;
9484           convert_toplevel_coords_to_window (event_win,
9485                                              toplevel_x, toplevel_y,
9486                                              &event->touch.x, &event->touch.y);
9487           gdk_event_get_root_coords (source_event,
9488                                      &event->touch.x_root,
9489                                      &event->touch.y_root);
9490
9491           event->touch.axes = g_memdup (source_event->touch.axes,
9492                                         sizeof (gdouble) * gdk_device_get_n_axes (source_event->touch.device));
9493         }
9494       else
9495         {
9496           event->motion.time = time_;
9497           event->motion.state = state;
9498           event->motion.is_hint = is_hint;
9499
9500           convert_toplevel_coords_to_window (event_win,
9501                                              toplevel_x, toplevel_y,
9502                                              &event->motion.x, &event->motion.y);
9503           gdk_event_get_root_coords (source_event,
9504                                      &event->motion.x_root,
9505                                      &event->motion.y_root);
9506
9507           if (is_touch_type (source_event->type))
9508             event->motion.axes = g_memdup (source_event->touch.axes,
9509                                            sizeof (gdouble) * gdk_device_get_n_axes (source_event->touch.device));
9510           else
9511             event->motion.axes = g_memdup (source_event->motion.axes,
9512                                            sizeof (gdouble) * gdk_device_get_n_axes (source_event->motion.device));
9513         }
9514
9515       /* Just insert the event */
9516       _gdk_event_queue_insert_after (gdk_window_get_display (event_win),
9517                                      source_event, event);
9518     }
9519
9520   /* unlink all move events from queue.
9521      We handle our own, including our emulated masks. */
9522   return TRUE;
9523 }
9524
9525 #define GDK_ANY_BUTTON_MASK (GDK_BUTTON1_MASK | \
9526                              GDK_BUTTON2_MASK | \
9527                              GDK_BUTTON3_MASK | \
9528                              GDK_BUTTON4_MASK | \
9529                              GDK_BUTTON5_MASK)
9530
9531 static gboolean
9532 proxy_button_event (GdkEvent *source_event,
9533                     gulong serial)
9534 {
9535   GdkWindow *toplevel_window, *event_window;
9536   GdkWindow *event_win;
9537   GdkWindow *pointer_window;
9538   GdkWindow *parent;
9539   GdkEvent *event;
9540   GdkPointerWindowInfo *pointer_info;
9541   GdkDeviceGrabInfo *pointer_grab;
9542   guint state;
9543   guint32 time_;
9544   GdkEventType type;
9545   gdouble toplevel_x, toplevel_y;
9546   GdkDisplay *display;
9547   GdkWindow *w;
9548   GdkDevice *device, *source_device;
9549   GdkEventMask evmask;
9550   GdkEventSequence *sequence;
9551
9552   type = source_event->any.type;
9553   event_window = source_event->any.window;
9554   gdk_event_get_coords (source_event, &toplevel_x, &toplevel_y);
9555   gdk_event_get_state (source_event, &state);
9556   time_ = gdk_event_get_time (source_event);
9557   device = gdk_event_get_device (source_event);
9558   source_device = gdk_event_get_source_device (source_event);
9559   display = gdk_window_get_display (source_event->any.window);
9560   toplevel_window = convert_native_coords_to_toplevel (event_window,
9561                                                        toplevel_x, toplevel_y,
9562                                                        &toplevel_x, &toplevel_y);
9563
9564   sequence = gdk_event_get_event_sequence (source_event);
9565
9566   pointer_info = _gdk_display_get_pointer_info (display, device);
9567   pointer_grab = _gdk_display_has_device_grab (display, device, serial);
9568
9569   if ((type == GDK_BUTTON_PRESS ||
9570        type == GDK_TOUCH_BEGIN) &&
9571       !source_event->any.send_event &&
9572       (!pointer_grab ||
9573        (type == GDK_TOUCH_BEGIN && pointer_grab->implicit &&
9574         !_gdk_event_get_pointer_emulated (source_event))))
9575     {
9576       pointer_window =
9577         _gdk_window_find_descendant_at (toplevel_window,
9578                                         toplevel_x, toplevel_y,
9579                                         NULL, NULL);
9580
9581       /* Find the event window, that gets the grab */
9582       w = pointer_window;
9583       while (w != NULL &&
9584              (parent = get_event_parent (w)) != NULL &&
9585              parent->window_type != GDK_WINDOW_ROOT)
9586         {
9587           if (w->event_mask & GDK_BUTTON_PRESS_MASK &&
9588               (type == GDK_BUTTON_PRESS ||
9589                _gdk_event_get_pointer_emulated (source_event)))
9590             break;
9591
9592           if (type == GDK_TOUCH_BEGIN &&
9593               w->event_mask & GDK_TOUCH_MASK)
9594             break;
9595
9596           w = parent;
9597         }
9598       pointer_window = w;
9599
9600       if (pointer_window)
9601         {
9602           if (type == GDK_TOUCH_BEGIN &&
9603               pointer_window->event_mask & GDK_TOUCH_MASK)
9604             {
9605               _gdk_display_add_touch_grab (display, device, sequence,
9606                                            pointer_window, event_window,
9607                                            gdk_window_get_events (pointer_window),
9608                                            serial, time_);
9609             }
9610           else if (type == GDK_BUTTON_PRESS ||
9611                    _gdk_event_get_pointer_emulated (source_event))
9612             {
9613               _gdk_display_add_device_grab  (display,
9614                                              device,
9615                                              pointer_window,
9616                                              event_window,
9617                                              GDK_OWNERSHIP_NONE,
9618                                              FALSE,
9619                                              gdk_window_get_events (pointer_window),
9620                                              serial,
9621                                              time_,
9622                                              TRUE);
9623               _gdk_display_device_grab_update (display, device,
9624                                                source_device, serial);
9625             }
9626         }
9627     }
9628
9629   pointer_window = get_pointer_window (display, toplevel_window, device,
9630                                        toplevel_x, toplevel_y,
9631                                        serial);
9632
9633   event_win = get_event_window (display,
9634                                 device,
9635                                 sequence,
9636                                 pointer_window,
9637                                 type, state,
9638                                 &evmask,
9639                                 _gdk_event_get_pointer_emulated (source_event),
9640                                 serial);
9641
9642   if (type == GDK_TOUCH_BEGIN || type == GDK_TOUCH_END)
9643     {
9644       if (_gdk_event_get_pointer_emulated (source_event))
9645         {
9646           if ((evmask & GDK_TOUCH_MASK) == 0 ||
9647               !_gdk_display_has_touch_grab (display, device, sequence, serial))
9648             {
9649               if (type == GDK_TOUCH_BEGIN)
9650                 type = GDK_BUTTON_PRESS;
9651               else if (type == GDK_TOUCH_END)
9652                 type = GDK_BUTTON_RELEASE;
9653             }
9654         }
9655       else if ((evmask & GDK_TOUCH_MASK) == 0)
9656         return TRUE;
9657     }
9658
9659   if (source_event->type == GDK_TOUCH_END && !is_touch_type (type))
9660     state |= GDK_BUTTON1_MASK;
9661
9662   if (event_win == NULL)
9663     return TRUE;
9664
9665   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER &&
9666       gdk_window_get_device_events (event_win, device) == 0)
9667     return TRUE;
9668
9669   if ((type == GDK_BUTTON_PRESS ||
9670        (type == GDK_TOUCH_BEGIN &&
9671         _gdk_event_get_pointer_emulated (source_event))) &&
9672       pointer_info->need_touch_press_enter)
9673     {
9674       GdkCrossingMode mode;
9675
9676       /* The last device to interact with the window was a touch device,
9677        * which synthesized a leave notify event, so synthesize another enter
9678        * notify to tell the pointer is on the window.
9679        */
9680       if (gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
9681         mode = GDK_CROSSING_TOUCH_BEGIN;
9682       else
9683         mode = GDK_CROSSING_DEVICE_SWITCH;
9684
9685       pointer_info->need_touch_press_enter = FALSE;
9686       _gdk_synthesize_crossing_events (display,
9687                                        NULL,
9688                                        pointer_info->window_under_pointer,
9689                                        device, source_device, mode,
9690                                        toplevel_x, toplevel_y,
9691                                        state, time_, source_event,
9692                                        serial, FALSE);
9693     }
9694   else if (type == GDK_SCROLL &&
9695            (((evmask & GDK_SMOOTH_SCROLL_MASK) == 0 &&
9696              source_event->scroll.direction == GDK_SCROLL_SMOOTH) ||
9697             ((evmask & GDK_SMOOTH_SCROLL_MASK) != 0 &&
9698              source_event->scroll.direction != GDK_SCROLL_SMOOTH &&
9699              _gdk_event_get_pointer_emulated (source_event))))
9700     return FALSE;
9701
9702   event = _gdk_make_event (event_win, type, source_event, FALSE);
9703
9704   switch (type)
9705     {
9706     case GDK_BUTTON_PRESS:
9707     case GDK_BUTTON_RELEASE:
9708       event->button.button = source_event->button.button;
9709       convert_toplevel_coords_to_window (event_win,
9710                                          toplevel_x, toplevel_y,
9711                                          &event->button.x, &event->button.y);
9712       gdk_event_get_root_coords (source_event,
9713                                  &event->button.x_root,
9714                                  &event->button.y_root);
9715       gdk_event_set_device (event, gdk_event_get_device (source_event));
9716       gdk_event_set_source_device (event, source_device);
9717
9718       if (is_touch_type (source_event->type))
9719         {
9720           if (type == GDK_BUTTON_RELEASE)
9721             event->button.state |= GDK_BUTTON1_MASK;
9722           event->button.button = 1;
9723           event->button.axes = g_memdup (source_event->touch.axes,
9724                                          sizeof (gdouble) * gdk_device_get_n_axes (source_event->touch.device));
9725         }
9726       else
9727         {
9728           event->button.button = source_event->button.button;
9729           event->button.axes = g_memdup (source_event->button.axes,
9730                                          sizeof (gdouble) * gdk_device_get_n_axes (source_event->button.device));
9731         }
9732
9733       if (type == GDK_BUTTON_PRESS)
9734         _gdk_event_button_generate (display, event);
9735       else if ((type == GDK_BUTTON_RELEASE ||
9736                 (type == GDK_TOUCH_END &&
9737                  _gdk_event_get_pointer_emulated (source_event))) &&
9738                pointer_window == pointer_info->window_under_pointer &&
9739                gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
9740         {
9741           /* Synthesize a leave notify event
9742            * whenever a touch device is released
9743            */
9744           pointer_info->need_touch_press_enter = TRUE;
9745           _gdk_synthesize_crossing_events (display,
9746                                            pointer_window, NULL,
9747                                            device, source_device,
9748                                            GDK_CROSSING_TOUCH_END,
9749                                            toplevel_x, toplevel_y,
9750                                            state, time_, NULL,
9751                                            serial, FALSE);
9752         }
9753       return TRUE;
9754
9755     case GDK_TOUCH_BEGIN:
9756     case GDK_TOUCH_END:
9757       convert_toplevel_coords_to_window (event_win,
9758                                          toplevel_x, toplevel_y,
9759                                          &event->button.x, &event->button.y);
9760       gdk_event_get_root_coords (source_event,
9761                                  &event->touch.x_root,
9762                                  &event->touch.y_root);
9763       event->touch.state = state;
9764       event->touch.device = source_event->touch.device;
9765       event->touch.axes = g_memdup (source_event->touch.axes,
9766                                      sizeof (gdouble) * gdk_device_get_n_axes (source_event->touch.device));
9767       event->touch.sequence = source_event->touch.sequence;
9768       event->touch.emulating_pointer = source_event->touch.emulating_pointer;
9769
9770       gdk_event_set_source_device (event, source_device);
9771
9772       if ((type == GDK_TOUCH_END &&
9773            _gdk_event_get_pointer_emulated (source_event)) &&
9774            pointer_window == pointer_info->window_under_pointer &&
9775            gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
9776         {
9777           /* Synthesize a leave notify event
9778            * whenever a touch device is released
9779            */
9780           pointer_info->need_touch_press_enter = TRUE;
9781           _gdk_synthesize_crossing_events (display,
9782                                            pointer_window, NULL,
9783                                            device, source_device,
9784                                            GDK_CROSSING_TOUCH_END,
9785                                            toplevel_x, toplevel_y,
9786                                            state, time_, NULL,
9787                                            serial, FALSE);
9788         }
9789       return TRUE;
9790
9791     case GDK_SCROLL:
9792       event->scroll.direction = source_event->scroll.direction;
9793       convert_toplevel_coords_to_window (event_win,
9794                                          toplevel_x, toplevel_y,
9795                                          &event->scroll.x, &event->scroll.y);
9796       event->scroll.x_root = source_event->scroll.x_root;
9797       event->scroll.y_root = source_event->scroll.y_root;
9798       event->scroll.state = state;
9799       event->scroll.device = source_event->scroll.device;
9800       event->scroll.delta_x = source_event->scroll.delta_x;
9801       event->scroll.delta_y = source_event->scroll.delta_y;
9802       gdk_event_set_source_device (event, source_device);
9803       return TRUE;
9804
9805     default:
9806       return FALSE;
9807     }
9808
9809   return TRUE; /* Always unlink original, we want to obey the emulated event mask */
9810 }
9811
9812 #ifdef DEBUG_WINDOW_PRINTING
9813
9814 #ifdef GDK_WINDOWING_X11
9815 #include "x11/gdkx.h"
9816 #endif
9817
9818 static void
9819 gdk_window_print (GdkWindow *window,
9820                   int indent)
9821 {
9822   char *s;
9823   const char *window_types[] = {
9824     "root",
9825     "toplevel",
9826     "child",
9827     "dialog",
9828     "temp",
9829     "foreign",
9830     "offscreen"
9831   };
9832
9833   g_print ("%*s%p: [%s] %d,%d %dx%d", indent, "", window,
9834            window->user_data ? g_type_name_from_instance (window->user_data) : "no widget",
9835            window->x, window->y,
9836            window->width, window->height
9837            );
9838
9839   if (gdk_window_has_impl (window))
9840     {
9841 #ifdef GDK_WINDOWING_X11
9842       g_print (" impl(0x%lx)", gdk_x11_window_get_xid (window));
9843 #endif
9844     }
9845
9846   if (window->window_type != GDK_WINDOW_CHILD)
9847     g_print (" %s", window_types[window->window_type]);
9848
9849   if (window->input_only)
9850     g_print (" input-only");
9851
9852   if (window->shaped)
9853     g_print (" shaped");
9854
9855   if (!gdk_window_is_visible ((GdkWindow *)window))
9856     g_print (" hidden");
9857
9858   g_print (" abs[%d,%d]",
9859            window->abs_x, window->abs_y);
9860
9861   s = print_region (window->clip_region);
9862   g_print (" clipbox[%s]", s);
9863
9864   g_print ("\n");
9865 }
9866
9867
9868 static void
9869 gdk_window_print_tree (GdkWindow *window,
9870                        int indent,
9871                        gboolean include_input_only)
9872 {
9873   GList *l;
9874
9875   if (window->input_only && !include_input_only)
9876     return;
9877
9878   gdk_window_print (window, indent);
9879
9880   for (l = window->children; l != NULL; l = l->next)
9881     gdk_window_print_tree (l->data, indent + 4, include_input_only);
9882 }
9883
9884 #endif /* DEBUG_WINDOW_PRINTING */
9885
9886 void
9887 _gdk_windowing_got_event (GdkDisplay *display,
9888                           GList      *event_link,
9889                           GdkEvent   *event,
9890                           gulong      serial)
9891 {
9892   GdkWindow *event_window;
9893   gdouble x, y;
9894   gboolean unlink_event;
9895   GdkDeviceGrabInfo *button_release_grab;
9896   GdkPointerWindowInfo *pointer_info = NULL;
9897   GdkDevice *device, *source_device;
9898   gboolean is_toplevel;
9899
9900   if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
9901     display->last_event_time = gdk_event_get_time (event);
9902
9903   device = gdk_event_get_device (event);
9904   source_device = gdk_event_get_source_device (event);
9905
9906   if (device)
9907     {
9908       GdkInputMode mode;
9909
9910       if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
9911         {
9912           pointer_info = _gdk_display_get_pointer_info (display, device);
9913
9914           if (source_device != pointer_info->last_slave &&
9915               gdk_device_get_device_type (source_device) == GDK_DEVICE_TYPE_SLAVE)
9916             pointer_info->last_slave = source_device;
9917           else if (pointer_info->last_slave)
9918             source_device = pointer_info->last_slave;
9919         }
9920
9921       g_object_get (device, "input-mode", &mode, NULL);
9922       _gdk_display_device_grab_update (display, device, source_device, serial);
9923
9924       if (mode == GDK_MODE_DISABLED ||
9925           !_gdk_display_check_grab_ownership (display, device, serial))
9926         {
9927           /* Device events are blocked by another
9928            * device grab, or the device is disabled
9929            */
9930           unlink_event = TRUE;
9931           goto out;
9932         }
9933     }
9934
9935   event_window = event->any.window;
9936   if (!event_window)
9937     return;
9938
9939 #ifdef DEBUG_WINDOW_PRINTING
9940   if (event->type == GDK_KEY_PRESS &&
9941       (event->key.keyval == 0xa7 ||
9942        event->key.keyval == 0xbd))
9943     {
9944       gdk_window_print_tree (event_window, 0, event->key.keyval == 0xbd);
9945     }
9946 #endif
9947
9948   if (event->type == GDK_VISIBILITY_NOTIFY)
9949     {
9950       event_window->native_visibility = event->visibility.state;
9951       gdk_window_update_visibility_recursively (event_window, event_window);
9952       return;
9953     }
9954
9955   if (!(is_button_type (event->type) ||
9956         is_motion_type (event->type)) ||
9957       event_window->window_type == GDK_WINDOW_ROOT)
9958     return;
9959
9960   is_toplevel = gdk_window_is_toplevel (event_window);
9961
9962   if ((event->type == GDK_ENTER_NOTIFY ||
9963        event->type == GDK_LEAVE_NOTIFY) &&
9964       (event->crossing.mode == GDK_CROSSING_GRAB ||
9965        event->crossing.mode == GDK_CROSSING_UNGRAB) &&
9966       (_gdk_display_has_device_grab (display, device, serial) ||
9967        event->crossing.detail == GDK_NOTIFY_INFERIOR))
9968     {
9969       /* We synthesize all crossing events due to grabs ourselves,
9970        * so we ignore the native ones caused by our native pointer_grab
9971        * calls. Otherwise we would proxy these crossing event and cause
9972        * multiple copies of crossing events for grabs.
9973        *
9974        * We do want to handle grabs from other clients though, as for
9975        * instance alt-tab in metacity causes grabs like these and
9976        * we want to handle those. Thus the has_pointer_grab check.
9977        *
9978        * Implicit grabs on child windows create some grabbing events
9979        * that are sent before the button press. This means we can't
9980        * detect these with the has_pointer_grab check (as the implicit
9981        * grab is only noticed when we get button press event), so we
9982        * detect these events by checking for INFERIOR enter or leave
9983        * events. These should never be a problem to filter out.
9984        */
9985
9986       /* We ended up in this window after some (perhaps other clients)
9987        * grab, so update the toplevel_under_window state
9988        */
9989       if (is_toplevel &&
9990           event->type == GDK_ENTER_NOTIFY &&
9991           event->crossing.mode == GDK_CROSSING_UNGRAB)
9992         {
9993           if (pointer_info->toplevel_under_pointer)
9994             g_object_unref (pointer_info->toplevel_under_pointer);
9995           pointer_info->toplevel_under_pointer = g_object_ref (event_window);
9996         }
9997
9998       unlink_event = TRUE;
9999       goto out;
10000     }
10001
10002   /* Track toplevel_under_pointer */
10003   if (is_toplevel)
10004     {
10005       if (event->type == GDK_ENTER_NOTIFY &&
10006           event->crossing.detail != GDK_NOTIFY_INFERIOR)
10007         {
10008           if (pointer_info->toplevel_under_pointer)
10009             g_object_unref (pointer_info->toplevel_under_pointer);
10010           pointer_info->toplevel_under_pointer = g_object_ref (event_window);
10011         }
10012       else if (event->type == GDK_LEAVE_NOTIFY &&
10013                event->crossing.detail != GDK_NOTIFY_INFERIOR &&
10014                pointer_info->toplevel_under_pointer == event_window)
10015         {
10016           if (pointer_info->toplevel_under_pointer)
10017             g_object_unref (pointer_info->toplevel_under_pointer);
10018           pointer_info->toplevel_under_pointer = NULL;
10019         }
10020     }
10021
10022   if (pointer_info &&
10023       (!is_touch_type (event->type) ||
10024        _gdk_event_get_pointer_emulated (event)))
10025     {
10026       guint old_state, old_button;
10027
10028       /* Store last pointer window and position/state */
10029       old_state = pointer_info->state;
10030       old_button = pointer_info->button;
10031
10032       gdk_event_get_coords (event, &x, &y);
10033       convert_native_coords_to_toplevel (event_window, x, y,  &x, &y);
10034       pointer_info->toplevel_x = x;
10035       pointer_info->toplevel_y = y;
10036       gdk_event_get_state (event, &pointer_info->state);
10037
10038       if (event->type == GDK_BUTTON_PRESS ||
10039           event->type == GDK_BUTTON_RELEASE)
10040         pointer_info->button = event->button.button;
10041       else if (event->type == GDK_TOUCH_BEGIN ||
10042                event->type == GDK_TOUCH_END)
10043         pointer_info->button = 1;
10044
10045       if (device &&
10046           (pointer_info->state != old_state ||
10047            pointer_info->button != old_button))
10048         _gdk_display_enable_motion_hints (display, device);
10049     }
10050
10051   unlink_event = FALSE;
10052   if (is_motion_type (event->type))
10053     unlink_event = proxy_pointer_event (display, event, serial);
10054   else if (is_button_type (event->type))
10055     unlink_event = proxy_button_event (event, serial);
10056
10057   if ((event->type == GDK_BUTTON_RELEASE ||
10058        event->type == GDK_TOUCH_END) &&
10059       !event->any.send_event)
10060     {
10061       GdkEventSequence *sequence;
10062
10063       sequence = gdk_event_get_event_sequence (event);
10064       if (event->type == GDK_TOUCH_END && sequence)
10065         {
10066           _gdk_display_end_touch_grab (display, device, sequence);
10067         }
10068
10069       if (event->type == GDK_BUTTON_RELEASE ||
10070           _gdk_event_get_pointer_emulated (event))
10071         {
10072           button_release_grab =
10073             _gdk_display_has_device_grab (display, device, serial);
10074
10075           if (button_release_grab &&
10076               button_release_grab->implicit &&
10077               (event->button.state & GDK_ANY_BUTTON_MASK & ~(GDK_BUTTON1_MASK << (event->button.button - 1))) == 0)
10078             {
10079               button_release_grab->serial_end = serial;
10080               button_release_grab->implicit_ungrab = FALSE;
10081               _gdk_display_device_grab_update (display, device, source_device, serial);
10082             }
10083         }
10084     }
10085
10086  out:
10087   if (unlink_event)
10088     {
10089       _gdk_event_queue_remove_link (display, event_link);
10090       g_list_free_1 (event_link);
10091       gdk_event_free (event);
10092     }
10093 }
10094
10095 /**
10096  * gdk_window_create_similar_surface:
10097  * @window: window to make new surface similar to
10098  * @content: the content for the new surface
10099  * @width: width of the new surface
10100  * @height: height of the new surface
10101  *
10102  * Create a new surface that is as compatible as possible with the
10103  * given @window. For example the new surface will have the same
10104  * fallback resolution and font options as @window. Generally, the new
10105  * surface will also use the same backend as @window, unless that is
10106  * not possible for some reason. The type of the returned surface may
10107  * be examined with cairo_surface_get_type().
10108  *
10109  * Initially the surface contents are all 0 (transparent if contents
10110  * have transparency, black otherwise.)
10111  *
10112  * Returns: a pointer to the newly allocated surface. The caller
10113  * owns the surface and should call cairo_surface_destroy() when done
10114  * with it.
10115  *
10116  * This function always returns a valid pointer, but it will return a
10117  * pointer to a "nil" surface if @other is already in an error state
10118  * or any other error occurs.
10119  *
10120  * Since: 2.22
10121  **/
10122 cairo_surface_t *
10123 gdk_window_create_similar_surface (GdkWindow *     window,
10124                                    cairo_content_t content,
10125                                    int             width,
10126                                    int             height)
10127 {
10128   cairo_surface_t *window_surface, *surface;
10129
10130   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
10131   
10132   window_surface = gdk_window_ref_impl_surface (window);
10133
10134   switch (_gdk_rendering_mode)
10135   {
10136     case GDK_RENDERING_MODE_RECORDING:
10137       {
10138         cairo_rectangle_t rect = { 0, 0, width, height };
10139         surface = cairo_recording_surface_create (content, &rect);
10140       }
10141       break;
10142     case GDK_RENDERING_MODE_IMAGE:
10143       surface = cairo_image_surface_create (content == CAIRO_CONTENT_COLOR ? CAIRO_FORMAT_RGB24 :
10144                                             content == CAIRO_CONTENT_ALPHA ? CAIRO_FORMAT_A8 : CAIRO_FORMAT_ARGB32,
10145                                             width, height);
10146       break;
10147     case GDK_RENDERING_MODE_SIMILAR:
10148     default:
10149       surface = cairo_surface_create_similar (window_surface,
10150                                               content,
10151                                               width, height);
10152       break;
10153   }
10154
10155   cairo_surface_destroy (window_surface);
10156
10157   return surface;
10158 }
10159
10160 /**
10161  * gdk_window_focus:
10162  * @window: a #GdkWindow
10163  * @timestamp: timestamp of the event triggering the window focus
10164  *
10165  * Sets keyboard focus to @window. In most cases, gtk_window_present()
10166  * should be used on a #GtkWindow, rather than calling this function.
10167  *
10168  **/
10169 void
10170 gdk_window_focus (GdkWindow *window,
10171                   guint32    timestamp)
10172 {
10173   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->focus (window, timestamp);
10174 }
10175
10176 /**
10177  * gdk_window_set_type_hint:
10178  * @window: A toplevel #GdkWindow
10179  * @hint: A hint of the function this window will have
10180  *
10181  * The application can use this call to provide a hint to the window
10182  * manager about the functionality of a window. The window manager
10183  * can use this information when determining the decoration and behaviour
10184  * of the window.
10185  *
10186  * The hint must be set before the window is mapped.
10187  **/
10188 void
10189 gdk_window_set_type_hint (GdkWindow        *window,
10190                           GdkWindowTypeHint hint)
10191 {
10192   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_type_hint (window, hint);
10193 }
10194
10195 /**
10196  * gdk_window_get_type_hint:
10197  * @window: A toplevel #GdkWindow
10198  *
10199  * This function returns the type hint set for a window.
10200  *
10201  * Return value: The type hint set for @window
10202  *
10203  * Since: 2.10
10204  **/
10205 GdkWindowTypeHint
10206 gdk_window_get_type_hint (GdkWindow *window)
10207 {
10208   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_type_hint (window);
10209 }
10210
10211 /**
10212  * gdk_window_set_modal_hint:
10213  * @window: A toplevel #GdkWindow
10214  * @modal: %TRUE if the window is modal, %FALSE otherwise.
10215  *
10216  * The application can use this hint to tell the window manager
10217  * that a certain window has modal behaviour. The window manager
10218  * can use this information to handle modal windows in a special
10219  * way.
10220  *
10221  * You should only use this on windows for which you have
10222  * previously called gdk_window_set_transient_for()
10223  **/
10224 void
10225 gdk_window_set_modal_hint (GdkWindow *window,
10226                            gboolean   modal)
10227 {
10228   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_modal_hint (window, modal);
10229 }
10230
10231 /**
10232  * gdk_window_set_skip_taskbar_hint:
10233  * @window: a toplevel #GdkWindow
10234  * @skips_taskbar: %TRUE to skip the taskbar
10235  *
10236  * Toggles whether a window should appear in a task list or window
10237  * list. If a window's semantic type as specified with
10238  * gdk_window_set_type_hint() already fully describes the window, this
10239  * function should <emphasis>not</emphasis> be called in addition,
10240  * instead you should allow the window to be treated according to
10241  * standard policy for its semantic type.
10242  *
10243  * Since: 2.2
10244  **/
10245 void
10246 gdk_window_set_skip_taskbar_hint (GdkWindow *window,
10247                                   gboolean   skips_taskbar)
10248 {
10249   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_skip_taskbar_hint (window, skips_taskbar);
10250 }
10251
10252 /**
10253  * gdk_window_set_skip_pager_hint:
10254  * @window: a toplevel #GdkWindow
10255  * @skips_pager: %TRUE to skip the pager
10256  *
10257  * Toggles whether a window should appear in a pager (workspace
10258  * switcher, or other desktop utility program that displays a small
10259  * thumbnail representation of the windows on the desktop). If a
10260  * window's semantic type as specified with gdk_window_set_type_hint()
10261  * already fully describes the window, this function should
10262  * <emphasis>not</emphasis> be called in addition, instead you should
10263  * allow the window to be treated according to standard policy for
10264  * its semantic type.
10265  *
10266  * Since: 2.2
10267  **/
10268 void
10269 gdk_window_set_skip_pager_hint (GdkWindow *window,
10270                                 gboolean   skips_pager)
10271 {
10272   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_skip_pager_hint (window, skips_pager);
10273 }
10274
10275 /**
10276  * gdk_window_set_urgency_hint:
10277  * @window: a toplevel #GdkWindow
10278  * @urgent: %TRUE if the window is urgent
10279  *
10280  * Toggles whether a window needs the user's
10281  * urgent attention.
10282  *
10283  * Since: 2.8
10284  **/
10285 void
10286 gdk_window_set_urgency_hint (GdkWindow *window,
10287                              gboolean   urgent)
10288 {
10289   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_urgency_hint (window, urgent);
10290 }
10291
10292 /**
10293  * gdk_window_set_geometry_hints:
10294  * @window: a toplevel #GdkWindow
10295  * @geometry: geometry hints
10296  * @geom_mask: bitmask indicating fields of @geometry to pay attention to
10297  *
10298  * Sets the geometry hints for @window. Hints flagged in @geom_mask
10299  * are set, hints not flagged in @geom_mask are unset.
10300  * To unset all hints, use a @geom_mask of 0 and a @geometry of %NULL.
10301  *
10302  * This function provides hints to the windowing system about
10303  * acceptable sizes for a toplevel window. The purpose of
10304  * this is to constrain user resizing, but the windowing system
10305  * will typically  (but is not required to) also constrain the
10306  * current size of the window to the provided values and
10307  * constrain programatic resizing via gdk_window_resize() or
10308  * gdk_window_move_resize().
10309  *
10310  * Note that on X11, this effect has no effect on windows
10311  * of type %GDK_WINDOW_TEMP or windows where override redirect
10312  * has been turned on via gdk_window_set_override_redirect()
10313  * since these windows are not resizable by the user.
10314  *
10315  * Since you can't count on the windowing system doing the
10316  * constraints for programmatic resizes, you should generally
10317  * call gdk_window_constrain_size() yourself to determine
10318  * appropriate sizes.
10319  *
10320  **/
10321 void
10322 gdk_window_set_geometry_hints (GdkWindow         *window,
10323                                const GdkGeometry *geometry,
10324                                GdkWindowHints     geom_mask)
10325 {
10326   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_geometry_hints (window, geometry, geom_mask);
10327 }
10328
10329 /**
10330  * gdk_window_set_title:
10331  * @window: a toplevel #GdkWindow
10332  * @title: title of @window
10333  *
10334  * Sets the title of a toplevel window, to be displayed in the titlebar.
10335  * If you haven't explicitly set the icon name for the window
10336  * (using gdk_window_set_icon_name()), the icon name will be set to
10337  * @title as well. @title must be in UTF-8 encoding (as with all
10338  * user-readable strings in GDK/GTK+). @title may not be %NULL.
10339  **/
10340 void
10341 gdk_window_set_title (GdkWindow   *window,
10342                       const gchar *title)
10343 {
10344   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_title (window, title);
10345 }
10346
10347 /**
10348  * gdk_window_set_role:
10349  * @window: a toplevel #GdkWindow
10350  * @role: a string indicating its role
10351  *
10352  * When using GTK+, typically you should use gtk_window_set_role() instead
10353  * of this low-level function.
10354  *
10355  * The window manager and session manager use a window's role to
10356  * distinguish it from other kinds of window in the same application.
10357  * When an application is restarted after being saved in a previous
10358  * session, all windows with the same title and role are treated as
10359  * interchangeable.  So if you have two windows with the same title
10360  * that should be distinguished for session management purposes, you
10361  * should set the role on those windows. It doesn't matter what string
10362  * you use for the role, as long as you have a different role for each
10363  * non-interchangeable kind of window.
10364  *
10365  **/
10366 void
10367 gdk_window_set_role (GdkWindow   *window,
10368                      const gchar *role)
10369 {
10370   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_role (window, role);
10371 }
10372
10373 /**
10374  * gdk_window_set_startup_id:
10375  * @window: a toplevel #GdkWindow
10376  * @startup_id: a string with startup-notification identifier
10377  *
10378  * When using GTK+, typically you should use gtk_window_set_startup_id()
10379  * instead of this low-level function.
10380  *
10381  * Since: 2.12
10382  *
10383  **/
10384 void
10385 gdk_window_set_startup_id (GdkWindow   *window,
10386                            const gchar *startup_id)
10387 {
10388   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_startup_id (window, startup_id);
10389 }
10390
10391 /**
10392  * gdk_window_set_transient_for:
10393  * @window: a toplevel #GdkWindow
10394  * @parent: another toplevel #GdkWindow
10395  *
10396  * Indicates to the window manager that @window is a transient dialog
10397  * associated with the application window @parent. This allows the
10398  * window manager to do things like center @window on @parent and
10399  * keep @window above @parent.
10400  *
10401  * See gtk_window_set_transient_for() if you're using #GtkWindow or
10402  * #GtkDialog.
10403  **/
10404 void
10405 gdk_window_set_transient_for (GdkWindow *window,
10406                               GdkWindow *parent)
10407 {
10408   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_transient_for (window, parent);
10409 }
10410
10411 /**
10412  * gdk_window_get_root_origin:
10413  * @window: a toplevel #GdkWindow
10414  * @x: (out): return location for X position of window frame
10415  * @y: (out): return location for Y position of window frame
10416  *
10417  * Obtains the top-left corner of the window manager frame in root
10418  * window coordinates.
10419  *
10420  **/
10421 void
10422 gdk_window_get_root_origin (GdkWindow *window,
10423                             gint      *x,
10424                             gint      *y)
10425 {
10426   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_root_origin (window, x, y);
10427 }
10428
10429 /**
10430  * gdk_window_get_frame_extents:
10431  * @window: a toplevel #GdkWindow
10432  * @rect: (out): rectangle to fill with bounding box of the window frame
10433  *
10434  * Obtains the bounding box of the window, including window manager
10435  * titlebar/borders if any. The frame position is given in root window
10436  * coordinates. To get the position of the window itself (rather than
10437  * the frame) in root window coordinates, use gdk_window_get_origin().
10438  *
10439  **/
10440 void
10441 gdk_window_get_frame_extents (GdkWindow    *window,
10442                               GdkRectangle *rect)
10443 {
10444   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_frame_extents (window, rect);
10445 }
10446
10447 /**
10448  * gdk_window_set_override_redirect:
10449  * @window: a toplevel #GdkWindow
10450  * @override_redirect: %TRUE if window should be override redirect
10451  *
10452  * An override redirect window is not under the control of the window manager.
10453  * This means it won't have a titlebar, won't be minimizable, etc. - it will
10454  * be entirely under the control of the application. The window manager
10455  * can't see the override redirect window at all.
10456  *
10457  * Override redirect should only be used for short-lived temporary
10458  * windows, such as popup menus. #GtkMenu uses an override redirect
10459  * window in its implementation, for example.
10460  *
10461  **/
10462 void
10463 gdk_window_set_override_redirect (GdkWindow *window,
10464                                   gboolean override_redirect)
10465 {
10466   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_override_redirect (window, override_redirect);
10467 }
10468
10469 /**
10470  * gdk_window_set_accept_focus:
10471  * @window: a toplevel #GdkWindow
10472  * @accept_focus: %TRUE if the window should receive input focus
10473  *
10474  * Setting @accept_focus to %FALSE hints the desktop environment that the
10475  * window doesn't want to receive input focus.
10476  *
10477  * On X, it is the responsibility of the window manager to interpret this
10478  * hint. ICCCM-compliant window manager usually respect it.
10479  *
10480  * Since: 2.4
10481  **/
10482 void
10483 gdk_window_set_accept_focus (GdkWindow *window,
10484                              gboolean accept_focus)
10485 {
10486   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_accept_focus (window, accept_focus);
10487 }
10488
10489 /**
10490  * gdk_window_set_focus_on_map:
10491  * @window: a toplevel #GdkWindow
10492  * @focus_on_map: %TRUE if the window should receive input focus when mapped
10493  *
10494  * Setting @focus_on_map to %FALSE hints the desktop environment that the
10495  * window doesn't want to receive input focus when it is mapped.
10496  * focus_on_map should be turned off for windows that aren't triggered
10497  * interactively (such as popups from network activity).
10498  *
10499  * On X, it is the responsibility of the window manager to interpret
10500  * this hint. Window managers following the freedesktop.org window
10501  * manager extension specification should respect it.
10502  *
10503  * Since: 2.6
10504  **/
10505 void
10506 gdk_window_set_focus_on_map (GdkWindow *window,
10507                              gboolean focus_on_map)
10508 {
10509   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_focus_on_map (window, focus_on_map);
10510 }
10511
10512 /**
10513  * gdk_window_set_icon_list:
10514  * @window: The #GdkWindow toplevel window to set the icon of.
10515  * @pixbufs: (transfer none) (element-type GdkPixbuf):
10516  *     A list of pixbufs, of different sizes.
10517  *
10518  * Sets a list of icons for the window. One of these will be used
10519  * to represent the window when it has been iconified. The icon is
10520  * usually shown in an icon box or some sort of task bar. Which icon
10521  * size is shown depends on the window manager. The window manager
10522  * can scale the icon  but setting several size icons can give better
10523  * image quality since the window manager may only need to scale the
10524  * icon by a small amount or not at all.
10525  *
10526  **/
10527 void
10528 gdk_window_set_icon_list (GdkWindow *window,
10529                           GList     *pixbufs)
10530 {
10531   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_icon_list (window, pixbufs);
10532 }
10533
10534 /**
10535  * gdk_window_set_icon_name:
10536  * @window: a toplevel #GdkWindow
10537  * @name: (allow-none): name of window while iconified (minimized)
10538  *
10539  * Windows may have a name used while minimized, distinct from the
10540  * name they display in their titlebar. Most of the time this is a bad
10541  * idea from a user interface standpoint. But you can set such a name
10542  * with this function, if you like.
10543  *
10544  * After calling this with a non-%NULL @name, calls to gdk_window_set_title()
10545  * will not update the icon title.
10546  *
10547  * Using %NULL for @name unsets the icon title; further calls to
10548  * gdk_window_set_title() will again update the icon title as well.
10549  **/
10550 void
10551 gdk_window_set_icon_name (GdkWindow   *window,
10552                           const gchar *name)
10553 {
10554   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_icon_name (window, name);
10555 }
10556
10557 /**
10558  * gdk_window_iconify:
10559  * @window: a toplevel #GdkWindow
10560  *
10561  * Asks to iconify (minimize) @window. The window manager may choose
10562  * to ignore the request, but normally will honor it. Using
10563  * gtk_window_iconify() is preferred, if you have a #GtkWindow widget.
10564  *
10565  * This function only makes sense when @window is a toplevel window.
10566  *
10567  **/
10568 void
10569 gdk_window_iconify (GdkWindow *window)
10570 {
10571   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->iconify (window);
10572 }
10573
10574 /**
10575  * gdk_window_deiconify:
10576  * @window: a toplevel #GdkWindow
10577  *
10578  * Attempt to deiconify (unminimize) @window. On X11 the window manager may
10579  * choose to ignore the request to deiconify. When using GTK+,
10580  * use gtk_window_deiconify() instead of the #GdkWindow variant. Or better yet,
10581  * you probably want to use gtk_window_present(), which raises the window, focuses it,
10582  * unminimizes it, and puts it on the current desktop.
10583  *
10584  **/
10585 void
10586 gdk_window_deiconify (GdkWindow *window)
10587 {
10588   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->deiconify (window);
10589 }
10590
10591 /**
10592  * gdk_window_stick:
10593  * @window: a toplevel #GdkWindow
10594  *
10595  * "Pins" a window such that it's on all workspaces and does not scroll
10596  * with viewports, for window managers that have scrollable viewports.
10597  * (When using #GtkWindow, gtk_window_stick() may be more useful.)
10598  *
10599  * On the X11 platform, this function depends on window manager
10600  * support, so may have no effect with many window managers. However,
10601  * GDK will do the best it can to convince the window manager to stick
10602  * the window. For window managers that don't support this operation,
10603  * there's nothing you can do to force it to happen.
10604  *
10605  **/
10606 void
10607 gdk_window_stick (GdkWindow *window)
10608 {
10609   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->stick (window);
10610 }
10611
10612 /**
10613  * gdk_window_unstick:
10614  * @window: a toplevel #GdkWindow
10615  *
10616  * Reverse operation for gdk_window_stick(); see gdk_window_stick(),
10617  * and gtk_window_unstick().
10618  *
10619  **/
10620 void
10621 gdk_window_unstick (GdkWindow *window)
10622 {
10623   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unstick (window);
10624 }
10625
10626 /**
10627  * gdk_window_maximize:
10628  * @window: a toplevel #GdkWindow
10629  *
10630  * Maximizes the window. If the window was already maximized, then
10631  * this function does nothing.
10632  *
10633  * On X11, asks the window manager to maximize @window, if the window
10634  * manager supports this operation. Not all window managers support
10635  * this, and some deliberately ignore it or don't have a concept of
10636  * "maximized"; so you can't rely on the maximization actually
10637  * happening. But it will happen with most standard window managers,
10638  * and GDK makes a best effort to get it to happen.
10639  *
10640  * On Windows, reliably maximizes the window.
10641  *
10642  **/
10643 void
10644 gdk_window_maximize (GdkWindow *window)
10645 {
10646   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->maximize (window);
10647 }
10648
10649 /**
10650  * gdk_window_unmaximize:
10651  * @window: a toplevel #GdkWindow
10652  *
10653  * Unmaximizes the window. If the window wasn't maximized, then this
10654  * function does nothing.
10655  *
10656  * On X11, asks the window manager to unmaximize @window, if the
10657  * window manager supports this operation. Not all window managers
10658  * support this, and some deliberately ignore it or don't have a
10659  * concept of "maximized"; so you can't rely on the unmaximization
10660  * actually happening. But it will happen with most standard window
10661  * managers, and GDK makes a best effort to get it to happen.
10662  *
10663  * On Windows, reliably unmaximizes the window.
10664  *
10665  **/
10666 void
10667 gdk_window_unmaximize (GdkWindow *window)
10668 {
10669   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unmaximize (window);
10670 }
10671
10672 /**
10673  * gdk_window_fullscreen:
10674  * @window: a toplevel #GdkWindow
10675  *
10676  * Moves the window into fullscreen mode. This means the
10677  * window covers the entire screen and is above any panels
10678  * or task bars.
10679  *
10680  * If the window was already fullscreen, then this function does nothing.
10681  *
10682  * On X11, asks the window manager to put @window in a fullscreen
10683  * state, if the window manager supports this operation. Not all
10684  * window managers support this, and some deliberately ignore it or
10685  * don't have a concept of "fullscreen"; so you can't rely on the
10686  * fullscreenification actually happening. But it will happen with
10687  * most standard window managers, and GDK makes a best effort to get
10688  * it to happen.
10689  *
10690  * Since: 2.2
10691  **/
10692 void
10693 gdk_window_fullscreen (GdkWindow *window)
10694 {
10695   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->fullscreen (window);
10696 }
10697
10698 /**
10699  * gdk_window_set_fullscreen_mode:
10700  * @window: a toplevel #GdkWindow
10701  * @mode: fullscreen mode
10702  *
10703  * Specifies whether the @window should span over all monitors (in a multi-head
10704  * setup) or only the current monitor when in fullscreen mode.
10705  *
10706  * The @mode argument is from the #GdkFullscreenMode enumeration.
10707  * If #GDK_FULLSCREEN_ON_ALL_MONITORS is specified, the fullscreen @window will
10708  * span over all monitors from the #GdkScreen.
10709  *
10710  * On X11, searches through the list of monitors from the #GdkScreen the ones
10711  * which delimit the 4 edges of the entire #GdkScreen and will ask the window
10712  * manager to span the @window over these monitors.
10713  *
10714  * If the XINERAMA extension is not available or not usable, this function
10715  * has no effect.
10716  *
10717  * Not all window managers support this, so you can't rely on the fullscreen
10718  * window to span over the multiple monitors when #GDK_FULLSCREEN_ON_ALL_MONITORS
10719  * is specified.
10720  *
10721  * Since: 3.8
10722  **/
10723 void
10724 gdk_window_set_fullscreen_mode (GdkWindow        *window,
10725                                 GdkFullscreenMode mode)
10726 {
10727   GdkWindowImplClass *impl_class;
10728
10729   g_return_if_fail (GDK_IS_WINDOW (window));
10730
10731   if (window->fullscreen_mode != mode)
10732     {
10733       window->fullscreen_mode = mode;
10734
10735       impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
10736       if (impl_class->apply_fullscreen_mode != NULL)
10737         impl_class->apply_fullscreen_mode (window);
10738     }
10739 }
10740
10741 /**
10742  * gdk_window_get_fullscreen_mode:
10743  * @window: a toplevel #GdkWindow
10744  *
10745  * Obtains the #GdkFullscreenMode of the @window.
10746  *
10747  * Returns: The #GdkFullscreenMode applied to the window when fullscreen.
10748  *
10749  * Since: 3.8
10750  **/
10751 GdkFullscreenMode
10752 gdk_window_get_fullscreen_mode (GdkWindow *window)
10753 {
10754   g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_FULLSCREEN_ON_CURRENT_MONITOR);
10755
10756   return window->fullscreen_mode;
10757 }
10758
10759 /**
10760  * gdk_window_unfullscreen:
10761  * @window: a toplevel #GdkWindow
10762  *
10763  * Moves the window out of fullscreen mode. If the window was not
10764  * fullscreen, does nothing.
10765  *
10766  * On X11, asks the window manager to move @window out of the fullscreen
10767  * state, if the window manager supports this operation. Not all
10768  * window managers support this, and some deliberately ignore it or
10769  * don't have a concept of "fullscreen"; so you can't rely on the
10770  * unfullscreenification actually happening. But it will happen with
10771  * most standard window managers, and GDK makes a best effort to get
10772  * it to happen.
10773  *
10774  * Since: 2.2
10775  **/
10776 void
10777 gdk_window_unfullscreen (GdkWindow *window)
10778 {
10779   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->unfullscreen (window);
10780 }
10781
10782 /**
10783  * gdk_window_set_keep_above:
10784  * @window: a toplevel #GdkWindow
10785  * @setting: whether to keep @window above other windows
10786  *
10787  * Set if @window must be kept above other windows. If the
10788  * window was already above, then this function does nothing.
10789  *
10790  * On X11, asks the window manager to keep @window above, if the window
10791  * manager supports this operation. Not all window managers support
10792  * this, and some deliberately ignore it or don't have a concept of
10793  * "keep above"; so you can't rely on the window being kept above.
10794  * But it will happen with most standard window managers,
10795  * and GDK makes a best effort to get it to happen.
10796  *
10797  * Since: 2.4
10798  **/
10799 void
10800 gdk_window_set_keep_above (GdkWindow *window,
10801                            gboolean   setting)
10802 {
10803   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_keep_above (window, setting);
10804 }
10805
10806 /**
10807  * gdk_window_set_keep_below:
10808  * @window: a toplevel #GdkWindow
10809  * @setting: whether to keep @window below other windows
10810  *
10811  * Set if @window must be kept below other windows. If the
10812  * window was already below, then this function does nothing.
10813  *
10814  * On X11, asks the window manager to keep @window below, if the window
10815  * manager supports this operation. Not all window managers support
10816  * this, and some deliberately ignore it or don't have a concept of
10817  * "keep below"; so you can't rely on the window being kept below.
10818  * But it will happen with most standard window managers,
10819  * and GDK makes a best effort to get it to happen.
10820  *
10821  * Since: 2.4
10822  **/
10823 void
10824 gdk_window_set_keep_below (GdkWindow *window, gboolean setting)
10825 {
10826   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_keep_below (window, setting);
10827 }
10828
10829 /**
10830  * gdk_window_get_group:
10831  * @window: a toplevel #GdkWindow
10832  *
10833  * Returns the group leader window for @window. See gdk_window_set_group().
10834  *
10835  * Return value: (transfer none): the group leader window for @window
10836  *
10837  * Since: 2.4
10838  **/
10839 GdkWindow *
10840 gdk_window_get_group (GdkWindow *window)
10841 {
10842   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_group (window);
10843 }
10844
10845 /**
10846  * gdk_window_set_group:
10847  * @window: a toplevel #GdkWindow
10848  * @leader: (allow-none): group leader window, or %NULL to restore the default group leader window
10849  *
10850  * Sets the group leader window for @window. By default,
10851  * GDK sets the group leader for all toplevel windows
10852  * to a global window implicitly created by GDK. With this function
10853  * you can override this default.
10854  *
10855  * The group leader window allows the window manager to distinguish
10856  * all windows that belong to a single application. It may for example
10857  * allow users to minimize/unminimize all windows belonging to an
10858  * application at once. You should only set a non-default group window
10859  * if your application pretends to be multiple applications.
10860  **/
10861 void
10862 gdk_window_set_group (GdkWindow *window,
10863                       GdkWindow *leader)
10864 {
10865   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_group (window, leader);
10866 }
10867
10868 /**
10869  * gdk_window_set_decorations:
10870  * @window: a toplevel #GdkWindow
10871  * @decorations: decoration hint mask
10872  *
10873  * "Decorations" are the features the window manager adds to a toplevel #GdkWindow.
10874  * This function sets the traditional Motif window manager hints that tell the
10875  * window manager which decorations you would like your window to have.
10876  * Usually you should use gtk_window_set_decorated() on a #GtkWindow instead of
10877  * using the GDK function directly.
10878  *
10879  * The @decorations argument is the logical OR of the fields in
10880  * the #GdkWMDecoration enumeration. If #GDK_DECOR_ALL is included in the
10881  * mask, the other bits indicate which decorations should be turned off.
10882  * If #GDK_DECOR_ALL is not included, then the other bits indicate
10883  * which decorations should be turned on.
10884  *
10885  * Most window managers honor a decorations hint of 0 to disable all decorations,
10886  * but very few honor all possible combinations of bits.
10887  *
10888  **/
10889 void
10890 gdk_window_set_decorations (GdkWindow      *window,
10891                             GdkWMDecoration decorations)
10892 {
10893   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_decorations (window, decorations);
10894 }
10895
10896 /**
10897  * gdk_window_get_decorations:
10898  * @window: The toplevel #GdkWindow to get the decorations from
10899  * @decorations: (out): The window decorations will be written here
10900  *
10901  * Returns the decorations set on the GdkWindow with
10902  * gdk_window_set_decorations().
10903  *
10904  * Returns: %TRUE if the window has decorations set, %FALSE otherwise.
10905  **/
10906 gboolean
10907 gdk_window_get_decorations(GdkWindow       *window,
10908                            GdkWMDecoration *decorations)
10909 {
10910   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_decorations (window, decorations);
10911 }
10912
10913 /**
10914  * gdk_window_set_functions:
10915  * @window: a toplevel #GdkWindow
10916  * @functions: bitmask of operations to allow on @window
10917  *
10918  * Sets hints about the window management functions to make available
10919  * via buttons on the window frame.
10920  *
10921  * On the X backend, this function sets the traditional Motif window
10922  * manager hint for this purpose. However, few window managers do
10923  * anything reliable or interesting with this hint. Many ignore it
10924  * entirely.
10925  *
10926  * The @functions argument is the logical OR of values from the
10927  * #GdkWMFunction enumeration. If the bitmask includes #GDK_FUNC_ALL,
10928  * then the other bits indicate which functions to disable; if
10929  * it doesn't include #GDK_FUNC_ALL, it indicates which functions to
10930  * enable.
10931  *
10932  **/
10933 void
10934 gdk_window_set_functions (GdkWindow    *window,
10935                           GdkWMFunction functions)
10936 {
10937   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_functions (window, functions);
10938 }
10939
10940 /**
10941  * gdk_window_begin_resize_drag_for_device:
10942  * @window: a toplevel #GdkWindow
10943  * @edge: the edge or corner from which the drag is started
10944  * @device: the device used for the operation
10945  * @button: the button being used to drag
10946  * @root_x: root window X coordinate of mouse click that began the drag
10947  * @root_y: root window Y coordinate of mouse click that began the drag
10948  * @timestamp: timestamp of mouse click that began the drag (use gdk_event_get_time())
10949  *
10950  * Begins a window resize operation (for a toplevel window).
10951  * You might use this function to implement a "window resize grip," for
10952  * example; in fact #GtkStatusbar uses it. The function works best
10953  * with window managers that support the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended Window Manager Hints</ulink>, but has a
10954  * fallback implementation for other window managers.
10955  *
10956  * Since: 3.4
10957  */
10958 void
10959 gdk_window_begin_resize_drag_for_device (GdkWindow     *window,
10960                                          GdkWindowEdge  edge,
10961                                          GdkDevice     *device,
10962                                          gint           button,
10963                                          gint           root_x,
10964                                          gint           root_y,
10965                                          guint32        timestamp)
10966 {
10967   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->begin_resize_drag (window, edge, device, button, root_x, root_y, timestamp);
10968 }
10969
10970 /**
10971  * gdk_window_begin_resize_drag:
10972  * @window: a toplevel #GdkWindow
10973  * @edge: the edge or corner from which the drag is started
10974  * @button: the button being used to drag
10975  * @root_x: root window X coordinate of mouse click that began the drag
10976  * @root_y: root window Y coordinate of mouse click that began the drag
10977  * @timestamp: timestamp of mouse click that began the drag (use gdk_event_get_time())
10978  *
10979  * Begins a window resize operation (for a toplevel window).
10980  *
10981  * This function assumes that the drag is controlled by the
10982  * client pointer device, use gdk_window_begin_resize_drag_for_device()
10983  * to begin a drag with a different device.
10984  */
10985 void
10986 gdk_window_begin_resize_drag (GdkWindow     *window,
10987                               GdkWindowEdge  edge,
10988                               gint           button,
10989                               gint           root_x,
10990                               gint           root_y,
10991                               guint32        timestamp)
10992 {
10993   GdkDeviceManager *device_manager;
10994   GdkDevice *device;
10995
10996   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
10997   device = gdk_device_manager_get_client_pointer (device_manager);
10998   gdk_window_begin_resize_drag_for_device (window, edge,
10999                                            device, button, root_x, root_y, timestamp);
11000 }
11001
11002 /**
11003  * gdk_window_begin_move_drag_for_device:
11004  * @window: a toplevel #GdkWindow
11005  * @device: the device used for the operation
11006  * @button: the button being used to drag
11007  * @root_x: root window X coordinate of mouse click that began the drag
11008  * @root_y: root window Y coordinate of mouse click that began the drag
11009  * @timestamp: timestamp of mouse click that began the drag
11010  *
11011  * Begins a window move operation (for a toplevel window).
11012  * You might use this function to implement a "window move grip," for
11013  * example. The function works best with window managers that support
11014  * the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
11015  * Window Manager Hints</ulink>, but has a fallback implementation for
11016  * other window managers.
11017  *
11018  * Since: 3.4
11019  */
11020 void
11021 gdk_window_begin_move_drag_for_device (GdkWindow *window,
11022                                        GdkDevice *device,
11023                                        gint       button,
11024                                        gint       root_x,
11025                                        gint       root_y,
11026                                        guint32    timestamp)
11027 {
11028   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->begin_move_drag (window,
11029                                                              device, button, root_x, root_y, timestamp);
11030 }
11031
11032 /**
11033  * gdk_window_begin_move_drag:
11034  * @window: a toplevel #GdkWindow
11035  * @button: the button being used to drag
11036  * @root_x: root window X coordinate of mouse click that began the drag
11037  * @root_y: root window Y coordinate of mouse click that began the drag
11038  * @timestamp: timestamp of mouse click that began the drag
11039  *
11040  * Begins a window move operation (for a toplevel window).
11041  *
11042  * This function assumes that the drag is controlled by the
11043  * client pointer device, use gdk_window_begin_move_drag_for_device()
11044  * to begin a drag with a different device.
11045  */
11046 void
11047 gdk_window_begin_move_drag (GdkWindow *window,
11048                             gint       button,
11049                             gint       root_x,
11050                             gint       root_y,
11051                             guint32    timestamp)
11052 {
11053   GdkDeviceManager *device_manager;
11054   GdkDevice *device;
11055
11056   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
11057   device = gdk_device_manager_get_client_pointer (device_manager);
11058   gdk_window_begin_move_drag_for_device (window, device, button, root_x, root_y, timestamp);
11059 }
11060
11061 /**
11062  * gdk_window_enable_synchronized_configure:
11063  * @window: a toplevel #GdkWindow
11064  *
11065  * Indicates that the application will cooperate with the window
11066  * system in synchronizing the window repaint with the window
11067  * manager during resizing operations. After an application calls
11068  * this function, it must call gdk_window_configure_finished() every
11069  * time it has finished all processing associated with a set of
11070  * Configure events. Toplevel GTK+ windows automatically use this
11071  * protocol.
11072  *
11073  * On X, calling this function makes @window participate in the
11074  * _NET_WM_SYNC_REQUEST window manager protocol.
11075  *
11076  * Since: 2.6
11077  **/
11078 void
11079 gdk_window_enable_synchronized_configure (GdkWindow *window)
11080 {
11081   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->enable_synchronized_configure (window);
11082 }
11083
11084 /**
11085  * gdk_window_configure_finished:
11086  * @window: a toplevel #GdkWindow
11087  * 
11088  * Signal to the window system that the application has finished
11089  * handling Configure events it has received. Window Managers can
11090  * use this to better synchronize the frame repaint with the
11091  * application. GTK+ applications will automatically call this
11092  * function when appropriate.
11093  *
11094  * This function can only be called if gdk_window_enable_synchronized_configure()
11095  * was called previously.
11096  *
11097  * Since: 2.6
11098  **/
11099 void
11100 gdk_window_configure_finished (GdkWindow *window)
11101 {
11102   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->configure_finished (window);
11103 }
11104
11105 /**
11106  * gdk_window_set_opacity:
11107  * @window: a top-level #GdkWindow
11108  * @opacity: opacity
11109  *
11110  * Request the windowing system to make @window partially transparent,
11111  * with opacity 0 being fully transparent and 1 fully opaque. (Values
11112  * of the opacity parameter are clamped to the [0,1] range.) 
11113  *
11114  * On X11, this works only on X screens with a compositing manager 
11115  * running.
11116  *
11117  * For setting up per-pixel alpha, see gdk_screen_get_rgba_visual().
11118  * For making non-toplevel windows translucent, see 
11119  * gdk_window_set_composited().
11120  *
11121  * Since: 2.12
11122  */
11123 void
11124 gdk_window_set_opacity (GdkWindow *window,
11125                         gdouble    opacity)
11126 {
11127   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->set_opacity (window, opacity);
11128 }
11129
11130 /* This function is called when the XWindow is really gone.
11131  */
11132 void
11133 gdk_window_destroy_notify (GdkWindow *window)
11134 {
11135   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->destroy_notify (window);
11136 }
11137
11138 /**
11139  * gdk_window_register_dnd:
11140  * @window: a #GdkWindow.
11141  *
11142  * Registers a window as a potential drop destination.
11143  */
11144 void
11145 gdk_window_register_dnd (GdkWindow *window)
11146 {
11147   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->register_dnd (window);
11148 }
11149
11150 /**
11151  * gdk_window_get_drag_protocol:
11152  * @window: the destination window
11153  * @target: (out) (allow-none) (transfer full): location of the window
11154  *    where the drop should happen. This may be @window or a proxy window,
11155  *    or %NULL if @window does not support Drag and Drop.
11156  *
11157  * Finds out the DND protocol supported by a window.
11158  *
11159  * Returns: the supported DND protocol.
11160  *
11161  * Since: 3.0
11162  */
11163 GdkDragProtocol
11164 gdk_window_get_drag_protocol (GdkWindow  *window,
11165                               GdkWindow **target)
11166 {
11167   g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_DRAG_PROTO_NONE);
11168
11169   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->get_drag_protocol (window, target);
11170 }
11171
11172 /**
11173  * gdk_drag_begin:
11174  * @window: the source window for this drag.
11175  * @targets: (transfer none) (element-type GdkAtom): the offered targets,
11176  *     as list of #GdkAtoms
11177  *
11178  * Starts a drag and creates a new drag context for it.
11179  * This function assumes that the drag is controlled by the
11180  * client pointer device, use gdk_drag_begin_for_device() to
11181  * begin a drag with a different device.
11182  *
11183  * This function is called by the drag source.
11184  *
11185  * Return value: (transfer full): a newly created #GdkDragContext
11186  */
11187 GdkDragContext *
11188 gdk_drag_begin (GdkWindow     *window,
11189                 GList         *targets)
11190 {
11191   GdkDeviceManager *device_manager;
11192   GdkDevice *device;
11193
11194   device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
11195   device = gdk_device_manager_get_client_pointer (device_manager);
11196
11197   return gdk_drag_begin_for_device (window, device, targets);
11198 }
11199
11200 /**
11201  * gdk_drag_begin_for_device:
11202  * @window: the source window for this drag
11203  * @device: the device that controls this drag
11204  * @targets: (transfer none) (element-type GdkAtom): the offered targets,
11205  *     as list of #GdkAtoms
11206  *
11207  * Starts a drag and creates a new drag context for it.
11208  *
11209  * This function is called by the drag source.
11210  *
11211  * Return value: (transfer full): a newly created #GdkDragContext
11212  */
11213 GdkDragContext *
11214 gdk_drag_begin_for_device (GdkWindow     *window,
11215                            GdkDevice     *device,
11216                            GList         *targets)
11217 {
11218   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)->drag_begin (window, device, targets);
11219 }
11220
11221 /**
11222  * gdk_test_render_sync:
11223  * @window: a mapped #GdkWindow
11224  *
11225  * Retrieves a pixel from @window to force the windowing
11226  * system to carry out any pending rendering commands.
11227  *
11228  * This function is intended to be used to synchronize with rendering
11229  * pipelines, to benchmark windowing system rendering operations.
11230  *
11231  * Since: 2.14
11232  **/
11233 void
11234 gdk_test_render_sync (GdkWindow *window)
11235 {
11236   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->sync_rendering (window);
11237 }
11238
11239 /**
11240  * gdk_test_simulate_key:
11241  * @window: a #GdkWindow to simulate a key event for
11242  * @x:      x coordinate within @window for the key event
11243  * @y:      y coordinate within @window for the key event
11244  * @keyval: A GDK keyboard value
11245  * @modifiers: Keyboard modifiers the event is setup with
11246  * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
11247  *
11248  * This function is intended to be used in GTK+ test programs.
11249  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
11250  * the given (@x,@y) coordinates within @window and simulate a
11251  * key press or release event.
11252  *
11253  * When the mouse pointer is warped to the target location, use
11254  * of this function outside of test programs that run in their
11255  * own virtual windowing system (e.g. Xvfb) is not recommended.
11256  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
11257  * be warped and @window origin will be used as mouse pointer
11258  * location for the event.
11259  *
11260  * Also, gdk_test_simulate_key() is a fairly low level function,
11261  * for most testing purposes, gtk_test_widget_send_key() is the
11262  * right function to call which will generate a key press event
11263  * followed by its accompanying key release event.
11264  *
11265  * Returns: whether all actions necessary for a key event simulation
11266  *     were carried out successfully
11267  *
11268  * Since: 2.14
11269  */
11270 gboolean
11271 gdk_test_simulate_key (GdkWindow      *window,
11272                        gint            x,
11273                        gint            y,
11274                        guint           keyval,
11275                        GdkModifierType modifiers,
11276                        GdkEventType    key_pressrelease)
11277 {
11278   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
11279     ->simulate_key (window, x, y, keyval, modifiers, key_pressrelease);
11280 }
11281
11282 /**
11283  * gdk_test_simulate_button:
11284  * @window: a #GdkWindow to simulate a button event for
11285  * @x:      x coordinate within @window for the button event
11286  * @y:      y coordinate within @window for the button event
11287  * @button: Number of the pointer button for the event, usually 1, 2 or 3
11288  * @modifiers: Keyboard modifiers the event is setup with
11289  * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
11290  *
11291  * This function is intended to be used in GTK+ test programs.
11292  * It will warp the mouse pointer to the given (@x,@y) coordinates
11293  * within @window and simulate a button press or release event.
11294  * Because the mouse pointer needs to be warped to the target
11295  * location, use of this function outside of test programs that
11296  * run in their own virtual windowing system (e.g. Xvfb) is not
11297  * recommended.
11298  *
11299 * Also, gdk_test_simulate_button() is a fairly low level function,
11300  * for most testing purposes, gtk_test_widget_click() is the right
11301  * function to call which will generate a button press event followed
11302  * by its accompanying button release event.
11303  *
11304  * Returns: whether all actions necessary for a button event simulation
11305  *     were carried out successfully
11306  *
11307  * Since: 2.14
11308  */
11309 gboolean
11310 gdk_test_simulate_button (GdkWindow      *window,
11311                           gint            x,
11312                           gint            y,
11313                           guint           button, /*1..3*/
11314                           GdkModifierType modifiers,
11315                           GdkEventType    button_pressrelease)
11316 {
11317   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
11318     ->simulate_button (window, x, y, button, modifiers, button_pressrelease);
11319 }
11320
11321 /**
11322  * gdk_property_get:
11323  * @window: a #GdkWindow
11324  * @property: the property to retrieve
11325  * @type: the desired property type, or %GDK_NONE, if any type of data
11326  *   is acceptable. If this does not match the actual
11327  *   type, then @actual_format and @actual_length will
11328  *   be filled in, a warning will be printed to stderr
11329  *   and no data will be returned.
11330  * @offset: the offset into the property at which to begin
11331  *   retrieving data, in 4 byte units.
11332  * @length: the length of the data to retrieve in bytes.  Data is
11333  *   considered to be retrieved in 4 byte chunks, so @length
11334  *   will be rounded up to the next highest 4 byte boundary
11335  *   (so be careful not to pass a value that might overflow
11336  *   when rounded up).
11337  * @pdelete: if %TRUE, delete the property after retrieving the
11338  *   data.
11339  * @actual_property_type: (out) (transfer none): location to store the
11340  *   actual type of the property.
11341  * @actual_format: (out): location to store the actual return format of the
11342  *   data; either 8, 16 or 32 bits.
11343  * @actual_length: location to store the length of the retrieved data, in
11344  *   bytes.  Data returned in the 32 bit format is stored
11345  *   in a long variable, so the actual number of 32 bit
11346  *   elements should be be calculated via
11347  *   @actual_length / sizeof(glong) to ensure portability to
11348  *   64 bit systems.
11349  * @data: (out) (array length=actual_length) (transfer full): location
11350  *   to store a pointer to the data. The retrieved data should be
11351  *   freed with g_free() when you are finished using it.
11352  *
11353  * Retrieves a portion of the contents of a property. If the
11354  * property does not exist, then the function returns %FALSE,
11355  * and %GDK_NONE will be stored in @actual_property_type.
11356  *
11357  * <note>
11358  * <para>
11359  * The XGetWindowProperty() function that gdk_property_get()
11360  * uses has a very confusing and complicated set of semantics.
11361  * Unfortunately, gdk_property_get() makes the situation
11362  * worse instead of better (the semantics should be considered
11363  * undefined), and also prints warnings to stderr in cases where it
11364  * should return a useful error to the program. You are advised to use
11365  * XGetWindowProperty() directly until a replacement function for
11366  * gdk_property_get()
11367  * is provided.
11368  * </para>
11369  * </note>
11370  *
11371  * Returns: %TRUE if data was successfully received and stored
11372  *   in @data, otherwise %FALSE.
11373  */
11374 gboolean
11375 gdk_property_get (GdkWindow  *window,
11376                   GdkAtom     property,
11377                   GdkAtom     type,
11378                   gulong      offset,
11379                   gulong      length,
11380                   gint        pdelete,
11381                   GdkAtom    *actual_property_type,
11382                   gint       *actual_format_type,
11383                   gint       *actual_length,
11384                   guchar    **data)
11385 {
11386   return GDK_WINDOW_IMPL_GET_CLASS (window->impl)
11387     ->get_property (window, property, type, offset, length, pdelete,
11388                     actual_property_type, actual_format_type,
11389                     actual_length, data);
11390 }
11391
11392 /**
11393  * gdk_property_change: (skip)
11394  * @window: a #GdkWindow
11395  * @property: the property to change
11396  * @type: the new type for the property. If @mode is
11397  *   %GDK_PROP_MODE_PREPEND or %GDK_PROP_MODE_APPEND, then this
11398  *   must match the existing type or an error will occur.
11399  * @format: the new format for the property. If @mode is
11400  *   %GDK_PROP_MODE_PREPEND or %GDK_PROP_MODE_APPEND, then this
11401  *   must match the existing format or an error will occur.
11402  * @mode: a value describing how the new data is to be combined
11403  *   with the current data.
11404  * @data: the data (a <literal>guchar *</literal>
11405  *   <literal>gushort *</literal>, or <literal>gulong *</literal>,
11406  *   depending on @format), cast to a <literal>guchar *</literal>.
11407  * @nelements: the number of elements of size determined by the format,
11408  *   contained in @data.
11409  *
11410  * Changes the contents of a property on a window.
11411  */
11412 void
11413 gdk_property_change (GdkWindow    *window,
11414                      GdkAtom       property,
11415                      GdkAtom       type,
11416                      gint          format,
11417                      GdkPropMode   mode,
11418                      const guchar *data,
11419                      gint          nelements)
11420 {
11421   GDK_WINDOW_IMPL_GET_CLASS (window->impl)
11422     ->change_property (window, property, type, format, mode, data, nelements);
11423 }
11424
11425 /**
11426  * gdk_property_delete:
11427  * @window: a #GdkWindow
11428  * @property: the property to delete
11429  *
11430  * Deletes a property from a window.
11431  */
11432 void
11433 gdk_property_delete (GdkWindow *window,
11434                      GdkAtom    property)
11435 {
11436   GDK_WINDOW_IMPL_GET_CLASS (window->impl)->delete_property (window, property);
11437 }