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"
24 static void gtk_cell_renderer_text_init (GtkCellRendererText *celltext);
25 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
26 static void gtk_cell_renderer_text_finalize (GObject *object);
28 static void gtk_cell_renderer_text_get_property (GObject *object,
32 static void gtk_cell_renderer_text_set_property (GObject *object,
36 static void gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
38 GdkRectangle *cell_area,
43 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
46 GdkRectangle *background_area,
47 GdkRectangle *cell_area,
48 GdkRectangle *expose_area,
80 /* Whether-a-style-arg-is-set args */
91 PROP_STRIKETHROUGH_SET,
96 static gpointer parent_class;
99 gtk_cell_renderer_text_get_type (void)
101 static GtkType cell_text_type = 0;
105 static const GTypeInfo cell_text_info =
107 sizeof (GtkCellRendererTextClass),
108 NULL, /* base_init */
109 NULL, /* base_finalize */
110 (GClassInitFunc) gtk_cell_renderer_text_class_init,
111 NULL, /* class_finalize */
112 NULL, /* class_data */
113 sizeof (GtkCellRendererText),
115 (GInstanceInitFunc) gtk_cell_renderer_text_init,
118 cell_text_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText", &cell_text_info, 0);
121 return cell_text_type;
125 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
127 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
128 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
129 GTK_CELL_RENDERER (celltext)->xpad = 2;
130 GTK_CELL_RENDERER (celltext)->ypad = 2;
132 celltext->fixed_height_rows = -1;
136 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
138 GObjectClass *object_class = G_OBJECT_CLASS (class);
139 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
141 parent_class = g_type_class_peek_parent (class);
143 object_class->finalize = gtk_cell_renderer_text_finalize;
145 object_class->get_property = gtk_cell_renderer_text_get_property;
146 object_class->set_property = gtk_cell_renderer_text_set_property;
148 cell_class->get_size = gtk_cell_renderer_text_get_size;
149 cell_class->render = gtk_cell_renderer_text_render;
151 g_object_class_install_property (object_class,
153 g_param_spec_string ("text",
159 g_object_class_install_property (object_class,
161 g_param_spec_string ("markup",
163 _("Marked up text to render"),
167 g_object_class_install_property (gobject_class,
169 g_param_spec_boxed ("attributes",
171 _("A list of style attributes to apply to the text of the renderer."),
172 PANGO_TYPE_ATTR_LIST,
175 g_object_class_install_property (object_class,
177 g_param_spec_string ("background",
178 _("Background color name"),
179 _("Background color as a string"),
183 g_object_class_install_property (object_class,
185 g_param_spec_boxed ("background_gdk",
186 _("Background color"),
187 _("Background color as a GdkColor"),
189 G_PARAM_READABLE | G_PARAM_WRITABLE));
191 g_object_class_install_property (object_class,
193 g_param_spec_string ("foreground",
194 _("Foreground color name"),
195 _("Foreground color as a string"),
199 g_object_class_install_property (object_class,
201 g_param_spec_boxed ("foreground_gdk",
202 _("Foreground color"),
203 _("Foreground color as a GdkColor"),
205 G_PARAM_READABLE | G_PARAM_WRITABLE));
208 g_object_class_install_property (object_class,
210 g_param_spec_boolean ("editable",
212 _("Whether the text can be modified by the user"),
214 G_PARAM_READABLE | G_PARAM_WRITABLE));
216 g_object_class_install_property (object_class,
218 g_param_spec_string ("font",
220 _("Font description as a string"),
222 G_PARAM_READABLE | G_PARAM_WRITABLE));
224 g_object_class_install_property (object_class,
226 g_param_spec_boxed ("font_desc",
228 _("Font description as a PangoFontDescription struct"),
229 PANGO_TYPE_FONT_DESCRIPTION,
230 G_PARAM_READABLE | G_PARAM_WRITABLE));
233 g_object_class_install_property (object_class,
235 g_param_spec_string ("family",
237 _("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
239 G_PARAM_READABLE | G_PARAM_WRITABLE));
241 g_object_class_install_property (object_class,
243 g_param_spec_enum ("style",
248 G_PARAM_READABLE | G_PARAM_WRITABLE));
250 g_object_class_install_property (object_class,
252 g_param_spec_enum ("variant",
256 PANGO_VARIANT_NORMAL,
257 G_PARAM_READABLE | G_PARAM_WRITABLE));
259 g_object_class_install_property (object_class,
261 g_param_spec_int ("weight",
267 G_PARAM_READABLE | G_PARAM_WRITABLE));
270 g_object_class_install_property (object_class,
272 g_param_spec_enum ("stretch",
276 PANGO_STRETCH_NORMAL,
277 G_PARAM_READABLE | G_PARAM_WRITABLE));
279 g_object_class_install_property (object_class,
281 g_param_spec_int ("size",
287 G_PARAM_READABLE | G_PARAM_WRITABLE));
289 g_object_class_install_property (object_class,
291 g_param_spec_double ("size_points",
293 _("Font size in points"),
297 G_PARAM_READABLE | G_PARAM_WRITABLE));
299 g_object_class_install_property (object_class,
301 g_param_spec_double ("scale",
303 _("Font scaling factor"),
307 G_PARAM_READABLE | G_PARAM_WRITABLE));
309 g_object_class_install_property (object_class,
311 g_param_spec_int ("rise",
313 _("Offset of text above the baseline (below the baseline if rise is negative)"),
317 G_PARAM_READABLE | G_PARAM_WRITABLE));
320 g_object_class_install_property (object_class,
322 g_param_spec_boolean ("strikethrough",
324 _("Whether to strike through the text"),
326 G_PARAM_READABLE | G_PARAM_WRITABLE));
328 g_object_class_install_property (object_class,
330 g_param_spec_enum ("underline",
332 _("Style of underline for this text"),
333 PANGO_TYPE_UNDERLINE,
334 PANGO_UNDERLINE_NONE,
335 G_PARAM_READABLE | G_PARAM_WRITABLE));
337 /* Style props are set or not */
339 #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))
341 ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
343 _("Whether this tag affects the background color"));
345 ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
347 _("Whether this tag affects the foreground color"));
349 ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
350 _("Editability set"),
351 _("Whether this tag affects text editability"));
353 ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
354 _("Font family set"),
355 _("Whether this tag affects the font family"));
357 ADD_SET_PROP ("style_set", PROP_STYLE_SET,
359 _("Whether this tag affects the font style"));
361 ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
362 _("Font variant set"),
363 _("Whether this tag affects the font variant"));
365 ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
366 _("Font weight set"),
367 _("Whether this tag affects the font weight"));
369 ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
370 _("Font stretch set"),
371 _("Whether this tag affects the font stretch"));
373 ADD_SET_PROP ("size_set", PROP_SIZE_SET,
375 _("Whether this tag affects the font size"));
377 ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
379 _("Whether this tag scales the font size by a factor"));
381 ADD_SET_PROP ("rise_set", PROP_RISE_SET,
383 _("Whether this tag affects the rise"));
385 ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
386 _("Strikethrough set"),
387 _("Whether this tag affects strikethrough"));
389 ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
391 _("Whether this tag affects underlining"));
395 gtk_cell_renderer_text_finalize (GObject *object)
397 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
399 if (celltext->font.family_name)
400 g_free (celltext->font.family_name);
403 g_free (celltext->text);
405 if (celltext->extra_attrs)
406 pango_attr_list_unref (celltext->extra_attrs);
408 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
412 gtk_cell_renderer_text_get_property (GObject *object,
417 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
422 g_value_set_string (value, celltext->text);
425 case PROP_ATTRIBUTES:
426 g_value_set_boxed (value, celltext->extra_attrs);
429 case PROP_BACKGROUND_GDK:
433 color.red = celltext->background.red;
434 color.green = celltext->background.green;
435 color.blue = celltext->background.blue;
437 g_value_set_boxed (value, &color);
441 case PROP_FOREGROUND_GDK:
445 color.red = celltext->foreground.red;
446 color.green = celltext->foreground.green;
447 color.blue = celltext->foreground.blue;
449 g_value_set_boxed (value, &color);
455 /* FIXME GValue imposes a totally gratuitous string copy
456 * here, we could just hand off string ownership
458 gchar *str = pango_font_description_to_string (&celltext->font);
459 g_value_set_string (value, str);
465 g_value_set_boxed (value, &celltext->font);
469 g_value_set_string (value, celltext->font.family_name);
473 g_value_set_enum (value, celltext->font.style);
477 g_value_set_enum (value, celltext->font.variant);
481 g_value_set_int (value, celltext->font.weight);
485 g_value_set_enum (value, celltext->font.stretch);
489 g_value_set_int (value, celltext->font.size);
492 case PROP_SIZE_POINTS:
493 g_value_set_double (value, ((double)celltext->font.size) / (double)PANGO_SCALE);
497 g_value_set_double (value, celltext->font_scale);
501 g_value_set_boolean (value, celltext->editable);
504 case PROP_STRIKETHROUGH:
505 g_value_set_boolean (value, celltext->strikethrough);
509 g_value_set_enum (value, celltext->underline_style);
513 g_value_set_int (value, celltext->rise);
516 case PROP_BACKGROUND_SET:
517 g_value_set_boolean (value, celltext->background_set);
520 case PROP_FOREGROUND_SET:
521 g_value_set_boolean (value, celltext->foreground_set);
524 case PROP_FAMILY_SET:
525 g_value_set_boolean (value, celltext->family_set);
529 g_value_set_boolean (value, celltext->style_set);
532 case PROP_VARIANT_SET:
533 g_value_set_boolean (value, celltext->variant_set);
536 case PROP_WEIGHT_SET:
537 g_value_set_boolean (value, celltext->weight_set);
540 case PROP_STRETCH_SET:
541 g_value_set_boolean (value, celltext->stretch_set);
545 g_value_set_boolean (value, celltext->size_set);
549 g_value_set_boolean (value, celltext->scale_set);
552 case PROP_EDITABLE_SET:
553 g_value_set_boolean (value, celltext->editable_set);
556 case PROP_STRIKETHROUGH_SET:
557 g_value_set_boolean (value, celltext->strikethrough_set);
560 case PROP_UNDERLINE_SET:
561 g_value_set_boolean (value, celltext->underline_set);
565 g_value_set_boolean (value, celltext->rise_set);
568 case PROP_BACKGROUND:
569 case PROP_FOREGROUND:
572 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
579 set_bg_color (GtkCellRendererText *celltext,
584 if (!celltext->background_set)
586 celltext->background_set = TRUE;
587 g_object_notify (G_OBJECT (celltext), "background_set");
590 celltext->background.red = color->red;
591 celltext->background.green = color->green;
592 celltext->background.blue = color->blue;
596 if (celltext->background_set)
598 celltext->background_set = FALSE;
599 g_object_notify (G_OBJECT (celltext), "background_set");
606 set_fg_color (GtkCellRendererText *celltext,
611 if (!celltext->foreground_set)
613 celltext->foreground_set = TRUE;
614 g_object_notify (G_OBJECT (celltext), "foreground_set");
617 celltext->foreground.red = color->red;
618 celltext->foreground.green = color->green;
619 celltext->foreground.blue = color->blue;
623 if (celltext->foreground_set)
625 celltext->foreground_set = FALSE;
626 g_object_notify (G_OBJECT (celltext), "foreground_set");
632 set_font_description (GtkCellRendererText *celltext,
633 PangoFontDescription *font_desc)
635 if (font_desc != NULL)
637 /* pango_font_description_from_string() will sometimes return
638 * a NULL family or -1 size, so handle those cases.
641 if (font_desc->family_name)
642 g_object_set (G_OBJECT (celltext),
643 "family", font_desc->family_name,
646 if (font_desc->size >= 0)
647 g_object_set (G_OBJECT (celltext),
648 "size", font_desc->size,
651 g_object_set (G_OBJECT (celltext),
652 "style", font_desc->style,
653 "variant", font_desc->variant,
654 "weight", font_desc->weight,
655 "stretch", font_desc->stretch,
660 if (celltext->family_set)
662 celltext->family_set = FALSE;
663 g_object_notify (G_OBJECT (celltext), "family_set");
665 if (celltext->style_set)
667 celltext->style_set = FALSE;
668 g_object_notify (G_OBJECT (celltext), "style_set");
670 if (celltext->variant_set)
672 celltext->variant_set = FALSE;
673 g_object_notify (G_OBJECT (celltext), "variant_set");
675 if (celltext->weight_set)
677 celltext->weight_set = FALSE;
678 g_object_notify (G_OBJECT (celltext), "weight_set");
680 if (celltext->stretch_set)
682 celltext->stretch_set = FALSE;
683 g_object_notify (G_OBJECT (celltext), "stretch_set");
685 if (celltext->size_set)
687 celltext->size_set = FALSE;
688 g_object_notify (G_OBJECT (celltext), "size_set");
694 gtk_cell_renderer_text_set_property (GObject *object,
699 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
705 g_free (celltext->text);
706 celltext->text = g_strdup (g_value_get_string (value));
707 g_object_notify(G_OBJECT(object), "text");
710 case PROP_ATTRIBUTES:
711 if (celltext->extra_attrs)
712 pango_attr_list_unref (celltext->extra_attrs);
714 celltext->extra_attrs = g_value_get_boxed (value);
715 pango_attr_list_ref (celltext->extra_attrs);
721 GError *error = NULL;
722 PangoAttrList *attrs = NULL;
725 g_free (celltext->text);
727 if (celltext->extra_attrs)
728 pango_attr_list_unref (celltext->extra_attrs);
730 str = g_value_get_string (value);
731 if (str && !pango_parse_markup (str,
739 g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
741 g_error_free (error);
745 celltext->text = text;
746 celltext->extra_attrs = attrs;
750 case PROP_BACKGROUND:
754 if (!g_value_get_string (value))
755 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
756 else if (gdk_color_parse (g_value_get_string (value), &color))
757 set_bg_color (celltext, &color);
759 g_warning ("Don't know color `%s'", g_value_get_string (value));
761 g_object_notify (G_OBJECT (celltext), "background_gdk");
765 case PROP_FOREGROUND:
769 if (!g_value_get_string (value))
770 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
771 else if (gdk_color_parse (g_value_get_string (value), &color))
772 set_fg_color (celltext, &color);
774 g_warning ("Don't know color `%s'", g_value_get_string (value));
776 g_object_notify (G_OBJECT (celltext), "foreground_gdk");
780 case PROP_BACKGROUND_GDK:
781 /* This notifies the GObject itself. */
782 set_bg_color (celltext, g_value_get_boxed (value));
785 case PROP_FOREGROUND_GDK:
786 /* This notifies the GObject itself. */
787 set_fg_color (celltext, g_value_get_boxed (value));
792 PangoFontDescription *font_desc = NULL;
795 name = g_value_get_string (value);
798 font_desc = pango_font_description_from_string (name);
800 /* This function notifies the relevant GObjects itself.
801 * I'm not sure how useful the notify() on 'font'
802 * actually is here, but it's here for consistency.
804 set_font_description (celltext, font_desc);
805 g_object_notify(G_OBJECT(object), "font_desc");
806 g_object_notify(G_OBJECT(object), "font");
809 pango_font_description_free (font_desc);
810 if (celltext->fixed_height_rows != -1)
811 celltext->calc_fixed_height = TRUE;
816 set_font_description (celltext, g_value_get_boxed (value));
817 if (celltext->fixed_height_rows != -1)
818 celltext->calc_fixed_height = TRUE;
822 if (celltext->font.family_name)
823 g_free (celltext->font.family_name);
824 celltext->font.family_name = g_strdup (g_value_get_string (value));
826 celltext->family_set = TRUE;
827 g_object_notify (G_OBJECT (celltext), "family_set");
828 g_object_notify (G_OBJECT (celltext), "font_desc");
829 g_object_notify (G_OBJECT (celltext), "font");
830 if (celltext->fixed_height_rows != -1)
831 celltext->calc_fixed_height = TRUE;
835 celltext->font.style = g_value_get_enum (value);
837 celltext->style_set = TRUE;
838 g_object_notify (G_OBJECT (celltext), "style_set");
839 g_object_notify (G_OBJECT (celltext), "font_desc");
840 g_object_notify (G_OBJECT (celltext), "font");
841 if (celltext->fixed_height_rows != -1)
842 celltext->calc_fixed_height = TRUE;
846 celltext->font.variant = g_value_get_enum (value);
848 celltext->variant_set = TRUE;
849 g_object_notify (G_OBJECT (celltext), "variant_set");
850 g_object_notify (G_OBJECT (celltext), "font_desc");
851 g_object_notify (G_OBJECT (celltext), "font");
852 if (celltext->fixed_height_rows != -1)
853 celltext->calc_fixed_height = TRUE;
857 celltext->font.weight = g_value_get_int (value);
859 celltext->weight_set = TRUE;
860 g_object_notify (G_OBJECT (celltext), "weight_set");
861 g_object_notify (G_OBJECT (celltext), "font_desc");
862 g_object_notify (G_OBJECT (celltext), "font");
863 if (celltext->fixed_height_rows != -1)
864 celltext->calc_fixed_height = TRUE;
868 celltext->font.stretch = g_value_get_enum (value);
870 celltext->stretch_set = TRUE;
871 g_object_notify (G_OBJECT (celltext), "stretch_set");
872 g_object_notify (G_OBJECT (celltext), "font_desc");
873 g_object_notify (G_OBJECT (celltext), "font");
874 if (celltext->fixed_height_rows != -1)
875 celltext->calc_fixed_height = TRUE;
879 celltext->font.size = g_value_get_int (value);
881 celltext->size_set = TRUE;
882 g_object_notify (G_OBJECT (celltext), "size_set");
883 g_object_notify (G_OBJECT (celltext), "font_desc");
884 g_object_notify (G_OBJECT (celltext), "font");
885 if (celltext->fixed_height_rows != -1)
886 celltext->calc_fixed_height = TRUE;
890 celltext->font_scale = g_value_get_double (value);
891 celltext->scale_set = TRUE;
892 if (celltext->fixed_height_rows != -1)
893 celltext->calc_fixed_height = TRUE;
896 case PROP_SIZE_POINTS:
897 celltext->font.size = g_value_get_double (value) * PANGO_SCALE;
899 celltext->size_set = TRUE;
900 g_object_notify (G_OBJECT (celltext), "size_set");
901 g_object_notify (G_OBJECT (celltext), "font_desc");
902 g_object_notify (G_OBJECT (celltext), "font");
903 if (celltext->fixed_height_rows != -1)
904 celltext->calc_fixed_height = TRUE;
908 celltext->editable = g_value_get_boolean (value);
909 celltext->editable_set = TRUE;
910 g_object_notify (G_OBJECT (celltext), "editable_set");
913 case PROP_STRIKETHROUGH:
914 celltext->strikethrough = g_value_get_boolean (value);
915 celltext->strikethrough_set = TRUE;
916 g_object_notify (G_OBJECT (celltext), "strikethrough_set");
920 celltext->underline_style = g_value_get_enum (value);
921 celltext->underline_set = TRUE;
922 g_object_notify (G_OBJECT (celltext), "underline_set");
927 celltext->rise = g_value_get_int (value);
928 celltext->rise_set = TRUE;
929 g_object_notify (G_OBJECT (celltext), "rise_set");
930 if (celltext->fixed_height_rows != -1)
931 celltext->calc_fixed_height = TRUE;
934 case PROP_BACKGROUND_SET:
935 celltext->background_set = g_value_get_boolean (value);
938 case PROP_FOREGROUND_SET:
939 celltext->foreground_set = g_value_get_boolean (value);
942 case PROP_FAMILY_SET:
943 celltext->family_set = g_value_get_boolean (value);
947 celltext->style_set = g_value_get_boolean (value);
950 case PROP_VARIANT_SET:
951 celltext->variant_set = g_value_get_boolean (value);
954 case PROP_WEIGHT_SET:
955 celltext->weight_set = g_value_get_boolean (value);
958 case PROP_STRETCH_SET:
959 celltext->stretch_set = g_value_get_boolean (value);
963 celltext->size_set = g_value_get_boolean (value);
967 celltext->scale_set = g_value_get_boolean (value);
970 case PROP_EDITABLE_SET:
971 celltext->editable_set = g_value_get_boolean (value);
974 case PROP_STRIKETHROUGH_SET:
975 celltext->strikethrough_set = g_value_get_boolean (value);
978 case PROP_UNDERLINE_SET:
979 celltext->underline_set = g_value_get_boolean (value);
983 celltext->rise_set = g_value_get_boolean (value);
987 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
993 * gtk_cell_renderer_text_new:
995 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
996 * object properties. Object properties can be
997 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
998 * you can bind a property to a value in a #GtkTreeModel. For example,
999 * you can bind the "text" property on the cell renderer to a string
1000 * value in the model, thus rendering a different string in each row
1001 * of the #GtkTreeView
1003 * Return value: the new cell renderer
1006 gtk_cell_renderer_text_new (void)
1008 return GTK_CELL_RENDERER (g_object_new (gtk_cell_renderer_text_get_type (), NULL));
1012 add_attr (PangoAttrList *attr_list,
1013 PangoAttribute *attr)
1015 attr->start_index = 0;
1016 attr->end_index = G_MAXINT;
1018 pango_attr_list_insert (attr_list, attr);
1022 get_layout (GtkCellRendererText *celltext,
1024 gboolean will_render,
1025 GtkCellRendererState flags)
1027 PangoAttrList *attr_list;
1028 PangoLayout *layout;
1029 PangoUnderline uline;
1031 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1033 if (celltext->extra_attrs)
1034 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1036 attr_list = pango_attr_list_new ();
1040 /* Add options that affect appearance but not size */
1042 /* note that background doesn't go here, since it affects
1043 * background_area not the PangoLayout area
1046 if (celltext->foreground_set)
1050 color = celltext->foreground;
1052 add_attr (attr_list,
1053 pango_attr_foreground_new (color.red, color.green, color.blue));
1056 if (celltext->strikethrough_set)
1057 add_attr (attr_list,
1058 pango_attr_strikethrough_new (celltext->strikethrough));
1061 if (celltext->family_set &&
1062 celltext->font.family_name)
1063 add_attr (attr_list, pango_attr_family_new (celltext->font.family_name));
1065 if (celltext->style_set)
1066 add_attr (attr_list, pango_attr_style_new (celltext->font.style));
1068 if (celltext->variant_set)
1069 add_attr (attr_list, pango_attr_variant_new (celltext->font.variant));
1071 if (celltext->weight_set)
1072 add_attr (attr_list, pango_attr_weight_new (celltext->font.weight));
1074 if (celltext->stretch_set)
1075 add_attr (attr_list, pango_attr_stretch_new (celltext->font.stretch));
1077 if (celltext->size_set &&
1078 celltext->font.size >= 0)
1079 add_attr (attr_list, pango_attr_size_new (celltext->font.size));
1081 if (celltext->scale_set &&
1082 celltext->font_scale != 1.0)
1083 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1085 if (celltext->underline_set)
1086 uline = celltext->underline_style;
1088 uline = PANGO_UNDERLINE_NONE;
1090 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1094 case PANGO_UNDERLINE_NONE:
1095 uline = PANGO_UNDERLINE_SINGLE;
1098 case PANGO_UNDERLINE_SINGLE:
1099 uline = PANGO_UNDERLINE_DOUBLE;
1107 if (uline != PANGO_UNDERLINE_NONE)
1108 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1110 if (celltext->rise_set)
1111 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1113 pango_layout_set_attributes (layout, attr_list);
1114 pango_layout_set_width (layout, -1);
1116 pango_attr_list_unref (attr_list);
1122 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1124 GdkRectangle *cell_area,
1130 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1131 PangoRectangle rect;
1132 PangoLayout *layout;
1134 if (celltext->calc_fixed_height)
1136 PangoContext *context;
1137 PangoFontMetrics metrics;
1138 PangoFontDescription font_desc;
1140 font_desc = (* widget->style->font_desc);
1142 if (celltext->family_set &&
1143 celltext->font.family_name)
1144 font_desc.family_name = celltext->font.family_name;
1145 if (celltext->style_set)
1146 font_desc.style = celltext->font.style;
1148 if (celltext->variant_set)
1149 font_desc.variant = celltext->font.variant;
1151 if (celltext->weight_set)
1152 font_desc.weight = celltext->font.weight;
1154 if (celltext->stretch_set)
1155 font_desc.stretch = celltext->font.stretch;
1157 if (celltext->size_set &&
1158 celltext->font.size >= 0)
1159 font_desc.size = celltext->font.size;
1161 context = gtk_widget_get_pango_context (widget);
1162 pango_context_get_metrics (context,
1164 pango_context_get_language (context),
1166 gtk_cell_renderer_set_fixed_size (cell,
1167 cell->width, 2*cell->ypad +
1168 celltext->fixed_height_rows*(metrics.ascent + metrics.descent)/PANGO_SCALE);
1171 *height = cell->height;
1174 celltext->calc_fixed_height = FALSE;
1178 layout = get_layout (celltext, widget, FALSE, 0);
1179 pango_layout_get_pixel_extents (layout, NULL, &rect);
1182 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1185 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1191 *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
1192 *x_offset = MAX (*x_offset, 0) + cell->xpad;
1196 *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1197 *y_offset = MAX (*y_offset, 0) + cell->ypad;
1201 g_object_unref (G_OBJECT (layout));
1205 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1208 GdkRectangle *background_area,
1209 GdkRectangle *cell_area,
1210 GdkRectangle *expose_area,
1214 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1215 PangoLayout *layout;
1220 layout = get_layout (celltext, widget, TRUE, flags);
1222 gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1224 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1225 state = GTK_STATE_SELECTED;
1227 state = GTK_STATE_NORMAL;
1229 if (celltext->background_set && state != GTK_STATE_SELECTED)
1234 color.red = celltext->background.red;
1235 color.green = celltext->background.green;
1236 color.blue = celltext->background.blue;
1238 gc = gdk_gc_new (window);
1240 gdk_gc_set_rgb_fg_color (gc, &color);
1242 gdk_draw_rectangle (window,
1247 background_area->width,
1248 background_area->height);
1250 g_object_unref (G_OBJECT (gc));
1253 gtk_paint_layout (widget->style,
1260 cell_area->x + x_offset,
1261 cell_area->y + y_offset,
1264 g_object_unref (G_OBJECT (layout));
1268 * gtk_cell_renderer_text_set_fixed_height_from_font:
1269 * @renderer: A #GtkCellRendererText
1270 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1272 * Sets the height of a renderer to explicitly be determined by the "font" and
1273 * "y_pad" property set on it. Further changes in these properties do not
1274 * affect the height, so they must be accompanied by a subsequent call to this
1275 * function. Using this function is unflexible, and should really only be used
1276 * if calculating the size of a cell is too slow. If @no_rows is -1, then the
1277 * fixed height is unset, and the height is determined by the properties again.
1280 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1281 gint number_of_rows)
1283 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1284 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1286 if (number_of_rows == -1)
1288 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1289 GTK_CELL_RENDERER (renderer)->width,
1294 renderer->fixed_height_rows = number_of_rows;
1295 renderer->calc_fixed_height = TRUE;