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