Partially support color scheme.
[lxde/lxappearance.git] / src / color-scheme.c
CommitLineData
15c155a6
HJYP
1// color-scheme.c
2//
3// Copyright 2010 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18// MA 02110-1301, USA.
19
20#include "lxappearance2.h"
21#include "color-scheme.h"
22#include <string.h>
23
71ce58a7
HJYP
24/* http://live.gnome.org/GnomeArt/Tutorials/GtkThemes/SymbolicColors/#Default_colors_in_GNOME */
25static const char* gnome_color_names[] = {
26 "fg_color", /* The base for the foreground colors. */
27 "bg_color", /* Color to generate the background colors from. */
28 "base_color", /* The base color. */
29 "text_color", /* The text color in input widgets. */
30 "selected_bg_color", /* Color for the background of selected text. */
31 "selected_fg_color", /* Color of selected text. */
32 "tooltip_bg_color", /* Background color of tooltips. */
33 "tooltip_fg_color", /* Text color for text in tooltips. */
34};
35
36static char* hash_to_color_scheme_str(GHashTable* hash)
37{
38 GHashTableIter it;
39 char* key, *val;
40 GString* ret = g_string_sized_new(100);
41 g_hash_table_iter_init (&it, hash);
42 while(g_hash_table_iter_next(&it, (gpointer*)&key, (gpointer*)&val))
43 g_string_append_printf(ret, "%s:%s\n", key, val);
44 return g_string_free(ret, FALSE);
45}
46
47static void on_color_set(GtkColorButton* btn, const char* color_name)
48{
49 GdkColor clr;
50 char* color_str;
51 gtk_color_button_get_color(btn, &clr);
52 color_str = gdk_color_to_string(&clr);
53
54 g_hash_table_replace(app.color_scheme_hash, g_strdup(color_name), color_str);
55 g_free(app.color_scheme);
56 app.color_scheme = hash_to_color_scheme_str(app.color_scheme_hash);
57
58 g_object_set(gtk_settings_get_default(), "gtk-color-scheme", app.color_scheme, NULL);
59
60 lxappearance_changed();
61}
62
63static void on_reset_color_schemes()
64{
65 /* FIXME: How to correctly do this? */
66
67 /* this resets gtk color schemes to default. */
68 g_free(app.color_scheme);
69 app.color_scheme = NULL;
70
71 g_object_set(gtk_settings_get_default(), "gtk-color-scheme", "", NULL);
72 color_scheme_update();
73
74 lxappearance_changed();
75}
76
77void color_scheme_init(GtkBuilder* b)
15c155a6 78{
71ce58a7
HJYP
79 int i;
80 GtkWidget* btn = GTK_WIDGET(gtk_builder_get_object(b, "default_color_scheme"));
81 g_signal_connect(btn, "clicked", G_CALLBACK(on_reset_color_schemes), NULL);
82 /* FIXME: there is no good way to restore default colors yet. */
83 gtk_widget_hide(btn);
84
85 app.color_page = GTK_WIDGET(gtk_builder_get_object(b, "color_page"));
86
87 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
88
89 for(i = 0; i < 8; ++i)
90 app.color_btns[i] = GTK_WIDGET(gtk_builder_get_object(b, gnome_color_names[i]));;
91
15c155a6 92 color_scheme_update();
71ce58a7
HJYP
93
94 for(i = 0; i < 8; ++i)
95 g_signal_connect(app.color_btns[i], "color-set", G_CALLBACK(on_color_set), gnome_color_names[i]);
15c155a6
HJYP
96}
97
98static gboolean gtkrc_supports_colors(const char* gtkrc_file, gboolean* support)
99{
100 char* content;
101 gboolean support_colors = FALSE;
71ce58a7 102 g_debug("check: %s", gtkrc_file);
15c155a6
HJYP
103 if(g_file_get_contents(gtkrc_file, &content, NULL, NULL))
104 {
105 /* FIXME: check included gtkrc files, too. */
106 if(strstr(content, "gtk-color-scheme"))
107 support_colors = TRUE;
108 g_free(content);
109 }
71ce58a7
HJYP
110 else
111 return FALSE;
15c155a6 112 *support = support_colors;
d023f0a4 113 return TRUE;
15c155a6
HJYP
114}
115
116/* check if current gtk+ widget theme supports color schemes */
117void color_scheme_update()
118{
119 gboolean supported = FALSE;
120 if(app.widget_theme)
121 {
122 gboolean file_found;
123 char* gtkrc = g_build_filename(g_get_home_dir(), ".themes", app.widget_theme, "gtk-2.0/gtkrc", NULL);
124 file_found = gtkrc_supports_colors(gtkrc, &supported);
125 g_free(gtkrc);
126
127 if(!file_found)
128 {
129 gtkrc = g_build_filename(gtk_rc_get_theme_dir(), app.widget_theme, "gtk-2.0/gtkrc", NULL);
130 gtkrc_supports_colors(gtkrc, &supported);
131 g_free(gtkrc);
132 }
133 }
134
135 g_free(app.color_scheme);
136 if(supported)
137 {
71ce58a7
HJYP
138 char** pairs, **pair;
139 int i;
140
15c155a6
HJYP
141 g_object_get(gtk_settings_get_default(), "gtk-color-scheme", &app.color_scheme, NULL);
142 g_debug("color-scheme: %s", app.color_scheme);
71ce58a7
HJYP
143 gtk_widget_set_sensitive(app.color_page, TRUE);
144
145 g_hash_table_remove_all(app.color_scheme_hash);
146
147 /* split color scheme string into key/value pairs */
148 pairs = g_strsplit_set(app.color_scheme, "\n;", -1);
149 for(pair = pairs; *pair; ++pair)
150 {
151 char* name = strtok(*pair, ": \t");
152 if(name)
153 {
154 char* val = strtok(NULL, " \t");
155 if(val)
156 g_hash_table_replace(app.color_scheme_hash, g_strdup(name), g_strdup(val));
157 }
158 }
159 g_strfreev(pairs);
160
161 /* set the color to buttons */
162 for(i = 0; i < 8; ++i)
163 {
164 GtkWidget* btn = app.color_btns[i];
165 const char* color_str = (const char*)g_hash_table_lookup(app.color_scheme_hash, gnome_color_names[i]);
166 g_debug("%s ='%s'", gnome_color_names[i], color_str);
167 if(color_str)
168 {
169 GdkColor clr;
170 if(gdk_color_parse(color_str, &clr))
171 gtk_color_button_set_color(GTK_COLOR_BUTTON(btn), &clr);
172 gtk_widget_set_sensitive(btn, TRUE);
173 }
174 else
175 gtk_widget_set_sensitive(btn, FALSE);
176 }
15c155a6
HJYP
177 }
178 else
179 {
180 app.color_scheme = NULL;
71ce58a7 181 gtk_widget_set_sensitive(app.color_page, FALSE);
15c155a6
HJYP
182 g_debug("color-scheme is not supported by this theme");
183 }
184}
185