150825823302b7c9ae0d398c97a5cb4743acef9e
[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 #include <glib/gstdio.h>
31
32 #include <X11/X.h>
33 #include <X11/Xatom.h>
34 #include <X11/Xlib.h>
35 #include <gdk/gdkx.h>
36 #include <string.h>
37
38 #if ENABLE_DBUS
39 #include <dbus/dbus.h>
40 #endif
41
42 #include "widget-theme.h"
43 #include "color-scheme.h"
44 #include "icon-theme.h"
45 #include "cursor-theme.h"
46 #include "font.h"
47 #include "other.h"
48 #include "plugin.h"
49
50 LXAppearance app = {0};
51
52 Atom lxsession_atom = 0;
53 static const char* lxsession_name = NULL;
54
55 /* Dbus functions Copy from lxsession-logout
56 TODO Create a library fro this ?
57 */
58
59 static gboolean check_lxde_dbus()
60 {
61 #if ENABLE_DBUS
62 DBusError error;
63 dbus_error_init(&error);
64 DBusConnection * connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
65 if (connection == NULL)
66 {
67 g_warning(G_STRLOC ": Failed to connect to the session message bus: %s", error.message);
68 dbus_error_free(&error);
69 return FALSE;
70 }
71
72 dbus_bool_t ret = dbus_bus_name_has_owner(connection,"org.lxde.SessionManager",NULL);
73
74 if (ret == TRUE)
75 {
76 return TRUE;
77 }
78 else
79 {
80 return FALSE;
81 }
82 #else
83 return FALSE;
84 #endif
85 }
86
87 static void check_lxsession()
88 {
89 lxsession_atom = XInternAtom( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), "_LXSESSION", True );
90 if( lxsession_atom != None )
91 {
92 XGrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
93 if( XGetSelectionOwner( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), lxsession_atom ) )
94 {
95 app.use_lxsession = TRUE;
96 lxsession_name = g_getenv("DESKTOP_SESSION");
97 }
98 XUngrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
99 }
100
101 /* Check Lxsession also with dbus */
102 if (check_lxde_dbus())
103 {
104 app.use_lxsession = TRUE;
105 lxsession_name = g_getenv("DESKTOP_SESSION");
106 }
107
108 }
109
110 static GOptionEntry option_entries[] =
111 {
112 { NULL }
113 };
114
115 static gboolean verify_cursor_theme(GKeyFile *kf, const char *cursor_theme,
116 const char *test)
117 {
118 char *fpath;
119 gboolean ret;
120
121 /* get the inherited theme name. */
122 fpath = g_build_filename(g_get_home_dir(), ".icons", cursor_theme,
123 "index.theme", NULL);
124 ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
125 g_free(fpath);
126
127 fpath = g_build_filename("icons", cursor_theme, "index.theme", NULL);
128 if (!ret)
129 ret = g_key_file_load_from_data_dirs(kf, fpath, NULL, 0, NULL);
130 g_free(fpath);
131
132 if (ret)
133 {
134 fpath = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
135 if (fpath == NULL) /* end of chain, success */
136 return TRUE;
137 if (strcmp(fpath, test) == 0) /* recursion */
138 ret = FALSE;
139 else if (!verify_cursor_theme(kf, fpath, test)) /* recursion */
140 ret = FALSE;
141 else /* check recursion against this one too */
142 ret = verify_cursor_theme(kf, fpath, cursor_theme);
143 g_free(fpath);
144 }
145 return ret;
146 }
147
148 static void save_cursor_theme_name()
149 {
150 char* dir_path;
151 GKeyFile* kf;
152
153 if (app.cursor_theme == NULL || strcmp(app.cursor_theme, "default") == 0)
154 return;
155
156 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
157 kf = g_key_file_new();
158 /* test if cursor theme isn't recursed and don't use it otherwise */
159 if (!verify_cursor_theme(kf, app.cursor_theme, "default"))
160 {
161 g_free(app.cursor_theme);
162 app.cursor_theme = NULL; /* FIXME: replace with "default"? */
163 /* FIXME: show an error message */
164 }
165 /* SF bug #614: ~/.icons/default may be symlink so remove symlink */
166 else if (g_file_test(dir_path, G_FILE_TEST_IS_SYMLINK) &&
167 g_unlink(dir_path) != 0)
168 {
169 /* FIXME: show an error message */
170 }
171 else if (0 == g_mkdir_with_parents(dir_path, 0700))
172 {
173 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
174 char* content = g_strdup_printf(
175 "# This file is written by LXAppearance. Do not edit.\n"
176 "[Icon Theme]\n"
177 "Name=Default\n"
178 "Comment=Default Cursor Theme\n"
179 "Inherits=%s\n", app.cursor_theme);
180 g_file_set_contents(index_theme, content, -1, NULL);
181 g_free(content);
182 g_free(index_theme);
183 }
184 g_key_file_free(kf);
185 g_free(dir_path);
186
187 /*
188 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
189 Xcursor.theme: name
190 Xcursor.size: [size]
191 g_file_set_contents(dir_path, "", -1, NULL);
192 g_free(dir_path);
193 */
194 }
195
196 static void reload_all_programs()
197 {
198 #if GTK_CHECK_VERSION(3, 0, 0)
199
200 /* TODO Port this to something else than gdk_event_send_clientmessage_toall */
201
202 #else
203 GdkEventClient event;
204 event.type = GDK_CLIENT_EVENT;
205 event.send_event = TRUE;
206 event.window = NULL;
207
208 if( app.use_lxsession )
209 {
210 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
211 event.data.b[0] = 0; /* LXS_RELOAD */
212 }
213 else
214 {
215 /* if( icon_only )
216 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
217 */
218 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
219 }
220 event.data_format = 8;
221 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
222 #endif
223 }
224
225 static void lxappearance_save_gtkrc()
226 {
227 static const char* tb_styles[]={
228 "GTK_TOOLBAR_ICONS",
229 "GTK_TOOLBAR_TEXT",
230 "GTK_TOOLBAR_BOTH",
231 "GTK_TOOLBAR_BOTH_HORIZ"
232 };
233 static const char* tb_icon_sizes[]={
234 "GTK_ICON_SIZE_INVALID",
235 "GTK_ICON_SIZE_MENU",
236 "GTK_ICON_SIZE_SMALL_TOOLBAR",
237 "GTK_ICON_SIZE_LARGE_TOOLBAR",
238 "GTK_ICON_SIZE_BUTTON",
239 "GTK_ICON_SIZE_DND",
240 "GTK_ICON_SIZE_DIALOG"
241 };
242
243 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
244 GString* content = g_string_sized_new(512);
245 g_string_append(content,
246 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
247 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n");
248
249 /* include ~/.gtkrc-2.0.mine first to be able to apply changes done
250 by LXAppearance if the same settings exist in that file */
251 g_string_append_printf(content,
252 "include \"%s/.gtkrc-2.0.mine\"\n",
253 g_get_home_dir());
254
255 if(app.widget_theme)
256 g_string_append_printf(content,
257 "gtk-theme-name=\"%s\"\n", app.widget_theme);
258 if(app.icon_theme)
259 g_string_append_printf(content,
260 "gtk-icon-theme-name=\"%s\"\n", app.icon_theme);
261 if(app.default_font)
262 g_string_append_printf(content,
263 "gtk-font-name=\"%s\"\n", app.default_font);
264 if(app.cursor_theme)
265 g_string_append_printf(content,
266 "gtk-cursor-theme-name=\"%s\"\n", app.cursor_theme);
267 save_cursor_theme_name();
268
269 g_string_append_printf(content,
270 "gtk-cursor-theme-size=%d\n"
271 "gtk-toolbar-style=%s\n"
272 "gtk-toolbar-icon-size=%s\n"
273 "gtk-button-images=%d\n"
274 "gtk-menu-images=%d\n"
275 #if GTK_CHECK_VERSION(2, 14, 0)
276 "gtk-enable-event-sounds=%d\n"
277 "gtk-enable-input-feedback-sounds=%d\n"
278 #endif
279 "gtk-xft-antialias=%d\n"
280 "gtk-xft-hinting=%d\n"
281
282 , app.cursor_theme_size,
283 tb_styles[app.toolbar_style],
284 tb_icon_sizes[app.toolbar_icon_size],
285 app.button_images ? 1 : 0,
286 app.menu_images ? 1 : 0,
287 #if GTK_CHECK_VERSION(2, 14, 0)
288 app.enable_event_sound ? 1 : 0,
289 app.enable_input_feedback ? 1 : 0,
290 #endif
291 app.enable_antialising ? 1 : 0,
292 app.enable_hinting ? 1 : 0
293 );
294
295 if(app.hinting_style)
296 g_string_append_printf(content,
297 "gtk-xft-hintstyle=\"%s\"\n", app.hinting_style);
298
299 if(app.font_rgba)
300 g_string_append_printf(content,
301 "gtk-xft-rgba=\"%s\"\n", app.font_rgba);
302
303 if(app.modules && app.modules[0])
304 g_string_append_printf(content, "gtk-modules=\"%s\"\n", app.modules);
305
306 #if 0
307 /* unfortunately we cannot set colors without XSETTINGS daemon,
308 themes will override any custom settings in .gtkrc-2.0 file */
309 /* FIXME: we should support other xsettings daemons too */
310 if(app.color_scheme)
311 {
312 char* escaped = g_strescape(app.color_scheme, NULL);
313 g_string_append_printf(content,
314 "gtk-color-scheme=\"%s\"\n",
315 escaped);
316 g_free(escaped);
317 }
318 #endif
319
320 g_file_set_contents(file_path, content->str, content->len, NULL);
321
322 /* Save also in GTK3 folder
323 Content shold be different from the gtk2 one
324 */
325 GKeyFile *content_gtk3 = g_key_file_new();
326 char* file_path_gtk3 = g_build_filename(g_get_user_config_dir(), "gtk-3.0", NULL);
327 char* file_path_settings = g_build_filename(file_path_gtk3, "settings.ini", NULL);
328
329 if (!g_file_test(file_path_gtk3, G_FILE_TEST_IS_DIR))
330 {
331 g_mkdir_with_parents(file_path_gtk3, 0755);
332 }
333
334 g_key_file_load_from_file(content_gtk3, file_path_settings, /* ignoring errors */
335 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
336
337 if(app.widget_theme)
338 g_key_file_set_string(content_gtk3, "Settings",
339 "gtk-theme-name", app.widget_theme);
340 if(app.icon_theme)
341 g_key_file_set_string(content_gtk3, "Settings",
342 "gtk-icon-theme-name", app.icon_theme);
343 if(app.default_font)
344 g_key_file_set_string(content_gtk3, "Settings",
345 "gtk-font-name", app.default_font);
346 if(app.cursor_theme)
347 g_key_file_set_string(content_gtk3, "Settings",
348 "gtk-cursor-theme-name", app.cursor_theme);
349 save_cursor_theme_name();
350
351 g_key_file_set_integer(content_gtk3, "Settings",
352 "gtk-cursor-theme-size", app.cursor_theme_size);
353 g_key_file_set_string(content_gtk3, "Settings",
354 "gtk-toolbar-style", tb_styles[app.toolbar_style]);
355 g_key_file_set_string(content_gtk3, "Settings",
356 "gtk-toolbar-icon-size", tb_icon_sizes[app.toolbar_icon_size]);
357 g_key_file_set_integer(content_gtk3, "Settings",
358 "gtk-button-images", app.button_images ? 1 : 0);
359 g_key_file_set_integer(content_gtk3, "Settings",
360 "gtk-menu-images", app.menu_images ? 1 : 0);
361 #if GTK_CHECK_VERSION(2, 14, 0)
362 g_key_file_set_integer(content_gtk3, "Settings",
363 "gtk-enable-event-sounds", app.enable_event_sound ? 1 : 0);
364 g_key_file_set_integer(content_gtk3, "Settings",
365 "gtk-enable-input-feedback-sounds", app.enable_input_feedback ? 1 : 0);
366 #endif
367 g_key_file_set_integer(content_gtk3, "Settings",
368 "gtk-xft-antialias", app.enable_antialising ? 1 : 0);
369 g_key_file_set_integer(content_gtk3, "Settings",
370 "gtk-xft-hinting", app.enable_hinting ? 1 : 0);
371
372 if(app.hinting_style)
373 g_key_file_set_string(content_gtk3, "Settings",
374 "gtk-xft-hintstyle", app.hinting_style);
375
376 if(app.font_rgba)
377 g_key_file_set_string(content_gtk3, "Settings",
378 "gtk-xft-rgba", app.font_rgba);
379
380 if(app.modules && app.modules[0])
381 g_key_file_set_string(content_gtk3, "Settings", "gtk-modules", app.modules);
382 else
383 g_key_file_remove_key(content_gtk3, "Settings", "gtk-modules", NULL);
384
385 #if 0
386 /* unfortunately we cannot set colors without XSETTINGS daemon,
387 themes will override any custom settings in .gtkrc-2.0 file */
388 /* FIXME: we should support other xsettings daemons too */
389 if(app.color_scheme)
390 {
391 char* escaped = g_strescape(app.color_scheme, NULL);
392 g_string_append_printf(content_gtk3,
393 "gtk-color-scheme=%s\n",
394 escaped);
395 g_free(escaped);
396 }
397 #endif
398
399 #if GLIB_CHECK_VERSION(2, 40, 0)
400 g_key_file_save_to_file(content_gtk3, file_path_settings, NULL);
401 #else
402 char *contents;
403 gsize s;
404
405 contents = g_key_file_to_data(content_gtk3, &s, NULL);
406 if (contents)
407 g_file_set_contents(file_path_settings, contents, s, NULL);
408 g_free(contents);
409 #endif
410
411 g_free(file_path_gtk3);
412 g_free(file_path_settings);
413 g_string_free(content, TRUE);
414 g_key_file_free(content_gtk3);
415 g_free(file_path);
416 }
417
418 static void lxappearance_save_lxsession()
419 {
420 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
421 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
422 char* buf;
423 gsize len;
424 GKeyFile* kf = g_key_file_new();
425
426 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
427 {
428 /* the user config file doesn't exist, create its parent dir */
429 len = strlen(user_config_file) - strlen("/desktop.conf");
430 user_config_file[len] = '\0';
431 g_debug("user_config_file = %s", user_config_file);
432 g_mkdir_with_parents(user_config_file, 0700);
433 user_config_file[len] = '/';
434
435 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
436 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
437 }
438
439 g_free(rel_path);
440
441 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
442 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
443
444 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
445
446 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
447
448 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
449 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
450 save_cursor_theme_name();
451
452 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
453 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
454
455 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
456 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
457
458 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
459 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
460
461 #if GTK_CHECK_VERSION(2, 14, 0)
462 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
463 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
464 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
465 #endif
466 g_key_file_set_integer( kf, "GTK", "iXft/Antialias", app.enable_antialising);
467 g_key_file_set_integer( kf, "GTK", "iXft/Hinting", app.enable_hinting);
468 g_key_file_set_string( kf, "GTK", "sXft/HintStyle", app.hinting_style);
469 g_key_file_set_string( kf, "GTK", "sXft/RGBA", app.font_rgba);
470
471 buf = g_key_file_to_data( kf, &len, NULL );
472 g_key_file_free(kf);
473
474 g_file_set_contents(user_config_file, buf, len, NULL);
475 g_free(buf);
476 g_free(user_config_file);
477 }
478
479 static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
480 {
481 switch(res)
482 {
483 case GTK_RESPONSE_APPLY:
484
485 if(app.use_lxsession)
486 lxappearance_save_lxsession();
487 else
488 lxappearance_save_gtkrc();
489
490 reload_all_programs();
491
492 app.changed = FALSE;
493 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
494 break;
495 case 1: /* about dialog */
496 {
497 GtkBuilder* b = gtk_builder_new();
498 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
499 {
500 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
501 gtk_dialog_run(GTK_DIALOG(dlg));
502 gtk_widget_destroy(dlg);
503 }
504 g_object_unref(b);
505 }
506 break;
507 default:
508 gtk_main_quit();
509 }
510 }
511
512 static void settings_init()
513 {
514 GtkSettings* settings = gtk_settings_get_default();
515 g_object_get(settings,
516 "gtk-theme-name", &app.widget_theme,
517 "gtk-font-name", &app.default_font,
518 "gtk-icon-theme-name", &app.icon_theme,
519 "gtk-cursor-theme-name", &app.cursor_theme,
520 "gtk-cursor-theme-size", &app.cursor_theme_size,
521 "gtk-toolbar-style", &app.toolbar_style,
522 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
523 "gtk-button-images", &app.button_images,
524 "gtk-menu-images", &app.menu_images,
525 #if GTK_CHECK_VERSION(2, 14, 0)
526 "gtk-enable-event-sounds", &app.enable_event_sound,
527 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
528 #endif
529 "gtk-xft-antialias", &app.enable_antialising,
530 "gtk-xft-hinting", &app.enable_hinting,
531 "gtk-xft-hintstyle", &app.hinting_style,
532 "gtk-xft-rgba", &app.font_rgba,
533 "gtk-modules", &app.modules,
534 NULL);
535 /* try to figure out cursor theme used. */
536 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
537 {
538 /* get the real theme name from default. */
539 GKeyFile* kf = g_key_file_new();
540 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
541 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
542 g_free(fpath);
543
544 if(!ret)
545 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
546
547 if(ret)
548 {
549 g_free(app.cursor_theme);
550 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
551 g_debug("cursor theme name: %s", app.cursor_theme);
552 }
553 g_key_file_free(kf);
554 }
555
556 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
557 /* try to load custom color scheme if available */
558 if(app.use_lxsession)
559 {
560 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
561 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
562 GKeyFile* kf = g_key_file_new();
563 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
564 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
565 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
566 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
567 g_key_file_free(kf);
568 g_free(rel_path);
569 g_free(user_config_file);
570
571 if(app.color_scheme)
572 {
573 if(*app.color_scheme)
574 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
575 else
576 {
577 g_free(app.color_scheme);
578 app.color_scheme = NULL;
579 }
580 }
581 }
582 #if 0
583 /* unfortunately we cannot set colors without XSETTINGS daemon,
584 themes will override any custom settings in .gtkrc-2.0 file */
585 /* FIXME: we should support other xsettings daemons too */
586 else
587 {
588 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
589 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
590 g_free(gtkrc_file);
591 if(g_hash_table_size(app.color_scheme_hash) > 0)
592 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
593 }
594 #endif
595 }
596
597 int main(int argc, char** argv)
598 {
599 GError* err = NULL;
600 GtkBuilder* b;
601
602 /* gettext support */
603 #ifdef ENABLE_NLS
604 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
605 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
606 textdomain ( GETTEXT_PACKAGE );
607 #endif
608
609 #if !GLIB_CHECK_VERSION(2, 32, 0)
610 g_thread_init(NULL);
611 #endif
612 /* initialize GTK+ and parse the command line arguments */
613 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
614 {
615 g_print( "Error: %s\n", err->message );
616 return 1;
617 }
618
619 app.abi_version = LXAPPEARANCE_ABI_VERSION;
620
621 /* check if we're under LXSession */
622 check_lxsession();
623
624 /* create GUI here */
625 b = gtk_builder_new();
626 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
627 return 1;
628
629 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
630 * Some properties of GtkSettings class are installed by some other gtk classes.
631 * For example, "gtk-menu-images" property is actually installed by initialization
632 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
633 * are created, this property gets correctly registered. So later it can be read. */
634
635 /* load config values */
636 settings_init();
637
638 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
639
640 widget_theme_init(b);
641 color_scheme_init(b);
642 icon_theme_init(b);
643 cursor_theme_init(b);
644 font_init(b);
645 other_init(b);
646 /* the page for window manager plugins */
647 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
648
649 plugins_init(b);
650
651 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
652
653 gtk_window_present(GTK_WINDOW(app.dlg));
654 g_object_unref(b);
655
656 gtk_main();
657
658 plugins_finalize();
659
660 return 0;
661 }
662
663 void lxappearance_changed()
664 {
665 if(!app.changed)
666 {
667 app.changed = TRUE;
668 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
669 }
670 }
671
672 void on_check_button_toggled(GtkToggleButton* btn, gpointer user_data)
673 {
674 gboolean* val = (gboolean*)user_data;
675 gboolean new_val = gtk_toggle_button_get_active(btn);
676 if(new_val != *val)
677 {
678 *val = new_val;
679 lxappearance_changed();
680 }
681 }