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"
23 #include "gtkeditable.h"
24 #include "gtkcellsizerequest.h"
26 #include "gtkmarshalers.h"
28 #include "gtkprivate.h"
29 #include "gtktreeprivate.h"
32 static void gtk_cell_renderer_text_finalize (GObject *object);
34 static void gtk_cell_renderer_text_get_property (GObject *object,
38 static void gtk_cell_renderer_text_set_property (GObject *object,
42 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
45 GdkRectangle *background_area,
46 GdkRectangle *cell_area,
47 GdkRectangle *expose_area,
48 GtkCellRendererState flags);
50 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
54 GdkRectangle *background_area,
55 GdkRectangle *cell_area,
56 GtkCellRendererState flags);
58 static void gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface);
59 static void gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
63 static void gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
67 static void gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
71 gint *natural_height);
84 PROP_SINGLE_PARAGRAPH_MODE,
113 /* Whether-a-style-arg-is-set args */
124 PROP_STRIKETHROUGH_SET,
132 static guint text_cell_renderer_signals [LAST_SIGNAL];
134 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
136 struct _GtkCellRendererTextPrivate
140 PangoAlignment align;
141 PangoAttrList *extra_attrs;
142 PangoColor foreground;
143 PangoColor background;
144 PangoEllipsizeMode ellipsize;
145 PangoFontDescription *font;
146 PangoLanguage *language;
147 PangoUnderline underline_style;
148 PangoWrapMode wrap_mode;
150 gboolean in_entry_menu;
157 gint fixed_height_rows;
159 gint max_width_chars;
162 guint strikethrough : 1;
165 guint foreground_set : 1;
166 guint background_set : 1;
167 guint underline_set : 1;
169 guint strikethrough_set : 1;
170 guint editable_set : 1;
171 guint calc_fixed_height : 1;
172 guint single_paragraph : 1;
173 guint language_set : 1;
174 guint markup_set : 1;
175 guint ellipsize_set : 1;
179 gulong populate_popup_id;
180 gulong entry_menu_popdown_timeout;
183 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER,
184 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_SIZE_REQUEST,
185 gtk_cell_renderer_text_cell_size_request_init))
188 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
190 GtkCellRendererTextPrivate *priv;
191 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
193 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
194 GTK_TYPE_CELL_RENDERER_TEXT,
195 GtkCellRendererTextPrivate);
196 priv = celltext->priv;
198 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
199 gtk_cell_renderer_set_padding (cell, 2, 2);
200 priv->font_scale = 1.0;
201 priv->fixed_height_rows = -1;
202 priv->font = pango_font_description_new ();
204 priv->width_chars = -1;
205 priv->max_width_chars = -1;
206 priv->wrap_width = -1;
207 priv->wrap_mode = PANGO_WRAP_CHAR;
208 priv->align = PANGO_ALIGN_LEFT;
209 priv->align_set = FALSE;
213 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
215 GObjectClass *object_class = G_OBJECT_CLASS (class);
216 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
218 object_class->finalize = gtk_cell_renderer_text_finalize;
220 object_class->get_property = gtk_cell_renderer_text_get_property;
221 object_class->set_property = gtk_cell_renderer_text_set_property;
223 cell_class->render = gtk_cell_renderer_text_render;
224 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
226 g_object_class_install_property (object_class,
228 g_param_spec_string ("text",
230 P_("Text to render"),
232 GTK_PARAM_READWRITE));
234 g_object_class_install_property (object_class,
236 g_param_spec_string ("markup",
238 P_("Marked up text to render"),
240 GTK_PARAM_WRITABLE));
242 g_object_class_install_property (object_class,
244 g_param_spec_boxed ("attributes",
246 P_("A list of style attributes to apply to the text of the renderer"),
247 PANGO_TYPE_ATTR_LIST,
248 GTK_PARAM_READWRITE));
250 g_object_class_install_property (object_class,
251 PROP_SINGLE_PARAGRAPH_MODE,
252 g_param_spec_boolean ("single-paragraph-mode",
253 P_("Single Paragraph Mode"),
254 P_("Whether to keep all text in a single paragraph"),
256 GTK_PARAM_READWRITE));
259 g_object_class_install_property (object_class,
261 g_param_spec_string ("background",
262 P_("Background color name"),
263 P_("Background color as a string"),
265 GTK_PARAM_WRITABLE));
267 g_object_class_install_property (object_class,
269 g_param_spec_boxed ("background-gdk",
270 P_("Background color"),
271 P_("Background color as a GdkColor"),
273 GTK_PARAM_READWRITE));
275 g_object_class_install_property (object_class,
277 g_param_spec_string ("foreground",
278 P_("Foreground color name"),
279 P_("Foreground color as a string"),
281 GTK_PARAM_WRITABLE));
283 g_object_class_install_property (object_class,
285 g_param_spec_boxed ("foreground-gdk",
286 P_("Foreground color"),
287 P_("Foreground color as a GdkColor"),
289 GTK_PARAM_READWRITE));
292 g_object_class_install_property (object_class,
294 g_param_spec_boolean ("editable",
296 P_("Whether the text can be modified by the user"),
298 GTK_PARAM_READWRITE));
300 g_object_class_install_property (object_class,
302 g_param_spec_string ("font",
304 P_("Font description as a string, e.g. \"Sans Italic 12\""),
306 GTK_PARAM_READWRITE));
308 g_object_class_install_property (object_class,
310 g_param_spec_boxed ("font-desc",
312 P_("Font description as a PangoFontDescription struct"),
313 PANGO_TYPE_FONT_DESCRIPTION,
314 GTK_PARAM_READWRITE));
317 g_object_class_install_property (object_class,
319 g_param_spec_string ("family",
321 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
323 GTK_PARAM_READWRITE));
325 g_object_class_install_property (object_class,
327 g_param_spec_enum ("style",
332 GTK_PARAM_READWRITE));
334 g_object_class_install_property (object_class,
336 g_param_spec_enum ("variant",
340 PANGO_VARIANT_NORMAL,
341 GTK_PARAM_READWRITE));
343 g_object_class_install_property (object_class,
345 g_param_spec_int ("weight",
351 GTK_PARAM_READWRITE));
353 g_object_class_install_property (object_class,
355 g_param_spec_enum ("stretch",
359 PANGO_STRETCH_NORMAL,
360 GTK_PARAM_READWRITE));
362 g_object_class_install_property (object_class,
364 g_param_spec_int ("size",
370 GTK_PARAM_READWRITE));
372 g_object_class_install_property (object_class,
374 g_param_spec_double ("size-points",
376 P_("Font size in points"),
380 GTK_PARAM_READWRITE));
382 g_object_class_install_property (object_class,
384 g_param_spec_double ("scale",
386 P_("Font scaling factor"),
390 GTK_PARAM_READWRITE));
392 g_object_class_install_property (object_class,
394 g_param_spec_int ("rise",
396 P_("Offset of text above the baseline "
397 "(below the baseline if rise is negative)"),
401 GTK_PARAM_READWRITE));
404 g_object_class_install_property (object_class,
406 g_param_spec_boolean ("strikethrough",
408 P_("Whether to strike through the text"),
410 GTK_PARAM_READWRITE));
412 g_object_class_install_property (object_class,
414 g_param_spec_enum ("underline",
416 P_("Style of underline for this text"),
417 PANGO_TYPE_UNDERLINE,
418 PANGO_UNDERLINE_NONE,
419 GTK_PARAM_READWRITE));
421 g_object_class_install_property (object_class,
423 g_param_spec_string ("language",
425 P_("The language this text is in, as an ISO code. "
426 "Pango can use this as a hint when rendering the text. "
427 "If you don't understand this parameter, you probably don't need it"),
429 GTK_PARAM_READWRITE));
433 * GtkCellRendererText:ellipsize:
435 * Specifies the preferred place to ellipsize the string, if the cell renderer
436 * does not have enough room to display the entire string. Setting it to
437 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
438 * for another way of making the text fit into a given width.
442 g_object_class_install_property (object_class,
444 g_param_spec_enum ("ellipsize",
446 P_("The preferred place to ellipsize the string, "
447 "if the cell renderer does not have enough room "
448 "to display the entire string"),
449 PANGO_TYPE_ELLIPSIZE_MODE,
450 PANGO_ELLIPSIZE_NONE,
451 GTK_PARAM_READWRITE));
454 * GtkCellRendererText:width-chars:
456 * The desired width of the cell, in characters. If this property is set to
457 * -1, the width will be calculated automatically, otherwise the cell will
458 * request either 3 characters or the property value, whichever is greater.
462 g_object_class_install_property (object_class,
464 g_param_spec_int ("width-chars",
465 P_("Width In Characters"),
466 P_("The desired width of the label, in characters"),
470 GTK_PARAM_READWRITE));
474 * GtkCellRendererText:max-width-chars:
476 * The desired maximum width of the cell, in characters. If this property
477 * is set to -1, the width will be calculated automatically.
479 * For cell renderers that ellipsize or wrap text; this property
480 * controls the maximum reported width of the cell. The
481 * cell should not receive any greater allocation unless it is
482 * set to expand in its #GtkCellLayout and all of the cell's siblings
483 * have received their natural width.
487 g_object_class_install_property (object_class,
488 PROP_MAX_WIDTH_CHARS,
489 g_param_spec_int ("max-width-chars",
490 P_("Maximum Width In Characters"),
491 P_("The maximum width of the cell, in characters"),
495 GTK_PARAM_READWRITE));
498 * GtkCellRendererText:wrap-mode:
500 * Specifies how to break the string into multiple lines, if the cell
501 * renderer does not have enough room to display the entire string.
502 * This property has no effect unless the wrap-width property is set.
506 g_object_class_install_property (object_class,
508 g_param_spec_enum ("wrap-mode",
510 P_("How to break the string into multiple lines, "
511 "if the cell renderer does not have enough room "
512 "to display the entire string"),
513 PANGO_TYPE_WRAP_MODE,
515 GTK_PARAM_READWRITE));
518 * GtkCellRendererText:wrap-width:
520 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
521 * be used to influence at what character positions the line breaks can be placed.
522 * Setting wrap-width to -1 turns wrapping off.
526 g_object_class_install_property (object_class,
528 g_param_spec_int ("wrap-width",
530 P_("The width at which the text is wrapped"),
534 GTK_PARAM_READWRITE));
537 * GtkCellRendererText:alignment:
539 * Specifies how to align the lines of text with respect to each other.
541 * Note that this property describes how to align the lines of text in
542 * case there are several of them. The "xalign" property of #GtkCellRenderer,
543 * on the other hand, sets the horizontal alignment of the whole text.
547 g_object_class_install_property (object_class,
549 g_param_spec_enum ("alignment",
551 P_("How to align the lines"),
552 PANGO_TYPE_ALIGNMENT,
554 GTK_PARAM_READWRITE));
558 /* Style props are set or not */
560 #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))
562 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
563 P_("Background set"),
564 P_("Whether this tag affects the background color"));
566 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
567 P_("Foreground set"),
568 P_("Whether this tag affects the foreground color"));
570 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
571 P_("Editability set"),
572 P_("Whether this tag affects text editability"));
574 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
575 P_("Font family set"),
576 P_("Whether this tag affects the font family"));
578 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
579 P_("Font style set"),
580 P_("Whether this tag affects the font style"));
582 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
583 P_("Font variant set"),
584 P_("Whether this tag affects the font variant"));
586 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
587 P_("Font weight set"),
588 P_("Whether this tag affects the font weight"));
590 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
591 P_("Font stretch set"),
592 P_("Whether this tag affects the font stretch"));
594 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
596 P_("Whether this tag affects the font size"));
598 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
599 P_("Font scale set"),
600 P_("Whether this tag scales the font size by a factor"));
602 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
604 P_("Whether this tag affects the rise"));
606 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
607 P_("Strikethrough set"),
608 P_("Whether this tag affects strikethrough"));
610 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
612 P_("Whether this tag affects underlining"));
614 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
616 P_("Whether this tag affects the language the text is rendered as"));
618 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
620 P_("Whether this tag affects the ellipsize mode"));
622 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
624 P_("Whether this tag affects the alignment mode"));
627 * GtkCellRendererText::edited
628 * @renderer: the object which received the signal
629 * @path: the path identifying the edited cell
630 * @new_text: the new text
632 * This signal is emitted after @renderer has been edited.
634 * It is the responsibility of the application to update the model
635 * and store @new_text at the position indicated by @path.
637 text_cell_renderer_signals [EDITED] =
638 g_signal_new (I_("edited"),
639 G_OBJECT_CLASS_TYPE (object_class),
641 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
643 _gtk_marshal_VOID__STRING_STRING,
648 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
652 gtk_cell_renderer_text_finalize (GObject *object)
654 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
655 GtkCellRendererTextPrivate *priv = celltext->priv;
657 pango_font_description_free (priv->font);
661 if (priv->extra_attrs)
662 pango_attr_list_unref (priv->extra_attrs);
665 g_object_unref (priv->language);
667 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
671 get_property_font_set_mask (guint prop_id)
675 case PROP_FAMILY_SET:
676 return PANGO_FONT_MASK_FAMILY;
678 return PANGO_FONT_MASK_STYLE;
679 case PROP_VARIANT_SET:
680 return PANGO_FONT_MASK_VARIANT;
681 case PROP_WEIGHT_SET:
682 return PANGO_FONT_MASK_WEIGHT;
683 case PROP_STRETCH_SET:
684 return PANGO_FONT_MASK_STRETCH;
686 return PANGO_FONT_MASK_SIZE;
693 gtk_cell_renderer_text_get_property (GObject *object,
698 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
699 GtkCellRendererTextPrivate *priv = celltext->priv;
704 g_value_set_string (value, priv->text);
707 case PROP_ATTRIBUTES:
708 g_value_set_boxed (value, priv->extra_attrs);
711 case PROP_SINGLE_PARAGRAPH_MODE:
712 g_value_set_boolean (value, priv->single_paragraph);
715 case PROP_BACKGROUND_GDK:
719 color.red = priv->background.red;
720 color.green = priv->background.green;
721 color.blue = priv->background.blue;
723 g_value_set_boxed (value, &color);
727 case PROP_FOREGROUND_GDK:
731 color.red = priv->foreground.red;
732 color.green = priv->foreground.green;
733 color.blue = priv->foreground.blue;
735 g_value_set_boxed (value, &color);
740 g_value_take_string (value, pango_font_description_to_string (priv->font));
744 g_value_set_boxed (value, priv->font);
748 g_value_set_string (value, pango_font_description_get_family (priv->font));
752 g_value_set_enum (value, pango_font_description_get_style (priv->font));
756 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
760 g_value_set_int (value, pango_font_description_get_weight (priv->font));
764 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
768 g_value_set_int (value, pango_font_description_get_size (priv->font));
771 case PROP_SIZE_POINTS:
772 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
776 g_value_set_double (value, priv->font_scale);
780 g_value_set_boolean (value, priv->editable);
783 case PROP_STRIKETHROUGH:
784 g_value_set_boolean (value, priv->strikethrough);
788 g_value_set_enum (value, priv->underline_style);
792 g_value_set_int (value, priv->rise);
796 g_value_set_static_string (value, pango_language_to_string (priv->language));
800 g_value_set_enum (value, priv->ellipsize);
804 g_value_set_enum (value, priv->wrap_mode);
807 case PROP_WRAP_WIDTH:
808 g_value_set_int (value, priv->wrap_width);
812 g_value_set_enum (value, priv->align);
815 case PROP_BACKGROUND_SET:
816 g_value_set_boolean (value, priv->background_set);
819 case PROP_FOREGROUND_SET:
820 g_value_set_boolean (value, priv->foreground_set);
823 case PROP_FAMILY_SET:
825 case PROP_VARIANT_SET:
826 case PROP_WEIGHT_SET:
827 case PROP_STRETCH_SET:
830 PangoFontMask mask = get_property_font_set_mask (param_id);
831 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
837 g_value_set_boolean (value, priv->scale_set);
840 case PROP_EDITABLE_SET:
841 g_value_set_boolean (value, priv->editable_set);
844 case PROP_STRIKETHROUGH_SET:
845 g_value_set_boolean (value, priv->strikethrough_set);
848 case PROP_UNDERLINE_SET:
849 g_value_set_boolean (value, priv->underline_set);
853 g_value_set_boolean (value, priv->rise_set);
856 case PROP_LANGUAGE_SET:
857 g_value_set_boolean (value, priv->language_set);
860 case PROP_ELLIPSIZE_SET:
861 g_value_set_boolean (value, priv->ellipsize_set);
865 g_value_set_boolean (value, priv->align_set);
868 case PROP_WIDTH_CHARS:
869 g_value_set_int (value, priv->width_chars);
872 case PROP_MAX_WIDTH_CHARS:
873 g_value_set_int (value, priv->max_width_chars);
876 case PROP_BACKGROUND:
877 case PROP_FOREGROUND:
880 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
887 set_bg_color (GtkCellRendererText *celltext,
890 GtkCellRendererTextPrivate *priv = celltext->priv;
894 if (!priv->background_set)
896 priv->background_set = TRUE;
897 g_object_notify (G_OBJECT (celltext), "background-set");
900 priv->background.red = color->red;
901 priv->background.green = color->green;
902 priv->background.blue = color->blue;
906 if (priv->background_set)
908 priv->background_set = FALSE;
909 g_object_notify (G_OBJECT (celltext), "background-set");
916 set_fg_color (GtkCellRendererText *celltext,
919 GtkCellRendererTextPrivate *priv = celltext->priv;
923 if (!priv->foreground_set)
925 priv->foreground_set = TRUE;
926 g_object_notify (G_OBJECT (celltext), "foreground-set");
929 priv->foreground.red = color->red;
930 priv->foreground.green = color->green;
931 priv->foreground.blue = color->blue;
935 if (priv->foreground_set)
937 priv->foreground_set = FALSE;
938 g_object_notify (G_OBJECT (celltext), "foreground-set");
944 set_font_desc_fields (PangoFontDescription *desc,
945 PangoFontMask to_set)
947 PangoFontMask changed_mask = 0;
949 if (to_set & PANGO_FONT_MASK_FAMILY)
951 const char *family = pango_font_description_get_family (desc);
955 changed_mask |= PANGO_FONT_MASK_FAMILY;
958 pango_font_description_set_family (desc, family);
960 if (to_set & PANGO_FONT_MASK_STYLE)
961 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
962 if (to_set & PANGO_FONT_MASK_VARIANT)
963 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
964 if (to_set & PANGO_FONT_MASK_WEIGHT)
965 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
966 if (to_set & PANGO_FONT_MASK_STRETCH)
967 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
968 if (to_set & PANGO_FONT_MASK_SIZE)
970 gint size = pango_font_description_get_size (desc);
973 size = 10 * PANGO_SCALE;
974 changed_mask |= PANGO_FONT_MASK_SIZE;
977 pango_font_description_set_size (desc, size);
984 notify_set_changed (GObject *object,
985 PangoFontMask changed_mask)
987 if (changed_mask & PANGO_FONT_MASK_FAMILY)
988 g_object_notify (object, "family-set");
989 if (changed_mask & PANGO_FONT_MASK_STYLE)
990 g_object_notify (object, "style-set");
991 if (changed_mask & PANGO_FONT_MASK_VARIANT)
992 g_object_notify (object, "variant-set");
993 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
994 g_object_notify (object, "weight-set");
995 if (changed_mask & PANGO_FONT_MASK_STRETCH)
996 g_object_notify (object, "stretch-set");
997 if (changed_mask & PANGO_FONT_MASK_SIZE)
998 g_object_notify (object, "size-set");
1002 notify_fields_changed (GObject *object,
1003 PangoFontMask changed_mask)
1005 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1006 g_object_notify (object, "family");
1007 if (changed_mask & PANGO_FONT_MASK_STYLE)
1008 g_object_notify (object, "style");
1009 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1010 g_object_notify (object, "variant");
1011 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1012 g_object_notify (object, "weight");
1013 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1014 g_object_notify (object, "stretch");
1015 if (changed_mask & PANGO_FONT_MASK_SIZE)
1016 g_object_notify (object, "size");
1020 set_font_description (GtkCellRendererText *celltext,
1021 PangoFontDescription *font_desc)
1023 GtkCellRendererTextPrivate *priv = celltext->priv;
1024 GObject *object = G_OBJECT (celltext);
1025 PangoFontDescription *new_font_desc;
1026 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1029 new_font_desc = pango_font_description_copy (font_desc);
1031 new_font_desc = pango_font_description_new ();
1033 old_mask = pango_font_description_get_set_fields (priv->font);
1034 new_mask = pango_font_description_get_set_fields (new_font_desc);
1036 changed_mask = old_mask | new_mask;
1037 set_changed_mask = old_mask ^ new_mask;
1039 pango_font_description_free (priv->font);
1040 priv->font = new_font_desc;
1042 g_object_freeze_notify (object);
1044 g_object_notify (object, "font-desc");
1045 g_object_notify (object, "font");
1047 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1048 g_object_notify (object, "family");
1049 if (changed_mask & PANGO_FONT_MASK_STYLE)
1050 g_object_notify (object, "style");
1051 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1052 g_object_notify (object, "variant");
1053 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1054 g_object_notify (object, "weight");
1055 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1056 g_object_notify (object, "stretch");
1057 if (changed_mask & PANGO_FONT_MASK_SIZE)
1059 g_object_notify (object, "size");
1060 g_object_notify (object, "size-points");
1063 notify_set_changed (object, set_changed_mask);
1065 g_object_thaw_notify (object);
1069 gtk_cell_renderer_text_set_property (GObject *object,
1071 const GValue *value,
1074 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1075 GtkCellRendererTextPrivate *priv = celltext->priv;
1080 g_free (priv->text);
1082 if (priv->markup_set)
1084 if (priv->extra_attrs)
1085 pango_attr_list_unref (priv->extra_attrs);
1086 priv->extra_attrs = NULL;
1087 priv->markup_set = FALSE;
1090 priv->text = g_value_dup_string (value);
1091 g_object_notify (object, "text");
1094 case PROP_ATTRIBUTES:
1095 if (priv->extra_attrs)
1096 pango_attr_list_unref (priv->extra_attrs);
1098 priv->extra_attrs = g_value_get_boxed (value);
1099 if (priv->extra_attrs)
1100 pango_attr_list_ref (priv->extra_attrs);
1106 GError *error = NULL;
1107 PangoAttrList *attrs = NULL;
1109 str = g_value_get_string (value);
1110 if (str && !pango_parse_markup (str,
1118 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1120 g_error_free (error);
1124 g_free (priv->text);
1126 if (priv->extra_attrs)
1127 pango_attr_list_unref (priv->extra_attrs);
1130 priv->extra_attrs = attrs;
1131 priv->markup_set = TRUE;
1135 case PROP_SINGLE_PARAGRAPH_MODE:
1136 priv->single_paragraph = g_value_get_boolean (value);
1139 case PROP_BACKGROUND:
1143 if (!g_value_get_string (value))
1144 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1145 else if (gdk_color_parse (g_value_get_string (value), &color))
1146 set_bg_color (celltext, &color);
1148 g_warning ("Don't know color `%s'", g_value_get_string (value));
1150 g_object_notify (object, "background-gdk");
1154 case PROP_FOREGROUND:
1158 if (!g_value_get_string (value))
1159 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1160 else if (gdk_color_parse (g_value_get_string (value), &color))
1161 set_fg_color (celltext, &color);
1163 g_warning ("Don't know color `%s'", g_value_get_string (value));
1165 g_object_notify (object, "foreground-gdk");
1169 case PROP_BACKGROUND_GDK:
1170 /* This notifies the GObject itself. */
1171 set_bg_color (celltext, g_value_get_boxed (value));
1174 case PROP_FOREGROUND_GDK:
1175 /* This notifies the GObject itself. */
1176 set_fg_color (celltext, g_value_get_boxed (value));
1181 PangoFontDescription *font_desc = NULL;
1184 name = g_value_get_string (value);
1187 font_desc = pango_font_description_from_string (name);
1189 set_font_description (celltext, font_desc);
1191 pango_font_description_free (font_desc);
1193 if (priv->fixed_height_rows != -1)
1194 priv->calc_fixed_height = TRUE;
1198 case PROP_FONT_DESC:
1199 set_font_description (celltext, g_value_get_boxed (value));
1201 if (priv->fixed_height_rows != -1)
1202 priv->calc_fixed_height = TRUE;
1211 case PROP_SIZE_POINTS:
1213 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1218 pango_font_description_set_family (priv->font,
1219 g_value_get_string (value));
1222 pango_font_description_set_style (priv->font,
1223 g_value_get_enum (value));
1226 pango_font_description_set_variant (priv->font,
1227 g_value_get_enum (value));
1230 pango_font_description_set_weight (priv->font,
1231 g_value_get_int (value));
1234 pango_font_description_set_stretch (priv->font,
1235 g_value_get_enum (value));
1238 pango_font_description_set_size (priv->font,
1239 g_value_get_int (value));
1240 g_object_notify (object, "size-points");
1242 case PROP_SIZE_POINTS:
1243 pango_font_description_set_size (priv->font,
1244 g_value_get_double (value) * PANGO_SCALE);
1245 g_object_notify (object, "size");
1249 if (priv->fixed_height_rows != -1)
1250 priv->calc_fixed_height = TRUE;
1252 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1253 g_object_notify (object, "font-desc");
1254 g_object_notify (object, "font");
1260 priv->font_scale = g_value_get_double (value);
1261 priv->scale_set = TRUE;
1262 if (priv->fixed_height_rows != -1)
1263 priv->calc_fixed_height = TRUE;
1264 g_object_notify (object, "scale-set");
1268 priv->editable = g_value_get_boolean (value);
1269 priv->editable_set = TRUE;
1271 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1273 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1274 g_object_notify (object, "editable-set");
1277 case PROP_STRIKETHROUGH:
1278 priv->strikethrough = g_value_get_boolean (value);
1279 priv->strikethrough_set = TRUE;
1280 g_object_notify (object, "strikethrough-set");
1283 case PROP_UNDERLINE:
1284 priv->underline_style = g_value_get_enum (value);
1285 priv->underline_set = TRUE;
1286 g_object_notify (object, "underline-set");
1291 priv->rise = g_value_get_int (value);
1292 priv->rise_set = TRUE;
1293 g_object_notify (object, "rise-set");
1294 if (priv->fixed_height_rows != -1)
1295 priv->calc_fixed_height = TRUE;
1299 priv->language_set = TRUE;
1301 g_object_unref (priv->language);
1302 priv->language = pango_language_from_string (g_value_get_string (value));
1303 g_object_notify (object, "language-set");
1306 case PROP_ELLIPSIZE:
1307 priv->ellipsize = g_value_get_enum (value);
1308 priv->ellipsize_set = TRUE;
1309 g_object_notify (object, "ellipsize-set");
1312 case PROP_WRAP_MODE:
1313 priv->wrap_mode = g_value_get_enum (value);
1316 case PROP_WRAP_WIDTH:
1317 priv->wrap_width = g_value_get_int (value);
1320 case PROP_WIDTH_CHARS:
1321 priv->width_chars = g_value_get_int (value);
1324 case PROP_MAX_WIDTH_CHARS:
1325 priv->max_width_chars = g_value_get_int (value);
1329 priv->align = g_value_get_enum (value);
1330 priv->align_set = TRUE;
1331 g_object_notify (object, "align-set");
1334 case PROP_BACKGROUND_SET:
1335 priv->background_set = g_value_get_boolean (value);
1338 case PROP_FOREGROUND_SET:
1339 priv->foreground_set = g_value_get_boolean (value);
1342 case PROP_FAMILY_SET:
1343 case PROP_STYLE_SET:
1344 case PROP_VARIANT_SET:
1345 case PROP_WEIGHT_SET:
1346 case PROP_STRETCH_SET:
1348 if (!g_value_get_boolean (value))
1350 pango_font_description_unset_fields (priv->font,
1351 get_property_font_set_mask (param_id));
1355 PangoFontMask changed_mask;
1357 changed_mask = set_font_desc_fields (priv->font,
1358 get_property_font_set_mask (param_id));
1359 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1363 case PROP_SCALE_SET:
1364 priv->scale_set = g_value_get_boolean (value);
1367 case PROP_EDITABLE_SET:
1368 priv->editable_set = g_value_get_boolean (value);
1371 case PROP_STRIKETHROUGH_SET:
1372 priv->strikethrough_set = g_value_get_boolean (value);
1375 case PROP_UNDERLINE_SET:
1376 priv->underline_set = g_value_get_boolean (value);
1380 priv->rise_set = g_value_get_boolean (value);
1383 case PROP_LANGUAGE_SET:
1384 priv->language_set = g_value_get_boolean (value);
1387 case PROP_ELLIPSIZE_SET:
1388 priv->ellipsize_set = g_value_get_boolean (value);
1391 case PROP_ALIGN_SET:
1392 priv->align_set = g_value_get_boolean (value);
1396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1402 * gtk_cell_renderer_text_new:
1404 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1405 * object properties. Object properties can be
1406 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1407 * you can bind a property to a value in a #GtkTreeModel. For example,
1408 * you can bind the "text" property on the cell renderer to a string
1409 * value in the model, thus rendering a different string in each row
1410 * of the #GtkTreeView
1412 * Return value: the new cell renderer
1415 gtk_cell_renderer_text_new (void)
1417 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1421 add_attr (PangoAttrList *attr_list,
1422 PangoAttribute *attr)
1424 attr->start_index = 0;
1425 attr->end_index = G_MAXINT;
1427 pango_attr_list_insert (attr_list, attr);
1431 get_layout (GtkCellRendererText *celltext,
1433 GdkRectangle *cell_area,
1434 GtkCellRendererState flags)
1436 GtkCellRendererTextPrivate *priv = celltext->priv;
1437 PangoAttrList *attr_list;
1438 PangoLayout *layout;
1439 PangoUnderline uline;
1442 layout = gtk_widget_create_pango_layout (widget, priv->text);
1444 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1446 if (priv->extra_attrs)
1447 attr_list = pango_attr_list_copy (priv->extra_attrs);
1449 attr_list = pango_attr_list_new ();
1451 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1455 /* Add options that affect appearance but not size */
1457 /* note that background doesn't go here, since it affects
1458 * background_area not the PangoLayout area
1461 if (priv->foreground_set
1462 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1466 color = priv->foreground;
1468 add_attr (attr_list,
1469 pango_attr_foreground_new (color.red, color.green, color.blue));
1472 if (priv->strikethrough_set)
1473 add_attr (attr_list,
1474 pango_attr_strikethrough_new (priv->strikethrough));
1477 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1479 if (priv->scale_set &&
1480 priv->font_scale != 1.0)
1481 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1483 if (priv->underline_set)
1484 uline = priv->underline_style;
1486 uline = PANGO_UNDERLINE_NONE;
1488 if (priv->language_set)
1489 add_attr (attr_list, pango_attr_language_new (priv->language));
1491 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1495 case PANGO_UNDERLINE_NONE:
1496 uline = PANGO_UNDERLINE_SINGLE;
1499 case PANGO_UNDERLINE_SINGLE:
1500 uline = PANGO_UNDERLINE_DOUBLE;
1508 if (uline != PANGO_UNDERLINE_NONE)
1509 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1512 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1514 /* Now apply the attributes as they will effect the outcome
1515 * of pango_layout_get_extents() */
1516 pango_layout_set_attributes (layout, attr_list);
1517 pango_attr_list_unref (attr_list);
1519 if (priv->ellipsize_set)
1520 pango_layout_set_ellipsize (layout, priv->ellipsize);
1522 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1524 if (priv->wrap_width != -1)
1526 PangoRectangle rect;
1527 gint width, text_width;
1529 pango_layout_get_extents (layout, NULL, &rect);
1530 text_width = rect.width;
1533 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1535 width = priv->wrap_width * PANGO_SCALE;
1537 width = MIN (width, text_width);
1539 pango_layout_set_width (layout, width);
1540 pango_layout_set_wrap (layout, priv->wrap_mode);
1544 pango_layout_set_width (layout, -1);
1545 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1548 if (priv->align_set)
1549 pango_layout_set_alignment (layout, priv->align);
1552 PangoAlignment align;
1554 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1555 align = PANGO_ALIGN_RIGHT;
1557 align = PANGO_ALIGN_LEFT;
1559 pango_layout_set_alignment (layout, align);
1567 get_size (GtkCellRenderer *cell,
1569 GdkRectangle *cell_area,
1570 PangoLayout *layout,
1576 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1577 GtkCellRendererTextPrivate *priv = celltext->priv;
1578 PangoRectangle rect;
1580 gint cell_width, cell_height;
1582 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1584 if (priv->calc_fixed_height)
1586 PangoContext *context;
1587 PangoFontMetrics *metrics;
1588 PangoFontDescription *font_desc;
1591 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1592 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1594 if (priv->scale_set)
1595 pango_font_description_set_size (font_desc,
1596 priv->font_scale * pango_font_description_get_size (font_desc));
1598 context = gtk_widget_get_pango_context (widget);
1600 metrics = pango_context_get_metrics (context,
1602 pango_context_get_language (context));
1603 row_height = (pango_font_metrics_get_ascent (metrics) +
1604 pango_font_metrics_get_descent (metrics));
1605 pango_font_metrics_unref (metrics);
1607 pango_font_description_free (font_desc);
1609 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1611 gtk_cell_renderer_set_fixed_size (cell,
1612 cell_width, 2 * ypad +
1613 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1617 *height = cell_height;
1620 priv->calc_fixed_height = FALSE;
1626 g_object_ref (layout);
1628 layout = get_layout (celltext, widget, NULL, 0);
1630 pango_layout_get_pixel_extents (layout, NULL, &rect);
1633 *height = ypad * 2 + rect.height;
1635 /* The minimum size for ellipsized labels is ~ 3 chars */
1638 if (priv->ellipsize || priv->width_chars > 0)
1640 PangoContext *context;
1641 PangoFontMetrics *metrics;
1644 context = pango_layout_get_context (layout);
1645 metrics = pango_context_get_metrics (context,
1646 gtk_widget_get_style (widget)->font_desc,
1647 pango_context_get_language (context));
1649 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1650 pango_font_metrics_unref (metrics);
1652 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1656 *width = xpad * 2 + rect.x + rect.width;
1662 gfloat xalign, yalign;
1664 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1668 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1669 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1671 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1673 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1674 *x_offset = MAX(*x_offset, 0);
1678 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1679 *y_offset = MAX (*y_offset, 0);
1684 if (x_offset) *x_offset = 0;
1685 if (y_offset) *y_offset = 0;
1688 g_object_unref (layout);
1692 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1693 GdkDrawable *window,
1695 GdkRectangle *background_area,
1696 GdkRectangle *cell_area,
1697 GdkRectangle *expose_area,
1698 GtkCellRendererState flags)
1701 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1702 GtkCellRendererTextPrivate *priv = celltext->priv;
1703 PangoLayout *layout;
1709 layout = get_layout (celltext, widget, cell_area, flags);
1710 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1712 if (!gtk_cell_renderer_get_sensitive (cell))
1714 state = GTK_STATE_INSENSITIVE;
1716 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1718 if (gtk_widget_has_focus (widget))
1719 state = GTK_STATE_SELECTED;
1721 state = GTK_STATE_ACTIVE;
1723 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1724 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1726 state = GTK_STATE_PRELIGHT;
1730 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1731 state = GTK_STATE_INSENSITIVE;
1733 state = GTK_STATE_NORMAL;
1736 if (priv->background_set &&
1737 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1739 cairo_t *cr = gdk_cairo_create (window);
1743 gdk_cairo_rectangle (cr, expose_area);
1747 gdk_cairo_rectangle (cr, background_area);
1748 cairo_set_source_rgb (cr,
1749 priv->background.red / 65535.,
1750 priv->background.green / 65535.,
1751 priv->background.blue / 65535.);
1757 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1759 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1760 pango_layout_set_width (layout,
1761 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1762 else if (priv->wrap_width == -1)
1763 pango_layout_set_width (layout, -1);
1765 gtk_paint_layout (gtk_widget_get_style (widget),
1772 cell_area->x + x_offset + xpad,
1773 cell_area->y + y_offset + ypad,
1776 g_object_unref (layout);
1780 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1783 GtkCellRendererTextPrivate *priv;
1785 const gchar *new_text;
1788 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1792 if (priv->focus_out_id > 0)
1794 g_signal_handler_disconnect (entry, priv->focus_out_id);
1795 priv->focus_out_id = 0;
1798 if (priv->populate_popup_id > 0)
1800 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1801 priv->populate_popup_id = 0;
1804 if (priv->entry_menu_popdown_timeout)
1806 g_source_remove (priv->entry_menu_popdown_timeout);
1807 priv->entry_menu_popdown_timeout = 0;
1810 g_object_get (entry,
1811 "editing-canceled", &canceled,
1813 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1818 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1819 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1821 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1825 popdown_timeout (gpointer data)
1827 GtkCellRendererTextPrivate *priv;
1829 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1831 priv->entry_menu_popdown_timeout = 0;
1833 if (!gtk_widget_has_focus (priv->entry))
1834 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1840 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1843 GtkCellRendererTextPrivate *priv;
1845 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1847 priv->in_entry_menu = FALSE;
1849 if (priv->entry_menu_popdown_timeout)
1852 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1857 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1861 GtkCellRendererTextPrivate *priv;
1863 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1865 if (priv->entry_menu_popdown_timeout)
1867 g_source_remove (priv->entry_menu_popdown_timeout);
1868 priv->entry_menu_popdown_timeout = 0;
1871 priv->in_entry_menu = TRUE;
1873 g_signal_connect (menu, "unmap",
1874 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1878 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1882 GtkCellRendererTextPrivate *priv;
1884 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1886 if (priv->in_entry_menu)
1889 g_object_set (entry,
1890 "editing-canceled", TRUE,
1892 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1893 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1895 /* entry needs focus-out-event */
1899 static GtkCellEditable *
1900 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1904 GdkRectangle *background_area,
1905 GdkRectangle *cell_area,
1906 GtkCellRendererState flags)
1908 GtkRequisition requisition;
1909 GtkCellRendererText *celltext;
1910 GtkCellRendererTextPrivate *priv;
1911 gfloat xalign, yalign;
1913 celltext = GTK_CELL_RENDERER_TEXT (cell);
1914 priv = celltext->priv;
1916 /* If the cell isn't editable we return NULL. */
1917 if (priv->editable == FALSE)
1920 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1922 priv->entry = gtk_entry_new ();
1923 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1924 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1927 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1928 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1930 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1932 gtk_widget_size_request (priv->entry, &requisition);
1933 if (requisition.height < cell_area->height)
1935 GtkBorder *style_border;
1938 gtk_widget_style_get (priv->entry,
1939 "inner-border", &style_border,
1944 border = *style_border;
1945 g_boxed_free (GTK_TYPE_BORDER, style_border);
1949 /* Since boxed style properties can't have default values ... */
1954 border.top = (cell_area->height - requisition.height) / 2;
1955 border.bottom = (cell_area->height - requisition.height) / 2;
1956 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
1959 priv->in_entry_menu = FALSE;
1960 if (priv->entry_menu_popdown_timeout)
1962 g_source_remove (priv->entry_menu_popdown_timeout);
1963 priv->entry_menu_popdown_timeout = 0;
1966 g_signal_connect (priv->entry,
1968 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1970 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
1971 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1973 priv->populate_popup_id =
1974 g_signal_connect (priv->entry, "populate-popup",
1975 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1978 gtk_widget_show (priv->entry);
1980 return GTK_CELL_EDITABLE (priv->entry);
1984 * gtk_cell_renderer_text_set_fixed_height_from_font:
1985 * @renderer: A #GtkCellRendererText
1986 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1988 * Sets the height of a renderer to explicitly be determined by the "font" and
1989 * "y_pad" property set on it. Further changes in these properties do not
1990 * affect the height, so they must be accompanied by a subsequent call to this
1991 * function. Using this function is unflexible, and should really only be used
1992 * if calculating the size of a cell is too slow (ie, a massive number of cells
1993 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1994 * the height is determined by the properties again.
1997 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1998 gint number_of_rows)
2000 GtkCellRendererTextPrivate *priv;
2001 GtkCellRenderer *cell;
2003 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2004 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2006 cell = GTK_CELL_RENDERER (renderer);
2007 priv = renderer->priv;
2009 if (number_of_rows == -1)
2013 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2014 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2018 priv->fixed_height_rows = number_of_rows;
2019 priv->calc_fixed_height = TRUE;
2024 gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface)
2026 iface->get_width = gtk_cell_renderer_text_get_width;
2027 iface->get_height = gtk_cell_renderer_text_get_height;
2028 iface->get_height_for_width = gtk_cell_renderer_text_get_height_for_width;
2032 gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
2037 GtkCellRendererTextPrivate *priv;
2038 GtkCellRendererText *celltext;
2040 PangoLayout *layout;
2041 PangoContext *context;
2042 PangoFontMetrics *metrics;
2043 PangoRectangle rect;
2044 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2045 gint min_width, nat_width;
2047 /* "width-chars" Hard-coded minimum width:
2048 * - minimum size should be MAX (width-chars, strlen ("..."));
2049 * - natural size should be MAX (width-chars, strlen (label->text));
2051 * "wrap-width" User specified natural wrap width
2052 * - minimum size should be MAX (width-chars, 0)
2053 * - natural size should be MIN (wrap-width, strlen (label->text))
2056 celltext = GTK_CELL_RENDERER_TEXT (cell);
2057 priv = celltext->priv;
2059 style = gtk_widget_get_style (widget);
2061 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, NULL);
2063 layout = get_layout (celltext, widget, NULL, 0);
2065 /* Get the layout with the text possibly wrapping at wrap_width */
2066 pango_layout_get_pixel_extents (layout, NULL, &rect);
2067 guess_width = rect.width;
2069 /* Fetch the length of the complete unwrapped text */
2070 pango_layout_set_width (layout, -1);
2071 pango_layout_get_extents (layout, NULL, &rect);
2072 text_width = rect.width;
2074 /* Fetch the average size of a charachter */
2075 context = pango_layout_get_context (layout);
2076 metrics = pango_context_get_metrics (context, style->font_desc,
2077 pango_context_get_language (context));
2079 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2080 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2081 char_pixels = MAX (char_width, digit_width);
2083 pango_font_metrics_unref (metrics);
2084 g_object_unref (layout);
2086 /* enforce minimum width for ellipsized labels at ~3 chars */
2087 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2088 ellipsize_chars = 3;
2090 ellipsize_chars = 0;
2092 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2094 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2095 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2096 else if (priv->wrap_width > -1)
2097 min_width = xpad * 2 + rect.x + priv->wrap_width;
2099 min_width = xpad * 2 + rect.x + guess_width;
2101 if (priv->width_chars > 0)
2102 nat_width = xpad * 2 +
2103 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2105 nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2107 nat_width = MAX (nat_width, min_width);
2109 if (priv->max_width_chars > 0)
2111 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2113 min_width = MIN (min_width, max_width);
2114 nat_width = MIN (nat_width, max_width);
2118 *minimum_size = min_width;
2121 *natural_size = nat_width;
2126 gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
2129 gint *minimum_height,
2130 gint *natural_height)
2132 GtkCellRendererTextPrivate *priv;
2133 GtkCellRendererText *celltext;
2134 PangoLayout *layout;
2135 gint text_height, xpad, ypad;
2138 celltext = GTK_CELL_RENDERER_TEXT (cell);
2139 priv = celltext->priv;
2141 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, &ypad);
2143 layout = get_layout (celltext, widget, FALSE, 0);
2145 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2146 pango_layout_get_pixel_size (layout, NULL, &text_height);
2149 *minimum_height = text_height + ypad * 2;
2152 *natural_height = text_height + ypad * 2;
2154 g_object_unref (layout);
2158 gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
2165 /* Thankfully cell renderers dont rotate, so they only have to do
2166 * height-for-width and not the opposite. Here we have only to return
2167 * the height for the base minimum width of the renderer.
2169 * Note this code path wont be followed by GtkTreeView which is
2170 * height-for-width specifically.
2172 gtk_cell_size_request_get_width (cell, widget, &min_width, NULL);
2173 gtk_cell_renderer_text_get_height_for_width (cell, widget, min_width,
2174 minimum_size, natural_size);