gtk2/gtk/gtkoffscreenwindow.c

338 lines
9.3 KiB
C
Raw Normal View History

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>
*/
#include "config.h"
#include "gtkoffscreenwindow.h"
#include "gtkwidgetprivate.h"
#include "gtkcontainerprivate.h"
2010-09-28 16:24:55 +00:00
#include "gtkprivate.h"
/**
* SECTION:gtkoffscreenwindow
2010-09-25 02:13:42 +00:00
* @short_description: A toplevel to manage offscreen rendering of child widgets
* @title: GtkOffscreenWindow
*
2010-09-25 02:13:42 +00:00
* GtkOffscreenWindow is strictly intended to be used for obtaining
* snapshots of widgets that are not part of a normal widget hierarchy.
* 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-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
* as a #cairo_surface_t or #GdkPixbuf.
*
2010-09-25 02:13:42 +00:00
* GtkOffscreenWindow derives from #GtkWindow only as an implementation
* 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.
*
2010-09-25 02:13:42 +00:00
* When contained offscreen widgets are redrawn, GtkOffscreenWindow
* will emit a #GtkWidget::damage-event signal.
*/
G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
static void
gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
gint *minimum,
gint *natural)
{
GtkBin *bin = GTK_BIN (widget);
2010-05-24 20:31:36 +00:00
GtkWidget *child;
gint default_width;
*minimum = 0;
*natural = 0;
2010-05-24 20:31:36 +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_width (child, &child_min, &child_nat);
*minimum += child_min;
*natural += child_nat;
}
gtk_window_get_default_size (GTK_WINDOW (widget),
&default_width, NULL);
*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;
*minimum = 0;
*natural = 0;
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);
}
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;
gtk_widget_set_allocation (widget, allocation);
if (gtk_widget_get_realized (widget))
gdk_window_move_resize (gtk_widget_get_window (widget),
allocation->x,
allocation->y,
allocation->width,
allocation->height);
2010-05-24 20:31:36 +00:00
child = gtk_bin_get_child (bin);
if (child != NULL && gtk_widget_get_visible (child))
{
GtkAllocation child_alloc;
child_alloc.x = 0;
child_alloc.y = 0;
child_alloc.width = allocation->width;
child_alloc.height = allocation->height;
2010-05-24 20:31:36 +00:00
gtk_widget_size_allocate (child, &child_alloc);
}
gtk_widget_queue_draw (widget);
}
static void
gtk_offscreen_window_realize (GtkWidget *widget)
{
GtkAllocation allocation;
GtkBin *bin;
2010-05-24 20:31:36 +00:00
GtkWidget *child;
GdkWindow *window;
GdkWindowAttr attributes;
gint attributes_mask;
bin = GTK_BIN (widget);
gtk_widget_set_realized (widget, TRUE);
gtk_widget_get_allocation (widget, &allocation);
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.window_type = GDK_WINDOW_OFFSCREEN;
attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes_mask = GDK_WA_X | GDK_WA_Y;
window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
gtk_widget_set_window (widget, window);
gtk_widget_register_window (widget, window);
2010-05-24 20:31:36 +00:00
child = gtk_bin_get_child (bin);
if (child)
gtk_widget_set_parent_window (child, window);
}
static void
gtk_offscreen_window_resize (GtkWidget *widget)
{
GtkAllocation allocation = { 0, 0 };
GtkRequisition requisition;
gtk_widget_get_preferred_size (widget, &requisition, NULL);
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);
if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
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);
need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
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);
gtk_widget_unmap (widget);
}
static void
gtk_offscreen_window_check_resize (GtkContainer *container)
{
GtkWidget *widget = GTK_WIDGET (container);
if (gtk_widget_get_visible (widget))
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->get_preferred_width = gtk_offscreen_window_get_preferred_width;
widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
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)
{
}
/* --- functions --- */
/**
* gtk_offscreen_window_new:
*
* Creates a toplevel container widget that is used to retrieve
* snapshots of widgets without showing them on the screen.
*
* Returns: A pointer to a #GtkWidget
2009-12-28 15:23:05 +00:00
*
* Since: 2.20
*/
GtkWidget *
gtk_offscreen_window_new (void)
{
return g_object_new (gtk_offscreen_window_get_type (), NULL);
}
/**
* gtk_offscreen_window_get_surface:
* @offscreen: the #GtkOffscreenWindow contained widget.
*
* Retrieves a snapshot of the contained widget in the form of
* a #cairo_surface_t. If you need to keep this around over window
* resizes then you should add a reference to it.
*
introspection: This patch fixes nullable return values fixes for the following symbols in gtk gtk_accel_group_query gtk_accel_group_from_accel_closure gtk_accel_label_get_accel_widget gtk_accessible_get_widget gtk_actionable_get_action_name gtk_app_chooser_get_app_info gtk_app_chooser_button_get_heading gtk_app_chooser_dialog_get_heading gtk_application_get_window_by_id gtk_assistant_get_nth_page gtk_binding_set_find gtk_builder_get_object gtk_builder_lookup_callback_symbol gtk_builder_get_application gtk_button_get_image gtk_cell_area_get_focus_from_sibling gtk_cell_renderer_start_editing gtk_cell_view_get_model gtk_cell_view_get_displayed_row gtk_clipboard_get_owner gtk_container_get_focus_child gtk_container_get_focus_vadjustment gtk_container_get_focus_hadjustment gtk_dialog_get_widget_for_response gtk_drag_get_source_widget gtk_drag_dest_get_target_list gtk_drag_source_get_target_list gtk_entry_completion_get_model gtk_entry_completion_compute_prefix gtk_expander_get_label_widget gtk_file_chooser_get_filename gtk_file_chooser_get_current_folder gtk_file_chooser_get_uri gtk_file_chooser_get_current_folder_uri gtk_file_chooser_get_preview_widget gtk_file_chooser_get_preview_file gtk_file_chooser_get_preview_filename gtk_file_chooser_get_preview_uri gtk_file_chooser_get_extra_widget gtk_file_chooser_get_filter gtk_file_chooser_native_get_accept_label gtk_file_chooser_native_get_cancel_label gtk_file_filter_get_name gtk_font_chooser_get_font_family gtk_font_chooser_get_font_face gtk_font_chooser_get_font gtk_font_chooser_get_font_desc gtk_font_chooser_get_font_map gtk_frame_get_label gtk_gesture_get_device gtk_gesture_get_window gtk_gl_area_get_error gtk_header_bar_get_title gtk_header_bar_get_subtitle gtk_header_bar_get_custom_title gtk_icon_info_get_filename gtk_icon_view_get_path_at_pos gtk_icon_view_get_model gtk_image_get_pixbuf gtk_image_get_animation gtk_label_get_mnemonic_widget gtk_label_get_attributes gtk_check_version gtk_menu_button_get_popup gtk_menu_button_get_menu_model gtk_menu_button_get_align_widget gtk_menu_button_get_popover gtk_menu_item_get_submenu gtk_menu_item_get_accel_path gtk_native_dialog_get_title gtk_native_dialog_get_transient_for gtk_notebook_get_nth_page gtk_notebook_get_tab_label_text gtk_notebook_get_menu_label gtk_notebook_get_menu_label_text gtk_notebook_get_group_name gtk_notebook_get_action_widget gtk_offscreen_window_get_surface gtk_offscreen_window_get_pixbuf gtk_paned_get_child1 gtk_paned_get_child2 gtk_places_sidebar_get_location gtk_places_sidebar_get_nth_bookmark gtk_plug_get_socket_window gtk_popover_get_default_widget gtk_progress_bar_get_text gtk_recent_filter_get_name gtk_recent_manager_lookup_item gtk_settings_get_default gtk_socket_get_plug_window gtk_stack_sidebar_get_stack gtk_stack_switcher_get_stack gtk_style_context_get_section gtk_style_context_get_parent gtk_style_context_get_frame_clock gtk_test_find_widget gtk_text_buffer_get_mark gtk_text_tag_table_lookup gtk_text_view_get_tabs gtk_text_view_toggle_cursor_visible gtk_text_view_get_window gtk_toolbar_get_nth_item gtk_tool_button_get_label gtk_tool_button_get_icon_name gtk_tool_button_get_label_widget gtk_tool_button_get_icon_widget gtk_tool_palette_get_drop_item gtk_tool_palette_get_drop_group gtk_tree_model_filter_convert_child_path_to_path gtk_tree_model_filter_convert_path_to_child_path gtk_tree_model_sort_convert_child_path_to_path gtk_tree_model_sort_convert_path_to_child_path gtk_tree_view_get_column gtk_tree_view_get_bin_window gtk_tree_view_column_get_widget gtk_tree_view_column_get_tree_view gtk_widget_get_frame_clock gtk_window_group_get_current_device_grab GtkTextBufferSerializeFunc
2015-12-28 20:14:08 +00:00
* Returns: (nullable) (transfer none): A #cairo_surface_t pointer to the offscreen
* surface, or %NULL.
2009-12-28 15:23:05 +00:00
*
* Since: 2.20
*/
cairo_surface_t *
gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
{
g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
}
/**
* gtk_offscreen_window_get_pixbuf:
* @offscreen: the #GtkOffscreenWindow contained widget.
*
* 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.
*
introspection: This patch fixes nullable return values fixes for the following symbols in gtk gtk_accel_group_query gtk_accel_group_from_accel_closure gtk_accel_label_get_accel_widget gtk_accessible_get_widget gtk_actionable_get_action_name gtk_app_chooser_get_app_info gtk_app_chooser_button_get_heading gtk_app_chooser_dialog_get_heading gtk_application_get_window_by_id gtk_assistant_get_nth_page gtk_binding_set_find gtk_builder_get_object gtk_builder_lookup_callback_symbol gtk_builder_get_application gtk_button_get_image gtk_cell_area_get_focus_from_sibling gtk_cell_renderer_start_editing gtk_cell_view_get_model gtk_cell_view_get_displayed_row gtk_clipboard_get_owner gtk_container_get_focus_child gtk_container_get_focus_vadjustment gtk_container_get_focus_hadjustment gtk_dialog_get_widget_for_response gtk_drag_get_source_widget gtk_drag_dest_get_target_list gtk_drag_source_get_target_list gtk_entry_completion_get_model gtk_entry_completion_compute_prefix gtk_expander_get_label_widget gtk_file_chooser_get_filename gtk_file_chooser_get_current_folder gtk_file_chooser_get_uri gtk_file_chooser_get_current_folder_uri gtk_file_chooser_get_preview_widget gtk_file_chooser_get_preview_file gtk_file_chooser_get_preview_filename gtk_file_chooser_get_preview_uri gtk_file_chooser_get_extra_widget gtk_file_chooser_get_filter gtk_file_chooser_native_get_accept_label gtk_file_chooser_native_get_cancel_label gtk_file_filter_get_name gtk_font_chooser_get_font_family gtk_font_chooser_get_font_face gtk_font_chooser_get_font gtk_font_chooser_get_font_desc gtk_font_chooser_get_font_map gtk_frame_get_label gtk_gesture_get_device gtk_gesture_get_window gtk_gl_area_get_error gtk_header_bar_get_title gtk_header_bar_get_subtitle gtk_header_bar_get_custom_title gtk_icon_info_get_filename gtk_icon_view_get_path_at_pos gtk_icon_view_get_model gtk_image_get_pixbuf gtk_image_get_animation gtk_label_get_mnemonic_widget gtk_label_get_attributes gtk_check_version gtk_menu_button_get_popup gtk_menu_button_get_menu_model gtk_menu_button_get_align_widget gtk_menu_button_get_popover gtk_menu_item_get_submenu gtk_menu_item_get_accel_path gtk_native_dialog_get_title gtk_native_dialog_get_transient_for gtk_notebook_get_nth_page gtk_notebook_get_tab_label_text gtk_notebook_get_menu_label gtk_notebook_get_menu_label_text gtk_notebook_get_group_name gtk_notebook_get_action_widget gtk_offscreen_window_get_surface gtk_offscreen_window_get_pixbuf gtk_paned_get_child1 gtk_paned_get_child2 gtk_places_sidebar_get_location gtk_places_sidebar_get_nth_bookmark gtk_plug_get_socket_window gtk_popover_get_default_widget gtk_progress_bar_get_text gtk_recent_filter_get_name gtk_recent_manager_lookup_item gtk_settings_get_default gtk_socket_get_plug_window gtk_stack_sidebar_get_stack gtk_stack_switcher_get_stack gtk_style_context_get_section gtk_style_context_get_parent gtk_style_context_get_frame_clock gtk_test_find_widget gtk_text_buffer_get_mark gtk_text_tag_table_lookup gtk_text_view_get_tabs gtk_text_view_toggle_cursor_visible gtk_text_view_get_window gtk_toolbar_get_nth_item gtk_tool_button_get_label gtk_tool_button_get_icon_name gtk_tool_button_get_label_widget gtk_tool_button_get_icon_widget gtk_tool_palette_get_drop_item gtk_tool_palette_get_drop_group gtk_tree_model_filter_convert_child_path_to_path gtk_tree_model_filter_convert_path_to_child_path gtk_tree_model_sort_convert_child_path_to_path gtk_tree_model_sort_convert_path_to_child_path gtk_tree_view_get_column gtk_tree_view_get_bin_window gtk_tree_view_column_get_widget gtk_tree_view_column_get_tree_view gtk_widget_get_frame_clock gtk_window_group_get_current_device_grab GtkTextBufferSerializeFunc
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
*/
GdkPixbuf *
gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
{
cairo_surface_t *surface;
GdkPixbuf *pixbuf = NULL;
GdkWindow *window;
g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
window = gtk_widget_get_window (GTK_WIDGET (offscreen));
surface = gdk_offscreen_window_get_surface (window);
if (surface != NULL)
{
pixbuf = gdk_pixbuf_get_from_surface (surface,
0, 0,
gdk_window_get_width (window),
gdk_window_get_height (window));
}
return pixbuf;
}