Added GtkCellRendererText::max-width-chars property

Added a property to limit the minimum/natural size request
of a text cell renderer.
This commit is contained in:
Tristan Van Berkom 2010-08-20 19:23:00 -04:00
parent f7a6c05158
commit af7a187919

View File

@ -83,6 +83,7 @@ enum {
PROP_ATTRIBUTES, PROP_ATTRIBUTES,
PROP_SINGLE_PARAGRAPH_MODE, PROP_SINGLE_PARAGRAPH_MODE,
PROP_WIDTH_CHARS, PROP_WIDTH_CHARS,
PROP_MAX_WIDTH_CHARS,
PROP_WRAP_WIDTH, PROP_WRAP_WIDTH,
PROP_ALIGN, PROP_ALIGN,
@ -155,6 +156,7 @@ struct _GtkCellRendererTextPriv
gint rise; gint rise;
gint fixed_height_rows; gint fixed_height_rows;
gint width_chars; gint width_chars;
gint max_width_chars;
gint wrap_width; gint wrap_width;
guint strikethrough : 1; guint strikethrough : 1;
@ -200,6 +202,7 @@ gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
priv->font = pango_font_description_new (); priv->font = pango_font_description_new ();
priv->width_chars = -1; priv->width_chars = -1;
priv->max_width_chars = -1;
priv->wrap_width = -1; priv->wrap_width = -1;
priv->wrap_mode = PANGO_WRAP_CHAR; priv->wrap_mode = PANGO_WRAP_CHAR;
priv->align = PANGO_ALIGN_LEFT; priv->align = PANGO_ALIGN_LEFT;
@ -466,6 +469,31 @@ gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
-1, -1,
GTK_PARAM_READWRITE)); GTK_PARAM_READWRITE));
/**
* GtkCellRendererText:max-width-chars:
*
* The desired maximum width of the cell, in characters. If this property
* is set to -1, the width will be calculated automatically.
*
* For cell renderers that ellipsize or wrap text; this property
* controls the maximum reported width of the cell. The
* cell should not receive any greater allocation unless it is
* set to expand in its #GtkCellLayout and all of the cell's siblings
* have received their natural width.
*
* Since: 3.0
**/
g_object_class_install_property (object_class,
PROP_MAX_WIDTH_CHARS,
g_param_spec_int ("max-width-chars",
P_("Maximum Width In Characters"),
P_("The maximum width of the cell, in characters"),
-1,
G_MAXINT,
-1,
GTK_PARAM_READWRITE));
/** /**
* GtkCellRendererText:wrap-mode: * GtkCellRendererText:wrap-mode:
* *
@ -525,6 +553,8 @@ gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
PANGO_ALIGN_LEFT, PANGO_ALIGN_LEFT,
GTK_PARAM_READWRITE)); GTK_PARAM_READWRITE));
/* Style props are set or not */ /* Style props are set or not */
#define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE)) #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE))
@ -839,6 +869,10 @@ gtk_cell_renderer_text_get_property (GObject *object,
g_value_set_int (value, priv->width_chars); g_value_set_int (value, priv->width_chars);
break; break;
case PROP_MAX_WIDTH_CHARS:
g_value_set_int (value, priv->max_width_chars);
break;
case PROP_BACKGROUND: case PROP_BACKGROUND:
case PROP_FOREGROUND: case PROP_FOREGROUND:
case PROP_MARKUP: case PROP_MARKUP:
@ -1287,6 +1321,10 @@ gtk_cell_renderer_text_set_property (GObject *object,
priv->width_chars = g_value_get_int (value); priv->width_chars = g_value_get_int (value);
break; break;
case PROP_MAX_WIDTH_CHARS:
priv->max_width_chars = g_value_get_int (value);
break;
case PROP_ALIGN: case PROP_ALIGN:
priv->align = g_value_get_enum (value); priv->align = g_value_get_enum (value);
priv->align_set = TRUE; priv->align_set = TRUE;
@ -2001,7 +2039,7 @@ gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
PangoFontMetrics *metrics; PangoFontMetrics *metrics;
PangoRectangle rect; PangoRectangle rect;
gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad; gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
gint min_width; gint min_width, nat_width;
/* "width-chars" Hard-coded minimum width: /* "width-chars" Hard-coded minimum width:
* - minimum size should be MAX (width-chars, strlen ("...")); * - minimum size should be MAX (width-chars, strlen ("..."));
@ -2055,17 +2093,28 @@ gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
else else
min_width = xpad * 2 + rect.x + guess_width; min_width = xpad * 2 + rect.x + guess_width;
if (priv->width_chars > 0)
nat_width = xpad * 2 +
MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
else
nat_width = xpad * 2 + PANGO_PIXELS (text_width);
nat_width = MAX (nat_width, min_width);
if (priv->max_width_chars > 0)
{
gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
min_width = MIN (min_width, max_width);
nat_width = MIN (nat_width, max_width);
}
if (minimum_size) if (minimum_size)
*minimum_size = min_width; *minimum_size = min_width;
if (natural_size) if (natural_size)
{ *natural_size = nat_width;
/* Control the max wrap width here possibly (add max-width-chars ?) */
*natural_size = xpad * 2 +
MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
*natural_size = MAX (*natural_size, min_width);
}
} }
static void static void