9454d1d69d69a393b9ac658422c908027215ffc9
[framework/uifw/e17.git] / src / modules / conf_theme / e_int_config_icon_themes.c
1 #include "e.h"
2
3 static void *_create_data(E_Config_Dialog *cfd);
4 static void _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
5 static int _basic_check_changed(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
6 static int _basic_apply(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
7 static Evas_Object *_basic_create(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
8 static int _sort_icon_themes(const void *data1, const void *data2);
9 static Evas_Object *_icon_new(Evas *evas, const char *theme, const char *icon, unsigned int size);
10
11 struct _E_Config_Dialog_Data
12 {
13    E_Config_Dialog *cfd;
14    Eina_List *icon_themes;
15    const char *themename;
16    int overrides;
17    int enable_icon_theme;
18    int populating;
19    struct 
20      {
21         Evas_Object *list;
22         Evas_Object *checkbox;
23         Evas_Object *preview[4]; /* same size as _icon_previews */
24      } gui;
25    Ecore_Idler *fill_icon_themes_delayed;
26 };
27
28 static const char *_icon_previews[4] = 
29 {
30    "system-run",
31    "system-file-manager",
32    "preferences-desktop-theme",
33    "text-x-generic"
34 };
35
36 #define PREVIEW_SIZE (48)
37
38 E_Config_Dialog *
39 e_int_config_icon_themes(E_Container *con, const char *params __UNUSED__)
40 {
41    E_Config_Dialog *cfd;
42    E_Config_Dialog_View *v;
43
44    if (e_config_dialog_find("E", "appearance/icon_theme")) return NULL;
45    v = E_NEW(E_Config_Dialog_View, 1);
46
47    v->create_cfdata = _create_data;
48    v->free_cfdata = _free_data;
49    v->basic.create_widgets = _basic_create;
50    v->basic.apply_cfdata = _basic_apply;
51    v->basic.check_changed = _basic_check_changed;
52
53    cfd = e_config_dialog_new(con, _("Icon Theme Settings"), 
54                              "E", "appearance/icon_theme",
55                              "preferences-icon-theme", 0, v, NULL);
56    return cfd;
57 }
58
59 static void
60 _fill_data(E_Config_Dialog_Data *cfdata)
61 {
62    cfdata->icon_themes = efreet_icon_theme_list_get();
63    cfdata->icon_themes = eina_list_sort(cfdata->icon_themes, 
64                                         eina_list_count(cfdata->icon_themes),
65                                         _sort_icon_themes);
66 }
67
68 static void *
69 _create_data(E_Config_Dialog *cfd)
70 {
71    E_Config_Dialog_Data *cfdata;
72
73    cfdata = E_NEW(E_Config_Dialog_Data, 1);
74    cfdata->cfd = cfd;
75    cfdata->themename = eina_stringshare_add(e_config->icon_theme);
76    cfdata->overrides = e_config->icon_theme_overrides;
77    cfdata->enable_icon_theme = !!(e_config->icon_theme);   
78    return cfdata;
79 }
80
81 static void
82 _free_data(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
83 {
84    if (cfdata->fill_icon_themes_delayed)
85      free(ecore_idler_del(cfdata->fill_icon_themes_delayed));
86
87    eina_list_free(cfdata->icon_themes);
88    eina_stringshare_del(cfdata->themename);
89    E_FREE(cfdata);
90 }
91
92 static int
93 _basic_check_changed(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
94 {
95    if (cfdata->overrides != e_config->icon_theme_overrides)
96      return 1;
97
98    if (cfdata->enable_icon_theme != !!(e_config->icon_theme))
99      return 1;
100
101    if ((!cfdata->themename) && (!e_config->icon_theme))
102      return 0;
103
104    if ((!cfdata->themename) || (!e_config->icon_theme))
105      return 1;
106
107    return strcmp(cfdata->themename, e_config->icon_theme) != 0;
108 }
109
110 static int
111 _basic_apply(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
112 {
113    E_Event_Config_Icon_Theme *ev;
114
115    if (!_basic_check_changed(cfd, cfdata)) return 1;
116
117    eina_stringshare_del(e_config->icon_theme);
118    if (cfdata->enable_icon_theme)
119      e_config->icon_theme = eina_stringshare_ref(cfdata->themename);
120    else
121      e_config->icon_theme = NULL;
122    
123    e_config->icon_theme_overrides = !!cfdata->overrides;
124    e_config_save_queue();
125
126    e_util_env_set("E_ICON_THEME", e_config->icon_theme);
127
128    ev = E_NEW(E_Event_Config_Icon_Theme, 1);
129    if (ev)
130      {
131         ev->icon_theme = e_config->icon_theme;
132         ecore_event_add(E_EVENT_CONFIG_ICON_THEME, ev, NULL, NULL);
133      }
134    return 1;
135 }
136
137 static void
138 _populate_preview(E_Config_Dialog_Data *cfdata)
139 {
140    const char *t = cfdata->themename;
141    unsigned int i;
142
143    for (i = 0; i < sizeof(_icon_previews)/sizeof(_icon_previews[0]); i++)
144      {
145         const char *path;
146
147         if (!(path = efreet_icon_path_find(t, _icon_previews[i], PREVIEW_SIZE)))
148           continue;
149         if (e_icon_file_set(cfdata->gui.preview[i], path))
150           e_icon_fill_inside_set(cfdata->gui.preview[i], EINA_TRUE);
151      }
152 }
153
154 struct _fill_icon_themes_data
155 {
156    Eina_List *l;
157    int i;
158    Evas *evas;
159    E_Config_Dialog_Data *cfdata;
160    Eina_Bool themes_loaded;
161 };
162
163 static Eina_Bool
164 _fill_icon_themes(void *data)
165 {
166    struct _fill_icon_themes_data *d = data;
167    Efreet_Icon_Theme *theme;
168    Evas_Object *oc = NULL;
169    const char **example_icon, *example_icons[] = 
170      {
171         NULL,
172         "folder",
173         "user-home",
174         "text-x-generic",
175         "system-run",
176         "preferences-system",
177         NULL,
178      };
179
180    if (!d->themes_loaded)
181      {
182         d->cfdata->icon_themes = eina_list_free(d->cfdata->icon_themes);
183         _fill_data(d->cfdata);
184         d->l = d->cfdata->icon_themes;
185         d->i = 0;
186         d->themes_loaded = 1;
187         return ECORE_CALLBACK_RENEW;
188      }
189
190    if (!d->l)
191      {
192         int mw, mh;
193
194         e_widget_ilist_go(d->cfdata->gui.list);
195
196         e_widget_size_min_get(d->cfdata->gui.list, &mw, &mh);
197         e_widget_size_min_set(d->cfdata->gui.list, mw, 100);
198
199         d->cfdata->fill_icon_themes_delayed = NULL;
200         d->cfdata->populating = EINA_FALSE;
201         _populate_preview(d->cfdata);
202         free(d);
203         return ECORE_CALLBACK_CANCEL;
204      }
205
206    theme = d->l->data;
207    if (theme->example_icon)
208      {
209         example_icons[0] = theme->example_icon;
210         example_icon = example_icons;
211      }
212    else
213      example_icon = example_icons + 1;
214
215    for (; (*example_icon) && (!oc); example_icon++)
216      oc = _icon_new(d->evas, theme->name.internal, *example_icon, 24);
217
218    if (oc)
219      {
220         e_widget_ilist_append(d->cfdata->gui.list, oc, theme->name.name,
221                               NULL, NULL, theme->name.internal);
222         if ((d->cfdata->themename) && (theme->name.internal) &&
223             (strcmp(d->cfdata->themename, theme->name.internal) == 0))
224           e_widget_ilist_selected_set(d->cfdata->gui.list, d->i);
225      }
226
227    d->i++;
228    d->l = d->l->next;
229    return ECORE_CALLBACK_RENEW;
230 }
231
232 static void
233 _icon_theme_changed(void *data, Evas_Object *o __UNUSED__)
234 {
235    E_Config_Dialog_Data *cfdata;
236
237    cfdata = data;
238    if (cfdata->populating) return;
239    _populate_preview(cfdata);
240 }
241
242 static Evas_Object *
243 _basic_create(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
244 {
245    Evas_Object *o, *ilist, *checkbox, *ol;
246    struct _fill_icon_themes_data *d;
247    unsigned int i;
248
249    o = e_widget_list_add(evas, 0, 0);
250    ilist = e_widget_ilist_add(evas, 24, 24, &(cfdata->themename));
251    cfdata->gui.list = ilist;
252    e_widget_size_min_set(cfdata->gui.list, 100, 100);
253
254    cfdata->populating = EINA_TRUE;
255    e_widget_on_change_hook_set(ilist, _icon_theme_changed, cfdata);
256    e_widget_list_object_append(o, ilist, 1, 1, 0.5);
257
258    ol = e_widget_framelist_add(evas, _("Preview"), 1);
259    for (i = 0; i < sizeof(_icon_previews)/sizeof(_icon_previews[0]); i++)
260      {
261         cfdata->gui.preview[i] = e_icon_add(evas);
262         e_icon_preload_set(cfdata->gui.preview[i], EINA_TRUE);
263         e_icon_scale_size_set(cfdata->gui.preview[i], PREVIEW_SIZE);
264         e_widget_framelist_object_append_full(ol, cfdata->gui.preview[i], 
265                                               0, 0, 0, 0, 0.5, 0.5,
266                                               PREVIEW_SIZE, PREVIEW_SIZE, 
267                                               PREVIEW_SIZE, PREVIEW_SIZE);
268      }
269    e_widget_list_object_append(o, ol, 0, 0, 0.5);
270
271    checkbox = e_widget_check_add(evas, _("Enable icon theme"), 
272                                  &(cfdata->enable_icon_theme));
273    e_widget_on_change_hook_set(checkbox, _icon_theme_changed, cfdata);
274    e_widget_list_object_append(o, checkbox, 0, 0, 0.0);
275
276    checkbox = e_widget_check_add(evas, _("Icons override general theme"), 
277                                  &(cfdata->overrides));
278    e_widget_on_change_hook_set(checkbox, _icon_theme_changed, cfdata);
279    e_widget_list_object_append(o, checkbox, 0, 0, 0.0);
280
281    e_dialog_resizable_set(cfd->dia, 1);
282
283    if (cfdata->fill_icon_themes_delayed)
284      free(ecore_idler_del(cfdata->fill_icon_themes_delayed));
285
286    d = malloc(sizeof(*d));
287    d->l = NULL;
288    d->cfdata = cfdata;
289    d->themes_loaded = 0;
290    d->evas = evas;
291    cfdata->fill_icon_themes_delayed = ecore_idler_add(_fill_icon_themes, d);
292
293    return o;
294 }
295
296 static int
297 _sort_icon_themes(const void *data1, const void *data2)
298 {
299    const Efreet_Icon_Theme *m1, *m2;
300
301    if (!data2) return -1;
302
303    m1 = data1;
304    m2 = data2;
305
306    if (!m1->name.name) return 1;
307    if (!m2->name.name) return -1;
308
309    return (strcmp(m1->name.name, m2->name.name));
310 }
311
312 static Evas_Object *
313 _icon_new(Evas *evas, const char *theme, const char *icon, unsigned int size)
314 {
315    Evas_Object *o;
316    const char *path;
317
318    if (!(path = efreet_icon_path_find(theme, icon, size))) return NULL;
319    o = e_icon_add(evas);
320    if (e_icon_file_set(o, path))
321      e_icon_fill_inside_set(o, 1);
322    else
323      {
324         evas_object_del(o);
325         o = NULL;
326      }
327
328    return o;
329 }