1/*
2 * Copyright © 2018 Benjamin Otte
3 *
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.1 of the License, or (at your option) any later version.
8 *
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.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <otte@gnome.org>
18 */
19
20#include "config.h"
21
22#include "gtkrendernodepaintableprivate.h"
23
24#include "gtksnapshot.h"
25
26struct _GtkRenderNodePaintable
27{
28 GObject parent_instance;
29
30 GskRenderNode *node;
31 graphene_rect_t bounds;
32};
33
34struct _GtkRenderNodePaintableClass
35{
36 GObjectClass parent_class;
37};
38
39static void
40gtk_render_node_paintable_paintable_snapshot (GdkPaintable *paintable,
41 GdkSnapshot *snapshot,
42 double width,
43 double height)
44{
45 GtkRenderNodePaintable *self = GTK_RENDER_NODE_PAINTABLE (ptr: paintable);
46
47 if (self->bounds.size.width <= 0 ||
48 self->bounds.size.height <= 0)
49 return;
50
51 gtk_snapshot_save (snapshot);
52
53 gtk_snapshot_scale (snapshot,
54 factor_x: width / (self->bounds.size.width),
55 factor_y: height / (self->bounds.size.height));
56 gtk_snapshot_translate (snapshot, point: &GRAPHENE_POINT_INIT (-self->bounds.origin.x, -self->bounds.origin.y));
57
58 gtk_snapshot_push_clip (snapshot, bounds: &self->bounds);
59
60 gtk_snapshot_append_node (snapshot, node: self->node);
61 //gtk_snapshot_append_color (snapshot, &(GdkRGBA) { 1, 0, 0, 1 }, &self->bounds);
62
63 gtk_snapshot_pop (snapshot);
64
65 gtk_snapshot_restore (snapshot);
66}
67
68static GdkPaintableFlags
69gtk_render_node_paintable_paintable_get_flags (GdkPaintable *paintable)
70{
71 return GDK_PAINTABLE_STATIC_CONTENTS | GDK_PAINTABLE_STATIC_SIZE;
72}
73
74static int
75gtk_render_node_paintable_paintable_get_intrinsic_width (GdkPaintable *paintable)
76{
77 GtkRenderNodePaintable *self = GTK_RENDER_NODE_PAINTABLE (ptr: paintable);
78
79 return ceilf (x: self->bounds.size.width);
80}
81
82static int
83gtk_render_node_paintable_paintable_get_intrinsic_height (GdkPaintable *paintable)
84{
85 GtkRenderNodePaintable *self = GTK_RENDER_NODE_PAINTABLE (ptr: paintable);
86
87 return ceilf (x: self->bounds.size.height);
88}
89
90static double
91gtk_render_node_paintable_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
92{
93 GtkRenderNodePaintable *self = GTK_RENDER_NODE_PAINTABLE (ptr: paintable);
94
95 if (self->bounds.size.height != 0)
96 return self->bounds.size.width / self->bounds.size.height;
97
98 return 0;
99}
100
101static void
102gtk_render_node_paintable_paintable_init (GdkPaintableInterface *iface)
103{
104 iface->snapshot = gtk_render_node_paintable_paintable_snapshot;
105 iface->get_flags = gtk_render_node_paintable_paintable_get_flags;
106 iface->get_intrinsic_width = gtk_render_node_paintable_paintable_get_intrinsic_width;
107 iface->get_intrinsic_height = gtk_render_node_paintable_paintable_get_intrinsic_height;
108 iface->get_intrinsic_aspect_ratio = gtk_render_node_paintable_paintable_get_intrinsic_aspect_ratio;
109}
110
111G_DEFINE_TYPE_EXTENDED (GtkRenderNodePaintable, gtk_render_node_paintable, G_TYPE_OBJECT, 0,
112 G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
113 gtk_render_node_paintable_paintable_init))
114
115static void
116gtk_render_node_paintable_dispose (GObject *object)
117{
118 GtkRenderNodePaintable *self = GTK_RENDER_NODE_PAINTABLE (ptr: object);
119
120 g_clear_pointer (&self->node, gsk_render_node_unref);
121
122 G_OBJECT_CLASS (gtk_render_node_paintable_parent_class)->dispose (object);
123}
124
125static void
126gtk_render_node_paintable_class_init (GtkRenderNodePaintableClass *klass)
127{
128 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
129
130 gobject_class->dispose = gtk_render_node_paintable_dispose;
131}
132
133static void
134gtk_render_node_paintable_init (GtkRenderNodePaintable *self)
135{
136}
137
138GdkPaintable *
139gtk_render_node_paintable_new (GskRenderNode *node,
140 const graphene_rect_t *bounds)
141{
142 GtkRenderNodePaintable *self;
143
144 g_return_val_if_fail (GSK_IS_RENDER_NODE (node), NULL);
145 g_return_val_if_fail (bounds != NULL, NULL);
146
147 self = g_object_new (GTK_TYPE_RENDER_NODE_PAINTABLE, NULL);
148
149 self->node = gsk_render_node_ref (node);
150 self->bounds = *bounds;
151
152 return GDK_PAINTABLE (ptr: self);
153}
154
155GskRenderNode *
156gtk_render_node_paintable_get_render_node (GtkRenderNodePaintable *self)
157{
158 g_return_val_if_fail (GTK_IS_RENDER_NODE_PAINTABLE (self), NULL);
159
160 return self->node;
161}
162

source code of gtk/gtk/gtkrendernodepaintable.c