Add new font configuration tab.
[lxde/lxappearance.git] / src / lxappearance.c
1 /*
2 * lxappearance.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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "lxappearance.h"
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 #include <X11/X.h>
32 #include <X11/Xatom.h>
33 #include <X11/Xlib.h>
34 #include <gdk/gdkx.h>
35 #include <string.h>
36
37 #include "widget-theme.h"
38 #include "color-scheme.h"
39 #include "icon-theme.h"
40 #include "cursor-theme.h"
41 #include "font.h"
42 #include "other.h"
43 #include "plugin.h"
44
45 LXAppearance app = {0};
46
47 Atom lxsession_atom = 0;
48 static const char* lxsession_name = NULL;
49
50 static void check_lxsession()
51 {
52 lxsession_atom = XInternAtom( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), "_LXSESSION", True );
53 if( lxsession_atom != None )
54 {
55 XGrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
56 if( XGetSelectionOwner( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), lxsession_atom ) )
57 {
58 app.use_lxsession = TRUE;
59 lxsession_name = g_getenv("DESKTOP_SESSION");
60 }
61 XUngrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
62 }
63 }
64
65 static GOptionEntry option_entries[] =
66 {
67 { NULL }
68 };
69
70 static void save_cursor_theme_name()
71 {
72 char* dir_path;
73 if(!app.cursor_theme || !g_strcmp0(app.cursor_theme, "default"))
74 return;
75
76 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
77 if(0 == g_mkdir_with_parents(dir_path, 0700))
78 {
79 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
80 char* content = g_strdup_printf(
81 "# This file is written by LXAppearance. Do not edit."
82 "[Icon Theme]\n"
83 "Name=Default\n"
84 "Comment=Default Cursor Theme\n"
85 "Inherits=%s\n", app.cursor_theme ? app.cursor_theme : "");
86 g_file_set_contents(index_theme, content, -1, NULL);
87 g_free(content);
88 g_free(index_theme);
89 }
90 g_free(dir_path);
91
92 /*
93 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
94 Xcursor.theme: name
95 Xcursor.size: [size]
96 g_file_set_contents(dir_path, "", -1, NULL);
97 g_free(dir_path);
98 */
99 }
100
101 static void reload_all_programs()
102 {
103 GdkEventClient event;
104 event.type = GDK_CLIENT_EVENT;
105 event.send_event = TRUE;
106 event.window = NULL;
107
108 if( app.use_lxsession )
109 {
110 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
111 event.data.b[0] = 0; /* LXS_RELOAD */
112 }
113 else
114 {
115 /* if( icon_only )
116 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
117 */
118 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
119 }
120 event.data_format = 8;
121 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
122 }
123
124 static const char* bool2str(gboolean val)
125 {
126 return val ? "TRUE" : "FALSE";
127 }
128
129 static void lxappearance_save_gtkrc()
130 {
131 static const char* tb_styles[]={
132 "GTK_TOOLBAR_ICONS",
133 "GTK_TOOLBAR_TEXT",
134 "GTK_TOOLBAR_BOTH",
135 "GTK_TOOLBAR_BOTH_HORIZ"
136 };
137 static const char* tb_icon_sizes[]={
138 "GTK_ICON_SIZE_INVALID",
139 "GTK_ICON_SIZE_MENU",
140 "GTK_ICON_SIZE_SMALL_TOOLBAR",
141 "GTK_ICON_SIZE_LARGE_TOOLBAR",
142 "GTK_ICON_SIZE_BUTTON",
143 "GTK_ICON_SIZE_DND",
144 "GTK_ICON_SIZE_DIALOG"
145 };
146
147 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
148 GString* content = g_string_sized_new(512);
149 g_string_append(content,
150 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
151 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n");
152 if(app.widget_theme)
153 g_string_append_printf(content,
154 "gtk-theme-name=\"%s\"\n", app.widget_theme);
155 if(app.icon_theme)
156 g_string_append_printf(content,
157 "gtk-icon-theme-name=\"%s\"\n", app.icon_theme);
158 if(app.default_font)
159 g_string_append_printf(content,
160 "gtk-font-name=\"%s\"\n", app.default_font);
161 if(app.cursor_theme)
162 g_string_append_printf(content,
163 "gtk-cursor-theme-name=\"%s\"\n", app.cursor_theme);
164 save_cursor_theme_name();
165
166 g_string_append_printf(content,
167 "gtk-cursor-theme-size=%d\n"
168 "gtk-toolbar-style=%s\n"
169 "gtk-toolbar-icon-size=%s\n"
170 "gtk-button-images=%d\n"
171 "gtk-menu-images=%d\n"
172 #if GTK_CHECK_VERSION(2, 14, 0)
173 "gtk-enable-event-sounds=%d\n"
174 "gtk-enable-input-feedback-sounds=%d\n"
175 #endif
176 "gtk-xft-antialias=%d\n"
177 "gtk-xft-hinting=%d\n"
178
179 , app.cursor_theme_size,
180 tb_styles[app.toolbar_style],
181 tb_icon_sizes[app.toolbar_icon_size],
182 app.button_images ? 1 : 0,
183 app.menu_images ? 1 : 0,
184 #if GTK_CHECK_VERSION(2, 14, 0)
185 app.enable_event_sound ? 1 : 0,
186 app.enable_input_feedback ? 1 : 0,
187 #endif
188 app.enable_antialising ? 1 : 0,
189 app.enable_hinting ? 1 : 0
190 );
191
192 if(app.hinting_style)
193 g_string_append_printf(content,
194 "gtk-xft-hintstyle=\"%s\"\n", app.hinting_style);
195
196 if(app.font_rgba)
197 g_string_append_printf(content,
198 "gtk-xft-rgba=\"%s\"\n", app.font_rgba);
199
200 if(app.color_scheme)
201 {
202 char* escaped = g_strescape(app.color_scheme, NULL);
203 g_string_append_printf(content,
204 "gtk-color-scheme=\"\"\n",
205 escaped);
206 g_free(escaped);
207 }
208
209 g_string_append_printf(content,
210 "include \"%s/.gtkrc-2.0.mine\"\n",
211 g_get_home_dir());
212
213 g_file_set_contents(file_path, content->str, content->len, NULL);
214 g_string_free(content, TRUE);
215 g_free(file_path);
216 }
217
218 static void lxappearance_save_lxsession()
219 {
220 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
221 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
222 char* buf;
223 gsize len;
224 GKeyFile* kf = g_key_file_new();
225
226 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
227 {
228 /* the user config file doesn't exist, create its parent dir */
229 len = strlen(user_config_file) - strlen("/desktop.conf");
230 user_config_file[len] = '\0';
231 g_debug("user_config_file = %s", user_config_file);
232 g_mkdir_with_parents(user_config_file, 0700);
233 user_config_file[len] = '/';
234
235 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
236 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
237 }
238
239 g_free(rel_path);
240
241 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
242 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
243
244 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
245
246 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
247
248 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
249 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
250 save_cursor_theme_name();
251
252 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
253 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
254
255 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
256 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
257
258 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
259 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
260
261 #if GTK_CHECK_VERSION(2, 14, 0)
262 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
263 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
264 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
265 #endif
266 g_key_file_set_integer( kf, "GTK", "iXft/Antialias", app.enable_antialising);
267 g_key_file_set_integer( kf, "GTK", "iXft/Hinting", app.enable_hinting);
268 g_key_file_set_string ( kf, "GTK", "sXft/HintStyle", app.hinting_style);
269 g_key_file_set_string ( kf, "GTK", "sXft/RGBA", app.font_rgba);
270
271 buf = g_key_file_to_data( kf, &len, NULL );
272 g_key_file_free(kf);
273
274 g_file_set_contents(user_config_file, buf, len, NULL);
275 g_free(buf);
276 g_free(user_config_file);
277 }
278
279 static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
280 {
281 switch(res)
282 {
283 case GTK_RESPONSE_APPLY:
284
285 if(app.use_lxsession)
286 lxappearance_save_lxsession();
287 else
288 lxappearance_save_gtkrc();
289
290 reload_all_programs();
291
292 app.changed = FALSE;
293 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
294 break;
295 case 1: /* about dialog */
296 {
297 GtkBuilder* b = gtk_builder_new();
298 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
299 {
300 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
301 gtk_dialog_run(GTK_DIALOG(dlg));
302 gtk_widget_destroy(dlg);
303 }
304 g_object_unref(b);
305 }
306 break;
307 default:
308 gtk_main_quit();
309 }
310 }
311
312 static void settings_init()
313 {
314 GtkSettings* settings = gtk_settings_get_default();
315 g_object_get(settings,
316 "gtk-theme-name", &app.widget_theme,
317 "gtk-font-name", &app.default_font,
318 "gtk-icon-theme-name", &app.icon_theme,
319 "gtk-cursor-theme-name", &app.cursor_theme,
320 "gtk-cursor-theme-size", &app.cursor_theme_size,
321 "gtk-toolbar-style", &app.toolbar_style,
322 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
323 "gtk-button-images", &app.button_images,
324 "gtk-menu-images", &app.menu_images,
325 #if GTK_CHECK_VERSION(2, 14, 0)
326 "gtk-enable-event-sounds", &app.enable_event_sound,
327 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
328 #endif
329 "gtk-xft-antialias", &app.enable_antialising,
330 "gtk-xft-hinting", &app.enable_hinting,
331 "gtk-xft-hintstyle", &app.hinting_style,
332 "gtk-xft-rgba", &app.font_rgba,
333 NULL);
334 /* try to figure out cursor theme used. */
335 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
336 {
337 /* get the real theme name from default. */
338 GKeyFile* kf = g_key_file_new();
339 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
340 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
341 g_free(fpath);
342
343 if(!ret)
344 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
345
346 if(ret)
347 {
348 g_free(app.cursor_theme);
349 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
350 g_debug("cursor theme name: %s", app.cursor_theme);
351 }
352 g_key_file_free(kf);
353 }
354
355 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
356 /* try to load custom color scheme if available */
357 if(app.use_lxsession)
358 {
359 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
360 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
361 GKeyFile* kf = g_key_file_new();
362 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
363 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
364 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
365 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
366 g_key_file_free(kf);
367 g_free(rel_path);
368 g_free(user_config_file);
369
370 if(app.color_scheme)
371 {
372 if(*app.color_scheme)
373 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
374 else
375 {
376 g_free(app.color_scheme);
377 app.color_scheme = NULL;
378 }
379 }
380 }
381 else
382 {
383 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
384 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
385 g_free(gtkrc_file);
386 if(g_hash_table_size(app.color_scheme_hash) > 0)
387 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
388 }
389 }
390
391 int main(int argc, char** argv)
392 {
393 GError* err = NULL;
394 GtkBuilder* b;
395
396 /* gettext support */
397 #ifdef ENABLE_NLS
398 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
399 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
400 textdomain ( GETTEXT_PACKAGE );
401 #endif
402
403 g_thread_init(NULL);
404 /* initialize GTK+ and parse the command line arguments */
405 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
406 {
407 g_print( "Error: %s\n", err->message );
408 return 1;
409 }
410
411 app.abi_version = LXAPPEARANCE_ABI_VERSION;
412
413 /* check if we're under LXSession */
414 check_lxsession();
415
416 /* create GUI here */
417 b = gtk_builder_new();
418 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
419 return 1;
420
421 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
422 * Some properties of GtkSettings class are installed by some other gtk classes.
423 * For example, "gtk-menu-images" property is actually installed by initialization
424 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
425 * are created, this property gets correctly registered. So later it can be read. */
426
427 /* load config values */
428 settings_init();
429
430 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
431
432 widget_theme_init(b);
433 color_scheme_init(b);
434 icon_theme_init(b);
435 cursor_theme_init(b);
436 font_init(b);
437 other_init(b);
438 /* the page for window manager plugins */
439 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
440
441 plugins_init(b);
442
443 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
444
445 gtk_window_present(GTK_WINDOW(app.dlg));
446 g_object_unref(b);
447
448 gtk_main();
449
450 plugins_finalize();
451
452 return 0;
453 }
454
455 void lxappearance_changed()
456 {
457 if(!app.changed)
458 {
459 app.changed = TRUE;
460 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
461 }
462 }
463
464 void on_check_button_toggled(GtkToggleButton* btn, gpointer user_data)
465 {
466 gboolean* val = (gboolean*)user_data;
467 gboolean new_val = gtk_toggle_button_get_active(btn);
468 if(new_val != *val)
469 {
470 *val = new_val;
471 lxappearance_changed();
472 }
473 }