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 <gtk/gtk.h> /* To define GTK_CHECK_VERSION */
24 #include "cellrenderericon.h"
28 #define FIXED_ICON_SIZE_MAX 512
30 #define TOGGLE_WIDTH 13
31 #define TOGGLE_SPACING 18
34 static void gqv_cell_renderer_icon_get_property(GObject *object,
38 static void gqv_cell_renderer_icon_set_property(GObject *object,
42 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *celltext);
43 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class);
44 static void gqv_cell_renderer_icon_finalize(GObject *object);
45 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
47 GdkRectangle *rectangle,
52 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
55 GdkRectangle *background_area,
56 GdkRectangle *cell_area,
57 GdkRectangle *expose_area,
58 GtkCellRendererState flags);
60 static gint gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
64 GdkRectangle *background_area,
65 GdkRectangle *cell_area,
66 GtkCellRendererState flags);
92 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
94 static gpointer parent_class;
97 gqv_cell_renderer_icon_get_type(void)
99 static GType cell_icon_type = 0;
103 static const GTypeInfo cell_icon_info =
105 sizeof(GQvCellRendererIconClass), /* class_size */
106 NULL, /* base_init */
107 NULL, /* base_finalize */
108 (GClassInitFunc) gqv_cell_renderer_icon_class_init, /* class_init */
109 NULL, /* class_finalize */
110 NULL, /* class_data */
111 sizeof(GQvCellRendererIcon), /* instance_size */
113 (GInstanceInitFunc) gqv_cell_renderer_icon_init, /* instance_init */
114 NULL, /* value_table */
117 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
118 "GQvCellRendererIcon",
122 return cell_icon_type;
126 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
128 GTK_CELL_RENDERER(cellicon)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
129 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 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",
169 "Background color as a GdkColor",
173 g_object_class_install_property(object_class,
175 g_param_spec_boxed("foreground_gdk",
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",
248 g_object_class_install_property(object_class,
250 g_param_spec_uint("marks",
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;
558 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
559 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
561 if (cellicon->fixed_width > 0)
563 calc_width = cellicon->fixed_width;
567 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
570 if (cellicon->fixed_height > 0)
572 calc_height = cellicon->fixed_height;
576 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
579 if (cellicon->show_text && cellicon->text)
584 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
585 pango_layout_get_pixel_extents(layout, NULL, &rect);
586 g_object_unref(layout);
588 calc_width = MAX(calc_width, rect.width);
589 calc_height += rect.height;
592 if (cellicon->show_marks)
594 calc_height += TOGGLE_SPACING;
595 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
598 calc_width += xpad * 2;
599 calc_height += ypad * 2;
601 if (x_offset) *x_offset = 0;
602 if (y_offset) *y_offset = 0;
604 if (cell_area && calc_width > 0 && calc_height > 0)
608 *x_offset = (xalign * (cell_area->width - calc_width - 2 * xpad));
609 *x_offset = MAX(*x_offset, 0) + xpad;
613 *y_offset = (yalign * (cell_area->height - calc_height - 2 * ypad));
614 *y_offset = MAX(*y_offset, 0) + ypad;
618 if (width) *width = calc_width;
619 if (height) *height = calc_height;
623 gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
626 GdkRectangle *background_area,
627 GdkRectangle *cell_area,
628 GdkRectangle *expose_area,
629 GtkCellRendererState flags)
632 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
635 GdkRectangle cell_rect;
640 pixbuf = cellicon->pixbuf;
641 text = cellicon->text;
643 if (!pixbuf && !text) return;
645 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
647 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
648 &cell_rect.x, &cell_rect.y,
649 &cell_rect.width, &cell_rect.height);
653 cell_rect.width -= xpad * 2;
654 cell_rect.height -= ypad * 2;
656 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
658 #if GTK_CHECK_VERSION(2,20,0)
659 if (gtk_widget_has_focus(widget))
661 if (GTK_WIDGET_HAS_FOCUS(widget))
663 state = GTK_STATE_SELECTED;
665 state = GTK_STATE_ACTIVE;
669 #if GTK_CHECK_VERSION(2,20,0)
670 if (gtk_widget_get_state(widget) == GTK_STATE_INSENSITIVE)
672 if (GTK_WIDGET_STATE(widget) == GTK_STATE_INSENSITIVE)
674 state = GTK_STATE_INSENSITIVE;
676 state = GTK_STATE_NORMAL;
681 GdkRectangle pix_rect;
682 GdkRectangle draw_rect;
684 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
685 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
687 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
689 if (cellicon->fixed_height > 0)
691 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
695 pix_rect.y = cell_area->y + cell_rect.y;
698 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
699 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
701 gdk_draw_pixbuf(window,
702 widget->style->black_gc,
704 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
705 draw_rect.x - pix_rect.x,
706 draw_rect.y - pix_rect.y,
711 GDK_RGB_DITHER_NORMAL,
716 if (cellicon->show_text && text)
719 PangoRectangle text_rect;
720 GdkRectangle pix_rect;
721 GdkRectangle draw_rect;
723 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
724 pango_layout_get_pixel_extents(layout, NULL, &text_rect);
726 pix_rect.width = text_rect.width;
727 pix_rect.height = text_rect.height;
728 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
729 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
731 if (cellicon->show_marks)
733 pix_rect.y -= TOGGLE_SPACING;
736 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
737 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
739 gtk_paint_layout(widget->style, window,
743 pix_rect.x - text_rect.x, pix_rect.y,
747 g_object_unref(layout);
750 if (cellicon->show_marks)
752 GdkRectangle pix_rect;
753 GdkRectangle draw_rect;
756 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
757 pix_rect.height = TOGGLE_SPACING;
758 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
759 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
761 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect) &&
762 gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect))
764 for (i = 0; i < cellicon->num_marks; i++)
766 gtk_paint_check(widget->style, window,
767 state, (cellicon->marks & (1 << i)) ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
768 cell_area, widget, "cellcheck",
769 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
771 TOGGLE_WIDTH, TOGGLE_WIDTH);
776 #if GTK_CHECK_VERSION(2,20,0)
777 if (cellicon->focused && gtk_widget_has_focus(widget))
779 if (cellicon->focused && GTK_WIDGET_HAS_FOCUS(widget))
782 gtk_paint_focus(widget->style, window,
786 cell_area->x, cell_area->y,
787 cell_area->width, cell_area->height);
792 gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
796 GdkRectangle *background_area,
797 GdkRectangle *cell_area,
798 GtkCellRendererState flags)
800 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
801 GdkEventButton *bevent = &event->button;
803 if (cellicon->show_marks &&
804 event->type == GDK_BUTTON_PRESS &&
805 !(bevent->state & GDK_SHIFT_MASK ) &&
806 !(bevent->state & GDK_CONTROL_MASK ))
809 GdkRectangle cell_rect;
813 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
815 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
816 &cell_rect.x, &cell_rect.y,
817 &cell_rect.width, &cell_rect.height);
821 cell_rect.width -= xpad * 2;
822 cell_rect.height -= ypad * 2;
824 rect.width = TOGGLE_WIDTH;
825 rect.height = TOGGLE_WIDTH;
826 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
827 for (i = 0; i < cellicon->num_marks; i++)
829 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
831 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
832 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
834 cellicon->toggled_mark = i;
835 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
842 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */