gui/window: Save width and height between sessions

Saving maximized state is too complicated, so let's just store width and
height of the window when it is not maximized.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-26 20:51:05 -05:00
parent 0c214dd1a9
commit 870a2b769b
4 changed files with 77 additions and 1 deletions

View File

@ -3,6 +3,45 @@
*/
#include <core/version.h>
#include <gui/builder.h>
#include <gui/settings.h>
static const gchar *SETTINGS_WIDTH = "gui.window.width";
static const gchar *SETTINGS_HEIGHT = "gui.window.height";
static int saved_width = 0;
static int saved_height = 0;
gboolean __window_configure(GtkWindow *window, GdkEventConfigure *event,
gpointer data)
{
int width = gui_settings_get(SETTINGS_WIDTH);
int height = gui_settings_get(SETTINGS_HEIGHT);
if (event->width != width) {
saved_width = width;
gui_settings_set(SETTINGS_WIDTH, event->width);
}
if (event->height != height) {
saved_height = height;
gui_settings_set(SETTINGS_HEIGHT, event->height);
}
return FALSE;
}
gboolean __window_state(GtkWindow *window, GdkEventWindowState *event,
gpointer data)
{
if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED &&
event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) {
gui_settings_set(SETTINGS_WIDTH, saved_width);
gui_settings_set(SETTINGS_HEIGHT, saved_height);
}
return FALSE;
}
void gui_window_init(const gchar *icon)
@ -13,6 +52,11 @@ void gui_window_init(const gchar *icon)
gtk_window_set_title(window, title);
gtk_window_set_icon_from_file(window, icon, NULL);
saved_width = gui_settings_get(SETTINGS_WIDTH);
saved_height = gui_settings_get(SETTINGS_HEIGHT);
if (saved_width > 0 && saved_height > 0)
gtk_window_resize(window, saved_width, saved_height);
g_free(title);
}

View File

@ -6,6 +6,7 @@
#ifndef OCARINA_GUI_SETTINGS_H
#define OCARINA_GUI_SETTINGS_H
#include <glib.h>
#include <stdbool.h>
/* Called to initialize GUI settings. */

View File

@ -216,6 +216,8 @@
<property name="title" translatable="yes">Ocarina 6.1.3</property>
<property name="default_width">1024</property>
<property name="default_height">683</property>
<signal name="configure-event" handler="__window_configure" swapped="no"/>
<signal name="window-state-event" handler="__window_state" swapped="no"/>
<child>
<object class="GtkBox" id="box2">
<property name="name">=</property>

View File

@ -2,14 +2,23 @@
* Copyright 2015 (c) Anna Schumaker.
*/
#include <gui/builder.h>
#include <gui/settings.h>
#include <gui/window.h>
#include <tests/test.h>
GMainLoop *main_loop;
void __audio_can_accel() {}
void __audio_pause_count() {}
void __audio_pause_enabled() {}
void __audio_seek() {}
static int test_on_idle(gpointer data)
{
g_main_loop_quit(main_loop);
return G_SOURCE_CONTINUE;
}
static void test_window()
{
GtkWindow *window;
@ -17,12 +26,32 @@ static void test_window()
gtk_init(&argc, NULL);
gui_builder_init("share/ocarina/ocarina6.glade");
gui_settings_init();
gui_window_init("share/ocarina/ocarina.png");
window = GTK_WINDOW(gui_builder_widget("o_window"));
main_loop = g_main_loop_new(NULL, FALSE);
window = GTK_WINDOW(gui_builder_widget("o_window"));
g_idle_add(test_on_idle, window);
test_equal(gui_settings_has("gui.window.width"), (bool)false);
test_equal(gui_settings_has("gui.window.height"), (bool)false);
test_equal(gtk_window_get_title(window), "Ocarina " CONFIG_VERSION);
g_main_loop_run(main_loop);
test_equal(gui_settings_has("gui.window.width"), (bool)true);
test_equal(gui_settings_has("gui.window.height"), (bool)true);
gui_settings_set("gui.window.width", 800);
gui_settings_set("gui.window.height", 600);
gui_window_init("share/ocarina/ocarina.png");
g_main_loop_run(main_loop);
test_equal(gui_settings_get("gui.window.width"), 800);
test_equal(gui_settings_get("gui.window.height"), 600);
g_main_loop_unref(main_loop);
gui_window_deinit();
gui_settings_deinit();
gui_builder_deinit();
}