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