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