From eab0ff8e4ed209275541781e8ff6a7a6a557092c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 22 Aug 2013 20:29:22 -0400 Subject: [PATCH] Add api to ellipsize labels to multiple lines When setting the lines property, the label will be ellipsized to that many lines, with the ellipsis only appearing in the last line. This is different from how ellipsization of multi-line labels normally works in GTK+. --- docs/reference/gtk/gtk3-sections.txt | 2 + gtk/gtklabel.c | 81 +++++++++++++++++++++++++++- gtk/gtklabel.h | 5 ++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt index 73d2b7fe0c..12e0825444 100644 --- a/docs/reference/gtk/gtk3-sections.txt +++ b/docs/reference/gtk/gtk3-sections.txt @@ -2055,6 +2055,7 @@ gtk_label_set_width_chars gtk_label_set_max_width_chars gtk_label_set_line_wrap gtk_label_set_line_wrap_mode +gtk_label_set_lines gtk_label_get_layout_offsets gtk_label_get_mnemonic_keyval gtk_label_get_selectable @@ -2073,6 +2074,7 @@ gtk_label_get_label gtk_label_get_layout gtk_label_get_line_wrap gtk_label_get_line_wrap_mode +gtk_label_get_lines gtk_label_get_mnemonic_widget gtk_label_get_selection_bounds gtk_label_get_use_markup diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c index b268c4e92a..0a1b32c8de 100644 --- a/gtk/gtklabel.c +++ b/gtk/gtklabel.c @@ -265,6 +265,7 @@ struct _GtkLabelPrivate gint width_chars; gint max_width_chars; + gint lines; }; /* Notes about the handling of links: @@ -355,7 +356,8 @@ enum { PROP_SINGLE_LINE_MODE, PROP_ANGLE, PROP_MAX_WIDTH_CHARS, - PROP_TRACK_VISITED_LINKS + PROP_TRACK_VISITED_LINKS, + PROP_LINES }; /* When rotating ellipsizable text we want the natural size to request @@ -961,6 +963,26 @@ gtk_label_class_init (GtkLabelClass *class) P_("Whether visited links should be tracked"), TRUE, GTK_PARAM_READWRITE)); + + /** + * GtkLabel:lines: + * + * The number of lines to which an ellipsized, wrapping label + * should be limited. This property has no effect if the + * label is not wrapping or ellipsized. Set this property to + * -1 if you don't want to limit the number of lines. + * + * Since: 3.10 + */ + g_object_class_install_property (gobject_class, + PROP_LINES, + g_param_spec_int ("lines", + P_("Number of lines"), + P_("The desired number of lines, when ellipsizing a wrapping label"), + -1, + G_MAXINT, + -1, + GTK_PARAM_READWRITE)); /* * Key bindings */ @@ -1138,6 +1160,9 @@ gtk_label_set_property (GObject *object, case PROP_TRACK_VISITED_LINKS: gtk_label_set_track_visited_links (label, g_value_get_boolean (value)); break; + case PROP_LINES: + gtk_label_set_lines (label, g_value_get_int (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -1209,6 +1234,9 @@ gtk_label_get_property (GObject *object, case PROP_TRACK_VISITED_LINKS: g_value_set_boolean (value, gtk_label_get_track_visited_links (label)); break; + case PROP_LINES: + g_value_set_int (value, gtk_label_get_lines (label)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -3432,6 +3460,8 @@ gtk_label_ensure_layout (GtkLabel *label) pango_layout_set_ellipsize (priv->layout, priv->ellipsize); pango_layout_set_wrap (priv->layout, priv->wrap_mode); pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode); + if (priv->lines > 0) + pango_layout_set_height (priv->layout, - priv->lines); gtk_label_update_layout_width (label); } @@ -6533,3 +6563,52 @@ _gtk_label_get_selection_bound (GtkLabel *label) return 0; } + +/** + * gtk_label_set_lines: + * @label: a #GtkLabel + * @lines: the desired number of lines, or -1 + * + * Sets the number of lines to which an ellipsized, wrapping label + * should be limited. This has no effect if the label is not wrapping + * or ellipsized. Set this to -1 if you don't want to limit the + * number of lines. + * + * Since: 3.10 + */ +void +gtk_label_set_lines (GtkLabel *label, + gint lines) +{ + GtkLabelPrivate *priv; + + g_return_if_fail (GTK_IS_LABEL (label)); + + priv = label->priv; + + if (priv->lines != lines) + { + priv->lines = lines; + g_object_notify (G_OBJECT (label), "lines"); + gtk_widget_queue_resize (GTK_WIDGET (label)); + } +} + +/** + * gtk_label_get_lines: + * @label: a #GtkLabel + * + * Gets the number of lines to which an ellipsized, wrapping + * label should be limited. See gtk_label_set_lines(). + * + * Returns: The number of lines + * + * Since: 3.10 + */ +gint +gtk_label_get_lines (GtkLabel *label) +{ + g_return_if_fail (GTK_IS_LABEL (label)); + + return label->priv->lines; +} diff --git a/gtk/gtklabel.h b/gtk/gtklabel.h index 1e5cfe976f..eecb8e8cb9 100644 --- a/gtk/gtklabel.h +++ b/gtk/gtklabel.h @@ -155,6 +155,11 @@ void gtk_label_set_max_width_chars (GtkLabel *label, gint n_chars); GDK_AVAILABLE_IN_ALL gint gtk_label_get_max_width_chars (GtkLabel *label); +GDK_AVAILABLE_IN_3_10 +void gtk_label_set_lines (GtkLabel *label, + gint lines); +GDK_AVAILABLE_IN_3_10 +gint gtk_label_get_lines (GtkLabel *label); GDK_AVAILABLE_IN_ALL void gtk_label_set_pattern (GtkLabel *label, const gchar *pattern);