Improve UI, add options to set toolbar icon size.
[lxde/lxappearance.git] / src / lxappearance2.c
CommitLineData
0cd84ed6
HJYP
1/*
2 * lxappearance2.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
15c155a6
HJYP
26#include "lxappearance2.h"
27
0cd84ed6
HJYP
28#include <gtk/gtk.h>
29#include <glib/gi18n.h>
15c155a6
HJYP
30
31#include <X11/X.h>
32#include <X11/Xatom.h>
33#include <X11/Xlib.h>
34#include <gdk/gdkx.h>
35
0cd84ed6
HJYP
36#include "widget-theme.h"
37#include "icon-theme.h"
38#include "cursor-theme.h"
e597424e 39#include "other.h"
0cd84ed6 40
15c155a6
HJYP
41LXAppearance app = {0};
42
43Atom lxsession_atom = 0;
44
45static void check_lxsession()
46{
47 lxsession_atom = XInternAtom( GDK_DISPLAY(), "_LXSESSION", True );
48 if( lxsession_atom != None )
49 {
50 XGrabServer( GDK_DISPLAY() );
51 if( XGetSelectionOwner( GDK_DISPLAY(), lxsession_atom ) )
52 app.use_lxsession = TRUE;
53 XUngrabServer( GDK_DISPLAY() );
54 }
55}
56
0cd84ed6
HJYP
57static GOptionEntry option_entries[] =
58{
59 { NULL }
60};
61
15c155a6
HJYP
62static void lxappearance_save_gtkrc()
63{
64
65}
66
67static void lxappearance_save_lxsession()
68{
69
70}
71
72static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
73{
74 switch(res)
75 {
76 case GTK_RESPONSE_APPLY:
77
78 if(app.use_lxsession)
79 lxappearance_save_lxsession();
80 else
81 lxappearance_save_gtkrc();
82
83 app.changed = FALSE;
84 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, FALSE);
85 break;
86 case 1: /* about dialog */
87 {
88 GtkBuilder* b = gtk_builder_new();
89 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
90 {
91 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
92 gtk_dialog_run(dlg);
93 gtk_widget_destroy(dlg);
94 }
95 g_object_unref(b);
96 }
97 break;
98 default:
99 gtk_main_quit();
100 }
101}
102
103static void settings_init()
104{
e597424e
HJYP
105 GtkSettings* settings = gtk_settings_get_default();
106 g_object_get(settings,
107 "gtk-theme-name", &app.widget_theme,
108 "gtk-icon-theme-name", &app.icon_theme,
109 "gtk-cursor-theme-name", &app.cursor_theme,
110 "gtk-toolbar-style", &app.toolbar_style,
111 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
112 NULL);
15c155a6 113 /* try to figure out cursor theme used. */
15c155a6
HJYP
114 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
115 {
116 /* get the real theme name from default. */
117 GKeyFile* kf = g_key_file_new();
118 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
119 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
120 g_free(fpath);
121
122 if(!ret)
123 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
124
125 if(ret)
126 {
e597424e 127 g_free(app.cursor_theme);
15c155a6
HJYP
128 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
129 g_debug("cursor theme name: %s", app.cursor_theme);
130 }
131 g_key_file_free(kf);
132 }
133}
134
0cd84ed6
HJYP
135int main(int argc, char** argv)
136{
137 GError* err = NULL;
138 GtkBuilder* b;
0cd84ed6
HJYP
139
140 /* gettext support */
141#ifdef ENABLE_NLS
142 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
143 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
144 textdomain ( GETTEXT_PACKAGE );
145#endif
146
147 /* init threading support */
148 /*
149 g_thread_init(NULL);
150 gdk_threads_init();
151 */
152
153 /* initialize GTK+ and parse the command line arguments */
154 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
155 {
156 g_print( "Error: %s\n", err->message );
157 return 1;
158 }
159
15c155a6
HJYP
160 /* check if we're under LXSession */
161 check_lxsession();
162
163 /* load config values */
164 settings_init();
165
0cd84ed6
HJYP
166 /* create GUI here */
167 b = gtk_builder_new();
168 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
169 return 1;
170
e597424e
HJYP
171 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
172
0cd84ed6
HJYP
173 widget_theme_init(b);
174 icon_theme_init(b);
175 cursor_theme_init(b);
e597424e 176 other_init(b);
0cd84ed6 177
15c155a6 178 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
0cd84ed6 179
15c155a6 180 gtk_window_present(GTK_WINDOW(app.dlg));
0cd84ed6
HJYP
181 g_object_unref(b);
182
183 gtk_main();
184
185 return 0;
186}
15c155a6
HJYP
187
188void lxappearance_changed()
189{
190 if(!app.changed)
191 {
192 app.changed = TRUE;
193 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, TRUE);
194 }
195}