[gtk/wip/otte/puzzle] Keep aspect ratio of the image



commit 4bf634292d059740655f20cbcf919c83ffe5a018
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 5 14:04:30 2018 -0400

    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 | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
---
diff --git a/demos/gtk-demo/sliding_puzzle.c b/demos/gtk-demo/sliding_puzzle.c
index 0ba76155d8..88487865f6 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 */
@@ -280,16 +281,25 @@ do_sliding_puzzle (GtkWidget *do_widget)
 
   if (!window)
     {
+      GdkPixbuf *pixbuf;
+
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
       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);
 
+      pixbuf = gdk_pixbuf_new_from_resource ("/sliding_puzzle/portland-rose.jpg", NULL);
+
+      frame = gtk_aspect_frame_new (NULL, 0.5, 0.5, (float) gdk_pixbuf_get_height (pixbuf) / (float) 
gdk_pixbuf_get_width (pixbuf), FALSE);
+      gtk_container_add (GTK_CONTAINER (window), frame);
+
       /* Start a puzzle with a default image */
-      puzzle = GDK_PAINTABLE (gdk_texture_new_from_resource ("/sliding_puzzle/portland-rose.jpg"));
+      
+      puzzle = GDK_PAINTABLE (gdk_texture_new_for_pixbuf (pixbuf));
+      g_object_unref (pixbuf);
       start_puzzle (puzzle);
       g_object_unref (puzzle);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]