gdkwindow.c 288 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
2 3
 * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
 * Josh MacDonald, Ryan Lortie
Elliot Lee's avatar
Elliot Lee committed
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
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
19
 */
20

21
/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
25
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 27
 */

28 29 30 31 32 33 34 35 36 37 38 39
#include "config.h"

#include "gdkwindow.h"

#ifdef GDK_WINDOWING_X11
#include "x11/gdkx.h"           /* For workaround */
#endif
#include "math.h"

#include "gdk.h"                /* For gdk_rectangle_union() */
#include "gdkinternals.h"
#include "gdkintl.h"
40 41
#include "gdkscreen.h"
#include "gdkdeviceprivate.h"
42 43 44 45 46 47 48 49
#include "gdkdrawable.h"
#include "gdkmarshalers.h"
#include "gdkscreen.h"
#include "gdkwindowimpl.h"

#undef DEBUG_WINDOW_PRINTING


50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/**
 * SECTION:windows
 * @Short_description: Onscreen display areas in the target window system
 * @Title: Windows
 *
 * A #GdkWindow is a rectangular region on the screen. It's a low-level object,
 * used to implement high-level objects such as #GtkWidget and #GtkWindow on the
 * GTK+ level. A #GtkWindow is a toplevel window, the thing a user might think
 * of as a "window" with a titlebar and so on; a #GtkWindow may contain many
 * #GdkWindow<!-- -->s. For example, each #GtkButton has a #GdkWindow associated
 * with it.
 *
 * <refsect2 id="COMPOSITED-WINDOWS">
 * <title>Composited Windows</title>
 * <para>
 * Normally, the windowing system takes care of rendering the contents of a
 * child window onto its parent window. This mechanism can be intercepted by
 * calling gdk_window_set_composited() on the child window. For a
 * <firstterm>composited</firstterm> window it is the responsibility of the
 * application to render the window contents at the right spot.
 * </para>
 * <example id="composited-window-example">
 * <title>Composited windows</title>
 * <programlisting>
 * <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>
 * </programlisting></example>
 * <para>
 * In the example <xref linkend="composited-window-example"/>, a button is
 * placed inside of an event box inside of a window. The event box is set as
 * composited and therefore is no longer automatically drawn to the screen.
 *
 * When the contents of the event box change, an expose event is generated on
 * it's parent window (which, in this case, belongs to the toplevel #GtkWindow).
 * The expose handler for this widget is responsible for merging the changes
 * back on the screen in the way that it wishes.
 *
 * In our case, we merge the contents with a 50% transparency. We also set the
 * background colour of the window to red. The effect is that the background
 * shows through the button.
 * </para>
 * </refsect2>
 * <refsect2 id="OFFSCREEN-WINDOWS">
 * <title>Offscreen Windows</title>
 * <para>
 * Offscreen windows are more general than composited windows, since they allow
 * not only to modify the rendering of the child window onto its parent, but
 * also to apply coordinate transformations.
 *
 * To integrate an offscreen window into a window hierarchy, one has to call
 * gdk_offscreen_window_set_embedder() and handle a number of signals. The
 * #GdkWindow::pick-embedded-child signal on the embedder window is used to
 * select an offscreen child at given coordinates, and the
 * #GdkWindow::to-embedder and #GdkWindow::from-embedder signals on the
 * offscreen window are used to translate coordinates between the embedder and
 * the offscreen window.
 *
 * For rendering an offscreen window onto its embedder, the contents of the
107 108
 * offscreen window are available as a surface, via
 * gdk_offscreen_window_get_surface().
109 110 111 112
 * </para>
 * </refsect2>
 */

113

114 115 116 117 118
/* Historically a GdkWindow always matches a platform native window,
 * be it a toplevel window or a child window. In this setup the
 * GdkWindow (and other GdkDrawables) were platform independent classes,
 * and the actual platform specific implementation was in a delegate
 * object availible as "impl" in the window object.
119
 *
120 121 122 123 124 125 126 127 128 129 130 131 132 133
 * With the addition of client side windows and offscreen windows this
 * changes a bit. The application-visible GdkWindow object behaves as
 * it did before, but not all such windows now have a corresponding native
 * window. Instead windows that are "client side" are emulated by the gdk
 * code such that clipping, drawing, moving, events etc work as expected.
 *
 * For GdkWindows that have a native window the "impl" object is the
 * same as before. However, for all client side windows the impl object
 * is shared with its parent (i.e. all client windows descendants of one
 * native window has the same impl.
 *
 * Additionally there is a new type of platform independent impl object,
 * GdkOffscreenWindow. All windows of type GDK_WINDOW_OFFSCREEN get an impl
 * of this type (while their children are generally GDK_WINDOW_CHILD virtual
134 135
 * windows). Such windows work by allocating a #cairo_surface_t as the backing
 * store for drawing operations, which is resized with the window.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 *
 * GdkWindows have a pointer to the "impl window" they are in, i.e.
 * the topmost GdkWindow which have the same "impl" value. This is stored
 * in impl_window, which is different from the window itself only for client
 * side windows.
 * All GdkWindows (native or not) track the position of the window in the parent
 * (x, y), the size of the window (width, height), the position of the window
 * with respect to the impl window (abs_x, abs_y). We also track the clip
 * region of the window wrt parent windows and siblings, in window-relative
 * coordinates with and without child windows included (clip_region,
 * clip_region_with_children).
 *
 * All toplevel windows are native windows, but also child windows can be
 * native (although not children of offscreens). We always listen to
 * a basic set of events (see get_native_event_mask) for these windows
 * so that we can emulate events for any client side children.
 *
 * For native windows we apply the calculated clip region as a window shape
 * so that eg. client side siblings that overlap the native child properly
 * draws over the native child window.
 *
 * In order to minimize flicker and for performance we use a couple of cacheing
 * tricks. First of all, every time we do a window to window copy area, for instance
 * when moving a client side window or when scrolling/moving a region in a window
 * we store this in outstanding_moves instead of applying immediately. We then
 * delay this move until we really need it (because something depends on being
 * able to read it), or until we're handing a redraw from an expose/invalidation
163
 * (actually we delay it past redraw, but before blitting the double buffer
164 165 166 167 168 169 170 171 172 173
 * to the window). This gives us two advantages. First of all it minimizes the time
 * from the window is moved to the exposes related to that move, secondly it allows
 * us to be smart about how to do the copy. We combine multiple moves into one (when
 * possible) and we don't actually do copies to anything that is or will be
 * invalidated and exposed anyway.
 *
 * Secondly, we use something called a "implicit paint" during repaint handling.
 * An implicit paint is similar to a regular paint for the paint stack, but it is
 * not put on the stack. Instead, it is set on the impl window, and later when
 * regular gdk_window_begin_paint_region()  happen on a window of this impl window
174
 * we reuse the surface from the implicit paint. During repaint we create and at the
175
 * end flush an implicit paint, which means we can collect all the paints on
176
 * multiple client side windows in the same backing store.
177
 */
178

179
#define USE_BACKING_STORE	/* Appears to work on Win32, too, now. */
180

181 182 183
/* This adds a local value to the GdkVisibilityState enum */
#define GDK_VISIBILITY_NOT_VIEWABLE 3

184
enum {
Alexander Larsson's avatar
Alexander Larsson committed
185 186 187
  PICK_EMBEDDED_CHILD, /* only called if children are embedded */
  TO_EMBEDDER,
  FROM_EMBEDDER,
188 189 190
  LAST_SIGNAL
};

Cody Russell's avatar
Cody Russell committed
191 192 193 194 195
enum {
  PROP_0,
  PROP_CURSOR
};

196 197 198 199 200 201
typedef enum {
  CLEAR_BG_NONE,
  CLEAR_BG_WINCLEARED, /* Clear backgrounds except those that the window system clears */
  CLEAR_BG_ALL
} ClearBg;

202 203
struct _GdkWindowPaint
{
204
  cairo_region_t *region;
205
  cairo_surface_t *surface;
206
  guint uses_implicit : 1;
207
  guint flushed : 1;
208
  guint32 region_tag;
209
};
210

211
typedef struct {
212
  cairo_region_t *dest_region; /* The destination region */
213
  int dx, dy; /* The amount that the source was moved to reach dest_region */
214 215
} GdkWindowRegionMove;

216
/* Global info */
217

218
static cairo_surface_t *gdk_window_ref_cairo_surface (GdkDrawable *drawable);
219 220 221
static cairo_surface_t *gdk_window_create_cairo_surface (GdkDrawable *drawable,
							 int width,
							 int height);
222
static void             gdk_window_drop_cairo_surface (GdkWindowObject *private);
223 224
static void             gdk_window_set_cairo_clip    (GdkDrawable *drawable,
						      cairo_t *cr);
225

226
static void   gdk_window_real_get_size  (GdkDrawable     *drawable,
227 228
					 gint            *width,
					 gint            *height);
229 230

static GdkColormap* gdk_window_real_get_colormap (GdkDrawable *drawable);
Havoc Pennington's avatar
Havoc Pennington committed
231

232 233
static cairo_region_t*   gdk_window_get_clip_region        (GdkDrawable *drawable);
static cairo_region_t*   gdk_window_get_visible_region     (GdkDrawable *drawable);
Havoc Pennington's avatar
Havoc Pennington committed
234

Owen Taylor's avatar
Owen Taylor committed
235
static void gdk_window_free_paint_stack (GdkWindow *window);
236

237 238
static void gdk_window_init       (GdkWindowObject      *window);
static void gdk_window_class_init (GdkWindowObjectClass *klass);
239
static void gdk_window_finalize   (GObject              *object);
Cody Russell's avatar
Cody Russell committed
240 241 242 243 244 245 246 247 248 249

static void gdk_window_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec);
static void gdk_window_get_property (GObject      *object,
                                     guint         prop_id,
                                     GValue       *value,
                                     GParamSpec   *pspec);

250
static void gdk_window_clear_backing_region (GdkWindow *window,
251
					     cairo_region_t *region);
252

253 254 255
static void recompute_visible_regions   (GdkWindowObject *private,
					 gboolean recalculate_siblings,
					 gboolean recalculate_children);
256
static void gdk_window_flush_outstanding_moves (GdkWindow *window);
257
static void gdk_window_flush_recursive  (GdkWindowObject *window);
258
static void do_move_region_bits_on_impl (GdkWindowObject *private,
259
					 cairo_region_t *region, /* In impl window coords */
260
					 int dx, int dy);
261
static void gdk_window_invalidate_in_parent (GdkWindowObject *private);
262
static void move_native_children        (GdkWindowObject *private);
263 264
static void update_cursor               (GdkDisplay *display,
                                         GdkDevice  *device);
265
static void impl_window_add_update_area (GdkWindowObject *impl_window,
266
					 cairo_region_t *region);
267
static void gdk_window_region_move_free (GdkWindowRegionMove *move);
268
static void gdk_window_invalidate_region_full (GdkWindow       *window,
269
					       const cairo_region_t *region,
270 271 272 273 274 275
					       gboolean         invalidate_children,
					       ClearBg          clear_bg);
static void gdk_window_invalidate_rect_full (GdkWindow          *window,
					     const GdkRectangle *rect,
					     gboolean            invalidate_children,
					     ClearBg             clear_bg);
276

277 278
static guint signals[LAST_SIGNAL] = { 0 };

279 280
static gpointer parent_class = NULL;

281 282
static const cairo_user_data_key_t gdk_window_cairo_key;

283 284 285 286 287 288 289 290
static guint32
new_region_tag (void)
{
  static guint32 tag = 0;

  return ++tag;
}

291 292 293 294 295 296 297 298 299 300 301 302 303
GType
gdk_window_object_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    object_type = g_type_register_static_simple (GDK_TYPE_DRAWABLE,
						 "GdkWindow",
						 sizeof (GdkWindowObjectClass),
						 (GClassInitFunc) gdk_window_class_init,
						 sizeof (GdkWindowObject),
						 (GInstanceInitFunc) gdk_window_init,
						 0);
304

305 306
  return object_type;
}
307

308 309 310 311 312 313 314
GType
_gdk_paintable_get_type (void)
{
  static GType paintable_type = 0;

  if (!paintable_type)
    {
315
      const GTypeInfo paintable_info =
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
      {
	sizeof (GdkPaintableIface),  /* class_size */
	NULL,                        /* base_init */
	NULL,                        /* base_finalize */
      };

      paintable_type = g_type_register_static (G_TYPE_INTERFACE,
					       g_intern_static_string ("GdkPaintable"),
					       &paintable_info, 0);

      g_type_interface_add_prerequisite (paintable_type, G_TYPE_OBJECT);
    }

  return paintable_type;
}

332
static void
333
gdk_window_init (GdkWindowObject *window)
334 335
{
  /* 0-initialization is good for all other fields. */
rhlabs's avatar
rhlabs committed
336

337
  window->window_type = GDK_WINDOW_CHILD;
Elliot Lee's avatar
Elliot Lee committed
338

Havoc Pennington's avatar
Havoc Pennington committed
339
  window->state = GDK_WINDOW_STATE_WITHDRAWN;
340 341 342
  window->width = 1;
  window->height = 1;
  window->toplevel_window_type = -1;
343 344 345 346 347
  /* starts hidden */
  window->effective_visibility = GDK_VISIBILITY_NOT_VIEWABLE;
  window->visibility = GDK_VISIBILITY_FULLY_OBSCURED;
  /* Default to unobscured since some backends don't send visibility events */
  window->native_visibility = GDK_VISIBILITY_UNOBSCURED;
348
}
349

350
/* Stop and return on the first non-NULL parent */
351
static gboolean
352 353 354 355
accumulate_get_window (GSignalInvocationHint *ihint,
		       GValue		       *return_accu,
		       const GValue	       *handler_return,
		       gpointer               data)
356 357 358
{
  g_value_copy (handler_return, return_accu);
  /* Continue while returning NULL */
359
  return g_value_get_object (handler_return) == NULL;
360 361
}

362 363
static GQuark quark_pointer_window = 0;

364
static void
365
gdk_window_class_init (GdkWindowObjectClass *klass)
366 367 368
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
369

370 371
  parent_class = g_type_class_peek_parent (klass);

372
  object_class->finalize = gdk_window_finalize;
Cody Russell's avatar
Cody Russell committed
373 374
  object_class->set_property = gdk_window_set_property;
  object_class->get_property = gdk_window_get_property;
375 376 377

  drawable_class->get_size = gdk_window_real_get_size;
  drawable_class->get_colormap = gdk_window_real_get_colormap;
378
  drawable_class->ref_cairo_surface = gdk_window_ref_cairo_surface;
379
  drawable_class->create_cairo_surface = gdk_window_create_cairo_surface;
380
  drawable_class->set_cairo_clip = gdk_window_set_cairo_clip;
381 382
  drawable_class->get_clip_region = gdk_window_get_clip_region;
  drawable_class->get_visible_region = gdk_window_get_visible_region;
383 384

  quark_pointer_window = g_quark_from_static_string ("gtk-pointer-window");
385 386


Cody Russell's avatar
Cody Russell committed
387
  /* Properties */
388 389 390 391 392 393 394 395 396

  /**
   * GdkWindow:cursor:
   *
   * The mouse pointer for a #GdkWindow. See gdk_window_set_cursor() and
   * gdk_window_get_cursor() for details.
   *
   * Since: 2.18
   */
Cody Russell's avatar
Cody Russell committed
397 398
  g_object_class_install_property (object_class,
                                   PROP_CURSOR,
399 400 401 402 403
                                   g_param_spec_boxed ("cursor",
                                                       P_("Cursor"),
                                                       P_("Cursor"),
                                                       GDK_TYPE_CURSOR,
                                                       G_PARAM_READWRITE));
Cody Russell's avatar
Cody Russell committed
404

Matthias Clasen's avatar
Matthias Clasen committed
405 406 407 408 409 410 411 412 413
  /**
   * GdkWindow::pick-embedded-child:
   * @window: the window on which the signal is emitted
   * @x: x coordinate in the window
   * @y: y coordinate in the window
   *
   * The ::pick-embedded-child signal is emitted to find an embedded
   * child at the given position.
   *
Matthias Clasen's avatar
Matthias Clasen committed
414
   * Returns: the #GdkWindow of the embedded child at @x, @y, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
415 416 417
   *
   * Since: 2.18
   */
Alexander Larsson's avatar
Alexander Larsson committed
418 419
  signals[PICK_EMBEDDED_CHILD] =
    g_signal_new (g_intern_static_string ("pick-embedded-child"),
420 421 422
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0,
423
		  accumulate_get_window, NULL,
Matthias Clasen's avatar
Matthias Clasen committed
424
		  _gdk_marshal_OBJECT__DOUBLE_DOUBLE,
425 426 427 428
		  GDK_TYPE_WINDOW,
		  2,
		  G_TYPE_DOUBLE,
		  G_TYPE_DOUBLE);
Matthias Clasen's avatar
Matthias Clasen committed
429 430 431 432 433 434 435 436 437 438 439 440

  /**
   * GdkWindow::to-embedder:
   * @window: the offscreen window on which the signal is emitted
   * @offscreen-x: x coordinate in the offscreen window
   * @offscreen-y: y coordinate in the offscreen window
   * @embedder-x: return location for the x coordinate in the embedder window
   * @embedder-y: return location for the y coordinate in the embedder window
   *
   * The ::to-embedder signal is emitted to translate coordinates
   * in an offscreen window to its embedder.
   *
Matthias Clasen's avatar
Matthias Clasen committed
441 442
   * See also #GtkWindow::from-embedder.
   *
Matthias Clasen's avatar
Matthias Clasen committed
443 444
   * Since: 2.18
   */
Alexander Larsson's avatar
Alexander Larsson committed
445 446
  signals[TO_EMBEDDER] =
    g_signal_new (g_intern_static_string ("to-embedder"),
447 448 449 450
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0,
		  NULL, NULL,
Matthias Clasen's avatar
Matthias Clasen committed
451
		  _gdk_marshal_VOID__DOUBLE_DOUBLE_POINTER_POINTER,
452 453 454 455 456 457
		  G_TYPE_NONE,
		  4,
		  G_TYPE_DOUBLE,
		  G_TYPE_DOUBLE,
		  G_TYPE_POINTER,
		  G_TYPE_POINTER);
Matthias Clasen's avatar
Matthias Clasen committed
458 459 460 461 462 463 464 465 466 467 468 469

  /**
   * GdkWindow::from-embedder:
   * @window: the offscreen window on which the signal is emitted
   * @embedder-x: x coordinate in the embedder window
   * @embedder-y: y coordinate in the embedder window
   * @offscreen-x: return location for the x coordinate in the offscreen window
   * @offscreen-y: return location for the y coordinate in the offscreen window
   *
   * The ::from-embedder signal is emitted to translate coordinates
   * in the embedder of an offscreen window to the offscreen window.
   *
Matthias Clasen's avatar
Matthias Clasen committed
470 471
   * See also #GtkWindow::to-embedder.
   *
Matthias Clasen's avatar
Matthias Clasen committed
472 473
   * Since: 2.18
   */
Alexander Larsson's avatar
Alexander Larsson committed
474 475
  signals[FROM_EMBEDDER] =
    g_signal_new (g_intern_static_string ("from-embedder"),
476 477 478 479
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0,
		  NULL, NULL,
Matthias Clasen's avatar
Matthias Clasen committed
480
		  _gdk_marshal_VOID__DOUBLE_DOUBLE_POINTER_POINTER,
481 482 483 484 485 486
		  G_TYPE_NONE,
		  4,
		  G_TYPE_DOUBLE,
		  G_TYPE_DOUBLE,
		  G_TYPE_POINTER,
		  G_TYPE_POINTER);
487
}
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
static void
device_removed_cb (GdkDeviceManager *device_manager,
                   GdkDevice        *device,
                   GdkWindow        *window)
{
  GdkWindowObject *private;

  private = (GdkWindowObject *) window;

  private->devices_inside = g_list_remove (private->devices_inside, device);
  g_hash_table_remove (private->device_cursor, device);

  if (private->device_events)
    g_hash_table_remove (private->device_events, device);
}

505 506 507 508 509
static void
gdk_window_finalize (GObject *object)
{
  GdkWindow *window = GDK_WINDOW (object);
  GdkWindowObject *obj = (GdkWindowObject *) object;
510 511
  GdkDeviceManager *device_manager;

512
  device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
513
  g_signal_handlers_disconnect_by_func (device_manager, device_removed_cb, window);
514

515 516 517 518 519 520 521 522 523 524 525 526 527
  if (!GDK_WINDOW_DESTROYED (window))
    {
      if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
	{
	  g_warning ("losing last reference to undestroyed window\n");
	  _gdk_window_destroy (window, FALSE);
	}
      else
	/* We use TRUE here, to keep us from actually calling
	 * XDestroyWindow() on the window
	 */
	_gdk_window_destroy (window, TRUE);
    }
528

529 530 531 532 533 534
  if (obj->impl)
    {
      g_object_unref (obj->impl);
      obj->impl = NULL;
    }

535 536 537 538 539
  if (obj->impl_window != obj)
    {
      g_object_unref (obj->impl_window);
      obj->impl_window = NULL;
    }
540

Alexander Larsson's avatar
Alexander Larsson committed
541
  if (obj->shape)
Benjamin Otte's avatar
Benjamin Otte committed
542
    cairo_region_destroy (obj->shape);
Alexander Larsson's avatar
Alexander Larsson committed
543 544

  if (obj->input_shape)
Benjamin Otte's avatar
Benjamin Otte committed
545
    cairo_region_destroy (obj->input_shape);
Alexander Larsson's avatar
Alexander Larsson committed
546

547 548 549
  if (obj->cursor)
    gdk_cursor_unref (obj->cursor);

550 551 552 553 554 555 556 557 558
  if (obj->device_cursor)
    g_hash_table_destroy (obj->device_cursor);

  if (obj->device_events)
    g_hash_table_destroy (obj->device_events);

  if (obj->devices_inside)
    g_list_free (obj->devices_inside);

559
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
560 561
}

Cody Russell's avatar
Cody Russell committed
562 563 564 565 566 567 568 569 570 571 572
static void
gdk_window_set_property (GObject      *object,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GdkWindow *window = (GdkWindow *)object;

  switch (prop_id)
    {
    case PROP_CURSOR:
573
      gdk_window_set_cursor (window, g_value_get_boxed (value));
Cody Russell's avatar
Cody Russell committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_window_get_property (GObject    *object,
                         guint       prop_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GdkWindow *window = (GdkWindow *) object;

  switch (prop_id)
    {
    case PROP_CURSOR:
593
      g_value_set_boxed (value, gdk_window_get_cursor (window));
Cody Russell's avatar
Cody Russell committed
594 595 596 597 598 599 600 601
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

602 603 604
static gboolean
gdk_window_is_offscreen (GdkWindowObject *window)
{
605
  return window->window_type == GDK_WINDOW_OFFSCREEN;
606 607 608 609 610
}

static GdkWindowObject *
gdk_window_get_impl_window (GdkWindowObject *window)
{
611
  return window->impl_window;
612 613 614 615 616 617 618 619 620 621 622
}

GdkWindow *
_gdk_window_get_impl_window (GdkWindow *window)
{
  return (GdkWindow *)gdk_window_get_impl_window ((GdkWindowObject *)window);
}

static gboolean
gdk_window_has_impl (GdkWindowObject *window)
{
623
  return window->impl_window == window;
624 625
}

626 627 628 629 630 631 632 633
static gboolean
gdk_window_is_toplevel (GdkWindowObject *window)
{
  return
    window->parent == NULL ||
    window->parent->window_type == GDK_WINDOW_ROOT;
}

634 635 636 637 638 639 640 641 642
gboolean
_gdk_window_has_impl (GdkWindow *window)
{
  return gdk_window_has_impl ((GdkWindowObject *)window);
}

static gboolean
gdk_window_has_no_impl (GdkWindowObject *window)
{
643
  return window->impl_window != window;
644 645 646 647 648
}

static void
remove_child_area (GdkWindowObject *private,
		   GdkWindowObject *until,
Alexander Larsson's avatar
Alexander Larsson committed
649
		   gboolean for_input,
650
		   cairo_region_t *region)
651 652
{
  GdkWindowObject *child;
653
  cairo_region_t *child_region;
654 655
  GdkRectangle r;
  GList *l;
656
  cairo_region_t *shape;
657

658 659 660 661 662 663
  for (l = private->children; l; l = l->next)
    {
      child = l->data;

      if (child == until)
	break;
664

665 666
      /* If region is empty already, no need to do
	 anything potentially costly */
Benjamin Otte's avatar
Benjamin Otte committed
667
      if (cairo_region_is_empty (region))
668 669
	break;

670
      if (!GDK_WINDOW_IS_MAPPED (child) || child->input_only || child->composited)
671 672 673 674 675 676 677 678 679 680 681
	continue;

      /* Ignore offscreen children, as they don't draw in their parent and
       * don't take part in the clipping */
      if (gdk_window_is_offscreen (child))
	continue;

      r.x = child->x;
      r.y = child->y;
      r.width = child->width;
      r.height = child->height;
Alexander Larsson's avatar
Alexander Larsson committed
682

683
      /* Bail early if child totally outside region */
Benjamin Otte's avatar
Benjamin Otte committed
684
      if (cairo_region_contains_rectangle (region, &r) == CAIRO_REGION_OVERLAP_OUT)
685 686
	continue;

Benjamin Otte's avatar
Benjamin Otte committed
687
      child_region = cairo_region_create_rectangle (&r);
688

Alexander Larsson's avatar
Alexander Larsson committed
689
      if (child->shape)
690 691
	{
	  /* Adjust shape region to parent window coords */
Benjamin Otte's avatar
Benjamin Otte committed
692 693 694
	  cairo_region_translate (child->shape, child->x, child->y);
	  cairo_region_intersect (child_region, child->shape);
	  cairo_region_translate (child->shape, -child->x, -child->y);
695
	}
Alexander Larsson's avatar
Alexander Larsson committed
696 697 698 699 700
      else if (private->window_type == GDK_WINDOW_FOREIGN)
	{
	  shape = _gdk_windowing_window_get_shape ((GdkWindow *)child);
	  if (shape)
	    {
Benjamin Otte's avatar
Benjamin Otte committed
701 702
	      cairo_region_intersect (child_region, shape);
	      cairo_region_destroy (shape);
Alexander Larsson's avatar
Alexander Larsson committed
703 704
	    }
	}
Alexander Larsson's avatar
Alexander Larsson committed
705 706 707 708

      if (for_input)
	{
	  if (child->input_shape)
Benjamin Otte's avatar
Benjamin Otte committed
709
	    cairo_region_intersect (child_region, child->input_shape);
Alexander Larsson's avatar
Alexander Larsson committed
710 711 712 713 714
	  else if (private->window_type == GDK_WINDOW_FOREIGN)
	    {
	      shape = _gdk_windowing_window_get_input_shape ((GdkWindow *)child);
	      if (shape)
		{
Benjamin Otte's avatar
Benjamin Otte committed
715 716
		  cairo_region_intersect (child_region, shape);
		  cairo_region_destroy (shape);
Alexander Larsson's avatar
Alexander Larsson committed
717 718 719
		}
	    }
	}
720

Benjamin Otte's avatar
Benjamin Otte committed
721 722
      cairo_region_subtract (region, child_region);
      cairo_region_destroy (child_region);
Alexander Larsson's avatar
Alexander Larsson committed
723

724 725 726
    }
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
static GdkVisibilityState
effective_visibility (GdkWindowObject *private)
{
  GdkVisibilityState native;

  if (!gdk_window_is_viewable ((GdkWindow *)private))
    return GDK_VISIBILITY_NOT_VIEWABLE;

  native = private->impl_window->native_visibility;

  if (native == GDK_VISIBILITY_FULLY_OBSCURED ||
      private->visibility == GDK_VISIBILITY_FULLY_OBSCURED)
    return GDK_VISIBILITY_FULLY_OBSCURED;
  else if (native == GDK_VISIBILITY_UNOBSCURED)
    return private->visibility;
  else /* native PARTIAL, private partial or unobscured  */
    return GDK_VISIBILITY_PARTIAL;
}

static void
gdk_window_update_visibility (GdkWindowObject *private)
{
  GdkVisibilityState new_visibility;
  GdkEvent *event;

  new_visibility = effective_visibility (private);

  if (new_visibility != private->effective_visibility)
    {
      private->effective_visibility = new_visibility;

      if (new_visibility != GDK_VISIBILITY_NOT_VIEWABLE &&
	  private->event_mask & GDK_VISIBILITY_NOTIFY)
	{
	  event = _gdk_make_event ((GdkWindow *)private, GDK_VISIBILITY_NOTIFY,
				   NULL, FALSE);
	  event->visibility.state = new_visibility;
	}
    }
}

static void
gdk_window_update_visibility_recursively (GdkWindowObject *private,
					  GdkWindowObject *only_for_impl)
{
  GdkWindowObject *child;
  GList *l;

  gdk_window_update_visibility (private);
  for (l = private->children; l != NULL; l = l->next)
    {
      child = l->data;
      if ((only_for_impl == NULL) ||
	  (only_for_impl == child->impl_window))
	gdk_window_update_visibility_recursively (child, only_for_impl);
    }
}

Alexander Larsson's avatar
Alexander Larsson committed
785 786 787 788 789 790
static gboolean
should_apply_clip_as_shape (GdkWindowObject *private)
{
  return
    gdk_window_has_impl (private) &&
    /* Not for offscreens */
791
    !gdk_window_is_offscreen (private) &&
Alexander Larsson's avatar
Alexander Larsson committed
792 793 794 795 796 797 798 799 800 801
    /* or for toplevels */
    !gdk_window_is_toplevel (private) &&
    /* or for foreign windows */
    private->window_type != GDK_WINDOW_FOREIGN &&
    /* or for the root window */
    private->window_type != GDK_WINDOW_ROOT;
}

static void
apply_shape (GdkWindowObject *private,
802
	     cairo_region_t *region)
Alexander Larsson's avatar
Alexander Larsson committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
{
  GdkWindowImplIface *impl_iface;

  /* We trash whether we applied a shape so that
     we can avoid unsetting it many times, which
     could happen in e.g. apply_clip_as_shape as
     windows get resized */
  impl_iface = GDK_WINDOW_IMPL_GET_IFACE (private->impl);
  if (region)
    impl_iface->shape_combine_region ((GdkWindow *)private,
				      region, 0, 0);
  else if (private->applied_shape)
    impl_iface->shape_combine_region ((GdkWindow *)private,
				      NULL, 0, 0);

  private->applied_shape = region != NULL;
}

Benjamin Otte's avatar
Benjamin Otte committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
static gboolean
region_rect_equal (const cairo_region_t *region,
                   const GdkRectangle *rect)
{
    GdkRectangle extents;

    if (cairo_region_num_rectangles (region) != 1)
        return FALSE;

    cairo_region_get_extents (region, &extents);

    return extents.x == rect->x &&
        extents.y == rect->y &&
        extents.width == rect->width &&
        extents.height == rect->height;
}

Alexander Larsson's avatar
Alexander Larsson committed
838 839 840 841 842 843 844 845 846 847 848 849 850
static void
apply_clip_as_shape (GdkWindowObject *private)
{
  GdkRectangle r;

  r.x = r.y = 0;
  r.width = private->width;
  r.height = private->height;

  /* We only apply the clip region if would differ
     from the actual clip region implied by the size
     of the window. This is to avoid unneccessarily
     adding meaningless shapes to all native subwindows */
Benjamin Otte's avatar
Benjamin Otte committed
851
  if (!region_rect_equal (private->clip_region, &r))
Alexander Larsson's avatar
Alexander Larsson committed
852 853 854 855 856
    apply_shape (private, private->clip_region);
  else
    apply_shape (private, NULL);
}

857 858 859 860 861 862 863 864 865
static void
recompute_visible_regions_internal (GdkWindowObject *private,
				    gboolean recalculate_clip,
				    gboolean recalculate_siblings,
				    gboolean recalculate_children)
{
  GdkRectangle r;
  GList *l;
  GdkWindowObject *child;
866
  cairo_region_t *new_clip, *old_clip_region_with_children;
867 868 869 870 871 872
  gboolean clip_region_changed;
  gboolean abs_pos_changed;
  int old_abs_x, old_abs_y;

  old_abs_x = private->abs_x;
  old_abs_y = private->abs_y;
873

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  /* Update absolute position */
  if (gdk_window_has_impl (private))
    {
      /* Native window starts here */
      private->abs_x = 0;
      private->abs_y = 0;
    }
  else
    {
      private->abs_x = private->parent->abs_x + private->x;
      private->abs_y = private->parent->abs_y + private->y;
    }

  abs_pos_changed =
    private->abs_x != old_abs_x ||
    private->abs_y != old_abs_y;

  /* Update clip region based on:
   * parent clip
   * window size
   * siblings in parents above window
   */
  clip_region_changed = FALSE;
897
  if (recalculate_clip)
898
    {
899
      if (private->viewable)
900
	{
901 902 903 904 905
	  /* Calculate visible region (sans children) in parent window coords */
	  r.x = private->x;
	  r.y = private->y;
	  r.width = private->width;
	  r.height = private->height;
Benjamin Otte's avatar
Benjamin Otte committed
906
	  new_clip = cairo_region_create_rectangle (&r);
907

908
	  if (!gdk_window_is_toplevel (private))
909
	    {
Benjamin Otte's avatar
Benjamin Otte committed
910
	      cairo_region_intersect (new_clip, private->parent->clip_region);
911

912 913 914 915 916 917 918 919
	      /* Remove all overlapping children from parent.
	       * Unless we're all native, because then we don't need to take
	       * siblings into account since X does that clipping for us.
	       * This makes things like SWT that modify the raw X stacking
	       * order without GDKs knowledge work.
	       */
	      if (!_gdk_native_windows)
		remove_child_area (private->parent, private, FALSE, new_clip);
920
	    }
921

922
	  /* Convert from parent coords to window coords */
Benjamin Otte's avatar
Benjamin Otte committed
923
	  cairo_region_translate (new_clip, -private->x, -private->y);
924

925
	  if (private->shape)
Benjamin Otte's avatar
Benjamin Otte committed
926
	    cairo_region_intersect (new_clip, private->shape);
927 928
	}
      else
Benjamin Otte's avatar
Benjamin Otte committed
929
	new_clip = cairo_region_create ();
Alexander Larsson's avatar
Alexander Larsson committed
930

931
      if (private->clip_region == NULL ||
Benjamin Otte's avatar
Benjamin Otte committed
932
	  !cairo_region_equal (private->clip_region, new_clip))
933
	clip_region_changed = TRUE;
934

935
      if (private->clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
936
	cairo_region_destroy (private->clip_region);
937 938
      private->clip_region = new_clip;

939
      old_clip_region_with_children = private->clip_region_with_children;
Benjamin Otte's avatar
Benjamin Otte committed
940
      private->clip_region_with_children = cairo_region_copy (private->clip_region);
941
      if (private->window_type != GDK_WINDOW_ROOT)
942
	remove_child_area (private, NULL, FALSE, private->clip_region_with_children);
943 944

      if (clip_region_changed ||
Benjamin Otte's avatar
Benjamin Otte committed
945
	  !cairo_region_equal (private->clip_region_with_children, old_clip_region_with_children))
946 947 948
	  private->clip_tag = new_region_tag ();

      if (old_clip_region_with_children)
Benjamin Otte's avatar
Benjamin Otte committed
949
	cairo_region_destroy (old_clip_region_with_children);
950 951
    }

952 953 954 955 956
  if (clip_region_changed)
    {
      GdkVisibilityState visibility;
      gboolean fully_visible;

Benjamin Otte's avatar
Benjamin Otte committed
957
      if (cairo_region_is_empty (private->clip_region))
958
	visibility = GDK_VISIBILITY_FULLY_OBSCURED;
959 960 961 962
      else
        {
          if (private->shape)
            {
Benjamin Otte's avatar
Benjamin Otte committed
963
	      fully_visible = cairo_region_equal (private->clip_region,
964 965 966 967 968 969 970 971
	                                        private->shape);
            }
          else
            {
	      r.x = 0;
	      r.y = 0;
	      r.width = private->width;
	      r.height = private->height;
Benjamin Otte's avatar
Benjamin Otte committed
972
	      fully_visible = region_rect_equal (private->clip_region, &r);
973 974 975 976 977 978
	    }

	  if (fully_visible)
	    visibility = GDK_VISIBILITY_UNOBSCURED;
	  else
	    visibility = GDK_VISIBILITY_PARTIAL;
979 980 981 982 983 984 985 986 987
	}

      if (private->visibility != visibility)
	{
	  private->visibility = visibility;
	  gdk_window_update_visibility (private);
	}
    }

988 989
  /* Update all children, recursively (except for root, where children are not exact). */
  if ((abs_pos_changed || clip_region_changed || recalculate_children) &&
990
      private->window_type != GDK_WINDOW_ROOT)
991 992 993 994 995 996 997 998
    {
      for (l = private->children; l; l = l->next)
	{
	  child = l->data;
	  /* Only recalculate clip if the the clip region changed, otherwise
	   * there is no way the child clip region could change (its has not e.g. moved)
	   * Except if recalculate_children is set to force child updates
	   */
999 1000 1001
	  recompute_visible_regions_internal (child,
					      recalculate_clip && (clip_region_changed || recalculate_children),
					      FALSE, FALSE);
1002 1003 1004 1005
	}
    }

  if (clip_region_changed &&
Alexander Larsson's avatar
Alexander Larsson committed
1006 1007
      should_apply_clip_as_shape (private))
    apply_clip_as_shape (private);
Alexander Larsson's avatar
Alexander Larsson committed
1008

1009
  if (recalculate_siblings &&
1010
      !gdk_window_is_toplevel (private))
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    {
      /* If we moved a child window in parent or changed the stacking order, then we
       * need to recompute the visible area of all the other children in the parent
       */
      for (l = private->parent->children; l; l = l->next)
	{
	  child = l->data;

	  if (child != private)
	    recompute_visible_regions_internal (child, TRUE, FALSE, FALSE);
	}

      /* We also need to recompute the _with_children clip for the parent */
      recompute_visible_regions_internal (private->parent, TRUE, FALSE, FALSE);
    }

1027 1028 1029 1030 1031
  if (private->cairo_surface &&
      (!gdk_window_has_impl (private) ||
       !_gdk_windowing_set_cairo_surface_size (private->cairo_surface,
                                               private->width,
                                               private->height)))
1032
    {
1033 1034
      cairo_surface_destroy (private->cairo_surface);
      private->cairo_surface = NULL;
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    }
}

/* Call this when private has changed in one or more of these ways:
 *  size changed
 *  window moved
 *  new window added
 *  stacking order of window changed
 *  child deleted
 *
 * It will recalculate abs_x/y and the clip regions
 *
 * Unless the window didn't change stacking order or size/pos, pass in TRUE
 * for recalculate_siblings. (Mostly used internally for the recursion)
1049
 *
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
 * If a child window was removed (and you can't use that child for
 * recompute_visible_regions), pass in TRUE for recalculate_children on the parent
 */
static void
recompute_visible_regions (GdkWindowObject *private,
			   gboolean recalculate_siblings,
			   gboolean recalculate_children)
{
  recompute_visible_regions_internal (private,
				      TRUE,
				      recalculate_siblings,
				      recalculate_children);
}

void
_gdk_window_update_size (GdkWindow *window)
{
  recompute_visible_regions ((GdkWindowObject *)window, TRUE, FALSE);
}

1070 1071 1072 1073 1074 1075 1076
/* Find the native window that would be just above "child"
 * in the native stacking order if "child" was a native window
 * (it doesn't have to be native). If there is no such native
 * window inside this native parent then NULL is returned.
 * If child is NULL, find lowest native window in parent.
 */
static GdkWindowObject *
1077 1078
find_native_sibling_above_helper (GdkWindowObject *parent,
				  GdkWindowObject *child)
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
{
  GdkWindowObject *w;
  GList *l;

  if (child)
    {
      l = g_list_find (parent->children, child);
      g_assert (l != NULL); /* Better be a child of its parent... */
      l = l->prev; /* Start looking at the one above the child */
    }
  else
    l = g_list_last (parent->children);

  for (; l != NULL; l = l->prev)
    {
      w = l->data;
1095

1096 1097
      if (gdk_window_has_impl (w))
	return w;
1098 1099 1100

      g_assert (parent != w);
      w = find_native_sibling_above_helper (w, NULL);
1101 1102 1103 1104
      if (w)
	return w;
    }

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  return NULL;
}


static GdkWindowObject *
find_native_sibling_above (GdkWindowObject *parent,
			   GdkWindowObject *child)
{
  GdkWindowObject *w;

  w = find_native_sibling_above_helper (parent, child);
  if (w)
    return w;

1119 1120 1121 1122 1123 1124
  if (gdk_window_has_impl (parent))
    return NULL;
  else
    return find_native_sibling_above (parent->parent, parent);
}

1125
static GdkEventMask
1126 1127
get_native_device_event_mask (GdkWindowObject *private,
                              GdkDevice       *device)
1128
{
1129 1130 1131 1132 1133 1134 1135
  GdkEventMask event_mask;

  if (device)
    event_mask = GPOINTER_TO_INT (g_hash_table_lookup (private->device_events, device));
  else
    event_mask = private->event_mask;

1136 1137 1138
  if (_gdk_native_windows ||
      private->window_type == GDK_WINDOW_ROOT ||
      private->