69c9adbbf126f4febba12f36da695816140b9e03
[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 <glib.h>
25 #include <glib/gi18n.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <gtk/gtktext.h>
29 #include <gdk/gdkkeysyms.h>
30
31 #include "utils.h"
32 #include "prefswindow.h"
33 #include "gtkutils.h"
34
35 enum { 
36         PREFS_PAGE_TITLE,               /* page title */
37         PREFS_PAGE_DATA,                /* PrefsTreeNode data */        
38         PREFS_PAGE_DATA_AUTO_FREE,      /* auto free for PREFS_PAGE_DATA */
39         PREFS_PAGE_WEIGHT,              /* weight */
40         PREFS_PAGE_INDEX,               /* index in original page list */
41         N_PREFS_PAGE_COLUMNS
42 };
43
44 typedef struct _PrefsWindow PrefsWindow;
45 typedef struct _PrefsTreeNode PrefsTreeNode;
46
47 struct _PrefsWindow
48 {
49         GtkWidget *window;
50         GtkWidget *table1;
51         GtkWidget *scrolledwindow1;
52         GtkWidget *tree_view;
53         GtkWidget *table2;
54         GtkWidget *pagelabel;
55         GtkWidget *labelframe;
56         GtkWidget *frame;
57         GtkWidget *notebook;
58         GtkWidget *confirm_area;
59         GtkWidget *ok_btn;
60         GtkWidget *cancel_btn;
61         GtkWidget *apply_btn;
62
63         GtkWidget *empty_page;
64
65         gpointer         data;
66         GSList          *prefs_pages;
67         GtkDestroyNotify func;
68 };
69
70 struct _PrefsTreeNode
71 {
72         PrefsPage *page;
73         gfloat     treeweight; /* GTK2: not used */
74 };
75
76 static GtkTreeStore *prefswindow_create_data_store      (void);
77 static GtkWidget *prefswindow_tree_view_create          (PrefsWindow* prefswindow);
78 static void prefs_filtering_create_tree_view_columns    (GtkWidget *tree_view);
79 static gboolean prefswindow_row_selected                (GtkTreeSelection *selector,
80                                                          GtkTreeModel *model, 
81                                                          GtkTreePath *path,
82                                                          gboolean currently_selected,
83                                                          gpointer data);
84
85 static void save_all_pages(GSList *prefs_pages)
86 {
87         GSList *cur;
88
89         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
90                 PrefsPage *page = (PrefsPage *) cur->data;
91
92                 if (page->page_open) {
93                         page->save_page(page);
94                 }
95         }
96 }
97
98 static gboolean query_can_close_all_pages(GSList *prefs_pages)
99 {
100         GSList *cur;
101
102         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
103                 PrefsPage *page = (PrefsPage *) cur->data;
104
105                 if (page->can_close)
106                         if (!page->can_close(page))
107                                 return FALSE;
108         }
109         return TRUE;
110 }
111
112 static void close_all_pages(GSList *prefs_pages)
113 {
114         GSList *cur;
115
116         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
117                 PrefsPage *page = (PrefsPage *) cur->data;
118
119                 if (page->page_open) {
120                         page->destroy_widget(page);
121                         page->page_open = FALSE;
122                 }
123         }       
124 }
125
126 static void apply_button_released(GtkButton *button, gpointer user_data)
127 {
128         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
129
130         save_all_pages(prefswindow->prefs_pages);
131 }
132
133 static void close_prefs_window(PrefsWindow *prefswindow)
134 {
135         debug_print("prefs window closed\n");
136
137         close_all_pages(prefswindow->prefs_pages);
138
139         gtk_widget_destroy(prefswindow->window);
140         g_slist_free(prefswindow->prefs_pages);
141         if(prefswindow->func != NULL)
142                 prefswindow->func(prefswindow->data);
143         g_free(prefswindow);
144 }
145
146 static void ok_button_released(GtkButton *button, gpointer user_data)
147 {
148         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
149
150         if (query_can_close_all_pages(prefswindow->prefs_pages)) {
151                 save_all_pages(prefswindow->prefs_pages);
152                 close_prefs_window(prefswindow);
153         }               
154 }
155
156 static void cancel_button_released(GtkButton *button, gpointer user_data)
157 {
158         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
159
160         close_prefs_window(prefswindow);
161 }
162
163 static gboolean window_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data)
164 {
165         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
166
167         close_prefs_window(prefswindow);
168         return FALSE;
169 }
170
171 static gboolean prefswindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
172                                     PrefsWindow *data)
173 {
174         GtkWidget *focused_child;
175
176         if (event) {
177                 switch (event->keyval) {
178                         case GDK_Escape :
179                                 cancel_button_released(NULL, data);
180                                 break;
181                         case GDK_Return : 
182                         case GDK_KP_Enter :
183                                 focused_child = gtkut_get_focused_child
184                                         (GTK_CONTAINER(data->notebook));
185                                 /* Press ok, if the focused child is not a text view
186                                  * and text (anything that accepts return) (can pass
187                                  * NULL to any of the GTK_xxx() casts) */
188                                 if (!GTK_IS_TEXT_VIEW(focused_child))
189                                         ok_button_released(NULL, data);
190                                 break;
191                         default:
192                                 break;
193                 }
194         }
195         return FALSE;
196 }
197
198 typedef struct FindNodeByName {
199         const gchar *name;
200         gboolean     found;
201         GtkTreeIter  node;
202 } FindNodeByName;
203
204 static gboolean find_node_by_name(GtkTreeModel *model, GtkTreePath *path,
205                                   GtkTreeIter *iter, FindNodeByName *data)
206 {
207         gchar *name;
208         gboolean result = FALSE;
209
210         gtk_tree_model_get(model, iter, PREFS_PAGE_TITLE, &name, -1);
211         if (name) {
212                 result = strcmp(name, data->name) == 0;
213                 if (result) {
214                         data->found = TRUE;
215                         data->node  = *iter;
216                 }                       
217                 g_free(name);
218         }
219         
220         return result;
221 }
222
223 static gint prefswindow_tree_sort_by_weight(GtkTreeModel *model, 
224                                             GtkTreeIter  *a,
225                                             GtkTreeIter  *b,
226                                             gpointer      context)
227 {
228         gfloat f1, f2;
229         gint i1, i2;
230
231         /* From observation sorting should keep in account the original
232          * order in the prefs_pages list. I.e. if equal weight, prefer 
233          * the index in the pages list */ 
234         gtk_tree_model_get(model, a, 
235                            PREFS_PAGE_INDEX,  &i1,
236                            PREFS_PAGE_WEIGHT, &f1, -1);
237         gtk_tree_model_get(model, b, 
238                            PREFS_PAGE_INDEX,  &i2,
239                            PREFS_PAGE_WEIGHT, &f2, -1);
240
241         return f1 < f2 ? -1 : (f1 > f2 ?  1 : 
242               (i1 < i2 ?  1 : (i1 > i2 ? -1 : 0)));
243 }
244                                   
245 static void prefswindow_build_tree(GtkWidget *tree_view, GSList *prefs_pages)
246 {
247         GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model
248                         (GTK_TREE_VIEW(tree_view)));
249         GSList *cur;
250         gint index; /* index in pages list */
251         GtkTreeSelection *selection;
252         GtkTreeIter iter;
253         
254         for (cur = prefs_pages, index = 0; cur != NULL; cur = g_slist_next(cur), index++) {
255                 PrefsPage *page = (PrefsPage *)cur->data;
256                 FindNodeByName find_name;
257                 GtkTreeIter node, child;
258                 PrefsTreeNode *prefs_node;
259                 int i;
260
261                 /* each page tree component string */
262                 for (i = 0; page->path[i] != NULL; i++) {
263                         find_name.found = FALSE;
264                         find_name.name  = page->path[i];
265                         
266                         /* find node to attach to 
267                          * FIXME: we search the entire tree, so this is suboptimal... */
268                         gtk_tree_model_foreach(GTK_TREE_MODEL(store), 
269                                                (GtkTreeModelForeachFunc) find_node_by_name,
270                                                &find_name);
271                         if (find_name.found) {
272                                 node = find_name.node;
273                                 gtk_tree_model_get(GTK_TREE_MODEL(store), &node,
274                                                    PREFS_PAGE_DATA, &prefs_node,
275                                                    -1);
276                         } else {
277                                 GAuto *autoptr; 
278                         
279                                 /* create a new top level */
280                                 gtk_tree_store_append(store, &child, i == 0 ? NULL : &node);
281                                 prefs_node = g_new0(PrefsTreeNode, 1);
282                                 autoptr = g_auto_pointer_new(prefs_node);
283                                 gtk_tree_store_set(store, &child,
284                                                    PREFS_PAGE_TITLE, page->path[i],
285                                                    PREFS_PAGE_DATA,  prefs_node,
286                                                    PREFS_PAGE_DATA_AUTO_FREE, autoptr,
287                                                    PREFS_PAGE_INDEX, index,
288                                                    PREFS_PAGE_WEIGHT, 0.0f,
289                                                    -1);
290                                 g_auto_pointer_free(autoptr);
291                                 node = child;
292                         }
293                 }
294
295                 /* right now we have a node and its prefs_node */
296                 prefs_node->page = page;
297
298                 /* parents "inherit" the max weight of the children */
299                 do {
300                         gfloat f;
301                         
302                         gtk_tree_model_get(GTK_TREE_MODEL(store), &node, 
303                                            PREFS_PAGE_WEIGHT, &f,
304                                            -1);
305                         if (page->weight > f) {
306                                 f = page->weight;
307                                 gtk_tree_store_set(store, &node,
308                                                    PREFS_PAGE_WEIGHT, f,
309                                                    -1);
310                         }       
311                         child = node;   
312                 } while (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store),      
313                                                     &node, &child));
314         }
315
316         gtk_tree_view_expand_all(GTK_TREE_VIEW(tree_view));
317
318         /* set sort func & sort */
319         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store),
320                                         PREFS_PAGE_WEIGHT,
321                                         prefswindow_tree_sort_by_weight,
322                                         NULL, NULL);
323         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
324                                              PREFS_PAGE_WEIGHT,
325                                              GTK_SORT_DESCENDING);
326
327         /* select first one */                                       
328         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
329         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
330                 gtk_tree_selection_select_iter(selection, &iter);
331 }
332
333 void prefswindow_open_full(const gchar *title, GSList *prefs_pages, gpointer data, GtkDestroyNotify func)
334 {
335         gint optsize;
336         PrefsWindow *prefswindow;
337         GtkTreeStore *store;
338
339         prefswindow = g_new0(PrefsWindow, 1);
340
341         prefswindow->data = data;
342         prefswindow->func = func;
343         prefswindow->prefs_pages = g_slist_copy(prefs_pages);
344
345         prefswindow->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
346         gtk_window_set_title(GTK_WINDOW(prefswindow->window), title);
347         gtk_window_set_default_size(GTK_WINDOW(prefswindow->window), 600, 440);
348         gtk_window_set_position (GTK_WINDOW(prefswindow->window), GTK_WIN_POS_CENTER);
349         gtk_window_set_modal (GTK_WINDOW (prefswindow->window), TRUE);
350         gtk_window_set_resizable (GTK_WINDOW(prefswindow->window), TRUE);
351         gtk_container_set_border_width(GTK_CONTAINER(prefswindow->window), 4);
352
353         prefswindow->table1 = gtk_table_new(2, 2, FALSE);
354         gtk_widget_show(prefswindow->table1);
355         gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->table1);
356
357         prefswindow->scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
358         gtk_widget_show(prefswindow->scrolledwindow1);
359         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
360         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->scrolledwindow1, 0, 1, 0, 1, GTK_FILL, GTK_FILL | GTK_EXPAND, 2, 2);
361
362         prefswindow->tree_view = prefswindow_tree_view_create(prefswindow);
363         gtk_widget_show(prefswindow->tree_view);
364         gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), 
365                           prefswindow->tree_view);
366
367         prefswindow->frame = gtk_frame_new(NULL);
368         gtk_widget_show(prefswindow->frame);
369         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->frame), GTK_SHADOW_IN);
370         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->frame, 1, 2, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 2, 2);
371
372         prefswindow->table2 = gtk_table_new(1, 2, FALSE);
373         gtk_widget_show(prefswindow->table2);
374         gtk_container_add(GTK_CONTAINER(prefswindow->frame), prefswindow->table2);
375
376         prefswindow->labelframe = gtk_frame_new(NULL);
377         gtk_widget_show(prefswindow->labelframe);
378         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->labelframe), GTK_SHADOW_OUT);
379         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->labelframe, 0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 1, 1);
380
381         prefswindow->pagelabel = gtk_label_new("");
382         gtk_widget_show(prefswindow->pagelabel);
383         gtk_label_set_justify(GTK_LABEL(prefswindow->pagelabel), GTK_JUSTIFY_LEFT);
384         gtk_misc_set_alignment(GTK_MISC(prefswindow->pagelabel), 0, 0.0);
385         gtk_container_add(GTK_CONTAINER(prefswindow->labelframe), prefswindow->pagelabel);
386
387         prefswindow->notebook = gtk_notebook_new();
388         gtk_widget_show(prefswindow->notebook);
389         gtk_notebook_set_scrollable(GTK_NOTEBOOK(prefswindow->notebook), TRUE);
390         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
391         gtk_notebook_set_show_border(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
392         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->notebook, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 8, 8);
393
394         prefswindow->empty_page = gtk_label_new("");
395         gtk_widget_show(prefswindow->empty_page);
396         gtk_container_add(GTK_CONTAINER(prefswindow->notebook), prefswindow->empty_page);
397
398         prefswindow_build_tree(prefswindow->tree_view, prefs_pages);            
399
400         gtk_widget_grab_focus(prefswindow->tree_view);
401
402         gtkut_stock_button_set_create(&prefswindow->confirm_area,
403                                       &prefswindow->ok_btn,     GTK_STOCK_OK,
404                                       &prefswindow->cancel_btn, GTK_STOCK_CANCEL,
405                                       &prefswindow->apply_btn,  GTK_STOCK_APPLY);
406
407         gtk_widget_show_all(prefswindow->confirm_area);
408
409         gtk_table_attach(GTK_TABLE(prefswindow->table1), prefswindow->confirm_area, 0, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL, 2, 2);
410
411         g_signal_connect(G_OBJECT(prefswindow->ok_btn), "released", 
412                          G_CALLBACK(ok_button_released), prefswindow);
413         g_signal_connect(G_OBJECT(prefswindow->cancel_btn), "released", 
414                          G_CALLBACK(cancel_button_released), prefswindow);
415         g_signal_connect(G_OBJECT(prefswindow->apply_btn), "released", 
416                          G_CALLBACK(apply_button_released), prefswindow);
417         g_signal_connect(G_OBJECT(prefswindow->window), "delete_event", 
418                          G_CALLBACK(window_closed), prefswindow);
419         g_signal_connect(G_OBJECT(prefswindow->window), "key_press_event",
420                            G_CALLBACK(prefswindow_key_pressed), &(prefswindow->window));
421
422         gtk_widget_show(prefswindow->window);
423 }
424
425 void prefswindow_open(const gchar *title, GSList *prefs_pages, gpointer data)
426 {
427         prefswindow_open_full(title, prefs_pages, data, NULL);
428 }
429
430 static GtkTreeStore *prefswindow_create_data_store(void)
431 {
432         return gtk_tree_store_new(N_PREFS_PAGE_COLUMNS,
433                                   G_TYPE_STRING,
434                                   G_TYPE_POINTER,
435                                   G_TYPE_AUTO_POINTER,
436                                   G_TYPE_FLOAT,
437                                   G_TYPE_INT,
438                                   -1);
439 }
440
441 static GtkWidget *prefswindow_tree_view_create(PrefsWindow *prefswindow)
442 {
443         GtkTreeView *tree_view;
444         GtkTreeSelection *selector;
445         GtkTreeModel *model;
446
447         model = GTK_TREE_MODEL(prefswindow_create_data_store());
448         tree_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
449         g_object_unref(model);
450         gtk_tree_view_set_rules_hint(tree_view, TRUE);
451         
452         selector = gtk_tree_view_get_selection(tree_view);
453         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
454         gtk_tree_selection_set_select_function(selector, prefswindow_row_selected,
455                                                prefswindow, NULL);
456
457         /* create the columns */
458         prefs_filtering_create_tree_view_columns(GTK_WIDGET(tree_view));
459
460         return GTK_WIDGET(tree_view);
461 }
462
463 static void prefs_filtering_create_tree_view_columns(GtkWidget *tree_view)
464 {
465         GtkTreeViewColumn *column;
466         GtkCellRenderer *renderer;
467
468         renderer = gtk_cell_renderer_text_new();
469         column = gtk_tree_view_column_new_with_attributes
470                 (_("Page Index"),
471                  renderer,
472                  "text", PREFS_PAGE_TITLE,
473                  NULL);
474         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);          
475 }
476
477 static gboolean prefswindow_row_selected(GtkTreeSelection *selector,
478                                          GtkTreeModel *model, 
479                                          GtkTreePath *path,
480                                          gboolean currently_selected,
481                                          gpointer data)
482 {
483         PrefsTreeNode *prefsnode;
484         PrefsPage *page;
485         PrefsWindow *prefswindow = (PrefsWindow *) data;
486         gchar *labeltext;
487         gint pagenum, i;
488         GtkTreeIter iter;
489
490         if (currently_selected) 
491                 return TRUE;
492
493         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
494                 return TRUE;
495
496         gtk_tree_model_get(model, &iter, PREFS_PAGE_DATA, &prefsnode, -1);
497         page = prefsnode->page;
498
499         debug_print("%f\n", prefsnode->treeweight);
500
501         if (page == NULL) {
502                 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), "");
503                 pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
504                                                 prefswindow->empty_page);
505                 gtk_notebook_set_page(GTK_NOTEBOOK(prefswindow->notebook), pagenum);
506                 return TRUE;
507         }
508
509         if (!page->page_open) {
510                 page->create_widget(page, GTK_WINDOW(prefswindow->window), prefswindow->data);
511                 gtk_container_add(GTK_CONTAINER(prefswindow->notebook), page->widget);
512                 page->page_open = TRUE;
513         }
514
515         i = 0;
516         while (page->path[i + 1] != 0)
517                 i++;
518         labeltext = page->path[i];
519
520         gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
521
522         pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
523                                         page->widget);
524         gtk_notebook_set_page(GTK_NOTEBOOK(prefswindow->notebook), pagenum);
525
526         return TRUE;
527 }
528