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/.
30 #include "gdkwindow.h"
31 #include "gdkinternals.h"
32 #include "gdkwindowimpl.h"
33 #include "gdkdrawable.h"
35 #include "gdkscreen.h"
41 * Offscreen windows can't be the child of a foreign window,
42 * nor contain foreign windows
43 * GDK_POINTER_MOTION_HINT_MASK isn't effective
46 typedef struct _GdkOffscreenWindow GdkOffscreenWindow;
47 typedef struct _GdkOffscreenWindowClass GdkOffscreenWindowClass;
49 struct _GdkOffscreenWindow
51 GdkDrawable parent_instance;
54 GdkColormap *colormap;
57 cairo_surface_t *surface;
61 struct _GdkOffscreenWindowClass
63 GdkDrawableClass parent_class;
66 #define GDK_TYPE_OFFSCREEN_WINDOW (gdk_offscreen_window_get_type())
67 #define GDK_OFFSCREEN_WINDOW(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindow))
68 #define GDK_IS_OFFSCREEN_WINDOW(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_OFFSCREEN_WINDOW))
69 #define GDK_OFFSCREEN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
70 #define GDK_IS_OFFSCREEN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_OFFSCREEN_WINDOW))
71 #define GDK_OFFSCREEN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
73 static void gdk_offscreen_window_impl_iface_init (GdkWindowImplIface *iface);
74 static void gdk_offscreen_window_hide (GdkWindow *window);
76 G_DEFINE_TYPE_WITH_CODE (GdkOffscreenWindow,
79 G_IMPLEMENT_INTERFACE (GDK_TYPE_WINDOW_IMPL,
80 gdk_offscreen_window_impl_iface_init));
84 gdk_offscreen_window_finalize (GObject *object)
86 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (object);
88 cairo_surface_destroy (offscreen->surface);
90 G_OBJECT_CLASS (gdk_offscreen_window_parent_class)->finalize (object);
94 gdk_offscreen_window_init (GdkOffscreenWindow *window)
99 gdk_offscreen_window_destroy (GdkWindow *window,
101 gboolean foreign_destroy)
103 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
104 GdkOffscreenWindow *offscreen;
106 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
108 gdk_offscreen_window_set_embedder (window, NULL);
111 gdk_offscreen_window_hide (window);
113 g_object_unref (offscreen->colormap);
114 offscreen->colormap = NULL;
118 is_parent_of (GdkWindow *parent,
129 w = gdk_window_get_parent (w);
135 static cairo_surface_t *
136 gdk_offscreen_window_ref_cairo_surface (GdkDrawable *drawable)
138 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (drawable);
140 return cairo_surface_reference (offscreen->surface);
144 gdk_offscreen_window_get_colormap (GdkDrawable *drawable)
146 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (drawable);
148 return offscreen->colormap;
152 _gdk_offscreen_window_new (GdkWindow *window,
154 GdkWindowAttr *attributes,
155 gint attributes_mask)
157 GdkWindowObject *private;
158 GdkOffscreenWindow *offscreen;
160 g_return_if_fail (attributes != NULL);
162 if (attributes->wclass != GDK_INPUT_OUTPUT)
163 return; /* Can't support input only offscreens */
165 private = (GdkWindowObject *)window;
167 if (private->parent != NULL && GDK_WINDOW_DESTROYED (private->parent))
170 private->impl = g_object_new (GDK_TYPE_OFFSCREEN_WINDOW, NULL);
171 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
172 offscreen->wrapper = window;
174 offscreen->screen = screen;
176 if (attributes_mask & GDK_WA_COLORMAP)
177 offscreen->colormap = g_object_ref (attributes->colormap);
180 if (gdk_screen_get_system_visual (screen) == private->visual)
182 offscreen->colormap = gdk_screen_get_system_colormap (screen);
183 g_object_ref (offscreen->colormap);
186 offscreen->colormap = gdk_colormap_new (private->visual, FALSE);
189 offscreen->surface = gdk_window_create_similar_surface ((GdkWindow *)private->parent,
196 gdk_offscreen_window_reparent (GdkWindow *window,
197 GdkWindow *new_parent,
201 GdkWindowObject *private = (GdkWindowObject *)window;
202 GdkWindowObject *new_parent_private = (GdkWindowObject *)new_parent;
203 GdkWindowObject *old_parent;
208 /* No input-output children of input-only windows */
209 if (new_parent_private->input_only && !private->input_only)
212 /* Don't create loops in hierarchy */
213 if (is_parent_of (window, new_parent))
217 was_mapped = GDK_WINDOW_IS_MAPPED (window);
219 gdk_window_hide (window);
222 private->parent->children = g_list_remove (private->parent->children, window);
224 old_parent = private->parent;
225 private->parent = new_parent_private;
229 if (new_parent_private)
230 private->parent->children = g_list_prepend (private->parent->children, window);
232 _gdk_synthesize_crossing_events_for_geometry_change (window);
234 _gdk_synthesize_crossing_events_for_geometry_change (GDK_WINDOW (old_parent));
240 from_embedder (GdkWindow *window,
241 double embedder_x, double embedder_y,
242 double *offscreen_x, double *offscreen_y)
244 GdkWindowObject *private;
246 private = (GdkWindowObject *)window;
248 g_signal_emit_by_name (private->impl_window,
250 embedder_x, embedder_y,
251 offscreen_x, offscreen_y,
256 to_embedder (GdkWindow *window,
257 double offscreen_x, double offscreen_y,
258 double *embedder_x, double *embedder_y)
260 GdkWindowObject *private;
262 private = (GdkWindowObject *)window;
264 g_signal_emit_by_name (private->impl_window,
266 offscreen_x, offscreen_y,
267 embedder_x, embedder_y,
272 gdk_offscreen_window_get_root_coords (GdkWindow *window,
278 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
279 GdkOffscreenWindow *offscreen;
285 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
286 if (offscreen->embedder)
292 tmpx = floor (dx + 0.5);
293 tmpy = floor (dy + 0.5);
294 gdk_window_get_root_coords (offscreen->embedder,
309 gdk_offscreen_window_get_device_state (GdkWindow *window,
313 GdkModifierType *mask)
315 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
316 GdkOffscreenWindow *offscreen;
319 GdkModifierType tmpmask;
325 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
326 if (offscreen->embedder != NULL)
328 gdk_window_get_device_position (offscreen->embedder, device, &tmpx, &tmpy, &tmpmask);
329 from_embedder (window,
332 tmpx = floor (dtmpx + 0.5);
333 tmpy = floor (dtmpy + 0.5);
346 * gdk_offscreen_window_get_surface:
347 * @window: a #GdkWindow
349 * Gets the offscreen surface that an offscreen window renders into.
350 * If you need to keep this around over window resizes, you need to
351 * add a reference to it.
353 * Returns: The offscreen surface, or %NULL if not offscreen
356 gdk_offscreen_window_get_surface (GdkWindow *window)
358 GdkWindowObject *private = (GdkWindowObject *)window;
359 GdkOffscreenWindow *offscreen;
361 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
363 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
366 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
367 return offscreen->surface;
371 gdk_offscreen_window_raise (GdkWindow *window)
373 /* gdk_window_raise already changed the stacking order */
374 _gdk_synthesize_crossing_events_for_geometry_change (window);
378 gdk_offscreen_window_lower (GdkWindow *window)
380 /* gdk_window_lower already changed the stacking order */
381 _gdk_synthesize_crossing_events_for_geometry_change (window);
385 gdk_offscreen_window_move_resize_internal (GdkWindow *window,
390 gboolean send_expose_events)
392 GdkWindowObject *private = (GdkWindowObject *)window;
393 GdkOffscreenWindow *offscreen;
395 cairo_surface_t *old_surface;
397 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
404 if (private->destroyed)
409 dw = width - private->width;
410 dh = height - private->height;
415 if (private->width != width ||
416 private->height != height)
420 private->width = width;
421 private->height = height;
423 old_surface = offscreen->surface;
424 offscreen->surface = cairo_surface_create_similar (old_surface,
425 cairo_surface_get_content (old_surface),
429 cr = cairo_create (offscreen->surface);
430 cairo_set_source_surface (cr, old_surface, 0, 0);
434 cairo_surface_destroy (old_surface);
437 if (GDK_WINDOW_IS_MAPPED (private))
439 // TODO: Only invalidate new area, i.e. for larger windows
440 gdk_window_invalidate_rect (window, NULL, TRUE);
441 _gdk_synthesize_crossing_events_for_geometry_change (window);
446 gdk_offscreen_window_move_resize (GdkWindow *window,
453 GdkWindowObject *private = (GdkWindowObject *)window;
454 GdkOffscreenWindow *offscreen;
456 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
465 width = private->width;
468 height = private->height;
470 gdk_offscreen_window_move_resize_internal (window, x, y,
476 gdk_offscreen_window_show (GdkWindow *window,
477 gboolean already_mapped)
479 GdkWindowObject *private = (GdkWindowObject *)window;
480 GdkRectangle area = { 0, 0, private->width, private->height };
482 gdk_window_clear_area (window, 0, 0,
483 private->width, private->height);
484 gdk_window_invalidate_rect (window, &area, FALSE);
489 gdk_offscreen_window_hide (GdkWindow *window)
491 GdkWindowObject *private;
492 GdkOffscreenWindow *offscreen;
495 g_return_if_fail (window != NULL);
497 private = (GdkWindowObject*) window;
498 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
500 /* May need to break grabs on children */
501 display = gdk_window_get_display (window);
503 /* TODO: This needs updating to the new grab world */
505 if (display->pointer_grab.window != NULL)
507 if (is_parent_of (window, display->pointer_grab.window))
509 /* Call this ourselves, even though gdk_display_pointer_ungrab
510 does so too, since we want to pass implicit == TRUE so the
511 broken grab event is generated */
512 _gdk_display_unset_has_pointer_grab (display,
516 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
523 gdk_offscreen_window_withdraw (GdkWindow *window)
528 gdk_offscreen_window_get_events (GdkWindow *window)
534 gdk_offscreen_window_set_events (GdkWindow *window,
535 GdkEventMask event_mask)
540 gdk_offscreen_window_set_background (GdkWindow *window,
541 cairo_pattern_t *pattern)
546 gdk_offscreen_window_shape_combine_region (GdkWindow *window,
547 const cairo_region_t *shape_region,
554 gdk_offscreen_window_input_shape_combine_region (GdkWindow *window,
555 const cairo_region_t *shape_region,
562 gdk_offscreen_window_set_static_gravities (GdkWindow *window,
569 gdk_offscreen_window_get_geometry (GdkWindow *window,
576 GdkWindowObject *private = (GdkWindowObject *)window;
578 g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
580 if (!GDK_WINDOW_DESTROYED (window))
587 *width = private->width;
589 *height = private->height;
591 *depth = private->depth;
596 gdk_offscreen_window_queue_antiexpose (GdkWindow *window,
597 cairo_region_t *area)
603 gdk_offscreen_window_translate (GdkWindow *window,
604 cairo_region_t *area,
608 GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (((GdkWindowObject *) window)->impl);
611 cr = cairo_create (offscreen->surface);
613 area = cairo_region_copy (area);
615 gdk_cairo_region (cr, area);
618 /* NB: This is a self-copy and Cairo doesn't support that yet.
619 * So we do a litle trick.
621 cairo_push_group (cr);
623 cairo_set_source_surface (cr, offscreen->surface, dx, dy);
626 cairo_pop_group_to_source (cr);
631 _gdk_window_add_damage (window, area);
635 * gdk_offscreen_window_set_embedder:
636 * @window: a #GdkWindow
637 * @embedder: the #GdkWindow that @window gets embedded in
639 * Sets @window to be embedded in @embedder.
641 * To fully embed an offscreen window, in addition to calling this
642 * function, it is also necessary to handle the #GdkWindow::pick-embedded-child
643 * signal on the @embedder and the #GdkWindow::to-embedder and
644 * #GdkWindow::from-embedder signals on @window.
649 gdk_offscreen_window_set_embedder (GdkWindow *window,
652 GdkWindowObject *private = (GdkWindowObject *)window;
653 GdkOffscreenWindow *offscreen;
655 g_return_if_fail (GDK_IS_WINDOW (window));
657 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
660 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
664 g_object_ref (embedder);
665 GDK_WINDOW_OBJECT (embedder)->num_offscreen_children++;
668 if (offscreen->embedder)
670 g_object_unref (offscreen->embedder);
671 GDK_WINDOW_OBJECT (offscreen->embedder)->num_offscreen_children--;
674 offscreen->embedder = embedder;
678 * gdk_offscreen_window_get_embedder:
679 * @window: a #GdkWindow
681 * Gets the window that @window is embedded in.
683 * Returns: the embedding #GdkWindow, or %NULL if @window is not an
684 * embedded offscreen window
689 gdk_offscreen_window_get_embedder (GdkWindow *window)
691 GdkWindowObject *private = (GdkWindowObject *)window;
692 GdkOffscreenWindow *offscreen;
694 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
696 if (!GDK_IS_OFFSCREEN_WINDOW (private->impl))
699 offscreen = GDK_OFFSCREEN_WINDOW (private->impl);
701 return offscreen->embedder;
705 gdk_offscreen_window_class_init (GdkOffscreenWindowClass *klass)
707 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
708 GObjectClass *object_class = G_OBJECT_CLASS (klass);
710 object_class->finalize = gdk_offscreen_window_finalize;
712 drawable_class->ref_cairo_surface = gdk_offscreen_window_ref_cairo_surface;
713 drawable_class->get_colormap = gdk_offscreen_window_get_colormap;
717 gdk_offscreen_window_impl_iface_init (GdkWindowImplIface *iface)
719 iface->show = gdk_offscreen_window_show;
720 iface->hide = gdk_offscreen_window_hide;
721 iface->withdraw = gdk_offscreen_window_withdraw;
722 iface->raise = gdk_offscreen_window_raise;
723 iface->lower = gdk_offscreen_window_lower;
724 iface->move_resize = gdk_offscreen_window_move_resize;
725 iface->set_background = gdk_offscreen_window_set_background;
726 iface->get_events = gdk_offscreen_window_get_events;
727 iface->set_events = gdk_offscreen_window_set_events;
728 iface->reparent = gdk_offscreen_window_reparent;
729 iface->get_geometry = gdk_offscreen_window_get_geometry;
730 iface->shape_combine_region = gdk_offscreen_window_shape_combine_region;
731 iface->input_shape_combine_region = gdk_offscreen_window_input_shape_combine_region;
732 iface->set_static_gravities = gdk_offscreen_window_set_static_gravities;
733 iface->queue_antiexpose = gdk_offscreen_window_queue_antiexpose;
734 iface->translate = gdk_offscreen_window_translate;
735 iface->get_root_coords = gdk_offscreen_window_get_root_coords;
736 iface->get_device_state = gdk_offscreen_window_get_device_state;
737 iface->destroy = gdk_offscreen_window_destroy;