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