1 | /* GTK - The GIMP Toolkit |
---|---|
2 | * Copyright (C) 2015 Benjamin Otte <otte@gnome.org> |
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 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 | |
18 | #include "config.h" |
19 | |
20 | #include "gtkimagedefinitionprivate.h" |
21 | |
22 | typedef struct _GtkImageDefinitionEmpty GtkImageDefinitionEmpty; |
23 | typedef struct _GtkImageDefinitionIconName GtkImageDefinitionIconName; |
24 | typedef struct _GtkImageDefinitionGIcon GtkImageDefinitionGIcon; |
25 | typedef struct _GtkImageDefinitionPaintable GtkImageDefinitionPaintable; |
26 | |
27 | struct _GtkImageDefinitionEmpty { |
28 | GtkImageType type; |
29 | int ref_count; |
30 | }; |
31 | |
32 | struct _GtkImageDefinitionIconName { |
33 | GtkImageType type; |
34 | int ref_count; |
35 | |
36 | char *icon_name; |
37 | }; |
38 | |
39 | struct _GtkImageDefinitionGIcon { |
40 | GtkImageType type; |
41 | int ref_count; |
42 | |
43 | GIcon *gicon; |
44 | }; |
45 | |
46 | struct _GtkImageDefinitionPaintable { |
47 | GtkImageType type; |
48 | int ref_count; |
49 | |
50 | GdkPaintable *paintable; |
51 | }; |
52 | |
53 | union _GtkImageDefinition |
54 | { |
55 | GtkImageType type; |
56 | GtkImageDefinitionEmpty empty; |
57 | GtkImageDefinitionIconName icon_name; |
58 | GtkImageDefinitionGIcon gicon; |
59 | GtkImageDefinitionPaintable paintable; |
60 | }; |
61 | |
62 | GtkImageDefinition * |
63 | gtk_image_definition_new_empty (void) |
64 | { |
65 | static GtkImageDefinitionEmpty empty = { GTK_IMAGE_EMPTY, 1 }; |
66 | |
67 | return gtk_image_definition_ref (def: (GtkImageDefinition *) &empty); |
68 | } |
69 | |
70 | static inline GtkImageDefinition * |
71 | gtk_image_definition_alloc (GtkImageType type) |
72 | { |
73 | static gsize sizes[] = { |
74 | sizeof (GtkImageDefinitionEmpty), |
75 | sizeof (GtkImageDefinitionIconName), |
76 | sizeof (GtkImageDefinitionGIcon), |
77 | sizeof (GtkImageDefinitionPaintable) |
78 | }; |
79 | GtkImageDefinition *def; |
80 | |
81 | g_assert (type < G_N_ELEMENTS (sizes)); |
82 | |
83 | def = g_malloc0 (n_bytes: sizes[type]); |
84 | def->type = type; |
85 | def->empty.ref_count = 1; |
86 | |
87 | return def; |
88 | } |
89 | |
90 | GtkImageDefinition * |
91 | gtk_image_definition_new_icon_name (const char *icon_name) |
92 | { |
93 | GtkImageDefinition *def; |
94 | |
95 | if (icon_name == NULL || icon_name[0] == '\0') |
96 | return NULL; |
97 | |
98 | def = gtk_image_definition_alloc (type: GTK_IMAGE_ICON_NAME); |
99 | def->icon_name.icon_name = g_strdup (str: icon_name); |
100 | |
101 | return def; |
102 | } |
103 | |
104 | GtkImageDefinition * |
105 | gtk_image_definition_new_gicon (GIcon *gicon) |
106 | { |
107 | GtkImageDefinition *def; |
108 | |
109 | if (gicon == NULL) |
110 | return NULL; |
111 | |
112 | def = gtk_image_definition_alloc (type: GTK_IMAGE_GICON); |
113 | def->gicon.gicon = g_object_ref (gicon); |
114 | |
115 | return def; |
116 | } |
117 | |
118 | GtkImageDefinition * |
119 | gtk_image_definition_new_paintable (GdkPaintable *paintable) |
120 | { |
121 | GtkImageDefinition *def; |
122 | |
123 | if (paintable == NULL) |
124 | return NULL; |
125 | |
126 | def = gtk_image_definition_alloc (type: GTK_IMAGE_PAINTABLE); |
127 | def->paintable.paintable = g_object_ref (paintable); |
128 | |
129 | return def; |
130 | } |
131 | |
132 | GtkImageDefinition * |
133 | gtk_image_definition_ref (GtkImageDefinition *def) |
134 | { |
135 | GtkImageDefinitionEmpty *empty = (GtkImageDefinitionEmpty *) def; |
136 | |
137 | empty->ref_count++; |
138 | |
139 | return def; |
140 | } |
141 | |
142 | void |
143 | gtk_image_definition_unref (GtkImageDefinition *def) |
144 | { |
145 | GtkImageDefinitionEmpty *empty = (GtkImageDefinitionEmpty *) def; |
146 | |
147 | empty->ref_count--; |
148 | |
149 | if (empty->ref_count > 0) |
150 | return; |
151 | |
152 | switch (def->type) |
153 | { |
154 | default: |
155 | case GTK_IMAGE_EMPTY: |
156 | g_assert_not_reached (); |
157 | break; |
158 | case GTK_IMAGE_PAINTABLE: |
159 | { |
160 | GtkImageDefinitionPaintable *paintable = (GtkImageDefinitionPaintable *) def; |
161 | g_object_unref (object: paintable->paintable); |
162 | } |
163 | break; |
164 | case GTK_IMAGE_ICON_NAME: |
165 | { |
166 | GtkImageDefinitionIconName *icon_name = (GtkImageDefinitionIconName *) def; |
167 | g_free (mem: icon_name->icon_name); |
168 | } |
169 | break; |
170 | case GTK_IMAGE_GICON: |
171 | { |
172 | GtkImageDefinitionGIcon *gicon = (GtkImageDefinitionGIcon *) def; |
173 | g_object_unref (object: gicon->gicon); |
174 | } |
175 | break; |
176 | } |
177 | |
178 | g_free (mem: def); |
179 | } |
180 | |
181 | GtkImageType |
182 | gtk_image_definition_get_storage_type (const GtkImageDefinition *def) |
183 | { |
184 | return def->type; |
185 | } |
186 | |
187 | int |
188 | gtk_image_definition_get_scale (const GtkImageDefinition *def) |
189 | { |
190 | switch (def->type) |
191 | { |
192 | default: |
193 | g_assert_not_reached (); |
194 | case GTK_IMAGE_EMPTY: |
195 | case GTK_IMAGE_PAINTABLE: |
196 | case GTK_IMAGE_ICON_NAME: |
197 | case GTK_IMAGE_GICON: |
198 | return 1; |
199 | } |
200 | } |
201 | |
202 | const char * |
203 | gtk_image_definition_get_icon_name (const GtkImageDefinition *def) |
204 | { |
205 | const GtkImageDefinitionIconName *icon_name = (const GtkImageDefinitionIconName *) def; |
206 | |
207 | if (def->type != GTK_IMAGE_ICON_NAME) |
208 | return NULL; |
209 | |
210 | return icon_name->icon_name; |
211 | } |
212 | |
213 | GIcon * |
214 | gtk_image_definition_get_gicon (const GtkImageDefinition *def) |
215 | { |
216 | const GtkImageDefinitionGIcon *gicon = (const GtkImageDefinitionGIcon *) def; |
217 | |
218 | if (def->type != GTK_IMAGE_GICON) |
219 | return NULL; |
220 | |
221 | return gicon->gicon; |
222 | } |
223 | |
224 | GdkPaintable * |
225 | gtk_image_definition_get_paintable (const GtkImageDefinition *def) |
226 | { |
227 | const GtkImageDefinitionPaintable *paintable = (const GtkImageDefinitionPaintable *) def; |
228 | |
229 | if (def->type != GTK_IMAGE_PAINTABLE) |
230 | return NULL; |
231 | |
232 | return paintable->paintable; |
233 | } |
234 |