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