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 const GdkRectangle *rectangle,
52 #if GTK_CHECK_VERSION(3,0,0)
53 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
56 const GdkRectangle *background_area,
57 const GdkRectangle *cell_area,
58 GtkCellRendererState flags);
60 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
63 GdkRectangle *background_area,
64 GdkRectangle *cell_area,
65 GdkRectangle *expose_area,
66 GtkCellRendererState flags);
70 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
74 const GdkRectangle *background_area,
75 const GdkRectangle *cell_area,
76 GtkCellRendererState flags);
102 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
104 static gpointer parent_class;
107 gqv_cell_renderer_icon_get_type(void)
109 static GType cell_icon_type = 0;
113 static const GTypeInfo cell_icon_info =
115 sizeof(GQvCellRendererIconClass), /* class_size */
116 NULL, /* base_init */
117 NULL, /* base_finalize */
118 (GClassInitFunc) gqv_cell_renderer_icon_class_init, /* class_init */
119 NULL, /* class_finalize */
120 NULL, /* class_data */
121 sizeof(GQvCellRendererIcon), /* instance_size */
123 (GInstanceInitFunc) gqv_cell_renderer_icon_init, /* instance_init */
124 NULL, /* value_table */
127 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
128 "GQvCellRendererIcon",
132 return cell_icon_type;
136 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
138 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
139 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
143 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class)
145 GObjectClass *object_class = G_OBJECT_CLASS(class);
146 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
148 parent_class = g_type_class_peek_parent(class);
150 object_class->finalize = gqv_cell_renderer_icon_finalize;
152 object_class->get_property = gqv_cell_renderer_icon_get_property;
153 object_class->set_property = gqv_cell_renderer_icon_set_property;
155 cell_class->get_size = gqv_cell_renderer_icon_get_size;
156 cell_class->render = gqv_cell_renderer_icon_render;
157 cell_class->activate = gqv_cell_renderer_icon_activate;
159 g_object_class_install_property(object_class,
161 g_param_spec_object("pixbuf",
163 "The pixbuf to render",
167 g_object_class_install_property(object_class,
169 g_param_spec_string("text",
175 g_object_class_install_property(object_class,
177 g_param_spec_boxed("background_gdk",
179 "Background color as a GdkColor",
183 g_object_class_install_property(object_class,
185 g_param_spec_boxed("foreground_gdk",
187 "Foreground color as a GdkColor",
191 g_object_class_install_property(object_class,
193 g_param_spec_boolean("has_focus",
195 "Draw focus indicator",
199 g_object_class_install_property(object_class,
201 g_param_spec_int("fixed_width",
204 -1, FIXED_ICON_SIZE_MAX,
208 g_object_class_install_property(object_class,
210 g_param_spec_int("fixed_height",
212 "Height of icon excluding text",
213 -1, FIXED_ICON_SIZE_MAX,
217 g_object_class_install_property(object_class,
219 g_param_spec_boolean("background_set",
221 "Whether this tag affects the background color",
225 g_object_class_install_property(object_class,
227 g_param_spec_boolean("foreground_set",
229 "Whether this tag affects the foreground color",
233 g_object_class_install_property(object_class,
235 g_param_spec_boolean("show_text",
237 "Whether the text is displayed",
241 g_object_class_install_property(object_class,
243 g_param_spec_boolean("show_marks",
245 "Whether the marks are displayed",
249 g_object_class_install_property(object_class,
251 g_param_spec_int("num_marks",
258 g_object_class_install_property(object_class,
260 g_param_spec_uint("marks",
267 g_object_class_install_property(object_class,
269 g_param_spec_uint("toggled_mark",
275 toggle_cell_signals[TOGGLED] =
276 g_signal_new("toggled",
277 G_OBJECT_CLASS_TYPE (object_class),
279 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
281 g_cclosure_marshal_VOID__STRING,
288 gqv_cell_renderer_icon_finalize(GObject *object)
290 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
292 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
294 g_free(cellicon->text);
296 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
300 gqv_cell_renderer_icon_get_property(GObject *object,
305 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
310 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : NULL);
313 g_value_set_string(value, cellicon->text);
315 case PROP_BACKGROUND_GDK:
319 color.red = cellicon->background.red;
320 color.green = cellicon->background.green;
321 color.blue = cellicon->background.blue;
323 g_value_set_boxed(value, &color);
326 case PROP_FOREGROUND_GDK:
330 color.red = cellicon->foreground.red;
331 color.green = cellicon->foreground.green;
332 color.blue = cellicon->foreground.blue;
334 g_value_set_boxed(value, &color);
338 g_value_set_boolean(value, cellicon->focused);
340 case PROP_FIXED_WIDTH:
341 g_value_set_int(value, cellicon->fixed_width);
343 case PROP_FIXED_HEIGHT:
344 g_value_set_int(value, cellicon->fixed_height);
346 case PROP_BACKGROUND_SET:
347 g_value_set_boolean(value, cellicon->background_set);
349 case PROP_FOREGROUND_SET:
350 g_value_set_boolean(value, cellicon->foreground_set);
353 g_value_set_boolean(value, cellicon->show_text);
355 case PROP_SHOW_MARKS:
356 g_value_set_boolean(value, cellicon->show_marks);
359 g_value_set_int(value, cellicon->num_marks);
362 g_value_set_uint(value, cellicon->marks);
365 g_value_set_uint(value, cellicon->toggled_mark);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
374 set_bg_color(GQvCellRendererIcon *cellicon,
379 if (!cellicon->background_set)
381 cellicon->background_set = TRUE;
382 g_object_notify(G_OBJECT(cellicon), "background_set");
385 cellicon->background.red = color->red;
386 cellicon->background.green = color->green;
387 cellicon->background.blue = color->blue;
391 if (cellicon->background_set)
393 cellicon->background_set = FALSE;
394 g_object_notify(G_OBJECT(cellicon), "background_set");
399 static void set_fg_color(GQvCellRendererIcon *cellicon,
404 if (!cellicon->foreground_set)
406 cellicon->foreground_set = TRUE;
407 g_object_notify(G_OBJECT(cellicon), "foreground_set");
410 cellicon->foreground.red = color->red;
411 cellicon->foreground.green = color->green;
412 cellicon->foreground.blue = color->blue;
416 if (cellicon->foreground_set)
418 cellicon->foreground_set = FALSE;
419 g_object_notify(G_OBJECT(cellicon), "foreground_set");
425 gqv_cell_renderer_icon_set_property(GObject *object,
430 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
438 pixbuf = (GdkPixbuf *) g_value_get_object(value);
439 if (pixbuf) g_object_ref(pixbuf);
440 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
441 cellicon->pixbuf = pixbuf;
448 text = cellicon->text;
449 cellicon->text = g_strdup(g_value_get_string(value));
452 g_object_notify(object, "text");
455 case PROP_BACKGROUND_GDK:
456 set_bg_color(cellicon, g_value_get_boxed(value));
458 case PROP_FOREGROUND_GDK:
459 set_fg_color(cellicon, g_value_get_boxed(value));
462 cellicon->focused = g_value_get_boolean(value);
464 case PROP_FIXED_WIDTH:
465 cellicon->fixed_width = g_value_get_int(value);
467 case PROP_FIXED_HEIGHT:
468 cellicon->fixed_height = g_value_get_int(value);
470 case PROP_BACKGROUND_SET:
471 cellicon->background_set = g_value_get_boolean(value);
473 case PROP_FOREGROUND_SET:
474 cellicon->foreground_set = g_value_get_boolean(value);
477 cellicon->show_text = g_value_get_boolean(value);
479 case PROP_SHOW_MARKS:
480 cellicon->show_marks = g_value_get_boolean(value);
483 cellicon->num_marks = g_value_get_int(value);
486 cellicon->marks = g_value_get_uint(value);
489 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
495 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
500 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
502 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
503 pango_layout_set_width(layout, width);
504 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
505 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
509 PangoAttrList *attr_list;
511 attr_list = pango_attr_list_new();
513 if (cellicon->foreground_set)
516 PangoAttribute *attr;
518 color = cellicon->foreground;
520 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
522 attr->start_index = 0;
523 attr->end_index = G_MAXINT;
524 pango_attr_list_insert(attr_list, attr);
527 pango_layout_set_attributes(layout, attr_list);
528 pango_attr_list_unref(attr_list);
535 * gqv_cell_renderer_icon_new:
537 * Creates a new #GQvCellRendererIcon. Adjust rendering
538 * parameters using object properties. Object properties can be set
539 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
540 * can bind a property to a value in a #GtkTreeModel. For example, you
541 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
542 * in the model, thus rendering a different image in each row of the
545 * Return value: the new cell renderer
548 gqv_cell_renderer_icon_new(void)
550 return g_object_new(GQV_TYPE_CELL_RENDERER_ICON, NULL);
554 gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
556 const GdkRectangle *cell_area,
562 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
566 gfloat xalign, yalign;
568 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
569 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
571 if (cellicon->fixed_width > 0)
573 calc_width = cellicon->fixed_width;
577 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
580 if (cellicon->fixed_height > 0)
582 calc_height = cellicon->fixed_height;
586 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
589 if (cellicon->show_text && cellicon->text)
594 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
595 pango_layout_get_pixel_extents(layout, NULL, &rect);
596 g_object_unref(layout);
598 calc_width = MAX(calc_width, rect.width);
599 calc_height += rect.height;
602 if (cellicon->show_marks)
604 calc_height += TOGGLE_SPACING;
605 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
608 calc_width += xpad * 2;
609 calc_height += ypad * 2;
611 if (x_offset) *x_offset = 0;
612 if (y_offset) *y_offset = 0;
614 if (cell_area && calc_width > 0 && calc_height > 0)
618 *x_offset = (xalign * (cell_area->width - calc_width - 2 * xpad));
619 *x_offset = MAX(*x_offset, 0) + xpad;
623 *y_offset = (yalign * (cell_area->height - calc_height - 2 * ypad));
624 *y_offset = MAX(*y_offset, 0) + ypad;
628 if (width) *width = calc_width;
629 if (height) *height = calc_height;
632 #if GTK_CHECK_VERSION(3,0,0)
633 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
636 const GdkRectangle *background_area,
637 const GdkRectangle *cell_area,
638 GtkCellRendererState flags)
641 GtkStyleContext *context = gtk_widget_get_style_context(widget);
644 gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
647 GdkRectangle *background_area,
648 GdkRectangle *cell_area,
649 GdkRectangle *expose_area,
650 GtkCellRendererState flags)
653 cairo_t *cr = gdk_cairo_create(window);
655 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
658 GdkRectangle cell_rect;
663 pixbuf = cellicon->pixbuf;
664 text = cellicon->text;
666 if (!pixbuf && !text) return;
668 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
670 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
671 &cell_rect.x, &cell_rect.y,
672 &cell_rect.width, &cell_rect.height);
676 cell_rect.width -= xpad * 2;
677 cell_rect.height -= ypad * 2;
679 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
681 if (gtk_widget_has_focus(widget))
682 state = GTK_STATE_SELECTED;
684 state = GTK_STATE_ACTIVE;
688 if (gtk_widget_get_state(widget) == GTK_STATE_INSENSITIVE)
689 state = GTK_STATE_INSENSITIVE;
691 state = GTK_STATE_NORMAL;
694 #if GTK_CHECK_VERSION(3,0,0)
695 gtk_style_context_set_state(context, state);
700 GdkRectangle pix_rect;
701 GdkRectangle draw_rect;
703 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
704 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
706 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
708 if (cellicon->fixed_height > 0)
710 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
714 pix_rect.y = cell_area->y + cell_rect.y;
717 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect)
718 #if !GTK_CHECK_VERSION(3,0,0)
719 && gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect)
723 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
734 if (cellicon->show_text && text)
737 PangoRectangle text_rect;
738 GdkRectangle pix_rect;
739 GdkRectangle draw_rect;
740 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
741 pango_layout_get_pixel_extents(layout, NULL, &text_rect);
743 pix_rect.width = text_rect.width;
744 pix_rect.height = text_rect.height;
745 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
746 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
748 if (cellicon->show_marks)
750 pix_rect.y -= TOGGLE_SPACING;
753 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect)
754 #if !GTK_CHECK_VERSION(3,0,0)
755 && gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect)
759 #if GTK_CHECK_VERSION(3,0,0)
760 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
763 gtk_paint_layout(gtk_widget_get_style(widget), window,
767 pix_rect.x - text_rect.x, pix_rect.y,
771 g_object_unref(layout);
774 if (cellicon->show_marks)
776 GdkRectangle pix_rect;
777 GdkRectangle draw_rect;
780 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
781 pix_rect.height = TOGGLE_SPACING;
782 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
783 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
785 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect)
786 #if !GTK_CHECK_VERSION(3,0,0)
787 && gdk_rectangle_intersect(expose_area, &draw_rect, &draw_rect)
791 for (i = 0; i < cellicon->num_marks; i++)
793 #if GTK_CHECK_VERSION(3,0,0)
794 state &= ~(GTK_STATE_FLAG_ACTIVE);
796 if ((cellicon->marks & (1 << i)))
797 state |= GTK_STATE_FLAG_ACTIVE;
802 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
804 TOGGLE_WIDTH, TOGGLE_WIDTH);
807 gtk_style_context_save(context);
808 gtk_style_context_set_state(context, state);
810 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
812 gtk_render_check(context, cr,
813 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
815 TOGGLE_WIDTH, TOGGLE_WIDTH);
816 gtk_style_context_restore(context);
820 gtk_paint_check(gtk_widget_get_style(widget), window,
821 state, (cellicon->marks & (1 << i)) ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
822 cell_area, widget, "cellcheck",
823 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
825 TOGGLE_WIDTH, TOGGLE_WIDTH);
831 if (cellicon->focused && gtk_widget_has_focus(widget))
833 #if GTK_CHECK_VERSION(3,0,0)
835 gtk_paint_focus(gtk_widget_get_style(widget), window,
839 cell_area->x, cell_area->y,
840 cell_area->width, cell_area->height);
843 #if !GTK_CHECK_VERSION(3,0,0)
849 gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
853 const GdkRectangle *background_area,
854 const GdkRectangle *cell_area,
855 GtkCellRendererState flags)
857 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
858 GdkEventButton *bevent = &event->button;
860 if (cellicon->show_marks &&
861 event->type == GDK_BUTTON_PRESS &&
862 !(bevent->state & GDK_SHIFT_MASK ) &&
863 !(bevent->state & GDK_CONTROL_MASK ))
866 GdkRectangle cell_rect;
870 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
872 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
873 &cell_rect.x, &cell_rect.y,
874 &cell_rect.width, &cell_rect.height);
878 cell_rect.width -= xpad * 2;
879 cell_rect.height -= ypad * 2;
881 rect.width = TOGGLE_WIDTH;
882 rect.height = TOGGLE_WIDTH;
883 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
884 for (i = 0; i < cellicon->num_marks; i++)
886 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
888 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
889 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
891 cellicon->toggled_mark = i;
892 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
899 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */