]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderertext.c
353e4e02e099b8d63e3e52b1269c12924951890d
[~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    g_object_class_install_property (object_class,
289                                    PROP_STRETCH,
290                                    g_param_spec_enum ("stretch",
291                                                       _("Font stretch"),
292                                                       _("Font stretch"),
293                                                       PANGO_TYPE_STRETCH,
294                                                       PANGO_STRETCH_NORMAL,
295                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
296   
297   g_object_class_install_property (object_class,
298                                    PROP_SIZE,
299                                    g_param_spec_int ("size",
300                                                      _("Font size"),
301                                                      _("Font size"),
302                                                      0,
303                                                      G_MAXINT,
304                                                      0,
305                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
306
307   g_object_class_install_property (object_class,
308                                    PROP_SIZE_POINTS,
309                                    g_param_spec_double ("size_points",
310                                                         _("Font points"),
311                                                         _("Font size in points"),
312                                                         0.0,
313                                                         G_MAXDOUBLE,
314                                                         0.0,
315                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));  
316
317   g_object_class_install_property (object_class,
318                                    PROP_SCALE,
319                                    g_param_spec_double ("scale",
320                                                         _("Font scale"),
321                                                         _("Font scaling factor"),
322                                                         0.0,
323                                                         G_MAXDOUBLE,
324                                                         1.0,
325                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
326   
327   g_object_class_install_property (object_class,
328                                    PROP_RISE,
329                                    g_param_spec_int ("rise",
330                                                      _("Rise"),
331                                                      _("Offset of text above the baseline (below the baseline if rise is negative)"),
332                                                      -G_MAXINT,
333                                                      G_MAXINT,
334                                                      0,
335                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
336
337
338   g_object_class_install_property (object_class,
339                                    PROP_STRIKETHROUGH,
340                                    g_param_spec_boolean ("strikethrough",
341                                                          _("Strikethrough"),
342                                                          _("Whether to strike through the text"),
343                                                          FALSE,
344                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
345   
346   g_object_class_install_property (object_class,
347                                    PROP_UNDERLINE,
348                                    g_param_spec_enum ("underline",
349                                                       _("Underline"),
350                                                       _("Style of underline for this text"),
351                                                       PANGO_TYPE_UNDERLINE,
352                                                       PANGO_UNDERLINE_NONE,
353                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
354
355   /* Style props are set or not */
356
357 #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))
358
359   ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
360                 _("Background set"),
361                 _("Whether this tag affects the background color"));
362
363   ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
364                 _("Foreground set"),
365                 _("Whether this tag affects the foreground color"));
366   
367   ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
368                 _("Editability set"),
369                 _("Whether this tag affects text editability"));
370
371   ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
372                 _("Font family set"),
373                 _("Whether this tag affects the font family"));  
374
375   ADD_SET_PROP ("style_set", PROP_STYLE_SET,
376                 _("Font style set"),
377                 _("Whether this tag affects the font style"));
378
379   ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
380                 _("Font variant set"),
381                 _("Whether this tag affects the font variant"));
382
383   ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
384                 _("Font weight set"),
385                 _("Whether this tag affects the font weight"));
386
387   ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
388                 _("Font stretch set"),
389                 _("Whether this tag affects the font stretch"));
390
391   ADD_SET_PROP ("size_set", PROP_SIZE_SET,
392                 _("Font size set"),
393                 _("Whether this tag affects the font size"));
394
395   ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
396                 _("Font scale set"),
397                 _("Whether this tag scales the font size by a factor"));
398   
399   ADD_SET_PROP ("rise_set", PROP_RISE_SET,
400                 _("Rise set"),
401                 _("Whether this tag affects the rise"));
402
403   ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
404                 _("Strikethrough set"),
405                 _("Whether this tag affects strikethrough"));
406
407   ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
408                 _("Underline set"),
409                 _("Whether this tag affects underlining"));
410
411   text_cell_renderer_signals [EDITED] =
412     gtk_signal_new ("edited",
413                     GTK_RUN_LAST,
414                     GTK_CLASS_TYPE (object_class),
415                     GTK_SIGNAL_OFFSET (GtkCellRendererTextClass, edited),
416                     gtk_marshal_VOID__STRING_STRING,
417                     GTK_TYPE_NONE, 2,
418                     G_TYPE_STRING,
419                     G_TYPE_STRING);
420
421 }
422
423 static void
424 gtk_cell_renderer_text_finalize (GObject *object)
425 {
426   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
427
428   pango_font_description_free (celltext->font);
429
430   if (celltext->text)
431     g_free (celltext->text);
432
433   if (celltext->extra_attrs)
434     pango_attr_list_unref (celltext->extra_attrs);
435
436   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
437 }
438
439 static PangoFontMask
440 get_property_font_set_mask (guint prop_id)
441 {
442   switch (prop_id)
443     {
444     case PROP_FAMILY_SET:
445       return PANGO_FONT_MASK_FAMILY;
446     case PROP_STYLE_SET:
447       return PANGO_FONT_MASK_STYLE;
448     case PROP_VARIANT_SET:
449       return PANGO_FONT_MASK_VARIANT;
450     case PROP_WEIGHT_SET:
451       return PANGO_FONT_MASK_WEIGHT;
452     case PROP_STRETCH_SET:
453       return PANGO_FONT_MASK_STRETCH;
454     case PROP_SIZE_SET:
455       return PANGO_FONT_MASK_SIZE;
456     }
457
458   return 0;
459 }
460
461 static void
462 gtk_cell_renderer_text_get_property (GObject        *object,
463                                      guint           param_id,
464                                      GValue         *value,
465                                      GParamSpec     *pspec)
466 {
467   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
468
469   switch (param_id)
470     {
471     case PROP_TEXT:
472       g_value_set_string (value, celltext->text);
473       break;
474
475     case PROP_ATTRIBUTES:
476       g_value_set_boxed (value, celltext->extra_attrs);
477       break;
478
479     case PROP_BACKGROUND_GDK:
480       {
481         GdkColor color;
482         
483         color.red = celltext->background.red;
484         color.green = celltext->background.green;
485         color.blue = celltext->background.blue;
486         
487         g_value_set_boxed (value, &color);
488       }
489       break;
490
491     case PROP_FOREGROUND_GDK:
492       {
493         GdkColor color;
494         
495         color.red = celltext->foreground.red;
496         color.green = celltext->foreground.green;
497         color.blue = celltext->foreground.blue;
498         
499         g_value_set_boxed (value, &color);
500       }
501       break;
502
503     case PROP_FONT:
504       {
505         /* FIXME GValue imposes a totally gratuitous string copy
506          * here, we could just hand off string ownership
507          */
508         gchar *str = pango_font_description_to_string (celltext->font);
509         g_value_set_string (value, str);
510         g_free (str);
511       }
512       break;
513       
514     case PROP_FONT_DESC:
515       g_value_set_boxed (value, celltext->font);
516       break;
517
518     case PROP_FAMILY:
519       g_value_set_string (value, pango_font_description_get_family (celltext->font));
520       break;
521
522     case PROP_STYLE:
523       g_value_set_enum (value, pango_font_description_get_style (celltext->font));
524       break;
525
526     case PROP_VARIANT:
527       g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
528       break;
529
530     case PROP_WEIGHT:
531       g_value_set_int (value, pango_font_description_get_weight (celltext->font));
532       break;
533
534     case PROP_STRETCH:
535       g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
536       break;
537
538     case PROP_SIZE:
539       g_value_set_int (value, pango_font_description_get_size (celltext->font));
540       break;
541
542     case PROP_SIZE_POINTS:
543       g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
544       break;
545
546     case PROP_SCALE:
547       g_value_set_double (value, celltext->font_scale);
548       break;
549       
550     case PROP_EDITABLE:
551       g_value_set_boolean (value, celltext->editable);
552       break;
553
554     case PROP_STRIKETHROUGH:
555       g_value_set_boolean (value, celltext->strikethrough);
556       break;
557
558     case PROP_UNDERLINE:
559       g_value_set_enum (value, celltext->underline_style);
560       break;
561
562     case PROP_RISE:
563       g_value_set_int (value, celltext->rise);
564       break;  
565
566     case PROP_BACKGROUND_SET:
567       g_value_set_boolean (value, celltext->background_set);
568       break;
569
570     case PROP_FOREGROUND_SET:
571       g_value_set_boolean (value, celltext->foreground_set);
572       break;
573
574     case PROP_FAMILY_SET:
575     case PROP_STYLE_SET:
576     case PROP_VARIANT_SET:
577     case PROP_WEIGHT_SET:
578     case PROP_STRETCH_SET:
579     case PROP_SIZE_SET:
580       {
581         PangoFontMask mask = get_property_font_set_mask (param_id);
582         g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
583         
584         break;
585       }
586
587     case PROP_SCALE_SET:
588       g_value_set_boolean (value, celltext->scale_set);
589       break;
590       
591     case PROP_EDITABLE_SET:
592       g_value_set_boolean (value, celltext->editable_set);
593       break;
594
595     case PROP_STRIKETHROUGH_SET:
596       g_value_set_boolean (value, celltext->strikethrough_set);
597       break;
598
599     case PROP_UNDERLINE_SET:
600       g_value_set_boolean (value, celltext->underline_set);
601       break;
602
603     case  PROP_RISE_SET:
604       g_value_set_boolean (value, celltext->rise_set);
605       break;
606       
607     case PROP_BACKGROUND:
608     case PROP_FOREGROUND:
609     case PROP_MARKUP:
610     default:
611       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
612       break;
613     }
614 }
615
616
617 static void
618 set_bg_color (GtkCellRendererText *celltext,
619               GdkColor            *color)
620 {
621   if (color)
622     {
623       if (!celltext->background_set)
624         {
625           celltext->background_set = TRUE;
626           g_object_notify (G_OBJECT (celltext), "background_set");
627         }
628       
629       celltext->background.red = color->red;
630       celltext->background.green = color->green;
631       celltext->background.blue = color->blue;
632     }
633   else
634     {
635       if (celltext->background_set)
636         {
637           celltext->background_set = FALSE;
638           g_object_notify (G_OBJECT (celltext), "background_set");
639         }
640     }
641 }
642
643
644 static void
645 set_fg_color (GtkCellRendererText *celltext,
646               GdkColor            *color)
647 {
648   if (color)
649     {
650       if (!celltext->foreground_set)
651         {
652           celltext->foreground_set = TRUE;
653           g_object_notify (G_OBJECT (celltext), "foreground_set");
654         }
655       
656       celltext->foreground.red = color->red;
657       celltext->foreground.green = color->green;
658       celltext->foreground.blue = color->blue;
659     }
660   else
661     {
662       if (celltext->foreground_set)
663         {
664           celltext->foreground_set = FALSE;
665           g_object_notify (G_OBJECT (celltext), "foreground_set");
666         }
667     }
668 }
669
670 static PangoFontMask
671 set_font_desc_fields (PangoFontDescription *desc,
672                       PangoFontMask         to_set)
673 {
674   PangoFontMask changed_mask = 0;
675   
676   if (to_set & PANGO_FONT_MASK_FAMILY)
677     {
678       const char *family = pango_font_description_get_family (desc);
679       if (!family)
680         {
681           family = "sans";
682           changed_mask |= PANGO_FONT_MASK_FAMILY;
683         }
684
685       pango_font_description_set_family (desc, family);
686     }
687   if (to_set & PANGO_FONT_MASK_STYLE)
688     pango_font_description_set_style (desc, pango_font_description_get_style (desc));
689   if (to_set & PANGO_FONT_MASK_VARIANT)
690     pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
691   if (to_set & PANGO_FONT_MASK_WEIGHT)
692     pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
693   if (to_set & PANGO_FONT_MASK_STRETCH)
694     pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
695   if (to_set & PANGO_FONT_MASK_SIZE)
696     {
697       gint size = pango_font_description_get_size (desc);
698       if (size <= 0)
699         {
700           size = 10 * PANGO_SCALE;
701           changed_mask |= PANGO_FONT_MASK_SIZE;
702         }
703       
704       pango_font_description_set_size (desc, size);
705     }
706
707   return changed_mask;
708 }
709
710 static void
711 notify_set_changed (GObject       *object,
712                     PangoFontMask  changed_mask)
713 {
714   if (changed_mask & PANGO_FONT_MASK_FAMILY)
715     g_object_notify (object, "family_set");
716   if (changed_mask & PANGO_FONT_MASK_STYLE)
717     g_object_notify (object, "style_set");
718   if (changed_mask & PANGO_FONT_MASK_VARIANT)
719     g_object_notify (object, "variant_set");
720   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
721     g_object_notify (object, "weight_set");
722   if (changed_mask & PANGO_FONT_MASK_STRETCH)
723     g_object_notify (object, "stretch_set");
724   if (changed_mask & PANGO_FONT_MASK_SIZE)
725     g_object_notify (object, "size_set");
726 }
727
728 static void
729 notify_fields_changed (GObject       *object,
730                        PangoFontMask  changed_mask)
731 {
732   if (changed_mask & PANGO_FONT_MASK_FAMILY)
733     g_object_notify (object, "family");
734   if (changed_mask & PANGO_FONT_MASK_STYLE)
735     g_object_notify (object, "style");
736   if (changed_mask & PANGO_FONT_MASK_VARIANT)
737     g_object_notify (object, "variant");
738   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
739     g_object_notify (object, "weight");
740   if (changed_mask & PANGO_FONT_MASK_STRETCH)
741     g_object_notify (object, "stretch");
742   if (changed_mask & PANGO_FONT_MASK_SIZE)
743     g_object_notify (object, "size");
744 }
745
746 static void
747 set_font_description (GtkCellRendererText  *celltext,
748                       PangoFontDescription *font_desc)
749 {
750   GObject *object = G_OBJECT (celltext);
751   PangoFontDescription *new_font_desc;
752   PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
753   
754   if (font_desc)
755     new_font_desc = pango_font_description_copy (font_desc);
756   else
757     new_font_desc = pango_font_description_new ();
758
759   old_mask = pango_font_description_get_set_fields (celltext->font);
760   new_mask = pango_font_description_get_set_fields (new_font_desc);
761
762   changed_mask = old_mask | new_mask;
763   set_changed_mask = old_mask ^ new_mask;
764
765   pango_font_description_free (celltext->font);
766   celltext->font = new_font_desc;
767   
768   g_object_freeze_notify (object);
769
770   g_object_notify (object, "font_desc");
771   g_object_notify (object, "font");
772   
773   if (changed_mask & PANGO_FONT_MASK_FAMILY)
774     g_object_notify (object, "family");
775   if (changed_mask & PANGO_FONT_MASK_STYLE)
776     g_object_notify (object, "style");
777   if (changed_mask & PANGO_FONT_MASK_VARIANT)
778     g_object_notify (object, "variant");
779   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
780     g_object_notify (object, "weight");
781   if (changed_mask & PANGO_FONT_MASK_STRETCH)
782     g_object_notify (object, "stretch");
783   if (changed_mask & PANGO_FONT_MASK_SIZE)
784     {
785       g_object_notify (object, "size");
786       g_object_notify (object, "size_points");
787     }
788
789   notify_set_changed (object, set_changed_mask);
790   
791   g_object_thaw_notify (object);
792 }
793
794 static void
795 gtk_cell_renderer_text_set_property (GObject      *object,
796                                      guint         param_id,
797                                      const GValue *value,
798                                      GParamSpec   *pspec)
799 {
800   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
801
802   switch (param_id)
803     {
804     case PROP_TEXT:
805       if (celltext->text)
806         g_free (celltext->text);
807       celltext->text = g_strdup (g_value_get_string (value));
808       g_object_notify (object, "text");
809       break;
810
811     case PROP_ATTRIBUTES:
812       if (celltext->extra_attrs)
813         pango_attr_list_unref (celltext->extra_attrs);
814
815       celltext->extra_attrs = g_value_get_boxed (value);
816       if (celltext->extra_attrs)
817         pango_attr_list_ref (celltext->extra_attrs);
818       break;
819     case PROP_MARKUP:
820       {
821         const gchar *str;
822         gchar *text = NULL;
823         GError *error = NULL;
824         PangoAttrList *attrs = NULL;
825         
826         if (celltext->text)
827           g_free (celltext->text);
828
829         if (celltext->extra_attrs)
830           pango_attr_list_unref (celltext->extra_attrs);
831
832         str = g_value_get_string (value);
833         if (str && !pango_parse_markup (str,
834                                         -1,
835                                         0,
836                                         &attrs,
837                                         &text,
838                                         NULL,
839                                         &error))
840           {
841             g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
842                        error->message);
843             g_error_free (error);
844             return;
845           }
846         
847         celltext->text = text;
848         celltext->extra_attrs = attrs;
849       }
850       break;
851       
852     case PROP_BACKGROUND:
853       {
854         GdkColor color;
855
856         if (!g_value_get_string (value))
857           set_bg_color (celltext, NULL);       /* reset to backgrounmd_set to FALSE */
858         else if (gdk_color_parse (g_value_get_string (value), &color))
859           set_bg_color (celltext, &color);
860         else
861           g_warning ("Don't know color `%s'", g_value_get_string (value));
862
863         g_object_notify (object, "background_gdk");
864       }
865       break;
866       
867     case PROP_FOREGROUND:
868       {
869         GdkColor color;
870
871         if (!g_value_get_string (value))
872           set_fg_color (celltext, NULL);       /* reset to foreground_set to FALSE */
873         else if (gdk_color_parse (g_value_get_string (value), &color))
874           set_fg_color (celltext, &color);
875         else
876           g_warning ("Don't know color `%s'", g_value_get_string (value));
877
878         g_object_notify (object, "foreground_gdk");
879       }
880       break;
881
882     case PROP_BACKGROUND_GDK:
883       /* This notifies the GObject itself. */
884       set_bg_color (celltext, g_value_get_boxed (value));
885       break;
886
887     case PROP_FOREGROUND_GDK:
888       /* This notifies the GObject itself. */
889       set_fg_color (celltext, g_value_get_boxed (value));
890       break;
891
892     case PROP_FONT:
893       {
894         PangoFontDescription *font_desc = NULL;
895         const gchar *name;
896
897         name = g_value_get_string (value);
898
899         if (name)
900           font_desc = pango_font_description_from_string (name);
901
902         set_font_description (celltext, font_desc);
903         
904         if (celltext->fixed_height_rows != -1)
905           celltext->calc_fixed_height = TRUE;
906       }
907       break;
908
909     case PROP_FONT_DESC:
910       set_font_description (celltext, g_value_get_boxed (value));
911       
912       if (celltext->fixed_height_rows != -1)
913         celltext->calc_fixed_height = TRUE;
914       break;
915
916     case PROP_FAMILY:
917     case PROP_STYLE:
918     case PROP_VARIANT:
919     case PROP_WEIGHT:
920     case PROP_STRETCH:
921     case PROP_SIZE:
922     case PROP_SIZE_POINTS:
923       {
924         PangoFontMask old_set_mask = pango_font_description_get_set_fields (celltext->font);
925         
926         switch (param_id)
927           {
928           case PROP_FAMILY:
929             pango_font_description_set_family (celltext->font,
930                                                g_value_get_string (value));
931             break;
932           case PROP_STYLE:
933             pango_font_description_set_style (celltext->font,
934                                               g_value_get_enum (value));
935             break;
936           case PROP_VARIANT:
937             pango_font_description_set_variant (celltext->font,
938                                                 g_value_get_enum (value));
939             break;
940           case PROP_WEIGHT:
941             pango_font_description_set_weight (celltext->font,
942                                                g_value_get_int (value));
943             break;
944           case PROP_STRETCH:
945             pango_font_description_set_stretch (celltext->font,
946                                                 g_value_get_enum (value));
947             break;
948           case PROP_SIZE:
949             pango_font_description_set_size (celltext->font,
950                                              g_value_get_int (value));
951             g_object_notify (object, "size_points");
952             break;
953           case PROP_SIZE_POINTS:
954             pango_font_description_set_size (celltext->font,
955                                              g_value_get_double (value) * PANGO_SCALE);
956             g_object_notify (object, "size");
957             break;
958           }
959         
960         if (celltext->fixed_height_rows != -1)
961           celltext->calc_fixed_height = TRUE;
962         
963         notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (celltext->font));
964         g_object_notify (object, "font_desc");
965         g_object_notify (object, "font");
966
967         break;
968       }
969       
970     case PROP_SCALE:
971       celltext->font_scale = g_value_get_double (value);
972       celltext->scale_set = TRUE;
973       if (celltext->fixed_height_rows != -1)
974         celltext->calc_fixed_height = TRUE;
975       g_object_notify (object, "scale_set");
976       break;
977       
978     case PROP_EDITABLE:
979       celltext->editable = g_value_get_boolean (value);
980       celltext->editable_set = TRUE;
981       g_object_notify (object, "editable_set");
982       break;
983
984     case PROP_STRIKETHROUGH:
985       celltext->strikethrough = g_value_get_boolean (value);
986       celltext->strikethrough_set = TRUE;
987       g_object_notify (object, "strikethrough_set");
988       break;
989
990     case PROP_UNDERLINE:
991       celltext->underline_style = g_value_get_enum (value);
992       celltext->underline_set = TRUE;
993       g_object_notify (object, "underline_set");
994             
995       break;
996
997     case PROP_RISE:
998       celltext->rise = g_value_get_int (value);
999       celltext->rise_set = TRUE;
1000       g_object_notify (object, "rise_set");
1001       if (celltext->fixed_height_rows != -1)
1002         celltext->calc_fixed_height = TRUE;
1003       break;  
1004
1005     case PROP_BACKGROUND_SET:
1006       celltext->background_set = g_value_get_boolean (value);
1007       break;
1008
1009     case PROP_FOREGROUND_SET:
1010       celltext->foreground_set = g_value_get_boolean (value);
1011       break;
1012
1013     case PROP_FAMILY_SET:
1014     case PROP_STYLE_SET:
1015     case PROP_VARIANT_SET:
1016     case PROP_WEIGHT_SET:
1017     case PROP_STRETCH_SET:
1018     case PROP_SIZE_SET:
1019       if (!g_value_get_boolean (value))
1020         {
1021           pango_font_description_unset_fields (celltext->font,
1022                                                get_property_font_set_mask (param_id));
1023         }
1024       else
1025         {
1026           PangoFontMask changed_mask;
1027           
1028           changed_mask = set_font_desc_fields (celltext->font,
1029                                                get_property_font_set_mask (param_id));
1030           notify_fields_changed (G_OBJECT (celltext), changed_mask);
1031         }
1032       break;
1033
1034     case PROP_SCALE_SET:
1035       celltext->scale_set = g_value_get_boolean (value);
1036       break;
1037       
1038     case PROP_EDITABLE_SET:
1039       celltext->editable_set = g_value_get_boolean (value);
1040       break;
1041
1042     case PROP_STRIKETHROUGH_SET:
1043       celltext->strikethrough_set = g_value_get_boolean (value);
1044       break;
1045
1046     case PROP_UNDERLINE_SET:
1047       celltext->underline_set = g_value_get_boolean (value);
1048       break;
1049
1050     case PROP_RISE_SET:
1051       celltext->rise_set = g_value_get_boolean (value);
1052       break;
1053
1054     default:
1055       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1056       break;
1057     }
1058 }
1059
1060 /**
1061  * gtk_cell_renderer_text_new:
1062  * 
1063  * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1064  * object properties. Object properties can be
1065  * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1066  * you can bind a property to a value in a #GtkTreeModel. For example,
1067  * you can bind the "text" property on the cell renderer to a string
1068  * value in the model, thus rendering a different string in each row
1069  * of the #GtkTreeView
1070  * 
1071  * Return value: the new cell renderer
1072  **/
1073 GtkCellRenderer *
1074 gtk_cell_renderer_text_new (void)
1075 {
1076   return GTK_CELL_RENDERER (g_object_new (gtk_cell_renderer_text_get_type (), NULL));
1077 }
1078
1079 static void
1080 add_attr (PangoAttrList  *attr_list,
1081           PangoAttribute *attr)
1082 {
1083   attr->start_index = 0;
1084   attr->end_index = G_MAXINT;
1085   
1086   pango_attr_list_insert (attr_list, attr);
1087 }
1088
1089 static PangoLayout*
1090 get_layout (GtkCellRendererText *celltext,
1091             GtkWidget           *widget,
1092             gboolean             will_render,
1093             GtkCellRendererState flags)
1094 {
1095   PangoAttrList *attr_list;
1096   PangoLayout *layout;
1097   PangoUnderline uline;
1098   
1099   layout = gtk_widget_create_pango_layout (widget, celltext->text);
1100
1101   if (celltext->extra_attrs)
1102     attr_list = pango_attr_list_copy (celltext->extra_attrs);
1103   else
1104     attr_list = pango_attr_list_new ();
1105
1106   if (will_render)
1107     {
1108       /* Add options that affect appearance but not size */
1109       
1110       /* note that background doesn't go here, since it affects
1111        * background_area not the PangoLayout area
1112        */
1113       
1114       if (celltext->foreground_set)
1115         {
1116           PangoColor color;
1117
1118           color = celltext->foreground;
1119           
1120           add_attr (attr_list,
1121                     pango_attr_foreground_new (color.red, color.green, color.blue));
1122         }
1123
1124       if (celltext->strikethrough_set)
1125         add_attr (attr_list,
1126                   pango_attr_strikethrough_new (celltext->strikethrough));
1127     }
1128
1129   add_attr (attr_list, pango_attr_font_desc_new (celltext->font));
1130
1131   if (celltext->scale_set &&
1132       celltext->font_scale != 1.0)
1133     add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
1134   
1135   if (celltext->underline_set)
1136     uline = celltext->underline_style;
1137   else
1138     uline = PANGO_UNDERLINE_NONE;
1139   
1140   if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1141     {
1142       switch (uline)
1143         {
1144         case PANGO_UNDERLINE_NONE:
1145           uline = PANGO_UNDERLINE_SINGLE;
1146           break;
1147
1148         case PANGO_UNDERLINE_SINGLE:
1149           uline = PANGO_UNDERLINE_DOUBLE;
1150           break;
1151
1152         default:
1153           break;
1154         }
1155     }
1156
1157   if (uline != PANGO_UNDERLINE_NONE)
1158     add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));
1159
1160   if (celltext->rise_set)
1161     add_attr (attr_list, pango_attr_rise_new (celltext->rise));
1162   
1163   pango_layout_set_attributes (layout, attr_list);
1164   pango_layout_set_width (layout, -1);
1165
1166   pango_attr_list_unref (attr_list);
1167   
1168   return layout;
1169 }
1170
1171 static void
1172 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
1173                                  GtkWidget       *widget,
1174                                  GdkRectangle    *cell_area,
1175                                  gint            *x_offset,
1176                                  gint            *y_offset,
1177                                  gint            *width,
1178                                  gint            *height)
1179 {
1180   GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1181   PangoRectangle rect;
1182   PangoLayout *layout;
1183
1184   if (celltext->calc_fixed_height)
1185     {
1186       PangoContext *context;
1187       PangoFontMetrics *metrics;
1188       PangoFontDescription *font_desc;
1189       gint row_height;
1190
1191       font_desc = pango_font_description_copy (widget->style->font_desc);
1192       pango_font_description_merge (font_desc, celltext->font, TRUE);
1193
1194       if (celltext->scale_set)
1195         pango_font_description_set_size (font_desc,
1196                                          celltext->font_scale * pango_font_description_get_size (font_desc));
1197
1198       context = gtk_widget_get_pango_context (widget);
1199
1200       metrics = pango_context_get_metrics (context,
1201                                            font_desc,
1202                                            pango_context_get_language (context));
1203       row_height = (pango_font_metrics_get_ascent (metrics) +
1204                     pango_font_metrics_get_descent (metrics));
1205       pango_font_metrics_unref (metrics);
1206       
1207       gtk_cell_renderer_set_fixed_size (cell,
1208                                         cell->width, 2*cell->ypad +
1209                                         celltext->fixed_height_rows * PANGO_PIXELS (row_height));
1210       
1211       if (height)
1212         {
1213           *height = cell->height;
1214           height = NULL;
1215         }
1216       celltext->calc_fixed_height = FALSE;
1217       if (width == NULL)
1218         return;
1219     }
1220   layout = get_layout (celltext, widget, FALSE, 0);
1221   pango_layout_get_pixel_extents (layout, NULL, &rect);
1222
1223   if (width)
1224     *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
1225
1226   if (height)
1227     *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
1228
1229   if (cell_area)
1230     {
1231       if (x_offset)
1232         {
1233           *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
1234           *x_offset = MAX (*x_offset, 0);
1235         }
1236       if (y_offset)
1237         {
1238           *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
1239           *y_offset = MAX (*y_offset, 0);
1240         }
1241     }
1242
1243   g_object_unref (G_OBJECT (layout));
1244 }
1245
1246 static void
1247 gtk_cell_renderer_text_render (GtkCellRenderer    *cell,
1248                                GdkWindow          *window,
1249                                GtkWidget          *widget,
1250                                GdkRectangle       *background_area,
1251                                GdkRectangle       *cell_area,
1252                                GdkRectangle       *expose_area,
1253                                guint               flags)
1254
1255 {
1256   GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1257   PangoLayout *layout;
1258   GtkStateType state;
1259   gint x_offset;
1260   gint y_offset;
1261
1262   layout = get_layout (celltext, widget, TRUE, flags);
1263
1264   gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1265
1266   if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1267     {
1268       if (GTK_WIDGET_HAS_FOCUS (widget))
1269         state = GTK_STATE_SELECTED;
1270       else
1271         state = GTK_STATE_ACTIVE;
1272     }
1273   else
1274     {
1275       state = GTK_STATE_NORMAL;
1276     }
1277
1278   if (celltext->background_set && state != GTK_STATE_SELECTED)
1279     {
1280       GdkColor color;
1281       GdkGC *gc;
1282       
1283       color.red = celltext->background.red;
1284       color.green = celltext->background.green;
1285       color.blue = celltext->background.blue;
1286
1287       gc = gdk_gc_new (window);
1288
1289       gdk_gc_set_rgb_fg_color (gc, &color);
1290       
1291       gdk_draw_rectangle (window,
1292                           gc,
1293                           TRUE,
1294                           background_area->x,
1295                           background_area->y,
1296                           background_area->width,
1297                           background_area->height);
1298
1299       g_object_unref (G_OBJECT (gc));
1300     }
1301
1302   gtk_paint_layout (widget->style,
1303                     window,
1304                     state,
1305                     TRUE,
1306                     cell_area,
1307                     widget,
1308                     "cellrenderertext",
1309                     cell_area->x + x_offset + cell->xpad,
1310                     cell_area->y + y_offset + cell->ypad,
1311                     layout);
1312
1313   g_object_unref (G_OBJECT (layout));
1314 }
1315 static void
1316 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1317                                      gpointer         data)
1318 {
1319   gchar *path;
1320   gchar *new_text;
1321
1322   path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1323   new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1324
1325   gtk_signal_emit (GTK_OBJECT (data), text_cell_renderer_signals[EDITED], path, new_text);
1326 }
1327
1328 static GtkCellEditable *
1329 gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
1330                                       GdkEvent             *event,
1331                                       GtkWidget            *widget,
1332                                       gchar                *path,
1333                                       GdkRectangle         *background_area,
1334                                       GdkRectangle         *cell_area,
1335                                       GtkCellRendererState  flags)
1336 {
1337   GtkCellRendererText *celltext;
1338   GtkWidget *entry;
1339   
1340   celltext = GTK_CELL_RENDERER_TEXT (cell);
1341
1342   /* If the cell isn't editable we return NULL. */
1343   if (celltext->editable == FALSE)
1344     return NULL;
1345
1346   entry = g_object_new (GTK_TYPE_ENTRY,
1347                         "has_frame", FALSE,
1348                         NULL);
1349
1350   gtk_entry_set_text (GTK_ENTRY (entry), celltext->text);
1351   g_object_set_data_full (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
1352   
1353   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1354   
1355   gtk_widget_show (entry);
1356   gtk_signal_connect (GTK_OBJECT (entry),
1357                       "editing_done",
1358                       G_CALLBACK (gtk_cell_renderer_text_editing_done),
1359                       celltext);
1360   return GTK_CELL_EDITABLE (entry);
1361
1362 }
1363
1364 /**
1365  * gtk_cell_renderer_text_set_fixed_height_from_font:
1366  * @renderer: A #GtkCellRendererText
1367  * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1368  * 
1369  * Sets the height of a renderer to explicitly be determined by the "font" and
1370  * "y_pad" property set on it.  Further changes in these properties do not
1371  * affect the height, so they must be accompanied by a subsequent call to this
1372  * function.  Using this function is unflexible, and should really only be used
1373  * if calculating the size of a cell is too slow (ie, a massive number of cells
1374  * displayed).  If @number_of_rows is -1, then the fixed height is unset, and
1375  * the height is determined by the properties again.
1376  **/
1377 void
1378 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
1379                                                    gint                 number_of_rows)
1380 {
1381   g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1382   g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1383
1384   if (number_of_rows == -1)
1385     {
1386       gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
1387                                         GTK_CELL_RENDERER (renderer)->width,
1388                                         -1);
1389     }
1390   else
1391     {
1392       renderer->fixed_height_rows = number_of_rows;
1393       renderer->calc_fixed_height = TRUE;
1394     }
1395 }