Commit 50cbd51d authored by Matthias Clasen's avatar Matthias Clasen Committed by Matthias Clasen
Browse files

Add a max-width-chars property, which can be used to specify the width of

2004-12-13  Matthias Clasen  <mclasen@redhat.com>

	* gtk/gtklabel.[hc]: Add a max-width-chars property, which can
	be used to specify the width of the label in characters, while
	still allowing it to fall short of this length if the text
	is shorter.  (#155944, Christian Persch)

	* gtk/gtk.symbols: Add new symbols.
parent 5191e341
2004-12-13 Matthias Clasen <mclasen@redhat.com> 2004-12-13 Matthias Clasen <mclasen@redhat.com>
* gtk/gtklabel.[hc]: Add a max-width-chars property, which can
be used to specify the width of the label in characters, while
still allowing it to fall short of this length if the text
is shorter. (#155944, Christian Persch)
* gtk/gtk.symbols: Add new symbols.
* gtk/gtkuimanager.c (update_smart_separators): Don't let the * gtk/gtkuimanager.c (update_smart_separators): Don't let the
empty menu filler affect visibility of separators. (#160500, empty menu filler affect visibility of separators. (#160500,
Christian Persch) Christian Persch)
......
2004-12-13 Matthias Clasen <mclasen@redhat.com> 2004-12-13 Matthias Clasen <mclasen@redhat.com>
* gtk/gtklabel.[hc]: Add a max-width-chars property, which can
be used to specify the width of the label in characters, while
still allowing it to fall short of this length if the text
is shorter. (#155944, Christian Persch)
* gtk/gtk.symbols: Add new symbols.
* gtk/gtkuimanager.c (update_smart_separators): Don't let the * gtk/gtkuimanager.c (update_smart_separators): Don't let the
empty menu filler affect visibility of separators. (#160500, empty menu filler affect visibility of separators. (#160500,
Christian Persch) Christian Persch)
......
2004-12-13 Matthias Clasen <mclasen@redhat.com> 2004-12-13 Matthias Clasen <mclasen@redhat.com>
* gtk/gtklabel.[hc]: Add a max-width-chars property, which can
be used to specify the width of the label in characters, while
still allowing it to fall short of this length if the text
is shorter. (#155944, Christian Persch)
* gtk/gtk.symbols: Add new symbols.
* gtk/gtkuimanager.c (update_smart_separators): Don't let the * gtk/gtkuimanager.c (update_smart_separators): Don't let the
empty menu filler affect visibility of separators. (#160500, empty menu filler affect visibility of separators. (#160500,
Christian Persch) Christian Persch)
......
2004-12-13 Matthias Clasen <mclasen@redhat.com> 2004-12-13 Matthias Clasen <mclasen@redhat.com>
* gtk/gtklabel.[hc]: Add a max-width-chars property, which can
be used to specify the width of the label in characters, while
still allowing it to fall short of this length if the text
is shorter. (#155944, Christian Persch)
* gtk/gtk.symbols: Add new symbols.
* gtk/gtkuimanager.c (update_smart_separators): Don't let the * gtk/gtkuimanager.c (update_smart_separators): Don't let the
empty menu filler affect visibility of separators. (#160500, empty menu filler affect visibility of separators. (#160500,
Christian Persch) Christian Persch)
......
2004-12-13 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Add new label methods.
2004-12-09 Matthias Clasen <mclasen@redhat.com> 2004-12-09 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Add new list store methods. * gtk/gtk-sections.txt: Add new list store methods.
......
...@@ -1908,6 +1908,7 @@ gtk_label_set_pattern ...@@ -1908,6 +1908,7 @@ gtk_label_set_pattern
gtk_label_set_justify gtk_label_set_justify
gtk_label_set_ellipsize gtk_label_set_ellipsize
gtk_label_set_width_chars gtk_label_set_width_chars
gtk_label_set_max_width_chars
gtk_label_get gtk_label_get
gtk_label_parse_uline gtk_label_parse_uline
gtk_label_set_line_wrap gtk_label_set_line_wrap
...@@ -1925,6 +1926,7 @@ gtk_label_get_attributes ...@@ -1925,6 +1926,7 @@ gtk_label_get_attributes
gtk_label_get_justify gtk_label_get_justify
gtk_label_get_ellipsize gtk_label_get_ellipsize
gtk_label_get_width_chars gtk_label_get_width_chars
gtk_label_get_max_width_chars
gtk_label_get_label gtk_label_get_label
gtk_label_get_layout gtk_label_get_layout
gtk_label_get_line_wrap gtk_label_get_line_wrap
......
...@@ -1307,6 +1307,7 @@ gtk_label_get_label ...@@ -1307,6 +1307,7 @@ gtk_label_get_label
gtk_label_get_layout gtk_label_get_layout
gtk_label_get_layout_offsets gtk_label_get_layout_offsets
gtk_label_get_line_wrap gtk_label_get_line_wrap
gtk_label_get_max_width_chars
gtk_label_get_mnemonic_keyval gtk_label_get_mnemonic_keyval
gtk_label_get_mnemonic_widget gtk_label_get_mnemonic_widget
gtk_label_get_selectable gtk_label_get_selectable
...@@ -1329,6 +1330,7 @@ gtk_label_set_label ...@@ -1329,6 +1330,7 @@ gtk_label_set_label
gtk_label_set_line_wrap gtk_label_set_line_wrap
gtk_label_set_markup gtk_label_set_markup
gtk_label_set_markup_with_mnemonic gtk_label_set_markup_with_mnemonic
gtk_label_set_max_width_chars
gtk_label_set_mnemonic_widget gtk_label_set_mnemonic_widget
gtk_label_set_pattern gtk_label_set_pattern
gtk_label_set_selectable gtk_label_set_selectable
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
typedef struct typedef struct
{ {
gint width_chars; gint width_chars;
gint max_width_chars;
guint single_line_mode : 1; guint single_line_mode : 1;
guint have_transform : 1; guint have_transform : 1;
gdouble angle; gdouble angle;
...@@ -91,7 +92,8 @@ enum { ...@@ -91,7 +92,8 @@ enum {
PROP_ELLIPSIZE, PROP_ELLIPSIZE,
PROP_WIDTH_CHARS, PROP_WIDTH_CHARS,
PROP_SINGLE_LINE_MODE, PROP_SINGLE_LINE_MODE,
PROP_ANGLE PROP_ANGLE,
PROP_MAX_WIDTH_CHARS
}; };
static guint signals[LAST_SIGNAL] = { 0 }; static guint signals[LAST_SIGNAL] = { 0 };
...@@ -440,7 +442,9 @@ gtk_label_class_init (GtkLabelClass *class) ...@@ -440,7 +442,9 @@ gtk_label_class_init (GtkLabelClass *class)
* The desired width of the label, in characters. If this property is set to * The desired width of the label, in characters. If this property is set to
* -1, the width will be calculated automatically, otherwise the label will * -1, the width will be calculated automatically, otherwise the label will
* request either 3 characters or the property value, whichever is greater. * request either 3 characters or the property value, whichever is greater.
* * If the width-chars property is set to a positive value, then the
* max-width-chars property is ignored.
*
* Since: 2.6 * Since: 2.6
**/ **/
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
...@@ -492,6 +496,26 @@ gtk_label_class_init (GtkLabelClass *class) ...@@ -492,6 +496,26 @@ gtk_label_class_init (GtkLabelClass *class)
0.0, 0.0,
G_PARAM_READWRITE)); G_PARAM_READWRITE));
/**
* GtkLabel:max-width-chars:
*
* The desired width maximal of the label, in characters. If this property
* is set to -1, the width will be calculated automatically, otherwise the
* label will request space for no more than the requested number of
* characters. If the width-chars property is set to a positive value,
* then the max-width-chars property is ignored.
*
* Since: 2.6
**/
g_object_class_install_property (gobject_class,
PROP_MAX_WIDTH_CHARS,
g_param_spec_int ("max_width_chars",
P_("Maximal Width In Characters"),
P_("The desired maximal width of the label, in characters"),
-1,
G_MAXINT,
-1,
G_PARAM_READWRITE));
/* /*
* Key bindings * Key bindings
*/ */
...@@ -622,7 +646,10 @@ gtk_label_set_property (GObject *object, ...@@ -622,7 +646,10 @@ gtk_label_set_property (GObject *object,
break; break;
case PROP_ANGLE: case PROP_ANGLE:
gtk_label_set_angle (label, g_value_get_double (value)); gtk_label_set_angle (label, g_value_get_double (value));
break; break;
case PROP_MAX_WIDTH_CHARS:
gtk_label_set_max_width_chars (label, g_value_get_int (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
...@@ -692,7 +719,7 @@ gtk_label_get_property (GObject *object, ...@@ -692,7 +719,7 @@ gtk_label_get_property (GObject *object,
g_value_set_enum (value, label->ellipsize); g_value_set_enum (value, label->ellipsize);
break; break;
case PROP_WIDTH_CHARS: case PROP_WIDTH_CHARS:
g_value_set_enum (value, gtk_label_get_width_chars (label)); g_value_set_int (value, gtk_label_get_width_chars (label));
break; break;
case PROP_SINGLE_LINE_MODE: case PROP_SINGLE_LINE_MODE:
g_value_set_boolean (value, gtk_label_get_single_line_mode (label)); g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
...@@ -700,6 +727,9 @@ gtk_label_get_property (GObject *object, ...@@ -700,6 +727,9 @@ gtk_label_get_property (GObject *object,
case PROP_ANGLE: case PROP_ANGLE:
g_value_set_double (value, gtk_label_get_angle (label)); g_value_set_double (value, gtk_label_get_angle (label));
break; break;
case PROP_MAX_WIDTH_CHARS:
g_value_set_int (value, gtk_label_get_max_width_chars (label));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
...@@ -717,6 +747,7 @@ gtk_label_init (GtkLabel *label) ...@@ -717,6 +747,7 @@ gtk_label_init (GtkLabel *label)
priv = GTK_LABEL_GET_PRIVATE (label); priv = GTK_LABEL_GET_PRIVATE (label);
priv->width_chars = -1; priv->width_chars = -1;
priv->angle = 0.0; priv->angle = 0.0;
priv->max_width_chars = -1;
label->label = NULL; label->label = NULL;
label->jtype = GTK_JUSTIFY_LEFT; label->jtype = GTK_JUSTIFY_LEFT;
...@@ -1452,7 +1483,6 @@ gtk_label_get_justify (GtkLabel *label) ...@@ -1452,7 +1483,6 @@ gtk_label_get_justify (GtkLabel *label)
return label->jtype; return label->jtype;
} }
/** /**
* gtk_label_set_ellipsize: * gtk_label_set_ellipsize:
* @label: a #GtkLabel * @label: a #GtkLabel
...@@ -1534,7 +1564,7 @@ gtk_label_set_width_chars (GtkLabel *label, ...@@ -1534,7 +1564,7 @@ gtk_label_set_width_chars (GtkLabel *label,
* Retrieves the desired width of @label, in characters. See * Retrieves the desired width of @label, in characters. See
* gtk_label_set_width_chars(). * gtk_label_set_width_chars().
* *
* Return value: the width of a label in characters. * Return value: the width of the label in characters.
* *
* Since: 2.6 * Since: 2.6
**/ **/
...@@ -1546,6 +1576,53 @@ gtk_label_get_width_chars (GtkLabel *label) ...@@ -1546,6 +1576,53 @@ gtk_label_get_width_chars (GtkLabel *label)
return GTK_LABEL_GET_PRIVATE (label)->width_chars; return GTK_LABEL_GET_PRIVATE (label)->width_chars;
} }
/**
* gtk_label_set_max_width_chars:
* @label: a #GtkLabel
* @n_chars: the new desired maximal width, in characters.
*
* Sets the desired maximal width in characters of @label to @n_chars.
*
* Since: 2.6
**/
void
gtk_label_set_max_width_chars (GtkLabel *label,
gint n_chars)
{
GtkLabelPrivate *priv;
g_return_if_fail (GTK_IS_LABEL (label));
priv = GTK_LABEL_GET_PRIVATE (label);
if (priv->max_width_chars != n_chars)
{
priv->max_width_chars = n_chars;
g_object_notify (G_OBJECT (label), "max-width-chars");
gtk_widget_queue_resize (GTK_WIDGET (label));
}
}
/**
* gtk_label_get_max_width_chars:
* @label: a #GtkLabel
*
* Retrieves the desired maximal width of @label, in characters. See
* gtk_label_set_width_chars().
*
* Return value: the maximal width of the label in characters.
*
* Since: 2.6
**/
gint
gtk_label_get_max_width_chars (GtkLabel *label)
{
g_return_val_if_fail (GTK_IS_LABEL (label), -1);
return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
}
/** /**
* gtk_label_set_line_wrap: * gtk_label_set_line_wrap:
* @label: a #GtkLabel * @label: a #GtkLabel
...@@ -1934,29 +2011,45 @@ gtk_label_size_request (GtkWidget *widget, ...@@ -1934,29 +2011,45 @@ gtk_label_size_request (GtkWidget *widget,
} }
else else
pango_layout_get_extents (label->layout, NULL, &logical_rect); pango_layout_get_extents (label->layout, NULL, &logical_rect);
if (label->ellipsize || priv->width_chars > 0) if ((label->wrap || label->ellipsize ||
priv->width_chars > 0 || priv->max_width_chars > 0) &&
aux_info && aux_info->width > 0)
width += aux_info->width;
else if (label->ellipsize || priv->width_chars > 0 || priv->width_chars > 0)
{ {
PangoContext *context; PangoContext *context;
PangoFontMetrics *metrics; PangoFontMetrics *metrics;
gint char_width; gint char_width, digit_width, char_pixels, w;
/* The minimum size for ellipsized labels is ~ 3 chars */
context = pango_layout_get_context (label->layout); context = pango_layout_get_context (label->layout);
metrics = pango_context_get_metrics (context, widget->style->font_desc, NULL); metrics = pango_context_get_metrics (context, widget->style->font_desc, NULL);
char_width = pango_font_metrics_get_approximate_char_width (metrics); char_width = pango_font_metrics_get_approximate_char_width (metrics);
digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
char_pixels = MAX (char_width, digit_width);
pango_font_metrics_unref (metrics); pango_font_metrics_unref (metrics);
width += (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3)); if (priv->width_chars < 0)
} {
else PangoRectangle rect;
{
if (label->wrap && aux_info && aux_info->width > 0) pango_layout_set_width (label->layout, -1);
width += aux_info->width; pango_layout_get_extents (label->layout, NULL, &rect);
w = char_pixels * MAX (priv->max_width_chars, 3);
w = MIN (rect.width, w);
}
else else
width += PANGO_PIXELS (logical_rect.width); {
/* enforce minimum width for ellipsized labels at ~3 chars */
w = char_pixels * MAX (priv->width_chars, 3);
}
width += PANGO_PIXELS (w);
} }
else
width += PANGO_PIXELS (logical_rect.width);
if (priv->single_line_mode) if (priv->single_line_mode)
{ {
......
...@@ -135,6 +135,9 @@ PangoEllipsizeMode gtk_label_get_ellipsize (GtkLabel *label); ...@@ -135,6 +135,9 @@ PangoEllipsizeMode gtk_label_get_ellipsize (GtkLabel *label);
void gtk_label_set_width_chars (GtkLabel *label, void gtk_label_set_width_chars (GtkLabel *label,
gint n_chars); gint n_chars);
gint gtk_label_get_width_chars (GtkLabel *label); gint gtk_label_get_width_chars (GtkLabel *label);
void gtk_label_set_max_width_chars (GtkLabel *label,
gint n_chars);
gint gtk_label_get_max_width_chars (GtkLabel *label);
void gtk_label_set_pattern (GtkLabel *label, void gtk_label_set_pattern (GtkLabel *label,
const gchar *pattern); const gchar *pattern);
void gtk_label_set_line_wrap (GtkLabel *label, void gtk_label_set_line_wrap (GtkLabel *label,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment