2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
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.
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.
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.
29 #include "prefswindow.h"
32 typedef struct _PrefsWindow PrefsWindow;
38 GtkWidget *scrolledwindow1;
42 GtkWidget *labelframe;
45 GtkWidget *confirm_area;
47 GtkWidget *cancel_btn;
50 GtkWidget *empty_page;
56 static gboolean ctree_select_row(GtkCTree *ctree, GList *node, gint column, gpointer user_data)
59 PrefsWindow *prefswindow = (PrefsWindow *) user_data;
63 page = (PrefsPage *) gtk_ctree_node_get_row_data(GTK_CTREE(ctree), GTK_CTREE_NODE(node));
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);
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;
80 labeltext = (gchar *) strrchr(page->path, '/');
81 if (labeltext == NULL)
82 labeltext = page->path;
84 labeltext = labeltext + 1;
85 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
87 pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
89 gtk_notebook_set_page(GTK_NOTEBOOK(prefswindow->notebook), pagenum);
94 static void save_all_pages(GSList *prefs_pages)
98 for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
99 PrefsPage *page = (PrefsPage *) cur->data;
101 if (page->page_open) {
102 page->save_page(page);
107 static void close_all_pages(GSList *prefs_pages)
111 for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
112 PrefsPage *page = (PrefsPage *) cur->data;
114 if (page->page_open) {
115 page->destroy_widget(page);
116 page->page_open = FALSE;
121 static void apply_button_released(GtkButton *button, gpointer user_data)
123 PrefsWindow *prefswindow = (PrefsWindow *) user_data;
125 save_all_pages(prefswindow->prefs_pages);
128 static void ok_button_released(GtkButton *button, gpointer user_data)
130 PrefsWindow *prefswindow = (PrefsWindow *) user_data;
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);
139 static void close_prefs_window(PrefsWindow *prefswindow)
141 debug_print("prefs window closed\n");
143 close_all_pages(prefswindow->prefs_pages);
144 gtk_widget_destroy(prefswindow->window);
145 g_slist_free(prefswindow->prefs_pages);
149 static void cancel_button_released(GtkButton *button, gpointer user_data)
151 PrefsWindow *prefswindow = (PrefsWindow *) user_data;
153 close_prefs_window(prefswindow);
156 static gboolean window_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data)
158 PrefsWindow *prefswindow = (PrefsWindow *) user_data;
160 close_prefs_window(prefswindow);
170 static gboolean find_child_by_name(GtkCTree *ctree, GtkCTreeNode *node, struct name_search *name_search)
174 text = GTK_CELL_TEXT(GTK_CTREE_ROW(node)->row.cell[0])->text;
178 if (strcmp(text, name_search->text) == 0)
179 name_search->node = node;
184 void prefswindow_open(const gchar *title, GSList *prefs_pages, gpointer data)
186 static gchar *titles [1];
189 PrefsWindow *prefswindow;
191 titles[0] = _("Page Index");
193 prefswindow = g_new0(PrefsWindow, 1);
195 prefswindow->data = data;
196 prefswindow->prefs_pages = g_slist_copy(prefs_pages);
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);
206 prefswindow->table1 = gtk_table_new(2, 2, FALSE);
207 gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->table1);
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);
213 prefswindow->ctree = gtk_ctree_new_with_titles(1, 0, titles);
214 gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), prefswindow->ctree);
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);
220 prefswindow->table2 = gtk_table_new(1, 2, FALSE);
221 gtk_container_add(GTK_CONTAINER(prefswindow->frame), prefswindow->table2);
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);
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);
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);
238 prefswindow->empty_page = gtk_label_new("");
239 gtk_container_add(GTK_CONTAINER(prefswindow->notebook), prefswindow->empty_page);
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;
247 struct name_search name_search;
249 split = g_strsplit(page->path, "/", 0);
250 for (i = 0; split[i] != NULL; i++) {
252 name_search.text = part;
253 name_search.node = NULL;
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);
257 if (name_search.node) {
258 node = name_search.node;
261 node = gtk_ctree_insert_node(GTK_CTREE(prefswindow->ctree), node, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, TRUE);
265 gtk_ctree_node_set_row_data(GTK_CTREE(prefswindow->ctree), node, page);
267 gtk_signal_connect(GTK_OBJECT(prefswindow->ctree), "tree-select-row", GTK_SIGNAL_FUNC(ctree_select_row), prefswindow);
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);
278 gtkut_button_set_create(&prefswindow->confirm_area,
279 &prefswindow->ok_btn, _("OK"),
280 &prefswindow->cancel_btn, _("Cancel"),
281 &prefswindow->apply_btn, _("Apply"));
283 gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->confirm_area, 0, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL, 2, 2);
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);
290 gtk_widget_show_all(prefswindow->window);