]> icculus.org git repositories - dana/obconf.git/blob - src/appearance.c
split handlers.c into a bunch of files
[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 *appr_setup_font(GtkWidget *w, const gchar *place);
27 static RrFont *appr_set_font(GtkFontButton *w, const gchar *place);
28
29 void appr_setup_window_border(GtkWidget *w)
30 {
31     gboolean border;
32
33     mapping = TRUE;
34
35     border = tree_get_bool("theme/keepBorder", TRUE);
36     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), border);
37
38     mapping = FALSE;
39 }
40
41 void appr_setup_title_layout(GtkWidget *w)
42 {
43     gchar *layout;
44
45     mapping = TRUE;
46
47     layout = tree_get_string("theme/titleLayout", "NLIMC");
48     gtk_entry_set_text(GTK_ENTRY(w), layout);
49     preview_update_set_title_layout(layout);
50     g_free(layout);
51
52     mapping = FALSE;
53 }
54
55 void appr_setup_font_active(GtkWidget *w)
56 {
57     preview_update_set_active_font(appr_setup_font(w, "ActiveWindow"));
58 }
59
60 void appr_setup_font_inactive(GtkWidget *w)
61 {
62     preview_update_set_inactive_font(appr_setup_font(w, "InactiveWindow"));
63 }
64
65 void appr_setup_font_menu_header(GtkWidget *w)
66 {
67     preview_update_set_menu_header_font(appr_setup_font(w, "MenuHeader"));
68 }
69
70 void appr_setup_font_menu_item(GtkWidget *w)
71 {
72     preview_update_set_menu_item_font(appr_setup_font(w, "MenuItem"));
73 }
74
75 void appr_setup_font_display(GtkWidget *w)
76 {
77     preview_update_set_osd_font(appr_setup_font(w, "OnScreenDisplay"));
78 }
79
80 void on_window_border_toggled(GtkToggleButton *w, gpointer data)
81 {
82     gboolean b;
83
84     if (mapping) return;
85
86     b = gtk_toggle_button_get_active(w);
87     tree_set_bool("theme/keepBorder", b);
88 }
89
90 void on_title_layout_changed(GtkEntry *w, gpointer data)
91 {
92     gchar *layout;
93     gchar *it, *it2;
94     gboolean n, d, s, l, i, m, c;
95
96     if (mapping) return;
97
98     layout = g_strdup(gtk_entry_get_text(w));
99
100     n = d = s = l = i = m = c = FALSE;
101
102     for (it = layout; *it; ++it) {
103         gboolean *b;
104
105         switch (*it) {
106         case 'N':
107         case 'n':
108             b = &n;
109             break;
110         case 'd':
111         case 'D':
112             b = &d;
113             break;
114         case 's':
115         case 'S':
116             b = &s;
117             break;
118         case 'l':
119         case 'L':
120             b = &l;
121             break;
122         case 'i':
123         case 'I':
124             b = &i;
125             break;
126         case 'm':
127         case 'M':
128             b = &m;
129             break;
130         case 'c':
131         case 'C':
132             b = &c;
133             break;
134         default:
135             b = NULL;
136             break;
137         }
138
139         if (!b || *b) {
140             /* drop the letter */
141             for (it2 = it; *it2; ++it2)
142                 *it2 = *(it2+1);
143         } else {
144             *it = toupper(*it);
145             *b = TRUE;
146         }
147     }
148
149     gtk_entry_set_text(w, layout);
150     tree_set_string("theme/titleLayout", layout);
151     preview_update_set_title_layout(layout);
152
153     g_free(layout);
154 }
155
156 void on_font_active_font_set(GtkFontButton *w, gpointer data)
157 {
158     preview_update_set_active_font(appr_set_font(w, "ActiveWindow"));
159 }
160
161 void on_font_inactive_font_set(GtkFontButton *w, gpointer data)
162 {
163     preview_update_set_inactive_font(appr_set_font(w, "InactiveWindow"));
164 }
165
166 void on_font_menu_header_font_set(GtkFontButton *w, gpointer data)
167 {
168     preview_update_set_menu_header_font(appr_set_font(w, "MenuHeader"));
169 }
170
171 void on_font_menu_item_font_set(GtkFontButton *w, gpointer data)
172 {
173     preview_update_set_menu_item_font(appr_set_font(w, "MenuItem"));
174 }
175
176 void on_font_display_font_set(GtkFontButton *w, gpointer data)
177 {
178     preview_update_set_osd_font(appr_set_font(w, "OnScreenDisplay"));
179 }
180
181 static RrFont *appr_setup_font(GtkWidget *w, const gchar *place)
182 {
183     RrFont *font;
184     gchar *fontstring, *node;
185     gchar *name, **names;
186     gchar *size;
187     gchar *weight;
188     gchar *slant;
189
190     RrFontWeight rr_weight = RR_FONTWEIGHT_NORMAL;
191     RrFontSlant rr_slant = RR_FONTSLANT_NORMAL;
192
193     mapping = TRUE;
194
195     node = g_strdup_printf("theme/font:place=%s/name", place);
196     name = tree_get_string(node, "Sans");
197     g_free(node);
198
199     node = g_strdup_printf("theme/font:place=%s/size", place);
200     size = tree_get_string(node, "8");
201     g_free(node);
202
203     node = g_strdup_printf("theme/font:place=%s/weight", place);
204     weight = tree_get_string(node, "");
205     g_free(node);
206
207     node = g_strdup_printf("theme/font:place=%s/slant", place);
208     slant = tree_get_string(node, "");
209     g_free(node);
210
211     /* get only the first font in the string */
212     names = g_strsplit(name, ",", 0);
213     g_free(name);
214     name = g_strdup(names[0]);
215     g_strfreev(names);
216
217     /* don't use "normal" in the gtk string */
218     if (!g_ascii_strcasecmp(weight, "normal")) {
219         g_free(weight); weight = g_strdup("");
220     }
221     if (!g_ascii_strcasecmp(slant, "normal")) {
222         g_free(slant); slant = g_strdup("");
223     }
224
225     fontstring = g_strdup_printf("%s %s %s %s", name, weight, slant, size);
226     gtk_font_button_set_font_name(GTK_FONT_BUTTON(w), fontstring);
227
228     if (!g_ascii_strcasecmp(weight, "Bold")) rr_weight = RR_FONTWEIGHT_BOLD;
229     if (!g_ascii_strcasecmp(slant, "Italic")) rr_slant = RR_FONTSLANT_ITALIC;
230     if (!g_ascii_strcasecmp(slant, "Oblique")) rr_slant = RR_FONTSLANT_OBLIQUE;
231
232     font = RrFontOpen(rrinst, name, atoi(size), rr_weight, rr_slant);
233     g_free(fontstring);
234     g_free(slant);
235     g_free(weight);
236     g_free(size);
237     g_free(name);
238
239     mapping = FALSE;
240
241     return font;
242 }
243
244 static RrFont *appr_set_font(GtkFontButton *w, const gchar *place)
245 {
246     gchar *c;
247     gchar *font, *node;
248     const gchar *size = NULL;
249     const gchar *bold = NULL;
250     const gchar *italic = NULL;
251
252     RrFontWeight weight = RR_FONTWEIGHT_NORMAL;
253     RrFontSlant slant = RR_FONTSLANT_NORMAL;
254
255     if (mapping) return;
256
257     font = g_strdup(gtk_font_button_get_font_name(w));
258     while ((c = strrchr(font, ' '))) {
259         if (!bold && !italic && !size && atoi(c+1))
260             size = c+1;
261         else if (!bold && !italic && !g_ascii_strcasecmp(c+1, "italic"))
262             italic = c+1;
263         else if (!bold && !g_ascii_strcasecmp(c+1, "bold"))
264             bold = c+1;
265         else
266             break;
267         *c = '\0';
268     }
269     if (!bold) bold = "Normal";
270     if (!italic) italic = "Normal";
271
272     node = g_strdup_printf("theme/font:place=%s/name", place);
273     tree_set_string(node, font);
274     g_free(node);
275
276     node = g_strdup_printf("theme/font:place=%s/size", place);
277     tree_set_string(node, size);
278     g_free(node);
279
280     node = g_strdup_printf("theme/font:place=%s/weight", place);
281     tree_set_string(node, bold);
282     g_free(node);
283
284     node = g_strdup_printf("theme/font:place=%s/slant", place);
285     tree_set_string(node, italic);
286     g_free(node);
287
288     if (!g_ascii_strcasecmp(bold, "Bold")) weight = RR_FONTWEIGHT_BOLD;
289     if (!g_ascii_strcasecmp(italic, "Italic")) slant = RR_FONTSLANT_ITALIC;
290     if (!g_ascii_strcasecmp(italic, "Oblique")) slant = RR_FONTSLANT_OBLIQUE;
291
292     return RrFontOpen(rrinst, font, atoi(size), weight, slant);
293
294     g_free(font);
295
296 }