2009-12-18 13:57:03 +00:00
|
|
|
/*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Authors: Cody Russell <crussell@canonical.com>
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2010-09-14 01:33:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-12-18 12:43:11 +00:00
|
|
|
#include "gtkoffscreenwindow.h"
|
2010-10-19 17:14:46 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2010-09-28 16:24:55 +00:00
|
|
|
#include "gtkprivate.h"
|
2010-09-14 01:33:06 +00:00
|
|
|
|
2009-12-19 12:08:20 +00:00
|
|
|
/**
|
2010-01-28 02:10:09 +00:00
|
|
|
* SECTION:gtkoffscreenwindow
|
2010-09-25 02:13:42 +00:00
|
|
|
* @short_description: A toplevel to manage offscreen rendering of child widgets
|
2010-01-28 02:10:09 +00:00
|
|
|
* @title: GtkOffscreenWindow
|
2009-12-19 12:08:20 +00:00
|
|
|
*
|
2010-09-25 02:13:42 +00:00
|
|
|
* GtkOffscreenWindow is strictly intended to be used for obtaining
|
2009-12-19 12:08:20 +00:00
|
|
|
* snapshots of widgets that are not part of a normal widget hierarchy.
|
2010-08-24 15:04:18 +00:00
|
|
|
* Since #GtkOffscreenWindow is a toplevel widget you cannot obtain
|
|
|
|
* snapshots of a full window with it since you cannot pack a toplevel
|
|
|
|
* widget in another toplevel.
|
2009-12-19 12:08:20 +00:00
|
|
|
*
|
2009-12-28 15:24:12 +00:00
|
|
|
* The idea is to take a widget and manually set the state of it,
|
2010-09-25 02:13:42 +00:00
|
|
|
* add it to a GtkOffscreenWindow and then retrieve the snapshot
|
2010-08-26 11:46:34 +00:00
|
|
|
* as a #cairo_surface_t or #GdkPixbuf.
|
2009-12-19 12:08:20 +00:00
|
|
|
*
|
2010-09-25 02:13:42 +00:00
|
|
|
* GtkOffscreenWindow derives from #GtkWindow only as an implementation
|
2009-12-19 12:08:20 +00:00
|
|
|
* detail. Applications should not use any API specific to #GtkWindow
|
|
|
|
* to operate on this object. It should be treated as a #GtkBin that
|
|
|
|
* has no parent widget.
|
2009-12-19 15:09:54 +00:00
|
|
|
*
|
2010-09-25 02:13:42 +00:00
|
|
|
* When contained offscreen widgets are redrawn, GtkOffscreenWindow
|
2010-01-28 02:10:09 +00:00
|
|
|
* will emit a #GtkWidget::damage-event signal.
|
2009-12-19 12:08:20 +00:00
|
|
|
*/
|
|
|
|
|
2009-12-18 12:43:11 +00:00
|
|
|
G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_size_request (GtkWidget *widget,
|
|
|
|
GtkRequisition *requisition)
|
|
|
|
{
|
|
|
|
GtkBin *bin = GTK_BIN (widget);
|
2010-05-24 20:31:36 +00:00
|
|
|
GtkWidget *child;
|
2009-12-18 12:43:11 +00:00
|
|
|
gint border_width;
|
|
|
|
gint default_width, default_height;
|
|
|
|
|
|
|
|
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
|
|
|
|
|
|
|
|
requisition->width = border_width * 2;
|
|
|
|
requisition->height = border_width * 2;
|
|
|
|
|
2010-05-24 20:31:36 +00:00
|
|
|
child = gtk_bin_get_child (bin);
|
2010-07-22 14:43:23 +00:00
|
|
|
|
|
|
|
if (child != NULL && gtk_widget_get_visible (child))
|
2009-12-18 12:43:11 +00:00
|
|
|
{
|
|
|
|
GtkRequisition child_req;
|
|
|
|
|
2010-09-21 14:35:17 +00:00
|
|
|
gtk_widget_get_preferred_size (child, &child_req, NULL);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
requisition->width += child_req.width;
|
|
|
|
requisition->height += child_req.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_get_default_size (GTK_WINDOW (widget),
|
|
|
|
&default_width, &default_height);
|
|
|
|
if (default_width > 0)
|
|
|
|
requisition->width = default_width;
|
|
|
|
|
|
|
|
if (default_height > 0)
|
|
|
|
requisition->height = default_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_size_allocate (GtkWidget *widget,
|
|
|
|
GtkAllocation *allocation)
|
|
|
|
{
|
|
|
|
GtkBin *bin = GTK_BIN (widget);
|
2010-05-24 20:31:36 +00:00
|
|
|
GtkWidget *child;
|
2009-12-18 12:43:11 +00:00
|
|
|
gint border_width;
|
|
|
|
|
2010-08-11 21:06:16 +00:00
|
|
|
gtk_widget_set_allocation (widget, allocation);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
|
|
|
|
|
2010-03-02 06:16:02 +00:00
|
|
|
if (gtk_widget_get_realized (widget))
|
2010-08-11 21:06:16 +00:00
|
|
|
gdk_window_move_resize (gtk_widget_get_window (widget),
|
2009-12-18 12:43:11 +00:00
|
|
|
allocation->x,
|
|
|
|
allocation->y,
|
|
|
|
allocation->width,
|
|
|
|
allocation->height);
|
|
|
|
|
2010-05-24 20:31:36 +00:00
|
|
|
child = gtk_bin_get_child (bin);
|
2010-07-22 14:43:23 +00:00
|
|
|
|
|
|
|
if (child != NULL && gtk_widget_get_visible (child))
|
2009-12-18 12:43:11 +00:00
|
|
|
{
|
|
|
|
GtkAllocation child_alloc;
|
|
|
|
|
|
|
|
child_alloc.x = border_width;
|
|
|
|
child_alloc.y = border_width;
|
|
|
|
child_alloc.width = allocation->width - 2 * border_width;
|
|
|
|
child_alloc.height = allocation->height - 2 * border_width;
|
|
|
|
|
2010-05-24 20:31:36 +00:00
|
|
|
gtk_widget_size_allocate (child, &child_alloc);
|
2009-12-18 12:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_realize (GtkWidget *widget)
|
|
|
|
{
|
2010-08-11 21:06:16 +00:00
|
|
|
GtkAllocation allocation;
|
2009-12-18 12:43:11 +00:00
|
|
|
GtkBin *bin;
|
2010-05-24 20:31:36 +00:00
|
|
|
GtkWidget *child;
|
2010-08-11 21:06:16 +00:00
|
|
|
GdkWindow *window;
|
2009-12-18 12:43:11 +00:00
|
|
|
GdkWindowAttr attributes;
|
|
|
|
gint attributes_mask;
|
|
|
|
gint border_width;
|
|
|
|
|
|
|
|
bin = GTK_BIN (widget);
|
|
|
|
|
2010-03-06 10:51:33 +00:00
|
|
|
gtk_widget_set_realized (widget, TRUE);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
|
|
|
|
|
2010-08-11 21:06:16 +00:00
|
|
|
gtk_widget_get_allocation (widget, &allocation);
|
|
|
|
|
|
|
|
attributes.x = allocation.x;
|
|
|
|
attributes.y = allocation.y;
|
|
|
|
attributes.width = allocation.width;
|
|
|
|
attributes.height = allocation.height;
|
2009-12-18 12:43:11 +00:00
|
|
|
attributes.window_type = GDK_WINDOW_OFFSCREEN;
|
|
|
|
attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
|
|
|
|
attributes.visual = gtk_widget_get_visual (widget);
|
|
|
|
attributes.wclass = GDK_INPUT_OUTPUT;
|
|
|
|
|
2010-08-28 16:04:08 +00:00
|
|
|
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-08-11 21:06:16 +00:00
|
|
|
window = gdk_window_new (gtk_widget_get_parent_window (widget),
|
|
|
|
&attributes, attributes_mask);
|
|
|
|
gtk_widget_set_window (widget, window);
|
|
|
|
gdk_window_set_user_data (window, widget);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-05-24 20:31:36 +00:00
|
|
|
child = gtk_bin_get_child (bin);
|
|
|
|
if (child)
|
2010-08-11 21:06:16 +00:00
|
|
|
gtk_widget_set_parent_window (child, window);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-08-11 21:06:16 +00:00
|
|
|
gtk_widget_style_attach (widget);
|
|
|
|
gtk_style_set_background (gtk_widget_get_style (widget),
|
|
|
|
window, GTK_STATE_NORMAL);
|
2009-12-18 12:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_resize (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkAllocation allocation = { 0, 0 };
|
|
|
|
GtkRequisition requisition;
|
|
|
|
|
2010-09-21 14:35:17 +00:00
|
|
|
gtk_widget_get_preferred_size (widget, &requisition, NULL);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
allocation.width = requisition.width;
|
|
|
|
allocation.height = requisition.height;
|
|
|
|
gtk_widget_size_allocate (widget, &allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
move_focus (GtkWidget *widget,
|
|
|
|
GtkDirectionType dir)
|
|
|
|
{
|
|
|
|
gtk_widget_child_focus (widget, dir);
|
|
|
|
|
2010-06-02 04:28:22 +00:00
|
|
|
if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
|
2009-12-18 12:43:11 +00:00
|
|
|
gtk_window_set_focus (GTK_WINDOW (widget), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_show (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
gboolean need_resize;
|
|
|
|
GtkContainer *container;
|
|
|
|
|
2010-09-18 23:57:32 +00:00
|
|
|
_gtk_widget_set_visible_flag (widget, TRUE);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
container = GTK_CONTAINER (widget);
|
2010-06-02 22:28:58 +00:00
|
|
|
need_resize = _gtk_container_get_need_resize (container) || !gtk_widget_get_realized (widget);
|
|
|
|
_gtk_container_set_need_resize (container, FALSE);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
|
|
|
if (need_resize)
|
|
|
|
gtk_offscreen_window_resize (widget);
|
|
|
|
|
|
|
|
gtk_widget_map (widget);
|
|
|
|
|
|
|
|
/* Try to make sure that we have some focused widget */
|
|
|
|
if (!gtk_window_get_focus (GTK_WINDOW (widget)))
|
|
|
|
move_focus (widget, GTK_DIR_TAB_FORWARD);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_hide (GtkWidget *widget)
|
|
|
|
{
|
2010-09-18 23:57:32 +00:00
|
|
|
_gtk_widget_set_visible_flag (widget, FALSE);
|
2009-12-18 12:43:11 +00:00
|
|
|
gtk_widget_unmap (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_check_resize (GtkContainer *container)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (container);
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (gtk_widget_get_visible (widget))
|
2009-12-18 12:43:11 +00:00
|
|
|
gtk_offscreen_window_resize (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
|
|
|
|
{
|
|
|
|
GtkWidgetClass *widget_class;
|
|
|
|
GtkContainerClass *container_class;
|
|
|
|
|
|
|
|
widget_class = GTK_WIDGET_CLASS (class);
|
|
|
|
container_class = GTK_CONTAINER_CLASS (class);
|
|
|
|
|
|
|
|
widget_class->realize = gtk_offscreen_window_realize;
|
|
|
|
widget_class->show = gtk_offscreen_window_show;
|
|
|
|
widget_class->hide = gtk_offscreen_window_hide;
|
|
|
|
widget_class->size_request = gtk_offscreen_window_size_request;
|
|
|
|
widget_class->size_allocate = gtk_offscreen_window_size_allocate;
|
|
|
|
|
|
|
|
container_class->check_resize = gtk_offscreen_window_check_resize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-18 13:57:03 +00:00
|
|
|
gtk_offscreen_window_init (GtkOffscreenWindow *window)
|
2009-12-18 12:43:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-12-19 12:08:20 +00:00
|
|
|
/* --- functions --- */
|
2009-12-18 19:08:45 +00:00
|
|
|
/**
|
|
|
|
* gtk_offscreen_window_new:
|
|
|
|
*
|
|
|
|
* Creates a toplevel container widget that is used to retrieve
|
2010-08-24 15:04:18 +00:00
|
|
|
* snapshots of widgets without showing them on the screen.
|
2009-12-18 19:08:45 +00:00
|
|
|
*
|
|
|
|
* Return value: A pointer to a #GtkWidget
|
2009-12-28 15:23:05 +00:00
|
|
|
*
|
|
|
|
* Since: 2.20
|
2010-01-28 02:10:09 +00:00
|
|
|
*/
|
2009-12-18 12:43:11 +00:00
|
|
|
GtkWidget *
|
|
|
|
gtk_offscreen_window_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (gtk_offscreen_window_get_type (), NULL);
|
|
|
|
}
|
|
|
|
|
2009-12-18 19:08:45 +00:00
|
|
|
/**
|
2010-08-26 11:46:34 +00:00
|
|
|
* gtk_offscreen_window_get_surface:
|
2010-01-28 02:10:09 +00:00
|
|
|
* @offscreen: the #GtkOffscreenWindow contained widget.
|
2009-12-18 19:08:45 +00:00
|
|
|
*
|
|
|
|
* Retrieves a snapshot of the contained widget in the form of
|
2010-08-26 11:46:34 +00:00
|
|
|
* a #cairo_surface_t. If you need to keep this around over window
|
2009-12-18 19:08:45 +00:00
|
|
|
* resizes then you should add a reference to it.
|
|
|
|
*
|
2010-08-26 11:46:34 +00:00
|
|
|
* Returns: (transfer none): A #cairo_surface_t pointer to the offscreen
|
|
|
|
* surface, or %NULL.
|
2009-12-28 15:23:05 +00:00
|
|
|
*
|
|
|
|
* Since: 2.20
|
2010-01-28 02:10:09 +00:00
|
|
|
*/
|
2010-08-26 11:46:34 +00:00
|
|
|
cairo_surface_t *
|
|
|
|
gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
|
2009-12-18 19:08:45 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
|
|
|
|
|
2010-08-26 11:46:34 +00:00
|
|
|
return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
|
2009-12-18 19:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_offscreen_window_get_pixbuf:
|
2010-01-28 02:10:09 +00:00
|
|
|
* @offscreen: the #GtkOffscreenWindow contained widget.
|
2009-12-18 19:08:45 +00:00
|
|
|
*
|
|
|
|
* Retrieves a snapshot of the contained widget in the form of
|
|
|
|
* a #GdkPixbuf. This is a new pixbuf with a reference count of 1,
|
|
|
|
* and the application should unreference it once it is no longer
|
|
|
|
* needed.
|
|
|
|
*
|
2010-09-21 04:18:11 +00:00
|
|
|
* Returns: (transfer full): A #GdkPixbuf pointer, or %NULL.
|
2009-12-28 15:23:05 +00:00
|
|
|
*
|
|
|
|
* Since: 2.20
|
2010-01-28 02:10:09 +00:00
|
|
|
*/
|
2009-12-18 19:08:45 +00:00
|
|
|
GdkPixbuf *
|
|
|
|
gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
|
|
|
|
{
|
2010-08-26 11:46:34 +00:00
|
|
|
cairo_surface_t *surface;
|
2009-12-18 19:08:45 +00:00
|
|
|
GdkPixbuf *pixbuf = NULL;
|
2010-08-26 11:46:34 +00:00
|
|
|
GdkWindow *window;
|
2009-12-18 19:08:45 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
|
|
|
|
|
2010-08-26 11:46:34 +00:00
|
|
|
window = gtk_widget_get_window (GTK_WIDGET (offscreen));
|
|
|
|
surface = gdk_offscreen_window_get_surface (window);
|
2009-12-18 19:08:45 +00:00
|
|
|
|
2010-08-26 11:46:34 +00:00
|
|
|
if (surface != NULL)
|
2009-12-18 19:08:45 +00:00
|
|
|
{
|
2010-10-02 00:47:55 +00:00
|
|
|
pixbuf = gdk_pixbuf_get_from_surface (surface,
|
|
|
|
0, 0,
|
2010-09-20 14:39:41 +00:00
|
|
|
gdk_window_get_width (window),
|
|
|
|
gdk_window_get_height (window));
|
2009-12-18 19:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
}
|