Keep aspect ratio of the image
authorMatthias Clasen <mclasen@redhat.com>
Tue, 5 Jun 2018 18:04:30 +0000 (14:04 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 8 Jun 2018 01:47:30 +0000 (21:47 -0400)
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

index 0ba76155d8a7722b0decde92549614973afea7ac..5a5ced17b42d383814c4ea55520872025f4dc099 100644 (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);
     }