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