]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconhelper.c
stylecontext: Do invalidation on first resize container
[~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     g_object_unref (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       if (icon_set != NULL)
329         ensure_pixbuf_for_icon_set (self, context, icon_set);
330       else
331         pixbuf = NULL;
332       break;
333
334     case GTK_IMAGE_ICON_SET:
335       icon_set = self->priv->icon_set;
336       ensure_pixbuf_for_icon_set (self, context, icon_set);
337       break;
338
339     case GTK_IMAGE_ICON_NAME:
340     case GTK_IMAGE_GICON:
341       ensure_pixbuf_for_icon_name_or_gicon (self, context);
342       break;
343
344     case GTK_IMAGE_ANIMATION:
345     case GTK_IMAGE_EMPTY:
346     default:
347       pixbuf = NULL;
348       break;
349     }
350
351   if (pixbuf == NULL &&
352       self->priv->rendered_pixbuf != NULL)
353     pixbuf = g_object_ref (self->priv->rendered_pixbuf);
354
355   return pixbuf;
356 }
357
358 void
359 _gtk_icon_helper_get_size (GtkIconHelper *self,
360                            GtkStyleContext *context,
361                            gint *width_out,
362                            gint *height_out)
363 {
364   GdkPixbuf *pix;
365   gint width, height;
366
367   width = height = 0;
368   pix = _gtk_icon_helper_ensure_pixbuf (self, context);
369
370   if (pix != NULL)
371     {
372       width = gdk_pixbuf_get_width (pix);
373       height = gdk_pixbuf_get_height (pix);
374
375       g_object_unref (pix);
376     }
377   else if (self->priv->storage_type == GTK_IMAGE_ANIMATION)
378     {
379       width = gdk_pixbuf_animation_get_width (self->priv->animation);
380       height = gdk_pixbuf_animation_get_height (self->priv->animation);
381     }
382   else if (self->priv->icon_size != -1)
383     {
384       ensure_icon_size (self, context, &width, &height);
385     }
386
387   if (width_out)
388     *width_out = width;
389   if (height_out)
390     *height_out = height;
391 }
392
393 void 
394 _gtk_icon_helper_set_gicon (GtkIconHelper *self,
395                             GIcon *gicon,
396                             GtkIconSize icon_size)
397 {
398   _gtk_icon_helper_clear (self);
399
400   if (gicon != NULL)
401     {
402       self->priv->storage_type = GTK_IMAGE_GICON;
403       self->priv->gicon = g_object_ref (gicon);
404       _gtk_icon_helper_set_icon_size (self, icon_size);
405     }
406 }
407
408 void 
409 _gtk_icon_helper_set_icon_name (GtkIconHelper *self,
410                                 const gchar *icon_name,
411                                 GtkIconSize icon_size)
412 {
413   _gtk_icon_helper_clear (self);
414
415   if (icon_name != NULL &&
416       icon_name[0] != '\0')
417     {
418       self->priv->storage_type = GTK_IMAGE_ICON_NAME;
419       self->priv->icon_name = g_strdup (icon_name);
420       _gtk_icon_helper_set_icon_size (self, icon_size);
421     }
422 }
423
424 void 
425 _gtk_icon_helper_set_icon_set (GtkIconHelper *self,
426                                GtkIconSet *icon_set,
427                                GtkIconSize icon_size)
428 {
429   _gtk_icon_helper_clear (self);
430
431   if (icon_set != NULL)
432     {
433       self->priv->storage_type = GTK_IMAGE_ICON_SET;
434       self->priv->icon_set = gtk_icon_set_ref (icon_set);
435       _gtk_icon_helper_set_icon_size (self, icon_size);
436     }
437 }
438
439 void 
440 _gtk_icon_helper_set_pixbuf (GtkIconHelper *self,
441                              GdkPixbuf *pixbuf)
442 {
443   _gtk_icon_helper_clear (self);
444
445   if (pixbuf != NULL)
446     {
447       self->priv->storage_type = GTK_IMAGE_PIXBUF;
448       self->priv->orig_pixbuf = g_object_ref (pixbuf);
449     }
450 }
451
452 void 
453 _gtk_icon_helper_set_animation (GtkIconHelper *self,
454                                 GdkPixbufAnimation *animation)
455 {
456   _gtk_icon_helper_clear (self);
457
458   if (animation != NULL)
459     {
460       self->priv->storage_type = GTK_IMAGE_ANIMATION;
461       self->priv->animation = g_object_ref (animation);
462     }
463 }
464
465 void 
466 _gtk_icon_helper_set_stock_id (GtkIconHelper *self,
467                                const gchar *stock_id,
468                                GtkIconSize icon_size)
469 {
470   _gtk_icon_helper_clear (self);
471
472   if (stock_id != NULL &&
473       stock_id[0] != '\0')
474     {
475       self->priv->storage_type = GTK_IMAGE_STOCK;
476       self->priv->stock_id = g_strdup (stock_id);
477       _gtk_icon_helper_set_icon_size (self, icon_size);
478     }
479 }
480
481 void 
482 _gtk_icon_helper_set_icon_size (GtkIconHelper *self,
483                                 GtkIconSize icon_size)
484 {
485   if (self->priv->icon_size != icon_size)
486     {
487       self->priv->icon_size = icon_size;
488       _gtk_icon_helper_invalidate (self);
489     }
490 }
491
492 void 
493 _gtk_icon_helper_set_pixel_size (GtkIconHelper *self,
494                                  gint pixel_size)
495 {
496   if (self->priv->pixel_size != pixel_size)
497     {
498       self->priv->pixel_size = pixel_size;
499       _gtk_icon_helper_invalidate (self);
500     }
501 }
502
503 void 
504 _gtk_icon_helper_set_use_fallback (GtkIconHelper *self,
505                                    gboolean use_fallback)
506 {
507   if (self->priv->use_fallback != use_fallback)
508     {
509       self->priv->use_fallback = use_fallback;
510       _gtk_icon_helper_invalidate (self);
511     }
512 }
513
514 GtkImageType
515 _gtk_icon_helper_get_storage_type (GtkIconHelper *self)
516 {
517   return self->priv->storage_type;
518 }
519
520 gboolean
521 _gtk_icon_helper_get_use_fallback (GtkIconHelper *self)
522 {
523   return self->priv->use_fallback;
524 }
525
526 GtkIconSize
527 _gtk_icon_helper_get_icon_size (GtkIconHelper *self)
528 {
529   return self->priv->icon_size;
530 }
531
532 gint
533 _gtk_icon_helper_get_pixel_size (GtkIconHelper *self)
534 {
535   return self->priv->pixel_size;
536 }
537
538 GdkPixbuf *
539 _gtk_icon_helper_peek_pixbuf (GtkIconHelper *self)
540 {
541   return self->priv->orig_pixbuf;
542 }
543
544 GIcon *
545 _gtk_icon_helper_peek_gicon (GtkIconHelper *self)
546 {
547   return self->priv->gicon;
548 }
549
550 GdkPixbufAnimation *
551 _gtk_icon_helper_peek_animation (GtkIconHelper *self)
552 {
553   return self->priv->animation;
554 }
555
556 GtkIconSet *
557 _gtk_icon_helper_peek_icon_set (GtkIconHelper *self)
558 {
559   return self->priv->icon_set;
560 }
561
562 const gchar *
563 _gtk_icon_helper_get_stock_id (GtkIconHelper *self)
564 {
565   return self->priv->stock_id;
566 }
567
568 const gchar *
569 _gtk_icon_helper_get_icon_name (GtkIconHelper *self)
570 {
571   return self->priv->icon_name;
572 }
573
574 GtkIconHelper *
575 _gtk_icon_helper_new (void)
576 {
577   return g_object_new (GTK_TYPE_ICON_HELPER, NULL);
578 }
579
580 void
581 _gtk_icon_helper_draw (GtkIconHelper *self,
582                        GtkStyleContext *context,
583                        cairo_t *cr,
584                        gdouble x,
585                        gdouble y)
586 {
587   GdkPixbuf *pixbuf;
588
589   pixbuf = _gtk_icon_helper_ensure_pixbuf (self, context);
590
591   if (pixbuf != NULL)
592     {
593       gtk_render_icon (context, cr, pixbuf, x, y);
594       g_object_unref (pixbuf);
595     }
596 }
597
598 gboolean
599 _gtk_icon_helper_get_is_empty (GtkIconHelper *self)
600 {
601   return (self->priv->storage_type == GTK_IMAGE_EMPTY);
602 }
603
604 gboolean
605 _gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self)
606 {
607   return self->priv->force_scale_pixbuf;
608 }
609
610 void
611 _gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
612                                          gboolean       force_scale)
613 {
614   if (self->priv->force_scale_pixbuf != force_scale)
615     {
616       self->priv->force_scale_pixbuf = force_scale;
617       _gtk_icon_helper_invalidate (self);
618     }
619 }