7a2431f0a80ed3301af9ccdb1fd41e1afca043e5
[claws.git] / src / gtk / prefswindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <gtk/gtk.h>
26
27 #include "intl.h"
28 #include "prefswindow.h"
29 #include "../gtkutils.h"
30
31 GSList *prefs_pages = NULL;
32
33 typedef struct _PrefsWindow PrefsWindow;
34
35 struct _PrefsWindow
36 {
37         GtkWidget *window;
38         GtkWidget *table1;
39         GtkWidget *scrolledwindow1;
40         GtkWidget *ctree;
41         GtkWidget *table2;
42         GtkWidget *pagelabel;
43         GtkWidget *labelframe;
44         GtkWidget *frame;
45         GtkWidget *page_widget;
46         GtkWidget *confirm_area;
47         GtkWidget *ok_btn;
48         GtkWidget *cancel_btn;
49         GtkWidget *apply_btn;
50 };
51
52 void prefswindow_register_new_page(PrefsPage *page)
53 {
54         prefs_pages = g_slist_append(prefs_pages, page);
55 }
56
57 static gboolean ctree_select_row(GtkCTree *ctree, GList *node, gint column, gpointer user_data)
58 {
59         PrefsPage *page;
60         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
61         gchar *labeltext;
62
63         page = (PrefsPage *) gtk_ctree_node_get_row_data(GTK_CTREE(ctree), GTK_CTREE_NODE(node));
64
65         if (prefswindow->page_widget != NULL)
66                 gtk_container_remove(GTK_CONTAINER(prefswindow->table2), prefswindow->page_widget);
67
68         if (page == NULL) {
69                 GtkWidget *widget;
70                 
71                 widget = gtk_label_new("");
72
73                 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), "");
74                 gtk_table_attach(GTK_TABLE(prefswindow->table2), widget, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
75                 prefswindow->page_widget = widget;
76                 return FALSE;
77         }
78
79         if (!page->page_open) {
80                 page->create_widget(page);
81                 gtk_widget_ref(page->widget);
82                 gtk_widget_show_all(page->widget);
83                 page->page_open = TRUE;
84         }
85
86         labeltext = (gchar *) strrchr(page->path, '/');
87         if (labeltext == NULL)
88                 labeltext = page->path;
89         else
90                 labeltext = labeltext + 1;
91         gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
92
93         prefswindow->page_widget = page->widget;
94         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->page_widget, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 8, 8);
95
96         return FALSE;
97 }
98
99 static void save_all_pages()
100 {
101         GSList *cur;
102
103         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
104                 PrefsPage *page = (PrefsPage *) cur->data;
105
106                 if (page->page_open) {
107                         page->save_page(page);
108                 }
109         }
110 }
111
112 static void close_all_pages()
113 {
114         GSList *cur;
115
116         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
117                 PrefsPage *page = (PrefsPage *) cur->data;
118
119                 if (page->page_open) {
120                         gtk_widget_unref(page->widget);
121                         page->destroy_widget(page);
122                         page->page_open = FALSE;
123                 }
124         }       
125 }
126
127 static void apply_button_released(GtkButton *button, gpointer user_data)
128 {
129         save_all_pages();
130 }
131
132 static void ok_button_released(GtkButton *button, gpointer user_data)
133 {
134         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
135
136         save_all_pages();
137         gtk_widget_destroy(prefswindow->window);
138         close_all_pages();
139         g_free(prefswindow);
140 }
141
142 static void cancel_button_released(GtkButton *button, gpointer user_data)
143 {
144         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
145
146         gtk_widget_destroy(prefswindow->window);
147         close_all_pages();
148         g_free(prefswindow);
149 }
150
151 struct name_search
152 {
153         gchar *text;
154         GtkCTreeNode *node;
155 };
156
157 static gboolean find_child_by_name(GtkCTree *ctree, GtkCTreeNode *node, struct name_search *name_search)
158 {
159         gchar *text = NULL;
160
161         text = GTK_CELL_TEXT(GTK_CTREE_ROW(node)->row.cell[0])->text;
162         if (text == NULL)
163                 return FALSE;
164
165         if (strcmp(text, name_search->text) == 0)
166                 name_search->node = node;
167
168         return FALSE;
169 }
170
171 void prefswindow_create()
172 {
173         static gchar *titles [] = {"Pages"};
174         GSList *cur;
175         gint optsize;
176         PrefsWindow *prefswindow;
177
178         prefswindow = g_new0(PrefsWindow, 1);
179
180         prefswindow->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
181         gtk_window_set_title(GTK_WINDOW(prefswindow->window), _("Preferences"));
182         gtk_window_set_default_size(GTK_WINDOW(prefswindow->window), 600, 340);
183         gtk_window_position (GTK_WINDOW(prefswindow->window), GTK_WIN_POS_CENTER);
184         gtk_window_set_modal (GTK_WINDOW (prefswindow->window), TRUE);
185         gtk_window_set_policy (GTK_WINDOW(prefswindow->window), FALSE, TRUE, FALSE);
186         gtk_container_set_border_width(GTK_CONTAINER(prefswindow->window), 4);
187
188         prefswindow->table1 = gtk_table_new(2, 2, FALSE);
189         gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->table1);
190
191         prefswindow->scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
192         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
193         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->scrolledwindow1, 0, 1, 0, 1, GTK_FILL, GTK_FILL | GTK_EXPAND, 2, 2);
194
195         prefswindow->ctree = gtk_ctree_new_with_titles(1, 0, titles);
196         gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), prefswindow->ctree);
197
198         prefswindow->frame = gtk_frame_new(NULL);
199         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->frame), GTK_SHADOW_IN);
200         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->frame, 1, 2, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 2, 2);
201
202         prefswindow->table2 = gtk_table_new(1, 2, FALSE);
203         gtk_container_add(GTK_CONTAINER(prefswindow->frame), prefswindow->table2);
204
205         prefswindow->labelframe = gtk_frame_new(NULL);
206         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->labelframe), GTK_SHADOW_OUT);
207         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->labelframe, 0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 1, 1);
208
209         prefswindow->pagelabel = gtk_label_new("");
210         gtk_label_set_justify(GTK_LABEL(prefswindow->pagelabel), GTK_JUSTIFY_LEFT);
211         gtk_misc_set_alignment(GTK_MISC(prefswindow->pagelabel), 0, 0.0);
212         gtk_container_add(GTK_CONTAINER(prefswindow->labelframe), prefswindow->pagelabel);
213
214         prefswindow->page_widget = gtk_label_new("");
215         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->page_widget, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 8, 8);
216
217         /* actually we should create a tree here */
218         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
219                 PrefsPage *page = (PrefsPage *)cur->data;
220                 GtkCTreeNode *node = NULL;
221                 gchar *text[2], **split, *part;
222                 int i;
223                 struct name_search name_search;
224
225                 split = g_strsplit(page->path, "/", 0);
226                 for (i = 0; split[i] != NULL; i++) {
227                         part = split[i];                        
228                         name_search.text = part;
229                         name_search.node = NULL;
230
231                         gtk_ctree_post_recursive_to_depth(GTK_CTREE(prefswindow->ctree), node, node != NULL ? GTK_CTREE_ROW(node)->level + 1 : 1, GTK_CTREE_FUNC(find_child_by_name), &name_search);
232
233                         if (name_search.node) {
234                                 node = name_search.node;
235                         } else {
236                                 text[0] = part;
237                                 node = gtk_ctree_insert_node(GTK_CTREE(prefswindow->ctree), node, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, TRUE);
238                         }
239                 }
240                 g_strfreev(split);
241                 gtk_ctree_node_set_row_data(GTK_CTREE(prefswindow->ctree), node, page);
242         }
243         gtk_signal_connect(GTK_OBJECT(prefswindow->ctree), "tree-select-row", GTK_SIGNAL_FUNC(ctree_select_row), prefswindow);
244
245         gtk_clist_set_selection_mode(GTK_CLIST(prefswindow->ctree), GTK_SELECTION_BROWSE);
246         gtk_clist_column_titles_passive(GTK_CLIST(prefswindow->ctree));
247         optsize = gtk_clist_optimal_column_width(GTK_CLIST(prefswindow->ctree), 0);
248         gtk_clist_set_column_resizeable(GTK_CLIST(prefswindow->ctree), 0, TRUE);
249         gtk_clist_set_column_auto_resize(GTK_CLIST(prefswindow->ctree), 0, FALSE);
250         gtk_clist_set_column_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
251         gtk_clist_set_column_min_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
252         gtk_clist_set_column_max_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
253
254         gtkut_button_set_create(&prefswindow->confirm_area,
255                                 &prefswindow->ok_btn,           _("OK"),
256                                 &prefswindow->cancel_btn,       _("Cancel"),
257                                 &prefswindow->apply_btn,        _("Apply"));
258
259         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->confirm_area, 0, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL, 2, 2);
260
261         gtk_signal_connect(GTK_OBJECT(prefswindow->ok_btn), "released", GTK_SIGNAL_FUNC(ok_button_released), prefswindow);
262         gtk_signal_connect(GTK_OBJECT(prefswindow->cancel_btn), "released", GTK_SIGNAL_FUNC(cancel_button_released), prefswindow);
263         gtk_signal_connect(GTK_OBJECT(prefswindow->apply_btn), "released", GTK_SIGNAL_FUNC(apply_button_released), prefswindow);
264
265         gtk_widget_show_all(prefswindow->window);
266 }
267
268 void prefswindow_destroy_all_pages()
269 {
270         GSList *cur;
271
272         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
273                 PrefsPage *page = (PrefsPage *) cur->data;
274
275                 page->destroy_page(page);
276         }
277 }