save/restore task sort col/order
[ptask.git] / src / ui.c
1 /*
2  * Copyright (C) 2012-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <log.h>
21 #include <ui.h>
22
23 static GtkTreeView *w_treeview;
24
25 static void save_settings(GtkWindow *window, GSettings *settings)
26 {
27         int w, h, x, y, sort_col_id;
28         GtkTreeSortable *model;
29         GtkSortType sort_order;
30
31         model = GTK_TREE_SORTABLE(gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview)));
32         gtk_tree_sortable_get_sort_column_id(model, 
33                                              &sort_col_id,
34                                              &sort_order);
35         log_debug("save_settings(): sort_col_id=%d", sort_col_id);
36         log_debug("save_settings(): sort_col_order=%d", sort_order);
37
38         g_settings_set_int(settings, "tasks-sort-col", sort_col_id);
39         g_settings_set_int(settings, "tasks-sort-order", sort_order);
40
41         gtk_window_get_size(window, &w, &h);
42         gtk_window_get_position(window, &x, &y);
43
44         log_debug("save_settings(): x=%d, y=%d, w=%d, h=%d", x, y, w, h);
45
46         g_settings_set_int(settings, "window-width", w);
47         g_settings_set_int(settings, "window-height", h);
48         g_settings_set_int(settings, "window-x", x);
49         g_settings_set_int(settings, "window-y", y);
50
51         g_settings_sync();
52 }
53
54 static gboolean delete_event_cbk(GtkWidget *w, GdkEvent *evt, gpointer data)
55 {
56         log_debug("delete_event_cbk");
57
58         save_settings(GTK_WINDOW(w), (GSettings *)data);
59         gtk_widget_destroy(w);
60         gtk_main_quit();
61
62         return TRUE;
63 }
64
65 GtkWindow *create_window(GtkBuilder *builder, GSettings *settings)
66 {
67         GtkWindow *window;
68         int x, y, h, w, sort_col_id;
69         GtkSortType sort_order;
70         GtkTreeSortable *model;
71
72         window = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
73
74         w = g_settings_get_int(settings, "window-width");
75         h = g_settings_get_int(settings, "window-height");
76         gtk_window_set_default_size(window, w, h);
77
78         x = g_settings_get_int(settings, "window-x");
79         y = g_settings_get_int(settings, "window-y");
80         gtk_window_move(window, x, y);
81
82         g_signal_connect(window, "delete_event",
83                          G_CALLBACK(delete_event_cbk), settings);
84
85         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
86
87         sort_col_id = g_settings_get_int(settings, "tasks-sort-col");
88         sort_order = g_settings_get_int(settings, "tasks-sort-order");
89         model = GTK_TREE_SORTABLE(gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview)));
90         gtk_tree_sortable_set_sort_column_id(model, sort_col_id, sort_order);
91
92         return window;
93 }
94