gtkcellrenderertext.c 59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* gtkcellrenderertext.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
21 22
#include <stdlib.h>
#include "gtkcellrenderertext.h"
23 24
#include "gtkeditable.h"
#include "gtkentry.h"
25
#include "gtkmarshalers.h"
26
#include "gtkintl.h"
27
#include "gtkprivate.h"
28
#include "gtktreeprivate.h"
29
#include "gtkalias.h"
30 31 32

static void gtk_cell_renderer_text_init       (GtkCellRendererText      *celltext);
static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
Havoc Pennington's avatar
Havoc Pennington committed
33
static void gtk_cell_renderer_text_finalize   (GObject                  *object);
34

35 36 37
static void gtk_cell_renderer_text_get_property  (GObject                  *object,
						  guint                     param_id,
						  GValue                   *value,
Tim Janik's avatar
Tim Janik committed
38
						  GParamSpec               *pspec);
39 40 41
static void gtk_cell_renderer_text_set_property  (GObject                  *object,
						  guint                     param_id,
						  const GValue             *value,
Tim Janik's avatar
Tim Janik committed
42
						  GParamSpec               *pspec);
43 44
static void gtk_cell_renderer_text_get_size   (GtkCellRenderer          *cell,
					       GtkWidget                *widget,
45 46 47
					       GdkRectangle             *cell_area,
					       gint                     *x_offset,
					       gint                     *y_offset,
48 49 50 51 52 53 54 55
					       gint                     *width,
					       gint                     *height);
static void gtk_cell_renderer_text_render     (GtkCellRenderer          *cell,
					       GdkWindow                *window,
					       GtkWidget                *widget,
					       GdkRectangle             *background_area,
					       GdkRectangle             *cell_area,
					       GdkRectangle             *expose_area,
56
					       GtkCellRendererState      flags);
57

58 59 60
static GtkCellEditable *gtk_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
							      GdkEvent             *event,
							      GtkWidget            *widget,
61
							      const gchar          *path,
62 63 64
							      GdkRectangle         *background_area,
							      GdkRectangle         *cell_area,
							      GtkCellRendererState  flags);
65

66 67 68 69
enum {
  EDITED,
  LAST_SIGNAL
};
Havoc Pennington's avatar
Havoc Pennington committed
70

71
enum {
Havoc Pennington's avatar
Havoc Pennington committed
72 73
  PROP_0,

74
  PROP_TEXT,
Alexander Larsson's avatar
Alexander Larsson committed
75
  PROP_MARKUP,
76
  PROP_ATTRIBUTES,
77
  PROP_SINGLE_PARAGRAPH_MODE,
78
  PROP_WIDTH_CHARS,
79
  PROP_WRAP_WIDTH,
80
  PROP_ALIGN,
Havoc Pennington's avatar
Havoc Pennington committed
81 82
  
  /* Style args */
83 84
  PROP_BACKGROUND,
  PROP_FOREGROUND,
Havoc Pennington's avatar
Havoc Pennington committed
85
  PROP_BACKGROUND_GDK,
86
  PROP_FOREGROUND_GDK,
Havoc Pennington's avatar
Havoc Pennington committed
87 88 89 90 91 92 93 94 95
  PROP_FONT,
  PROP_FONT_DESC,
  PROP_FAMILY,
  PROP_STYLE,
  PROP_VARIANT,
  PROP_WEIGHT,
  PROP_STRETCH,
  PROP_SIZE,
  PROP_SIZE_POINTS,
Havoc Pennington's avatar
Havoc Pennington committed
96
  PROP_SCALE,
Havoc Pennington's avatar
Havoc Pennington committed
97
  PROP_EDITABLE,
98 99
  PROP_STRIKETHROUGH,
  PROP_UNDERLINE,
Havoc Pennington's avatar
Havoc Pennington committed
100
  PROP_RISE,
101
  PROP_LANGUAGE,
102
  PROP_ELLIPSIZE,
103
  PROP_WRAP_MODE,
Havoc Pennington's avatar
Havoc Pennington committed
104 105 106 107 108 109 110 111 112 113
  
  /* Whether-a-style-arg-is-set args */
  PROP_BACKGROUND_SET,
  PROP_FOREGROUND_SET,
  PROP_FAMILY_SET,
  PROP_STYLE_SET,
  PROP_VARIANT_SET,
  PROP_WEIGHT_SET,
  PROP_STRETCH_SET,
  PROP_SIZE_SET,
Havoc Pennington's avatar
Havoc Pennington committed
114
  PROP_SCALE_SET,
Havoc Pennington's avatar
Havoc Pennington committed
115 116 117
  PROP_EDITABLE_SET,
  PROP_STRIKETHROUGH_SET,
  PROP_UNDERLINE_SET,
118
  PROP_RISE_SET,
119 120
  PROP_LANGUAGE_SET,
  PROP_ELLIPSIZE_SET
121 122
};

Havoc Pennington's avatar
Havoc Pennington committed
123
static gpointer parent_class;
124 125 126
static guint text_cell_renderer_signals [LAST_SIGNAL];

#define GTK_CELL_RENDERER_TEXT_PATH "gtk-cell-renderer-text-path"
Havoc Pennington's avatar
Havoc Pennington committed
127

128 129 130 131 132 133
#define GTK_CELL_RENDERER_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TEXT, GtkCellRendererTextPrivate))

typedef struct _GtkCellRendererTextPrivate GtkCellRendererTextPrivate;
struct _GtkCellRendererTextPrivate
{
  guint single_paragraph : 1;
134
  guint language_set : 1;
135
  guint markup_set : 1;
136 137
  guint ellipsize_set : 1;
  
138
  gulong focus_out_id;
139
  PangoLanguage *language;
140
  PangoEllipsizeMode ellipsize;
141
  PangoWrapMode wrap_mode;
142
  PangoAlignment align;
143
  
144 145 146
  gulong populate_popup_id;
  gulong entry_menu_popdown_timeout;
  gboolean in_entry_menu;
147 148
  
  gint width_chars;
149
  gint wrap_width;
150
  
151
  GtkWidget *entry;
152 153 154
};


Manish Singh's avatar
Manish Singh committed
155
GType
156 157
gtk_cell_renderer_text_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
158
  static GType cell_text_type = 0;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  if (!cell_text_type)
    {
      static const GTypeInfo cell_text_info =
      {
        sizeof (GtkCellRendererTextClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_cell_renderer_text_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkCellRendererText),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_cell_renderer_text_init,
      };

Manish Singh's avatar
Manish Singh committed
175
      cell_text_type =
Matthias Clasen's avatar
Matthias Clasen committed
176
	g_type_register_static (GTK_TYPE_CELL_RENDERER, I_("GtkCellRendererText"),
Manish Singh's avatar
Manish Singh committed
177
				&cell_text_info, 0);
178 179 180 181 182 183 184 185
    }

  return cell_text_type;
}

static void
gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
{
186 187 188 189
  GtkCellRendererTextPrivate *priv;

  priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (celltext);

190 191 192 193
  GTK_CELL_RENDERER (celltext)->xalign = 0.0;
  GTK_CELL_RENDERER (celltext)->yalign = 0.5;
  GTK_CELL_RENDERER (celltext)->xpad = 2;
  GTK_CELL_RENDERER (celltext)->ypad = 2;
194
  celltext->fixed_height_rows = -1;
195
  celltext->font = pango_font_description_new ();
196 197

  priv->width_chars = -1;
198
  priv->wrap_width = -1;
199
  priv->align = PANGO_ALIGN_LEFT;
200 201 202 203 204 205 206 207
}

static void
gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);

Havoc Pennington's avatar
Havoc Pennington committed
208 209 210 211
  parent_class = g_type_class_peek_parent (class);
  
  object_class->finalize = gtk_cell_renderer_text_finalize;
  
212 213
  object_class->get_property = gtk_cell_renderer_text_get_property;
  object_class->set_property = gtk_cell_renderer_text_set_property;
214 215 216

  cell_class->get_size = gtk_cell_renderer_text_get_size;
  cell_class->render = gtk_cell_renderer_text_render;
217
  cell_class->start_editing = gtk_cell_renderer_text_start_editing;
218

219
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
220 221
                                   PROP_TEXT,
                                   g_param_spec_string ("text",
222 223
                                                        P_("Text"),
                                                        P_("Text to render"),
Havoc Pennington's avatar
Havoc Pennington committed
224
                                                        NULL,
225
                                                        GTK_PARAM_READWRITE));
226
  
Alexander Larsson's avatar
Alexander Larsson committed
227 228 229
  g_object_class_install_property (object_class,
                                   PROP_MARKUP,
                                   g_param_spec_string ("markup",
230 231
                                                        P_("Markup"),
                                                        P_("Marked up text to render"),
Alexander Larsson's avatar
Alexander Larsson committed
232
                                                        NULL,
233
                                                        GTK_PARAM_WRITABLE));
234

235
  g_object_class_install_property (object_class,
236 237
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
238 239
						       P_("Attributes"),
						       P_("A list of style attributes to apply to the text of the renderer"),
240
						       PANGO_TYPE_ATTR_LIST,
241
						       GTK_PARAM_READWRITE));
242 243 244

  g_object_class_install_property (object_class,
                                   PROP_SINGLE_PARAGRAPH_MODE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
245
                                   g_param_spec_boolean ("single-paragraph-mode",
246 247
                                                         P_("Single Paragraph Mode"),
                                                         P_("Whether or not to keep all text in a single paragraph"),
248
                                                         FALSE,
249
                                                         GTK_PARAM_READWRITE));
250

Alexander Larsson's avatar
Alexander Larsson committed
251
  
252
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
253 254
                                   PROP_BACKGROUND,
                                   g_param_spec_string ("background",
255 256
                                                        P_("Background color name"),
                                                        P_("Background color as a string"),
Havoc Pennington's avatar
Havoc Pennington committed
257
                                                        NULL,
258
                                                        GTK_PARAM_WRITABLE));
Havoc Pennington's avatar
Havoc Pennington committed
259 260 261

  g_object_class_install_property (object_class,
                                   PROP_BACKGROUND_GDK,
Matthias Clasen's avatar
x  
Matthias Clasen committed
262
                                   g_param_spec_boxed ("background-gdk",
263 264
                                                       P_("Background color"),
                                                       P_("Background color as a GdkColor"),
265
                                                       GDK_TYPE_COLOR,
266
                                                       GTK_PARAM_READWRITE));  
Havoc Pennington's avatar
Havoc Pennington committed
267 268 269 270

  g_object_class_install_property (object_class,
                                   PROP_FOREGROUND,
                                   g_param_spec_string ("foreground",
271 272
                                                        P_("Foreground color name"),
                                                        P_("Foreground color as a string"),
Havoc Pennington's avatar
Havoc Pennington committed
273
                                                        NULL,
274
                                                        GTK_PARAM_WRITABLE));
Havoc Pennington's avatar
Havoc Pennington committed
275 276 277

  g_object_class_install_property (object_class,
                                   PROP_FOREGROUND_GDK,
Matthias Clasen's avatar
x  
Matthias Clasen committed
278
                                   g_param_spec_boxed ("foreground-gdk",
279 280
                                                       P_("Foreground color"),
                                                       P_("Foreground color as a GdkColor"),
281
                                                       GDK_TYPE_COLOR,
282
                                                       GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
283 284 285 286 287


  g_object_class_install_property (object_class,
                                   PROP_EDITABLE,
                                   g_param_spec_boolean ("editable",
288 289
                                                         P_("Editable"),
                                                         P_("Whether the text can be modified by the user"),
Kristian Rietveld's avatar
Kristian Rietveld committed
290
                                                         FALSE,
291
                                                         GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
292 293 294 295

  g_object_class_install_property (object_class,
                                   PROP_FONT,
                                   g_param_spec_string ("font",
296 297
                                                        P_("Font"),
                                                        P_("Font description as a string"),
Havoc Pennington's avatar
Havoc Pennington committed
298
                                                        NULL,
299
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
300

301
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
302
                                   PROP_FONT_DESC,
Matthias Clasen's avatar
x  
Matthias Clasen committed
303
                                   g_param_spec_boxed ("font-desc",
304 305
                                                       P_("Font"),
                                                       P_("Font description as a PangoFontDescription struct"),
306
                                                       PANGO_TYPE_FONT_DESCRIPTION,
307
                                                       GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
308

309 310
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
311 312
                                   PROP_FAMILY,
                                   g_param_spec_string ("family",
313 314
                                                        P_("Font family"),
                                                        P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
Havoc Pennington's avatar
Havoc Pennington committed
315
                                                        NULL,
316
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
317 318 319 320

  g_object_class_install_property (object_class,
                                   PROP_STYLE,
                                   g_param_spec_enum ("style",
321 322
                                                      P_("Font style"),
                                                      P_("Font style"),
Havoc Pennington's avatar
Havoc Pennington committed
323 324
                                                      PANGO_TYPE_STYLE,
                                                      PANGO_STYLE_NORMAL,
325
                                                      GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
326 327 328 329

  g_object_class_install_property (object_class,
                                   PROP_VARIANT,
                                   g_param_spec_enum ("variant",
330 331
                                                     P_("Font variant"),
                                                     P_("Font variant"),
Havoc Pennington's avatar
Havoc Pennington committed
332 333
                                                      PANGO_TYPE_VARIANT,
                                                      PANGO_VARIANT_NORMAL,
334
                                                      GTK_PARAM_READWRITE));
335 336
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
337
                                   PROP_WEIGHT,
CEST's avatar
-)  
CEST committed
338
                                   g_param_spec_int ("weight",
339 340
                                                     P_("Font weight"),
                                                     P_("Font weight"),
CEST's avatar
-)  
CEST committed
341 342
                                                     0,
                                                     G_MAXINT,
Havoc Pennington's avatar
Havoc Pennington committed
343
                                                     PANGO_WEIGHT_NORMAL,
344
                                                     GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
345

CEST's avatar
-)  
CEST committed
346
   g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
347 348
                                   PROP_STRETCH,
                                   g_param_spec_enum ("stretch",
349 350
                                                      P_("Font stretch"),
                                                      P_("Font stretch"),
Havoc Pennington's avatar
Havoc Pennington committed
351 352
                                                      PANGO_TYPE_STRETCH,
                                                      PANGO_STRETCH_NORMAL,
353
                                                      GTK_PARAM_READWRITE));
354 355
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
356 357
                                   PROP_SIZE,
                                   g_param_spec_int ("size",
358 359
                                                     P_("Font size"),
                                                     P_("Font size"),
Havoc Pennington's avatar
Havoc Pennington committed
360 361 362
                                                     0,
                                                     G_MAXINT,
                                                     0,
363
                                                     GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
364 365 366

  g_object_class_install_property (object_class,
                                   PROP_SIZE_POINTS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
367
                                   g_param_spec_double ("size-points",
368 369
                                                        P_("Font points"),
                                                        P_("Font size in points"),
Havoc Pennington's avatar
Havoc Pennington committed
370 371 372
                                                        0.0,
                                                        G_MAXDOUBLE,
                                                        0.0,
373
                                                        GTK_PARAM_READWRITE));  
Havoc Pennington's avatar
Havoc Pennington committed
374 375 376 377

  g_object_class_install_property (object_class,
                                   PROP_SCALE,
                                   g_param_spec_double ("scale",
378 379
                                                        P_("Font scale"),
                                                        P_("Font scaling factor"),
Havoc Pennington's avatar
Havoc Pennington committed
380 381 382
                                                        0.0,
                                                        G_MAXDOUBLE,
                                                        1.0,
383
                                                        GTK_PARAM_READWRITE));
384 385
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
386 387
                                   PROP_RISE,
                                   g_param_spec_int ("rise",
388
                                                     P_("Rise"),
389 390
                                                     P_("Offset of text above the baseline "
							"(below the baseline if rise is negative)"),
Havoc Pennington's avatar
Havoc Pennington committed
391 392 393
                                                     -G_MAXINT,
                                                     G_MAXINT,
                                                     0,
394
                                                     GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
395 396 397 398 399


  g_object_class_install_property (object_class,
                                   PROP_STRIKETHROUGH,
                                   g_param_spec_boolean ("strikethrough",
400 401
                                                         P_("Strikethrough"),
                                                         P_("Whether to strike through the text"),
Havoc Pennington's avatar
Havoc Pennington committed
402
                                                         FALSE,
403
                                                         GTK_PARAM_READWRITE));
404 405
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
406 407
                                   PROP_UNDERLINE,
                                   g_param_spec_enum ("underline",
408 409
                                                      P_("Underline"),
                                                      P_("Style of underline for this text"),
Havoc Pennington's avatar
Havoc Pennington committed
410 411
                                                      PANGO_TYPE_UNDERLINE,
                                                      PANGO_UNDERLINE_NONE,
412
                                                      GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
413

414 415 416
  g_object_class_install_property (object_class,
                                   PROP_LANGUAGE,
                                   g_param_spec_string ("language",
417
                                                        P_("Language"),
418 419 420
                                                        P_("The language this text is in, as an ISO code. "
							   "Pango can use this as a hint when rendering the text. "
							   "If you don't understand this parameter, you probably don't need it"),
421
                                                        NULL,
422
                                                        GTK_PARAM_READWRITE));
423 424


425 426 427 428 429 430 431 432 433 434
  /**
   * GtkCellRendererText:ellipsize:
   *
   * Specifies the preferred place to ellipsize the string, if the cell renderer 
   * does not have enough room to display the entire string. Setting it to 
   * %PANGO_ELLIPSIZE_NONE turns off ellipsizing. See the wrap-width property
   * for another way of making the text fit into a given width.
   *
   * Since: 2.6
   */
435 436 437 438
  g_object_class_install_property (object_class,
                                   PROP_ELLIPSIZE,
                                   g_param_spec_enum ("ellipsize",
						      P_("Ellipsize"),
439 440
						      P_("The preferred place to ellipsize the string, "
							 "if the cell renderer does not have enough room "
441
							 "to display the entire string"),
442 443
						      PANGO_TYPE_ELLIPSIZE_MODE,
						      PANGO_ELLIPSIZE_NONE,
444
						      GTK_PARAM_READWRITE));
445 446 447 448 449 450 451 452 453 454 455 456

  /**
   * GtkCellRendererText:width-chars:
   * 
   * The desired width of the cell, in characters. If this property is set to
   * -1, the width will be calculated automatically, otherwise the cell will
   * request either 3 characters or the property value, whichever is greater.
   * 
   * Since: 2.6
   **/
  g_object_class_install_property (object_class,
                                   PROP_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
457
                                   g_param_spec_int ("width-chars",
458 459 460 461 462
                                                     P_("Width In Characters"),
                                                     P_("The desired width of the label, in characters"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
463
                                                     GTK_PARAM_READWRITE));
464
  
465 466 467
  /**
   * GtkCellRendererText:wrap-mode:
   *
468 469 470
   * Specifies how to break the string into multiple lines, if the cell 
   * renderer does not have enough room to display the entire string. 
   * This property has no effect unless the wrap-width property is set.
471 472 473 474 475
   *
   * Since: 2.8
   */
  g_object_class_install_property (object_class,
                                   PROP_WRAP_MODE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
476
                                   g_param_spec_enum ("wrap-mode",
477 478 479 480 481 482
						      P_("Wrap mode"),
						      P_("How to break the string into multiple lines, "
							 "if the cell renderer does not have enough room "
							 "to display the entire string"),
						      PANGO_TYPE_WRAP_MODE,
						      PANGO_WRAP_CHAR,
483
						      GTK_PARAM_READWRITE));
484 485 486 487 488 489 490 491 492 493 494 495

  /**
   * GtkCellRendererText:wrap-width:
   *
   * Specifies the width at which the text is wrapped. The wrap-mode property can 
   * be used to influence at what character positions the line breaks can be placed.
   * Setting wrap-width to -1 turns wrapping off.
   *
   * Since: 2.8
   */
  g_object_class_install_property (object_class,
				   PROP_WRAP_WIDTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
496
				   g_param_spec_int ("wrap-width",
497 498 499 500 501
						     P_("Wrap width"),
						     P_("The width at which the text is wrapped"),
						     -1,
						     G_MAXINT,
						     -1,
502
						     GTK_PARAM_READWRITE));
503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
  /**
   * GtkCellRendererText:alignment:
   *
   * Specifies how to align the lines of text.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class,
                                   PROP_ALIGN,
                                   g_param_spec_enum ("alignment",
						      P_("Alignment"),
						      P_("How to align the lines"),
						      PANGO_TYPE_ALIGNMENT,
						      PANGO_ALIGN_LEFT,
						      GTK_PARAM_READWRITE));
519
  
Havoc Pennington's avatar
Havoc Pennington committed
520 521
  /* Style props are set or not */

522
#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))
Havoc Pennington's avatar
Havoc Pennington committed
523

Matthias Clasen's avatar
x  
Matthias Clasen committed
524
  ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
525 526
                P_("Background set"),
                P_("Whether this tag affects the background color"));
Havoc Pennington's avatar
Havoc Pennington committed
527

Matthias Clasen's avatar
x  
Matthias Clasen committed
528
  ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
529 530
                P_("Foreground set"),
                P_("Whether this tag affects the foreground color"));
Havoc Pennington's avatar
Havoc Pennington committed
531
  
Matthias Clasen's avatar
x  
Matthias Clasen committed
532
  ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
533 534
                P_("Editability set"),
                P_("Whether this tag affects text editability"));
Havoc Pennington's avatar
Havoc Pennington committed
535

Matthias Clasen's avatar
x  
Matthias Clasen committed
536
  ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
537 538
                P_("Font family set"),
                P_("Whether this tag affects the font family"));  
Havoc Pennington's avatar
Havoc Pennington committed
539

Matthias Clasen's avatar
x  
Matthias Clasen committed
540
  ADD_SET_PROP ("style-set", PROP_STYLE_SET,
541 542
                P_("Font style set"),
                P_("Whether this tag affects the font style"));
Havoc Pennington's avatar
Havoc Pennington committed
543

Matthias Clasen's avatar
x  
Matthias Clasen committed
544
  ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
545 546
                P_("Font variant set"),
                P_("Whether this tag affects the font variant"));
Havoc Pennington's avatar
Havoc Pennington committed
547

Matthias Clasen's avatar
x  
Matthias Clasen committed
548
  ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
549 550
                P_("Font weight set"),
                P_("Whether this tag affects the font weight"));
Havoc Pennington's avatar
Havoc Pennington committed
551

Matthias Clasen's avatar
x  
Matthias Clasen committed
552
  ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
553 554
                P_("Font stretch set"),
                P_("Whether this tag affects the font stretch"));
Havoc Pennington's avatar
Havoc Pennington committed
555

Matthias Clasen's avatar
x  
Matthias Clasen committed
556
  ADD_SET_PROP ("size-set", PROP_SIZE_SET,
557 558
                P_("Font size set"),
                P_("Whether this tag affects the font size"));
Havoc Pennington's avatar
Havoc Pennington committed
559

Matthias Clasen's avatar
x  
Matthias Clasen committed
560
  ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
561 562
                P_("Font scale set"),
                P_("Whether this tag scales the font size by a factor"));
Havoc Pennington's avatar
Havoc Pennington committed
563
  
Matthias Clasen's avatar
x  
Matthias Clasen committed
564
  ADD_SET_PROP ("rise-set", PROP_RISE_SET,
565 566
                P_("Rise set"),
                P_("Whether this tag affects the rise"));
Havoc Pennington's avatar
Havoc Pennington committed
567

Matthias Clasen's avatar
x  
Matthias Clasen committed
568
  ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
569 570
                P_("Strikethrough set"),
                P_("Whether this tag affects strikethrough"));
Havoc Pennington's avatar
Havoc Pennington committed
571

Matthias Clasen's avatar
x  
Matthias Clasen committed
572
  ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
573 574
                P_("Underline set"),
                P_("Whether this tag affects underlining"));
575

Matthias Clasen's avatar
x  
Matthias Clasen committed
576
  ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
577 578
                P_("Language set"),
                P_("Whether this tag affects the language the text is rendered as"));
579

Matthias Clasen's avatar
x  
Matthias Clasen committed
580
  ADD_SET_PROP ("ellipsize-set", PROP_ELLIPSIZE_SET,
581
                P_("Ellipsize set"),
582
                P_("Whether this tag affects the ellipsize mode"));
583

584
  text_cell_renderer_signals [EDITED] =
Matthias Clasen's avatar
Matthias Clasen committed
585
    g_signal_new (I_("edited"),
Manish Singh's avatar
Manish Singh committed
586 587 588 589 590 591 592 593
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkCellRendererTextClass, edited),
		  NULL, NULL,
		  _gtk_marshal_VOID__STRING_STRING,
		  G_TYPE_NONE, 2,
		  G_TYPE_STRING,
		  G_TYPE_STRING);
594

595
  g_type_class_add_private (object_class, sizeof (GtkCellRendererTextPrivate));
Havoc Pennington's avatar
Havoc Pennington committed
596 597 598 599 600 601
}

static void
gtk_cell_renderer_text_finalize (GObject *object)
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
602 603 604
  GtkCellRendererTextPrivate *priv;

  priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
Havoc Pennington's avatar
Havoc Pennington committed
605

606
  pango_font_description_free (celltext->font);
Havoc Pennington's avatar
Havoc Pennington committed
607 608 609

  if (celltext->text)
    g_free (celltext->text);
Alexander Larsson's avatar
Alexander Larsson committed
610 611 612 613

  if (celltext->extra_attrs)
    pango_attr_list_unref (celltext->extra_attrs);

614 615 616
  if (priv->language)
    g_object_unref (priv->language);

Havoc Pennington's avatar
Havoc Pennington committed
617
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
static PangoFontMask
get_property_font_set_mask (guint prop_id)
{
  switch (prop_id)
    {
    case PROP_FAMILY_SET:
      return PANGO_FONT_MASK_FAMILY;
    case PROP_STYLE_SET:
      return PANGO_FONT_MASK_STYLE;
    case PROP_VARIANT_SET:
      return PANGO_FONT_MASK_VARIANT;
    case PROP_WEIGHT_SET:
      return PANGO_FONT_MASK_WEIGHT;
    case PROP_STRETCH_SET:
      return PANGO_FONT_MASK_STRETCH;
    case PROP_SIZE_SET:
      return PANGO_FONT_MASK_SIZE;
    }

  return 0;
}

642
static void
643 644 645
gtk_cell_renderer_text_get_property (GObject        *object,
				     guint           param_id,
				     GValue         *value,
Tim Janik's avatar
Tim Janik committed
646
				     GParamSpec     *pspec)
647 648
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
649 650 651
  GtkCellRendererTextPrivate *priv;

  priv = GTK_CELL_RENDERER_TEXT_GET_PRIVATE (object);
652 653 654 655 656 657

  switch (param_id)
    {
    case PROP_TEXT:
      g_value_set_string (value, celltext->text);
      break;
658 659 660 661 662

    case PROP_ATTRIBUTES:
      g_value_set_boxed (value, celltext->extra_attrs);
      break;

663 664 665 666
    case PROP_SINGLE_PARAGRAPH_MODE:
      g_value_set_boolean (value, priv->single_paragraph);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
667 668 669 670 671 672 673 674 675 676
    case PROP_BACKGROUND_GDK:
      {
        GdkColor color;
        
        color.red = celltext->background.red;
        color.green = celltext->background.green;
        color.blue = celltext->background.blue;
        
        g_value_set_boxed (value, &color);
      }
677
      break;
Havoc Pennington's avatar
Havoc Pennington committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691

    case PROP_FOREGROUND_GDK:
      {
        GdkColor color;
        
        color.red = celltext->foreground.red;
        color.green = celltext->foreground.green;
        color.blue = celltext->foreground.blue;
        
        g_value_set_boxed (value, &color);
      }
      break;

    case PROP_FONT:
Matthias Clasen's avatar
Matthias Clasen committed
692
        g_value_take_string (value, pango_font_description_to_string (celltext->font);
693
      break;
Havoc Pennington's avatar
Havoc Pennington committed
694 695
      
    case PROP_FONT_DESC:
696
      g_value_set_boxed (value, celltext->font);
Havoc Pennington's avatar
Havoc Pennington committed
697 698 699
      break;

    case PROP_FAMILY:
700
      g_value_set_string (value, pango_font_description_get_family (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
701 702 703
      break;

    case PROP_STYLE:
704
      g_value_set_enum (value, pango_font_description_get_style (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
705 706 707
      break;

    case PROP_VARIANT:
708
      g_value_set_enum (value, pango_font_description_get_variant (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
709 710 711
      break;

    case PROP_WEIGHT:
712
      g_value_set_int (value, pango_font_description_get_weight (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
713 714 715
      break;

    case PROP_STRETCH:
716
      g_value_set_enum (value, pango_font_description_get_stretch (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
717 718 719
      break;

    case PROP_SIZE:
720
      g_value_set_int (value, pango_font_description_get_size (celltext->font));
Havoc Pennington's avatar
Havoc Pennington committed
721 722 723
      break;

    case PROP_SIZE_POINTS:
724
      g_value_set_double (value, ((double)pango_font_description_get_size (celltext->font)) / (double)PANGO_SCALE);
Havoc Pennington's avatar
Havoc Pennington committed
725 726
      break;

Havoc Pennington's avatar
Havoc Pennington committed
727 728 729 730
    case PROP_SCALE:
      g_value_set_double (value, celltext->font_scale);
      break;
      
731 732 733
    case PROP_EDITABLE:
      g_value_set_boolean (value, celltext->editable);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
734 735 736 737 738 739 740 741 742 743 744 745 746

    case PROP_STRIKETHROUGH:
      g_value_set_boolean (value, celltext->strikethrough);
      break;

    case PROP_UNDERLINE:
      g_value_set_enum (value, celltext->underline_style);
      break;

    case PROP_RISE:
      g_value_set_int (value, celltext->rise);
      break;  

747 748 749 750
    case PROP_LANGUAGE:
      g_value_set_string (value, pango_language_to_string (priv->language));
      break;

751 752 753 754
    case PROP_ELLIPSIZE:
      g_value_set_enum (value, priv->ellipsize);
      break;
      
755 756 757 758 759 760 761 762
    case PROP_WRAP_MODE:
      g_value_set_enum (value, priv->wrap_mode);
      break;

    case PROP_WRAP_WIDTH:
      g_value_set_int (value, priv->wrap_width);
      break;
      
763 764 765 766
    case PROP_ALIGN:
      g_value_set_enum (value, priv->align);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780
    case PROP_BACKGROUND_SET:
      g_value_set_boolean (value, celltext->background_set);
      break;

    case PROP_FOREGROUND_SET:
      g_value_set_boolean (value, celltext->foreground_set);
      break;

    case PROP_FAMILY_SET:
    case PROP_STYLE_SET:
    case PROP_VARIANT_SET:
    case PROP_WEIGHT_SET:
    case PROP_STRETCH_SET:
    case PROP_SIZE_SET:
781 782 783
      {
	PangoFontMask mask = get_property_font_set_mask (param_id);
	g_value_set_boolean (value, (pango_font_description_get_set_fields (celltext->font) & mask) != 0);
784 785
	
	break;
786
      }
Havoc Pennington's avatar
Havoc Pennington committed
787

Havoc Pennington's avatar
Havoc Pennington committed
788 789 790 791
    case PROP_SCALE_SET:
      g_value_set_boolean (value, celltext->scale_set);
      break;
      
Havoc Pennington's avatar
Havoc Pennington committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    case PROP_EDITABLE_SET:
      g_value_set_boolean (value, celltext->editable_set);
      break;

    case PROP_STRIKETHROUGH_SET:
      g_value_set_boolean (value, celltext->strikethrough_set);
      break;

    case PROP_UNDERLINE_SET:
      g_value_set_boolean (value, celltext->underline_set);
      break;

    case  PROP_RISE_SET:
      g_value_set_boolean (value, celltext->rise_set);
      break;
807 808 809 810

    case PROP_LANGUAGE_SET:
      g_value_set_boolean (value, priv->language_set);
      break;
811 812 813 814

    case PROP_ELLIPSIZE_SET:
      g_value_set_boolean (value, priv->ellipsize_set);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
815
      
816 817 818 819
    case PROP_WIDTH_CHARS:
      g_value_set_int (value, priv->width_chars);
      break;  

Havoc Pennington's avatar
Havoc Pennington committed
820 821
    case PROP_BACKGROUND:
    case PROP_FOREGROUND:
Alexander Larsson's avatar
Alexander Larsson committed
822
    case PROP_MARKUP:
823
    default:
824
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
825 826 827 828 829
      break;
    }
}


Havoc Pennington's avatar
Havoc Pennington committed
830 831 832 833 834 835 836 837 838
static void
set_bg_color (GtkCellRendererText *celltext,
              GdkColor            *color)
{
  if (color)
    {
      if (!celltext->background_set)
        {
          celltext->background_set = TRUE;
839
          g_object_notify (G_OBJECT (celltext), "background-set");
Havoc Pennington's avatar
Havoc Pennington committed
840 841 842 843 844 845 846 847 848 849 850
        }
      
      celltext->background.red = color->red;
      celltext->background.green = color->green;
      celltext->background.blue = color->blue;
    }
  else
    {
      if (celltext->background_set)
        {
          celltext->background_set = FALSE;
851
          g_object_notify (G_OBJECT (celltext), "background-set");
Havoc Pennington's avatar
Havoc Pennington committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865
        }
    }
}


static void
set_fg_color (GtkCellRendererText *celltext,
              GdkColor            *color)
{
  if (color)
    {
      if (!celltext->foreground_set)
        {
          celltext->foreground_set = TRUE;
866
          g_object_notify (G_OBJECT (celltext), "foreground-set");
Havoc Pennington's avatar
Havoc Pennington committed
867 868 869 870 871 872 873 874 875 876 877
        }
      
      celltext->foreground.red = color->red;
      celltext->foreground.green = color->green;
      celltext->foreground.blue = color->blue;
    }
  else
    {
      if (celltext->foreground_set)
        {
          celltext->foreground_set = FALSE;
878
          g_object_notify (G_OBJECT (celltext), "foreground-set");
Havoc Pennington's avatar
Havoc Pennington committed
879 880 881 882
        }
    }
}

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
static PangoFontMask
set_font_desc_fields (PangoFontDescription *desc,
		      PangoFontMask         to_set)
{
  PangoFontMask changed_mask = 0;
  
  if (to_set & PANGO_FONT_MASK_FAMILY)
    {
      const char *family = pango_font_description_get_family (desc);
      if (!family)
	{
	  family = "sans";
	  changed_mask |= PANGO_FONT_MASK_FAMILY;
	}

      pango_font_description_set_family (desc, family);
    }
  if (to_set & PANGO_FONT_MASK_STYLE)
    pango_font_description_set_style (desc, pango_font_description_get_style (desc));
  if (to_set & PANGO_FONT_MASK_VARIANT)
    pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
  if (to_set & PANGO_FONT_MASK_WEIGHT)
    pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
  if (to_set & PANGO_FONT_MASK_STRETCH)
    pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
  if (to_set & PANGO_FONT_MASK_SIZE)
    {
      gint size = pango_font_description_get_size (desc);
      if (size <= 0)
	{
	  size = 10 * PANGO_SCALE;
	  changed_mask |= PANGO_FONT_MASK_SIZE;
	}
      
      pango_font_description_set_size (desc, size);
    }

  return changed_mask;
}

923 924 925 926 927
static void
notify_set_changed (GObject       *object,
		    PangoFontMask  changed_mask)
{
  if (changed_mask & PANGO_FONT_MASK_FAMILY)
928
    g_object_notify (object, "family-set");
929
  if (changed_mask & PANGO_FONT_MASK_STYLE)
930
    g_object_notify (object, "style-set");
931
  if (changed_mask & PANGO_FONT_MASK_VARIANT)
932
    g_object_notify (object, "variant-set");
933
  if (changed_mask & PANGO_FONT_MASK_WEIGHT)
934
    g_object_notify (object, "weight-set");
935
  if (changed_mask & PANGO_FONT_MASK_STRETCH)
936
    g_object_notify (object, "stretch-set");
937
  if (changed_mask & PANGO_FONT_MASK_SIZE)
938
    g_object_notify (object, "size-set");
939 940