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;
133 celltext->font = pango_font_description_new ();
137 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
139 GObjectClass *object_class = G_OBJECT_CLASS (class);
140 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
142 parent_class = g_type_class_peek_parent (class);
144 object_class->finalize = gtk_cell_renderer_text_finalize;
146 object_class->get_property = gtk_cell_renderer_text_get_property;
147 object_class->set_property = gtk_cell_renderer_text_set_property;
149 cell_class->get_size = gtk_cell_renderer_text_get_size;
150 cell_class->render = gtk_cell_renderer_text_render;
152 g_object_class_install_property (object_class,
154 g_param_spec_string ("text",
160 g_object_class_install_property (object_class,
162 g_param_spec_string ("markup",
164 _("Marked up text to render"),
168 g_object_class_install_property (object_class,
170 g_param_spec_boxed ("attributes",
172 _("A list of style attributes to apply to the text of the renderer."),
173 PANGO_TYPE_ATTR_LIST,
176 g_object_class_install_property (object_class,
178 g_param_spec_string ("background",
179 _("Background color name"),
180 _("Background color as a string"),
184 g_object_class_install_property (object_class,
186 g_param_spec_boxed ("background_gdk",
187 _("Background color"),
188 _("Background color as a GdkColor"),
190 G_PARAM_READABLE | G_PARAM_WRITABLE));
192 g_object_class_install_property (object_class,
194 g_param_spec_string ("foreground",
195 _("Foreground color name"),
196 _("Foreground color as a string"),
200 g_object_class_install_property (object_class,
202 g_param_spec_boxed ("foreground_gdk",
203 _("Foreground color"),
204 _("Foreground color as a GdkColor"),
206 G_PARAM_READABLE | G_PARAM_WRITABLE));
209 g_object_class_install_property (object_class,
211 g_param_spec_boolean ("editable",
213 _("Whether the text can be modified by the user"),
215 G_PARAM_READABLE | G_PARAM_WRITABLE));
217 g_object_class_install_property (object_class,
219 g_param_spec_string ("font",
221 _("Font description as a string"),
223 G_PARAM_READABLE | G_PARAM_WRITABLE));
225 g_object_class_install_property (object_class,
227 g_param_spec_boxed ("font_desc",
229 _("Font description as a PangoFontDescription struct"),
230 PANGO_TYPE_FONT_DESCRIPTION,
231 G_PARAM_READABLE | G_PARAM_WRITABLE));
234 g_object_class_install_property (object_class,
236 g_param_spec_string ("family",
238 _("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
240 G_PARAM_READABLE | G_PARAM_WRITABLE));
242 g_object_class_install_property (object_class,
244 g_param_spec_enum ("style",
249 G_PARAM_READABLE | G_PARAM_WRITABLE));
251 g_object_class_install_property (object_class,
253 g_param_spec_enum ("variant",
257 PANGO_VARIANT_NORMAL,
258 G_PARAM_READABLE | G_PARAM_WRITABLE));
260 g_object_class_install_property (object_class,
262 g_param_spec_int ("weight",
268 G_PARAM_READABLE | G_PARAM_WRITABLE));
271 g_object_class_install_property (object_class,
273 g_param_spec_enum ("stretch",
277 PANGO_STRETCH_NORMAL,
278 G_PARAM_READABLE | G_PARAM_WRITABLE));
280 g_object_class_install_property (object_class,
282 g_param_spec_int ("size",
288 G_PARAM_READABLE | G_PARAM_WRITABLE));
290 g_object_class_install_property (object_class,
292 g_param_spec_double ("size_points",
294 _("Font size in points"),
298 G_PARAM_READABLE | G_PARAM_WRITABLE));
300 g_object_class_install_property (object_class,
302 g_param_spec_double ("scale",
304 _("Font scaling factor"),
308 G_PARAM_READABLE | G_PARAM_WRITABLE));
310 g_object_class_install_property (object_class,
312 g_param_spec_int ("rise",
314 _("Offset of text above the baseline (below the baseline if rise is negative)"),
318 G_PARAM_READABLE | G_PARAM_WRITABLE));
321 g_object_class_install_property (object_class,
323 g_param_spec_boolean ("strikethrough",
325 _("Whether to strike through the text"),
327 G_PARAM_READABLE | G_PARAM_WRITABLE));
329 g_object_class_install_property (object_class,
331 g_param_spec_enum ("underline",
333 _("Style of underline for this text"),
334 PANGO_TYPE_UNDERLINE,
335 PANGO_UNDERLINE_NONE,
336 G_PARAM_READABLE | G_PARAM_WRITABLE));
338 /* Style props are set or not */
340 #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))
342 ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
344 _("Whether this tag affects the background color"));
346 ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
348 _("Whether this tag affects the foreground color"));
350 ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
351 _("Editability set"),
352 _("Whether this tag affects text editability"));
354 ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
355 _("Font family set"),
356 _("Whether this tag affects the font family"));
358 ADD_SET_PROP ("style_set", PROP_STYLE_SET,
360 _("Whether this tag affects the font style"));
362 ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
363 _("Font variant set"),
364 _("Whether this tag affects the font variant"));
366 ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
367 _("Font weight set"),
368 _("Whether this tag affects the font weight"));
370 ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
371 _("Font stretch set"),
372 _("Whether this tag affects the font stretch"));
374 ADD_SET_PROP ("size_set", PROP_SIZE_SET,
376 _("Whether this tag affects the font size"));
378 ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
380 _("Whether this tag scales the font size by a factor"));
382 ADD_SET_PROP ("rise_set", PROP_RISE_SET,
384 _("Whether this tag affects the rise"));
386 ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
387 _("Strikethrough set"),
388 _("Whether this tag affects strikethrough"));
390 ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
392 _("Whether this tag affects underlining"));
396 gtk_cell_renderer_text_finalize (GObject *object)
398 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
400 pango_font_description_free (celltext->font);
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 get_property_font_set_mask (guint prop_id)
416 case PROP_FAMILY_SET:
417 return PANGO_FONT_MASK_FAMILY;
419 return PANGO_FONT_MASK_STYLE;
420 case PROP_VARIANT_SET:
421 return PANGO_FONT_MASK_VARIANT;
422 case PROP_WEIGHT_SET:
423 return PANGO_FONT_MASK_WEIGHT;
424 case PROP_STRETCH_SET:
425 return PANGO_FONT_MASK_STRETCH;
427 return PANGO_FONT_MASK_SIZE;
434 gtk_cell_renderer_text_get_property (GObject *object,
439 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
444 g_value_set_string (value, celltext->text);
447 case PROP_ATTRIBUTES:
448 g_value_set_boxed (value, celltext->extra_attrs);
451 case PROP_BACKGROUND_GDK:
455 color.red = celltext->background.red;
456 color.green = celltext->background.green;
457 color.blue = celltext->background.blue;
459 g_value_set_boxed (value, &color);
463 case PROP_FOREGROUND_GDK:
467 color.red = celltext->foreground.red;
468 color.green = celltext->foreground.green;
469 color.blue = celltext->foreground.blue;
471 g_value_set_boxed (value, &color);
477 /* FIXME GValue imposes a totally gratuitous string copy
478 * here, we could just hand off string ownership
480 gchar *str = pango_font_description_to_string (celltext->font);
481 g_value_set_string (value, str);
487 g_value_set_boxed (value, celltext->font);
491 g_value_set_string (value, pango_font_description_get_family (celltext->font));
495 g_value_set_enum (value, pango_font_description_get_style (celltext->font));
499 g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
503 g_value_set_int (value, pango_font_description_get_weight (celltext->font));
507 g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
511 g_value_set_int (value, pango_font_description_get_size (celltext->font));
514 case PROP_SIZE_POINTS:
515 g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
519 g_value_set_double (value, celltext->font_scale);
523 g_value_set_boolean (value, celltext->editable);
526 case PROP_STRIKETHROUGH:
527 g_value_set_boolean (value, celltext->strikethrough);
531 g_value_set_enum (value, celltext->underline_style);
535 g_value_set_int (value, celltext->rise);
538 case PROP_BACKGROUND_SET:
539 g_value_set_boolean (value, celltext->background_set);
542 case PROP_FOREGROUND_SET:
543 g_value_set_boolean (value, celltext->foreground_set);
546 case PROP_FAMILY_SET:
548 case PROP_VARIANT_SET:
549 case PROP_WEIGHT_SET:
550 case PROP_STRETCH_SET:
553 PangoFontMask mask = get_property_font_set_mask (param_id);
554 g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
558 g_value_set_boolean (value, celltext->scale_set);
561 case PROP_EDITABLE_SET:
562 g_value_set_boolean (value, celltext->editable_set);
565 case PROP_STRIKETHROUGH_SET:
566 g_value_set_boolean (value, celltext->strikethrough_set);
569 case PROP_UNDERLINE_SET:
570 g_value_set_boolean (value, celltext->underline_set);
574 g_value_set_boolean (value, celltext->rise_set);
577 case PROP_BACKGROUND:
578 case PROP_FOREGROUND:
581 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
588 set_bg_color (GtkCellRendererText *celltext,
593 if (!celltext->background_set)
595 celltext->background_set = TRUE;
596 g_object_notify (G_OBJECT (celltext), "background_set");
599 celltext->background.red = color->red;
600 celltext->background.green = color->green;
601 celltext->background.blue = color->blue;
605 if (celltext->background_set)
607 celltext->background_set = FALSE;
608 g_object_notify (G_OBJECT (celltext), "background_set");
615 set_fg_color (GtkCellRendererText *celltext,
620 if (!celltext->foreground_set)
622 celltext->foreground_set = TRUE;
623 g_object_notify (G_OBJECT (celltext), "foreground_set");
626 celltext->foreground.red = color->red;
627 celltext->foreground.green = color->green;
628 celltext->foreground.blue = color->blue;
632 if (celltext->foreground_set)
634 celltext->foreground_set = FALSE;
635 g_object_notify (G_OBJECT (celltext), "foreground_set");
641 notify_set_changed (GObject *object,
642 PangoFontMask changed_mask)
644 if (changed_mask & PANGO_FONT_MASK_FAMILY)
645 g_object_notify (object, "family_set");
646 if (changed_mask & PANGO_FONT_MASK_STYLE)
647 g_object_notify (object, "style_set");
648 if (changed_mask & PANGO_FONT_MASK_VARIANT)
649 g_object_notify (object, "variant_set");
650 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
651 g_object_notify (object, "weight_set");
652 if (changed_mask & PANGO_FONT_MASK_STRETCH)
653 g_object_notify (object, "stretch_set");
654 if (changed_mask & PANGO_FONT_MASK_SIZE)
655 g_object_notify (object, "size_set");
659 set_font_description (GtkCellRendererText *celltext,
660 PangoFontDescription *font_desc)
662 GObject *object = G_OBJECT (celltext);
663 PangoFontDescription *new_font_desc;
664 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
667 new_font_desc = pango_font_description_copy (font_desc);
669 new_font_desc = pango_font_description_new ();
671 old_mask = pango_font_description_get_set_fields (celltext->font);
672 new_mask = pango_font_description_get_set_fields (new_font_desc);
674 changed_mask = old_mask | new_mask;
675 set_changed_mask = old_mask ^ new_mask;
677 pango_font_description_free (celltext->font);
678 celltext->font = new_font_desc;
680 g_object_freeze_notify (object);
682 g_object_notify (object, "font_desc");
683 g_object_notify (object, "font");
685 if (changed_mask & PANGO_FONT_MASK_FAMILY)
686 g_object_notify (object, "family");
687 if (changed_mask & PANGO_FONT_MASK_STYLE)
688 g_object_notify (object, "style");
689 if (changed_mask & PANGO_FONT_MASK_VARIANT)
690 g_object_notify (object, "variant");
691 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
692 g_object_notify (object, "weight");
693 if (changed_mask & PANGO_FONT_MASK_STRETCH)
694 g_object_notify (object, "stretch");
695 if (changed_mask & PANGO_FONT_MASK_SIZE)
697 g_object_notify (object, "size");
698 g_object_notify (object, "size_points");
701 notify_set_changed (object, set_changed_mask);
703 g_object_thaw_notify (object);
707 gtk_cell_renderer_text_set_property (GObject *object,
712 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
718 g_free (celltext->text);
719 celltext->text = g_strdup (g_value_get_string (value));
720 g_object_notify (object, "text");
723 case PROP_ATTRIBUTES:
724 if (celltext->extra_attrs)
725 pango_attr_list_unref (celltext->extra_attrs);
727 celltext->extra_attrs = g_value_get_boxed (value);
728 if (celltext->extra_attrs)
729 pango_attr_list_ref (celltext->extra_attrs);
735 GError *error = NULL;
736 PangoAttrList *attrs = NULL;
739 g_free (celltext->text);
741 if (celltext->extra_attrs)
742 pango_attr_list_unref (celltext->extra_attrs);
744 str = g_value_get_string (value);
745 if (str && !pango_parse_markup (str,
753 g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
755 g_error_free (error);
759 celltext->text = text;
760 celltext->extra_attrs = attrs;
764 case PROP_BACKGROUND:
768 if (!g_value_get_string (value))
769 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
770 else if (gdk_color_parse (g_value_get_string (value), &color))
771 set_bg_color (celltext, &color);
773 g_warning ("Don't know color `%s'", g_value_get_string (value));
775 g_object_notify (object, "background_gdk");
779 case PROP_FOREGROUND:
783 if (!g_value_get_string (value))
784 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
785 else if (gdk_color_parse (g_value_get_string (value), &color))
786 set_fg_color (celltext, &color);
788 g_warning ("Don't know color `%s'", g_value_get_string (value));
790 g_object_notify (object, "foreground_gdk");
794 case PROP_BACKGROUND_GDK:
795 /* This notifies the GObject itself. */
796 set_bg_color (celltext, g_value_get_boxed (value));
799 case PROP_FOREGROUND_GDK:
800 /* This notifies the GObject itself. */
801 set_fg_color (celltext, g_value_get_boxed (value));
806 PangoFontDescription *font_desc = NULL;
809 name = g_value_get_string (value);
812 font_desc = pango_font_description_from_string (name);
814 set_font_description (celltext, font_desc);
816 if (celltext->fixed_height_rows != -1)
817 celltext->calc_fixed_height = TRUE;
822 set_font_description (celltext, g_value_get_boxed (value));
824 if (celltext->fixed_height_rows != -1)
825 celltext->calc_fixed_height = TRUE;
834 case PROP_SIZE_POINTS:
836 PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
841 pango_font_description_set_family (celltext->font,
842 g_value_get_string (value));
845 pango_font_description_set_style (celltext->font,
846 g_value_get_enum (value));
849 pango_font_description_set_variant (celltext->font,
850 g_value_get_enum (value));
853 pango_font_description_set_weight (celltext->font,
854 g_value_get_enum (value));
857 pango_font_description_set_stretch (celltext->font,
858 g_value_get_enum (value));
861 pango_font_description_set_size (celltext->font,
862 g_value_get_int (value));
863 g_object_notify (object, "size_points");
865 case PROP_SIZE_POINTS:
866 pango_font_description_set_size (celltext->font,
867 g_value_get_double (value) * PANGO_SCALE);
868 g_object_notify (object, "size");
872 if (celltext->fixed_height_rows != -1)
873 celltext->calc_fixed_height = TRUE;
875 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
876 g_object_notify (object, "font_desc");
877 g_object_notify (object, "font");
881 celltext->font_scale = g_value_get_double (value);
882 celltext->scale_set = TRUE;
883 if (celltext->fixed_height_rows != -1)
884 celltext->calc_fixed_height = TRUE;
885 g_object_notify (object, "scale_set");
889 celltext->editable = g_value_get_boolean (value);
890 celltext->editable_set = TRUE;
891 g_object_notify (object, "editable_set");
894 case PROP_STRIKETHROUGH:
895 celltext->strikethrough = g_value_get_boolean (value);
896 celltext->strikethrough_set = TRUE;
897 g_object_notify (object, "strikethrough_set");
901 celltext->underline_style = g_value_get_enum (value);
902 celltext->underline_set = TRUE;
903 g_object_notify (object, "underline_set");
908 celltext->rise = g_value_get_int (value);
909 celltext->rise_set = TRUE;
910 g_object_notify (object, "rise_set");
911 if (celltext->fixed_height_rows != -1)
912 celltext->calc_fixed_height = TRUE;
915 case PROP_BACKGROUND_SET:
916 celltext->background_set = g_value_get_boolean (value);
919 case PROP_FOREGROUND_SET:
920 celltext->foreground_set = g_value_get_boolean (value);
923 case PROP_FAMILY_SET:
925 case PROP_VARIANT_SET:
926 case PROP_WEIGHT_SET:
927 case PROP_STRETCH_SET:
929 if (!g_value_get_boolean (value))
930 pango_font_description_unset_fields (celltext->font,
931 get_property_font_set_mask (param_id));
935 celltext->scale_set = g_value_get_boolean (value);
938 case PROP_EDITABLE_SET:
939 celltext->editable_set = g_value_get_boolean (value);
942 case PROP_STRIKETHROUGH_SET:
943 celltext->strikethrough_set = g_value_get_boolean (value);
946 case PROP_UNDERLINE_SET:
947 celltext->underline_set = g_value_get_boolean (value);
951 celltext->rise_set = g_value_get_boolean (value);
955 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
961 * gtk_cell_renderer_text_new:
963 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
964 * object properties. Object properties can be
965 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
966 * you can bind a property to a value in a #GtkTreeModel. For example,
967 * you can bind the "text" property on the cell renderer to a string
968 * value in the model, thus rendering a different string in each row
969 * of the #GtkTreeView
971 * Return value: the new cell renderer
974 gtk_cell_renderer_text_new (void)
976 return GTK_CELL_RENDERER (g_object_new (gtk_cell_renderer_text_get_type (), NULL));
980 add_attr (PangoAttrList *attr_list,
981 PangoAttribute *attr)
983 attr->start_index = 0;
984 attr->end_index = G_MAXINT;
986 pango_attr_list_insert (attr_list, attr);
990 get_layout (GtkCellRendererText *celltext,
992 gboolean will_render,
993 GtkCellRendererState flags)
995 PangoAttrList *attr_list;
997 PangoUnderline uline;
999 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1001 if (celltext->extra_attrs)
1002 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1004 attr_list = pango_attr_list_new ();
1008 /* Add options that affect appearance but not size */
1010 /* note that background doesn't go here, since it affects
1011 * background_area not the PangoLayout area
1014 if (celltext->foreground_set)
1018 color = celltext->foreground;
1020 add_attr (attr_list,
1021 pango_attr_foreground_new (color.red, color.green, color.blue));
1024 if (celltext->strikethrough_set)
1025 add_attr (attr_list,
1026 pango_attr_strikethrough_new (celltext->strikethrough));
1029 add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1031 if (celltext->scale_set &&
1032 celltext->font_scale != 1.0)
1033 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1035 if (celltext->underline_set)
1036 uline = celltext->underline_style;
1038 uline = PANGO_UNDERLINE_NONE;
1040 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1044 case PANGO_UNDERLINE_NONE:
1045 uline = PANGO_UNDERLINE_SINGLE;
1048 case PANGO_UNDERLINE_SINGLE:
1049 uline = PANGO_UNDERLINE_DOUBLE;
1057 if (uline != PANGO_UNDERLINE_NONE)
1058 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1060 if (celltext->rise_set)
1061 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1063 pango_layout_set_attributes (layout, attr_list);
1064 pango_layout_set_width (layout, -1);
1066 pango_attr_list_unref (attr_list);
1072 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1074 GdkRectangle *cell_area,
1080 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1081 PangoRectangle rect;
1082 PangoLayout *layout;
1084 if (celltext->calc_fixed_height)
1086 PangoContext *context;
1087 PangoFontMetrics *metrics;
1088 PangoFontDescription *font_desc;
1091 font_desc = pango_font_description_copy (widget->style->font_desc);
1092 pango_font_description_merge (font_desc, celltext->font, TRUE);
1094 if (celltext->scale_set)
1095 pango_font_description_set_size (font_desc,
1096 celltext->font_scale * pango_font_description_get_size (font_desc));
1098 context = gtk_widget_get_pango_context (widget);
1100 metrics = pango_context_get_metrics (context,
1102 pango_context_get_language (context));
1103 row_height = (pango_font_metrics_get_ascent (metrics) +
1104 pango_font_metrics_get_descent (metrics));
1105 pango_font_metrics_unref (metrics);
1107 gtk_cell_renderer_set_fixed_size (cell,
1108 cell->width, 2*cell->ypad +
1109 celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1113 *height = cell->height;
1116 celltext->calc_fixed_height = FALSE;
1120 layout = get_layout (celltext, widget, FALSE, 0);
1121 pango_layout_get_pixel_extents (layout, NULL, &rect);
1124 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1127 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1133 *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
1134 *x_offset = MAX (*x_offset, 0) + cell->xpad;
1138 *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1139 *y_offset = MAX (*y_offset, 0) + cell->ypad;
1143 g_object_unref (G_OBJECT (layout));
1147 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1150 GdkRectangle *background_area,
1151 GdkRectangle *cell_area,
1152 GdkRectangle *expose_area,
1156 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1157 PangoLayout *layout;
1162 layout = get_layout (celltext, widget, TRUE, flags);
1164 gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1166 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1168 if (GTK_WIDGET_HAS_FOCUS (widget))
1169 state = GTK_STATE_SELECTED;
1171 state = GTK_STATE_ACTIVE;
1175 state = GTK_STATE_NORMAL;
1178 if (celltext->background_set && state != GTK_STATE_SELECTED)
1183 color.red = celltext->background.red;
1184 color.green = celltext->background.green;
1185 color.blue = celltext->background.blue;
1187 gc = gdk_gc_new (window);
1189 gdk_gc_set_rgb_fg_color (gc, &color);
1191 gdk_draw_rectangle (window,
1196 background_area->width,
1197 background_area->height);
1199 g_object_unref (G_OBJECT (gc));
1202 gtk_paint_layout (widget->style,
1209 cell_area->x + x_offset,
1210 cell_area->y + y_offset,
1213 g_object_unref (G_OBJECT (layout));
1217 * gtk_cell_renderer_text_set_fixed_height_from_font:
1218 * @renderer: A #GtkCellRendererText
1219 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1221 * Sets the height of a renderer to explicitly be determined by the "font" and
1222 * "y_pad" property set on it. Further changes in these properties do not
1223 * affect the height, so they must be accompanied by a subsequent call to this
1224 * function. Using this function is unflexible, and should really only be used
1225 * if calculating the size of a cell is too slow. If @no_rows is -1, then the
1226 * fixed height is unset, and the height is determined by the properties again.
1229 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1230 gint number_of_rows)
1232 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1233 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1235 if (number_of_rows == -1)
1237 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1238 GTK_CELL_RENDERER (renderer)->width,
1243 renderer->fixed_height_rows = number_of_rows;
1244 renderer->calc_fixed_height = TRUE;