2012-07-28 [ticho] 3.8.1cvs23
[claws.git] / src / gtk / prefswindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28 #include <gtk/gtk.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 #include "gtk/manage_window.h"
36
37 enum { 
38         PREFS_PAGE_TITLE,               /* page title */
39         PREFS_PAGE_DATA,                /* PrefsTreeNode data */        
40         PREFS_PAGE_DATA_AUTO_FREE,      /* auto free for PREFS_PAGE_DATA */
41         PREFS_PAGE_WEIGHT,              /* weight */
42         PREFS_PAGE_INDEX,               /* index in original page list */
43         N_PREFS_PAGE_COLUMNS
44 };
45
46 typedef struct _PrefsWindow PrefsWindow;
47 typedef struct _PrefsTreeNode PrefsTreeNode;
48
49 struct _PrefsWindow
50 {
51         GtkWidget *window;
52         GtkWidget *vbox;
53         GtkWidget *paned;
54         GtkWidget *scrolledwindow1;
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         GDestroyNotify 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 GENERIC_UMPC
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 GENERIC_UMPC
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 GENERIC_UMPC
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_KEY_Escape :
228                                 cancel_button_clicked(NULL, data);
229                                 break;
230                         case GDK_KEY_Return : 
231                         case GDK_KEY_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_page(PrefsWindow *prefswindow, PrefsPage *page)
295 {
296         GtkWidget *scrolledwin, *tmp;
297
298         if (!page->page_open) {
299                 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
300                 gtk_widget_show(scrolledwin);
301                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
302                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
303                 
304                 page->create_widget(page, GTK_WINDOW(prefswindow->window), prefswindow->data);
305                 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwin),
306                                         page->widget);
307
308                 gtk_container_add(GTK_CONTAINER(prefswindow->notebook), scrolledwin);
309                 tmp = gtk_bin_get_child(GTK_BIN(scrolledwin));
310
311                 gtk_widget_realize(tmp);
312                 gtk_widget_realize(page->widget);
313                 
314                 page->widget    = scrolledwin;
315                 page->page_open = TRUE;
316         }
317 }
318
319 static void prefswindow_build_all_pages(PrefsWindow *prefswindow, GSList *prefs_pages)
320 {
321         GSList *cur;
322
323         prefs_pages = g_slist_reverse(prefs_pages);
324         for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
325                 PrefsPage *page = (PrefsPage *) cur->data;
326
327                 prefswindow_build_page(prefswindow, page);
328         }
329         prefs_pages = g_slist_reverse(prefs_pages);
330 }
331
332 static void prefswindow_build_tree(GtkWidget *tree_view, GSList *prefs_pages,
333                                                                         PrefsWindow *prefswindow,
334                                                                         gboolean preload_pages)
335 {
336         GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model
337                         (GTK_TREE_VIEW(tree_view)));
338         GSList *cur;
339         gint index; /* index in pages list */
340 #ifndef GENERIC_UMPC
341         GtkTreeSelection *selection;
342         GtkTreeIter iter;
343 #endif
344
345         for (cur = prefs_pages, index = 0; cur != NULL; cur = g_slist_next(cur), index++) {
346                 PrefsPage *page = (PrefsPage *)cur->data;
347                 FindNodeByName find_name;
348                 GtkTreeIter node, child;
349                 PrefsTreeNode *prefs_node;
350                 int i;
351
352                 /* each page tree component string */
353                 for (i = 0; page->path[i] != NULL; i++) {
354                         find_name.found = FALSE;
355                         find_name.name  = page->path[i];
356                         
357                         /* find node to attach to 
358                          * FIXME: we search the entire tree, so this is suboptimal... */
359                         gtk_tree_model_foreach(GTK_TREE_MODEL(store), 
360                                                (GtkTreeModelForeachFunc) find_node_by_name,
361                                                &find_name);
362                         if (find_name.found && page->path[i] != page->path[i-1]) {
363                                 node = find_name.node;
364                                 gtk_tree_model_get(GTK_TREE_MODEL(store), &node,
365                                                    PREFS_PAGE_DATA, &prefs_node,
366                                                    -1);
367                         } else {
368                                 GAuto *autoptr; 
369                         
370                                 /* create a new top level */
371                                 gtk_tree_store_append(store, &child, i == 0 ? NULL : &node);
372                                 prefs_node = g_new0(PrefsTreeNode, 1);
373                                 autoptr = g_auto_pointer_new(prefs_node);
374                                 gtk_tree_store_set(store, &child,
375                                                    PREFS_PAGE_TITLE, page->path[i],
376                                                    PREFS_PAGE_DATA,  prefs_node,
377                                                    PREFS_PAGE_DATA_AUTO_FREE, autoptr,
378                                                    PREFS_PAGE_INDEX, index,
379                                                    PREFS_PAGE_WEIGHT, 0.0f,
380                                                    -1);
381                                 g_auto_pointer_free(autoptr);
382                                 node = child;
383                         }
384                 }
385
386                 /* right now we have a node and its prefs_node */
387                 prefs_node->page = page;
388
389                 /* parents "inherit" the max weight of the children */
390                 do {
391                         gfloat f;
392                         
393                         gtk_tree_model_get(GTK_TREE_MODEL(store), &node, 
394                                            PREFS_PAGE_WEIGHT, &f,
395                                            -1);
396                         if (page->weight > f) {
397                                 f = page->weight;
398                                 gtk_tree_store_set(store, &node,
399                                                    PREFS_PAGE_WEIGHT, f,
400                                                    -1);
401                         }       
402                         child = node;   
403                 } while (gtk_tree_model_iter_parent(GTK_TREE_MODEL(store),      
404                                                     &node, &child));
405         }
406
407         gtk_tree_view_expand_all(GTK_TREE_VIEW(tree_view));
408
409         /* set sort func & sort */
410         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store),
411                                         PREFS_PAGE_WEIGHT,
412                                         prefswindow_tree_sort_by_weight,
413                                         NULL, NULL);
414         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
415                                              PREFS_PAGE_WEIGHT,
416                                              GTK_SORT_DESCENDING);
417
418         if (preload_pages)
419                 prefswindow_build_all_pages(prefswindow, prefs_pages);
420
421         /* select first one or its first child if necessary */
422 #ifndef GENERIC_UMPC
423         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
424         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
425                 if (gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter)) {
426                         GtkTreeIter parent = iter;
427                         if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, &parent))
428                                 iter = parent;
429                 }
430                 gtk_tree_selection_select_iter(selection, &iter);
431         }
432 #endif
433 }
434
435 void prefswindow_open_full(const gchar *title, GSList *prefs_pages,
436                                                          gpointer data, GDestroyNotify func,
437                                                          gint *save_width, gint *save_height,
438                                                          gboolean preload_pages,
439                                                          PrefsOpenCallbackFunc open_cb,
440                                                          PrefsCloseCallbackFunc close_cb)
441 {
442         PrefsWindow *prefswindow;
443         gint x = gdk_screen_width();
444         gint y = gdk_screen_height();
445         static GdkGeometry geometry;
446         GtkAdjustment *adj;
447
448         prefswindow = g_new0(PrefsWindow, 1);
449
450         prefswindow->data = data;
451         prefswindow->func = func;
452         prefswindow->prefs_pages = g_slist_copy(prefs_pages);
453         prefswindow->save_width = save_width;
454         prefswindow->save_height = save_height;
455         prefswindow->open_cb = open_cb;
456         prefswindow->close_cb = close_cb;
457
458         prefswindow->window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefswindow");
459         gtk_window_set_title(GTK_WINDOW(prefswindow->window), title);
460
461         gtk_window_set_position (GTK_WINDOW(prefswindow->window), GTK_WIN_POS_CENTER);
462         gtk_window_set_modal (GTK_WINDOW (prefswindow->window), TRUE);
463         gtk_window_set_resizable (GTK_WINDOW(prefswindow->window), TRUE);
464         gtk_window_set_transient_for (GTK_WINDOW(prefswindow->window),
465                         GTK_WINDOW(mainwindow_get_mainwindow()->window));
466         gtk_container_set_border_width(GTK_CONTAINER(prefswindow->window), 4);
467
468         prefswindow->vbox = gtk_vbox_new(FALSE, 6);
469         gtk_widget_show(prefswindow->vbox);
470         
471         prefswindow->paned = gtk_hpaned_new();
472         gtk_widget_show(prefswindow->paned);
473
474         gtk_container_add(GTK_CONTAINER(prefswindow->window), prefswindow->vbox);
475
476         gtk_box_pack_start(GTK_BOX(prefswindow->vbox), prefswindow->paned, TRUE, TRUE, 0);
477
478         prefswindow->scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
479         gtk_widget_show(prefswindow->scrolledwindow1);
480         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1),
481                         GTK_SHADOW_IN);
482         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1),
483                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
484                         
485         gtk_paned_add1(GTK_PANED(prefswindow->paned), prefswindow->scrolledwindow1);
486
487         prefswindow->tree_view = prefswindow_tree_view_create(prefswindow);
488         gtk_widget_show(prefswindow->tree_view);
489         gtk_container_add(GTK_CONTAINER(prefswindow->scrolledwindow1), 
490                           prefswindow->tree_view);
491
492         prefswindow->vbox2 = gtk_vbox_new(FALSE, 2);
493         gtk_widget_show(prefswindow->vbox2);
494
495         gtk_paned_add2(GTK_PANED(prefswindow->paned), prefswindow->vbox2);
496
497         prefswindow->table2 = gtk_table_new(1, 2, FALSE);
498         gtk_widget_show(prefswindow->table2);
499         gtk_container_add(GTK_CONTAINER(prefswindow->vbox2), prefswindow->table2);
500
501         prefswindow->labelframe = gtk_frame_new(NULL);
502         gtk_widget_show(prefswindow->labelframe);
503         gtk_frame_set_shadow_type(GTK_FRAME(prefswindow->labelframe), GTK_SHADOW_OUT);
504         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->labelframe,
505                         0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 1, 1);
506
507         prefswindow->pagelabel = gtk_label_new("");
508         gtk_widget_show(prefswindow->pagelabel);
509         gtk_label_set_justify(GTK_LABEL(prefswindow->pagelabel), GTK_JUSTIFY_LEFT);
510         gtk_misc_set_alignment(GTK_MISC(prefswindow->pagelabel), 0, 0.0);
511         gtk_container_add(GTK_CONTAINER(prefswindow->labelframe), prefswindow->pagelabel);
512
513         prefswindow->notebook = gtk_notebook_new();
514         gtk_widget_show(prefswindow->notebook);
515         gtk_notebook_set_scrollable(GTK_NOTEBOOK(prefswindow->notebook), TRUE);
516         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
517         gtk_notebook_set_show_border(GTK_NOTEBOOK(prefswindow->notebook), FALSE);
518
519         gtk_table_attach(GTK_TABLE(prefswindow->table2), prefswindow->notebook,
520                         0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 4);
521
522         prefswindow->empty_page = gtk_label_new("");
523         gtk_widget_show(prefswindow->empty_page);
524         gtk_container_add(GTK_CONTAINER(prefswindow->notebook), prefswindow->empty_page);
525
526         prefswindow_build_tree(prefswindow->tree_view, prefs_pages, prefswindow,
527                                                         preload_pages);
528
529         if (open_cb)
530                 open_cb(GTK_WINDOW(prefswindow->window));
531
532         gtk_widget_grab_focus(prefswindow->tree_view);
533
534 #ifndef GENERIC_UMPC
535         gtkut_stock_button_set_create(&prefswindow->confirm_area,
536                                       &prefswindow->apply_btn,  GTK_STOCK_APPLY,
537                                       &prefswindow->cancel_btn, GTK_STOCK_CANCEL,
538                                       &prefswindow->ok_btn,     GTK_STOCK_OK);
539 #else
540         gtkut_stock_button_set_create(&prefswindow->confirm_area,
541                                       &prefswindow->apply_btn,  GTK_STOCK_APPLY,
542                                       &prefswindow->ok_btn,     GTK_STOCK_CLOSE,
543                                       NULL,                     NULL);
544 #endif
545         gtk_widget_show_all(prefswindow->confirm_area);
546         gtk_widget_show(prefswindow->vbox);
547         gtk_widget_show(prefswindow->scrolledwindow1);
548
549         gtk_box_pack_start(GTK_BOX(prefswindow->vbox), prefswindow->confirm_area, FALSE, FALSE, 0);
550
551 #ifndef GENERIC_UMPC
552         g_signal_connect(G_OBJECT(prefswindow->ok_btn), "clicked", 
553                          G_CALLBACK(ok_button_clicked), prefswindow);
554         g_signal_connect(G_OBJECT(prefswindow->cancel_btn), "clicked", 
555                          G_CALLBACK(cancel_button_clicked), prefswindow);
556         g_signal_connect(G_OBJECT(prefswindow->apply_btn), "clicked", 
557                          G_CALLBACK(apply_button_clicked), prefswindow);
558 #else
559         g_signal_connect(G_OBJECT(prefswindow->ok_btn), "clicked", 
560                          G_CALLBACK(ok_button_clicked), prefswindow);
561         g_signal_connect(G_OBJECT(prefswindow->apply_btn), "clicked", 
562                          G_CALLBACK(apply_button_clicked), prefswindow);
563 #endif
564
565         g_signal_connect(G_OBJECT(prefswindow->window), "delete_event", 
566                          G_CALLBACK(window_closed), prefswindow);
567
568 #ifdef MAEMO
569         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(prefswindow->window));
570 #else
571         g_signal_connect(G_OBJECT(prefswindow->window), "key_press_event",
572                            G_CALLBACK(prefswindow_key_pressed), &(prefswindow->window));
573 #endif
574
575         /* connect to callback only if we have non-NULL pointers to store size to */
576         if (prefswindow->save_width && prefswindow->save_height) {
577                 g_signal_connect(G_OBJECT(prefswindow->window), "size_allocate",
578                                  G_CALLBACK(prefs_size_allocate_cb), prefswindow);
579         }
580
581         MANAGE_WINDOW_SIGNALS_CONNECT(prefswindow->window);
582
583         if (!geometry.min_height) {
584                 
585                 if (x < 800 && y < 600) {
586                         geometry.min_width = 600;
587                         geometry.min_height = 440;
588                 } else {
589                         geometry.min_width = 700;
590                         geometry.min_height = 550;
591                 }
592         }
593         gtk_window_set_geometry_hints(GTK_WINDOW(prefswindow->window), NULL, &geometry,
594                                       GDK_HINT_MIN_SIZE);
595         if (prefswindow->save_width && prefswindow->save_height) {
596                 gtk_widget_set_size_request(prefswindow->window, *(prefswindow->save_width),
597                                             *(prefswindow->save_height));
598         }
599
600 #ifdef GENERIC_UMPC
601         prefs_show_sections(prefswindow);
602 #endif
603         gtk_widget_show(prefswindow->window);
604 #ifdef MAEMO
605         maemo_window_full_screen_if_needed(GTK_WINDOW(prefswindow->window));
606 #endif
607         adj = gtk_scrolled_window_get_vadjustment(
608                         GTK_SCROLLED_WINDOW(prefswindow->scrolledwindow1));
609         gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
610         gtk_adjustment_changed(adj);
611 }
612
613 void prefswindow_open(const gchar *title, GSList *prefs_pages, gpointer data,
614                                          gint *save_width, gint *save_height,
615                                          PrefsOpenCallbackFunc open_cb,
616                                          PrefsCloseCallbackFunc close_cb)
617 {
618         prefswindow_open_full(title, prefs_pages, data, NULL, save_width, save_height,
619                                                   FALSE, open_cb, close_cb);
620 }
621
622 /*!
623  *\brief        Save Gtk object size to prefs dataset
624  */
625 static void prefs_size_allocate_cb(GtkWidget *widget,
626                                          GtkAllocation *allocation, gpointer *user_data)
627 {
628         PrefsWindow *prefswindow = (PrefsWindow *) user_data;
629
630         cm_return_if_fail(allocation != NULL);
631
632         /* don't try to save size to NULL pointers */
633         if (prefswindow && prefswindow->save_width && prefswindow->save_height) {
634                 *(prefswindow->save_width) = allocation->width;
635                 *(prefswindow->save_height) = allocation->height;
636         }
637 }
638
639 static GtkTreeStore *prefswindow_create_data_store(void)
640 {
641         return gtk_tree_store_new(N_PREFS_PAGE_COLUMNS,
642                                   G_TYPE_STRING,
643                                   G_TYPE_POINTER,
644                                   G_TYPE_AUTO_POINTER,
645                                   G_TYPE_FLOAT,
646                                   G_TYPE_INT,
647                                   -1);
648 }
649
650 static GtkWidget *prefswindow_tree_view_create(PrefsWindow *prefswindow)
651 {
652         GtkTreeView *tree_view;
653         GtkTreeSelection *selector;
654         GtkTreeModel *model;
655
656         model = GTK_TREE_MODEL(prefswindow_create_data_store());
657         tree_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
658         g_object_unref(model);
659         gtk_tree_view_set_rules_hint(tree_view, prefs_common.use_stripes_everywhere);
660         
661         selector = gtk_tree_view_get_selection(tree_view);
662         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
663         gtk_tree_selection_set_select_function(selector, prefswindow_row_selected,
664                                                prefswindow, NULL);
665
666         /* create the columns */
667         prefs_filtering_create_tree_view_columns(GTK_WIDGET(tree_view));
668
669         return GTK_WIDGET(tree_view);
670 }
671
672 static void prefs_filtering_create_tree_view_columns(GtkWidget *tree_view)
673 {
674         GtkTreeViewColumn *column;
675         GtkCellRenderer *renderer;
676
677         renderer = gtk_cell_renderer_text_new();
678         column = gtk_tree_view_column_new_with_attributes
679                 (_("Page Index"),
680                  renderer,
681                  "text", PREFS_PAGE_TITLE,
682                  NULL);
683         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);          
684 }
685
686 static gboolean prefswindow_row_selected(GtkTreeSelection *selector,
687                                          GtkTreeModel *model, 
688                                          GtkTreePath *path,
689                                          gboolean currently_selected,
690                                          gpointer data)
691 {
692         PrefsTreeNode *prefsnode;
693         PrefsPage *page;
694         PrefsWindow *prefswindow = (PrefsWindow *) data;
695         gfloat lower;
696         gchar *labeltext;
697         gint pagenum, i;
698         GtkTreeIter iter;
699         GtkAdjustment *adj;
700
701         if (gtk_tree_path_get_depth(path) < 2)
702                 return FALSE;
703 #ifndef GENERIC_UMPC
704         if (currently_selected) 
705                 return TRUE;
706 #endif
707         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
708                 return TRUE;
709
710         gtk_tree_model_get(model, &iter, PREFS_PAGE_DATA, &prefsnode, -1);
711         page = prefsnode->page;
712
713         debug_print("%f\n", prefsnode->treeweight);
714
715         if (page == NULL) {
716                 gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), "");
717                 pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
718                                                 prefswindow->empty_page);
719                 gtk_notebook_set_current_page(GTK_NOTEBOOK(prefswindow->notebook),
720                                               pagenum);
721                 return TRUE;
722         }
723
724         prefswindow_build_page(prefswindow, page);
725
726         i = 0;
727         while (page->path[i + 1] != 0)
728                 i++;
729         labeltext = page->path[i];
730
731         gtk_label_set_text(GTK_LABEL(prefswindow->pagelabel), labeltext);
732
733         pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(prefswindow->notebook),
734                                         page->widget);
735         gtk_notebook_set_current_page(GTK_NOTEBOOK(prefswindow->notebook),
736                                       pagenum);
737
738         adj = gtk_scrolled_window_get_vadjustment(
739                         GTK_SCROLLED_WINDOW(page->widget));
740         lower = gtk_adjustment_get_lower(adj);
741         gtk_adjustment_set_value(adj, lower);
742         gtk_adjustment_changed(adj);
743         adj = gtk_scrolled_window_get_hadjustment(
744                         GTK_SCROLLED_WINDOW(page->widget));
745         gtk_adjustment_set_value(adj, lower);
746         gtk_adjustment_changed(adj);
747
748 #ifdef GENERIC_UMPC
749         prefs_show_page(prefswindow);
750 #endif
751         return TRUE;
752 }
753