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