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