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