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);
99 PROP_SINGLE_PARAGRAPH_MODE,
101 PROP_MAX_WIDTH_CHARS,
110 PROP_BACKGROUND_RGBA,
111 PROP_FOREGROUND_RGBA,
130 /* Whether-a-style-arg-is-set args */
141 PROP_STRIKETHROUGH_SET,
149 static guint text_cell_renderer_signals [LAST_SIGNAL];
151 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
153 struct _GtkCellRendererTextPrivate
157 PangoAlignment align;
158 PangoAttrList *extra_attrs;
161 PangoEllipsizeMode ellipsize;
162 PangoFontDescription *font;
163 PangoLanguage *language;
164 PangoUnderline underline_style;
165 PangoWrapMode wrap_mode;
167 gboolean in_entry_menu;
174 gint fixed_height_rows;
176 gint max_width_chars;
179 guint strikethrough : 1;
182 guint foreground_set : 1;
183 guint background_set : 1;
184 guint underline_set : 1;
186 guint strikethrough_set : 1;
187 guint editable_set : 1;
188 guint calc_fixed_height : 1;
189 guint single_paragraph : 1;
190 guint language_set : 1;
191 guint markup_set : 1;
192 guint ellipsize_set : 1;
196 gulong populate_popup_id;
197 gulong entry_menu_popdown_timeout;
200 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
203 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
205 GtkCellRendererTextPrivate *priv;
206 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
208 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
209 GTK_TYPE_CELL_RENDERER_TEXT,
210 GtkCellRendererTextPrivate);
211 priv = celltext->priv;
213 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
214 gtk_cell_renderer_set_padding (cell, 2, 2);
215 priv->font_scale = 1.0;
216 priv->fixed_height_rows = -1;
217 priv->font = pango_font_description_new ();
219 priv->width_chars = -1;
220 priv->max_width_chars = -1;
221 priv->wrap_width = -1;
222 priv->wrap_mode = PANGO_WRAP_CHAR;
223 priv->align = PANGO_ALIGN_LEFT;
224 priv->align_set = FALSE;
228 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
230 GObjectClass *object_class = G_OBJECT_CLASS (class);
231 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
233 object_class->finalize = gtk_cell_renderer_text_finalize;
235 object_class->get_property = gtk_cell_renderer_text_get_property;
236 object_class->set_property = gtk_cell_renderer_text_set_property;
238 cell_class->render = gtk_cell_renderer_text_render;
239 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
240 cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
241 cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
242 cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
244 g_object_class_install_property (object_class,
246 g_param_spec_string ("text",
248 P_("Text to render"),
250 GTK_PARAM_READWRITE));
252 g_object_class_install_property (object_class,
254 g_param_spec_string ("markup",
256 P_("Marked up text to render"),
258 GTK_PARAM_WRITABLE));
260 g_object_class_install_property (object_class,
262 g_param_spec_boxed ("attributes",
264 P_("A list of style attributes to apply to the text of the renderer"),
265 PANGO_TYPE_ATTR_LIST,
266 GTK_PARAM_READWRITE));
268 g_object_class_install_property (object_class,
269 PROP_SINGLE_PARAGRAPH_MODE,
270 g_param_spec_boolean ("single-paragraph-mode",
271 P_("Single Paragraph Mode"),
272 P_("Whether to keep all text in a single paragraph"),
274 GTK_PARAM_READWRITE));
277 g_object_class_install_property (object_class,
279 g_param_spec_string ("background",
280 P_("Background color name"),
281 P_("Background color as a string"),
283 GTK_PARAM_WRITABLE));
285 g_object_class_install_property (object_class,
287 g_param_spec_boxed ("background-gdk",
288 P_("Background color"),
289 P_("Background color as a GdkColor"),
291 GTK_PARAM_READWRITE));
294 * GtkCellRendererText:background-rgba:
296 * Background color as a #GdkRGBA
300 g_object_class_install_property (object_class,
301 PROP_BACKGROUND_RGBA,
302 g_param_spec_boxed ("background-rgba",
303 P_("Background color as RGBA"),
304 P_("Background color as a GdkRGBA"),
306 GTK_PARAM_READWRITE));
307 g_object_class_install_property (object_class,
309 g_param_spec_string ("foreground",
310 P_("Foreground color name"),
311 P_("Foreground color as a string"),
313 GTK_PARAM_WRITABLE));
315 g_object_class_install_property (object_class,
317 g_param_spec_boxed ("foreground-gdk",
318 P_("Foreground color"),
319 P_("Foreground color as a GdkColor"),
321 GTK_PARAM_READWRITE));
324 * GtkCellRendererText:foreground-rgba:
326 * Foreground color as a #GdkRGBA
330 g_object_class_install_property (object_class,
331 PROP_FOREGROUND_RGBA,
332 g_param_spec_boxed ("foreground-rgba",
333 P_("Foreground color as RGBA"),
334 P_("Foreground color as a GdkRGBA"),
336 GTK_PARAM_READWRITE));
339 g_object_class_install_property (object_class,
341 g_param_spec_boolean ("editable",
343 P_("Whether the text can be modified by the user"),
345 GTK_PARAM_READWRITE));
347 g_object_class_install_property (object_class,
349 g_param_spec_string ("font",
351 P_("Font description as a string, e.g. \"Sans Italic 12\""),
353 GTK_PARAM_READWRITE));
355 g_object_class_install_property (object_class,
357 g_param_spec_boxed ("font-desc",
359 P_("Font description as a PangoFontDescription struct"),
360 PANGO_TYPE_FONT_DESCRIPTION,
361 GTK_PARAM_READWRITE));
364 g_object_class_install_property (object_class,
366 g_param_spec_string ("family",
368 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
370 GTK_PARAM_READWRITE));
372 g_object_class_install_property (object_class,
374 g_param_spec_enum ("style",
379 GTK_PARAM_READWRITE));
381 g_object_class_install_property (object_class,
383 g_param_spec_enum ("variant",
387 PANGO_VARIANT_NORMAL,
388 GTK_PARAM_READWRITE));
390 g_object_class_install_property (object_class,
392 g_param_spec_int ("weight",
398 GTK_PARAM_READWRITE));
400 g_object_class_install_property (object_class,
402 g_param_spec_enum ("stretch",
406 PANGO_STRETCH_NORMAL,
407 GTK_PARAM_READWRITE));
409 g_object_class_install_property (object_class,
411 g_param_spec_int ("size",
417 GTK_PARAM_READWRITE));
419 g_object_class_install_property (object_class,
421 g_param_spec_double ("size-points",
423 P_("Font size in points"),
427 GTK_PARAM_READWRITE));
429 g_object_class_install_property (object_class,
431 g_param_spec_double ("scale",
433 P_("Font scaling factor"),
437 GTK_PARAM_READWRITE));
439 g_object_class_install_property (object_class,
441 g_param_spec_int ("rise",
443 P_("Offset of text above the baseline "
444 "(below the baseline if rise is negative)"),
448 GTK_PARAM_READWRITE));
451 g_object_class_install_property (object_class,
453 g_param_spec_boolean ("strikethrough",
455 P_("Whether to strike through the text"),
457 GTK_PARAM_READWRITE));
459 g_object_class_install_property (object_class,
461 g_param_spec_enum ("underline",
463 P_("Style of underline for this text"),
464 PANGO_TYPE_UNDERLINE,
465 PANGO_UNDERLINE_NONE,
466 GTK_PARAM_READWRITE));
468 g_object_class_install_property (object_class,
470 g_param_spec_string ("language",
472 P_("The language this text is in, as an ISO code. "
473 "Pango can use this as a hint when rendering the text. "
474 "If you don't understand this parameter, you probably don't need it"),
476 GTK_PARAM_READWRITE));
480 * GtkCellRendererText:ellipsize:
482 * Specifies the preferred place to ellipsize the string, if the cell renderer
483 * does not have enough room to display the entire string. Setting it to
484 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
485 * for another way of making the text fit into a given width.
489 g_object_class_install_property (object_class,
491 g_param_spec_enum ("ellipsize",
493 P_("The preferred place to ellipsize the string, "
494 "if the cell renderer does not have enough room "
495 "to display the entire string"),
496 PANGO_TYPE_ELLIPSIZE_MODE,
497 PANGO_ELLIPSIZE_NONE,
498 GTK_PARAM_READWRITE));
501 * GtkCellRendererText:width-chars:
503 * The desired width of the cell, in characters. If this property is set to
504 * -1, the width will be calculated automatically, otherwise the cell will
505 * request either 3 characters or the property value, whichever is greater.
509 g_object_class_install_property (object_class,
511 g_param_spec_int ("width-chars",
512 P_("Width In Characters"),
513 P_("The desired width of the label, in characters"),
517 GTK_PARAM_READWRITE));
521 * GtkCellRendererText:max-width-chars:
523 * The desired maximum width of the cell, in characters. If this property
524 * is set to -1, the width will be calculated automatically.
526 * For cell renderers that ellipsize or wrap text; this property
527 * controls the maximum reported width of the cell. The
528 * cell should not receive any greater allocation unless it is
529 * set to expand in its #GtkCellLayout and all of the cell's siblings
530 * have received their natural width.
534 g_object_class_install_property (object_class,
535 PROP_MAX_WIDTH_CHARS,
536 g_param_spec_int ("max-width-chars",
537 P_("Maximum Width In Characters"),
538 P_("The maximum width of the cell, in characters"),
542 GTK_PARAM_READWRITE));
545 * GtkCellRendererText:wrap-mode:
547 * Specifies how to break the string into multiple lines, if the cell
548 * renderer does not have enough room to display the entire string.
549 * This property has no effect unless the wrap-width property is set.
553 g_object_class_install_property (object_class,
555 g_param_spec_enum ("wrap-mode",
557 P_("How to break the string into multiple lines, "
558 "if the cell renderer does not have enough room "
559 "to display the entire string"),
560 PANGO_TYPE_WRAP_MODE,
562 GTK_PARAM_READWRITE));
565 * GtkCellRendererText:wrap-width:
567 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
568 * be used to influence at what character positions the line breaks can be placed.
569 * Setting wrap-width to -1 turns wrapping off.
573 g_object_class_install_property (object_class,
575 g_param_spec_int ("wrap-width",
577 P_("The width at which the text is wrapped"),
581 GTK_PARAM_READWRITE));
584 * GtkCellRendererText:alignment:
586 * Specifies how to align the lines of text with respect to each other.
588 * Note that this property describes how to align the lines of text in
589 * case there are several of them. The "xalign" property of #GtkCellRenderer,
590 * on the other hand, sets the horizontal alignment of the whole text.
594 g_object_class_install_property (object_class,
596 g_param_spec_enum ("alignment",
598 P_("How to align the lines"),
599 PANGO_TYPE_ALIGNMENT,
601 GTK_PARAM_READWRITE));
605 /* Style props are set or not */
607 #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))
609 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
610 P_("Background set"),
611 P_("Whether this tag affects the background color"));
613 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
614 P_("Foreground set"),
615 P_("Whether this tag affects the foreground color"));
617 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
618 P_("Editability set"),
619 P_("Whether this tag affects text editability"));
621 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
622 P_("Font family set"),
623 P_("Whether this tag affects the font family"));
625 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
626 P_("Font style set"),
627 P_("Whether this tag affects the font style"));
629 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
630 P_("Font variant set"),
631 P_("Whether this tag affects the font variant"));
633 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
634 P_("Font weight set"),
635 P_("Whether this tag affects the font weight"));
637 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
638 P_("Font stretch set"),
639 P_("Whether this tag affects the font stretch"));
641 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
643 P_("Whether this tag affects the font size"));
645 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
646 P_("Font scale set"),
647 P_("Whether this tag scales the font size by a factor"));
649 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
651 P_("Whether this tag affects the rise"));
653 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
654 P_("Strikethrough set"),
655 P_("Whether this tag affects strikethrough"));
657 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
659 P_("Whether this tag affects underlining"));
661 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
663 P_("Whether this tag affects the language the text is rendered as"));
665 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
667 P_("Whether this tag affects the ellipsize mode"));
669 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
671 P_("Whether this tag affects the alignment mode"));
674 * GtkCellRendererText::edited
675 * @renderer: the object which received the signal
676 * @path: the path identifying the edited cell
677 * @new_text: the new text
679 * This signal is emitted after @renderer has been edited.
681 * It is the responsibility of the application to update the model
682 * and store @new_text at the position indicated by @path.
684 text_cell_renderer_signals [EDITED] =
685 g_signal_new (I_("edited"),
686 G_OBJECT_CLASS_TYPE (object_class),
688 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
690 _gtk_marshal_VOID__STRING_STRING,
695 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
699 gtk_cell_renderer_text_finalize (GObject *object)
701 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
702 GtkCellRendererTextPrivate *priv = celltext->priv;
704 pango_font_description_free (priv->font);
708 if (priv->extra_attrs)
709 pango_attr_list_unref (priv->extra_attrs);
712 g_object_unref (priv->language);
714 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
718 get_property_font_set_mask (guint prop_id)
722 case PROP_FAMILY_SET:
723 return PANGO_FONT_MASK_FAMILY;
725 return PANGO_FONT_MASK_STYLE;
726 case PROP_VARIANT_SET:
727 return PANGO_FONT_MASK_VARIANT;
728 case PROP_WEIGHT_SET:
729 return PANGO_FONT_MASK_WEIGHT;
730 case PROP_STRETCH_SET:
731 return PANGO_FONT_MASK_STRETCH;
733 return PANGO_FONT_MASK_SIZE;
740 gtk_cell_renderer_text_get_property (GObject *object,
745 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
746 GtkCellRendererTextPrivate *priv = celltext->priv;
751 g_value_set_string (value, priv->text);
754 case PROP_ATTRIBUTES:
755 g_value_set_boxed (value, priv->extra_attrs);
758 case PROP_SINGLE_PARAGRAPH_MODE:
759 g_value_set_boolean (value, priv->single_paragraph);
762 case PROP_BACKGROUND_GDK:
766 color.red = (guint16) (priv->background.red * 65535);
767 color.green = (guint16) (priv->background.green * 65535);
768 color.blue = (guint16) (priv->background.blue * 65535);
770 g_value_set_boxed (value, &color);
774 case PROP_FOREGROUND_GDK:
778 color.red = (guint16) (priv->foreground.red * 65535);
779 color.green = (guint16) (priv->foreground.green * 65535);
780 color.blue = (guint16) (priv->foreground.blue * 65535);
782 g_value_set_boxed (value, &color);
786 case PROP_BACKGROUND_RGBA:
787 g_value_set_boxed (value, &priv->background);
790 case PROP_FOREGROUND_RGBA:
791 g_value_set_boxed (value, &priv->foreground);
795 g_value_take_string (value, pango_font_description_to_string (priv->font));
799 g_value_set_boxed (value, priv->font);
803 g_value_set_string (value, pango_font_description_get_family (priv->font));
807 g_value_set_enum (value, pango_font_description_get_style (priv->font));
811 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
815 g_value_set_int (value, pango_font_description_get_weight (priv->font));
819 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
823 g_value_set_int (value, pango_font_description_get_size (priv->font));
826 case PROP_SIZE_POINTS:
827 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
831 g_value_set_double (value, priv->font_scale);
835 g_value_set_boolean (value, priv->editable);
838 case PROP_STRIKETHROUGH:
839 g_value_set_boolean (value, priv->strikethrough);
843 g_value_set_enum (value, priv->underline_style);
847 g_value_set_int (value, priv->rise);
851 g_value_set_static_string (value, pango_language_to_string (priv->language));
855 g_value_set_enum (value, priv->ellipsize);
859 g_value_set_enum (value, priv->wrap_mode);
862 case PROP_WRAP_WIDTH:
863 g_value_set_int (value, priv->wrap_width);
867 g_value_set_enum (value, priv->align);
870 case PROP_BACKGROUND_SET:
871 g_value_set_boolean (value, priv->background_set);
874 case PROP_FOREGROUND_SET:
875 g_value_set_boolean (value, priv->foreground_set);
878 case PROP_FAMILY_SET:
880 case PROP_VARIANT_SET:
881 case PROP_WEIGHT_SET:
882 case PROP_STRETCH_SET:
885 PangoFontMask mask = get_property_font_set_mask (param_id);
886 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
892 g_value_set_boolean (value, priv->scale_set);
895 case PROP_EDITABLE_SET:
896 g_value_set_boolean (value, priv->editable_set);
899 case PROP_STRIKETHROUGH_SET:
900 g_value_set_boolean (value, priv->strikethrough_set);
903 case PROP_UNDERLINE_SET:
904 g_value_set_boolean (value, priv->underline_set);
908 g_value_set_boolean (value, priv->rise_set);
911 case PROP_LANGUAGE_SET:
912 g_value_set_boolean (value, priv->language_set);
915 case PROP_ELLIPSIZE_SET:
916 g_value_set_boolean (value, priv->ellipsize_set);
920 g_value_set_boolean (value, priv->align_set);
923 case PROP_WIDTH_CHARS:
924 g_value_set_int (value, priv->width_chars);
927 case PROP_MAX_WIDTH_CHARS:
928 g_value_set_int (value, priv->max_width_chars);
931 case PROP_BACKGROUND:
932 case PROP_FOREGROUND:
935 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
942 set_bg_color (GtkCellRendererText *celltext,
945 GtkCellRendererTextPrivate *priv = celltext->priv;
949 if (!priv->background_set)
951 priv->background_set = TRUE;
952 g_object_notify (G_OBJECT (celltext), "background-set");
955 priv->background = *rgba;
959 if (priv->background_set)
961 priv->background_set = FALSE;
962 g_object_notify (G_OBJECT (celltext), "background-set");
969 set_fg_color (GtkCellRendererText *celltext,
972 GtkCellRendererTextPrivate *priv = celltext->priv;
976 if (!priv->foreground_set)
978 priv->foreground_set = TRUE;
979 g_object_notify (G_OBJECT (celltext), "foreground-set");
982 priv->foreground = *rgba;
986 if (priv->foreground_set)
988 priv->foreground_set = FALSE;
989 g_object_notify (G_OBJECT (celltext), "foreground-set");
995 set_font_desc_fields (PangoFontDescription *desc,
996 PangoFontMask to_set)
998 PangoFontMask changed_mask = 0;
1000 if (to_set & PANGO_FONT_MASK_FAMILY)
1002 const char *family = pango_font_description_get_family (desc);
1006 changed_mask |= PANGO_FONT_MASK_FAMILY;
1009 pango_font_description_set_family (desc, family);
1011 if (to_set & PANGO_FONT_MASK_STYLE)
1012 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1013 if (to_set & PANGO_FONT_MASK_VARIANT)
1014 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1015 if (to_set & PANGO_FONT_MASK_WEIGHT)
1016 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1017 if (to_set & PANGO_FONT_MASK_STRETCH)
1018 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1019 if (to_set & PANGO_FONT_MASK_SIZE)
1021 gint size = pango_font_description_get_size (desc);
1024 size = 10 * PANGO_SCALE;
1025 changed_mask |= PANGO_FONT_MASK_SIZE;
1028 pango_font_description_set_size (desc, size);
1031 return changed_mask;
1035 notify_set_changed (GObject *object,
1036 PangoFontMask changed_mask)
1038 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1039 g_object_notify (object, "family-set");
1040 if (changed_mask & PANGO_FONT_MASK_STYLE)
1041 g_object_notify (object, "style-set");
1042 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1043 g_object_notify (object, "variant-set");
1044 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1045 g_object_notify (object, "weight-set");
1046 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1047 g_object_notify (object, "stretch-set");
1048 if (changed_mask & PANGO_FONT_MASK_SIZE)
1049 g_object_notify (object, "size-set");
1053 notify_fields_changed (GObject *object,
1054 PangoFontMask changed_mask)
1056 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1057 g_object_notify (object, "family");
1058 if (changed_mask & PANGO_FONT_MASK_STYLE)
1059 g_object_notify (object, "style");
1060 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1061 g_object_notify (object, "variant");
1062 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1063 g_object_notify (object, "weight");
1064 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1065 g_object_notify (object, "stretch");
1066 if (changed_mask & PANGO_FONT_MASK_SIZE)
1067 g_object_notify (object, "size");
1071 set_font_description (GtkCellRendererText *celltext,
1072 PangoFontDescription *font_desc)
1074 GtkCellRendererTextPrivate *priv = celltext->priv;
1075 GObject *object = G_OBJECT (celltext);
1076 PangoFontDescription *new_font_desc;
1077 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1080 new_font_desc = pango_font_description_copy (font_desc);
1082 new_font_desc = pango_font_description_new ();
1084 old_mask = pango_font_description_get_set_fields (priv->font);
1085 new_mask = pango_font_description_get_set_fields (new_font_desc);
1087 changed_mask = old_mask | new_mask;
1088 set_changed_mask = old_mask ^ new_mask;
1090 pango_font_description_free (priv->font);
1091 priv->font = new_font_desc;
1093 g_object_freeze_notify (object);
1095 g_object_notify (object, "font-desc");
1096 g_object_notify (object, "font");
1098 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1099 g_object_notify (object, "family");
1100 if (changed_mask & PANGO_FONT_MASK_STYLE)
1101 g_object_notify (object, "style");
1102 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1103 g_object_notify (object, "variant");
1104 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1105 g_object_notify (object, "weight");
1106 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1107 g_object_notify (object, "stretch");
1108 if (changed_mask & PANGO_FONT_MASK_SIZE)
1110 g_object_notify (object, "size");
1111 g_object_notify (object, "size-points");
1114 notify_set_changed (object, set_changed_mask);
1116 g_object_thaw_notify (object);
1120 gtk_cell_renderer_text_set_property (GObject *object,
1122 const GValue *value,
1125 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1126 GtkCellRendererTextPrivate *priv = celltext->priv;
1131 g_free (priv->text);
1133 if (priv->markup_set)
1135 if (priv->extra_attrs)
1136 pango_attr_list_unref (priv->extra_attrs);
1137 priv->extra_attrs = NULL;
1138 priv->markup_set = FALSE;
1141 priv->text = g_value_dup_string (value);
1142 g_object_notify (object, "text");
1145 case PROP_ATTRIBUTES:
1146 if (priv->extra_attrs)
1147 pango_attr_list_unref (priv->extra_attrs);
1149 priv->extra_attrs = g_value_get_boxed (value);
1150 if (priv->extra_attrs)
1151 pango_attr_list_ref (priv->extra_attrs);
1157 GError *error = NULL;
1158 PangoAttrList *attrs = NULL;
1160 str = g_value_get_string (value);
1161 if (str && !pango_parse_markup (str,
1169 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1171 g_error_free (error);
1175 g_free (priv->text);
1177 if (priv->extra_attrs)
1178 pango_attr_list_unref (priv->extra_attrs);
1181 priv->extra_attrs = attrs;
1182 priv->markup_set = TRUE;
1186 case PROP_SINGLE_PARAGRAPH_MODE:
1187 priv->single_paragraph = g_value_get_boolean (value);
1190 case PROP_BACKGROUND:
1194 if (!g_value_get_string (value))
1195 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1196 else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
1197 set_bg_color (celltext, &rgba);
1199 g_warning ("Don't know color `%s'", g_value_get_string (value));
1201 g_object_notify (object, "background-gdk");
1205 case PROP_FOREGROUND:
1209 if (!g_value_get_string (value))
1210 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1211 else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
1212 set_fg_color (celltext, &rgba);
1214 g_warning ("Don't know color `%s'", g_value_get_string (value));
1216 g_object_notify (object, "foreground-gdk");
1220 case PROP_BACKGROUND_GDK:
1224 color = g_value_get_boxed (value);
1229 rgba.red = color->red / 65535.;
1230 rgba.green = color->green / 65535.;
1231 rgba.blue = color->blue / 65535.;
1234 set_bg_color (celltext, &rgba);
1238 set_bg_color (celltext, NULL);
1243 case PROP_FOREGROUND_GDK:
1247 color = g_value_get_boxed (value);
1252 rgba.red = color->red / 65535.;
1253 rgba.green = color->green / 65535.;
1254 rgba.blue = color->blue / 65535.;
1257 set_fg_color (celltext, &rgba);
1261 set_fg_color (celltext, NULL);
1266 case PROP_BACKGROUND_RGBA:
1267 /* This notifies the GObject itself. */
1268 set_bg_color (celltext, g_value_get_boxed (value));
1271 case PROP_FOREGROUND_RGBA:
1272 /* This notifies the GObject itself. */
1273 set_fg_color (celltext, g_value_get_boxed (value));
1278 PangoFontDescription *font_desc = NULL;
1281 name = g_value_get_string (value);
1284 font_desc = pango_font_description_from_string (name);
1286 set_font_description (celltext, font_desc);
1288 pango_font_description_free (font_desc);
1290 if (priv->fixed_height_rows != -1)
1291 priv->calc_fixed_height = TRUE;
1295 case PROP_FONT_DESC:
1296 set_font_description (celltext, g_value_get_boxed (value));
1298 if (priv->fixed_height_rows != -1)
1299 priv->calc_fixed_height = TRUE;
1308 case PROP_SIZE_POINTS:
1310 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1315 pango_font_description_set_family (priv->font,
1316 g_value_get_string (value));
1319 pango_font_description_set_style (priv->font,
1320 g_value_get_enum (value));
1323 pango_font_description_set_variant (priv->font,
1324 g_value_get_enum (value));
1327 pango_font_description_set_weight (priv->font,
1328 g_value_get_int (value));
1331 pango_font_description_set_stretch (priv->font,
1332 g_value_get_enum (value));
1335 pango_font_description_set_size (priv->font,
1336 g_value_get_int (value));
1337 g_object_notify (object, "size-points");
1339 case PROP_SIZE_POINTS:
1340 pango_font_description_set_size (priv->font,
1341 g_value_get_double (value) * PANGO_SCALE);
1342 g_object_notify (object, "size");
1346 if (priv->fixed_height_rows != -1)
1347 priv->calc_fixed_height = TRUE;
1349 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1350 g_object_notify (object, "font-desc");
1351 g_object_notify (object, "font");
1357 priv->font_scale = g_value_get_double (value);
1358 priv->scale_set = TRUE;
1359 if (priv->fixed_height_rows != -1)
1360 priv->calc_fixed_height = TRUE;
1361 g_object_notify (object, "scale-set");
1365 priv->editable = g_value_get_boolean (value);
1366 priv->editable_set = TRUE;
1368 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1370 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1371 g_object_notify (object, "editable-set");
1374 case PROP_STRIKETHROUGH:
1375 priv->strikethrough = g_value_get_boolean (value);
1376 priv->strikethrough_set = TRUE;
1377 g_object_notify (object, "strikethrough-set");
1380 case PROP_UNDERLINE:
1381 priv->underline_style = g_value_get_enum (value);
1382 priv->underline_set = TRUE;
1383 g_object_notify (object, "underline-set");
1388 priv->rise = g_value_get_int (value);
1389 priv->rise_set = TRUE;
1390 g_object_notify (object, "rise-set");
1391 if (priv->fixed_height_rows != -1)
1392 priv->calc_fixed_height = TRUE;
1396 priv->language_set = TRUE;
1398 g_object_unref (priv->language);
1399 priv->language = pango_language_from_string (g_value_get_string (value));
1400 g_object_notify (object, "language-set");
1403 case PROP_ELLIPSIZE:
1404 priv->ellipsize = g_value_get_enum (value);
1405 priv->ellipsize_set = TRUE;
1406 g_object_notify (object, "ellipsize-set");
1409 case PROP_WRAP_MODE:
1410 priv->wrap_mode = g_value_get_enum (value);
1413 case PROP_WRAP_WIDTH:
1414 priv->wrap_width = g_value_get_int (value);
1417 case PROP_WIDTH_CHARS:
1418 priv->width_chars = g_value_get_int (value);
1421 case PROP_MAX_WIDTH_CHARS:
1422 priv->max_width_chars = g_value_get_int (value);
1426 priv->align = g_value_get_enum (value);
1427 priv->align_set = TRUE;
1428 g_object_notify (object, "align-set");
1431 case PROP_BACKGROUND_SET:
1432 priv->background_set = g_value_get_boolean (value);
1435 case PROP_FOREGROUND_SET:
1436 priv->foreground_set = g_value_get_boolean (value);
1439 case PROP_FAMILY_SET:
1440 case PROP_STYLE_SET:
1441 case PROP_VARIANT_SET:
1442 case PROP_WEIGHT_SET:
1443 case PROP_STRETCH_SET:
1445 if (!g_value_get_boolean (value))
1447 pango_font_description_unset_fields (priv->font,
1448 get_property_font_set_mask (param_id));
1452 PangoFontMask changed_mask;
1454 changed_mask = set_font_desc_fields (priv->font,
1455 get_property_font_set_mask (param_id));
1456 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1460 case PROP_SCALE_SET:
1461 priv->scale_set = g_value_get_boolean (value);
1464 case PROP_EDITABLE_SET:
1465 priv->editable_set = g_value_get_boolean (value);
1468 case PROP_STRIKETHROUGH_SET:
1469 priv->strikethrough_set = g_value_get_boolean (value);
1472 case PROP_UNDERLINE_SET:
1473 priv->underline_set = g_value_get_boolean (value);
1477 priv->rise_set = g_value_get_boolean (value);
1480 case PROP_LANGUAGE_SET:
1481 priv->language_set = g_value_get_boolean (value);
1484 case PROP_ELLIPSIZE_SET:
1485 priv->ellipsize_set = g_value_get_boolean (value);
1488 case PROP_ALIGN_SET:
1489 priv->align_set = g_value_get_boolean (value);
1493 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1499 * gtk_cell_renderer_text_new:
1501 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1502 * object properties. Object properties can be
1503 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1504 * you can bind a property to a value in a #GtkTreeModel. For example,
1505 * you can bind the "text" property on the cell renderer to a string
1506 * value in the model, thus rendering a different string in each row
1507 * of the #GtkTreeView
1509 * Return value: the new cell renderer
1512 gtk_cell_renderer_text_new (void)
1514 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1518 add_attr (PangoAttrList *attr_list,
1519 PangoAttribute *attr)
1521 attr->start_index = 0;
1522 attr->end_index = G_MAXINT;
1524 pango_attr_list_insert (attr_list, attr);
1528 get_layout (GtkCellRendererText *celltext,
1530 const GdkRectangle *cell_area,
1531 GtkCellRendererState flags)
1533 GtkCellRendererTextPrivate *priv = celltext->priv;
1534 PangoAttrList *attr_list;
1535 PangoLayout *layout;
1536 PangoUnderline uline;
1539 layout = gtk_widget_create_pango_layout (widget, priv->text);
1541 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1543 if (priv->extra_attrs)
1544 attr_list = pango_attr_list_copy (priv->extra_attrs);
1546 attr_list = pango_attr_list_new ();
1548 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1552 /* Add options that affect appearance but not size */
1554 /* note that background doesn't go here, since it affects
1555 * background_area not the PangoLayout area
1558 if (priv->foreground_set
1559 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1563 color.red = (guint16) (priv->foreground.red * 65535);
1564 color.green = (guint16) (priv->foreground.green * 65535);
1565 color.blue = (guint16) (priv->foreground.blue * 65535);
1567 add_attr (attr_list,
1568 pango_attr_foreground_new (color.red, color.green, color.blue));
1571 if (priv->strikethrough_set)
1572 add_attr (attr_list,
1573 pango_attr_strikethrough_new (priv->strikethrough));
1576 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1578 if (priv->scale_set &&
1579 priv->font_scale != 1.0)
1580 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1582 if (priv->underline_set)
1583 uline = priv->underline_style;
1585 uline = PANGO_UNDERLINE_NONE;
1587 if (priv->language_set)
1588 add_attr (attr_list, pango_attr_language_new (priv->language));
1590 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1594 case PANGO_UNDERLINE_NONE:
1595 uline = PANGO_UNDERLINE_SINGLE;
1598 case PANGO_UNDERLINE_SINGLE:
1599 uline = PANGO_UNDERLINE_DOUBLE;
1607 if (uline != PANGO_UNDERLINE_NONE)
1608 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1611 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1613 /* Now apply the attributes as they will effect the outcome
1614 * of pango_layout_get_extents() */
1615 pango_layout_set_attributes (layout, attr_list);
1616 pango_attr_list_unref (attr_list);
1618 if (priv->ellipsize_set)
1619 pango_layout_set_ellipsize (layout, priv->ellipsize);
1621 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1623 if (priv->wrap_width != -1)
1625 PangoRectangle rect;
1626 gint width, text_width;
1628 pango_layout_get_extents (layout, NULL, &rect);
1629 text_width = rect.width;
1632 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1634 width = priv->wrap_width * PANGO_SCALE;
1636 width = MIN (width, text_width);
1638 pango_layout_set_width (layout, width);
1639 pango_layout_set_wrap (layout, priv->wrap_mode);
1643 pango_layout_set_width (layout, -1);
1644 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1647 if (priv->align_set)
1648 pango_layout_set_alignment (layout, priv->align);
1651 PangoAlignment align;
1653 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1654 align = PANGO_ALIGN_RIGHT;
1656 align = PANGO_ALIGN_LEFT;
1658 pango_layout_set_alignment (layout, align);
1666 get_size (GtkCellRenderer *cell,
1668 const GdkRectangle *cell_area,
1669 PangoLayout *layout,
1675 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1676 GtkCellRendererTextPrivate *priv = celltext->priv;
1677 PangoRectangle rect;
1679 gint cell_width, cell_height;
1681 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1683 if (priv->calc_fixed_height)
1685 PangoContext *context;
1686 PangoFontMetrics *metrics;
1687 PangoFontDescription *font_desc;
1690 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1691 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1693 if (priv->scale_set)
1694 pango_font_description_set_size (font_desc,
1695 priv->font_scale * pango_font_description_get_size (font_desc));
1697 context = gtk_widget_get_pango_context (widget);
1699 metrics = pango_context_get_metrics (context,
1701 pango_context_get_language (context));
1702 row_height = (pango_font_metrics_get_ascent (metrics) +
1703 pango_font_metrics_get_descent (metrics));
1704 pango_font_metrics_unref (metrics);
1706 pango_font_description_free (font_desc);
1708 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1710 gtk_cell_renderer_set_fixed_size (cell,
1711 cell_width, 2 * ypad +
1712 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1716 *height = cell_height;
1719 priv->calc_fixed_height = FALSE;
1725 g_object_ref (layout);
1727 layout = get_layout (celltext, widget, NULL, 0);
1729 pango_layout_get_pixel_extents (layout, NULL, &rect);
1732 *height = ypad * 2 + rect.height;
1734 /* The minimum size for ellipsized labels is ~ 3 chars */
1737 if (priv->ellipsize || priv->width_chars > 0)
1739 PangoContext *context;
1740 PangoFontMetrics *metrics;
1743 context = pango_layout_get_context (layout);
1744 metrics = pango_context_get_metrics (context,
1745 gtk_widget_get_style (widget)->font_desc,
1746 pango_context_get_language (context));
1748 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1749 pango_font_metrics_unref (metrics);
1751 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1755 *width = xpad * 2 + rect.x + rect.width;
1761 gfloat xalign, yalign;
1763 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1767 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1768 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1770 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1772 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1773 *x_offset = MAX(*x_offset, 0);
1777 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1778 *y_offset = MAX (*y_offset, 0);
1783 if (x_offset) *x_offset = 0;
1784 if (y_offset) *y_offset = 0;
1787 g_object_unref (layout);
1791 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1794 const GdkRectangle *background_area,
1795 const GdkRectangle *cell_area,
1796 GtkCellRendererState flags)
1799 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1800 GtkCellRendererTextPrivate *priv = celltext->priv;
1801 PangoLayout *layout;
1807 layout = get_layout (celltext, widget, cell_area, flags);
1808 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1810 if (!gtk_cell_renderer_get_sensitive (cell))
1812 state = GTK_STATE_INSENSITIVE;
1814 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1816 if (gtk_widget_has_focus (widget))
1817 state = GTK_STATE_SELECTED;
1819 state = GTK_STATE_ACTIVE;
1821 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1822 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1824 state = GTK_STATE_PRELIGHT;
1828 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1829 state = GTK_STATE_INSENSITIVE;
1831 state = GTK_STATE_NORMAL;
1834 if (priv->background_set &&
1835 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1837 gdk_cairo_rectangle (cr, background_area);
1838 gdk_cairo_set_source_rgba (cr, &priv->background);
1842 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1844 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1845 pango_layout_set_width (layout,
1846 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1847 else if (priv->wrap_width == -1)
1848 pango_layout_set_width (layout, -1);
1850 gtk_paint_layout (gtk_widget_get_style (widget),
1856 cell_area->x + x_offset + xpad,
1857 cell_area->y + y_offset + ypad,
1860 g_object_unref (layout);
1864 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1867 GtkCellRendererTextPrivate *priv;
1869 const gchar *new_text;
1872 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1876 if (priv->focus_out_id > 0)
1878 g_signal_handler_disconnect (entry, priv->focus_out_id);
1879 priv->focus_out_id = 0;
1882 if (priv->populate_popup_id > 0)
1884 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1885 priv->populate_popup_id = 0;
1888 if (priv->entry_menu_popdown_timeout)
1890 g_source_remove (priv->entry_menu_popdown_timeout);
1891 priv->entry_menu_popdown_timeout = 0;
1894 g_object_get (entry,
1895 "editing-canceled", &canceled,
1897 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1902 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1903 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1905 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1909 popdown_timeout (gpointer data)
1911 GtkCellRendererTextPrivate *priv;
1913 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1915 priv->entry_menu_popdown_timeout = 0;
1917 if (!gtk_widget_has_focus (priv->entry))
1918 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1924 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1927 GtkCellRendererTextPrivate *priv;
1929 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1931 priv->in_entry_menu = FALSE;
1933 if (priv->entry_menu_popdown_timeout)
1936 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1941 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1945 GtkCellRendererTextPrivate *priv;
1947 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1949 if (priv->entry_menu_popdown_timeout)
1951 g_source_remove (priv->entry_menu_popdown_timeout);
1952 priv->entry_menu_popdown_timeout = 0;
1955 priv->in_entry_menu = TRUE;
1957 g_signal_connect (menu, "unmap",
1958 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1962 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1966 GtkCellRendererTextPrivate *priv;
1968 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1970 if (priv->in_entry_menu)
1973 g_object_set (entry,
1974 "editing-canceled", TRUE,
1976 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1977 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1979 /* entry needs focus-out-event */
1983 static GtkCellEditable *
1984 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1988 const GdkRectangle *background_area,
1989 const GdkRectangle *cell_area,
1990 GtkCellRendererState flags)
1992 GtkRequisition requisition;
1993 GtkCellRendererText *celltext;
1994 GtkCellRendererTextPrivate *priv;
1995 gfloat xalign, yalign;
1997 celltext = GTK_CELL_RENDERER_TEXT (cell);
1998 priv = celltext->priv;
2000 /* If the cell isn't editable we return NULL. */
2001 if (priv->editable == FALSE)
2004 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
2006 priv->entry = gtk_entry_new ();
2007 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
2008 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
2011 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
2012 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
2014 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
2016 gtk_widget_get_preferred_size (priv->entry, &requisition, NULL);
2017 if (requisition.height < cell_area->height)
2019 GtkBorder *style_border;
2022 gtk_widget_style_get (priv->entry,
2023 "inner-border", &style_border,
2028 border = *style_border;
2029 g_boxed_free (GTK_TYPE_BORDER, style_border);
2033 /* Since boxed style properties can't have default values ... */
2038 border.top = (cell_area->height - requisition.height) / 2;
2039 border.bottom = (cell_area->height - requisition.height) / 2;
2040 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
2043 priv->in_entry_menu = FALSE;
2044 if (priv->entry_menu_popdown_timeout)
2046 g_source_remove (priv->entry_menu_popdown_timeout);
2047 priv->entry_menu_popdown_timeout = 0;
2050 g_signal_connect (priv->entry,
2052 G_CALLBACK (gtk_cell_renderer_text_editing_done),
2054 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2055 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2057 priv->populate_popup_id =
2058 g_signal_connect (priv->entry, "populate-popup",
2059 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2062 gtk_widget_show (priv->entry);
2064 return GTK_CELL_EDITABLE (priv->entry);
2068 * gtk_cell_renderer_text_set_fixed_height_from_font:
2069 * @renderer: A #GtkCellRendererText
2070 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2072 * Sets the height of a renderer to explicitly be determined by the "font" and
2073 * "y_pad" property set on it. Further changes in these properties do not
2074 * affect the height, so they must be accompanied by a subsequent call to this
2075 * function. Using this function is unflexible, and should really only be used
2076 * if calculating the size of a cell is too slow (ie, a massive number of cells
2077 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
2078 * the height is determined by the properties again.
2081 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2082 gint number_of_rows)
2084 GtkCellRendererTextPrivate *priv;
2085 GtkCellRenderer *cell;
2087 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2088 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2090 cell = GTK_CELL_RENDERER (renderer);
2091 priv = renderer->priv;
2093 if (number_of_rows == -1)
2097 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2098 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2102 priv->fixed_height_rows = number_of_rows;
2103 priv->calc_fixed_height = TRUE;
2108 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2113 GtkCellRendererTextPrivate *priv;
2114 GtkCellRendererText *celltext;
2116 PangoLayout *layout;
2117 PangoContext *context;
2118 PangoFontMetrics *metrics;
2119 PangoRectangle rect;
2120 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2121 gint min_width, nat_width;
2123 /* "width-chars" Hard-coded minimum width:
2124 * - minimum size should be MAX (width-chars, strlen ("..."));
2125 * - natural size should be MAX (width-chars, strlen (label->text));
2127 * "wrap-width" User specified natural wrap width
2128 * - minimum size should be MAX (width-chars, 0)
2129 * - natural size should be MIN (wrap-width, strlen (label->text))
2132 celltext = GTK_CELL_RENDERER_TEXT (cell);
2133 priv = celltext->priv;
2135 style = gtk_widget_get_style (widget);
2137 gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2139 layout = get_layout (celltext, widget, NULL, 0);
2141 /* Get the layout with the text possibly wrapping at wrap_width */
2142 pango_layout_get_pixel_extents (layout, NULL, &rect);
2143 guess_width = rect.width;
2145 /* Fetch the length of the complete unwrapped text */
2146 pango_layout_set_width (layout, -1);
2147 pango_layout_get_extents (layout, NULL, &rect);
2148 text_width = rect.width;
2150 /* Fetch the average size of a charachter */
2151 context = pango_layout_get_context (layout);
2152 metrics = pango_context_get_metrics (context, style->font_desc,
2153 pango_context_get_language (context));
2155 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2156 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2157 char_pixels = MAX (char_width, digit_width);
2159 pango_font_metrics_unref (metrics);
2160 g_object_unref (layout);
2162 /* enforce minimum width for ellipsized labels at ~3 chars */
2163 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2164 ellipsize_chars = 3;
2166 ellipsize_chars = 0;
2168 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2170 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2171 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2172 else if (priv->wrap_width > -1)
2173 min_width = xpad * 2 + rect.x + priv->wrap_width;
2175 min_width = xpad * 2 + rect.x + guess_width;
2177 if (priv->width_chars > 0)
2178 nat_width = xpad * 2 +
2179 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2181 nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2183 nat_width = MAX (nat_width, min_width);
2185 if (priv->max_width_chars > 0)
2187 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2189 min_width = MIN (min_width, max_width);
2190 nat_width = MIN (nat_width, max_width);
2194 *minimum_size = min_width;
2197 *natural_size = nat_width;
2202 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2205 gint *minimum_height,
2206 gint *natural_height)
2208 GtkCellRendererTextPrivate *priv;
2209 GtkCellRendererText *celltext;
2210 PangoLayout *layout;
2211 gint text_height, xpad, ypad;
2214 celltext = GTK_CELL_RENDERER_TEXT (cell);
2215 priv = celltext->priv;
2217 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2219 layout = get_layout (celltext, widget, NULL, 0);
2221 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2222 pango_layout_get_pixel_size (layout, NULL, &text_height);
2225 *minimum_height = text_height + ypad * 2;
2228 *natural_height = text_height + ypad * 2;
2230 g_object_unref (layout);
2234 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2241 /* Thankfully cell renderers dont rotate, so they only have to do
2242 * height-for-width and not the opposite. Here we have only to return
2243 * the height for the base minimum width of the renderer.
2245 * Note this code path wont be followed by GtkTreeView which is
2246 * height-for-width specifically.
2248 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2249 gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2250 minimum_size, natural_size);