summaryrefslogtreecommitdiff
path: root/gtk/gtkimagedefinition.c
blob: 1d6e7d41c9a8bf11ac3f2a8b46c62914dec361d0 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* GTK - The GIMP Toolkit
 * Copyright (C) 2015 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 "gtkimagedefinitionprivate.h"

#include "deprecated/gtkiconfactory.h"

typedef struct _GtkImageDefinitionEmpty GtkImageDefinitionEmpty;
typedef struct _GtkImageDefinitionPixbuf GtkImageDefinitionPixbuf;
typedef struct _GtkImageDefinitionStock GtkImageDefinitionStock;
typedef struct _GtkImageDefinitionIconSet GtkImageDefinitionIconSet;
typedef struct _GtkImageDefinitionAnimation GtkImageDefinitionAnimation;
typedef struct _GtkImageDefinitionIconName GtkImageDefinitionIconName;
typedef struct _GtkImageDefinitionGIcon GtkImageDefinitionGIcon;
typedef struct _GtkImageDefinitionSurface GtkImageDefinitionSurface;

struct _GtkImageDefinitionEmpty {
  GtkImageType type;
  gint ref_count;
};

struct _GtkImageDefinitionPixbuf {
  GtkImageType type;
  gint ref_count;

  GdkPixbuf *pixbuf;
  int scale;
};

struct _GtkImageDefinitionStock {
  GtkImageType type;
  gint ref_count;

  char *id;
};

struct _GtkImageDefinitionIconSet {
  GtkImageType type;
  gint ref_count;

  GtkIconSet *icon_set;
};

struct _GtkImageDefinitionAnimation {
  GtkImageType type;
  gint ref_count;

  GdkPixbufAnimation *animation;
  int scale;
};

struct _GtkImageDefinitionIconName {
  GtkImageType type;
  gint ref_count;

  char *icon_name;
};

struct _GtkImageDefinitionGIcon {
  GtkImageType type;
  gint ref_count;

  GIcon *gicon;
};

struct _GtkImageDefinitionSurface {
  GtkImageType type;
  gint ref_count;

  cairo_surface_t *surface;
};

union _GtkImageDefinition
{
  GtkImageType type;
  GtkImageDefinitionEmpty empty;
  GtkImageDefinitionPixbuf pixbuf;
  GtkImageDefinitionStock stock;
  GtkImageDefinitionIconSet icon_set;
  GtkImageDefinitionAnimation animation;
  GtkImageDefinitionIconName icon_name;
  GtkImageDefinitionGIcon gicon;
  GtkImageDefinitionSurface surface;
};

GtkImageDefinition *
gtk_image_definition_new_empty (void)
{
  static GtkImageDefinitionEmpty empty = { GTK_IMAGE_EMPTY, 1 };

  return gtk_image_definition_ref ((GtkImageDefinition *) &empty);
}

static inline GtkImageDefinition *
gtk_image_definition_alloc (GtkImageType type)
{
  static gsize sizes[] = {
    sizeof (GtkImageDefinitionEmpty),
    sizeof (GtkImageDefinitionPixbuf),
    sizeof (GtkImageDefinitionStock),
    sizeof (GtkImageDefinitionIconSet),
    sizeof (GtkImageDefinitionAnimation),
    sizeof (GtkImageDefinitionIconName),
    sizeof (GtkImageDefinitionGIcon),
    sizeof (GtkImageDefinitionSurface)
  };
  GtkImageDefinition *def;

  g_assert (type < G_N_ELEMENTS (sizes));

  def = g_malloc0 (sizes[type]);
  def->type = type;
  def->empty.ref_count = 1;

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_pixbuf (GdkPixbuf *pixbuf,
                                 int        scale)
{
  GtkImageDefinition *def;

  if (pixbuf == NULL || scale <= 0)
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_PIXBUF);
  def->pixbuf.pixbuf = g_object_ref (pixbuf);
  def->pixbuf.scale = scale;

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_icon_set (GtkIconSet *icon_set)
{
  GtkImageDefinition *def;

  if (icon_set == NULL)
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_ICON_SET);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  def->icon_set.icon_set = gtk_icon_set_ref (icon_set);
G_GNUC_END_IGNORE_DEPRECATIONS;

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_animation (GdkPixbufAnimation *animation,
                                    int                 scale)
{
  GtkImageDefinition *def;

  if (animation == NULL || scale <= 0)
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_ANIMATION);
  def->animation.animation = g_object_ref (animation);
  def->animation.scale = scale;

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_icon_name (const char *icon_name)
{
  GtkImageDefinition *def;

  if (icon_name == NULL || icon_name[0] == '\0')
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_ICON_NAME);
  def->icon_name.icon_name = g_strdup (icon_name);

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_gicon (GIcon *gicon)
{
  GtkImageDefinition *def;

  if (gicon == NULL)
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_GICON);
  def->gicon.gicon = g_object_ref (gicon);

  return def;
}

GtkImageDefinition *
gtk_image_definition_new_surface (cairo_surface_t *surface)
{
  GtkImageDefinition *def;

  if (surface == NULL)
    return NULL;

  def = gtk_image_definition_alloc (GTK_IMAGE_SURFACE);
  def->surface.surface = cairo_surface_reference (surface);

  return def;
}

GtkImageDefinition *
gtk_image_definition_ref (GtkImageDefinition *def)
{
  def->empty.ref_count++;

  return def;
}

void
gtk_image_definition_unref (GtkImageDefinition *def)
{
  def->empty.ref_count--;

  if (def->empty.ref_count > 0)
    return;

  switch (def->type)
    {
    default:
    case GTK_IMAGE_EMPTY:
      g_assert_not_reached ();
      break;
    case GTK_IMAGE_PIXBUF:
      g_object_unref (def->pixbuf.pixbuf);
      break;
    case GTK_IMAGE_ANIMATION:
      g_object_unref (def->animation.animation);
      break;
    case GTK_IMAGE_SURFACE:
      cairo_surface_destroy (def->surface.surface);
      break;
    case GTK_IMAGE_ICON_SET:
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_icon_set_unref (def->icon_set.icon_set);
G_GNUC_END_IGNORE_DEPRECATIONS;
      break;
    case GTK_IMAGE_ICON_NAME:
      g_free (def->icon_name.icon_name);
      break;
    case GTK_IMAGE_GICON:
      g_object_unref (def->gicon.gicon);
      break;
    }

  g_free (def);
}

GtkImageType
gtk_image_definition_get_storage_type (const GtkImageDefinition *def)
{
  return def->type;
}

gint
gtk_image_definition_get_scale (const GtkImageDefinition *def)
{
  switch (def->type)
    {
    default:
      g_assert_not_reached ();
    case GTK_IMAGE_EMPTY:
    case GTK_IMAGE_SURFACE:
    case GTK_IMAGE_ICON_SET:
    case GTK_IMAGE_ICON_NAME:
    case GTK_IMAGE_GICON:
      return 1;
    case GTK_IMAGE_PIXBUF:
      return def->pixbuf.scale;
    case GTK_IMAGE_ANIMATION:
      return def->animation.scale;
    }
}

GdkPixbuf *
gtk_image_definition_get_pixbuf (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_PIXBUF)
    return NULL;

  return def->pixbuf.pixbuf;
}

GtkIconSet *
gtk_image_definition_get_icon_set (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_ICON_SET)
    return NULL;

  return def->icon_set.icon_set;
}

GdkPixbufAnimation *
gtk_image_definition_get_animation (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_ANIMATION)
    return NULL;

  return def->animation.animation;
}

const gchar *
gtk_image_definition_get_icon_name (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_ICON_NAME)
    return NULL;

  return def->icon_name.icon_name;
}

GIcon *
gtk_image_definition_get_gicon (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_GICON)
    return NULL;

  return def->gicon.gicon;
}

cairo_surface_t *
gtk_image_definition_get_surface (const GtkImageDefinition *def)
{
  if (def->type != GTK_IMAGE_SURFACE)
    return NULL;

  return def->surface.surface;
}