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