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