]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderertext.c
Support insensitive cells in tree views and combo boxes.
[~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 "gtkentry.h"
25 #include "gtkmarshalers.h"
26 #include "gtkintl.h"
27 #include "gtktreeprivate.h"
28
29 static void gtk_cell_renderer_text_init       (GtkCellRendererText      *celltext);
30 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
31 static void gtk_cell_renderer_text_finalize   (GObject                  *object);
32
33 static void gtk_cell_renderer_text_get_property  (GObject                  *object,
34                                                   guint                     param_id,
35                                                   GValue                   *value,
36                                                   GParamSpec               *pspec);
37 static void gtk_cell_renderer_text_set_property  (GObject                  *object,
38                                                   guint                     param_id,
39                                                   const GValue             *value,
40                                                   GParamSpec               *pspec);
41 static void gtk_cell_renderer_text_get_size   (GtkCellRenderer          *cell,
42                                                GtkWidget                *widget,
43                                                GdkRectangle             *cell_area,
44                                                gint                     *x_offset,
45                                                gint                     *y_offset,
46                                                gint                     *width,
47                                                gint                     *height);
48 static void gtk_cell_renderer_text_render     (GtkCellRenderer          *cell,
49                                                GdkWindow                *window,
50                                                GtkWidget                *widget,
51                                                GdkRectangle             *background_area,
52                                                GdkRectangle             *cell_area,
53                                                GdkRectangle             *expose_area,
54                                                GtkCellRendererState      flags);
55
56 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
57                                                               GdkEvent             *event,
58                                                               GtkWidget            *widget,
59                                                               const gchar          *path,
60                                                               GdkRectangle         *background_area,
61                                                               GdkRectangle         *cell_area,
62                                                               GtkCellRendererState  flags);
63
64 enum {
65   EDITED,
66   LAST_SIGNAL
67 };
68
69 enum {
70   PROP_0,
71
72   PROP_TEXT,
73   PROP_MARKUP,
74   PROP_ATTRIBUTES,
75   PROP_SINGLE_PARAGRAPH_MODE,
76   
77   /* Style args */
78   PROP_BACKGROUND,
79   PROP_FOREGROUND,
80   PROP_BACKGROUND_GDK,
81   PROP_FOREGROUND_GDK,
82   PROP_FONT,
83   PROP_FONT_DESC,
84   PROP_FAMILY,
85   PROP_STYLE,
86   PROP_VARIANT,
87   PROP_WEIGHT,
88   PROP_STRETCH,
89   PROP_SIZE,
90   PROP_SIZE_POINTS,
91   PROP_SCALE,
92   PROP_EDITABLE,
93   PROP_STRIKETHROUGH,
94   PROP_UNDERLINE,
95   PROP_RISE,
96   PROP_LANGUAGE,
97   
98   /* Whether-a-style-arg-is-set args */
99   PROP_BACKGROUND_SET,
100   PROP_FOREGROUND_SET,
101   PROP_FAMILY_SET,
102   PROP_STYLE_SET,
103   PROP_VARIANT_SET,
104   PROP_WEIGHT_SET,
105   PROP_STRETCH_SET,
106   PROP_SIZE_SET,
107   PROP_SCALE_SET,
108   PROP_EDITABLE_SET,
109   PROP_STRIKETHROUGH_SET,
110   PROP_UNDERLINE_SET,
111   PROP_RISE_SET,
112   PROP_LANGUAGE_SET
113 };
114
115 static gpointer parent_class;
116 static guint text_cell_renderer_signals [LAST_SIGNAL];
117
118 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
119
120 #define GTK_CELL_RENDERER_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TEXT, GtkCellRendererTextPrivate))
121
122 typedef struct _GtkCellRendererTextPrivate GtkCellRendererTextPrivate;
123 struct _GtkCellRendererTextPrivate
124 {
125   guint single_paragraph : 1;
126   guint language_set : 1;
127   guint markup_set : 1;
128
129   gulong focus_out_id;
130   PangoLanguage *language;
131 };
132
133
134 GType
135 gtk_cell_renderer_text_get_type (void)
136 {
137   static GType cell_text_type = 0;
138
139   if (!cell_text_type)
140     {
141       static const GTypeInfo cell_text_info =
142       {
143         sizeof (GtkCellRendererTextClass),
144         NULL,           /* base_init */
145         NULL,           /* base_finalize */
146         (GClassInitFunc) gtk_cell_renderer_text_class_init,
147         NULL,           /* class_finalize */
148         NULL,           /* class_data */
149         sizeof (GtkCellRendererText),
150         0,              /* n_preallocs */
151         (GInstanceInitFunc) gtk_cell_renderer_text_init,
152       };
153
154       cell_text_type =
155         g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText",
156                                 &cell_text_info, 0);
157     }
158
159   return cell_text_type;
160 }
161
162 static void
163 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
164 {
165   GTK_CELL_RENDERER (celltext)->xalign = 0.0;
166   GTK_CELL_RENDERER (celltext)->yalign = 0.5;
167   GTK_CELL_RENDERER (celltext)->xpad = 2;
168   GTK_CELL_RENDERER (celltext)->ypad = 2;
169   celltext->fixed_height_rows = -1;
170   celltext->font = pango_font_description_new ();
171 }
172
173 static void
174 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
175 {
176   GObjectClass *object_class = G_OBJECT_CLASS (class);
177   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
178
179   parent_class = g_type_class_peek_parent (class);
180   
181   object_class->finalize = gtk_cell_renderer_text_finalize;
182   
183   object_class->get_property = gtk_cell_renderer_text_get_property;
184   object_class->set_property = gtk_cell_renderer_text_set_property;
185
186   cell_class->get_size = gtk_cell_renderer_text_get_size;
187   cell_class->render = gtk_cell_renderer_text_render;
188   cell_class->start_editing = gtk_cell_renderer_text_start_editing;
189   
190   g_object_class_install_property (object_class,
191                                    PROP_TEXT,
192                                    g_param_spec_string ("text",
193                                                         P_("Text"),
194                                                         P_("Text to render"),
195                                                         NULL,
196                                                         G_PARAM_READWRITE));
197   
198   g_object_class_install_property (object_class,
199                                    PROP_MARKUP,
200                                    g_param_spec_string ("markup",
201                                                         P_("Markup"),
202                                                         P_("Marked up text to render"),
203                                                         NULL,
204                                                         G_PARAM_WRITABLE));
205
206   g_object_class_install_property (object_class,
207                                    PROP_ATTRIBUTES,
208                                    g_param_spec_boxed ("attributes",
209                                                        P_("Attributes"),
210                                                        P_("A list of style attributes to apply to the text of the renderer"),
211                                                        PANGO_TYPE_ATTR_LIST,
212                                                        G_PARAM_READWRITE));
213
214   g_object_class_install_property (object_class,
215                                    PROP_SINGLE_PARAGRAPH_MODE,
216                                    g_param_spec_boolean ("single_paragraph_mode",
217                                                          P_("Single Paragraph Mode"),
218                                                          P_("Whether or not to keep all text in a single paragraph"),
219                                                          FALSE,
220                                                          G_PARAM_READWRITE));
221
222   
223   g_object_class_install_property (object_class,
224                                    PROP_BACKGROUND,
225                                    g_param_spec_string ("background",
226                                                         P_("Background color name"),
227                                                         P_("Background color as a string"),
228                                                         NULL,
229                                                         G_PARAM_WRITABLE));
230
231   g_object_class_install_property (object_class,
232                                    PROP_BACKGROUND_GDK,
233                                    g_param_spec_boxed ("background_gdk",
234                                                        P_("Background color"),
235                                                        P_("Background color as a GdkColor"),
236                                                        GDK_TYPE_COLOR,
237                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));  
238
239   g_object_class_install_property (object_class,
240                                    PROP_FOREGROUND,
241                                    g_param_spec_string ("foreground",
242                                                         P_("Foreground color name"),
243                                                         P_("Foreground color as a string"),
244                                                         NULL,
245                                                         G_PARAM_WRITABLE));
246
247   g_object_class_install_property (object_class,
248                                    PROP_FOREGROUND_GDK,
249                                    g_param_spec_boxed ("foreground_gdk",
250                                                        P_("Foreground color"),
251                                                        P_("Foreground color as a GdkColor"),
252                                                        GDK_TYPE_COLOR,
253                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
254
255
256   g_object_class_install_property (object_class,
257                                    PROP_EDITABLE,
258                                    g_param_spec_boolean ("editable",
259                                                          P_("Editable"),
260                                                          P_("Whether the text can be modified by the user"),
261                                                          FALSE,
262                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
263
264   g_object_class_install_property (object_class,
265                                    PROP_FONT,
266                                    g_param_spec_string ("font",
267                                                         P_("Font"),
268                                                         P_("Font description as a string"),
269                                                         NULL,
270                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
271
272   g_object_class_install_property (object_class,
273                                    PROP_FONT_DESC,
274                                    g_param_spec_boxed ("font_desc",
275                                                        P_("Font"),
276                                                        P_("Font description as a PangoFontDescription struct"),
277                                                        PANGO_TYPE_FONT_DESCRIPTION,
278                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
279
280   
281   g_object_class_install_property (object_class,
282                                    PROP_FAMILY,
283                                    g_param_spec_string ("family",
284                                                         P_("Font family"),
285                                                         P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
286                                                         NULL,
287                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
288
289   g_object_class_install_property (object_class,
290                                    PROP_STYLE,
291                                    g_param_spec_enum ("style",
292                                                       P_("Font style"),
293                                                       P_("Font style"),
294                                                       PANGO_TYPE_STYLE,
295                                                       PANGO_STYLE_NORMAL,
296                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
297
298   g_object_class_install_property (object_class,
299                                    PROP_VARIANT,
300                                    g_param_spec_enum ("variant",
301                                                      P_("Font variant"),
302                                                      P_("Font variant"),
303                                                       PANGO_TYPE_VARIANT,
304                                                       PANGO_VARIANT_NORMAL,
305                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
306   
307   g_object_class_install_property (object_class,
308                                    PROP_WEIGHT,
309                                    g_param_spec_int ("weight",
310                                                      P_("Font weight"),
311                                                      P_("Font weight"),
312                                                      0,
313                                                      G_MAXINT,
314                                                      PANGO_WEIGHT_NORMAL,
315                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
316
317    g_object_class_install_property (object_class,
318                                    PROP_STRETCH,
319                                    g_param_spec_enum ("stretch",
320                                                       P_("Font stretch"),
321                                                       P_("Font stretch"),
322                                                       PANGO_TYPE_STRETCH,
323                                                       PANGO_STRETCH_NORMAL,
324                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
325   
326   g_object_class_install_property (object_class,
327                                    PROP_SIZE,
328                                    g_param_spec_int ("size",
329                                                      P_("Font size"),
330                                                      P_("Font size"),
331                                                      0,
332                                                      G_MAXINT,
333                                                      0,
334                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
335
336   g_object_class_install_property (object_class,
337                                    PROP_SIZE_POINTS,
338                                    g_param_spec_double ("size_points",
339                                                         P_("Font points"),
340                                                         P_("Font size in points"),
341                                                         0.0,
342                                                         G_MAXDOUBLE,
343                                                         0.0,
344                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));  
345
346   g_object_class_install_property (object_class,
347                                    PROP_SCALE,
348                                    g_param_spec_double ("scale",
349                                                         P_("Font scale"),
350                                                         P_("Font scaling factor"),
351                                                         0.0,
352                                                         G_MAXDOUBLE,
353                                                         1.0,
354                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
355   
356   g_object_class_install_property (object_class,
357                                    PROP_RISE,
358                                    g_param_spec_int ("rise",
359                                                      P_("Rise"),
360                                                      P_("Offset of text above the baseline (below the baseline if rise is negative)"),
361                                                      -G_MAXINT,
362                                                      G_MAXINT,
363                                                      0,
364                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
365
366
367   g_object_class_install_property (object_class,
368                                    PROP_STRIKETHROUGH,
369                                    g_param_spec_boolean ("strikethrough",
370                                                          P_("Strikethrough"),
371                                                          P_("Whether to strike through the text"),
372                                                          FALSE,
373                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
374   
375   g_object_class_install_property (object_class,
376                                    PROP_UNDERLINE,
377                                    g_param_spec_enum ("underline",
378                                                       P_("Underline"),
379                                                       P_("Style of underline for this text"),
380                                                       PANGO_TYPE_UNDERLINE,
381                                                       PANGO_UNDERLINE_NONE,
382                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
383
384   g_object_class_install_property (object_class,
385                                    PROP_LANGUAGE,
386                                    g_param_spec_string ("language",
387                                                         P_("Language"),
388                                                         P_("The language this text is in, as an ISO code. Pango can use this as a hint when rendering the text. If you don't understand this parameter, you probably don't need it"),
389                                                         NULL,
390                                                         G_PARAM_READWRITE));
391
392
393   /* Style props are set or not */
394
395 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, G_PARAM_READABLE | G_PARAM_WRITABLE))
396
397   ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
398                 P_("Background set"),
399                 P_("Whether this tag affects the background color"));
400
401   ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
402                 P_("Foreground set"),
403                 P_("Whether this tag affects the foreground color"));
404   
405   ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
406                 P_("Editability set"),
407                 P_("Whether this tag affects text editability"));
408
409   ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
410                 P_("Font family set"),
411                 P_("Whether this tag affects the font family"));  
412
413   ADD_SET_PROP ("style_set", PROP_STYLE_SET,
414                 P_("Font style set"),
415                 P_("Whether this tag affects the font style"));
416
417   ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
418                 P_("Font variant set"),
419                 P_("Whether this tag affects the font variant"));
420
421   ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
422                 P_("Font weight set"),
423                 P_("Whether this tag affects the font weight"));
424
425   ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
426                 P_("Font stretch set"),
427                 P_("Whether this tag affects the font stretch"));
428
429   ADD_SET_PROP ("size_set", PROP_SIZE_SET,
430                 P_("Font size set"),
431                 P_("Whether this tag affects the font size"));
432
433   ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
434                 P_("Font scale set"),
435                 P_("Whether this tag scales the font size by a factor"));
436   
437   ADD_SET_PROP ("rise_set", PROP_RISE_SET,
438                 P_("Rise set"),
439                 P_("Whether this tag affects the rise"));
440
441   ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
442                 P_("Strikethrough set"),
443                 P_("Whether this tag affects strikethrough"));
444
445   ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
446                 P_("Underline set"),
447                 P_("Whether this tag affects underlining"));
448
449   ADD_SET_PROP ("language_set", PROP_LANGUAGE_SET,
450                 P_("Language set"),
451                 P_("Whether this tag affects the language the text is rendered as"));
452
453   text_cell_renderer_signals [EDITED] =
454     g_signal_new ("edited",
455                   G_OBJECT_CLASS_TYPE (object_class),
456                   G_SIGNAL_RUN_LAST,
457                   G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
458                   NULL, NULL,
459                   _gtk_marshal_VOID__STRING_STRING,
460                   G_TYPE_NONE, 2,
461                   G_TYPE_STRING,
462                   G_TYPE_STRING);
463
464   g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
465 }
466
467 static void
468 gtk_cell_renderer_text_finalize (GObject *object)
469 {
470   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
471   GtkCellRendererTextPrivate *priv;
472
473   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
474
475   pango_font_description_free (celltext->font);
476
477   if (celltext->text)
478     g_free (celltext->text);
479
480   if (celltext->extra_attrs)
481     pango_attr_list_unref (celltext->extra_attrs);
482
483   if (priv->language)
484     g_object_unref (priv->language);
485
486   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
487 }
488
489 static PangoFontMask
490 get_property_font_set_mask (guint prop_id)
491 {
492   switch (prop_id)
493     {
494     case PROP_FAMILY_SET:
495       return PANGO_FONT_MASK_FAMILY;
496     case PROP_STYLE_SET:
497       return PANGO_FONT_MASK_STYLE;
498     case PROP_VARIANT_SET:
499       return PANGO_FONT_MASK_VARIANT;
500     case PROP_WEIGHT_SET:
501       return PANGO_FONT_MASK_WEIGHT;
502     case PROP_STRETCH_SET:
503       return PANGO_FONT_MASK_STRETCH;
504     case PROP_SIZE_SET:
505       return PANGO_FONT_MASK_SIZE;
506     }
507
508   return 0;
509 }
510
511 static void
512 gtk_cell_renderer_text_get_property (GObject        *object,
513                                      guint           param_id,
514                                      GValue         *value,
515                                      GParamSpec     *pspec)
516 {
517   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
518   GtkCellRendererTextPrivate *priv;
519
520   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
521
522   switch (param_id)
523     {
524     case PROP_TEXT:
525       g_value_set_string (value, celltext->text);
526       break;
527
528     case PROP_ATTRIBUTES:
529       g_value_set_boxed (value, celltext->extra_attrs);
530       break;
531
532     case PROP_SINGLE_PARAGRAPH_MODE:
533       g_value_set_boolean (value, priv->single_paragraph);
534       break;
535
536     case PROP_BACKGROUND_GDK:
537       {
538         GdkColor color;
539         
540         color.red = celltext->background.red;
541         color.green = celltext->background.green;
542         color.blue = celltext->background.blue;
543         
544         g_value_set_boxed (value, &color);
545       }
546       break;
547
548     case PROP_FOREGROUND_GDK:
549       {
550         GdkColor color;
551         
552         color.red = celltext->foreground.red;
553         color.green = celltext->foreground.green;
554         color.blue = celltext->foreground.blue;
555         
556         g_value_set_boxed (value, &color);
557       }
558       break;
559
560     case PROP_FONT:
561       {
562         /* FIXME GValue imposes a totally gratuitous string copy
563          * here, we could just hand off string ownership
564          */
565         gchar *str = pango_font_description_to_string (celltext->font);
566         g_value_set_string (value, str);
567         g_free (str);
568       }
569       break;
570       
571     case PROP_FONT_DESC:
572       g_value_set_boxed (value, celltext->font);
573       break;
574
575     case PROP_FAMILY:
576       g_value_set_string (value, pango_font_description_get_family (celltext->font));
577       break;
578
579     case PROP_STYLE:
580       g_value_set_enum (value, pango_font_description_get_style (celltext->font));
581       break;
582
583     case PROP_VARIANT:
584       g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
585       break;
586
587     case PROP_WEIGHT:
588       g_value_set_int (value, pango_font_description_get_weight (celltext->font));
589       break;
590
591     case PROP_STRETCH:
592       g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
593       break;
594
595     case PROP_SIZE:
596       g_value_set_int (value, pango_font_description_get_size (celltext->font));
597       break;
598
599     case PROP_SIZE_POINTS:
600       g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
601       break;
602
603     case PROP_SCALE:
604       g_value_set_double (value, celltext->font_scale);
605       break;
606       
607     case PROP_EDITABLE:
608       g_value_set_boolean (value, celltext->editable);
609       break;
610
611     case PROP_STRIKETHROUGH:
612       g_value_set_boolean (value, celltext->strikethrough);
613       break;
614
615     case PROP_UNDERLINE:
616       g_value_set_enum (value, celltext->underline_style);
617       break;
618
619     case PROP_RISE:
620       g_value_set_int (value, celltext->rise);
621       break;  
622
623     case PROP_LANGUAGE:
624       g_value_set_string (value, pango_language_to_string (priv->language));
625       break;
626
627     case PROP_BACKGROUND_SET:
628       g_value_set_boolean (value, celltext->background_set);
629       break;
630
631     case PROP_FOREGROUND_SET:
632       g_value_set_boolean (value, celltext->foreground_set);
633       break;
634
635     case PROP_FAMILY_SET:
636     case PROP_STYLE_SET:
637     case PROP_VARIANT_SET:
638     case PROP_WEIGHT_SET:
639     case PROP_STRETCH_SET:
640     case PROP_SIZE_SET:
641       {
642         PangoFontMask mask = get_property_font_set_mask (param_id);
643         g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
644         
645         break;
646       }
647
648     case PROP_SCALE_SET:
649       g_value_set_boolean (value, celltext->scale_set);
650       break;
651       
652     case PROP_EDITABLE_SET:
653       g_value_set_boolean (value, celltext->editable_set);
654       break;
655
656     case PROP_STRIKETHROUGH_SET:
657       g_value_set_boolean (value, celltext->strikethrough_set);
658       break;
659
660     case PROP_UNDERLINE_SET:
661       g_value_set_boolean (value, celltext->underline_set);
662       break;
663
664     case  PROP_RISE_SET:
665       g_value_set_boolean (value, celltext->rise_set);
666       break;
667
668     case PROP_LANGUAGE_SET:
669       g_value_set_boolean (value, priv->language_set);
670       break;
671       
672     case PROP_BACKGROUND:
673     case PROP_FOREGROUND:
674     case PROP_MARKUP:
675     default:
676       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
677       break;
678     }
679 }
680
681
682 static void
683 set_bg_color (GtkCellRendererText *celltext,
684               GdkColor            *color)
685 {
686   if (color)
687     {
688       if (!celltext->background_set)
689         {
690           celltext->background_set = TRUE;
691           g_object_notify (G_OBJECT (celltext), "background_set");
692         }
693       
694       celltext->background.red = color->red;
695       celltext->background.green = color->green;
696       celltext->background.blue = color->blue;
697     }
698   else
699     {
700       if (celltext->background_set)
701         {
702           celltext->background_set = FALSE;
703           g_object_notify (G_OBJECT (celltext), "background_set");
704         }
705     }
706 }
707
708
709 static void
710 set_fg_color (GtkCellRendererText *celltext,
711               GdkColor            *color)
712 {
713   if (color)
714     {
715       if (!celltext->foreground_set)
716         {
717           celltext->foreground_set = TRUE;
718           g_object_notify (G_OBJECT (celltext), "foreground_set");
719         }
720       
721       celltext->foreground.red = color->red;
722       celltext->foreground.green = color->green;
723       celltext->foreground.blue = color->blue;
724     }
725   else
726     {
727       if (celltext->foreground_set)
728         {
729           celltext->foreground_set = FALSE;
730           g_object_notify (G_OBJECT (celltext), "foreground_set");
731         }
732     }
733 }
734
735 static PangoFontMask
736 set_font_desc_fields (PangoFontDescription *desc,
737                       PangoFontMask         to_set)
738 {
739   PangoFontMask changed_mask = 0;
740   
741   if (to_set & PANGO_FONT_MASK_FAMILY)
742     {
743       const char *family = pango_font_description_get_family (desc);
744       if (!family)
745         {
746           family = "sans";
747           changed_mask |= PANGO_FONT_MASK_FAMILY;
748         }
749
750       pango_font_description_set_family (desc, family);
751     }
752   if (to_set & PANGO_FONT_MASK_STYLE)
753     pango_font_description_set_style (desc, pango_font_description_get_style (desc));
754   if (to_set & PANGO_FONT_MASK_VARIANT)
755     pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
756   if (to_set & PANGO_FONT_MASK_WEIGHT)
757     pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
758   if (to_set & PANGO_FONT_MASK_STRETCH)
759     pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
760   if (to_set & PANGO_FONT_MASK_SIZE)
761     {
762       gint size = pango_font_description_get_size (desc);
763       if (size <= 0)
764         {
765           size = 10 * PANGO_SCALE;
766           changed_mask |= PANGO_FONT_MASK_SIZE;
767         }
768       
769       pango_font_description_set_size (desc, size);
770     }
771
772   return changed_mask;
773 }
774
775 static void
776 notify_set_changed (GObject       *object,
777                     PangoFontMask  changed_mask)
778 {
779   if (changed_mask & PANGO_FONT_MASK_FAMILY)
780     g_object_notify (object, "family_set");
781   if (changed_mask & PANGO_FONT_MASK_STYLE)
782     g_object_notify (object, "style_set");
783   if (changed_mask & PANGO_FONT_MASK_VARIANT)
784     g_object_notify (object, "variant_set");
785   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
786     g_object_notify (object, "weight_set");
787   if (changed_mask & PANGO_FONT_MASK_STRETCH)
788     g_object_notify (object, "stretch_set");
789   if (changed_mask & PANGO_FONT_MASK_SIZE)
790     g_object_notify (object, "size_set");
791 }
792
793 static void
794 notify_fields_changed (GObject       *object,
795                        PangoFontMask  changed_mask)
796 {
797   if (changed_mask & PANGO_FONT_MASK_FAMILY)
798     g_object_notify (object, "family");
799   if (changed_mask & PANGO_FONT_MASK_STYLE)
800     g_object_notify (object, "style");
801   if (changed_mask & PANGO_FONT_MASK_VARIANT)
802     g_object_notify (object, "variant");
803   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
804     g_object_notify (object, "weight");
805   if (changed_mask & PANGO_FONT_MASK_STRETCH)
806     g_object_notify (object, "stretch");
807   if (changed_mask & PANGO_FONT_MASK_SIZE)
808     g_object_notify (object, "size");
809 }
810
811 static void
812 set_font_description (GtkCellRendererText  *celltext,
813                       PangoFontDescription *font_desc)
814 {
815   GObject *object = G_OBJECT (celltext);
816   PangoFontDescription *new_font_desc;
817   PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
818   
819   if (font_desc)
820     new_font_desc = pango_font_description_copy (font_desc);
821   else
822     new_font_desc = pango_font_description_new ();
823
824   old_mask = pango_font_description_get_set_fields (celltext->font);
825   new_mask = pango_font_description_get_set_fields (new_font_desc);
826
827   changed_mask = old_mask | new_mask;
828   set_changed_mask = old_mask ^ new_mask;
829
830   pango_font_description_free (celltext->font);
831   celltext->font = new_font_desc;
832   
833   g_object_freeze_notify (object);
834
835   g_object_notify (object, "font_desc");
836   g_object_notify (object, "font");
837   
838   if (changed_mask & PANGO_FONT_MASK_FAMILY)
839     g_object_notify (object, "family");
840   if (changed_mask & PANGO_FONT_MASK_STYLE)
841     g_object_notify (object, "style");
842   if (changed_mask & PANGO_FONT_MASK_VARIANT)
843     g_object_notify (object, "variant");
844   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
845     g_object_notify (object, "weight");
846   if (changed_mask & PANGO_FONT_MASK_STRETCH)
847     g_object_notify (object, "stretch");
848   if (changed_mask & PANGO_FONT_MASK_SIZE)
849     {
850       g_object_notify (object, "size");
851       g_object_notify (object, "size_points");
852     }
853
854   notify_set_changed (object, set_changed_mask);
855   
856   g_object_thaw_notify (object);
857 }
858
859 static void
860 gtk_cell_renderer_text_set_property (GObject      *object,
861                                      guint         param_id,
862                                      const GValue *value,
863                                      GParamSpec   *pspec)
864 {
865   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
866   GtkCellRendererTextPrivate *priv;
867
868   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
869
870   switch (param_id)
871     {
872     case PROP_TEXT:
873       if (celltext->text)
874         g_free (celltext->text);
875
876       if (priv->markup_set)
877         {
878           if (celltext->extra_attrs)
879             pango_attr_list_unref (celltext->extra_attrs);
880           celltext->extra_attrs = NULL;
881           priv->markup_set = FALSE;
882         }
883
884       celltext->text = g_strdup (g_value_get_string (value));
885       g_object_notify (object, "text");
886       break;
887
888     case PROP_ATTRIBUTES:
889       if (celltext->extra_attrs)
890         pango_attr_list_unref (celltext->extra_attrs);
891
892       celltext->extra_attrs = g_value_get_boxed (value);
893       if (celltext->extra_attrs)
894         pango_attr_list_ref (celltext->extra_attrs);
895       break;
896     case PROP_MARKUP:
897       {
898         const gchar *str;
899         gchar *text = NULL;
900         GError *error = NULL;
901         PangoAttrList *attrs = NULL;
902
903         str = g_value_get_string (value);
904         if (str && !pango_parse_markup (str,
905                                         -1,
906                                         0,
907                                         &attrs,
908                                         &text,
909                                         NULL,
910                                         &error))
911           {
912             g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
913                        error->message);
914             g_error_free (error);
915             return;
916           }
917
918         if (celltext->text)
919           g_free (celltext->text);
920
921         if (celltext->extra_attrs)
922           pango_attr_list_unref (celltext->extra_attrs);
923
924         celltext->text = text;
925         celltext->extra_attrs = attrs;
926         priv->markup_set = TRUE;
927       }
928       break;
929
930     case PROP_SINGLE_PARAGRAPH_MODE:
931       priv->single_paragraph = g_value_get_boolean (value);
932       break;
933       
934     case PROP_BACKGROUND:
935       {
936         GdkColor color;
937
938         if (!g_value_get_string (value))
939           set_bg_color (celltext, NULL);       /* reset to backgrounmd_set to FALSE */
940         else if (gdk_color_parse (g_value_get_string (value), &color))
941           set_bg_color (celltext, &color);
942         else
943           g_warning ("Don't know color `%s'", g_value_get_string (value));
944
945         g_object_notify (object, "background_gdk");
946       }
947       break;
948       
949     case PROP_FOREGROUND:
950       {
951         GdkColor color;
952
953         if (!g_value_get_string (value))
954           set_fg_color (celltext, NULL);       /* reset to foreground_set to FALSE */
955         else if (gdk_color_parse (g_value_get_string (value), &color))
956           set_fg_color (celltext, &color);
957         else
958           g_warning ("Don't know color `%s'", g_value_get_string (value));
959
960         g_object_notify (object, "foreground_gdk");
961       }
962       break;
963
964     case PROP_BACKGROUND_GDK:
965       /* This notifies the GObject itself. */
966       set_bg_color (celltext, g_value_get_boxed (value));
967       break;
968
969     case PROP_FOREGROUND_GDK:
970       /* This notifies the GObject itself. */
971       set_fg_color (celltext, g_value_get_boxed (value));
972       break;
973
974     case PROP_FONT:
975       {
976         PangoFontDescription *font_desc = NULL;
977         const gchar *name;
978
979         name = g_value_get_string (value);
980
981         if (name)
982           font_desc = pango_font_description_from_string (name);
983
984         set_font_description (celltext, font_desc);
985
986         pango_font_description_free (font_desc);
987         
988         if (celltext->fixed_height_rows != -1)
989           celltext->calc_fixed_height = TRUE;
990       }
991       break;
992
993     case PROP_FONT_DESC:
994       set_font_description (celltext, g_value_get_boxed (value));
995       
996       if (celltext->fixed_height_rows != -1)
997         celltext->calc_fixed_height = TRUE;
998       break;
999
1000     case PROP_FAMILY:
1001     case PROP_STYLE:
1002     case PROP_VARIANT:
1003     case PROP_WEIGHT:
1004     case PROP_STRETCH:
1005     case PROP_SIZE:
1006     case PROP_SIZE_POINTS:
1007       {
1008         PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
1009         
1010         switch (param_id)
1011           {
1012           case PROP_FAMILY:
1013             pango_font_description_set_family (celltext->font,
1014                                                g_value_get_string (value));
1015             break;
1016           case PROP_STYLE:
1017             pango_font_description_set_style (celltext->font,
1018                                               g_value_get_enum (value));
1019             break;
1020           case PROP_VARIANT:
1021             pango_font_description_set_variant (celltext->font,
1022                                                 g_value_get_enum (value));
1023             break;
1024           case PROP_WEIGHT:
1025             pango_font_description_set_weight (celltext->font,
1026                                                g_value_get_int (value));
1027             break;
1028           case PROP_STRETCH:
1029             pango_font_description_set_stretch (celltext->font,
1030                                                 g_value_get_enum (value));
1031             break;
1032           case PROP_SIZE:
1033             pango_font_description_set_size (celltext->font,
1034                                              g_value_get_int (value));
1035             g_object_notify (object, "size_points");
1036             break;
1037           case PROP_SIZE_POINTS:
1038             pango_font_description_set_size (celltext->font,
1039                                              g_value_get_double (value) * PANGO_SCALE);
1040             g_object_notify (object, "size");
1041             break;
1042           }
1043         
1044         if (celltext->fixed_height_rows != -1)
1045           celltext->calc_fixed_height = TRUE;
1046         
1047         notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
1048         g_object_notify (object, "font_desc");
1049         g_object_notify (object, "font");
1050
1051         break;
1052       }
1053       
1054     case PROP_SCALE:
1055       celltext->font_scale = g_value_get_double (value);
1056       celltext->scale_set = TRUE;
1057       if (celltext->fixed_height_rows != -1)
1058         celltext->calc_fixed_height = TRUE;
1059       g_object_notify (object, "scale_set");
1060       break;
1061       
1062     case PROP_EDITABLE:
1063       celltext->editable = g_value_get_boolean (value);
1064       celltext->editable_set = TRUE;
1065       if (celltext->editable)
1066         GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
1067       else
1068         GTK_CELL_RENDERER (celltext)->mode = GTK_CELL_RENDERER_MODE_INERT;
1069       g_object_notify (object, "editable_set");
1070       break;
1071
1072     case PROP_STRIKETHROUGH:
1073       celltext->strikethrough = g_value_get_boolean (value);
1074       celltext->strikethrough_set = TRUE;
1075       g_object_notify (object, "strikethrough_set");
1076       break;
1077
1078     case PROP_UNDERLINE:
1079       celltext->underline_style = g_value_get_enum (value);
1080       celltext->underline_set = TRUE;
1081       g_object_notify (object, "underline_set");
1082             
1083       break;
1084
1085     case PROP_RISE:
1086       celltext->rise = g_value_get_int (value);
1087       celltext->rise_set = TRUE;
1088       g_object_notify (object, "rise_set");
1089       if (celltext->fixed_height_rows != -1)
1090         celltext->calc_fixed_height = TRUE;
1091       break;  
1092
1093     case PROP_LANGUAGE:
1094       priv->language_set = TRUE;
1095       if (priv->language)
1096         g_object_unref (priv->language);
1097       priv->language = pango_language_from_string (g_value_get_string (value));
1098       g_object_notify (object, "language_set");
1099       break;
1100
1101     case PROP_BACKGROUND_SET:
1102       celltext->background_set = g_value_get_boolean (value);
1103       break;
1104
1105     case PROP_FOREGROUND_SET:
1106       celltext->foreground_set = g_value_get_boolean (value);
1107       break;
1108
1109     case PROP_FAMILY_SET:
1110     case PROP_STYLE_SET:
1111     case PROP_VARIANT_SET:
1112     case PROP_WEIGHT_SET:
1113     case PROP_STRETCH_SET:
1114     case PROP_SIZE_SET:
1115       if (!g_value_get_boolean (value))
1116         {
1117           pango_font_description_unset_fields (celltext->font,
1118                                                get_property_font_set_mask (param_id));
1119         }
1120       else
1121         {
1122           PangoFontMask changed_mask;
1123           
1124           changed_mask = set_font_desc_fields (celltext->font,
1125                                                get_property_font_set_mask (param_id));
1126           notify_fields_changed (G_OBJECT (celltext), changed_mask);
1127         }
1128       break;
1129
1130     case PROP_SCALE_SET:
1131       celltext->scale_set = g_value_get_boolean (value);
1132       break;
1133       
1134     case PROP_EDITABLE_SET:
1135       celltext->editable_set = g_value_get_boolean (value);
1136       break;
1137
1138     case PROP_STRIKETHROUGH_SET:
1139       celltext->strikethrough_set = g_value_get_boolean (value);
1140       break;
1141
1142     case PROP_UNDERLINE_SET:
1143       celltext->underline_set = g_value_get_boolean (value);
1144       break;
1145
1146     case PROP_RISE_SET:
1147       celltext->rise_set = g_value_get_boolean (value);
1148       break;
1149
1150     case PROP_LANGUAGE_SET:
1151       priv->language_set = g_value_get_boolean (value);
1152       break;
1153
1154     default:
1155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1156       break;
1157     }
1158 }
1159
1160 /**
1161  * gtk_cell_renderer_text_new:
1162  * 
1163  * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1164  * object properties. Object properties can be
1165  * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1166  * you can bind a property to a value in a #GtkTreeModel. For example,
1167  * you can bind the "text" property on the cell renderer to a string
1168  * value in the model, thus rendering a different string in each row
1169  * of the #GtkTreeView
1170  * 
1171  * Return value: the new cell renderer
1172  **/
1173 GtkCellRenderer *
1174 gtk_cell_renderer_text_new (void)
1175 {
1176   return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1177 }
1178
1179 static void
1180 add_attr (PangoAttrList  *attr_list,
1181           PangoAttribute *attr)
1182 {
1183   attr->start_index = 0;
1184   attr->end_index = G_MAXINT;
1185   
1186   pango_attr_list_insert (attr_list, attr);
1187 }
1188
1189 static PangoLayout*
1190 get_layout (GtkCellRendererText *celltext,
1191             GtkWidget           *widget,
1192             gboolean             will_render,
1193             GtkCellRendererState flags)
1194 {
1195   PangoAttrList *attr_list;
1196   PangoLayout *layout;
1197   PangoUnderline uline;
1198   GtkCellRendererTextPrivate *priv;
1199
1200   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);
1201   
1202   layout = gtk_widget_create_pango_layout (widget, celltext->text);
1203
1204   if (celltext->extra_attrs)
1205     attr_list = pango_attr_list_copy (celltext->extra_attrs);
1206   else
1207     attr_list = pango_attr_list_new ();
1208
1209   pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1210
1211   if (will_render)
1212     {
1213       /* Add options that affect appearance but not size */
1214       
1215       /* note that background doesn't go here, since it affects
1216        * background_area not the PangoLayout area
1217        */
1218       
1219       if (celltext->foreground_set)
1220         {
1221           PangoColor color;
1222
1223           color = celltext->foreground;
1224           
1225           add_attr (attr_list,
1226                     pango_attr_foreground_new (color.red, color.green, color.blue));
1227         }
1228
1229       if (celltext->strikethrough_set)
1230         add_attr (attr_list,
1231                   pango_attr_strikethrough_new (celltext->strikethrough));
1232     }
1233
1234   add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1235
1236   if (celltext->scale_set &&
1237       celltext->font_scale != 1.0)
1238     add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1239   
1240   if (celltext->underline_set)
1241     uline = celltext->underline_style;
1242   else
1243     uline = PANGO_UNDERLINE_NONE;
1244
1245   if (priv->language_set)
1246     add_attr (attr_list, pango_attr_language_new (priv->language));
1247   
1248   if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1249     {
1250       switch (uline)
1251         {
1252         case PANGO_UNDERLINE_NONE:
1253           uline = PANGO_UNDERLINE_SINGLE;
1254           break;
1255
1256         case PANGO_UNDERLINE_SINGLE:
1257           uline = PANGO_UNDERLINE_DOUBLE;
1258           break;
1259
1260         default:
1261           break;
1262         }
1263     }
1264
1265   if (uline != PANGO_UNDERLINE_NONE)
1266     add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1267
1268   if (celltext->rise_set)
1269     add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1270   
1271   pango_layout_set_attributes (layout, attr_list);
1272   pango_layout_set_width (layout, -1);
1273
1274   pango_attr_list_unref (attr_list);
1275   
1276   return layout;
1277 }
1278
1279 static void
1280 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1281                                  GtkWidget       *widget,
1282                                  GdkRectangle    *cell_area,
1283                                  gint            *x_offset,
1284                                  gint            *y_offset,
1285                                  gint            *width,
1286                                  gint            *height)
1287 {
1288   GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1289   PangoRectangle rect;
1290   PangoLayout *layout;
1291
1292   if (celltext->calc_fixed_height)
1293     {
1294       PangoContext *context;
1295       PangoFontMetrics *metrics;
1296       PangoFontDescription *font_desc;
1297       gint row_height;
1298
1299       font_desc = pango_font_description_copy (widget->style->font_desc);
1300       pango_font_description_merge (font_desc, celltext->font, TRUE);
1301
1302       if (celltext->scale_set)
1303         pango_font_description_set_size (font_desc,
1304                                          celltext->font_scale * pango_font_description_get_size (font_desc));
1305
1306       context = gtk_widget_get_pango_context (widget);
1307
1308       metrics = pango_context_get_metrics (context,
1309                                            font_desc,
1310                                            pango_context_get_language (context));
1311       row_height = (pango_font_metrics_get_ascent (metrics) +
1312                     pango_font_metrics_get_descent (metrics));
1313       pango_font_metrics_unref (metrics);
1314
1315       pango_font_description_free (font_desc);
1316
1317       gtk_cell_renderer_set_fixed_size (cell,
1318                                         cell->width, 2*cell->ypad +
1319                                         celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1320       
1321       if (height)
1322         {
1323           *height = cell->height;
1324           height = NULL;
1325         }
1326       celltext->calc_fixed_height = FALSE;
1327       if (width == NULL)
1328         return;
1329     }
1330   layout = get_layout (celltext, widget, FALSE, 0);
1331   pango_layout_get_pixel_extents (layout, NULL, &rect);
1332
1333   if (width)
1334     *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1335
1336   if (height)
1337     *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1338
1339   if (cell_area)
1340     {
1341       if (x_offset)
1342         {
1343           *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
1344                        (1.0 - cell->xalign) : cell->xalign) * (cell_area->width - rect.width - (2 * cell->xpad));
1345           *x_offset = MAX (*x_offset, 0);
1346         }
1347       if (y_offset)
1348         {
1349           *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1350           *y_offset = MAX (*y_offset, 0);
1351         }
1352     }
1353
1354   g_object_unref (layout);
1355 }
1356
1357 static void
1358 gtk_cell_renderer_text_render (GtkCellRenderer      *cell,
1359                                GdkDrawable          *window,
1360                                GtkWidget            *widget,
1361                                GdkRectangle         *background_area,
1362                                GdkRectangle         *cell_area,
1363                                GdkRectangle         *expose_area,
1364                                GtkCellRendererState  flags)
1365
1366 {
1367   GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1368   PangoLayout *layout;
1369   GtkStateType state;
1370   gint x_offset;
1371   gint y_offset;
1372
1373   layout = get_layout (celltext, widget, TRUE, flags);
1374
1375   gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1376
1377   if (!cell->sensitive) 
1378     {
1379       state = GTK_STATE_INSENSITIVE;
1380     }
1381   else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1382     {
1383       if (GTK_WIDGET_HAS_FOCUS (widget))
1384         state = GTK_STATE_SELECTED;
1385       else
1386         state = GTK_STATE_ACTIVE;
1387     }
1388   else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
1389            GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT)
1390     {
1391       state = GTK_STATE_PRELIGHT;
1392     }
1393   else
1394     {
1395       if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
1396         state = GTK_STATE_INSENSITIVE;
1397       else
1398         state = GTK_STATE_NORMAL;
1399     }
1400
1401   if (celltext->background_set && state != GTK_STATE_SELECTED)
1402     {
1403       GdkColor color;
1404       GdkGC *gc;
1405       
1406       color.red = celltext->background.red;
1407       color.green = celltext->background.green;
1408       color.blue = celltext->background.blue;
1409
1410       gc = gdk_gc_new (window);
1411
1412       gdk_gc_set_rgb_fg_color (gc, &color);
1413
1414       if (expose_area)               
1415         gdk_gc_set_clip_rectangle (gc, expose_area);
1416       gdk_draw_rectangle (window,
1417                           gc,
1418                           TRUE,
1419                           background_area->x,
1420                           background_area->y,
1421                           background_area->width,
1422                           background_area->height);
1423       if (expose_area)               
1424         gdk_gc_set_clip_rectangle (gc, NULL);
1425       g_object_unref (gc);
1426     }
1427
1428   gtk_paint_layout (widget->style,
1429                     window,
1430                     state,
1431                     TRUE,
1432                     expose_area,
1433                     widget,
1434                     "cellrenderertext",
1435                     cell_area->x + x_offset + cell->xpad,
1436                     cell_area->y + y_offset + cell->ypad,
1437                     layout);
1438
1439   g_object_unref (layout);
1440 }
1441
1442 static void
1443 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1444                                      gpointer         data)
1445 {
1446   const gchar *path;
1447   const gchar *new_text;
1448   GtkCellRendererTextPrivate *priv;
1449
1450   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (data);
1451
1452   if (priv->focus_out_id > 0)
1453     {
1454       g_signal_handler_disconnect (entry, priv->focus_out_id);
1455       priv->focus_out_id = 0;
1456     }
1457
1458   if (GTK_ENTRY (entry)->editing_canceled)
1459     {
1460       gtk_cell_renderer_editing_canceled (GTK_CELL_RENDERER (data));
1461       return;
1462     }
1463
1464   path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1465   new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1466
1467   g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1468 }
1469
1470 static gboolean
1471 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
1472                                         GdkEvent  *event,
1473                                         gpointer   data)
1474 {
1475   gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (entry), data);
1476
1477   /* entry needs focus-out-event */
1478   return FALSE;
1479 }
1480
1481 static GtkCellEditable *
1482 gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
1483                                       GdkEvent             *event,
1484                                       GtkWidget            *widget,
1485                                       const gchar          *path,
1486                                       GdkRectangle         *background_area,
1487                                       GdkRectangle         *cell_area,
1488                                       GtkCellRendererState  flags)
1489 {
1490   GtkWidget *entry;
1491   GtkCellRendererText *celltext;
1492   GtkCellRendererTextPrivate *priv;
1493
1494   celltext = GTK_CELL_RENDERER_TEXT (cell);
1495   priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (cell);
1496
1497   /* If the cell isn't editable we return NULL. */
1498   if (celltext->editable == FALSE)
1499     return NULL;
1500
1501   entry = g_object_new (GTK_TYPE_ENTRY,
1502                         "has_frame", FALSE,
1503                         NULL);
1504
1505   if (celltext->text)
1506     gtk_entry_set_text (GTK_ENTRY (entry), celltext->text);
1507   g_object_set_data_full (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
1508   
1509   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1510   
1511   gtk_widget_show (entry);
1512   g_signal_connect (entry,
1513                     "editing_done",
1514                     G_CALLBACK (gtk_cell_renderer_text_editing_done),
1515                     celltext);
1516   priv->focus_out_id = g_signal_connect (entry, "focus_out_event",
1517                          G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
1518                          celltext);
1519
1520   return GTK_CELL_EDITABLE (entry);
1521
1522 }
1523
1524 /**
1525  * gtk_cell_renderer_text_set_fixed_height_from_font:
1526  * @renderer: A #GtkCellRendererText
1527  * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1528  * 
1529  * Sets the height of a renderer to explicitly be determined by the "font" and
1530  * "y_pad" property set on it.  Further changes in these properties do not
1531  * affect the height, so they must be accompanied by a subsequent call to this
1532  * function.  Using this function is unflexible, and should really only be used
1533  * if calculating the size of a cell is too slow (ie, a massive number of cells
1534  * displayed).  If @number_of_rows is -1, then the fixed height is unset, and
1535  * the height is determined by the properties again.
1536  **/
1537 void
1538 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1539                                                    gint                 number_of_rows)
1540 {
1541   g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1542   g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1543
1544   if (number_of_rows == -1)
1545     {
1546       gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1547                                         GTK_CELL_RENDERER (renderer)->width,
1548                                         -1);
1549     }
1550   else
1551     {
1552       renderer->fixed_height_rows = number_of_rows;
1553       renderer->calc_fixed_height = TRUE;
1554     }
1555 }