From 0853697df23c1ced2d47514d7aa41860300a13cc Mon Sep 17 00:00:00 2001 From: Søren Sandmann Date: Mon, 10 Mar 2008 01:40:47 -0400 Subject: Meta data --- TODO | 13 ++++++- siv.c | 42 ++++++++++++++++++---- siv.h | 2 +- window.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 170 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index 79bf5af..86e65bc 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,16 @@ + - Make sure filenames are escaped if they contains [ or ] + - Save adjustment positions + +- Open + +- Dragging + +- Icon + - Recent files + +Done: + - Meta data -- Open diff --git a/siv.c b/siv.c index fa19ef4..2a7b5b8 100644 --- a/siv.c +++ b/siv.c @@ -72,7 +72,17 @@ make_filename (void) static void foreach (gpointer key, gpointer value, gpointer user_data) { - /* FIXME */ + gchar *filename = key; + MetaData *meta = value; + GKeyFile *key_file = user_data; + + g_key_file_set_integer (key_file, filename, "window_x", meta->window_x); + g_key_file_set_integer (key_file, filename, "window_y", meta->window_y); + g_key_file_set_integer (key_file, filename, "window_width", meta->window_width); + g_key_file_set_integer (key_file, filename, "window_height", meta->window_height); + g_key_file_set_integer (key_file, filename, "background", meta->background); + g_key_file_set_integer (key_file, filename, "smooth_image", meta->smooth_image); + g_key_file_set_integer (key_file, filename, "zoom_level", meta->zoom_level); } void @@ -80,8 +90,8 @@ app_set_meta_data (App *app, const char *filename, int window_x, int window_y, - int window_height, int window_width, + int window_height, gboolean smooth_image, BackgroundType background, int zoom_level) @@ -111,7 +121,7 @@ app_set_meta_data (App *app, output = g_key_file_to_data (keyfile, NULL, NULL); if (output) { - g_file_set_contents (filename, output, -1, NULL); + g_file_set_contents (key_filename, output, -1, NULL); g_free (output); } @@ -190,7 +200,7 @@ load_meta_data (void) if (!get_int (keyfile, group, "zoom_level", &(data->zoom_level))) data->zoom_level = 0; - g_hash_table_insert (result, group, data); + g_hash_table_insert (result, g_strdup (group), data); } g_strfreev (groups); @@ -213,6 +223,7 @@ app_new (int argc, char **argv) for (i = 1; i < argc; ++i) { char *option = argv[i]; + char *name; if (strcmp (option, "--version") == 0 || strcmp (option, "-v") == 0) @@ -223,7 +234,16 @@ app_new (int argc, char **argv) return; } - filenames = g_list_prepend (filenames, option); + if (g_path_is_absolute (option)) + { + name = option; + } + else + { + name = g_build_filename (g_get_current_dir(), option, NULL); + } + + filenames = g_list_prepend (filenames, name); } app = g_new0 (App, 1); @@ -238,11 +258,18 @@ app_new (int argc, char **argv) if (filenames) { + int i; + + i = 0; for (list = filenames; list != NULL; list = list->next) { Window *window = window_new (app); GError *err = NULL; char *filename = list->data; + + /* Don't open more than 32 windows */ + if (++i > 32) + break; if (!window_load_file (window, list->data, &err)) { @@ -254,7 +281,10 @@ app_new (int argc, char **argv) window_show (window); } } - + + for (list = filenames; list != NULL; list = list->next) + g_free (list->data); + g_list_free (filenames); } else diff --git a/siv.h b/siv.h index f842a5e..40a3abd 100644 --- a/siv.h +++ b/siv.h @@ -16,7 +16,7 @@ typedef enum { BG_FIRST, BG_NONE = BG_FIRST, - BG_CHECKERBAORD, + BG_CHECKERBOARD, BG_WHITE, BG_LAST } BackgroundType; diff --git a/window.c b/window.c index 764972b..808613f 100644 --- a/window.c +++ b/window.c @@ -200,6 +200,46 @@ set_title (Window *window) } } +static void +save_meta_data (Window *window) +{ + int x, y, width, height; + + if (window->filename) + { + BackgroundType bg; + gboolean smooth; + + gtk_window_get_position (get_widget (window, "main_window"), &x, &y); + gtk_window_get_size (get_widget (window, "main_window"), &width, &height); + + if (gtk_check_menu_item_get_active (get_widget (window, "menu_no"))) + { + bg = BG_NONE; + } + else if (gtk_check_menu_item_get_active (get_widget (window, "menu_white"))) + { + bg = BG_WHITE; + } + else if (gtk_check_menu_item_get_active (get_widget (window, "menu_checkerboard"))) + { + bg = BG_CHECKERBOARD; + } + else + { + bg = BG_NONE; + } + + smooth = gtk_check_menu_item_get_active (get_widget (window, "menu_smooth_image")); + + app_set_meta_data (window->app, + window->filename, + x, y, width, height, + bg, smooth, + window->zoom_level); + } +} + static void rebuild (Window *window) { @@ -305,7 +345,7 @@ on_size_allocate (GtkWidget *widget, GtkAllocation *allocation, gpointer data) { gtk_widget_queue_draw (widget); } - else + else if (GTK_WIDGET_REALIZED (widget)) { GdkRectangle old; GdkRectangle new; @@ -419,6 +459,36 @@ connect_signals (Window *window) } } +static void +set_defaults (Window *window) +{ + GtkWidget *widget = get_widget (window, "main_window"); + GdkScreen *screen = gtk_widget_get_screen (widget); + GdkRectangle monitor; + int monitor_num; + int width, height; + + monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window); + + gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor); + + g_assert (window->original); + + width = gdk_pixbuf_get_width (window->original) + 48; + height = gdk_pixbuf_get_height (window->original) + 96; + + if (width > monitor.width) + width = monitor.width - 32; + + if (height > monitor.height) + height = monitor.height - 32; + + gtk_window_set_default_size (get_widget (window, "main_window"), width, height); + + gtk_check_menu_item_set_active (get_widget (window, "menu_smooth_image"), TRUE); + gtk_check_menu_item_set_active (get_widget (window, "menu_no"), TRUE); +} + gboolean window_load_file (Window *window, const char *filename, GError **err) { @@ -434,7 +504,50 @@ window_load_file (Window *window, const char *filename, GError **err) } else { + const MetaData *data; + window->original = pixbuf; + + data = app_get_meta_data (window->app, window->filename); + + if (data) + { + GtkCheckMenuItem *item; + + gtk_window_move (get_widget (window, "main_window"), + data->window_x, data->window_y); + + gtk_window_resize (get_widget (window, "main_window"), + data->window_width, data->window_height); + + switch (data->background) + { + default: + case BG_NONE: + item = get_widget (window, "menu_no"); + break; + + case BG_CHECKERBOARD: + item = get_widget (window, "menu_checkerboard"); + break; + + case BG_WHITE: + item = get_widget (window, "menu_white"); + break; + } + + gtk_check_menu_item_set_active (item, TRUE); + + gtk_check_menu_item_set_active (get_widget (window, "menu_smooth_image"), + data->smooth_image); + + window->zoom_level = data->zoom_level; + } + else + { + set_defaults (window); + } + rebuild (window); return TRUE; } @@ -451,24 +564,30 @@ window_new (App *app) gtk_widget_add_events (get_widget (window, "drawing_area"), GDK_SCROLL_MASK); gtk_widget_set_redraw_on_allocate ( get_widget (window, "drawing_area"), FALSE); + + gtk_widget_realize (get_widget (window, "main_window")); /* Connect signals */ connect_signals (window); window->zoom_level = 0; window->first = TRUE; + + gtk_window_set_default_size (get_widget (window, "main_window"), 640, 480); rebuild (window); app_register_window (app, window); return window; - } void window_free (Window *window) { + if (window->filename) + save_meta_data (window); + app_unregister_window (window->app, window); gtk_widget_destroy (get_widget (window, "main_window")); -- cgit v1.2.3