| File: | ctk/ctkimagedefinition.c |
| Warning: | line 128, column 9 Cast a region whose size is not a multiple of the destination type size |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* CTK - 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 "ctkimagedefinitionprivate.h" | |||
| 21 | ||||
| 22 | #include "ctkiconfactory.h" | |||
| 23 | ||||
| 24 | typedef struct _CtkImageDefinitionEmpty CtkImageDefinitionEmpty; | |||
| 25 | typedef struct _CtkImageDefinitionPixbuf CtkImageDefinitionPixbuf; | |||
| 26 | typedef struct _CtkImageDefinitionStock CtkImageDefinitionStock; | |||
| 27 | typedef struct _CtkImageDefinitionIconSet CtkImageDefinitionIconSet; | |||
| 28 | typedef struct _CtkImageDefinitionAnimation CtkImageDefinitionAnimation; | |||
| 29 | typedef struct _CtkImageDefinitionIconName CtkImageDefinitionIconName; | |||
| 30 | typedef struct _CtkImageDefinitionGIcon CtkImageDefinitionGIcon; | |||
| 31 | typedef struct _CtkImageDefinitionSurface CtkImageDefinitionSurface; | |||
| 32 | ||||
| 33 | struct _CtkImageDefinitionEmpty { | |||
| 34 | CtkImageType type; | |||
| 35 | gint ref_count; | |||
| 36 | }; | |||
| 37 | ||||
| 38 | struct _CtkImageDefinitionPixbuf { | |||
| 39 | CtkImageType type; | |||
| 40 | gint ref_count; | |||
| 41 | ||||
| 42 | GdkPixbuf *pixbuf; | |||
| 43 | int scale; | |||
| 44 | }; | |||
| 45 | ||||
| 46 | struct _CtkImageDefinitionStock { | |||
| 47 | CtkImageType type; | |||
| 48 | gint ref_count; | |||
| 49 | ||||
| 50 | char *id; | |||
| 51 | }; | |||
| 52 | ||||
| 53 | struct _CtkImageDefinitionIconSet { | |||
| 54 | CtkImageType type; | |||
| 55 | gint ref_count; | |||
| 56 | ||||
| 57 | CtkIconSet *icon_set; | |||
| 58 | }; | |||
| 59 | ||||
| 60 | struct _CtkImageDefinitionAnimation { | |||
| 61 | CtkImageType type; | |||
| 62 | gint ref_count; | |||
| 63 | ||||
| 64 | GdkPixbufAnimation *animation; | |||
| 65 | int scale; | |||
| 66 | }; | |||
| 67 | ||||
| 68 | struct _CtkImageDefinitionIconName { | |||
| 69 | CtkImageType type; | |||
| 70 | gint ref_count; | |||
| 71 | ||||
| 72 | char *icon_name; | |||
| 73 | }; | |||
| 74 | ||||
| 75 | struct _CtkImageDefinitionGIcon { | |||
| 76 | CtkImageType type; | |||
| 77 | gint ref_count; | |||
| 78 | ||||
| 79 | GIcon *gicon; | |||
| 80 | }; | |||
| 81 | ||||
| 82 | struct _CtkImageDefinitionSurface { | |||
| 83 | CtkImageType type; | |||
| 84 | gint ref_count; | |||
| 85 | ||||
| 86 | cairo_surface_t *surface; | |||
| 87 | }; | |||
| 88 | ||||
| 89 | union _CtkImageDefinition | |||
| 90 | { | |||
| 91 | CtkImageType type; | |||
| 92 | CtkImageDefinitionEmpty empty; | |||
| 93 | CtkImageDefinitionPixbuf pixbuf; | |||
| 94 | CtkImageDefinitionStock stock; | |||
| 95 | CtkImageDefinitionIconSet icon_set; | |||
| 96 | CtkImageDefinitionAnimation animation; | |||
| 97 | CtkImageDefinitionIconName icon_name; | |||
| 98 | CtkImageDefinitionGIcon gicon; | |||
| 99 | CtkImageDefinitionSurface surface; | |||
| 100 | }; | |||
| 101 | ||||
| 102 | CtkImageDefinition * | |||
| 103 | ctk_image_definition_new_empty (void) | |||
| 104 | { | |||
| 105 | static CtkImageDefinitionEmpty empty = { CTK_IMAGE_EMPTY, 1 }; | |||
| 106 | ||||
| 107 | return ctk_image_definition_ref ((CtkImageDefinition *) &empty); | |||
| 108 | } | |||
| 109 | ||||
| 110 | static inline CtkImageDefinition * | |||
| 111 | ctk_image_definition_alloc (CtkImageType type) | |||
| 112 | { | |||
| 113 | static gsize sizes[] = { | |||
| 114 | sizeof (CtkImageDefinitionEmpty), | |||
| 115 | sizeof (CtkImageDefinitionPixbuf), | |||
| 116 | sizeof (CtkImageDefinitionStock), | |||
| 117 | sizeof (CtkImageDefinitionIconSet), | |||
| 118 | sizeof (CtkImageDefinitionAnimation), | |||
| 119 | sizeof (CtkImageDefinitionIconName), | |||
| 120 | sizeof (CtkImageDefinitionGIcon), | |||
| 121 | sizeof (CtkImageDefinitionSurface) | |||
| 122 | }; | |||
| 123 | CtkImageDefinition *def; | |||
| 124 | CtkImageDefinitionEmpty *empty_def; | |||
| 125 | ||||
| 126 | g_assert (type < G_N_ELEMENTS (sizes))do { if (type < (sizeof (sizes) / sizeof ((sizes)[0]))) ; else g_assertion_message_expr ("Ctk", "ctkimagedefinition.c", 126 , ((const char*) (__func__)), "type < G_N_ELEMENTS (sizes)" ); } while (0); | |||
| 127 | ||||
| 128 | def = g_malloc0 (sizes[type]); | |||
| ||||
| 129 | empty_def = (CtkImageDefinitionEmpty *) def; | |||
| 130 | empty_def->type = type; | |||
| 131 | empty_def->ref_count = 1; | |||
| 132 | ||||
| 133 | return def; | |||
| 134 | } | |||
| 135 | ||||
| 136 | CtkImageDefinition * | |||
| 137 | ctk_image_definition_new_pixbuf (GdkPixbuf *pixbuf, | |||
| 138 | int scale) | |||
| 139 | { | |||
| 140 | CtkImageDefinition *def; | |||
| 141 | CtkImageDefinitionPixbuf *pixbuf_def; | |||
| 142 | ||||
| 143 | if (pixbuf == NULL((void*)0) || scale <= 0) | |||
| 144 | return NULL((void*)0); | |||
| 145 | ||||
| 146 | def = ctk_image_definition_alloc (CTK_IMAGE_PIXBUF); | |||
| 147 | pixbuf_def = (CtkImageDefinitionPixbuf *) def; | |||
| 148 | pixbuf_def->pixbuf = g_object_ref (pixbuf)((__typeof__ (pixbuf)) (g_object_ref) (pixbuf)); | |||
| 149 | pixbuf_def->scale = scale; | |||
| 150 | ||||
| 151 | return def; | |||
| 152 | } | |||
| 153 | ||||
| 154 | CtkImageDefinition * | |||
| 155 | ctk_image_definition_new_stock (const char *stock_id) | |||
| 156 | { | |||
| 157 | CtkImageDefinition *def; | |||
| 158 | CtkImageDefinitionStock *stock_def; | |||
| 159 | ||||
| 160 | if (stock_id == NULL((void*)0) || stock_id[0] == '\0') | |||
| 161 | return NULL((void*)0); | |||
| 162 | ||||
| 163 | def = ctk_image_definition_alloc (CTK_IMAGE_STOCK); | |||
| 164 | stock_def = (CtkImageDefinitionStock *) def; | |||
| 165 | stock_def->id = g_strdup (stock_id)g_strdup_inline (stock_id); | |||
| 166 | ||||
| 167 | return def; | |||
| 168 | } | |||
| 169 | ||||
| 170 | CtkImageDefinition * | |||
| 171 | ctk_image_definition_new_icon_set (CtkIconSet *icon_set) | |||
| 172 | { | |||
| 173 | CtkImageDefinition *def; | |||
| 174 | CtkImageDefinitionIconSet *icon_set_def; | |||
| 175 | ||||
| 176 | if (icon_set == NULL((void*)0)) | |||
| 177 | return NULL((void*)0); | |||
| 178 | ||||
| 179 | def = ctk_image_definition_alloc (CTK_IMAGE_ICON_SET); | |||
| 180 | icon_set_def = (CtkImageDefinitionIconSet *) def; | |||
| 181 | icon_set_def->icon_set = ctk_icon_set_ref (icon_set); | |||
| 182 | ||||
| 183 | return def; | |||
| 184 | } | |||
| 185 | ||||
| 186 | CtkImageDefinition * | |||
| 187 | ctk_image_definition_new_animation (GdkPixbufAnimation *animation, | |||
| 188 | int scale) | |||
| 189 | { | |||
| 190 | CtkImageDefinition *def; | |||
| 191 | CtkImageDefinitionAnimation *animation_def; | |||
| 192 | ||||
| 193 | if (animation == NULL((void*)0) || scale <= 0) | |||
| 194 | return NULL((void*)0); | |||
| 195 | ||||
| 196 | def = ctk_image_definition_alloc (CTK_IMAGE_ANIMATION); | |||
| 197 | animation_def = (CtkImageDefinitionAnimation *) def; | |||
| 198 | animation_def->animation = g_object_ref (animation)((__typeof__ (animation)) (g_object_ref) (animation)); | |||
| 199 | animation_def->scale = scale; | |||
| 200 | ||||
| 201 | return def; | |||
| 202 | } | |||
| 203 | ||||
| 204 | CtkImageDefinition * | |||
| 205 | ctk_image_definition_new_icon_name (const char *icon_name) | |||
| 206 | { | |||
| 207 | CtkImageDefinition *def; | |||
| 208 | CtkImageDefinitionIconName *icon_name_def; | |||
| 209 | ||||
| 210 | if (icon_name == NULL((void*)0) || icon_name[0] == '\0') | |||
| 211 | return NULL((void*)0); | |||
| 212 | ||||
| 213 | def = ctk_image_definition_alloc (CTK_IMAGE_ICON_NAME); | |||
| 214 | icon_name_def = (CtkImageDefinitionIconName *) def; | |||
| 215 | icon_name_def->icon_name = g_strdup (icon_name)g_strdup_inline (icon_name); | |||
| 216 | ||||
| 217 | return def; | |||
| 218 | } | |||
| 219 | ||||
| 220 | CtkImageDefinition * | |||
| 221 | ctk_image_definition_new_gicon (GIcon *gicon) | |||
| 222 | { | |||
| 223 | CtkImageDefinition *def; | |||
| 224 | CtkImageDefinitionGIcon *gicon_def; | |||
| 225 | ||||
| 226 | if (gicon == NULL((void*)0)) | |||
| 227 | return NULL((void*)0); | |||
| 228 | ||||
| 229 | def = ctk_image_definition_alloc (CTK_IMAGE_GICON); | |||
| 230 | gicon_def = (CtkImageDefinitionGIcon *) def; | |||
| 231 | gicon_def->gicon = g_object_ref (gicon)((__typeof__ (gicon)) (g_object_ref) (gicon)); | |||
| 232 | ||||
| 233 | return def; | |||
| 234 | } | |||
| 235 | ||||
| 236 | CtkImageDefinition * | |||
| 237 | ctk_image_definition_new_surface (cairo_surface_t *surface) | |||
| 238 | { | |||
| 239 | CtkImageDefinition *def; | |||
| 240 | CtkImageDefinitionSurface *surface_def; | |||
| 241 | ||||
| 242 | if (surface == NULL((void*)0)) | |||
| ||||
| 243 | return NULL((void*)0); | |||
| 244 | ||||
| 245 | def = ctk_image_definition_alloc (CTK_IMAGE_SURFACE); | |||
| 246 | surface_def = (CtkImageDefinitionSurface *) def; | |||
| 247 | surface_def->surface = cairo_surface_reference (surface); | |||
| 248 | ||||
| 249 | return def; | |||
| 250 | } | |||
| 251 | ||||
| 252 | CtkImageDefinition * | |||
| 253 | ctk_image_definition_ref (CtkImageDefinition *def) | |||
| 254 | { | |||
| 255 | CtkImageDefinitionEmpty *empty_def; | |||
| 256 | ||||
| 257 | empty_def = (CtkImageDefinitionEmpty *) def; | |||
| 258 | empty_def->ref_count++; | |||
| 259 | ||||
| 260 | return def; | |||
| 261 | } | |||
| 262 | ||||
| 263 | void | |||
| 264 | ctk_image_definition_unref (CtkImageDefinition *def) | |||
| 265 | { | |||
| 266 | CtkImageDefinitionEmpty *empty_def; | |||
| 267 | CtkImageDefinitionPixbuf *pixbuf_def; | |||
| 268 | CtkImageDefinitionAnimation *animation_def; | |||
| 269 | CtkImageDefinitionSurface *surface_def; | |||
| 270 | CtkImageDefinitionStock *stock_def; | |||
| 271 | CtkImageDefinitionIconSet *icon_set_def; | |||
| 272 | CtkImageDefinitionIconName *icon_name_def; | |||
| 273 | CtkImageDefinitionGIcon *gicon_def; | |||
| 274 | ||||
| 275 | empty_def = (CtkImageDefinitionEmpty *) def; | |||
| 276 | empty_def->ref_count--; | |||
| 277 | ||||
| 278 | if (empty_def->ref_count > 0) | |||
| 279 | return; | |||
| 280 | ||||
| 281 | switch (def->type) | |||
| 282 | { | |||
| 283 | default: | |||
| 284 | case CTK_IMAGE_EMPTY: | |||
| 285 | g_assert_not_reached ()do { g_assertion_message_expr ("Ctk", "ctkimagedefinition.c", 285, ((const char*) (__func__)), ((void*)0)); } while (0); | |||
| 286 | break; | |||
| 287 | case CTK_IMAGE_PIXBUF: | |||
| 288 | pixbuf_def = (CtkImageDefinitionPixbuf *) def; | |||
| 289 | g_object_unref (pixbuf_def->pixbuf); | |||
| 290 | break; | |||
| 291 | case CTK_IMAGE_ANIMATION: | |||
| 292 | animation_def = (CtkImageDefinitionAnimation *) def; | |||
| 293 | g_object_unref (animation_def->animation); | |||
| 294 | break; | |||
| 295 | case CTK_IMAGE_SURFACE: | |||
| 296 | surface_def = (CtkImageDefinitionSurface *) def; | |||
| 297 | cairo_surface_destroy (surface_def->surface); | |||
| 298 | break; | |||
| 299 | case CTK_IMAGE_STOCK: | |||
| 300 | stock_def = (CtkImageDefinitionStock *) def; | |||
| 301 | g_free (stock_def->id); | |||
| 302 | break; | |||
| 303 | case CTK_IMAGE_ICON_SET: | |||
| 304 | icon_set_def = (CtkImageDefinitionIconSet *) def; | |||
| 305 | ctk_icon_set_unref (icon_set_def->icon_set); | |||
| 306 | break; | |||
| 307 | case CTK_IMAGE_ICON_NAME: | |||
| 308 | icon_name_def = (CtkImageDefinitionIconName *) def; | |||
| 309 | g_free (icon_name_def->icon_name); | |||
| 310 | break; | |||
| 311 | case CTK_IMAGE_GICON: | |||
| 312 | gicon_def = (CtkImageDefinitionGIcon *) def; | |||
| 313 | g_object_unref (gicon_def->gicon); | |||
| 314 | break; | |||
| 315 | } | |||
| 316 | ||||
| 317 | g_free (def); | |||
| 318 | } | |||
| 319 | ||||
| 320 | CtkImageType | |||
| 321 | ctk_image_definition_get_storage_type (const CtkImageDefinition *def) | |||
| 322 | { | |||
| 323 | return def->type; | |||
| 324 | } | |||
| 325 | ||||
| 326 | gint | |||
| 327 | ctk_image_definition_get_scale (const CtkImageDefinition *def) | |||
| 328 | { | |||
| 329 | switch (def->type) | |||
| 330 | { | |||
| 331 | default: | |||
| 332 | g_assert_not_reached ()do { g_assertion_message_expr ("Ctk", "ctkimagedefinition.c", 332, ((const char*) (__func__)), ((void*)0)); } while (0); | |||
| 333 | case CTK_IMAGE_EMPTY: | |||
| 334 | case CTK_IMAGE_SURFACE: | |||
| 335 | case CTK_IMAGE_STOCK: | |||
| 336 | case CTK_IMAGE_ICON_SET: | |||
| 337 | case CTK_IMAGE_ICON_NAME: | |||
| 338 | case CTK_IMAGE_GICON: | |||
| 339 | return 1; | |||
| 340 | case CTK_IMAGE_PIXBUF: | |||
| 341 | return def->pixbuf.scale; | |||
| 342 | case CTK_IMAGE_ANIMATION: | |||
| 343 | return def->animation.scale; | |||
| 344 | } | |||
| 345 | } | |||
| 346 | ||||
| 347 | GdkPixbuf * | |||
| 348 | ctk_image_definition_get_pixbuf (const CtkImageDefinition *def) | |||
| 349 | { | |||
| 350 | if (def->type != CTK_IMAGE_PIXBUF) | |||
| 351 | return NULL((void*)0); | |||
| 352 | ||||
| 353 | return def->pixbuf.pixbuf; | |||
| 354 | } | |||
| 355 | ||||
| 356 | const gchar * | |||
| 357 | ctk_image_definition_get_stock (const CtkImageDefinition *def) | |||
| 358 | { | |||
| 359 | if (def->type != CTK_IMAGE_STOCK) | |||
| 360 | return NULL((void*)0); | |||
| 361 | ||||
| 362 | return def->stock.id; | |||
| 363 | } | |||
| 364 | ||||
| 365 | CtkIconSet * | |||
| 366 | ctk_image_definition_get_icon_set (const CtkImageDefinition *def) | |||
| 367 | { | |||
| 368 | if (def->type != CTK_IMAGE_ICON_SET) | |||
| 369 | return NULL((void*)0); | |||
| 370 | ||||
| 371 | return def->icon_set.icon_set; | |||
| 372 | } | |||
| 373 | ||||
| 374 | GdkPixbufAnimation * | |||
| 375 | ctk_image_definition_get_animation (const CtkImageDefinition *def) | |||
| 376 | { | |||
| 377 | if (def->type != CTK_IMAGE_ANIMATION) | |||
| 378 | return NULL((void*)0); | |||
| 379 | ||||
| 380 | return def->animation.animation; | |||
| 381 | } | |||
| 382 | ||||
| 383 | const gchar * | |||
| 384 | ctk_image_definition_get_icon_name (const CtkImageDefinition *def) | |||
| 385 | { | |||
| 386 | if (def->type != CTK_IMAGE_ICON_NAME) | |||
| 387 | return NULL((void*)0); | |||
| 388 | ||||
| 389 | return def->icon_name.icon_name; | |||
| 390 | } | |||
| 391 | ||||
| 392 | GIcon * | |||
| 393 | ctk_image_definition_get_gicon (const CtkImageDefinition *def) | |||
| 394 | { | |||
| 395 | if (def->type != CTK_IMAGE_GICON) | |||
| 396 | return NULL((void*)0); | |||
| 397 | ||||
| 398 | return def->gicon.gicon; | |||
| 399 | } | |||
| 400 | ||||
| 401 | cairo_surface_t * | |||
| 402 | ctk_image_definition_get_surface (const CtkImageDefinition *def) | |||
| 403 | { | |||
| 404 | if (def->type != CTK_IMAGE_SURFACE) | |||
| 405 | return NULL((void*)0); | |||
| 406 | ||||
| 407 | return def->surface.surface; | |||
| 408 | } |