[tepl: 2/2] Better fix problem of GtkWindow height that grows when adding infobars



commit 80770a1faf58958d153f44096d1cbd61e23bdc04
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jun 23 14:44:49 2017 +0200

    Better fix problem of GtkWindow height that grows when adding infobars

 tepl/tepl-info-bar.c |   24 +++++++++++++++++++++++-
 tepl/tepl-tab.c      |   29 ++++++++++++++++++++++-------
 2 files changed, 45 insertions(+), 8 deletions(-)
---
diff --git a/tepl/tepl-info-bar.c b/tepl/tepl-info-bar.c
index f30d4a2..0f660d8 100644
--- a/tepl/tepl-info-bar.c
+++ b/tepl/tepl-info-bar.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016, 2017 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -384,5 +384,27 @@ tepl_info_bar_create_label (void)
        gtk_label_set_line_wrap_mode (label, PANGO_WRAP_WORD_CHAR);
        gtk_label_set_selectable (label, TRUE);
 
+       /* Since the wrapping is enabled, we need to set a minimum width.
+        *
+        * If a minimum width is not set, adding an info bar to a container
+        * (e.g. a TeplTab) can make the GtkWindow height to grow. Because
+        * without a minimum width (and without ellipsization), when the user
+        * resizes the window (e.g. reducing the width) the widgets inside the
+        * window must be able to be drawn. When the info bar must be drawn with
+        * a width of e.g. 20 pixels, it takes a huge height because of the text
+        * wrapping. So by setting a minimum width to the label, the maximum
+        * height that the info bar can take is limited, so in most cases the
+        * GtkWindow current height is sufficient to draw the info bar with its
+        * maximum height.
+        *
+        * See:
+        * https://wiki.gnome.org/HowDoI/Labels
+        *
+        * There is also a safety net in tepl_tab_add_info_bar() which calls
+        * gtk_widget_set_size_request() on the GtkInfoBar, to set a minimum
+        * width.
+        */
+       gtk_label_set_width_chars (label, 30);
+
        return label;
 }
diff --git a/tepl/tepl-tab.c b/tepl/tepl-tab.c
index 0c787f6..5a3a363 100644
--- a/tepl/tepl-tab.c
+++ b/tepl/tepl-tab.c
@@ -69,13 +69,6 @@ create_scrolled_window (void)
                      "expand", TRUE,
                      NULL);
 
-       /* If a size request is not set to the scrolled window, adding info bars
-        * makes the GtkWindow height to grow, probably because there is a
-        * gtk_widget_queue_resize() which takes the natural size of the
-        * scrolled window. Setting a size request fixes the problem.
-        */
-       gtk_widget_set_size_request (scrolled_window, 100, 40);
-
        return GTK_SCROLLED_WINDOW (scrolled_window);
 }
 
@@ -323,8 +316,30 @@ void
 tepl_tab_add_info_bar (TeplTab    *tab,
                       GtkInfoBar *info_bar)
 {
+       gint min_width;
+       gint min_height;
+
        g_return_if_fail (TEPL_IS_TAB (tab));
        g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
 
+       gtk_widget_get_size_request (GTK_WIDGET (info_bar), &min_width, &min_height);
+
+       /* If min_width != -1, gtk_widget_set_size_request() has already been
+        * called, so don't change the value.
+        */
+       if (min_width == -1)
+       {
+               /* Safety net to avoid in most cases the GtkWindow height to
+                * grow.
+                *
+                * The gtk_label_set_width_chars() call in
+                * tepl_info_bar_create_label() fixes the problem at the root,
+                * but we cannot enforce all GtkLabel of @info_bar to have been
+                * created with tepl_info_bar_create_label(), so a safety net is
+                * better.
+                */
+               gtk_widget_set_size_request (GTK_WIDGET (info_bar), 300, min_height);
+       }
+
        TEPL_TAB_GET_CLASS (tab)->pack_info_bar (tab, info_bar);
 }


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