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"
35 static void gtk_cell_renderer_text_finalize (GObject *object);
37 static void gtk_cell_renderer_text_get_property (GObject *object,
41 static void gtk_cell_renderer_text_set_property (GObject *object,
45 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
48 const GdkRectangle *background_area,
49 const GdkRectangle *cell_area,
50 GtkCellRendererState flags);
52 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
56 GdkRectangle *background_area,
57 GdkRectangle *cell_area,
58 GtkCellRendererState flags);
60 static void gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
64 static void gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
68 static void gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
72 gint *natural_height);
85 PROP_SINGLE_PARAGRAPH_MODE,
114 /* Whether-a-style-arg-is-set args */
125 PROP_STRIKETHROUGH_SET,
133 static guint text_cell_renderer_signals [LAST_SIGNAL];
135 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
137 struct _GtkCellRendererTextPrivate
141 PangoAlignment align;
142 PangoAttrList *extra_attrs;
143 PangoColor foreground;
144 PangoColor background;
145 PangoEllipsizeMode ellipsize;
146 PangoFontDescription *font;
147 PangoLanguage *language;
148 PangoUnderline underline_style;
149 PangoWrapMode wrap_mode;
151 gboolean in_entry_menu;
158 gint fixed_height_rows;
160 gint max_width_chars;
163 guint strikethrough : 1;
166 guint foreground_set : 1;
167 guint background_set : 1;
168 guint underline_set : 1;
170 guint strikethrough_set : 1;
171 guint editable_set : 1;
172 guint calc_fixed_height : 1;
173 guint single_paragraph : 1;
174 guint language_set : 1;
175 guint markup_set : 1;
176 guint ellipsize_set : 1;
180 gulong populate_popup_id;
181 gulong entry_menu_popdown_timeout;
184 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
187 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
189 GtkCellRendererTextPrivate *priv;
190 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
192 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
193 GTK_TYPE_CELL_RENDERER_TEXT,
194 GtkCellRendererTextPrivate);
195 priv = celltext->priv;
197 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
198 gtk_cell_renderer_set_padding (cell, 2, 2);
199 priv->font_scale = 1.0;
200 priv->fixed_height_rows = -1;
201 priv->font = pango_font_description_new ();
203 priv->width_chars = -1;
204 priv->max_width_chars = -1;
205 priv->wrap_width = -1;
206 priv->wrap_mode = PANGO_WRAP_CHAR;
207 priv->align = PANGO_ALIGN_LEFT;
208 priv->align_set = FALSE;
212 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
214 GObjectClass *object_class = G_OBJECT_CLASS (class);
215 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
217 object_class->finalize = gtk_cell_renderer_text_finalize;
219 object_class->get_property = gtk_cell_renderer_text_get_property;
220 object_class->set_property = gtk_cell_renderer_text_set_property;
222 cell_class->render = gtk_cell_renderer_text_render;
223 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
224 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
225 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
226 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
228 g_object_class_install_property (object_class,
230 g_param_spec_string ("text",
232 P_("Text to render"),
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (object_class,
238 g_param_spec_string ("markup",
240 P_("Marked up text to render"),
242 GTK_PARAM_WRITABLE));
244 g_object_class_install_property (object_class,
246 g_param_spec_boxed ("attributes",
248 P_("A list of style attributes to apply to the text of the renderer"),
249 PANGO_TYPE_ATTR_LIST,
250 GTK_PARAM_READWRITE));
252 g_object_class_install_property (object_class,
253 PROP_SINGLE_PARAGRAPH_MODE,
254 g_param_spec_boolean ("single-paragraph-mode",
255 P_("Single Paragraph Mode"),
256 P_("Whether to keep all text in a single paragraph"),
258 GTK_PARAM_READWRITE));
261 g_object_class_install_property (object_class,
263 g_param_spec_string ("background",
264 P_("Background color name"),
265 P_("Background color as a string"),
267 GTK_PARAM_WRITABLE));
269 g_object_class_install_property (object_class,
271 g_param_spec_boxed ("background-gdk",
272 P_("Background color"),
273 P_("Background color as a GdkColor"),
275 GTK_PARAM_READWRITE));
277 g_object_class_install_property (object_class,
279 g_param_spec_string ("foreground",
280 P_("Foreground color name"),
281 P_("Foreground color as a string"),
283 GTK_PARAM_WRITABLE));
285 g_object_class_install_property (object_class,
287 g_param_spec_boxed ("foreground-gdk",
288 P_("Foreground color"),
289 P_("Foreground color as a GdkColor"),
291 GTK_PARAM_READWRITE));
294 g_object_class_install_property (object_class,
296 g_param_spec_boolean ("editable",
298 P_("Whether the text can be modified by the user"),
300 GTK_PARAM_READWRITE));
302 g_object_class_install_property (object_class,
304 g_param_spec_string ("font",
306 P_("Font description as a string, e.g. \"Sans Italic 12\""),
308 GTK_PARAM_READWRITE));
310 g_object_class_install_property (object_class,
312 g_param_spec_boxed ("font-desc",
314 P_("Font description as a PangoFontDescription struct"),
315 PANGO_TYPE_FONT_DESCRIPTION,
316 GTK_PARAM_READWRITE));
319 g_object_class_install_property (object_class,
321 g_param_spec_string ("family",
323 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
325 GTK_PARAM_READWRITE));
327 g_object_class_install_property (object_class,
329 g_param_spec_enum ("style",
334 GTK_PARAM_READWRITE));
336 g_object_class_install_property (object_class,
338 g_param_spec_enum ("variant",
342 PANGO_VARIANT_NORMAL,
343 GTK_PARAM_READWRITE));
345 g_object_class_install_property (object_class,
347 g_param_spec_int ("weight",
353 GTK_PARAM_READWRITE));
355 g_object_class_install_property (object_class,
357 g_param_spec_enum ("stretch",
361 PANGO_STRETCH_NORMAL,
362 GTK_PARAM_READWRITE));
364 g_object_class_install_property (object_class,
366 g_param_spec_int ("size",
372 GTK_PARAM_READWRITE));
374 g_object_class_install_property (object_class,
376 g_param_spec_double ("size-points",
378 P_("Font size in points"),
382 GTK_PARAM_READWRITE));
384 g_object_class_install_property (object_class,
386 g_param_spec_double ("scale",
388 P_("Font scaling factor"),
392 GTK_PARAM_READWRITE));
394 g_object_class_install_property (object_class,
396 g_param_spec_int ("rise",
398 P_("Offset of text above the baseline "
399 "(below the baseline if rise is negative)"),
403 GTK_PARAM_READWRITE));
406 g_object_class_install_property (object_class,
408 g_param_spec_boolean ("strikethrough",
410 P_("Whether to strike through the text"),
412 GTK_PARAM_READWRITE));
414 g_object_class_install_property (object_class,
416 g_param_spec_enum ("underline",
418 P_("Style of underline for this text"),
419 PANGO_TYPE_UNDERLINE,
420 PANGO_UNDERLINE_NONE,
421 GTK_PARAM_READWRITE));
423 g_object_class_install_property (object_class,
425 g_param_spec_string ("language",
427 P_("The language this text is in, as an ISO code. "
428 "Pango can use this as a hint when rendering the text. "
429 "If you don't understand this parameter, you probably don't need it"),
431 GTK_PARAM_READWRITE));
435 * GtkCellRendererText:ellipsize:
437 * Specifies the preferred place to ellipsize the string, if the cell renderer
438 * does not have enough room to display the entire string. Setting it to
439 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
440 * for another way of making the text fit into a given width.
444 g_object_class_install_property (object_class,
446 g_param_spec_enum ("ellipsize",
448 P_("The preferred place to ellipsize the string, "
449 "if the cell renderer does not have enough room "
450 "to display the entire string"),
451 PANGO_TYPE_ELLIPSIZE_MODE,
452 PANGO_ELLIPSIZE_NONE,
453 GTK_PARAM_READWRITE));
456 * GtkCellRendererText:width-chars:
458 * The desired width of the cell, in characters. If this property is set to
459 * -1, the width will be calculated automatically, otherwise the cell will
460 * request either 3 characters or the property value, whichever is greater.
464 g_object_class_install_property (object_class,
466 g_param_spec_int ("width-chars",
467 P_("Width In Characters"),
468 P_("The desired width of the label, in characters"),
472 GTK_PARAM_READWRITE));
476 * GtkCellRendererText:max-width-chars:
478 * The desired maximum width of the cell, in characters. If this property
479 * is set to -1, the width will be calculated automatically.
481 * For cell renderers that ellipsize or wrap text; this property
482 * controls the maximum reported width of the cell. The
483 * cell should not receive any greater allocation unless it is
484 * set to expand in its #GtkCellLayout and all of the cell's siblings
485 * have received their natural width.
489 g_object_class_install_property (object_class,
490 PROP_MAX_WIDTH_CHARS,
491 g_param_spec_int ("max-width-chars",
492 P_("Maximum Width In Characters"),
493 P_("The maximum width of the cell, in characters"),
497 GTK_PARAM_READWRITE));
500 * GtkCellRendererText:wrap-mode:
502 * Specifies how to break the string into multiple lines, if the cell
503 * renderer does not have enough room to display the entire string.
504 * This property has no effect unless the wrap-width property is set.
508 g_object_class_install_property (object_class,
510 g_param_spec_enum ("wrap-mode",
512 P_("How to break the string into multiple lines, "
513 "if the cell renderer does not have enough room "
514 "to display the entire string"),
515 PANGO_TYPE_WRAP_MODE,
517 GTK_PARAM_READWRITE));
520 * GtkCellRendererText:wrap-width:
522 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
523 * be used to influence at what character positions the line breaks can be placed.
524 * Setting wrap-width to -1 turns wrapping off.
528 g_object_class_install_property (object_class,
530 g_param_spec_int ("wrap-width",
532 P_("The width at which the text is wrapped"),
536 GTK_PARAM_READWRITE));
539 * GtkCellRendererText:alignment:
541 * Specifies how to align the lines of text with respect to each other.
543 * Note that this property describes how to align the lines of text in
544 * case there are several of them. The "xalign" property of #GtkCellRenderer,
545 * on the other hand, sets the horizontal alignment of the whole text.
549 g_object_class_install_property (object_class,
551 g_param_spec_enum ("alignment",
553 P_("How to align the lines"),
554 PANGO_TYPE_ALIGNMENT,
556 GTK_PARAM_READWRITE));
560 /* Style props are set or not */
562 #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))
564 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
565 P_("Background set"),
566 P_("Whether this tag affects the background color"));
568 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
569 P_("Foreground set"),
570 P_("Whether this tag affects the foreground color"));
572 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
573 P_("Editability set"),
574 P_("Whether this tag affects text editability"));
576 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
577 P_("Font family set"),
578 P_("Whether this tag affects the font family"));
580 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
581 P_("Font style set"),
582 P_("Whether this tag affects the font style"));
584 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
585 P_("Font variant set"),
586 P_("Whether this tag affects the font variant"));
588 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
589 P_("Font weight set"),
590 P_("Whether this tag affects the font weight"));
592 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
593 P_("Font stretch set"),
594 P_("Whether this tag affects the font stretch"));
596 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
598 P_("Whether this tag affects the font size"));
600 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
601 P_("Font scale set"),
602 P_("Whether this tag scales the font size by a factor"));
604 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
606 P_("Whether this tag affects the rise"));
608 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
609 P_("Strikethrough set"),
610 P_("Whether this tag affects strikethrough"));
612 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
614 P_("Whether this tag affects underlining"));
616 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
618 P_("Whether this tag affects the language the text is rendered as"));
620 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
622 P_("Whether this tag affects the ellipsize mode"));
624 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
626 P_("Whether this tag affects the alignment mode"));
629 * GtkCellRendererText::edited
630 * @renderer: the object which received the signal
631 * @path: the path identifying the edited cell
632 * @new_text: the new text
634 * This signal is emitted after @renderer has been edited.
636 * It is the responsibility of the application to update the model
637 * and store @new_text at the position indicated by @path.
639 text_cell_renderer_signals [EDITED] =
640 g_signal_new (I_("edited"),
641 G_OBJECT_CLASS_TYPE (object_class),
643 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
645 _gtk_marshal_VOID__STRING_STRING,
650 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
654 gtk_cell_renderer_text_finalize (GObject *object)
656 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
657 GtkCellRendererTextPrivate *priv = celltext->priv;
659 pango_font_description_free (priv->font);
663 if (priv->extra_attrs)
664 pango_attr_list_unref (priv->extra_attrs);
667 g_object_unref (priv->language);
669 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
673 get_property_font_set_mask (guint prop_id)
677 case PROP_FAMILY_SET:
678 return PANGO_FONT_MASK_FAMILY;
680 return PANGO_FONT_MASK_STYLE;
681 case PROP_VARIANT_SET:
682 return PANGO_FONT_MASK_VARIANT;
683 case PROP_WEIGHT_SET:
684 return PANGO_FONT_MASK_WEIGHT;
685 case PROP_STRETCH_SET:
686 return PANGO_FONT_MASK_STRETCH;
688 return PANGO_FONT_MASK_SIZE;
695 gtk_cell_renderer_text_get_property (GObject *object,
700 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
701 GtkCellRendererTextPrivate *priv = celltext->priv;
706 g_value_set_string (value, priv->text);
709 case PROP_ATTRIBUTES:
710 g_value_set_boxed (value, priv->extra_attrs);
713 case PROP_SINGLE_PARAGRAPH_MODE:
714 g_value_set_boolean (value, priv->single_paragraph);
717 case PROP_BACKGROUND_GDK:
721 color.red = priv->background.red;
722 color.green = priv->background.green;
723 color.blue = priv->background.blue;
725 g_value_set_boxed (value, &color);
729 case PROP_FOREGROUND_GDK:
733 color.red = priv->foreground.red;
734 color.green = priv->foreground.green;
735 color.blue = priv->foreground.blue;
737 g_value_set_boxed (value, &color);
742 g_value_take_string (value, pango_font_description_to_string (priv->font));
746 g_value_set_boxed (value, priv->font);
750 g_value_set_string (value, pango_font_description_get_family (priv->font));
754 g_value_set_enum (value, pango_font_description_get_style (priv->font));
758 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
762 g_value_set_int (value, pango_font_description_get_weight (priv->font));
766 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
770 g_value_set_int (value, pango_font_description_get_size (priv->font));
773 case PROP_SIZE_POINTS:
774 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
778 g_value_set_double (value, priv->font_scale);
782 g_value_set_boolean (value, priv->editable);
785 case PROP_STRIKETHROUGH:
786 g_value_set_boolean (value, priv->strikethrough);
790 g_value_set_enum (value, priv->underline_style);
794 g_value_set_int (value, priv->rise);
798 g_value_set_static_string (value, pango_language_to_string (priv->language));
802 g_value_set_enum (value, priv->ellipsize);
806 g_value_set_enum (value, priv->wrap_mode);
809 case PROP_WRAP_WIDTH:
810 g_value_set_int (value, priv->wrap_width);
814 g_value_set_enum (value, priv->align);
817 case PROP_BACKGROUND_SET:
818 g_value_set_boolean (value, priv->background_set);
821 case PROP_FOREGROUND_SET:
822 g_value_set_boolean (value, priv->foreground_set);
825 case PROP_FAMILY_SET:
827 case PROP_VARIANT_SET:
828 case PROP_WEIGHT_SET:
829 case PROP_STRETCH_SET:
832 PangoFontMask mask = get_property_font_set_mask (param_id);
833 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
839 g_value_set_boolean (value, priv->scale_set);
842 case PROP_EDITABLE_SET:
843 g_value_set_boolean (value, priv->editable_set);
846 case PROP_STRIKETHROUGH_SET:
847 g_value_set_boolean (value, priv->strikethrough_set);
850 case PROP_UNDERLINE_SET:
851 g_value_set_boolean (value, priv->underline_set);
855 g_value_set_boolean (value, priv->rise_set);
858 case PROP_LANGUAGE_SET:
859 g_value_set_boolean (value, priv->language_set);
862 case PROP_ELLIPSIZE_SET:
863 g_value_set_boolean (value, priv->ellipsize_set);
867 g_value_set_boolean (value, priv->align_set);
870 case PROP_WIDTH_CHARS:
871 g_value_set_int (value, priv->width_chars);
874 case PROP_MAX_WIDTH_CHARS:
875 g_value_set_int (value, priv->max_width_chars);
878 case PROP_BACKGROUND:
879 case PROP_FOREGROUND:
882 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
889 set_bg_color (GtkCellRendererText *celltext,
892 GtkCellRendererTextPrivate *priv = celltext->priv;
896 if (!priv->background_set)
898 priv->background_set = TRUE;
899 g_object_notify (G_OBJECT (celltext), "background-set");
902 priv->background.red = color->red;
903 priv->background.green = color->green;
904 priv->background.blue = color->blue;
908 if (priv->background_set)
910 priv->background_set = FALSE;
911 g_object_notify (G_OBJECT (celltext), "background-set");
918 set_fg_color (GtkCellRendererText *celltext,
921 GtkCellRendererTextPrivate *priv = celltext->priv;
925 if (!priv->foreground_set)
927 priv->foreground_set = TRUE;
928 g_object_notify (G_OBJECT (celltext), "foreground-set");
931 priv->foreground.red = color->red;
932 priv->foreground.green = color->green;
933 priv->foreground.blue = color->blue;
937 if (priv->foreground_set)
939 priv->foreground_set = FALSE;
940 g_object_notify (G_OBJECT (celltext), "foreground-set");
946 set_font_desc_fields (PangoFontDescription *desc,
947 PangoFontMask to_set)
949 PangoFontMask changed_mask = 0;
951 if (to_set & PANGO_FONT_MASK_FAMILY)
953 const char *family = pango_font_description_get_family (desc);
957 changed_mask |= PANGO_FONT_MASK_FAMILY;
960 pango_font_description_set_family (desc, family);
962 if (to_set & PANGO_FONT_MASK_STYLE)
963 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
964 if (to_set & PANGO_FONT_MASK_VARIANT)
965 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
966 if (to_set & PANGO_FONT_MASK_WEIGHT)
967 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
968 if (to_set & PANGO_FONT_MASK_STRETCH)
969 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
970 if (to_set & PANGO_FONT_MASK_SIZE)
972 gint size = pango_font_description_get_size (desc);
975 size = 10 * PANGO_SCALE;
976 changed_mask |= PANGO_FONT_MASK_SIZE;
979 pango_font_description_set_size (desc, size);
986 notify_set_changed (GObject *object,
987 PangoFontMask changed_mask)
989 if (changed_mask & PANGO_FONT_MASK_FAMILY)
990 g_object_notify (object, "family-set");
991 if (changed_mask & PANGO_FONT_MASK_STYLE)
992 g_object_notify (object, "style-set");
993 if (changed_mask & PANGO_FONT_MASK_VARIANT)
994 g_object_notify (object, "variant-set");
995 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
996 g_object_notify (object, "weight-set");
997 if (changed_mask & PANGO_FONT_MASK_STRETCH)
998 g_object_notify (object, "stretch-set");
999 if (changed_mask & PANGO_FONT_MASK_SIZE)
1000 g_object_notify (object, "size-set");
1004 notify_fields_changed (GObject *object,
1005 PangoFontMask changed_mask)
1007 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1008 g_object_notify (object, "family");
1009 if (changed_mask & PANGO_FONT_MASK_STYLE)
1010 g_object_notify (object, "style");
1011 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1012 g_object_notify (object, "variant");
1013 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1014 g_object_notify (object, "weight");
1015 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1016 g_object_notify (object, "stretch");
1017 if (changed_mask & PANGO_FONT_MASK_SIZE)
1018 g_object_notify (object, "size");
1022 set_font_description (GtkCellRendererText *celltext,
1023 PangoFontDescription *font_desc)
1025 GtkCellRendererTextPrivate *priv = celltext->priv;
1026 GObject *object = G_OBJECT (celltext);
1027 PangoFontDescription *new_font_desc;
1028 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1031 new_font_desc = pango_font_description_copy (font_desc);
1033 new_font_desc = pango_font_description_new ();
1035 old_mask = pango_font_description_get_set_fields (priv->font);
1036 new_mask = pango_font_description_get_set_fields (new_font_desc);
1038 changed_mask = old_mask | new_mask;
1039 set_changed_mask = old_mask ^ new_mask;
1041 pango_font_description_free (priv->font);
1042 priv->font = new_font_desc;
1044 g_object_freeze_notify (object);
1046 g_object_notify (object, "font-desc");
1047 g_object_notify (object, "font");
1049 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1050 g_object_notify (object, "family");
1051 if (changed_mask & PANGO_FONT_MASK_STYLE)
1052 g_object_notify (object, "style");
1053 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1054 g_object_notify (object, "variant");
1055 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1056 g_object_notify (object, "weight");
1057 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1058 g_object_notify (object, "stretch");
1059 if (changed_mask & PANGO_FONT_MASK_SIZE)
1061 g_object_notify (object, "size");
1062 g_object_notify (object, "size-points");
1065 notify_set_changed (object, set_changed_mask);
1067 g_object_thaw_notify (object);
1071 gtk_cell_renderer_text_set_property (GObject *object,
1073 const GValue *value,
1076 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1077 GtkCellRendererTextPrivate *priv = celltext->priv;
1082 g_free (priv->text);
1084 if (priv->markup_set)
1086 if (priv->extra_attrs)
1087 pango_attr_list_unref (priv->extra_attrs);
1088 priv->extra_attrs = NULL;
1089 priv->markup_set = FALSE;
1092 priv->text = g_value_dup_string (value);
1093 g_object_notify (object, "text");
1096 case PROP_ATTRIBUTES:
1097 if (priv->extra_attrs)
1098 pango_attr_list_unref (priv->extra_attrs);
1100 priv->extra_attrs = g_value_get_boxed (value);
1101 if (priv->extra_attrs)
1102 pango_attr_list_ref (priv->extra_attrs);
1108 GError *error = NULL;
1109 PangoAttrList *attrs = NULL;
1111 str = g_value_get_string (value);
1112 if (str && !pango_parse_markup (str,
1120 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1122 g_error_free (error);
1126 g_free (priv->text);
1128 if (priv->extra_attrs)
1129 pango_attr_list_unref (priv->extra_attrs);
1132 priv->extra_attrs = attrs;
1133 priv->markup_set = TRUE;
1137 case PROP_SINGLE_PARAGRAPH_MODE:
1138 priv->single_paragraph = g_value_get_boolean (value);
1141 case PROP_BACKGROUND:
1145 if (!g_value_get_string (value))
1146 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1147 else if (gdk_color_parse (g_value_get_string (value), &color))
1148 set_bg_color (celltext, &color);
1150 g_warning ("Don't know color `%s'", g_value_get_string (value));
1152 g_object_notify (object, "background-gdk");
1156 case PROP_FOREGROUND:
1160 if (!g_value_get_string (value))
1161 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1162 else if (gdk_color_parse (g_value_get_string (value), &color))
1163 set_fg_color (celltext, &color);
1165 g_warning ("Don't know color `%s'", g_value_get_string (value));
1167 g_object_notify (object, "foreground-gdk");
1171 case PROP_BACKGROUND_GDK:
1172 /* This notifies the GObject itself. */
1173 set_bg_color (celltext, g_value_get_boxed (value));
1176 case PROP_FOREGROUND_GDK:
1177 /* This notifies the GObject itself. */
1178 set_fg_color (celltext, g_value_get_boxed (value));
1183 PangoFontDescription *font_desc = NULL;
1186 name = g_value_get_string (value);
1189 font_desc = pango_font_description_from_string (name);
1191 set_font_description (celltext, font_desc);
1193 pango_font_description_free (font_desc);
1195 if (priv->fixed_height_rows != -1)
1196 priv->calc_fixed_height = TRUE;
1200 case PROP_FONT_DESC:
1201 set_font_description (celltext, g_value_get_boxed (value));
1203 if (priv->fixed_height_rows != -1)
1204 priv->calc_fixed_height = TRUE;
1213 case PROP_SIZE_POINTS:
1215 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1220 pango_font_description_set_family (priv->font,
1221 g_value_get_string (value));
1224 pango_font_description_set_style (priv->font,
1225 g_value_get_enum (value));
1228 pango_font_description_set_variant (priv->font,
1229 g_value_get_enum (value));
1232 pango_font_description_set_weight (priv->font,
1233 g_value_get_int (value));
1236 pango_font_description_set_stretch (priv->font,
1237 g_value_get_enum (value));
1240 pango_font_description_set_size (priv->font,
1241 g_value_get_int (value));
1242 g_object_notify (object, "size-points");
1244 case PROP_SIZE_POINTS:
1245 pango_font_description_set_size (priv->font,
1246 g_value_get_double (value) * PANGO_SCALE);
1247 g_object_notify (object, "size");
1251 if (priv->fixed_height_rows != -1)
1252 priv->calc_fixed_height = TRUE;
1254 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1255 g_object_notify (object, "font-desc");
1256 g_object_notify (object, "font");
1262 priv->font_scale = g_value_get_double (value);
1263 priv->scale_set = TRUE;
1264 if (priv->fixed_height_rows != -1)
1265 priv->calc_fixed_height = TRUE;
1266 g_object_notify (object, "scale-set");
1270 priv->editable = g_value_get_boolean (value);
1271 priv->editable_set = TRUE;
1273 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1275 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1276 g_object_notify (object, "editable-set");
1279 case PROP_STRIKETHROUGH:
1280 priv->strikethrough = g_value_get_boolean (value);
1281 priv->strikethrough_set = TRUE;
1282 g_object_notify (object, "strikethrough-set");
1285 case PROP_UNDERLINE:
1286 priv->underline_style = g_value_get_enum (value);
1287 priv->underline_set = TRUE;
1288 g_object_notify (object, "underline-set");
1293 priv->rise = g_value_get_int (value);
1294 priv->rise_set = TRUE;
1295 g_object_notify (object, "rise-set");
1296 if (priv->fixed_height_rows != -1)
1297 priv->calc_fixed_height = TRUE;
1301 priv->language_set = TRUE;
1303 g_object_unref (priv->language);
1304 priv->language = pango_language_from_string (g_value_get_string (value));
1305 g_object_notify (object, "language-set");
1308 case PROP_ELLIPSIZE:
1309 priv->ellipsize = g_value_get_enum (value);
1310 priv->ellipsize_set = TRUE;
1311 g_object_notify (object, "ellipsize-set");
1314 case PROP_WRAP_MODE:
1315 priv->wrap_mode = g_value_get_enum (value);
1318 case PROP_WRAP_WIDTH:
1319 priv->wrap_width = g_value_get_int (value);
1322 case PROP_WIDTH_CHARS:
1323 priv->width_chars = g_value_get_int (value);
1326 case PROP_MAX_WIDTH_CHARS:
1327 priv->max_width_chars = g_value_get_int (value);
1331 priv->align = g_value_get_enum (value);
1332 priv->align_set = TRUE;
1333 g_object_notify (object, "align-set");
1336 case PROP_BACKGROUND_SET:
1337 priv->background_set = g_value_get_boolean (value);
1340 case PROP_FOREGROUND_SET:
1341 priv->foreground_set = g_value_get_boolean (value);
1344 case PROP_FAMILY_SET:
1345 case PROP_STYLE_SET:
1346 case PROP_VARIANT_SET:
1347 case PROP_WEIGHT_SET:
1348 case PROP_STRETCH_SET:
1350 if (!g_value_get_boolean (value))
1352 pango_font_description_unset_fields (priv->font,
1353 get_property_font_set_mask (param_id));
1357 PangoFontMask changed_mask;
1359 changed_mask = set_font_desc_fields (priv->font,
1360 get_property_font_set_mask (param_id));
1361 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1365 case PROP_SCALE_SET:
1366 priv->scale_set = g_value_get_boolean (value);
1369 case PROP_EDITABLE_SET:
1370 priv->editable_set = g_value_get_boolean (value);
1373 case PROP_STRIKETHROUGH_SET:
1374 priv->strikethrough_set = g_value_get_boolean (value);
1377 case PROP_UNDERLINE_SET:
1378 priv->underline_set = g_value_get_boolean (value);
1382 priv->rise_set = g_value_get_boolean (value);
1385 case PROP_LANGUAGE_SET:
1386 priv->language_set = g_value_get_boolean (value);
1389 case PROP_ELLIPSIZE_SET:
1390 priv->ellipsize_set = g_value_get_boolean (value);
1393 case PROP_ALIGN_SET:
1394 priv->align_set = g_value_get_boolean (value);
1398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1404 * gtk_cell_renderer_text_new:
1406 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1407 * object properties. Object properties can be
1408 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1409 * you can bind a property to a value in a #GtkTreeModel. For example,
1410 * you can bind the "text" property on the cell renderer to a string
1411 * value in the model, thus rendering a different string in each row
1412 * of the #GtkTreeView
1414 * Return value: the new cell renderer
1417 gtk_cell_renderer_text_new (void)
1419 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1423 add_attr (PangoAttrList *attr_list,
1424 PangoAttribute *attr)
1426 attr->start_index = 0;
1427 attr->end_index = G_MAXINT;
1429 pango_attr_list_insert (attr_list, attr);
1433 get_layout (GtkCellRendererText *celltext,
1435 const GdkRectangle *cell_area,
1436 GtkCellRendererState flags)
1438 GtkCellRendererTextPrivate *priv = celltext->priv;
1439 PangoAttrList *attr_list;
1440 PangoLayout *layout;
1441 PangoUnderline uline;
1444 layout = gtk_widget_create_pango_layout (widget, priv->text);
1446 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1448 if (priv->extra_attrs)
1449 attr_list = pango_attr_list_copy (priv->extra_attrs);
1451 attr_list = pango_attr_list_new ();
1453 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1457 /* Add options that affect appearance but not size */
1459 /* note that background doesn't go here, since it affects
1460 * background_area not the PangoLayout area
1463 if (priv->foreground_set
1464 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1468 color = priv->foreground;
1470 add_attr (attr_list,
1471 pango_attr_foreground_new (color.red, color.green, color.blue));
1474 if (priv->strikethrough_set)
1475 add_attr (attr_list,
1476 pango_attr_strikethrough_new (priv->strikethrough));
1479 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1481 if (priv->scale_set &&
1482 priv->font_scale != 1.0)
1483 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1485 if (priv->underline_set)
1486 uline = priv->underline_style;
1488 uline = PANGO_UNDERLINE_NONE;
1490 if (priv->language_set)
1491 add_attr (attr_list, pango_attr_language_new (priv->language));
1493 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1497 case PANGO_UNDERLINE_NONE:
1498 uline = PANGO_UNDERLINE_SINGLE;
1501 case PANGO_UNDERLINE_SINGLE:
1502 uline = PANGO_UNDERLINE_DOUBLE;
1510 if (uline != PANGO_UNDERLINE_NONE)
1511 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1514 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1516 /* Now apply the attributes as they will effect the outcome
1517 * of pango_layout_get_extents() */
1518 pango_layout_set_attributes (layout, attr_list);
1519 pango_attr_list_unref (attr_list);
1521 if (priv->ellipsize_set)
1522 pango_layout_set_ellipsize (layout, priv->ellipsize);
1524 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1526 if (priv->wrap_width != -1)
1528 PangoRectangle rect;
1529 gint width, text_width;
1531 pango_layout_get_extents (layout, NULL, &rect);
1532 text_width = rect.width;
1535 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1537 width = priv->wrap_width * PANGO_SCALE;
1539 width = MIN (width, text_width);
1541 pango_layout_set_width (layout, width);
1542 pango_layout_set_wrap (layout, priv->wrap_mode);
1546 pango_layout_set_width (layout, -1);
1547 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1550 if (priv->align_set)
1551 pango_layout_set_alignment (layout, priv->align);
1554 PangoAlignment align;
1556 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1557 align = PANGO_ALIGN_RIGHT;
1559 align = PANGO_ALIGN_LEFT;
1561 pango_layout_set_alignment (layout, align);
1569 get_size (GtkCellRenderer *cell,
1571 GdkRectangle *cell_area,
1572 PangoLayout *layout,
1578 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1579 GtkCellRendererTextPrivate *priv = celltext->priv;
1580 PangoRectangle rect;
1582 gint cell_width, cell_height;
1584 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1586 if (priv->calc_fixed_height)
1588 PangoContext *context;
1589 PangoFontMetrics *metrics;
1590 PangoFontDescription *font_desc;
1593 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1594 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1596 if (priv->scale_set)
1597 pango_font_description_set_size (font_desc,
1598 priv->font_scale * pango_font_description_get_size (font_desc));
1600 context = gtk_widget_get_pango_context (widget);
1602 metrics = pango_context_get_metrics (context,
1604 pango_context_get_language (context));
1605 row_height = (pango_font_metrics_get_ascent (metrics) +
1606 pango_font_metrics_get_descent (metrics));
1607 pango_font_metrics_unref (metrics);
1609 pango_font_description_free (font_desc);
1611 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1613 gtk_cell_renderer_set_fixed_size (cell,
1614 cell_width, 2 * ypad +
1615 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1619 *height = cell_height;
1622 priv->calc_fixed_height = FALSE;
1628 g_object_ref (layout);
1630 layout = get_layout (celltext, widget, NULL, 0);
1632 pango_layout_get_pixel_extents (layout, NULL, &rect);
1635 *height = ypad * 2 + rect.height;
1637 /* The minimum size for ellipsized labels is ~ 3 chars */
1640 if (priv->ellipsize || priv->width_chars > 0)
1642 PangoContext *context;
1643 PangoFontMetrics *metrics;
1646 context = pango_layout_get_context (layout);
1647 metrics = pango_context_get_metrics (context,
1648 gtk_widget_get_style (widget)->font_desc,
1649 pango_context_get_language (context));
1651 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1652 pango_font_metrics_unref (metrics);
1654 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1658 *width = xpad * 2 + rect.x + rect.width;
1664 gfloat xalign, yalign;
1666 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1670 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1671 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1673 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1675 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1676 *x_offset = MAX(*x_offset, 0);
1680 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1681 *y_offset = MAX (*y_offset, 0);
1686 if (x_offset) *x_offset = 0;
1687 if (y_offset) *y_offset = 0;
1690 g_object_unref (layout);
1694 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1697 const GdkRectangle *background_area,
1698 const GdkRectangle *cell_area,
1699 GtkCellRendererState flags)
1702 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1703 GtkCellRendererTextPrivate *priv = celltext->priv;
1704 PangoLayout *layout;
1710 layout = get_layout (celltext, widget, cell_area, flags);
1711 get_size (cell, widget, (GdkRectangle *) cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1713 if (!gtk_cell_renderer_get_sensitive (cell))
1715 state = GTK_STATE_INSENSITIVE;
1717 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1719 if (gtk_widget_has_focus (widget))
1720 state = GTK_STATE_SELECTED;
1722 state = GTK_STATE_ACTIVE;
1724 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1725 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1727 state = GTK_STATE_PRELIGHT;
1731 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1732 state = GTK_STATE_INSENSITIVE;
1734 state = GTK_STATE_NORMAL;
1737 if (priv->background_set &&
1738 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1740 gdk_cairo_rectangle (cr, background_area);
1741 cairo_set_source_rgb (cr,
1742 priv->background.red / 65535.,
1743 priv->background.green / 65535.,
1744 priv->background.blue / 65535.);
1748 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1750 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1751 pango_layout_set_width (layout,
1752 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1753 else if (priv->wrap_width == -1)
1754 pango_layout_set_width (layout, -1);
1756 gtk_paint_layout (gtk_widget_get_style (widget),
1762 cell_area->x + x_offset + xpad,
1763 cell_area->y + y_offset + ypad,
1766 g_object_unref (layout);
1770 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1773 GtkCellRendererTextPrivate *priv;
1775 const gchar *new_text;
1778 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1782 if (priv->focus_out_id > 0)
1784 g_signal_handler_disconnect (entry, priv->focus_out_id);
1785 priv->focus_out_id = 0;
1788 if (priv->populate_popup_id > 0)
1790 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1791 priv->populate_popup_id = 0;
1794 if (priv->entry_menu_popdown_timeout)
1796 g_source_remove (priv->entry_menu_popdown_timeout);
1797 priv->entry_menu_popdown_timeout = 0;
1800 g_object_get (entry,
1801 "editing-canceled", &canceled,
1803 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1808 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1809 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1811 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1815 popdown_timeout (gpointer data)
1817 GtkCellRendererTextPrivate *priv;
1819 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1821 priv->entry_menu_popdown_timeout = 0;
1823 if (!gtk_widget_has_focus (priv->entry))
1824 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1830 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1833 GtkCellRendererTextPrivate *priv;
1835 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1837 priv->in_entry_menu = FALSE;
1839 if (priv->entry_menu_popdown_timeout)
1842 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1847 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1851 GtkCellRendererTextPrivate *priv;
1853 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1855 if (priv->entry_menu_popdown_timeout)
1857 g_source_remove (priv->entry_menu_popdown_timeout);
1858 priv->entry_menu_popdown_timeout = 0;
1861 priv->in_entry_menu = TRUE;
1863 g_signal_connect (menu, "unmap",
1864 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1868 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1872 GtkCellRendererTextPrivate *priv;
1874 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1876 if (priv->in_entry_menu)
1879 g_object_set (entry,
1880 "editing-canceled", TRUE,
1882 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1883 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1885 /* entry needs focus-out-event */
1889 static GtkCellEditable *
1890 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1894 GdkRectangle *background_area,
1895 GdkRectangle *cell_area,
1896 GtkCellRendererState flags)
1898 GtkRequisition requisition;
1899 GtkCellRendererText *celltext;
1900 GtkCellRendererTextPrivate *priv;
1901 gfloat xalign, yalign;
1903 celltext = GTK_CELL_RENDERER_TEXT (cell);
1904 priv = celltext->priv;
1906 /* If the cell isn't editable we return NULL. */
1907 if (priv->editable == FALSE)
1910 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1912 priv->entry = gtk_entry_new ();
1913 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1914 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1917 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1918 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1920 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1922 gtk_widget_get_preferred_size (priv->entry, &requisition, NULL);
1923 if (requisition.height < cell_area->height)
1925 GtkBorder *style_border;
1928 gtk_widget_style_get (priv->entry,
1929 "inner-border", &style_border,
1934 border = *style_border;
1935 g_boxed_free (GTK_TYPE_BORDER, style_border);
1939 /* Since boxed style properties can't have default values ... */
1944 border.top = (cell_area->height - requisition.height) / 2;
1945 border.bottom = (cell_area->height - requisition.height) / 2;
1946 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
1949 priv->in_entry_menu = FALSE;
1950 if (priv->entry_menu_popdown_timeout)
1952 g_source_remove (priv->entry_menu_popdown_timeout);
1953 priv->entry_menu_popdown_timeout = 0;
1956 g_signal_connect (priv->entry,
1958 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1960 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
1961 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1963 priv->populate_popup_id =
1964 g_signal_connect (priv->entry, "populate-popup",
1965 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1968 gtk_widget_show (priv->entry);
1970 return GTK_CELL_EDITABLE (priv->entry);
1974 * gtk_cell_renderer_text_set_fixed_height_from_font:
1975 * @renderer: A #GtkCellRendererText
1976 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1978 * Sets the height of a renderer to explicitly be determined by the "font" and
1979 * "y_pad" property set on it. Further changes in these properties do not
1980 * affect the height, so they must be accompanied by a subsequent call to this
1981 * function. Using this function is unflexible, and should really only be used
1982 * if calculating the size of a cell is too slow (ie, a massive number of cells
1983 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1984 * the height is determined by the properties again.
1987 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1988 gint number_of_rows)
1990 GtkCellRendererTextPrivate *priv;
1991 GtkCellRenderer *cell;
1993 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1994 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1996 cell = GTK_CELL_RENDERER (renderer);
1997 priv = renderer->priv;
1999 if (number_of_rows == -1)
2003 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2004 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2008 priv->fixed_height_rows = number_of_rows;
2009 priv->calc_fixed_height = TRUE;
2014 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2019 GtkCellRendererTextPrivate *priv;
2020 GtkCellRendererText *celltext;
2022 PangoLayout *layout;
2023 PangoContext *context;
2024 PangoFontMetrics *metrics;
2025 PangoRectangle rect;
2026 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2027 gint min_width, nat_width;
2029 /* "width-chars" Hard-coded minimum width:
2030 * - minimum size should be MAX (width-chars, strlen ("..."));
2031 * - natural size should be MAX (width-chars, strlen (label->text));
2033 * "wrap-width" User specified natural wrap width
2034 * - minimum size should be MAX (width-chars, 0)
2035 * - natural size should be MIN (wrap-width, strlen (label->text))
2038 celltext = GTK_CELL_RENDERER_TEXT (cell);
2039 priv = celltext->priv;
2041 style = gtk_widget_get_style (widget);
2043 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2045 layout = get_layout (celltext, widget, NULL, 0);
2047 /* Get the layout with the text possibly wrapping at wrap_width */
2048 pango_layout_get_pixel_extents (layout, NULL, &rect);
2049 guess_width = rect.width;
2051 /* Fetch the length of the complete unwrapped text */
2052 pango_layout_set_width (layout, -1);
2053 pango_layout_get_extents (layout, NULL, &rect);
2054 text_width = rect.width;
2056 /* Fetch the average size of a charachter */
2057 context = pango_layout_get_context (layout);
2058 metrics = pango_context_get_metrics (context, style->font_desc,
2059 pango_context_get_language (context));
2061 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2062 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2063 char_pixels = MAX (char_width, digit_width);
2065 pango_font_metrics_unref (metrics);
2066 g_object_unref (layout);
2068 /* enforce minimum width for ellipsized labels at ~3 chars */
2069 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2070 ellipsize_chars = 3;
2072 ellipsize_chars = 0;
2074 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2076 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2077 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2078 else if (priv->wrap_width > -1)
2079 min_width = xpad * 2 + rect.x + priv->wrap_width;
2081 min_width = xpad * 2 + rect.x + guess_width;
2083 if (priv->width_chars > 0)
2084 nat_width = xpad * 2 +
2085 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2087 nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2089 nat_width = MAX (nat_width, min_width);
2091 if (priv->max_width_chars > 0)
2093 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2095 min_width = MIN (min_width, max_width);
2096 nat_width = MIN (nat_width, max_width);
2100 *minimum_size = min_width;
2103 *natural_size = nat_width;
2108 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2111 gint *minimum_height,
2112 gint *natural_height)
2114 GtkCellRendererTextPrivate *priv;
2115 GtkCellRendererText *celltext;
2116 PangoLayout *layout;
2117 gint text_height, xpad, ypad;
2120 celltext = GTK_CELL_RENDERER_TEXT (cell);
2121 priv = celltext->priv;
2123 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2125 layout = get_layout (celltext, widget, NULL, 0);
2127 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2128 pango_layout_get_pixel_size (layout, NULL, &text_height);
2131 *minimum_height = text_height + ypad * 2;
2134 *natural_height = text_height + ypad * 2;
2136 g_object_unref (layout);
2140 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2147 /* Thankfully cell renderers dont rotate, so they only have to do
2148 * height-for-width and not the opposite. Here we have only to return
2149 * the height for the base minimum width of the renderer.
2151 * Note this code path wont be followed by GtkTreeView which is
2152 * height-for-width specifically.
2154 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2155 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2156 minimum_size, natural_size);