Implement UI for icon and cursor themes.
[lxde/lxappearance.git] / src / widget-theme.c
CommitLineData
0cd84ed6
HJYP
1/*
2 * widget-theme.c
3 *
4 * Copyright 2010 PCMan <pcman.tw@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
15c155a6 22#include "lxappearance2.h"
0cd84ed6 23#include "widget-theme.h"
15c155a6 24#include "color-scheme.h"
0cd84ed6
HJYP
25#include <string.h>
26
0cd84ed6
HJYP
27static GSList* load_themes_in_dir(const char* theme_dir, GSList* themes)
28{
29 GDir* dir = g_dir_open(theme_dir, 0, NULL);
30 if(dir)
31 {
32 const char* name;
33 while(name = g_dir_read_name(dir))
34 {
35 /* test if we already have this in list */
36 if(!g_slist_find_custom(themes, name, (GCompareFunc)strcmp))
37 {
38 /* test if this is a gtk theme */
39 char* gtkrc = g_build_filename(theme_dir, name, "gtk-2.0/gtkrc", NULL);
40 if(g_file_test(gtkrc, G_FILE_TEST_EXISTS))
15c155a6 41 themes = g_slist_prepend(themes, g_strdup(name));
0cd84ed6
HJYP
42 g_free(gtkrc);
43 }
44 }
45 g_dir_close(dir);
46 }
47 return themes;
48}
49
50static void on_sel_changed(GtkTreeSelection* sel, gpointer user_data)
51{
52 GtkTreeIter it;
53 GtkTreeModel* model;
54 if(gtk_tree_selection_get_selected(sel, &model, &it))
55 {
15c155a6
HJYP
56 g_free(app.widget_theme);
57 gtk_tree_model_get(model, &it, 0, &app.widget_theme, -1);
58 g_object_set(gtk_settings_get_default(), "gtk-theme-name", app.widget_theme, NULL);
59 lxappearance_changed();
60
61 /* check if current theme support color schemes. */
62 color_scheme_update();
0cd84ed6
HJYP
63 }
64}
65
66static void load_themes()
67{
68 char* dir;
69 GSList* themes = NULL, *l;
0cd84ed6
HJYP
70 GtkTreeIter sel_it = {0};
71 GtkTreeSelection* tree_sel;
72
0cd84ed6
HJYP
73 /* load user dir */
74 dir = g_build_filename(g_get_home_dir(), ".themes", NULL);
75 themes = load_themes_in_dir(dir, themes);
76 g_free(dir);
77
78 /* load system default */
79 dir = gtk_rc_get_theme_dir();
80 themes = load_themes_in_dir(dir, themes);
81 g_free(dir);
82
0cd84ed6
HJYP
83 themes = g_slist_sort(themes, (GCompareFunc)strcmp);
84 for(l = themes; l; l=l->next)
85 {
86 GtkTreeIter it;
87 char* name = (char*)l->data;
15c155a6 88 gtk_list_store_insert_with_values(app.widget_theme_store, &it, -1, 0, name, -1);
0cd84ed6
HJYP
89 /* if this theme is the one currently in use */
90 if(!sel_it.user_data)
91 {
15c155a6 92 if(strcmp(name, app.widget_theme) == 0)
0cd84ed6
HJYP
93 sel_it = it;
94 }
95 g_free(name);
96 }
0cd84ed6 97
15c155a6
HJYP
98 gtk_tree_view_set_model(app.widget_theme_view, GTK_TREE_MODEL(app.widget_theme_store));
99 tree_sel = gtk_tree_view_get_selection(app.widget_theme_view);
0cd84ed6
HJYP
100 if(sel_it.user_data)
101 gtk_tree_selection_select_iter(tree_sel, &sel_it);
102
15c155a6 103 g_slist_free(themes);
0cd84ed6
HJYP
104
105 g_signal_connect(tree_sel, "changed", G_CALLBACK(on_sel_changed), NULL);
106
107 /* FIXME: we need to handle this, too. */
108 // g_signal_connect(gtk_settings_get_default(), "notify::gtk-theme-name", G_CALLBACK(on_sel_changed), NULL);
109}
110
111void widget_theme_init(GtkBuilder* b)
112{
113 GtkWidget* demo;
114 GdkColor black = {0, 0, 0, 0};
115
116 demo = GTK_WIDGET(gtk_builder_get_object(b, "demo"));
15c155a6 117 app.widget_theme_view = GTK_WIDGET(gtk_builder_get_object(b, "widget_theme_view"));
0cd84ed6
HJYP
118
119 gtk_widget_modify_bg(demo, GTK_STATE_NORMAL, &black);
120
15c155a6
HJYP
121 app.widget_theme_store = gtk_list_store_new(1, G_TYPE_STRING);
122
0cd84ed6 123 /* load available themes */
0cd84ed6
HJYP
124 load_themes();
125}