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.
21 #include "gtkcellrenderertext.h"
22 #include "gtkeditable.h"
24 #include "gtkmarshalers.h"
26 #include "gtktreeprivate.h"
28 static void gtk_cell_renderer_text_init (GtkCellRendererText *celltext);
29 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
30 static void gtk_cell_renderer_text_finalize (GObject *object);
32 static void gtk_cell_renderer_text_get_property (GObject *object,
36 static void gtk_cell_renderer_text_set_property (GObject *object,
40 static void gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
42 GdkRectangle *cell_area,
47 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
50 GdkRectangle *background_area,
51 GdkRectangle *cell_area,
52 GdkRectangle *expose_area,
53 GtkCellRendererState flags);
55 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
59 GdkRectangle *background_area,
60 GdkRectangle *cell_area,
61 GtkCellRendererState flags);
74 PROP_SINGLE_PARAGRAPH_MODE,
97 /* Whether-a-style-arg-is-set args */
108 PROP_STRIKETHROUGH_SET,
114 static gpointer parent_class;
115 static guint text_cell_renderer_signals [LAST_SIGNAL];
117 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
119 #define GTK_CELL_RENDERER_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TEXT, GtkCellRendererTextPrivate))
121 typedef struct _GtkCellRendererTextPrivate GtkCellRendererTextPrivate;
122 struct _GtkCellRendererTextPrivate
124 guint single_paragraph : 1;
125 guint language_set : 1;
126 guint markup_set : 1;
129 PangoLanguage *language;
134 gtk_cell_renderer_text_get_type (void)
136 static GType cell_text_type = 0;
140 static const GTypeInfo cell_text_info =
142 sizeof (GtkCellRendererTextClass),
143 NULL, /* base_init */
144 NULL, /* base_finalize */
145 (GClassInitFunc) gtk_cell_renderer_text_class_init,
146 NULL, /* class_finalize */
147 NULL, /* class_data */
148 sizeof (GtkCellRendererText),
150 (GInstanceInitFunc) gtk_cell_renderer_text_init,
154 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText",
158 return cell_text_type;
162 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
164 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
165 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
166 GTK_CELL_RENDERER (celltext)->xpad = 2;
167 GTK_CELL_RENDERER (celltext)->ypad = 2;
168 celltext->fixed_height_rows = -1;
169 celltext->font = pango_font_description_new ();
173 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
175 GObjectClass *object_class = G_OBJECT_CLASS (class);
176 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
178 parent_class = g_type_class_peek_parent (class);
180 object_class->finalize = gtk_cell_renderer_text_finalize;
182 object_class->get_property = gtk_cell_renderer_text_get_property;
183 object_class->set_property = gtk_cell_renderer_text_set_property;
185 cell_class->get_size = gtk_cell_renderer_text_get_size;
186 cell_class->render = gtk_cell_renderer_text_render;
187 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
189 g_object_class_install_property (object_class,
191 g_param_spec_string ("text",
193 P_("Text to render"),
197 g_object_class_install_property (object_class,
199 g_param_spec_string ("markup",
201 P_("Marked up text to render"),
205 g_object_class_install_property (object_class,
207 g_param_spec_boxed ("attributes",
209 P_("A list of style attributes to apply to the text of the renderer"),
210 PANGO_TYPE_ATTR_LIST,
213 g_object_class_install_property (object_class,
214 PROP_SINGLE_PARAGRAPH_MODE,
215 g_param_spec_boolean ("single_paragraph_mode",
216 P_("Single Paragraph Mode"),
217 P_("Whether or not to keep all text in a single paragraph"),
222 g_object_class_install_property (object_class,
224 g_param_spec_string ("background",
225 P_("Background color name"),
226 P_("Background color as a string"),
230 g_object_class_install_property (object_class,
232 g_param_spec_boxed ("background_gdk",
233 P_("Background color"),
234 P_("Background color as a GdkColor"),
236 G_PARAM_READABLE | G_PARAM_WRITABLE));
238 g_object_class_install_property (object_class,
240 g_param_spec_string ("foreground",
241 P_("Foreground color name"),
242 P_("Foreground color as a string"),
246 g_object_class_install_property (object_class,
248 g_param_spec_boxed ("foreground_gdk",
249 P_("Foreground color"),
250 P_("Foreground color as a GdkColor"),
252 G_PARAM_READABLE | G_PARAM_WRITABLE));
255 g_object_class_install_property (object_class,
257 g_param_spec_boolean ("editable",
259 P_("Whether the text can be modified by the user"),
261 G_PARAM_READABLE | G_PARAM_WRITABLE));
263 g_object_class_install_property (object_class,
265 g_param_spec_string ("font",
267 P_("Font description as a string"),
269 G_PARAM_READABLE | G_PARAM_WRITABLE));
271 g_object_class_install_property (object_class,
273 g_param_spec_boxed ("font_desc",
275 P_("Font description as a PangoFontDescription struct"),
276 PANGO_TYPE_FONT_DESCRIPTION,
277 G_PARAM_READABLE | G_PARAM_WRITABLE));
280 g_object_class_install_property (object_class,
282 g_param_spec_string ("family",
284 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
286 G_PARAM_READABLE | G_PARAM_WRITABLE));
288 g_object_class_install_property (object_class,
290 g_param_spec_enum ("style",
295 G_PARAM_READABLE | G_PARAM_WRITABLE));
297 g_object_class_install_property (object_class,
299 g_param_spec_enum ("variant",
303 PANGO_VARIANT_NORMAL,
304 G_PARAM_READABLE | G_PARAM_WRITABLE));
306 g_object_class_install_property (object_class,
308 g_param_spec_int ("weight",
314 G_PARAM_READABLE | G_PARAM_WRITABLE));
316 g_object_class_install_property (object_class,
318 g_param_spec_enum ("stretch",
322 PANGO_STRETCH_NORMAL,
323 G_PARAM_READABLE | G_PARAM_WRITABLE));
325 g_object_class_install_property (object_class,
327 g_param_spec_int ("size",
333 G_PARAM_READABLE | G_PARAM_WRITABLE));
335 g_object_class_install_property (object_class,
337 g_param_spec_double ("size_points",
339 P_("Font size in points"),
343 G_PARAM_READABLE | G_PARAM_WRITABLE));
345 g_object_class_install_property (object_class,
347 g_param_spec_double ("scale",
349 P_("Font scaling factor"),
353 G_PARAM_READABLE | G_PARAM_WRITABLE));
355 g_object_class_install_property (object_class,
357 g_param_spec_int ("rise",
359 P_("Offset of text above the baseline (below the baseline if rise is negative)"),
363 G_PARAM_READABLE | G_PARAM_WRITABLE));
366 g_object_class_install_property (object_class,
368 g_param_spec_boolean ("strikethrough",
370 P_("Whether to strike through the text"),
372 G_PARAM_READABLE | G_PARAM_WRITABLE));
374 g_object_class_install_property (object_class,
376 g_param_spec_enum ("underline",
378 P_("Style of underline for this text"),
379 PANGO_TYPE_UNDERLINE,
380 PANGO_UNDERLINE_NONE,
381 G_PARAM_READABLE | G_PARAM_WRITABLE));
383 g_object_class_install_property (object_class,
385 g_param_spec_string ("language",
387 P_("The language this text is in, as an ISO code. Pango can use this as a hint when rendering the text. If you don't understand this parameter, you probably don't need it"),
392 /* Style props are set or not */
394 #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))
396 ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
397 P_("Background set"),
398 P_("Whether this tag affects the background color"));
400 ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
401 P_("Foreground set"),
402 P_("Whether this tag affects the foreground color"));
404 ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
405 P_("Editability set"),
406 P_("Whether this tag affects text editability"));
408 ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
409 P_("Font family set"),
410 P_("Whether this tag affects the font family"));
412 ADD_SET_PROP ("style_set", PROP_STYLE_SET,
413 P_("Font style set"),
414 P_("Whether this tag affects the font style"));
416 ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
417 P_("Font variant set"),
418 P_("Whether this tag affects the font variant"));
420 ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
421 P_("Font weight set"),
422 P_("Whether this tag affects the font weight"));
424 ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
425 P_("Font stretch set"),
426 P_("Whether this tag affects the font stretch"));
428 ADD_SET_PROP ("size_set", PROP_SIZE_SET,
430 P_("Whether this tag affects the font size"));
432 ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
433 P_("Font scale set"),
434 P_("Whether this tag scales the font size by a factor"));
436 ADD_SET_PROP ("rise_set", PROP_RISE_SET,
438 P_("Whether this tag affects the rise"));
440 ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
441 P_("Strikethrough set"),
442 P_("Whether this tag affects strikethrough"));
444 ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
446 P_("Whether this tag affects underlining"));
448 ADD_SET_PROP ("language_set", PROP_LANGUAGE_SET,
450 P_("Whether this tag affects the language the text is rendered as"));
452 text_cell_renderer_signals [EDITED] =
453 g_signal_new ("edited",
454 G_OBJECT_CLASS_TYPE (object_class),
456 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
458 _gtk_marshal_VOID__STRING_STRING,
463 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
467 gtk_cell_renderer_text_finalize (GObject *object)
469 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
470 GtkCellRendererTextPrivate *priv;
472 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
474 pango_font_description_free (celltext->font);
477 g_free (celltext->text);
479 if (celltext->extra_attrs)
480 pango_attr_list_unref (celltext->extra_attrs);
483 g_object_unref (priv->language);
485 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
489 get_property_font_set_mask (guint prop_id)
493 case PROP_FAMILY_SET:
494 return PANGO_FONT_MASK_FAMILY;
496 return PANGO_FONT_MASK_STYLE;
497 case PROP_VARIANT_SET:
498 return PANGO_FONT_MASK_VARIANT;
499 case PROP_WEIGHT_SET:
500 return PANGO_FONT_MASK_WEIGHT;
501 case PROP_STRETCH_SET:
502 return PANGO_FONT_MASK_STRETCH;
504 return PANGO_FONT_MASK_SIZE;
511 gtk_cell_renderer_text_get_property (GObject *object,
516 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
517 GtkCellRendererTextPrivate *priv;
519 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
524 g_value_set_string (value, celltext->text);
527 case PROP_ATTRIBUTES:
528 g_value_set_boxed (value, celltext->extra_attrs);
531 case PROP_SINGLE_PARAGRAPH_MODE:
532 g_value_set_boolean (value, priv->single_paragraph);
535 case PROP_BACKGROUND_GDK:
539 color.red = celltext->background.red;
540 color.green = celltext->background.green;
541 color.blue = celltext->background.blue;
543 g_value_set_boxed (value, &color);
547 case PROP_FOREGROUND_GDK:
551 color.red = celltext->foreground.red;
552 color.green = celltext->foreground.green;
553 color.blue = celltext->foreground.blue;
555 g_value_set_boxed (value, &color);
561 /* FIXME GValue imposes a totally gratuitous string copy
562 * here, we could just hand off string ownership
564 gchar *str = pango_font_description_to_string (celltext->font);
565 g_value_set_string (value, str);
571 g_value_set_boxed (value, celltext->font);
575 g_value_set_string (value, pango_font_description_get_family (celltext->font));
579 g_value_set_enum (value, pango_font_description_get_style (celltext->font));
583 g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
587 g_value_set_int (value, pango_font_description_get_weight (celltext->font));
591 g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
595 g_value_set_int (value, pango_font_description_get_size (celltext->font));
598 case PROP_SIZE_POINTS:
599 g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
603 g_value_set_double (value, celltext->font_scale);
607 g_value_set_boolean (value, celltext->editable);
610 case PROP_STRIKETHROUGH:
611 g_value_set_boolean (value, celltext->strikethrough);
615 g_value_set_enum (value, celltext->underline_style);
619 g_value_set_int (value, celltext->rise);
623 g_value_set_string (value, pango_language_to_string (priv->language));
626 case PROP_BACKGROUND_SET:
627 g_value_set_boolean (value, celltext->background_set);
630 case PROP_FOREGROUND_SET:
631 g_value_set_boolean (value, celltext->foreground_set);
634 case PROP_FAMILY_SET:
636 case PROP_VARIANT_SET:
637 case PROP_WEIGHT_SET:
638 case PROP_STRETCH_SET:
641 PangoFontMask mask = get_property_font_set_mask (param_id);
642 g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
648 g_value_set_boolean (value, celltext->scale_set);
651 case PROP_EDITABLE_SET:
652 g_value_set_boolean (value, celltext->editable_set);
655 case PROP_STRIKETHROUGH_SET:
656 g_value_set_boolean (value, celltext->strikethrough_set);
659 case PROP_UNDERLINE_SET:
660 g_value_set_boolean (value, celltext->underline_set);
664 g_value_set_boolean (value, celltext->rise_set);
667 case PROP_LANGUAGE_SET:
668 g_value_set_boolean (value, priv->language_set);
671 case PROP_BACKGROUND:
672 case PROP_FOREGROUND:
675 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
682 set_bg_color (GtkCellRendererText *celltext,
687 if (!celltext->background_set)
689 celltext->background_set = TRUE;
690 g_object_notify (G_OBJECT (celltext), "background_set");
693 celltext->background.red = color->red;
694 celltext->background.green = color->green;
695 celltext->background.blue = color->blue;
699 if (celltext->background_set)
701 celltext->background_set = FALSE;
702 g_object_notify (G_OBJECT (celltext), "background_set");
709 set_fg_color (GtkCellRendererText *celltext,
714 if (!celltext->foreground_set)
716 celltext->foreground_set = TRUE;
717 g_object_notify (G_OBJECT (celltext), "foreground_set");
720 celltext->foreground.red = color->red;
721 celltext->foreground.green = color->green;
722 celltext->foreground.blue = color->blue;
726 if (celltext->foreground_set)
728 celltext->foreground_set = FALSE;
729 g_object_notify (G_OBJECT (celltext), "foreground_set");
735 set_font_desc_fields (PangoFontDescription *desc,
736 PangoFontMask to_set)
738 PangoFontMask changed_mask = 0;
740 if (to_set & PANGO_FONT_MASK_FAMILY)
742 const char *family = pango_font_description_get_family (desc);
746 changed_mask |= PANGO_FONT_MASK_FAMILY;
749 pango_font_description_set_family (desc, family);
751 if (to_set & PANGO_FONT_MASK_STYLE)
752 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
753 if (to_set & PANGO_FONT_MASK_VARIANT)
754 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
755 if (to_set & PANGO_FONT_MASK_WEIGHT)
756 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
757 if (to_set & PANGO_FONT_MASK_STRETCH)
758 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
759 if (to_set & PANGO_FONT_MASK_SIZE)
761 gint size = pango_font_description_get_size (desc);
764 size = 10 * PANGO_SCALE;
765 changed_mask |= PANGO_FONT_MASK_SIZE;
768 pango_font_description_set_size (desc, size);
775 notify_set_changed (GObject *object,
776 PangoFontMask changed_mask)
778 if (changed_mask & PANGO_FONT_MASK_FAMILY)
779 g_object_notify (object, "family_set");
780 if (changed_mask & PANGO_FONT_MASK_STYLE)
781 g_object_notify (object, "style_set");
782 if (changed_mask & PANGO_FONT_MASK_VARIANT)
783 g_object_notify (object, "variant_set");
784 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
785 g_object_notify (object, "weight_set");
786 if (changed_mask & PANGO_FONT_MASK_STRETCH)
787 g_object_notify (object, "stretch_set");
788 if (changed_mask & PANGO_FONT_MASK_SIZE)
789 g_object_notify (object, "size_set");
793 notify_fields_changed (GObject *object,
794 PangoFontMask changed_mask)
796 if (changed_mask & PANGO_FONT_MASK_FAMILY)
797 g_object_notify (object, "family");
798 if (changed_mask & PANGO_FONT_MASK_STYLE)
799 g_object_notify (object, "style");
800 if (changed_mask & PANGO_FONT_MASK_VARIANT)
801 g_object_notify (object, "variant");
802 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
803 g_object_notify (object, "weight");
804 if (changed_mask & PANGO_FONT_MASK_STRETCH)
805 g_object_notify (object, "stretch");
806 if (changed_mask & PANGO_FONT_MASK_SIZE)
807 g_object_notify (object, "size");
811 set_font_description (GtkCellRendererText *celltext,
812 PangoFontDescription *font_desc)
814 GObject *object = G_OBJECT (celltext);
815 PangoFontDescription *new_font_desc;
816 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
819 new_font_desc = pango_font_description_copy (font_desc);
821 new_font_desc = pango_font_description_new ();
823 old_mask = pango_font_description_get_set_fields (celltext->font);
824 new_mask = pango_font_description_get_set_fields (new_font_desc);
826 changed_mask = old_mask | new_mask;
827 set_changed_mask = old_mask ^ new_mask;
829 pango_font_description_free (celltext->font);
830 celltext->font = new_font_desc;
832 g_object_freeze_notify (object);
834 g_object_notify (object, "font_desc");
835 g_object_notify (object, "font");
837 if (changed_mask & PANGO_FONT_MASK_FAMILY)
838 g_object_notify (object, "family");
839 if (changed_mask & PANGO_FONT_MASK_STYLE)
840 g_object_notify (object, "style");
841 if (changed_mask & PANGO_FONT_MASK_VARIANT)
842 g_object_notify (object, "variant");
843 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
844 g_object_notify (object, "weight");
845 if (changed_mask & PANGO_FONT_MASK_STRETCH)
846 g_object_notify (object, "stretch");
847 if (changed_mask & PANGO_FONT_MASK_SIZE)
849 g_object_notify (object, "size");
850 g_object_notify (object, "size_points");
853 notify_set_changed (object, set_changed_mask);
855 g_object_thaw_notify (object);
859 gtk_cell_renderer_text_set_property (GObject *object,
864 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
865 GtkCellRendererTextPrivate *priv;
867 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
873 g_free (celltext->text);
875 if (priv->markup_set)
877 if (celltext->extra_attrs)
878 pango_attr_list_unref (celltext->extra_attrs);
879 celltext->extra_attrs = NULL;
880 priv->markup_set = FALSE;
883 celltext->text = g_strdup (g_value_get_string (value));
884 g_object_notify (object, "text");
887 case PROP_ATTRIBUTES:
888 if (celltext->extra_attrs)
889 pango_attr_list_unref (celltext->extra_attrs);
891 celltext->extra_attrs = g_value_get_boxed (value);
892 if (celltext->extra_attrs)
893 pango_attr_list_ref (celltext->extra_attrs);
899 GError *error = NULL;
900 PangoAttrList *attrs = NULL;
902 str = g_value_get_string (value);
903 if (str && !pango_parse_markup (str,
911 g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
913 g_error_free (error);
918 g_free (celltext->text);
920 if (celltext->extra_attrs)
921 pango_attr_list_unref (celltext->extra_attrs);
923 celltext->text = text;
924 celltext->extra_attrs = attrs;
925 priv->markup_set = TRUE;
929 case PROP_SINGLE_PARAGRAPH_MODE:
930 priv->single_paragraph = g_value_get_boolean (value);
933 case PROP_BACKGROUND:
937 if (!g_value_get_string (value))
938 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
939 else if (gdk_color_parse (g_value_get_string (value), &color))
940 set_bg_color (celltext, &color);
942 g_warning ("Don't know color `%s'", g_value_get_string (value));
944 g_object_notify (object, "background_gdk");
948 case PROP_FOREGROUND:
952 if (!g_value_get_string (value))
953 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
954 else if (gdk_color_parse (g_value_get_string (value), &color))
955 set_fg_color (celltext, &color);
957 g_warning ("Don't know color `%s'", g_value_get_string (value));
959 g_object_notify (object, "foreground_gdk");
963 case PROP_BACKGROUND_GDK:
964 /* This notifies the GObject itself. */
965 set_bg_color (celltext, g_value_get_boxed (value));
968 case PROP_FOREGROUND_GDK:
969 /* This notifies the GObject itself. */
970 set_fg_color (celltext, g_value_get_boxed (value));
975 PangoFontDescription *font_desc = NULL;
978 name = g_value_get_string (value);
981 font_desc = pango_font_description_from_string (name);
983 set_font_description (celltext, font_desc);
985 if (celltext->fixed_height_rows != -1)
986 celltext->calc_fixed_height = TRUE;
991 set_font_description (celltext, g_value_get_boxed (value));
993 if (celltext->fixed_height_rows != -1)
994 celltext->calc_fixed_height = TRUE;
1003 case PROP_SIZE_POINTS:
1005 PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
1010 pango_font_description_set_family (celltext->font,
1011 g_value_get_string (value));
1014 pango_font_description_set_style (celltext->font,
1015 g_value_get_enum (value));
1018 pango_font_description_set_variant (celltext->font,
1019 g_value_get_enum (value));
1022 pango_font_description_set_weight (celltext->font,
1023 g_value_get_int (value));
1026 pango_font_description_set_stretch (celltext->font,
1027 g_value_get_enum (value));
1030 pango_font_description_set_size (celltext->font,
1031 g_value_get_int (value));
1032 g_object_notify (object, "size_points");
1034 case PROP_SIZE_POINTS:
1035 pango_font_description_set_size (celltext->font,
1036 g_value_get_double (value) * PANGO_SCALE);
1037 g_object_notify (object, "size");
1041 if (celltext->fixed_height_rows != -1)
1042 celltext->calc_fixed_height = TRUE;
1044 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
1045 g_object_notify (object, "font_desc");
1046 g_object_notify (object, "font");
1052 celltext->font_scale = g_value_get_double (value);
1053 celltext->scale_set = TRUE;
1054 if (celltext->fixed_height_rows != -1)
1055 celltext->calc_fixed_height = TRUE;
1056 g_object_notify (object, "scale_set");
1060 celltext->editable = g_value_get_boolean (value);
1061 celltext->editable_set = TRUE;
1062 if (celltext->editable)
1063 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
1065 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_INERT;
1066 g_object_notify (object, "editable_set");
1069 case PROP_STRIKETHROUGH:
1070 celltext->strikethrough = g_value_get_boolean (value);
1071 celltext->strikethrough_set = TRUE;
1072 g_object_notify (object, "strikethrough_set");
1075 case PROP_UNDERLINE:
1076 celltext->underline_style = g_value_get_enum (value);
1077 celltext->underline_set = TRUE;
1078 g_object_notify (object, "underline_set");
1083 celltext->rise = g_value_get_int (value);
1084 celltext->rise_set = TRUE;
1085 g_object_notify (object, "rise_set");
1086 if (celltext->fixed_height_rows != -1)
1087 celltext->calc_fixed_height = TRUE;
1091 priv->language_set = TRUE;
1093 g_object_unref (priv->language);
1094 priv->language = pango_language_from_string (g_value_get_string (value));
1095 g_object_notify (object, "language_set");
1098 case PROP_BACKGROUND_SET:
1099 celltext->background_set = g_value_get_boolean (value);
1102 case PROP_FOREGROUND_SET:
1103 celltext->foreground_set = g_value_get_boolean (value);
1106 case PROP_FAMILY_SET:
1107 case PROP_STYLE_SET:
1108 case PROP_VARIANT_SET:
1109 case PROP_WEIGHT_SET:
1110 case PROP_STRETCH_SET:
1112 if (!g_value_get_boolean (value))
1114 pango_font_description_unset_fields (celltext->font,
1115 get_property_font_set_mask (param_id));
1119 PangoFontMask changed_mask;
1121 changed_mask = set_font_desc_fields (celltext->font,
1122 get_property_font_set_mask (param_id));
1123 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1127 case PROP_SCALE_SET:
1128 celltext->scale_set = g_value_get_boolean (value);
1131 case PROP_EDITABLE_SET:
1132 celltext->editable_set = g_value_get_boolean (value);
1135 case PROP_STRIKETHROUGH_SET:
1136 celltext->strikethrough_set = g_value_get_boolean (value);
1139 case PROP_UNDERLINE_SET:
1140 celltext->underline_set = g_value_get_boolean (value);
1144 celltext->rise_set = g_value_get_boolean (value);
1147 case PROP_LANGUAGE_SET:
1148 priv->language_set = g_value_get_boolean (value);
1152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1158 * gtk_cell_renderer_text_new:
1160 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1161 * object properties. Object properties can be
1162 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1163 * you can bind a property to a value in a #GtkTreeModel. For example,
1164 * you can bind the "text" property on the cell renderer to a string
1165 * value in the model, thus rendering a different string in each row
1166 * of the #GtkTreeView
1168 * Return value: the new cell renderer
1171 gtk_cell_renderer_text_new (void)
1173 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1177 add_attr (PangoAttrList *attr_list,
1178 PangoAttribute *attr)
1180 attr->start_index = 0;
1181 attr->end_index = G_MAXINT;
1183 pango_attr_list_insert (attr_list, attr);
1187 get_layout (GtkCellRendererText *celltext,
1189 gboolean will_render,
1190 GtkCellRendererState flags)
1192 PangoAttrList *attr_list;
1193 PangoLayout *layout;
1194 PangoUnderline uline;
1195 GtkCellRendererTextPrivate *priv;
1197 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
1199 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1201 if (celltext->extra_attrs)
1202 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1204 attr_list = pango_attr_list_new ();
1206 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1210 /* Add options that affect appearance but not size */
1212 /* note that background doesn't go here, since it affects
1213 * background_area not the PangoLayout area
1216 if (celltext->foreground_set)
1220 color = celltext->foreground;
1222 add_attr (attr_list,
1223 pango_attr_foreground_new (color.red, color.green, color.blue));
1226 if (celltext->strikethrough_set)
1227 add_attr (attr_list,
1228 pango_attr_strikethrough_new (celltext->strikethrough));
1231 add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1233 if (celltext->scale_set &&
1234 celltext->font_scale != 1.0)
1235 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1237 if (celltext->underline_set)
1238 uline = celltext->underline_style;
1240 uline = PANGO_UNDERLINE_NONE;
1242 if (priv->language_set)
1243 add_attr (attr_list, pango_attr_language_new (priv->language));
1245 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1249 case PANGO_UNDERLINE_NONE:
1250 uline = PANGO_UNDERLINE_SINGLE;
1253 case PANGO_UNDERLINE_SINGLE:
1254 uline = PANGO_UNDERLINE_DOUBLE;
1262 if (uline != PANGO_UNDERLINE_NONE)
1263 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1265 if (celltext->rise_set)
1266 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1268 pango_layout_set_attributes (layout, attr_list);
1269 pango_layout_set_width (layout, -1);
1271 pango_attr_list_unref (attr_list);
1277 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1279 GdkRectangle *cell_area,
1285 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1286 PangoRectangle rect;
1287 PangoLayout *layout;
1289 if (celltext->calc_fixed_height)
1291 PangoContext *context;
1292 PangoFontMetrics *metrics;
1293 PangoFontDescription *font_desc;
1296 font_desc = pango_font_description_copy (widget->style->font_desc);
1297 pango_font_description_merge (font_desc, celltext->font, TRUE);
1299 if (celltext->scale_set)
1300 pango_font_description_set_size (font_desc,
1301 celltext->font_scale * pango_font_description_get_size (font_desc));
1303 context = gtk_widget_get_pango_context (widget);
1305 metrics = pango_context_get_metrics (context,
1307 pango_context_get_language (context));
1308 row_height = (pango_font_metrics_get_ascent (metrics) +
1309 pango_font_metrics_get_descent (metrics));
1310 pango_font_metrics_unref (metrics);
1312 pango_font_description_free (font_desc);
1314 gtk_cell_renderer_set_fixed_size (cell,
1315 cell->width, 2*cell->ypad +
1316 celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1320 *height = cell->height;
1323 celltext->calc_fixed_height = FALSE;
1327 layout = get_layout (celltext, widget, FALSE, 0);
1328 pango_layout_get_pixel_extents (layout, NULL, &rect);
1331 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1334 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1340 *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
1341 (1.0 - cell->xalign) : cell->xalign) * (cell_area->width - rect.width - (2 * cell->xpad));
1342 *x_offset = MAX (*x_offset, 0);
1346 *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1347 *y_offset = MAX (*y_offset, 0);
1351 g_object_unref (layout);
1355 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1356 GdkDrawable *window,
1358 GdkRectangle *background_area,
1359 GdkRectangle *cell_area,
1360 GdkRectangle *expose_area,
1361 GtkCellRendererState flags)
1364 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1365 PangoLayout *layout;
1370 layout = get_layout (celltext, widget, TRUE, flags);
1372 gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1375 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1377 if (GTK_WIDGET_HAS_FOCUS (widget))
1378 state = GTK_STATE_SELECTED;
1380 state = GTK_STATE_ACTIVE;
1384 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
1385 state = GTK_STATE_INSENSITIVE;
1387 state = GTK_STATE_NORMAL;
1390 if (celltext->background_set && state != GTK_STATE_SELECTED)
1395 color.red = celltext->background.red;
1396 color.green = celltext->background.green;
1397 color.blue = celltext->background.blue;
1399 gc = gdk_gc_new (window);
1401 gdk_gc_set_rgb_fg_color (gc, &color);
1404 gdk_gc_set_clip_rectangle (gc, expose_area);
1405 gdk_draw_rectangle (window,
1410 background_area->width,
1411 background_area->height);
1413 gdk_gc_set_clip_rectangle (gc, NULL);
1414 g_object_unref (gc);
1417 gtk_paint_layout (widget->style,
1424 cell_area->x + x_offset + cell->xpad,
1425 cell_area->y + y_offset + cell->ypad,
1428 g_object_unref (layout);
1432 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1436 const gchar *new_text;
1437 GtkCellRendererTextPrivate *priv;
1439 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1441 if (priv->focus_out_id > 0)
1443 g_signal_handler_disconnect (entry, priv->focus_out_id);
1444 priv->focus_out_id = 0;
1447 if (GTK_ENTRY (entry)->editing_canceled)
1449 gtk_cell_renderer_editing_canceled (GTK_CELL_RENDERER (data));
1453 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1454 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1456 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1460 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1464 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (entry), data);
1466 /* entry needs focus-out-event */
1470 static GtkCellEditable *
1471 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1475 GdkRectangle *background_area,
1476 GdkRectangle *cell_area,
1477 GtkCellRendererState flags)
1480 GtkCellRendererText *celltext;
1481 GtkCellRendererTextPrivate *priv;
1483 celltext = GTK_CELL_RENDERER_TEXT (cell);
1484 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1486 /* If the cell isn't editable we return NULL. */
1487 if (celltext->editable == FALSE)
1490 entry = g_object_new (GTK_TYPE_ENTRY,
1495 gtk_entry_set_text (GTK_ENTRY (entry), celltext->text);
1496 g_object_set_data_full (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
1498 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1500 gtk_widget_show (entry);
1501 g_signal_connect (entry,
1503 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1505 priv->focus_out_id = g_signal_connect (entry, "focus_out_event",
1506 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1509 return GTK_CELL_EDITABLE (entry);
1514 * gtk_cell_renderer_text_set_fixed_height_from_font:
1515 * @renderer: A #GtkCellRendererText
1516 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1518 * Sets the height of a renderer to explicitly be determined by the "font" and
1519 * "y_pad" property set on it. Further changes in these properties do not
1520 * affect the height, so they must be accompanied by a subsequent call to this
1521 * function. Using this function is unflexible, and should really only be used
1522 * if calculating the size of a cell is too slow (ie, a massive number of cells
1523 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1524 * the height is determined by the properties again.
1527 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1528 gint number_of_rows)
1530 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1531 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1533 if (number_of_rows == -1)
1535 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1536 GTK_CELL_RENDERER (renderer)->width,
1541 renderer->fixed_height_rows = number_of_rows;
1542 renderer->calc_fixed_height = TRUE;