summaryrefslogtreecommitdiff
path: root/gtk/gtkcellrenderertext.c
blob: 751659fc5ca6b5f187197c20122d2753261c977a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* 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.
 */

#include <stdlib.h>
#include "gtkcellrenderertext.h"
#include "gtkintl.h"

static void gtk_cell_renderer_text_init       (GtkCellRendererText      *celltext);
static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);

static void gtk_cell_renderer_text_get_property  (GObject                  *object,
						  guint                     param_id,
						  GValue                   *value,
						  GParamSpec               *pspec,
						  const gchar              *trailer);
static void gtk_cell_renderer_text_set_property  (GObject                  *object,
						  guint                     param_id,
						  const GValue             *value,
						  GParamSpec               *pspec,
						  const gchar              *trailer);
static void gtk_cell_renderer_text_get_size   (GtkCellRenderer          *cell,
					       GtkWidget                *widget,
					       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,
					       guint                     flags);


enum {
  PROP_ZERO,
  PROP_TEXT,
  PROP_FONT,
  PROP_BACKGROUND,
  PROP_BACKGROUND_GDK,
  PROP_FOREGROUND,
  PROP_FOREGROUND_GDK,
  PROP_STRIKETHROUGH,
  PROP_UNDERLINE,
  PROP_EDITABLE,
  PROP_ITALIC,
  PROP_BOLD
};

GtkType
gtk_cell_renderer_text_get_type (void)
{
  static GtkType cell_text_type = 0;

  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,
      };

      cell_text_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText", &cell_text_info, 0);
    }

  return cell_text_type;
}

static void
gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
{
  celltext->attr_list = pango_attr_list_new ();
  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;
  celltext->underline = FALSE;
}

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);

  object_class->get_property = gtk_cell_renderer_text_get_property;
  object_class->set_property = gtk_cell_renderer_text_set_property;

  cell_class->get_size = gtk_cell_renderer_text_get_size;
  cell_class->render = gtk_cell_renderer_text_render;
  
  g_object_class_install_property (object_class,
				   PROP_TEXT,
				   g_param_spec_string ("text",
							_("Text String"),
							_("The text of the renderer."),
							"",
							G_PARAM_READABLE |
							G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_FONT,
				   g_param_spec_string ("font",
							_("Font String"),
							_("The string of the font."),
							"",
							G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_BACKGROUND,
				   g_param_spec_string ("background",
							_("Background Color string"),
							_("The color for the background of the text."),
							"white",
							G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_FOREGROUND,
				   g_param_spec_string ("foreground",
							_("Foreground Color string"),
							_("The color for the background of the text."),
							"black",
							G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_STRIKETHROUGH,
				   g_param_spec_boolean ("strikethrough",
							 _("Strikethrough"),
							 _("Draw a line through the text."),
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_UNDERLINE,
				   g_param_spec_boolean ("underline",
							 _("Underline"),
							 _("Underline the text."),
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_EDITABLE,
				   g_param_spec_boolean ("editable",
							 _("Editable"),
							 _("Make the text editable."),
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_ITALIC,
				   g_param_spec_boolean ("italic",
							 _("Italic"),
							 _("Make the text italic."),
							 FALSE,
							 G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
				   PROP_BOLD,
				   g_param_spec_boolean ("bold",
							 _("Bold"),
							 _("Make the text bold."),
							 FALSE,
							 G_PARAM_WRITABLE));
}

static void
gtk_cell_renderer_text_get_property (GObject        *object,
				     guint           param_id,
				     GValue         *value,
				     GParamSpec     *pspec,
				     const gchar    *trailer)
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
  PangoAttrIterator *attr_iter;
  PangoAttribute *attr;

  switch (param_id)
    {
    case PROP_TEXT:
      g_value_set_string (value, celltext->text);
      break;
    case PROP_STRIKETHROUGH:
      attr_iter = pango_attr_list_get_iterator (celltext->attr_list);
      attr = pango_attr_iterator_get (attr_iter,
				      PANGO_ATTR_STRIKETHROUGH);
      g_value_set_boolean (value, ((PangoAttrInt*) attr)->value);
      break;
    case PROP_UNDERLINE:
      g_value_set_boolean (value, celltext->underline);
      break;
    case PROP_EDITABLE:
      g_value_set_boolean (value, celltext->editable);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}


static void
gtk_cell_renderer_text_set_property (GObject      *object,
				     guint         param_id,
				     const GValue *value,
				     GParamSpec   *pspec,
				     const gchar  *trailer)
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);

  GdkColor color;
  PangoFontDescription *font_desc;
  gchar *string;
  PangoAttribute *attribute;
  gchar *font;

  switch (param_id)
    {
    case PROP_TEXT:
      g_free (celltext->text);
      celltext->text = g_value_dup_string (value);
      break;
    case PROP_FONT:
      font = g_value_get_string (value);

      if (font)
	{
	  font_desc = pango_font_description_from_string (font);
	  attribute = pango_attr_font_desc_new (font_desc);
	  attribute->start_index = 0;
	  attribute->end_index = G_MAXINT;
	  pango_font_description_free (font_desc);
	  pango_attr_list_change (celltext->attr_list,
				  attribute);
	}
      break;
    case PROP_BACKGROUND:
      string = g_value_get_string (value);
      if (string && gdk_color_parse (string, &color))
	{
	  attribute = pango_attr_background_new (color.red,
						 color.green,
						 color.blue);
	  attribute->start_index = 0;
	  attribute->end_index = G_MAXINT;
	  pango_attr_list_change (celltext->attr_list,
				  attribute);
	}
      break;
    case PROP_BACKGROUND_GDK:
      break;
    case PROP_FOREGROUND:
      string = g_value_get_string (value);
      if (string && gdk_color_parse (string, &color))
	{
	  attribute = pango_attr_foreground_new (color.red,
						 color.green,
						 color.blue);
	  attribute->start_index = 0;
	  attribute->end_index = G_MAXINT;
	  pango_attr_list_change (celltext->attr_list,
				  attribute);
	}
      break;
    case PROP_FOREGROUND_GDK:
      break;
    case PROP_STRIKETHROUGH:
      attribute = pango_attr_strikethrough_new (g_value_get_boolean (value));
      attribute->start_index = 0;
      attribute->end_index = G_MAXINT;
      pango_attr_list_change (celltext->attr_list,
			      attribute);
      break;
    case PROP_UNDERLINE:
      celltext->underline = g_value_get_boolean (value);
      attribute = pango_attr_underline_new (celltext->underline ?
					    PANGO_UNDERLINE_SINGLE :
					    PANGO_UNDERLINE_NONE);
      attribute->start_index = 0;
      attribute->end_index = G_MAXINT;
      pango_attr_list_change (celltext->attr_list,
			      attribute);
      break;
    case PROP_EDITABLE:
      break;
    case PROP_ITALIC:
      attribute = pango_attr_style_new (g_value_get_boolean (value) ?
					PANGO_STYLE_ITALIC :
					PANGO_STYLE_NORMAL);
      attribute->start_index = 0;
      attribute->end_index = G_MAXINT;
      pango_attr_list_change (celltext->attr_list,
			      attribute);
      break;
    case PROP_BOLD:
      attribute = pango_attr_weight_new (g_value_get_boolean (value) ?
					 PANGO_WEIGHT_BOLD :
					 PANGO_WEIGHT_NORMAL);
      attribute->start_index = 0;
      attribute->end_index = G_MAXINT;
      pango_attr_list_change (celltext->attr_list,
			      attribute);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}

/**
 * gtk_cell_renderer_text_new:
 * 
 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
 * object properties. Object properties can be
 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
 * you can bind a property to a value in a #GtkTreeModel. For example,
 * you can bind the "text" property on the cell renderer to a string
 * value in the model, thus rendering a different string in each row
 * of the #GtkTreeView
 * 
 * Return value: the new cell renderer
 **/
GtkCellRenderer *
gtk_cell_renderer_text_new (void)
{
  return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_text_get_type ()));
}

static void
gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
				 GtkWidget       *widget,
				 gint            *width,
				 gint            *height)
{
  GtkCellRendererText *celltext = (GtkCellRendererText *)cell;
  PangoRectangle rect;
  PangoLayout *layout;
  PangoAttribute *attr;
  PangoUnderline underline;

  layout = gtk_widget_create_pango_layout (widget, celltext->text);
  underline = celltext->underline ?
    PANGO_UNDERLINE_DOUBLE :
    PANGO_UNDERLINE_NONE;

  attr = pango_attr_underline_new (underline);
  attr->start_index = 0;
  attr->end_index = G_MAXINT;

  pango_attr_list_change (celltext->attr_list, attr);
  pango_layout_set_attributes (layout, celltext->attr_list);
  pango_layout_set_width (layout, -1);
  pango_layout_get_pixel_extents (layout, NULL, &rect);

  if (width)
    *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;

  if (height)
    *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;

  g_object_unref (G_OBJECT (layout));
}

static void
gtk_cell_renderer_text_render (GtkCellRenderer    *cell,
			       GdkWindow          *window,
			       GtkWidget          *widget,
			       GdkRectangle       *background_area,
			       GdkRectangle       *cell_area,
			       GdkRectangle       *expose_area,
			       guint               flags)

{
  GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
  PangoRectangle rect;
  PangoLayout *layout;
  PangoAttribute *attr;
  PangoUnderline underline;
  GtkStateType state;
  
  gint real_xoffset;
  gint real_yoffset;

  layout = gtk_widget_create_pango_layout (widget, celltext->text);

  if (celltext->underline)
    underline = PANGO_UNDERLINE_SINGLE;
  else
    underline = PANGO_UNDERLINE_NONE;

  attr = pango_attr_underline_new (underline);
  attr->start_index = 0;
  attr->end_index = G_MAXINT;
  pango_attr_list_change (celltext->attr_list, attr);
  pango_layout_set_attributes (layout, celltext->attr_list);

  pango_layout_set_width (layout, -1);
  pango_layout_get_pixel_extents (layout, NULL, &rect);

  if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
    underline = (celltext->underline) ? PANGO_UNDERLINE_DOUBLE:PANGO_UNDERLINE_SINGLE;
  else
    underline = (celltext->underline) ? PANGO_UNDERLINE_SINGLE:PANGO_UNDERLINE_NONE;

  attr = pango_attr_underline_new (underline);
  attr->start_index = 0;
  attr->end_index = G_MAXINT;
  pango_attr_list_change (celltext->attr_list, attr);
  pango_layout_set_attributes (layout, celltext->attr_list);

  real_xoffset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
  real_xoffset = MAX (real_xoffset, 0) + cell->xpad;
  real_yoffset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
  real_yoffset = MAX (real_yoffset, 0) + cell->ypad;

  if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
    state = GTK_STATE_SELECTED;
  else
    state = GTK_STATE_NORMAL;
  
  gtk_paint_layout (widget->style,
                    window,
                    state,
                    cell_area,
                    widget,
                    "cellrenderertext",
                    cell_area->x + real_xoffset,
                    cell_area->y + real_yoffset,
                    layout);

  g_object_unref (G_OBJECT (layout));
}