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);
59 static gint gqv_cell_renderer_icon_activate (GtkCellRenderer *cell,
63 GdkRectangle *background_area,
64 GdkRectangle *cell_area,
65 GtkCellRendererState flags);
91 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
93 static gpointer parent_class;
96 gqv_cell_renderer_icon_get_type(void)
98 static GType cell_icon_type = 0;
102 static const GTypeInfo cell_icon_info =
104 sizeof(GQvCellRendererIconClass), /* class_size */
105 NULL, /* base_init */
106 NULL, /* base_finalize */
107 (GClassInitFunc) gqv_cell_renderer_icon_class_init, /* class_init */
108 NULL, /* class_finalize */
109 NULL, /* class_data */
110 sizeof(GQvCellRendererIcon), /* instance_size */
112 (GInstanceInitFunc) gqv_cell_renderer_icon_init, /* instance_init */
113 NULL, /* value_table */
116 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
117 "GQvCellRendererIcon",
121 return cell_icon_type;
125 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
127 GTK_CELL_RENDERER(cellicon)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
128 GTK_CELL_RENDERER(cellicon)->xpad = 2;
129 GTK_CELL_RENDERER(cellicon)->ypad = 2;
133 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class)
135 GObjectClass *object_class = G_OBJECT_CLASS(class);
136 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
138 parent_class = g_type_class_peek_parent(class);
140 object_class->finalize = gqv_cell_renderer_icon_finalize;
142 object_class->get_property = gqv_cell_renderer_icon_get_property;
143 object_class->set_property = gqv_cell_renderer_icon_set_property;
145 cell_class->get_size = gqv_cell_renderer_icon_get_size;
146 cell_class->render = gqv_cell_renderer_icon_render;
147 cell_class->activate = gqv_cell_renderer_icon_activate;
149 g_object_class_install_property(object_class,
151 g_param_spec_object("pixbuf",
153 _("The pixbuf to render"),
157 g_object_class_install_property(object_class,
159 g_param_spec_string("text",
165 g_object_class_install_property(object_class,
167 g_param_spec_boxed("background_gdk",
168 _("Background color"),
169 _("Background color as a GdkColor"),
173 g_object_class_install_property(object_class,
175 g_param_spec_boxed("foreground_gdk",
176 _("Foreground color"),
177 _("Foreground color as a GdkColor"),
181 g_object_class_install_property(object_class,
183 g_param_spec_boolean("has_focus",
185 _("Draw focus indicator"),
189 g_object_class_install_property(object_class,
191 g_param_spec_int("fixed_width",
194 -1, FIXED_ICON_SIZE_MAX,
198 g_object_class_install_property(object_class,
200 g_param_spec_int("fixed_height",
202 _("Height of icon excluding text"),
203 -1, FIXED_ICON_SIZE_MAX,
207 g_object_class_install_property(object_class,
209 g_param_spec_boolean("background_set",
211 _("Whether this tag affects the background color"),
215 g_object_class_install_property(object_class,
217 g_param_spec_boolean("foreground_set",
219 _("Whether this tag affects the foreground color"),
223 g_object_class_install_property(object_class,
225 g_param_spec_boolean("show_text",
227 _("Whether the text is displayed"),
231 g_object_class_install_property(object_class,
233 g_param_spec_boolean("show_marks",
235 _("Whether the marks are displayed"),
239 g_object_class_install_property(object_class,
241 g_param_spec_int("num_marks",
242 _("Number of marks"),
243 _("Number of marks"),
248 g_object_class_install_property(object_class,
250 g_param_spec_uint("marks",
252 _("Marks bit array"),
257 g_object_class_install_property(object_class,
259 g_param_spec_uint("toggled_mark",
265 toggle_cell_signals[TOGGLED] =
266 g_signal_new ("toggled",
267 G_OBJECT_CLASS_TYPE (object_class),
269 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
271 g_cclosure_marshal_VOID__STRING,
278 gqv_cell_renderer_icon_finalize(GObject *object)
280 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
282 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
284 g_free(cellicon->text);
286 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
290 gqv_cell_renderer_icon_get_property(GObject *object,
295 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
300 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : NULL);
303 g_value_set_string(value, cellicon->text);
305 case PROP_BACKGROUND_GDK:
309 color.red = cellicon->background.red;
310 color.green = cellicon->background.green;
311 color.blue = cellicon->background.blue;
313 g_value_set_boxed(value, &color);
316 case PROP_FOREGROUND_GDK:
320 color.red = cellicon->foreground.red;
321 color.green = cellicon->foreground.green;
322 color.blue = cellicon->foreground.blue;
324 g_value_set_boxed(value, &color);
328 g_value_set_boolean(value, cellicon->focused);
330 case PROP_FIXED_WIDTH:
331 g_value_set_int(value, cellicon->fixed_width);
333 case PROP_FIXED_HEIGHT:
334 g_value_set_int(value, cellicon->fixed_height);
336 case PROP_BACKGROUND_SET:
337 g_value_set_boolean(value, cellicon->background_set);
339 case PROP_FOREGROUND_SET:
340 g_value_set_boolean(value, cellicon->foreground_set);
343 g_value_set_boolean(value, cellicon->show_text);
345 case PROP_SHOW_MARKS:
346 g_value_set_boolean(value, cellicon->show_marks);
349 g_value_set_int(value, cellicon->num_marks);
352 g_value_set_uint(value, cellicon->marks);
355 g_value_set_uint(value, cellicon->toggled_mark);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
364 set_bg_color(GQvCellRendererIcon *cellicon,
369 if (!cellicon->background_set)
371 cellicon->background_set = TRUE;
372 g_object_notify(G_OBJECT(cellicon), "background_set");
375 cellicon->background.red = color->red;
376 cellicon->background.green = color->green;
377 cellicon->background.blue = color->blue;
381 if (cellicon->background_set)
383 cellicon->background_set = FALSE;
384 g_object_notify(G_OBJECT(cellicon), "background_set");
389 static void set_fg_color(GQvCellRendererIcon *cellicon,
394 if (!cellicon->foreground_set)
396 cellicon->foreground_set = TRUE;
397 g_object_notify(G_OBJECT(cellicon), "foreground_set");
400 cellicon->foreground.red = color->red;
401 cellicon->foreground.green = color->green;
402 cellicon->foreground.blue = color->blue;
406 if (cellicon->foreground_set)
408 cellicon->foreground_set = FALSE;
409 g_object_notify(G_OBJECT(cellicon), "foreground_set");
415 gqv_cell_renderer_icon_set_property(GObject *object,
420 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
428 pixbuf = (GdkPixbuf *) g_value_get_object(value);
429 if (pixbuf) g_object_ref(pixbuf);
430 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
431 cellicon->pixbuf = pixbuf;
438 text = cellicon->text;
439 cellicon->text = g_strdup(g_value_get_string(value));
442 g_object_notify(object, "text");
445 case PROP_BACKGROUND_GDK:
446 set_bg_color(cellicon, g_value_get_boxed(value));
448 case PROP_FOREGROUND_GDK:
449 set_fg_color(cellicon, g_value_get_boxed(value));
452 cellicon->focused = g_value_get_boolean(value);
454 case PROP_FIXED_WIDTH:
455 cellicon->fixed_width = g_value_get_int(value);
457 case PROP_FIXED_HEIGHT:
458 cellicon->fixed_height = g_value_get_int(value);
460 case PROP_BACKGROUND_SET:
461 cellicon->background_set = g_value_get_boolean(value);
463 case PROP_FOREGROUND_SET:
464 cellicon->foreground_set = g_value_get_boolean(value);
467 cellicon->show_text = g_value_get_boolean(value);
469 case PROP_SHOW_MARKS:
470 cellicon->show_marks = g_value_get_boolean(value);
473 cellicon->num_marks = g_value_get_int(value);
476 cellicon->marks = g_value_get_uint(value);
479 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
485 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
490 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
492 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
493 pango_layout_set_width(layout, width);
494 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
495 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
499 PangoAttrList *attr_list;
501 attr_list = pango_attr_list_new();
503 if (cellicon->foreground_set)
506 PangoAttribute *attr;
508 color = cellicon->foreground;
510 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
512 attr->start_index = 0;
513 attr->end_index = G_MAXINT;
514 pango_attr_list_insert(attr_list, attr);
517 pango_layout_set_attributes(layout, attr_list);
518 pango_attr_list_unref(attr_list);
525 * gqv_cell_renderer_icon_new:
527 * Creates a new #GQvCellRendererIcon. Adjust rendering
528 * parameters using object properties. Object properties can be set
529 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
530 * can bind a property to a value in a #GtkTreeModel. For example, you
531 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
532 * in the model, thus rendering a different image in each row of the
535 * Return value: the new cell renderer
538 gqv_cell_renderer_icon_new(void)
540 return g_object_new(GQV_TYPE_CELL_RENDERER_ICON, NULL);
544 gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
546 GdkRectangle *cell_area,
552 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
556 if (cellicon->fixed_width > 0)
558 calc_width = cellicon->fixed_width;
562 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
565 if (cellicon->fixed_height > 0)
567 calc_height = cellicon->fixed_height;
571 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
574 if (cellicon->show_text && cellicon->text)
579 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
580 pango_layout_get_pixel_extents(layout, NULL, &rect);
581 g_object_unref(layout);
583 calc_width = MAX(calc_width, rect.width);
584 calc_height += rect.height;
587 if (cellicon->show_marks)
589 calc_height += TOGGLE_SPACING;
590 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
593 calc_width += (gint)cell->xpad * 2;
594 calc_height += (gint)cell->ypad * 2;
596 if (x_offset) *x_offset = 0;
597 if (y_offset) *y_offset = 0;
599 if (cell_area && calc_width > 0 && calc_height > 0)
603 *x_offset = (cell->xalign * (cell_area->width - calc_width - 2 * cell->xpad));
604 *x_offset = MAX(*x_offset, 0) + cell->xpad;
608 *y_offset = (cell->yalign * (cell_area->height - calc_height - 2 * cell->ypad));
609 *y_offset = MAX(*y_offset, 0) + cell->ypad;
613 if (width) *width = calc_width;
614 if (height) *height = calc_height;
618 gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
621 GdkRectangle *background_area,
622 GdkRectangle *cell_area,
623 GdkRectangle *expose_area,
624 GtkCellRendererState flags)
627 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
630 GdkRectangle cell_rect;
633 pixbuf = cellicon->pixbuf;
634 text = cellicon->text;
636 if (!pixbuf && !text) return;
638 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
639 &cell_rect.x, &cell_rect.y,
640 &cell_rect.width, &cell_rect.height);
642 cell_rect.x += cell->xpad;
643 cell_rect.y += cell->ypad;
644 cell_rect.width -= cell->xpad * 2;
645 cell_rect.height -= cell->ypad * 2;
647 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
649 if (GTK_WIDGET_HAS_FOCUS(widget))
650 state = GTK_STATE_SELECTED;
652 state = GTK_STATE_ACTIVE;
656 if (GTK_WIDGET_STATE(widget) == GTK_STATE_INSENSITIVE)
657 state = GTK_STATE_INSENSITIVE;
659 state = GTK_STATE_NORMAL;
664 GdkRectangle pix_rect;
665 GdkRectangle draw_rect;
667 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
668 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
670 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
672 if (cellicon->fixed_height > 0)
674 pix_rect.y = cell_area->y + cell->ypad + (cellicon->fixed_height - pix_rect.height) / 2;
678 pix_rect.y = cell_area->y + cell_rect.y;
681 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
682 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
684 gdk_draw_pixbuf(window,
685 widget->style->black_gc,
687 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
688 draw_rect.x - pix_rect.x,
689 draw_rect.y - pix_rect.y,
694 GDK_RGB_DITHER_NORMAL,
699 if (cellicon->show_text && text)
702 PangoRectangle text_rect;
703 GdkRectangle pix_rect;
704 GdkRectangle draw_rect;
706 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
707 pango_layout_get_pixel_extents(layout, NULL, &text_rect);
709 pix_rect.width = text_rect.width;
710 pix_rect.height = text_rect.height;
711 pix_rect.x = cell_area->x + cell->xpad + (cell_rect.width - text_rect.width + 1) / 2;
712 pix_rect.y = cell_area->y + cell->ypad + (cell_rect.height - text_rect.height);
714 if (cellicon->show_marks)
716 pix_rect.y -= TOGGLE_SPACING;
719 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
720 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
722 gtk_paint_layout(widget->style, window,
726 pix_rect.x - text_rect.x, pix_rect.y,
730 g_object_unref(layout);
733 if (cellicon->show_marks)
735 GdkRectangle pix_rect;
736 GdkRectangle draw_rect;
739 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
740 pix_rect.height = TOGGLE_SPACING;
741 pix_rect.x = cell_area->x + cell->xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
742 pix_rect.y = cell_area->y + cell->ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
744 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
745 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
747 for (i = 0; i < cellicon->num_marks; i++)
749 gtk_paint_check (widget->style, window,
750 state, (cellicon->marks & (1 << i)) ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
751 cell_area, widget, "cellcheck",
752 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
754 TOGGLE_WIDTH, TOGGLE_WIDTH);
759 if (cellicon->focused && GTK_WIDGET_HAS_FOCUS(widget))
761 gtk_paint_focus(widget->style, window,
765 cell_area->x, cell_area->y,
766 cell_area->width, cell_area->height);
771 gqv_cell_renderer_icon_activate (GtkCellRenderer *cell,
775 GdkRectangle *background_area,
776 GdkRectangle *cell_area,
777 GtkCellRendererState flags)
779 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
780 GdkEventButton *bevent = &event->button;
782 if (cellicon->show_marks &&
783 event->type == GDK_BUTTON_PRESS &&
784 !(bevent->state & GDK_SHIFT_MASK ) &&
785 !(bevent->state & GDK_CONTROL_MASK ))
788 GdkRectangle cell_rect;
791 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
792 &cell_rect.x, &cell_rect.y,
793 &cell_rect.width, &cell_rect.height);
795 cell_rect.x += cell->xpad;
796 cell_rect.y += cell->ypad;
797 cell_rect.width -= cell->xpad * 2;
798 cell_rect.height -= cell->ypad * 2;
800 rect.width = TOGGLE_WIDTH;
801 rect.height = TOGGLE_WIDTH;
802 rect.y = cell_area->y + cell->ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
803 for (i = 0; i < cellicon->num_marks; i++)
805 rect.x = cell_area->x + cell->xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
807 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
808 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
810 cellicon->toggled_mark = i;
811 g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
818 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */