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,
116 PROP_BACKGROUND_RGBA,
117 PROP_FOREGROUND_RGBA,
136 /* Whether-a-style-arg-is-set args */
147 PROP_STRIKETHROUGH_SET,
155 static guint text_cell_renderer_signals [LAST_SIGNAL];
157 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
159 struct _GtkCellRendererTextPrivate
163 PangoAttrList *extra_attrs;
166 PangoAlignment align;
167 PangoEllipsizeMode ellipsize;
168 PangoFontDescription *font;
169 PangoLanguage *language;
170 PangoUnderline underline_style;
171 PangoWrapMode wrap_mode;
178 gint fixed_height_rows;
180 gint max_width_chars;
183 guint in_entry_menu : 1;
184 guint strikethrough : 1;
187 guint foreground_set : 1;
188 guint background_set : 1;
189 guint underline_set : 1;
191 guint strikethrough_set : 1;
192 guint editable_set : 1;
193 guint calc_fixed_height : 1;
194 guint single_paragraph : 1;
195 guint language_set : 1;
196 guint markup_set : 1;
197 guint ellipsize_set : 1;
201 gulong populate_popup_id;
202 gulong entry_menu_popdown_timeout;
205 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
208 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
210 GtkCellRendererTextPrivate *priv;
211 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
213 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
214 GTK_TYPE_CELL_RENDERER_TEXT,
215 GtkCellRendererTextPrivate);
216 priv = celltext->priv;
218 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
219 gtk_cell_renderer_set_padding (cell, 2, 2);
220 priv->font_scale = 1.0;
221 priv->fixed_height_rows = -1;
222 priv->font = pango_font_description_new ();
224 priv->width_chars = -1;
225 priv->max_width_chars = -1;
226 priv->wrap_width = -1;
227 priv->wrap_mode = PANGO_WRAP_CHAR;
228 priv->align = PANGO_ALIGN_LEFT;
229 priv->align_set = FALSE;
233 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
235 GObjectClass *object_class = G_OBJECT_CLASS (class);
236 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
238 object_class->finalize = gtk_cell_renderer_text_finalize;
240 object_class->get_property = gtk_cell_renderer_text_get_property;
241 object_class->set_property = gtk_cell_renderer_text_set_property;
243 cell_class->render = gtk_cell_renderer_text_render;
244 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
245 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
246 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
247 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
248 cell_class->get_aligned_area = gtk_cell_renderer_text_get_aligned_area;
250 g_object_class_install_property (object_class,
252 g_param_spec_string ("text",
254 P_("Text to render"),
256 GTK_PARAM_READWRITE));
258 g_object_class_install_property (object_class,
260 g_param_spec_string ("markup",
262 P_("Marked up text to render"),
264 GTK_PARAM_WRITABLE));
266 g_object_class_install_property (object_class,
268 g_param_spec_boxed ("attributes",
270 P_("A list of style attributes to apply to the text of the renderer"),
271 PANGO_TYPE_ATTR_LIST,
272 GTK_PARAM_READWRITE));
274 g_object_class_install_property (object_class,
275 PROP_SINGLE_PARAGRAPH_MODE,
276 g_param_spec_boolean ("single-paragraph-mode",
277 P_("Single Paragraph Mode"),
278 P_("Whether to keep all text in a single paragraph"),
280 GTK_PARAM_READWRITE));
283 g_object_class_install_property (object_class,
285 g_param_spec_string ("background",
286 P_("Background color name"),
287 P_("Background color as a string"),
289 GTK_PARAM_WRITABLE));
292 * GtkCellRendererText:background-gdk:
294 * Background color as a #GdkColor
296 * Deprecated: 3.4: Use #GtkCellRendererText:background-rgba instead.
298 g_object_class_install_property (object_class,
300 g_param_spec_boxed ("background-gdk",
301 P_("Background color"),
302 P_("Background color as a GdkColor"),
304 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
307 * GtkCellRendererText:background-rgba:
309 * Background color as a #GdkRGBA
313 g_object_class_install_property (object_class,
314 PROP_BACKGROUND_RGBA,
315 g_param_spec_boxed ("background-rgba",
316 P_("Background color as RGBA"),
317 P_("Background color as a GdkRGBA"),
319 GTK_PARAM_READWRITE));
320 g_object_class_install_property (object_class,
322 g_param_spec_string ("foreground",
323 P_("Foreground color name"),
324 P_("Foreground color as a string"),
326 GTK_PARAM_WRITABLE));
329 * GtkCellRendererText:foreground-gdk:
331 * Foreground color as a #GdkColor
333 * Deprecated: 3.4: Use #GtkCellRendererText:foreground-rgba instead.
335 g_object_class_install_property (object_class,
337 g_param_spec_boxed ("foreground-gdk",
338 P_("Foreground color"),
339 P_("Foreground color as a GdkColor"),
341 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
344 * GtkCellRendererText:foreground-rgba:
346 * Foreground color as a #GdkRGBA
350 g_object_class_install_property (object_class,
351 PROP_FOREGROUND_RGBA,
352 g_param_spec_boxed ("foreground-rgba",
353 P_("Foreground color as RGBA"),
354 P_("Foreground color as a GdkRGBA"),
356 GTK_PARAM_READWRITE));
359 g_object_class_install_property (object_class,
361 g_param_spec_boolean ("editable",
363 P_("Whether the text can be modified by the user"),
365 GTK_PARAM_READWRITE));
367 g_object_class_install_property (object_class,
369 g_param_spec_string ("font",
371 P_("Font description as a string, e.g. \"Sans Italic 12\""),
373 GTK_PARAM_READWRITE));
375 g_object_class_install_property (object_class,
377 g_param_spec_boxed ("font-desc",
379 P_("Font description as a PangoFontDescription struct"),
380 PANGO_TYPE_FONT_DESCRIPTION,
381 GTK_PARAM_READWRITE));
384 g_object_class_install_property (object_class,
386 g_param_spec_string ("family",
388 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
390 GTK_PARAM_READWRITE));
392 g_object_class_install_property (object_class,
394 g_param_spec_enum ("style",
399 GTK_PARAM_READWRITE));
401 g_object_class_install_property (object_class,
403 g_param_spec_enum ("variant",
407 PANGO_VARIANT_NORMAL,
408 GTK_PARAM_READWRITE));
410 g_object_class_install_property (object_class,
412 g_param_spec_int ("weight",
418 GTK_PARAM_READWRITE));
420 g_object_class_install_property (object_class,
422 g_param_spec_enum ("stretch",
426 PANGO_STRETCH_NORMAL,
427 GTK_PARAM_READWRITE));
429 g_object_class_install_property (object_class,
431 g_param_spec_int ("size",
437 GTK_PARAM_READWRITE));
439 g_object_class_install_property (object_class,
441 g_param_spec_double ("size-points",
443 P_("Font size in points"),
447 GTK_PARAM_READWRITE));
449 g_object_class_install_property (object_class,
451 g_param_spec_double ("scale",
453 P_("Font scaling factor"),
457 GTK_PARAM_READWRITE));
459 g_object_class_install_property (object_class,
461 g_param_spec_int ("rise",
463 P_("Offset of text above the baseline "
464 "(below the baseline if rise is negative)"),
468 GTK_PARAM_READWRITE));
471 g_object_class_install_property (object_class,
473 g_param_spec_boolean ("strikethrough",
475 P_("Whether to strike through the text"),
477 GTK_PARAM_READWRITE));
479 g_object_class_install_property (object_class,
481 g_param_spec_enum ("underline",
483 P_("Style of underline for this text"),
484 PANGO_TYPE_UNDERLINE,
485 PANGO_UNDERLINE_NONE,
486 GTK_PARAM_READWRITE));
488 g_object_class_install_property (object_class,
490 g_param_spec_string ("language",
492 P_("The language this text is in, as an ISO code. "
493 "Pango can use this as a hint when rendering the text. "
494 "If you don't understand this parameter, you probably don't need it"),
496 GTK_PARAM_READWRITE));
500 * GtkCellRendererText:ellipsize:
502 * Specifies the preferred place to ellipsize the string, if the cell renderer
503 * does not have enough room to display the entire string. Setting it to
504 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
505 * for another way of making the text fit into a given width.
509 g_object_class_install_property (object_class,
511 g_param_spec_enum ("ellipsize",
513 P_("The preferred place to ellipsize the string, "
514 "if the cell renderer does not have enough room "
515 "to display the entire string"),
516 PANGO_TYPE_ELLIPSIZE_MODE,
517 PANGO_ELLIPSIZE_NONE,
518 GTK_PARAM_READWRITE));
521 * GtkCellRendererText:width-chars:
523 * The desired width of the cell, in characters. If this property is set to
524 * -1, the width will be calculated automatically, otherwise the cell will
525 * request either 3 characters or the property value, whichever is greater.
529 g_object_class_install_property (object_class,
531 g_param_spec_int ("width-chars",
532 P_("Width In Characters"),
533 P_("The desired width of the label, in characters"),
537 GTK_PARAM_READWRITE));
541 * GtkCellRendererText:max-width-chars:
543 * The desired maximum width of the cell, in characters. If this property
544 * is set to -1, the width will be calculated automatically.
546 * For cell renderers that ellipsize or wrap text; this property
547 * controls the maximum reported width of the cell. The
548 * cell should not receive any greater allocation unless it is
549 * set to expand in its #GtkCellLayout and all of the cell's siblings
550 * have received their natural width.
554 g_object_class_install_property (object_class,
555 PROP_MAX_WIDTH_CHARS,
556 g_param_spec_int ("max-width-chars",
557 P_("Maximum Width In Characters"),
558 P_("The maximum width of the cell, in characters"),
562 GTK_PARAM_READWRITE));
565 * GtkCellRendererText:wrap-mode:
567 * Specifies how to break the string into multiple lines, if the cell
568 * renderer does not have enough room to display the entire string.
569 * This property has no effect unless the wrap-width property is set.
573 g_object_class_install_property (object_class,
575 g_param_spec_enum ("wrap-mode",
577 P_("How to break the string into multiple lines, "
578 "if the cell renderer does not have enough room "
579 "to display the entire string"),
580 PANGO_TYPE_WRAP_MODE,
582 GTK_PARAM_READWRITE));
585 * GtkCellRendererText:wrap-width:
587 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
588 * be used to influence at what character positions the line breaks can be placed.
589 * Setting wrap-width to -1 turns wrapping off.
593 g_object_class_install_property (object_class,
595 g_param_spec_int ("wrap-width",
597 P_("The width at which the text is wrapped"),
601 GTK_PARAM_READWRITE));
604 * GtkCellRendererText:alignment:
606 * Specifies how to align the lines of text with respect to each other.
608 * Note that this property describes how to align the lines of text in
609 * case there are several of them. The "xalign" property of #GtkCellRenderer,
610 * on the other hand, sets the horizontal alignment of the whole text.
614 g_object_class_install_property (object_class,
616 g_param_spec_enum ("alignment",
618 P_("How to align the lines"),
619 PANGO_TYPE_ALIGNMENT,
621 GTK_PARAM_READWRITE));
625 /* Style props are set or not */
627 #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))
629 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
630 P_("Background set"),
631 P_("Whether this tag affects the background color"));
633 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
634 P_("Foreground set"),
635 P_("Whether this tag affects the foreground color"));
637 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
638 P_("Editability set"),
639 P_("Whether this tag affects text editability"));
641 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
642 P_("Font family set"),
643 P_("Whether this tag affects the font family"));
645 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
646 P_("Font style set"),
647 P_("Whether this tag affects the font style"));
649 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
650 P_("Font variant set"),
651 P_("Whether this tag affects the font variant"));
653 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
654 P_("Font weight set"),
655 P_("Whether this tag affects the font weight"));
657 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
658 P_("Font stretch set"),
659 P_("Whether this tag affects the font stretch"));
661 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
663 P_("Whether this tag affects the font size"));
665 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
666 P_("Font scale set"),
667 P_("Whether this tag scales the font size by a factor"));
669 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
671 P_("Whether this tag affects the rise"));
673 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
674 P_("Strikethrough set"),
675 P_("Whether this tag affects strikethrough"));
677 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
679 P_("Whether this tag affects underlining"));
681 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
683 P_("Whether this tag affects the language the text is rendered as"));
685 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
687 P_("Whether this tag affects the ellipsize mode"));
689 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
691 P_("Whether this tag affects the alignment mode"));
694 * GtkCellRendererText::edited
695 * @renderer: the object which received the signal
696 * @path: the path identifying the edited cell
697 * @new_text: the new text
699 * This signal is emitted after @renderer has been edited.
701 * It is the responsibility of the application to update the model
702 * and store @new_text at the position indicated by @path.
704 text_cell_renderer_signals [EDITED] =
705 g_signal_new (I_("edited"),
706 G_OBJECT_CLASS_TYPE (object_class),
708 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
710 _gtk_marshal_VOID__STRING_STRING,
715 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
717 _gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_TEXT_CELL_ACCESSIBLE);
721 gtk_cell_renderer_text_finalize (GObject *object)
723 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
724 GtkCellRendererTextPrivate *priv = celltext->priv;
726 pango_font_description_free (priv->font);
730 if (priv->extra_attrs)
731 pango_attr_list_unref (priv->extra_attrs);
734 g_object_unref (priv->language);
736 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
740 get_property_font_set_mask (guint prop_id)
744 case PROP_FAMILY_SET:
745 return PANGO_FONT_MASK_FAMILY;
747 return PANGO_FONT_MASK_STYLE;
748 case PROP_VARIANT_SET:
749 return PANGO_FONT_MASK_VARIANT;
750 case PROP_WEIGHT_SET:
751 return PANGO_FONT_MASK_WEIGHT;
752 case PROP_STRETCH_SET:
753 return PANGO_FONT_MASK_STRETCH;
755 return PANGO_FONT_MASK_SIZE;
762 gtk_cell_renderer_text_get_property (GObject *object,
767 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
768 GtkCellRendererTextPrivate *priv = celltext->priv;
773 g_value_set_string (value, priv->text);
776 case PROP_ATTRIBUTES:
777 g_value_set_boxed (value, priv->extra_attrs);
780 case PROP_SINGLE_PARAGRAPH_MODE:
781 g_value_set_boolean (value, priv->single_paragraph);
784 case PROP_BACKGROUND_GDK:
788 color.red = (guint16) (priv->background.red * 65535);
789 color.green = (guint16) (priv->background.green * 65535);
790 color.blue = (guint16) (priv->background.blue * 65535);
792 g_value_set_boxed (value, &color);
796 case PROP_FOREGROUND_GDK:
800 color.red = (guint16) (priv->foreground.red * 65535);
801 color.green = (guint16) (priv->foreground.green * 65535);
802 color.blue = (guint16) (priv->foreground.blue * 65535);
804 g_value_set_boxed (value, &color);
808 case PROP_BACKGROUND_RGBA:
809 g_value_set_boxed (value, &priv->background);
812 case PROP_FOREGROUND_RGBA:
813 g_value_set_boxed (value, &priv->foreground);
817 g_value_take_string (value, pango_font_description_to_string (priv->font));
821 g_value_set_boxed (value, priv->font);
825 g_value_set_string (value, pango_font_description_get_family (priv->font));
829 g_value_set_enum (value, pango_font_description_get_style (priv->font));
833 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
837 g_value_set_int (value, pango_font_description_get_weight (priv->font));
841 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
845 g_value_set_int (value, pango_font_description_get_size (priv->font));
848 case PROP_SIZE_POINTS:
849 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
853 g_value_set_double (value, priv->font_scale);
857 g_value_set_boolean (value, priv->editable);
860 case PROP_STRIKETHROUGH:
861 g_value_set_boolean (value, priv->strikethrough);
865 g_value_set_enum (value, priv->underline_style);
869 g_value_set_int (value, priv->rise);
873 g_value_set_static_string (value, pango_language_to_string (priv->language));
877 g_value_set_enum (value, priv->ellipsize);
881 g_value_set_enum (value, priv->wrap_mode);
884 case PROP_WRAP_WIDTH:
885 g_value_set_int (value, priv->wrap_width);
889 g_value_set_enum (value, priv->align);
892 case PROP_BACKGROUND_SET:
893 g_value_set_boolean (value, priv->background_set);
896 case PROP_FOREGROUND_SET:
897 g_value_set_boolean (value, priv->foreground_set);
900 case PROP_FAMILY_SET:
902 case PROP_VARIANT_SET:
903 case PROP_WEIGHT_SET:
904 case PROP_STRETCH_SET:
907 PangoFontMask mask = get_property_font_set_mask (param_id);
908 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
914 g_value_set_boolean (value, priv->scale_set);
917 case PROP_EDITABLE_SET:
918 g_value_set_boolean (value, priv->editable_set);
921 case PROP_STRIKETHROUGH_SET:
922 g_value_set_boolean (value, priv->strikethrough_set);
925 case PROP_UNDERLINE_SET:
926 g_value_set_boolean (value, priv->underline_set);
930 g_value_set_boolean (value, priv->rise_set);
933 case PROP_LANGUAGE_SET:
934 g_value_set_boolean (value, priv->language_set);
937 case PROP_ELLIPSIZE_SET:
938 g_value_set_boolean (value, priv->ellipsize_set);
942 g_value_set_boolean (value, priv->align_set);
945 case PROP_WIDTH_CHARS:
946 g_value_set_int (value, priv->width_chars);
949 case PROP_MAX_WIDTH_CHARS:
950 g_value_set_int (value, priv->max_width_chars);
953 case PROP_BACKGROUND:
954 case PROP_FOREGROUND:
957 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
964 set_bg_color (GtkCellRendererText *celltext,
967 GtkCellRendererTextPrivate *priv = celltext->priv;
971 if (!priv->background_set)
973 priv->background_set = TRUE;
974 g_object_notify (G_OBJECT (celltext), "background-set");
977 priv->background = *rgba;
981 if (priv->background_set)
983 priv->background_set = FALSE;
984 g_object_notify (G_OBJECT (celltext), "background-set");
991 set_fg_color (GtkCellRendererText *celltext,
994 GtkCellRendererTextPrivate *priv = celltext->priv;
998 if (!priv->foreground_set)
1000 priv->foreground_set = TRUE;
1001 g_object_notify (G_OBJECT (celltext), "foreground-set");
1004 priv->foreground = *rgba;
1008 if (priv->foreground_set)
1010 priv->foreground_set = FALSE;
1011 g_object_notify (G_OBJECT (celltext), "foreground-set");
1016 static PangoFontMask
1017 set_font_desc_fields (PangoFontDescription *desc,
1018 PangoFontMask to_set)
1020 PangoFontMask changed_mask = 0;
1022 if (to_set & PANGO_FONT_MASK_FAMILY)
1024 const char *family = pango_font_description_get_family (desc);
1028 changed_mask |= PANGO_FONT_MASK_FAMILY;
1031 pango_font_description_set_family (desc, family);
1033 if (to_set & PANGO_FONT_MASK_STYLE)
1034 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1035 if (to_set & PANGO_FONT_MASK_VARIANT)
1036 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1037 if (to_set & PANGO_FONT_MASK_WEIGHT)
1038 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1039 if (to_set & PANGO_FONT_MASK_STRETCH)
1040 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1041 if (to_set & PANGO_FONT_MASK_SIZE)
1043 gint size = pango_font_description_get_size (desc);
1046 size = 10 * PANGO_SCALE;
1047 changed_mask |= PANGO_FONT_MASK_SIZE;
1050 pango_font_description_set_size (desc, size);
1053 return changed_mask;
1057 notify_set_changed (GObject *object,
1058 PangoFontMask changed_mask)
1060 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1061 g_object_notify (object, "family-set");
1062 if (changed_mask & PANGO_FONT_MASK_STYLE)
1063 g_object_notify (object, "style-set");
1064 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1065 g_object_notify (object, "variant-set");
1066 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1067 g_object_notify (object, "weight-set");
1068 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1069 g_object_notify (object, "stretch-set");
1070 if (changed_mask & PANGO_FONT_MASK_SIZE)
1071 g_object_notify (object, "size-set");
1075 notify_fields_changed (GObject *object,
1076 PangoFontMask changed_mask)
1078 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1079 g_object_notify (object, "family");
1080 if (changed_mask & PANGO_FONT_MASK_STYLE)
1081 g_object_notify (object, "style");
1082 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1083 g_object_notify (object, "variant");
1084 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1085 g_object_notify (object, "weight");
1086 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1087 g_object_notify (object, "stretch");
1088 if (changed_mask & PANGO_FONT_MASK_SIZE)
1089 g_object_notify (object, "size");
1093 set_font_description (GtkCellRendererText *celltext,
1094 PangoFontDescription *font_desc)
1096 GtkCellRendererTextPrivate *priv = celltext->priv;
1097 GObject *object = G_OBJECT (celltext);
1098 PangoFontDescription *new_font_desc;
1099 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1102 new_font_desc = pango_font_description_copy (font_desc);
1104 new_font_desc = pango_font_description_new ();
1106 old_mask = pango_font_description_get_set_fields (priv->font);
1107 new_mask = pango_font_description_get_set_fields (new_font_desc);
1109 changed_mask = old_mask | new_mask;
1110 set_changed_mask = old_mask ^ new_mask;
1112 pango_font_description_free (priv->font);
1113 priv->font = new_font_desc;
1115 g_object_freeze_notify (object);
1117 g_object_notify (object, "font-desc");
1118 g_object_notify (object, "font");
1120 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1121 g_object_notify (object, "family");
1122 if (changed_mask & PANGO_FONT_MASK_STYLE)
1123 g_object_notify (object, "style");
1124 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1125 g_object_notify (object, "variant");
1126 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1127 g_object_notify (object, "weight");
1128 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1129 g_object_notify (object, "stretch");
1130 if (changed_mask & PANGO_FONT_MASK_SIZE)
1132 g_object_notify (object, "size");
1133 g_object_notify (object, "size-points");
1136 notify_set_changed (object, set_changed_mask);
1138 g_object_thaw_notify (object);
1142 gtk_cell_renderer_text_set_property (GObject *object,
1144 const GValue *value,
1147 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1148 GtkCellRendererTextPrivate *priv = celltext->priv;
1153 g_free (priv->text);
1155 if (priv->markup_set)
1157 if (priv->extra_attrs)
1158 pango_attr_list_unref (priv->extra_attrs);
1159 priv->extra_attrs = NULL;
1160 priv->markup_set = FALSE;
1163 priv->text = g_value_dup_string (value);
1164 g_object_notify (object, "text");
1167 case PROP_ATTRIBUTES:
1168 if (priv->extra_attrs)
1169 pango_attr_list_unref (priv->extra_attrs);
1171 priv->extra_attrs = g_value_get_boxed (value);
1172 if (priv->extra_attrs)
1173 pango_attr_list_ref (priv->extra_attrs);
1179 GError *error = NULL;
1180 PangoAttrList *attrs = NULL;
1182 str = g_value_get_string (value);
1183 if (str && !pango_parse_markup (str,
1191 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1193 g_error_free (error);
1197 g_free (priv->text);
1199 if (priv->extra_attrs)
1200 pango_attr_list_unref (priv->extra_attrs);
1203 priv->extra_attrs = attrs;
1204 priv->markup_set = TRUE;
1208 case PROP_SINGLE_PARAGRAPH_MODE:
1209 priv->single_paragraph = g_value_get_boolean (value);
1212 case PROP_BACKGROUND:
1216 if (!g_value_get_string (value))
1217 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1218 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1219 set_bg_color (celltext, &rgba);
1221 g_warning ("Don't know color `%s'", g_value_get_string (value));
1223 g_object_notify (object, "background-gdk");
1227 case PROP_FOREGROUND:
1231 if (!g_value_get_string (value))
1232 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1233 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1234 set_fg_color (celltext, &rgba);
1236 g_warning ("Don't know color `%s'", g_value_get_string (value));
1238 g_object_notify (object, "foreground-gdk");
1242 case PROP_BACKGROUND_GDK:
1246 color = g_value_get_boxed (value);
1251 rgba.red = color->red / 65535.;
1252 rgba.green = color->green / 65535.;
1253 rgba.blue = color->blue / 65535.;
1256 set_bg_color (celltext, &rgba);
1260 set_bg_color (celltext, NULL);
1265 case PROP_FOREGROUND_GDK:
1269 color = g_value_get_boxed (value);
1274 rgba.red = color->red / 65535.;
1275 rgba.green = color->green / 65535.;
1276 rgba.blue = color->blue / 65535.;
1279 set_fg_color (celltext, &rgba);
1283 set_fg_color (celltext, NULL);
1288 case PROP_BACKGROUND_RGBA:
1289 /* This notifies the GObject itself. */
1290 set_bg_color (celltext, g_value_get_boxed (value));
1293 case PROP_FOREGROUND_RGBA:
1294 /* This notifies the GObject itself. */
1295 set_fg_color (celltext, g_value_get_boxed (value));
1300 PangoFontDescription *font_desc = NULL;
1303 name = g_value_get_string (value);
1306 font_desc = pango_font_description_from_string (name);
1308 set_font_description (celltext, font_desc);
1310 pango_font_description_free (font_desc);
1312 if (priv->fixed_height_rows != -1)
1313 priv->calc_fixed_height = TRUE;
1317 case PROP_FONT_DESC:
1318 set_font_description (celltext, g_value_get_boxed (value));
1320 if (priv->fixed_height_rows != -1)
1321 priv->calc_fixed_height = TRUE;
1330 case PROP_SIZE_POINTS:
1332 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1337 pango_font_description_set_family (priv->font,
1338 g_value_get_string (value));
1341 pango_font_description_set_style (priv->font,
1342 g_value_get_enum (value));
1345 pango_font_description_set_variant (priv->font,
1346 g_value_get_enum (value));
1349 pango_font_description_set_weight (priv->font,
1350 g_value_get_int (value));
1353 pango_font_description_set_stretch (priv->font,
1354 g_value_get_enum (value));
1357 pango_font_description_set_size (priv->font,
1358 g_value_get_int (value));
1359 g_object_notify (object, "size-points");
1361 case PROP_SIZE_POINTS:
1362 pango_font_description_set_size (priv->font,
1363 g_value_get_double (value) * PANGO_SCALE);
1364 g_object_notify (object, "size");
1368 if (priv->fixed_height_rows != -1)
1369 priv->calc_fixed_height = TRUE;
1371 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1372 g_object_notify (object, "font-desc");
1373 g_object_notify (object, "font");
1379 priv->font_scale = g_value_get_double (value);
1380 priv->scale_set = TRUE;
1381 if (priv->fixed_height_rows != -1)
1382 priv->calc_fixed_height = TRUE;
1383 g_object_notify (object, "scale-set");
1387 priv->editable = g_value_get_boolean (value);
1388 priv->editable_set = TRUE;
1390 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1392 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1393 g_object_notify (object, "editable-set");
1396 case PROP_STRIKETHROUGH:
1397 priv->strikethrough = g_value_get_boolean (value);
1398 priv->strikethrough_set = TRUE;
1399 g_object_notify (object, "strikethrough-set");
1402 case PROP_UNDERLINE:
1403 priv->underline_style = g_value_get_enum (value);
1404 priv->underline_set = TRUE;
1405 g_object_notify (object, "underline-set");
1410 priv->rise = g_value_get_int (value);
1411 priv->rise_set = TRUE;
1412 g_object_notify (object, "rise-set");
1413 if (priv->fixed_height_rows != -1)
1414 priv->calc_fixed_height = TRUE;
1418 priv->language_set = TRUE;
1420 g_object_unref (priv->language);
1421 priv->language = pango_language_from_string (g_value_get_string (value));
1422 g_object_notify (object, "language-set");
1425 case PROP_ELLIPSIZE:
1426 priv->ellipsize = g_value_get_enum (value);
1427 priv->ellipsize_set = TRUE;
1428 g_object_notify (object, "ellipsize-set");
1431 case PROP_WRAP_MODE:
1432 priv->wrap_mode = g_value_get_enum (value);
1435 case PROP_WRAP_WIDTH:
1436 priv->wrap_width = g_value_get_int (value);
1439 case PROP_WIDTH_CHARS:
1440 priv->width_chars = g_value_get_int (value);
1443 case PROP_MAX_WIDTH_CHARS:
1444 priv->max_width_chars = g_value_get_int (value);
1448 priv->align = g_value_get_enum (value);
1449 priv->align_set = TRUE;
1450 g_object_notify (object, "align-set");
1453 case PROP_BACKGROUND_SET:
1454 priv->background_set = g_value_get_boolean (value);
1457 case PROP_FOREGROUND_SET:
1458 priv->foreground_set = g_value_get_boolean (value);
1461 case PROP_FAMILY_SET:
1462 case PROP_STYLE_SET:
1463 case PROP_VARIANT_SET:
1464 case PROP_WEIGHT_SET:
1465 case PROP_STRETCH_SET:
1467 if (!g_value_get_boolean (value))
1469 pango_font_description_unset_fields (priv->font,
1470 get_property_font_set_mask (param_id));
1474 PangoFontMask changed_mask;
1476 changed_mask = set_font_desc_fields (priv->font,
1477 get_property_font_set_mask (param_id));
1478 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1482 case PROP_SCALE_SET:
1483 priv->scale_set = g_value_get_boolean (value);
1486 case PROP_EDITABLE_SET:
1487 priv->editable_set = g_value_get_boolean (value);
1490 case PROP_STRIKETHROUGH_SET:
1491 priv->strikethrough_set = g_value_get_boolean (value);
1494 case PROP_UNDERLINE_SET:
1495 priv->underline_set = g_value_get_boolean (value);
1499 priv->rise_set = g_value_get_boolean (value);
1502 case PROP_LANGUAGE_SET:
1503 priv->language_set = g_value_get_boolean (value);
1506 case PROP_ELLIPSIZE_SET:
1507 priv->ellipsize_set = g_value_get_boolean (value);
1510 case PROP_ALIGN_SET:
1511 priv->align_set = g_value_get_boolean (value);
1515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1521 * gtk_cell_renderer_text_new:
1523 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1524 * object properties. Object properties can be
1525 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1526 * you can bind a property to a value in a #GtkTreeModel. For example,
1527 * you can bind the "text" property on the cell renderer to a string
1528 * value in the model, thus rendering a different string in each row
1529 * of the #GtkTreeView
1531 * Return value: the new cell renderer
1534 gtk_cell_renderer_text_new (void)
1536 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1540 add_attr (PangoAttrList *attr_list,
1541 PangoAttribute *attr)
1543 attr->start_index = 0;
1544 attr->end_index = G_MAXINT;
1546 pango_attr_list_insert (attr_list, attr);
1550 get_layout (GtkCellRendererText *celltext,
1552 const GdkRectangle *cell_area,
1553 GtkCellRendererState flags)
1555 GtkCellRendererTextPrivate *priv = celltext->priv;
1556 PangoAttrList *attr_list;
1557 PangoLayout *layout;
1558 PangoUnderline uline;
1561 layout = gtk_widget_create_pango_layout (widget, priv->text);
1563 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1565 if (priv->extra_attrs)
1566 attr_list = pango_attr_list_copy (priv->extra_attrs);
1568 attr_list = pango_attr_list_new ();
1570 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1574 /* Add options that affect appearance but not size */
1576 /* note that background doesn't go here, since it affects
1577 * background_area not the PangoLayout area
1580 if (priv->foreground_set
1581 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1585 color.red = (guint16) (priv->foreground.red * 65535);
1586 color.green = (guint16) (priv->foreground.green * 65535);
1587 color.blue = (guint16) (priv->foreground.blue * 65535);
1589 add_attr (attr_list,
1590 pango_attr_foreground_new (color.red, color.green, color.blue));
1593 if (priv->strikethrough_set)
1594 add_attr (attr_list,
1595 pango_attr_strikethrough_new (priv->strikethrough));
1598 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1600 if (priv->scale_set &&
1601 priv->font_scale != 1.0)
1602 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1604 if (priv->underline_set)
1605 uline = priv->underline_style;
1607 uline = PANGO_UNDERLINE_NONE;
1609 if (priv->language_set)
1610 add_attr (attr_list, pango_attr_language_new (priv->language));
1612 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1616 case PANGO_UNDERLINE_NONE:
1617 uline = PANGO_UNDERLINE_SINGLE;
1620 case PANGO_UNDERLINE_SINGLE:
1621 uline = PANGO_UNDERLINE_DOUBLE;
1629 if (uline != PANGO_UNDERLINE_NONE)
1630 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1633 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1635 /* Now apply the attributes as they will effect the outcome
1636 * of pango_layout_get_extents() */
1637 pango_layout_set_attributes (layout, attr_list);
1638 pango_attr_list_unref (attr_list);
1640 if (priv->ellipsize_set)
1641 pango_layout_set_ellipsize (layout, priv->ellipsize);
1643 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1645 if (priv->wrap_width != -1)
1647 PangoRectangle rect;
1648 gint width, text_width;
1650 pango_layout_get_extents (layout, NULL, &rect);
1651 text_width = rect.width;
1654 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1656 width = priv->wrap_width * PANGO_SCALE;
1658 width = MIN (width, text_width);
1660 pango_layout_set_width (layout, width);
1661 pango_layout_set_wrap (layout, priv->wrap_mode);
1665 pango_layout_set_width (layout, -1);
1666 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1669 if (priv->align_set)
1670 pango_layout_set_alignment (layout, priv->align);
1673 PangoAlignment align;
1675 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1676 align = PANGO_ALIGN_RIGHT;
1678 align = PANGO_ALIGN_LEFT;
1680 pango_layout_set_alignment (layout, align);
1688 get_size (GtkCellRenderer *cell,
1690 const GdkRectangle *cell_area,
1691 PangoLayout *layout,
1697 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1698 GtkCellRendererTextPrivate *priv = celltext->priv;
1699 PangoRectangle rect;
1701 gint cell_width, cell_height;
1703 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1705 if (priv->calc_fixed_height)
1707 GtkStyleContext *style_context;
1708 GtkStateFlags state;
1709 PangoContext *context;
1710 PangoFontMetrics *metrics;
1711 PangoFontDescription *font_desc;
1714 style_context = gtk_widget_get_style_context (widget);
1715 state = gtk_widget_get_state_flags (widget);
1717 font_desc = pango_font_description_copy_static (gtk_style_context_get_font (style_context, state));
1718 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1720 if (priv->scale_set)
1721 pango_font_description_set_size (font_desc,
1722 priv->font_scale * pango_font_description_get_size (font_desc));
1724 context = gtk_widget_get_pango_context (widget);
1726 metrics = pango_context_get_metrics (context,
1728 pango_context_get_language (context));
1729 row_height = (pango_font_metrics_get_ascent (metrics) +
1730 pango_font_metrics_get_descent (metrics));
1731 pango_font_metrics_unref (metrics);
1733 pango_font_description_free (font_desc);
1735 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1737 gtk_cell_renderer_set_fixed_size (cell,
1738 cell_width, 2 * ypad +
1739 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1743 *height = cell_height;
1746 priv->calc_fixed_height = FALSE;
1752 g_object_ref (layout);
1754 layout = get_layout (celltext, widget, NULL, 0);
1756 pango_layout_get_pixel_extents (layout, NULL, &rect);
1760 gfloat xalign, yalign;
1762 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1764 rect.height = MIN (rect.height, cell_area->height - 2 * ypad);
1765 rect.width = MIN (rect.width, cell_area->width - 2 * xpad);
1769 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1770 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.width + (2 * xpad)));
1772 *x_offset = xalign * (cell_area->width - (rect.width + (2 * xpad)));
1774 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1775 *x_offset = MAX(*x_offset, 0);
1779 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1780 *y_offset = MAX (*y_offset, 0);
1785 if (x_offset) *x_offset = 0;
1786 if (y_offset) *y_offset = 0;
1790 *height = ypad * 2 + rect.height;
1793 *width = xpad * 2 + rect.width;
1795 g_object_unref (layout);
1799 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1802 const GdkRectangle *background_area,
1803 const GdkRectangle *cell_area,
1804 GtkCellRendererState flags)
1807 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1808 GtkCellRendererTextPrivate *priv = celltext->priv;
1809 GtkStyleContext *context;
1810 PangoLayout *layout;
1814 PangoRectangle rect;
1816 layout = get_layout (celltext, widget, cell_area, flags);
1817 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1818 context = gtk_widget_get_style_context (widget);
1820 if (priv->background_set && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1822 gdk_cairo_rectangle (cr, background_area);
1823 gdk_cairo_set_source_rgba (cr, &priv->background);
1827 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1829 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1830 pango_layout_set_width (layout,
1831 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1832 else if (priv->wrap_width == -1)
1833 pango_layout_set_width (layout, -1);
1835 pango_layout_get_pixel_extents (layout, NULL, &rect);
1836 x_offset = x_offset - rect.x;
1840 gdk_cairo_rectangle (cr, cell_area);
1843 gtk_render_layout (context, cr,
1844 cell_area->x + x_offset + xpad,
1845 cell_area->y + y_offset + ypad,
1850 g_object_unref (layout);
1854 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1857 GtkCellRendererTextPrivate *priv;
1859 const gchar *new_text;
1862 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1866 if (priv->focus_out_id > 0)
1868 g_signal_handler_disconnect (entry, priv->focus_out_id);
1869 priv->focus_out_id = 0;
1872 if (priv->populate_popup_id > 0)
1874 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1875 priv->populate_popup_id = 0;
1878 if (priv->entry_menu_popdown_timeout)
1880 g_source_remove (priv->entry_menu_popdown_timeout);
1881 priv->entry_menu_popdown_timeout = 0;
1884 g_object_get (entry,
1885 "editing-canceled", &canceled,
1887 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1892 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1893 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1895 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1899 popdown_timeout (gpointer data)
1901 GtkCellRendererTextPrivate *priv;
1903 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1905 priv->entry_menu_popdown_timeout = 0;
1907 if (!gtk_widget_has_focus (priv->entry))
1908 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1914 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1917 GtkCellRendererTextPrivate *priv;
1919 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1921 priv->in_entry_menu = FALSE;
1923 if (priv->entry_menu_popdown_timeout)
1926 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1931 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1935 GtkCellRendererTextPrivate *priv;
1937 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1939 if (priv->entry_menu_popdown_timeout)
1941 g_source_remove (priv->entry_menu_popdown_timeout);
1942 priv->entry_menu_popdown_timeout = 0;
1945 priv->in_entry_menu = TRUE;
1947 g_signal_connect (menu, "unmap",
1948 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1952 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1956 GtkCellRendererTextPrivate *priv;
1958 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1960 if (priv->in_entry_menu)
1963 g_object_set (entry,
1964 "editing-canceled", TRUE,
1966 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1967 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1969 /* entry needs focus-out-event */
1973 static GtkCellEditable *
1974 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1978 const GdkRectangle *background_area,
1979 const GdkRectangle *cell_area,
1980 GtkCellRendererState flags)
1982 GtkCellRendererText *celltext;
1983 GtkCellRendererTextPrivate *priv;
1984 gfloat xalign, yalign;
1986 celltext = GTK_CELL_RENDERER_TEXT (cell);
1987 priv = celltext->priv;
1989 /* If the cell isn't editable we return NULL. */
1990 if (priv->editable == FALSE)
1993 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1995 priv->entry = gtk_entry_new ();
1996 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1997 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
2000 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
2001 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
2003 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
2005 priv->in_entry_menu = FALSE;
2006 if (priv->entry_menu_popdown_timeout)
2008 g_source_remove (priv->entry_menu_popdown_timeout);
2009 priv->entry_menu_popdown_timeout = 0;
2012 g_signal_connect (priv->entry,
2014 G_CALLBACK (gtk_cell_renderer_text_editing_done),
2016 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2017 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2019 priv->populate_popup_id =
2020 g_signal_connect (priv->entry, "populate-popup",
2021 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2024 gtk_widget_show (priv->entry);
2026 return GTK_CELL_EDITABLE (priv->entry);
2030 * gtk_cell_renderer_text_set_fixed_height_from_font:
2031 * @renderer: A #GtkCellRendererText
2032 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2034 * Sets the height of a renderer to explicitly be determined by the "font" and
2035 * "y_pad" property set on it. Further changes in these properties do not
2036 * affect the height, so they must be accompanied by a subsequent call to this
2037 * function. Using this function is unflexible, and should really only be used
2038 * if calculating the size of a cell is too slow (ie, a massive number of cells
2039 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
2040 * the height is determined by the properties again.
2043 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2044 gint number_of_rows)
2046 GtkCellRendererTextPrivate *priv;
2047 GtkCellRenderer *cell;
2049 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2050 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2052 cell = GTK_CELL_RENDERER (renderer);
2053 priv = renderer->priv;
2055 if (number_of_rows == -1)
2059 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2060 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2064 priv->fixed_height_rows = number_of_rows;
2065 priv->calc_fixed_height = TRUE;
2070 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2075 GtkCellRendererTextPrivate *priv;
2076 GtkCellRendererText *celltext;
2077 GtkStyleContext *style_context;
2078 const PangoFontDescription *font_desc;
2079 PangoLayout *layout;
2080 PangoContext *context;
2081 PangoFontMetrics *metrics;
2082 PangoRectangle rect;
2083 gint char_width, text_width, ellipsize_chars, xpad;
2084 gint min_width, nat_width;
2086 /* "width-chars" Hard-coded minimum width:
2087 * - minimum size should be MAX (width-chars, strlen ("..."));
2088 * - natural size should be MAX (width-chars, strlen (label->text));
2090 * "wrap-width" User specified natural wrap width
2091 * - minimum size should be MAX (width-chars, 0)
2092 * - natural size should be MIN (wrap-width, strlen (label->text))
2095 celltext = GTK_CELL_RENDERER_TEXT (cell);
2096 priv = celltext->priv;
2098 style_context = gtk_widget_get_style_context (widget);
2100 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2102 layout = get_layout (celltext, widget, NULL, 0);
2104 /* Fetch the length of the complete unwrapped text */
2105 pango_layout_set_width (layout, -1);
2106 pango_layout_get_extents (layout, NULL, &rect);
2107 text_width = rect.width;
2109 /* Fetch the average size of a charachter */
2110 context = pango_layout_get_context (layout);
2111 font_desc = gtk_style_context_get_font (style_context, 0);
2112 metrics = pango_context_get_metrics (context, font_desc,
2113 pango_context_get_language (context));
2115 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2117 pango_font_metrics_unref (metrics);
2118 g_object_unref (layout);
2120 /* enforce minimum width for ellipsized labels at ~3 chars */
2121 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2122 ellipsize_chars = 3;
2124 ellipsize_chars = 0;
2126 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2127 min_width = xpad * 2 +
2128 MIN (PANGO_PIXELS_CEIL (text_width),
2129 (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
2130 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2131 else if (priv->wrap_width > -1)
2132 min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
2134 min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
2136 if (priv->width_chars > 0)
2137 nat_width = xpad * 2 +
2138 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
2140 nat_width = xpad * 2 + PANGO_PIXELS_CEIL (text_width);
2142 nat_width = MAX (nat_width, min_width);
2144 if (priv->max_width_chars > 0)
2146 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2148 min_width = MIN (min_width, max_width);
2149 nat_width = MIN (nat_width, max_width);
2153 *minimum_size = min_width;
2156 *natural_size = nat_width;
2160 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2163 gint *minimum_height,
2164 gint *natural_height)
2166 GtkCellRendererText *celltext;
2167 PangoLayout *layout;
2168 gint text_height, xpad, ypad;
2171 celltext = GTK_CELL_RENDERER_TEXT (cell);
2173 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2175 layout = get_layout (celltext, widget, NULL, 0);
2177 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2178 pango_layout_get_pixel_size (layout, NULL, &text_height);
2181 *minimum_height = text_height + ypad * 2;
2184 *natural_height = text_height + ypad * 2;
2186 g_object_unref (layout);
2190 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2197 /* Thankfully cell renderers dont rotate, so they only have to do
2198 * height-for-width and not the opposite. Here we have only to return
2199 * the height for the base minimum width of the renderer.
2201 * Note this code path wont be followed by GtkTreeView which is
2202 * height-for-width specifically.
2204 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2205 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2206 minimum_size, natural_size);
2210 gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
2212 GtkCellRendererState flags,
2213 const GdkRectangle *cell_area,
2214 GdkRectangle *aligned_area)
2216 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
2217 PangoLayout *layout;
2221 layout = get_layout (celltext, widget, cell_area, flags);
2222 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset,
2223 &aligned_area->width, &aligned_area->height);
2225 aligned_area->x = cell_area->x + x_offset;
2226 aligned_area->y = cell_area->y + y_offset;
2228 g_object_unref (layout);