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 "gtksignal.h"
27 static void gtk_cell_renderer_text_init (GtkCellRendererText *celltext);
28 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
29 static void gtk_cell_renderer_text_finalize (GObject *object);
31 static void gtk_cell_renderer_text_get_property (GObject *object,
35 static void gtk_cell_renderer_text_set_property (GObject *object,
39 static void gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
41 GdkRectangle *cell_area,
46 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
49 GdkRectangle *background_area,
50 GdkRectangle *cell_area,
51 GdkRectangle *expose_area,
54 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
58 GdkRectangle *background_area,
59 GdkRectangle *cell_area,
60 GtkCellRendererState flags);
94 /* Whether-a-style-arg-is-set args */
105 PROP_STRIKETHROUGH_SET,
110 static gpointer parent_class;
111 static guint text_cell_renderer_signals [LAST_SIGNAL];
113 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
116 gtk_cell_renderer_text_get_type (void)
118 static GtkType cell_text_type = 0;
122 static const GTypeInfo cell_text_info =
124 sizeof (GtkCellRendererTextClass),
125 NULL, /* base_init */
126 NULL, /* base_finalize */
127 (GClassInitFunc) gtk_cell_renderer_text_class_init,
128 NULL, /* class_finalize */
129 NULL, /* class_data */
130 sizeof (GtkCellRendererText),
132 (GInstanceInitFunc) gtk_cell_renderer_text_init,
135 cell_text_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText", &cell_text_info, 0);
138 return cell_text_type;
142 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
144 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
145 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
146 GTK_CELL_RENDERER (celltext)->xpad = 2;
147 GTK_CELL_RENDERER (celltext)->ypad = 2;
148 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
149 celltext->fixed_height_rows = -1;
150 celltext->font = pango_font_description_new ();
154 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
156 GObjectClass *object_class = G_OBJECT_CLASS (class);
157 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
159 parent_class = g_type_class_peek_parent (class);
161 object_class->finalize = gtk_cell_renderer_text_finalize;
163 object_class->get_property = gtk_cell_renderer_text_get_property;
164 object_class->set_property = gtk_cell_renderer_text_set_property;
166 cell_class->get_size = gtk_cell_renderer_text_get_size;
167 cell_class->render = gtk_cell_renderer_text_render;
168 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
170 g_object_class_install_property (object_class,
172 g_param_spec_string ("text",
178 g_object_class_install_property (object_class,
180 g_param_spec_string ("markup",
182 _("Marked up text to render"),
186 g_object_class_install_property (object_class,
188 g_param_spec_boxed ("attributes",
190 _("A list of style attributes to apply to the text of the renderer."),
191 PANGO_TYPE_ATTR_LIST,
194 g_object_class_install_property (object_class,
196 g_param_spec_string ("background",
197 _("Background color name"),
198 _("Background color as a string"),
202 g_object_class_install_property (object_class,
204 g_param_spec_boxed ("background_gdk",
205 _("Background color"),
206 _("Background color as a GdkColor"),
208 G_PARAM_READABLE | G_PARAM_WRITABLE));
210 g_object_class_install_property (object_class,
212 g_param_spec_string ("foreground",
213 _("Foreground color name"),
214 _("Foreground color as a string"),
218 g_object_class_install_property (object_class,
220 g_param_spec_boxed ("foreground_gdk",
221 _("Foreground color"),
222 _("Foreground color as a GdkColor"),
224 G_PARAM_READABLE | G_PARAM_WRITABLE));
227 g_object_class_install_property (object_class,
229 g_param_spec_boolean ("editable",
231 _("Whether the text can be modified by the user"),
233 G_PARAM_READABLE | G_PARAM_WRITABLE));
235 g_object_class_install_property (object_class,
237 g_param_spec_string ("font",
239 _("Font description as a string"),
241 G_PARAM_READABLE | G_PARAM_WRITABLE));
243 g_object_class_install_property (object_class,
245 g_param_spec_boxed ("font_desc",
247 _("Font description as a PangoFontDescription struct"),
248 PANGO_TYPE_FONT_DESCRIPTION,
249 G_PARAM_READABLE | G_PARAM_WRITABLE));
252 g_object_class_install_property (object_class,
254 g_param_spec_string ("family",
256 _("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
258 G_PARAM_READABLE | G_PARAM_WRITABLE));
260 g_object_class_install_property (object_class,
262 g_param_spec_enum ("style",
267 G_PARAM_READABLE | G_PARAM_WRITABLE));
269 g_object_class_install_property (object_class,
271 g_param_spec_enum ("variant",
275 PANGO_VARIANT_NORMAL,
276 G_PARAM_READABLE | G_PARAM_WRITABLE));
278 g_object_class_install_property (object_class,
280 g_param_spec_int ("weight",
286 G_PARAM_READABLE | G_PARAM_WRITABLE));
289 g_object_class_install_property (object_class,
291 g_param_spec_enum ("stretch",
295 PANGO_STRETCH_NORMAL,
296 G_PARAM_READABLE | G_PARAM_WRITABLE));
298 g_object_class_install_property (object_class,
300 g_param_spec_int ("size",
306 G_PARAM_READABLE | G_PARAM_WRITABLE));
308 g_object_class_install_property (object_class,
310 g_param_spec_double ("size_points",
312 _("Font size in points"),
316 G_PARAM_READABLE | G_PARAM_WRITABLE));
318 g_object_class_install_property (object_class,
320 g_param_spec_double ("scale",
322 _("Font scaling factor"),
326 G_PARAM_READABLE | G_PARAM_WRITABLE));
328 g_object_class_install_property (object_class,
330 g_param_spec_int ("rise",
332 _("Offset of text above the baseline (below the baseline if rise is negative)"),
336 G_PARAM_READABLE | G_PARAM_WRITABLE));
339 g_object_class_install_property (object_class,
341 g_param_spec_boolean ("strikethrough",
343 _("Whether to strike through the text"),
345 G_PARAM_READABLE | G_PARAM_WRITABLE));
347 g_object_class_install_property (object_class,
349 g_param_spec_enum ("underline",
351 _("Style of underline for this text"),
352 PANGO_TYPE_UNDERLINE,
353 PANGO_UNDERLINE_NONE,
354 G_PARAM_READABLE | G_PARAM_WRITABLE));
356 /* Style props are set or not */
358 #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))
360 ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
362 _("Whether this tag affects the background color"));
364 ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
366 _("Whether this tag affects the foreground color"));
368 ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
369 _("Editability set"),
370 _("Whether this tag affects text editability"));
372 ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
373 _("Font family set"),
374 _("Whether this tag affects the font family"));
376 ADD_SET_PROP ("style_set", PROP_STYLE_SET,
378 _("Whether this tag affects the font style"));
380 ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
381 _("Font variant set"),
382 _("Whether this tag affects the font variant"));
384 ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
385 _("Font weight set"),
386 _("Whether this tag affects the font weight"));
388 ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
389 _("Font stretch set"),
390 _("Whether this tag affects the font stretch"));
392 ADD_SET_PROP ("size_set", PROP_SIZE_SET,
394 _("Whether this tag affects the font size"));
396 ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
398 _("Whether this tag scales the font size by a factor"));
400 ADD_SET_PROP ("rise_set", PROP_RISE_SET,
402 _("Whether this tag affects the rise"));
404 ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
405 _("Strikethrough set"),
406 _("Whether this tag affects strikethrough"));
408 ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
410 _("Whether this tag affects underlining"));
412 text_cell_renderer_signals [EDITED] =
413 gtk_signal_new ("edited",
415 GTK_CLASS_TYPE (object_class),
416 GTK_SIGNAL_OFFSET (GtkCellRendererTextClass, edited),
417 gtk_marshal_VOID__STRING_STRING,
425 gtk_cell_renderer_text_finalize (GObject *object)
427 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
429 pango_font_description_free (celltext->font);
432 g_free (celltext->text);
434 if (celltext->extra_attrs)
435 pango_attr_list_unref (celltext->extra_attrs);
437 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
441 get_property_font_set_mask (guint prop_id)
445 case PROP_FAMILY_SET:
446 return PANGO_FONT_MASK_FAMILY;
448 return PANGO_FONT_MASK_STYLE;
449 case PROP_VARIANT_SET:
450 return PANGO_FONT_MASK_VARIANT;
451 case PROP_WEIGHT_SET:
452 return PANGO_FONT_MASK_WEIGHT;
453 case PROP_STRETCH_SET:
454 return PANGO_FONT_MASK_STRETCH;
456 return PANGO_FONT_MASK_SIZE;
463 gtk_cell_renderer_text_get_property (GObject *object,
468 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
473 g_value_set_string (value, celltext->text);
476 case PROP_ATTRIBUTES:
477 g_value_set_boxed (value, celltext->extra_attrs);
480 case PROP_BACKGROUND_GDK:
484 color.red = celltext->background.red;
485 color.green = celltext->background.green;
486 color.blue = celltext->background.blue;
488 g_value_set_boxed (value, &color);
492 case PROP_FOREGROUND_GDK:
496 color.red = celltext->foreground.red;
497 color.green = celltext->foreground.green;
498 color.blue = celltext->foreground.blue;
500 g_value_set_boxed (value, &color);
506 /* FIXME GValue imposes a totally gratuitous string copy
507 * here, we could just hand off string ownership
509 gchar *str = pango_font_description_to_string (celltext->font);
510 g_value_set_string (value, str);
516 g_value_set_boxed (value, celltext->font);
520 g_value_set_string (value, pango_font_description_get_family (celltext->font));
524 g_value_set_enum (value, pango_font_description_get_style (celltext->font));
528 g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
532 g_value_set_int (value, pango_font_description_get_weight (celltext->font));
536 g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
540 g_value_set_int (value, pango_font_description_get_size (celltext->font));
543 case PROP_SIZE_POINTS:
544 g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
548 g_value_set_double (value, celltext->font_scale);
552 g_value_set_boolean (value, celltext->editable);
555 case PROP_STRIKETHROUGH:
556 g_value_set_boolean (value, celltext->strikethrough);
560 g_value_set_enum (value, celltext->underline_style);
564 g_value_set_int (value, celltext->rise);
567 case PROP_BACKGROUND_SET:
568 g_value_set_boolean (value, celltext->background_set);
571 case PROP_FOREGROUND_SET:
572 g_value_set_boolean (value, celltext->foreground_set);
575 case PROP_FAMILY_SET:
577 case PROP_VARIANT_SET:
578 case PROP_WEIGHT_SET:
579 case PROP_STRETCH_SET:
582 PangoFontMask mask = get_property_font_set_mask (param_id);
583 g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
589 g_value_set_boolean (value, celltext->scale_set);
592 case PROP_EDITABLE_SET:
593 g_value_set_boolean (value, celltext->editable_set);
596 case PROP_STRIKETHROUGH_SET:
597 g_value_set_boolean (value, celltext->strikethrough_set);
600 case PROP_UNDERLINE_SET:
601 g_value_set_boolean (value, celltext->underline_set);
605 g_value_set_boolean (value, celltext->rise_set);
608 case PROP_BACKGROUND:
609 case PROP_FOREGROUND:
612 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
619 set_bg_color (GtkCellRendererText *celltext,
624 if (!celltext->background_set)
626 celltext->background_set = TRUE;
627 g_object_notify (G_OBJECT (celltext), "background_set");
630 celltext->background.red = color->red;
631 celltext->background.green = color->green;
632 celltext->background.blue = color->blue;
636 if (celltext->background_set)
638 celltext->background_set = FALSE;
639 g_object_notify (G_OBJECT (celltext), "background_set");
646 set_fg_color (GtkCellRendererText *celltext,
651 if (!celltext->foreground_set)
653 celltext->foreground_set = TRUE;
654 g_object_notify (G_OBJECT (celltext), "foreground_set");
657 celltext->foreground.red = color->red;
658 celltext->foreground.green = color->green;
659 celltext->foreground.blue = color->blue;
663 if (celltext->foreground_set)
665 celltext->foreground_set = FALSE;
666 g_object_notify (G_OBJECT (celltext), "foreground_set");
672 notify_set_changed (GObject *object,
673 PangoFontMask changed_mask)
675 if (changed_mask & PANGO_FONT_MASK_FAMILY)
676 g_object_notify (object, "family_set");
677 if (changed_mask & PANGO_FONT_MASK_STYLE)
678 g_object_notify (object, "style_set");
679 if (changed_mask & PANGO_FONT_MASK_VARIANT)
680 g_object_notify (object, "variant_set");
681 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
682 g_object_notify (object, "weight_set");
683 if (changed_mask & PANGO_FONT_MASK_STRETCH)
684 g_object_notify (object, "stretch_set");
685 if (changed_mask & PANGO_FONT_MASK_SIZE)
686 g_object_notify (object, "size_set");
690 set_font_description (GtkCellRendererText *celltext,
691 PangoFontDescription *font_desc)
693 GObject *object = G_OBJECT (celltext);
694 PangoFontDescription *new_font_desc;
695 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
698 new_font_desc = pango_font_description_copy (font_desc);
700 new_font_desc = pango_font_description_new ();
702 old_mask = pango_font_description_get_set_fields (celltext->font);
703 new_mask = pango_font_description_get_set_fields (new_font_desc);
705 changed_mask = old_mask | new_mask;
706 set_changed_mask = old_mask ^ new_mask;
708 pango_font_description_free (celltext->font);
709 celltext->font = new_font_desc;
711 g_object_freeze_notify (object);
713 g_object_notify (object, "font_desc");
714 g_object_notify (object, "font");
716 if (changed_mask & PANGO_FONT_MASK_FAMILY)
717 g_object_notify (object, "family");
718 if (changed_mask & PANGO_FONT_MASK_STYLE)
719 g_object_notify (object, "style");
720 if (changed_mask & PANGO_FONT_MASK_VARIANT)
721 g_object_notify (object, "variant");
722 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
723 g_object_notify (object, "weight");
724 if (changed_mask & PANGO_FONT_MASK_STRETCH)
725 g_object_notify (object, "stretch");
726 if (changed_mask & PANGO_FONT_MASK_SIZE)
728 g_object_notify (object, "size");
729 g_object_notify (object, "size_points");
732 notify_set_changed (object, set_changed_mask);
734 g_object_thaw_notify (object);
738 gtk_cell_renderer_text_set_property (GObject *object,
743 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
749 g_free (celltext->text);
750 celltext->text = g_strdup (g_value_get_string (value));
751 g_object_notify (object, "text");
754 case PROP_ATTRIBUTES:
755 if (celltext->extra_attrs)
756 pango_attr_list_unref (celltext->extra_attrs);
758 celltext->extra_attrs = g_value_get_boxed (value);
759 if (celltext->extra_attrs)
760 pango_attr_list_ref (celltext->extra_attrs);
766 GError *error = NULL;
767 PangoAttrList *attrs = NULL;
770 g_free (celltext->text);
772 if (celltext->extra_attrs)
773 pango_attr_list_unref (celltext->extra_attrs);
775 str = g_value_get_string (value);
776 if (str && !pango_parse_markup (str,
784 g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
786 g_error_free (error);
790 celltext->text = text;
791 celltext->extra_attrs = attrs;
795 case PROP_BACKGROUND:
799 if (!g_value_get_string (value))
800 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
801 else if (gdk_color_parse (g_value_get_string (value), &color))
802 set_bg_color (celltext, &color);
804 g_warning ("Don't know color `%s'", g_value_get_string (value));
806 g_object_notify (object, "background_gdk");
810 case PROP_FOREGROUND:
814 if (!g_value_get_string (value))
815 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
816 else if (gdk_color_parse (g_value_get_string (value), &color))
817 set_fg_color (celltext, &color);
819 g_warning ("Don't know color `%s'", g_value_get_string (value));
821 g_object_notify (object, "foreground_gdk");
825 case PROP_BACKGROUND_GDK:
826 /* This notifies the GObject itself. */
827 set_bg_color (celltext, g_value_get_boxed (value));
830 case PROP_FOREGROUND_GDK:
831 /* This notifies the GObject itself. */
832 set_fg_color (celltext, g_value_get_boxed (value));
837 PangoFontDescription *font_desc = NULL;
840 name = g_value_get_string (value);
843 font_desc = pango_font_description_from_string (name);
845 set_font_description (celltext, font_desc);
847 if (celltext->fixed_height_rows != -1)
848 celltext->calc_fixed_height = TRUE;
853 set_font_description (celltext, g_value_get_boxed (value));
855 if (celltext->fixed_height_rows != -1)
856 celltext->calc_fixed_height = TRUE;
865 case PROP_SIZE_POINTS:
867 PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
872 pango_font_description_set_family (celltext->font,
873 g_value_get_string (value));
876 pango_font_description_set_style (celltext->font,
877 g_value_get_enum (value));
880 pango_font_description_set_variant (celltext->font,
881 g_value_get_enum (value));
884 pango_font_description_set_weight (celltext->font,
885 g_value_get_enum (value));
888 pango_font_description_set_stretch (celltext->font,
889 g_value_get_enum (value));
892 pango_font_description_set_size (celltext->font,
893 g_value_get_int (value));
894 g_object_notify (object, "size_points");
896 case PROP_SIZE_POINTS:
897 pango_font_description_set_size (celltext->font,
898 g_value_get_double (value) * PANGO_SCALE);
899 g_object_notify (object, "size");
903 if (celltext->fixed_height_rows != -1)
904 celltext->calc_fixed_height = TRUE;
906 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
907 g_object_notify (object, "font_desc");
908 g_object_notify (object, "font");
914 celltext->font_scale = g_value_get_double (value);
915 celltext->scale_set = TRUE;
916 if (celltext->fixed_height_rows != -1)
917 celltext->calc_fixed_height = TRUE;
918 g_object_notify (object, "scale_set");
922 celltext->editable = g_value_get_boolean (value);
923 celltext->editable_set = TRUE;
924 g_object_notify (object, "editable_set");
927 case PROP_STRIKETHROUGH:
928 celltext->strikethrough = g_value_get_boolean (value);
929 celltext->strikethrough_set = TRUE;
930 g_object_notify (object, "strikethrough_set");
934 celltext->underline_style = g_value_get_enum (value);
935 celltext->underline_set = TRUE;
936 g_object_notify (object, "underline_set");
941 celltext->rise = g_value_get_int (value);
942 celltext->rise_set = TRUE;
943 g_object_notify (object, "rise_set");
944 if (celltext->fixed_height_rows != -1)
945 celltext->calc_fixed_height = TRUE;
948 case PROP_BACKGROUND_SET:
949 celltext->background_set = g_value_get_boolean (value);
952 case PROP_FOREGROUND_SET:
953 celltext->foreground_set = g_value_get_boolean (value);
956 case PROP_FAMILY_SET:
958 case PROP_VARIANT_SET:
959 case PROP_WEIGHT_SET:
960 case PROP_STRETCH_SET:
962 if (!g_value_get_boolean (value))
963 pango_font_description_unset_fields (celltext->font,
964 get_property_font_set_mask (param_id));
968 celltext->scale_set = g_value_get_boolean (value);
971 case PROP_EDITABLE_SET:
972 celltext->editable_set = g_value_get_boolean (value);
975 case PROP_STRIKETHROUGH_SET:
976 celltext->strikethrough_set = g_value_get_boolean (value);
979 case PROP_UNDERLINE_SET:
980 celltext->underline_set = g_value_get_boolean (value);
984 celltext->rise_set = g_value_get_boolean (value);
988 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
994 * gtk_cell_renderer_text_new:
996 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
997 * object properties. Object properties can be
998 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
999 * you can bind a property to a value in a #GtkTreeModel. For example,
1000 * you can bind the "text" property on the cell renderer to a string
1001 * value in the model, thus rendering a different string in each row
1002 * of the #GtkTreeView
1004 * Return value: the new cell renderer
1007 gtk_cell_renderer_text_new (void)
1009 return GTK_CELL_RENDERER (g_object_new (gtk_cell_renderer_text_get_type (), NULL));
1013 add_attr (PangoAttrList *attr_list,
1014 PangoAttribute *attr)
1016 attr->start_index = 0;
1017 attr->end_index = G_MAXINT;
1019 pango_attr_list_insert (attr_list, attr);
1023 get_layout (GtkCellRendererText *celltext,
1025 gboolean will_render,
1026 GtkCellRendererState flags)
1028 PangoAttrList *attr_list;
1029 PangoLayout *layout;
1030 PangoUnderline uline;
1032 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1034 if (celltext->extra_attrs)
1035 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1037 attr_list = pango_attr_list_new ();
1041 /* Add options that affect appearance but not size */
1043 /* note that background doesn't go here, since it affects
1044 * background_area not the PangoLayout area
1047 if (celltext->foreground_set)
1051 color = celltext->foreground;
1053 add_attr (attr_list,
1054 pango_attr_foreground_new (color.red, color.green, color.blue));
1057 if (celltext->strikethrough_set)
1058 add_attr (attr_list,
1059 pango_attr_strikethrough_new (celltext->strikethrough));
1062 add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1064 if (celltext->scale_set &&
1065 celltext->font_scale != 1.0)
1066 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1068 if (celltext->underline_set)
1069 uline = celltext->underline_style;
1071 uline = PANGO_UNDERLINE_NONE;
1073 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1077 case PANGO_UNDERLINE_NONE:
1078 uline = PANGO_UNDERLINE_SINGLE;
1081 case PANGO_UNDERLINE_SINGLE:
1082 uline = PANGO_UNDERLINE_DOUBLE;
1090 if (uline != PANGO_UNDERLINE_NONE)
1091 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1093 if (celltext->rise_set)
1094 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1096 pango_layout_set_attributes (layout, attr_list);
1097 pango_layout_set_width (layout, -1);
1099 pango_attr_list_unref (attr_list);
1105 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1107 GdkRectangle *cell_area,
1113 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1114 PangoRectangle rect;
1115 PangoLayout *layout;
1117 if (celltext->calc_fixed_height)
1119 PangoContext *context;
1120 PangoFontMetrics *metrics;
1121 PangoFontDescription *font_desc;
1124 font_desc = pango_font_description_copy (widget->style->font_desc);
1125 pango_font_description_merge (font_desc, celltext->font, TRUE);
1127 if (celltext->scale_set)
1128 pango_font_description_set_size (font_desc,
1129 celltext->font_scale * pango_font_description_get_size (font_desc));
1131 context = gtk_widget_get_pango_context (widget);
1133 metrics = pango_context_get_metrics (context,
1135 pango_context_get_language (context));
1136 row_height = (pango_font_metrics_get_ascent (metrics) +
1137 pango_font_metrics_get_descent (metrics));
1138 pango_font_metrics_unref (metrics);
1140 gtk_cell_renderer_set_fixed_size (cell,
1141 cell->width, 2*cell->ypad +
1142 celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1146 *height = cell->height;
1149 celltext->calc_fixed_height = FALSE;
1153 layout = get_layout (celltext, widget, FALSE, 0);
1154 pango_layout_get_pixel_extents (layout, NULL, &rect);
1157 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1160 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1166 *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
1167 *x_offset = MAX (*x_offset, 0);
1171 *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1172 *y_offset = MAX (*y_offset, 0);
1176 g_object_unref (G_OBJECT (layout));
1180 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1183 GdkRectangle *background_area,
1184 GdkRectangle *cell_area,
1185 GdkRectangle *expose_area,
1189 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1190 PangoLayout *layout;
1195 layout = get_layout (celltext, widget, TRUE, flags);
1197 gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1199 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1201 if (GTK_WIDGET_HAS_FOCUS (widget))
1202 state = GTK_STATE_SELECTED;
1204 state = GTK_STATE_ACTIVE;
1208 state = GTK_STATE_NORMAL;
1211 if (celltext->background_set && state != GTK_STATE_SELECTED)
1216 color.red = celltext->background.red;
1217 color.green = celltext->background.green;
1218 color.blue = celltext->background.blue;
1220 gc = gdk_gc_new (window);
1222 gdk_gc_set_rgb_fg_color (gc, &color);
1224 gdk_draw_rectangle (window,
1229 background_area->width,
1230 background_area->height);
1232 g_object_unref (G_OBJECT (gc));
1235 gtk_paint_layout (widget->style,
1242 cell_area->x + x_offset + cell->xpad,
1243 cell_area->y + y_offset + cell->ypad,
1246 g_object_unref (G_OBJECT (layout));
1249 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1255 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1256 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1258 gtk_signal_emit (GTK_OBJECT (data), text_cell_renderer_signals[EDITED], path, new_text);
1261 static GtkCellEditable *
1262 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1266 GdkRectangle *background_area,
1267 GdkRectangle *cell_area,
1268 GtkCellRendererState flags)
1270 GtkCellRendererText *celltext;
1273 celltext = GTK_CELL_RENDERER_TEXT (cell);
1275 /* If the cell isn't editable we return NULL. */
1276 if (celltext->editable == FALSE)
1279 entry = g_object_new (GTK_TYPE_ENTRY,
1283 gtk_entry_set_text (GTK_ENTRY (entry), celltext->text);
1284 g_object_set_data_full (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
1286 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1288 gtk_widget_show (entry);
1289 gtk_signal_connect (GTK_OBJECT (entry),
1291 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1293 return GTK_CELL_EDITABLE (entry);
1298 * gtk_cell_renderer_text_set_fixed_height_from_font:
1299 * @renderer: A #GtkCellRendererText
1300 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1302 * Sets the height of a renderer to explicitly be determined by the "font" and
1303 * "y_pad" property set on it. Further changes in these properties do not
1304 * affect the height, so they must be accompanied by a subsequent call to this
1305 * function. Using this function is unflexible, and should really only be used
1306 * if calculating the size of a cell is too slow (ie, a massive number of cells
1307 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1308 * the height is determined by the properties again.
1311 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1312 gint number_of_rows)
1314 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1315 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1317 if (number_of_rows == -1)
1319 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1320 GTK_CELL_RENDERER (renderer)->width,
1325 renderer->fixed_height_rows = number_of_rows;
1326 renderer->calc_fixed_height = TRUE;