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 GdkRectangle *background_area,
50 GdkRectangle *cell_area,
51 GdkRectangle *expose_area,
52 GtkCellRendererState flags);
54 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
58 GdkRectangle *background_area,
59 GdkRectangle *cell_area,
60 GtkCellRendererState flags);
62 static void gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface);
63 static void gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
67 static void gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
71 static void gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
75 gint *natural_height);
88 PROP_SINGLE_PARAGRAPH_MODE,
117 /* Whether-a-style-arg-is-set args */
128 PROP_STRIKETHROUGH_SET,
136 static guint text_cell_renderer_signals [LAST_SIGNAL];
138 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
140 struct _GtkCellRendererTextPrivate
144 PangoAlignment align;
145 PangoAttrList *extra_attrs;
146 PangoColor foreground;
147 PangoColor background;
148 PangoEllipsizeMode ellipsize;
149 PangoFontDescription *font;
150 PangoLanguage *language;
151 PangoUnderline underline_style;
152 PangoWrapMode wrap_mode;
154 gboolean in_entry_menu;
161 gint fixed_height_rows;
163 gint max_width_chars;
166 guint strikethrough : 1;
169 guint foreground_set : 1;
170 guint background_set : 1;
171 guint underline_set : 1;
173 guint strikethrough_set : 1;
174 guint editable_set : 1;
175 guint calc_fixed_height : 1;
176 guint single_paragraph : 1;
177 guint language_set : 1;
178 guint markup_set : 1;
179 guint ellipsize_set : 1;
183 gulong populate_popup_id;
184 gulong entry_menu_popdown_timeout;
187 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER,
188 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_SIZE_REQUEST,
189 gtk_cell_renderer_text_cell_size_request_init))
192 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
194 GtkCellRendererTextPrivate *priv;
195 GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
197 celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
198 GTK_TYPE_CELL_RENDERER_TEXT,
199 GtkCellRendererTextPrivate);
200 priv = celltext->priv;
202 gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
203 gtk_cell_renderer_set_padding (cell, 2, 2);
204 priv->font_scale = 1.0;
205 priv->fixed_height_rows = -1;
206 priv->font = pango_font_description_new ();
208 priv->width_chars = -1;
209 priv->max_width_chars = -1;
210 priv->wrap_width = -1;
211 priv->wrap_mode = PANGO_WRAP_CHAR;
212 priv->align = PANGO_ALIGN_LEFT;
213 priv->align_set = FALSE;
217 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
219 GObjectClass *object_class = G_OBJECT_CLASS (class);
220 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
222 object_class->finalize = gtk_cell_renderer_text_finalize;
224 object_class->get_property = gtk_cell_renderer_text_get_property;
225 object_class->set_property = gtk_cell_renderer_text_set_property;
227 cell_class->render = gtk_cell_renderer_text_render;
228 cell_class->start_editing = gtk_cell_renderer_text_start_editing;
230 g_object_class_install_property (object_class,
232 g_param_spec_string ("text",
234 P_("Text to render"),
236 GTK_PARAM_READWRITE));
238 g_object_class_install_property (object_class,
240 g_param_spec_string ("markup",
242 P_("Marked up text to render"),
244 GTK_PARAM_WRITABLE));
246 g_object_class_install_property (object_class,
248 g_param_spec_boxed ("attributes",
250 P_("A list of style attributes to apply to the text of the renderer"),
251 PANGO_TYPE_ATTR_LIST,
252 GTK_PARAM_READWRITE));
254 g_object_class_install_property (object_class,
255 PROP_SINGLE_PARAGRAPH_MODE,
256 g_param_spec_boolean ("single-paragraph-mode",
257 P_("Single Paragraph Mode"),
258 P_("Whether to keep all text in a single paragraph"),
260 GTK_PARAM_READWRITE));
263 g_object_class_install_property (object_class,
265 g_param_spec_string ("background",
266 P_("Background color name"),
267 P_("Background color as a string"),
269 GTK_PARAM_WRITABLE));
271 g_object_class_install_property (object_class,
273 g_param_spec_boxed ("background-gdk",
274 P_("Background color"),
275 P_("Background color as a GdkColor"),
277 GTK_PARAM_READWRITE));
279 g_object_class_install_property (object_class,
281 g_param_spec_string ("foreground",
282 P_("Foreground color name"),
283 P_("Foreground color as a string"),
285 GTK_PARAM_WRITABLE));
287 g_object_class_install_property (object_class,
289 g_param_spec_boxed ("foreground-gdk",
290 P_("Foreground color"),
291 P_("Foreground color as a GdkColor"),
293 GTK_PARAM_READWRITE));
296 g_object_class_install_property (object_class,
298 g_param_spec_boolean ("editable",
300 P_("Whether the text can be modified by the user"),
302 GTK_PARAM_READWRITE));
304 g_object_class_install_property (object_class,
306 g_param_spec_string ("font",
308 P_("Font description as a string, e.g. \"Sans Italic 12\""),
310 GTK_PARAM_READWRITE));
312 g_object_class_install_property (object_class,
314 g_param_spec_boxed ("font-desc",
316 P_("Font description as a PangoFontDescription struct"),
317 PANGO_TYPE_FONT_DESCRIPTION,
318 GTK_PARAM_READWRITE));
321 g_object_class_install_property (object_class,
323 g_param_spec_string ("family",
325 P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
327 GTK_PARAM_READWRITE));
329 g_object_class_install_property (object_class,
331 g_param_spec_enum ("style",
336 GTK_PARAM_READWRITE));
338 g_object_class_install_property (object_class,
340 g_param_spec_enum ("variant",
344 PANGO_VARIANT_NORMAL,
345 GTK_PARAM_READWRITE));
347 g_object_class_install_property (object_class,
349 g_param_spec_int ("weight",
355 GTK_PARAM_READWRITE));
357 g_object_class_install_property (object_class,
359 g_param_spec_enum ("stretch",
363 PANGO_STRETCH_NORMAL,
364 GTK_PARAM_READWRITE));
366 g_object_class_install_property (object_class,
368 g_param_spec_int ("size",
374 GTK_PARAM_READWRITE));
376 g_object_class_install_property (object_class,
378 g_param_spec_double ("size-points",
380 P_("Font size in points"),
384 GTK_PARAM_READWRITE));
386 g_object_class_install_property (object_class,
388 g_param_spec_double ("scale",
390 P_("Font scaling factor"),
394 GTK_PARAM_READWRITE));
396 g_object_class_install_property (object_class,
398 g_param_spec_int ("rise",
400 P_("Offset of text above the baseline "
401 "(below the baseline if rise is negative)"),
405 GTK_PARAM_READWRITE));
408 g_object_class_install_property (object_class,
410 g_param_spec_boolean ("strikethrough",
412 P_("Whether to strike through the text"),
414 GTK_PARAM_READWRITE));
416 g_object_class_install_property (object_class,
418 g_param_spec_enum ("underline",
420 P_("Style of underline for this text"),
421 PANGO_TYPE_UNDERLINE,
422 PANGO_UNDERLINE_NONE,
423 GTK_PARAM_READWRITE));
425 g_object_class_install_property (object_class,
427 g_param_spec_string ("language",
429 P_("The language this text is in, as an ISO code. "
430 "Pango can use this as a hint when rendering the text. "
431 "If you don't understand this parameter, you probably don't need it"),
433 GTK_PARAM_READWRITE));
437 * GtkCellRendererText:ellipsize:
439 * Specifies the preferred place to ellipsize the string, if the cell renderer
440 * does not have enough room to display the entire string. Setting it to
441 * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
442 * for another way of making the text fit into a given width.
446 g_object_class_install_property (object_class,
448 g_param_spec_enum ("ellipsize",
450 P_("The preferred place to ellipsize the string, "
451 "if the cell renderer does not have enough room "
452 "to display the entire string"),
453 PANGO_TYPE_ELLIPSIZE_MODE,
454 PANGO_ELLIPSIZE_NONE,
455 GTK_PARAM_READWRITE));
458 * GtkCellRendererText:width-chars:
460 * The desired width of the cell, in characters. If this property is set to
461 * -1, the width will be calculated automatically, otherwise the cell will
462 * request either 3 characters or the property value, whichever is greater.
466 g_object_class_install_property (object_class,
468 g_param_spec_int ("width-chars",
469 P_("Width In Characters"),
470 P_("The desired width of the label, in characters"),
474 GTK_PARAM_READWRITE));
478 * GtkCellRendererText:max-width-chars:
480 * The desired maximum width of the cell, in characters. If this property
481 * is set to -1, the width will be calculated automatically.
483 * For cell renderers that ellipsize or wrap text; this property
484 * controls the maximum reported width of the cell. The
485 * cell should not receive any greater allocation unless it is
486 * set to expand in its #GtkCellLayout and all of the cell's siblings
487 * have received their natural width.
491 g_object_class_install_property (object_class,
492 PROP_MAX_WIDTH_CHARS,
493 g_param_spec_int ("max-width-chars",
494 P_("Maximum Width In Characters"),
495 P_("The maximum width of the cell, in characters"),
499 GTK_PARAM_READWRITE));
502 * GtkCellRendererText:wrap-mode:
504 * Specifies how to break the string into multiple lines, if the cell
505 * renderer does not have enough room to display the entire string.
506 * This property has no effect unless the wrap-width property is set.
510 g_object_class_install_property (object_class,
512 g_param_spec_enum ("wrap-mode",
514 P_("How to break the string into multiple lines, "
515 "if the cell renderer does not have enough room "
516 "to display the entire string"),
517 PANGO_TYPE_WRAP_MODE,
519 GTK_PARAM_READWRITE));
522 * GtkCellRendererText:wrap-width:
524 * Specifies the minimum width at which the text is wrapped. The wrap-mode property can
525 * be used to influence at what character positions the line breaks can be placed.
526 * Setting wrap-width to -1 turns wrapping off.
530 g_object_class_install_property (object_class,
532 g_param_spec_int ("wrap-width",
534 P_("The width at which the text is wrapped"),
538 GTK_PARAM_READWRITE));
541 * GtkCellRendererText:alignment:
543 * Specifies how to align the lines of text with respect to each other.
545 * Note that this property describes how to align the lines of text in
546 * case there are several of them. The "xalign" property of #GtkCellRenderer,
547 * on the other hand, sets the horizontal alignment of the whole text.
551 g_object_class_install_property (object_class,
553 g_param_spec_enum ("alignment",
555 P_("How to align the lines"),
556 PANGO_TYPE_ALIGNMENT,
558 GTK_PARAM_READWRITE));
562 /* Style props are set or not */
564 #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))
566 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
567 P_("Background set"),
568 P_("Whether this tag affects the background color"));
570 ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
571 P_("Foreground set"),
572 P_("Whether this tag affects the foreground color"));
574 ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
575 P_("Editability set"),
576 P_("Whether this tag affects text editability"));
578 ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
579 P_("Font family set"),
580 P_("Whether this tag affects the font family"));
582 ADD_SET_PROP ("style-set", PROP_STYLE_SET,
583 P_("Font style set"),
584 P_("Whether this tag affects the font style"));
586 ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
587 P_("Font variant set"),
588 P_("Whether this tag affects the font variant"));
590 ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
591 P_("Font weight set"),
592 P_("Whether this tag affects the font weight"));
594 ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
595 P_("Font stretch set"),
596 P_("Whether this tag affects the font stretch"));
598 ADD_SET_PROP ("size-set", PROP_SIZE_SET,
600 P_("Whether this tag affects the font size"));
602 ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
603 P_("Font scale set"),
604 P_("Whether this tag scales the font size by a factor"));
606 ADD_SET_PROP ("rise-set", PROP_RISE_SET,
608 P_("Whether this tag affects the rise"));
610 ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
611 P_("Strikethrough set"),
612 P_("Whether this tag affects strikethrough"));
614 ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
616 P_("Whether this tag affects underlining"));
618 ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
620 P_("Whether this tag affects the language the text is rendered as"));
622 ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
624 P_("Whether this tag affects the ellipsize mode"));
626 ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
628 P_("Whether this tag affects the alignment mode"));
631 * GtkCellRendererText::edited
632 * @renderer: the object which received the signal
633 * @path: the path identifying the edited cell
634 * @new_text: the new text
636 * This signal is emitted after @renderer has been edited.
638 * It is the responsibility of the application to update the model
639 * and store @new_text at the position indicated by @path.
641 text_cell_renderer_signals [EDITED] =
642 g_signal_new (I_("edited"),
643 G_OBJECT_CLASS_TYPE (object_class),
645 G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
647 _gtk_marshal_VOID__STRING_STRING,
652 g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
656 gtk_cell_renderer_text_finalize (GObject *object)
658 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
659 GtkCellRendererTextPrivate *priv = celltext->priv;
661 pango_font_description_free (priv->font);
665 if (priv->extra_attrs)
666 pango_attr_list_unref (priv->extra_attrs);
669 g_object_unref (priv->language);
671 G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
675 get_property_font_set_mask (guint prop_id)
679 case PROP_FAMILY_SET:
680 return PANGO_FONT_MASK_FAMILY;
682 return PANGO_FONT_MASK_STYLE;
683 case PROP_VARIANT_SET:
684 return PANGO_FONT_MASK_VARIANT;
685 case PROP_WEIGHT_SET:
686 return PANGO_FONT_MASK_WEIGHT;
687 case PROP_STRETCH_SET:
688 return PANGO_FONT_MASK_STRETCH;
690 return PANGO_FONT_MASK_SIZE;
697 gtk_cell_renderer_text_get_property (GObject *object,
702 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
703 GtkCellRendererTextPrivate *priv = celltext->priv;
708 g_value_set_string (value, priv->text);
711 case PROP_ATTRIBUTES:
712 g_value_set_boxed (value, priv->extra_attrs);
715 case PROP_SINGLE_PARAGRAPH_MODE:
716 g_value_set_boolean (value, priv->single_paragraph);
719 case PROP_BACKGROUND_GDK:
723 color.red = priv->background.red;
724 color.green = priv->background.green;
725 color.blue = priv->background.blue;
727 g_value_set_boxed (value, &color);
731 case PROP_FOREGROUND_GDK:
735 color.red = priv->foreground.red;
736 color.green = priv->foreground.green;
737 color.blue = priv->foreground.blue;
739 g_value_set_boxed (value, &color);
744 g_value_take_string (value, pango_font_description_to_string (priv->font));
748 g_value_set_boxed (value, priv->font);
752 g_value_set_string (value, pango_font_description_get_family (priv->font));
756 g_value_set_enum (value, pango_font_description_get_style (priv->font));
760 g_value_set_enum (value, pango_font_description_get_variant (priv->font));
764 g_value_set_int (value, pango_font_description_get_weight (priv->font));
768 g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
772 g_value_set_int (value, pango_font_description_get_size (priv->font));
775 case PROP_SIZE_POINTS:
776 g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
780 g_value_set_double (value, priv->font_scale);
784 g_value_set_boolean (value, priv->editable);
787 case PROP_STRIKETHROUGH:
788 g_value_set_boolean (value, priv->strikethrough);
792 g_value_set_enum (value, priv->underline_style);
796 g_value_set_int (value, priv->rise);
800 g_value_set_static_string (value, pango_language_to_string (priv->language));
804 g_value_set_enum (value, priv->ellipsize);
808 g_value_set_enum (value, priv->wrap_mode);
811 case PROP_WRAP_WIDTH:
812 g_value_set_int (value, priv->wrap_width);
816 g_value_set_enum (value, priv->align);
819 case PROP_BACKGROUND_SET:
820 g_value_set_boolean (value, priv->background_set);
823 case PROP_FOREGROUND_SET:
824 g_value_set_boolean (value, priv->foreground_set);
827 case PROP_FAMILY_SET:
829 case PROP_VARIANT_SET:
830 case PROP_WEIGHT_SET:
831 case PROP_STRETCH_SET:
834 PangoFontMask mask = get_property_font_set_mask (param_id);
835 g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
841 g_value_set_boolean (value, priv->scale_set);
844 case PROP_EDITABLE_SET:
845 g_value_set_boolean (value, priv->editable_set);
848 case PROP_STRIKETHROUGH_SET:
849 g_value_set_boolean (value, priv->strikethrough_set);
852 case PROP_UNDERLINE_SET:
853 g_value_set_boolean (value, priv->underline_set);
857 g_value_set_boolean (value, priv->rise_set);
860 case PROP_LANGUAGE_SET:
861 g_value_set_boolean (value, priv->language_set);
864 case PROP_ELLIPSIZE_SET:
865 g_value_set_boolean (value, priv->ellipsize_set);
869 g_value_set_boolean (value, priv->align_set);
872 case PROP_WIDTH_CHARS:
873 g_value_set_int (value, priv->width_chars);
876 case PROP_MAX_WIDTH_CHARS:
877 g_value_set_int (value, priv->max_width_chars);
880 case PROP_BACKGROUND:
881 case PROP_FOREGROUND:
884 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
891 set_bg_color (GtkCellRendererText *celltext,
894 GtkCellRendererTextPrivate *priv = celltext->priv;
898 if (!priv->background_set)
900 priv->background_set = TRUE;
901 g_object_notify (G_OBJECT (celltext), "background-set");
904 priv->background.red = color->red;
905 priv->background.green = color->green;
906 priv->background.blue = color->blue;
910 if (priv->background_set)
912 priv->background_set = FALSE;
913 g_object_notify (G_OBJECT (celltext), "background-set");
920 set_fg_color (GtkCellRendererText *celltext,
923 GtkCellRendererTextPrivate *priv = celltext->priv;
927 if (!priv->foreground_set)
929 priv->foreground_set = TRUE;
930 g_object_notify (G_OBJECT (celltext), "foreground-set");
933 priv->foreground.red = color->red;
934 priv->foreground.green = color->green;
935 priv->foreground.blue = color->blue;
939 if (priv->foreground_set)
941 priv->foreground_set = FALSE;
942 g_object_notify (G_OBJECT (celltext), "foreground-set");
948 set_font_desc_fields (PangoFontDescription *desc,
949 PangoFontMask to_set)
951 PangoFontMask changed_mask = 0;
953 if (to_set & PANGO_FONT_MASK_FAMILY)
955 const char *family = pango_font_description_get_family (desc);
959 changed_mask |= PANGO_FONT_MASK_FAMILY;
962 pango_font_description_set_family (desc, family);
964 if (to_set & PANGO_FONT_MASK_STYLE)
965 pango_font_description_set_style (desc, pango_font_description_get_style (desc));
966 if (to_set & PANGO_FONT_MASK_VARIANT)
967 pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
968 if (to_set & PANGO_FONT_MASK_WEIGHT)
969 pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
970 if (to_set & PANGO_FONT_MASK_STRETCH)
971 pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
972 if (to_set & PANGO_FONT_MASK_SIZE)
974 gint size = pango_font_description_get_size (desc);
977 size = 10 * PANGO_SCALE;
978 changed_mask |= PANGO_FONT_MASK_SIZE;
981 pango_font_description_set_size (desc, size);
988 notify_set_changed (GObject *object,
989 PangoFontMask changed_mask)
991 if (changed_mask & PANGO_FONT_MASK_FAMILY)
992 g_object_notify (object, "family-set");
993 if (changed_mask & PANGO_FONT_MASK_STYLE)
994 g_object_notify (object, "style-set");
995 if (changed_mask & PANGO_FONT_MASK_VARIANT)
996 g_object_notify (object, "variant-set");
997 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
998 g_object_notify (object, "weight-set");
999 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1000 g_object_notify (object, "stretch-set");
1001 if (changed_mask & PANGO_FONT_MASK_SIZE)
1002 g_object_notify (object, "size-set");
1006 notify_fields_changed (GObject *object,
1007 PangoFontMask changed_mask)
1009 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1010 g_object_notify (object, "family");
1011 if (changed_mask & PANGO_FONT_MASK_STYLE)
1012 g_object_notify (object, "style");
1013 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1014 g_object_notify (object, "variant");
1015 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1016 g_object_notify (object, "weight");
1017 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1018 g_object_notify (object, "stretch");
1019 if (changed_mask & PANGO_FONT_MASK_SIZE)
1020 g_object_notify (object, "size");
1024 set_font_description (GtkCellRendererText *celltext,
1025 PangoFontDescription *font_desc)
1027 GtkCellRendererTextPrivate *priv = celltext->priv;
1028 GObject *object = G_OBJECT (celltext);
1029 PangoFontDescription *new_font_desc;
1030 PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1033 new_font_desc = pango_font_description_copy (font_desc);
1035 new_font_desc = pango_font_description_new ();
1037 old_mask = pango_font_description_get_set_fields (priv->font);
1038 new_mask = pango_font_description_get_set_fields (new_font_desc);
1040 changed_mask = old_mask | new_mask;
1041 set_changed_mask = old_mask ^ new_mask;
1043 pango_font_description_free (priv->font);
1044 priv->font = new_font_desc;
1046 g_object_freeze_notify (object);
1048 g_object_notify (object, "font-desc");
1049 g_object_notify (object, "font");
1051 if (changed_mask & PANGO_FONT_MASK_FAMILY)
1052 g_object_notify (object, "family");
1053 if (changed_mask & PANGO_FONT_MASK_STYLE)
1054 g_object_notify (object, "style");
1055 if (changed_mask & PANGO_FONT_MASK_VARIANT)
1056 g_object_notify (object, "variant");
1057 if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1058 g_object_notify (object, "weight");
1059 if (changed_mask & PANGO_FONT_MASK_STRETCH)
1060 g_object_notify (object, "stretch");
1061 if (changed_mask & PANGO_FONT_MASK_SIZE)
1063 g_object_notify (object, "size");
1064 g_object_notify (object, "size-points");
1067 notify_set_changed (object, set_changed_mask);
1069 g_object_thaw_notify (object);
1073 gtk_cell_renderer_text_set_property (GObject *object,
1075 const GValue *value,
1078 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1079 GtkCellRendererTextPrivate *priv = celltext->priv;
1084 g_free (priv->text);
1086 if (priv->markup_set)
1088 if (priv->extra_attrs)
1089 pango_attr_list_unref (priv->extra_attrs);
1090 priv->extra_attrs = NULL;
1091 priv->markup_set = FALSE;
1094 priv->text = g_value_dup_string (value);
1095 g_object_notify (object, "text");
1098 case PROP_ATTRIBUTES:
1099 if (priv->extra_attrs)
1100 pango_attr_list_unref (priv->extra_attrs);
1102 priv->extra_attrs = g_value_get_boxed (value);
1103 if (priv->extra_attrs)
1104 pango_attr_list_ref (priv->extra_attrs);
1110 GError *error = NULL;
1111 PangoAttrList *attrs = NULL;
1113 str = g_value_get_string (value);
1114 if (str && !pango_parse_markup (str,
1122 g_warning ("Failed to set text from markup due to error parsing markup: %s",
1124 g_error_free (error);
1128 g_free (priv->text);
1130 if (priv->extra_attrs)
1131 pango_attr_list_unref (priv->extra_attrs);
1134 priv->extra_attrs = attrs;
1135 priv->markup_set = TRUE;
1139 case PROP_SINGLE_PARAGRAPH_MODE:
1140 priv->single_paragraph = g_value_get_boolean (value);
1143 case PROP_BACKGROUND:
1147 if (!g_value_get_string (value))
1148 set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
1149 else if (gdk_color_parse (g_value_get_string (value), &color))
1150 set_bg_color (celltext, &color);
1152 g_warning ("Don't know color `%s'", g_value_get_string (value));
1154 g_object_notify (object, "background-gdk");
1158 case PROP_FOREGROUND:
1162 if (!g_value_get_string (value))
1163 set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
1164 else if (gdk_color_parse (g_value_get_string (value), &color))
1165 set_fg_color (celltext, &color);
1167 g_warning ("Don't know color `%s'", g_value_get_string (value));
1169 g_object_notify (object, "foreground-gdk");
1173 case PROP_BACKGROUND_GDK:
1174 /* This notifies the GObject itself. */
1175 set_bg_color (celltext, g_value_get_boxed (value));
1178 case PROP_FOREGROUND_GDK:
1179 /* This notifies the GObject itself. */
1180 set_fg_color (celltext, g_value_get_boxed (value));
1185 PangoFontDescription *font_desc = NULL;
1188 name = g_value_get_string (value);
1191 font_desc = pango_font_description_from_string (name);
1193 set_font_description (celltext, font_desc);
1195 pango_font_description_free (font_desc);
1197 if (priv->fixed_height_rows != -1)
1198 priv->calc_fixed_height = TRUE;
1202 case PROP_FONT_DESC:
1203 set_font_description (celltext, g_value_get_boxed (value));
1205 if (priv->fixed_height_rows != -1)
1206 priv->calc_fixed_height = TRUE;
1215 case PROP_SIZE_POINTS:
1217 PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1222 pango_font_description_set_family (priv->font,
1223 g_value_get_string (value));
1226 pango_font_description_set_style (priv->font,
1227 g_value_get_enum (value));
1230 pango_font_description_set_variant (priv->font,
1231 g_value_get_enum (value));
1234 pango_font_description_set_weight (priv->font,
1235 g_value_get_int (value));
1238 pango_font_description_set_stretch (priv->font,
1239 g_value_get_enum (value));
1242 pango_font_description_set_size (priv->font,
1243 g_value_get_int (value));
1244 g_object_notify (object, "size-points");
1246 case PROP_SIZE_POINTS:
1247 pango_font_description_set_size (priv->font,
1248 g_value_get_double (value) * PANGO_SCALE);
1249 g_object_notify (object, "size");
1253 if (priv->fixed_height_rows != -1)
1254 priv->calc_fixed_height = TRUE;
1256 notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1257 g_object_notify (object, "font-desc");
1258 g_object_notify (object, "font");
1264 priv->font_scale = g_value_get_double (value);
1265 priv->scale_set = TRUE;
1266 if (priv->fixed_height_rows != -1)
1267 priv->calc_fixed_height = TRUE;
1268 g_object_notify (object, "scale-set");
1272 priv->editable = g_value_get_boolean (value);
1273 priv->editable_set = TRUE;
1275 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1277 g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1278 g_object_notify (object, "editable-set");
1281 case PROP_STRIKETHROUGH:
1282 priv->strikethrough = g_value_get_boolean (value);
1283 priv->strikethrough_set = TRUE;
1284 g_object_notify (object, "strikethrough-set");
1287 case PROP_UNDERLINE:
1288 priv->underline_style = g_value_get_enum (value);
1289 priv->underline_set = TRUE;
1290 g_object_notify (object, "underline-set");
1295 priv->rise = g_value_get_int (value);
1296 priv->rise_set = TRUE;
1297 g_object_notify (object, "rise-set");
1298 if (priv->fixed_height_rows != -1)
1299 priv->calc_fixed_height = TRUE;
1303 priv->language_set = TRUE;
1305 g_object_unref (priv->language);
1306 priv->language = pango_language_from_string (g_value_get_string (value));
1307 g_object_notify (object, "language-set");
1310 case PROP_ELLIPSIZE:
1311 priv->ellipsize = g_value_get_enum (value);
1312 priv->ellipsize_set = TRUE;
1313 g_object_notify (object, "ellipsize-set");
1316 case PROP_WRAP_MODE:
1317 priv->wrap_mode = g_value_get_enum (value);
1320 case PROP_WRAP_WIDTH:
1321 priv->wrap_width = g_value_get_int (value);
1324 case PROP_WIDTH_CHARS:
1325 priv->width_chars = g_value_get_int (value);
1328 case PROP_MAX_WIDTH_CHARS:
1329 priv->max_width_chars = g_value_get_int (value);
1333 priv->align = g_value_get_enum (value);
1334 priv->align_set = TRUE;
1335 g_object_notify (object, "align-set");
1338 case PROP_BACKGROUND_SET:
1339 priv->background_set = g_value_get_boolean (value);
1342 case PROP_FOREGROUND_SET:
1343 priv->foreground_set = g_value_get_boolean (value);
1346 case PROP_FAMILY_SET:
1347 case PROP_STYLE_SET:
1348 case PROP_VARIANT_SET:
1349 case PROP_WEIGHT_SET:
1350 case PROP_STRETCH_SET:
1352 if (!g_value_get_boolean (value))
1354 pango_font_description_unset_fields (priv->font,
1355 get_property_font_set_mask (param_id));
1359 PangoFontMask changed_mask;
1361 changed_mask = set_font_desc_fields (priv->font,
1362 get_property_font_set_mask (param_id));
1363 notify_fields_changed (G_OBJECT (celltext), changed_mask);
1367 case PROP_SCALE_SET:
1368 priv->scale_set = g_value_get_boolean (value);
1371 case PROP_EDITABLE_SET:
1372 priv->editable_set = g_value_get_boolean (value);
1375 case PROP_STRIKETHROUGH_SET:
1376 priv->strikethrough_set = g_value_get_boolean (value);
1379 case PROP_UNDERLINE_SET:
1380 priv->underline_set = g_value_get_boolean (value);
1384 priv->rise_set = g_value_get_boolean (value);
1387 case PROP_LANGUAGE_SET:
1388 priv->language_set = g_value_get_boolean (value);
1391 case PROP_ELLIPSIZE_SET:
1392 priv->ellipsize_set = g_value_get_boolean (value);
1395 case PROP_ALIGN_SET:
1396 priv->align_set = g_value_get_boolean (value);
1400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1406 * gtk_cell_renderer_text_new:
1408 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1409 * object properties. Object properties can be
1410 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1411 * you can bind a property to a value in a #GtkTreeModel. For example,
1412 * you can bind the "text" property on the cell renderer to a string
1413 * value in the model, thus rendering a different string in each row
1414 * of the #GtkTreeView
1416 * Return value: the new cell renderer
1419 gtk_cell_renderer_text_new (void)
1421 return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1425 add_attr (PangoAttrList *attr_list,
1426 PangoAttribute *attr)
1428 attr->start_index = 0;
1429 attr->end_index = G_MAXINT;
1431 pango_attr_list_insert (attr_list, attr);
1435 get_layout (GtkCellRendererText *celltext,
1437 GdkRectangle *cell_area,
1438 GtkCellRendererState flags)
1440 GtkCellRendererTextPrivate *priv = celltext->priv;
1441 PangoAttrList *attr_list;
1442 PangoLayout *layout;
1443 PangoUnderline uline;
1446 layout = gtk_widget_create_pango_layout (widget, priv->text);
1448 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1450 if (priv->extra_attrs)
1451 attr_list = pango_attr_list_copy (priv->extra_attrs);
1453 attr_list = pango_attr_list_new ();
1455 pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1459 /* Add options that affect appearance but not size */
1461 /* note that background doesn't go here, since it affects
1462 * background_area not the PangoLayout area
1465 if (priv->foreground_set
1466 && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1470 color = priv->foreground;
1472 add_attr (attr_list,
1473 pango_attr_foreground_new (color.red, color.green, color.blue));
1476 if (priv->strikethrough_set)
1477 add_attr (attr_list,
1478 pango_attr_strikethrough_new (priv->strikethrough));
1481 add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1483 if (priv->scale_set &&
1484 priv->font_scale != 1.0)
1485 add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1487 if (priv->underline_set)
1488 uline = priv->underline_style;
1490 uline = PANGO_UNDERLINE_NONE;
1492 if (priv->language_set)
1493 add_attr (attr_list, pango_attr_language_new (priv->language));
1495 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1499 case PANGO_UNDERLINE_NONE:
1500 uline = PANGO_UNDERLINE_SINGLE;
1503 case PANGO_UNDERLINE_SINGLE:
1504 uline = PANGO_UNDERLINE_DOUBLE;
1512 if (uline != PANGO_UNDERLINE_NONE)
1513 add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1516 add_attr (attr_list, pango_attr_rise_new (priv->rise));
1518 /* Now apply the attributes as they will effect the outcome
1519 * of pango_layout_get_extents() */
1520 pango_layout_set_attributes (layout, attr_list);
1521 pango_attr_list_unref (attr_list);
1523 if (priv->ellipsize_set)
1524 pango_layout_set_ellipsize (layout, priv->ellipsize);
1526 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1528 if (priv->wrap_width != -1)
1530 PangoRectangle rect;
1531 gint width, text_width;
1533 pango_layout_get_extents (layout, NULL, &rect);
1534 text_width = rect.width;
1537 width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1539 width = priv->wrap_width * PANGO_SCALE;
1541 width = MIN (width, text_width);
1543 pango_layout_set_width (layout, width);
1544 pango_layout_set_wrap (layout, priv->wrap_mode);
1548 pango_layout_set_width (layout, -1);
1549 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1552 if (priv->align_set)
1553 pango_layout_set_alignment (layout, priv->align);
1556 PangoAlignment align;
1558 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1559 align = PANGO_ALIGN_RIGHT;
1561 align = PANGO_ALIGN_LEFT;
1563 pango_layout_set_alignment (layout, align);
1571 get_size (GtkCellRenderer *cell,
1573 GdkRectangle *cell_area,
1574 PangoLayout *layout,
1580 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1581 GtkCellRendererTextPrivate *priv = celltext->priv;
1582 PangoRectangle rect;
1584 gint cell_width, cell_height;
1586 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1588 if (priv->calc_fixed_height)
1590 PangoContext *context;
1591 PangoFontMetrics *metrics;
1592 PangoFontDescription *font_desc;
1595 font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1596 pango_font_description_merge_static (font_desc, priv->font, TRUE);
1598 if (priv->scale_set)
1599 pango_font_description_set_size (font_desc,
1600 priv->font_scale * pango_font_description_get_size (font_desc));
1602 context = gtk_widget_get_pango_context (widget);
1604 metrics = pango_context_get_metrics (context,
1606 pango_context_get_language (context));
1607 row_height = (pango_font_metrics_get_ascent (metrics) +
1608 pango_font_metrics_get_descent (metrics));
1609 pango_font_metrics_unref (metrics);
1611 pango_font_description_free (font_desc);
1613 gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1615 gtk_cell_renderer_set_fixed_size (cell,
1616 cell_width, 2 * ypad +
1617 priv->fixed_height_rows * PANGO_PIXELS (row_height));
1621 *height = cell_height;
1624 priv->calc_fixed_height = FALSE;
1630 g_object_ref (layout);
1632 layout = get_layout (celltext, widget, NULL, 0);
1634 pango_layout_get_pixel_extents (layout, NULL, &rect);
1637 *height = ypad * 2 + rect.height;
1639 /* The minimum size for ellipsized labels is ~ 3 chars */
1642 if (priv->ellipsize || priv->width_chars > 0)
1644 PangoContext *context;
1645 PangoFontMetrics *metrics;
1648 context = pango_layout_get_context (layout);
1649 metrics = pango_context_get_metrics (context,
1650 gtk_widget_get_style (widget)->font_desc,
1651 pango_context_get_language (context));
1653 char_width = pango_font_metrics_get_approximate_char_width (metrics);
1654 pango_font_metrics_unref (metrics);
1656 *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1660 *width = xpad * 2 + rect.x + rect.width;
1666 gfloat xalign, yalign;
1668 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1672 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1673 *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1675 *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1677 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1678 *x_offset = MAX(*x_offset, 0);
1682 *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1683 *y_offset = MAX (*y_offset, 0);
1688 if (x_offset) *x_offset = 0;
1689 if (y_offset) *y_offset = 0;
1692 g_object_unref (layout);
1696 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
1697 GdkDrawable *window,
1699 GdkRectangle *background_area,
1700 GdkRectangle *cell_area,
1701 GdkRectangle *expose_area,
1702 GtkCellRendererState flags)
1705 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1706 GtkCellRendererTextPrivate *priv = celltext->priv;
1707 PangoLayout *layout;
1713 layout = get_layout (celltext, widget, cell_area, flags);
1714 get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1716 if (!gtk_cell_renderer_get_sensitive (cell))
1718 state = GTK_STATE_INSENSITIVE;
1720 else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1722 if (gtk_widget_has_focus (widget))
1723 state = GTK_STATE_SELECTED;
1725 state = GTK_STATE_ACTIVE;
1727 else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1728 gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1730 state = GTK_STATE_PRELIGHT;
1734 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1735 state = GTK_STATE_INSENSITIVE;
1737 state = GTK_STATE_NORMAL;
1740 if (priv->background_set &&
1741 (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1743 cairo_t *cr = gdk_cairo_create (window);
1747 gdk_cairo_rectangle (cr, expose_area);
1751 gdk_cairo_rectangle (cr, background_area);
1752 cairo_set_source_rgb (cr,
1753 priv->background.red / 65535.,
1754 priv->background.green / 65535.,
1755 priv->background.blue / 65535.);
1761 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1763 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1764 pango_layout_set_width (layout,
1765 (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1766 else if (priv->wrap_width == -1)
1767 pango_layout_set_width (layout, -1);
1769 gtk_paint_layout (gtk_widget_get_style (widget),
1776 cell_area->x + x_offset + xpad,
1777 cell_area->y + y_offset + ypad,
1780 g_object_unref (layout);
1784 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1787 GtkCellRendererTextPrivate *priv;
1789 const gchar *new_text;
1792 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1796 if (priv->focus_out_id > 0)
1798 g_signal_handler_disconnect (entry, priv->focus_out_id);
1799 priv->focus_out_id = 0;
1802 if (priv->populate_popup_id > 0)
1804 g_signal_handler_disconnect (entry, priv->populate_popup_id);
1805 priv->populate_popup_id = 0;
1808 if (priv->entry_menu_popdown_timeout)
1810 g_source_remove (priv->entry_menu_popdown_timeout);
1811 priv->entry_menu_popdown_timeout = 0;
1814 g_object_get (entry,
1815 "editing-canceled", &canceled,
1817 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1822 path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1823 new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1825 g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1829 popdown_timeout (gpointer data)
1831 GtkCellRendererTextPrivate *priv;
1833 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1835 priv->entry_menu_popdown_timeout = 0;
1837 if (!gtk_widget_has_focus (priv->entry))
1838 gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1844 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1847 GtkCellRendererTextPrivate *priv;
1849 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1851 priv->in_entry_menu = FALSE;
1853 if (priv->entry_menu_popdown_timeout)
1856 priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1861 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1865 GtkCellRendererTextPrivate *priv;
1867 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1869 if (priv->entry_menu_popdown_timeout)
1871 g_source_remove (priv->entry_menu_popdown_timeout);
1872 priv->entry_menu_popdown_timeout = 0;
1875 priv->in_entry_menu = TRUE;
1877 g_signal_connect (menu, "unmap",
1878 G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1882 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1886 GtkCellRendererTextPrivate *priv;
1888 priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1890 if (priv->in_entry_menu)
1893 g_object_set (entry,
1894 "editing-canceled", TRUE,
1896 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1897 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1899 /* entry needs focus-out-event */
1903 static GtkCellEditable *
1904 gtk_cell_renderer_text_start_editing (GtkCellRenderer *cell,
1908 GdkRectangle *background_area,
1909 GdkRectangle *cell_area,
1910 GtkCellRendererState flags)
1912 GtkRequisition requisition;
1913 GtkCellRendererText *celltext;
1914 GtkCellRendererTextPrivate *priv;
1915 gfloat xalign, yalign;
1917 celltext = GTK_CELL_RENDERER_TEXT (cell);
1918 priv = celltext->priv;
1920 /* If the cell isn't editable we return NULL. */
1921 if (priv->editable == FALSE)
1924 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1926 priv->entry = gtk_entry_new ();
1927 gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1928 gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1931 gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1932 g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1934 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1936 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->entry),
1937 &requisition, NULL);
1938 if (requisition.height < cell_area->height)
1940 GtkBorder *style_border;
1943 gtk_widget_style_get (priv->entry,
1944 "inner-border", &style_border,
1949 border = *style_border;
1950 g_boxed_free (GTK_TYPE_BORDER, style_border);
1954 /* Since boxed style properties can't have default values ... */
1959 border.top = (cell_area->height - requisition.height) / 2;
1960 border.bottom = (cell_area->height - requisition.height) / 2;
1961 gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
1964 priv->in_entry_menu = FALSE;
1965 if (priv->entry_menu_popdown_timeout)
1967 g_source_remove (priv->entry_menu_popdown_timeout);
1968 priv->entry_menu_popdown_timeout = 0;
1971 g_signal_connect (priv->entry,
1973 G_CALLBACK (gtk_cell_renderer_text_editing_done),
1975 priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
1976 G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1978 priv->populate_popup_id =
1979 g_signal_connect (priv->entry, "populate-popup",
1980 G_CALLBACK (gtk_cell_renderer_text_populate_popup),
1983 gtk_widget_show (priv->entry);
1985 return GTK_CELL_EDITABLE (priv->entry);
1989 * gtk_cell_renderer_text_set_fixed_height_from_font:
1990 * @renderer: A #GtkCellRendererText
1991 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1993 * Sets the height of a renderer to explicitly be determined by the "font" and
1994 * "y_pad" property set on it. Further changes in these properties do not
1995 * affect the height, so they must be accompanied by a subsequent call to this
1996 * function. Using this function is unflexible, and should really only be used
1997 * if calculating the size of a cell is too slow (ie, a massive number of cells
1998 * displayed). If @number_of_rows is -1, then the fixed height is unset, and
1999 * the height is determined by the properties again.
2002 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2003 gint number_of_rows)
2005 GtkCellRendererTextPrivate *priv;
2006 GtkCellRenderer *cell;
2008 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2009 g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2011 cell = GTK_CELL_RENDERER (renderer);
2012 priv = renderer->priv;
2014 if (number_of_rows == -1)
2018 gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2019 gtk_cell_renderer_set_fixed_size (cell, width, -1);
2023 priv->fixed_height_rows = number_of_rows;
2024 priv->calc_fixed_height = TRUE;
2029 gtk_cell_renderer_text_cell_size_request_init (GtkCellSizeRequestIface *iface)
2031 iface->get_width = gtk_cell_renderer_text_get_width;
2032 iface->get_height = gtk_cell_renderer_text_get_height;
2033 iface->get_height_for_width = gtk_cell_renderer_text_get_height_for_width;
2037 gtk_cell_renderer_text_get_width (GtkCellSizeRequest *cell,
2042 GtkCellRendererTextPrivate *priv;
2043 GtkCellRendererText *celltext;
2045 PangoLayout *layout;
2046 PangoContext *context;
2047 PangoFontMetrics *metrics;
2048 PangoRectangle rect;
2049 gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2050 gint min_width, nat_width;
2052 /* "width-chars" Hard-coded minimum width:
2053 * - minimum size should be MAX (width-chars, strlen ("..."));
2054 * - natural size should be MAX (width-chars, strlen (label->text));
2056 * "wrap-width" User specified natural wrap width
2057 * - minimum size should be MAX (width-chars, 0)
2058 * - natural size should be MIN (wrap-width, strlen (label->text))
2061 celltext = GTK_CELL_RENDERER_TEXT (cell);
2062 priv = celltext->priv;
2064 style = gtk_widget_get_style (widget);
2066 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, NULL);
2068 layout = get_layout (celltext, widget, NULL, 0);
2070 /* Get the layout with the text possibly wrapping at wrap_width */
2071 pango_layout_get_pixel_extents (layout, NULL, &rect);
2072 guess_width = rect.width;
2074 /* Fetch the length of the complete unwrapped text */
2075 pango_layout_set_width (layout, -1);
2076 pango_layout_get_extents (layout, NULL, &rect);
2077 text_width = rect.width;
2079 /* Fetch the average size of a charachter */
2080 context = pango_layout_get_context (layout);
2081 metrics = pango_context_get_metrics (context, style->font_desc,
2082 pango_context_get_language (context));
2084 char_width = pango_font_metrics_get_approximate_char_width (metrics);
2085 digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2086 char_pixels = MAX (char_width, digit_width);
2088 pango_font_metrics_unref (metrics);
2089 g_object_unref (layout);
2091 /* enforce minimum width for ellipsized labels at ~3 chars */
2092 if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2093 ellipsize_chars = 3;
2095 ellipsize_chars = 0;
2097 if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2099 xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2100 /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2101 else if (priv->wrap_width > -1)
2102 min_width = xpad * 2 + rect.x + priv->wrap_width;
2104 min_width = xpad * 2 + rect.x + guess_width;
2106 if (priv->width_chars > 0)
2107 nat_width = xpad * 2 +
2108 MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2110 nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2112 nat_width = MAX (nat_width, min_width);
2114 if (priv->max_width_chars > 0)
2116 gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2118 min_width = MIN (min_width, max_width);
2119 nat_width = MIN (nat_width, max_width);
2123 *minimum_size = min_width;
2126 *natural_size = nat_width;
2131 gtk_cell_renderer_text_get_height_for_width (GtkCellSizeRequest *cell,
2134 gint *minimum_height,
2135 gint *natural_height)
2137 GtkCellRendererTextPrivate *priv;
2138 GtkCellRendererText *celltext;
2139 PangoLayout *layout;
2140 gint text_height, xpad, ypad;
2143 celltext = GTK_CELL_RENDERER_TEXT (cell);
2144 priv = celltext->priv;
2146 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cell), &xpad, &ypad);
2148 layout = get_layout (celltext, widget, FALSE, 0);
2150 pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2151 pango_layout_get_pixel_size (layout, NULL, &text_height);
2154 *minimum_height = text_height + ypad * 2;
2157 *natural_height = text_height + ypad * 2;
2159 g_object_unref (layout);
2163 gtk_cell_renderer_text_get_height (GtkCellSizeRequest *cell,
2170 /* Thankfully cell renderers dont rotate, so they only have to do
2171 * height-for-width and not the opposite. Here we have only to return
2172 * the height for the base minimum width of the renderer.
2174 * Note this code path wont be followed by GtkTreeView which is
2175 * height-for-width specifically.
2177 gtk_cell_size_request_get_width (cell, widget, &min_width, NULL);
2178 gtk_cell_renderer_text_get_height_for_width (cell, widget, min_width,
2179 minimum_size, natural_size);