From 12f08d38b8b215afd07733e6da87e9728aa480d6 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Fri, 3 May 2019 15:42:45 +0200 Subject: [PATCH] Set the xalign property for Gtk::Labels (!40) With the same case as from the previous commit, the very long "Mounted on ..." text is now wrapped, but the text may not be left justified. Slowly adjust the dialog width and see how the text wrapping is updated to fit the size adjustment but the text is centred rather than left justified. This is because setting the halign property to Gtk::ALIGN_START does not guarantee left alignment of text for wrapped or ellipsized Gtk::Labels. Use the xalign property instead. To set the xalign property there is a method in the GtkMisc (Gtk::Misc) base class: gtk_misc_set_alignment (Gtk::Misc::set_alignment) However, GtkMisc (Gtk::Misc) was deprecated in Gtk 3.14 (Gtkmm 3.14) and in Gtk 3.16 (gtkmm 3.16) set_alignment() was replaced with the introduction of two new methods: gtk_label_set_xalign (Gtk::Label::set_xalign) gtk_label_set_yalign (Gtk::Label::set_yalign) Add a check for Gtkmm method Gtk::Label::set_xalign() in configure.ac and use it when available. References: [1] Gtk3 Reference Documentation - gtk_misc_set_alignment() https://developer.gnome.org/gtk3/stable/GtkMisc.html#gtk-misc-set-alignment "gtk_misc_set_alignment has been deprecated since version 3.14 and should not be used in newly-written code. Use GtkWidget's alignment ("halign" and "valign") and margin properties or GtkLabel's "xalign" and "yalign" properties." [2] Gtkmm 3.16 Gtk::Misc Class Reference, set_alignment() method https://developer.gnome.org/gtkmm/3.16/classGtk_1_1Misc.html#a52b2675874cf46a3097938756b9fe9e8 [3] GNOME BugZilla - EmptyBoxes: instructions_label's alignment is off https://bugzilla.gnome.org/show_bug.cgi?id=735841 [4] Gtk commit from 2014-09-16: GtkLabel: add x/yalign properties https://gitlab.gnome.org/GNOME/gtk/commit/d39424fc [5] Gtk3 Reference Documentation - gtk_label_set_xalign() https://developer.gnome.org/gtk3/stable/GtkLabel.html#gtk-label-set-xalign [6] Gtkmm 3.16 Gtk::Label Class Reference, set_xalign() method https://developer.gnome.org/gtkmm/3.16/classGtk_1_1Label.html#acee7d4e87d7cc14080a7b8ded5f84e5e Closes !40 - Limit wrapping labels --- configure.ac | 12 ++++++++++++ include/Utils.h | 2 +- src/Utils.cc | 10 ++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 63fd222e..58f4f3a5 100644 --- a/configure.ac +++ b/configure.ac @@ -242,6 +242,18 @@ if test "x$need_cxx_compile_stdcxx_11" = xyes; then fi +dnl Check for gtkmm >= 3.16 to determine availability of Gtk::Label::set_xalign(). +AC_MSG_CHECKING([for Gtk::Label::set_xalign() method]) +PKG_CHECK_EXISTS( + [gtkmm-3.0 >= 3.16.0], + [AC_DEFINE([HAVE_LABEL_SET_XALIGN], 1, + [Define to 1 if gtkmm provides Gtk::Label::set_xalign() method.]) + AC_MSG_RESULT([yes]) + ], + [AC_MSG_RESULT([no])] +) + + dnl Check for gtkmm >= 3.22 to determine availability of Gtk::ScrolledWindow::set_propagate_natural_width(). AC_MSG_CHECKING([for Gtk::ScrolledWindow::set_propagate_natural_width() method]) PKG_CHECK_EXISTS( diff --git a/include/Utils.h b/include/Utils.h index 2b8406ea..ab0d7b3d 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -128,7 +128,7 @@ public: , bool use_markup = true , bool wrap = false , bool selectable = false - , Gtk::Align yalign = Gtk::ALIGN_CENTER + , Gtk::Align valign = Gtk::ALIGN_CENTER ) ; static Gtk::Image* mk_image(const Gtk::StockID& stock_id, Gtk::IconSize icon_size); static Glib::RefPtr mk_pixbuf(Gtk::Widget& widget, diff --git a/src/Utils.cc b/src/Utils.cc index 09f06647..c746b15b 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -62,11 +62,17 @@ Gtk::Label * Utils::mk_label( const Glib::ustring & text , bool use_markup , bool wrap , bool selectable - , Gtk::Align yalign + , Gtk::Align valign ) { - Gtk::Label * label = manage(new Gtk::Label(text, Gtk::ALIGN_START, yalign)); + Gtk::Label *label = manage(new Gtk::Label(text)); +#if HAVE_LABEL_SET_XALIGN + label->set_xalign(0.0); +#else + label->set_alignment(0.0, 0.5); +#endif + label->set_valign(valign); label ->set_use_markup( use_markup ) ; if (wrap) {