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