shithub: puzzles

Download patch

ref: 4e1cc6570180381866735d7e23223f4ac4a68b08
parent: 37232c3c304d07fcfb8d8f512c0359a7f23fea82
author: Simon Tatham <anakin@pobox.com>
date: Sat Oct 3 08:28:13 EDT 2015

GTK 3 port: condition out the complicated window resize code.

In GTK 2, we had a big pile of horrible code to deal with the fact
that it's very hard to open a window in such a way as to make it easy
to resize smaller as well as bigger.

Essentially, we'd open the window with the drawing area's minimum size
request set to the desired _initial_ window size; then we'd wait until
GTK had finished creating other window components (menu bar, status
line) and the window's size allocation had settled down, and finally
reduce the size request to the real minimum size so that now the
window was resizable in both directions. This also involved some
deliberate checking of the Ubuntu Unity GTK extension which steals the
menu bar and put it elsewhere (see commit 8f8333a35), to avoid us
waiting forever for a menu bar that wasn't going to show up.

But in GTK3, this has all become actually sensible! All we now have to
do is to set the window's geometry hints to indicate the drawing area
itself as the base widget (probably a good plan anyway), and then we
can set the initial size using gtk_window_set_default_geometry() and
resize it later using gtk_window_resize_to_geometry(). So now we can
completely condition out all of the previous horrors, and consider
them to be legacy GTK2 compatibility code only. Phew.

--- a/gtk.c
+++ b/gtk.c
@@ -156,13 +156,15 @@
 #ifdef OLD_FILESEL
     char *filesel_name;
 #endif
-    int drawing_area_shrink_pending;
     GSList *preset_radio;
     int n_preset_menu_items;
     int preset_threaded;
     GtkWidget *preset_custom;
     GtkWidget *copy_menu_item;
+#if !GTK_CHECK_VERSION(3,0,0)
+    int drawing_area_shrink_pending;
     int menubar_is_local;
+#endif
 };
 
 struct blitter {
@@ -1763,6 +1765,7 @@
     }
 }
 
+#if !GTK_CHECK_VERSION(3,0,0)
 static gboolean not_size_allocated_yet(GtkWidget *w)
 {
     /*
@@ -1811,11 +1814,12 @@
         fe->drawing_area_shrink_pending = FALSE;
     }
 }
+#endif /* !GTK_CHECK_VERSION(3,0,0) */
 
 static gint configure_window(GtkWidget *widget,
                              GdkEventConfigure *event, gpointer data)
 {
-    frontend *fe = (frontend *)data;
+#if !GTK_CHECK_VERSION(3,0,0)
     /*
      * When the main puzzle window changes size, it might be because
      * the menu bar or status bar has turned up after starting off
@@ -1822,7 +1826,9 @@
      * absent, in which case we should have another go at enacting a
      * pending shrink of the drawing area.
      */
+    frontend *fe = (frontend *)data;
     try_shrink_drawing_area(fe);
+#endif
     return FALSE;
 }
 
@@ -1831,6 +1837,10 @@
     int x, y;
 
     get_size(fe, &x, &y);
+
+#if GTK_CHECK_VERSION(3,0,0)
+    gtk_window_resize_to_geometry(GTK_WINDOW(fe->window), x, y);
+#else
     fe->w = x;
     fe->h = y;
     fe->drawing_area_shrink_pending = FALSE;
@@ -1842,6 +1852,7 @@
     }
     fe->drawing_area_shrink_pending = TRUE;
     try_shrink_drawing_area(fe);
+#endif
 }
 
 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
@@ -2271,6 +2282,7 @@
 	midend_new_game(fe->me);
     }
 
+#if !GTK_CHECK_VERSION(3,0,0)
     {
         /*
          * try_shrink_drawing_area() will do some fiddling with the
@@ -2297,6 +2309,7 @@
             fe->menubar_is_local = !unity_mode;
         }
     }
+#endif
 
     fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
@@ -2514,9 +2527,19 @@
 #if GTK_CHECK_VERSION(2,0,0)
     gtk_widget_set_double_buffered(fe->area, FALSE);
 #endif
+    {
+        GdkGeometry geom;
+        geom.base_width = geom.base_height = 0;
+        gtk_window_set_geometry_hints(GTK_WINDOW(fe->window), fe->area,
+                                      &geom, GDK_HINT_BASE_SIZE);
+    }
     get_size(fe, &x, &y);
+#if GTK_CHECK_VERSION(3,0,0)
+    gtk_window_set_default_geometry(GTK_WINDOW(fe->window), x, y);
+#else
     fe->drawing_area_shrink_pending = FALSE;
     gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
+#endif
     fe->w = x;
     fe->h = y;
 
@@ -2581,8 +2604,11 @@
     gtk_widget_show(fe->area);
     gtk_widget_show(fe->window);
 
+#if !GTK_CHECK_VERSION(3,0,0)
     fe->drawing_area_shrink_pending = TRUE;
     try_shrink_drawing_area(fe);
+#endif
+
     set_window_background(fe, 0);
 
     return fe;