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