summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2009-12-23 22:26:34 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2009-12-23 22:26:34 +0100
commit211177a9747939002f8e7da1d569a5551c4e0600 (patch)
treef08d0e96034cf40854e7b32adc3f0a82d520be24
parent9030ddb9d47663d16327a9e734ccfd0c74ba0bb7 (diff)
Use GKeyFile to store window positions.
-rw-r--r--walls.ui7
-rw-r--r--walls_conf.c56
-rw-r--r--walls_conf.h12
-rw-r--r--window_main.c46
4 files changed, 112 insertions, 9 deletions
diff --git a/walls.ui b/walls.ui
index 0a75f8e..bc9a71e 100644
--- a/walls.ui
+++ b/walls.ui
@@ -3,6 +3,7 @@
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window">
+ <property name="title" translatable="yes">walls</property>
<signal name="destroy" handler="gtk_main_quit"/>
<signal name="size_allocate" handler="on_window_size_allocate"/>
<child>
@@ -42,7 +43,7 @@
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<accelerator key="q" signal="activate" modifiers="GDK_CONTROL_MASK"/>
- <signal name="activate" handler="gtk_main_quit"/>
+ <signal name="activate" handler="on_file_quit_activate"/>
</object>
</child>
</object>
@@ -86,7 +87,7 @@
<object class="GtkHPaned" id="window_hpane">
<property name="visible">True</property>
<child>
- <object class="GtkVPaned" id="vpaned1">
+ <object class="GtkVPaned" id="window_vpane">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="orientation">vertical</property>
@@ -154,8 +155,6 @@
<property name="visible">True</property>
<child>
<object class="GtkImage" id="image">
- <property name="width_request">100</property>
- <property name="height_request">80</property>
<property name="visible">True</property>
</object>
</child>
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 <glib.h>
+
+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;
}