2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "cellrenderericon.h"
25 FIXED_ICON_SIZE_MAX = 512
29 static void gqv_cell_renderer_icon_get_property(GObject *object,
33 static void gqv_cell_renderer_icon_set_property(GObject *object,
37 static void gqv_cell_renderer_icon_init_wrapper(void *, void *);
38 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon);
39 static void gqv_cell_renderer_icon_class_init_wrapper(void *, void *);
40 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class);
41 static void gqv_cell_renderer_icon_finalize(GObject *object);
42 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
44 const GdkRectangle *cell_area,
50 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
53 const GdkRectangle *background_area,
54 const GdkRectangle *cell_area,
55 GtkCellRendererState flags);
57 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
61 const GdkRectangle *background_area,
62 const GdkRectangle *cell_area,
63 GtkCellRendererState flags);
88 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
90 static gpointer parent_class;
93 gqv_cell_renderer_icon_get_type()
95 static GType cell_icon_type = 0;
99 static const GTypeInfo cell_icon_info =
101 sizeof(GQvCellRendererIconClass), /* class_size */
102 nullptr, /* base_init */
103 nullptr, /* base_finalize */
104 static_cast<GClassInitFunc>(gqv_cell_renderer_icon_class_init_wrapper), /* class_init */
105 nullptr, /* class_finalize */
106 nullptr, /* class_data */
107 sizeof(GQvCellRendererIcon), /* instance_size */
109 reinterpret_cast<GInstanceInitFunc>(gqv_cell_renderer_icon_init_wrapper), /* instance_init */
110 nullptr, /* value_table */
113 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
114 "GQvCellRendererIcon",
115 &cell_icon_info, static_cast<GTypeFlags>(0));
118 return cell_icon_type;
122 gqv_cell_renderer_icon_init_wrapper(void *data, void *)
124 gqv_cell_renderer_icon_init(static_cast<GQvCellRendererIcon *>(data));
128 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
130 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
131 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
135 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *)
137 gqv_cell_renderer_icon_class_init(static_cast<GQvCellRendererIconClass *>(data));
141 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class)
143 GObjectClass *object_class = G_OBJECT_CLASS(icon_class);
144 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(icon_class);
146 parent_class = g_type_class_peek_parent(icon_class);
148 object_class->finalize = gqv_cell_renderer_icon_finalize;
150 object_class->get_property = gqv_cell_renderer_icon_get_property;
151 object_class->set_property = gqv_cell_renderer_icon_set_property;
153 cell_class->get_size = gqv_cell_renderer_icon_get_size;
154 cell_class->render = gqv_cell_renderer_icon_render;
155 cell_class->activate = gqv_cell_renderer_icon_activate;
157 g_object_class_install_property(object_class,
159 g_param_spec_object("pixbuf",
161 "The pixbuf to render",
165 g_object_class_install_property(object_class,
167 g_param_spec_string("text",
173 g_object_class_install_property(object_class,
175 g_param_spec_boxed("background_rgba",
177 "Background color as a GdkRGBA",
181 g_object_class_install_property(object_class,
183 g_param_spec_boxed("foreground_rgba",
185 "Foreground color as a GdkRGBA",
189 g_object_class_install_property(object_class,
191 g_param_spec_boolean("has_focus",
193 "Draw focus indicator",
197 g_object_class_install_property(object_class,
199 g_param_spec_int("fixed_width",
202 -1, FIXED_ICON_SIZE_MAX,
206 g_object_class_install_property(object_class,
208 g_param_spec_int("fixed_height",
210 "Height of icon excluding text",
211 -1, FIXED_ICON_SIZE_MAX,
215 g_object_class_install_property(object_class,
217 g_param_spec_boolean("background_set",
219 "Whether this tag affects the background color",
223 g_object_class_install_property(object_class,
225 g_param_spec_boolean("foreground_set",
227 "Whether this tag affects the foreground color",
231 g_object_class_install_property(object_class,
233 g_param_spec_boolean("show_text",
235 "Whether the text is displayed",
239 g_object_class_install_property(object_class,
241 g_param_spec_boolean("show_marks",
243 "Whether the marks are displayed",
247 g_object_class_install_property(object_class,
249 g_param_spec_int("num_marks",
256 g_object_class_install_property(object_class,
258 g_param_spec_uint("marks",
265 g_object_class_install_property(object_class,
267 g_param_spec_uint("toggled_mark",
273 toggle_cell_signals[TOGGLED] =
274 g_signal_new("toggled",
275 G_OBJECT_CLASS_TYPE (object_class),
277 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
279 g_cclosure_marshal_VOID__STRING,
286 gqv_cell_renderer_icon_finalize(GObject *object)
288 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
290 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
292 g_free(cellicon->text);
294 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
298 gqv_cell_renderer_icon_get_property(GObject *object,
303 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
308 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : nullptr);
311 g_value_set_string(value, cellicon->text);
313 case PROP_BACKGROUND_GDK:
317 color.red = cellicon->background.red / 65535.0;
318 color.green = cellicon->background.green / 65535.0;
319 color.blue = cellicon->background.blue / 65535.0;
321 g_value_set_boxed(value, &color);
324 case PROP_FOREGROUND_GDK:
328 color.red = cellicon->foreground.red / 65535.0;
329 color.green = cellicon->foreground.green / 65535.0;
330 color.blue = cellicon->foreground.blue / 65535.0;
332 g_value_set_boxed(value, &color);
336 g_value_set_boolean(value, cellicon->focused);
338 case PROP_FIXED_WIDTH:
339 g_value_set_int(value, cellicon->fixed_width);
341 case PROP_FIXED_HEIGHT:
342 g_value_set_int(value, cellicon->fixed_height);
344 case PROP_BACKGROUND_SET:
345 g_value_set_boolean(value, cellicon->background_set);
347 case PROP_FOREGROUND_SET:
348 g_value_set_boolean(value, cellicon->foreground_set);
351 g_value_set_boolean(value, cellicon->show_text);
353 case PROP_SHOW_MARKS:
354 g_value_set_boolean(value, cellicon->show_marks);
357 g_value_set_int(value, cellicon->num_marks);
360 g_value_set_uint(value, cellicon->marks);
363 g_value_set_uint(value, cellicon->toggled_mark);
366 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
371 static void set_bg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
375 if (!cellicon->background_set)
377 cellicon->background_set = TRUE;
378 g_object_notify(G_OBJECT(cellicon), "background_set");
381 cellicon->background.red = color->red * 65535;
382 cellicon->background.green = color->green * 65535;
383 cellicon->background.blue = color->blue * 65535;
387 if (cellicon->background_set)
389 cellicon->background_set = FALSE;
390 g_object_notify(G_OBJECT(cellicon), "background_set");
395 static void set_fg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
399 if (!cellicon->foreground_set)
401 cellicon->foreground_set = TRUE;
402 g_object_notify(G_OBJECT(cellicon), "foreground_set");
404 cellicon->foreground.red = color->red * 65535;
405 cellicon->foreground.green = color->green * 65535;
406 cellicon->foreground.blue = color->blue * 65535;
410 if (cellicon->foreground_set)
412 cellicon->foreground_set = FALSE;
413 g_object_notify(G_OBJECT(cellicon), "foreground_set");
419 gqv_cell_renderer_icon_set_property(GObject *object,
424 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
432 pixbuf = static_cast<GdkPixbuf *>(g_value_get_object(value));
433 if (pixbuf) g_object_ref(pixbuf);
434 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
435 cellicon->pixbuf = pixbuf;
442 text = cellicon->text;
443 cellicon->text = g_strdup(g_value_get_string(value));
446 g_object_notify(object, "text");
449 case PROP_BACKGROUND_GDK:
450 set_bg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
452 case PROP_FOREGROUND_GDK:
453 set_fg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
456 cellicon->focused = g_value_get_boolean(value);
458 case PROP_FIXED_WIDTH:
459 cellicon->fixed_width = g_value_get_int(value);
461 case PROP_FIXED_HEIGHT:
462 cellicon->fixed_height = g_value_get_int(value);
464 case PROP_BACKGROUND_SET:
465 cellicon->background_set = g_value_get_boolean(value);
467 case PROP_FOREGROUND_SET:
468 cellicon->foreground_set = g_value_get_boolean(value);
471 cellicon->show_text = g_value_get_boolean(value);
473 case PROP_SHOW_MARKS:
474 cellicon->show_marks = g_value_get_boolean(value);
477 cellicon->num_marks = g_value_get_int(value);
480 cellicon->marks = g_value_get_uint(value);
483 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
489 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
494 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
496 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
497 pango_layout_set_width(layout, width);
498 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
499 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
503 PangoAttrList *attr_list;
505 attr_list = pango_attr_list_new();
507 if (cellicon->foreground_set)
510 PangoAttribute *attr;
512 color = cellicon->foreground;
514 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
516 attr->start_index = 0;
517 attr->end_index = G_MAXINT;
518 pango_attr_list_insert(attr_list, attr);
521 pango_layout_set_attributes(layout, attr_list);
522 pango_attr_list_unref(attr_list);
529 * gqv_cell_renderer_icon_new:
531 * Creates a new #GQvCellRendererIcon. Adjust rendering
532 * parameters using object properties. Object properties can be set
533 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
534 * can bind a property to a value in a #GtkTreeModel. For example, you
535 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
536 * in the model, thus rendering a different image in each row of the
539 * Return value: the new cell renderer
542 gqv_cell_renderer_icon_new()
544 return static_cast<GtkCellRenderer *>(g_object_new(GQV_TYPE_CELL_RENDERER_ICON, nullptr));
547 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
549 const GdkRectangle *cell_area,
555 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
563 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
564 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
566 if (cellicon->fixed_width > 0)
568 calc_width = cellicon->fixed_width;
572 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
575 if (cellicon->fixed_height > 0)
577 calc_height = cellicon->fixed_height;
581 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
584 if (cellicon->show_text && cellicon->text)
589 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
590 pango_layout_get_pixel_extents(layout, nullptr, &rect);
591 g_object_unref(layout);
593 calc_width = MAX(calc_width, rect.width);
594 calc_height += rect.height;
597 if (cellicon->show_marks)
599 calc_height += TOGGLE_SPACING;
600 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
603 calc_width += xpad * 2;
604 calc_height += ypad * 2;
606 if (x_offset) *x_offset = 0;
607 if (y_offset) *y_offset = 0;
609 if (cell_area && calc_width > 0 && calc_height > 0)
613 *x_offset = (xalign * (cell_area->width - calc_width - 2 * xpad));
614 *x_offset = MAX(*x_offset, 0) + xpad;
618 *y_offset = (yalign * (cell_area->height - calc_height - 2 * ypad));
619 *y_offset = MAX(*y_offset, 0) + ypad;
623 if (width) *width = calc_width;
624 if (height) *height = calc_height;
627 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
630 const GdkRectangle *,
631 const GdkRectangle *cell_area,
632 GtkCellRendererState flags)
635 GtkStyleContext *context = gtk_widget_get_style_context(widget);
636 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
639 GdkRectangle cell_rect;
645 pixbuf = cellicon->pixbuf;
646 text = cellicon->text;
647 if (!pixbuf && !text)
652 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
654 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
655 &cell_rect.x, &cell_rect.y,
656 &cell_rect.width, &cell_rect.height);
660 cell_rect.width -= xpad * 2;
661 cell_rect.height -= ypad * 2;
663 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
665 if (gtk_widget_has_focus(widget))
666 state = GTK_STATE_FLAG_SELECTED;
668 state = GTK_STATE_FLAG_ACTIVE;
672 if (!gtk_widget_is_sensitive(widget))
673 state = GTK_STATE_FLAG_INSENSITIVE;
675 state = GTK_STATE_FLAG_NORMAL;
677 gtk_style_context_set_state(context, state);
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))
700 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
711 if (cellicon->show_text && text)
714 PangoRectangle text_rect;
715 GdkRectangle pix_rect;
716 GdkRectangle draw_rect;
717 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
718 pango_layout_get_pixel_extents(layout, nullptr, &text_rect);
720 pix_rect.width = text_rect.width;
721 pix_rect.height = text_rect.height;
722 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
723 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
725 if (cellicon->show_marks)
727 pix_rect.y -= TOGGLE_SPACING;
730 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
732 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
734 g_object_unref(layout);
737 if (cellicon->show_marks)
739 GdkRectangle pix_rect;
740 GdkRectangle draw_rect;
743 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
744 pix_rect.height = TOGGLE_SPACING;
745 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
746 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
748 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
750 for (i = 0; i < cellicon->num_marks; i++)
752 state = static_cast<GtkStateFlags>(state & ~GTK_STATE_FLAG_CHECKED);
754 if ((cellicon->marks & (1 << i)))
755 state = static_cast<GtkStateFlags>(state | GTK_STATE_FLAG_CHECKED);
759 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
761 TOGGLE_WIDTH, TOGGLE_WIDTH);
764 gtk_style_context_save(context);
765 gtk_style_context_set_state(context, state);
767 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
769 gtk_style_context_add_class(context, "marks");
771 if (state & GTK_STATE_FLAG_CHECKED)
773 gtk_render_check(context, cr,
774 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
776 TOGGLE_WIDTH, TOGGLE_WIDTH);
778 gtk_render_frame(context, cr,
779 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
781 TOGGLE_WIDTH, TOGGLE_WIDTH);
783 if (cellicon->focused && gtk_widget_has_focus(widget))
785 gtk_render_focus(context, cr,
786 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
787 pix_rect.y, TOGGLE_WIDTH, TOGGLE_WIDTH);
789 gtk_style_context_restore(context);
796 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
800 const GdkRectangle *,
801 const GdkRectangle *cell_area,
802 GtkCellRendererState)
804 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
805 GdkEventButton *bevent = &event->button;
807 if (cellicon->show_marks &&
808 event->type == GDK_BUTTON_PRESS &&
809 !(bevent->state & GDK_SHIFT_MASK ) &&
810 !(bevent->state & GDK_CONTROL_MASK ))
813 GdkRectangle cell_rect;
818 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
820 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
821 &cell_rect.x, &cell_rect.y,
822 &cell_rect.width, &cell_rect.height);
826 cell_rect.width -= xpad * 2;
827 cell_rect.height -= ypad * 2;
829 rect.width = TOGGLE_WIDTH;
830 rect.height = TOGGLE_WIDTH;
831 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
832 for (i = 0; i < cellicon->num_marks; i++)
834 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
836 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
837 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
839 cellicon->toggled_mark = i;
840 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
847 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */