1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2005. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkwindow.h"
30 #include "gdkinternals.h"
31 #include "gdkwindowimpl.h"
37 * Offscreen windows can't be the child of a foreign window,
38 * nor contain foreign windows
39 * GDK_POINTER_MOTION_HINT_MASK isn't effective
42 typedef struct _GdkOffscreenWindow GdkOffscreenWindow;
43 typedef struct _GdkOffscreenWindowClass GdkOffscreenWindowClass;
45 struct _GdkOffscreenWindow
47 GdkDrawable parent_instance;
51 cairo_surface_t *surface;
55 struct _GdkOffscreenWindowClass
57 GdkDrawableClass parent_class;
60 #define GDK_TYPE_OFFSCREEN_WINDOW (gdk_offscreen_window_get_type())
61 #define GDK_OFFSCREEN_WINDOW(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindow))
62 #define GDK_IS_OFFSCREEN_WINDOW(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_OFFSCREEN_WINDOW))
63 #define GDK_OFFSCREEN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
64 #define GDK_IS_OFFSCREEN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_OFFSCREEN_WINDOW))
65 #define GDK_OFFSCREEN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
67 static void gdk_offscreen_window_impl_iface_init (GdkWindowImplIface *iface);
68 static void gdk_offscreen_window_hide (GdkWindow *window);
70 G_DEFINE_TYPE_WITH_CODE (GdkOffscreenWindow,
73 G_IMPLEMENT_INTERFACE (GDK_TYPE_WINDOW_IMPL,
74 gdk_offscreen_window_impl_iface_init));
78 gdk_offscreen_window_finalize (GObject *object)
80 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (object);
82 if (offscreen->surface)
83 cairo_surface_destroy (offscreen->surface);
85 G_OBJECT_CLASS (gdk_offscreen_window_parent_class)->finalize (object);
89 gdk_offscreen_window_init (GdkOffscreenWindow *window)
94 gdk_offscreen_window_destroy (GdkWindow *window,
96 gboolean foreign_destroy)
98 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
99 GdkOffscreenWindow *offscreen;
101 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
103 gdk_offscreen_window_set_embedder (window, NULL);
106 gdk_offscreen_window_hide (window);
109 static cairo_surface_t *
110 get_surface (GdkOffscreenWindow *offscreen)
112 if (! offscreen->surface)
114 GdkWindowObject *private = (GdkWindowObject *) offscreen->wrapper;
116 g_signal_emit_by_name (private, "create-surface",
119 &offscreen->surface);
122 return offscreen->surface;
126 is_parent_of (GdkWindow *parent,
137 w = gdk_window_get_parent (w);
143 static cairo_surface_t *
144 gdk_offscreen_window_ref_cairo_surface (GdkDrawable *drawable)
146 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (drawable);
148 return cairo_surface_reference (get_surface (offscreen));
152 _gdk_offscreen_window_create_surface (GdkWindow *offscreen,
156 GdkWindowObject *private = (GdkWindowObject *) offscreen;
157 cairo_surface_t *similar;
158 cairo_surface_t *surface;
159 cairo_content_t content = CAIRO_CONTENT_COLOR;
161 g_return_val_if_fail (GDK_IS_OFFSCREEN_WINDOW (private->impl), NULL);
163 similar = _gdk_drawable_ref_cairo_surface ((GdkWindow *)private->parent);
165 if (gdk_window_get_visual (offscreen) ==
166 gdk_screen_get_rgba_visual (gdk_window_get_screen (offscreen)))
168 content = CAIRO_CONTENT_COLOR_ALPHA;
171 surface = cairo_surface_create_similar (similar, content, width, height);
173 cairo_surface_destroy (similar);
179 _gdk_offscreen_window_new (GdkWindow *window,
180 GdkWindowAttr *attributes,
181 gint attributes_mask)
183 GdkWindowObject *private;
184 GdkOffscreenWindow *offscreen;
186 g_return_if_fail (attributes != NULL);
188 if (attributes->wclass != GDK_INPUT_OUTPUT)
189 return; /* Can't support input only offscreens */
191 private = (GdkWindowObject *)window;
193 if (private->parent != NULL && GDK_WINDOW_DESTROYED (private->parent))
196 private->impl = g_object_new (GDK_TYPE_OFFSCREEN_WINDOW, NULL);
197 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
198 offscreen->wrapper = window;
202 gdk_offscreen_window_reparent (GdkWindow *window,
203 GdkWindow *new_parent,
207 GdkWindowObject *private = (GdkWindowObject *)window;
208 GdkWindowObject *new_parent_private = (GdkWindowObject *)new_parent;
209 GdkWindowObject *old_parent;
214 /* No input-output children of input-only windows */
215 if (new_parent_private->input_only && !private->input_only)
218 /* Don't create loops in hierarchy */
219 if (is_parent_of (window, new_parent))
223 was_mapped = GDK_WINDOW_IS_MAPPED (window);
225 gdk_window_hide (window);
228 private->parent->children = g_list_remove (private->parent->children, window);
230 old_parent = private->parent;
231 private->parent = new_parent_private;
235 if (new_parent_private)
236 private->parent->children = g_list_prepend (private->parent->children, window);
238 _gdk_synthesize_crossing_events_for_geometry_change (window);
240 _gdk_synthesize_crossing_events_for_geometry_change (GDK_WINDOW (old_parent));
246 from_embedder (GdkWindow *window,
247 double embedder_x, double embedder_y,
248 double *offscreen_x, double *offscreen_y)
250 GdkWindowObject *private;
252 private = (GdkWindowObject *)window;
254 g_signal_emit_by_name (private->impl_window,
256 embedder_x, embedder_y,
257 offscreen_x, offscreen_y,
262 to_embedder (GdkWindow *window,
263 double offscreen_x, double offscreen_y,
264 double *embedder_x, double *embedder_y)
266 GdkWindowObject *private;
268 private = (GdkWindowObject *)window;
270 g_signal_emit_by_name (private->impl_window,
272 offscreen_x, offscreen_y,
273 embedder_x, embedder_y,
278 gdk_offscreen_window_get_root_coords (GdkWindow *window,
284 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
285 GdkOffscreenWindow *offscreen;
291 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
292 if (offscreen->embedder)
298 tmpx = floor (dx + 0.5);
299 tmpy = floor (dy + 0.5);
300 gdk_window_get_root_coords (offscreen->embedder,
315 gdk_offscreen_window_get_device_state (GdkWindow *window,
319 GdkModifierType *mask)
321 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
322 GdkOffscreenWindow *offscreen;
325 GdkModifierType tmpmask;
331 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
332 if (offscreen->embedder != NULL)
334 gdk_window_get_device_position (offscreen->embedder, device, &tmpx, &tmpy, &tmpmask);
335 from_embedder (window,
338 tmpx = floor (dtmpx + 0.5);
339 tmpy = floor (dtmpy + 0.5);
352 * gdk_offscreen_window_get_surface:
353 * @window: a #GdkWindow
355 * Gets the offscreen surface that an offscreen window renders into.
356 * If you need to keep this around over window resizes, you need to
357 * add a reference to it.
359 * Returns: The offscreen surface, or %NULL if not offscreen
362 gdk_offscreen_window_get_surface (GdkWindow *window)
364 GdkWindowObject *private = (GdkWindowObject *)window;
365 GdkOffscreenWindow *offscreen;
367 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
369 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
372 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
374 return get_surface (offscreen);
378 gdk_offscreen_window_raise (GdkWindow *window)
380 /* gdk_window_raise already changed the stacking order */
381 _gdk_synthesize_crossing_events_for_geometry_change (window);
385 gdk_offscreen_window_lower (GdkWindow *window)
387 /* gdk_window_lower already changed the stacking order */
388 _gdk_synthesize_crossing_events_for_geometry_change (window);
392 gdk_offscreen_window_move_resize_internal (GdkWindow *window,
397 gboolean send_expose_events)
399 GdkWindowObject *private = (GdkWindowObject *)window;
400 GdkOffscreenWindow *offscreen;
403 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
410 if (private->destroyed)
415 dw = width - private->width;
416 dh = height - private->height;
421 if (private->width != width ||
422 private->height != height)
424 private->width = width;
425 private->height = height;
427 if (offscreen->surface)
429 cairo_surface_t *old_surface;
432 old_surface = offscreen->surface;
433 offscreen->surface = NULL;
435 offscreen->surface = get_surface (offscreen);
437 cr = cairo_create (offscreen->surface);
438 cairo_set_source_surface (cr, old_surface, 0, 0);
442 cairo_surface_destroy (old_surface);
446 if (GDK_WINDOW_IS_MAPPED (private))
448 // TODO: Only invalidate new area, i.e. for larger windows
449 gdk_window_invalidate_rect (window, NULL, TRUE);
450 _gdk_synthesize_crossing_events_for_geometry_change (window);
455 gdk_offscreen_window_move_resize (GdkWindow *window,
462 GdkWindowObject *private = (GdkWindowObject *)window;
463 GdkOffscreenWindow *offscreen;
465 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
474 width = private->width;
477 height = private->height;
479 gdk_offscreen_window_move_resize_internal (window, x, y,
485 gdk_offscreen_window_show (GdkWindow *window,
486 gboolean already_mapped)
488 GdkWindowObject *private = (GdkWindowObject *)window;
489 GdkRectangle area = { 0, 0, private->width, private->height };
491 gdk_window_invalidate_rect (window, &area, FALSE);
496 gdk_offscreen_window_hide (GdkWindow *window)
498 GdkWindowObject *private;
499 GdkOffscreenWindow *offscreen;
502 g_return_if_fail (window != NULL);
504 private = (GdkWindowObject*) window;
505 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
507 /* May need to break grabs on children */
508 display = gdk_window_get_display (window);
510 /* TODO: This needs updating to the new grab world */
512 if (display->pointer_grab.window != NULL)
514 if (is_parent_of (window, display->pointer_grab.window))
516 /* Call this ourselves, even though gdk_display_pointer_ungrab
517 does so too, since we want to pass implicit == TRUE so the
518 broken grab event is generated */
519 _gdk_display_unset_has_pointer_grab (display,
523 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
530 gdk_offscreen_window_withdraw (GdkWindow *window)
535 gdk_offscreen_window_get_events (GdkWindow *window)
541 gdk_offscreen_window_set_events (GdkWindow *window,
542 GdkEventMask event_mask)
547 gdk_offscreen_window_set_background (GdkWindow *window,
548 cairo_pattern_t *pattern)
553 gdk_offscreen_window_shape_combine_region (GdkWindow *window,
554 const cairo_region_t *shape_region,
561 gdk_offscreen_window_input_shape_combine_region (GdkWindow *window,
562 const cairo_region_t *shape_region,
569 gdk_offscreen_window_set_static_gravities (GdkWindow *window,
576 gdk_offscreen_window_get_geometry (GdkWindow *window,
583 GdkWindowObject *private = (GdkWindowObject *)window;
585 g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
587 if (!GDK_WINDOW_DESTROYED (window))
594 *width = private->width;
596 *height = private->height;
598 *depth = private->depth;
603 gdk_offscreen_window_queue_antiexpose (GdkWindow *window,
604 cairo_region_t *area)
610 gdk_offscreen_window_translate (GdkWindow *window,
611 cairo_region_t *area,
615 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (((GdkWindowObject *) window)->impl);
617 if (offscreen->surface)
621 cr = cairo_create (offscreen->surface);
623 area = cairo_region_copy (area);
625 gdk_cairo_region (cr, area);
628 /* NB: This is a self-copy and Cairo doesn't support that yet.
629 * So we do a litle trick.
631 cairo_push_group (cr);
633 cairo_set_source_surface (cr, offscreen->surface, dx, dy);
636 cairo_pop_group_to_source (cr);
642 _gdk_window_add_damage (window, area);
645 static cairo_surface_t *
646 gdk_offscreen_window_resize_cairo_surface (GdkWindow *window,
647 cairo_surface_t *surface,
651 /* No-op. The surface gets resized in
652 * gdk_offscreen_window_move_resize_internal().
658 * gdk_offscreen_window_set_embedder:
659 * @window: a #GdkWindow
660 * @embedder: the #GdkWindow that @window gets embedded in
662 * Sets @window to be embedded in @embedder.
664 * To fully embed an offscreen window, in addition to calling this
665 * function, it is also necessary to handle the #GdkWindow::pick-embedded-child
666 * signal on the @embedder and the #GdkWindow::to-embedder and
667 * #GdkWindow::from-embedder signals on @window.
672 gdk_offscreen_window_set_embedder (GdkWindow *window,
675 GdkWindowObject *private = (GdkWindowObject *)window;
676 GdkOffscreenWindow *offscreen;
678 g_return_if_fail (GDK_IS_WINDOW (window));
680 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
683 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
687 g_object_ref (embedder);
688 GDK_WINDOW_OBJECT (embedder)->num_offscreen_children++;
691 if (offscreen->embedder)
693 g_object_unref (offscreen->embedder);
694 GDK_WINDOW_OBJECT (offscreen->embedder)->num_offscreen_children--;
697 offscreen->embedder = embedder;
701 * gdk_offscreen_window_get_embedder:
702 * @window: a #GdkWindow
704 * Gets the window that @window is embedded in.
706 * Returns: the embedding #GdkWindow, or %NULL if @window is not an
707 * embedded offscreen window
712 gdk_offscreen_window_get_embedder (GdkWindow *window)
714 GdkWindowObject *private = (GdkWindowObject *)window;
715 GdkOffscreenWindow *offscreen;
717 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
719 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
722 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
724 return offscreen->embedder;
728 gdk_offscreen_window_class_init (GdkOffscreenWindowClass *klass)
730 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
731 GObjectClass *object_class = G_OBJECT_CLASS (klass);
733 object_class->finalize = gdk_offscreen_window_finalize;
735 drawable_class->ref_cairo_surface = gdk_offscreen_window_ref_cairo_surface;
739 gdk_offscreen_window_impl_iface_init (GdkWindowImplIface *iface)
741 iface->show = gdk_offscreen_window_show;
742 iface->hide = gdk_offscreen_window_hide;
743 iface->withdraw = gdk_offscreen_window_withdraw;
744 iface->raise = gdk_offscreen_window_raise;
745 iface->lower = gdk_offscreen_window_lower;
746 iface->move_resize = gdk_offscreen_window_move_resize;
747 iface->set_background = gdk_offscreen_window_set_background;
748 iface->get_events = gdk_offscreen_window_get_events;
749 iface->set_events = gdk_offscreen_window_set_events;
750 iface->reparent = gdk_offscreen_window_reparent;
751 iface->get_geometry = gdk_offscreen_window_get_geometry;
752 iface->shape_combine_region = gdk_offscreen_window_shape_combine_region;
753 iface->input_shape_combine_region = gdk_offscreen_window_input_shape_combine_region;
754 iface->set_static_gravities = gdk_offscreen_window_set_static_gravities;
755 iface->queue_antiexpose = gdk_offscreen_window_queue_antiexpose;
756 iface->translate = gdk_offscreen_window_translate;
757 iface->get_root_coords = gdk_offscreen_window_get_root_coords;
758 iface->get_device_state = gdk_offscreen_window_get_device_state;
759 iface->destroy = gdk_offscreen_window_destroy;
760 iface->resize_cairo_surface = gdk_offscreen_window_resize_cairo_surface;