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