1 /* gtkcellrenderertext.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
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.
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.
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.
21 #include "gtkcellrenderertext.h"
27 static void gtk_cell_renderer_text_init (GtkCellRendererText *celltext);
28 static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
30 static void gtk_cell_renderer_text_get_param (GObject *object,
34 const gchar *trailer);
35 static void gtk_cell_renderer_text_set_param (GObject *object,
39 const gchar *trailer);
40 static void gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
44 static void gtk_cell_renderer_text_render (GtkCellRenderer *cell,
47 GdkRectangle *background_area,
48 GdkRectangle *cell_area,
49 GdkRectangle *expose_area,
69 gtk_cell_renderer_text_get_type (void)
71 static GtkType cell_text_type = 0;
75 static const GTypeInfo cell_text_info =
77 sizeof (GtkCellRendererTextClass),
79 NULL, /* base_finalize */
80 (GClassInitFunc) gtk_cell_renderer_text_class_init,
81 NULL, /* class_finalize */
82 NULL, /* class_data */
83 sizeof (GtkCellRendererText),
85 (GInstanceInitFunc) gtk_cell_renderer_text_init,
88 cell_text_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText", &cell_text_info, 0);
91 return cell_text_type;
95 gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
97 celltext->attr_list = pango_attr_list_new ();
98 GTK_CELL_RENDERER (celltext)->xalign = 0.0;
99 GTK_CELL_RENDERER (celltext)->yalign = 0.5;
100 GTK_CELL_RENDERER (celltext)->xpad = 2;
101 GTK_CELL_RENDERER (celltext)->ypad = 2;
102 celltext->underline = FALSE;
106 gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
108 GObjectClass *object_class = G_OBJECT_CLASS (class);
109 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
111 object_class->get_param = gtk_cell_renderer_text_get_param;
112 object_class->set_param = gtk_cell_renderer_text_set_param;
114 cell_class->get_size = gtk_cell_renderer_text_get_size;
115 cell_class->render = gtk_cell_renderer_text_render;
117 g_object_class_install_param (object_class,
119 g_param_spec_string ("text",
121 _("The text of the renderer."),
126 g_object_class_install_param (object_class,
128 g_param_spec_string ("font",
130 _("The string of the font."),
134 g_object_class_install_param (object_class,
136 g_param_spec_string ("background",
137 _("Background Color string"),
138 _("The color for the background of the text."),
142 g_object_class_install_param (object_class,
144 g_param_spec_string ("foreground",
145 _("Foreground Color string"),
146 _("The color for the background of the text."),
150 g_object_class_install_param (object_class,
152 g_param_spec_boolean ("strikethrough",
154 _("Draw a line through the text."),
159 g_object_class_install_param (object_class,
161 g_param_spec_boolean ("underline",
163 _("Underline the text."),
168 g_object_class_install_param (object_class,
170 g_param_spec_boolean ("editable",
172 _("Make the text editable."),
177 g_object_class_install_param (object_class,
179 g_param_spec_boolean ("italic",
181 _("Make the text italic."),
185 g_object_class_install_param (object_class,
187 g_param_spec_boolean ("bold",
189 _("Make the text bold."),
195 gtk_cell_renderer_text_get_param (GObject *object,
199 const gchar *trailer)
201 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
202 PangoAttrIterator *attr_iter;
203 PangoAttribute *attr;
208 g_value_init (value, G_TYPE_STRING);
209 g_value_set_string (value, celltext->text);
211 case PROP_STRIKETHROUGH:
212 g_value_init (value, G_TYPE_BOOLEAN);
213 attr_iter = pango_attr_list_get_iterator (celltext->attr_list);
214 attr = pango_attr_iterator_get (attr_iter,
215 PANGO_ATTR_STRIKETHROUGH);
216 g_value_set_boolean (value, ((PangoAttrInt*) attr)->value);
219 g_value_init (value, G_TYPE_BOOLEAN);
220 g_value_set_boolean (value, celltext->underline);
223 g_value_init (value, G_TYPE_BOOLEAN);
224 g_value_set_boolean (value, celltext->editable);
233 gtk_cell_renderer_text_set_param (GObject *object,
237 const gchar *trailer)
239 GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);
242 PangoFontDescription *font_desc;
244 PangoAttribute *attribute;
250 g_free (celltext->text);
251 celltext->text = g_value_dup_string (value);
254 font = g_value_get_string (value);
258 font_desc = pango_font_description_from_string (font);
259 attribute = pango_attr_font_desc_new (font_desc);
260 attribute->start_index = 0;
261 attribute->end_index = G_MAXINT;
262 pango_font_description_free (font_desc);
263 pango_attr_list_change (celltext->attr_list,
267 case PROP_BACKGROUND:
268 string = g_value_get_string (value);
269 if (string && gdk_color_parse (string, &color))
271 attribute = pango_attr_background_new (color.red,
274 attribute->start_index = 0;
275 attribute->end_index = G_MAXINT;
276 pango_attr_list_change (celltext->attr_list,
280 case PROP_BACKGROUND_GDK:
282 case PROP_FOREGROUND:
283 string = g_value_get_string (value);
284 if (string && gdk_color_parse (string, &color))
286 attribute = pango_attr_foreground_new (color.red,
289 attribute->start_index = 0;
290 attribute->end_index = G_MAXINT;
291 pango_attr_list_change (celltext->attr_list,
295 case PROP_FOREGROUND_GDK:
297 case PROP_STRIKETHROUGH:
298 attribute = pango_attr_strikethrough_new (g_value_get_boolean (value));
299 attribute->start_index = 0;
300 attribute->end_index = G_MAXINT;
301 pango_attr_list_change (celltext->attr_list,
305 celltext->underline = g_value_get_boolean (value);
306 attribute = pango_attr_underline_new (celltext->underline ?
307 PANGO_UNDERLINE_SINGLE :
308 PANGO_UNDERLINE_NONE);
309 attribute->start_index = 0;
310 attribute->end_index = G_MAXINT;
311 pango_attr_list_change (celltext->attr_list,
317 attribute = pango_attr_style_new (g_value_get_boolean (value) ?
320 attribute->start_index = 0;
321 attribute->end_index = G_MAXINT;
322 pango_attr_list_change (celltext->attr_list,
326 attribute = pango_attr_weight_new (g_value_get_boolean (value) ?
328 PANGO_WEIGHT_NORMAL);
329 attribute->start_index = 0;
330 attribute->end_index = G_MAXINT;
331 pango_attr_list_change (celltext->attr_list,
340 gtk_cell_renderer_text_new (void)
342 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_text_get_type ()));
346 gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
351 GtkCellRendererText *celltext = (GtkCellRendererText *)cell;
354 PangoAttribute *attr;
355 PangoUnderline underline;
357 layout = gtk_widget_create_pango_layout (widget, celltext->text);
358 underline = celltext->underline ?
359 PANGO_UNDERLINE_DOUBLE :
360 PANGO_UNDERLINE_NONE;
362 attr = pango_attr_underline_new (underline);
363 attr->start_index = 0;
364 attr->end_index = G_MAXINT;
366 pango_attr_list_change (celltext->attr_list, attr);
367 pango_layout_set_attributes (layout, celltext->attr_list);
368 pango_layout_set_width (layout, -1);
369 pango_layout_get_pixel_extents (layout, NULL, &rect);
372 *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;
375 *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;
377 g_object_unref (G_OBJECT (layout));
381 gtk_cell_renderer_text_render (GtkCellRenderer *cell,
384 GdkRectangle *background_area,
385 GdkRectangle *cell_area,
386 GdkRectangle *expose_area,
390 GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
393 PangoAttribute *attr;
394 PangoUnderline underline;
399 GdkGC *font_gc = NULL;
401 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
402 font_gc = widget->style->fg_gc [GTK_STATE_SELECTED];
404 font_gc = widget->style->fg_gc [GTK_STATE_NORMAL];
406 layout = gtk_widget_create_pango_layout (widget, celltext->text);
408 if (celltext->underline)
409 underline = PANGO_UNDERLINE_SINGLE;
411 underline = PANGO_UNDERLINE_NONE;
413 attr = pango_attr_underline_new (underline);
414 attr->start_index = 0;
415 attr->end_index = G_MAXINT;
416 pango_attr_list_change (celltext->attr_list, attr);
417 pango_layout_set_attributes (layout, celltext->attr_list);
419 pango_layout_set_width (layout, -1);
420 pango_layout_get_pixel_extents (layout, NULL, &rect);
422 if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
423 underline = (celltext->underline) ? PANGO_UNDERLINE_DOUBLE:PANGO_UNDERLINE_SINGLE;
425 underline = (celltext->underline) ? PANGO_UNDERLINE_SINGLE:PANGO_UNDERLINE_NONE;
427 attr = pango_attr_underline_new (underline);
428 attr->start_index = 0;
429 attr->end_index = G_MAXINT;
430 pango_attr_list_change (celltext->attr_list, attr);
431 pango_layout_set_attributes (layout, celltext->attr_list);
433 gdk_gc_set_clip_rectangle (font_gc, cell_area);
435 real_xoffset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
436 real_xoffset = MAX (real_xoffset, 0) + cell->xpad;
437 real_yoffset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
438 real_yoffset = MAX (real_yoffset, 0) + cell->ypad;
440 gdk_draw_layout (window,
442 cell_area->x + real_xoffset,
443 cell_area->y + real_yoffset,
446 g_object_unref (G_OBJECT (layout));
448 gdk_gc_set_clip_rectangle (font_gc, NULL);