From ddad5c3ce97b4ff9ebd31f9400e8c49d5c04e81f Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 5 Jun 2018 14:04:30 -0400 Subject: [PATCH] Keep aspect ratio of the image Since we don't have a good way to control the window size tightly, make the content keep aspect ratio. --- demos/gtk-demo/sliding_puzzle.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/demos/gtk-demo/sliding_puzzle.c b/demos/gtk-demo/sliding_puzzle.c index 0ba76155d8..5a5ced17b4 100644 --- a/demos/gtk-demo/sliding_puzzle.c +++ b/demos/gtk-demo/sliding_puzzle.c @@ -11,6 +11,7 @@ #include "puzzlepiece.h" static GtkWidget *window = NULL; +static GtkWidget *frame = NULL; static gboolean solved = TRUE; static guint width = 6; @@ -220,14 +221,14 @@ start_puzzle (GdkPaintable *puzzle) guint x, y; /* Remove the old grid (if there is one) */ - grid = gtk_bin_get_child (GTK_BIN (window)); + grid = gtk_bin_get_child (GTK_BIN (frame)); if (grid) - gtk_container_remove (GTK_CONTAINER (window), grid); + gtk_container_remove (GTK_CONTAINER (frame), grid); /* Create a new grid */ grid = gtk_grid_new (); gtk_widget_set_can_focus (grid, TRUE); - gtk_container_add (GTK_CONTAINER (window), grid); + gtk_container_add (GTK_CONTAINER (frame), grid); /* Add a key event controller so people can use the arrow * keys to move the puzzle */ @@ -284,12 +285,17 @@ do_sliding_puzzle (GtkWidget *do_widget) gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget)); gtk_window_set_title (GTK_WINDOW (window), "Sliding Puzzle"); - gtk_window_set_default_size (GTK_WINDOW (window), 300, 200); + gtk_window_set_default_size (GTK_WINDOW (window), 400, 300); g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window); - /* Start a puzzle with a default image */ puzzle = GDK_PAINTABLE (gdk_texture_new_from_resource ("/sliding_puzzle/portland-rose.jpg")); + + frame = gtk_aspect_frame_new (NULL, 0.5, 0.5, (float) gdk_paintable_get_intrinsic_aspect_ratio (puzzle), FALSE); + gtk_container_add (GTK_CONTAINER (window), frame); + + /* Start a puzzle with a default image */ + start_puzzle (puzzle); g_object_unref (puzzle); }