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