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"
26 #include "gtkeditable.h"
28 #include "gtksizerequest.h"
29 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtktreeprivate.h"
36 * SECTION:gtkcellrenderertext
37 * @Short_description: Renders text in a cell
38 * @Title: GtkCellRendererText
40 * A #GtkCellRendererText renders a given text in its cell, using the font, color and
41 * style information provided by its properties. The text will be ellipsized if it is
42 * too long and the #GtkCellRendererText:ellipsize property allows it.
44 * If the #GtkCellRenderer:mode is %GTK_CELL_RENDERER_MODE_EDITABLE,
45 * the #GtkCellRendererText allows to edit its text using an entry.
49 static void gtk_cell_renderer_text_finalize (GObject *object);
51 static void gtk_cell_renderer_text_get_property (GObject *object,
55 static void gtk_cell_renderer_text_set_property (GObject *object,
59 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
62 const GdkRectangle *background_area,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
66 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
70 const GdkRectangle *background_area,
71 const GdkRectangle *cell_area,
72 GtkCellRendererState flags);
74 static void gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
78 static void gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
82 static void gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
86 gint *natural_height);
87 static void gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
89 GtkCellRendererState flags,
90 const GdkRectangle *cell_area,
91 GdkRectangle *aligned_area);
106 PROP_SINGLE_PARAGRAPH_MODE,
108 PROP_MAX_WIDTH_CHARS,
117 PROP_BACKGROUND_RGBA,
118 PROP_FOREGROUND_RGBA,
137 /* Whether-a-style-arg-is-set args */
148 PROP_STRIKETHROUGH_SET,
156 static guint text_cell_renderer_signals [LAST_SIGNAL];
158 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
160 struct _GtkCellRendererTextPrivate
164 PangoAlignment align;
165 PangoAttrList *extra_attrs;
168 PangoEllipsizeMode ellipsize;
169 PangoFontDescription *font;
170 PangoLanguage *language;
171 PangoUnderline underline_style;
172 PangoWrapMode wrap_mode;
174 gboolean in_entry_menu;
181 gint fixed_height_rows;
183 gint max_width_chars;
186 guint strikethrough : 1;
189 guint foreground_set : 1;
190 guint background_set : 1;
191 guint underline_set : 1;
193 guint strikethrough_set : 1;
194 guint editable_set : 1;
195 guint calc_fixed_height : 1;
196 guint single_paragraph : 1;
197 guint language_set : 1;
198 guint markup_set : 1;
199 guint ellipsize_set : 1;
203 gulong populate_popup_id;
204 gulong entry_menu_popdown_timeout;
207 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
210 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
212 GtkCellRendererTextPrivate *priv;
213 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
215 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
216 GTK_TYPE_CELL_RENDERER_TEXT,
217 GtkCellRendererTextPrivate);
218 priv = celltext->priv;
220 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
221 gtk_cell_renderer_set_padding (cell, 2, 2);
222 priv->font_scale = 1.0;
223 priv->fixed_height_rows = -1;
224 priv->font = pango_font_description_new ();
226 priv->width_chars = -1;
227 priv->max_width_chars = -1;
228 priv->wrap_width = -1;
229 priv->wrap_mode = PANGO_WRAP_CHAR;
230 priv->align = PANGO_ALIGN_LEFT;
231 priv->align_set = FALSE;
235 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
237 GObjectClass *object_class = G_OBJECT_CLASS (class);
238 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
240 object_class->finalize = gtk_cell_renderer_text_finalize;
242 object_class->get_property = gtk_cell_renderer_text_get_property;
243 object_class->set_property = gtk_cell_renderer_text_set_property;
245 cell_class->render = gtk_cell_renderer_text_render;
246 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
247 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
248 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
249 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
250 cell_class->get_aligned_area = gtk_cell_renderer_text_get_aligned_area;
252 g_object_class_install_property (object_class,
254 g_param_spec_string ("text",
256 P_("Text to render"),
258 GTK_PARAM_READWRITE));
260 g_object_class_install_property (object_class,
262 g_param_spec_string ("markup",
264 P_("Marked up text to render"),
266 GTK_PARAM_WRITABLE));
268 g_object_class_install_property (object_class,
270 g_param_spec_boxed ("attributes",
272 P_("A list of style attributes to apply to the text of the renderer"),
273 PANGO_TYPE_ATTR_LIST,
274 GTK_PARAM_READWRITE));
276 g_object_class_install_property (object_class,
277 PROP_SINGLE_PARAGRAPH_MODE,
278 g_param_spec_boolean ("single-paragraph-mode",
279 P_("Single Paragraph Mode"),
280 P_("Whether to keep all text in a single paragraph"),
282 GTK_PARAM_READWRITE));
285 g_object_class_install_property (object_class,
287 g_param_spec_string ("background",
288 P_("Background color name"),
289 P_("Background color as a string"),
291 GTK_PARAM_WRITABLE));
293 g_object_class_install_property (object_class,
295 g_param_spec_boxed ("background-gdk",
296 P_("Background color"),
297 P_("Background color as a GdkColor"),
299 GTK_PARAM_READWRITE));
302 * GtkCellRendererText:background-rgba:
304 * Background color as a #GdkRGBA
308 g_object_class_install_property (object_class,
309 PROP_BACKGROUND_RGBA,
310 g_param_spec_boxed ("background-rgba",
311 P_("Background color as RGBA"),
312 P_("Background color as a GdkRGBA"),
314 GTK_PARAM_READWRITE));
315 g_object_class_install_property (object_class,
317 g_param_spec_string ("foreground",
318 P_("Foreground color name"),
319 P_("Foreground color as a string"),
321 GTK_PARAM_WRITABLE));
323 g_object_class_install_property (object_class,
325 g_param_spec_boxed ("foreground-gdk",
326 P_("Foreground color"),
327 P_("Foreground color as a GdkColor"),
329 GTK_PARAM_READWRITE));
332 * GtkCellRendererText:foreground-rgba:
334 * Foreground color as a #GdkRGBA
338 g_object_class_install_property (object_class,
339 PROP_FOREGROUND_RGBA,
340 g_param_spec_boxed ("foreground-rgba",
341 P_("Foreground color as RGBA"),
342 P_("Foreground color as a GdkRGBA"),
344 GTK_PARAM_READWRITE));
347 g_object_class_install_property (object_class,
349 g_param_spec_boolean ("editable",
351 P_("Whether the text can be modified by the user"),
353 GTK_PARAM_READWRITE));
355 g_object_class_install_property (object_class,
357 g_param_spec_string ("font",
359 P_("Font description as a string, e.g. \"Sans Italic 12\""),
361 GTK_PARAM_READWRITE));
363 g_object_class_install_property (object_class,
365 g_param_spec_boxed ("font-desc",
367 P_("Font description as a PangoFontDescription struct"),
368 PANGO_TYPE_FONT_DESCRIPTION,
369 GTK_PARAM_READWRITE));
372 g_object_class_install_property (object_class,
374 g_param_spec_string ("family",
376 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
378 GTK_PARAM_READWRITE));
380 g_object_class_install_property (object_class,
382 g_param_spec_enum ("style",
387 GTK_PARAM_READWRITE));
389 g_object_class_install_property (object_class,
391 g_param_spec_enum ("variant",
395 PANGO_VARIANT_NORMAL,
396 GTK_PARAM_READWRITE));
398 g_object_class_install_property (object_class,
400 g_param_spec_int ("weight",
406 GTK_PARAM_READWRITE));
408 g_object_class_install_property (object_class,
410 g_param_spec_enum ("stretch",
414 PANGO_STRETCH_NORMAL,
415 GTK_PARAM_READWRITE));
417 g_object_class_install_property (object_class,
419 g_param_spec_int ("size",
425 GTK_PARAM_READWRITE));
427 g_object_class_install_property (object_class,
429 g_param_spec_double ("size-points",
431 P_("Font size in points"),
435 GTK_PARAM_READWRITE));
437 g_object_class_install_property (object_class,
439 g_param_spec_double ("scale",
441 P_("Font scaling factor"),
445 GTK_PARAM_READWRITE));
447 g_object_class_install_property (object_class,
449 g_param_spec_int ("rise",
451 P_("Offset of text above the baseline "
452 "(below the baseline if rise is negative)"),
456 GTK_PARAM_READWRITE));
459 g_object_class_install_property (object_class,
461 g_param_spec_boolean ("strikethrough",
463 P_("Whether to strike through the text"),
465 GTK_PARAM_READWRITE));
467 g_object_class_install_property (object_class,
469 g_param_spec_enum ("underline",
471 P_("Style of underline for this text"),
472 PANGO_TYPE_UNDERLINE,
473 PANGO_UNDERLINE_NONE,
474 GTK_PARAM_READWRITE));
476 g_object_class_install_property (object_class,
478 g_param_spec_string ("language",
480 P_("The language this text is in, as an ISO code. "
481 "Pango can use this as a hint when rendering the text. "
482 "If you don't understand this parameter, you probably don't need it"),
484 GTK_PARAM_READWRITE));
488 * GtkCellRendererText:ellipsize:
490 * Specifies the preferred place to ellipsize the string, if the cell renderer
491 * does not have enough room to display the entire string. Setting it to
492 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
493 * for another way of making the text fit into a given width.
497 g_object_class_install_property (object_class,
499 g_param_spec_enum ("ellipsize",
501 P_("The preferred place to ellipsize the string, "
502 "if the cell renderer does not have enough room "
503 "to display the entire string"),
504 PANGO_TYPE_ELLIPSIZE_MODE,
505 PANGO_ELLIPSIZE_NONE,
506 GTK_PARAM_READWRITE));
509 * GtkCellRendererText:width-chars:
511 * The desired width of the cell, in characters. If this property is set to
512 * -1, the width will be calculated automatically, otherwise the cell will
513 * request either 3 characters or the property value, whichever is greater.
517 g_object_class_install_property (object_class,
519 g_param_spec_int ("width-chars",
520 P_("Width In Characters"),
521 P_("The desired width of the label, in characters"),
525 GTK_PARAM_READWRITE));
529 * GtkCellRendererText:max-width-chars:
531 * The desired maximum width of the cell, in characters. If this property
532 * is set to -1, the width will be calculated automatically.
534 * For cell renderers that ellipsize or wrap text; this property
535 * controls the maximum reported width of the cell. The
536 * cell should not receive any greater allocation unless it is
537 * set to expand in its #GtkCellLayout and all of the cell's siblings
538 * have received their natural width.
542 g_object_class_install_property (object_class,
543 PROP_MAX_WIDTH_CHARS,
544 g_param_spec_int ("max-width-chars",
545 P_("Maximum Width In Characters"),
546 P_("The maximum width of the cell, in characters"),
550 GTK_PARAM_READWRITE));
553 * GtkCellRendererText:wrap-mode:
555 * Specifies how to break the string into multiple lines, if the cell
556 * renderer does not have enough room to display the entire string.
557 * This property has no effect unless the wrap-width property is set.
561 g_object_class_install_property (object_class,
563 g_param_spec_enum ("wrap-mode",
565 P_("How to break the string into multiple lines, "
566 "if the cell renderer does not have enough room "
567 "to display the entire string"),
568 PANGO_TYPE_WRAP_MODE,
570 GTK_PARAM_READWRITE));
573 * GtkCellRendererText:wrap-width:
575 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
576 * be used to influence at what character positions the line breaks can be placed.
577 * Setting wrap-width to -1 turns wrapping off.
581 g_object_class_install_property (object_class,
583 g_param_spec_int ("wrap-width",
585 P_("The width at which the text is wrapped"),
589 GTK_PARAM_READWRITE));
592 * GtkCellRendererText:alignment:
594 * Specifies how to align the lines of text with respect to each other.
596 * Note that this property describes how to align the lines of text in
597 * case there are several of them. The "xalign" property of #GtkCellRenderer,
598 * on the other hand, sets the horizontal alignment of the whole text.
602 g_object_class_install_property (object_class,
604 g_param_spec_enum ("alignment",
606 P_("How to align the lines"),
607 PANGO_TYPE_ALIGNMENT,
609 GTK_PARAM_READWRITE));
613 /* Style props are set or not */
615 #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))
617 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
618 P_("Background set"),
619 P_("Whether this tag affects the background color"));
621 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
622 P_("Foreground set"),
623 P_("Whether this tag affects the foreground color"));
625 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
626 P_("Editability set"),
627 P_("Whether this tag affects text editability"));
629 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
630 P_("Font family set"),
631 P_("Whether this tag affects the font family"));
633 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
634 P_("Font style set"),
635 P_("Whether this tag affects the font style"));
637 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
638 P_("Font variant set"),
639 P_("Whether this tag affects the font variant"));
641 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
642 P_("Font weight set"),
643 P_("Whether this tag affects the font weight"));
645 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
646 P_("Font stretch set"),
647 P_("Whether this tag affects the font stretch"));
649 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
651 P_("Whether this tag affects the font size"));
653 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
654 P_("Font scale set"),
655 P_("Whether this tag scales the font size by a factor"));
657 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
659 P_("Whether this tag affects the rise"));
661 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
662 P_("Strikethrough set"),
663 P_("Whether this tag affects strikethrough"));
665 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
667 P_("Whether this tag affects underlining"));
669 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
671 P_("Whether this tag affects the language the text is rendered as"));
673 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
675 P_("Whether this tag affects the ellipsize mode"));
677 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
679 P_("Whether this tag affects the alignment mode"));
682 * GtkCellRendererText::edited
683 * @renderer: the object which received the signal
684 * @path: the path identifying the edited cell
685 * @new_text: the new text
687 * This signal is emitted after @renderer has been edited.
689 * It is the responsibility of the application to update the model
690 * and store @new_text at the position indicated by @path.
692 text_cell_renderer_signals [EDITED] =
693 g_signal_new (I_("edited"),
694 G_OBJECT_CLASS_TYPE (object_class),
696 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
698 _gtk_marshal_VOID__STRING_STRING,
703 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
707 gtk_cell_renderer_text_finalize (GObject *object)
709 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
710 GtkCellRendererTextPrivate *priv = celltext->priv;
712 pango_font_description_free (priv->font);
716 if (priv->extra_attrs)
717 pango_attr_list_unref (priv->extra_attrs);
720 g_object_unref (priv->language);
722 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
726 get_property_font_set_mask (guint prop_id)
730 case PROP_FAMILY_SET:
731 return PANGO_FONT_MASK_FAMILY;
733 return PANGO_FONT_MASK_STYLE;
734 case PROP_VARIANT_SET:
735 return PANGO_FONT_MASK_VARIANT;
736 case PROP_WEIGHT_SET:
737 return PANGO_FONT_MASK_WEIGHT;
738 case PROP_STRETCH_SET:
739 return PANGO_FONT_MASK_STRETCH;
741 return PANGO_FONT_MASK_SIZE;
748 gtk_cell_renderer_text_get_property (GObject *object,
753 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
754 GtkCellRendererTextPrivate *priv = celltext->priv;
759 g_value_set_string (value, priv->text);
762 case PROP_ATTRIBUTES:
763 g_value_set_boxed (value, priv->extra_attrs);
766 case PROP_SINGLE_PARAGRAPH_MODE:
767 g_value_set_boolean (value, priv->single_paragraph);
770 case PROP_BACKGROUND_GDK:
774 color.red = (guint16) (priv->background.red * 65535);
775 color.green = (guint16) (priv->background.green * 65535);
776 color.blue = (guint16) (priv->background.blue * 65535);
778 g_value_set_boxed (value, &color);
782 case PROP_FOREGROUND_GDK:
786 color.red = (guint16) (priv->foreground.red * 65535);
787 color.green = (guint16) (priv->foreground.green * 65535);
788 color.blue = (guint16) (priv->foreground.blue * 65535);
790 g_value_set_boxed (value, &color);
794 case PROP_BACKGROUND_RGBA:
795 g_value_set_boxed (value, &priv->background);
798 case PROP_FOREGROUND_RGBA:
799 g_value_set_boxed (value, &priv->foreground);
803 g_value_take_string (value, pango_font_description_to_string (priv->font));
807 g_value_set_boxed (value, priv->font);
811 g_value_set_string (value, pango_font_description_get_family (priv->font));
815 g_value_set_enum (value, pango_font_description_get_style (priv->font));
819 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
823 g_value_set_int (value, pango_font_description_get_weight (priv->font));
827 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
831 g_value_set_int (value, pango_font_description_get_size (priv->font));
834 case PROP_SIZE_POINTS:
835 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
839 g_value_set_double (value, priv->font_scale);
843 g_value_set_boolean (value, priv->editable);
846 case PROP_STRIKETHROUGH:
847 g_value_set_boolean (value, priv->strikethrough);
851 g_value_set_enum (value, priv->underline_style);
855 g_value_set_int (value, priv->rise);
859 g_value_set_static_string (value, pango_language_to_string (priv->language));
863 g_value_set_enum (value, priv->ellipsize);
867 g_value_set_enum (value, priv->wrap_mode);
870 case PROP_WRAP_WIDTH:
871 g_value_set_int (value, priv->wrap_width);
875 g_value_set_enum (value, priv->align);
878 case PROP_BACKGROUND_SET:
879 g_value_set_boolean (value, priv->background_set);
882 case PROP_FOREGROUND_SET:
883 g_value_set_boolean (value, priv->foreground_set);
886 case PROP_FAMILY_SET:
888 case PROP_VARIANT_SET:
889 case PROP_WEIGHT_SET:
890 case PROP_STRETCH_SET:
893 PangoFontMask mask = get_property_font_set_mask (param_id);
894 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
900 g_value_set_boolean (value, priv->scale_set);
903 case PROP_EDITABLE_SET:
904 g_value_set_boolean (value, priv->editable_set);
907 case PROP_STRIKETHROUGH_SET:
908 g_value_set_boolean (value, priv->strikethrough_set);
911 case PROP_UNDERLINE_SET:
912 g_value_set_boolean (value, priv->underline_set);
916 g_value_set_boolean (value, priv->rise_set);
919 case PROP_LANGUAGE_SET:
920 g_value_set_boolean (value, priv->language_set);
923 case PROP_ELLIPSIZE_SET:
924 g_value_set_boolean (value, priv->ellipsize_set);
928 g_value_set_boolean (value, priv->align_set);
931 case PROP_WIDTH_CHARS:
932 g_value_set_int (value, priv->width_chars);
935 case PROP_MAX_WIDTH_CHARS:
936 g_value_set_int (value, priv->max_width_chars);
939 case PROP_BACKGROUND:
940 case PROP_FOREGROUND:
943 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
950 set_bg_color (GtkCellRendererText *celltext,
953 GtkCellRendererTextPrivate *priv = celltext->priv;
957 if (!priv->background_set)
959 priv->background_set = TRUE;
960 g_object_notify (G_OBJECT (celltext), "background-set");
963 priv->background = *rgba;
967 if (priv->background_set)
969 priv->background_set = FALSE;
970 g_object_notify (G_OBJECT (celltext), "background-set");
977 set_fg_color (GtkCellRendererText *celltext,
980 GtkCellRendererTextPrivate *priv = celltext->priv;
984 if (!priv->foreground_set)
986 priv->foreground_set = TRUE;
987 g_object_notify (G_OBJECT (celltext), "foreground-set");
990 priv->foreground = *rgba;
994 if (priv->foreground_set)
996 priv->foreground_set = FALSE;
997 g_object_notify (G_OBJECT (celltext), "foreground-set");
1002 static PangoFontMask
1003 set_font_desc_fields (PangoFontDescription *desc,
1004 PangoFontMask to_set)
1006 PangoFontMask changed_mask = 0;
1008 if (to_set & PANGO_FONT_MASK_FAMILY)
1010 const char *family = pango_font_description_get_family (desc);
1014 changed_mask |= PANGO_FONT_MASK_FAMILY;
1017 pango_font_description_set_family (desc, family);
1019 if (to_set & PANGO_FONT_MASK_STYLE)
1020 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1021 if (to_set & PANGO_FONT_MASK_VARIANT)
1022 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1023 if (to_set & PANGO_FONT_MASK_WEIGHT)
1024 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1025 if (to_set & PANGO_FONT_MASK_STRETCH)
1026 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1027 if (to_set & PANGO_FONT_MASK_SIZE)
1029 gint size = pango_font_description_get_size (desc);
1032 size = 10 * PANGO_SCALE;
1033 changed_mask |= PANGO_FONT_MASK_SIZE;
1036 pango_font_description_set_size (desc, size);
1039 return changed_mask;
1043 notify_set_changed (GObject *object,
1044 PangoFontMask changed_mask)
1046 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1047 g_object_notify (object, "family-set");
1048 if (changed_mask & PANGO_FONT_MASK_STYLE)
1049 g_object_notify (object, "style-set");
1050 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1051 g_object_notify (object, "variant-set");
1052 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1053 g_object_notify (object, "weight-set");
1054 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1055 g_object_notify (object, "stretch-set");
1056 if (changed_mask & PANGO_FONT_MASK_SIZE)
1057 g_object_notify (object, "size-set");
1061 notify_fields_changed (GObject *object,
1062 PangoFontMask changed_mask)
1064 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1065 g_object_notify (object, "family");
1066 if (changed_mask & PANGO_FONT_MASK_STYLE)
1067 g_object_notify (object, "style");
1068 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1069 g_object_notify (object, "variant");
1070 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1071 g_object_notify (object, "weight");
1072 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1073 g_object_notify (object, "stretch");
1074 if (changed_mask & PANGO_FONT_MASK_SIZE)
1075 g_object_notify (object, "size");
1079 set_font_description (GtkCellRendererText *celltext,
1080 PangoFontDescription *font_desc)
1082 GtkCellRendererTextPrivate *priv = celltext->priv;
1083 GObject *object = G_OBJECT (celltext);
1084 PangoFontDescription *new_font_desc;
1085 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1088 new_font_desc = pango_font_description_copy (font_desc);
1090 new_font_desc = pango_font_description_new ();
1092 old_mask = pango_font_description_get_set_fields (priv->font);
1093 new_mask = pango_font_description_get_set_fields (new_font_desc);
1095 changed_mask = old_mask | new_mask;
1096 set_changed_mask = old_mask ^ new_mask;
1098 pango_font_description_free (priv->font);
1099 priv->font = new_font_desc;
1101 g_object_freeze_notify (object);
1103 g_object_notify (object, "font-desc");
1104 g_object_notify (object, "font");
1106 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1107 g_object_notify (object, "family");
1108 if (changed_mask & PANGO_FONT_MASK_STYLE)
1109 g_object_notify (object, "style");
1110 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1111 g_object_notify (object, "variant");
1112 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1113 g_object_notify (object, "weight");
1114 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1115 g_object_notify (object, "stretch");
1116 if (changed_mask & PANGO_FONT_MASK_SIZE)
1118 g_object_notify (object, "size");
1119 g_object_notify (object, "size-points");
1122 notify_set_changed (object, set_changed_mask);
1124 g_object_thaw_notify (object);
1128 gtk_cell_renderer_text_set_property (GObject *object,
1130 const GValue *value,
1133 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1134 GtkCellRendererTextPrivate *priv = celltext->priv;
1139 g_free (priv->text);
1141 if (priv->markup_set)
1143 if (priv->extra_attrs)
1144 pango_attr_list_unref (priv->extra_attrs);
1145 priv->extra_attrs = NULL;
1146 priv->markup_set = FALSE;
1149 priv->text = g_value_dup_string (value);
1150 g_object_notify (object, "text");
1153 case PROP_ATTRIBUTES:
1154 if (priv->extra_attrs)
1155 pango_attr_list_unref (priv->extra_attrs);
1157 priv->extra_attrs = g_value_get_boxed (value);
1158 if (priv->extra_attrs)
1159 pango_attr_list_ref (priv->extra_attrs);
1165 GError *error = NULL;
1166 PangoAttrList *attrs = NULL;
1168 str = g_value_get_string (value);
1169 if (str && !pango_parse_markup (str,
1177 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1179 g_error_free (error);
1183 g_free (priv->text);
1185 if (priv->extra_attrs)
1186 pango_attr_list_unref (priv->extra_attrs);
1189 priv->extra_attrs = attrs;
1190 priv->markup_set = TRUE;
1194 case PROP_SINGLE_PARAGRAPH_MODE:
1195 priv->single_paragraph = g_value_get_boolean (value);
1198 case PROP_BACKGROUND:
1202 if (!g_value_get_string (value))
1203 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1204 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1205 set_bg_color (celltext, &rgba);
1207 g_warning ("Don't know color `%s'", g_value_get_string (value));
1209 g_object_notify (object, "background-gdk");
1213 case PROP_FOREGROUND:
1217 if (!g_value_get_string (value))
1218 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1219 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1220 set_fg_color (celltext, &rgba);
1222 g_warning ("Don't know color `%s'", g_value_get_string (value));
1224 g_object_notify (object, "foreground-gdk");
1228 case PROP_BACKGROUND_GDK:
1232 color = g_value_get_boxed (value);
1237 rgba.red = color->red / 65535.;
1238 rgba.green = color->green / 65535.;
1239 rgba.blue = color->blue / 65535.;
1242 set_bg_color (celltext, &rgba);
1246 set_bg_color (celltext, NULL);
1251 case PROP_FOREGROUND_GDK:
1255 color = g_value_get_boxed (value);
1260 rgba.red = color->red / 65535.;
1261 rgba.green = color->green / 65535.;
1262 rgba.blue = color->blue / 65535.;
1265 set_fg_color (celltext, &rgba);
1269 set_fg_color (celltext, NULL);
1274 case PROP_BACKGROUND_RGBA:
1275 /* This notifies the GObject itself. */
1276 set_bg_color (celltext, g_value_get_boxed (value));
1279 case PROP_FOREGROUND_RGBA:
1280 /* This notifies the GObject itself. */
1281 set_fg_color (celltext, g_value_get_boxed (value));
1286 PangoFontDescription *font_desc = NULL;
1289 name = g_value_get_string (value);
1292 font_desc = pango_font_description_from_string (name);
1294 set_font_description (celltext, font_desc);
1296 pango_font_description_free (font_desc);
1298 if (priv->fixed_height_rows != -1)
1299 priv->calc_fixed_height = TRUE;
1303 case PROP_FONT_DESC:
1304 set_font_description (celltext, g_value_get_boxed (value));
1306 if (priv->fixed_height_rows != -1)
1307 priv->calc_fixed_height = TRUE;
1316 case PROP_SIZE_POINTS:
1318 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1323 pango_font_description_set_family (priv->font,
1324 g_value_get_string (value));
1327 pango_font_description_set_style (priv->font,
1328 g_value_get_enum (value));
1331 pango_font_description_set_variant (priv->font,
1332 g_value_get_enum (value));
1335 pango_font_description_set_weight (priv->font,
1336 g_value_get_int (value));
1339 pango_font_description_set_stretch (priv->font,
1340 g_value_get_enum (value));
1343 pango_font_description_set_size (priv->font,
1344 g_value_get_int (value));
1345 g_object_notify (object, "size-points");
1347 case PROP_SIZE_POINTS:
1348 pango_font_description_set_size (priv->font,
1349 g_value_get_double (value) * PANGO_SCALE);
1350 g_object_notify (object, "size");
1354 if (priv->fixed_height_rows != -1)
1355 priv->calc_fixed_height = TRUE;
1357 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1358 g_object_notify (object, "font-desc");
1359 g_object_notify (object, "font");
1365 priv->font_scale = g_value_get_double (value);
1366 priv->scale_set = TRUE;
1367 if (priv->fixed_height_rows != -1)
1368 priv->calc_fixed_height = TRUE;
1369 g_object_notify (object, "scale-set");
1373 priv->editable = g_value_get_boolean (value);
1374 priv->editable_set = TRUE;
1376 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1378 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1379 g_object_notify (object, "editable-set");
1382 case PROP_STRIKETHROUGH:
1383 priv->strikethrough = g_value_get_boolean (value);
1384 priv->strikethrough_set = TRUE;
1385 g_object_notify (object, "strikethrough-set");
1388 case PROP_UNDERLINE:
1389 priv->underline_style = g_value_get_enum (value);
1390 priv->underline_set = TRUE;
1391 g_object_notify (object, "underline-set");
1396 priv->rise = g_value_get_int (value);
1397 priv->rise_set = TRUE;
1398 g_object_notify (object, "rise-set");
1399 if (priv->fixed_height_rows != -1)
1400 priv->calc_fixed_height = TRUE;
1404 priv->language_set = TRUE;
1406 g_object_unref (priv->language);
1407 priv->language = pango_language_from_string (g_value_get_string (value));
1408 g_object_notify (object, "language-set");
1411 case PROP_ELLIPSIZE:
1412 priv->ellipsize = g_value_get_enum (value);
1413 priv->ellipsize_set = TRUE;
1414 g_object_notify (object, "ellipsize-set");
1417 case PROP_WRAP_MODE:
1418 priv->wrap_mode = g_value_get_enum (value);
1421 case PROP_WRAP_WIDTH:
1422 priv->wrap_width = g_value_get_int (value);
1425 case PROP_WIDTH_CHARS:
1426 priv->width_chars = g_value_get_int (value);
1429 case PROP_MAX_WIDTH_CHARS:
1430 priv->max_width_chars = g_value_get_int (value);
1434 priv->align = g_value_get_enum (value);
1435 priv->align_set = TRUE;
1436 g_object_notify (object, "align-set");
1439 case PROP_BACKGROUND_SET:
1440 priv->background_set = g_value_get_boolean (value);
1443 case PROP_FOREGROUND_SET:
1444 priv->foreground_set = g_value_get_boolean (value);
1447 case PROP_FAMILY_SET:
1448 case PROP_STYLE_SET:
1449 case PROP_VARIANT_SET:
1450 case PROP_WEIGHT_SET:
1451 case PROP_STRETCH_SET:
1453 if (!g_value_get_boolean (value))
1455 pango_font_description_unset_fields (priv->font,
1456 get_property_font_set_mask (param_id));
1460 PangoFontMask changed_mask;
1462 changed_mask = set_font_desc_fields (priv->font,
1463 get_property_font_set_mask (param_id));
1464 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1468 case PROP_SCALE_SET:
1469 priv->scale_set = g_value_get_boolean (value);
1472 case PROP_EDITABLE_SET:
1473 priv->editable_set = g_value_get_boolean (value);
1476 case PROP_STRIKETHROUGH_SET:
1477 priv->strikethrough_set = g_value_get_boolean (value);
1480 case PROP_UNDERLINE_SET:
1481 priv->underline_set = g_value_get_boolean (value);
1485 priv->rise_set = g_value_get_boolean (value);
1488 case PROP_LANGUAGE_SET:
1489 priv->language_set = g_value_get_boolean (value);
1492 case PROP_ELLIPSIZE_SET:
1493 priv->ellipsize_set = g_value_get_boolean (value);
1496 case PROP_ALIGN_SET:
1497 priv->align_set = g_value_get_boolean (value);
1501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1507 * gtk_cell_renderer_text_new:
1509 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1510 * object properties. Object properties can be
1511 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1512 * you can bind a property to a value in a #GtkTreeModel. For example,
1513 * you can bind the "text" property on the cell renderer to a string
1514 * value in the model, thus rendering a different string in each row
1515 * of the #GtkTreeView
1517 * Return value: the new cell renderer
1520 gtk_cell_renderer_text_new (void)
1522 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1526 add_attr (PangoAttrList *attr_list,
1527 PangoAttribute *attr)
1529 attr->start_index = 0;
1530 attr->end_index = G_MAXINT;
1532 pango_attr_list_insert (attr_list, attr);
1536 get_layout (GtkCellRendererText *celltext,
1538 const GdkRectangle *cell_area,
1539 GtkCellRendererState flags)
1541 GtkCellRendererTextPrivate *priv = celltext->priv;
1542 PangoAttrList *attr_list;
1543 PangoLayout *layout;
1544 PangoUnderline uline;
1547 layout = gtk_widget_create_pango_layout (widget, priv->text);
1549 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1551 if (priv->extra_attrs)
1552 attr_list = pango_attr_list_copy (priv->extra_attrs);
1554 attr_list = pango_attr_list_new ();
1556 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1560 /* Add options that affect appearance but not size */
1562 /* note that background doesn't go here, since it affects
1563 * background_area not the PangoLayout area
1566 if (priv->foreground_set
1567 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1571 color.red = (guint16) (priv->foreground.red * 65535);
1572 color.green = (guint16) (priv->foreground.green * 65535);
1573 color.blue = (guint16) (priv->foreground.blue * 65535);
1575 add_attr (attr_list,
1576 pango_attr_foreground_new (color.red, color.green, color.blue));
1579 if (priv->strikethrough_set)
1580 add_attr (attr_list,
1581 pango_attr_strikethrough_new (priv->strikethrough));
1584 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1586 if (priv->scale_set &&
1587 priv->font_scale != 1.0)
1588 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1590 if (priv->underline_set)
1591 uline = priv->underline_style;
1593 uline = PANGO_UNDERLINE_NONE;
1595 if (priv->language_set)
1596 add_attr (attr_list, pango_attr_language_new (priv->language));
1598 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1602 case PANGO_UNDERLINE_NONE:
1603 uline = PANGO_UNDERLINE_SINGLE;
1606 case PANGO_UNDERLINE_SINGLE:
1607 uline = PANGO_UNDERLINE_DOUBLE;
1615 if (uline != PANGO_UNDERLINE_NONE)
1616 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1619 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1621 /* Now apply the attributes as they will effect the outcome
1622 * of pango_layout_get_extents() */
1623 pango_layout_set_attributes (layout, attr_list);
1624 pango_attr_list_unref (attr_list);
1626 if (priv->ellipsize_set)
1627 pango_layout_set_ellipsize (layout, priv->ellipsize);
1629 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1631 if (priv->wrap_width != -1)
1633 PangoRectangle rect;
1634 gint width, text_width;
1636 pango_layout_get_extents (layout, NULL, &rect);
1637 text_width = rect.width;
1640 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1642 width = priv->wrap_width * PANGO_SCALE;
1644 width = MIN (width, text_width);
1646 pango_layout_set_width (layout, width);
1647 pango_layout_set_wrap (layout, priv->wrap_mode);
1651 pango_layout_set_width (layout, -1);
1652 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1655 if (priv->align_set)
1656 pango_layout_set_alignment (layout, priv->align);
1659 PangoAlignment align;
1661 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1662 align = PANGO_ALIGN_RIGHT;
1664 align = PANGO_ALIGN_LEFT;
1666 pango_layout_set_alignment (layout, align);
1674 get_size (GtkCellRenderer *cell,
1676 const GdkRectangle *cell_area,
1677 PangoLayout *layout,
1683 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1684 GtkCellRendererTextPrivate *priv = celltext->priv;
1685 PangoRectangle rect;
1687 gint cell_width, cell_height;
1689 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1691 if (priv->calc_fixed_height)
1693 PangoContext *context;
1694 PangoFontMetrics *metrics;
1695 PangoFontDescription *font_desc;
1698 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1699 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1701 if (priv->scale_set)
1702 pango_font_description_set_size (font_desc,
1703 priv->font_scale * pango_font_description_get_size (font_desc));
1705 context = gtk_widget_get_pango_context (widget);
1707 metrics = pango_context_get_metrics (context,
1709 pango_context_get_language (context));
1710 row_height = (pango_font_metrics_get_ascent (metrics) +
1711 pango_font_metrics_get_descent (metrics));
1712 pango_font_metrics_unref (metrics);
1714 pango_font_description_free (font_desc);
1716 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1718 gtk_cell_renderer_set_fixed_size (cell,
1719 cell_width, 2 * ypad +
1720 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1724 *height = cell_height;
1727 priv->calc_fixed_height = FALSE;
1733 g_object_ref (layout);
1735 layout = get_layout (celltext, widget, NULL, 0);
1737 pango_layout_get_pixel_extents (layout, NULL, &rect);
1741 gfloat xalign, yalign;
1743 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1745 rect.height = MIN (rect.height, cell_area->height - 2 * ypad);
1746 rect.width = MIN (rect.width, cell_area->width - (2 * xpad) - rect.x);
1750 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1751 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1753 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1755 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1756 *x_offset = MAX(*x_offset, 0);
1760 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1761 *y_offset = MAX (*y_offset, 0);
1766 if (x_offset) *x_offset = 0;
1767 if (y_offset) *y_offset = 0;
1771 *height = ypad * 2 + rect.height;
1774 *width = xpad * 2 + rect.x + rect.width;
1776 g_object_unref (layout);
1780 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1783 const GdkRectangle *background_area,
1784 const GdkRectangle *cell_area,
1785 GtkCellRendererState flags)
1788 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1789 GtkCellRendererTextPrivate *priv = celltext->priv;
1790 PangoLayout *layout;
1796 layout = get_layout (celltext, widget, cell_area, flags);
1797 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1799 if (!gtk_cell_renderer_get_sensitive (cell))
1801 state = GTK_STATE_INSENSITIVE;
1803 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1805 if (gtk_widget_has_focus (widget))
1806 state = GTK_STATE_SELECTED;
1808 state = GTK_STATE_ACTIVE;
1810 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1811 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1813 state = GTK_STATE_PRELIGHT;
1817 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1818 state = GTK_STATE_INSENSITIVE;
1820 state = GTK_STATE_NORMAL;
1823 if (priv->background_set &&
1824 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1826 gdk_cairo_rectangle (cr, background_area);
1827 gdk_cairo_set_source_rgba (cr, &priv->background);
1831 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1833 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1834 pango_layout_set_width (layout,
1835 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1836 else if (priv->wrap_width == -1)
1837 pango_layout_set_width (layout, -1);
1841 gdk_cairo_rectangle (cr, cell_area);
1844 gtk_paint_layout (gtk_widget_get_style (widget),
1850 cell_area->x + x_offset + xpad,
1851 cell_area->y + y_offset + ypad,
1856 g_object_unref (layout);
1860 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1863 GtkCellRendererTextPrivate *priv;
1865 const gchar *new_text;
1868 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1872 if (priv->focus_out_id > 0)
1874 g_signal_handler_disconnect (entry, priv->focus_out_id);
1875 priv->focus_out_id = 0;
1878 if (priv->populate_popup_id > 0)
1880 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1881 priv->populate_popup_id = 0;
1884 if (priv->entry_menu_popdown_timeout)
1886 g_source_remove (priv->entry_menu_popdown_timeout);
1887 priv->entry_menu_popdown_timeout = 0;
1890 g_object_get (entry,
1891 "editing-canceled", &canceled,
1893 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1898 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1899 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1901 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1905 popdown_timeout (gpointer data)
1907 GtkCellRendererTextPrivate *priv;
1909 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1911 priv->entry_menu_popdown_timeout = 0;
1913 if (!gtk_widget_has_focus (priv->entry))
1914 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1920 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1923 GtkCellRendererTextPrivate *priv;
1925 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1927 priv->in_entry_menu = FALSE;
1929 if (priv->entry_menu_popdown_timeout)
1932 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1937 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1941 GtkCellRendererTextPrivate *priv;
1943 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1945 if (priv->entry_menu_popdown_timeout)
1947 g_source_remove (priv->entry_menu_popdown_timeout);
1948 priv->entry_menu_popdown_timeout = 0;
1951 priv->in_entry_menu = TRUE;
1953 g_signal_connect (menu, "unmap",
1954 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1958 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1962 GtkCellRendererTextPrivate *priv;
1964 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1966 if (priv->in_entry_menu)
1969 g_object_set (entry,
1970 "editing-canceled", TRUE,
1972 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1973 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1975 /* entry needs focus-out-event */
1979 static GtkCellEditable *
1980 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1984 const GdkRectangle *background_area,
1985 const GdkRectangle *cell_area,
1986 GtkCellRendererState flags)
1988 GtkRequisition requisition;
1989 GtkCellRendererText *celltext;
1990 GtkCellRendererTextPrivate *priv;
1991 gfloat xalign, yalign;
1993 celltext = GTK_CELL_RENDERER_TEXT (cell);
1994 priv = celltext->priv;
1996 /* If the cell isn't editable we return NULL. */
1997 if (priv->editable == FALSE)
2000 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
2002 priv->entry = gtk_entry_new ();
2003 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
2004 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
2007 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
2008 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
2010 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
2012 gtk_widget_get_preferred_size (priv->entry, &requisition, NULL);
2013 if (requisition.height < cell_area->height)
2015 GtkBorder *style_border;
2018 gtk_widget_style_get (priv->entry,
2019 "inner-border", &style_border,
2024 border = *style_border;
2025 g_boxed_free (GTK_TYPE_BORDER, style_border);
2029 /* Since boxed style properties can't have default values ... */
2034 border.top = (cell_area->height - requisition.height) / 2;
2035 border.bottom = (cell_area->height - requisition.height) / 2;
2036 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
2039 priv->in_entry_menu = FALSE;
2040 if (priv->entry_menu_popdown_timeout)
2042 g_source_remove (priv->entry_menu_popdown_timeout);
2043 priv->entry_menu_popdown_timeout = 0;
2046 g_signal_connect (priv->entry,
2048 G_CALLBACK (gtk_cell_renderer_text_editing_done),
2050 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2051 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2053 priv->populate_popup_id =
2054 g_signal_connect (priv->entry, "populate-popup",
2055 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2058 gtk_widget_show (priv->entry);
2060 return GTK_CELL_EDITABLE (priv->entry);
2064 * gtk_cell_renderer_text_set_fixed_height_from_font:
2065 * @renderer: A #GtkCellRendererText
2066 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2068 * Sets the height of a renderer to explicitly be determined by the "font" and
2069 * "y_pad" property set on it. Further changes in these properties do not
2070 * affect the height, so they must be accompanied by a subsequent call to this
2071 * function. Using this function is unflexible, and should really only be used
2072 * if calculating the size of a cell is too slow (ie, a massive number of cells
2073 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
2074 * the height is determined by the properties again.
2077 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2078 gint number_of_rows)
2080 GtkCellRendererTextPrivate *priv;
2081 GtkCellRenderer *cell;
2083 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2084 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2086 cell = GTK_CELL_RENDERER (renderer);
2087 priv = renderer->priv;
2089 if (number_of_rows == -1)
2093 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2094 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2098 priv->fixed_height_rows = number_of_rows;
2099 priv->calc_fixed_height = TRUE;
2104 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2109 GtkCellRendererTextPrivate *priv;
2110 GtkCellRendererText *celltext;
2112 PangoLayout *layout;
2113 PangoContext *context;
2114 PangoFontMetrics *metrics;
2115 PangoRectangle rect;
2116 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, xpad;
2117 gint min_width, nat_width;
2119 /* "width-chars" Hard-coded minimum width:
2120 * - minimum size should be MAX (width-chars, strlen ("..."));
2121 * - natural size should be MAX (width-chars, strlen (label->text));
2123 * "wrap-width" User specified natural wrap width
2124 * - minimum size should be MAX (width-chars, 0)
2125 * - natural size should be MIN (wrap-width, strlen (label->text))
2128 celltext = GTK_CELL_RENDERER_TEXT (cell);
2129 priv = celltext->priv;
2131 style = gtk_widget_get_style (widget);
2133 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2135 layout = get_layout (celltext, widget, NULL, 0);
2137 /* Fetch the length of the complete unwrapped text */
2138 pango_layout_set_width (layout, -1);
2139 pango_layout_get_extents (layout, NULL, &rect);
2140 text_width = rect.width;
2142 /* Fetch the average size of a charachter */
2143 context = pango_layout_get_context (layout);
2144 metrics = pango_context_get_metrics (context, style->font_desc,
2145 pango_context_get_language (context));
2147 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2148 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2149 char_pixels = MAX (char_width, digit_width);
2151 pango_font_metrics_unref (metrics);
2152 g_object_unref (layout);
2154 /* enforce minimum width for ellipsized labels at ~3 chars */
2155 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2156 ellipsize_chars = 3;
2158 ellipsize_chars = 0;
2160 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2163 MIN (PANGO_PIXELS_CEIL (text_width),
2164 (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
2165 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2166 else if (priv->wrap_width > -1)
2167 min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
2169 min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
2171 if (priv->width_chars > 0)
2172 nat_width = xpad * 2 +
2173 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
2175 nat_width = xpad * 2 + PANGO_PIXELS_CEIL (text_width);
2178 nat_width = MAX (nat_width, min_width);
2180 if (priv->max_width_chars > 0)
2182 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2184 min_width = MIN (min_width, max_width);
2185 nat_width = MIN (nat_width, max_width);
2189 *minimum_size = min_width;
2192 *natural_size = nat_width;
2196 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2199 gint *minimum_height,
2200 gint *natural_height)
2202 GtkCellRendererTextPrivate *priv;
2203 GtkCellRendererText *celltext;
2204 PangoLayout *layout;
2205 gint text_height, xpad, ypad;
2208 celltext = GTK_CELL_RENDERER_TEXT (cell);
2209 priv = celltext->priv;
2211 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2213 layout = get_layout (celltext, widget, NULL, 0);
2215 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2216 pango_layout_get_pixel_size (layout, NULL, &text_height);
2219 *minimum_height = text_height + ypad * 2;
2222 *natural_height = text_height + ypad * 2;
2224 g_object_unref (layout);
2228 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2235 /* Thankfully cell renderers dont rotate, so they only have to do
2236 * height-for-width and not the opposite. Here we have only to return
2237 * the height for the base minimum width of the renderer.
2239 * Note this code path wont be followed by GtkTreeView which is
2240 * height-for-width specifically.
2242 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2243 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2244 minimum_size, natural_size);
2248 gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer *cell,
2250 GtkCellRendererState flags,
2251 const GdkRectangle *cell_area,
2252 GdkRectangle *aligned_area)
2254 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
2255 PangoLayout *layout;
2259 layout = get_layout (celltext, widget, cell_area, flags);
2260 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset,
2261 &aligned_area->width, &aligned_area->height);
2263 aligned_area->x = cell_area->x + x_offset;
2264 aligned_area->y = cell_area->y + y_offset;
2266 g_object_unref (layout);