save/restore window position and size
[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 void save_settings(GtkWindow *window, GSettings *settings)
24 {
25         int w, h, x, y;
26
27         gtk_window_get_size(window, &w, &h);
28         gtk_window_get_position(window, &x, &y);
29
30         log_debug("save_settings(): x=%d, y=%d, w=%d, h=%d", x, y, w, h);
31
32         g_settings_set_int(settings, "window-width", w);
33         g_settings_set_int(settings, "window-height", h);
34         g_settings_set_int(settings, "window-x", x);
35         g_settings_set_int(settings, "window-y", y);
36
37         g_settings_sync();
38 }
39
40 static gboolean delete_event_cbk(GtkWidget *w, GdkEvent *evt, gpointer data)
41 {
42         log_debug("delete_event_cbk");
43
44         save_settings(GTK_WINDOW(w), (GSettings *)data);
45         gtk_widget_destroy(w);
46         gtk_main_quit();
47
48         return TRUE;
49 }
50
51 GtkWindow *create_window(GtkBuilder *builder, GSettings *settings)
52 {
53         GtkWindow *window;
54         int x, y, h, w;
55
56         window = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
57
58         x = g_settings_get_int(settings, "window-x");
59         y = g_settings_get_int(settings, "window-y");
60         w = g_settings_get_int(settings, "window-width");
61         h = g_settings_get_int(settings, "window-height");
62
63         gtk_window_set_default_size(window, w, h);
64         gtk_window_move(window, x, y);
65
66         g_signal_connect(window, "delete_event",
67                          G_CALLBACK(delete_event_cbk), settings);
68
69         return window;
70 }
71