1 /* gtkcellrenderertext.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcellrenderertext.h"
23 #include "gtkeditable.h"
24 #include "gtkcellsizerequest.h"
26 #include "gtkmarshalers.h"
28 #include "gtkprivate.h"
29 #include "gtktreeprivate.h"
32 static void gtk_cell_renderer_text_finalize (GObject *object);
34 static void gtk_cell_renderer_text_get_property (GObject *object,
38 static void gtk_cell_renderer_text_set_property (GObject *object,
42 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
45 GdkRectangle *background_area,
46 GdkRectangle *cell_area,
47 GdkRectangle *expose_area,
48 GtkCellRendererState flags);
50 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
54 GdkRectangle *background_area,
55 GdkRectangle *cell_area,
56 GtkCellRendererState flags);
58 static void gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface);
59 static void gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
63 static void gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
67 static void gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
71 gint *natural_height);
84 PROP_SINGLE_PARAGRAPH_MODE,
112 /* Whether-a-style-arg-is-set args */
123 PROP_STRIKETHROUGH_SET,
131 static guint text_cell_renderer_signals [LAST_SIGNAL];
133 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
135 struct _GtkCellRendererTextPriv
139 PangoAlignment align;
140 PangoAttrList *extra_attrs;
141 PangoColor foreground;
142 PangoColor background;
143 PangoEllipsizeMode ellipsize;
144 PangoFontDescription *font;
145 PangoLanguage *language;
146 PangoUnderline underline_style;
147 PangoWrapMode wrap_mode;
149 gboolean in_entry_menu;
156 gint fixed_height_rows;
160 guint strikethrough : 1;
163 guint foreground_set : 1;
164 guint background_set : 1;
165 guint underline_set : 1;
167 guint strikethrough_set : 1;
168 guint editable_set : 1;
169 guint calc_fixed_height : 1;
170 guint single_paragraph : 1;
171 guint language_set : 1;
172 guint markup_set : 1;
173 guint ellipsize_set : 1;
177 gulong populate_popup_id;
178 gulong entry_menu_popdown_timeout;
181 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER,
182 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_SIZE_REQUEST,
183 gtk_cell_renderer_text_cell_size_request_init))
186 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
188 GtkCellRendererTextPriv *priv;
189 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
191 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
192 GTK_TYPE_CELL_RENDERER_TEXT,
193 GtkCellRendererTextPriv);
194 priv = celltext->priv;
196 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
197 gtk_cell_renderer_set_padding (cell, 2, 2);
198 priv->font_scale = 1.0;
199 priv->fixed_height_rows = -1;
200 priv->font = pango_font_description_new ();
202 priv->width_chars = -1;
203 priv->wrap_width = -1;
204 priv->wrap_mode = PANGO_WRAP_CHAR;
205 priv->align = PANGO_ALIGN_LEFT;
206 priv->align_set = FALSE;
210 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
212 GObjectClass *object_class = G_OBJECT_CLASS (class);
213 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
215 object_class->finalize = gtk_cell_renderer_text_finalize;
217 object_class->get_property = gtk_cell_renderer_text_get_property;
218 object_class->set_property = gtk_cell_renderer_text_set_property;
220 cell_class->render = gtk_cell_renderer_text_render;
221 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
223 g_object_class_install_property (object_class,
225 g_param_spec_string ("text",
227 P_("Text to render"),
229 GTK_PARAM_READWRITE));
231 g_object_class_install_property (object_class,
233 g_param_spec_string ("markup",
235 P_("Marked up text to render"),
237 GTK_PARAM_WRITABLE));
239 g_object_class_install_property (object_class,
241 g_param_spec_boxed ("attributes",
243 P_("A list of style attributes to apply to the text of the renderer"),
244 PANGO_TYPE_ATTR_LIST,
245 GTK_PARAM_READWRITE));
247 g_object_class_install_property (object_class,
248 PROP_SINGLE_PARAGRAPH_MODE,
249 g_param_spec_boolean ("single-paragraph-mode",
250 P_("Single Paragraph Mode"),
251 P_("Whether to keep all text in a single paragraph"),
253 GTK_PARAM_READWRITE));
256 g_object_class_install_property (object_class,
258 g_param_spec_string ("background",
259 P_("Background color name"),
260 P_("Background color as a string"),
262 GTK_PARAM_WRITABLE));
264 g_object_class_install_property (object_class,
266 g_param_spec_boxed ("background-gdk",
267 P_("Background color"),
268 P_("Background color as a GdkColor"),
270 GTK_PARAM_READWRITE));
272 g_object_class_install_property (object_class,
274 g_param_spec_string ("foreground",
275 P_("Foreground color name"),
276 P_("Foreground color as a string"),
278 GTK_PARAM_WRITABLE));
280 g_object_class_install_property (object_class,
282 g_param_spec_boxed ("foreground-gdk",
283 P_("Foreground color"),
284 P_("Foreground color as a GdkColor"),
286 GTK_PARAM_READWRITE));
289 g_object_class_install_property (object_class,
291 g_param_spec_boolean ("editable",
293 P_("Whether the text can be modified by the user"),
295 GTK_PARAM_READWRITE));
297 g_object_class_install_property (object_class,
299 g_param_spec_string ("font",
301 P_("Font description as a string, e.g. \"Sans Italic 12\""),
303 GTK_PARAM_READWRITE));
305 g_object_class_install_property (object_class,
307 g_param_spec_boxed ("font-desc",
309 P_("Font description as a PangoFontDescription struct"),
310 PANGO_TYPE_FONT_DESCRIPTION,
311 GTK_PARAM_READWRITE));
314 g_object_class_install_property (object_class,
316 g_param_spec_string ("family",
318 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
320 GTK_PARAM_READWRITE));
322 g_object_class_install_property (object_class,
324 g_param_spec_enum ("style",
329 GTK_PARAM_READWRITE));
331 g_object_class_install_property (object_class,
333 g_param_spec_enum ("variant",
337 PANGO_VARIANT_NORMAL,
338 GTK_PARAM_READWRITE));
340 g_object_class_install_property (object_class,
342 g_param_spec_int ("weight",
348 GTK_PARAM_READWRITE));
350 g_object_class_install_property (object_class,
352 g_param_spec_enum ("stretch",
356 PANGO_STRETCH_NORMAL,
357 GTK_PARAM_READWRITE));
359 g_object_class_install_property (object_class,
361 g_param_spec_int ("size",
367 GTK_PARAM_READWRITE));
369 g_object_class_install_property (object_class,
371 g_param_spec_double ("size-points",
373 P_("Font size in points"),
377 GTK_PARAM_READWRITE));
379 g_object_class_install_property (object_class,
381 g_param_spec_double ("scale",
383 P_("Font scaling factor"),
387 GTK_PARAM_READWRITE));
389 g_object_class_install_property (object_class,
391 g_param_spec_int ("rise",
393 P_("Offset of text above the baseline "
394 "(below the baseline if rise is negative)"),
398 GTK_PARAM_READWRITE));
401 g_object_class_install_property (object_class,
403 g_param_spec_boolean ("strikethrough",
405 P_("Whether to strike through the text"),
407 GTK_PARAM_READWRITE));
409 g_object_class_install_property (object_class,
411 g_param_spec_enum ("underline",
413 P_("Style of underline for this text"),
414 PANGO_TYPE_UNDERLINE,
415 PANGO_UNDERLINE_NONE,
416 GTK_PARAM_READWRITE));
418 g_object_class_install_property (object_class,
420 g_param_spec_string ("language",
422 P_("The language this text is in, as an ISO code. "
423 "Pango can use this as a hint when rendering the text. "
424 "If you don't understand this parameter, you probably don't need it"),
426 GTK_PARAM_READWRITE));
430 * GtkCellRendererText:ellipsize:
432 * Specifies the preferred place to ellipsize the string, if the cell renderer
433 * does not have enough room to display the entire string. Setting it to
434 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
435 * for another way of making the text fit into a given width.
439 g_object_class_install_property (object_class,
441 g_param_spec_enum ("ellipsize",
443 P_("The preferred place to ellipsize the string, "
444 "if the cell renderer does not have enough room "
445 "to display the entire string"),
446 PANGO_TYPE_ELLIPSIZE_MODE,
447 PANGO_ELLIPSIZE_NONE,
448 GTK_PARAM_READWRITE));
451 * GtkCellRendererText:width-chars:
453 * The desired width of the cell, in characters. If this property is set to
454 * -1, the width will be calculated automatically, otherwise the cell will
455 * request either 3 characters or the property value, whichever is greater.
459 g_object_class_install_property (object_class,
461 g_param_spec_int ("width-chars",
462 P_("Width In Characters"),
463 P_("The desired width of the label, in characters"),
467 GTK_PARAM_READWRITE));
470 * GtkCellRendererText:wrap-mode:
472 * Specifies how to break the string into multiple lines, if the cell
473 * renderer does not have enough room to display the entire string.
474 * This property has no effect unless the wrap-width property is set.
478 g_object_class_install_property (object_class,
480 g_param_spec_enum ("wrap-mode",
482 P_("How to break the string into multiple lines, "
483 "if the cell renderer does not have enough room "
484 "to display the entire string"),
485 PANGO_TYPE_WRAP_MODE,
487 GTK_PARAM_READWRITE));
490 * GtkCellRendererText:wrap-width:
492 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
493 * be used to influence at what character positions the line breaks can be placed.
494 * Setting wrap-width to -1 turns wrapping off.
498 g_object_class_install_property (object_class,
500 g_param_spec_int ("wrap-width",
502 P_("The width at which the text is wrapped"),
506 GTK_PARAM_READWRITE));
509 * GtkCellRendererText:alignment:
511 * Specifies how to align the lines of text with respect to each other.
513 * Note that this property describes how to align the lines of text in
514 * case there are several of them. The "xalign" property of #GtkCellRenderer,
515 * on the other hand, sets the horizontal alignment of the whole text.
519 g_object_class_install_property (object_class,
521 g_param_spec_enum ("alignment",
523 P_("How to align the lines"),
524 PANGO_TYPE_ALIGNMENT,
526 GTK_PARAM_READWRITE));
528 /* Style props are set or not */
530 #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))
532 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
533 P_("Background set"),
534 P_("Whether this tag affects the background color"));
536 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
537 P_("Foreground set"),
538 P_("Whether this tag affects the foreground color"));
540 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
541 P_("Editability set"),
542 P_("Whether this tag affects text editability"));
544 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
545 P_("Font family set"),
546 P_("Whether this tag affects the font family"));
548 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
549 P_("Font style set"),
550 P_("Whether this tag affects the font style"));
552 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
553 P_("Font variant set"),
554 P_("Whether this tag affects the font variant"));
556 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
557 P_("Font weight set"),
558 P_("Whether this tag affects the font weight"));
560 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
561 P_("Font stretch set"),
562 P_("Whether this tag affects the font stretch"));
564 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
566 P_("Whether this tag affects the font size"));
568 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
569 P_("Font scale set"),
570 P_("Whether this tag scales the font size by a factor"));
572 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
574 P_("Whether this tag affects the rise"));
576 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
577 P_("Strikethrough set"),
578 P_("Whether this tag affects strikethrough"));
580 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
582 P_("Whether this tag affects underlining"));
584 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
586 P_("Whether this tag affects the language the text is rendered as"));
588 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
590 P_("Whether this tag affects the ellipsize mode"));
592 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
594 P_("Whether this tag affects the alignment mode"));
597 * GtkCellRendererText::edited
598 * @renderer: the object which received the signal
599 * @path: the path identifying the edited cell
600 * @new_text: the new text
602 * This signal is emitted after @renderer has been edited.
604 * It is the responsibility of the application to update the model
605 * and store @new_text at the position indicated by @path.
607 text_cell_renderer_signals [EDITED] =
608 g_signal_new (I_("edited"),
609 G_OBJECT_CLASS_TYPE (object_class),
611 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
613 _gtk_marshal_VOID__STRING_STRING,
618 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPriv));
622 gtk_cell_renderer_text_finalize (GObject *object)
624 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
625 GtkCellRendererTextPriv *priv = celltext->priv;
627 pango_font_description_free (priv->font);
631 if (priv->extra_attrs)
632 pango_attr_list_unref (priv->extra_attrs);
635 g_object_unref (priv->language);
637 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
641 get_property_font_set_mask (guint prop_id)
645 case PROP_FAMILY_SET:
646 return PANGO_FONT_MASK_FAMILY;
648 return PANGO_FONT_MASK_STYLE;
649 case PROP_VARIANT_SET:
650 return PANGO_FONT_MASK_VARIANT;
651 case PROP_WEIGHT_SET:
652 return PANGO_FONT_MASK_WEIGHT;
653 case PROP_STRETCH_SET:
654 return PANGO_FONT_MASK_STRETCH;
656 return PANGO_FONT_MASK_SIZE;
663 gtk_cell_renderer_text_get_property (GObject *object,
668 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
669 GtkCellRendererTextPriv *priv = celltext->priv;
674 g_value_set_string (value, priv->text);
677 case PROP_ATTRIBUTES:
678 g_value_set_boxed (value, priv->extra_attrs);
681 case PROP_SINGLE_PARAGRAPH_MODE:
682 g_value_set_boolean (value, priv->single_paragraph);
685 case PROP_BACKGROUND_GDK:
689 color.red = priv->background.red;
690 color.green = priv->background.green;
691 color.blue = priv->background.blue;
693 g_value_set_boxed (value, &color);
697 case PROP_FOREGROUND_GDK:
701 color.red = priv->foreground.red;
702 color.green = priv->foreground.green;
703 color.blue = priv->foreground.blue;
705 g_value_set_boxed (value, &color);
710 g_value_take_string (value, pango_font_description_to_string (priv->font));
714 g_value_set_boxed (value, priv->font);
718 g_value_set_string (value, pango_font_description_get_family (priv->font));
722 g_value_set_enum (value, pango_font_description_get_style (priv->font));
726 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
730 g_value_set_int (value, pango_font_description_get_weight (priv->font));
734 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
738 g_value_set_int (value, pango_font_description_get_size (priv->font));
741 case PROP_SIZE_POINTS:
742 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
746 g_value_set_double (value, priv->font_scale);
750 g_value_set_boolean (value, priv->editable);
753 case PROP_STRIKETHROUGH:
754 g_value_set_boolean (value, priv->strikethrough);
758 g_value_set_enum (value, priv->underline_style);
762 g_value_set_int (value, priv->rise);
766 g_value_set_static_string (value, pango_language_to_string (priv->language));
770 g_value_set_enum (value, priv->ellipsize);
774 g_value_set_enum (value, priv->wrap_mode);
777 case PROP_WRAP_WIDTH:
778 g_value_set_int (value, priv->wrap_width);
782 g_value_set_enum (value, priv->align);
785 case PROP_BACKGROUND_SET:
786 g_value_set_boolean (value, priv->background_set);
789 case PROP_FOREGROUND_SET:
790 g_value_set_boolean (value, priv->foreground_set);
793 case PROP_FAMILY_SET:
795 case PROP_VARIANT_SET:
796 case PROP_WEIGHT_SET:
797 case PROP_STRETCH_SET:
800 PangoFontMask mask = get_property_font_set_mask (param_id);
801 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
807 g_value_set_boolean (value, priv->scale_set);
810 case PROP_EDITABLE_SET:
811 g_value_set_boolean (value, priv->editable_set);
814 case PROP_STRIKETHROUGH_SET:
815 g_value_set_boolean (value, priv->strikethrough_set);
818 case PROP_UNDERLINE_SET:
819 g_value_set_boolean (value, priv->underline_set);
823 g_value_set_boolean (value, priv->rise_set);
826 case PROP_LANGUAGE_SET:
827 g_value_set_boolean (value, priv->language_set);
830 case PROP_ELLIPSIZE_SET:
831 g_value_set_boolean (value, priv->ellipsize_set);
835 g_value_set_boolean (value, priv->align_set);
838 case PROP_WIDTH_CHARS:
839 g_value_set_int (value, priv->width_chars);
842 case PROP_BACKGROUND:
843 case PROP_FOREGROUND:
846 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
853 set_bg_color (GtkCellRendererText *celltext,
856 GtkCellRendererTextPriv *priv = celltext->priv;
860 if (!priv->background_set)
862 priv->background_set = TRUE;
863 g_object_notify (G_OBJECT (celltext), "background-set");
866 priv->background.red = color->red;
867 priv->background.green = color->green;
868 priv->background.blue = color->blue;
872 if (priv->background_set)
874 priv->background_set = FALSE;
875 g_object_notify (G_OBJECT (celltext), "background-set");
882 set_fg_color (GtkCellRendererText *celltext,
885 GtkCellRendererTextPriv *priv = celltext->priv;
889 if (!priv->foreground_set)
891 priv->foreground_set = TRUE;
892 g_object_notify (G_OBJECT (celltext), "foreground-set");
895 priv->foreground.red = color->red;
896 priv->foreground.green = color->green;
897 priv->foreground.blue = color->blue;
901 if (priv->foreground_set)
903 priv->foreground_set = FALSE;
904 g_object_notify (G_OBJECT (celltext), "foreground-set");
910 set_font_desc_fields (PangoFontDescription *desc,
911 PangoFontMask to_set)
913 PangoFontMask changed_mask = 0;
915 if (to_set & PANGO_FONT_MASK_FAMILY)
917 const char *family = pango_font_description_get_family (desc);
921 changed_mask |= PANGO_FONT_MASK_FAMILY;
924 pango_font_description_set_family (desc, family);
926 if (to_set & PANGO_FONT_MASK_STYLE)
927 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
928 if (to_set & PANGO_FONT_MASK_VARIANT)
929 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
930 if (to_set & PANGO_FONT_MASK_WEIGHT)
931 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
932 if (to_set & PANGO_FONT_MASK_STRETCH)
933 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
934 if (to_set & PANGO_FONT_MASK_SIZE)
936 gint size = pango_font_description_get_size (desc);
939 size = 10 * PANGO_SCALE;
940 changed_mask |= PANGO_FONT_MASK_SIZE;
943 pango_font_description_set_size (desc, size);
950 notify_set_changed (GObject *object,
951 PangoFontMask changed_mask)
953 if (changed_mask & PANGO_FONT_MASK_FAMILY)
954 g_object_notify (object, "family-set");
955 if (changed_mask & PANGO_FONT_MASK_STYLE)
956 g_object_notify (object, "style-set");
957 if (changed_mask & PANGO_FONT_MASK_VARIANT)
958 g_object_notify (object, "variant-set");
959 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
960 g_object_notify (object, "weight-set");
961 if (changed_mask & PANGO_FONT_MASK_STRETCH)
962 g_object_notify (object, "stretch-set");
963 if (changed_mask & PANGO_FONT_MASK_SIZE)
964 g_object_notify (object, "size-set");
968 notify_fields_changed (GObject *object,
969 PangoFontMask changed_mask)
971 if (changed_mask & PANGO_FONT_MASK_FAMILY)
972 g_object_notify (object, "family");
973 if (changed_mask & PANGO_FONT_MASK_STYLE)
974 g_object_notify (object, "style");
975 if (changed_mask & PANGO_FONT_MASK_VARIANT)
976 g_object_notify (object, "variant");
977 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
978 g_object_notify (object, "weight");
979 if (changed_mask & PANGO_FONT_MASK_STRETCH)
980 g_object_notify (object, "stretch");
981 if (changed_mask & PANGO_FONT_MASK_SIZE)
982 g_object_notify (object, "size");
986 set_font_description (GtkCellRendererText *celltext,
987 PangoFontDescription *font_desc)
989 GtkCellRendererTextPriv *priv = celltext->priv;
990 GObject *object = G_OBJECT (celltext);
991 PangoFontDescription *new_font_desc;
992 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
995 new_font_desc = pango_font_description_copy (font_desc);
997 new_font_desc = pango_font_description_new ();
999 old_mask = pango_font_description_get_set_fields (priv->font);
1000 new_mask = pango_font_description_get_set_fields (new_font_desc);
1002 changed_mask = old_mask | new_mask;
1003 set_changed_mask = old_mask ^ new_mask;
1005 pango_font_description_free (priv->font);
1006 priv->font = new_font_desc;
1008 g_object_freeze_notify (object);
1010 g_object_notify (object, "font-desc");
1011 g_object_notify (object, "font");
1013 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1014 g_object_notify (object, "family");
1015 if (changed_mask & PANGO_FONT_MASK_STYLE)
1016 g_object_notify (object, "style");
1017 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1018 g_object_notify (object, "variant");
1019 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1020 g_object_notify (object, "weight");
1021 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1022 g_object_notify (object, "stretch");
1023 if (changed_mask & PANGO_FONT_MASK_SIZE)
1025 g_object_notify (object, "size");
1026 g_object_notify (object, "size-points");
1029 notify_set_changed (object, set_changed_mask);
1031 g_object_thaw_notify (object);
1035 gtk_cell_renderer_text_set_property (GObject *object,
1037 const GValue *value,
1040 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1041 GtkCellRendererTextPriv *priv = celltext->priv;
1046 g_free (priv->text);
1048 if (priv->markup_set)
1050 if (priv->extra_attrs)
1051 pango_attr_list_unref (priv->extra_attrs);
1052 priv->extra_attrs = NULL;
1053 priv->markup_set = FALSE;
1056 priv->text = g_value_dup_string (value);
1057 g_object_notify (object, "text");
1060 case PROP_ATTRIBUTES:
1061 if (priv->extra_attrs)
1062 pango_attr_list_unref (priv->extra_attrs);
1064 priv->extra_attrs = g_value_get_boxed (value);
1065 if (priv->extra_attrs)
1066 pango_attr_list_ref (priv->extra_attrs);
1072 GError *error = NULL;
1073 PangoAttrList *attrs = NULL;
1075 str = g_value_get_string (value);
1076 if (str && !pango_parse_markup (str,
1084 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1086 g_error_free (error);
1090 g_free (priv->text);
1092 if (priv->extra_attrs)
1093 pango_attr_list_unref (priv->extra_attrs);
1096 priv->extra_attrs = attrs;
1097 priv->markup_set = TRUE;
1101 case PROP_SINGLE_PARAGRAPH_MODE:
1102 priv->single_paragraph = g_value_get_boolean (value);
1105 case PROP_BACKGROUND:
1109 if (!g_value_get_string (value))
1110 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1111 else if (gdk_color_parse (g_value_get_string (value), &color))
1112 set_bg_color (celltext, &color);
1114 g_warning ("Don't know color `%s'", g_value_get_string (value));
1116 g_object_notify (object, "background-gdk");
1120 case PROP_FOREGROUND:
1124 if (!g_value_get_string (value))
1125 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1126 else if (gdk_color_parse (g_value_get_string (value), &color))
1127 set_fg_color (celltext, &color);
1129 g_warning ("Don't know color `%s'", g_value_get_string (value));
1131 g_object_notify (object, "foreground-gdk");
1135 case PROP_BACKGROUND_GDK:
1136 /* This notifies the GObject itself. */
1137 set_bg_color (celltext, g_value_get_boxed (value));
1140 case PROP_FOREGROUND_GDK:
1141 /* This notifies the GObject itself. */
1142 set_fg_color (celltext, g_value_get_boxed (value));
1147 PangoFontDescription *font_desc = NULL;
1150 name = g_value_get_string (value);
1153 font_desc = pango_font_description_from_string (name);
1155 set_font_description (celltext, font_desc);
1157 pango_font_description_free (font_desc);
1159 if (priv->fixed_height_rows != -1)
1160 priv->calc_fixed_height = TRUE;
1164 case PROP_FONT_DESC:
1165 set_font_description (celltext, g_value_get_boxed (value));
1167 if (priv->fixed_height_rows != -1)
1168 priv->calc_fixed_height = TRUE;
1177 case PROP_SIZE_POINTS:
1179 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1184 pango_font_description_set_family (priv->font,
1185 g_value_get_string (value));
1188 pango_font_description_set_style (priv->font,
1189 g_value_get_enum (value));
1192 pango_font_description_set_variant (priv->font,
1193 g_value_get_enum (value));
1196 pango_font_description_set_weight (priv->font,
1197 g_value_get_int (value));
1200 pango_font_description_set_stretch (priv->font,
1201 g_value_get_enum (value));
1204 pango_font_description_set_size (priv->font,
1205 g_value_get_int (value));
1206 g_object_notify (object, "size-points");
1208 case PROP_SIZE_POINTS:
1209 pango_font_description_set_size (priv->font,
1210 g_value_get_double (value) * PANGO_SCALE);
1211 g_object_notify (object, "size");
1215 if (priv->fixed_height_rows != -1)
1216 priv->calc_fixed_height = TRUE;
1218 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1219 g_object_notify (object, "font-desc");
1220 g_object_notify (object, "font");
1226 priv->font_scale = g_value_get_double (value);
1227 priv->scale_set = TRUE;
1228 if (priv->fixed_height_rows != -1)
1229 priv->calc_fixed_height = TRUE;
1230 g_object_notify (object, "scale-set");
1234 priv->editable = g_value_get_boolean (value);
1235 priv->editable_set = TRUE;
1237 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1239 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1240 g_object_notify (object, "editable-set");
1243 case PROP_STRIKETHROUGH:
1244 priv->strikethrough = g_value_get_boolean (value);
1245 priv->strikethrough_set = TRUE;
1246 g_object_notify (object, "strikethrough-set");
1249 case PROP_UNDERLINE:
1250 priv->underline_style = g_value_get_enum (value);
1251 priv->underline_set = TRUE;
1252 g_object_notify (object, "underline-set");
1257 priv->rise = g_value_get_int (value);
1258 priv->rise_set = TRUE;
1259 g_object_notify (object, "rise-set");
1260 if (priv->fixed_height_rows != -1)
1261 priv->calc_fixed_height = TRUE;
1265 priv->language_set = TRUE;
1267 g_object_unref (priv->language);
1268 priv->language = pango_language_from_string (g_value_get_string (value));
1269 g_object_notify (object, "language-set");
1272 case PROP_ELLIPSIZE:
1273 priv->ellipsize = g_value_get_enum (value);
1274 priv->ellipsize_set = TRUE;
1275 g_object_notify (object, "ellipsize-set");
1278 case PROP_WRAP_MODE:
1279 priv->wrap_mode = g_value_get_enum (value);
1282 case PROP_WRAP_WIDTH:
1283 priv->wrap_width = g_value_get_int (value);
1286 case PROP_WIDTH_CHARS:
1287 priv->width_chars = g_value_get_int (value);
1291 priv->align = g_value_get_enum (value);
1292 priv->align_set = TRUE;
1293 g_object_notify (object, "align-set");
1296 case PROP_BACKGROUND_SET:
1297 priv->background_set = g_value_get_boolean (value);
1300 case PROP_FOREGROUND_SET:
1301 priv->foreground_set = g_value_get_boolean (value);
1304 case PROP_FAMILY_SET:
1305 case PROP_STYLE_SET:
1306 case PROP_VARIANT_SET:
1307 case PROP_WEIGHT_SET:
1308 case PROP_STRETCH_SET:
1310 if (!g_value_get_boolean (value))
1312 pango_font_description_unset_fields (priv->font,
1313 get_property_font_set_mask (param_id));
1317 PangoFontMask changed_mask;
1319 changed_mask = set_font_desc_fields (priv->font,
1320 get_property_font_set_mask (param_id));
1321 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1325 case PROP_SCALE_SET:
1326 priv->scale_set = g_value_get_boolean (value);
1329 case PROP_EDITABLE_SET:
1330 priv->editable_set = g_value_get_boolean (value);
1333 case PROP_STRIKETHROUGH_SET:
1334 priv->strikethrough_set = g_value_get_boolean (value);
1337 case PROP_UNDERLINE_SET:
1338 priv->underline_set = g_value_get_boolean (value);
1342 priv->rise_set = g_value_get_boolean (value);
1345 case PROP_LANGUAGE_SET:
1346 priv->language_set = g_value_get_boolean (value);
1349 case PROP_ELLIPSIZE_SET:
1350 priv->ellipsize_set = g_value_get_boolean (value);
1353 case PROP_ALIGN_SET:
1354 priv->align_set = g_value_get_boolean (value);
1358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1364 * gtk_cell_renderer_text_new:
1366 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1367 * object properties. Object properties can be
1368 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1369 * you can bind a property to a value in a #GtkTreeModel. For example,
1370 * you can bind the "text" property on the cell renderer to a string
1371 * value in the model, thus rendering a different string in each row
1372 * of the #GtkTreeView
1374 * Return value: the new cell renderer
1377 gtk_cell_renderer_text_new (void)
1379 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1383 add_attr (PangoAttrList *attr_list,
1384 PangoAttribute *attr)
1386 attr->start_index = 0;
1387 attr->end_index = G_MAXINT;
1389 pango_attr_list_insert (attr_list, attr);
1393 get_layout (GtkCellRendererText *celltext,
1395 GdkRectangle *cell_area,
1396 GtkCellRendererState flags)
1398 GtkCellRendererTextPriv *priv = celltext->priv;
1399 PangoAttrList *attr_list;
1400 PangoLayout *layout;
1401 PangoUnderline uline;
1404 layout = gtk_widget_create_pango_layout (widget, priv->text);
1406 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1408 if (priv->extra_attrs)
1409 attr_list = pango_attr_list_copy (priv->extra_attrs);
1411 attr_list = pango_attr_list_new ();
1413 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1417 /* Add options that affect appearance but not size */
1419 /* note that background doesn't go here, since it affects
1420 * background_area not the PangoLayout area
1423 if (priv->foreground_set
1424 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1428 color = priv->foreground;
1430 add_attr (attr_list,
1431 pango_attr_foreground_new (color.red, color.green, color.blue));
1434 if (priv->strikethrough_set)
1435 add_attr (attr_list,
1436 pango_attr_strikethrough_new (priv->strikethrough));
1439 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1441 if (priv->scale_set &&
1442 priv->font_scale != 1.0)
1443 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1445 if (priv->underline_set)
1446 uline = priv->underline_style;
1448 uline = PANGO_UNDERLINE_NONE;
1450 if (priv->language_set)
1451 add_attr (attr_list, pango_attr_language_new (priv->language));
1453 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1457 case PANGO_UNDERLINE_NONE:
1458 uline = PANGO_UNDERLINE_SINGLE;
1461 case PANGO_UNDERLINE_SINGLE:
1462 uline = PANGO_UNDERLINE_DOUBLE;
1470 if (uline != PANGO_UNDERLINE_NONE)
1471 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1474 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1476 /* Now apply the attributes as they will effect the outcome
1477 * of pango_layout_get_extents() */
1478 pango_layout_set_attributes (layout, attr_list);
1479 pango_attr_list_unref (attr_list);
1481 if (priv->ellipsize_set)
1482 pango_layout_set_ellipsize (layout, priv->ellipsize);
1484 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1486 if (priv->wrap_width != -1)
1488 PangoRectangle rect;
1489 gint width, text_width;
1491 pango_layout_get_extents (layout, NULL, &rect);
1492 text_width = rect.width;
1495 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1497 width = priv->wrap_width * PANGO_SCALE;
1499 width = MIN (width, text_width);
1501 pango_layout_set_width (layout, width);
1502 pango_layout_set_wrap (layout, priv->wrap_mode);
1506 pango_layout_set_width (layout, -1);
1507 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1510 if (priv->align_set)
1511 pango_layout_set_alignment (layout, priv->align);
1514 PangoAlignment align;
1516 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1517 align = PANGO_ALIGN_RIGHT;
1519 align = PANGO_ALIGN_LEFT;
1521 pango_layout_set_alignment (layout, align);
1529 get_size (GtkCellRenderer *cell,
1531 GdkRectangle *cell_area,
1532 PangoLayout *layout,
1538 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1539 GtkCellRendererTextPriv *priv = celltext->priv;
1540 PangoRectangle rect;
1542 gint cell_width, cell_height;
1544 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1546 if (priv->calc_fixed_height)
1548 PangoContext *context;
1549 PangoFontMetrics *metrics;
1550 PangoFontDescription *font_desc;
1553 font_desc = pango_font_description_copy_static (widget->style->font_desc);
1554 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1556 if (priv->scale_set)
1557 pango_font_description_set_size (font_desc,
1558 priv->font_scale * pango_font_description_get_size (font_desc));
1560 context = gtk_widget_get_pango_context (widget);
1562 metrics = pango_context_get_metrics (context,
1564 pango_context_get_language (context));
1565 row_height = (pango_font_metrics_get_ascent (metrics) +
1566 pango_font_metrics_get_descent (metrics));
1567 pango_font_metrics_unref (metrics);
1569 pango_font_description_free (font_desc);
1571 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1573 gtk_cell_renderer_set_fixed_size (cell,
1574 cell_width, 2 * ypad +
1575 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1579 *height = cell_height;
1582 priv->calc_fixed_height = FALSE;
1588 g_object_ref (layout);
1590 layout = get_layout (celltext, widget, NULL, 0);
1592 pango_layout_get_pixel_extents (layout, NULL, &rect);
1595 *height = ypad * 2 + rect.height;
1597 /* The minimum size for ellipsized labels is ~ 3 chars */
1600 if (priv->ellipsize || priv->width_chars > 0)
1602 PangoContext *context;
1603 PangoFontMetrics *metrics;
1606 context = pango_layout_get_context (layout);
1607 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
1609 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1610 pango_font_metrics_unref (metrics);
1612 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1616 *width = xpad * 2 + rect.x + rect.width;
1622 gfloat xalign, yalign;
1624 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1628 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1629 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1631 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1633 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1634 *x_offset = MAX(*x_offset, 0);
1638 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1639 *y_offset = MAX (*y_offset, 0);
1644 if (x_offset) *x_offset = 0;
1645 if (y_offset) *y_offset = 0;
1648 g_object_unref (layout);
1652 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1653 GdkDrawable *window,
1655 GdkRectangle *background_area,
1656 GdkRectangle *cell_area,
1657 GdkRectangle *expose_area,
1658 GtkCellRendererState flags)
1661 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1662 GtkCellRendererTextPriv *priv = celltext->priv;
1663 PangoLayout *layout;
1669 layout = get_layout (celltext, widget, cell_area, flags);
1670 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1672 if (!gtk_cell_renderer_get_sensitive (cell))
1674 state = GTK_STATE_INSENSITIVE;
1676 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1678 if (gtk_widget_has_focus (widget))
1679 state = GTK_STATE_SELECTED;
1681 state = GTK_STATE_ACTIVE;
1683 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1684 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1686 state = GTK_STATE_PRELIGHT;
1690 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1691 state = GTK_STATE_INSENSITIVE;
1693 state = GTK_STATE_NORMAL;
1696 if (priv->background_set &&
1697 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1699 cairo_t *cr = gdk_cairo_create (window);
1703 gdk_cairo_rectangle (cr, expose_area);
1707 gdk_cairo_rectangle (cr, background_area);
1708 cairo_set_source_rgb (cr,
1709 priv->background.red / 65535.,
1710 priv->background.green / 65535.,
1711 priv->background.blue / 65535.);
1717 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1719 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1720 pango_layout_set_width (layout,
1721 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1722 else if (priv->wrap_width == -1)
1723 pango_layout_set_width (layout, -1);
1725 gtk_paint_layout (widget->style,
1732 cell_area->x + x_offset + xpad,
1733 cell_area->y + y_offset + ypad,
1736 g_object_unref (layout);
1740 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1743 GtkCellRendererTextPriv *priv;
1745 const gchar *new_text;
1748 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1752 if (priv->focus_out_id > 0)
1754 g_signal_handler_disconnect (entry, priv->focus_out_id);
1755 priv->focus_out_id = 0;
1758 if (priv->populate_popup_id > 0)
1760 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1761 priv->populate_popup_id = 0;
1764 if (priv->entry_menu_popdown_timeout)
1766 g_source_remove (priv->entry_menu_popdown_timeout);
1767 priv->entry_menu_popdown_timeout = 0;
1770 g_object_get (entry,
1771 "editing-canceled", &canceled,
1773 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1778 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1779 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1781 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1785 popdown_timeout (gpointer data)
1787 GtkCellRendererTextPriv *priv;
1789 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1791 priv->entry_menu_popdown_timeout = 0;
1793 if (!gtk_widget_has_focus (priv->entry))
1794 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1800 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1803 GtkCellRendererTextPriv *priv;
1805 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1807 priv->in_entry_menu = FALSE;
1809 if (priv->entry_menu_popdown_timeout)
1812 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1817 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1821 GtkCellRendererTextPriv *priv;
1823 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1825 if (priv->entry_menu_popdown_timeout)
1827 g_source_remove (priv->entry_menu_popdown_timeout);
1828 priv->entry_menu_popdown_timeout = 0;
1831 priv->in_entry_menu = TRUE;
1833 g_signal_connect (menu, "unmap",
1834 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1838 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1842 GtkCellRendererTextPriv *priv;
1844 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1846 if (priv->in_entry_menu)
1849 g_object_set (entry,
1850 "editing-canceled", TRUE,
1852 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1853 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1855 /* entry needs focus-out-event */
1859 static GtkCellEditable *
1860 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1864 GdkRectangle *background_area,
1865 GdkRectangle *cell_area,
1866 GtkCellRendererState flags)
1868 GtkRequisition requisition;
1869 GtkCellRendererText *celltext;
1870 GtkCellRendererTextPriv *priv;
1871 gfloat xalign, yalign;
1873 celltext = GTK_CELL_RENDERER_TEXT (cell);
1874 priv = celltext->priv;
1876 /* If the cell isn't editable we return NULL. */
1877 if (priv->editable == FALSE)
1880 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1882 priv->entry = gtk_entry_new ();
1883 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1884 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1887 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1888 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1890 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1892 gtk_widget_size_request (priv->entry, &requisition);
1893 if (requisition.height < cell_area->height)
1895 GtkBorder *style_border;
1898 gtk_widget_style_get (priv->entry,
1899 "inner-border", &style_border,
1904 border = *style_border;
1905 g_boxed_free (GTK_TYPE_BORDER, style_border);
1909 /* Since boxed style properties can't have default values ... */
1914 border.top = (cell_area->height - requisition.height) / 2;
1915 border.bottom = (cell_area->height - requisition.height) / 2;
1916 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
1919 priv->in_entry_menu = FALSE;
1920 if (priv->entry_menu_popdown_timeout)
1922 g_source_remove (priv->entry_menu_popdown_timeout);
1923 priv->entry_menu_popdown_timeout = 0;
1926 g_signal_connect (priv->entry,
1928 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1930 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
1931 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1933 priv->populate_popup_id =
1934 g_signal_connect (priv->entry, "populate-popup",
1935 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1938 gtk_widget_show (priv->entry);
1940 return GTK_CELL_EDITABLE (priv->entry);
1944 * gtk_cell_renderer_text_set_fixed_height_from_font:
1945 * @renderer: A #GtkCellRendererText
1946 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1948 * Sets the height of a renderer to explicitly be determined by the "font" and
1949 * "y_pad" property set on it. Further changes in these properties do not
1950 * affect the height, so they must be accompanied by a subsequent call to this
1951 * function. Using this function is unflexible, and should really only be used
1952 * if calculating the size of a cell is too slow (ie, a massive number of cells
1953 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1954 * the height is determined by the properties again.
1957 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1958 gint number_of_rows)
1960 GtkCellRendererTextPriv *priv;
1961 GtkCellRenderer *cell;
1963 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1964 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1966 cell = GTK_CELL_RENDERER (renderer);
1967 priv = renderer->priv;
1969 if (number_of_rows == -1)
1973 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
1974 gtk_cell_renderer_set_fixed_size (cell, width, -1);
1978 priv->fixed_height_rows = number_of_rows;
1979 priv->calc_fixed_height = TRUE;
1984 gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface)
1986 iface->get_width = gtk_cell_renderer_text_get_width;
1987 iface->get_height = gtk_cell_renderer_text_get_height;
1988 iface->get_height_for_width = gtk_cell_renderer_text_get_height_for_width;
1992 gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
1997 GtkCellRendererTextPriv *priv;
1998 GtkCellRendererText *celltext;
1999 PangoLayout *layout;
2000 PangoContext *context;
2001 PangoFontMetrics *metrics;
2002 PangoRectangle rect;
2003 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2006 /* "width-chars" Hard-coded minimum width:
2007 * - minimum size should be MAX (width-chars, strlen ("..."));
2008 * - natural size should be MAX (width-chars, strlen (label->text));
2010 * "wrap-width" User specified natural wrap width
2011 * - minimum size should be MAX (width-chars, 0)
2012 * - natural size should be MIN (wrap-width, strlen (label->text))
2015 celltext = GTK_CELL_RENDERER_TEXT (cell);
2016 priv = celltext->priv;
2018 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, NULL);
2020 layout = get_layout (celltext, widget, NULL, 0);
2022 /* Get the layout with the text possibly wrapping at wrap_width */
2023 pango_layout_get_pixel_extents (layout, NULL, &rect);
2024 guess_width = rect.width;
2026 /* Fetch the length of the complete unwrapped text */
2027 pango_layout_set_width (layout, -1);
2028 pango_layout_get_extents (layout, NULL, &rect);
2029 text_width = rect.width;
2031 /* Fetch the average size of a charachter */
2032 context = pango_layout_get_context (layout);
2033 metrics = pango_context_get_metrics (context, widget->style->font_desc,
2034 pango_context_get_language (context));
2036 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2037 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2038 char_pixels = MAX (char_width, digit_width);
2040 pango_font_metrics_unref (metrics);
2041 g_object_unref (layout);
2043 /* enforce minimum width for ellipsized labels at ~3 chars */
2044 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2045 ellipsize_chars = 3;
2047 ellipsize_chars = 0;
2049 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2051 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2052 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2053 else if (priv->wrap_width > -1)
2054 min_width = xpad * 2 + rect.x + priv->wrap_width;
2056 min_width = xpad * 2 + rect.x + guess_width;
2059 *minimum_size = min_width;
2063 /* Control the max wrap width here possibly (add max-width-chars ?) */
2064 *natural_size = xpad * 2 +
2065 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2067 *natural_size = MAX (*natural_size, min_width);
2072 gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
2075 gint *minimum_height,
2076 gint *natural_height)
2078 GtkCellRendererTextPriv *priv;
2079 GtkCellRendererText *celltext;
2080 PangoLayout *layout;
2081 gint text_height, xpad, ypad;
2084 celltext = GTK_CELL_RENDERER_TEXT (cell);
2085 priv = celltext->priv;
2087 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, &ypad);
2089 layout = get_layout (celltext, widget, FALSE, 0);
2091 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2092 pango_layout_get_pixel_size (layout, NULL, &text_height);
2095 *minimum_height = text_height + ypad * 2;
2098 *natural_height = text_height + ypad * 2;
2100 g_object_unref (layout);
2104 gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
2111 /* Thankfully cell renderers dont rotate, so they only have to do
2112 * height-for-width and not the opposite. Here we have only to return
2113 * the height for the base minimum width of the renderer.
2115 * Note this code path wont be followed by GtkTreeView which is
2116 * height-for-width specifically.
2118 gtk_cell_size_request_get_width (cell, widget, &min_width, NULL);
2119 gtk_cell_renderer_text_get_height_for_width (cell, widget, min_width,
2120 minimum_size, natural_size);