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