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