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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2009-12-18 13:57:03 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2011-06-05 23:31:59 +00:00
|
|
|
#include "gtkcontainerprivate.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
|
2010-10-27 05:43:30 +00:00
|
|
|
gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
|
|
|
|
gint *minimum,
|
|
|
|
gint *natural)
|
2009-12-18 12:43:11 +00:00
|
|
|
{
|
|
|
|
GtkBin *bin = GTK_BIN (widget);
|
2010-05-24 20:31:36 +00:00
|
|
|
GtkWidget *child;
|
2010-10-27 05:43:30 +00:00
|
|
|
gint default_width;
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2016-10-09 00:32:00 +00:00
|
|
|
*minimum = 0;
|
|
|
|
*natural = 0;
|
2009-12-18 12:43:11 +00:00
|
|
|
|
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
|
|
|
{
|
2010-10-27 05:43:30 +00:00
|
|
|
gint child_min, child_nat;
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-10-27 05:43:30 +00:00
|
|
|
gtk_widget_get_preferred_width (child, &child_min, &child_nat);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-10-27 05:43:30 +00:00
|
|
|
*minimum += child_min;
|
|
|
|
*natural += child_nat;
|
2009-12-18 12:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_get_default_size (GTK_WINDOW (widget),
|
2010-10-27 05:43:30 +00:00
|
|
|
&default_width, NULL);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2010-10-27 05:43:30 +00:00
|
|
|
*minimum = MAX (*minimum, default_width);
|
|
|
|
*natural = MAX (*natural, default_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
|
|
|
|
gint *minimum,
|
|
|
|
gint *natural)
|
|
|
|
{
|
|
|
|
GtkBin *bin = GTK_BIN (widget);
|
|
|
|
GtkWidget *child;
|
|
|
|
gint default_height;
|
|
|
|
|
2016-10-09 00:32:00 +00:00
|
|
|
*minimum = 0;
|
|
|
|
*natural = 0;
|
2010-10-27 05:43:30 +00:00
|
|
|
|
|
|
|
child = gtk_bin_get_child (bin);
|
|
|
|
|
|
|
|
if (child != NULL && gtk_widget_get_visible (child))
|
|
|
|
{
|
|
|
|
gint child_min, child_nat;
|
|
|
|
|
|
|
|
gtk_widget_get_preferred_height (child, &child_min, &child_nat);
|
|
|
|
|
|
|
|
*minimum += child_min;
|
|
|
|
*natural += child_nat;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_get_default_size (GTK_WINDOW (widget),
|
|
|
|
NULL, &default_height);
|
|
|
|
|
|
|
|
*minimum = MAX (*minimum, default_height);
|
|
|
|
*natural = MAX (*natural, default_height);
|
2009-12-18 12:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-08-11 21:06:16 +00:00
|
|
|
gtk_widget_set_allocation (widget, allocation);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
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;
|
|
|
|
|
2016-10-09 00:32:00 +00:00
|
|
|
child_alloc.x = 0;
|
|
|
|
child_alloc.y = 0;
|
|
|
|
child_alloc.width = allocation->width;
|
|
|
|
child_alloc.height = allocation->height;
|
2009-12-18 12:43:11 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
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.wclass = GDK_INPUT_OUTPUT;
|
|
|
|
|
2016-10-06 14:02:39 +00:00
|
|
|
attributes_mask = GDK_WA_X | GDK_WA_Y;
|
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);
|
2013-02-05 08:36:49 +00:00
|
|
|
gtk_widget_register_window (widget, window);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-09-18 23:57:32 +00:00
|
|
|
_gtk_widget_set_visible_flag (widget, TRUE);
|
2009-12-18 12:43:11 +00:00
|
|
|
|
2012-04-09 18:53:37 +00:00
|
|
|
need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
|
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;
|
2010-10-27 05:43:30 +00:00
|
|
|
widget_class->get_preferred_width = gtk_offscreen_window_get_preferred_width;
|
|
|
|
widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
|
2009-12-18 12:43:11 +00:00
|
|
|
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
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
* Returns: 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.
|
|
|
|
*
|
2015-12-28 20:14:08 +00:00
|
|
|
* Returns: (nullable) (transfer none): A #cairo_surface_t pointer to the offscreen
|
2010-08-26 11:46:34 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-12-28 20:14:08 +00:00
|
|
|
* Returns: (nullable) (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;
|
|
|
|
}
|