Web lists-archives.com

Resize all images that are part of a notebook




Greetings,
I am trying to create a program that has a notebook with several images. I want to be
able to grab the corner of the window and expand or contract the image size,
maintaining aspect ratio of any image on any notebook page. The following is my latest attempt. However, this program auto increases in size and the user has no 
control. Someone attempting to try this would need two images in the folder with the
executable, image1.jpg and image2.jpg.
Compile with GTK3 libs.
Thanks,
Jack

#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

GtkWidget *notebook;

gboolean resize_image(GtkWidget *widget, GdkRectangle *allocation,
gpointer user_data)
{
int w,h, pagenum;
GdkPixbuf *pxbscaled;
GtkWidget *image;
GdkPixbuf *pixbuf;

pagenum = gtk_notebook_get_current_page (GTK_NOTEBOOK(notebook));
image = gtk_notebook_get_nth_page (GTK_NOTEBOOK(notebook), pagenum);
// GtkImageType image_type = gtk_image_get_storage_type
// (GTK_IMAGE(image));

pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));

h = allocation->height;
w = (gdk_pixbuf_get_width(pixbuf) * h) / gdk_pixbuf_get_height(pixbuf);

pxbscaled = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);

printf("Allocation height %d width %d.\n", h, w);

gtk_image_set_from_pixbuf(GTK_IMAGE(image), pxbscaled);

g_object_unref (pxbscaled);

sleep(2);

return FALSE;
}

static gboolean delete( GtkWidget *widget,
GtkWidget *event,
gpointer   data )
{
gtk_main_quit ();
return FALSE;
}

int main( int argc,
  char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget *label;
GtkWidget *image;
int i;
char bufferl[32];

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
// gtk_widget_set_size_request (GTK_WIDGET(window), 800, 480);

g_signal_connect (window, "delete-event",
  G_CALLBACK (delete), NULL);

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

table = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), table);

/* Create notebook, place position of tabs */
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
gtk_grid_attach (GTK_GRID (table), notebook, 0, 6, 3, 3);
gtk_widget_show (notebook);

/* Append pages to the notebook */
for (i = 0; i < 2; i++) {
sprintf(bufferl, "Page %d", i + 1);

if (i == 0) {
image = gtk_image_new_from_file("image1.jpg");
} else {
image = gtk_image_new_from_file("image2.jpg");
}

gtk_widget_set_halign(image, GTK_ALIGN_START);
gtk_widget_set_valign(image, GTK_ALIGN_START);

g_signal_connect(window, "size-allocate", 
G_CALLBACK(resize_image), NULL);

label = gtk_label_new (bufferl);
gtk_notebook_append_page (GTK_NOTEBOOK(notebook), 
image, label);
}

/* Create a close button */
button = gtk_button_new_with_label ("close");
g_signal_connect (button, "clicked",
  G_CALLBACK (delete), NULL);

gtk_grid_attach (GTK_GRID (table), button, 0, 10, 1, 1);

gtk_widget_show_all (window);

gtk_main ();

return 0;
}

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list