1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* Copyright © 2018 Benjamin Otte
*
* 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.1 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/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "gtkloaderprivate.h"
#include <gtk/gtk.h>
enum {
PROP_RESOURCE = 1,
};
struct _GtkLoader
{
GObject parent_instance;
GdkTexture *texture;
};
struct _GtkLoaderClass
{
GObjectClass parent_class;
};
static void
gtk_loader_paintable_snapshot (GdkPaintable *paintable,
GdkSnapshot *snapshot,
double width,
double height)
{
GtkLoader *self = GTK_LOADER (paintable);
if (self->texture)
gdk_paintable_snapshot (GDK_PAINTABLE (self->texture), snapshot, width, height);
}
static GdkPaintable *
gtk_loader_paintable_get_current_image (GdkPaintable *paintable)
{
GtkLoader *self = GTK_LOADER (paintable);
if (self->texture)
return gdk_paintable_get_current_image (GDK_PAINTABLE (self->texture));
// FIXME: return a loading image
return NULL;
}
static int
gtk_loader_paintable_get_intrinsic_width (GdkPaintable *paintable)
{
GtkLoader *self = GTK_LOADER (paintable);
if (self->texture)
return gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (self->texture));
return 0;
}
static int
gtk_loader_paintable_get_intrinsic_height (GdkPaintable *paintable)
{
GtkLoader *self = GTK_LOADER (paintable);
if (self->texture)
return gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (self->texture));
return 0;
}
static double
gtk_loader_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
{
GtkLoader *self = GTK_LOADER (paintable);
if (self->texture)
return gdk_paintable_get_intrinsic_aspect_ratio (GDK_PAINTABLE (self->texture));
return 0;
};
static void
gtk_loader_paintable_init (GdkPaintableInterface *iface)
{
iface->snapshot = gtk_loader_paintable_snapshot;
iface->get_current_image = gtk_loader_paintable_get_current_image;
iface->get_intrinsic_width = gtk_loader_paintable_get_intrinsic_width;
iface->get_intrinsic_height = gtk_loader_paintable_get_intrinsic_height;
iface->get_intrinsic_aspect_ratio = gtk_loader_paintable_get_intrinsic_aspect_ratio;
}
G_DEFINE_TYPE_EXTENDED (GtkLoader, gtk_loader, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
gtk_loader_paintable_init))
static void
gtk_loader_dispose (GObject *object)
{
GtkLoader *self = GTK_LOADER (object);
g_clear_object (&self->texture);
G_OBJECT_CLASS (gtk_loader_parent_class)->dispose (object);
}
static void gtk_loader_set_resource (GtkLoader *self, const char *resource);
static void
gtk_loader_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkLoader *self = (GtkLoader *)object;
switch (prop_id)
{
case PROP_RESOURCE:
gtk_loader_set_resource (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_loader_class_init (GtkLoaderClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gtk_loader_dispose;
gobject_class->set_property = gtk_loader_set_property;
g_object_class_install_property (gobject_class, PROP_RESOURCE,
g_param_spec_string ("resource", "", "",
NULL,
G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY));
}
static void
gtk_loader_init (GtkLoader *self)
{
}
static void
load_texture_in_thread (GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable)
{
const char *resource = task_data;
GdkTexture *texture;
GError *error = NULL;
texture = gdk_texture_new_from_resource (resource);
if (texture)
g_task_return_pointer (task, texture, g_object_unref);
else
g_task_return_error (task, error);
}
static void
texture_finished (GObject *source,
GAsyncResult *result,
gpointer data)
{
GtkLoader *self = GTK_LOADER (source);
GdkTexture *texture;
GError *error = NULL;
texture = g_task_propagate_pointer (G_TASK (result), &error);
if (texture)
{
self->texture = g_object_ref (texture);
gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
}
}
static void
gtk_loader_set_resource (GtkLoader *self,
const char *resource)
{
GTask *task;
task = g_task_new (self, NULL, texture_finished, NULL);
g_task_set_task_data (task, g_strdup (resource), (GDestroyNotify)g_free);
g_task_run_in_thread (task, load_texture_in_thread);
g_object_unref (task);
}
GdkPaintable *
gtk_loader_new (void)
{
return g_object_new (GTK_TYPE_LOADER, NULL);
}
|