gtk: Add gtk_widget_snapshot()

It's functionally equivalent to gtk_widget_get_render_node() but uses a
GtkSnapshot argument to carry the state.
This commit is contained in:
Benjamin Otte 2016-11-07 19:10:49 +01:00
parent f90dcb33f9
commit 93cbba6c3e
7 changed files with 207 additions and 19 deletions

View File

@ -293,6 +293,7 @@ gtk_public_h_sources = \
gtkstacksidebar.h \
gtksizegroup.h \
gtksizerequest.h \
gtksnapshot.h \
gtkspinbutton.h \
gtkspinner.h \
gtkstack.h \
@ -540,6 +541,7 @@ gtk_private_h_sources = \
gtksidebarrowprivate.h \
gtksizegroup-private.h \
gtksizerequestcacheprivate.h \
gtksnapshotprivate.h \
gtkstyleanimationprivate.h \
gtkstylecascadeprivate.h \
gtkstylecontextprivate.h \
@ -871,10 +873,11 @@ gtk_base_c_sources = \
gtksizerequest.c \
gtksizerequestcache.c \
gtkshow.c \
gtkstacksidebar.c \
gtksnapshot.c \
gtkspinbutton.c \
gtkspinner.c \
gtkstack.c \
gtkstacksidebar.c \
gtkstackswitcher.c \
gtkstatusbar.c \
gtkstyleanimation.c \

View File

@ -193,6 +193,7 @@
#include <gtk/gtkshortcutsshortcut.h>
#include <gtk/gtkshortcutswindow.h>
#include <gtk/gtkshow.h>
#include <gtk/gtksnapshot.h>
#include <gtk/gtkstacksidebar.h>
#include <gtk/gtksizegroup.h>
#include <gtk/gtksizerequest.h>

80
gtk/gtksnapshot.c Normal file
View File

@ -0,0 +1,80 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2016 Benjamin Otte <otte@gnome.org>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtksnapshot.h"
#include "gtksnapshotprivate.h"
void
gtk_snapshot_init (GtkSnapshot *state,
const GtkSnapshot *parent,
const graphene_matrix_t *transform)
{
state->parent = parent;
state->renderer = parent->renderer;
graphene_matrix_init_from_matrix (&state->transform, transform);
}
void
gtk_snapshot_init_root (GtkSnapshot *state,
GskRenderer *renderer)
{
state->parent = NULL;
state->renderer = renderer;
graphene_matrix_init_identity (&state->transform);
}
void
gtk_snapshot_finish (GtkSnapshot *state)
{
/* nothing to do so far */
}
GskRenderer *
gtk_snapshot_get_renderer (const GtkSnapshot *state)
{
return state->renderer;
}
GskRenderNode *
gtk_snapshot_create_render_node (const GtkSnapshot *state,
const char *name,
...)
{
GskRenderNode *node;
node = gsk_renderer_create_render_node (state->renderer);
if (name)
{
va_list args;
char *str;
va_start (args, name);
str = g_strdup_vprintf (name, args);
va_end (args);
gsk_render_node_set_name (node, str);
g_free (str);
}
return node;
}

50
gtk/gtksnapshot.h Normal file
View File

@ -0,0 +1,50 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2016 Benjamin Otte <otte@gnome.org>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_SNAPSHOT_H__
#define __GTK_SNAPSHOT_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gsk/gsk.h>
G_BEGIN_DECLS
typedef struct _GtkSnapshot GtkSnapshot;
GDK_AVAILABLE_IN_3_90
GskRenderer * gtk_snapshot_get_renderer (const GtkSnapshot *state);
GDK_AVAILABLE_IN_3_90
GskRenderNode * gtk_snapshot_create_render_node (const GtkSnapshot *state,
const char *name,
...) G_GNUC_PRINTF(2, 3);
G_END_DECLS
#endif /* __GTK_SNAPSHOT_H__ */

43
gtk/gtksnapshotprivate.h Normal file
View File

@ -0,0 +1,43 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2016 Benjamin Otte <otte@gnome.org>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_SNAPSHOT_PRIVATE_H__
#define __GTK_SNAPSHOT_PRIVATE_H__
#include "gtksnapshot.h"
G_BEGIN_DECLS
struct _GtkSnapshot {
const GtkSnapshot *parent;
GskRenderer *renderer;
graphene_matrix_t transform;
};
void gtk_snapshot_init (GtkSnapshot *state,
const GtkSnapshot *parent,
const graphene_matrix_t *transform);
void gtk_snapshot_init_root (GtkSnapshot *state,
GskRenderer *renderer);
void gtk_snapshot_finish (GtkSnapshot *state);
G_END_DECLS
#endif /* __GTK_SNAPSHOT_PRIVATE_H__ */

View File

@ -67,6 +67,7 @@
#include "gtkapplicationprivate.h"
#include "gtkgestureprivate.h"
#include "gtkwidgetpathprivate.h"
#include "gtksnapshotprivate.h"
#include "inspector/window.h"
@ -15627,6 +15628,22 @@ gtk_widget_create_render_node (GtkWidget *widget,
GskRenderNode *
gtk_widget_get_render_node (GtkWidget *widget,
GskRenderer *renderer)
{
GtkSnapshot snapshot;
GskRenderNode *node;
gtk_snapshot_init_root (&snapshot, renderer);
node = gtk_widget_snapshot (widget, &snapshot);
gtk_snapshot_finish (&snapshot);
return node;
}
GskRenderNode *
gtk_widget_snapshot (GtkWidget *widget,
const GtkSnapshot *snapshot)
{
GtkWidgetClass *klass = GTK_WIDGET_GET_CLASS (widget);
GskRenderNode *node;
@ -15651,27 +15668,21 @@ gtk_widget_get_render_node (GtkWidget *widget,
{
GskRenderNode *tmp;
cairo_t *cr;
char *str;
str = g_strconcat ("Fallback<", G_OBJECT_TYPE_NAME (widget), ">", NULL);
tmp = gsk_renderer_create_render_node (renderer);
gsk_render_node_set_name (tmp, str);
tmp = gtk_snapshot_create_render_node (snapshot, "Fallback<%s>", G_OBJECT_TYPE_NAME (widget));
gsk_render_node_set_bounds (tmp, &bounds);
gsk_render_node_set_transform (tmp, &m);
cr = gsk_render_node_get_draw_context (tmp, renderer);
cr = gsk_render_node_get_draw_context (tmp, gtk_snapshot_get_renderer (snapshot));
cairo_translate (cr, alloc.x - clip.x, alloc.y - clip.y);
gtk_widget_draw_internal (widget, cr, TRUE);
cairo_destroy (cr);
g_free (str);
node = tmp;
}
else
{
node = klass->get_render_node (widget, renderer);
node = klass->get_render_node (widget, gtk_snapshot_get_renderer (snapshot));
/* Compatibility mode: if there's a ::draw signal handler, we add a
* child node with the contents of the handler
@ -15681,21 +15692,15 @@ gtk_widget_get_render_node (GtkWidget *widget,
GskRenderNode *tmp;
gboolean result;
cairo_t *cr;
char *str;
str = g_strconcat ("DrawSignal<", G_OBJECT_TYPE_NAME (widget), ">", NULL);
tmp = gsk_renderer_create_render_node (renderer);
gsk_render_node_set_name (tmp, str);
tmp = gtk_snapshot_create_render_node (snapshot, "DrawSignal<%s>", G_OBJECT_TYPE_NAME (widget));
gsk_render_node_set_bounds (tmp, &bounds);
cr = gsk_render_node_get_draw_context (tmp, renderer);
cr = gsk_render_node_get_draw_context (tmp, gtk_snapshot_get_renderer (snapshot));
cairo_translate (cr, alloc.x - clip.x, alloc.y - clip.y);
g_signal_emit (widget, widget_signals[DRAW], 0, cr, &result);
cairo_destroy (cr);
g_free (str);
if (node != NULL)
{
gsk_render_node_append_child (node, tmp);
@ -15717,6 +15722,7 @@ gtk_widget_render (GtkWidget *widget,
const cairo_region_t *region)
{
GdkDrawingContext *context;
GtkSnapshot snapshot;
GskRenderer *renderer;
GskRenderNode *root;
@ -15728,7 +15734,9 @@ gtk_widget_render (GtkWidget *widget,
if (renderer == NULL)
return;
root = gtk_widget_get_render_node (widget, renderer);
gtk_snapshot_init_root (&snapshot, renderer);
root = gtk_widget_snapshot (widget, &snapshot);
gtk_snapshot_finish (&snapshot);
if (root == NULL)
return;

View File

@ -31,6 +31,7 @@
#include "gtkeventcontroller.h"
#include "gtkactionmuxer.h"
#include "gtksizerequestcacheprivate.h"
#include "gtksnapshot.h"
G_BEGIN_DECLS
@ -285,6 +286,8 @@ void gtk_widget_render (GtkWidget
GskRenderNode * gtk_widget_get_render_node (GtkWidget *widget,
GskRenderer *renderer);
GskRenderNode * gtk_widget_snapshot (GtkWidget *widget,
const GtkSnapshot *snapshot);
GskRenderNode * gtk_widget_create_render_node (GtkWidget *widget,
GskRenderer *renderer,