+static void
+gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
+ GtkWidget *widget,
+ gint *minimum_size,
+ gint *natural_size)
+{
+ GtkCellRendererTextPrivate *priv;
+ GtkCellRendererText *celltext;
+ PangoLayout *layout;
+ PangoContext *context;
+ PangoFontMetrics *metrics;
+ PangoRectangle rect;
+ gint char_width, text_width, ellipsize_chars, xpad;
+ gint min_width, nat_width;
+
+ /* "width-chars" Hard-coded minimum width:
+ * - minimum size should be MAX (width-chars, strlen ("..."));
+ * - natural size should be MAX (width-chars, strlen (label->text));
+ *
+ * "wrap-width" User specified natural wrap width
+ * - minimum size should be MAX (width-chars, 0)
+ * - natural size should be MIN (wrap-width, strlen (label->text))
+ */
+
+ celltext = GTK_CELL_RENDERER_TEXT (cell);
+ priv = celltext->priv;
+
+ gtk_cell_renderer_get_padding (cell, &xpad, NULL);
+
+ layout = get_layout (celltext, widget, NULL, 0);
+
+ /* Fetch the length of the complete unwrapped text */
+ pango_layout_set_width (layout, -1);
+ pango_layout_get_extents (layout, NULL, &rect);
+ text_width = rect.width;
+
+ /* Fetch the average size of a charachter */
+ context = pango_layout_get_context (layout);
+ metrics = pango_context_get_metrics (context,
+ pango_context_get_font_description (context),
+ pango_context_get_language (context));
+
+ char_width = pango_font_metrics_get_approximate_char_width (metrics);
+
+ pango_font_metrics_unref (metrics);
+ g_object_unref (layout);
+
+ /* enforce minimum width for ellipsized labels at ~3 chars */
+ if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
+ ellipsize_chars = 3;
+ else
+ ellipsize_chars = 0;
+
+ if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
+ min_width = xpad * 2 +
+ MIN (PANGO_PIXELS_CEIL (text_width),
+ (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
+ /* If no width-chars set, minimum for wrapping text will be the wrap-width */
+ else if (priv->wrap_width > -1)
+ min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
+ else
+ min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
+
+ if (priv->width_chars > 0)
+ nat_width = xpad * 2 +
+ MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
+ else
+ nat_width = xpad * 2 + PANGO_PIXELS_CEIL (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)
+ *minimum_size = min_width;
+
+ if (natural_size)
+ *natural_size = nat_width;
+}
+
+static void
+gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
+ GtkWidget *widget,
+ gint width,
+ gint *minimum_height,
+ gint *natural_height)
+{
+ GtkCellRendererText *celltext;
+ PangoLayout *layout;
+ gint text_height, xpad, ypad;
+
+
+ celltext = GTK_CELL_RENDERER_TEXT (cell);
+
+ gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
+
+ layout = get_layout (celltext, widget, NULL, 0);
+
+ pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
+ pango_layout_get_pixel_size (layout, NULL, &text_height);
+
+ if (minimum_height)
+ *minimum_height = text_height + ypad * 2;
+
+ if (natural_height)
+ *natural_height = text_height + ypad * 2;
+
+ g_object_unref (layout);
+}
+
+static void
+gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
+ GtkWidget *widget,
+ gint *minimum_size,
+ gint *natural_size)
+{
+ gint min_width;
+
+ /* Thankfully cell renderers dont rotate, so they only have to do
+ * height-for-width and not the opposite. Here we have only to return
+ * the height for the base minimum width of the renderer.
+ *
+ * Note this code path wont be followed by GtkTreeView which is
+ * height-for-width specifically.
+ */
+ gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
+ gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
+ minimum_size, natural_size);
+}
+
+static void
+gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
+ GtkWidget *widget,
+ GtkCellRendererState flags,
+ const GdkRectangle *cell_area,
+ GdkRectangle *aligned_area)
+{
+ GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
+ PangoLayout *layout;
+ gint x_offset = 0;
+ gint y_offset = 0;
+
+ layout = get_layout (celltext, widget, cell_area, flags);
+ get_size (cell, widget, cell_area, layout, &x_offset, &y_offset,
+ &aligned_area->width, &aligned_area->height);
+
+ aligned_area->x = cell_area->x + x_offset;
+ aligned_area->y = cell_area->y + y_offset;
+
+ g_object_unref (layout);
+}