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