]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderertext.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcellrenderertext.h"
21
22 #include <stdlib.h>
23
24 #include "gtkeditable.h"
25 #include "gtkentry.h"
26 #include "gtksizerequest.h"
27 #include "gtkmarshalers.h"
28 #include "gtkintl.h"
29 #include "gtkprivate.h"
30 #include "gtktreeprivate.h"
31 #include "a11y/gtktextcellaccessible.h"
32
33
34 /**
35  * SECTION:gtkcellrenderertext
36  * @Short_description: Renders text in a cell
37  * @Title: GtkCellRendererText
38  *
39  * A #GtkCellRendererText renders a given text in its cell, using the font, color and
40  * style information provided by its properties. The text will be ellipsized if it is
41  * too long and the #GtkCellRendererText:ellipsize property allows it.
42  *
43  * If the #GtkCellRenderer:mode is %GTK_CELL_RENDERER_MODE_EDITABLE,
44  * the #GtkCellRendererText allows to edit its text using an entry.
45  */
46
47
48 static void gtk_cell_renderer_text_finalize   (GObject                  *object);
49
50 static void gtk_cell_renderer_text_get_property  (GObject                  *object,
51                                                   guint                     param_id,
52                                                   GValue                   *value,
53                                                   GParamSpec               *pspec);
54 static void gtk_cell_renderer_text_set_property  (GObject                  *object,
55                                                   guint                     param_id,
56                                                   const GValue             *value,
57                                                   GParamSpec               *pspec);
58 static void gtk_cell_renderer_text_render     (GtkCellRenderer          *cell,
59                                                cairo_t                  *cr,
60                                                GtkWidget                *widget,
61                                                const GdkRectangle       *background_area,
62                                                const GdkRectangle       *cell_area,
63                                                GtkCellRendererState      flags);
64
65 static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
66                                                               GdkEvent             *event,
67                                                               GtkWidget            *widget,
68                                                               const gchar          *path,
69                                                               const GdkRectangle   *background_area,
70                                                               const GdkRectangle   *cell_area,
71                                                               GtkCellRendererState  flags);
72
73 static void       gtk_cell_renderer_text_get_preferred_width            (GtkCellRenderer       *cell,
74                                                                          GtkWidget             *widget,
75                                                                          gint                  *minimal_size,
76                                                                          gint                  *natural_size);
77 static void       gtk_cell_renderer_text_get_preferred_height           (GtkCellRenderer       *cell,
78                                                                          GtkWidget             *widget,
79                                                                          gint                  *minimal_size,
80                                                                          gint                  *natural_size);
81 static void       gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer       *cell,
82                                                                          GtkWidget             *widget,
83                                                                          gint                   width,
84                                                                          gint                  *minimum_height,
85                                                                          gint                  *natural_height);
86 static void       gtk_cell_renderer_text_get_aligned_area               (GtkCellRenderer       *cell,
87                                                                          GtkWidget             *widget,
88                                                                          GtkCellRendererState   flags,
89                                                                          const GdkRectangle    *cell_area,
90                                                                          GdkRectangle          *aligned_area);
91
92
93
94 enum {
95   EDITED,
96   LAST_SIGNAL
97 };
98
99 enum {
100   PROP_0,
101
102   PROP_TEXT,
103   PROP_MARKUP,
104   PROP_ATTRIBUTES,
105   PROP_SINGLE_PARAGRAPH_MODE,
106   PROP_WIDTH_CHARS,
107   PROP_MAX_WIDTH_CHARS,
108   PROP_WRAP_WIDTH,
109   PROP_ALIGN,
110   PROP_PLACEHOLDER_TEXT,
111
112   /* Style args */
113   PROP_BACKGROUND,
114   PROP_FOREGROUND,
115   PROP_BACKGROUND_GDK,
116   PROP_FOREGROUND_GDK,
117   PROP_BACKGROUND_RGBA,
118   PROP_FOREGROUND_RGBA,
119   PROP_FONT,
120   PROP_FONT_DESC,
121   PROP_FAMILY,
122   PROP_STYLE,
123   PROP_VARIANT,
124   PROP_WEIGHT,
125   PROP_STRETCH,
126   PROP_SIZE,
127   PROP_SIZE_POINTS,
128   PROP_SCALE,
129   PROP_EDITABLE,
130   PROP_STRIKETHROUGH,
131   PROP_UNDERLINE,
132   PROP_RISE,
133   PROP_LANGUAGE,
134   PROP_ELLIPSIZE,
135   PROP_WRAP_MODE,
136   
137   /* Whether-a-style-arg-is-set args */
138   PROP_BACKGROUND_SET,
139   PROP_FOREGROUND_SET,
140   PROP_FAMILY_SET,
141   PROP_STYLE_SET,
142   PROP_VARIANT_SET,
143   PROP_WEIGHT_SET,
144   PROP_STRETCH_SET,
145   PROP_SIZE_SET,
146   PROP_SCALE_SET,
147   PROP_EDITABLE_SET,
148   PROP_STRIKETHROUGH_SET,
149   PROP_UNDERLINE_SET,
150   PROP_RISE_SET,
151   PROP_LANGUAGE_SET,
152   PROP_ELLIPSIZE_SET,
153   PROP_ALIGN_SET
154 };
155
156 static guint text_cell_renderer_signals [LAST_SIGNAL];
157
158 #define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
159
160 struct _GtkCellRendererTextPrivate
161 {
162   GtkWidget *entry;
163
164   PangoAttrList        *extra_attrs;
165   GdkRGBA               foreground;
166   GdkRGBA               background;
167   PangoAlignment        align;
168   PangoEllipsizeMode    ellipsize;
169   PangoFontDescription *font;
170   PangoLanguage        *language;
171   PangoUnderline        underline_style;
172   PangoWrapMode         wrap_mode;
173
174   gchar *text;
175   gchar *placeholder_text;
176
177   gdouble font_scale;
178
179   gint rise;
180   gint fixed_height_rows;
181   gint width_chars;
182   gint max_width_chars;
183   gint wrap_width;
184
185   guint in_entry_menu     : 1;
186   guint strikethrough     : 1;
187   guint editable          : 1;
188   guint scale_set         : 1;
189   guint foreground_set    : 1;
190   guint background_set    : 1;
191   guint underline_set     : 1;
192   guint rise_set          : 1;
193   guint strikethrough_set : 1;
194   guint editable_set      : 1;
195   guint calc_fixed_height : 1;
196   guint single_paragraph  : 1;
197   guint language_set      : 1;
198   guint markup_set        : 1;
199   guint ellipsize_set     : 1;
200   guint align_set         : 1;
201
202   gulong focus_out_id;
203   gulong populate_popup_id;
204   gulong entry_menu_popdown_timeout;
205 };
206
207 G_DEFINE_TYPE (GtkCellRendererText, gtk_cell_renderer_text, GTK_TYPE_CELL_RENDERER)
208
209 static void
210 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
211 {
212   GtkCellRendererTextPrivate *priv;
213   GtkCellRenderer *cell = GTK_CELL_RENDERER (celltext);
214
215   celltext->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltext,
216                                                 GTK_TYPE_CELL_RENDERER_TEXT,
217                                                 GtkCellRendererTextPrivate);
218   priv = celltext->priv;
219
220   gtk_cell_renderer_set_alignment (cell, 0.0, 0.5);
221   gtk_cell_renderer_set_padding (cell, 2, 2);
222   priv->font_scale = 1.0;
223   priv->fixed_height_rows = -1;
224   priv->font = pango_font_description_new ();
225
226   priv->width_chars = -1;
227   priv->max_width_chars = -1;
228   priv->wrap_width = -1;
229   priv->wrap_mode = PANGO_WRAP_CHAR;
230   priv->align = PANGO_ALIGN_LEFT;
231   priv->align_set = FALSE;
232 }
233
234 static void
235 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
236 {
237   GObjectClass *object_class = G_OBJECT_CLASS (class);
238   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
239
240   object_class->finalize = gtk_cell_renderer_text_finalize;
241   
242   object_class->get_property = gtk_cell_renderer_text_get_property;
243   object_class->set_property = gtk_cell_renderer_text_set_property;
244
245   cell_class->render = gtk_cell_renderer_text_render;
246   cell_class->start_editing = gtk_cell_renderer_text_start_editing;
247   cell_class->get_preferred_width = gtk_cell_renderer_text_get_preferred_width;
248   cell_class->get_preferred_height = gtk_cell_renderer_text_get_preferred_height;
249   cell_class->get_preferred_height_for_width = gtk_cell_renderer_text_get_preferred_height_for_width;
250   cell_class->get_aligned_area = gtk_cell_renderer_text_get_aligned_area;
251
252   g_object_class_install_property (object_class,
253                                    PROP_TEXT,
254                                    g_param_spec_string ("text",
255                                                         P_("Text"),
256                                                         P_("Text to render"),
257                                                         NULL,
258                                                         GTK_PARAM_READWRITE));
259   
260   g_object_class_install_property (object_class,
261                                    PROP_MARKUP,
262                                    g_param_spec_string ("markup",
263                                                         P_("Markup"),
264                                                         P_("Marked up text to render"),
265                                                         NULL,
266                                                         GTK_PARAM_WRITABLE));
267
268   g_object_class_install_property (object_class,
269                                    PROP_ATTRIBUTES,
270                                    g_param_spec_boxed ("attributes",
271                                                        P_("Attributes"),
272                                                        P_("A list of style attributes to apply to the text of the renderer"),
273                                                        PANGO_TYPE_ATTR_LIST,
274                                                        GTK_PARAM_READWRITE));
275
276   g_object_class_install_property (object_class,
277                                    PROP_SINGLE_PARAGRAPH_MODE,
278                                    g_param_spec_boolean ("single-paragraph-mode",
279                                                          P_("Single Paragraph Mode"),
280                                                          P_("Whether to keep all text in a single paragraph"),
281                                                          FALSE,
282                                                          GTK_PARAM_READWRITE));
283
284   
285   g_object_class_install_property (object_class,
286                                    PROP_BACKGROUND,
287                                    g_param_spec_string ("background",
288                                                         P_("Background color name"),
289                                                         P_("Background color as a string"),
290                                                         NULL,
291                                                         GTK_PARAM_WRITABLE));
292
293   /**
294    * GtkCellRendererText:background-gdk:
295    *
296    * Background color as a #GdkColor
297    *
298    * Deprecated: 3.4: Use #GtkCellRendererText:background-rgba instead.
299    */
300   g_object_class_install_property (object_class,
301                                    PROP_BACKGROUND_GDK,
302                                    g_param_spec_boxed ("background-gdk",
303                                                        P_("Background color"),
304                                                        P_("Background color as a GdkColor"),
305                                                        GDK_TYPE_COLOR,
306                                                        GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
307
308   /**
309    * GtkCellRendererText:background-rgba:
310    *
311    * Background color as a #GdkRGBA
312    *
313    * Since: 3.0
314    */
315   g_object_class_install_property (object_class,
316                                    PROP_BACKGROUND_RGBA,
317                                    g_param_spec_boxed ("background-rgba",
318                                                        P_("Background color as RGBA"),
319                                                        P_("Background color as a GdkRGBA"),
320                                                        GDK_TYPE_RGBA,
321                                                        GTK_PARAM_READWRITE));
322   g_object_class_install_property (object_class,
323                                    PROP_FOREGROUND,
324                                    g_param_spec_string ("foreground",
325                                                         P_("Foreground color name"),
326                                                         P_("Foreground color as a string"),
327                                                         NULL,
328                                                         GTK_PARAM_WRITABLE));
329
330   /**
331    * GtkCellRendererText:foreground-gdk:
332    *
333    * Foreground color as a #GdkColor
334    *
335    * Deprecated: 3.4: Use #GtkCellRendererText:foreground-rgba instead.
336    */
337   g_object_class_install_property (object_class,
338                                    PROP_FOREGROUND_GDK,
339                                    g_param_spec_boxed ("foreground-gdk",
340                                                        P_("Foreground color"),
341                                                        P_("Foreground color as a GdkColor"),
342                                                        GDK_TYPE_COLOR,
343                                                        GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
344
345   /**
346    * GtkCellRendererText:foreground-rgba:
347    *
348    * Foreground color as a #GdkRGBA
349    *
350    * Since: 3.0
351    */
352   g_object_class_install_property (object_class,
353                                    PROP_FOREGROUND_RGBA,
354                                    g_param_spec_boxed ("foreground-rgba",
355                                                        P_("Foreground color as RGBA"),
356                                                        P_("Foreground color as a GdkRGBA"),
357                                                        GDK_TYPE_RGBA,
358                                                        GTK_PARAM_READWRITE));
359
360
361   g_object_class_install_property (object_class,
362                                    PROP_EDITABLE,
363                                    g_param_spec_boolean ("editable",
364                                                          P_("Editable"),
365                                                          P_("Whether the text can be modified by the user"),
366                                                          FALSE,
367                                                          GTK_PARAM_READWRITE));
368
369   g_object_class_install_property (object_class,
370                                    PROP_FONT,
371                                    g_param_spec_string ("font",
372                                                         P_("Font"),
373                                                         P_("Font description as a string, e.g. \"Sans Italic 12\""),
374                                                         NULL,
375                                                         GTK_PARAM_READWRITE));
376
377   g_object_class_install_property (object_class,
378                                    PROP_FONT_DESC,
379                                    g_param_spec_boxed ("font-desc",
380                                                        P_("Font"),
381                                                        P_("Font description as a PangoFontDescription struct"),
382                                                        PANGO_TYPE_FONT_DESCRIPTION,
383                                                        GTK_PARAM_READWRITE));
384
385   
386   g_object_class_install_property (object_class,
387                                    PROP_FAMILY,
388                                    g_param_spec_string ("family",
389                                                         P_("Font family"),
390                                                         P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
391                                                         NULL,
392                                                         GTK_PARAM_READWRITE));
393
394   g_object_class_install_property (object_class,
395                                    PROP_STYLE,
396                                    g_param_spec_enum ("style",
397                                                       P_("Font style"),
398                                                       P_("Font style"),
399                                                       PANGO_TYPE_STYLE,
400                                                       PANGO_STYLE_NORMAL,
401                                                       GTK_PARAM_READWRITE));
402
403   g_object_class_install_property (object_class,
404                                    PROP_VARIANT,
405                                    g_param_spec_enum ("variant",
406                                                      P_("Font variant"),
407                                                      P_("Font variant"),
408                                                       PANGO_TYPE_VARIANT,
409                                                       PANGO_VARIANT_NORMAL,
410                                                       GTK_PARAM_READWRITE));
411   
412   g_object_class_install_property (object_class,
413                                    PROP_WEIGHT,
414                                    g_param_spec_int ("weight",
415                                                      P_("Font weight"),
416                                                      P_("Font weight"),
417                                                      0,
418                                                      G_MAXINT,
419                                                      PANGO_WEIGHT_NORMAL,
420                                                      GTK_PARAM_READWRITE));
421
422    g_object_class_install_property (object_class,
423                                    PROP_STRETCH,
424                                    g_param_spec_enum ("stretch",
425                                                       P_("Font stretch"),
426                                                       P_("Font stretch"),
427                                                       PANGO_TYPE_STRETCH,
428                                                       PANGO_STRETCH_NORMAL,
429                                                       GTK_PARAM_READWRITE));
430   
431   g_object_class_install_property (object_class,
432                                    PROP_SIZE,
433                                    g_param_spec_int ("size",
434                                                      P_("Font size"),
435                                                      P_("Font size"),
436                                                      0,
437                                                      G_MAXINT,
438                                                      0,
439                                                      GTK_PARAM_READWRITE));
440
441   g_object_class_install_property (object_class,
442                                    PROP_SIZE_POINTS,
443                                    g_param_spec_double ("size-points",
444                                                         P_("Font points"),
445                                                         P_("Font size in points"),
446                                                         0.0,
447                                                         G_MAXDOUBLE,
448                                                         0.0,
449                                                         GTK_PARAM_READWRITE));  
450
451   g_object_class_install_property (object_class,
452                                    PROP_SCALE,
453                                    g_param_spec_double ("scale",
454                                                         P_("Font scale"),
455                                                         P_("Font scaling factor"),
456                                                         0.0,
457                                                         G_MAXDOUBLE,
458                                                         1.0,
459                                                         GTK_PARAM_READWRITE));
460   
461   g_object_class_install_property (object_class,
462                                    PROP_RISE,
463                                    g_param_spec_int ("rise",
464                                                      P_("Rise"),
465                                                      P_("Offset of text above the baseline "
466                                                         "(below the baseline if rise is negative)"),
467                                                      -G_MAXINT,
468                                                      G_MAXINT,
469                                                      0,
470                                                      GTK_PARAM_READWRITE));
471
472
473   g_object_class_install_property (object_class,
474                                    PROP_STRIKETHROUGH,
475                                    g_param_spec_boolean ("strikethrough",
476                                                          P_("Strikethrough"),
477                                                          P_("Whether to strike through the text"),
478                                                          FALSE,
479                                                          GTK_PARAM_READWRITE));
480   
481   g_object_class_install_property (object_class,
482                                    PROP_UNDERLINE,
483                                    g_param_spec_enum ("underline",
484                                                       P_("Underline"),
485                                                       P_("Style of underline for this text"),
486                                                       PANGO_TYPE_UNDERLINE,
487                                                       PANGO_UNDERLINE_NONE,
488                                                       GTK_PARAM_READWRITE));
489
490   g_object_class_install_property (object_class,
491                                    PROP_LANGUAGE,
492                                    g_param_spec_string ("language",
493                                                         P_("Language"),
494                                                         P_("The language this text is in, as an ISO code. "
495                                                            "Pango can use this as a hint when rendering the text. "
496                                                            "If you don't understand this parameter, you probably don't need it"),
497                                                         NULL,
498                                                         GTK_PARAM_READWRITE));
499
500
501   /**
502    * GtkCellRendererText:ellipsize:
503    *
504    * Specifies the preferred place to ellipsize the string, if the cell renderer 
505    * does not have enough room to display the entire string. Setting it to 
506    * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
507    * for another way of making the text fit into a given width.
508    *
509    * Since: 2.6
510    */
511   g_object_class_install_property (object_class,
512                                    PROP_ELLIPSIZE,
513                                    g_param_spec_enum ("ellipsize",
514                                                       P_("Ellipsize"),
515                                                       P_("The preferred place to ellipsize the string, "
516                                                          "if the cell renderer does not have enough room "
517                                                          "to display the entire string"),
518                                                       PANGO_TYPE_ELLIPSIZE_MODE,
519                                                       PANGO_ELLIPSIZE_NONE,
520                                                       GTK_PARAM_READWRITE));
521
522   /**
523    * GtkCellRendererText:width-chars:
524    * 
525    * The desired width of the cell, in characters. If this property is set to
526    * -1, the width will be calculated automatically, otherwise the cell will
527    * request either 3 characters or the property value, whichever is greater.
528    * 
529    * Since: 2.6
530    **/
531   g_object_class_install_property (object_class,
532                                    PROP_WIDTH_CHARS,
533                                    g_param_spec_int ("width-chars",
534                                                      P_("Width In Characters"),
535                                                      P_("The desired width of the label, in characters"),
536                                                      -1,
537                                                      G_MAXINT,
538                                                      -1,
539                                                      GTK_PARAM_READWRITE));
540   
541
542   /**
543    * GtkCellRendererText:max-width-chars:
544    * 
545    * The desired maximum width of the cell, in characters. If this property 
546    * is set to -1, the width will be calculated automatically.
547    *
548    * For cell renderers that ellipsize or wrap text; this property
549    * controls the maximum reported width of the cell. The
550    * cell should not receive any greater allocation unless it is
551    * set to expand in its #GtkCellLayout and all of the cell's siblings
552    * have received their natural width.
553    *
554    * Since: 3.0
555    **/
556   g_object_class_install_property (object_class,
557                                    PROP_MAX_WIDTH_CHARS,
558                                    g_param_spec_int ("max-width-chars",
559                                                      P_("Maximum Width In Characters"),
560                                                      P_("The maximum width of the cell, in characters"),
561                                                      -1,
562                                                      G_MAXINT,
563                                                      -1,
564                                                      GTK_PARAM_READWRITE));
565   
566   /**
567    * GtkCellRendererText:wrap-mode:
568    *
569    * Specifies how to break the string into multiple lines, if the cell 
570    * renderer does not have enough room to display the entire string. 
571    * This property has no effect unless the wrap-width property is set.
572    *
573    * Since: 2.8
574    */
575   g_object_class_install_property (object_class,
576                                    PROP_WRAP_MODE,
577                                    g_param_spec_enum ("wrap-mode",
578                                                       P_("Wrap mode"),
579                                                       P_("How to break the string into multiple lines, "
580                                                          "if the cell renderer does not have enough room "
581                                                          "to display the entire string"),
582                                                       PANGO_TYPE_WRAP_MODE,
583                                                       PANGO_WRAP_CHAR,
584                                                       GTK_PARAM_READWRITE));
585
586   /**
587    * GtkCellRendererText:wrap-width:
588    *
589    * Specifies the minimum width at which the text is wrapped. The wrap-mode property can 
590    * be used to influence at what character positions the line breaks can be placed.
591    * Setting wrap-width to -1 turns wrapping off.
592    *
593    * Since: 2.8
594    */
595   g_object_class_install_property (object_class,
596                                    PROP_WRAP_WIDTH,
597                                    g_param_spec_int ("wrap-width",
598                                                      P_("Wrap width"),
599                                                      P_("The width at which the text is wrapped"),
600                                                      -1,
601                                                      G_MAXINT,
602                                                      -1,
603                                                      GTK_PARAM_READWRITE));
604
605   /**
606    * GtkCellRendererText:alignment:
607    *
608    * Specifies how to align the lines of text with respect to each other. 
609    *
610    * Note that this property describes how to align the lines of text in 
611    * case there are several of them. The "xalign" property of #GtkCellRenderer, 
612    * on the other hand, sets the horizontal alignment of the whole text.
613    *
614    * Since: 2.10
615    */
616   g_object_class_install_property (object_class,
617                                    PROP_ALIGN,
618                                    g_param_spec_enum ("alignment",
619                                                       P_("Alignment"),
620                                                       P_("How to align the lines"),
621                                                       PANGO_TYPE_ALIGNMENT,
622                                                       PANGO_ALIGN_LEFT,
623                                                       GTK_PARAM_READWRITE));
624
625   /**
626    * GtkCellRendererText:placeholder-text:
627    *
628    * The text that will be displayed in the #GtkCellRenderer if
629    * #GtkCellRendererText:editable is %TRUE and the cell is empty.
630    *
631    * Since 3.6
632    */
633   g_object_class_install_property (object_class,
634                                    PROP_PLACEHOLDER_TEXT,
635                                    g_param_spec_string ("placeholder-text",
636                                                         P_("Placeholder text"),
637                                                         P_("Text rendered when an editable cell is empty"),
638                                                         NULL,
639                                                         GTK_PARAM_READWRITE));
640
641
642   /* Style props are set or not */
643
644 #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))
645
646   ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
647                 P_("Background set"),
648                 P_("Whether this tag affects the background color"));
649
650   ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
651                 P_("Foreground set"),
652                 P_("Whether this tag affects the foreground color"));
653   
654   ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
655                 P_("Editability set"),
656                 P_("Whether this tag affects text editability"));
657
658   ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
659                 P_("Font family set"),
660                 P_("Whether this tag affects the font family"));  
661
662   ADD_SET_PROP ("style-set", PROP_STYLE_SET,
663                 P_("Font style set"),
664                 P_("Whether this tag affects the font style"));
665
666   ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
667                 P_("Font variant set"),
668                 P_("Whether this tag affects the font variant"));
669
670   ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
671                 P_("Font weight set"),
672                 P_("Whether this tag affects the font weight"));
673
674   ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
675                 P_("Font stretch set"),
676                 P_("Whether this tag affects the font stretch"));
677
678   ADD_SET_PROP ("size-set", PROP_SIZE_SET,
679                 P_("Font size set"),
680                 P_("Whether this tag affects the font size"));
681
682   ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
683                 P_("Font scale set"),
684                 P_("Whether this tag scales the font size by a factor"));
685   
686   ADD_SET_PROP ("rise-set", PROP_RISE_SET,
687                 P_("Rise set"),
688                 P_("Whether this tag affects the rise"));
689
690   ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
691                 P_("Strikethrough set"),
692                 P_("Whether this tag affects strikethrough"));
693
694   ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
695                 P_("Underline set"),
696                 P_("Whether this tag affects underlining"));
697
698   ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
699                 P_("Language set"),
700                 P_("Whether this tag affects the language the text is rendered as"));
701
702   ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
703                 P_("Ellipsize set"),
704                 P_("Whether this tag affects the ellipsize mode"));
705
706   ADD_SET_PROP ("align-set", PROP_ALIGN_SET,
707                 P_("Align set"),
708                 P_("Whether this tag affects the alignment mode"));
709
710   /**
711    * GtkCellRendererText::edited:
712    * @renderer: the object which received the signal
713    * @path: the path identifying the edited cell
714    * @new_text: the new text
715    *
716    * This signal is emitted after @renderer has been edited.
717    *
718    * It is the responsibility of the application to update the model
719    * and store @new_text at the position indicated by @path.
720    */
721   text_cell_renderer_signals [EDITED] =
722     g_signal_new (I_("edited"),
723                   G_OBJECT_CLASS_TYPE (object_class),
724                   G_SIGNAL_RUN_LAST,
725                   G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
726                   NULL, NULL,
727                   _gtk_marshal_VOID__STRING_STRING,
728                   G_TYPE_NONE, 2,
729                   G_TYPE_STRING,
730                   G_TYPE_STRING);
731
732   g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
733
734   gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_TEXT_CELL_ACCESSIBLE);
735 }
736
737 static void
738 gtk_cell_renderer_text_finalize (GObject *object)
739 {
740   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
741   GtkCellRendererTextPrivate *priv = celltext->priv;
742
743   pango_font_description_free (priv->font);
744
745   g_free (priv->text);
746   g_free (priv->placeholder_text);
747
748   if (priv->extra_attrs)
749     pango_attr_list_unref (priv->extra_attrs);
750
751   if (priv->language)
752     g_object_unref (priv->language);
753
754   g_clear_object (&priv->entry);
755
756   G_OBJECT_CLASS (gtk_cell_renderer_text_parent_class)->finalize (object);
757 }
758
759 static PangoFontMask
760 get_property_font_set_mask (guint prop_id)
761 {
762   switch (prop_id)
763     {
764     case PROP_FAMILY_SET:
765       return PANGO_FONT_MASK_FAMILY;
766     case PROP_STYLE_SET:
767       return PANGO_FONT_MASK_STYLE;
768     case PROP_VARIANT_SET:
769       return PANGO_FONT_MASK_VARIANT;
770     case PROP_WEIGHT_SET:
771       return PANGO_FONT_MASK_WEIGHT;
772     case PROP_STRETCH_SET:
773       return PANGO_FONT_MASK_STRETCH;
774     case PROP_SIZE_SET:
775       return PANGO_FONT_MASK_SIZE;
776     }
777
778   return 0;
779 }
780
781 static void
782 gtk_cell_renderer_text_get_property (GObject        *object,
783                                      guint           param_id,
784                                      GValue         *value,
785                                      GParamSpec     *pspec)
786 {
787   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
788   GtkCellRendererTextPrivate *priv = celltext->priv;
789
790   switch (param_id)
791     {
792     case PROP_TEXT:
793       g_value_set_string (value, priv->text);
794       break;
795
796     case PROP_ATTRIBUTES:
797       g_value_set_boxed (value, priv->extra_attrs);
798       break;
799
800     case PROP_SINGLE_PARAGRAPH_MODE:
801       g_value_set_boolean (value, priv->single_paragraph);
802       break;
803
804     case PROP_BACKGROUND_GDK:
805       {
806         GdkColor color;
807
808         color.red = (guint16) (priv->background.red * 65535);
809         color.green = (guint16) (priv->background.green * 65535);
810         color.blue = (guint16) (priv->background.blue * 65535);
811
812         g_value_set_boxed (value, &color);
813       }
814       break;
815
816     case PROP_FOREGROUND_GDK:
817       {
818         GdkColor color;
819
820         color.red = (guint16) (priv->foreground.red * 65535);
821         color.green = (guint16) (priv->foreground.green * 65535);
822         color.blue = (guint16) (priv->foreground.blue * 65535);
823
824         g_value_set_boxed (value, &color);
825       }
826       break;
827
828     case PROP_BACKGROUND_RGBA:
829       g_value_set_boxed (value, &priv->background);
830       break;
831
832     case PROP_FOREGROUND_RGBA:
833       g_value_set_boxed (value, &priv->foreground);
834       break;
835
836     case PROP_FONT:
837         g_value_take_string (value, pango_font_description_to_string (priv->font));
838       break;
839       
840     case PROP_FONT_DESC:
841       g_value_set_boxed (value, priv->font);
842       break;
843
844     case PROP_FAMILY:
845       g_value_set_string (value, pango_font_description_get_family (priv->font));
846       break;
847
848     case PROP_STYLE:
849       g_value_set_enum (value, pango_font_description_get_style (priv->font));
850       break;
851
852     case PROP_VARIANT:
853       g_value_set_enum (value, pango_font_description_get_variant (priv->font));
854       break;
855
856     case PROP_WEIGHT:
857       g_value_set_int (value, pango_font_description_get_weight (priv->font));
858       break;
859
860     case PROP_STRETCH:
861       g_value_set_enum (value, pango_font_description_get_stretch (priv->font));
862       break;
863
864     case PROP_SIZE:
865       g_value_set_int (value, pango_font_description_get_size (priv->font));
866       break;
867
868     case PROP_SIZE_POINTS:
869       g_value_set_double (value, ((double)pango_font_description_get_size (priv->font)) / (double)PANGO_SCALE);
870       break;
871
872     case PROP_SCALE:
873       g_value_set_double (value, priv->font_scale);
874       break;
875       
876     case PROP_EDITABLE:
877       g_value_set_boolean (value, priv->editable);
878       break;
879
880     case PROP_STRIKETHROUGH:
881       g_value_set_boolean (value, priv->strikethrough);
882       break;
883
884     case PROP_UNDERLINE:
885       g_value_set_enum (value, priv->underline_style);
886       break;
887
888     case PROP_RISE:
889       g_value_set_int (value, priv->rise);
890       break;  
891
892     case PROP_LANGUAGE:
893       g_value_set_static_string (value, pango_language_to_string (priv->language));
894       break;
895
896     case PROP_ELLIPSIZE:
897       g_value_set_enum (value, priv->ellipsize);
898       break;
899       
900     case PROP_WRAP_MODE:
901       g_value_set_enum (value, priv->wrap_mode);
902       break;
903
904     case PROP_WRAP_WIDTH:
905       g_value_set_int (value, priv->wrap_width);
906       break;
907       
908     case PROP_ALIGN:
909       g_value_set_enum (value, priv->align);
910       break;
911
912     case PROP_BACKGROUND_SET:
913       g_value_set_boolean (value, priv->background_set);
914       break;
915
916     case PROP_FOREGROUND_SET:
917       g_value_set_boolean (value, priv->foreground_set);
918       break;
919
920     case PROP_FAMILY_SET:
921     case PROP_STYLE_SET:
922     case PROP_VARIANT_SET:
923     case PROP_WEIGHT_SET:
924     case PROP_STRETCH_SET:
925     case PROP_SIZE_SET:
926       {
927         PangoFontMask mask = get_property_font_set_mask (param_id);
928         g_value_set_boolean (value, (pango_font_description_get_set_fields (priv->font) & mask) != 0);
929         
930         break;
931       }
932
933     case PROP_SCALE_SET:
934       g_value_set_boolean (value, priv->scale_set);
935       break;
936       
937     case PROP_EDITABLE_SET:
938       g_value_set_boolean (value, priv->editable_set);
939       break;
940
941     case PROP_STRIKETHROUGH_SET:
942       g_value_set_boolean (value, priv->strikethrough_set);
943       break;
944
945     case PROP_UNDERLINE_SET:
946       g_value_set_boolean (value, priv->underline_set);
947       break;
948
949     case  PROP_RISE_SET:
950       g_value_set_boolean (value, priv->rise_set);
951       break;
952
953     case PROP_LANGUAGE_SET:
954       g_value_set_boolean (value, priv->language_set);
955       break;
956
957     case PROP_ELLIPSIZE_SET:
958       g_value_set_boolean (value, priv->ellipsize_set);
959       break;
960
961     case PROP_ALIGN_SET:
962       g_value_set_boolean (value, priv->align_set);
963       break;
964       
965     case PROP_WIDTH_CHARS:
966       g_value_set_int (value, priv->width_chars);
967       break;  
968
969     case PROP_MAX_WIDTH_CHARS:
970       g_value_set_int (value, priv->max_width_chars);
971       break;  
972
973     case PROP_PLACEHOLDER_TEXT:
974       g_value_set_string (value, priv->placeholder_text);
975       break;
976
977     case PROP_BACKGROUND:
978     case PROP_FOREGROUND:
979     case PROP_MARKUP:
980     default:
981       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
982       break;
983     }
984 }
985
986
987 static void
988 set_bg_color (GtkCellRendererText *celltext,
989               GdkRGBA             *rgba)
990 {
991   GtkCellRendererTextPrivate *priv = celltext->priv;
992
993   if (rgba)
994     {
995       if (!priv->background_set)
996         {
997           priv->background_set = TRUE;
998           g_object_notify (G_OBJECT (celltext), "background-set");
999         }
1000
1001       priv->background = *rgba;
1002     }
1003   else
1004     {
1005       if (priv->background_set)
1006         {
1007           priv->background_set = FALSE;
1008           g_object_notify (G_OBJECT (celltext), "background-set");
1009         }
1010     }
1011 }
1012
1013
1014 static void
1015 set_fg_color (GtkCellRendererText *celltext,
1016               GdkRGBA             *rgba)
1017 {
1018   GtkCellRendererTextPrivate *priv = celltext->priv;
1019
1020   if (rgba)
1021     {
1022       if (!priv->foreground_set)
1023         {
1024           priv->foreground_set = TRUE;
1025           g_object_notify (G_OBJECT (celltext), "foreground-set");
1026         }
1027
1028       priv->foreground = *rgba;
1029     }
1030   else
1031     {
1032       if (priv->foreground_set)
1033         {
1034           priv->foreground_set = FALSE;
1035           g_object_notify (G_OBJECT (celltext), "foreground-set");
1036         }
1037     }
1038 }
1039
1040 static PangoFontMask
1041 set_font_desc_fields (PangoFontDescription *desc,
1042                       PangoFontMask         to_set)
1043 {
1044   PangoFontMask changed_mask = 0;
1045   
1046   if (to_set & PANGO_FONT_MASK_FAMILY)
1047     {
1048       const char *family = pango_font_description_get_family (desc);
1049       if (!family)
1050         {
1051           family = "sans";
1052           changed_mask |= PANGO_FONT_MASK_FAMILY;
1053         }
1054
1055       pango_font_description_set_family (desc, family);
1056     }
1057   if (to_set & PANGO_FONT_MASK_STYLE)
1058     pango_font_description_set_style (desc, pango_font_description_get_style (desc));
1059   if (to_set & PANGO_FONT_MASK_VARIANT)
1060     pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
1061   if (to_set & PANGO_FONT_MASK_WEIGHT)
1062     pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
1063   if (to_set & PANGO_FONT_MASK_STRETCH)
1064     pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
1065   if (to_set & PANGO_FONT_MASK_SIZE)
1066     {
1067       gint size = pango_font_description_get_size (desc);
1068       if (size <= 0)
1069         {
1070           size = 10 * PANGO_SCALE;
1071           changed_mask |= PANGO_FONT_MASK_SIZE;
1072         }
1073       
1074       pango_font_description_set_size (desc, size);
1075     }
1076
1077   return changed_mask;
1078 }
1079
1080 static void
1081 notify_set_changed (GObject       *object,
1082                     PangoFontMask  changed_mask)
1083 {
1084   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1085     g_object_notify (object, "family-set");
1086   if (changed_mask & PANGO_FONT_MASK_STYLE)
1087     g_object_notify (object, "style-set");
1088   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1089     g_object_notify (object, "variant-set");
1090   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1091     g_object_notify (object, "weight-set");
1092   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1093     g_object_notify (object, "stretch-set");
1094   if (changed_mask & PANGO_FONT_MASK_SIZE)
1095     g_object_notify (object, "size-set");
1096 }
1097
1098 static void
1099 notify_fields_changed (GObject       *object,
1100                        PangoFontMask  changed_mask)
1101 {
1102   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1103     g_object_notify (object, "family");
1104   if (changed_mask & PANGO_FONT_MASK_STYLE)
1105     g_object_notify (object, "style");
1106   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1107     g_object_notify (object, "variant");
1108   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1109     g_object_notify (object, "weight");
1110   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1111     g_object_notify (object, "stretch");
1112   if (changed_mask & PANGO_FONT_MASK_SIZE)
1113     g_object_notify (object, "size");
1114 }
1115
1116 static void
1117 set_font_description (GtkCellRendererText  *celltext,
1118                       PangoFontDescription *font_desc)
1119 {
1120   GtkCellRendererTextPrivate *priv = celltext->priv;
1121   GObject *object = G_OBJECT (celltext);
1122   PangoFontDescription *new_font_desc;
1123   PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
1124   
1125   if (font_desc)
1126     new_font_desc = pango_font_description_copy (font_desc);
1127   else
1128     new_font_desc = pango_font_description_new ();
1129
1130   old_mask = pango_font_description_get_set_fields (priv->font);
1131   new_mask = pango_font_description_get_set_fields (new_font_desc);
1132
1133   changed_mask = old_mask | new_mask;
1134   set_changed_mask = old_mask ^ new_mask;
1135
1136   pango_font_description_free (priv->font);
1137   priv->font = new_font_desc;
1138   
1139   g_object_freeze_notify (object);
1140
1141   g_object_notify (object, "font-desc");
1142   g_object_notify (object, "font");
1143   
1144   if (changed_mask & PANGO_FONT_MASK_FAMILY)
1145     g_object_notify (object, "family");
1146   if (changed_mask & PANGO_FONT_MASK_STYLE)
1147     g_object_notify (object, "style");
1148   if (changed_mask & PANGO_FONT_MASK_VARIANT)
1149     g_object_notify (object, "variant");
1150   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
1151     g_object_notify (object, "weight");
1152   if (changed_mask & PANGO_FONT_MASK_STRETCH)
1153     g_object_notify (object, "stretch");
1154   if (changed_mask & PANGO_FONT_MASK_SIZE)
1155     {
1156       g_object_notify (object, "size");
1157       g_object_notify (object, "size-points");
1158     }
1159
1160   notify_set_changed (object, set_changed_mask);
1161   
1162   g_object_thaw_notify (object);
1163 }
1164
1165 static void
1166 gtk_cell_renderer_text_set_property (GObject      *object,
1167                                      guint         param_id,
1168                                      const GValue *value,
1169                                      GParamSpec   *pspec)
1170 {
1171   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
1172   GtkCellRendererTextPrivate *priv = celltext->priv;
1173
1174   switch (param_id)
1175     {
1176     case PROP_TEXT:
1177       g_free (priv->text);
1178
1179       if (priv->markup_set)
1180         {
1181           if (priv->extra_attrs)
1182             pango_attr_list_unref (priv->extra_attrs);
1183           priv->extra_attrs = NULL;
1184           priv->markup_set = FALSE;
1185         }
1186
1187       priv->text = g_value_dup_string (value);
1188       g_object_notify (object, "text");
1189       break;
1190
1191     case PROP_ATTRIBUTES:
1192       if (priv->extra_attrs)
1193         pango_attr_list_unref (priv->extra_attrs);
1194
1195       priv->extra_attrs = g_value_get_boxed (value);
1196       if (priv->extra_attrs)
1197         pango_attr_list_ref (priv->extra_attrs);
1198       break;
1199     case PROP_MARKUP:
1200       {
1201         const gchar *str;
1202         gchar *text = NULL;
1203         GError *error = NULL;
1204         PangoAttrList *attrs = NULL;
1205
1206         str = g_value_get_string (value);
1207         if (str && !pango_parse_markup (str,
1208                                         -1,
1209                                         0,
1210                                         &attrs,
1211                                         &text,
1212                                         NULL,
1213                                         &error))
1214           {
1215             g_warning ("Failed to set text from markup due to error parsing markup: %s",
1216                        error->message);
1217             g_error_free (error);
1218             return;
1219           }
1220
1221         g_free (priv->text);
1222
1223         if (priv->extra_attrs)
1224           pango_attr_list_unref (priv->extra_attrs);
1225
1226         priv->text = text;
1227         priv->extra_attrs = attrs;
1228         priv->markup_set = TRUE;
1229       }
1230       break;
1231
1232     case PROP_SINGLE_PARAGRAPH_MODE:
1233       priv->single_paragraph = g_value_get_boolean (value);
1234       break;
1235       
1236     case PROP_BACKGROUND:
1237       {
1238         GdkRGBA rgba;
1239
1240         if (!g_value_get_string (value))
1241           set_bg_color (celltext, NULL);       /* reset to background_set to FALSE */
1242         else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1243           set_bg_color (celltext, &rgba);
1244         else
1245           g_warning ("Don't know color `%s'", g_value_get_string (value));
1246
1247         g_object_notify (object, "background-gdk");
1248       }
1249       break;
1250       
1251     case PROP_FOREGROUND:
1252       {
1253         GdkRGBA rgba;
1254
1255         if (!g_value_get_string (value))
1256           set_fg_color (celltext, NULL);       /* reset to foreground_set to FALSE */
1257         else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
1258           set_fg_color (celltext, &rgba);
1259         else
1260           g_warning ("Don't know color `%s'", g_value_get_string (value));
1261
1262         g_object_notify (object, "foreground-gdk");
1263       }
1264       break;
1265
1266     case PROP_BACKGROUND_GDK:
1267       {
1268         GdkColor *color;
1269
1270         color = g_value_get_boxed (value);
1271         if (color)
1272           {
1273             GdkRGBA rgba;
1274
1275             rgba.red = color->red / 65535.;
1276             rgba.green = color->green / 65535.;
1277             rgba.blue = color->blue / 65535.;
1278             rgba.alpha = 1;
1279
1280             set_bg_color (celltext, &rgba);
1281           }
1282         else
1283           {
1284             set_bg_color (celltext, NULL);
1285           }
1286       }
1287       break;
1288
1289     case PROP_FOREGROUND_GDK:
1290       {
1291         GdkColor *color;
1292
1293         color = g_value_get_boxed (value);
1294         if (color)
1295           {
1296             GdkRGBA rgba;
1297
1298             rgba.red = color->red / 65535.;
1299             rgba.green = color->green / 65535.;
1300             rgba.blue = color->blue / 65535.;
1301             rgba.alpha = 1;
1302
1303             set_fg_color (celltext, &rgba);
1304           }
1305         else
1306           {
1307             set_fg_color (celltext, NULL);
1308           }
1309       }
1310       break;
1311
1312     case PROP_BACKGROUND_RGBA:
1313       /* This notifies the GObject itself. */
1314       set_bg_color (celltext, g_value_get_boxed (value));
1315       break;
1316
1317     case PROP_FOREGROUND_RGBA:
1318       /* This notifies the GObject itself. */
1319       set_fg_color (celltext, g_value_get_boxed (value));
1320       break;
1321
1322     case PROP_FONT:
1323       {
1324         PangoFontDescription *font_desc = NULL;
1325         const gchar *name;
1326
1327         name = g_value_get_string (value);
1328
1329         if (name)
1330           font_desc = pango_font_description_from_string (name);
1331
1332         set_font_description (celltext, font_desc);
1333
1334         pango_font_description_free (font_desc);
1335         
1336         if (priv->fixed_height_rows != -1)
1337           priv->calc_fixed_height = TRUE;
1338       }
1339       break;
1340
1341     case PROP_FONT_DESC:
1342       set_font_description (celltext, g_value_get_boxed (value));
1343       
1344       if (priv->fixed_height_rows != -1)
1345         priv->calc_fixed_height = TRUE;
1346       break;
1347
1348     case PROP_FAMILY:
1349     case PROP_STYLE:
1350     case PROP_VARIANT:
1351     case PROP_WEIGHT:
1352     case PROP_STRETCH:
1353     case PROP_SIZE:
1354     case PROP_SIZE_POINTS:
1355       {
1356         PangoFontMask old_set_mask = pango_font_description_get_set_fields (priv->font);
1357         
1358         switch (param_id)
1359           {
1360           case PROP_FAMILY:
1361             pango_font_description_set_family (priv->font,
1362                                                g_value_get_string (value));
1363             break;
1364           case PROP_STYLE:
1365             pango_font_description_set_style (priv->font,
1366                                               g_value_get_enum (value));
1367             break;
1368           case PROP_VARIANT:
1369             pango_font_description_set_variant (priv->font,
1370                                                 g_value_get_enum (value));
1371             break;
1372           case PROP_WEIGHT:
1373             pango_font_description_set_weight (priv->font,
1374                                                g_value_get_int (value));
1375             break;
1376           case PROP_STRETCH:
1377             pango_font_description_set_stretch (priv->font,
1378                                                 g_value_get_enum (value));
1379             break;
1380           case PROP_SIZE:
1381             pango_font_description_set_size (priv->font,
1382                                              g_value_get_int (value));
1383             g_object_notify (object, "size-points");
1384             break;
1385           case PROP_SIZE_POINTS:
1386             pango_font_description_set_size (priv->font,
1387                                              g_value_get_double (value) * PANGO_SCALE);
1388             g_object_notify (object, "size");
1389             break;
1390           }
1391         
1392         if (priv->fixed_height_rows != -1)
1393           priv->calc_fixed_height = TRUE;
1394         
1395         notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (priv->font));
1396         g_object_notify (object, "font-desc");
1397         g_object_notify (object, "font");
1398
1399         break;
1400       }
1401       
1402     case PROP_SCALE:
1403       priv->font_scale = g_value_get_double (value);
1404       priv->scale_set = TRUE;
1405       if (priv->fixed_height_rows != -1)
1406         priv->calc_fixed_height = TRUE;
1407       g_object_notify (object, "scale-set");
1408       break;
1409       
1410     case PROP_EDITABLE:
1411       priv->editable = g_value_get_boolean (value);
1412       priv->editable_set = TRUE;
1413       if (priv->editable)
1414         g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1415       else
1416         g_object_set (celltext, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1417       g_object_notify (object, "editable-set");
1418       break;
1419
1420     case PROP_STRIKETHROUGH:
1421       priv->strikethrough = g_value_get_boolean (value);
1422       priv->strikethrough_set = TRUE;
1423       g_object_notify (object, "strikethrough-set");
1424       break;
1425
1426     case PROP_UNDERLINE:
1427       priv->underline_style = g_value_get_enum (value);
1428       priv->underline_set = TRUE;
1429       g_object_notify (object, "underline-set");
1430             
1431       break;
1432
1433     case PROP_RISE:
1434       priv->rise = g_value_get_int (value);
1435       priv->rise_set = TRUE;
1436       g_object_notify (object, "rise-set");
1437       if (priv->fixed_height_rows != -1)
1438         priv->calc_fixed_height = TRUE;
1439       break;  
1440
1441     case PROP_LANGUAGE:
1442       priv->language_set = TRUE;
1443       if (priv->language)
1444         g_object_unref (priv->language);
1445       priv->language = pango_language_from_string (g_value_get_string (value));
1446       g_object_notify (object, "language-set");
1447       break;
1448
1449     case PROP_ELLIPSIZE:
1450       priv->ellipsize = g_value_get_enum (value);
1451       priv->ellipsize_set = TRUE;
1452       g_object_notify (object, "ellipsize-set");
1453       break;
1454       
1455     case PROP_WRAP_MODE:
1456       priv->wrap_mode = g_value_get_enum (value);
1457       break;
1458       
1459     case PROP_WRAP_WIDTH:
1460       priv->wrap_width = g_value_get_int (value);
1461       break;
1462             
1463     case PROP_WIDTH_CHARS:
1464       priv->width_chars = g_value_get_int (value);
1465       break;  
1466
1467     case PROP_MAX_WIDTH_CHARS:
1468       priv->max_width_chars = g_value_get_int (value);
1469       break;  
1470
1471     case PROP_ALIGN:
1472       priv->align = g_value_get_enum (value);
1473       priv->align_set = TRUE;
1474       g_object_notify (object, "align-set");
1475       break;
1476
1477     case PROP_BACKGROUND_SET:
1478       priv->background_set = g_value_get_boolean (value);
1479       break;
1480
1481     case PROP_FOREGROUND_SET:
1482       priv->foreground_set = g_value_get_boolean (value);
1483       break;
1484
1485     case PROP_FAMILY_SET:
1486     case PROP_STYLE_SET:
1487     case PROP_VARIANT_SET:
1488     case PROP_WEIGHT_SET:
1489     case PROP_STRETCH_SET:
1490     case PROP_SIZE_SET:
1491       if (!g_value_get_boolean (value))
1492         {
1493           pango_font_description_unset_fields (priv->font,
1494                                                get_property_font_set_mask (param_id));
1495         }
1496       else
1497         {
1498           PangoFontMask changed_mask;
1499           
1500           changed_mask = set_font_desc_fields (priv->font,
1501                                                get_property_font_set_mask (param_id));
1502           notify_fields_changed (G_OBJECT (celltext), changed_mask);
1503         }
1504       break;
1505
1506     case PROP_SCALE_SET:
1507       priv->scale_set = g_value_get_boolean (value);
1508       break;
1509       
1510     case PROP_EDITABLE_SET:
1511       priv->editable_set = g_value_get_boolean (value);
1512       break;
1513
1514     case PROP_STRIKETHROUGH_SET:
1515       priv->strikethrough_set = g_value_get_boolean (value);
1516       break;
1517
1518     case PROP_UNDERLINE_SET:
1519       priv->underline_set = g_value_get_boolean (value);
1520       break;
1521
1522     case PROP_RISE_SET:
1523       priv->rise_set = g_value_get_boolean (value);
1524       break;
1525
1526     case PROP_LANGUAGE_SET:
1527       priv->language_set = g_value_get_boolean (value);
1528       break;
1529
1530     case PROP_ELLIPSIZE_SET:
1531       priv->ellipsize_set = g_value_get_boolean (value);
1532       break;
1533
1534     case PROP_ALIGN_SET:
1535       priv->align_set = g_value_get_boolean (value);
1536       break;
1537
1538     case PROP_PLACEHOLDER_TEXT:
1539       g_free (priv->placeholder_text);
1540       priv->placeholder_text = g_value_dup_string (value);
1541       break;
1542
1543     default:
1544       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1545       break;
1546     }
1547 }
1548
1549 /**
1550  * gtk_cell_renderer_text_new:
1551  * 
1552  * Creates a new #GtkCellRendererText. Adjust how text is drawn using
1553  * object properties. Object properties can be
1554  * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
1555  * you can bind a property to a value in a #GtkTreeModel. For example,
1556  * you can bind the "text" property on the cell renderer to a string
1557  * value in the model, thus rendering a different string in each row
1558  * of the #GtkTreeView
1559  * 
1560  * Return value: the new cell renderer
1561  **/
1562 GtkCellRenderer *
1563 gtk_cell_renderer_text_new (void)
1564 {
1565   return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
1566 }
1567
1568 static inline gboolean
1569 show_placeholder_text (GtkCellRendererText *celltext)
1570 {
1571   GtkCellRendererTextPrivate *priv = celltext->priv;
1572
1573   return priv->editable && priv->placeholder_text &&
1574     (!priv->text || !priv->text[0]);
1575 }
1576
1577 static void
1578 add_attr (PangoAttrList  *attr_list,
1579           PangoAttribute *attr)
1580 {
1581   attr->start_index = 0;
1582   attr->end_index = G_MAXINT;
1583   
1584   pango_attr_list_insert (attr_list, attr);
1585 }
1586
1587 static PangoLayout*
1588 get_layout (GtkCellRendererText *celltext,
1589             GtkWidget           *widget,
1590             const GdkRectangle  *cell_area,
1591             GtkCellRendererState flags)
1592 {
1593   GtkCellRendererTextPrivate *priv = celltext->priv;
1594   PangoAttrList *attr_list;
1595   PangoLayout *layout;
1596   PangoUnderline uline;
1597   gint xpad;
1598   gboolean placeholder_layout = show_placeholder_text (celltext);
1599
1600   layout = gtk_widget_create_pango_layout (widget, placeholder_layout ?
1601                                            priv->placeholder_text : priv->text);
1602
1603   gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
1604
1605   if (priv->extra_attrs)
1606     attr_list = pango_attr_list_copy (priv->extra_attrs);
1607   else
1608     attr_list = pango_attr_list_new ();
1609
1610   pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
1611
1612   if (!placeholder_layout && cell_area)
1613     {
1614       /* Add options that affect appearance but not size */
1615       
1616       /* note that background doesn't go here, since it affects
1617        * background_area not the PangoLayout area
1618        */
1619       
1620       if (priv->foreground_set
1621           && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1622         {
1623           PangoColor color;
1624
1625           color.red = (guint16) (priv->foreground.red * 65535);
1626           color.green = (guint16) (priv->foreground.green * 65535);
1627           color.blue = (guint16) (priv->foreground.blue * 65535);
1628
1629           add_attr (attr_list,
1630                     pango_attr_foreground_new (color.red, color.green, color.blue));
1631         }
1632
1633       if (priv->strikethrough_set)
1634         add_attr (attr_list,
1635                   pango_attr_strikethrough_new (priv->strikethrough));
1636     }
1637   else if (placeholder_layout)
1638     {
1639       PangoColor color;
1640       GtkStyleContext *context;
1641       GdkRGBA fg = { 0.5, 0.5, 0.5 };
1642
1643       context = gtk_widget_get_style_context (widget);
1644       gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
1645
1646       color.red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
1647       color.green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
1648       color.blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
1649
1650       add_attr (attr_list,
1651                 pango_attr_foreground_new (color.red, color.green, color.blue));
1652     }
1653
1654   add_attr (attr_list, pango_attr_font_desc_new (priv->font));
1655
1656   if (priv->scale_set &&
1657       priv->font_scale != 1.0)
1658     add_attr (attr_list, pango_attr_scale_new (priv->font_scale));
1659   
1660   if (priv->underline_set)
1661     uline = priv->underline_style;
1662   else
1663     uline = PANGO_UNDERLINE_NONE;
1664
1665   if (priv->language_set)
1666     add_attr (attr_list, pango_attr_language_new (priv->language));
1667   
1668   if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
1669     {
1670       switch (uline)
1671         {
1672         case PANGO_UNDERLINE_NONE:
1673           uline = PANGO_UNDERLINE_SINGLE;
1674           break;
1675
1676         case PANGO_UNDERLINE_SINGLE:
1677           uline = PANGO_UNDERLINE_DOUBLE;
1678           break;
1679
1680         default:
1681           break;
1682         }
1683     }
1684
1685   if (uline != PANGO_UNDERLINE_NONE)
1686     add_attr (attr_list, pango_attr_underline_new (priv->underline_style));
1687
1688   if (priv->rise_set)
1689     add_attr (attr_list, pango_attr_rise_new (priv->rise));
1690
1691   /* Now apply the attributes as they will effect the outcome
1692    * of pango_layout_get_extents() */
1693   pango_layout_set_attributes (layout, attr_list);
1694   pango_attr_list_unref (attr_list);
1695
1696   if (priv->ellipsize_set)
1697     pango_layout_set_ellipsize (layout, priv->ellipsize);
1698   else
1699     pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
1700
1701   if (priv->wrap_width != -1)
1702     {
1703       PangoRectangle rect;
1704       gint           width, text_width;
1705
1706       pango_layout_get_extents (layout, NULL, &rect);
1707       text_width = rect.width;
1708
1709       if (cell_area)
1710         width = (cell_area->width - xpad * 2) * PANGO_SCALE;
1711       else
1712         width = priv->wrap_width * PANGO_SCALE;
1713
1714       width = MIN (width, text_width);
1715
1716       pango_layout_set_width (layout, width);
1717       pango_layout_set_wrap (layout, priv->wrap_mode);
1718     }
1719   else
1720     {
1721       pango_layout_set_width (layout, -1);
1722       pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1723     }
1724
1725   if (priv->align_set)
1726     pango_layout_set_alignment (layout, priv->align);
1727   else
1728     {
1729       PangoAlignment align;
1730
1731       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1732         align = PANGO_ALIGN_RIGHT;
1733       else
1734         align = PANGO_ALIGN_LEFT;
1735
1736       pango_layout_set_alignment (layout, align);
1737     }
1738   
1739   return layout;
1740 }
1741
1742
1743 static void
1744 get_size (GtkCellRenderer    *cell,
1745           GtkWidget          *widget,
1746           const GdkRectangle *cell_area,
1747           PangoLayout        *layout,
1748           gint               *x_offset,
1749           gint               *y_offset,
1750           gint               *width,
1751           gint               *height)
1752 {
1753   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1754   GtkCellRendererTextPrivate *priv = celltext->priv;
1755   PangoRectangle rect;
1756   gint xpad, ypad;
1757   gint cell_width, cell_height;
1758
1759   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1760
1761   if (priv->calc_fixed_height)
1762     {
1763       GtkStyleContext *style_context;
1764       GtkStateFlags state;
1765       PangoContext *context;
1766       PangoFontMetrics *metrics;
1767       PangoFontDescription *font_desc;
1768       gint row_height;
1769
1770       style_context = gtk_widget_get_style_context (widget);
1771       state = gtk_widget_get_state_flags (widget);
1772
1773       gtk_style_context_get (style_context, state, "font", &font_desc, NULL);
1774       pango_font_description_merge_static (font_desc, priv->font, TRUE);
1775
1776       if (priv->scale_set)
1777         pango_font_description_set_size (font_desc,
1778                                          priv->font_scale * pango_font_description_get_size (font_desc));
1779
1780       context = gtk_widget_get_pango_context (widget);
1781
1782       metrics = pango_context_get_metrics (context,
1783                                            font_desc,
1784                                            pango_context_get_language (context));
1785       row_height = (pango_font_metrics_get_ascent (metrics) +
1786                     pango_font_metrics_get_descent (metrics));
1787       pango_font_metrics_unref (metrics);
1788
1789       pango_font_description_free (font_desc);
1790
1791       gtk_cell_renderer_get_fixed_size (cell, &cell_width, &cell_height);
1792
1793       gtk_cell_renderer_set_fixed_size (cell,
1794                                         cell_width, 2 * ypad +
1795                                         priv->fixed_height_rows * PANGO_PIXELS (row_height));
1796       
1797       if (height)
1798         {
1799           *height = cell_height;
1800           height = NULL;
1801         }
1802       priv->calc_fixed_height = FALSE;
1803       if (width == NULL)
1804         return;
1805     }
1806   
1807   if (layout)
1808     g_object_ref (layout);
1809   else
1810     layout = get_layout (celltext, widget, NULL, 0);
1811
1812   pango_layout_get_pixel_extents (layout, NULL, &rect);
1813
1814   if (cell_area)
1815     {
1816       gfloat xalign, yalign;
1817
1818       gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
1819
1820       rect.height = MIN (rect.height, cell_area->height - 2 * ypad);
1821       rect.width  = MIN (rect.width, cell_area->width - 2 * xpad);
1822
1823       if (x_offset)
1824         {
1825           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1826             *x_offset = (1.0 - xalign) * (cell_area->width - (rect.width + (2 * xpad)));
1827           else 
1828             *x_offset = xalign * (cell_area->width - (rect.width + (2 * xpad)));
1829
1830           if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->wrap_width != -1)
1831             *x_offset = MAX(*x_offset, 0);
1832         }
1833       if (y_offset)
1834         {
1835           *y_offset = yalign * (cell_area->height - (rect.height + (2 * ypad)));
1836           *y_offset = MAX (*y_offset, 0);
1837         }
1838     }
1839   else
1840     {
1841       if (x_offset) *x_offset = 0;
1842       if (y_offset) *y_offset = 0;
1843     }
1844
1845   if (height)
1846     *height = ypad * 2 + rect.height;
1847
1848   if (width)
1849     *width = xpad * 2 + rect.width;
1850
1851   g_object_unref (layout);
1852 }
1853
1854 static void
1855 gtk_cell_renderer_text_render (GtkCellRenderer      *cell,
1856                                cairo_t              *cr,
1857                                GtkWidget            *widget,
1858                                const GdkRectangle   *background_area,
1859                                const GdkRectangle   *cell_area,
1860                                GtkCellRendererState  flags)
1861
1862 {
1863   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
1864   GtkCellRendererTextPrivate *priv = celltext->priv;
1865   GtkStyleContext *context;
1866   PangoLayout *layout;
1867   gint x_offset = 0;
1868   gint y_offset = 0;
1869   gint xpad, ypad;
1870   PangoRectangle rect;
1871
1872   layout = get_layout (celltext, widget, cell_area, flags);
1873   get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, NULL, NULL);
1874   context = gtk_widget_get_style_context (widget);
1875
1876   if (priv->background_set && (flags & GTK_CELL_RENDERER_SELECTED) == 0)
1877     {
1878       gdk_cairo_rectangle (cr, background_area);
1879       gdk_cairo_set_source_rgba (cr, &priv->background);
1880       cairo_fill (cr);
1881     }
1882
1883   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
1884
1885   if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
1886     pango_layout_set_width (layout,
1887                             (cell_area->width - x_offset - 2 * xpad) * PANGO_SCALE);
1888   else if (priv->wrap_width == -1)
1889     pango_layout_set_width (layout, -1);
1890
1891   pango_layout_get_pixel_extents (layout, NULL, &rect);
1892   x_offset = x_offset - rect.x;
1893
1894   cairo_save (cr);
1895
1896   gdk_cairo_rectangle (cr, cell_area);
1897   cairo_clip (cr);
1898
1899   gtk_render_layout (context, cr,
1900                      cell_area->x + x_offset + xpad,
1901                      cell_area->y + y_offset + ypad,
1902                      layout);
1903
1904   cairo_restore (cr);
1905
1906   g_object_unref (layout);
1907 }
1908
1909 static void
1910 gtk_cell_renderer_text_editing_done (GtkCellEditable *entry,
1911                                      gpointer         data)
1912 {
1913   GtkCellRendererTextPrivate *priv;
1914   const gchar *path;
1915   const gchar *new_text;
1916   gboolean canceled;
1917
1918   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1919
1920   g_clear_object (&priv->entry);
1921
1922   if (priv->focus_out_id > 0)
1923     {
1924       g_signal_handler_disconnect (entry, priv->focus_out_id);
1925       priv->focus_out_id = 0;
1926     }
1927
1928   if (priv->populate_popup_id > 0)
1929     {
1930       g_signal_handler_disconnect (entry, priv->populate_popup_id);
1931       priv->populate_popup_id = 0;
1932     }
1933
1934   if (priv->entry_menu_popdown_timeout)
1935     {
1936       g_source_remove (priv->entry_menu_popdown_timeout);
1937       priv->entry_menu_popdown_timeout = 0;
1938     }
1939
1940   g_object_get (entry,
1941                 "editing-canceled", &canceled,
1942                 NULL);
1943   gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
1944
1945   if (canceled)
1946     return;
1947
1948   path = g_object_get_data (G_OBJECT (entry), GTK_CELL_RENDERER_TEXT_PATH);
1949   new_text = gtk_entry_get_text (GTK_ENTRY (entry));
1950
1951   g_signal_emit (data, text_cell_renderer_signals[EDITED], 0, path, new_text);
1952 }
1953
1954 static gboolean
1955 popdown_timeout (gpointer data)
1956 {
1957   GtkCellRendererTextPrivate *priv;
1958
1959   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1960
1961   priv->entry_menu_popdown_timeout = 0;
1962
1963   if (!gtk_widget_has_focus (priv->entry))
1964     gtk_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);
1965
1966   return FALSE;
1967 }
1968
1969 static void
1970 gtk_cell_renderer_text_popup_unmap (GtkMenu *menu,
1971                                     gpointer data)
1972 {
1973   GtkCellRendererTextPrivate *priv;
1974
1975   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1976
1977   priv->in_entry_menu = FALSE;
1978
1979   if (priv->entry_menu_popdown_timeout)
1980     return;
1981
1982   priv->entry_menu_popdown_timeout = gdk_threads_add_timeout (500, popdown_timeout,
1983                                                     data);
1984 }
1985
1986 static void
1987 gtk_cell_renderer_text_populate_popup (GtkEntry *entry,
1988                                        GtkMenu  *menu,
1989                                        gpointer  data)
1990 {
1991   GtkCellRendererTextPrivate *priv;
1992
1993   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
1994
1995   if (priv->entry_menu_popdown_timeout)
1996     {
1997       g_source_remove (priv->entry_menu_popdown_timeout);
1998       priv->entry_menu_popdown_timeout = 0;
1999     }
2000
2001   priv->in_entry_menu = TRUE;
2002
2003   g_signal_connect (menu, "unmap",
2004                     G_CALLBACK (gtk_cell_renderer_text_popup_unmap), data);
2005 }
2006
2007 static gboolean
2008 gtk_cell_renderer_text_focus_out_event (GtkWidget *entry,
2009                                         GdkEvent  *event,
2010                                         gpointer   data)
2011 {
2012   GtkCellRendererTextPrivate *priv;
2013
2014   priv = GTK_CELL_RENDERER_TEXT (data)->priv;
2015
2016   if (priv->in_entry_menu)
2017     return FALSE;
2018
2019   g_object_set (entry,
2020                 "editing-canceled", TRUE,
2021                 NULL);
2022   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2023   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2024
2025   /* entry needs focus-out-event */
2026   return FALSE;
2027 }
2028
2029 static GtkCellEditable *
2030 gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
2031                                       GdkEvent             *event,
2032                                       GtkWidget            *widget,
2033                                       const gchar          *path,
2034                                       const GdkRectangle   *background_area,
2035                                       const GdkRectangle   *cell_area,
2036                                       GtkCellRendererState  flags)
2037 {
2038   GtkCellRendererText *celltext;
2039   GtkCellRendererTextPrivate *priv;
2040   gfloat xalign, yalign;
2041
2042   celltext = GTK_CELL_RENDERER_TEXT (cell);
2043   priv = celltext->priv;
2044
2045   /* If the cell isn't editable we return NULL. */
2046   if (priv->editable == FALSE)
2047     return NULL;
2048
2049   gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
2050
2051   priv->entry = gtk_entry_new ();
2052   g_object_ref_sink (G_OBJECT (priv->entry));
2053
2054   gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
2055   gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);
2056
2057   if (priv->text)
2058     gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->text);
2059   g_object_set_data_full (G_OBJECT (priv->entry), I_(GTK_CELL_RENDERER_TEXT_PATH), g_strdup (path), g_free);
2060   
2061   gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
2062
2063   priv->in_entry_menu = FALSE;
2064   if (priv->entry_menu_popdown_timeout)
2065     {
2066       g_source_remove (priv->entry_menu_popdown_timeout);
2067       priv->entry_menu_popdown_timeout = 0;
2068     }
2069
2070   g_signal_connect (priv->entry,
2071                     "editing-done",
2072                     G_CALLBACK (gtk_cell_renderer_text_editing_done),
2073                     celltext);
2074   priv->focus_out_id = g_signal_connect_after (priv->entry, "focus-out-event",
2075                                                G_CALLBACK (gtk_cell_renderer_text_focus_out_event),
2076                                                celltext);
2077   priv->populate_popup_id =
2078     g_signal_connect (priv->entry, "populate-popup",
2079                       G_CALLBACK (gtk_cell_renderer_text_populate_popup),
2080                       celltext);
2081  
2082   gtk_widget_show (priv->entry);
2083
2084   return GTK_CELL_EDITABLE (priv->entry);
2085 }
2086
2087 /**
2088  * gtk_cell_renderer_text_set_fixed_height_from_font:
2089  * @renderer: A #GtkCellRendererText
2090  * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
2091  * 
2092  * Sets the height of a renderer to explicitly be determined by the "font" and
2093  * "y_pad" property set on it.  Further changes in these properties do not
2094  * affect the height, so they must be accompanied by a subsequent call to this
2095  * function.  Using this function is unflexible, and should really only be used
2096  * if calculating the size of a cell is too slow (ie, a massive number of cells
2097  * displayed).  If @number_of_rows is -1, then the fixed height is unset, and
2098  * the height is determined by the properties again.
2099  **/
2100 void
2101 gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
2102                                                    gint                 number_of_rows)
2103 {
2104   GtkCellRendererTextPrivate *priv;
2105   GtkCellRenderer *cell;
2106
2107   g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
2108   g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
2109
2110   cell = GTK_CELL_RENDERER (renderer);
2111   priv = renderer->priv;
2112
2113   if (number_of_rows == -1)
2114     {
2115       gint width, height;
2116
2117       gtk_cell_renderer_get_fixed_size (cell, &width, &height);
2118       gtk_cell_renderer_set_fixed_size (cell, width, -1);
2119     }
2120   else
2121     {
2122       priv->fixed_height_rows = number_of_rows;
2123       priv->calc_fixed_height = TRUE;
2124     }
2125 }
2126
2127 static void
2128 gtk_cell_renderer_text_get_preferred_width (GtkCellRenderer *cell,
2129                                             GtkWidget       *widget,
2130                                             gint            *minimum_size,
2131                                             gint            *natural_size)
2132 {
2133   GtkCellRendererTextPrivate *priv;
2134   GtkCellRendererText        *celltext;
2135   PangoLayout                *layout;
2136   PangoContext               *context;
2137   PangoFontMetrics           *metrics;
2138   PangoRectangle              rect;
2139   gint char_width, text_width, ellipsize_chars, xpad;
2140   gint min_width, nat_width;
2141
2142   /* "width-chars" Hard-coded minimum width:
2143    *    - minimum size should be MAX (width-chars, strlen ("..."));
2144    *    - natural size should be MAX (width-chars, strlen (label->text));
2145    *
2146    * "wrap-width" User specified natural wrap width
2147    *    - minimum size should be MAX (width-chars, 0)
2148    *    - natural size should be MIN (wrap-width, strlen (label->text))
2149    */
2150
2151   celltext = GTK_CELL_RENDERER_TEXT (cell);
2152   priv = celltext->priv;
2153
2154   gtk_cell_renderer_get_padding (cell, &xpad, NULL);
2155
2156   layout = get_layout (celltext, widget, NULL, 0);
2157
2158   /* Fetch the length of the complete unwrapped text */
2159   pango_layout_set_width (layout, -1);
2160   pango_layout_get_extents (layout, NULL, &rect);
2161   text_width = rect.width;
2162
2163   /* Fetch the average size of a charachter */
2164   context = pango_layout_get_context (layout);
2165   metrics = pango_context_get_metrics (context,
2166                                        pango_context_get_font_description (context),
2167                                        pango_context_get_language (context));
2168
2169   char_width = pango_font_metrics_get_approximate_char_width (metrics);
2170
2171   pango_font_metrics_unref (metrics);
2172   g_object_unref (layout);
2173
2174   /* enforce minimum width for ellipsized labels at ~3 chars */
2175   if (priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE)
2176     ellipsize_chars = 3;
2177   else
2178     ellipsize_chars = 0;
2179
2180   if ((priv->ellipsize_set && priv->ellipsize != PANGO_ELLIPSIZE_NONE) || priv->width_chars > 0)
2181     min_width = xpad * 2 +
2182       MIN (PANGO_PIXELS_CEIL (text_width),
2183            (PANGO_PIXELS (char_width) * MAX (priv->width_chars, ellipsize_chars)));
2184   /* If no width-chars set, minimum for wrapping text will be the wrap-width */
2185   else if (priv->wrap_width > -1)
2186     min_width = xpad * 2 + rect.x + MIN (PANGO_PIXELS_CEIL (text_width), priv->wrap_width);
2187   else
2188     min_width = xpad * 2 + rect.x + PANGO_PIXELS_CEIL (text_width);
2189
2190   if (priv->width_chars > 0)
2191     nat_width = xpad * 2 +
2192       MAX ((PANGO_PIXELS (char_width) * priv->width_chars), PANGO_PIXELS_CEIL (text_width));
2193   else
2194     nat_width = xpad * 2 + PANGO_PIXELS_CEIL (text_width);
2195
2196   nat_width = MAX (nat_width, min_width);
2197
2198   if (priv->max_width_chars > 0)
2199     {
2200       gint max_width = xpad * 2 + PANGO_PIXELS (char_width) * priv->max_width_chars;
2201       
2202       min_width = MIN (min_width, max_width);
2203       nat_width = MIN (nat_width, max_width);
2204     }
2205
2206   if (minimum_size)
2207     *minimum_size = min_width;
2208
2209   if (natural_size)
2210     *natural_size = nat_width;
2211 }
2212
2213 static void
2214 gtk_cell_renderer_text_get_preferred_height_for_width (GtkCellRenderer *cell,
2215                                                        GtkWidget       *widget,
2216                                                        gint             width,
2217                                                        gint            *minimum_height,
2218                                                        gint            *natural_height)
2219 {
2220   GtkCellRendererText *celltext;
2221   PangoLayout         *layout;
2222   gint                 text_height, xpad, ypad;
2223
2224
2225   celltext = GTK_CELL_RENDERER_TEXT (cell);
2226
2227   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
2228
2229   layout = get_layout (celltext, widget, NULL, 0);
2230
2231   pango_layout_set_width (layout, (width - xpad * 2) * PANGO_SCALE);
2232   pango_layout_get_pixel_size (layout, NULL, &text_height);
2233
2234   if (minimum_height)
2235     *minimum_height = text_height + ypad * 2;
2236
2237   if (natural_height)
2238     *natural_height = text_height + ypad * 2;
2239
2240   g_object_unref (layout);
2241 }
2242
2243 static void
2244 gtk_cell_renderer_text_get_preferred_height (GtkCellRenderer *cell,
2245                                              GtkWidget       *widget,
2246                                              gint            *minimum_size,
2247                                              gint            *natural_size)
2248 {
2249   gint min_width;
2250
2251   /* Thankfully cell renderers dont rotate, so they only have to do
2252    * height-for-width and not the opposite. Here we have only to return
2253    * the height for the base minimum width of the renderer.
2254    *
2255    * Note this code path wont be followed by GtkTreeView which is
2256    * height-for-width specifically.
2257    */
2258   gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, NULL);
2259   gtk_cell_renderer_text_get_preferred_height_for_width (cell, widget, min_width,
2260                                                          minimum_size, natural_size);
2261 }
2262
2263 static void
2264 gtk_cell_renderer_text_get_aligned_area (GtkCellRenderer       *cell,
2265                                          GtkWidget             *widget,
2266                                          GtkCellRendererState   flags,
2267                                          const GdkRectangle    *cell_area,
2268                                          GdkRectangle          *aligned_area)
2269 {
2270   GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (cell);
2271   PangoLayout *layout;
2272   gint x_offset = 0;
2273   gint y_offset = 0;
2274
2275   layout = get_layout (celltext, widget, cell_area, flags);
2276   get_size (cell, widget, cell_area, layout, &x_offset, &y_offset, 
2277             &aligned_area->width, &aligned_area->height);
2278
2279   aligned_area->x = cell_area->x + x_offset;
2280   aligned_area->y = cell_area->y + y_offset;
2281
2282   g_object_unref (layout);
2283 }