]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconhelper.c
6f23914a8c5e83b41b3e700d5d14ad851a034619
[~andy/gtk] / gtk / gtkiconhelper.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * Authors: Cosimo Cecchi <cosimoc@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include "gtkiconhelperprivate.h"
23
24 G_DEFINE_TYPE (GtkIconHelper, _gtk_icon_helper, G_TYPE_OBJECT)
25
26 struct _GtkIconHelperPrivate {
27   GtkImageType storage_type;
28
29   GdkPixbuf *orig_pixbuf;
30   GdkPixbufAnimation *animation;
31   GIcon *gicon;
32   GtkIconSet *icon_set;
33   gchar *icon_name;
34   gchar *stock_id;
35
36   GtkIconSize icon_size;
37   gint pixel_size;
38
39   guint use_fallback : 1;
40   guint force_scale_pixbuf : 1;
41
42   GdkPixbuf *rendered_pixbuf;
43   GtkStateFlags last_rendered_state;
44 };
45
46 void
47 _gtk_icon_helper_clear (GtkIconHelper *self)
48 {
49   g_clear_object (&self->priv->gicon);
50   g_clear_object (&self->priv->orig_pixbuf);
51   g_clear_object (&self->priv->animation);
52   g_clear_object (&self->priv->rendered_pixbuf);
53
54   if (self->priv->icon_set != NULL)
55     {
56       gtk_icon_set_unref (self->priv->icon_set);
57       self->priv->icon_set = NULL;
58     }
59
60   g_free (self->priv->icon_name);
61   self->priv->icon_name = NULL;
62
63   g_free (self->priv->stock_id);
64   self->priv->stock_id = NULL;
65
66   self->priv->storage_type = GTK_IMAGE_EMPTY;
67   self->priv->icon_size = GTK_ICON_SIZE_INVALID;
68   self->priv->last_rendered_state = GTK_STATE_FLAG_NORMAL;
69 }
70
71 void
72 _gtk_icon_helper_invalidate (GtkIconHelper *self)
73 {
74   g_clear_object (&self->priv->rendered_pixbuf);
75 }
76
77 static void
78 gtk_icon_helper_finalize (GObject *object)
79 {
80   GtkIconHelper *self = GTK_ICON_HELPER (object);
81
82   _gtk_icon_helper_clear (self);
83   
84   G_OBJECT_CLASS (_gtk_icon_helper_parent_class)->finalize (object);
85 }
86
87 static void
88 _gtk_icon_helper_class_init (GtkIconHelperClass *klass)
89 {
90   GObjectClass *oclass;
91
92   oclass = G_OBJECT_CLASS (klass);
93   oclass->finalize = gtk_icon_helper_finalize;
94
95   g_type_class_add_private (klass, sizeof (GtkIconHelperPrivate));
96 }
97
98 static void
99 _gtk_icon_helper_init (GtkIconHelper *self)
100 {
101   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_ICON_HELPER, GtkIconHelperPrivate);
102
103   self->priv->storage_type = GTK_IMAGE_EMPTY;
104   self->priv->icon_size = GTK_ICON_SIZE_INVALID;
105   self->priv->pixel_size = -1;
106   self->priv->last_rendered_state = GTK_STATE_FLAG_NORMAL;
107 }
108
109 static void
110 ensure_icon_size (GtkIconHelper *self,
111                   GtkStyleContext *context,
112                   gint *width_out,
113                   gint *height_out)
114 {
115   gint width, height;
116   GtkSettings *settings;
117   GdkScreen *screen;
118
119   screen = gtk_style_context_get_screen (context);
120   settings = gtk_settings_get_for_screen (screen);
121
122   if (self->priv->pixel_size != -1)
123     {
124       width = height = self->priv->pixel_size;
125     }
126   else if (!gtk_icon_size_lookup_for_settings (settings,
127                                                self->priv->icon_size,
128                                                &width, &height))
129     {
130       if (self->priv->icon_size == GTK_ICON_SIZE_INVALID)
131         {
132           width = height = 0;
133         }
134       else
135         {
136           g_warning ("Invalid icon size %d\n", self->priv->icon_size);
137           width = height = 24;
138         }
139     }
140
141   *width_out = width;
142   *height_out = height;
143 }
144
145 static GdkPixbuf *
146 ensure_stated_icon_from_info (GtkIconHelper *self,
147                               GtkStyleContext *context,
148                               GtkIconInfo *info)
149 {
150   GdkPixbuf *destination = NULL;
151   gboolean symbolic;
152
153   symbolic = FALSE;
154
155   if (info)
156     destination =
157       gtk_icon_info_load_symbolic_for_context (info,
158                                                context,
159                                                &symbolic,
160                                                NULL);
161
162   if (destination == NULL)
163     {
164       GtkIconSet *icon_set;
165       icon_set = gtk_style_context_lookup_icon_set (context, GTK_STOCK_MISSING_IMAGE);
166
167       destination =
168         gtk_icon_set_render_icon_pixbuf (icon_set, context, self->priv->icon_size);
169     }
170   else if (!symbolic)
171     {
172       GtkIconSource *source;
173       GdkPixbuf *rendered;
174
175       source = gtk_icon_source_new ();
176       gtk_icon_source_set_pixbuf (source, destination);
177       /* The size here is arbitrary; since size isn't
178        * wildcarded in the source, it isn't supposed to be
179        * scaled by the engine function
180        */
181       gtk_icon_source_set_size (source,
182                                 GTK_ICON_SIZE_SMALL_TOOLBAR);
183       gtk_icon_source_set_size_wildcarded (source, FALSE);
184
185       rendered = gtk_render_icon_pixbuf (context, source, (GtkIconSize) -1);
186       gtk_icon_source_free (source);
187
188       g_object_unref (destination);
189       destination = rendered;
190     }
191
192   return destination;
193 }
194
195 static gboolean
196 check_invalidate_pixbuf (GtkIconHelper *self,
197                          GtkStyleContext *context)
198 {
199   GtkStateFlags state;
200
201   state = gtk_style_context_get_state (context);
202
203   if ((self->priv->rendered_pixbuf != NULL) &&
204       (self->priv->last_rendered_state == state))
205     return FALSE;
206
207   self->priv->last_rendered_state = state;
208   g_clear_object (&self->priv->rendered_pixbuf);
209
210   return TRUE;
211 }
212
213 static GtkIconLookupFlags
214 get_icon_lookup_flags (GtkIconHelper *self)
215 {
216   GtkIconLookupFlags flags = GTK_ICON_LOOKUP_USE_BUILTIN;
217
218   if (self->priv->use_fallback)
219     flags |= GTK_ICON_LOOKUP_GENERIC_FALLBACK;
220   if (self->priv->pixel_size != -1)
221     flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
222
223   return flags;
224 }
225
226 static void
227 ensure_pixbuf_for_icon_name_or_gicon (GtkIconHelper *self,
228                                       GtkStyleContext *context)
229 {
230   GtkIconTheme *icon_theme;
231   gint width, height;
232   GtkIconInfo *info;
233   GtkIconLookupFlags flags;
234
235   if (!check_invalidate_pixbuf (self, context))
236     return;
237
238   icon_theme = gtk_icon_theme_get_default ();
239   flags = get_icon_lookup_flags (self);
240
241   ensure_icon_size (self, context, &width, &height);
242
243   if (self->priv->storage_type == GTK_IMAGE_ICON_NAME &&
244       self->priv->icon_name != NULL)
245     {
246       info = gtk_icon_theme_lookup_icon (icon_theme,
247                                          self->priv->icon_name,
248                                          MIN (width, height), flags);
249     }
250   else if (self->priv->storage_type == GTK_IMAGE_GICON &&
251            self->priv->gicon != NULL)
252     {
253       info = gtk_icon_theme_lookup_by_gicon (icon_theme,
254                                              self->priv->gicon,
255                                              MIN (width, height), flags);
256     }
257   else
258     {
259       g_assert_not_reached ();
260       return;
261     }
262
263   self->priv->rendered_pixbuf = ensure_stated_icon_from_info (self, context, info);
264
265   if (info)
266     gtk_icon_info_free (info);
267 }
268
269 static void
270 ensure_pixbuf_for_icon_set (GtkIconHelper *self,
271                             GtkStyleContext *context,
272                             GtkIconSet *icon_set)
273 {
274   if (!check_invalidate_pixbuf (self, context))
275     return;
276
277   self->priv->rendered_pixbuf = 
278     gtk_icon_set_render_icon_pixbuf (icon_set, context, self->priv->icon_size);
279 }
280
281 static void
282 ensure_pixbuf_at_size (GtkIconHelper   *self,
283                        GtkStyleContext *context)
284 {
285   gint width, height;
286
287   if (!check_invalidate_pixbuf (self, context))
288     return;
289
290   if (self->priv->rendered_pixbuf)
291     return;
292
293   if (self->priv->pixel_size != -1 ||
294       self->priv->icon_size != GTK_ICON_SIZE_INVALID)
295     {
296       ensure_icon_size (self, context, &width, &height);
297
298       if (width < gdk_pixbuf_get_width (self->priv->orig_pixbuf) ||
299           height < gdk_pixbuf_get_height (self->priv->orig_pixbuf))
300         self->priv->rendered_pixbuf =
301           gdk_pixbuf_scale_simple (self->priv->orig_pixbuf,
302                                    width, height,
303                                    GDK_INTERP_BILINEAR);
304     }
305
306   if (!self->priv->rendered_pixbuf)
307     self->priv->rendered_pixbuf = g_object_ref (self->priv->orig_pixbuf);
308 }
309
310 GdkPixbuf *
311 _gtk_icon_helper_ensure_pixbuf (GtkIconHelper *self,
312                                 GtkStyleContext *context)
313 {
314   GdkPixbuf *pixbuf = NULL;
315   GtkIconSet *icon_set;
316
317   switch (self->priv->storage_type)
318     {
319     case GTK_IMAGE_PIXBUF:
320       if (self->priv->force_scale_pixbuf)
321         ensure_pixbuf_at_size (self, context);
322       else
323         pixbuf = g_object_ref (self->priv->orig_pixbuf);
324       break;
325
326     case GTK_IMAGE_STOCK:
327       icon_set = gtk_style_context_lookup_icon_set (context, self->priv->stock_id);
328       ensure_pixbuf_for_icon_set (self, context, icon_set);
329       break;
330
331     case GTK_IMAGE_ICON_SET:
332       icon_set = self->priv->icon_set;
333       ensure_pixbuf_for_icon_set (self, context, icon_set);
334       break;
335
336     case GTK_IMAGE_ICON_NAME:
337     case GTK_IMAGE_GICON:
338       ensure_pixbuf_for_icon_name_or_gicon (self, context);
339       break;
340
341     case GTK_IMAGE_ANIMATION:
342     case GTK_IMAGE_EMPTY:
343     default:
344       pixbuf = NULL;
345       break;
346     }
347
348   if (pixbuf == NULL &&
349       self->priv->rendered_pixbuf != NULL)
350     pixbuf = g_object_ref (self->priv->rendered_pixbuf);
351
352   return pixbuf;
353 }
354
355 void
356 _gtk_icon_helper_get_size (GtkIconHelper *self,
357                            GtkStyleContext *context,
358                            gint *width_out,
359                            gint *height_out)
360 {
361   GdkPixbuf *pix;
362   gint width, height;
363
364   width = height = 0;
365   pix = _gtk_icon_helper_ensure_pixbuf (self, context);
366
367   if (pix != NULL)
368     {
369       width = gdk_pixbuf_get_width (pix);
370       height = gdk_pixbuf_get_height (pix);
371
372       g_object_unref (pix);
373     }
374   else if (self->priv->storage_type == GTK_IMAGE_ANIMATION)
375     {
376       width = gdk_pixbuf_animation_get_width (self->priv->animation);
377       height = gdk_pixbuf_animation_get_height (self->priv->animation);
378     }
379   else if (self->priv->icon_size != -1)
380     {
381       ensure_icon_size (self, context, &width, &height);
382     }
383
384   if (width_out)
385     *width_out = width;
386   if (height_out)
387     *height_out = height;
388 }
389
390 void 
391 _gtk_icon_helper_set_gicon (GtkIconHelper *self,
392                             GIcon *gicon,
393                             GtkIconSize icon_size)
394 {
395   _gtk_icon_helper_clear (self);
396
397   if (gicon != NULL)
398     {
399       self->priv->storage_type = GTK_IMAGE_GICON;
400       self->priv->gicon = g_object_ref (gicon);
401       _gtk_icon_helper_set_icon_size (self, icon_size);
402     }
403 }
404
405 void 
406 _gtk_icon_helper_set_icon_name (GtkIconHelper *self,
407                                 const gchar *icon_name,
408                                 GtkIconSize icon_size)
409 {
410   _gtk_icon_helper_clear (self);
411
412   if (icon_name != NULL &&
413       g_strcmp0 (icon_name, "") != 0)
414     {
415       self->priv->storage_type = GTK_IMAGE_ICON_NAME;
416       self->priv->icon_name = g_strdup (icon_name);
417       _gtk_icon_helper_set_icon_size (self, icon_size);
418     }
419 }
420
421 void 
422 _gtk_icon_helper_set_icon_set (GtkIconHelper *self,
423                                GtkIconSet *icon_set,
424                                GtkIconSize icon_size)
425 {
426   _gtk_icon_helper_clear (self);
427
428   if (icon_set != NULL)
429     {
430       self->priv->storage_type = GTK_IMAGE_ICON_SET;
431       self->priv->icon_set = gtk_icon_set_ref (icon_set);
432       _gtk_icon_helper_set_icon_size (self, icon_size);
433     }
434 }
435
436 void 
437 _gtk_icon_helper_set_pixbuf (GtkIconHelper *self,
438                              GdkPixbuf *pixbuf)
439 {
440   _gtk_icon_helper_clear (self);
441
442   if (pixbuf != NULL)
443     {
444       self->priv->storage_type = GTK_IMAGE_PIXBUF;
445       self->priv->orig_pixbuf = g_object_ref (pixbuf);
446     }
447 }
448
449 void 
450 _gtk_icon_helper_set_animation (GtkIconHelper *self,
451                                 GdkPixbufAnimation *animation)
452 {
453   _gtk_icon_helper_clear (self);
454
455   if (animation != NULL)
456     {
457       self->priv->storage_type = GTK_IMAGE_ANIMATION;
458       self->priv->animation = g_object_ref (animation);
459     }
460 }
461
462 void 
463 _gtk_icon_helper_set_stock_id (GtkIconHelper *self,
464                                const gchar *stock_id,
465                                GtkIconSize icon_size)
466 {
467   _gtk_icon_helper_clear (self);
468
469   if (stock_id != NULL)
470     {
471       self->priv->storage_type = GTK_IMAGE_STOCK;
472       self->priv->stock_id = g_strdup (stock_id);
473       _gtk_icon_helper_set_icon_size (self, icon_size);
474     }
475 }
476
477 void 
478 _gtk_icon_helper_set_icon_size (GtkIconHelper *self,
479                                 GtkIconSize icon_size)
480 {
481   if (self->priv->icon_size != icon_size)
482     {
483       self->priv->icon_size = icon_size;
484       _gtk_icon_helper_invalidate (self);
485     }
486 }
487
488 void 
489 _gtk_icon_helper_set_pixel_size (GtkIconHelper *self,
490                                  gint pixel_size)
491 {
492   if (self->priv->pixel_size != pixel_size)
493     {
494       self->priv->pixel_size = pixel_size;
495       _gtk_icon_helper_invalidate (self);
496     }
497 }
498
499 void 
500 _gtk_icon_helper_set_use_fallback (GtkIconHelper *self,
501                                    gboolean use_fallback)
502 {
503   if (self->priv->use_fallback != use_fallback)
504     {
505       self->priv->use_fallback = use_fallback;
506       _gtk_icon_helper_invalidate (self);
507     }
508 }
509
510 GtkImageType
511 _gtk_icon_helper_get_storage_type (GtkIconHelper *self)
512 {
513   return self->priv->storage_type;
514 }
515
516 gboolean
517 _gtk_icon_helper_get_use_fallback (GtkIconHelper *self)
518 {
519   return self->priv->use_fallback;
520 }
521
522 GtkIconSize
523 _gtk_icon_helper_get_icon_size (GtkIconHelper *self)
524 {
525   return self->priv->icon_size;
526 }
527
528 gint
529 _gtk_icon_helper_get_pixel_size (GtkIconHelper *self)
530 {
531   return self->priv->pixel_size;
532 }
533
534 GdkPixbuf *
535 _gtk_icon_helper_peek_pixbuf (GtkIconHelper *self)
536 {
537   return self->priv->orig_pixbuf;
538 }
539
540 GIcon *
541 _gtk_icon_helper_peek_gicon (GtkIconHelper *self)
542 {
543   return self->priv->gicon;
544 }
545
546 GdkPixbufAnimation *
547 _gtk_icon_helper_peek_animation (GtkIconHelper *self)
548 {
549   return self->priv->animation;
550 }
551
552 GtkIconSet *
553 _gtk_icon_helper_peek_icon_set (GtkIconHelper *self)
554 {
555   return self->priv->icon_set;
556 }
557
558 const gchar *
559 _gtk_icon_helper_get_stock_id (GtkIconHelper *self)
560 {
561   return self->priv->stock_id;
562 }
563
564 const gchar *
565 _gtk_icon_helper_get_icon_name (GtkIconHelper *self)
566 {
567   return self->priv->icon_name;
568 }
569
570 GtkIconHelper *
571 _gtk_icon_helper_new (void)
572 {
573   return g_object_new (GTK_TYPE_ICON_HELPER, NULL);
574 }
575
576 void
577 _gtk_icon_helper_draw (GtkIconHelper *self,
578                        GtkStyleContext *context,
579                        cairo_t *cr,
580                        gdouble x,
581                        gdouble y)
582 {
583   GdkPixbuf *pixbuf;
584
585   pixbuf = _gtk_icon_helper_ensure_pixbuf (self, context);
586
587   if (pixbuf != NULL)
588     {
589       gtk_render_icon (context, cr, pixbuf, x, y);
590       g_object_unref (pixbuf);
591     }
592 }
593
594 gboolean
595 _gtk_icon_helper_get_is_empty (GtkIconHelper *self)
596 {
597   return (self->priv->storage_type == GTK_IMAGE_EMPTY);
598 }
599
600 gboolean
601 _gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self)
602 {
603   return self->priv->force_scale_pixbuf;
604 }
605
606 void
607 _gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
608                                          gboolean       force_scale)
609 {
610   if (self->priv->force_scale_pixbuf != force_scale)
611     {
612       self->priv->force_scale_pixbuf = force_scale;
613       _gtk_icon_helper_invalidate (self);
614     }
615 }