2827a7f6662870e1625fb20e77b343e20c9b8c4d
[lxde/lxappearance.git] / src / main-dlg.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <gtk/gtk.h>
6
7 #include "main-dlg.h"
8 #include "main-dlg-ui.h"
9 #include "glade-support.h"
10
11 #define GET_WIDGET( name ) name = lookup_widget( dlg, #name )
12 #define INIT_LIST(name, prop) \
13 GET_WIDGET( name##_view ); \
14 name##_list = init_tree_view( name##_view, G_CALLBACK(on_list_sel_changed), prop ); \
15 load_##name##s( name##_list );
16
17 static GtkTreeView* gtk_theme_view = NULL;
18 static GtkListStore* gtk_theme_list = NULL;
19
20 static GtkTreeView* icon_theme_view = NULL;
21 static GtkListStore* icon_theme_list = NULL;
22
23 static GtkTreeView* font_view = NULL;
24 static GtkListStore* font_list = NULL;
25
26 static GtkIconView* icon_view = NULL;
27
28 static void reload_demo_icons()
29 {
30 static const char** icon_names[]={
31 "gnome-fs-home",
32 "gnome-fs-desktop",
33 "gnome-fs-directory",
34 "gnome-fs-trash-empty",
35 "gnome-fs-regular",
36 "gnome-fs-executable",
37 "gnome-mime-image",
38 "gnome-mime-text"
39 };
40
41 int i;
42 GtkIconTheme* theme = gtk_icon_theme_get_default();
43 GtkListStore* demo_icon_list;
44
45 demo_icon_list = gtk_list_store_new( 2, GDK_TYPE_PIXBUF, G_TYPE_STRING );
46
47 for( i = 0; i < G_N_ELEMENTS(icon_names); ++i )
48 {
49 GtkTreeIter it;
50 GdkPixbuf* icon = gtk_icon_theme_load_icon( theme, icon_names[i], 32, 0, NULL );
51 gtk_list_store_append( demo_icon_list, &it );
52 gtk_list_store_set( demo_icon_list, &it, 0, icon, 1, icon_names[i], -1 );
53 if( icon )
54 g_object_unref( icon );
55 }
56 gtk_icon_view_set_model( icon_view, demo_icon_list );
57 g_object_unref( demo_icon_list );
58 }
59
60 static void reload_theme()
61 {
62
63 gtk_rc_reparse_all();
64 }
65
66 static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
67 {
68 GtkTreeIter it;
69 GtkTreeModel* model;
70 if( gtk_tree_selection_get_selected( sel, &model, &it ) )
71 {
72 char* name;
73 gtk_tree_model_get( model, &it, 0, &name, -1 );
74 if( model == font_list ) /* this is a font */
75 {
76
77 }
78 g_object_set( gtk_settings_get_default(), prop, name, NULL );
79 g_free( name );
80
81 if( model == icon_theme_list ) /* icon theme is changed */
82 reload_demo_icons();
83 }
84 }
85
86 static GtkListStore* init_tree_view( GtkTreeView* view, GCallback on_sel_changed, const char* prop )
87 {
88 GtkTreeViewColumn* col;
89 GtkListStore* list;
90 col = gtk_tree_view_column_new_with_attributes( NULL, gtk_cell_renderer_text_new(), "text", 0, NULL );
91 gtk_tree_view_append_column( view, col );
92 g_signal_connect( gtk_tree_view_get_selection(view), "changed", on_sel_changed, prop );
93
94 list = gtk_list_store_new( 1, G_TYPE_STRING );
95 gtk_tree_view_set_model( view, (GtkTreeModel*)list );
96 g_object_unref( list );
97 return list;
98 }
99
100 static void load_themes_from_dir( GtkListStore* list,
101 const char* dir_path,
102 const char* lookup )
103 {
104 GDir* dir;
105 if( dir = g_dir_open( dir_path, 0, NULL ) )
106 {
107 char* name;
108 while( name = g_dir_read_name( dir ) )
109 {
110 char* file = g_build_filename( dir_path, name, lookup, NULL );
111 if( g_file_test( file, G_FILE_TEST_EXISTS ) )
112 {
113 GtkTreeIter it;
114 gtk_list_store_append( list, &it );
115 gtk_list_store_set( list, &it, 0, name, -1 );
116 }
117 g_free( file );
118 }
119 g_dir_close( dir );
120 }
121 }
122
123 static void load_from_data_dirs( GtkListStore* list,
124 const char* relative_path,
125 const char* lookup )
126 {
127 const char* const *dirs = g_get_system_data_dirs();
128 const char* const *dir;
129 char* dir_path;
130 for( dir = dirs; *dir; ++dir )
131 {
132 dir_path = g_build_filename( *dir, relative_path, NULL );
133 load_themes_from_dir( list, dir_path, lookup );
134 g_free( dir_path );
135 }
136 dir_path = g_build_filename( g_get_user_data_dir(), relative_path, NULL );
137 load_themes_from_dir( list, dir_path, lookup );
138 g_free( dir_path );
139 }
140
141 static void load_gtk_themes( GtkListStore* list )
142 {
143 load_from_data_dirs( list, "themes", "gtk-2.0" );
144 }
145
146 static void load_icon_themes( GtkListStore* list )
147 {
148 char* path;
149 load_from_data_dirs( list, "icons", "index.theme" );
150 path = g_build_filename( g_get_home_dir(), ".icons", NULL );
151 load_themes_from_dir( list, path, "index.theme" );
152 g_free( path );
153 }
154
155 static void load_fonts( GtkListStore* list )
156 {
157
158 }
159
160 void main_dlg_init( GtkWidget* dlg )
161 {
162 INIT_LIST( gtk_theme, "gtk-theme-name" )
163 INIT_LIST( icon_theme, "gtk-icon-theme-name" )
164 INIT_LIST( font, "gtk-font-name" )
165
166 GET_WIDGET( icon_view );
167 gtk_icon_view_set_pixbuf_column( icon_view, 0 );
168 gtk_icon_view_set_text_column( icon_view, 1 );
169 gtk_icon_view_set_item_width( icon_view, 64 );
170 gtk_icon_view_set_column_spacing( icon_view, 8 );
171 gtk_icon_view_set_row_spacing( icon_view, 8 );
172 reload_demo_icons();
173 }
174
175 void
176 on_apply_clicked (GtkButton *button,
177 gpointer user_data)
178 {
179
180 }
181