From 0ccb0f910cf0a19b577685c0f1598b97d3d825cb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 23 Dec 2009 01:09:34 +0100 Subject: Fixed automatic resizing of opened image. Resizing the pane is currently very slow and buggy. This is will be fixed at a later time. --- window_main.c | 77 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 18 deletions(-) (limited to 'window_main.c') diff --git a/window_main.c b/window_main.c index 57c8885..47c70ec 100644 --- a/window_main.c +++ b/window_main.c @@ -7,9 +7,13 @@ #include "db.h" #include "walls_model.h" -GdkPixbuf *pixbuf = NULL; +GdkPixbuf *orig_pixbuf = NULL; +gint last_width = 0, + last_height = 0, + last_pos = 0; GtkImage *image = NULL; +GtkWidget *layout = NULL; void on_filetree_selection_changed(GtkTreeSelection *treeselection, gpointer user_data); @@ -63,24 +67,17 @@ inline static void filetree_init(GtkTreeView *filetree) { g_signal_connect(selection, "changed", G_CALLBACK(on_filetree_selection_changed), filetree); } -static void load_pixbuf(struct wallpaper_t *wall) { - GdkPixbuf *pb, *pb2; - GError *error = NULL; +static void resize_pixbuf() { + GdkPixbuf *pb; GdkWindow *window; gint win_width, win_height, img_width, img_height, width, height; gdouble scalex, scaley, width_ratio, height_ratio, max_ratio; - pb = gdk_pixbuf_new_from_file(wall->filepath, &error); - if(!pb) { - g_warning("%s", error->message); - g_free(error); - return; - } - - window = gtk_widget_get_window(GTK_WIDGET(image)); + window = gtk_widget_get_window(layout); gdk_drawable_get_size(window, &win_width, &win_height); - img_width = gdk_pixbuf_get_width(pb); - img_height = gdk_pixbuf_get_height(pb); + gtk_layout_set_size(GTK_LAYOUT(layout), win_width, win_height); + img_width = gdk_pixbuf_get_width(orig_pixbuf); + img_height = gdk_pixbuf_get_height(orig_pixbuf); /* do we need to resize? */ if(img_width > win_width || img_height > win_height) { @@ -98,17 +95,57 @@ static void load_pixbuf(struct wallpaper_t *wall) { scalex = (gdouble)width / (gdouble)img_width; scaley = (gdouble)height / (gdouble)img_height; } - pb2 = pb; pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); - gdk_pixbuf_scale(pb2, pb, 0, 0, width, height, 0, 0, scalex, scaley, GDK_INTERP_BILINEAR); - gdk_pixbuf_unref(pb2); + gdk_pixbuf_scale(orig_pixbuf, pb, 0, 0, width, height, 0, 0, scalex, scaley, GDK_INTERP_BILINEAR); + } else { + width = img_width; + height = img_height; + pb = gdk_pixbuf_copy(orig_pixbuf); } - //gtk_image_set_from_file(image, wall->filepath); + gtk_widget_set_size_request(GTK_WIDGET(image), width, height); + gtk_widget_set_uposition(GTK_WIDGET(image), (width < win_width ? win_width / 2 - width / 2 : 0), (height < win_height ? win_height / 2 - height / 2 : 0)); gtk_image_set_from_pixbuf(image, pb); gdk_pixbuf_unref(pb); } +void on_window_size_allocate(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data) { + if(orig_pixbuf && (allocation->width != last_width || allocation->height != last_height)) { + resize_pixbuf(); + last_width = allocation->width; + last_height = allocation->height; + } +} + +void on_window_hpane_resized(GObject *gobject, GParamSpec *pspec, gpointer user_data) { + gint pos; + + pos = gtk_paned_get_position(GTK_PANED(gobject)); + if(orig_pixbuf && pos != last_pos) { + resize_pixbuf(); + last_pos = pos; + } +} + +static void load_pixbuf(struct wallpaper_t *wall) { + GdkPixbuf *pb; + GError *error = NULL; + GdkWindow *window; + gint win_width, win_height, img_width, img_height, width, height; + gdouble scalex, scaley, width_ratio, height_ratio, max_ratio; + + if(orig_pixbuf) + gdk_pixbuf_unref(orig_pixbuf); + orig_pixbuf = gdk_pixbuf_new_from_file(wall->filepath, &error); + if(!orig_pixbuf) { + g_warning("%s", error->message); + g_free(error); + return; + } + + resize_pixbuf(); +} + void on_filetree_selection_changed(GtkTreeSelection *treeselection, gpointer user_data) { GtkTreeModel *model; GtkTreeIter iter; @@ -125,6 +162,7 @@ void on_filetree_selection_changed(GtkTreeSelection *treeselection, gpointer use int gui_main(int argc, char **argv) { GtkWidget *window; GtkWidget *filetree; + GtkWidget *window_hpane; GtkBuilder *builder ; GError *error = NULL; @@ -147,6 +185,9 @@ int gui_main(int argc, char **argv) { filetree_init(GTK_TREE_VIEW(filetree)); image = GTK_IMAGE(gtk_builder_get_object(builder, "image")); + layout = GTK_WIDGET(gtk_builder_get_object(builder, "layout")); + 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); gtk_builder_connect_signals(builder, NULL); -- cgit v1.2.3