[gtk+] window: Be smarter about computing the default size



commit 85aebe74cf465dd39d45cb2668c2f0691c754f98
Author: Benjamin Otte <otte redhat com>
Date:   Wed Apr 27 00:05:01 2011 +0200

    window: Be smarter about computing the default size
    
    See the code comments for the reasoning behind this. After we don't
    force a "guessed" minimum size for labels anymore, a lot of issues
    started to surface that this patch attempts to fix. In particular:
    
    1) Tooltips where wrapped as much as possible.
    2) The recentchooser submenu displayed only ellipsize dots.

 gtk/gtkwindow.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 4 deletions(-)
---
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 4042d82..3ac8628 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -4833,18 +4833,71 @@ gtk_window_unmap (GtkWidget *widget)
     gtk_widget_unmap (child);
 }
 
+/* (Note: Replace "size" with "width" or "height". Also, the request
+ * mode is honoured.)
+ * For selecting the default window size, the following conditions
+ * should hold (in order of importance):
+ * - the size is not below the minimum size
+ *   Windows cannot be resized below their minimum size, so we must
+ *   ensure we don't do that either.
+ * - the size is not above the natural size
+ *   It seems weird to allocate more than this in an initial guess.
+ * - the size does not exceed that of a maximized window
+ *   We want to see the whole window after all.
+ *   (Note that this may not be possible to achieve due to imperfect
+ *    information from the windowing system.)
+ */
+
+/* We use these for now to not make windows too big by accident. Note
+ * that we still clamp these numbers by screen size. Also note that
+ * minimum size still overrides this. So keep your windows small! :)
+ */
+#define MAX_DEFAULT_WINDOW_WIDTH 640
+#define MAX_DEFAULT_WINDOW_HEIGHT 480
+
 static void
 gtk_window_guess_default_size (GtkWindow *window,
                                gint      *width,
                                gint      *height)
 {
   GtkWidget *widget = GTK_WIDGET (window);
-  GtkRequisition requisition;
+  GdkScreen *screen;
+  int minimum, natural;
+
+  screen = gtk_widget_get_screen (widget);
+
+  *width = gdk_screen_get_width (screen);
+  *height = gdk_screen_get_height (screen);
+
+  if (*width < *height)
+    {
+      /* landscape */
+      *width = MIN (*width, MAX_DEFAULT_WINDOW_WIDTH);
+      *height = MIN (*height, MAX_DEFAULT_WINDOW_HEIGHT);
+    }
+  else
+    {
+      /* portrait */
+      *width = MIN (*width, MAX_DEFAULT_WINDOW_HEIGHT);
+      *height = MIN (*height, MAX_DEFAULT_WINDOW_WIDTH);
+    }
 
-  gtk_widget_get_preferred_size (widget, &requisition, NULL);
+  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
+    {
+      gtk_widget_get_preferred_height (widget, &minimum, &natural);
+      *height = MAX (minimum, MIN (*height, natural));
 
-  *width = requisition.width;
-  *height = requisition.height;
+      gtk_widget_get_preferred_width_for_height (widget, *height, &minimum, &natural);
+      *width = MAX (minimum, MIN (*width, natural));
+    }
+  else /* GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or CONSTANT_SIZE */
+    {
+      gtk_widget_get_preferred_width (widget, &minimum, &natural);
+      *width = MAX (minimum, MIN (*width, natural));
+
+      gtk_widget_get_preferred_height_for_width (widget, *width, &minimum, &natural);
+      *height = MAX (minimum, MIN (*height, natural));
+    }
 }
 
 static void



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