1 /* gtkcellrenderertext.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcellrenderertext.h"
23 #include "gtkeditable.h"
25 #include "gtkmarshalers.h"
27 #include "gtkprivate.h"
28 #include "gtktreeprivate.h"
31 static void gtk_cell_renderer_text_finalize (GObject *object);
33 static void gtk_cell_renderer_text_get_property (GObject *object,
37 static void gtk_cell_renderer_text_set_property (GObject *object,
41 static void gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
43 GdkRectangle *cell_area,
48 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
51 GdkRectangle *background_area,
52 GdkRectangle *cell_area,
53 GdkRectangle *expose_area,
54 GtkCellRendererState flags);
56 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
60 GdkRectangle *background_area,
61 GdkRectangle *cell_area,
62 GtkCellRendererState flags);
75 PROP_SINGLE_PARAGRAPH_MODE,
103 /* Whether-a-style-arg-is-set args */
114 PROP_STRIKETHROUGH_SET,
121 static guint text_cell_renderer_signals [LAST_SIGNAL];
123 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
125 #define GTK_CELL_RENDERER_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TEXT, GtkCellRendererTextPrivate))
127 typedef struct _GtkCellRendererTextPrivate GtkCellRendererTextPrivate;
128 struct _GtkCellRendererTextPrivate
130 guint single_paragraph : 1;
131 guint language_set : 1;
132 guint markup_set : 1;
133 guint ellipsize_set : 1;
136 PangoLanguage *language;
137 PangoEllipsizeMode ellipsize;
138 PangoWrapMode wrap_mode;
139 PangoAlignment align;
141 gulong populate_popup_id;
142 gulong entry_menu_popdown_timeout;
143 gboolean in_entry_menu;
151 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
154 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
156 GtkCellRendererTextPrivate *priv;
158 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
160 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
161 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
162 GTK_CELL_RENDERER (celltext)->xpad = 2;
163 GTK_CELL_RENDERER (celltext)->ypad = 2;
164 celltext->fixed_height_rows = -1;
165 celltext->font = pango_font_description_new ();
167 priv->width_chars = -1;
168 priv->wrap_width = -1;
169 priv->align = PANGO_ALIGN_LEFT;
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 object_class->finalize = gtk_cell_renderer_text_finalize;
180 object_class->get_property = gtk_cell_renderer_text_get_property;
181 object_class->set_property = gtk_cell_renderer_text_set_property;
183 cell_class->get_size = gtk_cell_renderer_text_get_size;
184 cell_class->render = gtk_cell_renderer_text_render;
185 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
187 g_object_class_install_property (object_class,
189 g_param_spec_string ("text",
191 P_("Text to render"),
193 GTK_PARAM_READWRITE));
195 g_object_class_install_property (object_class,
197 g_param_spec_string ("markup",
199 P_("Marked up text to render"),
201 GTK_PARAM_WRITABLE));
203 g_object_class_install_property (object_class,
205 g_param_spec_boxed ("attributes",
207 P_("A list of style attributes to apply to the text of the renderer"),
208 PANGO_TYPE_ATTR_LIST,
209 GTK_PARAM_READWRITE));
211 g_object_class_install_property (object_class,
212 PROP_SINGLE_PARAGRAPH_MODE,
213 g_param_spec_boolean ("single-paragraph-mode",
214 P_("Single Paragraph Mode"),
215 P_("Whether or not to keep all text in a single paragraph"),
217 GTK_PARAM_READWRITE));
220 g_object_class_install_property (object_class,
222 g_param_spec_string ("background",
223 P_("Background color name"),
224 P_("Background color as a string"),
226 GTK_PARAM_WRITABLE));
228 g_object_class_install_property (object_class,
230 g_param_spec_boxed ("background-gdk",
231 P_("Background color"),
232 P_("Background color as a GdkColor"),
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (object_class,
238 g_param_spec_string ("foreground",
239 P_("Foreground color name"),
240 P_("Foreground color as a string"),
242 GTK_PARAM_WRITABLE));
244 g_object_class_install_property (object_class,
246 g_param_spec_boxed ("foreground-gdk",
247 P_("Foreground color"),
248 P_("Foreground color as a GdkColor"),
250 GTK_PARAM_READWRITE));
253 g_object_class_install_property (object_class,
255 g_param_spec_boolean ("editable",
257 P_("Whether the text can be modified by the user"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (object_class,
263 g_param_spec_string ("font",
265 P_("Font description as a string, e.g. \"Sans Italic 12\""),
267 GTK_PARAM_READWRITE));
269 g_object_class_install_property (object_class,
271 g_param_spec_boxed ("font-desc",
273 P_("Font description as a PangoFontDescription struct"),
274 PANGO_TYPE_FONT_DESCRIPTION,
275 GTK_PARAM_READWRITE));
278 g_object_class_install_property (object_class,
280 g_param_spec_string ("family",
282 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
284 GTK_PARAM_READWRITE));
286 g_object_class_install_property (object_class,
288 g_param_spec_enum ("style",
293 GTK_PARAM_READWRITE));
295 g_object_class_install_property (object_class,
297 g_param_spec_enum ("variant",
301 PANGO_VARIANT_NORMAL,
302 GTK_PARAM_READWRITE));
304 g_object_class_install_property (object_class,
306 g_param_spec_int ("weight",
312 GTK_PARAM_READWRITE));
314 g_object_class_install_property (object_class,
316 g_param_spec_enum ("stretch",
320 PANGO_STRETCH_NORMAL,
321 GTK_PARAM_READWRITE));
323 g_object_class_install_property (object_class,
325 g_param_spec_int ("size",
331 GTK_PARAM_READWRITE));
333 g_object_class_install_property (object_class,
335 g_param_spec_double ("size-points",
337 P_("Font size in points"),
341 GTK_PARAM_READWRITE));
343 g_object_class_install_property (object_class,
345 g_param_spec_double ("scale",
347 P_("Font scaling factor"),
351 GTK_PARAM_READWRITE));
353 g_object_class_install_property (object_class,
355 g_param_spec_int ("rise",
357 P_("Offset of text above the baseline "
358 "(below the baseline if rise is negative)"),
362 GTK_PARAM_READWRITE));
365 g_object_class_install_property (object_class,
367 g_param_spec_boolean ("strikethrough",
369 P_("Whether to strike through the text"),
371 GTK_PARAM_READWRITE));
373 g_object_class_install_property (object_class,
375 g_param_spec_enum ("underline",
377 P_("Style of underline for this text"),
378 PANGO_TYPE_UNDERLINE,
379 PANGO_UNDERLINE_NONE,
380 GTK_PARAM_READWRITE));
382 g_object_class_install_property (object_class,
384 g_param_spec_string ("language",
386 P_("The language this text is in, as an ISO code. "
387 "Pango can use this as a hint when rendering the text. "
388 "If you don't understand this parameter, you probably don't need it"),
390 GTK_PARAM_READWRITE));
394 * GtkCellRendererText:ellipsize:
396 * Specifies the preferred place to ellipsize the string, if the cell renderer
397 * does not have enough room to display the entire string. Setting it to
398 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
399 * for another way of making the text fit into a given width.
403 g_object_class_install_property (object_class,
405 g_param_spec_enum ("ellipsize",
407 P_("The preferred place to ellipsize the string, "
408 "if the cell renderer does not have enough room "
409 "to display the entire string"),
410 PANGO_TYPE_ELLIPSIZE_MODE,
411 PANGO_ELLIPSIZE_NONE,
412 GTK_PARAM_READWRITE));
415 * GtkCellRendererText:width-chars:
417 * The desired width of the cell, in characters. If this property is set to
418 * -1, the width will be calculated automatically, otherwise the cell will
419 * request either 3 characters or the property value, whichever is greater.
423 g_object_class_install_property (object_class,
425 g_param_spec_int ("width-chars",
426 P_("Width In Characters"),
427 P_("The desired width of the label, in characters"),
431 GTK_PARAM_READWRITE));
434 * GtkCellRendererText:wrap-mode:
436 * Specifies how to break the string into multiple lines, if the cell
437 * renderer does not have enough room to display the entire string.
438 * This property has no effect unless the wrap-width property is set.
442 g_object_class_install_property (object_class,
444 g_param_spec_enum ("wrap-mode",
446 P_("How to break the string into multiple lines, "
447 "if the cell renderer does not have enough room "
448 "to display the entire string"),
449 PANGO_TYPE_WRAP_MODE,
451 GTK_PARAM_READWRITE));
454 * GtkCellRendererText:wrap-width:
456 * Specifies the width at which the text is wrapped. The wrap-mode property can
457 * be used to influence at what character positions the line breaks can be placed.
458 * Setting wrap-width to -1 turns wrapping off.
462 g_object_class_install_property (object_class,
464 g_param_spec_int ("wrap-width",
466 P_("The width at which the text is wrapped"),
470 GTK_PARAM_READWRITE));
473 * GtkCellRendererText:alignment:
475 * Specifies how to align the lines of text.
479 g_object_class_install_property (object_class,
481 g_param_spec_enum ("alignment",
483 P_("How to align the lines"),
484 PANGO_TYPE_ALIGNMENT,
486 GTK_PARAM_READWRITE));
488 /* Style props are set or not */
490 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE))
492 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
493 P_("Background set"),
494 P_("Whether this tag affects the background color"));
496 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
497 P_("Foreground set"),
498 P_("Whether this tag affects the foreground color"));
500 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
501 P_("Editability set"),
502 P_("Whether this tag affects text editability"));
504 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
505 P_("Font family set"),
506 P_("Whether this tag affects the font family"));
508 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
509 P_("Font style set"),
510 P_("Whether this tag affects the font style"));
512 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
513 P_("Font variant set"),
514 P_("Whether this tag affects the font variant"));
516 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
517 P_("Font weight set"),
518 P_("Whether this tag affects the font weight"));
520 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
521 P_("Font stretch set"),
522 P_("Whether this tag affects the font stretch"));
524 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
526 P_("Whether this tag affects the font size"));
528 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
529 P_("Font scale set"),
530 P_("Whether this tag scales the font size by a factor"));
532 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
534 P_("Whether this tag affects the rise"));
536 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
537 P_("Strikethrough set"),
538 P_("Whether this tag affects strikethrough"));
540 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
542 P_("Whether this tag affects underlining"));
544 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
546 P_("Whether this tag affects the language the text is rendered as"));
548 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
550 P_("Whether this tag affects the ellipsize mode"));
552 text_cell_renderer_signals [EDITED] =
553 g_signal_new (I_("edited"),
554 G_OBJECT_CLASS_TYPE (object_class),
556 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
558 _gtk_marshal_VOID__STRING_STRING,
563 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
567 gtk_cell_renderer_text_finalize (GObject *object)
569 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
570 GtkCellRendererTextPrivate *priv;
572 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
574 pango_font_description_free (celltext->font);
577 g_free (celltext->text);
579 if (celltext->extra_attrs)
580 pango_attr_list_unref (celltext->extra_attrs);
583 g_object_unref (priv->language);
585 (* G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize) (object);
589 get_property_font_set_mask (guint prop_id)
593 case PROP_FAMILY_SET:
594 return PANGO_FONT_MASK_FAMILY;
596 return PANGO_FONT_MASK_STYLE;
597 case PROP_VARIANT_SET:
598 return PANGO_FONT_MASK_VARIANT;
599 case PROP_WEIGHT_SET:
600 return PANGO_FONT_MASK_WEIGHT;
601 case PROP_STRETCH_SET:
602 return PANGO_FONT_MASK_STRETCH;
604 return PANGO_FONT_MASK_SIZE;
611 gtk_cell_renderer_text_get_property (GObject *object,
616 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
617 GtkCellRendererTextPrivate *priv;
619 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
624 g_value_set_string (value, celltext->text);
627 case PROP_ATTRIBUTES:
628 g_value_set_boxed (value, celltext->extra_attrs);
631 case PROP_SINGLE_PARAGRAPH_MODE:
632 g_value_set_boolean (value, priv->single_paragraph);
635 case PROP_BACKGROUND_GDK:
639 color.red = celltext->background.red;
640 color.green = celltext->background.green;
641 color.blue = celltext->background.blue;
643 g_value_set_boxed (value, &color);
647 case PROP_FOREGROUND_GDK:
651 color.red = celltext->foreground.red;
652 color.green = celltext->foreground.green;
653 color.blue = celltext->foreground.blue;
655 g_value_set_boxed (value, &color);
660 g_value_take_string (value, pango_font_description_to_string (celltext->font));
664 g_value_set_boxed (value, celltext->font);
668 g_value_set_string (value, pango_font_description_get_family (celltext->font));
672 g_value_set_enum (value, pango_font_description_get_style (celltext->font));
676 g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
680 g_value_set_int (value, pango_font_description_get_weight (celltext->font));
684 g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
688 g_value_set_int (value, pango_font_description_get_size (celltext->font));
691 case PROP_SIZE_POINTS:
692 g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
696 g_value_set_double (value, celltext->font_scale);
700 g_value_set_boolean (value, celltext->editable);
703 case PROP_STRIKETHROUGH:
704 g_value_set_boolean (value, celltext->strikethrough);
708 g_value_set_enum (value, celltext->underline_style);
712 g_value_set_int (value, celltext->rise);
716 g_value_set_string (value, pango_language_to_string (priv->language));
720 g_value_set_enum (value, priv->ellipsize);
724 g_value_set_enum (value, priv->wrap_mode);
727 case PROP_WRAP_WIDTH:
728 g_value_set_int (value, priv->wrap_width);
732 g_value_set_enum (value, priv->align);
735 case PROP_BACKGROUND_SET:
736 g_value_set_boolean (value, celltext->background_set);
739 case PROP_FOREGROUND_SET:
740 g_value_set_boolean (value, celltext->foreground_set);
743 case PROP_FAMILY_SET:
745 case PROP_VARIANT_SET:
746 case PROP_WEIGHT_SET:
747 case PROP_STRETCH_SET:
750 PangoFontMask mask = get_property_font_set_mask (param_id);
751 g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
757 g_value_set_boolean (value, celltext->scale_set);
760 case PROP_EDITABLE_SET:
761 g_value_set_boolean (value, celltext->editable_set);
764 case PROP_STRIKETHROUGH_SET:
765 g_value_set_boolean (value, celltext->strikethrough_set);
768 case PROP_UNDERLINE_SET:
769 g_value_set_boolean (value, celltext->underline_set);
773 g_value_set_boolean (value, celltext->rise_set);
776 case PROP_LANGUAGE_SET:
777 g_value_set_boolean (value, priv->language_set);
780 case PROP_ELLIPSIZE_SET:
781 g_value_set_boolean (value, priv->ellipsize_set);
784 case PROP_WIDTH_CHARS:
785 g_value_set_int (value, priv->width_chars);
788 case PROP_BACKGROUND:
789 case PROP_FOREGROUND:
792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
799 set_bg_color (GtkCellRendererText *celltext,
804 if (!celltext->background_set)
806 celltext->background_set = TRUE;
807 g_object_notify (G_OBJECT (celltext), "background-set");
810 celltext->background.red = color->red;
811 celltext->background.green = color->green;
812 celltext->background.blue = color->blue;
816 if (celltext->background_set)
818 celltext->background_set = FALSE;
819 g_object_notify (G_OBJECT (celltext), "background-set");
826 set_fg_color (GtkCellRendererText *celltext,
831 if (!celltext->foreground_set)
833 celltext->foreground_set = TRUE;
834 g_object_notify (G_OBJECT (celltext), "foreground-set");
837 celltext->foreground.red = color->red;
838 celltext->foreground.green = color->green;
839 celltext->foreground.blue = color->blue;
843 if (celltext->foreground_set)
845 celltext->foreground_set = FALSE;
846 g_object_notify (G_OBJECT (celltext), "foreground-set");
852 set_font_desc_fields (PangoFontDescription *desc,
853 PangoFontMask to_set)
855 PangoFontMask changed_mask = 0;
857 if (to_set & PANGO_FONT_MASK_FAMILY)
859 const char *family = pango_font_description_get_family (desc);
863 changed_mask |= PANGO_FONT_MASK_FAMILY;
866 pango_font_description_set_family (desc, family);
868 if (to_set & PANGO_FONT_MASK_STYLE)
869 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
870 if (to_set & PANGO_FONT_MASK_VARIANT)
871 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
872 if (to_set & PANGO_FONT_MASK_WEIGHT)
873 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
874 if (to_set & PANGO_FONT_MASK_STRETCH)
875 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
876 if (to_set & PANGO_FONT_MASK_SIZE)
878 gint size = pango_font_description_get_size (desc);
881 size = 10 * PANGO_SCALE;
882 changed_mask |= PANGO_FONT_MASK_SIZE;
885 pango_font_description_set_size (desc, size);
892 notify_set_changed (GObject *object,
893 PangoFontMask changed_mask)
895 if (changed_mask & PANGO_FONT_MASK_FAMILY)
896 g_object_notify (object, "family-set");
897 if (changed_mask & PANGO_FONT_MASK_STYLE)
898 g_object_notify (object, "style-set");
899 if (changed_mask & PANGO_FONT_MASK_VARIANT)
900 g_object_notify (object, "variant-set");
901 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
902 g_object_notify (object, "weight-set");
903 if (changed_mask & PANGO_FONT_MASK_STRETCH)
904 g_object_notify (object, "stretch-set");
905 if (changed_mask & PANGO_FONT_MASK_SIZE)
906 g_object_notify (object, "size-set");
910 notify_fields_changed (GObject *object,
911 PangoFontMask changed_mask)
913 if (changed_mask & PANGO_FONT_MASK_FAMILY)
914 g_object_notify (object, "family");
915 if (changed_mask & PANGO_FONT_MASK_STYLE)
916 g_object_notify (object, "style");
917 if (changed_mask & PANGO_FONT_MASK_VARIANT)
918 g_object_notify (object, "variant");
919 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
920 g_object_notify (object, "weight");
921 if (changed_mask & PANGO_FONT_MASK_STRETCH)
922 g_object_notify (object, "stretch");
923 if (changed_mask & PANGO_FONT_MASK_SIZE)
924 g_object_notify (object, "size");
928 set_font_description (GtkCellRendererText *celltext,
929 PangoFontDescription *font_desc)
931 GObject *object = G_OBJECT (celltext);
932 PangoFontDescription *new_font_desc;
933 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
936 new_font_desc = pango_font_description_copy (font_desc);
938 new_font_desc = pango_font_description_new ();
940 old_mask = pango_font_description_get_set_fields (celltext->font);
941 new_mask = pango_font_description_get_set_fields (new_font_desc);
943 changed_mask = old_mask | new_mask;
944 set_changed_mask = old_mask ^ new_mask;
946 pango_font_description_free (celltext->font);
947 celltext->font = new_font_desc;
949 g_object_freeze_notify (object);
951 g_object_notify (object, "font-desc");
952 g_object_notify (object, "font");
954 if (changed_mask & PANGO_FONT_MASK_FAMILY)
955 g_object_notify (object, "family");
956 if (changed_mask & PANGO_FONT_MASK_STYLE)
957 g_object_notify (object, "style");
958 if (changed_mask & PANGO_FONT_MASK_VARIANT)
959 g_object_notify (object, "variant");
960 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
961 g_object_notify (object, "weight");
962 if (changed_mask & PANGO_FONT_MASK_STRETCH)
963 g_object_notify (object, "stretch");
964 if (changed_mask & PANGO_FONT_MASK_SIZE)
966 g_object_notify (object, "size");
967 g_object_notify (object, "size-points");
970 notify_set_changed (object, set_changed_mask);
972 g_object_thaw_notify (object);
976 gtk_cell_renderer_text_set_property (GObject *object,
981 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
982 GtkCellRendererTextPrivate *priv;
984 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
990 g_free (celltext->text);
992 if (priv->markup_set)
994 if (celltext->extra_attrs)
995 pango_attr_list_unref (celltext->extra_attrs);
996 celltext->extra_attrs = NULL;
997 priv->markup_set = FALSE;
1000 celltext->text = g_strdup (g_value_get_string (value));
1001 g_object_notify (object, "text");
1004 case PROP_ATTRIBUTES:
1005 if (celltext->extra_attrs)
1006 pango_attr_list_unref (celltext->extra_attrs);
1008 celltext->extra_attrs = g_value_get_boxed (value);
1009 if (celltext->extra_attrs)
1010 pango_attr_list_ref (celltext->extra_attrs);
1016 GError *error = NULL;
1017 PangoAttrList *attrs = NULL;
1019 str = g_value_get_string (value);
1020 if (str && !pango_parse_markup (str,
1028 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1030 g_error_free (error);
1035 g_free (celltext->text);
1037 if (celltext->extra_attrs)
1038 pango_attr_list_unref (celltext->extra_attrs);
1040 celltext->text = text;
1041 celltext->extra_attrs = attrs;
1042 priv->markup_set = TRUE;
1046 case PROP_SINGLE_PARAGRAPH_MODE:
1047 priv->single_paragraph = g_value_get_boolean (value);
1050 case PROP_BACKGROUND:
1054 if (!g_value_get_string (value))
1055 set_bg_color (celltext, NULL); /* reset to backgrounmd_set to FALSE */
1056 else if (gdk_color_parse (g_value_get_string (value), &color))
1057 set_bg_color (celltext, &color);
1059 g_warning ("Don't know color `%s'", g_value_get_string (value));
1061 g_object_notify (object, "background-gdk");
1065 case PROP_FOREGROUND:
1069 if (!g_value_get_string (value))
1070 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1071 else if (gdk_color_parse (g_value_get_string (value), &color))
1072 set_fg_color (celltext, &color);
1074 g_warning ("Don't know color `%s'", g_value_get_string (value));
1076 g_object_notify (object, "foreground-gdk");
1080 case PROP_BACKGROUND_GDK:
1081 /* This notifies the GObject itself. */
1082 set_bg_color (celltext, g_value_get_boxed (value));
1085 case PROP_FOREGROUND_GDK:
1086 /* This notifies the GObject itself. */
1087 set_fg_color (celltext, g_value_get_boxed (value));
1092 PangoFontDescription *font_desc = NULL;
1095 name = g_value_get_string (value);
1098 font_desc = pango_font_description_from_string (name);
1100 set_font_description (celltext, font_desc);
1102 pango_font_description_free (font_desc);
1104 if (celltext->fixed_height_rows != -1)
1105 celltext->calc_fixed_height = TRUE;
1109 case PROP_FONT_DESC:
1110 set_font_description (celltext, g_value_get_boxed (value));
1112 if (celltext->fixed_height_rows != -1)
1113 celltext->calc_fixed_height = TRUE;
1122 case PROP_SIZE_POINTS:
1124 PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
1129 pango_font_description_set_family (celltext->font,
1130 g_value_get_string (value));
1133 pango_font_description_set_style (celltext->font,
1134 g_value_get_enum (value));
1137 pango_font_description_set_variant (celltext->font,
1138 g_value_get_enum (value));
1141 pango_font_description_set_weight (celltext->font,
1142 g_value_get_int (value));
1145 pango_font_description_set_stretch (celltext->font,
1146 g_value_get_enum (value));
1149 pango_font_description_set_size (celltext->font,
1150 g_value_get_int (value));
1151 g_object_notify (object, "size-points");
1153 case PROP_SIZE_POINTS:
1154 pango_font_description_set_size (celltext->font,
1155 g_value_get_double (value) * PANGO_SCALE);
1156 g_object_notify (object, "size");
1160 if (celltext->fixed_height_rows != -1)
1161 celltext->calc_fixed_height = TRUE;
1163 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
1164 g_object_notify (object, "font-desc");
1165 g_object_notify (object, "font");
1171 celltext->font_scale = g_value_get_double (value);
1172 celltext->scale_set = TRUE;
1173 if (celltext->fixed_height_rows != -1)
1174 celltext->calc_fixed_height = TRUE;
1175 g_object_notify (object, "scale-set");
1179 celltext->editable = g_value_get_boolean (value);
1180 celltext->editable_set = TRUE;
1181 if (celltext->editable)
1182 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
1184 GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_INERT;
1185 g_object_notify (object, "editable-set");
1188 case PROP_STRIKETHROUGH:
1189 celltext->strikethrough = g_value_get_boolean (value);
1190 celltext->strikethrough_set = TRUE;
1191 g_object_notify (object, "strikethrough-set");
1194 case PROP_UNDERLINE:
1195 celltext->underline_style = g_value_get_enum (value);
1196 celltext->underline_set = TRUE;
1197 g_object_notify (object, "underline-set");
1202 celltext->rise = g_value_get_int (value);
1203 celltext->rise_set = TRUE;
1204 g_object_notify (object, "rise-set");
1205 if (celltext->fixed_height_rows != -1)
1206 celltext->calc_fixed_height = TRUE;
1210 priv->language_set = TRUE;
1212 g_object_unref (priv->language);
1213 priv->language = pango_language_from_string (g_value_get_string (value));
1214 g_object_notify (object, "language-set");
1217 case PROP_ELLIPSIZE:
1218 priv->ellipsize = g_value_get_enum (value);
1219 priv->ellipsize_set = TRUE;
1220 g_object_notify (object, "ellipsize-set");
1223 case PROP_WRAP_MODE:
1224 priv->wrap_mode = g_value_get_enum (value);
1227 case PROP_WRAP_WIDTH:
1228 priv->wrap_width = g_value_get_int (value);
1231 case PROP_WIDTH_CHARS:
1232 priv->width_chars = g_value_get_int (value);
1236 priv->align = g_value_get_enum (value);
1239 case PROP_BACKGROUND_SET:
1240 celltext->background_set = g_value_get_boolean (value);
1243 case PROP_FOREGROUND_SET:
1244 celltext->foreground_set = g_value_get_boolean (value);
1247 case PROP_FAMILY_SET:
1248 case PROP_STYLE_SET:
1249 case PROP_VARIANT_SET:
1250 case PROP_WEIGHT_SET:
1251 case PROP_STRETCH_SET:
1253 if (!g_value_get_boolean (value))
1255 pango_font_description_unset_fields (celltext->font,
1256 get_property_font_set_mask (param_id));
1260 PangoFontMask changed_mask;
1262 changed_mask = set_font_desc_fields (celltext->font,
1263 get_property_font_set_mask (param_id));
1264 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1268 case PROP_SCALE_SET:
1269 celltext->scale_set = g_value_get_boolean (value);
1272 case PROP_EDITABLE_SET:
1273 celltext->editable_set = g_value_get_boolean (value);
1276 case PROP_STRIKETHROUGH_SET:
1277 celltext->strikethrough_set = g_value_get_boolean (value);
1280 case PROP_UNDERLINE_SET:
1281 celltext->underline_set = g_value_get_boolean (value);
1285 celltext->rise_set = g_value_get_boolean (value);
1288 case PROP_LANGUAGE_SET:
1289 priv->language_set = g_value_get_boolean (value);
1292 case PROP_ELLIPSIZE_SET:
1293 priv->ellipsize_set = g_value_get_boolean (value);
1297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1303 * gtk_cell_renderer_text_new:
1305 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1306 * object properties. Object properties can be
1307 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1308 * you can bind a property to a value in a #GtkTreeModel. For example,
1309 * you can bind the "text" property on the cell renderer to a string
1310 * value in the model, thus rendering a different string in each row
1311 * of the #GtkTreeView
1313 * Return value: the new cell renderer
1316 gtk_cell_renderer_text_new (void)
1318 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1322 add_attr (PangoAttrList *attr_list,
1323 PangoAttribute *attr)
1325 attr->start_index = 0;
1326 attr->end_index = G_MAXINT;
1328 pango_attr_list_insert (attr_list, attr);
1332 get_layout (GtkCellRendererText *celltext,
1334 gboolean will_render,
1335 GtkCellRendererState flags)
1337 PangoAttrList *attr_list;
1338 PangoLayout *layout;
1339 PangoUnderline uline;
1340 GtkCellRendererTextPrivate *priv;
1342 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
1344 layout = gtk_widget_create_pango_layout (widget, celltext->text);
1346 if (celltext->extra_attrs)
1347 attr_list = pango_attr_list_copy (celltext->extra_attrs);
1349 attr_list = pango_attr_list_new ();
1351 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1355 /* Add options that affect appearance but not size */
1357 /* note that background doesn't go here, since it affects
1358 * background_area not the PangoLayout area
1361 if (celltext->foreground_set
1362 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1366 color = celltext->foreground;
1368 add_attr (attr_list,
1369 pango_attr_foreground_new (color.red, color.green, color.blue));
1372 if (celltext->strikethrough_set)
1373 add_attr (attr_list,
1374 pango_attr_strikethrough_new (celltext->strikethrough));
1377 add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1379 if (celltext->scale_set &&
1380 celltext->font_scale != 1.0)
1381 add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1383 if (celltext->underline_set)
1384 uline = celltext->underline_style;
1386 uline = PANGO_UNDERLINE_NONE;
1388 if (priv->language_set)
1389 add_attr (attr_list, pango_attr_language_new (priv->language));
1391 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1395 case PANGO_UNDERLINE_NONE:
1396 uline = PANGO_UNDERLINE_SINGLE;
1399 case PANGO_UNDERLINE_SINGLE:
1400 uline = PANGO_UNDERLINE_DOUBLE;
1408 if (uline != PANGO_UNDERLINE_NONE)
1409 add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1411 if (celltext->rise_set)
1412 add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1414 if (priv->ellipsize_set)
1415 pango_layout_set_ellipsize (layout, priv->ellipsize);
1417 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1419 if (priv->wrap_width != -1)
1421 pango_layout_set_width (layout, priv->wrap_width * PANGO_SCALE);
1422 pango_layout_set_wrap (layout, priv->wrap_mode);
1424 if (pango_layout_get_line_count (layout) == 1)
1426 pango_layout_set_width (layout, -1);
1427 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1432 pango_layout_set_width (layout, -1);
1433 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1436 pango_layout_set_alignment (layout, priv->align);
1438 pango_layout_set_attributes (layout, attr_list);
1440 pango_attr_list_unref (attr_list);
1446 get_size (GtkCellRenderer *cell,
1448 GdkRectangle *cell_area,
1449 PangoLayout *layout,
1455 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1456 PangoRectangle rect;
1457 GtkCellRendererTextPrivate *priv;
1459 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1461 if (celltext->calc_fixed_height)
1463 PangoContext *context;
1464 PangoFontMetrics *metrics;
1465 PangoFontDescription *font_desc;
1468 font_desc = pango_font_description_copy_static (widget->style->font_desc);
1469 pango_font_description_merge_static (font_desc, celltext->font, TRUE);
1471 if (celltext->scale_set)
1472 pango_font_description_set_size (font_desc,
1473 celltext->font_scale * pango_font_description_get_size (font_desc));
1475 context = gtk_widget_get_pango_context (widget);
1477 metrics = pango_context_get_metrics (context,
1479 pango_context_get_language (context));
1480 row_height = (pango_font_metrics_get_ascent (metrics) +
1481 pango_font_metrics_get_descent (metrics));
1482 pango_font_metrics_unref (metrics);
1484 pango_font_description_free (font_desc);
1486 gtk_cell_renderer_set_fixed_size (cell,
1487 cell->width, 2*cell->ypad +
1488 celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1492 *height = cell->height;
1495 celltext->calc_fixed_height = FALSE;
1501 g_object_ref (layout);
1503 layout = get_layout (celltext, widget, FALSE, 0);
1505 pango_layout_get_pixel_extents (layout, NULL, &rect);
1508 *height = cell->ypad * 2 + rect.height;
1510 /* The minimum size for ellipsized labels is ~ 3 chars */
1513 if (priv->ellipsize || priv->width_chars > 0)
1515 PangoContext *context;
1516 PangoFontMetrics *metrics;
1519 context = pango_layout_get_context (layout);
1520 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
1522 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1523 pango_font_metrics_unref (metrics);
1525 *width = cell->xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1529 *width = cell->xpad * 2 + rect.x + rect.width;
1537 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1538 *x_offset = (1.0 - cell->xalign) * (cell_area->width - (rect.x + rect.width + (2 * cell->xpad)));
1540 *x_offset = cell->xalign * (cell_area->width - (rect.x + rect.width + (2 * cell->xpad)));
1542 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1543 *x_offset = MAX(*x_offset, 0);
1547 *y_offset = cell->yalign * (cell_area->height - (rect.height + (2 * cell->ypad)));
1548 *y_offset = MAX (*y_offset, 0);
1552 g_object_unref (layout);
1557 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1559 GdkRectangle *cell_area,
1565 get_size (cell, widget, cell_area, NULL,
1566 x_offset, y_offset, width, height);
1570 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1571 GdkDrawable *window,
1573 GdkRectangle *background_area,
1574 GdkRectangle *cell_area,
1575 GdkRectangle *expose_area,
1576 GtkCellRendererState flags)
1579 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1580 PangoLayout *layout;
1584 GtkCellRendererTextPrivate *priv;
1586 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1588 layout = get_layout (celltext, widget, TRUE, flags);
1589 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1591 if (!cell->sensitive)
1593 state = GTK_STATE_INSENSITIVE;
1595 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1597 if (GTK_WIDGET_HAS_FOCUS (widget))
1598 state = GTK_STATE_SELECTED;
1600 state = GTK_STATE_ACTIVE;
1602 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1603 GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT)
1605 state = GTK_STATE_PRELIGHT;
1609 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
1610 state = GTK_STATE_INSENSITIVE;
1612 state = GTK_STATE_NORMAL;
1615 if (celltext->background_set &&
1616 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1618 cairo_t *cr = gdk_cairo_create (window);
1622 gdk_cairo_rectangle (cr, expose_area);
1626 gdk_cairo_rectangle (cr, background_area);
1627 cairo_set_source_rgb (cr,
1628 celltext->background.red / 65535.,
1629 celltext->background.green / 65535.,
1630 celltext->background.blue / 65535.);
1636 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1637 pango_layout_set_width (layout,
1638 (cell_area->width - x_offset - 2 * cell->xpad) * PANGO_SCALE);
1639 else if (priv->wrap_width == -1)
1640 pango_layout_set_width (layout, -1);
1642 gtk_paint_layout (widget->style,
1649 cell_area->x + x_offset + cell->xpad,
1650 cell_area->y + y_offset + cell->ypad,
1653 g_object_unref (layout);
1657 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1661 const gchar *new_text;
1662 GtkCellRendererTextPrivate *priv;
1664 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1668 if (priv->focus_out_id > 0)
1670 g_signal_handler_disconnect (entry, priv->focus_out_id);
1671 priv->focus_out_id = 0;
1674 if (priv->populate_popup_id > 0)
1676 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1677 priv->populate_popup_id = 0;
1680 if (priv->entry_menu_popdown_timeout)
1682 g_source_remove (priv->entry_menu_popdown_timeout);
1683 priv->entry_menu_popdown_timeout = 0;
1686 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data),
1687 GTK_ENTRY (entry)->editing_canceled);
1688 if (GTK_ENTRY (entry)->editing_canceled)
1691 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1692 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1694 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1698 popdown_timeout (gpointer data)
1700 GtkCellRendererTextPrivate *priv;
1702 GDK_THREADS_ENTER ();
1704 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1706 priv->entry_menu_popdown_timeout = 0;
1708 if (!GTK_WIDGET_HAS_FOCUS (priv->entry))
1709 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1711 GDK_THREADS_LEAVE ();
1717 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1720 GtkCellRendererTextPrivate *priv;
1722 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1724 priv->in_entry_menu = FALSE;
1726 if (priv->entry_menu_popdown_timeout)
1729 priv->entry_menu_popdown_timeout = g_timeout_add (500, popdown_timeout,
1734 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1738 GtkCellRendererTextPrivate *priv;
1740 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1742 if (priv->entry_menu_popdown_timeout)
1744 g_source_remove (priv->entry_menu_popdown_timeout);
1745 priv->entry_menu_popdown_timeout = 0;
1748 priv->in_entry_menu = TRUE;
1750 g_signal_connect (menu, "unmap",
1751 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1755 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1759 GtkCellRendererTextPrivate *priv;
1761 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1763 if (priv->in_entry_menu)
1766 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (entry), data);
1768 /* entry needs focus-out-event */
1772 static GtkCellEditable *
1773 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1777 GdkRectangle *background_area,
1778 GdkRectangle *cell_area,
1779 GtkCellRendererState flags)
1781 GtkCellRendererText *celltext;
1782 GtkCellRendererTextPrivate *priv;
1784 celltext = GTK_CELL_RENDERER_TEXT (cell);
1785 priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1787 /* If the cell isn't editable we return NULL. */
1788 if (celltext->editable == FALSE)
1791 priv->entry = g_object_new (GTK_TYPE_ENTRY,
1793 "xalign", cell->xalign,
1797 gtk_entry_set_text (GTK_ENTRY (priv->entry), celltext->text);
1798 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1800 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1803 priv->in_entry_menu = FALSE;
1804 if (priv->entry_menu_popdown_timeout)
1806 g_source_remove (priv->entry_menu_popdown_timeout);
1807 priv->entry_menu_popdown_timeout = 0;
1810 g_signal_connect (priv->entry,
1812 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1814 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus_out_event",
1815 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1817 priv->populate_popup_id =
1818 g_signal_connect (priv->entry, "populate_popup",
1819 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1822 gtk_widget_show (priv->entry);
1824 return GTK_CELL_EDITABLE (priv->entry);
1828 * gtk_cell_renderer_text_set_fixed_height_from_font:
1829 * @renderer: A #GtkCellRendererText
1830 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1832 * Sets the height of a renderer to explicitly be determined by the "font" and
1833 * "y_pad" property set on it. Further changes in these properties do not
1834 * affect the height, so they must be accompanied by a subsequent call to this
1835 * function. Using this function is unflexible, and should really only be used
1836 * if calculating the size of a cell is too slow (ie, a massive number of cells
1837 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1838 * the height is determined by the properties again.
1841 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1842 gint number_of_rows)
1844 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1845 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1847 if (number_of_rows == -1)
1849 gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1850 GTK_CELL_RENDERER (renderer)->width,
1855 renderer->fixed_height_rows = number_of_rows;
1856 renderer->calc_fixed_height = TRUE;
1860 #define __GTK_CELL_RENDERER_TEXT_C__
1861 #include "gtkaliasdef.c"