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