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"
27 #include "gtkcellsizerequest.h"
29 #include "gtksizerequest.h"
30 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
33 #include "gtktreeprivate.h"
36 static void gtk_cell_renderer_text_finalize (GObject *object);
38 static void gtk_cell_renderer_text_get_property (GObject *object,
42 static void gtk_cell_renderer_text_set_property (GObject *object,
46 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
49 const GdkRectangle *background_area,
50 const GdkRectangle *cell_area,
51 GtkCellRendererState flags);
53 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
57 GdkRectangle *background_area,
58 GdkRectangle *cell_area,
59 GtkCellRendererState flags);
61 static void gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface);
62 static void gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
66 static void gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
70 static void gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
74 gint *natural_height);
87 PROP_SINGLE_PARAGRAPH_MODE,
116 /* Whether-a-style-arg-is-set args */
127 PROP_STRIKETHROUGH_SET,
135 static guint text_cell_renderer_signals [LAST_SIGNAL];
137 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
139 struct _GtkCellRendererTextPrivate
143 PangoAlignment align;
144 PangoAttrList *extra_attrs;
145 PangoColor foreground;
146 PangoColor background;
147 PangoEllipsizeMode ellipsize;
148 PangoFontDescription *font;
149 PangoLanguage *language;
150 PangoUnderline underline_style;
151 PangoWrapMode wrap_mode;
153 gboolean in_entry_menu;
160 gint fixed_height_rows;
162 gint max_width_chars;
165 guint strikethrough : 1;
168 guint foreground_set : 1;
169 guint background_set : 1;
170 guint underline_set : 1;
172 guint strikethrough_set : 1;
173 guint editable_set : 1;
174 guint calc_fixed_height : 1;
175 guint single_paragraph : 1;
176 guint language_set : 1;
177 guint markup_set : 1;
178 guint ellipsize_set : 1;
182 gulong populate_popup_id;
183 gulong entry_menu_popdown_timeout;
186 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER,
187 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_SIZE_REQUEST,
188 gtk_cell_renderer_text_cell_size_request_init))
191 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
193 GtkCellRendererTextPrivate *priv;
194 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
196 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
197 GTK_TYPE_CELL_RENDERER_TEXT,
198 GtkCellRendererTextPrivate);
199 priv = celltext->priv;
201 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
202 gtk_cell_renderer_set_padding (cell, 2, 2);
203 priv->font_scale = 1.0;
204 priv->fixed_height_rows = -1;
205 priv->font = pango_font_description_new ();
207 priv->width_chars = -1;
208 priv->max_width_chars = -1;
209 priv->wrap_width = -1;
210 priv->wrap_mode = PANGO_WRAP_CHAR;
211 priv->align = PANGO_ALIGN_LEFT;
212 priv->align_set = FALSE;
216 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
218 GObjectClass *object_class = G_OBJECT_CLASS (class);
219 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
221 object_class->finalize = gtk_cell_renderer_text_finalize;
223 object_class->get_property = gtk_cell_renderer_text_get_property;
224 object_class->set_property = gtk_cell_renderer_text_set_property;
226 cell_class->render = gtk_cell_renderer_text_render;
227 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
229 g_object_class_install_property (object_class,
231 g_param_spec_string ("text",
233 P_("Text to render"),
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (object_class,
239 g_param_spec_string ("markup",
241 P_("Marked up text to render"),
243 GTK_PARAM_WRITABLE));
245 g_object_class_install_property (object_class,
247 g_param_spec_boxed ("attributes",
249 P_("A list of style attributes to apply to the text of the renderer"),
250 PANGO_TYPE_ATTR_LIST,
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (object_class,
254 PROP_SINGLE_PARAGRAPH_MODE,
255 g_param_spec_boolean ("single-paragraph-mode",
256 P_("Single Paragraph Mode"),
257 P_("Whether to keep all text in a single paragraph"),
259 GTK_PARAM_READWRITE));
262 g_object_class_install_property (object_class,
264 g_param_spec_string ("background",
265 P_("Background color name"),
266 P_("Background color as a string"),
268 GTK_PARAM_WRITABLE));
270 g_object_class_install_property (object_class,
272 g_param_spec_boxed ("background-gdk",
273 P_("Background color"),
274 P_("Background color as a GdkColor"),
276 GTK_PARAM_READWRITE));
278 g_object_class_install_property (object_class,
280 g_param_spec_string ("foreground",
281 P_("Foreground color name"),
282 P_("Foreground color as a string"),
284 GTK_PARAM_WRITABLE));
286 g_object_class_install_property (object_class,
288 g_param_spec_boxed ("foreground-gdk",
289 P_("Foreground color"),
290 P_("Foreground color as a GdkColor"),
292 GTK_PARAM_READWRITE));
295 g_object_class_install_property (object_class,
297 g_param_spec_boolean ("editable",
299 P_("Whether the text can be modified by the user"),
301 GTK_PARAM_READWRITE));
303 g_object_class_install_property (object_class,
305 g_param_spec_string ("font",
307 P_("Font description as a string, e.g. \"Sans Italic 12\""),
309 GTK_PARAM_READWRITE));
311 g_object_class_install_property (object_class,
313 g_param_spec_boxed ("font-desc",
315 P_("Font description as a PangoFontDescription struct"),
316 PANGO_TYPE_FONT_DESCRIPTION,
317 GTK_PARAM_READWRITE));
320 g_object_class_install_property (object_class,
322 g_param_spec_string ("family",
324 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
326 GTK_PARAM_READWRITE));
328 g_object_class_install_property (object_class,
330 g_param_spec_enum ("style",
335 GTK_PARAM_READWRITE));
337 g_object_class_install_property (object_class,
339 g_param_spec_enum ("variant",
343 PANGO_VARIANT_NORMAL,
344 GTK_PARAM_READWRITE));
346 g_object_class_install_property (object_class,
348 g_param_spec_int ("weight",
354 GTK_PARAM_READWRITE));
356 g_object_class_install_property (object_class,
358 g_param_spec_enum ("stretch",
362 PANGO_STRETCH_NORMAL,
363 GTK_PARAM_READWRITE));
365 g_object_class_install_property (object_class,
367 g_param_spec_int ("size",
373 GTK_PARAM_READWRITE));
375 g_object_class_install_property (object_class,
377 g_param_spec_double ("size-points",
379 P_("Font size in points"),
383 GTK_PARAM_READWRITE));
385 g_object_class_install_property (object_class,
387 g_param_spec_double ("scale",
389 P_("Font scaling factor"),
393 GTK_PARAM_READWRITE));
395 g_object_class_install_property (object_class,
397 g_param_spec_int ("rise",
399 P_("Offset of text above the baseline "
400 "(below the baseline if rise is negative)"),
404 GTK_PARAM_READWRITE));
407 g_object_class_install_property (object_class,
409 g_param_spec_boolean ("strikethrough",
411 P_("Whether to strike through the text"),
413 GTK_PARAM_READWRITE));
415 g_object_class_install_property (object_class,
417 g_param_spec_enum ("underline",
419 P_("Style of underline for this text"),
420 PANGO_TYPE_UNDERLINE,
421 PANGO_UNDERLINE_NONE,
422 GTK_PARAM_READWRITE));
424 g_object_class_install_property (object_class,
426 g_param_spec_string ("language",
428 P_("The language this text is in, as an ISO code. "
429 "Pango can use this as a hint when rendering the text. "
430 "If you don't understand this parameter, you probably don't need it"),
432 GTK_PARAM_READWRITE));
436 * GtkCellRendererText:ellipsize:
438 * Specifies the preferred place to ellipsize the string, if the cell renderer
439 * does not have enough room to display the entire string. Setting it to
440 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
441 * for another way of making the text fit into a given width.
445 g_object_class_install_property (object_class,
447 g_param_spec_enum ("ellipsize",
449 P_("The preferred place to ellipsize the string, "
450 "if the cell renderer does not have enough room "
451 "to display the entire string"),
452 PANGO_TYPE_ELLIPSIZE_MODE,
453 PANGO_ELLIPSIZE_NONE,
454 GTK_PARAM_READWRITE));
457 * GtkCellRendererText:width-chars:
459 * The desired width of the cell, in characters. If this property is set to
460 * -1, the width will be calculated automatically, otherwise the cell will
461 * request either 3 characters or the property value, whichever is greater.
465 g_object_class_install_property (object_class,
467 g_param_spec_int ("width-chars",
468 P_("Width In Characters"),
469 P_("The desired width of the label, in characters"),
473 GTK_PARAM_READWRITE));
477 * GtkCellRendererText:max-width-chars:
479 * The desired maximum width of the cell, in characters. If this property
480 * is set to -1, the width will be calculated automatically.
482 * For cell renderers that ellipsize or wrap text; this property
483 * controls the maximum reported width of the cell. The
484 * cell should not receive any greater allocation unless it is
485 * set to expand in its #GtkCellLayout and all of the cell's siblings
486 * have received their natural width.
490 g_object_class_install_property (object_class,
491 PROP_MAX_WIDTH_CHARS,
492 g_param_spec_int ("max-width-chars",
493 P_("Maximum Width In Characters"),
494 P_("The maximum width of the cell, in characters"),
498 GTK_PARAM_READWRITE));
501 * GtkCellRendererText:wrap-mode:
503 * Specifies how to break the string into multiple lines, if the cell
504 * renderer does not have enough room to display the entire string.
505 * This property has no effect unless the wrap-width property is set.
509 g_object_class_install_property (object_class,
511 g_param_spec_enum ("wrap-mode",
513 P_("How to break the string into multiple lines, "
514 "if the cell renderer does not have enough room "
515 "to display the entire string"),
516 PANGO_TYPE_WRAP_MODE,
518 GTK_PARAM_READWRITE));
521 * GtkCellRendererText:wrap-width:
523 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
524 * be used to influence at what character positions the line breaks can be placed.
525 * Setting wrap-width to -1 turns wrapping off.
529 g_object_class_install_property (object_class,
531 g_param_spec_int ("wrap-width",
533 P_("The width at which the text is wrapped"),
537 GTK_PARAM_READWRITE));
540 * GtkCellRendererText:alignment:
542 * Specifies how to align the lines of text with respect to each other.
544 * Note that this property describes how to align the lines of text in
545 * case there are several of them. The "xalign" property of #GtkCellRenderer,
546 * on the other hand, sets the horizontal alignment of the whole text.
550 g_object_class_install_property (object_class,
552 g_param_spec_enum ("alignment",
554 P_("How to align the lines"),
555 PANGO_TYPE_ALIGNMENT,
557 GTK_PARAM_READWRITE));
561 /* Style props are set or not */
563 #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))
565 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
566 P_("Background set"),
567 P_("Whether this tag affects the background color"));
569 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
570 P_("Foreground set"),
571 P_("Whether this tag affects the foreground color"));
573 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
574 P_("Editability set"),
575 P_("Whether this tag affects text editability"));
577 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
578 P_("Font family set"),
579 P_("Whether this tag affects the font family"));
581 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
582 P_("Font style set"),
583 P_("Whether this tag affects the font style"));
585 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
586 P_("Font variant set"),
587 P_("Whether this tag affects the font variant"));
589 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
590 P_("Font weight set"),
591 P_("Whether this tag affects the font weight"));
593 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
594 P_("Font stretch set"),
595 P_("Whether this tag affects the font stretch"));
597 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
599 P_("Whether this tag affects the font size"));
601 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
602 P_("Font scale set"),
603 P_("Whether this tag scales the font size by a factor"));
605 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
607 P_("Whether this tag affects the rise"));
609 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
610 P_("Strikethrough set"),
611 P_("Whether this tag affects strikethrough"));
613 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
615 P_("Whether this tag affects underlining"));
617 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
619 P_("Whether this tag affects the language the text is rendered as"));
621 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
623 P_("Whether this tag affects the ellipsize mode"));
625 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
627 P_("Whether this tag affects the alignment mode"));
630 * GtkCellRendererText::edited
631 * @renderer: the object which received the signal
632 * @path: the path identifying the edited cell
633 * @new_text: the new text
635 * This signal is emitted after @renderer has been edited.
637 * It is the responsibility of the application to update the model
638 * and store @new_text at the position indicated by @path.
640 text_cell_renderer_signals [EDITED] =
641 g_signal_new (I_("edited"),
642 G_OBJECT_CLASS_TYPE (object_class),
644 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
646 _gtk_marshal_VOID__STRING_STRING,
651 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
655 gtk_cell_renderer_text_finalize (GObject *object)
657 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
658 GtkCellRendererTextPrivate *priv = celltext->priv;
660 pango_font_description_free (priv->font);
664 if (priv->extra_attrs)
665 pango_attr_list_unref (priv->extra_attrs);
668 g_object_unref (priv->language);
670 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
674 get_property_font_set_mask (guint prop_id)
678 case PROP_FAMILY_SET:
679 return PANGO_FONT_MASK_FAMILY;
681 return PANGO_FONT_MASK_STYLE;
682 case PROP_VARIANT_SET:
683 return PANGO_FONT_MASK_VARIANT;
684 case PROP_WEIGHT_SET:
685 return PANGO_FONT_MASK_WEIGHT;
686 case PROP_STRETCH_SET:
687 return PANGO_FONT_MASK_STRETCH;
689 return PANGO_FONT_MASK_SIZE;
696 gtk_cell_renderer_text_get_property (GObject *object,
701 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
702 GtkCellRendererTextPrivate *priv = celltext->priv;
707 g_value_set_string (value, priv->text);
710 case PROP_ATTRIBUTES:
711 g_value_set_boxed (value, priv->extra_attrs);
714 case PROP_SINGLE_PARAGRAPH_MODE:
715 g_value_set_boolean (value, priv->single_paragraph);
718 case PROP_BACKGROUND_GDK:
722 color.red = priv->background.red;
723 color.green = priv->background.green;
724 color.blue = priv->background.blue;
726 g_value_set_boxed (value, &color);
730 case PROP_FOREGROUND_GDK:
734 color.red = priv->foreground.red;
735 color.green = priv->foreground.green;
736 color.blue = priv->foreground.blue;
738 g_value_set_boxed (value, &color);
743 g_value_take_string (value, pango_font_description_to_string (priv->font));
747 g_value_set_boxed (value, priv->font);
751 g_value_set_string (value, pango_font_description_get_family (priv->font));
755 g_value_set_enum (value, pango_font_description_get_style (priv->font));
759 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
763 g_value_set_int (value, pango_font_description_get_weight (priv->font));
767 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
771 g_value_set_int (value, pango_font_description_get_size (priv->font));
774 case PROP_SIZE_POINTS:
775 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
779 g_value_set_double (value, priv->font_scale);
783 g_value_set_boolean (value, priv->editable);
786 case PROP_STRIKETHROUGH:
787 g_value_set_boolean (value, priv->strikethrough);
791 g_value_set_enum (value, priv->underline_style);
795 g_value_set_int (value, priv->rise);
799 g_value_set_static_string (value, pango_language_to_string (priv->language));
803 g_value_set_enum (value, priv->ellipsize);
807 g_value_set_enum (value, priv->wrap_mode);
810 case PROP_WRAP_WIDTH:
811 g_value_set_int (value, priv->wrap_width);
815 g_value_set_enum (value, priv->align);
818 case PROP_BACKGROUND_SET:
819 g_value_set_boolean (value, priv->background_set);
822 case PROP_FOREGROUND_SET:
823 g_value_set_boolean (value, priv->foreground_set);
826 case PROP_FAMILY_SET:
828 case PROP_VARIANT_SET:
829 case PROP_WEIGHT_SET:
830 case PROP_STRETCH_SET:
833 PangoFontMask mask = get_property_font_set_mask (param_id);
834 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
840 g_value_set_boolean (value, priv->scale_set);
843 case PROP_EDITABLE_SET:
844 g_value_set_boolean (value, priv->editable_set);
847 case PROP_STRIKETHROUGH_SET:
848 g_value_set_boolean (value, priv->strikethrough_set);
851 case PROP_UNDERLINE_SET:
852 g_value_set_boolean (value, priv->underline_set);
856 g_value_set_boolean (value, priv->rise_set);
859 case PROP_LANGUAGE_SET:
860 g_value_set_boolean (value, priv->language_set);
863 case PROP_ELLIPSIZE_SET:
864 g_value_set_boolean (value, priv->ellipsize_set);
868 g_value_set_boolean (value, priv->align_set);
871 case PROP_WIDTH_CHARS:
872 g_value_set_int (value, priv->width_chars);
875 case PROP_MAX_WIDTH_CHARS:
876 g_value_set_int (value, priv->max_width_chars);
879 case PROP_BACKGROUND:
880 case PROP_FOREGROUND:
883 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
890 set_bg_color (GtkCellRendererText *celltext,
893 GtkCellRendererTextPrivate *priv = celltext->priv;
897 if (!priv->background_set)
899 priv->background_set = TRUE;
900 g_object_notify (G_OBJECT (celltext), "background-set");
903 priv->background.red = color->red;
904 priv->background.green = color->green;
905 priv->background.blue = color->blue;
909 if (priv->background_set)
911 priv->background_set = FALSE;
912 g_object_notify (G_OBJECT (celltext), "background-set");
919 set_fg_color (GtkCellRendererText *celltext,
922 GtkCellRendererTextPrivate *priv = celltext->priv;
926 if (!priv->foreground_set)
928 priv->foreground_set = TRUE;
929 g_object_notify (G_OBJECT (celltext), "foreground-set");
932 priv->foreground.red = color->red;
933 priv->foreground.green = color->green;
934 priv->foreground.blue = color->blue;
938 if (priv->foreground_set)
940 priv->foreground_set = FALSE;
941 g_object_notify (G_OBJECT (celltext), "foreground-set");
947 set_font_desc_fields (PangoFontDescription *desc,
948 PangoFontMask to_set)
950 PangoFontMask changed_mask = 0;
952 if (to_set & PANGO_FONT_MASK_FAMILY)
954 const char *family = pango_font_description_get_family (desc);
958 changed_mask |= PANGO_FONT_MASK_FAMILY;
961 pango_font_description_set_family (desc, family);
963 if (to_set & PANGO_FONT_MASK_STYLE)
964 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
965 if (to_set & PANGO_FONT_MASK_VARIANT)
966 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
967 if (to_set & PANGO_FONT_MASK_WEIGHT)
968 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
969 if (to_set & PANGO_FONT_MASK_STRETCH)
970 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
971 if (to_set & PANGO_FONT_MASK_SIZE)
973 gint size = pango_font_description_get_size (desc);
976 size = 10 * PANGO_SCALE;
977 changed_mask |= PANGO_FONT_MASK_SIZE;
980 pango_font_description_set_size (desc, size);
987 notify_set_changed (GObject *object,
988 PangoFontMask changed_mask)
990 if (changed_mask & PANGO_FONT_MASK_FAMILY)
991 g_object_notify (object, "family-set");
992 if (changed_mask & PANGO_FONT_MASK_STYLE)
993 g_object_notify (object, "style-set");
994 if (changed_mask & PANGO_FONT_MASK_VARIANT)
995 g_object_notify (object, "variant-set");
996 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
997 g_object_notify (object, "weight-set");
998 if (changed_mask & PANGO_FONT_MASK_STRETCH)
999 g_object_notify (object, "stretch-set");
1000 if (changed_mask & PANGO_FONT_MASK_SIZE)
1001 g_object_notify (object, "size-set");
1005 notify_fields_changed (GObject *object,
1006 PangoFontMask changed_mask)
1008 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1009 g_object_notify (object, "family");
1010 if (changed_mask & PANGO_FONT_MASK_STYLE)
1011 g_object_notify (object, "style");
1012 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1013 g_object_notify (object, "variant");
1014 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1015 g_object_notify (object, "weight");
1016 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1017 g_object_notify (object, "stretch");
1018 if (changed_mask & PANGO_FONT_MASK_SIZE)
1019 g_object_notify (object, "size");
1023 set_font_description (GtkCellRendererText *celltext,
1024 PangoFontDescription *font_desc)
1026 GtkCellRendererTextPrivate *priv = celltext->priv;
1027 GObject *object = G_OBJECT (celltext);
1028 PangoFontDescription *new_font_desc;
1029 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1032 new_font_desc = pango_font_description_copy (font_desc);
1034 new_font_desc = pango_font_description_new ();
1036 old_mask = pango_font_description_get_set_fields (priv->font);
1037 new_mask = pango_font_description_get_set_fields (new_font_desc);
1039 changed_mask = old_mask | new_mask;
1040 set_changed_mask = old_mask ^ new_mask;
1042 pango_font_description_free (priv->font);
1043 priv->font = new_font_desc;
1045 g_object_freeze_notify (object);
1047 g_object_notify (object, "font-desc");
1048 g_object_notify (object, "font");
1050 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1051 g_object_notify (object, "family");
1052 if (changed_mask & PANGO_FONT_MASK_STYLE)
1053 g_object_notify (object, "style");
1054 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1055 g_object_notify (object, "variant");
1056 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1057 g_object_notify (object, "weight");
1058 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1059 g_object_notify (object, "stretch");
1060 if (changed_mask & PANGO_FONT_MASK_SIZE)
1062 g_object_notify (object, "size");
1063 g_object_notify (object, "size-points");
1066 notify_set_changed (object, set_changed_mask);
1068 g_object_thaw_notify (object);
1072 gtk_cell_renderer_text_set_property (GObject *object,
1074 const GValue *value,
1077 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1078 GtkCellRendererTextPrivate *priv = celltext->priv;
1083 g_free (priv->text);
1085 if (priv->markup_set)
1087 if (priv->extra_attrs)
1088 pango_attr_list_unref (priv->extra_attrs);
1089 priv->extra_attrs = NULL;
1090 priv->markup_set = FALSE;
1093 priv->text = g_value_dup_string (value);
1094 g_object_notify (object, "text");
1097 case PROP_ATTRIBUTES:
1098 if (priv->extra_attrs)
1099 pango_attr_list_unref (priv->extra_attrs);
1101 priv->extra_attrs = g_value_get_boxed (value);
1102 if (priv->extra_attrs)
1103 pango_attr_list_ref (priv->extra_attrs);
1109 GError *error = NULL;
1110 PangoAttrList *attrs = NULL;
1112 str = g_value_get_string (value);
1113 if (str && !pango_parse_markup (str,
1121 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1123 g_error_free (error);
1127 g_free (priv->text);
1129 if (priv->extra_attrs)
1130 pango_attr_list_unref (priv->extra_attrs);
1133 priv->extra_attrs = attrs;
1134 priv->markup_set = TRUE;
1138 case PROP_SINGLE_PARAGRAPH_MODE:
1139 priv->single_paragraph = g_value_get_boolean (value);
1142 case PROP_BACKGROUND:
1146 if (!g_value_get_string (value))
1147 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1148 else if (gdk_color_parse (g_value_get_string (value), &color))
1149 set_bg_color (celltext, &color);
1151 g_warning ("Don't know color `%s'", g_value_get_string (value));
1153 g_object_notify (object, "background-gdk");
1157 case PROP_FOREGROUND:
1161 if (!g_value_get_string (value))
1162 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1163 else if (gdk_color_parse (g_value_get_string (value), &color))
1164 set_fg_color (celltext, &color);
1166 g_warning ("Don't know color `%s'", g_value_get_string (value));
1168 g_object_notify (object, "foreground-gdk");
1172 case PROP_BACKGROUND_GDK:
1173 /* This notifies the GObject itself. */
1174 set_bg_color (celltext, g_value_get_boxed (value));
1177 case PROP_FOREGROUND_GDK:
1178 /* This notifies the GObject itself. */
1179 set_fg_color (celltext, g_value_get_boxed (value));
1184 PangoFontDescription *font_desc = NULL;
1187 name = g_value_get_string (value);
1190 font_desc = pango_font_description_from_string (name);
1192 set_font_description (celltext, font_desc);
1194 pango_font_description_free (font_desc);
1196 if (priv->fixed_height_rows != -1)
1197 priv->calc_fixed_height = TRUE;
1201 case PROP_FONT_DESC:
1202 set_font_description (celltext, g_value_get_boxed (value));
1204 if (priv->fixed_height_rows != -1)
1205 priv->calc_fixed_height = TRUE;
1214 case PROP_SIZE_POINTS:
1216 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1221 pango_font_description_set_family (priv->font,
1222 g_value_get_string (value));
1225 pango_font_description_set_style (priv->font,
1226 g_value_get_enum (value));
1229 pango_font_description_set_variant (priv->font,
1230 g_value_get_enum (value));
1233 pango_font_description_set_weight (priv->font,
1234 g_value_get_int (value));
1237 pango_font_description_set_stretch (priv->font,
1238 g_value_get_enum (value));
1241 pango_font_description_set_size (priv->font,
1242 g_value_get_int (value));
1243 g_object_notify (object, "size-points");
1245 case PROP_SIZE_POINTS:
1246 pango_font_description_set_size (priv->font,
1247 g_value_get_double (value) * PANGO_SCALE);
1248 g_object_notify (object, "size");
1252 if (priv->fixed_height_rows != -1)
1253 priv->calc_fixed_height = TRUE;
1255 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1256 g_object_notify (object, "font-desc");
1257 g_object_notify (object, "font");
1263 priv->font_scale = g_value_get_double (value);
1264 priv->scale_set = TRUE;
1265 if (priv->fixed_height_rows != -1)
1266 priv->calc_fixed_height = TRUE;
1267 g_object_notify (object, "scale-set");
1271 priv->editable = g_value_get_boolean (value);
1272 priv->editable_set = TRUE;
1274 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1276 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1277 g_object_notify (object, "editable-set");
1280 case PROP_STRIKETHROUGH:
1281 priv->strikethrough = g_value_get_boolean (value);
1282 priv->strikethrough_set = TRUE;
1283 g_object_notify (object, "strikethrough-set");
1286 case PROP_UNDERLINE:
1287 priv->underline_style = g_value_get_enum (value);
1288 priv->underline_set = TRUE;
1289 g_object_notify (object, "underline-set");
1294 priv->rise = g_value_get_int (value);
1295 priv->rise_set = TRUE;
1296 g_object_notify (object, "rise-set");
1297 if (priv->fixed_height_rows != -1)
1298 priv->calc_fixed_height = TRUE;
1302 priv->language_set = TRUE;
1304 g_object_unref (priv->language);
1305 priv->language = pango_language_from_string (g_value_get_string (value));
1306 g_object_notify (object, "language-set");
1309 case PROP_ELLIPSIZE:
1310 priv->ellipsize = g_value_get_enum (value);
1311 priv->ellipsize_set = TRUE;
1312 g_object_notify (object, "ellipsize-set");
1315 case PROP_WRAP_MODE:
1316 priv->wrap_mode = g_value_get_enum (value);
1319 case PROP_WRAP_WIDTH:
1320 priv->wrap_width = g_value_get_int (value);
1323 case PROP_WIDTH_CHARS:
1324 priv->width_chars = g_value_get_int (value);
1327 case PROP_MAX_WIDTH_CHARS:
1328 priv->max_width_chars = g_value_get_int (value);
1332 priv->align = g_value_get_enum (value);
1333 priv->align_set = TRUE;
1334 g_object_notify (object, "align-set");
1337 case PROP_BACKGROUND_SET:
1338 priv->background_set = g_value_get_boolean (value);
1341 case PROP_FOREGROUND_SET:
1342 priv->foreground_set = g_value_get_boolean (value);
1345 case PROP_FAMILY_SET:
1346 case PROP_STYLE_SET:
1347 case PROP_VARIANT_SET:
1348 case PROP_WEIGHT_SET:
1349 case PROP_STRETCH_SET:
1351 if (!g_value_get_boolean (value))
1353 pango_font_description_unset_fields (priv->font,
1354 get_property_font_set_mask (param_id));
1358 PangoFontMask changed_mask;
1360 changed_mask = set_font_desc_fields (priv->font,
1361 get_property_font_set_mask (param_id));
1362 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1366 case PROP_SCALE_SET:
1367 priv->scale_set = g_value_get_boolean (value);
1370 case PROP_EDITABLE_SET:
1371 priv->editable_set = g_value_get_boolean (value);
1374 case PROP_STRIKETHROUGH_SET:
1375 priv->strikethrough_set = g_value_get_boolean (value);
1378 case PROP_UNDERLINE_SET:
1379 priv->underline_set = g_value_get_boolean (value);
1383 priv->rise_set = g_value_get_boolean (value);
1386 case PROP_LANGUAGE_SET:
1387 priv->language_set = g_value_get_boolean (value);
1390 case PROP_ELLIPSIZE_SET:
1391 priv->ellipsize_set = g_value_get_boolean (value);
1394 case PROP_ALIGN_SET:
1395 priv->align_set = g_value_get_boolean (value);
1399 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1405 * gtk_cell_renderer_text_new:
1407 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1408 * object properties. Object properties can be
1409 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1410 * you can bind a property to a value in a #GtkTreeModel. For example,
1411 * you can bind the "text" property on the cell renderer to a string
1412 * value in the model, thus rendering a different string in each row
1413 * of the #GtkTreeView
1415 * Return value: the new cell renderer
1418 gtk_cell_renderer_text_new (void)
1420 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1424 add_attr (PangoAttrList *attr_list,
1425 PangoAttribute *attr)
1427 attr->start_index = 0;
1428 attr->end_index = G_MAXINT;
1430 pango_attr_list_insert (attr_list, attr);
1434 get_layout (GtkCellRendererText *celltext,
1436 const GdkRectangle *cell_area,
1437 GtkCellRendererState flags)
1439 GtkCellRendererTextPrivate *priv = celltext->priv;
1440 PangoAttrList *attr_list;
1441 PangoLayout *layout;
1442 PangoUnderline uline;
1445 layout = gtk_widget_create_pango_layout (widget, priv->text);
1447 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1449 if (priv->extra_attrs)
1450 attr_list = pango_attr_list_copy (priv->extra_attrs);
1452 attr_list = pango_attr_list_new ();
1454 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1458 /* Add options that affect appearance but not size */
1460 /* note that background doesn't go here, since it affects
1461 * background_area not the PangoLayout area
1464 if (priv->foreground_set
1465 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1469 color = priv->foreground;
1471 add_attr (attr_list,
1472 pango_attr_foreground_new (color.red, color.green, color.blue));
1475 if (priv->strikethrough_set)
1476 add_attr (attr_list,
1477 pango_attr_strikethrough_new (priv->strikethrough));
1480 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1482 if (priv->scale_set &&
1483 priv->font_scale != 1.0)
1484 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1486 if (priv->underline_set)
1487 uline = priv->underline_style;
1489 uline = PANGO_UNDERLINE_NONE;
1491 if (priv->language_set)
1492 add_attr (attr_list, pango_attr_language_new (priv->language));
1494 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1498 case PANGO_UNDERLINE_NONE:
1499 uline = PANGO_UNDERLINE_SINGLE;
1502 case PANGO_UNDERLINE_SINGLE:
1503 uline = PANGO_UNDERLINE_DOUBLE;
1511 if (uline != PANGO_UNDERLINE_NONE)
1512 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1515 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1517 /* Now apply the attributes as they will effect the outcome
1518 * of pango_layout_get_extents() */
1519 pango_layout_set_attributes (layout, attr_list);
1520 pango_attr_list_unref (attr_list);
1522 if (priv->ellipsize_set)
1523 pango_layout_set_ellipsize (layout, priv->ellipsize);
1525 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1527 if (priv->wrap_width != -1)
1529 PangoRectangle rect;
1530 gint width, text_width;
1532 pango_layout_get_extents (layout, NULL, &rect);
1533 text_width = rect.width;
1536 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1538 width = priv->wrap_width * PANGO_SCALE;
1540 width = MIN (width, text_width);
1542 pango_layout_set_width (layout, width);
1543 pango_layout_set_wrap (layout, priv->wrap_mode);
1547 pango_layout_set_width (layout, -1);
1548 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1551 if (priv->align_set)
1552 pango_layout_set_alignment (layout, priv->align);
1555 PangoAlignment align;
1557 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1558 align = PANGO_ALIGN_RIGHT;
1560 align = PANGO_ALIGN_LEFT;
1562 pango_layout_set_alignment (layout, align);
1570 get_size (GtkCellRenderer *cell,
1572 GdkRectangle *cell_area,
1573 PangoLayout *layout,
1579 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1580 GtkCellRendererTextPrivate *priv = celltext->priv;
1581 PangoRectangle rect;
1583 gint cell_width, cell_height;
1585 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1587 if (priv->calc_fixed_height)
1589 PangoContext *context;
1590 PangoFontMetrics *metrics;
1591 PangoFontDescription *font_desc;
1594 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1595 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1597 if (priv->scale_set)
1598 pango_font_description_set_size (font_desc,
1599 priv->font_scale * pango_font_description_get_size (font_desc));
1601 context = gtk_widget_get_pango_context (widget);
1603 metrics = pango_context_get_metrics (context,
1605 pango_context_get_language (context));
1606 row_height = (pango_font_metrics_get_ascent (metrics) +
1607 pango_font_metrics_get_descent (metrics));
1608 pango_font_metrics_unref (metrics);
1610 pango_font_description_free (font_desc);
1612 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1614 gtk_cell_renderer_set_fixed_size (cell,
1615 cell_width, 2 * ypad +
1616 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1620 *height = cell_height;
1623 priv->calc_fixed_height = FALSE;
1629 g_object_ref (layout);
1631 layout = get_layout (celltext, widget, NULL, 0);
1633 pango_layout_get_pixel_extents (layout, NULL, &rect);
1636 *height = ypad * 2 + rect.height;
1638 /* The minimum size for ellipsized labels is ~ 3 chars */
1641 if (priv->ellipsize || priv->width_chars > 0)
1643 PangoContext *context;
1644 PangoFontMetrics *metrics;
1647 context = pango_layout_get_context (layout);
1648 metrics = pango_context_get_metrics (context,
1649 gtk_widget_get_style (widget)->font_desc,
1650 pango_context_get_language (context));
1652 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1653 pango_font_metrics_unref (metrics);
1655 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1659 *width = xpad * 2 + rect.x + rect.width;
1665 gfloat xalign, yalign;
1667 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1671 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1672 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1674 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1676 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1677 *x_offset = MAX(*x_offset, 0);
1681 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1682 *y_offset = MAX (*y_offset, 0);
1687 if (x_offset) *x_offset = 0;
1688 if (y_offset) *y_offset = 0;
1691 g_object_unref (layout);
1695 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1698 const GdkRectangle *background_area,
1699 const GdkRectangle *cell_area,
1700 GtkCellRendererState flags)
1703 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1704 GtkCellRendererTextPrivate *priv = celltext->priv;
1705 PangoLayout *layout;
1711 layout = get_layout (celltext, widget, cell_area, flags);
1712 get_size (cell, widget, (GdkRectangle *) cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1714 if (!gtk_cell_renderer_get_sensitive (cell))
1716 state = GTK_STATE_INSENSITIVE;
1718 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1720 if (gtk_widget_has_focus (widget))
1721 state = GTK_STATE_SELECTED;
1723 state = GTK_STATE_ACTIVE;
1725 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1726 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1728 state = GTK_STATE_PRELIGHT;
1732 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1733 state = GTK_STATE_INSENSITIVE;
1735 state = GTK_STATE_NORMAL;
1738 if (priv->background_set &&
1739 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1741 gdk_cairo_rectangle (cr, background_area);
1742 cairo_set_source_rgb (cr,
1743 priv->background.red / 65535.,
1744 priv->background.green / 65535.,
1745 priv->background.blue / 65535.);
1749 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1751 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1752 pango_layout_set_width (layout,
1753 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1754 else if (priv->wrap_width == -1)
1755 pango_layout_set_width (layout, -1);
1757 gtk_paint_layout (gtk_widget_get_style (widget),
1763 cell_area->x + x_offset + xpad,
1764 cell_area->y + y_offset + ypad,
1767 g_object_unref (layout);
1771 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1774 GtkCellRendererTextPrivate *priv;
1776 const gchar *new_text;
1779 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1783 if (priv->focus_out_id > 0)
1785 g_signal_handler_disconnect (entry, priv->focus_out_id);
1786 priv->focus_out_id = 0;
1789 if (priv->populate_popup_id > 0)
1791 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1792 priv->populate_popup_id = 0;
1795 if (priv->entry_menu_popdown_timeout)
1797 g_source_remove (priv->entry_menu_popdown_timeout);
1798 priv->entry_menu_popdown_timeout = 0;
1801 g_object_get (entry,
1802 "editing-canceled", &canceled,
1804 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1809 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1810 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1812 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1816 popdown_timeout (gpointer data)
1818 GtkCellRendererTextPrivate *priv;
1820 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1822 priv->entry_menu_popdown_timeout = 0;
1824 if (!gtk_widget_has_focus (priv->entry))
1825 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1831 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1834 GtkCellRendererTextPrivate *priv;
1836 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1838 priv->in_entry_menu = FALSE;
1840 if (priv->entry_menu_popdown_timeout)
1843 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1848 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1852 GtkCellRendererTextPrivate *priv;
1854 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1856 if (priv->entry_menu_popdown_timeout)
1858 g_source_remove (priv->entry_menu_popdown_timeout);
1859 priv->entry_menu_popdown_timeout = 0;
1862 priv->in_entry_menu = TRUE;
1864 g_signal_connect (menu, "unmap",
1865 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1869 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1873 GtkCellRendererTextPrivate *priv;
1875 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1877 if (priv->in_entry_menu)
1880 g_object_set (entry,
1881 "editing-canceled", TRUE,
1883 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1884 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1886 /* entry needs focus-out-event */
1890 static GtkCellEditable *
1891 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1895 GdkRectangle *background_area,
1896 GdkRectangle *cell_area,
1897 GtkCellRendererState flags)
1899 GtkRequisition requisition;
1900 GtkCellRendererText *celltext;
1901 GtkCellRendererTextPrivate *priv;
1902 gfloat xalign, yalign;
1904 celltext = GTK_CELL_RENDERER_TEXT (cell);
1905 priv = celltext->priv;
1907 /* If the cell isn't editable we return NULL. */
1908 if (priv->editable == FALSE)
1911 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1913 priv->entry = gtk_entry_new ();
1914 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1915 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1918 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1919 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1921 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1923 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->entry),
1924 &requisition, NULL);
1925 if (requisition.height < cell_area->height)
1927 GtkBorder *style_border;
1930 gtk_widget_style_get (priv->entry,
1931 "inner-border", &style_border,
1936 border = *style_border;
1937 g_boxed_free (GTK_TYPE_BORDER, style_border);
1941 /* Since boxed style properties can't have default values ... */
1946 border.top = (cell_area->height - requisition.height) / 2;
1947 border.bottom = (cell_area->height - requisition.height) / 2;
1948 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
1951 priv->in_entry_menu = FALSE;
1952 if (priv->entry_menu_popdown_timeout)
1954 g_source_remove (priv->entry_menu_popdown_timeout);
1955 priv->entry_menu_popdown_timeout = 0;
1958 g_signal_connect (priv->entry,
1960 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1962 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
1963 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1965 priv->populate_popup_id =
1966 g_signal_connect (priv->entry, "populate-popup",
1967 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1970 gtk_widget_show (priv->entry);
1972 return GTK_CELL_EDITABLE (priv->entry);
1976 * gtk_cell_renderer_text_set_fixed_height_from_font:
1977 * @renderer: A #GtkCellRendererText
1978 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1980 * Sets the height of a renderer to explicitly be determined by the "font" and
1981 * "y_pad" property set on it. Further changes in these properties do not
1982 * affect the height, so they must be accompanied by a subsequent call to this
1983 * function. Using this function is unflexible, and should really only be used
1984 * if calculating the size of a cell is too slow (ie, a massive number of cells
1985 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1986 * the height is determined by the properties again.
1989 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1990 gint number_of_rows)
1992 GtkCellRendererTextPrivate *priv;
1993 GtkCellRenderer *cell;
1995 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1996 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1998 cell = GTK_CELL_RENDERER (renderer);
1999 priv = renderer->priv;
2001 if (number_of_rows == -1)
2005 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2006 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2010 priv->fixed_height_rows = number_of_rows;
2011 priv->calc_fixed_height = TRUE;
2016 gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface)
2018 iface->get_width = gtk_cell_renderer_text_get_width;
2019 iface->get_height = gtk_cell_renderer_text_get_height;
2020 iface->get_height_for_width = gtk_cell_renderer_text_get_height_for_width;
2024 gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
2029 GtkCellRendererTextPrivate *priv;
2030 GtkCellRendererText *celltext;
2032 PangoLayout *layout;
2033 PangoContext *context;
2034 PangoFontMetrics *metrics;
2035 PangoRectangle rect;
2036 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2037 gint min_width, nat_width;
2039 /* "width-chars" Hard-coded minimum width:
2040 * - minimum size should be MAX (width-chars, strlen ("..."));
2041 * - natural size should be MAX (width-chars, strlen (label->text));
2043 * "wrap-width" User specified natural wrap width
2044 * - minimum size should be MAX (width-chars, 0)
2045 * - natural size should be MIN (wrap-width, strlen (label->text))
2048 celltext = GTK_CELL_RENDERER_TEXT (cell);
2049 priv = celltext->priv;
2051 style = gtk_widget_get_style (widget);
2053 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, NULL);
2055 layout = get_layout (celltext, widget, NULL, 0);
2057 /* Get the layout with the text possibly wrapping at wrap_width */
2058 pango_layout_get_pixel_extents (layout, NULL, &rect);
2059 guess_width = rect.width;
2061 /* Fetch the length of the complete unwrapped text */
2062 pango_layout_set_width (layout, -1);
2063 pango_layout_get_extents (layout, NULL, &rect);
2064 text_width = rect.width;
2066 /* Fetch the average size of a charachter */
2067 context = pango_layout_get_context (layout);
2068 metrics = pango_context_get_metrics (context, style->font_desc,
2069 pango_context_get_language (context));
2071 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2072 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2073 char_pixels = MAX (char_width, digit_width);
2075 pango_font_metrics_unref (metrics);
2076 g_object_unref (layout);
2078 /* enforce minimum width for ellipsized labels at ~3 chars */
2079 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2080 ellipsize_chars = 3;
2082 ellipsize_chars = 0;
2084 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2086 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2087 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2088 else if (priv->wrap_width > -1)
2089 min_width = xpad * 2 + rect.x + priv->wrap_width;
2091 min_width = xpad * 2 + rect.x + guess_width;
2093 if (priv->width_chars > 0)
2094 nat_width = xpad * 2 +
2095 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2097 nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2099 nat_width = MAX (nat_width, min_width);
2101 if (priv->max_width_chars > 0)
2103 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2105 min_width = MIN (min_width, max_width);
2106 nat_width = MIN (nat_width, max_width);
2110 *minimum_size = min_width;
2113 *natural_size = nat_width;
2118 gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
2121 gint *minimum_height,
2122 gint *natural_height)
2124 GtkCellRendererTextPrivate *priv;
2125 GtkCellRendererText *celltext;
2126 PangoLayout *layout;
2127 gint text_height, xpad, ypad;
2130 celltext = GTK_CELL_RENDERER_TEXT (cell);
2131 priv = celltext->priv;
2133 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, &ypad);
2135 layout = get_layout (celltext, widget, NULL, 0);
2137 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2138 pango_layout_get_pixel_size (layout, NULL, &text_height);
2141 *minimum_height = text_height + ypad * 2;
2144 *natural_height = text_height + ypad * 2;
2146 g_object_unref (layout);
2150 gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
2157 /* Thankfully cell renderers dont rotate, so they only have to do
2158 * height-for-width and not the opposite. Here we have only to return
2159 * the height for the base minimum width of the renderer.
2161 * Note this code path wont be followed by GtkTreeView which is
2162 * height-for-width specifically.
2164 gtk_cell_size_request_get_width (cell, widget, &min_width, NULL);
2165 gtk_cell_renderer_text_get_height_for_width (cell, widget, min_width,
2166 minimum_size, natural_size);