2008-03-06 [wwp] 3.3.1cvs14
[claws.git] / src / gtk / prefswindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 *vbox;
52         GtkWidget *paned;
53         GtkWidget *scrolledwindow1;
54         GtkWidget *scrolledwindow2;
55         GtkWidget *tree_view;
56         GtkWidget *table2;
57         GtkWidget *pagelabel;
58         GtkWidget *labelframe;
59         GtkWidget *vbox2;
60         GtkWidget *notebook;
61         GtkWidget *confirm_area;
62         GtkWidget *ok_btn;
63         GtkWidget *cancel_btn;
64         GtkWidget *apply_btn;
65         gint *save_width;
66         gint *save_height;
67         PrefsCloseCallbackFunc open_cb;
68         PrefsCloseCallbackFunc close_cb;
69
70         GtkWidget *empty_page;
71
72         gpointer         data;
73         GSList          *prefs_pages;
74         GtkDestroyNotify func;
75 };
76
77 struct _PrefsTreeNode
78 {
79         PrefsPage *page;
80         gfloat     treeweight; /* GTK2: not used */
81 };
82
83 static void prefs_size_allocate_cb(GtkWidget *widget,
84                                                          GtkAllocation *allocation, gpointer *user_data);
85 static GtkTreeStore *prefswindow_create_data_store      (void);
86 static GtkWidget *prefswindow_tree_view_create          (PrefsWindow* prefswindow);
87 static void prefs_filtering_create_tree_view_columns    (GtkWidget *tree_view);
88 static gboolean prefswindow_row_selected                (GtkTreeSelection *selector,
89                                                          GtkTreeModel *model, 
90                                                          GtkTreePath *path,
91                                                          gboolean currently_selected,
92                                                          gpointer data);
93
94 static void save_all_pages(GSList *prefs_pages)
95 {
96         GSList *cur;
97
98         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
99                 PrefsPage *page = (PrefsPage *) cur->data;
100
101                 if (page->page_open) {
102                         page->save_page(page);
103                 }
104         }
105 }
106
107 static gboolean query_can_close_all_pages(GSList *prefs_pages)
108 {
109         GSList *cur;
110
111         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
112                 PrefsPage *page = (PrefsPage *) cur->data;
113
114                 if (page->can_close)
115                         if (!page->can_close(page))
116                                 return FALSE;
117         }
118         return TRUE;
119 }
120
121 static void close_all_pages(GSList *prefs_pages)
122 {
123         GSList *cur;
124
125         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
126                 PrefsPage *page = (PrefsPage *) cur->data;
127
128                 if (page->page_open) {
129                         page->destroy_widget(page);
130                         page->page_open = FALSE;
131                 }
132         }       
133 }
134
135 #ifdef MAEMO
136 static void prefs_show_sections(PrefsWindow *prefswindow)
137 {
138         gint max;
139         GtkWidget *paned = prefswindow->paned;
140         
141         g_object_get (G_OBJECT(paned),
142                         "max-position",
143                         &max, NULL);
144
145         gtk_widget_show(gtk_paned_get_child1(GTK_PANED(paned)));
146         gtk_widget_hide(gtk_paned_get_child2(GTK_PANED(paned)));
147         gtk_paned_set_position(GTK_PANED(paned), max);
148 }
149
150 static void prefs_show_page(PrefsWindow *prefswindow)
151 {
152         gint min;
153         GtkWidget *paned = prefswindow->paned;
154         
155         g_object_get (G_OBJECT(paned),
156                         "min-position",
157                         &min, NULL);
158
159         gtk_widget_hide(gtk_paned_get_child1(GTK_PANED(paned)));
160         gtk_widget_show(gtk_paned_get_child2(GTK_PANED(paned)));
161         gtk_paned_set_position(GTK_PANED(paned), min);
162 }
163 #endif
164
165 static void apply_button_clicked(GtkButton *button, gpointer user_data)
166 {
167         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
168
169         save_all_pages(prefswindow->prefs_pages);
170 #ifdef MAEMO
171         prefs_show_sections(prefswindow);
172 #endif
173 }
174
175 static void close_prefs_window(PrefsWindow *prefswindow)
176 {
177         debug_print("prefs window closed\n");
178
179         close_all_pages(prefswindow->prefs_pages);
180
181         if (prefswindow->close_cb)
182                 prefswindow->close_cb(GTK_WINDOW(prefswindow->window));
183
184         gtk_widget_destroy(prefswindow->window);
185         g_slist_free(prefswindow->prefs_pages);
186         if(prefswindow->func != NULL)
187                 prefswindow->func(prefswindow->data);
188         g_free(prefswindow);
189 }
190
191 static void ok_button_clicked(GtkButton *button, gpointer user_data)
192 {
193         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
194
195         if (query_can_close_all_pages(prefswindow->prefs_pages)) {
196                 save_all_pages(prefswindow->prefs_pages);
197                 close_prefs_window(prefswindow);
198         }               
199 }
200 #ifndef MAEMO
201 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
202 {
203         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
204
205         close_prefs_window(prefswindow);
206 }
207 #endif
208 static gboolean window_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data)
209 {
210         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
211
212 #ifdef MAEMO
213         save_all_pages(prefswindow->prefs_pages);
214 #endif
215         close_prefs_window(prefswindow);
216
217         return FALSE;
218 }
219 #ifndef MAEMO
220 static gboolean prefswindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
221                                     PrefsWindow *data)
222 {
223         GtkWidget *focused_child;
224
225         if (event) {
226                 switch (event->keyval) {
227                         case GDK_Escape :
228                                 cancel_button_clicked(NULL, data);
229                                 break;
230                         case GDK_Return : 
231                         case GDK_KP_Enter :
232                                 focused_child = gtkut_get_focused_child
233                                         (GTK_CONTAINER(data->notebook));
234                                 /* Press ok, if the focused child is not a text view
235                                  * and text (anything that accepts return) (can pass
236                                  * NULL to any of the GTK_xxx() casts) */
237                                 if (!GTK_IS_TEXT_VIEW(focused_child))
238                                         ok_button_clicked(NULL, data);
239                                 break;
240                         default:
241                                 break;
242                 }
243         }
244         return FALSE;
245 }
246 #endif
247 typedef struct FindNodeByName {
248         const gchar *name;
249         gboolean     found;
250         GtkTreeIter  node;
251 } FindNodeByName;
252
253 static gboolean find_node_by_name(GtkTreeModel *model, GtkTreePath *path,
254                                   GtkTreeIter *iter, FindNodeByName *data)
255 {
256         gchar *name;
257         gboolean result = FALSE;
258
259         gtk_tree_model_get(model, iter, PREFS_PAGE_TITLE, &name, -1);
260         if (name) {
261                 result = strcmp(name, data->name) == 0;
262                 if (result) {
263                         data->found = TRUE;
264                         data->node  = *iter;
265                 }                       
266                 g_free(name);
267         }
268         
269         return result;
270 }
271
272 static gint prefswindow_tree_sort_by_weight(GtkTreeModel *model, 
273                                             GtkTreeIter  *a,
274                                             GtkTreeIter  *b,
275                                             gpointer      context)
276 {
277         gfloat f1, f2;
278         gint i1, i2;
279
280         /* From observation sorting should keep in account the original
281          * order in the prefs_pages list. I.e. if equal weight, prefer 
282          * the index in the pages list */ 
283         gtk_tree_model_get(model, a, 
284                            PREFS_PAGE_INDEX,  &i1,
285                            PREFS_PAGE_WEIGHT, &f1, -1);
286         gtk_tree_model_get(model, b, 
287                            PREFS_PAGE_INDEX,  &i2,
288                            PREFS_PAGE_WEIGHT, &f2, -1);
289
290         return f1 < f2 ? -1 : (f1 > f2 ?  1 : 
291               (i1 < i2 ?  1 : (i1 > i2 ? -1 : 0)));
292 }
293                                   
294 static void prefswindow_build_all_pages(PrefsWindow *prefswindow, GSList *prefs_pages)
295 {
296         GSList *cur;
297
298         prefs_pages = g_slist_reverse(prefs_pages);
299         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
300                 PrefsPage *page = (PrefsPage *) cur->data;
301
302                 if (!page->page_open) {
303                         page->create_widget(page, GTK_WINDOW(prefswindow->window), prefswindow->data);
304                         gtk_container_add(GTK_CONTAINER(prefswindow->notebook), page->widget);
305                         page->page_open = TRUE;
306                 }
307         }
308         prefs_pages = g_slist_reverse(prefs_pages);
309 }
310
311 static void prefswindow_build_tree(GtkWidget *tree_view, GSList *prefs_pages,
312                                                                         PrefsWindow *prefswindow,
313                                                                         gboolean preload_pages,
314                                                                         gboolean activate_child)
315 {
316         GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model
317                         (GTK_TREE_VIEW(tree_view)));
318         GSList *cur;
319         gint index; /* index in pages list */
320 #ifndef MAEMO
321         GtkTreeSelection *selection;
322         GtkTreeIter iter;
323 #endif
324
325         for (cur = prefs_pages, index = 0; cur != NULL; cur = g_slist_next(cur), index++) {
326                 PrefsPage *page = (PrefsPage *)cur->data;
327                 FindNodeByName find_name;
328                 GtkTreeIter node, child;
329                 PrefsTreeNode *prefs_node;
330                 int i;
331
332                 /* each page tree component string */
333                 for (i = 0; page->path[i] != NULL; i++) {
334                         find_name.found = FALSE;
335                         find_name.name  = page->path[i];
336                         
337                         /* find node to attach to 
338                          * FIXME: we search the entire tree, so this is suboptimal... */
339                         gtk_tree_model_foreach(GTK_TREE_MODEL(store), 
340                                                (GtkTreeModelForeachFunc) find_node_by_name,
341                                                &find_name);
342                         if (find_name.found) {
343                                 node = find_name.node;
344                                 gtk_tree_model_get(GTK_TREE_MODEL(store), &node,
345                                                    PREFS_PAGE_DATA, &prefs_node,
346                                                    -1);
347                         } else {
348                                 GAuto *autoptr; 
349                         
350                                 /* create a new top level */
351                                 gtk_tree_store_append(store, &child, i == 0 ? NULL : &node);
352                                 prefs_node = g_new0(PrefsTreeNode, 1);
353                                 autoptr = g_auto_pointer_new(prefs_node);
354                                 gtk_tree_store_set(store, &child,
355                                                    PREFS_PAGE_TITLE, page->path[i],
356                                                    PREFS_PAGE_DATA,  prefs_node,
357                                                    PREFS_PAGE_DATA_AUTO_FREE, autoptr,
358                                                    PREFS_PAGE_INDEX, index,
359                                                    PREFS_PAGE_WEIGHT, 0.0f,
360                                                    -1);
361                                 g_auto_pointer_free(autoptr);
362                                 node = child;
363                         }
364                 }
365
366                 /* right now we have a node and its prefs_node */
367                 prefs_node->page = page;
368
369                 /* parents "inherit" the max weight of the children */
370                 do {
371                         gfloat f;
372                         
373                         gtk_tree_model_get(GTK_TREE_MODEL(store), &node, 
374                                            PREFS_PAGE_WEIGHT, &f,
375                                            -1);
376                         if (page->weight > f) {
377                                 f = page->weight;
378                                 gtk_tree_store_set(store, &node,
379                                                    PREFS_PAGE_WEIGHT, f,
380                                                    -1);
381                         }       
382                         child = node;   
383                 } while (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store),      
384                                                     &node, &child));
385         }
386
387         gtk_tree_view_expand_all(GTK_TREE_VIEW(tree_view));
388
389         /* set sort func & sort */
390         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store),
391                                         PREFS_PAGE_WEIGHT,
392                                         prefswindow_tree_sort_by_weight,
393                                         NULL, NULL);
394         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
395                                              PREFS_PAGE_WEIGHT,
396                                              GTK_SORT_DESCENDING);
397
398         if (preload_pages)
399                 prefswindow_build_all_pages(prefswindow, prefs_pages);
400
401         /* select first one or its first child if necessary */
402 #ifndef MAEMO
403         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
404         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
405                 if (activate_child && gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter)) {
406                         GtkTreeIter parent = iter;
407                         if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, &parent))
408                                 iter = parent;
409                 }
410                 gtk_tree_selection_select_iter(selection, &iter);
411         }
412 #endif
413 }
414
415 void prefswindow_open_full(const gchar *title, GSList *prefs_pages,
416                                                          gpointer data, GtkDestroyNotify func,
417                                                          gint *save_width, gint *save_height,
418                                                          gboolean preload_pages, gboolean activate_child,
419                                                          PrefsOpenCallbackFunc open_cb,
420                                                          PrefsCloseCallbackFunc close_cb)
421 {
422         PrefsWindow *prefswindow;
423         gint x = gdk_screen_width();
424         gint y = gdk_screen_height();
425         static GdkGeometry geometry;
426         GtkAdjustment *adj;
427         GtkWidget *tmp;
428
429         prefswindow = g_new0(PrefsWindow, 1);
430
431         prefswindow->data = data;
432         prefswindow->func = func;
433         prefswindow->prefs_pages = g_slist_copy(prefs_pages);
434         prefswindow->save_width = save_width;
435         prefswindow->save_height = save_height;
436         prefswindow->open_cb = open_cb;
437         prefswindow->close_cb = close_cb;
438
439         prefswindow->window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefswindow");
440         gtk_window_set_title(GTK_WINDOW(prefswindow->window), title);
441
442         gtk_window_set_position (GTK_WINDOW(prefswindow->window), GTK_WIN_POS_CENTER);
443         gtk_window_set_modal (GTK_WINDOW (prefswindow->window), TRUE);
444         gtk_window_set_resizable (GTK_WINDOW(prefswindow->window), TRUE);
445         gtk_container_set_border_width(GTK_CONTAINER(prefswindow->window), 4);
446
447         prefswindow->vbox = gtk_vbox_new(FALSE, 6);
448         gtk_widget_show(prefswindow->vbox);
449         
450         prefswindow->paned = gtk_hpaned_new();
451         gtk_widget_show(prefswindow->paned);
452
453         gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->vbox);
454
455         gtk_box_pack_start(GTK_BOX(prefswindow->vbox), prefswindow->paned, TRUE, TRUE, 0);
456
457         prefswindow->scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
458         gtk_widget_show(prefswindow->scrolledwindow1);
459         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1),
460                         GTK_SHADOW_IN);
461         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1),
462                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
463                         
464         gtk_paned_add1(GTK_PANED(prefswindow->paned), prefswindow->scrolledwindow1);
465
466         prefswindow->tree_view = prefswindow_tree_view_create(prefswindow);
467         gtk_widget_show(prefswindow->tree_view);
468         gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), 
469                           prefswindow->tree_view);
470
471         prefswindow->vbox2 = gtk_vbox_new(FALSE, 2);
472         gtk_widget_show(prefswindow->vbox2);
473
474         gtk_paned_add2(GTK_PANED(prefswindow->paned), prefswindow->vbox2);
475
476         prefswindow->table2 = gtk_table_new(1, 2, FALSE);
477         gtk_widget_show(prefswindow->table2);
478         gtk_container_add(GTK_CONTAINER(prefswindow->vbox2), prefswindow->table2);
479
480         prefswindow->labelframe = gtk_frame_new(NULL);
481         gtk_widget_show(prefswindow->labelframe);
482         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->labelframe), GTK_SHADOW_OUT);
483         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->labelframe,
484                         0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 1, 1);
485
486         prefswindow->pagelabel = gtk_label_new("");
487         gtk_widget_show(prefswindow->pagelabel);
488         gtk_label_set_justify(GTK_LABEL(prefswindow->pagelabel), GTK_JUSTIFY_LEFT);
489         gtk_misc_set_alignment(GTK_MISC(prefswindow->pagelabel), 0, 0.0);
490         gtk_container_add(GTK_CONTAINER(prefswindow->labelframe), prefswindow->pagelabel);
491
492         prefswindow->scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
493         gtk_widget_show(prefswindow->scrolledwindow2);
494         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2),
495                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
496
497         prefswindow->notebook = gtk_notebook_new();
498         gtk_widget_show(prefswindow->notebook);
499         gtk_notebook_set_scrollable(GTK_NOTEBOOK(prefswindow->notebook), TRUE);
500         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
501         gtk_notebook_set_show_border(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
502         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2),
503                                         prefswindow->notebook);
504         tmp = gtk_bin_get_child(GTK_BIN(prefswindow->scrolledwindow2));
505         gtk_viewport_set_shadow_type(GTK_VIEWPORT(tmp), GTK_SHADOW_NONE);
506         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->scrolledwindow2,
507                         0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 4);
508
509         prefswindow->empty_page = gtk_label_new("");
510         gtk_widget_show(prefswindow->empty_page);
511         gtk_container_add(GTK_CONTAINER(prefswindow->notebook), prefswindow->empty_page);
512
513         prefswindow_build_tree(prefswindow->tree_view, prefs_pages, prefswindow,
514                                                         preload_pages, activate_child);
515
516         if (open_cb)
517                 open_cb(GTK_WINDOW(prefswindow->window));
518
519         gtk_widget_grab_focus(prefswindow->tree_view);
520
521 #ifndef MAEMO
522         gtkut_stock_button_set_create(&prefswindow->confirm_area,
523                                       &prefswindow->apply_btn,  GTK_STOCK_APPLY,
524                                       &prefswindow->cancel_btn, GTK_STOCK_CANCEL,
525                                       &prefswindow->ok_btn,     GTK_STOCK_OK);
526 #else
527         gtkut_stock_button_set_create(&prefswindow->confirm_area,
528                                       &prefswindow->apply_btn,  GTK_STOCK_APPLY,
529                                       &prefswindow->ok_btn,     GTK_STOCK_CLOSE,
530                                       NULL,                     NULL);
531 #endif
532         gtk_widget_show_all(prefswindow->confirm_area);
533         gtk_widget_show(prefswindow->vbox);
534         gtk_widget_show(prefswindow->scrolledwindow1);
535         gtk_widget_show(prefswindow->scrolledwindow2);
536
537         gtk_box_pack_start(GTK_BOX(prefswindow->vbox), prefswindow->confirm_area, FALSE, FALSE, 0);
538
539 #ifndef MAEMO
540         g_signal_connect(G_OBJECT(prefswindow->ok_btn), "clicked", 
541                          G_CALLBACK(ok_button_clicked), prefswindow);
542         g_signal_connect(G_OBJECT(prefswindow->cancel_btn), "clicked", 
543                          G_CALLBACK(cancel_button_clicked), prefswindow);
544         g_signal_connect(G_OBJECT(prefswindow->apply_btn), "clicked", 
545                          G_CALLBACK(apply_button_clicked), prefswindow);
546 #else
547         g_signal_connect(G_OBJECT(prefswindow->ok_btn), "clicked", 
548                          G_CALLBACK(ok_button_clicked), prefswindow);
549         g_signal_connect(G_OBJECT(prefswindow->apply_btn), "clicked", 
550                          G_CALLBACK(apply_button_clicked), prefswindow);
551 #endif
552
553         g_signal_connect(G_OBJECT(prefswindow->window), "delete_event", 
554                          G_CALLBACK(window_closed), prefswindow);
555
556 #ifdef MAEMO
557         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(prefswindow->window));
558 #else
559         g_signal_connect(G_OBJECT(prefswindow->window), "key_press_event",
560                            G_CALLBACK(prefswindow_key_pressed), &(prefswindow->window));
561 #endif
562
563         /* connect to callback only if we hhave non-NULL pointers to store size to */
564         if (prefswindow->save_width && prefswindow->save_height) {
565                 g_signal_connect(G_OBJECT(prefswindow->window), "size_allocate",
566                                  G_CALLBACK(prefs_size_allocate_cb), prefswindow);
567         }
568
569         if (!geometry.min_height) {
570                 
571                 if (x < 800 && y < 600) {
572                         geometry.min_width = 600;
573                         geometry.min_height = 440;
574                 } else {
575                         geometry.min_width = 700;
576                         geometry.min_height = 550;
577                 }
578         }
579         gtk_window_set_geometry_hints(GTK_WINDOW(prefswindow->window), NULL, &geometry,
580                                       GDK_HINT_MIN_SIZE);
581         if (prefswindow->save_width && prefswindow->save_height) {
582                 gtk_widget_set_size_request(prefswindow->window, *(prefswindow->save_width),
583                                             *(prefswindow->save_height));
584         }
585
586 #ifdef MAEMO
587         prefs_show_sections(prefswindow);
588 #endif
589         gtk_widget_show(prefswindow->window);
590 #ifdef MAEMO
591         maemo_window_full_screen_if_needed(GTK_WINDOW(prefswindow->window));
592 #endif
593         adj = gtk_scrolled_window_get_vadjustment(
594                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1));
595         gtk_adjustment_set_value(adj, adj->lower);
596         gtk_adjustment_changed(adj);
597         adj = gtk_scrolled_window_get_vadjustment(
598                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2));
599         gtk_adjustment_set_value(adj, adj->lower);
600         gtk_adjustment_changed(adj);
601         adj = gtk_scrolled_window_get_hadjustment(
602                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2));
603         gtk_adjustment_set_value(adj, adj->lower);
604         gtk_adjustment_changed(adj);
605 }
606
607 void prefswindow_open(const gchar *title, GSList *prefs_pages, gpointer data,
608                                          gint *save_width, gint *save_height,
609                                          PrefsOpenCallbackFunc open_cb,
610                                          PrefsCloseCallbackFunc close_cb)
611 {
612         prefswindow_open_full(title, prefs_pages, data, NULL, save_width, save_height,
613                                                   FALSE, FALSE, open_cb, close_cb);
614 }
615
616 /*!
617  *\brief        Save Gtk object size to prefs dataset
618  */
619 static void prefs_size_allocate_cb(GtkWidget *widget,
620                                          GtkAllocation *allocation, gpointer *user_data)
621 {
622         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
623
624         g_return_if_fail(allocation != NULL);
625
626         /* don't try to save size to NULL pointers */
627         if (prefswindow && prefswindow->save_width && prefswindow->save_height) {
628                 *(prefswindow->save_width) = allocation->width;
629                 *(prefswindow->save_height) = allocation->height;
630         }
631 }
632
633 static GtkTreeStore *prefswindow_create_data_store(void)
634 {
635         return gtk_tree_store_new(N_PREFS_PAGE_COLUMNS,
636                                   G_TYPE_STRING,
637                                   G_TYPE_POINTER,
638                                   G_TYPE_AUTO_POINTER,
639                                   G_TYPE_FLOAT,
640                                   G_TYPE_INT,
641                                   -1);
642 }
643
644 static GtkWidget *prefswindow_tree_view_create(PrefsWindow *prefswindow)
645 {
646         GtkTreeView *tree_view;
647         GtkTreeSelection *selector;
648         GtkTreeModel *model;
649
650         model = GTK_TREE_MODEL(prefswindow_create_data_store());
651         tree_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
652         g_object_unref(model);
653         gtk_tree_view_set_rules_hint(tree_view, prefs_common.use_stripes_everywhere);
654         
655         selector = gtk_tree_view_get_selection(tree_view);
656         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
657         gtk_tree_selection_set_select_function(selector, prefswindow_row_selected,
658                                                prefswindow, NULL);
659
660         /* create the columns */
661         prefs_filtering_create_tree_view_columns(GTK_WIDGET(tree_view));
662
663         return GTK_WIDGET(tree_view);
664 }
665
666 static void prefs_filtering_create_tree_view_columns(GtkWidget *tree_view)
667 {
668         GtkTreeViewColumn *column;
669         GtkCellRenderer *renderer;
670
671         renderer = gtk_cell_renderer_text_new();
672         column = gtk_tree_view_column_new_with_attributes
673                 (_("Page Index"),
674                  renderer,
675                  "text", PREFS_PAGE_TITLE,
676                  NULL);
677         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);          
678 }
679
680 static gboolean prefswindow_row_selected(GtkTreeSelection *selector,
681                                          GtkTreeModel *model, 
682                                          GtkTreePath *path,
683                                          gboolean currently_selected,
684                                          gpointer data)
685 {
686         PrefsTreeNode *prefsnode;
687         PrefsPage *page;
688         PrefsWindow *prefswindow = (PrefsWindow *) data;
689         gchar *labeltext;
690         gint pagenum, i;
691         GtkTreeIter iter;
692         GtkAdjustment *adj;
693
694 #ifndef MAEMO
695         if (currently_selected) 
696                 return TRUE;
697 #endif
698         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
699                 return TRUE;
700
701         gtk_tree_model_get(model, &iter, PREFS_PAGE_DATA, &prefsnode, -1);
702         page = prefsnode->page;
703
704         debug_print("%f\n", prefsnode->treeweight);
705
706         if (page == NULL) {
707                 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), "");
708                 pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
709                                                 prefswindow->empty_page);
710                 gtk_notebook_set_current_page(GTK_NOTEBOOK(prefswindow->notebook),
711                                               pagenum);
712                 return TRUE;
713         }
714
715         if (!page->page_open) {
716                 page->create_widget(page, GTK_WINDOW(prefswindow->window), prefswindow->data);
717                 gtk_container_add(GTK_CONTAINER(prefswindow->notebook), page->widget);
718                 page->page_open = TRUE;
719         }
720
721         i = 0;
722         while (page->path[i + 1] != 0)
723                 i++;
724         labeltext = page->path[i];
725
726         gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
727
728         pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
729                                         page->widget);
730         gtk_notebook_set_current_page(GTK_NOTEBOOK(prefswindow->notebook),
731                                       pagenum);
732
733         adj = gtk_scrolled_window_get_vadjustment(
734                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2));
735         gtk_adjustment_set_value(adj, adj->lower);
736         gtk_adjustment_changed(adj);
737         adj = gtk_scrolled_window_get_hadjustment(
738                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow2));
739         gtk_adjustment_set_value(adj, adj->lower);
740         gtk_adjustment_changed(adj);
741
742 #ifdef MAEMO
743         prefs_show_page(prefswindow);
744 #endif
745         return TRUE;
746 }
747