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.
This commit is contained in:
Matthias Clasen 2018-06-05 14:04:30 -04:00
parent b6c8943bbf
commit ddad5c3ce9

View File

@ -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);
}