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