OSDN Git Service

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