/
gtkicontheme.c
5657 lines (4848 loc) · 169 KB
/
gtkicontheme.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GtkIconTheme - a loader for icon themes
* gtk-icon-theme.c Copyright (C) 2002, 2003 Red Hat, Inc.
*
* 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 <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <glib.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
#endif
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include "win32/gdkwin32.h"
#endif /* G_OS_WIN32 */
#include "gtkiconthemeprivate.h"
#include "gtkcsspalettevalueprivate.h"
#include "gtkcssrgbavalueprivate.h"
#include "gtkdebug.h"
#include "deprecated/gtkiconfactory.h"
#include "gtkiconcache.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include "deprecated/gtknumerableiconprivate.h"
#include "gtksettingsprivate.h"
#include "gtkstylecontextprivate.h"
#include "gtkprivate.h"
#include "gdkpixbufutilsprivate.h"
#undef GDK_DEPRECATED
#undef GDK_DEPRECATED_FOR
#define GDK_DEPRECATED
#define GDK_DEPRECATED_FOR(f)
#include "deprecated/gtkstyle.h"
/* this is in case round() is not provided by the compiler,
* such as in the case of C89 compilers, like MSVC
*/
#include "fallback-c89.c"
/**
* SECTION:gtkicontheme
* @Short_description: Looking up icons by name
* @Title: GtkIconTheme
*
* #GtkIconTheme provides a facility for looking up icons by name
* and size. The main reason for using a name rather than simply
* providing a filename is to allow different icons to be used
* depending on what “icon theme” is selected
* by the user. The operation of icon themes on Linux and Unix
* follows the [Icon Theme Specification](http://www.freedesktop.org/Standards/icon-theme-spec)
* There is a fallback icon theme, named `hicolor`, where applications
* should install their icons, but additional icon themes can be installed
* as operating system vendors and users choose.
*
* Named icons are similar to the deprecated [Stock Items][gtkstock],
* and the distinction between the two may be a bit confusing.
* A few things to keep in mind:
*
* - Stock images usually are used in conjunction with
* [Stock Items][gtkstock], such as %GTK_STOCK_OK or
* %GTK_STOCK_OPEN. Named icons are easier to set up and therefore
* are more useful for new icons that an application wants to
* add, such as application icons or window icons.
*
* - Stock images can only be loaded at the symbolic sizes defined
* by the #GtkIconSize enumeration, or by custom sizes defined
* by gtk_icon_size_register(), while named icons are more flexible
* and any pixel size can be specified.
*
* - Because stock images are closely tied to stock items, and thus
* to actions in the user interface, stock images may come in
* multiple variants for different widget states or writing
* directions.
*
* A good rule of thumb is that if there is a stock image for what
* you want to use, use it, otherwise use a named icon. It turns
* out that internally stock images are generally defined in
* terms of one or more named icons. (An example of the
* more than one case is icons that depend on writing direction;
* %GTK_STOCK_GO_FORWARD uses the two themed icons
* “gtk-stock-go-forward-ltr” and “gtk-stock-go-forward-rtl”.)
*
* In many cases, named themes are used indirectly, via #GtkImage
* or stock items, rather than directly, but looking up icons
* directly is also simple. The #GtkIconTheme object acts
* as a database of all the icons in the current theme. You
* can create new #GtkIconTheme objects, but it’s much more
* efficient to use the standard icon theme for the #GdkScreen
* so that the icon information is shared with other people
* looking up icons.
* |[<!-- language="C" -->
* GError *error = NULL;
* GtkIconTheme *icon_theme;
* GdkPixbuf *pixbuf;
*
* icon_theme = gtk_icon_theme_get_default ();
* pixbuf = gtk_icon_theme_load_icon (icon_theme,
* "my-icon-name", // icon name
* 48, // icon size
* 0, // flags
* &error);
* if (!pixbuf)
* {
* g_warning ("Couldn’t load icon: %s", error->message);
* g_error_free (error);
* }
* else
* {
* // Use the pixbuf
* g_object_unref (pixbuf);
* }
* ]|
*/
#define FALLBACK_ICON_THEME "hicolor"
typedef enum
{
ICON_THEME_DIR_FIXED,
ICON_THEME_DIR_SCALABLE,
ICON_THEME_DIR_THRESHOLD,
ICON_THEME_DIR_UNTHEMED
} IconThemeDirType;
/* In reverse search order: */
typedef enum
{
ICON_SUFFIX_NONE = 0,
ICON_SUFFIX_XPM = 1 << 0,
ICON_SUFFIX_SVG = 1 << 1,
ICON_SUFFIX_PNG = 1 << 2,
HAS_ICON_FILE = 1 << 3,
ICON_SUFFIX_SYMBOLIC_PNG = 1 << 4
} IconSuffix;
#define INFO_CACHE_LRU_SIZE 32
#if 0
#define DEBUG_CACHE(args) g_print args
#else
#define DEBUG_CACHE(args)
#endif
struct _GtkIconThemePrivate
{
GHashTable *info_cache;
GList *info_cache_lru;
gchar *current_theme;
gchar **search_path;
gint search_path_len;
GList *resource_paths;
guint custom_theme : 1;
guint is_screen_singleton : 1;
guint pixbuf_supports_svg : 1;
guint themes_valid : 1;
guint loading_themes : 1;
/* A list of all the themes needed to look up icons.
* In search order, without duplicates
*/
GList *themes;
GHashTable *unthemed_icons;
/* GdkScreen for the icon theme (may be NULL) */
GdkScreen *screen;
/* time when we last stat:ed for theme changes */
glong last_stat_time;
GList *dir_mtimes;
gulong theme_changed_idle;
};
typedef struct {
gchar **icon_names;
gint size;
gint scale;
GtkIconLookupFlags flags;
} IconInfoKey;
typedef struct _SymbolicPixbufCache SymbolicPixbufCache;
struct _SymbolicPixbufCache {
GdkPixbuf *pixbuf;
GdkPixbuf *proxy_pixbuf;
GdkRGBA fg;
GdkRGBA success_color;
GdkRGBA warning_color;
GdkRGBA error_color;
SymbolicPixbufCache *next;
};
struct _GtkIconInfoClass
{
GObjectClass parent_class;
};
struct _GtkIconInfo
{
GObject parent_instance;
/* Information about the source
*/
IconInfoKey key;
GtkIconTheme *in_cache;
gchar *filename;
GFile *icon_file;
GLoadableIcon *loadable;
GSList *emblem_infos;
/* Cache pixbuf (if there is any) */
GdkPixbuf *cache_pixbuf;
/* Information about the directory where
* the source was found
*/
IconThemeDirType dir_type;
gint dir_size;
gint dir_scale;
gint min_size;
gint max_size;
/* Parameters influencing the scaled icon
*/
gint desired_size;
gint desired_scale;
guint forced_size : 1;
guint emblems_applied : 1;
guint is_svg : 1;
guint is_resource : 1;
/* Cached information if we go ahead and try to load
* the icon.
*/
GdkPixbuf *pixbuf;
GdkPixbuf *proxy_pixbuf;
GError *load_error;
gdouble unscaled_scale;
gdouble scale;
SymbolicPixbufCache *symbolic_pixbuf_cache;
gint symbolic_width;
gint symbolic_height;
};
typedef struct
{
gchar *name;
gchar *display_name;
gchar *comment;
gchar *example;
/* In search order */
GList *dirs;
} IconTheme;
typedef struct
{
IconThemeDirType type;
GQuark context;
gint size;
gint min_size;
gint max_size;
gint threshold;
gint scale;
gboolean is_resource;
gchar *dir;
gchar *subdir;
gint subdir_index;
GtkIconCache *cache;
GHashTable *icons;
} IconThemeDir;
typedef struct
{
gchar *svg_filename;
gchar *no_svg_filename;
gboolean is_resource;
} UnthemedIcon;
typedef struct
{
gint size;
GdkPixbuf *pixbuf;
} BuiltinIcon;
typedef struct
{
gchar *dir;
time_t mtime;
GtkIconCache *cache;
gboolean exists;
} IconThemeDirMtime;
static void gtk_icon_theme_finalize (GObject *object);
static void theme_dir_destroy (IconThemeDir *dir);
static void theme_destroy (IconTheme *theme);
static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
const gchar *icon_name,
gint size,
gint scale,
gboolean allow_svg,
gboolean use_default_icons);
static void theme_list_icons (IconTheme *theme,
GHashTable *icons,
GQuark context);
static gboolean theme_has_icon (IconTheme *theme,
const gchar *icon_name);
static void theme_list_contexts (IconTheme *theme,
GHashTable *contexts);
static void theme_subdir_load (GtkIconTheme *icon_theme,
IconTheme *theme,
GKeyFile *theme_file,
gchar *subdir);
static void do_theme_change (GtkIconTheme *icon_theme);
static void blow_themes (GtkIconTheme *icon_themes);
static gboolean rescan_themes (GtkIconTheme *icon_themes);
static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
const gchar *icon_name,
gboolean *has_icon_file);
static GtkIconInfo *icon_info_new (IconThemeDirType type,
gint dir_size,
gint dir_scale);
static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
static IconSuffix suffix_from_name (const gchar *name);
static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
gint size,
gint scale,
gint *min_difference_p);
static void remove_from_lru_cache (GtkIconTheme *icon_theme,
GtkIconInfo *icon_info);
static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo* icon_info);
static guint signal_changed = 0;
static GHashTable *icon_theme_builtin_icons;
static guint
icon_info_key_hash (gconstpointer _key)
{
const IconInfoKey *key = _key;
guint h = 0;
int i;
for (i = 0; key->icon_names[i] != NULL; i++)
h ^= g_str_hash (key->icon_names[i]);
h ^= key->size * 0x10001;
h ^= key->scale * 0x1000010;
h ^= key->flags * 0x100000100;
return h;
}
static gboolean
icon_info_key_equal (gconstpointer _a,
gconstpointer _b)
{
const IconInfoKey *a = _a;
const IconInfoKey *b = _b;
int i;
if (a->size != b->size)
return FALSE;
if (a->scale != b->scale)
return FALSE;
if (a->flags != b->flags)
return FALSE;
for (i = 0;
a->icon_names[i] != NULL &&
b->icon_names[i] != NULL; i++)
{
if (strcmp (a->icon_names[i], b->icon_names[i]) != 0)
return FALSE;
}
return a->icon_names[i] == NULL && b->icon_names[i] == NULL;
}
G_DEFINE_TYPE_WITH_PRIVATE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
/**
* gtk_icon_theme_new:
*
* Creates a new icon theme object. Icon theme objects are used
* to lookup up an icon by name in a particular icon theme.
* Usually, you’ll want to use gtk_icon_theme_get_default()
* or gtk_icon_theme_get_for_screen() rather than creating
* a new icon theme object for scratch.
*
* Returns: the newly created #GtkIconTheme object.
*
* Since: 2.4
*/
GtkIconTheme *
gtk_icon_theme_new (void)
{
return g_object_new (GTK_TYPE_ICON_THEME, NULL);
}
/**
* gtk_icon_theme_get_default:
*
* Gets the icon theme for the default screen. See
* gtk_icon_theme_get_for_screen().
*
* Returns: (transfer none): A unique #GtkIconTheme associated with
* the default screen. This icon theme is associated with
* the screen and can be used as long as the screen
* is open. Do not ref or unref it.
*
* Since: 2.4
*/
GtkIconTheme *
gtk_icon_theme_get_default (void)
{
return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
}
/**
* gtk_icon_theme_get_for_screen:
* @screen: a #GdkScreen
*
* Gets the icon theme object associated with @screen; if this
* function has not previously been called for the given
* screen, a new icon theme object will be created and
* associated with the screen. Icon theme objects are
* fairly expensive to create, so using this function
* is usually a better choice than calling than gtk_icon_theme_new()
* and setting the screen yourself; by using this function
* a single icon theme object will be shared between users.
*
* Returns: (transfer none): A unique #GtkIconTheme associated with
* the given screen. This icon theme is associated with
* the screen and can be used as long as the screen
* is open. Do not ref or unref it.
*
* Since: 2.4
*/
GtkIconTheme *
gtk_icon_theme_get_for_screen (GdkScreen *screen)
{
GtkIconTheme *icon_theme;
g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
if (!icon_theme)
{
GtkIconThemePrivate *priv;
icon_theme = gtk_icon_theme_new ();
gtk_icon_theme_set_screen (icon_theme, screen);
priv = icon_theme->priv;
priv->is_screen_singleton = TRUE;
g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
}
return icon_theme;
}
static void
gtk_icon_theme_class_init (GtkIconThemeClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = gtk_icon_theme_finalize;
/**
* GtkIconTheme::changed:
* @icon_theme: the icon theme
*
* Emitted when the current icon theme is switched or GTK+ detects
* that a change has occurred in the contents of the current
* icon theme.
*/
signal_changed = g_signal_new (I_("changed"),
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkIconThemeClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
/* Callback when the display that the icon theme is attached
* to is closed; unset the screen, and if it’s the unique theme
* for the screen, drop the reference
*/
static void
display_closed (GdkDisplay *display,
gboolean is_error,
GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv = icon_theme->priv;
GdkScreen *screen = priv->screen;
gboolean was_screen_singleton = priv->is_screen_singleton;
if (was_screen_singleton)
{
g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
priv->is_screen_singleton = FALSE;
}
gtk_icon_theme_set_screen (icon_theme, NULL);
if (was_screen_singleton)
{
g_object_unref (icon_theme);
}
}
static void
update_current_theme (GtkIconTheme *icon_theme)
{
#define theme_changed(_old, _new) \
((_old && !_new) || (!_old && _new) || \
(_old && _new && strcmp (_old, _new) != 0))
GtkIconThemePrivate *priv = icon_theme->priv;
if (!priv->custom_theme)
{
gchar *theme = NULL;
gboolean changed = FALSE;
if (priv->screen)
{
GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
}
if (theme_changed (priv->current_theme, theme))
{
g_free (priv->current_theme);
priv->current_theme = theme;
changed = TRUE;
}
else
g_free (theme);
if (changed)
do_theme_change (icon_theme);
}
#undef theme_changed
}
/* Callback when the icon theme GtkSetting changes
*/
static void
theme_changed (GtkSettings *settings,
GParamSpec *pspec,
GtkIconTheme *icon_theme)
{
update_current_theme (icon_theme);
}
static void
unset_screen (GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv = icon_theme->priv;
GtkSettings *settings;
GdkDisplay *display;
if (priv->screen)
{
settings = gtk_settings_get_for_screen (priv->screen);
display = gdk_screen_get_display (priv->screen);
g_signal_handlers_disconnect_by_func (display,
(gpointer) display_closed,
icon_theme);
g_signal_handlers_disconnect_by_func (settings,
(gpointer) theme_changed,
icon_theme);
priv->screen = NULL;
}
}
/**
* gtk_icon_theme_set_screen:
* @icon_theme: a #GtkIconTheme
* @screen: a #GdkScreen
*
* Sets the screen for an icon theme; the screen is used
* to track the user’s currently configured icon theme,
* which might be different for different screens.
*
* Since: 2.4
*/
void
gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
GdkScreen *screen)
{
GtkIconThemePrivate *priv;
GtkSettings *settings;
GdkDisplay *display;
g_return_if_fail (GTK_ICON_THEME (icon_theme));
g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
priv = icon_theme->priv;
unset_screen (icon_theme);
if (screen)
{
display = gdk_screen_get_display (screen);
settings = gtk_settings_get_for_screen (screen);
priv->screen = screen;
g_signal_connect (display, "closed",
G_CALLBACK (display_closed), icon_theme);
g_signal_connect (settings, "notify::gtk-icon-theme-name",
G_CALLBACK (theme_changed), icon_theme);
}
update_current_theme (icon_theme);
}
/* Checks whether a loader for SVG files has been registered
* with GdkPixbuf.
*/
static gboolean
pixbuf_supports_svg (void)
{
GSList *formats;
GSList *tmp_list;
static gint found_svg = -1;
if (found_svg != -1)
return found_svg;
formats = gdk_pixbuf_get_formats ();
found_svg = FALSE;
for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
{
gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
gchar **mime_type;
for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
{
if (strcmp (*mime_type, "image/svg") == 0)
found_svg = TRUE;
}
g_strfreev (mime_types);
}
g_slist_free (formats);
return found_svg;
}
/* The icon info was removed from the icon_info_hash hash table */
static void
icon_info_uncached (GtkIconInfo *icon_info)
{
GtkIconTheme *icon_theme = icon_info->in_cache;
DEBUG_CACHE (("removing %p (%s %d 0x%x) from cache (icon_them: %p) (cache size %d)\n",
icon_info,
g_strjoinv (",", icon_info->key.icon_names),
icon_info->key.size, icon_info->key.flags,
icon_theme,
icon_theme != NULL ? g_hash_table_size (icon_theme->priv->info_cache) : 0));
icon_info->in_cache = NULL;
if (icon_theme != NULL)
remove_from_lru_cache (icon_theme, icon_info);
}
static void
gtk_icon_theme_init (GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv;
const gchar * const *xdg_data_dirs;
int i, j;
priv = gtk_icon_theme_get_instance_private (icon_theme);
icon_theme->priv = priv;
priv->info_cache = g_hash_table_new_full (icon_info_key_hash, icon_info_key_equal, NULL,
(GDestroyNotify)icon_info_uncached);
priv->custom_theme = FALSE;
xdg_data_dirs = g_get_system_data_dirs ();
for (i = 0; xdg_data_dirs[i]; i++) ;
priv->search_path_len = 2 * i + 2;
priv->search_path = g_new (char *, priv->search_path_len);
i = 0;
priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
for (j = 0; xdg_data_dirs[j]; j++)
priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
for (j = 0; xdg_data_dirs[j]; j++)
priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
priv->resource_paths = g_list_append (NULL, g_strdup ("/org/gtk/libgtk/icons/"));
priv->themes_valid = FALSE;
priv->themes = NULL;
priv->unthemed_icons = NULL;
priv->pixbuf_supports_svg = pixbuf_supports_svg ();
}
static void
free_dir_mtime (IconThemeDirMtime *dir_mtime)
{
if (dir_mtime->cache)
_gtk_icon_cache_unref (dir_mtime->cache);
g_free (dir_mtime->dir);
g_slice_free (IconThemeDirMtime, dir_mtime);
}
static gboolean
theme_changed_idle (gpointer user_data)
{
GtkIconTheme *icon_theme;
GtkIconThemePrivate *priv;
icon_theme = GTK_ICON_THEME (user_data);
priv = icon_theme->priv;
g_signal_emit (icon_theme, signal_changed, 0);
if (priv->screen && priv->is_screen_singleton)
gtk_style_context_reset_widgets (priv->screen);
priv->theme_changed_idle = 0;
return FALSE;
}
static void
queue_theme_changed (GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv = icon_theme->priv;
if (!priv->theme_changed_idle)
{
priv->theme_changed_idle =
gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
theme_changed_idle, icon_theme, NULL);
g_source_set_name_by_id (priv->theme_changed_idle, "[gtk+] theme_changed_idle");
}
}
static void
do_theme_change (GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv = icon_theme->priv;
g_hash_table_remove_all (priv->info_cache);
if (!priv->themes_valid)
return;
GTK_NOTE (ICONTHEME,
g_message ("change to icon theme \"%s\"", priv->current_theme));
blow_themes (icon_theme);
queue_theme_changed (icon_theme);
}
static void
blow_themes (GtkIconTheme *icon_theme)
{
GtkIconThemePrivate *priv = icon_theme->priv;
if (priv->themes_valid)
{
g_list_free_full (priv->themes, (GDestroyNotify) theme_destroy);
g_list_free_full (priv->dir_mtimes, (GDestroyNotify) free_dir_mtime);
g_hash_table_destroy (priv->unthemed_icons);
}
priv->themes = NULL;
priv->unthemed_icons = NULL;
priv->dir_mtimes = NULL;
priv->themes_valid = FALSE;
}
static void
gtk_icon_theme_finalize (GObject *object)
{
GtkIconTheme *icon_theme;
GtkIconThemePrivate *priv;
int i;
icon_theme = GTK_ICON_THEME (object);
priv = icon_theme->priv;
g_hash_table_destroy (priv->info_cache);
g_assert (priv->info_cache_lru == NULL);
if (priv->theme_changed_idle)
g_source_remove (priv->theme_changed_idle);
unset_screen (icon_theme);
g_free (priv->current_theme);
for (i = 0; i < priv->search_path_len; i++)
g_free (priv->search_path[i]);
g_free (priv->search_path);
g_list_free_full (priv->resource_paths, g_free);
blow_themes (icon_theme);
G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
}
/**
* gtk_icon_theme_set_search_path:
* @icon_theme: a #GtkIconTheme
* @path: (array length=n_elements) (element-type filename): array of
* directories that are searched for icon themes
* @n_elements: number of elements in @path.
*
* Sets the search path for the icon theme object. When looking
* for an icon theme, GTK+ will search for a subdirectory of
* one or more of the directories in @path with the same name
* as the icon theme containing an index.theme file. (Themes from
* multiple of the path elements are combined to allow themes to be
* extended by adding icons in the user’s home directory.)
*
* In addition if an icon found isn’t found either in the current
* icon theme or the default icon theme, and an image file with
* the right name is found directly in one of the elements of
* @path, then that image will be used for the icon name.
* (This is legacy feature, and new icons should be put
* into the fallback icon theme, which is called hicolor,
* rather than directly on the icon path.)
*
* Since: 2.4
*/
void
gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
const gchar *path[],
gint n_elements)
{
GtkIconThemePrivate *priv;
gint i;
g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
priv = icon_theme->priv;
for (i = 0; i < priv->search_path_len; i++)
g_free (priv->search_path[i]);
g_free (priv->search_path);
priv->search_path = g_new (gchar *, n_elements);
priv->search_path_len = n_elements;
for (i = 0; i < priv->search_path_len; i++)
priv->search_path[i] = g_strdup (path[i]);
do_theme_change (icon_theme);
}
/**
* gtk_icon_theme_get_search_path:
* @icon_theme: a #GtkIconTheme
* @path: (allow-none) (array length=n_elements) (element-type filename) (out):
* location to store a list of icon theme path directories or %NULL.
* The stored value should be freed with g_strfreev().
* @n_elements: location to store number of elements in @path, or %NULL
*
* Gets the current search path. See gtk_icon_theme_set_search_path().
*
* Since: 2.4
*/
void
gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
gchar **path[],
gint *n_elements)
{
GtkIconThemePrivate *priv;
gint i;
g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
priv = icon_theme->priv;
if (n_elements)
*n_elements = priv->search_path_len;
if (path)
{
*path = g_new (gchar *, priv->search_path_len + 1);
for (i = 0; i < priv->search_path_len; i++)
(*path)[i] = g_strdup (priv->search_path[i]);
(*path)[i] = NULL;
}
}
/**
* gtk_icon_theme_append_search_path:
* @icon_theme: a #GtkIconTheme
* @path: (type filename): directory name to append to the icon path
*
* Appends a directory to the search path.
* See gtk_icon_theme_set_search_path().
*
* Since: 2.4
*/
void
gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
const gchar *path)
{
GtkIconThemePrivate *priv;
g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
g_return_if_fail (path != NULL);
priv = icon_theme->priv;
priv->search_path_len++;
priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
priv->search_path[priv->search_path_len-1] = g_strdup (path);
do_theme_change (icon_theme);
}
/**
* gtk_icon_theme_prepend_search_path:
* @icon_theme: a #GtkIconTheme
* @path: (type filename): directory name to prepend to the icon path
*
* Prepends a directory to the search path.
* See gtk_icon_theme_set_search_path().
*
* Since: 2.4
*/
void
gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
const gchar *path)
{
GtkIconThemePrivate *priv;
gint i;
g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
g_return_if_fail (path != NULL);
priv = icon_theme->priv;
priv->search_path_len++;