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"
25 #include "gtkmarshalers.h"
27 #include "gtktreeprivate.h"
30 static void gtk_cell_renderer_text_init (GtkCellRendererText *celltext);
31 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
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_get_size (GtkCellRenderer *cell,
44 GdkRectangle *cell_area,
49 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
52 GdkRectangle *background_area,
53 GdkRectangle *cell_area,
54 GdkRectangle *expose_area,
55 GtkCellRendererState flags);
57 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
61 GdkRectangle *background_area,
62 GdkRectangle *cell_area,
63 GtkCellRendererState flags);
76 PROP_SINGLE_PARAGRAPH_MODE,
103 /* Whether-a-style-arg-is-set args */
114 PROP_STRIKETHROUGH_SET,
121 static gpointer parent_class;
122 static guint text_cell_renderer_signals [LAST_SIGNAL];
124 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
126 #define GTK_CELL_RENDERER_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TEXT, GtkCellRendererTextPrivate))
128 typedef struct _GtkCellRendererTextPrivate GtkCellRendererTextPrivate;
129 struct _GtkCellRendererTextPrivate
131 guint single_paragraph : 1;
132 guint language_set : 1;
133 guint markup_set : 1;
134 guint ellipsize_set : 1;
137 PangoLanguage *language;
138 PangoEllipsizeMode ellipsize;
139 PangoWrapMode wrap_mode;
141 gulong populate_popup_id;
142 gulong entry_menu_popdown_timeout;
143 gboolean in_entry_menu;
153 gtk_cell_renderer_text_get_type (void)
155 static GType cell_text_type = 0;
159 static const GTypeInfo cell_text_info =
161 sizeof (GtkCellRendererTextClass),
162 NULL, /* base_init */
163 NULL, /* base_finalize */
164 (GClassInitFunc) gtk_cell_renderer_text_class_init,
165 NULL, /* class_finalize */
166 NULL, /* class_data */
167 sizeof (GtkCellRendererText),
169 (GInstanceInitFunc) gtk_cell_renderer_text_init,
173 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText",
177 return cell_text_type;
181 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
183 GtkCellRendererTextPrivate *priv;
185 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
187 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
188 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
189 GTK_CELL_RENDERER (celltext)->xpad = 2;
190 GTK_CELL_RENDERER (celltext)->ypad = 2;
191 celltext->fixed_height_rows = -1;
192 celltext->font = pango_font_description_new ();
194 priv->width_chars = -1;
195 priv->wrap_width = -1;
199 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
201 GObjectClass *object_class = G_OBJECT_CLASS (class);
202 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
204 parent_class = g_type_class_peek_parent (class);
206 object_class->finalize = gtk_cell_renderer_text_finalize;
208 object_class->get_property = gtk_cell_renderer_text_get_property;
209 object_class->set_property = gtk_cell_renderer_text_set_property;
211 cell_class->get_size = gtk_cell_renderer_text_get_size;
212 cell_class->render = gtk_cell_renderer_text_render;
213 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
215 g_object_class_install_property (object_class,
217 g_param_spec_string ("text",
219 P_("Text to render"),
223 g_object_class_install_property (object_class,
225 g_param_spec_string ("markup",
227 P_("Marked up text to render"),
231 g_object_class_install_property (object_class,
233 g_param_spec_boxed ("attributes",
235 P_("A list of style attributes to apply to the text of the renderer"),
236 PANGO_TYPE_ATTR_LIST,
239 g_object_class_install_property (object_class,
240 PROP_SINGLE_PARAGRAPH_MODE,
241 g_param_spec_boolean ("single-paragraph-mode",
242 P_("Single Paragraph Mode"),
243 P_("Whether or not to keep all text in a single paragraph"),
248 g_object_class_install_property (object_class,
250 g_param_spec_string ("background",
251 P_("Background color name"),
252 P_("Background color as a string"),
256 g_object_class_install_property (object_class,
258 g_param_spec_boxed ("background-gdk",
259 P_("Background color"),
260 P_("Background color as a GdkColor"),
262 G_PARAM_READABLE | G_PARAM_WRITABLE));
264 g_object_class_install_property (object_class,
266 g_param_spec_string ("foreground",
267 P_("Foreground color name"),
268 P_("Foreground color as a string"),
272 g_object_class_install_property (object_class,
274 g_param_spec_boxed ("foreground-gdk",
275 P_("Foreground color"),
276 P_("Foreground color as a GdkColor"),
278 G_PARAM_READABLE | G_PARAM_WRITABLE));
281 g_object_class_install_property (object_class,
283 g_param_spec_boolean ("editable",
285 P_("Whether the text can be modified by the user"),
287 G_PARAM_READABLE | G_PARAM_WRITABLE));
289 g_object_class_install_property (object_class,
291 g_param_spec_string ("font",
293 P_("Font description as a string"),
295 G_PARAM_READABLE | G_PARAM_WRITABLE));
297 g_object_class_install_property (object_class,
299 g_param_spec_boxed ("font-desc",
301 P_("Font description as a PangoFontDescription struct"),
302 PANGO_TYPE_FONT_DESCRIPTION,
303 G_PARAM_READABLE | G_PARAM_WRITABLE));
306 g_object_class_install_property (object_class,
308 g_param_spec_string ("family",
310 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
312 G_PARAM_READABLE | G_PARAM_WRITABLE));
314 g_object_class_install_property (object_class,
316 g_param_spec_enum ("style",
321 G_PARAM_READABLE | G_PARAM_WRITABLE));
323 g_object_class_install_property (object_class,
325 g_param_spec_enum ("variant",
329 PANGO_VARIANT_NORMAL,
330 G_PARAM_READABLE | G_PARAM_WRITABLE));
332 g_object_class_install_property (object_class,
334 g_param_spec_int ("weight",
340 G_PARAM_READABLE | G_PARAM_WRITABLE));
342 g_object_class_install_property (object_class,
344 g_param_spec_enum ("stretch",
348 PANGO_STRETCH_NORMAL,
349 G_PARAM_READABLE | G_PARAM_WRITABLE));
351 g_object_class_install_property (object_class,
353 g_param_spec_int ("size",
359 G_PARAM_READABLE | G_PARAM_WRITABLE));
361 g_object_class_install_property (object_class,
363 g_param_spec_double ("size-points",
365 P_("Font size in points"),
369 G_PARAM_READABLE | G_PARAM_WRITABLE));
371 g_object_class_install_property (object_class,
373 g_param_spec_double ("scale",
375 P_("Font scaling factor"),
379 G_PARAM_READABLE | G_PARAM_WRITABLE));
381 g_object_class_install_property (object_class,
383 g_param_spec_int ("rise",
385 P_("Offset of text above the baseline "
386 "(below the baseline if rise is negative)"),
390 G_PARAM_READABLE | G_PARAM_WRITABLE));
393 g_object_class_install_property (object_class,
395 g_param_spec_boolean ("strikethrough",
397 P_("Whether to strike through the text"),
399 G_PARAM_READABLE | G_PARAM_WRITABLE));
401 g_object_class_install_property (object_class,
403 g_param_spec_enum ("underline",
405 P_("Style of underline for this text"),
406 PANGO_TYPE_UNDERLINE,
407 PANGO_UNDERLINE_NONE,
408 G_PARAM_READABLE | G_PARAM_WRITABLE));
410 g_object_class_install_property (object_class,
412 g_param_spec_string ("language",
414 P_("The language this text is in, as an ISO code. "
415 "Pango can use this as a hint when rendering the text. "
416 "If you don't understand this parameter, you probably don't need it"),
422 * GtkCellRendererText:ellipsize:
424 * Specifies the preferred place to ellipsize the string, if the cell renderer
425 * does not have enough room to display the entire string. Setting it to
426 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
427 * for another way of making the text fit into a given width.
431 g_object_class_install_property (object_class,
433 g_param_spec_enum ("ellipsize",
435 P_("The preferred place to ellipsize the string, "
436 "if the cell renderer does not have enough room "
437 "to display the entire string, if at all"),
438 PANGO_TYPE_ELLIPSIZE_MODE,
439 PANGO_ELLIPSIZE_NONE,
443 * GtkCellRendererText:width-chars:
445 * The desired width of the cell, in characters. If this property is set to
446 * -1, the width will be calculated automatically, otherwise the cell will
447 * request either 3 characters or the property value, whichever is greater.
451 g_object_class_install_property (object_class,
453 g_param_spec_int ("width-chars",
454 P_("Width In Characters"),
455 P_("The desired width of the label, in characters"),
462 * GtkCellRendererText:wrap-mode:
464 * Specifies how to break the string into multiple lines, if the cell renderer
465 * does not have enough room to display the entire string. This property has no
466 * effect unless the wrap-width property is set.
470 g_object_class_install_property (object_class,
472 g_param_spec_enum ("wrap-mode",
474 P_("How to break the string into multiple lines, "
475 "if the cell renderer does not have enough room "
476 "to display the entire string"),
477 PANGO_TYPE_WRAP_MODE,
482 * GtkCellRendererText:wrap-width:
484 * Specifies the width at which the text is wrapped. The wrap-mode property can
485 * be used to influence at what character positions the line breaks can be placed.
486 * Setting wrap-width to -1 turns wrapping off.
490 g_object_class_install_property (object_class,
492 g_param_spec_int ("wrap-width",
494 P_("The width at which the text is wrapped"),
501 /* Style props are set or not */
503 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, G_PARAM_READABLE | G_PARAM_WRITABLE))
505 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
506 P_("Background set"),
507 P_("Whether this tag affects the background color"));
509 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
510 P_("Foreground set"),
511 P_("Whether this tag affects the foreground color"));
513 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
514 P_("Editability set"),
515 P_("Whether this tag affects text editability"));
517 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
518 P_("Font family set"),
519 P_("Whether this tag affects the font family"));
521 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
522 P_("Font style set"),
523 P_("Whether this tag affects the font style"));
525 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
526 P_("Font variant set"),
527 P_("Whether this tag affects the font variant"));
529 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
530 P_("Font weight set"),
531 P_("Whether this tag affects the font weight"));
533 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
534 P_("Font stretch set"),
535 P_("Whether this tag affects the font stretch"));
537 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
539 P_("Whether this tag affects the font size"));
541 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
542 P_("Font scale set"),
543 P_("Whether this tag scales the font size by a factor"));
545 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
547 P_("Whether this tag affects the rise"));
549 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
550 P_("Strikethrough set"),
551 P_("Whether this tag affects strikethrough"));
553 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
555 P_("Whether this tag affects underlining"));
557 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
559 P_("Whether this tag affects the language the text is rendered as"));
561 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
563 P_("Whether this tag affects the ellipsize mode"));
565 text_cell_renderer_signals [EDITED] =
566 g_signal_new ("edited",
567 G_OBJECT_CLASS_TYPE (object_class),
569 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
571 _gtk_marshal_VOID__STRING_STRING,
576 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
580 gtk_cell_renderer_text_finalize (GObject *object)
582 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
583 GtkCellRendererTextPrivate *priv;
585 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
587 pango_font_description_free (celltext->font);
590 g_free (celltext->text);
592 if (celltext->extra_attrs)
593 pango_attr_list_unref (celltext->extra_attrs);
596 g_object_unref (priv->language);
598 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
602 get_property_font_set_mask (guint prop_id)
606 case PROP_FAMILY_SET:
607 return PANGO_FONT_MASK_FAMILY;
609 return PANGO_FONT_MASK_STYLE;
610 case PROP_VARIANT_SET:
611 return PANGO_FONT_MASK_VARIANT;
612 case PROP_WEIGHT_SET:
613 return PANGO_FONT_MASK_WEIGHT;
614 case PROP_STRETCH_SET:
615 return PANGO_FONT_MASK_STRETCH;
617 return PANGO_FONT_MASK_SIZE;
624 gtk_cell_renderer_text_get_property (GObject *object,
629 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
630 GtkCellRendererTextPrivate *priv;
632 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
637 g_value_set_string (value, celltext->text);
640 case PROP_ATTRIBUTES:
641 g_value_set_boxed (value, celltext->extra_attrs);
644 case PROP_SINGLE_PARAGRAPH_MODE:
645 g_value_set_boolean (value, priv->single_paragraph);
648 case PROP_BACKGROUND_GDK:
652 color.red = celltext->background.red;
653 color.green = celltext->background.green;
654 color.blue = celltext->background.blue;
656 g_value_set_boxed (value, &color);
660 case PROP_FOREGROUND_GDK:
664 color.red = celltext->foreground.red;
665 color.green = celltext->foreground.green;
666 color.blue = celltext->foreground.blue;
668 g_value_set_boxed (value, &color);
674 /* FIXME GValue imposes a totally gratuitous string copy
675 * here, we could just hand off string ownership
677 gchar *str = pango_font_description_to_string (celltext->font);
678 g_value_set_string (value, str);
684 g_value_set_boxed (value, celltext->font);
688 g_value_set_string (value, pango_font_description_get_family (celltext->font));
692 g_value_set_enum (value, pango_font_description_get_style (celltext->font));
696 g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
700 g_value_set_int (value, pango_font_description_get_weight (celltext->font));
704 g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
708 g_value_set_int (value, pango_font_description_get_size (celltext->font));
711 case PROP_SIZE_POINTS:
712 g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
716 g_value_set_double (value, celltext->font_scale);
720 g_value_set_boolean (value, celltext->editable);
723 case PROP_STRIKETHROUGH:
724 g_value_set_boolean (value, celltext->strikethrough);
728 g_value_set_enum (value, celltext->underline_style);
732 g_value_set_int (value, celltext->rise);
736 g_value_set_string (value, pango_language_to_string (priv->language));
740 g_value_set_enum (value, priv->ellipsize);
744 g_value_set_enum (value, priv->wrap_mode);
747 case PROP_WRAP_WIDTH:
748 g_value_set_int (value, priv->wrap_width);
751 case PROP_BACKGROUND_SET:
752 g_value_set_boolean (value, celltext->background_set);
755 case PROP_FOREGROUND_SET:
756 g_value_set_boolean (value, celltext->foreground_set);
759 case PROP_FAMILY_SET:
761 case PROP_VARIANT_SET:
762 case PROP_WEIGHT_SET:
763 case PROP_STRETCH_SET:
766 PangoFontMask mask = get_property_font_set_mask (param_id);
767 g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
773 g_value_set_boolean (value, celltext->scale_set);
776 case PROP_EDITABLE_SET:
777 g_value_set_boolean (value, celltext->editable_set);
780 case PROP_STRIKETHROUGH_SET:
781 g_value_set_boolean (value, celltext->strikethrough_set);
784 case PROP_UNDERLINE_SET:
785 g_value_set_boolean (value, celltext->underline_set);
789 g_value_set_boolean (value, celltext->rise_set);
792 case PROP_LANGUAGE_SET:
793 g_value_set_boolean (value, priv->language_set);
796 case PROP_ELLIPSIZE_SET:
797 g_value_set_boolean (value, priv->ellipsize_set);
800 case PROP_WIDTH_CHARS:
801 g_value_set_int (value, priv->width_chars);
804 case PROP_BACKGROUND:
805 case PROP_FOREGROUND:
808 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
815 set_bg_color (GtkCellRendererText *celltext,
820 if (!celltext->background_set)
822 celltext->background_set = TRUE;
823 g_object_notify (G_OBJECT (celltext), "background_set");
826 celltext->background.red = color->red;
827 celltext->background.green = color->green;
828 celltext->background.blue = color->blue;
832 if (celltext->background_set)
834 celltext->background_set = FALSE;
835 g_object_notify (G_OBJECT (celltext), "background_set");
842 set_fg_color (GtkCellRendererText *celltext,
847 if (!celltext->foreground_set)
849 celltext->foreground_set = TRUE;
850 g_object_notify (G_OBJECT (celltext), "foreground_set");
853 celltext->foreground.red = color->red;
854 celltext->foreground.green = color->green;
855 celltext->foreground.blue = color->blue;
859 if (celltext->foreground_set)
861 celltext->foreground_set = FALSE;
862 g_object_notify (G_OBJECT (celltext), "foreground_set");
868 set_font_desc_fields (PangoFontDescription *desc,
869 PangoFontMask to_set)
871 PangoFontMask changed_mask = 0;
873 if (to_set & PANGO_FONT_MASK_FAMILY)
875 const char *family = pango_font_description_get_family (desc);
879 changed_mask |= PANGO_FONT_MASK_FAMILY;
882 pango_font_description_set_family (desc, family);
884 if (to_set & PANGO_FONT_MASK_STYLE)
885 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
886 if (to_set & PANGO_FONT_MASK_VARIANT)
887 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
888 if (to_set & PANGO_FONT_MASK_WEIGHT)
889 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
890 if (to_set & PANGO_FONT_MASK_STRETCH)
891 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
892 if (to_set & PANGO_FONT_MASK_SIZE)
894 gint size = pango_font_description_get_size (desc);
897 size = 10 * PANGO_SCALE;
898 changed_mask |= PANGO_FONT_MASK_SIZE;
901 pango_font_description_set_size (desc, size);
908 notify_set_changed (GObject *object,
909 PangoFontMask changed_mask)
911 if (changed_mask & PANGO_FONT_MASK_FAMILY)
912 g_object_notify (object, "family_set");
913 if (changed_mask & PANGO_FONT_MASK_STYLE)
914 g_object_notify (object, "style_set");
915 if (changed_mask & PANGO_FONT_MASK_VARIANT)
916 g_object_notify (object, "variant_set");
917 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
918 g_object_notify (object, "weight_set");
919 if (changed_mask & PANGO_FONT_MASK_STRETCH)
920 g_object_notify (object, "stretch_set");
921 if (changed_mask & PANGO_FONT_MASK_SIZE)
922 g_object_notify (object, "size_set");
926 notify_fields_changed (GObject *object,
927 PangoFontMask changed_mask)
929 if (changed_mask & PANGO_FONT_MASK_FAMILY)
930 g_object_notify (object, "family");
931 if (changed_mask & PANGO_FONT_MASK_STYLE)
932 g_object_notify (object, "style");
933 if (changed_mask & PANGO_FONT_MASK_VARIANT)
934 g_object_notify (object, "variant");
935 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
936 g_object_notify (object, "weight");
937 if (changed_mask & PANGO_FONT_MASK_STRETCH)
938 g_object_notify (object, "stretch");
939 if (changed_mask & PANGO_FONT_MASK_SIZE)
940 g_object_notify (object, "size");
944 set_font_description (GtkCellRendererText *celltext,
945 PangoFontDescription *font_desc)
947 GObject *object = G_OBJECT (celltext);
948 PangoFontDescription *new_font_desc;
949 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
952 new_font_desc = pango_font_description_copy (font_desc);
954 new_font_desc = pango_font_description_new ();
956 old_mask = pango_font_description_get_set_fields (celltext->font);
957 new_mask = pango_font_description_get_set_fields (new_font_desc);
959 changed_mask = old_mask | new_mask;
960 set_changed_mask = old_mask ^ new_mask;
962 pango_font_description_free (celltext->font);
963 celltext->font = new_font_desc;
965 g_object_freeze_notify (object);
967 g_object_notify (object, "font_desc");
968 g_object_notify (object, "font");
970 if (changed_mask & PANGO_FONT_MASK_FAMILY)
971 g_object_notify (object, "family");
972 if (changed_mask & PANGO_FONT_MASK_STYLE)
973 g_object_notify (object, "style");
974 if (changed_mask & PANGO_FONT_MASK_VARIANT)
975 g_object_notify (object, "variant");
976 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
977 g_object_notify (object, "weight");
978 if (changed_mask & PANGO_FONT_MASK_STRETCH)
979 g_object_notify (object, "stretch");
980 if (changed_mask & PANGO_FONT_MASK_SIZE)
982 g_object_notify (object, "size");
983 g_object_notify (object, "size_points");
986 notify_set_changed (object, set_changed_mask);
988 g_object_thaw_notify (object);
992 gtk_cell_renderer_text_set_property (GObject *object,
997 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
998 GtkCellRendererTextPrivate *priv;
1000 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
1006 g_free (celltext->text);
1008 if (priv->markup_set)
1010 if (celltext->extra_attrs)
1011 pango_attr_list_unref (celltext->extra_attrs);
1012 celltext->extra_attrs = NULL;
1013 priv->markup_set = FALSE;
1016 celltext->text = g_strdup (g_value_get_string (value));
1017 g_object_notify (object, "text");
1020 case PROP_ATTRIBUTES:
1021 if (celltext->extra_attrs)
1022 pango_attr_list_unref (celltext->extra_attrs);
1024 celltext->extra_attrs = g_value_get_boxed (value);
1025 if (celltext->extra_attrs)
1026 pango_attr_list_ref (celltext->extra_attrs);
1032 GError *error = NULL;
1033 PangoAttrList *attrs = NULL;
1035 str = g_value_get_string (value);
1036 if (str && !pango_parse_markup (str,
1044 g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
1046 g_error_free (error);
1051 g_free (celltext->text);
1053 if (celltext->extra_attrs)
1054 pango_attr_list_unref (celltext->extra_attrs);
1056 celltext->text = text;
1057 celltext->extra_attrs = attrs;
1058 priv->markup_set = TRUE;
1062 case PROP_SINGLE_PARAGRAPH_MODE:
1063 priv->single_paragraph = g_value_get_boolean (value);
1066 case PROP_BACKGROUND:
1070 if (!g_value_get_string (value))
1071 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
1072 else if (gdk_color_parse (g_value_get_string (value), &color))
1073 set_bg_color (celltext, &color);
1075 g_warning ("Don't know color `%s'", g_value_get_string (value));
1077 g_object_notify (object, "background_gdk");
1081 case PROP_FOREGROUND:
1085 if (!g_value_get_string (value))
1086 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1087 else if (gdk_color_parse (g_value_get_string (value), &color))
1088 set_fg_color (celltext, &color);
1090 g_warning ("Don't know color `%s'", g_value_get_string (value));
1092 g_object_notify (object, "foreground_gdk");
1096 case PROP_BACKGROUND_GDK:
1097 /* This notifies the GObject itself. */
1098 set_bg_color (celltext, g_value_get_boxed (value));
1101 case PROP_FOREGROUND_GDK:
1102 /* This notifies the GObject itself. */
1103 set_fg_color (celltext, g_value_get_boxed (value));
1108 PangoFontDescription *font_desc = NULL;
1111 name = g_value_get_string (value);
1114 font_desc = pango_font_description_from_string (name);
1116 set_font_description (celltext, font_desc);
1118 pango_font_description_free (font_desc);
1120 if (celltext->fixed_height_rows != -1)
1121 celltext->calc_fixed_height = TRUE;
1125 case PROP_FONT_DESC:
1126 set_font_description (celltext, g_value_get_boxed (value));
1128 if (celltext->fixed_height_rows != -1)
1129 celltext->calc_fixed_height = TRUE;
1138 case PROP_SIZE_POINTS:
1140 PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
1145 pango_font_description_set_family (celltext->font,
1146 g_value_get_string (value));
1149 pango_font_description_set_style (celltext->font,
1150 g_value_get_enum (value));
1153 pango_font_description_set_variant (celltext->font,
1154 g_value_get_enum (value));
1157 pango_font_description_set_weight (celltext->font,
1158 g_value_get_int (value));
1161 pango_font_description_set_stretch (celltext->font,
1162 g_value_get_enum (value));
1165 pango_font_description_set_size (celltext->font,
1166 g_value_get_int (value));
1167 g_object_notify (object, "size_points");
1169 case PROP_SIZE_POINTS:
1170 pango_font_description_set_size (celltext->font,
1171 g_value_get_double (value) * PANGO_SCALE);
1172 g_object_notify (object, "size");
1176 if (celltext->fixed_height_rows != -1)
1177 celltext->calc_fixed_height = TRUE;
1179 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
1180 g_object_notify (object, "font_desc");
1181 g_object_notify (object, "font");
1187 celltext->font_scale = g_value_get_double (value);
1188 celltext->scale_set = TRUE;
1189 if (celltext->fixed_height_rows != -1)
1190 celltext->calc_fixed_height = TRUE;
1191 g_object_notify (object, "scale_set");
1195 celltext->editable = g_value_get_boolean (value);
1196 celltext->editable_set = TRUE;
1197 if (celltext->editable)
1198 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
1200 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_INERT;
1201 g_object_notify (object, "editable_set");
1204 case PROP_STRIKETHROUGH:
1205 celltext->strikethrough = g_value_get_boolean (value);
1206 celltext->strikethrough_set = TRUE;
1207 g_object_notify (object, "strikethrough_set");
1210 case PROP_UNDERLINE:
1211 celltext->underline_style = g_value_get_enum (value);
1212 celltext->underline_set = TRUE;
1213 g_object_notify (object, "underline_set");
1218 celltext->rise = g_value_get_int (value);
1219 celltext->rise_set = TRUE;
1220 g_object_notify (object, "rise_set");
1221 if (celltext->fixed_height_rows != -1)
1222 celltext->calc_fixed_height = TRUE;
1226 priv->language_set = TRUE;
1228 g_object_unref (priv->language);
1229 priv->language = pango_language_from_string (g_value_get_string (value));
1230 g_object_notify (object, "language_set");
1233 case PROP_ELLIPSIZE:
1234 priv->ellipsize = g_value_get_enum (value);
1235 priv->ellipsize_set = TRUE;
1236 g_object_notify (object, "ellipsize_set");
1239 case PROP_WRAP_MODE:
1240 priv->wrap_mode = g_value_get_enum (value);
1243 case PROP_WRAP_WIDTH:
1244 priv->wrap_width = g_value_get_int (value);
1247 case PROP_WIDTH_CHARS:
1248 priv->width_chars = g_value_get_int (value);
1251 case PROP_BACKGROUND_SET:
1252 celltext->background_set = g_value_get_boolean (value);
1255 case PROP_FOREGROUND_SET:
1256 celltext->foreground_set = g_value_get_boolean (value);
1259 case PROP_FAMILY_SET:
1260 case PROP_STYLE_SET:
1261 case PROP_VARIANT_SET:
1262 case PROP_WEIGHT_SET:
1263 case PROP_STRETCH_SET:
1265 if (!g_value_get_boolean (value))
1267 pango_font_description_unset_fields (celltext->font,
1268 get_property_font_set_mask (param_id));
1272 PangoFontMask changed_mask;
1274 changed_mask = set_font_desc_fields (celltext->font,
1275 get_property_font_set_mask (param_id));
1276 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1280 case PROP_SCALE_SET:
1281 celltext->scale_set = g_value_get_boolean (value);
1284 case PROP_EDITABLE_SET:
1285 celltext->editable_set = g_value_get_boolean (value);
1288 case PROP_STRIKETHROUGH_SET:
1289 celltext->strikethrough_set = g_value_get_boolean (value);
1292 case PROP_UNDERLINE_SET:
1293 celltext->underline_set = g_value_get_boolean (value);
1297 celltext->rise_set = g_value_get_boolean (value);
1300 case PROP_LANGUAGE_SET:
1301 priv->language_set = g_value_get_boolean (value);
1304 case PROP_ELLIPSIZE_SET:
1305 priv->ellipsize_set = g_value_get_boolean (value);
1309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1315 * gtk_cell_renderer_text_new:
1317 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1318 * object properties. Object properties can be
1319 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1320 * you can bind a property to a value in a #GtkTreeModel. For example,
1321 * you can bind the "text" property on the cell renderer to a string
1322 * value in the model, thus rendering a different string in each row
1323 * of the #GtkTreeView
1325 * Return value: the new cell renderer
1328 gtk_cell_renderer_text_new (void)
1330 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1334 add_attr (PangoAttrList *attr_list,
1335 PangoAttribute *attr)
1337 attr->start_index = 0;
1338 attr->end_index = G_MAXINT;
1340 pango_attr_list_insert (attr_list, attr);
1344 get_layout (GtkCellRendererText *celltext,
1346 gboolean will_render,
1347 GtkCellRendererState flags)
1349 PangoAttrList *attr_list;
1350 PangoLayout *layout;
1351 PangoUnderline uline;
1352 GtkCellRendererTextPrivate *priv;
1354 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
1356 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1358 if (celltext->extra_attrs)
1359 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1361 attr_list = pango_attr_list_new ();
1363 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1367 /* Add options that affect appearance but not size */
1369 /* note that background doesn't go here, since it affects
1370 * background_area not the PangoLayout area
1373 if (celltext->foreground_set
1374 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1378 color = celltext->foreground;
1380 add_attr (attr_list,
1381 pango_attr_foreground_new (color.red, color.green, color.blue));
1384 if (celltext->strikethrough_set)
1385 add_attr (attr_list,
1386 pango_attr_strikethrough_new (celltext->strikethrough));
1389 add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1391 if (celltext->scale_set &&
1392 celltext->font_scale != 1.0)
1393 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1395 if (celltext->underline_set)
1396 uline = celltext->underline_style;
1398 uline = PANGO_UNDERLINE_NONE;
1400 if (priv->language_set)
1401 add_attr (attr_list, pango_attr_language_new (priv->language));
1403 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1407 case PANGO_UNDERLINE_NONE:
1408 uline = PANGO_UNDERLINE_SINGLE;
1411 case PANGO_UNDERLINE_SINGLE:
1412 uline = PANGO_UNDERLINE_DOUBLE;
1420 if (uline != PANGO_UNDERLINE_NONE)
1421 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1423 if (celltext->rise_set)
1424 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1426 if (priv->ellipsize_set)
1427 pango_layout_set_ellipsize (layout, priv->ellipsize);
1429 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1431 if (priv->wrap_width != -1)
1433 pango_layout_set_width (layout, priv->wrap_width * PANGO_SCALE);
1434 pango_layout_set_wrap (layout, priv->wrap_mode);
1436 if (pango_layout_get_line_count (layout) == 1)
1438 pango_layout_set_width (layout, -1);
1439 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1444 pango_layout_set_width (layout, -1);
1445 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1448 pango_layout_set_attributes (layout, attr_list);
1450 pango_attr_list_unref (attr_list);
1456 get_size (GtkCellRenderer *cell,
1458 GdkRectangle *cell_area,
1459 PangoLayout *layout,
1465 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1466 PangoRectangle rect;
1467 GtkCellRendererTextPrivate *priv;
1469 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1471 if (celltext->calc_fixed_height)
1473 PangoContext *context;
1474 PangoFontMetrics *metrics;
1475 PangoFontDescription *font_desc;
1478 font_desc = pango_font_description_copy (widget->style->font_desc);
1479 pango_font_description_merge (font_desc, celltext->font, TRUE);
1481 if (celltext->scale_set)
1482 pango_font_description_set_size (font_desc,
1483 celltext->font_scale * pango_font_description_get_size (font_desc));
1485 context = gtk_widget_get_pango_context (widget);
1487 metrics = pango_context_get_metrics (context,
1489 pango_context_get_language (context));
1490 row_height = (pango_font_metrics_get_ascent (metrics) +
1491 pango_font_metrics_get_descent (metrics));
1492 pango_font_metrics_unref (metrics);
1494 pango_font_description_free (font_desc);
1496 gtk_cell_renderer_set_fixed_size (cell,
1497 cell->width, 2*cell->ypad +
1498 celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1502 *height = cell->height;
1505 celltext->calc_fixed_height = FALSE;
1511 g_object_ref (layout);
1513 layout = get_layout (celltext, widget, FALSE, 0);
1515 pango_layout_get_pixel_extents (layout, NULL, &rect);
1518 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1520 /* The minimum size for ellipsized labels is ~ 3 chars */
1523 if (priv->ellipsize || priv->width_chars > 0)
1525 PangoContext *context;
1526 PangoFontMetrics *metrics;
1529 context = pango_layout_get_context (layout);
1530 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
1532 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1533 pango_font_metrics_unref (metrics);
1535 *width = GTK_CELL_RENDERER (celltext)->xpad * 2
1536 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1540 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1548 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1549 *x_offset = (1.0 - cell->xalign) * (cell_area->width - rect.width - (2 * cell->xpad));
1551 *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
1553 if (priv->ellipsize_set || priv->wrap_width != -1)
1554 *x_offset = MAX(*x_offset, 0);
1558 *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1559 *y_offset = MAX (*y_offset, 0);
1563 g_object_unref (layout);
1568 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1570 GdkRectangle *cell_area,
1576 get_size (cell, widget, cell_area, NULL,
1577 x_offset, y_offset, width, height);
1581 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1582 GdkDrawable *window,
1584 GdkRectangle *background_area,
1585 GdkRectangle *cell_area,
1586 GdkRectangle *expose_area,
1587 GtkCellRendererState flags)
1590 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1591 PangoLayout *layout;
1595 GtkCellRendererTextPrivate *priv;
1597 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1599 layout = get_layout (celltext, widget, TRUE, flags);
1600 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1602 if (!cell->sensitive)
1604 state = GTK_STATE_INSENSITIVE;
1606 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1608 if (GTK_WIDGET_HAS_FOCUS (widget))
1609 state = GTK_STATE_SELECTED;
1611 state = GTK_STATE_ACTIVE;
1613 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1614 GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT)
1616 state = GTK_STATE_PRELIGHT;
1620 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
1621 state = GTK_STATE_INSENSITIVE;
1623 state = GTK_STATE_NORMAL;
1626 if (celltext->background_set &&
1627 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1632 color.red = celltext->background.red;
1633 color.green = celltext->background.green;
1634 color.blue = celltext->background.blue;
1636 gc = gdk_gc_new (window);
1638 gdk_gc_set_rgb_fg_color (gc, &color);
1641 gdk_gc_set_clip_rectangle (gc, expose_area);
1642 gdk_draw_rectangle (window,
1647 background_area->width,
1648 background_area->height);
1650 gdk_gc_set_clip_rectangle (gc, NULL);
1651 g_object_unref (gc);
1654 if (priv->ellipsize_set)
1655 pango_layout_set_width (layout,
1656 (cell_area->width - x_offset - 2 * cell->xpad) * PANGO_SCALE);
1657 else if (priv->wrap_width == -1)
1658 pango_layout_set_width (layout, -1);
1660 gtk_paint_layout (widget->style,
1667 cell_area->x + x_offset + cell->xpad,
1668 cell_area->y + y_offset + cell->ypad,
1671 g_object_unref (layout);
1675 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1679 const gchar *new_text;
1680 GtkCellRendererTextPrivate *priv;
1682 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1686 if (priv->focus_out_id > 0)
1688 g_signal_handler_disconnect (entry, priv->focus_out_id);
1689 priv->focus_out_id = 0;
1692 if (priv->populate_popup_id > 0)
1694 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1695 priv->populate_popup_id = 0;
1698 if (priv->entry_menu_popdown_timeout)
1700 g_source_remove (priv->entry_menu_popdown_timeout);
1701 priv->entry_menu_popdown_timeout = 0;
1704 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data),
1705 GTK_ENTRY (entry)->editing_canceled);
1706 if (GTK_ENTRY (entry)->editing_canceled)
1709 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1710 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1712 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1716 popdown_timeout (gpointer data)
1718 GtkCellRendererTextPrivate *priv;
1720 GDK_THREADS_ENTER ();
1722 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1724 priv->entry_menu_popdown_timeout = 0;
1726 if (!GTK_WIDGET_HAS_FOCUS (priv->entry))
1727 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1729 GDK_THREADS_LEAVE ();
1735 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1738 GtkCellRendererTextPrivate *priv;
1740 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1742 priv->in_entry_menu = FALSE;
1744 if (priv->entry_menu_popdown_timeout)
1747 priv->entry_menu_popdown_timeout = g_timeout_add (500, popdown_timeout,
1752 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1756 GtkCellRendererTextPrivate *priv;
1758 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1760 if (priv->entry_menu_popdown_timeout)
1762 g_source_remove (priv->entry_menu_popdown_timeout);
1763 priv->entry_menu_popdown_timeout = 0;
1766 priv->in_entry_menu = TRUE;
1768 g_signal_connect (menu, "unmap",
1769 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1773 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1777 GtkCellRendererTextPrivate *priv;
1779 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1781 if (priv->in_entry_menu)
1784 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (entry), data);
1786 /* entry needs focus-out-event */
1790 static GtkCellEditable *
1791 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1795 GdkRectangle *background_area,
1796 GdkRectangle *cell_area,
1797 GtkCellRendererState flags)
1799 GtkCellRendererText *celltext;
1800 GtkCellRendererTextPrivate *priv;
1802 celltext = GTK_CELL_RENDERER_TEXT (cell);
1803 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1805 /* If the cell isn't editable we return NULL. */
1806 if (celltext->editable == FALSE)
1809 priv->entry = g_object_new (GTK_TYPE_ENTRY,
1811 "xalign", cell->xalign,
1815 gtk_entry_set_text (GTK_ENTRY (priv->entry), celltext->text);
1816 g_object_set_data_full (G_OBJECT (priv->entry), GTK_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
1818 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1821 priv->in_entry_menu = FALSE;
1822 if (priv->entry_menu_popdown_timeout)
1824 g_source_remove (priv->entry_menu_popdown_timeout);
1825 priv->entry_menu_popdown_timeout = 0;
1828 g_signal_connect (priv->entry,
1830 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1832 priv->focus_out_id = g_signal_connect (priv->entry, "focus_out_event",
1833 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1835 priv->populate_popup_id =
1836 g_signal_connect (priv->entry, "populate_popup",
1837 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1840 gtk_widget_show (priv->entry);
1842 return GTK_CELL_EDITABLE (priv->entry);
1846 * gtk_cell_renderer_text_set_fixed_height_from_font:
1847 * @renderer: A #GtkCellRendererText
1848 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1850 * Sets the height of a renderer to explicitly be determined by the "font" and
1851 * "y_pad" property set on it. Further changes in these properties do not
1852 * affect the height, so they must be accompanied by a subsequent call to this
1853 * function. Using this function is unflexible, and should really only be used
1854 * if calculating the size of a cell is too slow (ie, a massive number of cells
1855 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1856 * the height is determined by the properties again.
1859 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1860 gint number_of_rows)
1862 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1863 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1865 if (number_of_rows == -1)
1867 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1868 GTK_CELL_RENDERER (renderer)->width,
1873 renderer->fixed_height_rows = number_of_rows;
1874 renderer->calc_fixed_height = TRUE;
1878 #define __GTK_CELL_RENDERER_TEXT_C__
1879 #include "gtkaliasdef.c"