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