1 /* gtkcellrenderertext.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkcellrenderertext.h"
24 #include "gtkeditable.h"
26 #include "gtksizerequest.h"
27 #include "gtkmarshalers.h"
29 #include "gtkprivate.h"
30 #include "gtktreeprivate.h"
31 #include "a11y/gtktextcellaccessible.h"
35 * SECTION:gtkcellrenderertext
36 * @Short_description: Renders text in a cell
37 * @Title: GtkCellRendererText
39 * A #GtkCellRendererText renders a given text in its cell, using the font, color and
40 * style information provided by its properties. The text will be ellipsized if it is
41 * too long and the #GtkCellRendererText:ellipsize property allows it.
43 * If the #GtkCellRenderer:mode is %GTK_CELL_RENDERER_MODE_EDITABLE,
44 * the #GtkCellRendererText allows to edit its text using an entry.
48 static void gtk_cell_renderer_text_finalize (GObject *object);
50 static void gtk_cell_renderer_text_get_property (GObject *object,
54 static void gtk_cell_renderer_text_set_property (GObject *object,
58 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
61 const GdkRectangle *background_area,
62 const GdkRectangle *cell_area,
63 GtkCellRendererState flags);
65 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
69 const GdkRectangle *background_area,
70 const GdkRectangle *cell_area,
71 GtkCellRendererState flags);
73 static void gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
77 static void gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
81 static void gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
85 gint *natural_height);
86 static void gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
88 GtkCellRendererState flags,
89 const GdkRectangle *cell_area,
90 GdkRectangle *aligned_area);
105 PROP_SINGLE_PARAGRAPH_MODE,
107 PROP_MAX_WIDTH_CHARS,
110 PROP_PLACEHOLDER_TEXT,
117 PROP_BACKGROUND_RGBA,
118 PROP_FOREGROUND_RGBA,
137 /* Whether-a-style-arg-is-set args */
148 PROP_STRIKETHROUGH_SET,
156 static guint text_cell_renderer_signals [LAST_SIGNAL];
158 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
160 struct _GtkCellRendererTextPrivate
164 PangoAttrList *extra_attrs;
167 PangoAlignment align;
168 PangoEllipsizeMode ellipsize;
169 PangoFontDescription *font;
170 PangoLanguage *language;
171 PangoUnderline underline_style;
172 PangoWrapMode wrap_mode;
175 gchar *placeholder_text;
180 gint fixed_height_rows;
182 gint max_width_chars;
185 guint in_entry_menu : 1;
186 guint strikethrough : 1;
189 guint foreground_set : 1;
190 guint background_set : 1;
191 guint underline_set : 1;
193 guint strikethrough_set : 1;
194 guint editable_set : 1;
195 guint calc_fixed_height : 1;
196 guint single_paragraph : 1;
197 guint language_set : 1;
198 guint markup_set : 1;
199 guint ellipsize_set : 1;
203 gulong populate_popup_id;
204 gulong entry_menu_popdown_timeout;
207 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
210 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
212 GtkCellRendererTextPrivate *priv;
213 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
215 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
216 GTK_TYPE_CELL_RENDERER_TEXT,
217 GtkCellRendererTextPrivate);
218 priv = celltext->priv;
220 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
221 gtk_cell_renderer_set_padding (cell, 2, 2);
222 priv->font_scale = 1.0;
223 priv->fixed_height_rows = -1;
224 priv->font = pango_font_description_new ();
226 priv->width_chars = -1;
227 priv->max_width_chars = -1;
228 priv->wrap_width = -1;
229 priv->wrap_mode = PANGO_WRAP_CHAR;
230 priv->align = PANGO_ALIGN_LEFT;
231 priv->align_set = FALSE;
235 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
237 GObjectClass *object_class = G_OBJECT_CLASS (class);
238 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
240 object_class->finalize = gtk_cell_renderer_text_finalize;
242 object_class->get_property = gtk_cell_renderer_text_get_property;
243 object_class->set_property = gtk_cell_renderer_text_set_property;
245 cell_class->render = gtk_cell_renderer_text_render;
246 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
247 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
248 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
249 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
250 cell_class->get_aligned_area = gtk_cell_renderer_text_get_aligned_area;
252 g_object_class_install_property (object_class,
254 g_param_spec_string ("text",
256 P_("Text to render"),
258 GTK_PARAM_READWRITE));
260 g_object_class_install_property (object_class,
262 g_param_spec_string ("markup",
264 P_("Marked up text to render"),
266 GTK_PARAM_WRITABLE));
268 g_object_class_install_property (object_class,
270 g_param_spec_boxed ("attributes",
272 P_("A list of style attributes to apply to the text of the renderer"),
273 PANGO_TYPE_ATTR_LIST,
274 GTK_PARAM_READWRITE));
276 g_object_class_install_property (object_class,
277 PROP_SINGLE_PARAGRAPH_MODE,
278 g_param_spec_boolean ("single-paragraph-mode",
279 P_("Single Paragraph Mode"),
280 P_("Whether to keep all text in a single paragraph"),
282 GTK_PARAM_READWRITE));
285 g_object_class_install_property (object_class,
287 g_param_spec_string ("background",
288 P_("Background color name"),
289 P_("Background color as a string"),
291 GTK_PARAM_WRITABLE));
294 * GtkCellRendererText:background-gdk:
296 * Background color as a #GdkColor
298 * Deprecated: 3.4: Use #GtkCellRendererText:background-rgba instead.
300 g_object_class_install_property (object_class,
302 g_param_spec_boxed ("background-gdk",
303 P_("Background color"),
304 P_("Background color as a GdkColor"),
306 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
309 * GtkCellRendererText:background-rgba:
311 * Background color as a #GdkRGBA
315 g_object_class_install_property (object_class,
316 PROP_BACKGROUND_RGBA,
317 g_param_spec_boxed ("background-rgba",
318 P_("Background color as RGBA"),
319 P_("Background color as a GdkRGBA"),
321 GTK_PARAM_READWRITE));
322 g_object_class_install_property (object_class,
324 g_param_spec_string ("foreground",
325 P_("Foreground color name"),
326 P_("Foreground color as a string"),
328 GTK_PARAM_WRITABLE));
331 * GtkCellRendererText:foreground-gdk:
333 * Foreground color as a #GdkColor
335 * Deprecated: 3.4: Use #GtkCellRendererText:foreground-rgba instead.
337 g_object_class_install_property (object_class,
339 g_param_spec_boxed ("foreground-gdk",
340 P_("Foreground color"),
341 P_("Foreground color as a GdkColor"),
343 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
346 * GtkCellRendererText:foreground-rgba:
348 * Foreground color as a #GdkRGBA
352 g_object_class_install_property (object_class,
353 PROP_FOREGROUND_RGBA,
354 g_param_spec_boxed ("foreground-rgba",
355 P_("Foreground color as RGBA"),
356 P_("Foreground color as a GdkRGBA"),
358 GTK_PARAM_READWRITE));
361 g_object_class_install_property (object_class,
363 g_param_spec_boolean ("editable",
365 P_("Whether the text can be modified by the user"),
367 GTK_PARAM_READWRITE));
369 g_object_class_install_property (object_class,
371 g_param_spec_string ("font",
373 P_("Font description as a string, e.g. \"Sans Italic 12\""),
375 GTK_PARAM_READWRITE));
377 g_object_class_install_property (object_class,
379 g_param_spec_boxed ("font-desc",
381 P_("Font description as a PangoFontDescription struct"),
382 PANGO_TYPE_FONT_DESCRIPTION,
383 GTK_PARAM_READWRITE));
386 g_object_class_install_property (object_class,
388 g_param_spec_string ("family",
390 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
392 GTK_PARAM_READWRITE));
394 g_object_class_install_property (object_class,
396 g_param_spec_enum ("style",
401 GTK_PARAM_READWRITE));
403 g_object_class_install_property (object_class,
405 g_param_spec_enum ("variant",
409 PANGO_VARIANT_NORMAL,
410 GTK_PARAM_READWRITE));
412 g_object_class_install_property (object_class,
414 g_param_spec_int ("weight",
420 GTK_PARAM_READWRITE));
422 g_object_class_install_property (object_class,
424 g_param_spec_enum ("stretch",
428 PANGO_STRETCH_NORMAL,
429 GTK_PARAM_READWRITE));
431 g_object_class_install_property (object_class,
433 g_param_spec_int ("size",
439 GTK_PARAM_READWRITE));
441 g_object_class_install_property (object_class,
443 g_param_spec_double ("size-points",
445 P_("Font size in points"),
449 GTK_PARAM_READWRITE));
451 g_object_class_install_property (object_class,
453 g_param_spec_double ("scale",
455 P_("Font scaling factor"),
459 GTK_PARAM_READWRITE));
461 g_object_class_install_property (object_class,
463 g_param_spec_int ("rise",
465 P_("Offset of text above the baseline "
466 "(below the baseline if rise is negative)"),
470 GTK_PARAM_READWRITE));
473 g_object_class_install_property (object_class,
475 g_param_spec_boolean ("strikethrough",
477 P_("Whether to strike through the text"),
479 GTK_PARAM_READWRITE));
481 g_object_class_install_property (object_class,
483 g_param_spec_enum ("underline",
485 P_("Style of underline for this text"),
486 PANGO_TYPE_UNDERLINE,
487 PANGO_UNDERLINE_NONE,
488 GTK_PARAM_READWRITE));
490 g_object_class_install_property (object_class,
492 g_param_spec_string ("language",
494 P_("The language this text is in, as an ISO code. "
495 "Pango can use this as a hint when rendering the text. "
496 "If you don't understand this parameter, you probably don't need it"),
498 GTK_PARAM_READWRITE));
502 * GtkCellRendererText:ellipsize:
504 * Specifies the preferred place to ellipsize the string, if the cell renderer
505 * does not have enough room to display the entire string. Setting it to
506 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
507 * for another way of making the text fit into a given width.
511 g_object_class_install_property (object_class,
513 g_param_spec_enum ("ellipsize",
515 P_("The preferred place to ellipsize the string, "
516 "if the cell renderer does not have enough room "
517 "to display the entire string"),
518 PANGO_TYPE_ELLIPSIZE_MODE,
519 PANGO_ELLIPSIZE_NONE,
520 GTK_PARAM_READWRITE));
523 * GtkCellRendererText:width-chars:
525 * The desired width of the cell, in characters. If this property is set to
526 * -1, the width will be calculated automatically, otherwise the cell will
527 * request either 3 characters or the property value, whichever is greater.
531 g_object_class_install_property (object_class,
533 g_param_spec_int ("width-chars",
534 P_("Width In Characters"),
535 P_("The desired width of the label, in characters"),
539 GTK_PARAM_READWRITE));
543 * GtkCellRendererText:max-width-chars:
545 * The desired maximum width of the cell, in characters. If this property
546 * is set to -1, the width will be calculated automatically.
548 * For cell renderers that ellipsize or wrap text; this property
549 * controls the maximum reported width of the cell. The
550 * cell should not receive any greater allocation unless it is
551 * set to expand in its #GtkCellLayout and all of the cell's siblings
552 * have received their natural width.
556 g_object_class_install_property (object_class,
557 PROP_MAX_WIDTH_CHARS,
558 g_param_spec_int ("max-width-chars",
559 P_("Maximum Width In Characters"),
560 P_("The maximum width of the cell, in characters"),
564 GTK_PARAM_READWRITE));
567 * GtkCellRendererText:wrap-mode:
569 * Specifies how to break the string into multiple lines, if the cell
570 * renderer does not have enough room to display the entire string.
571 * This property has no effect unless the wrap-width property is set.
575 g_object_class_install_property (object_class,
577 g_param_spec_enum ("wrap-mode",
579 P_("How to break the string into multiple lines, "
580 "if the cell renderer does not have enough room "
581 "to display the entire string"),
582 PANGO_TYPE_WRAP_MODE,
584 GTK_PARAM_READWRITE));
587 * GtkCellRendererText:wrap-width:
589 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
590 * be used to influence at what character positions the line breaks can be placed.
591 * Setting wrap-width to -1 turns wrapping off.
595 g_object_class_install_property (object_class,
597 g_param_spec_int ("wrap-width",
599 P_("The width at which the text is wrapped"),
603 GTK_PARAM_READWRITE));
606 * GtkCellRendererText:alignment:
608 * Specifies how to align the lines of text with respect to each other.
610 * Note that this property describes how to align the lines of text in
611 * case there are several of them. The "xalign" property of #GtkCellRenderer,
612 * on the other hand, sets the horizontal alignment of the whole text.
616 g_object_class_install_property (object_class,
618 g_param_spec_enum ("alignment",
620 P_("How to align the lines"),
621 PANGO_TYPE_ALIGNMENT,
623 GTK_PARAM_READWRITE));
626 * GtkCellRendererText:placeholder-text:
628 * The text that will be displayed in the #GtkCellRenderer if
629 * #GtkCellRendererText:editable is %TRUE and the cell is empty.
633 g_object_class_install_property (object_class,
634 PROP_PLACEHOLDER_TEXT,
635 g_param_spec_string ("placeholder-text",
636 P_("Placeholder text"),
637 P_("Text rendered when an editable cell is empty"),
639 GTK_PARAM_READWRITE));
642 /* Style props are set or not */
644 #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))
646 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
647 P_("Background set"),
648 P_("Whether this tag affects the background color"));
650 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
651 P_("Foreground set"),
652 P_("Whether this tag affects the foreground color"));
654 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
655 P_("Editability set"),
656 P_("Whether this tag affects text editability"));
658 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
659 P_("Font family set"),
660 P_("Whether this tag affects the font family"));
662 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
663 P_("Font style set"),
664 P_("Whether this tag affects the font style"));
666 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
667 P_("Font variant set"),
668 P_("Whether this tag affects the font variant"));
670 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
671 P_("Font weight set"),
672 P_("Whether this tag affects the font weight"));
674 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
675 P_("Font stretch set"),
676 P_("Whether this tag affects the font stretch"));
678 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
680 P_("Whether this tag affects the font size"));
682 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
683 P_("Font scale set"),
684 P_("Whether this tag scales the font size by a factor"));
686 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
688 P_("Whether this tag affects the rise"));
690 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
691 P_("Strikethrough set"),
692 P_("Whether this tag affects strikethrough"));
694 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
696 P_("Whether this tag affects underlining"));
698 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
700 P_("Whether this tag affects the language the text is rendered as"));
702 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
704 P_("Whether this tag affects the ellipsize mode"));
706 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
708 P_("Whether this tag affects the alignment mode"));
711 * GtkCellRendererText::edited:
712 * @renderer: the object which received the signal
713 * @path: the path identifying the edited cell
714 * @new_text: the new text
716 * This signal is emitted after @renderer has been edited.
718 * It is the responsibility of the application to update the model
719 * and store @new_text at the position indicated by @path.
721 text_cell_renderer_signals [EDITED] =
722 g_signal_new (I_("edited"),
723 G_OBJECT_CLASS_TYPE (object_class),
725 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
727 _gtk_marshal_VOID__STRING_STRING,
732 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
734 _gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_TEXT_CELL_ACCESSIBLE);
738 gtk_cell_renderer_text_finalize (GObject *object)
740 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
741 GtkCellRendererTextPrivate *priv = celltext->priv;
743 pango_font_description_free (priv->font);
746 g_free (priv->placeholder_text);
748 if (priv->extra_attrs)
749 pango_attr_list_unref (priv->extra_attrs);
752 g_object_unref (priv->language);
754 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
758 get_property_font_set_mask (guint prop_id)
762 case PROP_FAMILY_SET:
763 return PANGO_FONT_MASK_FAMILY;
765 return PANGO_FONT_MASK_STYLE;
766 case PROP_VARIANT_SET:
767 return PANGO_FONT_MASK_VARIANT;
768 case PROP_WEIGHT_SET:
769 return PANGO_FONT_MASK_WEIGHT;
770 case PROP_STRETCH_SET:
771 return PANGO_FONT_MASK_STRETCH;
773 return PANGO_FONT_MASK_SIZE;
780 gtk_cell_renderer_text_get_property (GObject *object,
785 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
786 GtkCellRendererTextPrivate *priv = celltext->priv;
791 g_value_set_string (value, priv->text);
794 case PROP_ATTRIBUTES:
795 g_value_set_boxed (value, priv->extra_attrs);
798 case PROP_SINGLE_PARAGRAPH_MODE:
799 g_value_set_boolean (value, priv->single_paragraph);
802 case PROP_BACKGROUND_GDK:
806 color.red = (guint16) (priv->background.red * 65535);
807 color.green = (guint16) (priv->background.green * 65535);
808 color.blue = (guint16) (priv->background.blue * 65535);
810 g_value_set_boxed (value, &color);
814 case PROP_FOREGROUND_GDK:
818 color.red = (guint16) (priv->foreground.red * 65535);
819 color.green = (guint16) (priv->foreground.green * 65535);
820 color.blue = (guint16) (priv->foreground.blue * 65535);
822 g_value_set_boxed (value, &color);
826 case PROP_BACKGROUND_RGBA:
827 g_value_set_boxed (value, &priv->background);
830 case PROP_FOREGROUND_RGBA:
831 g_value_set_boxed (value, &priv->foreground);
835 g_value_take_string (value, pango_font_description_to_string (priv->font));
839 g_value_set_boxed (value, priv->font);
843 g_value_set_string (value, pango_font_description_get_family (priv->font));
847 g_value_set_enum (value, pango_font_description_get_style (priv->font));
851 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
855 g_value_set_int (value, pango_font_description_get_weight (priv->font));
859 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
863 g_value_set_int (value, pango_font_description_get_size (priv->font));
866 case PROP_SIZE_POINTS:
867 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
871 g_value_set_double (value, priv->font_scale);
875 g_value_set_boolean (value, priv->editable);
878 case PROP_STRIKETHROUGH:
879 g_value_set_boolean (value, priv->strikethrough);
883 g_value_set_enum (value, priv->underline_style);
887 g_value_set_int (value, priv->rise);
891 g_value_set_static_string (value, pango_language_to_string (priv->language));
895 g_value_set_enum (value, priv->ellipsize);
899 g_value_set_enum (value, priv->wrap_mode);
902 case PROP_WRAP_WIDTH:
903 g_value_set_int (value, priv->wrap_width);
907 g_value_set_enum (value, priv->align);
910 case PROP_BACKGROUND_SET:
911 g_value_set_boolean (value, priv->background_set);
914 case PROP_FOREGROUND_SET:
915 g_value_set_boolean (value, priv->foreground_set);
918 case PROP_FAMILY_SET:
920 case PROP_VARIANT_SET:
921 case PROP_WEIGHT_SET:
922 case PROP_STRETCH_SET:
925 PangoFontMask mask = get_property_font_set_mask (param_id);
926 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
932 g_value_set_boolean (value, priv->scale_set);
935 case PROP_EDITABLE_SET:
936 g_value_set_boolean (value, priv->editable_set);
939 case PROP_STRIKETHROUGH_SET:
940 g_value_set_boolean (value, priv->strikethrough_set);
943 case PROP_UNDERLINE_SET:
944 g_value_set_boolean (value, priv->underline_set);
948 g_value_set_boolean (value, priv->rise_set);
951 case PROP_LANGUAGE_SET:
952 g_value_set_boolean (value, priv->language_set);
955 case PROP_ELLIPSIZE_SET:
956 g_value_set_boolean (value, priv->ellipsize_set);
960 g_value_set_boolean (value, priv->align_set);
963 case PROP_WIDTH_CHARS:
964 g_value_set_int (value, priv->width_chars);
967 case PROP_MAX_WIDTH_CHARS:
968 g_value_set_int (value, priv->max_width_chars);
971 case PROP_PLACEHOLDER_TEXT:
972 g_value_set_string (value, priv->placeholder_text);
975 case PROP_BACKGROUND:
976 case PROP_FOREGROUND:
979 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
986 set_bg_color (GtkCellRendererText *celltext,
989 GtkCellRendererTextPrivate *priv = celltext->priv;
993 if (!priv->background_set)
995 priv->background_set = TRUE;
996 g_object_notify (G_OBJECT (celltext), "background-set");
999 priv->background = *rgba;
1003 if (priv->background_set)
1005 priv->background_set = FALSE;
1006 g_object_notify (G_OBJECT (celltext), "background-set");
1013 set_fg_color (GtkCellRendererText *celltext,
1016 GtkCellRendererTextPrivate *priv = celltext->priv;
1020 if (!priv->foreground_set)
1022 priv->foreground_set = TRUE;
1023 g_object_notify (G_OBJECT (celltext), "foreground-set");
1026 priv->foreground = *rgba;
1030 if (priv->foreground_set)
1032 priv->foreground_set = FALSE;
1033 g_object_notify (G_OBJECT (celltext), "foreground-set");
1038 static PangoFontMask
1039 set_font_desc_fields (PangoFontDescription *desc,
1040 PangoFontMask to_set)
1042 PangoFontMask changed_mask = 0;
1044 if (to_set & PANGO_FONT_MASK_FAMILY)
1046 const char *family = pango_font_description_get_family (desc);
1050 changed_mask |= PANGO_FONT_MASK_FAMILY;
1053 pango_font_description_set_family (desc, family);
1055 if (to_set & PANGO_FONT_MASK_STYLE)
1056 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1057 if (to_set & PANGO_FONT_MASK_VARIANT)
1058 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1059 if (to_set & PANGO_FONT_MASK_WEIGHT)
1060 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1061 if (to_set & PANGO_FONT_MASK_STRETCH)
1062 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1063 if (to_set & PANGO_FONT_MASK_SIZE)
1065 gint size = pango_font_description_get_size (desc);
1068 size = 10 * PANGO_SCALE;
1069 changed_mask |= PANGO_FONT_MASK_SIZE;
1072 pango_font_description_set_size (desc, size);
1075 return changed_mask;
1079 notify_set_changed (GObject *object,
1080 PangoFontMask changed_mask)
1082 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1083 g_object_notify (object, "family-set");
1084 if (changed_mask & PANGO_FONT_MASK_STYLE)
1085 g_object_notify (object, "style-set");
1086 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1087 g_object_notify (object, "variant-set");
1088 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1089 g_object_notify (object, "weight-set");
1090 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1091 g_object_notify (object, "stretch-set");
1092 if (changed_mask & PANGO_FONT_MASK_SIZE)
1093 g_object_notify (object, "size-set");
1097 notify_fields_changed (GObject *object,
1098 PangoFontMask changed_mask)
1100 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1101 g_object_notify (object, "family");
1102 if (changed_mask & PANGO_FONT_MASK_STYLE)
1103 g_object_notify (object, "style");
1104 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1105 g_object_notify (object, "variant");
1106 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1107 g_object_notify (object, "weight");
1108 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1109 g_object_notify (object, "stretch");
1110 if (changed_mask & PANGO_FONT_MASK_SIZE)
1111 g_object_notify (object, "size");
1115 set_font_description (GtkCellRendererText *celltext,
1116 PangoFontDescription *font_desc)
1118 GtkCellRendererTextPrivate *priv = celltext->priv;
1119 GObject *object = G_OBJECT (celltext);
1120 PangoFontDescription *new_font_desc;
1121 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1124 new_font_desc = pango_font_description_copy (font_desc);
1126 new_font_desc = pango_font_description_new ();
1128 old_mask = pango_font_description_get_set_fields (priv->font);
1129 new_mask = pango_font_description_get_set_fields (new_font_desc);
1131 changed_mask = old_mask | new_mask;
1132 set_changed_mask = old_mask ^ new_mask;
1134 pango_font_description_free (priv->font);
1135 priv->font = new_font_desc;
1137 g_object_freeze_notify (object);
1139 g_object_notify (object, "font-desc");
1140 g_object_notify (object, "font");
1142 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1143 g_object_notify (object, "family");
1144 if (changed_mask & PANGO_FONT_MASK_STYLE)
1145 g_object_notify (object, "style");
1146 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1147 g_object_notify (object, "variant");
1148 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1149 g_object_notify (object, "weight");
1150 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1151 g_object_notify (object, "stretch");
1152 if (changed_mask & PANGO_FONT_MASK_SIZE)
1154 g_object_notify (object, "size");
1155 g_object_notify (object, "size-points");
1158 notify_set_changed (object, set_changed_mask);
1160 g_object_thaw_notify (object);
1164 gtk_cell_renderer_text_set_property (GObject *object,
1166 const GValue *value,
1169 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1170 GtkCellRendererTextPrivate *priv = celltext->priv;
1175 g_free (priv->text);
1177 if (priv->markup_set)
1179 if (priv->extra_attrs)
1180 pango_attr_list_unref (priv->extra_attrs);
1181 priv->extra_attrs = NULL;
1182 priv->markup_set = FALSE;
1185 priv->text = g_value_dup_string (value);
1186 g_object_notify (object, "text");
1189 case PROP_ATTRIBUTES:
1190 if (priv->extra_attrs)
1191 pango_attr_list_unref (priv->extra_attrs);
1193 priv->extra_attrs = g_value_get_boxed (value);
1194 if (priv->extra_attrs)
1195 pango_attr_list_ref (priv->extra_attrs);
1201 GError *error = NULL;
1202 PangoAttrList *attrs = NULL;
1204 str = g_value_get_string (value);
1205 if (str && !pango_parse_markup (str,
1213 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1215 g_error_free (error);
1219 g_free (priv->text);
1221 if (priv->extra_attrs)
1222 pango_attr_list_unref (priv->extra_attrs);
1225 priv->extra_attrs = attrs;
1226 priv->markup_set = TRUE;
1230 case PROP_SINGLE_PARAGRAPH_MODE:
1231 priv->single_paragraph = g_value_get_boolean (value);
1234 case PROP_BACKGROUND:
1238 if (!g_value_get_string (value))
1239 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1240 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1241 set_bg_color (celltext, &rgba);
1243 g_warning ("Don't know color `%s'", g_value_get_string (value));
1245 g_object_notify (object, "background-gdk");
1249 case PROP_FOREGROUND:
1253 if (!g_value_get_string (value))
1254 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1255 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1256 set_fg_color (celltext, &rgba);
1258 g_warning ("Don't know color `%s'", g_value_get_string (value));
1260 g_object_notify (object, "foreground-gdk");
1264 case PROP_BACKGROUND_GDK:
1268 color = g_value_get_boxed (value);
1273 rgba.red = color->red / 65535.;
1274 rgba.green = color->green / 65535.;
1275 rgba.blue = color->blue / 65535.;
1278 set_bg_color (celltext, &rgba);
1282 set_bg_color (celltext, NULL);
1287 case PROP_FOREGROUND_GDK:
1291 color = g_value_get_boxed (value);
1296 rgba.red = color->red / 65535.;
1297 rgba.green = color->green / 65535.;
1298 rgba.blue = color->blue / 65535.;
1301 set_fg_color (celltext, &rgba);
1305 set_fg_color (celltext, NULL);
1310 case PROP_BACKGROUND_RGBA:
1311 /* This notifies the GObject itself. */
1312 set_bg_color (celltext, g_value_get_boxed (value));
1315 case PROP_FOREGROUND_RGBA:
1316 /* This notifies the GObject itself. */
1317 set_fg_color (celltext, g_value_get_boxed (value));
1322 PangoFontDescription *font_desc = NULL;
1325 name = g_value_get_string (value);
1328 font_desc = pango_font_description_from_string (name);
1330 set_font_description (celltext, font_desc);
1332 pango_font_description_free (font_desc);
1334 if (priv->fixed_height_rows != -1)
1335 priv->calc_fixed_height = TRUE;
1339 case PROP_FONT_DESC:
1340 set_font_description (celltext, g_value_get_boxed (value));
1342 if (priv->fixed_height_rows != -1)
1343 priv->calc_fixed_height = TRUE;
1352 case PROP_SIZE_POINTS:
1354 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1359 pango_font_description_set_family (priv->font,
1360 g_value_get_string (value));
1363 pango_font_description_set_style (priv->font,
1364 g_value_get_enum (value));
1367 pango_font_description_set_variant (priv->font,
1368 g_value_get_enum (value));
1371 pango_font_description_set_weight (priv->font,
1372 g_value_get_int (value));
1375 pango_font_description_set_stretch (priv->font,
1376 g_value_get_enum (value));
1379 pango_font_description_set_size (priv->font,
1380 g_value_get_int (value));
1381 g_object_notify (object, "size-points");
1383 case PROP_SIZE_POINTS:
1384 pango_font_description_set_size (priv->font,
1385 g_value_get_double (value) * PANGO_SCALE);
1386 g_object_notify (object, "size");
1390 if (priv->fixed_height_rows != -1)
1391 priv->calc_fixed_height = TRUE;
1393 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1394 g_object_notify (object, "font-desc");
1395 g_object_notify (object, "font");
1401 priv->font_scale = g_value_get_double (value);
1402 priv->scale_set = TRUE;
1403 if (priv->fixed_height_rows != -1)
1404 priv->calc_fixed_height = TRUE;
1405 g_object_notify (object, "scale-set");
1409 priv->editable = g_value_get_boolean (value);
1410 priv->editable_set = TRUE;
1412 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1414 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1415 g_object_notify (object, "editable-set");
1418 case PROP_STRIKETHROUGH:
1419 priv->strikethrough = g_value_get_boolean (value);
1420 priv->strikethrough_set = TRUE;
1421 g_object_notify (object, "strikethrough-set");
1424 case PROP_UNDERLINE:
1425 priv->underline_style = g_value_get_enum (value);
1426 priv->underline_set = TRUE;
1427 g_object_notify (object, "underline-set");
1432 priv->rise = g_value_get_int (value);
1433 priv->rise_set = TRUE;
1434 g_object_notify (object, "rise-set");
1435 if (priv->fixed_height_rows != -1)
1436 priv->calc_fixed_height = TRUE;
1440 priv->language_set = TRUE;
1442 g_object_unref (priv->language);
1443 priv->language = pango_language_from_string (g_value_get_string (value));
1444 g_object_notify (object, "language-set");
1447 case PROP_ELLIPSIZE:
1448 priv->ellipsize = g_value_get_enum (value);
1449 priv->ellipsize_set = TRUE;
1450 g_object_notify (object, "ellipsize-set");
1453 case PROP_WRAP_MODE:
1454 priv->wrap_mode = g_value_get_enum (value);
1457 case PROP_WRAP_WIDTH:
1458 priv->wrap_width = g_value_get_int (value);
1461 case PROP_WIDTH_CHARS:
1462 priv->width_chars = g_value_get_int (value);
1465 case PROP_MAX_WIDTH_CHARS:
1466 priv->max_width_chars = g_value_get_int (value);
1470 priv->align = g_value_get_enum (value);
1471 priv->align_set = TRUE;
1472 g_object_notify (object, "align-set");
1475 case PROP_BACKGROUND_SET:
1476 priv->background_set = g_value_get_boolean (value);
1479 case PROP_FOREGROUND_SET:
1480 priv->foreground_set = g_value_get_boolean (value);
1483 case PROP_FAMILY_SET:
1484 case PROP_STYLE_SET:
1485 case PROP_VARIANT_SET:
1486 case PROP_WEIGHT_SET:
1487 case PROP_STRETCH_SET:
1489 if (!g_value_get_boolean (value))
1491 pango_font_description_unset_fields (priv->font,
1492 get_property_font_set_mask (param_id));
1496 PangoFontMask changed_mask;
1498 changed_mask = set_font_desc_fields (priv->font,
1499 get_property_font_set_mask (param_id));
1500 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1504 case PROP_SCALE_SET:
1505 priv->scale_set = g_value_get_boolean (value);
1508 case PROP_EDITABLE_SET:
1509 priv->editable_set = g_value_get_boolean (value);
1512 case PROP_STRIKETHROUGH_SET:
1513 priv->strikethrough_set = g_value_get_boolean (value);
1516 case PROP_UNDERLINE_SET:
1517 priv->underline_set = g_value_get_boolean (value);
1521 priv->rise_set = g_value_get_boolean (value);
1524 case PROP_LANGUAGE_SET:
1525 priv->language_set = g_value_get_boolean (value);
1528 case PROP_ELLIPSIZE_SET:
1529 priv->ellipsize_set = g_value_get_boolean (value);
1532 case PROP_ALIGN_SET:
1533 priv->align_set = g_value_get_boolean (value);
1536 case PROP_PLACEHOLDER_TEXT:
1537 g_free (priv->placeholder_text);
1538 priv->placeholder_text = g_value_dup_string (value);
1542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1548 * gtk_cell_renderer_text_new:
1550 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1551 * object properties. Object properties can be
1552 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1553 * you can bind a property to a value in a #GtkTreeModel. For example,
1554 * you can bind the "text" property on the cell renderer to a string
1555 * value in the model, thus rendering a different string in each row
1556 * of the #GtkTreeView
1558 * Return value: the new cell renderer
1561 gtk_cell_renderer_text_new (void)
1563 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1566 static inline gboolean
1567 show_placeholder_text (GtkCellRendererText *celltext)
1569 GtkCellRendererTextPrivate *priv = celltext->priv;
1571 return priv->editable && priv->placeholder_text &&
1572 (!priv->text || !priv->text[0]);
1576 add_attr (PangoAttrList *attr_list,
1577 PangoAttribute *attr)
1579 attr->start_index = 0;
1580 attr->end_index = G_MAXINT;
1582 pango_attr_list_insert (attr_list, attr);
1586 get_layout (GtkCellRendererText *celltext,
1588 const GdkRectangle *cell_area,
1589 GtkCellRendererState flags)
1591 GtkCellRendererTextPrivate *priv = celltext->priv;
1592 PangoAttrList *attr_list;
1593 PangoLayout *layout;
1594 PangoUnderline uline;
1596 gboolean placeholder_layout = show_placeholder_text (celltext);
1598 layout = gtk_widget_create_pango_layout (widget, placeholder_layout ?
1599 priv->placeholder_text : priv->text);
1601 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1603 if (priv->extra_attrs)
1604 attr_list = pango_attr_list_copy (priv->extra_attrs);
1606 attr_list = pango_attr_list_new ();
1608 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1610 if (!placeholder_layout && cell_area)
1612 /* Add options that affect appearance but not size */
1614 /* note that background doesn't go here, since it affects
1615 * background_area not the PangoLayout area
1618 if (priv->foreground_set
1619 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1623 color.red = (guint16) (priv->foreground.red * 65535);
1624 color.green = (guint16) (priv->foreground.green * 65535);
1625 color.blue = (guint16) (priv->foreground.blue * 65535);
1627 add_attr (attr_list,
1628 pango_attr_foreground_new (color.red, color.green, color.blue));
1631 if (priv->strikethrough_set)
1632 add_attr (attr_list,
1633 pango_attr_strikethrough_new (priv->strikethrough));
1635 else if (placeholder_layout)
1638 GtkStyleContext *context;
1639 GdkRGBA fg = { 0.5, 0.5, 0.5 };
1641 context = gtk_widget_get_style_context (widget);
1642 gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
1644 color.red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
1645 color.green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
1646 color.blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
1648 add_attr (attr_list,
1649 pango_attr_foreground_new (color.red, color.green, color.blue));
1652 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1654 if (priv->scale_set &&
1655 priv->font_scale != 1.0)
1656 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1658 if (priv->underline_set)
1659 uline = priv->underline_style;
1661 uline = PANGO_UNDERLINE_NONE;
1663 if (priv->language_set)
1664 add_attr (attr_list, pango_attr_language_new (priv->language));
1666 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1670 case PANGO_UNDERLINE_NONE:
1671 uline = PANGO_UNDERLINE_SINGLE;
1674 case PANGO_UNDERLINE_SINGLE:
1675 uline = PANGO_UNDERLINE_DOUBLE;
1683 if (uline != PANGO_UNDERLINE_NONE)
1684 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1687 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1689 /* Now apply the attributes as they will effect the outcome
1690 * of pango_layout_get_extents() */
1691 pango_layout_set_attributes (layout, attr_list);
1692 pango_attr_list_unref (attr_list);
1694 if (priv->ellipsize_set)
1695 pango_layout_set_ellipsize (layout, priv->ellipsize);
1697 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1699 if (priv->wrap_width != -1)
1701 PangoRectangle rect;
1702 gint width, text_width;
1704 pango_layout_get_extents (layout, NULL, &rect);
1705 text_width = rect.width;
1708 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1710 width = priv->wrap_width * PANGO_SCALE;
1712 width = MIN (width, text_width);
1714 pango_layout_set_width (layout, width);
1715 pango_layout_set_wrap (layout, priv->wrap_mode);
1719 pango_layout_set_width (layout, -1);
1720 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1723 if (priv->align_set)
1724 pango_layout_set_alignment (layout, priv->align);
1727 PangoAlignment align;
1729 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1730 align = PANGO_ALIGN_RIGHT;
1732 align = PANGO_ALIGN_LEFT;
1734 pango_layout_set_alignment (layout, align);
1742 get_size (GtkCellRenderer *cell,
1744 const GdkRectangle *cell_area,
1745 PangoLayout *layout,
1751 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1752 GtkCellRendererTextPrivate *priv = celltext->priv;
1753 PangoRectangle rect;
1755 gint cell_width, cell_height;
1757 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1759 if (priv->calc_fixed_height)
1761 GtkStyleContext *style_context;
1762 GtkStateFlags state;
1763 PangoContext *context;
1764 PangoFontMetrics *metrics;
1765 PangoFontDescription *font_desc;
1768 style_context = gtk_widget_get_style_context (widget);
1769 state = gtk_widget_get_state_flags (widget);
1771 gtk_style_context_get (style_context, state, "font", &font_desc, NULL);
1772 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1774 if (priv->scale_set)
1775 pango_font_description_set_size (font_desc,
1776 priv->font_scale * pango_font_description_get_size (font_desc));
1778 context = gtk_widget_get_pango_context (widget);
1780 metrics = pango_context_get_metrics (context,
1782 pango_context_get_language (context));
1783 row_height = (pango_font_metrics_get_ascent (metrics) +
1784 pango_font_metrics_get_descent (metrics));
1785 pango_font_metrics_unref (metrics);
1787 pango_font_description_free (font_desc);
1789 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1791 gtk_cell_renderer_set_fixed_size (cell,
1792 cell_width, 2 * ypad +
1793 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1797 *height = cell_height;
1800 priv->calc_fixed_height = FALSE;
1806 g_object_ref (layout);
1808 layout = get_layout (celltext, widget, NULL, 0);
1810 pango_layout_get_pixel_extents (layout, NULL, &rect);
1814 gfloat xalign, yalign;
1816 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1818 rect.height = MIN (rect.height, cell_area->height - 2 * ypad);
1819 rect.width = MIN (rect.width, cell_area->width - 2 * xpad);
1823 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1824 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.width + (2 * xpad)));
1826 *x_offset = xalign * (cell_area->width - (rect.width + (2 * xpad)));
1828 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1829 *x_offset = MAX(*x_offset, 0);
1833 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1834 *y_offset = MAX (*y_offset, 0);
1839 if (x_offset) *x_offset = 0;
1840 if (y_offset) *y_offset = 0;
1844 *height = ypad * 2 + rect.height;
1847 *width = xpad * 2 + rect.width;
1849 g_object_unref (layout);
1853 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1856 const GdkRectangle *background_area,
1857 const GdkRectangle *cell_area,
1858 GtkCellRendererState flags)
1861 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1862 GtkCellRendererTextPrivate *priv = celltext->priv;
1863 GtkStyleContext *context;
1864 PangoLayout *layout;
1868 PangoRectangle rect;
1870 layout = get_layout (celltext, widget, cell_area, flags);
1871 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1872 context = gtk_widget_get_style_context (widget);
1874 if (priv->background_set && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1876 gdk_cairo_rectangle (cr, background_area);
1877 gdk_cairo_set_source_rgba (cr, &priv->background);
1881 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1883 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1884 pango_layout_set_width (layout,
1885 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1886 else if (priv->wrap_width == -1)
1887 pango_layout_set_width (layout, -1);
1889 pango_layout_get_pixel_extents (layout, NULL, &rect);
1890 x_offset = x_offset - rect.x;
1894 gdk_cairo_rectangle (cr, cell_area);
1897 gtk_render_layout (context, cr,
1898 cell_area->x + x_offset + xpad,
1899 cell_area->y + y_offset + ypad,
1904 g_object_unref (layout);
1908 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1911 GtkCellRendererTextPrivate *priv;
1913 const gchar *new_text;
1916 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1920 if (priv->focus_out_id > 0)
1922 g_signal_handler_disconnect (entry, priv->focus_out_id);
1923 priv->focus_out_id = 0;
1926 if (priv->populate_popup_id > 0)
1928 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1929 priv->populate_popup_id = 0;
1932 if (priv->entry_menu_popdown_timeout)
1934 g_source_remove (priv->entry_menu_popdown_timeout);
1935 priv->entry_menu_popdown_timeout = 0;
1938 g_object_get (entry,
1939 "editing-canceled", &canceled,
1941 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1946 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1947 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1949 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1953 popdown_timeout (gpointer data)
1955 GtkCellRendererTextPrivate *priv;
1957 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1959 priv->entry_menu_popdown_timeout = 0;
1961 if (!gtk_widget_has_focus (priv->entry))
1962 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1968 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1971 GtkCellRendererTextPrivate *priv;
1973 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1975 priv->in_entry_menu = FALSE;
1977 if (priv->entry_menu_popdown_timeout)
1980 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1985 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1989 GtkCellRendererTextPrivate *priv;
1991 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1993 if (priv->entry_menu_popdown_timeout)
1995 g_source_remove (priv->entry_menu_popdown_timeout);
1996 priv->entry_menu_popdown_timeout = 0;
1999 priv->in_entry_menu = TRUE;
2001 g_signal_connect (menu, "unmap",
2002 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
2006 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
2010 GtkCellRendererTextPrivate *priv;
2012 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
2014 if (priv->in_entry_menu)
2017 g_object_set (entry,
2018 "editing-canceled", TRUE,
2020 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2021 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2023 /* entry needs focus-out-event */
2027 static GtkCellEditable *
2028 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
2032 const GdkRectangle *background_area,
2033 const GdkRectangle *cell_area,
2034 GtkCellRendererState flags)
2036 GtkCellRendererText *celltext;
2037 GtkCellRendererTextPrivate *priv;
2038 gfloat xalign, yalign;
2040 celltext = GTK_CELL_RENDERER_TEXT (cell);
2041 priv = celltext->priv;
2043 /* If the cell isn't editable we return NULL. */
2044 if (priv->editable == FALSE)
2047 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
2049 priv->entry = gtk_entry_new ();
2050 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
2051 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
2054 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
2055 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
2057 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
2059 priv->in_entry_menu = FALSE;
2060 if (priv->entry_menu_popdown_timeout)
2062 g_source_remove (priv->entry_menu_popdown_timeout);
2063 priv->entry_menu_popdown_timeout = 0;
2066 g_signal_connect (priv->entry,
2068 G_CALLBACK (gtk_cell_renderer_text_editing_done),
2070 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2071 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2073 priv->populate_popup_id =
2074 g_signal_connect (priv->entry, "populate-popup",
2075 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2078 gtk_widget_show (priv->entry);
2080 return GTK_CELL_EDITABLE (priv->entry);
2084 * gtk_cell_renderer_text_set_fixed_height_from_font:
2085 * @renderer: A #GtkCellRendererText
2086 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2088 * Sets the height of a renderer to explicitly be determined by the "font" and
2089 * "y_pad" property set on it. Further changes in these properties do not
2090 * affect the height, so they must be accompanied by a subsequent call to this
2091 * function. Using this function is unflexible, and should really only be used
2092 * if calculating the size of a cell is too slow (ie, a massive number of cells
2093 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
2094 * the height is determined by the properties again.
2097 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2098 gint number_of_rows)
2100 GtkCellRendererTextPrivate *priv;
2101 GtkCellRenderer *cell;
2103 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2104 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2106 cell = GTK_CELL_RENDERER (renderer);
2107 priv = renderer->priv;
2109 if (number_of_rows == -1)
2113 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2114 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2118 priv->fixed_height_rows = number_of_rows;
2119 priv->calc_fixed_height = TRUE;
2124 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2129 GtkCellRendererTextPrivate *priv;
2130 GtkCellRendererText *celltext;
2131 PangoLayout *layout;
2132 PangoContext *context;
2133 PangoFontMetrics *metrics;
2134 PangoRectangle rect;
2135 gint char_width, text_width, ellipsize_chars, xpad;
2136 gint min_width, nat_width;
2138 /* "width-chars" Hard-coded minimum width:
2139 * - minimum size should be MAX (width-chars, strlen ("..."));
2140 * - natural size should be MAX (width-chars, strlen (label->text));
2142 * "wrap-width" User specified natural wrap width
2143 * - minimum size should be MAX (width-chars, 0)
2144 * - natural size should be MIN (wrap-width, strlen (label->text))
2147 celltext = GTK_CELL_RENDERER_TEXT (cell);
2148 priv = celltext->priv;
2150 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2152 layout = get_layout (celltext, widget, NULL, 0);
2154 /* Fetch the length of the complete unwrapped text */
2155 pango_layout_set_width (layout, -1);
2156 pango_layout_get_extents (layout, NULL, &rect);
2157 text_width = rect.width;
2159 /* Fetch the average size of a charachter */
2160 context = pango_layout_get_context (layout);
2161 metrics = pango_context_get_metrics (context,
2162 pango_context_get_font_description (context),
2163 pango_context_get_language (context));
2165 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2167 pango_font_metrics_unref (metrics);
2168 g_object_unref (layout);
2170 /* enforce minimum width for ellipsized labels at ~3 chars */
2171 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2172 ellipsize_chars = 3;
2174 ellipsize_chars = 0;
2176 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2177 min_width = xpad * 2 +
2178 MIN (PANGO_PIXELS_CEIL (text_width),
2179 (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
2180 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2181 else if (priv->wrap_width > -1)
2182 min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
2184 min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
2186 if (priv->width_chars > 0)
2187 nat_width = xpad * 2 +
2188 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
2190 nat_width = xpad * 2 + PANGO_PIXELS_CEIL (text_width);
2192 nat_width = MAX (nat_width, min_width);
2194 if (priv->max_width_chars > 0)
2196 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2198 min_width = MIN (min_width, max_width);
2199 nat_width = MIN (nat_width, max_width);
2203 *minimum_size = min_width;
2206 *natural_size = nat_width;
2210 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2213 gint *minimum_height,
2214 gint *natural_height)
2216 GtkCellRendererText *celltext;
2217 PangoLayout *layout;
2218 gint text_height, xpad, ypad;
2221 celltext = GTK_CELL_RENDERER_TEXT (cell);
2223 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2225 layout = get_layout (celltext, widget, NULL, 0);
2227 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2228 pango_layout_get_pixel_size (layout, NULL, &text_height);
2231 *minimum_height = text_height + ypad * 2;
2234 *natural_height = text_height + ypad * 2;
2236 g_object_unref (layout);
2240 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2247 /* Thankfully cell renderers dont rotate, so they only have to do
2248 * height-for-width and not the opposite. Here we have only to return
2249 * the height for the base minimum width of the renderer.
2251 * Note this code path wont be followed by GtkTreeView which is
2252 * height-for-width specifically.
2254 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2255 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2256 minimum_size, natural_size);
2260 gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
2262 GtkCellRendererState flags,
2263 const GdkRectangle *cell_area,
2264 GdkRectangle *aligned_area)
2266 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
2267 PangoLayout *layout;
2271 layout = get_layout (celltext, widget, cell_area, flags);
2272 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset,
2273 &aligned_area->width, &aligned_area->height);
2275 aligned_area->x = cell_area->x + x_offset;
2276 aligned_area->y = cell_area->y + y_offset;
2278 g_object_unref (layout);