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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcellrenderertext.h"
26 #include "gtkeditable.h"
28 #include "gtksizerequest.h"
29 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtktreeprivate.h"
36 * SECTION:gtkcellrenderertext
37 * @Short_description: Renders text in a cell
38 * @Title: GtkCellRendererText
40 * A #GtkCellRendererText renders a given text in its cell, using the font, color and
41 * style information provided by its properties. The text will be ellipsized if it is
42 * too long and the #GtkCellRendererText:ellipsize property allows it.
44 * If the #GtkCellRenderer:mode is %GTK_CELL_RENDERER_MODE_EDITABLE,
45 * the #GtkCellRendererText allows to edit its text using an entry.
49 static void gtk_cell_renderer_text_finalize (GObject *object);
51 static void gtk_cell_renderer_text_get_property (GObject *object,
55 static void gtk_cell_renderer_text_set_property (GObject *object,
59 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
62 const GdkRectangle *background_area,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
66 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
70 const GdkRectangle *background_area,
71 const GdkRectangle *cell_area,
72 GtkCellRendererState flags);
74 static void gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
78 static void gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
82 static void gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
86 gint *natural_height);
87 static void gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
89 GtkCellRendererState flags,
90 const GdkRectangle *cell_area,
91 GdkRectangle *aligned_area);
106 PROP_SINGLE_PARAGRAPH_MODE,
108 PROP_MAX_WIDTH_CHARS,
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;
179 gint fixed_height_rows;
181 gint max_width_chars;
184 guint in_entry_menu : 1;
185 guint strikethrough : 1;
188 guint foreground_set : 1;
189 guint background_set : 1;
190 guint underline_set : 1;
192 guint strikethrough_set : 1;
193 guint editable_set : 1;
194 guint calc_fixed_height : 1;
195 guint single_paragraph : 1;
196 guint language_set : 1;
197 guint markup_set : 1;
198 guint ellipsize_set : 1;
202 gulong populate_popup_id;
203 gulong entry_menu_popdown_timeout;
206 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
209 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
211 GtkCellRendererTextPrivate *priv;
212 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
214 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
215 GTK_TYPE_CELL_RENDERER_TEXT,
216 GtkCellRendererTextPrivate);
217 priv = celltext->priv;
219 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
220 gtk_cell_renderer_set_padding (cell, 2, 2);
221 priv->font_scale = 1.0;
222 priv->fixed_height_rows = -1;
223 priv->font = pango_font_description_new ();
225 priv->width_chars = -1;
226 priv->max_width_chars = -1;
227 priv->wrap_width = -1;
228 priv->wrap_mode = PANGO_WRAP_CHAR;
229 priv->align = PANGO_ALIGN_LEFT;
230 priv->align_set = FALSE;
234 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
236 GObjectClass *object_class = G_OBJECT_CLASS (class);
237 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
239 object_class->finalize = gtk_cell_renderer_text_finalize;
241 object_class->get_property = gtk_cell_renderer_text_get_property;
242 object_class->set_property = gtk_cell_renderer_text_set_property;
244 cell_class->render = gtk_cell_renderer_text_render;
245 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
246 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
247 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
248 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
249 cell_class->get_aligned_area = gtk_cell_renderer_text_get_aligned_area;
251 g_object_class_install_property (object_class,
253 g_param_spec_string ("text",
255 P_("Text to render"),
257 GTK_PARAM_READWRITE));
259 g_object_class_install_property (object_class,
261 g_param_spec_string ("markup",
263 P_("Marked up text to render"),
265 GTK_PARAM_WRITABLE));
267 g_object_class_install_property (object_class,
269 g_param_spec_boxed ("attributes",
271 P_("A list of style attributes to apply to the text of the renderer"),
272 PANGO_TYPE_ATTR_LIST,
273 GTK_PARAM_READWRITE));
275 g_object_class_install_property (object_class,
276 PROP_SINGLE_PARAGRAPH_MODE,
277 g_param_spec_boolean ("single-paragraph-mode",
278 P_("Single Paragraph Mode"),
279 P_("Whether to keep all text in a single paragraph"),
281 GTK_PARAM_READWRITE));
284 g_object_class_install_property (object_class,
286 g_param_spec_string ("background",
287 P_("Background color name"),
288 P_("Background color as a string"),
290 GTK_PARAM_WRITABLE));
292 g_object_class_install_property (object_class,
294 g_param_spec_boxed ("background-gdk",
295 P_("Background color"),
296 P_("Background color as a GdkColor"),
298 GTK_PARAM_READWRITE));
301 * GtkCellRendererText:background-rgba:
303 * Background color as a #GdkRGBA
307 g_object_class_install_property (object_class,
308 PROP_BACKGROUND_RGBA,
309 g_param_spec_boxed ("background-rgba",
310 P_("Background color as RGBA"),
311 P_("Background color as a GdkRGBA"),
313 GTK_PARAM_READWRITE));
314 g_object_class_install_property (object_class,
316 g_param_spec_string ("foreground",
317 P_("Foreground color name"),
318 P_("Foreground color as a string"),
320 GTK_PARAM_WRITABLE));
322 g_object_class_install_property (object_class,
324 g_param_spec_boxed ("foreground-gdk",
325 P_("Foreground color"),
326 P_("Foreground color as a GdkColor"),
328 GTK_PARAM_READWRITE));
331 * GtkCellRendererText:foreground-rgba:
333 * Foreground color as a #GdkRGBA
337 g_object_class_install_property (object_class,
338 PROP_FOREGROUND_RGBA,
339 g_param_spec_boxed ("foreground-rgba",
340 P_("Foreground color as RGBA"),
341 P_("Foreground color as a GdkRGBA"),
343 GTK_PARAM_READWRITE));
346 g_object_class_install_property (object_class,
348 g_param_spec_boolean ("editable",
350 P_("Whether the text can be modified by the user"),
352 GTK_PARAM_READWRITE));
354 g_object_class_install_property (object_class,
356 g_param_spec_string ("font",
358 P_("Font description as a string, e.g. \"Sans Italic 12\""),
360 GTK_PARAM_READWRITE));
362 g_object_class_install_property (object_class,
364 g_param_spec_boxed ("font-desc",
366 P_("Font description as a PangoFontDescription struct"),
367 PANGO_TYPE_FONT_DESCRIPTION,
368 GTK_PARAM_READWRITE));
371 g_object_class_install_property (object_class,
373 g_param_spec_string ("family",
375 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
377 GTK_PARAM_READWRITE));
379 g_object_class_install_property (object_class,
381 g_param_spec_enum ("style",
386 GTK_PARAM_READWRITE));
388 g_object_class_install_property (object_class,
390 g_param_spec_enum ("variant",
394 PANGO_VARIANT_NORMAL,
395 GTK_PARAM_READWRITE));
397 g_object_class_install_property (object_class,
399 g_param_spec_int ("weight",
405 GTK_PARAM_READWRITE));
407 g_object_class_install_property (object_class,
409 g_param_spec_enum ("stretch",
413 PANGO_STRETCH_NORMAL,
414 GTK_PARAM_READWRITE));
416 g_object_class_install_property (object_class,
418 g_param_spec_int ("size",
424 GTK_PARAM_READWRITE));
426 g_object_class_install_property (object_class,
428 g_param_spec_double ("size-points",
430 P_("Font size in points"),
434 GTK_PARAM_READWRITE));
436 g_object_class_install_property (object_class,
438 g_param_spec_double ("scale",
440 P_("Font scaling factor"),
444 GTK_PARAM_READWRITE));
446 g_object_class_install_property (object_class,
448 g_param_spec_int ("rise",
450 P_("Offset of text above the baseline "
451 "(below the baseline if rise is negative)"),
455 GTK_PARAM_READWRITE));
458 g_object_class_install_property (object_class,
460 g_param_spec_boolean ("strikethrough",
462 P_("Whether to strike through the text"),
464 GTK_PARAM_READWRITE));
466 g_object_class_install_property (object_class,
468 g_param_spec_enum ("underline",
470 P_("Style of underline for this text"),
471 PANGO_TYPE_UNDERLINE,
472 PANGO_UNDERLINE_NONE,
473 GTK_PARAM_READWRITE));
475 g_object_class_install_property (object_class,
477 g_param_spec_string ("language",
479 P_("The language this text is in, as an ISO code. "
480 "Pango can use this as a hint when rendering the text. "
481 "If you don't understand this parameter, you probably don't need it"),
483 GTK_PARAM_READWRITE));
487 * GtkCellRendererText:ellipsize:
489 * Specifies the preferred place to ellipsize the string, if the cell renderer
490 * does not have enough room to display the entire string. Setting it to
491 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
492 * for another way of making the text fit into a given width.
496 g_object_class_install_property (object_class,
498 g_param_spec_enum ("ellipsize",
500 P_("The preferred place to ellipsize the string, "
501 "if the cell renderer does not have enough room "
502 "to display the entire string"),
503 PANGO_TYPE_ELLIPSIZE_MODE,
504 PANGO_ELLIPSIZE_NONE,
505 GTK_PARAM_READWRITE));
508 * GtkCellRendererText:width-chars:
510 * The desired width of the cell, in characters. If this property is set to
511 * -1, the width will be calculated automatically, otherwise the cell will
512 * request either 3 characters or the property value, whichever is greater.
516 g_object_class_install_property (object_class,
518 g_param_spec_int ("width-chars",
519 P_("Width In Characters"),
520 P_("The desired width of the label, in characters"),
524 GTK_PARAM_READWRITE));
528 * GtkCellRendererText:max-width-chars:
530 * The desired maximum width of the cell, in characters. If this property
531 * is set to -1, the width will be calculated automatically.
533 * For cell renderers that ellipsize or wrap text; this property
534 * controls the maximum reported width of the cell. The
535 * cell should not receive any greater allocation unless it is
536 * set to expand in its #GtkCellLayout and all of the cell's siblings
537 * have received their natural width.
541 g_object_class_install_property (object_class,
542 PROP_MAX_WIDTH_CHARS,
543 g_param_spec_int ("max-width-chars",
544 P_("Maximum Width In Characters"),
545 P_("The maximum width of the cell, in characters"),
549 GTK_PARAM_READWRITE));
552 * GtkCellRendererText:wrap-mode:
554 * Specifies how to break the string into multiple lines, if the cell
555 * renderer does not have enough room to display the entire string.
556 * This property has no effect unless the wrap-width property is set.
560 g_object_class_install_property (object_class,
562 g_param_spec_enum ("wrap-mode",
564 P_("How to break the string into multiple lines, "
565 "if the cell renderer does not have enough room "
566 "to display the entire string"),
567 PANGO_TYPE_WRAP_MODE,
569 GTK_PARAM_READWRITE));
572 * GtkCellRendererText:wrap-width:
574 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
575 * be used to influence at what character positions the line breaks can be placed.
576 * Setting wrap-width to -1 turns wrapping off.
580 g_object_class_install_property (object_class,
582 g_param_spec_int ("wrap-width",
584 P_("The width at which the text is wrapped"),
588 GTK_PARAM_READWRITE));
591 * GtkCellRendererText:alignment:
593 * Specifies how to align the lines of text with respect to each other.
595 * Note that this property describes how to align the lines of text in
596 * case there are several of them. The "xalign" property of #GtkCellRenderer,
597 * on the other hand, sets the horizontal alignment of the whole text.
601 g_object_class_install_property (object_class,
603 g_param_spec_enum ("alignment",
605 P_("How to align the lines"),
606 PANGO_TYPE_ALIGNMENT,
608 GTK_PARAM_READWRITE));
612 /* Style props are set or not */
614 #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))
616 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
617 P_("Background set"),
618 P_("Whether this tag affects the background color"));
620 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
621 P_("Foreground set"),
622 P_("Whether this tag affects the foreground color"));
624 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
625 P_("Editability set"),
626 P_("Whether this tag affects text editability"));
628 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
629 P_("Font family set"),
630 P_("Whether this tag affects the font family"));
632 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
633 P_("Font style set"),
634 P_("Whether this tag affects the font style"));
636 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
637 P_("Font variant set"),
638 P_("Whether this tag affects the font variant"));
640 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
641 P_("Font weight set"),
642 P_("Whether this tag affects the font weight"));
644 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
645 P_("Font stretch set"),
646 P_("Whether this tag affects the font stretch"));
648 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
650 P_("Whether this tag affects the font size"));
652 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
653 P_("Font scale set"),
654 P_("Whether this tag scales the font size by a factor"));
656 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
658 P_("Whether this tag affects the rise"));
660 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
661 P_("Strikethrough set"),
662 P_("Whether this tag affects strikethrough"));
664 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
666 P_("Whether this tag affects underlining"));
668 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
670 P_("Whether this tag affects the language the text is rendered as"));
672 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
674 P_("Whether this tag affects the ellipsize mode"));
676 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
678 P_("Whether this tag affects the alignment mode"));
681 * GtkCellRendererText::edited
682 * @renderer: the object which received the signal
683 * @path: the path identifying the edited cell
684 * @new_text: the new text
686 * This signal is emitted after @renderer has been edited.
688 * It is the responsibility of the application to update the model
689 * and store @new_text at the position indicated by @path.
691 text_cell_renderer_signals [EDITED] =
692 g_signal_new (I_("edited"),
693 G_OBJECT_CLASS_TYPE (object_class),
695 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
697 _gtk_marshal_VOID__STRING_STRING,
702 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
706 gtk_cell_renderer_text_finalize (GObject *object)
708 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
709 GtkCellRendererTextPrivate *priv = celltext->priv;
711 pango_font_description_free (priv->font);
715 if (priv->extra_attrs)
716 pango_attr_list_unref (priv->extra_attrs);
719 g_object_unref (priv->language);
721 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
725 get_property_font_set_mask (guint prop_id)
729 case PROP_FAMILY_SET:
730 return PANGO_FONT_MASK_FAMILY;
732 return PANGO_FONT_MASK_STYLE;
733 case PROP_VARIANT_SET:
734 return PANGO_FONT_MASK_VARIANT;
735 case PROP_WEIGHT_SET:
736 return PANGO_FONT_MASK_WEIGHT;
737 case PROP_STRETCH_SET:
738 return PANGO_FONT_MASK_STRETCH;
740 return PANGO_FONT_MASK_SIZE;
747 gtk_cell_renderer_text_get_property (GObject *object,
752 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
753 GtkCellRendererTextPrivate *priv = celltext->priv;
758 g_value_set_string (value, priv->text);
761 case PROP_ATTRIBUTES:
762 g_value_set_boxed (value, priv->extra_attrs);
765 case PROP_SINGLE_PARAGRAPH_MODE:
766 g_value_set_boolean (value, priv->single_paragraph);
769 case PROP_BACKGROUND_GDK:
773 color.red = (guint16) (priv->background.red * 65535);
774 color.green = (guint16) (priv->background.green * 65535);
775 color.blue = (guint16) (priv->background.blue * 65535);
777 g_value_set_boxed (value, &color);
781 case PROP_FOREGROUND_GDK:
785 color.red = (guint16) (priv->foreground.red * 65535);
786 color.green = (guint16) (priv->foreground.green * 65535);
787 color.blue = (guint16) (priv->foreground.blue * 65535);
789 g_value_set_boxed (value, &color);
793 case PROP_BACKGROUND_RGBA:
794 g_value_set_boxed (value, &priv->background);
797 case PROP_FOREGROUND_RGBA:
798 g_value_set_boxed (value, &priv->foreground);
802 g_value_take_string (value, pango_font_description_to_string (priv->font));
806 g_value_set_boxed (value, priv->font);
810 g_value_set_string (value, pango_font_description_get_family (priv->font));
814 g_value_set_enum (value, pango_font_description_get_style (priv->font));
818 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
822 g_value_set_int (value, pango_font_description_get_weight (priv->font));
826 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
830 g_value_set_int (value, pango_font_description_get_size (priv->font));
833 case PROP_SIZE_POINTS:
834 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
838 g_value_set_double (value, priv->font_scale);
842 g_value_set_boolean (value, priv->editable);
845 case PROP_STRIKETHROUGH:
846 g_value_set_boolean (value, priv->strikethrough);
850 g_value_set_enum (value, priv->underline_style);
854 g_value_set_int (value, priv->rise);
858 g_value_set_static_string (value, pango_language_to_string (priv->language));
862 g_value_set_enum (value, priv->ellipsize);
866 g_value_set_enum (value, priv->wrap_mode);
869 case PROP_WRAP_WIDTH:
870 g_value_set_int (value, priv->wrap_width);
874 g_value_set_enum (value, priv->align);
877 case PROP_BACKGROUND_SET:
878 g_value_set_boolean (value, priv->background_set);
881 case PROP_FOREGROUND_SET:
882 g_value_set_boolean (value, priv->foreground_set);
885 case PROP_FAMILY_SET:
887 case PROP_VARIANT_SET:
888 case PROP_WEIGHT_SET:
889 case PROP_STRETCH_SET:
892 PangoFontMask mask = get_property_font_set_mask (param_id);
893 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
899 g_value_set_boolean (value, priv->scale_set);
902 case PROP_EDITABLE_SET:
903 g_value_set_boolean (value, priv->editable_set);
906 case PROP_STRIKETHROUGH_SET:
907 g_value_set_boolean (value, priv->strikethrough_set);
910 case PROP_UNDERLINE_SET:
911 g_value_set_boolean (value, priv->underline_set);
915 g_value_set_boolean (value, priv->rise_set);
918 case PROP_LANGUAGE_SET:
919 g_value_set_boolean (value, priv->language_set);
922 case PROP_ELLIPSIZE_SET:
923 g_value_set_boolean (value, priv->ellipsize_set);
927 g_value_set_boolean (value, priv->align_set);
930 case PROP_WIDTH_CHARS:
931 g_value_set_int (value, priv->width_chars);
934 case PROP_MAX_WIDTH_CHARS:
935 g_value_set_int (value, priv->max_width_chars);
938 case PROP_BACKGROUND:
939 case PROP_FOREGROUND:
942 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
949 set_bg_color (GtkCellRendererText *celltext,
952 GtkCellRendererTextPrivate *priv = celltext->priv;
956 if (!priv->background_set)
958 priv->background_set = TRUE;
959 g_object_notify (G_OBJECT (celltext), "background-set");
962 priv->background = *rgba;
966 if (priv->background_set)
968 priv->background_set = FALSE;
969 g_object_notify (G_OBJECT (celltext), "background-set");
976 set_fg_color (GtkCellRendererText *celltext,
979 GtkCellRendererTextPrivate *priv = celltext->priv;
983 if (!priv->foreground_set)
985 priv->foreground_set = TRUE;
986 g_object_notify (G_OBJECT (celltext), "foreground-set");
989 priv->foreground = *rgba;
993 if (priv->foreground_set)
995 priv->foreground_set = FALSE;
996 g_object_notify (G_OBJECT (celltext), "foreground-set");
1001 static PangoFontMask
1002 set_font_desc_fields (PangoFontDescription *desc,
1003 PangoFontMask to_set)
1005 PangoFontMask changed_mask = 0;
1007 if (to_set & PANGO_FONT_MASK_FAMILY)
1009 const char *family = pango_font_description_get_family (desc);
1013 changed_mask |= PANGO_FONT_MASK_FAMILY;
1016 pango_font_description_set_family (desc, family);
1018 if (to_set & PANGO_FONT_MASK_STYLE)
1019 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1020 if (to_set & PANGO_FONT_MASK_VARIANT)
1021 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1022 if (to_set & PANGO_FONT_MASK_WEIGHT)
1023 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1024 if (to_set & PANGO_FONT_MASK_STRETCH)
1025 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1026 if (to_set & PANGO_FONT_MASK_SIZE)
1028 gint size = pango_font_description_get_size (desc);
1031 size = 10 * PANGO_SCALE;
1032 changed_mask |= PANGO_FONT_MASK_SIZE;
1035 pango_font_description_set_size (desc, size);
1038 return changed_mask;
1042 notify_set_changed (GObject *object,
1043 PangoFontMask changed_mask)
1045 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1046 g_object_notify (object, "family-set");
1047 if (changed_mask & PANGO_FONT_MASK_STYLE)
1048 g_object_notify (object, "style-set");
1049 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1050 g_object_notify (object, "variant-set");
1051 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1052 g_object_notify (object, "weight-set");
1053 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1054 g_object_notify (object, "stretch-set");
1055 if (changed_mask & PANGO_FONT_MASK_SIZE)
1056 g_object_notify (object, "size-set");
1060 notify_fields_changed (GObject *object,
1061 PangoFontMask changed_mask)
1063 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1064 g_object_notify (object, "family");
1065 if (changed_mask & PANGO_FONT_MASK_STYLE)
1066 g_object_notify (object, "style");
1067 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1068 g_object_notify (object, "variant");
1069 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1070 g_object_notify (object, "weight");
1071 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1072 g_object_notify (object, "stretch");
1073 if (changed_mask & PANGO_FONT_MASK_SIZE)
1074 g_object_notify (object, "size");
1078 set_font_description (GtkCellRendererText *celltext,
1079 PangoFontDescription *font_desc)
1081 GtkCellRendererTextPrivate *priv = celltext->priv;
1082 GObject *object = G_OBJECT (celltext);
1083 PangoFontDescription *new_font_desc;
1084 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1087 new_font_desc = pango_font_description_copy (font_desc);
1089 new_font_desc = pango_font_description_new ();
1091 old_mask = pango_font_description_get_set_fields (priv->font);
1092 new_mask = pango_font_description_get_set_fields (new_font_desc);
1094 changed_mask = old_mask | new_mask;
1095 set_changed_mask = old_mask ^ new_mask;
1097 pango_font_description_free (priv->font);
1098 priv->font = new_font_desc;
1100 g_object_freeze_notify (object);
1102 g_object_notify (object, "font-desc");
1103 g_object_notify (object, "font");
1105 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1106 g_object_notify (object, "family");
1107 if (changed_mask & PANGO_FONT_MASK_STYLE)
1108 g_object_notify (object, "style");
1109 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1110 g_object_notify (object, "variant");
1111 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1112 g_object_notify (object, "weight");
1113 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1114 g_object_notify (object, "stretch");
1115 if (changed_mask & PANGO_FONT_MASK_SIZE)
1117 g_object_notify (object, "size");
1118 g_object_notify (object, "size-points");
1121 notify_set_changed (object, set_changed_mask);
1123 g_object_thaw_notify (object);
1127 gtk_cell_renderer_text_set_property (GObject *object,
1129 const GValue *value,
1132 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1133 GtkCellRendererTextPrivate *priv = celltext->priv;
1138 g_free (priv->text);
1140 if (priv->markup_set)
1142 if (priv->extra_attrs)
1143 pango_attr_list_unref (priv->extra_attrs);
1144 priv->extra_attrs = NULL;
1145 priv->markup_set = FALSE;
1148 priv->text = g_value_dup_string (value);
1149 g_object_notify (object, "text");
1152 case PROP_ATTRIBUTES:
1153 if (priv->extra_attrs)
1154 pango_attr_list_unref (priv->extra_attrs);
1156 priv->extra_attrs = g_value_get_boxed (value);
1157 if (priv->extra_attrs)
1158 pango_attr_list_ref (priv->extra_attrs);
1164 GError *error = NULL;
1165 PangoAttrList *attrs = NULL;
1167 str = g_value_get_string (value);
1168 if (str && !pango_parse_markup (str,
1176 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1178 g_error_free (error);
1182 g_free (priv->text);
1184 if (priv->extra_attrs)
1185 pango_attr_list_unref (priv->extra_attrs);
1188 priv->extra_attrs = attrs;
1189 priv->markup_set = TRUE;
1193 case PROP_SINGLE_PARAGRAPH_MODE:
1194 priv->single_paragraph = g_value_get_boolean (value);
1197 case PROP_BACKGROUND:
1201 if (!g_value_get_string (value))
1202 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1203 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1204 set_bg_color (celltext, &rgba);
1206 g_warning ("Don't know color `%s'", g_value_get_string (value));
1208 g_object_notify (object, "background-gdk");
1212 case PROP_FOREGROUND:
1216 if (!g_value_get_string (value))
1217 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1218 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1219 set_fg_color (celltext, &rgba);
1221 g_warning ("Don't know color `%s'", g_value_get_string (value));
1223 g_object_notify (object, "foreground-gdk");
1227 case PROP_BACKGROUND_GDK:
1231 color = g_value_get_boxed (value);
1236 rgba.red = color->red / 65535.;
1237 rgba.green = color->green / 65535.;
1238 rgba.blue = color->blue / 65535.;
1241 set_bg_color (celltext, &rgba);
1245 set_bg_color (celltext, NULL);
1250 case PROP_FOREGROUND_GDK:
1254 color = g_value_get_boxed (value);
1259 rgba.red = color->red / 65535.;
1260 rgba.green = color->green / 65535.;
1261 rgba.blue = color->blue / 65535.;
1264 set_fg_color (celltext, &rgba);
1268 set_fg_color (celltext, NULL);
1273 case PROP_BACKGROUND_RGBA:
1274 /* This notifies the GObject itself. */
1275 set_bg_color (celltext, g_value_get_boxed (value));
1278 case PROP_FOREGROUND_RGBA:
1279 /* This notifies the GObject itself. */
1280 set_fg_color (celltext, g_value_get_boxed (value));
1285 PangoFontDescription *font_desc = NULL;
1288 name = g_value_get_string (value);
1291 font_desc = pango_font_description_from_string (name);
1293 set_font_description (celltext, font_desc);
1295 pango_font_description_free (font_desc);
1297 if (priv->fixed_height_rows != -1)
1298 priv->calc_fixed_height = TRUE;
1302 case PROP_FONT_DESC:
1303 set_font_description (celltext, g_value_get_boxed (value));
1305 if (priv->fixed_height_rows != -1)
1306 priv->calc_fixed_height = TRUE;
1315 case PROP_SIZE_POINTS:
1317 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1322 pango_font_description_set_family (priv->font,
1323 g_value_get_string (value));
1326 pango_font_description_set_style (priv->font,
1327 g_value_get_enum (value));
1330 pango_font_description_set_variant (priv->font,
1331 g_value_get_enum (value));
1334 pango_font_description_set_weight (priv->font,
1335 g_value_get_int (value));
1338 pango_font_description_set_stretch (priv->font,
1339 g_value_get_enum (value));
1342 pango_font_description_set_size (priv->font,
1343 g_value_get_int (value));
1344 g_object_notify (object, "size-points");
1346 case PROP_SIZE_POINTS:
1347 pango_font_description_set_size (priv->font,
1348 g_value_get_double (value) * PANGO_SCALE);
1349 g_object_notify (object, "size");
1353 if (priv->fixed_height_rows != -1)
1354 priv->calc_fixed_height = TRUE;
1356 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1357 g_object_notify (object, "font-desc");
1358 g_object_notify (object, "font");
1364 priv->font_scale = g_value_get_double (value);
1365 priv->scale_set = TRUE;
1366 if (priv->fixed_height_rows != -1)
1367 priv->calc_fixed_height = TRUE;
1368 g_object_notify (object, "scale-set");
1372 priv->editable = g_value_get_boolean (value);
1373 priv->editable_set = TRUE;
1375 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1377 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1378 g_object_notify (object, "editable-set");
1381 case PROP_STRIKETHROUGH:
1382 priv->strikethrough = g_value_get_boolean (value);
1383 priv->strikethrough_set = TRUE;
1384 g_object_notify (object, "strikethrough-set");
1387 case PROP_UNDERLINE:
1388 priv->underline_style = g_value_get_enum (value);
1389 priv->underline_set = TRUE;
1390 g_object_notify (object, "underline-set");
1395 priv->rise = g_value_get_int (value);
1396 priv->rise_set = TRUE;
1397 g_object_notify (object, "rise-set");
1398 if (priv->fixed_height_rows != -1)
1399 priv->calc_fixed_height = TRUE;
1403 priv->language_set = TRUE;
1405 g_object_unref (priv->language);
1406 priv->language = pango_language_from_string (g_value_get_string (value));
1407 g_object_notify (object, "language-set");
1410 case PROP_ELLIPSIZE:
1411 priv->ellipsize = g_value_get_enum (value);
1412 priv->ellipsize_set = TRUE;
1413 g_object_notify (object, "ellipsize-set");
1416 case PROP_WRAP_MODE:
1417 priv->wrap_mode = g_value_get_enum (value);
1420 case PROP_WRAP_WIDTH:
1421 priv->wrap_width = g_value_get_int (value);
1424 case PROP_WIDTH_CHARS:
1425 priv->width_chars = g_value_get_int (value);
1428 case PROP_MAX_WIDTH_CHARS:
1429 priv->max_width_chars = g_value_get_int (value);
1433 priv->align = g_value_get_enum (value);
1434 priv->align_set = TRUE;
1435 g_object_notify (object, "align-set");
1438 case PROP_BACKGROUND_SET:
1439 priv->background_set = g_value_get_boolean (value);
1442 case PROP_FOREGROUND_SET:
1443 priv->foreground_set = g_value_get_boolean (value);
1446 case PROP_FAMILY_SET:
1447 case PROP_STYLE_SET:
1448 case PROP_VARIANT_SET:
1449 case PROP_WEIGHT_SET:
1450 case PROP_STRETCH_SET:
1452 if (!g_value_get_boolean (value))
1454 pango_font_description_unset_fields (priv->font,
1455 get_property_font_set_mask (param_id));
1459 PangoFontMask changed_mask;
1461 changed_mask = set_font_desc_fields (priv->font,
1462 get_property_font_set_mask (param_id));
1463 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1467 case PROP_SCALE_SET:
1468 priv->scale_set = g_value_get_boolean (value);
1471 case PROP_EDITABLE_SET:
1472 priv->editable_set = g_value_get_boolean (value);
1475 case PROP_STRIKETHROUGH_SET:
1476 priv->strikethrough_set = g_value_get_boolean (value);
1479 case PROP_UNDERLINE_SET:
1480 priv->underline_set = g_value_get_boolean (value);
1484 priv->rise_set = g_value_get_boolean (value);
1487 case PROP_LANGUAGE_SET:
1488 priv->language_set = g_value_get_boolean (value);
1491 case PROP_ELLIPSIZE_SET:
1492 priv->ellipsize_set = g_value_get_boolean (value);
1495 case PROP_ALIGN_SET:
1496 priv->align_set = g_value_get_boolean (value);
1500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1506 * gtk_cell_renderer_text_new:
1508 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1509 * object properties. Object properties can be
1510 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1511 * you can bind a property to a value in a #GtkTreeModel. For example,
1512 * you can bind the "text" property on the cell renderer to a string
1513 * value in the model, thus rendering a different string in each row
1514 * of the #GtkTreeView
1516 * Return value: the new cell renderer
1519 gtk_cell_renderer_text_new (void)
1521 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1525 add_attr (PangoAttrList *attr_list,
1526 PangoAttribute *attr)
1528 attr->start_index = 0;
1529 attr->end_index = G_MAXINT;
1531 pango_attr_list_insert (attr_list, attr);
1535 get_layout (GtkCellRendererText *celltext,
1537 const GdkRectangle *cell_area,
1538 GtkCellRendererState flags)
1540 GtkCellRendererTextPrivate *priv = celltext->priv;
1541 PangoAttrList *attr_list;
1542 PangoLayout *layout;
1543 PangoUnderline uline;
1546 layout = gtk_widget_create_pango_layout (widget, priv->text);
1548 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1550 if (priv->extra_attrs)
1551 attr_list = pango_attr_list_copy (priv->extra_attrs);
1553 attr_list = pango_attr_list_new ();
1555 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1559 /* Add options that affect appearance but not size */
1561 /* note that background doesn't go here, since it affects
1562 * background_area not the PangoLayout area
1565 if (priv->foreground_set
1566 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1570 color.red = (guint16) (priv->foreground.red * 65535);
1571 color.green = (guint16) (priv->foreground.green * 65535);
1572 color.blue = (guint16) (priv->foreground.blue * 65535);
1574 add_attr (attr_list,
1575 pango_attr_foreground_new (color.red, color.green, color.blue));
1578 if (priv->strikethrough_set)
1579 add_attr (attr_list,
1580 pango_attr_strikethrough_new (priv->strikethrough));
1583 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1585 if (priv->scale_set &&
1586 priv->font_scale != 1.0)
1587 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1589 if (priv->underline_set)
1590 uline = priv->underline_style;
1592 uline = PANGO_UNDERLINE_NONE;
1594 if (priv->language_set)
1595 add_attr (attr_list, pango_attr_language_new (priv->language));
1597 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1601 case PANGO_UNDERLINE_NONE:
1602 uline = PANGO_UNDERLINE_SINGLE;
1605 case PANGO_UNDERLINE_SINGLE:
1606 uline = PANGO_UNDERLINE_DOUBLE;
1614 if (uline != PANGO_UNDERLINE_NONE)
1615 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1618 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1620 /* Now apply the attributes as they will effect the outcome
1621 * of pango_layout_get_extents() */
1622 pango_layout_set_attributes (layout, attr_list);
1623 pango_attr_list_unref (attr_list);
1625 if (priv->ellipsize_set)
1626 pango_layout_set_ellipsize (layout, priv->ellipsize);
1628 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1630 if (priv->wrap_width != -1)
1632 PangoRectangle rect;
1633 gint width, text_width;
1635 pango_layout_get_extents (layout, NULL, &rect);
1636 text_width = rect.width;
1639 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1641 width = priv->wrap_width * PANGO_SCALE;
1643 width = MIN (width, text_width);
1645 pango_layout_set_width (layout, width);
1646 pango_layout_set_wrap (layout, priv->wrap_mode);
1650 pango_layout_set_width (layout, -1);
1651 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1654 if (priv->align_set)
1655 pango_layout_set_alignment (layout, priv->align);
1658 PangoAlignment align;
1660 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1661 align = PANGO_ALIGN_RIGHT;
1663 align = PANGO_ALIGN_LEFT;
1665 pango_layout_set_alignment (layout, align);
1673 get_size (GtkCellRenderer *cell,
1675 const GdkRectangle *cell_area,
1676 PangoLayout *layout,
1682 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1683 GtkCellRendererTextPrivate *priv = celltext->priv;
1684 PangoRectangle rect;
1686 gint cell_width, cell_height;
1688 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1690 if (priv->calc_fixed_height)
1692 GtkStyleContext *style_context;
1693 GtkStateFlags state;
1694 PangoContext *context;
1695 PangoFontMetrics *metrics;
1696 PangoFontDescription *font_desc;
1699 style_context = gtk_widget_get_style_context (widget);
1700 state = gtk_widget_get_state_flags (widget);
1702 font_desc = pango_font_description_copy_static (gtk_style_context_get_font (style_context, state));
1703 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1705 if (priv->scale_set)
1706 pango_font_description_set_size (font_desc,
1707 priv->font_scale * pango_font_description_get_size (font_desc));
1709 context = gtk_widget_get_pango_context (widget);
1711 metrics = pango_context_get_metrics (context,
1713 pango_context_get_language (context));
1714 row_height = (pango_font_metrics_get_ascent (metrics) +
1715 pango_font_metrics_get_descent (metrics));
1716 pango_font_metrics_unref (metrics);
1718 pango_font_description_free (font_desc);
1720 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1722 gtk_cell_renderer_set_fixed_size (cell,
1723 cell_width, 2 * ypad +
1724 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1728 *height = cell_height;
1731 priv->calc_fixed_height = FALSE;
1737 g_object_ref (layout);
1739 layout = get_layout (celltext, widget, NULL, 0);
1741 pango_layout_get_pixel_extents (layout, NULL, &rect);
1745 gfloat xalign, yalign;
1747 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1749 rect.height = MIN (rect.height, cell_area->height - 2 * ypad);
1750 rect.width = MIN (rect.width, cell_area->width - 2 * xpad);
1754 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1755 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.width + (2 * xpad)));
1757 *x_offset = xalign * (cell_area->width - (rect.width + (2 * xpad)));
1759 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1760 *x_offset = MAX(*x_offset, 0);
1764 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1765 *y_offset = MAX (*y_offset, 0);
1770 if (x_offset) *x_offset = 0;
1771 if (y_offset) *y_offset = 0;
1775 *height = ypad * 2 + rect.height;
1778 *width = xpad * 2 + rect.width;
1780 g_object_unref (layout);
1784 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1787 const GdkRectangle *background_area,
1788 const GdkRectangle *cell_area,
1789 GtkCellRendererState flags)
1792 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1793 GtkCellRendererTextPrivate *priv = celltext->priv;
1794 GtkStyleContext *context;
1795 PangoLayout *layout;
1799 PangoRectangle rect;
1801 layout = get_layout (celltext, widget, cell_area, flags);
1802 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1803 context = gtk_widget_get_style_context (widget);
1805 if (priv->background_set && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1807 gdk_cairo_rectangle (cr, background_area);
1808 gdk_cairo_set_source_rgba (cr, &priv->background);
1812 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1814 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1815 pango_layout_set_width (layout,
1816 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1817 else if (priv->wrap_width == -1)
1818 pango_layout_set_width (layout, -1);
1820 pango_layout_get_pixel_extents (layout, NULL, &rect);
1821 x_offset = x_offset - rect.x;
1825 gdk_cairo_rectangle (cr, cell_area);
1828 gtk_render_layout (context, cr,
1829 cell_area->x + x_offset + xpad,
1830 cell_area->y + y_offset + ypad,
1835 g_object_unref (layout);
1839 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1842 GtkCellRendererTextPrivate *priv;
1844 const gchar *new_text;
1847 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1851 if (priv->focus_out_id > 0)
1853 g_signal_handler_disconnect (entry, priv->focus_out_id);
1854 priv->focus_out_id = 0;
1857 if (priv->populate_popup_id > 0)
1859 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1860 priv->populate_popup_id = 0;
1863 if (priv->entry_menu_popdown_timeout)
1865 g_source_remove (priv->entry_menu_popdown_timeout);
1866 priv->entry_menu_popdown_timeout = 0;
1869 g_object_get (entry,
1870 "editing-canceled", &canceled,
1872 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1877 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1878 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1880 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1884 popdown_timeout (gpointer data)
1886 GtkCellRendererTextPrivate *priv;
1888 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1890 priv->entry_menu_popdown_timeout = 0;
1892 if (!gtk_widget_has_focus (priv->entry))
1893 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1899 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1902 GtkCellRendererTextPrivate *priv;
1904 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1906 priv->in_entry_menu = FALSE;
1908 if (priv->entry_menu_popdown_timeout)
1911 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1916 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1920 GtkCellRendererTextPrivate *priv;
1922 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1924 if (priv->entry_menu_popdown_timeout)
1926 g_source_remove (priv->entry_menu_popdown_timeout);
1927 priv->entry_menu_popdown_timeout = 0;
1930 priv->in_entry_menu = TRUE;
1932 g_signal_connect (menu, "unmap",
1933 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1937 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1941 GtkCellRendererTextPrivate *priv;
1943 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1945 if (priv->in_entry_menu)
1948 g_object_set (entry,
1949 "editing-canceled", TRUE,
1951 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1952 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1954 /* entry needs focus-out-event */
1958 static GtkCellEditable *
1959 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1963 const GdkRectangle *background_area,
1964 const GdkRectangle *cell_area,
1965 GtkCellRendererState flags)
1967 GtkRequisition requisition;
1968 GtkCellRendererText *celltext;
1969 GtkCellRendererTextPrivate *priv;
1970 gfloat xalign, yalign;
1972 celltext = GTK_CELL_RENDERER_TEXT (cell);
1973 priv = celltext->priv;
1975 /* If the cell isn't editable we return NULL. */
1976 if (priv->editable == FALSE)
1979 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1981 priv->entry = gtk_entry_new ();
1982 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1983 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1986 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1987 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1989 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1991 gtk_widget_get_preferred_size (priv->entry, &requisition, NULL);
1992 if (requisition.height < cell_area->height)
1994 GtkBorder *style_border;
1997 gtk_widget_style_get (priv->entry,
1998 "inner-border", &style_border,
2003 border = *style_border;
2004 g_boxed_free (GTK_TYPE_BORDER, style_border);
2008 /* Since boxed style properties can't have default values ... */
2013 border.top = (cell_area->height - requisition.height) / 2;
2014 border.bottom = (cell_area->height - requisition.height) / 2;
2015 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
2018 priv->in_entry_menu = FALSE;
2019 if (priv->entry_menu_popdown_timeout)
2021 g_source_remove (priv->entry_menu_popdown_timeout);
2022 priv->entry_menu_popdown_timeout = 0;
2025 g_signal_connect (priv->entry,
2027 G_CALLBACK (gtk_cell_renderer_text_editing_done),
2029 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2030 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2032 priv->populate_popup_id =
2033 g_signal_connect (priv->entry, "populate-popup",
2034 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2037 gtk_widget_show (priv->entry);
2039 return GTK_CELL_EDITABLE (priv->entry);
2043 * gtk_cell_renderer_text_set_fixed_height_from_font:
2044 * @renderer: A #GtkCellRendererText
2045 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2047 * Sets the height of a renderer to explicitly be determined by the "font" and
2048 * "y_pad" property set on it. Further changes in these properties do not
2049 * affect the height, so they must be accompanied by a subsequent call to this
2050 * function. Using this function is unflexible, and should really only be used
2051 * if calculating the size of a cell is too slow (ie, a massive number of cells
2052 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
2053 * the height is determined by the properties again.
2056 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2057 gint number_of_rows)
2059 GtkCellRendererTextPrivate *priv;
2060 GtkCellRenderer *cell;
2062 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2063 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2065 cell = GTK_CELL_RENDERER (renderer);
2066 priv = renderer->priv;
2068 if (number_of_rows == -1)
2072 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2073 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2077 priv->fixed_height_rows = number_of_rows;
2078 priv->calc_fixed_height = TRUE;
2083 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2088 GtkCellRendererTextPrivate *priv;
2089 GtkCellRendererText *celltext;
2090 GtkStyleContext *style_context;
2091 const PangoFontDescription *font_desc;
2092 PangoLayout *layout;
2093 PangoContext *context;
2094 PangoFontMetrics *metrics;
2095 PangoRectangle rect;
2096 gint char_width, text_width, ellipsize_chars, xpad;
2097 gint min_width, nat_width;
2099 /* "width-chars" Hard-coded minimum width:
2100 * - minimum size should be MAX (width-chars, strlen ("..."));
2101 * - natural size should be MAX (width-chars, strlen (label->text));
2103 * "wrap-width" User specified natural wrap width
2104 * - minimum size should be MAX (width-chars, 0)
2105 * - natural size should be MIN (wrap-width, strlen (label->text))
2108 celltext = GTK_CELL_RENDERER_TEXT (cell);
2109 priv = celltext->priv;
2111 style_context = gtk_widget_get_style_context (widget);
2113 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2115 layout = get_layout (celltext, widget, NULL, 0);
2117 /* Fetch the length of the complete unwrapped text */
2118 pango_layout_set_width (layout, -1);
2119 pango_layout_get_extents (layout, NULL, &rect);
2120 text_width = rect.width;
2122 /* Fetch the average size of a charachter */
2123 context = pango_layout_get_context (layout);
2124 font_desc = gtk_style_context_get_font (style_context, 0);
2125 metrics = pango_context_get_metrics (context, font_desc,
2126 pango_context_get_language (context));
2128 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2130 pango_font_metrics_unref (metrics);
2131 g_object_unref (layout);
2133 /* enforce minimum width for ellipsized labels at ~3 chars */
2134 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2135 ellipsize_chars = 3;
2137 ellipsize_chars = 0;
2139 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2140 min_width = xpad * 2 +
2141 MIN (PANGO_PIXELS_CEIL (text_width),
2142 (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
2143 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2144 else if (priv->wrap_width > -1)
2145 min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
2147 min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
2149 if (priv->width_chars > 0)
2150 nat_width = xpad * 2 +
2151 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
2153 nat_width = xpad * 2 + PANGO_PIXELS_CEIL (text_width);
2155 nat_width = MAX (nat_width, min_width);
2157 if (priv->max_width_chars > 0)
2159 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2161 min_width = MIN (min_width, max_width);
2162 nat_width = MIN (nat_width, max_width);
2166 *minimum_size = min_width;
2169 *natural_size = nat_width;
2173 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2176 gint *minimum_height,
2177 gint *natural_height)
2179 GtkCellRendererText *celltext;
2180 PangoLayout *layout;
2181 gint text_height, xpad, ypad;
2184 celltext = GTK_CELL_RENDERER_TEXT (cell);
2186 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2188 layout = get_layout (celltext, widget, NULL, 0);
2190 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2191 pango_layout_get_pixel_size (layout, NULL, &text_height);
2194 *minimum_height = text_height + ypad * 2;
2197 *natural_height = text_height + ypad * 2;
2199 g_object_unref (layout);
2203 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2210 /* Thankfully cell renderers dont rotate, so they only have to do
2211 * height-for-width and not the opposite. Here we have only to return
2212 * the height for the base minimum width of the renderer.
2214 * Note this code path wont be followed by GtkTreeView which is
2215 * height-for-width specifically.
2217 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2218 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2219 minimum_size, natural_size);
2223 gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
2225 GtkCellRendererState flags,
2226 const GdkRectangle *cell_area,
2227 GdkRectangle *aligned_area)
2229 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
2230 PangoLayout *layout;
2234 layout = get_layout (celltext, widget, cell_area, flags);
2235 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset,
2236 &aligned_area->width, &aligned_area->height);
2238 aligned_area->x = cell_area->x + x_offset;
2239 aligned_area->y = cell_area->y + y_offset;
2241 g_object_unref (layout);