Fix memory leaks and compiler warnings.
[lxde/lxappearance.git] / src / main-dlg.c
CommitLineData
f57233d5
HJYP
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <gtk/gtk.h>
a0647843 6#include <gdk/gdkx.h>
5ef7a66e 7#include <glib/gi18n.h>
a0647843
HJYP
8
9#include <stdio.h>
42bce29e
HJYP
10#include <stdlib.h>
11#include <unistd.h>
684d617e 12#include <string.h>
f57233d5 13
a0647843
HJYP
14/* for kill & waitpid */
15#include <sys/types.h>
16#include <signal.h>
17#include <sys/wait.h>
18
684d617e 19#include "main-dlg.h"
850bd787 20#include "demo.h"
684d617e 21
3e21f6eb
HJYP
22enum {
23 COL_DISP_NAME,
24 COL_NAME,
25 N_COLS
26};
27
5ef7a66e
HJYP
28#define LOOKUP(name) gtk_builder_get_object( builder, #name )
29#define GET_WIDGET_WITH_TYPE(name, type) name = type(LOOKUP(name))
684d617e 30
c0c2320a 31#define INIT_LIST(name, prop) \
14a9f0b4
MTW
32 GET_WIDGET_WITH_TYPE( name##_view, GTK_TREE_VIEW ); \
33 name##_list = init_tree_view( name##_view, G_CALLBACK(on_list_sel_changed), prop); \
da434a12 34 load_##name##s( name##_list, name##_name );
c0c2320a 35
14a9f0b4
MTW
36#define enable_apply() gtk_dialog_set_response_sensitive(GTK_DIALOG (main_dlg), GTK_RESPONSE_APPLY, TRUE )
37#define disable_apply() gtk_dialog_set_response_sensitive(GTK_DIALOG (main_dlg), GTK_RESPONSE_APPLY, FALSE )
684d617e 38
bf6886d5 39extern gboolean under_lxsession; /* wether we are under lxsession */
6b1f0c67 40
684d617e
HJYP
41extern GtkWidget* main_dlg; /* defined in main.c */
42
c0c2320a 43static GtkTreeView* gtk_theme_view = NULL;
10002786 44static GtkListStore* gtk_theme_list = NULL;
c0c2320a
HJYP
45
46static GtkTreeView* icon_theme_view = NULL;
10002786 47static GtkListStore* icon_theme_list = NULL;
c0c2320a 48
938e9a87 49#if CURSOR_THEME
966feb70
MTW
50static GtkTreeView* cursor_theme_view = NULL;
51static GtkListStore* cursor_theme_list = NULL;
938e9a87 52#endif
966feb70 53
a0647843
HJYP
54static char* gtk_theme_name = NULL;
55static char* icon_theme_name = NULL;
56static char* font_name = NULL;
966feb70
MTW
57static char* cursor_theme_name = NULL;
58static gint cursor_theme_size = 0;
da434a12 59static GtkToolbarStyle tb_style = GTK_TOOLBAR_BOTH;
a0647843 60
da434a12 61extern char tmp_rc_file[];
ddac316c 62static char* rc_file = NULL;
a0647843
HJYP
63
64/*
c0c2320a 65static GtkTreeView* font_view = NULL;
10002786 66static GtkListStore* font_list = NULL;
a0647843 67*/
8c98cd54 68static GtkWidget* demo_box = NULL;
a0647843
HJYP
69static GtkWidget* demo_socket = NULL;
70static GPid demo_pid = 0;
c0c2320a 71
bf6886d5 72static const char* session_name = NULL;
e4ff4ccd 73
bf6886d5 74extern Atom lxsession_atom; /* defined in main.c */
e4ff4ccd 75
ac36e6fa 76static void reload_demo_process()
8c98cd54
HJYP
77{
78 char* argv[5];
79 char wid[16];
80
81 if( demo_pid > 0 ) /* kill old demo */
82 {
83 int stat;
8c98cd54
HJYP
84 kill( demo_pid, SIGTERM );
85 waitpid( demo_pid, &stat, 0 );
86 demo_pid = 0;
87 }
88
14a9f0b4 89 g_snprintf( wid, 16, "%ld", gtk_socket_get_id(GTK_SOCKET (demo_socket)) );
8c98cd54
HJYP
90
91 argv[0] = g_get_prgname();
92 argv[1] = "demo";
93 argv[2] = wid;
94 argv[3] = tmp_rc_file;
95 argv[4] = NULL;
96 g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &demo_pid, NULL );
684d617e
HJYP
97
98 /* reloading demo means the current selected theme is changed */
99 enable_apply();
8c98cd54
HJYP
100}
101
a0647843 102static void write_rc_file( const char* path )
10002786 103{
a0647843 104 FILE* f;
a7477b61
HJYP
105 static char* tb_styles[] = {
106 "GTK_TOOLBAR_ICONS",
107 "GTK_TOOLBAR_TEXT",
108 "GTK_TOOLBAR_BOTH",
109 "GTK_TOOLBAR_BOTH_HORIZ"
110 };
111
a0647843 112 if( f = fopen( path, "w" ) )
10002786 113 {
a0647843 114 fputs( "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
42bce29e 115 "# Any customization should be done in ~/.gtkrc-2.0.mine\n\n", f );
a0647843
HJYP
116
117 fprintf( f, "gtk-theme-name=\"%s\"\n", gtk_theme_name );
118 fprintf( f, "gtk-icon-theme-name=\"%s\"\n", icon_theme_name );
119 fprintf( f, "gtk-font-name=\"%s\"\n", font_name );
a7477b61 120 fprintf( f, "gtk-toolbar-style=%d\n", tb_style );
129d4788 121#if CURSOR_THEME
966feb70 122 fprintf( f, "gtk-cursor-theme-name=\"%s\"\n", cursor_theme_name );
360b2260 123 fprintf( f, "gtk-cursor-theme-size=%d\n", cursor_theme_size );
129d4788 124#endif
a7477b61 125
a0647843
HJYP
126 fprintf( f, "include \"%s/.gtkrc-2.0.mine\"\n", g_get_home_dir() );
127
128 fclose( f );
10002786 129 }
10002786
HJYP
130}
131
bf6886d5 132static void create_lxsession_config_dir()
6b1f0c67 133{
bf6886d5 134 char* dir = g_build_filename( g_get_user_config_dir(), "lxsession", session_name, NULL );
6b1f0c67 135 g_mkdir_with_parents( dir, 0755 );
6b1f0c67
HJYP
136 g_free( dir );
137}
138
bf6886d5 139static void write_lxsession_config()
6b1f0c67
HJYP
140{
141 FILE* f;
966feb70
MTW
142 char* file, *data;
143 gsize len;
144 GKeyFile* kf = g_key_file_new();
145 gboolean ret;
bf6886d5
HJYP
146
147 file = g_build_filename( g_get_user_config_dir(), "lxsession", session_name, "desktop.conf", NULL );
966feb70 148 ret = g_key_file_load_from_file( kf, file, G_KEY_FILE_KEEP_COMMENTS, NULL );
bf6886d5 149
966feb70
MTW
150 if( ! ret )
151 {
152 const gchar* const * dir;
bf6886d5
HJYP
153 const gchar* const * dirs = g_get_system_config_dirs();
154 create_lxsession_config_dir();
155
966feb70
MTW
156 /* load system-wide config file */
157 for( dir = dirs; *dir; ++dir )
158 {
bf6886d5
HJYP
159 char* path = g_build_filename( *dir, "lxsession", session_name, "desktop.conf", NULL );
160 ret = g_key_file_load_from_file( kf, path, G_KEY_FILE_KEEP_COMMENTS, NULL );
966feb70
MTW
161 g_free( path );
162 if( ret )
163 break;
164 }
165 }
bf6886d5 166
966feb70
MTW
167 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", gtk_theme_name );
168 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", icon_theme_name );
169 g_key_file_set_string( kf, "GTK", "sGtk/FontName", font_name );
170 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", tb_style );
129d4788 171#if CURSOR_THEME
966feb70
MTW
172 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", cursor_theme_name );
173 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", cursor_theme_size );
129d4788 174#endif
bf6886d5 175
966feb70
MTW
176 data = g_key_file_to_data( kf, &len, NULL );
177 g_key_file_free( kf );
bf6886d5 178
6b1f0c67
HJYP
179 if( f = fopen( file, "w" ) )
180 {
181 fwrite( data, sizeof(char), len, f );
182 fclose( f );
183 }
184 g_free( data );
185}
186
10002786
HJYP
187static void reload_theme()
188{
189
190 gtk_rc_reparse_all();
191}
192
193static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
c0c2320a
HJYP
194{
195 GtkTreeIter it;
196 GtkTreeModel* model;
197 if( gtk_tree_selection_get_selected( sel, &model, &it ) )
198 {
199 char* name;
3e21f6eb 200 gtk_tree_model_get( model, &it, COL_NAME, &name, -1 );
c0c2320a 201
14a9f0b4 202 if( model == GTK_TREE_MODEL (gtk_theme_list) ) /* gtk+ theme */
a0647843
HJYP
203 {
204 if( name && gtk_theme_name && 0 == strcmp( name, gtk_theme_name ) )
205 goto out;
206 g_free( gtk_theme_name );
207 gtk_theme_name = name;
6b1f0c67 208
bf6886d5 209 if( under_lxsession )
6b1f0c67 210 g_object_set( gtk_settings_get_default(), "gtk-theme-name", name, NULL );
a0647843 211 }
14a9f0b4 212 else if( model == GTK_TREE_MODEL (icon_theme_list) ) /* icon theme */
a0647843
HJYP
213 {
214 if( name && icon_theme_name && 0 == strcmp( name, icon_theme_name ) )
215 goto out;
216 g_free( icon_theme_name );
217 icon_theme_name = name;
6b1f0c67 218
bf6886d5 219 if( under_lxsession )
6b1f0c67 220 g_object_set( gtk_settings_get_default(), "gtk-icon-theme-name", name, NULL );
c0c2320a 221 }
938e9a87 222#if CURSOR_THEME
966feb70
MTW
223 else if( model == GTK_TREE_MODEL (cursor_theme_list) ) /* cursor theme */
224 {
225 if( name && cursor_theme_name && 0 == strcmp( name, cursor_theme_name ) )
226 goto out;
227 g_free( cursor_theme_name );
228 cursor_theme_name = g_strdup(name);
229 //gdk_x11_display_set_cursor_theme ( gdk_display_get_default (), name, cursor_theme_size );
230
bf6886d5 231 if( under_lxsession )
966feb70
MTW
232 g_object_set( gtk_settings_get_default(), "gtk-cursor-theme-name", name, NULL );
233 }
938e9a87 234#endif
6b1f0c67 235
bf6886d5 236 if( under_lxsession )
14a9f0b4
MTW
237 {
238 enable_apply();
239 }
240 else
241 {
242 write_rc_file( tmp_rc_file );
243 //gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
244 reload_demo_process();
245 }
a0647843
HJYP
246 return;
247 out:
c0c2320a
HJYP
248 g_free( name );
249 }
250}
251
e4ff4ccd
HJYP
252static gint sort_func( GtkTreeModel* model, GtkTreeIter* it1, GtkTreeIter* it2, gpointer data )
253{
254 char* str1, *str2;
255 int ret;
3e21f6eb
HJYP
256 gtk_tree_model_get( model, it1, COL_DISP_NAME, &str1, -1 );
257 gtk_tree_model_get( model, it2, COL_DISP_NAME, &str2, -1 );
258 ret = g_utf8_collate( str1, str2 );
e4ff4ccd
HJYP
259 g_free( str1 );
260 g_free( str2 );
261 return ret;
262}
263
c0c2320a
HJYP
264static GtkListStore* init_tree_view( GtkTreeView* view, GCallback on_sel_changed, const char* prop )
265{
266 GtkTreeViewColumn* col;
267 GtkListStore* list;
a0647843 268 GtkTreeSelection* sel;
966feb70 269 int text_col = strcmp(prop, "gtk-theme-name") && strcmp(prop, "gtk-cursor-theme-name") ? COL_DISP_NAME : COL_NAME;
3e21f6eb
HJYP
270
271 col = gtk_tree_view_column_new_with_attributes( NULL, gtk_cell_renderer_text_new(),
272 "text", text_col, NULL );
c0c2320a 273 gtk_tree_view_append_column( view, col );
3e21f6eb 274
a0647843 275 sel = gtk_tree_view_get_selection(view);
14a9f0b4 276 g_signal_connect( sel, "changed", on_sel_changed, GINT_TO_POINTER(prop) );
c0c2320a 277
3e21f6eb
HJYP
278 list = gtk_list_store_new( N_COLS, G_TYPE_STRING, G_TYPE_STRING );
279 gtk_tree_sortable_set_sort_func( (GtkTreeSortable*)list, text_col, sort_func, NULL, NULL );
c0c2320a
HJYP
280 gtk_tree_view_set_model( view, (GtkTreeModel*)list );
281 g_object_unref( list );
282 return list;
283}
284
3e21f6eb
HJYP
285typedef gboolean (*ThemeFunc)(const char* file, const char* dir, const char* name, GtkListStore* list, GtkTreeIter* it);
286
c0c2320a 287static void load_themes_from_dir( GtkListStore* list,
3e21f6eb
HJYP
288 const char* dir_path,
289 const char* lookup,
290 GtkTreeSelection* sel,
291 const char* init_sel,
292 ThemeFunc theme_func )
c0c2320a
HJYP
293{
294 GDir* dir;
295 if( dir = g_dir_open( dir_path, 0, NULL ) )
296 {
14a9f0b4 297 const char* name;
c0c2320a
HJYP
298 while( name = g_dir_read_name( dir ) )
299 {
300 char* file = g_build_filename( dir_path, name, lookup, NULL );
301 if( g_file_test( file, G_FILE_TEST_EXISTS ) )
302 {
3e21f6eb 303 gboolean add = TRUE;
c0c2320a 304 GtkTreeIter it;
3e21f6eb
HJYP
305
306 /* prevent duplication */
14a9f0b4 307 if( gtk_tree_model_get_iter_first(GTK_TREE_MODEL (list), &it ) )
a0647843 308 {
3e21f6eb
HJYP
309 char* _name;
310 do {
311 _name = NULL;
14a9f0b4 312 gtk_tree_model_get(GTK_TREE_MODEL (list), &it, COL_NAME, &_name, -1);
3e21f6eb
HJYP
313 if( _name && strcmp(_name, name) == 0 )
314 {
315 add = FALSE;
316 g_free(_name);
317 break;
318 }
319 g_free(_name);
320 }
14a9f0b4 321 while( gtk_tree_model_iter_next(GTK_TREE_MODEL (list), &it ) );
3e21f6eb
HJYP
322 }
323
324 if( add )
325 {
326 gtk_list_store_append( list, &it );
327 gtk_list_store_set( list, &it, COL_NAME, name, -1 );
328
329 if( theme_func )
330 {
14a9f0b4 331 if( ! theme_func(file, (char *) dir, name, list, &it) )
3e21f6eb
HJYP
332 add = FALSE;
333 }
334
335 if( add )
336 {
337 if( 0 == strcmp( name, init_sel ) )
338 {
339 GtkTreeView* view;
340 GtkTreePath* tp;
341 gtk_tree_selection_select_iter( sel, &it );
342 view = gtk_tree_selection_get_tree_view( sel );
343 tp = gtk_tree_model_get_path( (GtkTreeModel*)list, &it );
344 gtk_tree_view_scroll_to_cell( view, tp, NULL, FALSE, 0, 0 );
345 gtk_tree_path_free( tp );
346 }
347 }
348 else
349 gtk_list_store_remove( list, &it );
a0647843 350 }
c0c2320a
HJYP
351 }
352 g_free( file );
353 }
354 g_dir_close( dir );
355 }
356}
357
358static void load_from_data_dirs( GtkListStore* list,
3e21f6eb
HJYP
359 const char* relative_path,
360 const char* lookup,
361 GtkTreeSelection* sel,
362 const char* init_sel,
363 ThemeFunc theme_func )
c0c2320a
HJYP
364{
365 const char* const *dirs = g_get_system_data_dirs();
366 const char* const *dir;
367 char* dir_path;
368 for( dir = dirs; *dir; ++dir )
369 {
370 dir_path = g_build_filename( *dir, relative_path, NULL );
3e21f6eb 371 load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
c0c2320a
HJYP
372 g_free( dir_path );
373 }
374 dir_path = g_build_filename( g_get_user_data_dir(), relative_path, NULL );
3e21f6eb 375 load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
c0c2320a
HJYP
376 g_free( dir_path );
377}
378
3e21f6eb
HJYP
379static gboolean icon_theme_func(const char* file, const char* dir, const char* name, GtkListStore* list, GtkTreeIter* it)
380{
381 GKeyFile* kf;
382 char* disp_name = NULL;
383 if( g_str_has_prefix( name, "default." ) )
384 return FALSE;
385
386 kf = g_key_file_new();
387 if( g_key_file_load_from_file(kf, file, 0, NULL) )
388 {
389 if( g_key_file_has_key(kf, "Icon Theme", "Directories", NULL)
390 && ! g_key_file_get_boolean(kf, "Icon Theme", "Hidden", NULL) )
391 {
392 disp_name = g_key_file_get_locale_string(kf, "Icon Theme", "Name", NULL, NULL);
393 gtk_list_store_set( list, it, COL_DISP_NAME, disp_name ? disp_name : name, -1 );
394 }
395 }
396 g_key_file_free(kf);
397 return disp_name != NULL;
398}
399
938e9a87 400#if CURSOR_THEME
3e21f6eb
HJYP
401static gboolean cursor_theme_func(const char* dir, const char* name, const char* lookup)
402{
403 char* ret = NULL;
404/*
405 GKeyFile* kf = g_key_file_new();
406 if( g_key_file_load_from_file(kf, lookup, 0, NULL) )
407 {
408 if( g_key_file_has_key(kf, "Icon Theme", "Directories", NULL) )
409 {
410 ret = g_key_file_get_locale_string(kf, "Icon Theme", "Name", NULL, NULL);
411 }
412 }
413 g_key_file_free(kf);
414*/
415 return ret != NULL;
416}
938e9a87 417#endif
3e21f6eb 418
da434a12 419static void load_gtk_themes( GtkListStore* list, const char* cur_sel )
c0c2320a 420{
a7477b61 421 char* path;
a0647843 422 GtkTreeSelection* sel = gtk_tree_view_get_selection( gtk_theme_view );
3e21f6eb 423 load_from_data_dirs( list, "themes", "gtk-2.0", sel, cur_sel, NULL );
a7477b61 424 path = g_build_filename( g_get_home_dir(), ".themes", NULL );
3e21f6eb 425 load_themes_from_dir( list, path, "gtk-2.0", sel, cur_sel, NULL );
a7477b61 426 g_free( path );
e4ff4ccd 427 gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
c0c2320a
HJYP
428}
429
da434a12 430static void load_icon_themes( GtkListStore* list, const char* cur_sel )
c0c2320a
HJYP
431{
432 char* path;
a0647843 433 GtkTreeSelection* sel = gtk_tree_view_get_selection( icon_theme_view );
3e21f6eb 434 load_from_data_dirs( list, "icons", "index.theme", sel, cur_sel, icon_theme_func );
c0c2320a 435 path = g_build_filename( g_get_home_dir(), ".icons", NULL );
3e21f6eb 436 load_themes_from_dir( list, path, "index.theme", sel, cur_sel, icon_theme_func );
c0c2320a 437 g_free( path );
e4ff4ccd 438 gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
c0c2320a
HJYP
439}
440
938e9a87 441#if CURSOR_THEME
966feb70
MTW
442static void load_cursor_themes( GtkListStore* list, const char* cur_sel )
443{
444 char* path;
445 GtkTreeSelection* sel = gtk_tree_view_get_selection( cursor_theme_view );
446 load_from_data_dirs( list, "icons", "cursors", sel, cur_sel, NULL );
447 path = g_build_filename( g_get_home_dir(), ".icons", NULL );
448 load_themes_from_dir( list, path, "cursors", sel, cur_sel, NULL );
449 g_free( path );
450 gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
451}
938e9a87
MTW
452#endif
453
a0647843 454/*
c0c2320a
HJYP
455static void load_fonts( GtkListStore* list )
456{
457
458}
a0647843
HJYP
459*/
460
14a9f0b4 461gboolean center_win( GtkWidget* dlg )
ac36e6fa 462{
ac36e6fa 463 gtk_widget_show( dlg );
da434a12
HJYP
464 return FALSE;
465}
466
467static void on_demo_loaded( GtkSocket* socket, GtkWidget* dlg )
468{
469 /* sleep for 0.8 sec for loading of the demo window */
470 /* FIXME: we need a better way to do this, such as IPC */
471 g_timeout_add_full( G_PRIORITY_LOW, 800, (GSourceFunc)center_win, dlg, NULL );
ac36e6fa
HJYP
472 g_signal_handlers_disconnect_by_func( socket, on_demo_loaded, dlg );
473}
474
5ef7a66e 475GtkWidget* main_dlg_new()
c0c2320a 476{
5ef7a66e
HJYP
477 GtkBuilder* builder = gtk_builder_new();
478 GtkWidget* dlg;
ddac316c
HJYP
479 char* files[] = { tmp_rc_file, NULL };
480 char** def_files = gtk_rc_get_default_files();
481 char** file;
482
bf6886d5 483 if( under_lxsession )
6b1f0c67 484 {
bf6886d5
HJYP
485 session_name = g_getenv("DESKTOP_SESSION");
486 }
487 else
488 {
489 /* no lxsession-settings daemon, use gtkrc-2.0 */
6b1f0c67
HJYP
490 for( file = def_files; *file; ++file )
491 {
492 if( 0 == access( *file, W_OK ) )
493 rc_file = *file;
494 }
495 if( rc_file )
496 rc_file = g_strdup( rc_file );
497 else
498 rc_file = g_build_filename( g_get_home_dir(), ".gtkrc-2.0", NULL );
499 }
a7477b61
HJYP
500 g_object_get( gtk_settings_get_default(),
501 "gtk-theme-name", &gtk_theme_name,
502 "gtk-icon-theme-name", &icon_theme_name,
503 "gtk-font-name", &font_name,
504 "gtk-toolbar-style", &tb_style,
938e9a87 505#if CURSOR_THEME
966feb70
MTW
506 "gtk-cursor-theme-name", &cursor_theme_name,
507 "gtk-cursor-theme-size", &cursor_theme_size,
938e9a87 508#endif
a7477b61
HJYP
509 NULL );
510
42bce29e
HJYP
511 if( ! gtk_theme_name )
512 gtk_theme_name = g_strdup( "Raleigh" );
42bce29e 513 if( ! icon_theme_name )
966feb70 514 icon_theme_name = g_strdup( "hicolor" );
938e9a87 515#if CURSOR_THEME
966feb70
MTW
516 if( ! cursor_theme_name )
517 cursor_theme_name = g_strdup( "default" );
938e9a87 518#endif
42bce29e
HJYP
519 if( ! font_name )
520 font_name = g_strdup( "Sans 10" );
966feb70
MTW
521 if( ! cursor_theme_size )
522 cursor_theme_size = 16;
a0647843 523
bf6886d5
HJYP
524 /* no lxsession-settings daemon, use gtkrc-2.0 */
525 if( ! under_lxsession )
6b1f0c67 526 write_rc_file( tmp_rc_file );
a0647843 527
5ef7a66e
HJYP
528 gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/lxappearance/lxappearance.ui", NULL);
529 gtk_builder_connect_signals(builder, NULL);
530
531 GET_WIDGET_WITH_TYPE(dlg, GTK_WIDGET);
532 main_dlg = dlg;
533
c0c2320a
HJYP
534 INIT_LIST( gtk_theme, "gtk-theme-name" )
535 INIT_LIST( icon_theme, "gtk-icon-theme-name" )
938e9a87
MTW
536
537#if CURSOR_THEME
966feb70 538 INIT_LIST( cursor_theme, "gtk-cursor-theme-name" )
938e9a87
MTW
539#endif
540
5ef7a66e 541 gtk_font_button_set_font_name( (GtkFontButton*)LOOKUP(font), font_name );
938e9a87
MTW
542
543#if CURSOR_THEME
5ef7a66e 544 gtk_range_set_value( GTK_RANGE(LOOKUP(cursor_theme_size)), cursor_theme_size );
938e9a87 545#endif
42bce29e 546
5ef7a66e 547 gtk_combo_box_set_active( GTK_COMBO_BOX(LOOKUP(tb_style)), tb_style < 4 ? tb_style : 3 );
8c98cd54 548
14a9f0b4 549 GET_WIDGET_WITH_TYPE( demo_box, GTK_WIDGET );
a0647843 550 gtk_widget_show( demo_box );
ac36e6fa 551
bf6886d5 552 if( under_lxsession )
6b1f0c67 553 {
bf6886d5 554 /* XSettings daemon of lxsession is running, gtkrc is useless.
6b1f0c67
HJYP
555 * We should set properties of GtkSettings object on the fly.
556 * This will cause problems with some themes, but we have no choice.
557 */
558 show_demo( (GdkNativeWindow)demo_box );
26bb4e49 559 gtk_widget_show( dlg );
6b1f0c67
HJYP
560 }
561 else
562 {
bf6886d5 563 /* no lxsession-settings daemon, use gtkrc-2.0 and load preview in another process */
6b1f0c67
HJYP
564 demo_socket = gtk_socket_new();
565 g_signal_connect( demo_socket, "plug-added", G_CALLBACK(on_demo_loaded), dlg );
566 g_signal_connect( demo_socket, "plug-removed", G_CALLBACK(gtk_true), NULL );
567 gtk_widget_show( demo_socket );
568 gtk_container_add( (GtkContainer*)demo_box, demo_socket );
569
570 gtk_widget_realize( dlg );
571 reload_demo_process();
572 }
ac36e6fa 573
684d617e 574 disable_apply();
850bd787
HJYP
575
576 g_object_unref(builder);
5ef7a66e 577 return dlg;
c0c2320a 578}
f57233d5 579
ddac316c
HJYP
580static void reload_all_programs( gboolean icon_only )
581{
582 GdkEventClient event;
583 event.type = GDK_CLIENT_EVENT;
584 event.send_event = TRUE;
585 event.window = NULL;
586
bf6886d5 587 if( under_lxsession )
6b1f0c67 588 {
bf6886d5
HJYP
589/*
590 char* argv[]={"lxsession", "-r", NULL};
591 g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, NULL, NULL);
592 return;
593*/
594 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
6b1f0c67
HJYP
595 event.data.b[0] = 0; /* LXS_RELOAD */
596 }
597 else
598 {
599 if( icon_only )
600 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
601 else
602 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
603 }
604 event.data_format = 8;
605 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
ddac316c
HJYP
606}
607
f57233d5
HJYP
608void
609on_apply_clicked (GtkButton *button,
610 gpointer user_data)
611{
bf6886d5
HJYP
612 if( under_lxsession )
613 write_lxsession_config();
6b1f0c67
HJYP
614 else
615 write_rc_file( rc_file );
616
617 reload_all_programs( FALSE );
684d617e 618 disable_apply();
f57233d5
HJYP
619}
620
a0647843
HJYP
621
622void
623on_font_changed (GtkFontButton *fontbutton,
624 gpointer user_data)
625{
626 const char* name = gtk_font_button_get_font_name(fontbutton);
684d617e 627 if( name && font_name && 0 == strcmp( name, font_name ) )
a0647843
HJYP
628 return;
629 g_free( font_name );
630 font_name = g_strdup( name );
6b1f0c67 631
bf6886d5 632 if( under_lxsession )
6b1f0c67
HJYP
633 {
634 g_object_set( gtk_settings_get_default(), "gtk-font-name", font_name, NULL );
635
636 enable_apply();
637 }
638 else
639 {
640 write_rc_file( tmp_rc_file );
641 reload_demo_process();
642 }
a0647843
HJYP
643}
644
a6ea4c44
HJYP
645void
646on_install_theme_clicked (GtkButton *button,
647 gpointer user_data)
648{
a7477b61
HJYP
649 GtkFileFilter* filter = gtk_file_filter_new();
650 GtkWidget* fc = gtk_file_chooser_dialog_new( _("Select an icon theme"), NULL,
651 GTK_FILE_CHOOSER_ACTION_OPEN,
652 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
653 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
a6ea4c44 654
a7477b61
HJYP
655 gtk_file_filter_add_pattern( filter, "*.tar.gz" );
656 gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
657 gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
658
14a9f0b4
MTW
659 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
660 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
a7477b61
HJYP
661
662 if( gtk_dialog_run( (GtkDialog*)fc ) == GTK_RESPONSE_OK )
663 {
14a9f0b4 664 char* file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
da434a12
HJYP
665 char* argv[]={
666 PACKAGE_DATA_DIR"/lxappearance/install-icon-theme.sh",
667 file, NULL };
668 int status = 0;
684d617e
HJYP
669 char* stdo = NULL;
670 if( g_spawn_sync( NULL, argv, NULL, 0, NULL, NULL, &stdo, NULL, &status, NULL ) && 0 == status )
da434a12 671 {
684d617e
HJYP
672 char* sep = stdo ? strchr( stdo, '\n' ) : NULL;
673 if( sep )
674 *sep = '\0';
675
da434a12
HJYP
676 /* reload all icon themes */
677 gtk_list_store_clear( icon_theme_list );
684d617e 678 load_icon_themes( icon_theme_list, stdo ? stdo : "" );
966feb70 679
938e9a87 680#if CURSOR_THEME
966feb70
MTW
681 /* reload all cursor themes */
682 gtk_list_store_clear( cursor_theme_list );
683 load_cursor_themes( cursor_theme_list, stdo ? stdo : "" );
938e9a87 684#endif
da434a12
HJYP
685 }
686 g_free( file );
a7477b61
HJYP
687 }
688
689 gtk_widget_destroy( fc );
a6ea4c44
HJYP
690}
691
692
693void
694on_remove_theme_clicked (GtkButton *button,
695 gpointer user_data)
696{
697
698}
699
938e9a87 700#if CURSOR_THEME
966feb70
MTW
701void
702on_cursor_size_changed (GtkHScale *cursorsizescale,
703 gpointer user_data)
704{
705 cursor_theme_size = gtk_range_get_value( GTK_RANGE(cursorsizescale) );
706
bf6886d5 707 if( under_lxsession )
966feb70
MTW
708 {
709 g_object_set( gtk_settings_get_default(), "gtk-cursor-theme-size", cursor_theme_size, NULL );
710 enable_apply();
711 }
712 else
713 {
714 write_rc_file( tmp_rc_file );
715 reload_demo_process();
716 }
717}
938e9a87 718#endif
a7477b61
HJYP
719
720void
721on_tb_style_changed (GtkComboBox *combobox,
722 gpointer user_data)
723{
724 int sel = gtk_combo_box_get_active( combobox );
725 if( sel == tb_style || sel < 0 )
726 return;
727 tb_style = sel;
6b1f0c67 728
bf6886d5 729 if( under_lxsession )
6b1f0c67
HJYP
730 {
731 g_object_set( gtk_settings_get_default(), "gtk-toolbar-style", tb_style, NULL );
732 enable_apply();
733 }
734 else
735 {
736 write_rc_file( tmp_rc_file );
737 reload_demo_process();
738 }
a7477b61
HJYP
739}
740