1 /* cellrenderericon.c, based on:
3 * gtkcellrendererpixbuf.c
4 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "cellrenderericon.h"
27 #define FIXED_ICON_SIZE_MAX 512
29 #define TOGGLE_WIDTH 13
30 #define TOGGLE_SPACING 18
33 static void gqv_cell_renderer_icon_get_property(GObject *object,
37 static void gqv_cell_renderer_icon_set_property(GObject *object,
41 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *celltext);
42 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class);
43 static void gqv_cell_renderer_icon_finalize(GObject *object);
44 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
46 GdkRectangle *rectangle,
51 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
54 GdkRectangle *background_area,
55 GdkRectangle *cell_area,
56 GdkRectangle *expose_area,
57 GtkCellRendererState flags);
78 static gpointer parent_class;
81 gqv_cell_renderer_icon_get_type(void)
83 static GType cell_icon_type = 0;
87 static const GTypeInfo cell_icon_info =
89 sizeof(GQvCellRendererIconClass), /* class_size */
91 NULL, /* base_finalize */
92 (GClassInitFunc) gqv_cell_renderer_icon_class_init, /* class_init */
93 NULL, /* class_finalize */
94 NULL, /* class_data */
95 sizeof(GQvCellRendererIcon), /* instance_size */
97 (GInstanceInitFunc) gqv_cell_renderer_icon_init, /* instance_init */
98 NULL, /* value_table */
101 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
102 "GQvCellRendererIcon",
106 return cell_icon_type;
110 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
112 GTK_CELL_RENDERER(cellicon)->xpad = 2;
113 GTK_CELL_RENDERER(cellicon)->ypad = 2;
117 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class)
119 GObjectClass *object_class = G_OBJECT_CLASS(class);
120 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
122 parent_class = g_type_class_peek_parent(class);
124 object_class->finalize = gqv_cell_renderer_icon_finalize;
126 object_class->get_property = gqv_cell_renderer_icon_get_property;
127 object_class->set_property = gqv_cell_renderer_icon_set_property;
129 cell_class->get_size = gqv_cell_renderer_icon_get_size;
130 cell_class->render = gqv_cell_renderer_icon_render;
132 g_object_class_install_property(object_class,
134 g_param_spec_object("pixbuf",
136 _("The pixbuf to render"),
140 g_object_class_install_property(object_class,
142 g_param_spec_string("text",
148 g_object_class_install_property(object_class,
150 g_param_spec_boxed("background_gdk",
151 _("Background color"),
152 _("Background color as a GdkColor"),
156 g_object_class_install_property(object_class,
158 g_param_spec_boxed("foreground_gdk",
159 _("Foreground color"),
160 _("Foreground color as a GdkColor"),
164 g_object_class_install_property(object_class,
166 g_param_spec_boolean("has_focus",
168 _("Draw focus indicator"),
172 g_object_class_install_property(object_class,
174 g_param_spec_int("fixed_width",
177 -1, FIXED_ICON_SIZE_MAX,
181 g_object_class_install_property(object_class,
183 g_param_spec_int("fixed_height",
185 _("Height of icon excluding text"),
186 -1, FIXED_ICON_SIZE_MAX,
190 g_object_class_install_property(object_class,
192 g_param_spec_boolean("background_set",
194 _("Whether this tag affects the background color"),
198 g_object_class_install_property(object_class,
200 g_param_spec_boolean("foreground_set",
202 _("Whether this tag affects the foreground color"),
206 g_object_class_install_property(object_class,
208 g_param_spec_boolean("show_text",
210 _("Whether the text is displayed"),
214 g_object_class_install_property(object_class,
216 g_param_spec_boolean("show_marks",
218 _("Whether the marks are displayed"),
222 g_object_class_install_property(object_class,
224 g_param_spec_int("num_marks",
225 _("Number of marks"),
226 _("Number of marks"),
231 g_object_class_install_property(object_class,
233 g_param_spec_uint("marks",
235 _("Marks bit array"),
242 gqv_cell_renderer_icon_finalize(GObject *object)
244 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
246 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
248 g_free(cellicon->text);
250 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
254 gqv_cell_renderer_icon_get_property(GObject *object,
259 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
264 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : NULL);
267 g_value_set_string(value, cellicon->text);
269 case PROP_BACKGROUND_GDK:
273 color.red = cellicon->background.red;
274 color.green = cellicon->background.green;
275 color.blue = cellicon->background.blue;
277 g_value_set_boxed(value, &color);
280 case PROP_FOREGROUND_GDK:
284 color.red = cellicon->foreground.red;
285 color.green = cellicon->foreground.green;
286 color.blue = cellicon->foreground.blue;
288 g_value_set_boxed(value, &color);
292 g_value_set_boolean(value, cellicon->focused);
294 case PROP_FIXED_WIDTH:
295 g_value_set_int(value, cellicon->fixed_width);
297 case PROP_FIXED_HEIGHT:
298 g_value_set_int(value, cellicon->fixed_height);
300 case PROP_BACKGROUND_SET:
301 g_value_set_boolean(value, cellicon->background_set);
303 case PROP_FOREGROUND_SET:
304 g_value_set_boolean(value, cellicon->foreground_set);
307 g_value_set_boolean(value, cellicon->show_text);
309 case PROP_SHOW_MARKS:
310 g_value_set_boolean(value, cellicon->show_marks);
313 g_value_set_int(value, cellicon->num_marks);
316 g_value_set_uint(value, cellicon->marks);
319 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
325 set_bg_color(GQvCellRendererIcon *cellicon,
330 if (!cellicon->background_set)
332 cellicon->background_set = TRUE;
333 g_object_notify(G_OBJECT(cellicon), "background_set");
336 cellicon->background.red = color->red;
337 cellicon->background.green = color->green;
338 cellicon->background.blue = color->blue;
342 if (cellicon->background_set)
344 cellicon->background_set = FALSE;
345 g_object_notify(G_OBJECT(cellicon), "background_set");
350 static void set_fg_color(GQvCellRendererIcon *cellicon,
355 if (!cellicon->foreground_set)
357 cellicon->foreground_set = TRUE;
358 g_object_notify(G_OBJECT(cellicon), "foreground_set");
361 cellicon->foreground.red = color->red;
362 cellicon->foreground.green = color->green;
363 cellicon->foreground.blue = color->blue;
367 if (cellicon->foreground_set)
369 cellicon->foreground_set = FALSE;
370 g_object_notify(G_OBJECT(cellicon), "foreground_set");
376 gqv_cell_renderer_icon_set_property(GObject *object,
381 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
389 pixbuf = (GdkPixbuf*) g_value_get_object(value);
390 if (pixbuf) g_object_ref(pixbuf);
391 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
392 cellicon->pixbuf = pixbuf;
399 text = cellicon->text;
400 cellicon->text = g_strdup(g_value_get_string(value));
403 g_object_notify(object, "text");
406 case PROP_BACKGROUND_GDK:
407 set_bg_color(cellicon, g_value_get_boxed(value));
409 case PROP_FOREGROUND_GDK:
410 set_fg_color(cellicon, g_value_get_boxed(value));
413 cellicon->focused = g_value_get_boolean(value);
415 case PROP_FIXED_WIDTH:
416 cellicon->fixed_width = g_value_get_int(value);
418 case PROP_FIXED_HEIGHT:
419 cellicon->fixed_height = g_value_get_int(value);
421 case PROP_BACKGROUND_SET:
422 cellicon->background_set = g_value_get_boolean(value);
424 case PROP_FOREGROUND_SET:
425 cellicon->foreground_set = g_value_get_boolean(value);
428 cellicon->show_text = g_value_get_boolean(value);
430 case PROP_SHOW_MARKS:
431 cellicon->show_marks = g_value_get_boolean(value);
434 cellicon->num_marks = g_value_get_int(value);
437 cellicon->marks = g_value_get_uint(value);
440 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
446 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
451 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
453 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
454 pango_layout_set_width(layout, width);
455 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
456 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
460 PangoAttrList *attr_list;
462 attr_list = pango_attr_list_new();
464 if (cellicon->foreground_set)
467 PangoAttribute *attr;
469 color = cellicon->foreground;
471 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
473 attr->start_index = 0;
474 attr->end_index = G_MAXINT;
475 pango_attr_list_insert(attr_list, attr);
478 pango_layout_set_attributes(layout, attr_list);
479 pango_attr_list_unref(attr_list);
486 * gqv_cell_renderer_icon_new:
488 * Creates a new #GQvCellRendererIcon. Adjust rendering
489 * parameters using object properties. Object properties can be set
490 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
491 * can bind a property to a value in a #GtkTreeModel. For example, you
492 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
493 * in the model, thus rendering a different image in each row of the
496 * Return value: the new cell renderer
499 gqv_cell_renderer_icon_new(void)
501 return g_object_new(GQV_TYPE_CELL_RENDERER_ICON, NULL);
505 gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
507 GdkRectangle *cell_area,
513 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
517 if (cellicon->fixed_width > 0)
519 calc_width = cellicon->fixed_width;
523 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
526 if (cellicon->fixed_height > 0)
528 calc_height = cellicon->fixed_height;
532 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
535 if (cellicon->show_text && cellicon->text)
540 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
541 pango_layout_get_pixel_extents(layout, NULL, &rect);
542 g_object_unref(layout);
544 calc_width = MAX(calc_width, rect.width);
545 calc_height += rect.height;
548 if (cellicon->show_marks)
550 calc_height += TOGGLE_SPACING;
551 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
554 calc_width += (gint)cell->xpad * 2;
555 calc_height += (gint)cell->ypad * 2;
557 if (x_offset) *x_offset = 0;
558 if (y_offset) *y_offset = 0;
560 if (cell_area && calc_width > 0 && calc_height > 0)
564 *x_offset = (cell->xalign * (cell_area->width - calc_width - 2 * cell->xpad));
565 *x_offset = MAX(*x_offset, 0) + cell->xpad;
569 *y_offset = (cell->yalign * (cell_area->height - calc_height - 2 * cell->ypad));
570 *y_offset = MAX(*y_offset, 0) + cell->ypad;
574 if (width) *width = calc_width;
575 if (height) *height = calc_height;
579 gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
582 GdkRectangle *background_area,
583 GdkRectangle *cell_area,
584 GdkRectangle *expose_area,
585 GtkCellRendererState flags)
588 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
591 GdkRectangle cell_rect;
594 pixbuf = cellicon->pixbuf;
595 text = cellicon->text;
597 if (!pixbuf && !text) return;
599 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
600 &cell_rect.x, &cell_rect.y,
601 &cell_rect.width, &cell_rect.height);
603 cell_rect.x += cell->xpad;
604 cell_rect.y += cell->ypad;
605 cell_rect.width -= cell->xpad * 2;
606 cell_rect.height -= cell->ypad * 2;
608 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
610 if (GTK_WIDGET_HAS_FOCUS(widget))
611 state = GTK_STATE_SELECTED;
613 state = GTK_STATE_ACTIVE;
617 if (GTK_WIDGET_STATE(widget) == GTK_STATE_INSENSITIVE)
618 state = GTK_STATE_INSENSITIVE;
620 state = GTK_STATE_NORMAL;
625 GdkRectangle pix_rect;
626 GdkRectangle draw_rect;
628 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
629 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
631 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
633 if (cellicon->fixed_height > 0)
635 pix_rect.y = cell_area->y + cell->ypad + (cellicon->fixed_height - pix_rect.height) / 2;
639 pix_rect.y = cell_area->y + cell_rect.y;
642 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
643 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
645 gdk_draw_pixbuf(window,
646 widget->style->black_gc,
648 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
649 draw_rect.x - pix_rect.x,
650 draw_rect.y - pix_rect.y,
655 GDK_RGB_DITHER_NORMAL,
660 if (cellicon->show_text && text)
663 PangoRectangle text_rect;
664 GdkRectangle pix_rect;
665 GdkRectangle draw_rect;
667 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
668 pango_layout_get_pixel_extents(layout, NULL, &text_rect);
670 pix_rect.width = text_rect.width;
671 pix_rect.height = text_rect.height;
672 pix_rect.x = cell_area->x + cell->xpad + (cell_rect.width - text_rect.width + 1) / 2;
673 pix_rect.y = cell_area->y + cell->ypad + (cell_rect.height - text_rect.height);
675 if (cellicon->show_marks)
677 pix_rect.y -= TOGGLE_SPACING;
680 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
681 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
683 gtk_paint_layout(widget->style, window,
687 pix_rect.x - text_rect.x, pix_rect.y,
691 g_object_unref(layout);
694 if (cellicon->show_marks)
696 GdkRectangle pix_rect;
697 GdkRectangle draw_rect;
700 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
701 pix_rect.height = TOGGLE_SPACING;
702 pix_rect.x = cell_area->x + cell->xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
703 pix_rect.y = cell_area->y + cell->ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
705 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
706 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
708 for (i = 0; i < cellicon->num_marks; i++)
710 gtk_paint_check (widget->style, window,
711 state, (cellicon->marks & (1 << i)) ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
712 cell_area, widget, "cellcheck",
713 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
715 TOGGLE_WIDTH, TOGGLE_WIDTH);
720 if (cellicon->focused && GTK_WIDGET_HAS_FOCUS(widget))
722 gtk_paint_focus(widget->style, window,
726 cell_area->x, cell_area->y,
727 cell_area->width, cell_area->height);