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