From 211177a9747939002f8e7da1d569a5551c4e0600 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 23 Dec 2009 22:26:34 +0100 Subject: Use GKeyFile to store window positions. --- walls.ui | 7 +++---- walls_conf.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ walls_conf.h | 12 ++++++++++++ window_main.c | 46 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 walls_conf.c create mode 100644 walls_conf.h diff --git a/walls.ui b/walls.ui index 0a75f8e..bc9a71e 100644 --- a/walls.ui +++ b/walls.ui @@ -3,6 +3,7 @@ + walls @@ -42,7 +43,7 @@ True True - + @@ -86,7 +87,7 @@ True - + True True vertical @@ -154,8 +155,6 @@ True - 100 - 80 True diff --git a/walls_conf.c b/walls_conf.c new file mode 100644 index 0000000..7ef3a8d --- /dev/null +++ b/walls_conf.c @@ -0,0 +1,56 @@ +#include "walls_conf.h" + +GKeyFile *keyfile = NULL; + +void conf_open() { + gchar *filename; + GError *error = NULL; + + keyfile = g_key_file_new(); + if(!keyfile) { + g_warning("Could not create key"); + return; + } + + filename = g_strdup_printf("%s/walls/config", g_get_user_config_dir()); + + if(!g_key_file_load_from_file(keyfile, filename, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) { + g_warning("%s", error->message); + g_error_free(error); + } + + g_free(filename); +} + +gint conf_get_int(const gchar *group_name, const gchar *key, gint _default) { + GError *error = NULL; + gint ret; + + ret = g_key_file_get_integer(keyfile, group_name, key, &error); + + if(ret == 0) { + g_warning("%s", error->message); + g_error_free(error); + return _default; + } + return ret; +} + +void conf_close() { + gchar *s, *filename; + GError *error = NULL; + + if(keyfile) { + filename = g_strdup_printf("%s/walls/config", g_get_user_config_dir()); + s = g_key_file_to_data(keyfile, NULL, NULL); + if(!g_file_set_contents(filename, s, -1, &error)) { + g_warning("%s", error->message); + g_error_free(error); + } + g_free(filename); + g_free(s); + + g_key_file_free(keyfile); + keyfile = NULL; + } +} diff --git a/walls_conf.h b/walls_conf.h new file mode 100644 index 0000000..f0f7859 --- /dev/null +++ b/walls_conf.h @@ -0,0 +1,12 @@ +#ifndef _WALLS_CONF_H_ +#define _WALLS_CONF_H_ + +#include + +void conf_open(); +void conf_close(); +gint conf_get_int(const gchar*, const gchar*, gint); + +extern GKeyFile *keyfile; + +#endif diff --git a/window_main.c b/window_main.c index 4038206..04209b9 100644 --- a/window_main.c +++ b/window_main.c @@ -11,6 +11,7 @@ #include "walls_model.h" #include "wallpapers.h" #include "thumbnails.h" +#include "walls_conf.h" struct wallpaper_t *cur_wall = NULL; GdkPixbuf *orig_pixbuf = NULL; @@ -18,10 +19,12 @@ gint last_width = 0, last_height = 0, last_pos = 0; +GtkWindow *window; GtkImage *image = NULL; GtkWidget *layout = NULL; GtkStatusbar *statusbar; GtkIconView *thumbview; +GtkWidget *window_hpane, *window_vpane; GThread *add_thread = NULL, *thumb_thread = NULL; @@ -261,10 +264,38 @@ void on_add_dir_action_activate(GtkAction *action, gpointer user_data) { gtk_widget_destroy(dialog); } +static void save_window() { + gint width, height; + + gtk_window_get_size(window, &width, &height); + g_key_file_set_integer(keyfile, "window", "width", width); + g_key_file_set_integer(keyfile, "window", "height", height); + g_key_file_set_integer(keyfile, "window", "hpane_pos", gtk_paned_get_position(GTK_PANED(window_hpane))); + g_key_file_set_integer(keyfile, "window", "vpane_pos", gtk_paned_get_position(GTK_PANED(window_vpane))); +} + +void on_file_quit_activate(GtkMenuItem *menuitem, gpointer user_data) { + save_window(); + + gtk_main_quit(); +} + +static void set_sizes() { + gint width, height, hpane_pos, vpane_pos; + + width = conf_get_int("window", "width", 800); + height = conf_get_int("window", "height", 600); + gtk_window_set_default_size(window, width, height); + + hpane_pos = conf_get_int("window", "hpane_pos", 190); + vpane_pos = conf_get_int("window", "vpane_pos", 160); + + gtk_paned_set_position(GTK_PANED(window_hpane), hpane_pos); + gtk_paned_set_position(GTK_PANED(window_vpane), vpane_pos); +} + int gui_main(int argc, char **argv) { - GtkWidget *window; GtkWidget *foldtree; - GtkWidget *window_hpane; GtkBuilder *builder ; GError *error = NULL; GdkColor color; @@ -276,6 +307,8 @@ int gui_main(int argc, char **argv) { if(!db_open()) return 1; + conf_open(); + gtk_init(&argc, &argv); builder = gtk_builder_new(); @@ -285,8 +318,7 @@ int gui_main(int argc, char **argv) { return 1; } - window = GTK_WIDGET(gtk_builder_get_object(builder, "window")); - gtk_window_set_title(GTK_WINDOW(window), "walls"); + window = GTK_WINDOW(gtk_builder_get_object(builder, "window")); foldtree = GTK_WIDGET(gtk_builder_get_object(builder, "foldtree")); foldtree_init(GTK_TREE_VIEW(foldtree)); @@ -302,6 +334,7 @@ int gui_main(int argc, char **argv) { window_hpane = GTK_WIDGET(gtk_builder_get_object(builder, "window_hpane")); g_signal_connect(window_hpane, "notify::position", G_CALLBACK(on_window_hpane_resized), window_hpane); + window_vpane = GTK_WIDGET(gtk_builder_get_object(builder, "window_vpane")); statusbar = GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar")); image_context = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "Image display"); @@ -310,11 +343,14 @@ int gui_main(int argc, char **argv) { g_object_unref(G_OBJECT(builder)); - gtk_widget_show_all(window); + set_sizes(); + + gtk_widget_show_all(GTK_WIDGET(window)); gtk_main(); db_close(); + conf_close(); return 0; } -- cgit v1.2.3