Adding upstream version 0.4.0.
[debian/lxappearance.git] / src / main-dlg.c
CommitLineData
08024023
DB
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <gtk/gtk.h>
6#include <gdk/gdkx.h>
a78e4e46 7#include <glib/gi18n.h>
08024023
DB
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <string.h>
13
14/* for kill & waitpid */
15#include <sys/types.h>
16#include <signal.h>
17#include <sys/wait.h>
18
19#include "main-dlg.h"
a78e4e46 20#include "demo.h"
08024023 21
bd2899cf
DB
22enum {
23 COL_DISP_NAME,
24 COL_NAME,
25 N_COLS
26};
27
a78e4e46
DB
28#define LOOKUP(name) gtk_builder_get_object( builder, #name )
29#define GET_WIDGET_WITH_TYPE(name, type) name = type(LOOKUP(name))
08024023
DB
30
31#define INIT_LIST(name, prop) \
bd2899cf
DB
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); \
08024023
DB
34 load_##name##s( name##_list, name##_name );
35
bd2899cf
DB
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 )
08024023 38
13f1ec51 39extern gboolean under_lxsession; /* wether we are under lxsession */
08024023
DB
40
41extern GtkWidget* main_dlg; /* defined in main.c */
42
43static GtkTreeView* gtk_theme_view = NULL;
44static GtkListStore* gtk_theme_list = NULL;
45
46static GtkTreeView* icon_theme_view = NULL;
47static GtkListStore* icon_theme_list = NULL;
48
7fbfd5a0 49#if CURSOR_THEME
13f1ec51
DB
50static GtkTreeView* cursor_theme_view = NULL;
51static GtkListStore* cursor_theme_list = NULL;
7fbfd5a0 52#endif
13f1ec51 53
08024023
DB
54static char* gtk_theme_name = NULL;
55static char* icon_theme_name = NULL;
56static char* font_name = NULL;
13f1ec51
DB
57static char* cursor_theme_name = NULL;
58static gint cursor_theme_size = 0;
08024023
DB
59static GtkToolbarStyle tb_style = GTK_TOOLBAR_BOTH;
60
61extern char tmp_rc_file[];
62static char* rc_file = NULL;
63
64/*
65static GtkTreeView* font_view = NULL;
66static GtkListStore* font_list = NULL;
67*/
68static GtkWidget* demo_box = NULL;
69static GtkWidget* demo_socket = NULL;
70static GPid demo_pid = 0;
71
13f1ec51 72static const char* session_name = NULL;
08024023 73
13f1ec51 74extern Atom lxsession_atom; /* defined in main.c */
08024023
DB
75
76static void reload_demo_process()
77{
78 char* argv[5];
79 char wid[16];
80
81 if( demo_pid > 0 ) /* kill old demo */
82 {
83 int stat;
84 kill( demo_pid, SIGTERM );
85 waitpid( demo_pid, &stat, 0 );
86 demo_pid = 0;
87 }
88
bd2899cf 89 g_snprintf( wid, 16, "%ld", gtk_socket_get_id(GTK_SOCKET (demo_socket)) );
08024023
DB
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 );
97
98 /* reloading demo means the current selected theme is changed */
99 enable_apply();
100}
101
102static void write_rc_file( const char* path )
103{
104 FILE* f;
105 static char* tb_styles[] = {
106 "GTK_TOOLBAR_ICONS",
107 "GTK_TOOLBAR_TEXT",
108 "GTK_TOOLBAR_BOTH",
109 "GTK_TOOLBAR_BOTH_HORIZ"
110 };
111
112 if( f = fopen( path, "w" ) )
113 {
114 fputs( "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
115 "# Any customization should be done in ~/.gtkrc-2.0.mine\n\n", f );
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 );
120 fprintf( f, "gtk-toolbar-style=%d\n", tb_style );
a78e4e46 121#if CURSOR_THEME
13f1ec51
DB
122 fprintf( f, "gtk-cursor-theme-name=\"%s\"\n", cursor_theme_name );
123 fprintf( f, "gtk-cursor-theme-size=%d\n", cursor_theme_size );
a78e4e46 124#endif
08024023
DB
125
126 fprintf( f, "include \"%s/.gtkrc-2.0.mine\"\n", g_get_home_dir() );
127
128 fclose( f );
129 }
130}
131
13f1ec51 132static void create_lxsession_config_dir()
08024023 133{
13f1ec51 134 char* dir = g_build_filename( g_get_user_config_dir(), "lxsession", session_name, NULL );
08024023 135 g_mkdir_with_parents( dir, 0755 );
08024023
DB
136 g_free( dir );
137}
138
13f1ec51 139static void write_lxsession_config()
08024023
DB
140{
141 FILE* f;
13f1ec51
DB
142 char* file, *data;
143 gsize len;
144 GKeyFile* kf = g_key_file_new();
145 gboolean ret;
08024023 146
13f1ec51
DB
147 file = g_build_filename( g_get_user_config_dir(), "lxsession", session_name, "desktop.conf", NULL );
148 ret = g_key_file_load_from_file( kf, file, G_KEY_FILE_KEEP_COMMENTS, NULL );
08024023 149
13f1ec51
DB
150 if( ! ret )
151 {
152 const gchar* const * dir;
153 const gchar* const * dirs = g_get_system_config_dirs();
154 create_lxsession_config_dir();
155
156 /* load system-wide config file */
157 for( dir = dirs; *dir; ++dir )
158 {
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 );
161 g_free( path );
162 if( ret )
163 break;
164 }
165 }
08024023 166
13f1ec51
DB
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 );
a78e4e46 171#if CURSOR_THEME
13f1ec51
DB
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 );
a78e4e46 174#endif
08024023 175
13f1ec51
DB
176 data = g_key_file_to_data( kf, &len, NULL );
177 g_key_file_free( kf );
08024023
DB
178
179 if( f = fopen( file, "w" ) )
180 {
181 fwrite( data, sizeof(char), len, f );
182 fclose( f );
183 }
184 g_free( data );
185}
186
187static void reload_theme()
188{
189
190 gtk_rc_reparse_all();
191}
192
193static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
194{
195 GtkTreeIter it;
196 GtkTreeModel* model;
197 if( gtk_tree_selection_get_selected( sel, &model, &it ) )
198 {
199 char* name;
bd2899cf 200 gtk_tree_model_get( model, &it, COL_NAME, &name, -1 );
08024023 201
bd2899cf 202 if( model == GTK_TREE_MODEL (gtk_theme_list) ) /* gtk+ theme */
08024023
DB
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;
208
13f1ec51 209 if( under_lxsession )
08024023
DB
210 g_object_set( gtk_settings_get_default(), "gtk-theme-name", name, NULL );
211 }
bd2899cf 212 else if( model == GTK_TREE_MODEL (icon_theme_list) ) /* icon theme */
08024023
DB
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;
218
13f1ec51 219 if( under_lxsession )
08024023
DB
220 g_object_set( gtk_settings_get_default(), "gtk-icon-theme-name", name, NULL );
221 }
7fbfd5a0 222#if CURSOR_THEME
13f1ec51
DB
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 );
08024023 230
13f1ec51
DB
231 if( under_lxsession )
232 g_object_set( gtk_settings_get_default(), "gtk-cursor-theme-name", name, NULL );
233 }
7fbfd5a0 234#endif
13f1ec51
DB
235
236 if( under_lxsession )
bd2899cf
DB
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 }
08024023
DB
246 return;
247 out:
248 g_free( name );
249 }
250}
251
252static gint sort_func( GtkTreeModel* model, GtkTreeIter* it1, GtkTreeIter* it2, gpointer data )
253{
254 char* str1, *str2;
255 int ret;
bd2899cf
DB
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 );
08024023
DB
259 g_free( str1 );
260 g_free( str2 );
261 return ret;
262}
263
264static GtkListStore* init_tree_view( GtkTreeView* view, GCallback on_sel_changed, const char* prop )
265{
266 GtkTreeViewColumn* col;
267 GtkListStore* list;
268 GtkTreeSelection* sel;
13f1ec51 269 int text_col = strcmp(prop, "gtk-theme-name") && strcmp(prop, "gtk-cursor-theme-name") ? COL_DISP_NAME : COL_NAME;
bd2899cf
DB
270
271 col = gtk_tree_view_column_new_with_attributes( NULL, gtk_cell_renderer_text_new(),
272 "text", text_col, NULL );
08024023 273 gtk_tree_view_append_column( view, col );
bd2899cf 274
08024023 275 sel = gtk_tree_view_get_selection(view);
bd2899cf 276 g_signal_connect( sel, "changed", on_sel_changed, GINT_TO_POINTER(prop) );
08024023 277
bd2899cf
DB
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 );
08024023
DB
280 gtk_tree_view_set_model( view, (GtkTreeModel*)list );
281 g_object_unref( list );
282 return list;
283}
284
bd2899cf
DB
285typedef gboolean (*ThemeFunc)(const char* file, const char* dir, const char* name, GtkListStore* list, GtkTreeIter* it);
286
08024023 287static void load_themes_from_dir( GtkListStore* list,
bd2899cf
DB
288 const char* dir_path,
289 const char* lookup,
290 GtkTreeSelection* sel,
291 const char* init_sel,
292 ThemeFunc theme_func )
08024023
DB
293{
294 GDir* dir;
295 if( dir = g_dir_open( dir_path, 0, NULL ) )
296 {
bd2899cf 297 const char* name;
08024023
DB
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 {
bd2899cf 303 gboolean add = TRUE;
08024023 304 GtkTreeIter it;
bd2899cf
DB
305
306 /* prevent duplication */
307 if( gtk_tree_model_get_iter_first(GTK_TREE_MODEL (list), &it ) )
08024023 308 {
bd2899cf
DB
309 char* _name;
310 do {
311 _name = NULL;
312 gtk_tree_model_get(GTK_TREE_MODEL (list), &it, COL_NAME, &_name, -1);
313 if( _name && strcmp(_name, name) == 0 )
314 {
315 add = FALSE;
316 g_free(_name);
317 break;
318 }
319 g_free(_name);
320 }
321 while( gtk_tree_model_iter_next(GTK_TREE_MODEL (list), &it ) );
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 {
331 if( ! theme_func(file, (char *) dir, name, list, &it) )
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 );
08024023
DB
350 }
351 }
352 g_free( file );
353 }
354 g_dir_close( dir );
355 }
356}
357
358static void load_from_data_dirs( GtkListStore* list,
bd2899cf
DB
359 const char* relative_path,
360 const char* lookup,
361 GtkTreeSelection* sel,
362 const char* init_sel,
363 ThemeFunc theme_func )
08024023
DB
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 );
bd2899cf 371 load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
08024023
DB
372 g_free( dir_path );
373 }
374 dir_path = g_build_filename( g_get_user_data_dir(), relative_path, NULL );
bd2899cf 375 load_themes_from_dir( list, dir_path, lookup, sel, init_sel, theme_func );
08024023
DB
376 g_free( dir_path );
377}
378
bd2899cf
DB
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
7fbfd5a0 400#if CURSOR_THEME
bd2899cf
DB
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}
7fbfd5a0 417#endif
bd2899cf 418
08024023
DB
419static void load_gtk_themes( GtkListStore* list, const char* cur_sel )
420{
421 char* path;
422 GtkTreeSelection* sel = gtk_tree_view_get_selection( gtk_theme_view );
bd2899cf 423 load_from_data_dirs( list, "themes", "gtk-2.0", sel, cur_sel, NULL );
08024023 424 path = g_build_filename( g_get_home_dir(), ".themes", NULL );
bd2899cf 425 load_themes_from_dir( list, path, "gtk-2.0", sel, cur_sel, NULL );
08024023
DB
426 g_free( path );
427 gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
428}
429
430static void load_icon_themes( GtkListStore* list, const char* cur_sel )
431{
432 char* path;
433 GtkTreeSelection* sel = gtk_tree_view_get_selection( icon_theme_view );
bd2899cf 434 load_from_data_dirs( list, "icons", "index.theme", sel, cur_sel, icon_theme_func );
08024023 435 path = g_build_filename( g_get_home_dir(), ".icons", NULL );
bd2899cf 436 load_themes_from_dir( list, path, "index.theme", sel, cur_sel, icon_theme_func );
08024023
DB
437 g_free( path );
438 gtk_tree_sortable_set_sort_column_id( (GtkTreeSortable*)list, 0, GTK_SORT_ASCENDING );
439}
440
7fbfd5a0 441#if CURSOR_THEME
13f1ec51
DB
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}
7fbfd5a0
DB
452#endif
453
08024023
DB
454/*
455static void load_fonts( GtkListStore* list )
456{
457
458}
459*/
460
bd2899cf 461gboolean center_win( GtkWidget* dlg )
08024023
DB
462{
463 gtk_widget_show( dlg );
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 );
472 g_signal_handlers_disconnect_by_func( socket, on_demo_loaded, dlg );
473}
474
a78e4e46 475GtkWidget* main_dlg_new()
08024023 476{
a78e4e46
DB
477 GtkBuilder* builder = gtk_builder_new();
478 GtkWidget* dlg;
08024023
DB
479 char* files[] = { tmp_rc_file, NULL };
480 char** def_files = gtk_rc_get_default_files();
481 char** file;
482
13f1ec51
DB
483 if( under_lxsession )
484 {
485 session_name = g_getenv("DESKTOP_SESSION");
486 }
487 else
08024023 488 {
13f1ec51 489 /* no lxsession-settings daemon, use gtkrc-2.0 */
08024023
DB
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 }
08024023
DB
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,
7fbfd5a0 505#if CURSOR_THEME
13f1ec51
DB
506 "gtk-cursor-theme-name", &cursor_theme_name,
507 "gtk-cursor-theme-size", &cursor_theme_size,
7fbfd5a0 508#endif
08024023
DB
509 NULL );
510
511 if( ! gtk_theme_name )
512 gtk_theme_name = g_strdup( "Raleigh" );
513 if( ! icon_theme_name )
13f1ec51 514 icon_theme_name = g_strdup( "hicolor" );
7fbfd5a0 515#if CURSOR_THEME
13f1ec51
DB
516 if( ! cursor_theme_name )
517 cursor_theme_name = g_strdup( "default" );
7fbfd5a0 518#endif
08024023
DB
519 if( ! font_name )
520 font_name = g_strdup( "Sans 10" );
13f1ec51
DB
521 if( ! cursor_theme_size )
522 cursor_theme_size = 16;
08024023 523
13f1ec51
DB
524 /* no lxsession-settings daemon, use gtkrc-2.0 */
525 if( ! under_lxsession )
08024023
DB
526 write_rc_file( tmp_rc_file );
527
a78e4e46
DB
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
08024023
DB
534 INIT_LIST( gtk_theme, "gtk-theme-name" )
535 INIT_LIST( icon_theme, "gtk-icon-theme-name" )
7fbfd5a0
DB
536
537#if CURSOR_THEME
13f1ec51 538 INIT_LIST( cursor_theme, "gtk-cursor-theme-name" )
7fbfd5a0
DB
539#endif
540
a78e4e46 541 gtk_font_button_set_font_name( (GtkFontButton*)LOOKUP(font), font_name );
7fbfd5a0
DB
542
543#if CURSOR_THEME
a78e4e46 544 gtk_range_set_value( GTK_RANGE(LOOKUP(cursor_theme_size)), cursor_theme_size );
7fbfd5a0 545#endif
08024023 546
a78e4e46 547 gtk_combo_box_set_active( GTK_COMBO_BOX(LOOKUP(tb_style)), tb_style < 4 ? tb_style : 3 );
08024023 548
bd2899cf 549 GET_WIDGET_WITH_TYPE( demo_box, GTK_WIDGET );
08024023
DB
550 gtk_widget_show( demo_box );
551
13f1ec51 552 if( under_lxsession )
08024023 553 {
13f1ec51 554 /* XSettings daemon of lxsession is running, gtkrc is useless.
08024023
DB
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 );
a78e4e46 559 gtk_widget_show( dlg );
08024023
DB
560 }
561 else
562 {
13f1ec51 563 /* no lxsession-settings daemon, use gtkrc-2.0 and load preview in another process */
08024023
DB
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 }
573
574 disable_apply();
a78e4e46
DB
575
576 g_object_unref(builder);
577 return dlg;
08024023
DB
578}
579
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
13f1ec51 587 if( under_lxsession )
08024023 588 {
13f1ec51
DB
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");
08024023
DB
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);
606}
607
608void
609on_apply_clicked (GtkButton *button,
610 gpointer user_data)
611{
13f1ec51
DB
612 if( under_lxsession )
613 write_lxsession_config();
08024023
DB
614 else
615 write_rc_file( rc_file );
616
617 reload_all_programs( FALSE );
618 disable_apply();
619}
620
621
622void
623on_font_changed (GtkFontButton *fontbutton,
624 gpointer user_data)
625{
626 const char* name = gtk_font_button_get_font_name(fontbutton);
627 if( name && font_name && 0 == strcmp( name, font_name ) )
628 return;
629 g_free( font_name );
630 font_name = g_strdup( name );
631
13f1ec51 632 if( under_lxsession )
08024023
DB
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 }
643}
644
645void
646on_install_theme_clicked (GtkButton *button,
647 gpointer user_data)
648{
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 );
654
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
bd2899cf
DB
659 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
660 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
08024023
DB
661
662 if( gtk_dialog_run( (GtkDialog*)fc ) == GTK_RESPONSE_OK )
663 {
bd2899cf 664 char* file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
08024023
DB
665 char* argv[]={
666 PACKAGE_DATA_DIR"/lxappearance/install-icon-theme.sh",
667 file, NULL };
668 int status = 0;
669 char* stdo = NULL;
670 if( g_spawn_sync( NULL, argv, NULL, 0, NULL, NULL, &stdo, NULL, &status, NULL ) && 0 == status )
671 {
672 char* sep = stdo ? strchr( stdo, '\n' ) : NULL;
673 if( sep )
674 *sep = '\0';
675
676 /* reload all icon themes */
677 gtk_list_store_clear( icon_theme_list );
678 load_icon_themes( icon_theme_list, stdo ? stdo : "" );
13f1ec51 679
7fbfd5a0 680#if CURSOR_THEME
13f1ec51
DB
681 /* reload all cursor themes */
682 gtk_list_store_clear( cursor_theme_list );
683 load_cursor_themes( cursor_theme_list, stdo ? stdo : "" );
7fbfd5a0 684#endif
08024023
DB
685 }
686 g_free( file );
687 }
688
689 gtk_widget_destroy( fc );
690}
691
692
693void
694on_remove_theme_clicked (GtkButton *button,
695 gpointer user_data)
696{
697
698}
699
13f1ec51
DB
700void
701on_cursor_size_changed (GtkHScale *cursorsizescale,
702 gpointer user_data)
703{
a78e4e46 704#if CURSOR_THEME
13f1ec51
DB
705 cursor_theme_size = gtk_range_get_value( GTK_RANGE(cursorsizescale) );
706
707 if( under_lxsession )
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 }
7fbfd5a0 717#endif
a78e4e46 718}
08024023
DB
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;
728
13f1ec51 729 if( under_lxsession )
08024023
DB
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 }
739}
740