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