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