]> icculus.org git repositories - dana/obconf.git/blob - src/appearance.c
fix new strings on the desktop tab
[dana/obconf.git] / src / appearance.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    appearance.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2003-2007   Dana Jansens
5    Copyright (c) 2003        Tim Riley
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "main.h"
21 #include "tree.h"
22 #include "preview_update.h"
23
24 static gboolean mapping = FALSE;
25
26 static RrFont *read_font(GtkFontButton *w, const gchar *place);
27 static RrFont *write_font(GtkFontButton *w, const gchar *place);
28
29 void appearance_setup_tab()
30 {
31     GtkWidget *w;
32     gchar *layout;
33     RrFont *f;
34
35     mapping = TRUE;
36
37     w = get_widget("window_border");
38     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
39                                  tree_get_bool("theme/keepBorder", TRUE));
40
41     w = get_widget("animate_iconify");
42     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
43                                  tree_get_bool("theme/animateIconify", TRUE));
44
45     w = get_widget("title_layout");
46     layout = tree_get_string("theme/titleLayout", "NLIMC");
47     gtk_entry_set_text(GTK_ENTRY(w), layout);
48     preview_update_set_title_layout(layout);
49     g_free(layout);
50
51     w = get_widget("font_active");
52     f = read_font(GTK_FONT_BUTTON(w), "ActiveWindow");
53     preview_update_set_active_font(f);
54
55     w = get_widget("font_inactive");
56     f = read_font(GTK_FONT_BUTTON(w), "InactiveWindow");
57     preview_update_set_inactive_font(f);
58
59     w = get_widget("font_menu_header");
60     f = read_font(GTK_FONT_BUTTON(w), "MenuHeader");
61     preview_update_set_menu_header_font(f);
62
63     w = get_widget("font_menu_item");
64     f = read_font(GTK_FONT_BUTTON(w), "MenuItem");
65     preview_update_set_menu_item_font(f);
66
67     w = get_widget("font_display");
68     f = read_font(GTK_FONT_BUTTON(w), "OnScreenDisplay");
69     preview_update_set_osd_font(f);
70
71     mapping = FALSE;
72 }
73
74 void on_window_border_toggled(GtkToggleButton *w, gpointer data)
75 {
76     gboolean b;
77
78     if (mapping) return;
79
80     b = gtk_toggle_button_get_active(w);
81     tree_set_bool("theme/keepBorder", b);
82 }
83
84 void on_animate_iconify_toggled(GtkToggleButton *w, gpointer data)
85 {
86     gboolean b;
87
88     if (mapping) return;
89
90     b = gtk_toggle_button_get_active(w);
91     tree_set_bool("theme/animateIconify", b);
92 }
93
94 void on_title_layout_changed(GtkEntry *w, gpointer data)
95 {
96     gchar *layout;
97     gchar *it, *it2;
98     gboolean n, d, s, l, i, m, c;
99
100     if (mapping) return;
101
102     layout = g_strdup(gtk_entry_get_text(w));
103
104     n = d = s = l = i = m = c = FALSE;
105
106     for (it = layout; *it; ++it) {
107         gboolean *b;
108
109         switch (*it) {
110         case 'N':
111         case 'n':
112             b = &n;
113             break;
114         case 'd':
115         case 'D':
116             b = &d;
117             break;
118         case 's':
119         case 'S':
120             b = &s;
121             break;
122         case 'l':
123         case 'L':
124             b = &l;
125             break;
126         case 'i':
127         case 'I':
128             b = &i;
129             break;
130         case 'm':
131         case 'M':
132             b = &m;
133             break;
134         case 'c':
135         case 'C':
136             b = &c;
137             break;
138         default:
139             b = NULL;
140             break;
141         }
142
143         if (!b || *b) {
144             /* drop the letter */
145             for (it2 = it; *it2; ++it2)
146                 *it2 = *(it2+1);
147         } else {
148             *it = toupper(*it);
149             *b = TRUE;
150         }
151     }
152
153     gtk_entry_set_text(w, layout);
154     tree_set_string("theme/titleLayout", layout);
155     preview_update_set_title_layout(layout);
156
157     g_free(layout);
158 }
159
160 void on_font_active_font_set(GtkFontButton *w, gpointer data)
161 {
162     if (mapping) return;
163
164     preview_update_set_active_font(write_font(w, "ActiveWindow"));
165 }
166
167 void on_font_inactive_font_set(GtkFontButton *w, gpointer data)
168 {
169     if (mapping) return;
170
171     preview_update_set_inactive_font(write_font(w, "InactiveWindow"));
172 }
173
174 void on_font_menu_header_font_set(GtkFontButton *w, gpointer data)
175 {
176     if (mapping) return;
177
178     preview_update_set_menu_header_font(write_font(w, "MenuHeader"));
179 }
180
181 void on_font_menu_item_font_set(GtkFontButton *w, gpointer data)
182 {
183     if (mapping) return;
184
185     preview_update_set_menu_item_font(write_font(w, "MenuItem"));
186 }
187
188 void on_font_display_font_set(GtkFontButton *w, gpointer data)
189 {
190     if (mapping) return;
191
192     preview_update_set_osd_font(write_font(w, "OnScreenDisplay"));
193 }
194
195 static RrFont *read_font(GtkFontButton *w, const gchar *place)
196 {
197     RrFont *font;
198     gchar *fontstring, *node;
199     gchar *name, **names;
200     gchar *size;
201     gchar *weight;
202     gchar *slant;
203
204     RrFontWeight rr_weight = RR_FONTWEIGHT_NORMAL;
205     RrFontSlant rr_slant = RR_FONTSLANT_NORMAL;
206
207     mapping = TRUE;
208
209     node = g_strdup_printf("theme/font:place=%s/name", place);
210     name = tree_get_string(node, "Sans");
211     g_free(node);
212
213     node = g_strdup_printf("theme/font:place=%s/size", place);
214     size = tree_get_string(node, "8");
215     g_free(node);
216
217     node = g_strdup_printf("theme/font:place=%s/weight", place);
218     weight = tree_get_string(node, "");
219     g_free(node);
220
221     node = g_strdup_printf("theme/font:place=%s/slant", place);
222     slant = tree_get_string(node, "");
223     g_free(node);
224
225     /* get only the first font in the string */
226     names = g_strsplit(name, ",", 0);
227     g_free(name);
228     name = g_strdup(names[0]);
229     g_strfreev(names);
230
231     /* don't use "normal" in the gtk string */
232     if (!g_ascii_strcasecmp(weight, "normal")) {
233         g_free(weight); weight = g_strdup("");
234     }
235     if (!g_ascii_strcasecmp(slant, "normal")) {
236         g_free(slant); slant = g_strdup("");
237     }
238
239     fontstring = g_strdup_printf("%s %s %s %s", name, weight, slant, size);
240     gtk_font_button_set_font_name(w, fontstring);
241
242     if (!g_ascii_strcasecmp(weight, "Bold")) rr_weight = RR_FONTWEIGHT_BOLD;
243     if (!g_ascii_strcasecmp(slant, "Italic")) rr_slant = RR_FONTSLANT_ITALIC;
244     if (!g_ascii_strcasecmp(slant, "Oblique")) rr_slant = RR_FONTSLANT_OBLIQUE;
245
246     font = RrFontOpen(rrinst, name, atoi(size), rr_weight, rr_slant);
247     g_free(fontstring);
248     g_free(slant);
249     g_free(weight);
250     g_free(size);
251     g_free(name);
252
253     mapping = FALSE;
254
255     return font;
256 }
257
258 static RrFont *write_font(GtkFontButton *w, const gchar *place)
259 {
260     gchar *c;
261     gchar *font, *node;
262     const gchar *size = NULL;
263     const gchar *bold = NULL;
264     const gchar *italic = NULL;
265
266     RrFontWeight weight = RR_FONTWEIGHT_NORMAL;
267     RrFontSlant slant = RR_FONTSLANT_NORMAL;
268
269     if (mapping) return;
270
271     font = g_strdup(gtk_font_button_get_font_name(w));
272     while ((c = strrchr(font, ' '))) {
273         if (!bold && !italic && !size && atoi(c+1))
274             size = c+1;
275         else if (!bold && !italic && !g_ascii_strcasecmp(c+1, "italic"))
276             italic = c+1;
277         else if (!bold && !g_ascii_strcasecmp(c+1, "bold"))
278             bold = c+1;
279         else
280             break;
281         *c = '\0';
282     }
283     if (!bold) bold = "Normal";
284     if (!italic) italic = "Normal";
285
286     node = g_strdup_printf("theme/font:place=%s/name", place);
287     tree_set_string(node, font);
288     g_free(node);
289
290     node = g_strdup_printf("theme/font:place=%s/size", place);
291     tree_set_string(node, size);
292     g_free(node);
293
294     node = g_strdup_printf("theme/font:place=%s/weight", place);
295     tree_set_string(node, bold);
296     g_free(node);
297
298     node = g_strdup_printf("theme/font:place=%s/slant", place);
299     tree_set_string(node, italic);
300     g_free(node);
301
302     if (!g_ascii_strcasecmp(bold, "Bold")) weight = RR_FONTWEIGHT_BOLD;
303     if (!g_ascii_strcasecmp(italic, "Italic")) slant = RR_FONTSLANT_ITALIC;
304     if (!g_ascii_strcasecmp(italic, "Oblique")) slant = RR_FONTSLANT_OBLIQUE;
305
306     return RrFontOpen(rrinst, font, atoi(size), weight, slant);
307
308     g_free(font);
309
310 }