0.9.6claws5
[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 "utils.h"
29 #include "prefswindow.h"
30 #include "gtkutils.h"
31
32 typedef struct _PrefsWindow PrefsWindow;
33 typedef struct _PrefsTreeNode PrefsTreeNode;
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 *notebook;
46         GtkWidget *confirm_area;
47         GtkWidget *ok_btn;
48         GtkWidget *cancel_btn;
49         GtkWidget *apply_btn;
50
51         GtkWidget *empty_page;
52
53         gpointer   data;
54         GSList    *prefs_pages;
55 };
56
57 struct _PrefsTreeNode
58 {
59         PrefsPage *page;
60         gfloat     treeweight;
61 };
62
63 static gboolean ctree_select_row(GtkCTree *ctree, GList *node, gint column, gpointer user_data)
64 {
65         PrefsTreeNode *prefsnode;
66         PrefsPage *page;
67         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
68         gchar *labeltext;
69         gint pagenum;
70
71         prefsnode = gtk_ctree_node_get_row_data(GTK_CTREE(ctree), GTK_CTREE_NODE(node));
72         page = prefsnode->page;
73
74         debug_print("%f\n", prefsnode->treeweight);
75
76         if (page == NULL) {
77                 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), "");
78                 pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
79                                                 prefswindow->empty_page);
80                 gtk_notebook_set_page(GTK_NOTEBOOK(prefswindow->notebook), pagenum);
81                 return FALSE;
82         }
83
84         if (!page->page_open) {
85                 page->create_widget(page, GTK_WINDOW(prefswindow->window), prefswindow->data);
86                 gtk_container_add(GTK_CONTAINER(prefswindow->notebook), page->widget);
87                 page->page_open = TRUE;
88         }
89
90         labeltext = (gchar *) strrchr(page->path, '/');
91         if (labeltext == NULL)
92                 labeltext = page->path;
93         else
94                 labeltext = labeltext + 1;
95         gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
96
97         pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
98                                         page->widget);
99         gtk_notebook_set_page(GTK_NOTEBOOK(prefswindow->notebook), pagenum);
100
101         return FALSE;
102 }
103
104 static void save_all_pages(GSList *prefs_pages)
105 {
106         GSList *cur;
107
108         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
109                 PrefsPage *page = (PrefsPage *) cur->data;
110
111                 if (page->page_open) {
112                         page->save_page(page);
113                 }
114         }
115 }
116
117 static void close_all_pages(GSList *prefs_pages)
118 {
119         GSList *cur;
120
121         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
122                 PrefsPage *page = (PrefsPage *) cur->data;
123
124                 if (page->page_open) {
125                         page->destroy_widget(page);
126                         page->page_open = FALSE;
127                 }
128         }       
129 }
130
131 static void apply_button_released(GtkButton *button, gpointer user_data)
132 {
133         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
134
135         save_all_pages(prefswindow->prefs_pages);
136 }
137
138 static void ok_button_released(GtkButton *button, gpointer user_data)
139 {
140         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
141
142         save_all_pages(prefswindow->prefs_pages);
143         close_all_pages(prefswindow->prefs_pages);
144         gtk_widget_destroy(prefswindow->window);
145         g_slist_free(prefswindow->prefs_pages);
146         g_free(prefswindow);
147 }
148
149 static void close_prefs_window(PrefsWindow *prefswindow)
150 {
151         debug_print("prefs window closed\n");
152
153         close_all_pages(prefswindow->prefs_pages);
154         gtk_widget_destroy(prefswindow->window);
155         g_slist_free(prefswindow->prefs_pages);
156         g_free(prefswindow);
157 }
158
159 static void cancel_button_released(GtkButton *button, gpointer user_data)
160 {
161         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
162
163         close_prefs_window(prefswindow);
164 }
165
166 static gboolean window_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data)
167 {
168         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
169
170         close_prefs_window(prefswindow);
171         return FALSE;
172 }
173
174 struct name_search
175 {
176         gchar *text;
177         GtkCTreeNode *node;
178 };
179
180 static gboolean find_child_by_name(GtkCTree *ctree, GtkCTreeNode *node, struct name_search *name_search)
181 {
182         gchar *text = NULL;
183
184         text = GTK_CELL_TEXT(GTK_CTREE_ROW(node)->row.cell[0])->text;
185         if (text == NULL)
186                 return FALSE;
187
188         if (strcmp(text, name_search->text) == 0)
189                 name_search->node = node;
190
191         return FALSE;
192 }
193
194 gint compare_func(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
195 {
196         PrefsTreeNode *prefsnode1 = ((GtkCListRow *)ptr1)->data;
197         PrefsTreeNode *prefsnode2 = ((GtkCListRow *)ptr2)->data;
198
199         if (prefsnode1 == NULL || prefsnode2 == NULL)
200                 return 0;
201
202         return prefsnode1->treeweight > prefsnode2->treeweight ? -1 : 
203                prefsnode1->treeweight < prefsnode2->treeweight ?  1 : 
204                                                                   0;
205 }
206
207 void prefswindow_open(const gchar *title, GSList *prefs_pages, gpointer data)
208 {
209         static gchar *titles [1];
210         GSList *cur;
211         gint optsize;
212         PrefsWindow *prefswindow;
213
214         titles[0] = _("Page Index");
215
216         prefswindow = g_new0(PrefsWindow, 1);
217
218         prefswindow->data = data;
219         prefswindow->prefs_pages = g_slist_copy(prefs_pages);
220
221         prefswindow->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
222         gtk_window_set_title(GTK_WINDOW(prefswindow->window), title);
223         gtk_window_set_default_size(GTK_WINDOW(prefswindow->window), 600, 340);
224         gtk_window_position (GTK_WINDOW(prefswindow->window), GTK_WIN_POS_CENTER);
225         gtk_window_set_modal (GTK_WINDOW (prefswindow->window), TRUE);
226         gtk_window_set_policy (GTK_WINDOW(prefswindow->window), FALSE, TRUE, FALSE);
227         gtk_container_set_border_width(GTK_CONTAINER(prefswindow->window), 4);
228
229         prefswindow->table1 = gtk_table_new(2, 2, FALSE);
230         gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->table1);
231
232         prefswindow->scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
233         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
234         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->scrolledwindow1, 0, 1, 0, 1, GTK_FILL, GTK_FILL | GTK_EXPAND, 2, 2);
235
236         prefswindow->ctree = gtk_ctree_new_with_titles(1, 0, titles);
237         gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), prefswindow->ctree);
238
239         prefswindow->frame = gtk_frame_new(NULL);
240         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->frame), GTK_SHADOW_IN);
241         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->frame, 1, 2, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 2, 2);
242
243         prefswindow->table2 = gtk_table_new(1, 2, FALSE);
244         gtk_container_add(GTK_CONTAINER(prefswindow->frame), prefswindow->table2);
245
246         prefswindow->labelframe = gtk_frame_new(NULL);
247         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->labelframe), GTK_SHADOW_OUT);
248         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->labelframe, 0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 1, 1);
249
250         prefswindow->pagelabel = gtk_label_new("");
251         gtk_label_set_justify(GTK_LABEL(prefswindow->pagelabel), GTK_JUSTIFY_LEFT);
252         gtk_misc_set_alignment(GTK_MISC(prefswindow->pagelabel), 0, 0.0);
253         gtk_container_add(GTK_CONTAINER(prefswindow->labelframe), prefswindow->pagelabel);
254
255         prefswindow->notebook = gtk_notebook_new();
256         gtk_notebook_set_scrollable(GTK_NOTEBOOK(prefswindow->notebook), TRUE);
257         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
258         gtk_notebook_set_show_border(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
259         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->notebook, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 8, 8);
260
261         prefswindow->empty_page = gtk_label_new("");
262         gtk_container_add(GTK_CONTAINER(prefswindow->notebook), prefswindow->empty_page);
263
264         /* actually we should create a tree here */
265         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
266                 PrefsPage *page = (PrefsPage *)cur->data;
267                 GtkCTreeNode *node = NULL;
268                 gchar *text[2], **split, *part;
269                 int i;
270                 struct name_search name_search;
271                 PrefsTreeNode *prefsnode;
272
273                 split = g_strsplit(page->path, "/", 0);
274                 for (i = 0; split[i] != NULL; i++) {
275                         part = split[i];                        
276                         name_search.text = part;
277                         name_search.node = NULL;
278
279                         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);
280
281                         if (name_search.node) {
282                                 node = name_search.node;
283                         } else {
284                                 text[0] = part;
285                                 node = gtk_ctree_insert_node(GTK_CTREE(prefswindow->ctree), node, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, TRUE);
286
287                                 prefsnode = g_new0(PrefsTreeNode, 1);
288                                 prefsnode->treeweight = 0.0;
289                                 gtk_ctree_node_set_row_data_full(GTK_CTREE(prefswindow->ctree), node, prefsnode, g_free);
290                         }
291                 }
292                 g_strfreev(split);
293
294                 prefsnode = (PrefsTreeNode *) GTK_CTREE_ROW(node)->row.data;
295                 prefsnode->page = page;
296
297                 for (; node != NULL; node = GTK_CTREE_ROW(node)->parent) {
298                         PrefsTreeNode *curnode = (PrefsTreeNode *) GTK_CTREE_ROW(node)->row.data;
299
300                         if (page->weight > curnode->treeweight)
301                                 curnode->treeweight = page->weight;
302                 }
303         }
304         gtk_signal_connect(GTK_OBJECT(prefswindow->ctree), "tree-select-row", GTK_SIGNAL_FUNC(ctree_select_row), prefswindow);
305
306         gtk_clist_set_selection_mode(GTK_CLIST(prefswindow->ctree), GTK_SELECTION_BROWSE);
307         gtk_clist_column_titles_passive(GTK_CLIST(prefswindow->ctree));
308         optsize = gtk_clist_optimal_column_width(GTK_CLIST(prefswindow->ctree), 0);
309         gtk_clist_set_column_resizeable(GTK_CLIST(prefswindow->ctree), 0, TRUE);
310         gtk_clist_set_column_auto_resize(GTK_CLIST(prefswindow->ctree), 0, FALSE);
311         gtk_clist_set_column_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
312         gtk_clist_set_column_min_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
313         gtk_clist_set_column_max_width(GTK_CLIST(prefswindow->ctree), 0, optsize);
314         gtk_clist_set_compare_func(GTK_CLIST(prefswindow->ctree), compare_func);
315         gtk_ctree_sort_recursive(GTK_CTREE(prefswindow->ctree), NULL);
316
317         gtkut_button_set_create(&prefswindow->confirm_area,
318                                 &prefswindow->ok_btn,           _("OK"),
319                                 &prefswindow->cancel_btn,       _("Cancel"),
320                                 &prefswindow->apply_btn,        _("Apply"));
321
322         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->confirm_area, 0, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL, 2, 2);
323
324         gtk_signal_connect(GTK_OBJECT(prefswindow->ok_btn), "released", GTK_SIGNAL_FUNC(ok_button_released), prefswindow);
325         gtk_signal_connect(GTK_OBJECT(prefswindow->cancel_btn), "released", GTK_SIGNAL_FUNC(cancel_button_released), prefswindow);
326         gtk_signal_connect(GTK_OBJECT(prefswindow->apply_btn), "released", GTK_SIGNAL_FUNC(apply_button_released), prefswindow);
327         gtk_signal_connect(GTK_OBJECT(prefswindow->window), "delete_event", GTK_SIGNAL_FUNC(window_closed), prefswindow);
328
329         gtk_widget_show_all(prefswindow->window);
330 }