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