]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderertext.c
Merge branch 'master' into treeview-refactor
[~andy/gtk] / gtk / gtkcellrenderertext.c
1 /* gtkcellrenderertext.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "gtkcellrenderertext.h"
23
24 #include <stdlib.h>
25
26 #include "gtkeditable.h"
27 #include "gtkentry.h"
28 #include "gtksizerequest.h"
29 #include "gtkmarshalers.h"
30 #include "gtkintl.h"
31 #include "gtkprivate.h"
32 #include "gtktreeprivate.h"
33
34
35 static void gtk_cell_renderer_text_finalize   (GObject                  *object);
36
37 static void gtk_cell_renderer_text_get_property  (GObject                  *object,
38                                                   guint                     param_id,
39                                                   GValue                   *value,
40                                                   GParamSpec               *pspec);
41 static void gtk_cell_renderer_text_set_property  (GObject                  *object,
42                                                   guint                     param_id,
43                                                   const GValue             *value,
44                                                   GParamSpec               *pspec);
45 static void gtk_cell_renderer_text_render     (GtkCellRenderer          *cell,
46                                                cairo_t                  *cr,
47                                                GtkWidget                *widget,
48                                                const GdkRectangle       *background_area,
49                                                const GdkRectangle       *cell_area,
50                                                GtkCellRendererState      flags);
51
52 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
53                                                               GdkEvent             *event,
54                                                               GtkWidget            *widget,
55                                                               const gchar          *path,
56                                                               const GdkRectangle   *background_area,
57                                                               const GdkRectangle   *cell_area,
58                                                               GtkCellRendererState  flags);
59
60 static void       gtk_cell_renderer_text_get_preferred_width            (GtkCellRenderer       *cell,
61                                                                          GtkWidget             *widget,
62                                                                          gint                  *minimal_size,
63                                                                          gint                  *natural_size);
64 static void       gtk_cell_renderer_text_get_preferred_height           (GtkCellRenderer       *cell,
65                                                                          GtkWidget             *widget,
66                                                                          gint                  *minimal_size,
67                                                                          gint                  *natural_size);
68 static void       gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer       *cell,
69                                                                          GtkWidget             *widget,
70                                                                          gint                   width,
71                                                                          gint                  *minimum_height,
72                                                                          gint                  *natural_height);
73
74 enum {
75   EDITED,
76   LAST_SIGNAL
77 };
78
79 enum {
80   PROP_0,
81
82   PROP_TEXT,
83   PROP_MARKUP,
84   PROP_ATTRIBUTES,
85   PROP_SINGLE_PARAGRAPH_MODE,
86   PROP_WIDTH_CHARS,
87   PROP_MAX_WIDTH_CHARS,
88   PROP_WRAP_WIDTH,
89   PROP_ALIGN,
90   
91   /* Style args */
92   PROP_BACKGROUND,
93   PROP_FOREGROUND,
94   PROP_BACKGROUND_GDK,
95   PROP_FOREGROUND_GDK,
96   PROP_BACKGROUND_RGBA,
97   PROP_FOREGROUND_RGBA,
98   PROP_FONT,
99   PROP_FONT_DESC,
100   PROP_FAMILY,
101   PROP_STYLE,
102   PROP_VARIANT,
103   PROP_WEIGHT,
104   PROP_STRETCH,
105   PROP_SIZE,
106   PROP_SIZE_POINTS,
107   PROP_SCALE,
108   PROP_EDITABLE,
109   PROP_STRIKETHROUGH,
110   PROP_UNDERLINE,
111   PROP_RISE,
112   PROP_LANGUAGE,
113   PROP_ELLIPSIZE,
114   PROP_WRAP_MODE,
115   
116   /* Whether-a-style-arg-is-set args */
117   PROP_BACKGROUND_SET,
118   PROP_FOREGROUND_SET,
119   PROP_FAMILY_SET,
120   PROP_STYLE_SET,
121   PROP_VARIANT_SET,
122   PROP_WEIGHT_SET,
123   PROP_STRETCH_SET,
124   PROP_SIZE_SET,
125   PROP_SCALE_SET,
126   PROP_EDITABLE_SET,
127   PROP_STRIKETHROUGH_SET,
128   PROP_UNDERLINE_SET,
129   PROP_RISE_SET,
130   PROP_LANGUAGE_SET,
131   PROP_ELLIPSIZE_SET,
132   PROP_ALIGN_SET
133 };
134
135 static guint text_cell_renderer_signals [LAST_SIGNAL];
136
137 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
138
139 struct _GtkCellRendererTextPrivate
140 {
141   GtkWidget *entry;
142
143   PangoAlignment        align;
144   PangoAttrList        *extra_attrs;
145   GdkRGBA               foreground;
146   GdkRGBA               background;
147   PangoEllipsizeMode    ellipsize;
148   PangoFontDescription *font;
149   PangoLanguage        *language;
150   PangoUnderline        underline_style;
151   PangoWrapMode         wrap_mode;
152
153   gboolean in_entry_menu;
154
155   gchar *text;
156
157   gdouble font_scale;
158
159   gint rise;
160   gint fixed_height_rows;
161   gint width_chars;
162   gint max_width_chars;
163   gint wrap_width;
164
165   guint strikethrough     : 1;
166   guint editable          : 1;
167   guint scale_set         : 1;
168   guint foreground_set    : 1;
169   guint background_set    : 1;
170   guint underline_set     : 1;
171   guint rise_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;
179   guint align_set         : 1;
180
181   gulong focus_out_id;
182   gulong populate_popup_id;
183   gulong entry_menu_popdown_timeout;
184 };
185
186 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
187
188 static void
189 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
190 {
191   GtkCellRendererTextPrivate *priv;
192   GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
193
194   celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
195                                                 GTK_TYPE_CELL_RENDERER_TEXT,
196                                                 GtkCellRendererTextPrivate);
197   priv = celltext->priv;
198
199   gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
200   gtk_cell_renderer_set_padding (cell, 2, 2);
201   priv->font_scale = 1.0;
202   priv->fixed_height_rows = -1;
203   priv->font = pango_font_description_new ();
204
205   priv->width_chars = -1;
206   priv->max_width_chars = -1;
207   priv->wrap_width = -1;
208   priv->wrap_mode = PANGO_WRAP_CHAR;
209   priv->align = PANGO_ALIGN_LEFT;
210   priv->align_set = FALSE;
211 }
212
213 static void
214 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
215 {
216   GObjectClass *object_class = G_OBJECT_CLASS (class);
217   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
218
219   object_class->finalize = gtk_cell_renderer_text_finalize;
220   
221   object_class->get_property = gtk_cell_renderer_text_get_property;
222   object_class->set_property = gtk_cell_renderer_text_set_property;
223
224   cell_class->render = gtk_cell_renderer_text_render;
225   cell_class->start_editing = gtk_cell_renderer_text_start_editing;
226   cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
227   cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
228   cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
229
230   g_object_class_install_property (object_class,
231                                    PROP_TEXT,
232                                    g_param_spec_string ("text",
233                                                         P_("Text"),
234                                                         P_("Text to render"),
235                                                         NULL,
236                                                         GTK_PARAM_READWRITE));
237   
238   g_object_class_install_property (object_class,
239                                    PROP_MARKUP,
240                                    g_param_spec_string ("markup",
241                                                         P_("Markup"),
242                                                         P_("Marked up text to render"),
243                                                         NULL,
244                                                         GTK_PARAM_WRITABLE));
245
246   g_object_class_install_property (object_class,
247                                    PROP_ATTRIBUTES,
248                                    g_param_spec_boxed ("attributes",
249                                                        P_("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));
253
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"),
259                                                          FALSE,
260                                                          GTK_PARAM_READWRITE));
261
262   
263   g_object_class_install_property (object_class,
264                                    PROP_BACKGROUND,
265                                    g_param_spec_string ("background",
266                                                         P_("Background color name"),
267                                                         P_("Background color as a string"),
268                                                         NULL,
269                                                         GTK_PARAM_WRITABLE));
270
271   g_object_class_install_property (object_class,
272                                    PROP_BACKGROUND_GDK,
273                                    g_param_spec_boxed ("background-gdk",
274                                                        P_("Background color"),
275                                                        P_("Background color as a GdkColor"),
276                                                        GDK_TYPE_COLOR,
277                                                        GTK_PARAM_READWRITE));  
278
279   /**
280    * GtkCellRendererText:background-rgba:
281    *
282    * Background color as a #GdkRGBA
283    *
284    * Since: 3.0
285    */
286   g_object_class_install_property (object_class,
287                                    PROP_BACKGROUND_RGBA,
288                                    g_param_spec_boxed ("background-rgba",
289                                                        P_("Background color as RGBA"),
290                                                        P_("Background color as a GdkRGBA"),
291                                                        GDK_TYPE_RGBA,
292                                                        GTK_PARAM_READWRITE));
293   g_object_class_install_property (object_class,
294                                    PROP_FOREGROUND,
295                                    g_param_spec_string ("foreground",
296                                                         P_("Foreground color name"),
297                                                         P_("Foreground color as a string"),
298                                                         NULL,
299                                                         GTK_PARAM_WRITABLE));
300
301   g_object_class_install_property (object_class,
302                                    PROP_FOREGROUND_GDK,
303                                    g_param_spec_boxed ("foreground-gdk",
304                                                        P_("Foreground color"),
305                                                        P_("Foreground color as a GdkColor"),
306                                                        GDK_TYPE_COLOR,
307                                                        GTK_PARAM_READWRITE));
308
309   /**
310    * GtkCellRendererText:foreground-rgba:
311    *
312    * Foreground color as a #GdkRGBA
313    *
314    * Since: 3.0
315    */
316   g_object_class_install_property (object_class,
317                                    PROP_FOREGROUND_RGBA,
318                                    g_param_spec_boxed ("foreground-rgba",
319                                                        P_("Foreground color as RGBA"),
320                                                        P_("Foreground color as a GdkRGBA"),
321                                                        GDK_TYPE_RGBA,
322                                                        GTK_PARAM_READWRITE));
323
324
325   g_object_class_install_property (object_class,
326                                    PROP_EDITABLE,
327                                    g_param_spec_boolean ("editable",
328                                                          P_("Editable"),
329                                                          P_("Whether the text can be modified by the user"),
330                                                          FALSE,
331                                                          GTK_PARAM_READWRITE));
332
333   g_object_class_install_property (object_class,
334                                    PROP_FONT,
335                                    g_param_spec_string ("font",
336                                                         P_("Font"),
337                                                         P_("Font description as a string, e.g. \"Sans Italic 12\""),
338                                                         NULL,
339                                                         GTK_PARAM_READWRITE));
340
341   g_object_class_install_property (object_class,
342                                    PROP_FONT_DESC,
343                                    g_param_spec_boxed ("font-desc",
344                                                        P_("Font"),
345                                                        P_("Font description as a PangoFontDescription struct"),
346                                                        PANGO_TYPE_FONT_DESCRIPTION,
347                                                        GTK_PARAM_READWRITE));
348
349   
350   g_object_class_install_property (object_class,
351                                    PROP_FAMILY,
352                                    g_param_spec_string ("family",
353                                                         P_("Font family"),
354                                                         P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
355                                                         NULL,
356                                                         GTK_PARAM_READWRITE));
357
358   g_object_class_install_property (object_class,
359                                    PROP_STYLE,
360                                    g_param_spec_enum ("style",
361                                                       P_("Font style"),
362                                                       P_("Font style"),
363                                                       PANGO_TYPE_STYLE,
364                                                       PANGO_STYLE_NORMAL,
365                                                       GTK_PARAM_READWRITE));
366
367   g_object_class_install_property (object_class,
368                                    PROP_VARIANT,
369                                    g_param_spec_enum ("variant",
370                                                      P_("Font variant"),
371                                                      P_("Font variant"),
372                                                       PANGO_TYPE_VARIANT,
373                                                       PANGO_VARIANT_NORMAL,
374                                                       GTK_PARAM_READWRITE));
375   
376   g_object_class_install_property (object_class,
377                                    PROP_WEIGHT,
378                                    g_param_spec_int ("weight",
379                                                      P_("Font weight"),
380                                                      P_("Font weight"),
381                                                      0,
382                                                      G_MAXINT,
383                                                      PANGO_WEIGHT_NORMAL,
384                                                      GTK_PARAM_READWRITE));
385
386    g_object_class_install_property (object_class,
387                                    PROP_STRETCH,
388                                    g_param_spec_enum ("stretch",
389                                                       P_("Font stretch"),
390                                                       P_("Font stretch"),
391                                                       PANGO_TYPE_STRETCH,
392                                                       PANGO_STRETCH_NORMAL,
393                                                       GTK_PARAM_READWRITE));
394   
395   g_object_class_install_property (object_class,
396                                    PROP_SIZE,
397                                    g_param_spec_int ("size",
398                                                      P_("Font size"),
399                                                      P_("Font size"),
400                                                      0,
401                                                      G_MAXINT,
402                                                      0,
403                                                      GTK_PARAM_READWRITE));
404
405   g_object_class_install_property (object_class,
406                                    PROP_SIZE_POINTS,
407                                    g_param_spec_double ("size-points",
408                                                         P_("Font points"),
409                                                         P_("Font size in points"),
410                                                         0.0,
411                                                         G_MAXDOUBLE,
412                                                         0.0,
413                                                         GTK_PARAM_READWRITE));  
414
415   g_object_class_install_property (object_class,
416                                    PROP_SCALE,
417                                    g_param_spec_double ("scale",
418                                                         P_("Font scale"),
419                                                         P_("Font scaling factor"),
420                                                         0.0,
421                                                         G_MAXDOUBLE,
422                                                         1.0,
423                                                         GTK_PARAM_READWRITE));
424   
425   g_object_class_install_property (object_class,
426                                    PROP_RISE,
427                                    g_param_spec_int ("rise",
428                                                      P_("Rise"),
429                                                      P_("Offset of text above the baseline "
430                                                         "(below the baseline if rise is negative)"),
431                                                      -G_MAXINT,
432                                                      G_MAXINT,
433                                                      0,
434                                                      GTK_PARAM_READWRITE));
435
436
437   g_object_class_install_property (object_class,
438                                    PROP_STRIKETHROUGH,
439                                    g_param_spec_boolean ("strikethrough",
440                                                          P_("Strikethrough"),
441                                                          P_("Whether to strike through the text"),
442                                                          FALSE,
443                                                          GTK_PARAM_READWRITE));
444   
445   g_object_class_install_property (object_class,
446                                    PROP_UNDERLINE,
447                                    g_param_spec_enum ("underline",
448                                                       P_("Underline"),
449                                                       P_("Style of underline for this text"),
450                                                       PANGO_TYPE_UNDERLINE,
451                                                       PANGO_UNDERLINE_NONE,
452                                                       GTK_PARAM_READWRITE));
453
454   g_object_class_install_property (object_class,
455                                    PROP_LANGUAGE,
456                                    g_param_spec_string ("language",
457                                                         P_("Language"),
458                                                         P_("The language this text is in, as an ISO code. "
459                                                            "Pango can use this as a hint when rendering the text. "
460                                                            "If you don't understand this parameter, you probably don't need it"),
461                                                         NULL,
462                                                         GTK_PARAM_READWRITE));
463
464
465   /**
466    * GtkCellRendererText:ellipsize:
467    *
468    * Specifies the preferred place to ellipsize the string, if the cell renderer 
469    * does not have enough room to display the entire string. Setting it to 
470    * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
471    * for another way of making the text fit into a given width.
472    *
473    * Since: 2.6
474    */
475   g_object_class_install_property (object_class,
476                                    PROP_ELLIPSIZE,
477                                    g_param_spec_enum ("ellipsize",
478                                                       P_("Ellipsize"),
479                                                       P_("The preferred place to ellipsize the string, "
480                                                          "if the cell renderer does not have enough room "
481                                                          "to display the entire string"),
482                                                       PANGO_TYPE_ELLIPSIZE_MODE,
483                                                       PANGO_ELLIPSIZE_NONE,
484                                                       GTK_PARAM_READWRITE));
485
486   /**
487    * GtkCellRendererText:width-chars:
488    * 
489    * The desired width of the cell, in characters. If this property is set to
490    * -1, the width will be calculated automatically, otherwise the cell will
491    * request either 3 characters or the property value, whichever is greater.
492    * 
493    * Since: 2.6
494    **/
495   g_object_class_install_property (object_class,
496                                    PROP_WIDTH_CHARS,
497                                    g_param_spec_int ("width-chars",
498                                                      P_("Width In Characters"),
499                                                      P_("The desired width of the label, in characters"),
500                                                      -1,
501                                                      G_MAXINT,
502                                                      -1,
503                                                      GTK_PARAM_READWRITE));
504   
505
506   /**
507    * GtkCellRendererText:max-width-chars:
508    * 
509    * The desired maximum width of the cell, in characters. If this property 
510    * is set to -1, the width will be calculated automatically.
511    *
512    * For cell renderers that ellipsize or wrap text; this property
513    * controls the maximum reported width of the cell. The
514    * cell should not receive any greater allocation unless it is
515    * set to expand in its #GtkCellLayout and all of the cell's siblings
516    * have received their natural width.
517    *
518    * Since: 3.0
519    **/
520   g_object_class_install_property (object_class,
521                                    PROP_MAX_WIDTH_CHARS,
522                                    g_param_spec_int ("max-width-chars",
523                                                      P_("Maximum Width In Characters"),
524                                                      P_("The maximum width of the cell, in characters"),
525                                                      -1,
526                                                      G_MAXINT,
527                                                      -1,
528                                                      GTK_PARAM_READWRITE));
529   
530   /**
531    * GtkCellRendererText:wrap-mode:
532    *
533    * Specifies how to break the string into multiple lines, if the cell 
534    * renderer does not have enough room to display the entire string. 
535    * This property has no effect unless the wrap-width property is set.
536    *
537    * Since: 2.8
538    */
539   g_object_class_install_property (object_class,
540                                    PROP_WRAP_MODE,
541                                    g_param_spec_enum ("wrap-mode",
542                                                       P_("Wrap mode"),
543                                                       P_("How to break the string into multiple lines, "
544                                                          "if the cell renderer does not have enough room "
545                                                          "to display the entire string"),
546                                                       PANGO_TYPE_WRAP_MODE,
547                                                       PANGO_WRAP_CHAR,
548                                                       GTK_PARAM_READWRITE));
549
550   /**
551    * GtkCellRendererText:wrap-width:
552    *
553    * Specifies the minimum width at which the text is wrapped. The wrap-mode property can 
554    * be used to influence at what character positions the line breaks can be placed.
555    * Setting wrap-width to -1 turns wrapping off.
556    *
557    * Since: 2.8
558    */
559   g_object_class_install_property (object_class,
560                                    PROP_WRAP_WIDTH,
561                                    g_param_spec_int ("wrap-width",
562                                                      P_("Wrap width"),
563                                                      P_("The width at which the text is wrapped"),
564                                                      -1,
565                                                      G_MAXINT,
566                                                      -1,
567                                                      GTK_PARAM_READWRITE));
568
569   /**
570    * GtkCellRendererText:alignment:
571    *
572    * Specifies how to align the lines of text with respect to each other. 
573    *
574    * Note that this property describes how to align the lines of text in 
575    * case there are several of them. The "xalign" property of #GtkCellRenderer, 
576    * on the other hand, sets the horizontal alignment of the whole text.
577    *
578    * Since: 2.10
579    */
580   g_object_class_install_property (object_class,
581                                    PROP_ALIGN,
582                                    g_param_spec_enum ("alignment",
583                                                       P_("Alignment"),
584                                                       P_("How to align the lines"),
585                                                       PANGO_TYPE_ALIGNMENT,
586                                                       PANGO_ALIGN_LEFT,
587                                                       GTK_PARAM_READWRITE));
588   
589
590
591   /* Style props are set or not */
592
593 #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))
594
595   ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
596                 P_("Background set"),
597                 P_("Whether this tag affects the background color"));
598
599   ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
600                 P_("Foreground set"),
601                 P_("Whether this tag affects the foreground color"));
602   
603   ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
604                 P_("Editability set"),
605                 P_("Whether this tag affects text editability"));
606
607   ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
608                 P_("Font family set"),
609                 P_("Whether this tag affects the font family"));  
610
611   ADD_SET_PROP ("style-set", PROP_STYLE_SET,
612                 P_("Font style set"),
613                 P_("Whether this tag affects the font style"));
614
615   ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
616                 P_("Font variant set"),
617                 P_("Whether this tag affects the font variant"));
618
619   ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
620                 P_("Font weight set"),
621                 P_("Whether this tag affects the font weight"));
622
623   ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
624                 P_("Font stretch set"),
625                 P_("Whether this tag affects the font stretch"));
626
627   ADD_SET_PROP ("size-set", PROP_SIZE_SET,
628                 P_("Font size set"),
629                 P_("Whether this tag affects the font size"));
630
631   ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
632                 P_("Font scale set"),
633                 P_("Whether this tag scales the font size by a factor"));
634   
635   ADD_SET_PROP ("rise-set", PROP_RISE_SET,
636                 P_("Rise set"),
637                 P_("Whether this tag affects the rise"));
638
639   ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
640                 P_("Strikethrough set"),
641                 P_("Whether this tag affects strikethrough"));
642
643   ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
644                 P_("Underline set"),
645                 P_("Whether this tag affects underlining"));
646
647   ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
648                 P_("Language set"),
649                 P_("Whether this tag affects the language the text is rendered as"));
650
651   ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
652                 P_("Ellipsize set"),
653                 P_("Whether this tag affects the ellipsize mode"));
654
655   ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
656                 P_("Align set"),
657                 P_("Whether this tag affects the alignment mode"));
658
659   /**
660    * GtkCellRendererText::edited
661    * @renderer: the object which received the signal
662    * @path: the path identifying the edited cell
663    * @new_text: the new text
664    *
665    * This signal is emitted after @renderer has been edited.
666    *
667    * It is the responsibility of the application to update the model
668    * and store @new_text at the position indicated by @path.
669    */
670   text_cell_renderer_signals [EDITED] =
671     g_signal_new (I_("edited"),
672                   G_OBJECT_CLASS_TYPE (object_class),
673                   G_SIGNAL_RUN_LAST,
674                   G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
675                   NULL, NULL,
676                   _gtk_marshal_VOID__STRING_STRING,
677                   G_TYPE_NONE, 2,
678                   G_TYPE_STRING,
679                   G_TYPE_STRING);
680
681   g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
682 }
683
684 static void
685 gtk_cell_renderer_text_finalize (GObject *object)
686 {
687   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
688   GtkCellRendererTextPrivate *priv = celltext->priv;
689
690   pango_font_description_free (priv->font);
691
692   g_free (priv->text);
693
694   if (priv->extra_attrs)
695     pango_attr_list_unref (priv->extra_attrs);
696
697   if (priv->language)
698     g_object_unref (priv->language);
699
700   G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
701 }
702
703 static PangoFontMask
704 get_property_font_set_mask (guint prop_id)
705 {
706   switch (prop_id)
707     {
708     case PROP_FAMILY_SET:
709       return PANGO_FONT_MASK_FAMILY;
710     case PROP_STYLE_SET:
711       return PANGO_FONT_MASK_STYLE;
712     case PROP_VARIANT_SET:
713       return PANGO_FONT_MASK_VARIANT;
714     case PROP_WEIGHT_SET:
715       return PANGO_FONT_MASK_WEIGHT;
716     case PROP_STRETCH_SET:
717       return PANGO_FONT_MASK_STRETCH;
718     case PROP_SIZE_SET:
719       return PANGO_FONT_MASK_SIZE;
720     }
721
722   return 0;
723 }
724
725 static void
726 gtk_cell_renderer_text_get_property (GObject        *object,
727                                      guint           param_id,
728                                      GValue         *value,
729                                      GParamSpec     *pspec)
730 {
731   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
732   GtkCellRendererTextPrivate *priv = celltext->priv;
733
734   switch (param_id)
735     {
736     case PROP_TEXT:
737       g_value_set_string (value, priv->text);
738       break;
739
740     case PROP_ATTRIBUTES:
741       g_value_set_boxed (value, priv->extra_attrs);
742       break;
743
744     case PROP_SINGLE_PARAGRAPH_MODE:
745       g_value_set_boolean (value, priv->single_paragraph);
746       break;
747
748     case PROP_BACKGROUND_GDK:
749       {
750         GdkColor color;
751
752         color.red = (guint16) (priv->background.red * 65535);
753         color.green = (guint16) (priv->background.green * 65535);
754         color.blue = (guint16) (priv->background.blue * 65535);
755
756         g_value_set_boxed (value, &color);
757       }
758       break;
759
760     case PROP_FOREGROUND_GDK:
761       {
762         GdkColor color;
763
764         color.red = (guint16) (priv->foreground.red * 65535);
765         color.green = (guint16) (priv->foreground.green * 65535);
766         color.blue = (guint16) (priv->foreground.blue * 65535);
767
768         g_value_set_boxed (value, &color);
769       }
770       break;
771
772     case PROP_BACKGROUND_RGBA:
773       g_value_set_boxed (value, &priv->background);
774       break;
775
776     case PROP_FOREGROUND_RGBA:
777       g_value_set_boxed (value, &priv->foreground);
778       break;
779
780     case PROP_FONT:
781         g_value_take_string (value, pango_font_description_to_string (priv->font));
782       break;
783       
784     case PROP_FONT_DESC:
785       g_value_set_boxed (value, priv->font);
786       break;
787
788     case PROP_FAMILY:
789       g_value_set_string (value, pango_font_description_get_family (priv->font));
790       break;
791
792     case PROP_STYLE:
793       g_value_set_enum (value, pango_font_description_get_style (priv->font));
794       break;
795
796     case PROP_VARIANT:
797       g_value_set_enum (value, pango_font_description_get_variant (priv->font));
798       break;
799
800     case PROP_WEIGHT:
801       g_value_set_int (value, pango_font_description_get_weight (priv->font));
802       break;
803
804     case PROP_STRETCH:
805       g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
806       break;
807
808     case PROP_SIZE:
809       g_value_set_int (value, pango_font_description_get_size (priv->font));
810       break;
811
812     case PROP_SIZE_POINTS:
813       g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
814       break;
815
816     case PROP_SCALE:
817       g_value_set_double (value, priv->font_scale);
818       break;
819       
820     case PROP_EDITABLE:
821       g_value_set_boolean (value, priv->editable);
822       break;
823
824     case PROP_STRIKETHROUGH:
825       g_value_set_boolean (value, priv->strikethrough);
826       break;
827
828     case PROP_UNDERLINE:
829       g_value_set_enum (value, priv->underline_style);
830       break;
831
832     case PROP_RISE:
833       g_value_set_int (value, priv->rise);
834       break;  
835
836     case PROP_LANGUAGE:
837       g_value_set_static_string (value, pango_language_to_string (priv->language));
838       break;
839
840     case PROP_ELLIPSIZE:
841       g_value_set_enum (value, priv->ellipsize);
842       break;
843       
844     case PROP_WRAP_MODE:
845       g_value_set_enum (value, priv->wrap_mode);
846       break;
847
848     case PROP_WRAP_WIDTH:
849       g_value_set_int (value, priv->wrap_width);
850       break;
851       
852     case PROP_ALIGN:
853       g_value_set_enum (value, priv->align);
854       break;
855
856     case PROP_BACKGROUND_SET:
857       g_value_set_boolean (value, priv->background_set);
858       break;
859
860     case PROP_FOREGROUND_SET:
861       g_value_set_boolean (value, priv->foreground_set);
862       break;
863
864     case PROP_FAMILY_SET:
865     case PROP_STYLE_SET:
866     case PROP_VARIANT_SET:
867     case PROP_WEIGHT_SET:
868     case PROP_STRETCH_SET:
869     case PROP_SIZE_SET:
870       {
871         PangoFontMask mask = get_property_font_set_mask (param_id);
872         g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
873         
874         break;
875       }
876
877     case PROP_SCALE_SET:
878       g_value_set_boolean (value, priv->scale_set);
879       break;
880       
881     case PROP_EDITABLE_SET:
882       g_value_set_boolean (value, priv->editable_set);
883       break;
884
885     case PROP_STRIKETHROUGH_SET:
886       g_value_set_boolean (value, priv->strikethrough_set);
887       break;
888
889     case PROP_UNDERLINE_SET:
890       g_value_set_boolean (value, priv->underline_set);
891       break;
892
893     case  PROP_RISE_SET:
894       g_value_set_boolean (value, priv->rise_set);
895       break;
896
897     case PROP_LANGUAGE_SET:
898       g_value_set_boolean (value, priv->language_set);
899       break;
900
901     case PROP_ELLIPSIZE_SET:
902       g_value_set_boolean (value, priv->ellipsize_set);
903       break;
904
905     case PROP_ALIGN_SET:
906       g_value_set_boolean (value, priv->align_set);
907       break;
908       
909     case PROP_WIDTH_CHARS:
910       g_value_set_int (value, priv->width_chars);
911       break;  
912
913     case PROP_MAX_WIDTH_CHARS:
914       g_value_set_int (value, priv->max_width_chars);
915       break;  
916
917     case PROP_BACKGROUND:
918     case PROP_FOREGROUND:
919     case PROP_MARKUP:
920     default:
921       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
922       break;
923     }
924 }
925
926
927 static void
928 set_bg_color (GtkCellRendererText *celltext,
929               GdkRGBA             *rgba)
930 {
931   GtkCellRendererTextPrivate *priv = celltext->priv;
932
933   if (rgba)
934     {
935       if (!priv->background_set)
936         {
937           priv->background_set = TRUE;
938           g_object_notify (G_OBJECT (celltext), "background-set");
939         }
940
941       priv->background = *rgba;
942     }
943   else
944     {
945       if (priv->background_set)
946         {
947           priv->background_set = FALSE;
948           g_object_notify (G_OBJECT (celltext), "background-set");
949         }
950     }
951 }
952
953
954 static void
955 set_fg_color (GtkCellRendererText *celltext,
956               GdkRGBA             *rgba)
957 {
958   GtkCellRendererTextPrivate *priv = celltext->priv;
959
960   if (rgba)
961     {
962       if (!priv->foreground_set)
963         {
964           priv->foreground_set = TRUE;
965           g_object_notify (G_OBJECT (celltext), "foreground-set");
966         }
967
968       priv->foreground = *rgba;
969     }
970   else
971     {
972       if (priv->foreground_set)
973         {
974           priv->foreground_set = FALSE;
975           g_object_notify (G_OBJECT (celltext), "foreground-set");
976         }
977     }
978 }
979
980 static PangoFontMask
981 set_font_desc_fields (PangoFontDescription *desc,
982                       PangoFontMask         to_set)
983 {
984   PangoFontMask changed_mask = 0;
985   
986   if (to_set & PANGO_FONT_MASK_FAMILY)
987     {
988       const char *family = pango_font_description_get_family (desc);
989       if (!family)
990         {
991           family = "sans";
992           changed_mask |= PANGO_FONT_MASK_FAMILY;
993         }
994
995       pango_font_description_set_family (desc, family);
996     }
997   if (to_set & PANGO_FONT_MASK_STYLE)
998     pango_font_description_set_style (desc, pango_font_description_get_style (desc));
999   if (to_set & PANGO_FONT_MASK_VARIANT)
1000     pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1001   if (to_set & PANGO_FONT_MASK_WEIGHT)
1002     pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1003   if (to_set & PANGO_FONT_MASK_STRETCH)
1004     pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1005   if (to_set & PANGO_FONT_MASK_SIZE)
1006     {
1007       gint size = pango_font_description_get_size (desc);
1008       if (size <= 0)
1009         {
1010           size = 10 * PANGO_SCALE;
1011           changed_mask |= PANGO_FONT_MASK_SIZE;
1012         }
1013       
1014       pango_font_description_set_size (desc, size);
1015     }
1016
1017   return changed_mask;
1018 }
1019
1020 static void
1021 notify_set_changed (GObject       *object,
1022                     PangoFontMask  changed_mask)
1023 {
1024   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1025     g_object_notify (object, "family-set");
1026   if (changed_mask & PANGO_FONT_MASK_STYLE)
1027     g_object_notify (object, "style-set");
1028   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1029     g_object_notify (object, "variant-set");
1030   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1031     g_object_notify (object, "weight-set");
1032   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1033     g_object_notify (object, "stretch-set");
1034   if (changed_mask & PANGO_FONT_MASK_SIZE)
1035     g_object_notify (object, "size-set");
1036 }
1037
1038 static void
1039 notify_fields_changed (GObject       *object,
1040                        PangoFontMask  changed_mask)
1041 {
1042   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1043     g_object_notify (object, "family");
1044   if (changed_mask & PANGO_FONT_MASK_STYLE)
1045     g_object_notify (object, "style");
1046   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1047     g_object_notify (object, "variant");
1048   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1049     g_object_notify (object, "weight");
1050   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1051     g_object_notify (object, "stretch");
1052   if (changed_mask & PANGO_FONT_MASK_SIZE)
1053     g_object_notify (object, "size");
1054 }
1055
1056 static void
1057 set_font_description (GtkCellRendererText  *celltext,
1058                       PangoFontDescription *font_desc)
1059 {
1060   GtkCellRendererTextPrivate *priv = celltext->priv;
1061   GObject *object = G_OBJECT (celltext);
1062   PangoFontDescription *new_font_desc;
1063   PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1064   
1065   if (font_desc)
1066     new_font_desc = pango_font_description_copy (font_desc);
1067   else
1068     new_font_desc = pango_font_description_new ();
1069
1070   old_mask = pango_font_description_get_set_fields (priv->font);
1071   new_mask = pango_font_description_get_set_fields (new_font_desc);
1072
1073   changed_mask = old_mask | new_mask;
1074   set_changed_mask = old_mask ^ new_mask;
1075
1076   pango_font_description_free (priv->font);
1077   priv->font = new_font_desc;
1078   
1079   g_object_freeze_notify (object);
1080
1081   g_object_notify (object, "font-desc");
1082   g_object_notify (object, "font");
1083   
1084   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1085     g_object_notify (object, "family");
1086   if (changed_mask & PANGO_FONT_MASK_STYLE)
1087     g_object_notify (object, "style");
1088   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1089     g_object_notify (object, "variant");
1090   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1091     g_object_notify (object, "weight");
1092   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1093     g_object_notify (object, "stretch");
1094   if (changed_mask & PANGO_FONT_MASK_SIZE)
1095     {
1096       g_object_notify (object, "size");
1097       g_object_notify (object, "size-points");
1098     }
1099
1100   notify_set_changed (object, set_changed_mask);
1101   
1102   g_object_thaw_notify (object);
1103 }
1104
1105 static void
1106 gtk_cell_renderer_text_set_property (GObject      *object,
1107                                      guint         param_id,
1108                                      const GValue *value,
1109                                      GParamSpec   *pspec)
1110 {
1111   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1112   GtkCellRendererTextPrivate *priv = celltext->priv;
1113
1114   switch (param_id)
1115     {
1116     case PROP_TEXT:
1117       g_free (priv->text);
1118
1119       if (priv->markup_set)
1120         {
1121           if (priv->extra_attrs)
1122             pango_attr_list_unref (priv->extra_attrs);
1123           priv->extra_attrs = NULL;
1124           priv->markup_set = FALSE;
1125         }
1126
1127       priv->text = g_value_dup_string (value);
1128       g_object_notify (object, "text");
1129       break;
1130
1131     case PROP_ATTRIBUTES:
1132       if (priv->extra_attrs)
1133         pango_attr_list_unref (priv->extra_attrs);
1134
1135       priv->extra_attrs = g_value_get_boxed (value);
1136       if (priv->extra_attrs)
1137         pango_attr_list_ref (priv->extra_attrs);
1138       break;
1139     case PROP_MARKUP:
1140       {
1141         const gchar *str;
1142         gchar *text = NULL;
1143         GError *error = NULL;
1144         PangoAttrList *attrs = NULL;
1145
1146         str = g_value_get_string (value);
1147         if (str && !pango_parse_markup (str,
1148                                         -1,
1149                                         0,
1150                                         &attrs,
1151                                         &text,
1152                                         NULL,
1153                                         &error))
1154           {
1155             g_warning ("Failed to set text from markup due to error parsing markup: %s",
1156                        error->message);
1157             g_error_free (error);
1158             return;
1159           }
1160
1161         g_free (priv->text);
1162
1163         if (priv->extra_attrs)
1164           pango_attr_list_unref (priv->extra_attrs);
1165
1166         priv->text = text;
1167         priv->extra_attrs = attrs;
1168         priv->markup_set = TRUE;
1169       }
1170       break;
1171
1172     case PROP_SINGLE_PARAGRAPH_MODE:
1173       priv->single_paragraph = g_value_get_boolean (value);
1174       break;
1175       
1176     case PROP_BACKGROUND:
1177       {
1178         GdkRGBA rgba;
1179
1180         if (!g_value_get_string (value))
1181           set_bg_color (celltext, NULL);       /* reset to background_set to FALSE */
1182         else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
1183           set_bg_color (celltext, &rgba);
1184         else
1185           g_warning ("Don't know color `%s'", g_value_get_string (value));
1186
1187         g_object_notify (object, "background-gdk");
1188       }
1189       break;
1190       
1191     case PROP_FOREGROUND:
1192       {
1193         GdkRGBA rgba;
1194
1195         if (!g_value_get_string (value))
1196           set_fg_color (celltext, NULL);       /* reset to foreground_set to FALSE */
1197         else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
1198           set_fg_color (celltext, &rgba);
1199         else
1200           g_warning ("Don't know color `%s'", g_value_get_string (value));
1201
1202         g_object_notify (object, "foreground-gdk");
1203       }
1204       break;
1205
1206     case PROP_BACKGROUND_GDK:
1207       {
1208         GdkColor *color;
1209         GdkRGBA rgba;
1210
1211         color = g_value_get_boxed (value);
1212         rgba.red = color->red / 65535.;
1213         rgba.green = color->green / 65535.;
1214         rgba.blue = color->blue / 65535.;
1215
1216         set_bg_color (celltext, &rgba);
1217       }
1218       break;
1219
1220     case PROP_FOREGROUND_GDK:
1221       {
1222         GdkColor *color;
1223         GdkRGBA rgba;
1224
1225         color = g_value_get_boxed (value);
1226         rgba.red = color->red / 65535.;
1227         rgba.green = color->green / 65535.;
1228         rgba.blue = color->blue / 65535.;
1229
1230         set_fg_color (celltext, &rgba);
1231       }
1232       break;
1233
1234     case PROP_BACKGROUND_RGBA:
1235       /* This notifies the GObject itself. */
1236       set_bg_color (celltext, g_value_get_boxed (value));
1237       break;
1238
1239     case PROP_FOREGROUND_RGBA:
1240       /* This notifies the GObject itself. */
1241       set_fg_color (celltext, g_value_get_boxed (value));
1242       break;
1243
1244     case PROP_FONT:
1245       {
1246         PangoFontDescription *font_desc = NULL;
1247         const gchar *name;
1248
1249         name = g_value_get_string (value);
1250
1251         if (name)
1252           font_desc = pango_font_description_from_string (name);
1253
1254         set_font_description (celltext, font_desc);
1255
1256         pango_font_description_free (font_desc);
1257         
1258         if (priv->fixed_height_rows != -1)
1259           priv->calc_fixed_height = TRUE;
1260       }
1261       break;
1262
1263     case PROP_FONT_DESC:
1264       set_font_description (celltext, g_value_get_boxed (value));
1265       
1266       if (priv->fixed_height_rows != -1)
1267         priv->calc_fixed_height = TRUE;
1268       break;
1269
1270     case PROP_FAMILY:
1271     case PROP_STYLE:
1272     case PROP_VARIANT:
1273     case PROP_WEIGHT:
1274     case PROP_STRETCH:
1275     case PROP_SIZE:
1276     case PROP_SIZE_POINTS:
1277       {
1278         PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1279         
1280         switch (param_id)
1281           {
1282           case PROP_FAMILY:
1283             pango_font_description_set_family (priv->font,
1284                                                g_value_get_string (value));
1285             break;
1286           case PROP_STYLE:
1287             pango_font_description_set_style (priv->font,
1288                                               g_value_get_enum (value));
1289             break;
1290           case PROP_VARIANT:
1291             pango_font_description_set_variant (priv->font,
1292                                                 g_value_get_enum (value));
1293             break;
1294           case PROP_WEIGHT:
1295             pango_font_description_set_weight (priv->font,
1296                                                g_value_get_int (value));
1297             break;
1298           case PROP_STRETCH:
1299             pango_font_description_set_stretch (priv->font,
1300                                                 g_value_get_enum (value));
1301             break;
1302           case PROP_SIZE:
1303             pango_font_description_set_size (priv->font,
1304                                              g_value_get_int (value));
1305             g_object_notify (object, "size-points");
1306             break;
1307           case PROP_SIZE_POINTS:
1308             pango_font_description_set_size (priv->font,
1309                                              g_value_get_double (value) * PANGO_SCALE);
1310             g_object_notify (object, "size");
1311             break;
1312           }
1313         
1314         if (priv->fixed_height_rows != -1)
1315           priv->calc_fixed_height = TRUE;
1316         
1317         notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1318         g_object_notify (object, "font-desc");
1319         g_object_notify (object, "font");
1320
1321         break;
1322       }
1323       
1324     case PROP_SCALE:
1325       priv->font_scale = g_value_get_double (value);
1326       priv->scale_set = TRUE;
1327       if (priv->fixed_height_rows != -1)
1328         priv->calc_fixed_height = TRUE;
1329       g_object_notify (object, "scale-set");
1330       break;
1331       
1332     case PROP_EDITABLE:
1333       priv->editable = g_value_get_boolean (value);
1334       priv->editable_set = TRUE;
1335       if (priv->editable)
1336         g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1337       else
1338         g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1339       g_object_notify (object, "editable-set");
1340       break;
1341
1342     case PROP_STRIKETHROUGH:
1343       priv->strikethrough = g_value_get_boolean (value);
1344       priv->strikethrough_set = TRUE;
1345       g_object_notify (object, "strikethrough-set");
1346       break;
1347
1348     case PROP_UNDERLINE:
1349       priv->underline_style = g_value_get_enum (value);
1350       priv->underline_set = TRUE;
1351       g_object_notify (object, "underline-set");
1352             
1353       break;
1354
1355     case PROP_RISE:
1356       priv->rise = g_value_get_int (value);
1357       priv->rise_set = TRUE;
1358       g_object_notify (object, "rise-set");
1359       if (priv->fixed_height_rows != -1)
1360         priv->calc_fixed_height = TRUE;
1361       break;  
1362
1363     case PROP_LANGUAGE:
1364       priv->language_set = TRUE;
1365       if (priv->language)
1366         g_object_unref (priv->language);
1367       priv->language = pango_language_from_string (g_value_get_string (value));
1368       g_object_notify (object, "language-set");
1369       break;
1370
1371     case PROP_ELLIPSIZE:
1372       priv->ellipsize = g_value_get_enum (value);
1373       priv->ellipsize_set = TRUE;
1374       g_object_notify (object, "ellipsize-set");
1375       break;
1376       
1377     case PROP_WRAP_MODE:
1378       priv->wrap_mode = g_value_get_enum (value);
1379       break;
1380       
1381     case PROP_WRAP_WIDTH:
1382       priv->wrap_width = g_value_get_int (value);
1383       break;
1384             
1385     case PROP_WIDTH_CHARS:
1386       priv->width_chars = g_value_get_int (value);
1387       break;  
1388
1389     case PROP_MAX_WIDTH_CHARS:
1390       priv->max_width_chars = g_value_get_int (value);
1391       break;  
1392
1393     case PROP_ALIGN:
1394       priv->align = g_value_get_enum (value);
1395       priv->align_set = TRUE;
1396       g_object_notify (object, "align-set");
1397       break;
1398
1399     case PROP_BACKGROUND_SET:
1400       priv->background_set = g_value_get_boolean (value);
1401       break;
1402
1403     case PROP_FOREGROUND_SET:
1404       priv->foreground_set = g_value_get_boolean (value);
1405       break;
1406
1407     case PROP_FAMILY_SET:
1408     case PROP_STYLE_SET:
1409     case PROP_VARIANT_SET:
1410     case PROP_WEIGHT_SET:
1411     case PROP_STRETCH_SET:
1412     case PROP_SIZE_SET:
1413       if (!g_value_get_boolean (value))
1414         {
1415           pango_font_description_unset_fields (priv->font,
1416                                                get_property_font_set_mask (param_id));
1417         }
1418       else
1419         {
1420           PangoFontMask changed_mask;
1421           
1422           changed_mask = set_font_desc_fields (priv->font,
1423                                                get_property_font_set_mask (param_id));
1424           notify_fields_changed (G_OBJECT (celltext), changed_mask);
1425         }
1426       break;
1427
1428     case PROP_SCALE_SET:
1429       priv->scale_set = g_value_get_boolean (value);
1430       break;
1431       
1432     case PROP_EDITABLE_SET:
1433       priv->editable_set = g_value_get_boolean (value);
1434       break;
1435
1436     case PROP_STRIKETHROUGH_SET:
1437       priv->strikethrough_set = g_value_get_boolean (value);
1438       break;
1439
1440     case PROP_UNDERLINE_SET:
1441       priv->underline_set = g_value_get_boolean (value);
1442       break;
1443
1444     case PROP_RISE_SET:
1445       priv->rise_set = g_value_get_boolean (value);
1446       break;
1447
1448     case PROP_LANGUAGE_SET:
1449       priv->language_set = g_value_get_boolean (value);
1450       break;
1451
1452     case PROP_ELLIPSIZE_SET:
1453       priv->ellipsize_set = g_value_get_boolean (value);
1454       break;
1455
1456     case PROP_ALIGN_SET:
1457       priv->align_set = g_value_get_boolean (value);
1458       break;
1459       
1460     default:
1461       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1462       break;
1463     }
1464 }
1465
1466 /**
1467  * gtk_cell_renderer_text_new:
1468  * 
1469  * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1470  * object properties. Object properties can be
1471  * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1472  * you can bind a property to a value in a #GtkTreeModel. For example,
1473  * you can bind the "text" property on the cell renderer to a string
1474  * value in the model, thus rendering a different string in each row
1475  * of the #GtkTreeView
1476  * 
1477  * Return value: the new cell renderer
1478  **/
1479 GtkCellRenderer *
1480 gtk_cell_renderer_text_new (void)
1481 {
1482   return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1483 }
1484
1485 static void
1486 add_attr (PangoAttrList  *attr_list,
1487           PangoAttribute *attr)
1488 {
1489   attr->start_index = 0;
1490   attr->end_index = G_MAXINT;
1491   
1492   pango_attr_list_insert (attr_list, attr);
1493 }
1494
1495 static PangoLayout*
1496 get_layout (GtkCellRendererText *celltext,
1497             GtkWidget           *widget,
1498             const GdkRectangle  *cell_area,
1499             GtkCellRendererState flags)
1500 {
1501   GtkCellRendererTextPrivate *priv = celltext->priv;
1502   PangoAttrList *attr_list;
1503   PangoLayout *layout;
1504   PangoUnderline uline;
1505   gint xpad;
1506
1507   layout = gtk_widget_create_pango_layout (widget, priv->text);
1508
1509   gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1510
1511   if (priv->extra_attrs)
1512     attr_list = pango_attr_list_copy (priv->extra_attrs);
1513   else
1514     attr_list = pango_attr_list_new ();
1515
1516   pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1517
1518   if (cell_area)
1519     {
1520       /* Add options that affect appearance but not size */
1521       
1522       /* note that background doesn't go here, since it affects
1523        * background_area not the PangoLayout area
1524        */
1525       
1526       if (priv->foreground_set
1527           && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1528         {
1529           PangoColor color;
1530
1531           color.red = (guint16) (priv->foreground.red * 65535);
1532           color.green = (guint16) (priv->foreground.green * 65535);
1533           color.blue = (guint16) (priv->foreground.blue * 65535);
1534
1535           add_attr (attr_list,
1536                     pango_attr_foreground_new (color.red, color.green, color.blue));
1537         }
1538
1539       if (priv->strikethrough_set)
1540         add_attr (attr_list,
1541                   pango_attr_strikethrough_new (priv->strikethrough));
1542     }
1543
1544   add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1545
1546   if (priv->scale_set &&
1547       priv->font_scale != 1.0)
1548     add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1549   
1550   if (priv->underline_set)
1551     uline = priv->underline_style;
1552   else
1553     uline = PANGO_UNDERLINE_NONE;
1554
1555   if (priv->language_set)
1556     add_attr (attr_list, pango_attr_language_new (priv->language));
1557   
1558   if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1559     {
1560       switch (uline)
1561         {
1562         case PANGO_UNDERLINE_NONE:
1563           uline = PANGO_UNDERLINE_SINGLE;
1564           break;
1565
1566         case PANGO_UNDERLINE_SINGLE:
1567           uline = PANGO_UNDERLINE_DOUBLE;
1568           break;
1569
1570         default:
1571           break;
1572         }
1573     }
1574
1575   if (uline != PANGO_UNDERLINE_NONE)
1576     add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1577
1578   if (priv->rise_set)
1579     add_attr (attr_list, pango_attr_rise_new (priv->rise));
1580
1581   /* Now apply the attributes as they will effect the outcome
1582    * of pango_layout_get_extents() */
1583   pango_layout_set_attributes (layout, attr_list);
1584   pango_attr_list_unref (attr_list);
1585
1586   if (priv->ellipsize_set)
1587     pango_layout_set_ellipsize (layout, priv->ellipsize);
1588   else
1589     pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1590
1591   if (priv->wrap_width != -1)
1592     {
1593       PangoRectangle rect;
1594       gint           width, text_width;
1595
1596       pango_layout_get_extents (layout, NULL, &rect);
1597       text_width = rect.width;
1598
1599       if (cell_area)
1600         width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1601       else
1602         width = priv->wrap_width * PANGO_SCALE;
1603
1604       width = MIN (width, text_width);
1605
1606       pango_layout_set_width (layout, width);
1607       pango_layout_set_wrap (layout, priv->wrap_mode);
1608     }
1609   else
1610     {
1611       pango_layout_set_width (layout, -1);
1612       pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1613     }
1614
1615   if (priv->align_set)
1616     pango_layout_set_alignment (layout, priv->align);
1617   else
1618     {
1619       PangoAlignment align;
1620
1621       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1622         align = PANGO_ALIGN_RIGHT;
1623       else
1624         align = PANGO_ALIGN_LEFT;
1625
1626       pango_layout_set_alignment (layout, align);
1627     }
1628   
1629   return layout;
1630 }
1631
1632
1633 static void
1634 get_size (GtkCellRenderer    *cell,
1635           GtkWidget          *widget,
1636           const GdkRectangle *cell_area,
1637           PangoLayout        *layout,
1638           gint               *x_offset,
1639           gint               *y_offset,
1640           gint               *width,
1641           gint               *height)
1642 {
1643   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1644   GtkCellRendererTextPrivate *priv = celltext->priv;
1645   PangoRectangle rect;
1646   gint xpad, ypad;
1647   gint cell_width, cell_height;
1648
1649   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1650
1651   if (priv->calc_fixed_height)
1652     {
1653       PangoContext *context;
1654       PangoFontMetrics *metrics;
1655       PangoFontDescription *font_desc;
1656       gint row_height;
1657
1658       font_desc = pango_font_description_copy_static (gtk_widget_get_style (widget)->font_desc);
1659       pango_font_description_merge_static (font_desc, priv->font, TRUE);
1660
1661       if (priv->scale_set)
1662         pango_font_description_set_size (font_desc,
1663                                          priv->font_scale * pango_font_description_get_size (font_desc));
1664
1665       context = gtk_widget_get_pango_context (widget);
1666
1667       metrics = pango_context_get_metrics (context,
1668                                            font_desc,
1669                                            pango_context_get_language (context));
1670       row_height = (pango_font_metrics_get_ascent (metrics) +
1671                     pango_font_metrics_get_descent (metrics));
1672       pango_font_metrics_unref (metrics);
1673
1674       pango_font_description_free (font_desc);
1675
1676       gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1677
1678       gtk_cell_renderer_set_fixed_size (cell,
1679                                         cell_width, 2 * ypad +
1680                                         priv->fixed_height_rows * PANGO_PIXELS (row_height));
1681       
1682       if (height)
1683         {
1684           *height = cell_height;
1685           height = NULL;
1686         }
1687       priv->calc_fixed_height = FALSE;
1688       if (width == NULL)
1689         return;
1690     }
1691   
1692   if (layout)
1693     g_object_ref (layout);
1694   else
1695     layout = get_layout (celltext, widget, NULL, 0);
1696
1697   pango_layout_get_pixel_extents (layout, NULL, &rect);
1698
1699   if (height)
1700     *height = ypad * 2 + rect.height;
1701
1702   /* The minimum size for ellipsized labels is ~ 3 chars */
1703   if (width)
1704     {
1705       if (priv->ellipsize || priv->width_chars > 0)
1706         {
1707           PangoContext *context;
1708           PangoFontMetrics *metrics;
1709           gint char_width;
1710
1711           context = pango_layout_get_context (layout);
1712           metrics = pango_context_get_metrics (context,
1713                                                gtk_widget_get_style (widget)->font_desc,
1714                                                pango_context_get_language (context));
1715
1716           char_width = pango_font_metrics_get_approximate_char_width (metrics);
1717           pango_font_metrics_unref (metrics);
1718           
1719           *width = xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, 3));
1720         }
1721       else
1722         {
1723           *width = xpad * 2 + rect.x + rect.width;
1724         }         
1725     }
1726
1727   if (cell_area)
1728     {
1729       gfloat xalign, yalign;
1730
1731       gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1732
1733       if (x_offset)
1734         {
1735           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1736             *x_offset = (1.0 - xalign) * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1737           else 
1738             *x_offset = xalign * (cell_area->width - (rect.x + rect.width + (2 * xpad)));
1739
1740           if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1741             *x_offset = MAX(*x_offset, 0);
1742         }
1743       if (y_offset)
1744         {
1745           *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1746           *y_offset = MAX (*y_offset, 0);
1747         }
1748     }
1749   else
1750     {
1751       if (x_offset) *x_offset = 0;
1752       if (y_offset) *y_offset = 0;
1753     }
1754
1755   g_object_unref (layout);
1756 }
1757
1758 static void
1759 gtk_cell_renderer_text_render (GtkCellRenderer      *cell,
1760                                cairo_t              *cr,
1761                                GtkWidget            *widget,
1762                                const GdkRectangle   *background_area,
1763                                const GdkRectangle   *cell_area,
1764                                GtkCellRendererState  flags)
1765
1766 {
1767   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1768   GtkCellRendererTextPrivate *priv = celltext->priv;
1769   PangoLayout *layout;
1770   GtkStateType state;
1771   gint x_offset = 0;
1772   gint y_offset = 0;
1773   gint xpad, ypad;
1774
1775   layout = get_layout (celltext, widget, cell_area, flags);
1776   get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1777
1778   if (!gtk_cell_renderer_get_sensitive (cell))
1779     {
1780       state = GTK_STATE_INSENSITIVE;
1781     }
1782   else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1783     {
1784       if (gtk_widget_has_focus (widget))
1785         state = GTK_STATE_SELECTED;
1786       else
1787         state = GTK_STATE_ACTIVE;
1788     }
1789   else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1790            gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
1791     {
1792       state = GTK_STATE_PRELIGHT;
1793     }
1794   else
1795     {
1796       if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
1797         state = GTK_STATE_INSENSITIVE;
1798       else
1799         state = GTK_STATE_NORMAL;
1800     }
1801
1802   if (priv->background_set && 
1803       (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1804     {
1805       gdk_cairo_rectangle (cr, background_area);
1806       gdk_cairo_set_source_rgba (cr, &priv->background);
1807       cairo_fill (cr);
1808     }
1809
1810   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1811
1812   if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1813     pango_layout_set_width (layout, 
1814                             (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1815   else if (priv->wrap_width == -1)
1816     pango_layout_set_width (layout, -1);
1817
1818   gtk_paint_layout (gtk_widget_get_style (widget),
1819                           cr,
1820                           state,
1821                           TRUE,
1822                           widget,
1823                           "cellrenderertext",
1824                           cell_area->x + x_offset + xpad,
1825                           cell_area->y + y_offset + ypad,
1826                           layout);
1827
1828   g_object_unref (layout);
1829 }
1830
1831 static void
1832 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1833                                      gpointer         data)
1834 {
1835   GtkCellRendererTextPrivate *priv;
1836   const gchar *path;
1837   const gchar *new_text;
1838   gboolean canceled;
1839
1840   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1841
1842   priv->entry = NULL;
1843
1844   if (priv->focus_out_id > 0)
1845     {
1846       g_signal_handler_disconnect (entry, priv->focus_out_id);
1847       priv->focus_out_id = 0;
1848     }
1849
1850   if (priv->populate_popup_id > 0)
1851     {
1852       g_signal_handler_disconnect (entry, priv->populate_popup_id);
1853       priv->populate_popup_id = 0;
1854     }
1855
1856   if (priv->entry_menu_popdown_timeout)
1857     {
1858       g_source_remove (priv->entry_menu_popdown_timeout);
1859       priv->entry_menu_popdown_timeout = 0;
1860     }
1861
1862   g_object_get (entry,
1863                 "editing-canceled", &canceled,
1864                 NULL);
1865   gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1866
1867   if (canceled)
1868     return;
1869
1870   path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1871   new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1872
1873   g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1874 }
1875
1876 static gboolean
1877 popdown_timeout (gpointer data)
1878 {
1879   GtkCellRendererTextPrivate *priv;
1880
1881   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1882
1883   priv->entry_menu_popdown_timeout = 0;
1884
1885   if (!gtk_widget_has_focus (priv->entry))
1886     gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1887
1888   return FALSE;
1889 }
1890
1891 static void
1892 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1893                                     gpointer data)
1894 {
1895   GtkCellRendererTextPrivate *priv;
1896
1897   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1898
1899   priv->in_entry_menu = FALSE;
1900
1901   if (priv->entry_menu_popdown_timeout)
1902     return;
1903
1904   priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1905                                                     data);
1906 }
1907
1908 static void
1909 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1910                                        GtkMenu  *menu,
1911                                        gpointer  data)
1912 {
1913   GtkCellRendererTextPrivate *priv;
1914
1915   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1916
1917   if (priv->entry_menu_popdown_timeout)
1918     {
1919       g_source_remove (priv->entry_menu_popdown_timeout);
1920       priv->entry_menu_popdown_timeout = 0;
1921     }
1922
1923   priv->in_entry_menu = TRUE;
1924
1925   g_signal_connect (menu, "unmap",
1926                     G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
1927 }
1928
1929 static gboolean
1930 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1931                                         GdkEvent  *event,
1932                                         gpointer   data)
1933 {
1934   GtkCellRendererTextPrivate *priv;
1935
1936   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1937
1938   if (priv->in_entry_menu)
1939     return FALSE;
1940
1941   g_object_set (entry,
1942                 "editing-canceled", TRUE,
1943                 NULL);
1944   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
1945   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
1946
1947   /* entry needs focus-out-event */
1948   return FALSE;
1949 }
1950
1951 static GtkCellEditable *
1952 gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
1953                                       GdkEvent             *event,
1954                                       GtkWidget            *widget,
1955                                       const gchar          *path,
1956                                       const GdkRectangle   *background_area,
1957                                       const GdkRectangle   *cell_area,
1958                                       GtkCellRendererState  flags)
1959 {
1960   GtkRequisition requisition;
1961   GtkCellRendererText *celltext;
1962   GtkCellRendererTextPrivate *priv;
1963   gfloat xalign, yalign;
1964
1965   celltext = GTK_CELL_RENDERER_TEXT (cell);
1966   priv = celltext->priv;
1967
1968   /* If the cell isn't editable we return NULL. */
1969   if (priv->editable == FALSE)
1970     return NULL;
1971
1972   gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1973
1974   priv->entry = gtk_entry_new ();
1975   gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
1976   gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
1977
1978   if (priv->text)
1979     gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
1980   g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
1981   
1982   gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
1983
1984   gtk_widget_get_preferred_size (priv->entry, &requisition, NULL);
1985   if (requisition.height < cell_area->height)
1986     {
1987       GtkBorder *style_border;
1988       GtkBorder border;
1989
1990       gtk_widget_style_get (priv->entry,
1991                             "inner-border", &style_border,
1992                             NULL);
1993
1994       if (style_border)
1995         {
1996           border = *style_border;
1997           g_boxed_free (GTK_TYPE_BORDER, style_border);
1998         }
1999       else
2000         {
2001           /* Since boxed style properties can't have default values ... */
2002           border.left = 2;
2003           border.right = 2;
2004         }
2005
2006       border.top = (cell_area->height - requisition.height) / 2;
2007       border.bottom = (cell_area->height - requisition.height) / 2;
2008       gtk_entry_set_inner_border (GTK_ENTRY (priv->entry), &border);
2009     }
2010
2011   priv->in_entry_menu = FALSE;
2012   if (priv->entry_menu_popdown_timeout)
2013     {
2014       g_source_remove (priv->entry_menu_popdown_timeout);
2015       priv->entry_menu_popdown_timeout = 0;
2016     }
2017
2018   g_signal_connect (priv->entry,
2019                     "editing-done",
2020                     G_CALLBACK (gtk_cell_renderer_text_editing_done),
2021                     celltext);
2022   priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2023                                                G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2024                                                celltext);
2025   priv->populate_popup_id =
2026     g_signal_connect (priv->entry, "populate-popup",
2027                       G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2028                       celltext);
2029  
2030   gtk_widget_show (priv->entry);
2031
2032   return GTK_CELL_EDITABLE (priv->entry);
2033 }
2034
2035 /**
2036  * gtk_cell_renderer_text_set_fixed_height_from_font:
2037  * @renderer: A #GtkCellRendererText
2038  * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2039  * 
2040  * Sets the height of a renderer to explicitly be determined by the "font" and
2041  * "y_pad" property set on it.  Further changes in these properties do not
2042  * affect the height, so they must be accompanied by a subsequent call to this
2043  * function.  Using this function is unflexible, and should really only be used
2044  * if calculating the size of a cell is too slow (ie, a massive number of cells
2045  * displayed).  If @number_of_rows is -1, then the fixed height is unset, and
2046  * the height is determined by the properties again.
2047  **/
2048 void
2049 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2050                                                    gint                 number_of_rows)
2051 {
2052   GtkCellRendererTextPrivate *priv;
2053   GtkCellRenderer *cell;
2054
2055   g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2056   g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2057
2058   cell = GTK_CELL_RENDERER (renderer);
2059   priv = renderer->priv;
2060
2061   if (number_of_rows == -1)
2062     {
2063       gint width, height;
2064
2065       gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2066       gtk_cell_renderer_set_fixed_size (cell, width, -1);
2067     }
2068   else
2069     {
2070       priv->fixed_height_rows = number_of_rows;
2071       priv->calc_fixed_height = TRUE;
2072     }
2073 }
2074
2075 static void
2076 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2077                                             GtkWidget       *widget,
2078                                             gint            *minimum_size,
2079                                             gint            *natural_size)
2080 {
2081   GtkCellRendererTextPrivate    *priv;
2082   GtkCellRendererText        *celltext;
2083   GtkStyle                   *style;
2084   PangoLayout                *layout;
2085   PangoContext               *context;
2086   PangoFontMetrics           *metrics;
2087   PangoRectangle              rect;
2088   gint char_width, digit_width, char_pixels, text_width, ellipsize_chars, guess_width, xpad;
2089   gint min_width, nat_width;
2090
2091   /* "width-chars" Hard-coded minimum width:
2092    *    - minimum size should be MAX (width-chars, strlen ("..."));
2093    *    - natural size should be MAX (width-chars, strlen (label->text));
2094    *
2095    * "wrap-width" User specified natural wrap width
2096    *    - minimum size should be MAX (width-chars, 0)
2097    *    - natural size should be MIN (wrap-width, strlen (label->text))
2098    */
2099
2100   celltext = GTK_CELL_RENDERER_TEXT (cell);
2101   priv = celltext->priv;
2102
2103   style = gtk_widget_get_style (widget);
2104
2105   gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2106
2107   layout = get_layout (celltext, widget, NULL, 0);
2108
2109   /* Get the layout with the text possibly wrapping at wrap_width */
2110   pango_layout_get_pixel_extents (layout, NULL, &rect);
2111   guess_width = rect.width;
2112
2113   /* Fetch the length of the complete unwrapped text */
2114   pango_layout_set_width (layout, -1);
2115   pango_layout_get_extents (layout, NULL, &rect);
2116   text_width = rect.width;
2117
2118   /* Fetch the average size of a charachter */
2119   context = pango_layout_get_context (layout);
2120   metrics = pango_context_get_metrics (context, style->font_desc,
2121                                        pango_context_get_language (context));
2122   
2123   char_width = pango_font_metrics_get_approximate_char_width (metrics);
2124   digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2125   char_pixels = MAX (char_width, digit_width);
2126
2127   pango_font_metrics_unref (metrics);
2128   g_object_unref (layout);
2129
2130   /* enforce minimum width for ellipsized labels at ~3 chars */
2131   if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2132     ellipsize_chars = 3;
2133   else
2134     ellipsize_chars = 0;
2135   
2136   if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2137     min_width = 
2138       xpad * 2 + (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars));
2139   /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2140   else if (priv->wrap_width > -1)
2141     min_width = xpad * 2 + rect.x + priv->wrap_width;
2142   else
2143     min_width = xpad * 2 + rect.x + guess_width;
2144
2145   if (priv->width_chars > 0)
2146     nat_width = xpad * 2 + 
2147       MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS (text_width));
2148   else
2149     nat_width = xpad * 2 + PANGO_PIXELS (text_width);
2150
2151   nat_width = MAX (nat_width, min_width);
2152   
2153   if (priv->max_width_chars > 0)
2154     {
2155       gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2156       
2157       min_width = MIN (min_width, max_width);
2158       nat_width = MIN (nat_width, max_width);
2159     }
2160
2161   if (minimum_size)
2162     *minimum_size = min_width;
2163
2164   if (natural_size)
2165     *natural_size = nat_width;
2166
2167 }
2168
2169 static void
2170 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2171                                                        GtkWidget       *widget,
2172                                                        gint             width,
2173                                                        gint            *minimum_height,
2174                                                        gint            *natural_height)
2175 {
2176   GtkCellRendererTextPrivate    *priv;
2177   GtkCellRendererText        *celltext;
2178   PangoLayout                *layout;
2179   gint                        text_height, xpad, ypad;
2180
2181
2182   celltext = GTK_CELL_RENDERER_TEXT (cell);
2183   priv = celltext->priv;
2184
2185   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2186
2187   layout = get_layout (celltext, widget, NULL, 0);
2188
2189   pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2190   pango_layout_get_pixel_size (layout, NULL, &text_height);
2191
2192   if (minimum_height)
2193     *minimum_height = text_height + ypad * 2;
2194
2195   if (natural_height)
2196     *natural_height = text_height + ypad * 2;
2197
2198   g_object_unref (layout);
2199 }
2200
2201 static void
2202 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2203                                              GtkWidget       *widget,
2204                                              gint            *minimum_size,
2205                                              gint            *natural_size)
2206 {
2207   gint min_width;
2208
2209   /* Thankfully cell renderers dont rotate, so they only have to do
2210    * height-for-width and not the opposite. Here we have only to return
2211    * the height for the base minimum width of the renderer.
2212    *
2213    * Note this code path wont be followed by GtkTreeView which is
2214    * height-for-width specifically.
2215    */
2216   gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2217   gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2218                                                          minimum_size, natural_size);
2219 }
2220