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"
30 constexpr gint FIXED_ICON_SIZE_MAX = 512;
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_wrapper(void *, void *);
43 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon);
44 static void gqv_cell_renderer_icon_class_init_wrapper(void *, void *);
45 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class);
46 static void gqv_cell_renderer_icon_finalize(GObject *object);
47 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
49 const GdkRectangle *cell_area,
55 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
58 const GdkRectangle *background_area,
59 const GdkRectangle *cell_area,
60 GtkCellRendererState flags);
62 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
66 const GdkRectangle *background_area,
67 const GdkRectangle *cell_area,
68 GtkCellRendererState flags);
93 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
95 static gpointer parent_class;
98 gqv_cell_renderer_icon_get_type()
100 static GType cell_icon_type = 0;
104 static const GTypeInfo cell_icon_info =
106 sizeof(GQvCellRendererIconClass), /* class_size */
107 nullptr, /* base_init */
108 nullptr, /* base_finalize */
109 static_cast<GClassInitFunc>(gqv_cell_renderer_icon_class_init_wrapper), /* class_init */
110 nullptr, /* class_finalize */
111 nullptr, /* class_data */
112 sizeof(GQvCellRendererIcon), /* instance_size */
114 reinterpret_cast<GInstanceInitFunc>(gqv_cell_renderer_icon_init_wrapper), /* instance_init */
115 nullptr, /* value_table */
118 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
119 "GQvCellRendererIcon",
120 &cell_icon_info, static_cast<GTypeFlags>(0));
123 return cell_icon_type;
127 gqv_cell_renderer_icon_init_wrapper(void *data, void *)
129 gqv_cell_renderer_icon_init(static_cast<GQvCellRendererIcon *>(data));
133 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
135 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
136 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
140 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *)
142 gqv_cell_renderer_icon_class_init(static_cast<GQvCellRendererIconClass *>(data));
146 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class)
148 GObjectClass *object_class = G_OBJECT_CLASS(icon_class);
149 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(icon_class);
151 parent_class = g_type_class_peek_parent(icon_class);
153 object_class->finalize = gqv_cell_renderer_icon_finalize;
155 object_class->get_property = gqv_cell_renderer_icon_get_property;
156 object_class->set_property = gqv_cell_renderer_icon_set_property;
158 cell_class->get_size = gqv_cell_renderer_icon_get_size;
159 cell_class->render = gqv_cell_renderer_icon_render;
160 cell_class->activate = gqv_cell_renderer_icon_activate;
162 g_object_class_install_property(object_class,
164 g_param_spec_object("pixbuf",
166 "The pixbuf to render",
170 g_object_class_install_property(object_class,
172 g_param_spec_string("text",
178 g_object_class_install_property(object_class,
180 g_param_spec_boxed("background_rgba",
182 "Background color as a GdkRGBA",
186 g_object_class_install_property(object_class,
188 g_param_spec_boxed("foreground_rgba",
190 "Foreground color as a GdkRGBA",
194 g_object_class_install_property(object_class,
196 g_param_spec_boolean("has_focus",
198 "Draw focus indicator",
202 g_object_class_install_property(object_class,
204 g_param_spec_int("fixed_width",
207 -1, FIXED_ICON_SIZE_MAX,
211 g_object_class_install_property(object_class,
213 g_param_spec_int("fixed_height",
215 "Height of icon excluding text",
216 -1, FIXED_ICON_SIZE_MAX,
220 g_object_class_install_property(object_class,
222 g_param_spec_boolean("background_set",
224 "Whether this tag affects the background color",
228 g_object_class_install_property(object_class,
230 g_param_spec_boolean("foreground_set",
232 "Whether this tag affects the foreground color",
236 g_object_class_install_property(object_class,
238 g_param_spec_boolean("show_text",
240 "Whether the text is displayed",
244 g_object_class_install_property(object_class,
246 g_param_spec_boolean("show_marks",
248 "Whether the marks are displayed",
252 g_object_class_install_property(object_class,
254 g_param_spec_int("num_marks",
261 g_object_class_install_property(object_class,
263 g_param_spec_uint("marks",
270 g_object_class_install_property(object_class,
272 g_param_spec_uint("toggled_mark",
278 toggle_cell_signals[TOGGLED] =
279 g_signal_new("toggled",
280 G_OBJECT_CLASS_TYPE (object_class),
282 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
284 g_cclosure_marshal_VOID__STRING,
291 gqv_cell_renderer_icon_finalize(GObject *object)
293 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
295 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
297 g_free(cellicon->text);
299 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
303 gqv_cell_renderer_icon_get_property(GObject *object,
308 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
313 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : nullptr);
316 g_value_set_string(value, cellicon->text);
318 case PROP_BACKGROUND_GDK:
322 color.red = cellicon->background.red / 65535.0;
323 color.green = cellicon->background.green / 65535.0;
324 color.blue = cellicon->background.blue / 65535.0;
326 g_value_set_boxed(value, &color);
329 case PROP_FOREGROUND_GDK:
333 color.red = cellicon->foreground.red / 65535.0;
334 color.green = cellicon->foreground.green / 65535.0;
335 color.blue = cellicon->foreground.blue / 65535.0;
337 g_value_set_boxed(value, &color);
341 g_value_set_boolean(value, cellicon->focused);
343 case PROP_FIXED_WIDTH:
344 g_value_set_int(value, cellicon->fixed_width);
346 case PROP_FIXED_HEIGHT:
347 g_value_set_int(value, cellicon->fixed_height);
349 case PROP_BACKGROUND_SET:
350 g_value_set_boolean(value, cellicon->background_set);
352 case PROP_FOREGROUND_SET:
353 g_value_set_boolean(value, cellicon->foreground_set);
356 g_value_set_boolean(value, cellicon->show_text);
358 case PROP_SHOW_MARKS:
359 g_value_set_boolean(value, cellicon->show_marks);
362 g_value_set_int(value, cellicon->num_marks);
365 g_value_set_uint(value, cellicon->marks);
368 g_value_set_uint(value, cellicon->toggled_mark);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
376 static void set_bg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
380 if (!cellicon->background_set)
382 cellicon->background_set = TRUE;
383 g_object_notify(G_OBJECT(cellicon), "background_set");
386 cellicon->background.red = color->red * 65535;
387 cellicon->background.green = color->green * 65535;
388 cellicon->background.blue = color->blue * 65535;
392 if (cellicon->background_set)
394 cellicon->background_set = FALSE;
395 g_object_notify(G_OBJECT(cellicon), "background_set");
400 static void set_fg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
404 if (!cellicon->foreground_set)
406 cellicon->foreground_set = TRUE;
407 g_object_notify(G_OBJECT(cellicon), "foreground_set");
409 cellicon->foreground.red = color->red * 65535;
410 cellicon->foreground.green = color->green * 65535;
411 cellicon->foreground.blue = color->blue * 65535;
415 if (cellicon->foreground_set)
417 cellicon->foreground_set = FALSE;
418 g_object_notify(G_OBJECT(cellicon), "foreground_set");
424 gqv_cell_renderer_icon_set_property(GObject *object,
429 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
437 pixbuf = static_cast<GdkPixbuf *>(g_value_get_object(value));
438 if (pixbuf) g_object_ref(pixbuf);
439 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
440 cellicon->pixbuf = pixbuf;
447 text = cellicon->text;
448 cellicon->text = g_strdup(g_value_get_string(value));
451 g_object_notify(object, "text");
454 case PROP_BACKGROUND_GDK:
455 set_bg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
457 case PROP_FOREGROUND_GDK:
458 set_fg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
461 cellicon->focused = g_value_get_boolean(value);
463 case PROP_FIXED_WIDTH:
464 cellicon->fixed_width = g_value_get_int(value);
466 case PROP_FIXED_HEIGHT:
467 cellicon->fixed_height = g_value_get_int(value);
469 case PROP_BACKGROUND_SET:
470 cellicon->background_set = g_value_get_boolean(value);
472 case PROP_FOREGROUND_SET:
473 cellicon->foreground_set = g_value_get_boolean(value);
476 cellicon->show_text = g_value_get_boolean(value);
478 case PROP_SHOW_MARKS:
479 cellicon->show_marks = g_value_get_boolean(value);
482 cellicon->num_marks = g_value_get_int(value);
485 cellicon->marks = g_value_get_uint(value);
488 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
494 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
499 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
501 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
502 pango_layout_set_width(layout, width);
503 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
504 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
508 PangoAttrList *attr_list;
510 attr_list = pango_attr_list_new();
512 if (cellicon->foreground_set)
515 PangoAttribute *attr;
517 color = cellicon->foreground;
519 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
521 attr->start_index = 0;
522 attr->end_index = G_MAXINT;
523 pango_attr_list_insert(attr_list, attr);
526 pango_layout_set_attributes(layout, attr_list);
527 pango_attr_list_unref(attr_list);
534 * gqv_cell_renderer_icon_new:
536 * Creates a new #GQvCellRendererIcon. Adjust rendering
537 * parameters using object properties. Object properties can be set
538 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
539 * can bind a property to a value in a #GtkTreeModel. For example, you
540 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
541 * in the model, thus rendering a different image in each row of the
544 * Return value: the new cell renderer
547 gqv_cell_renderer_icon_new()
549 return static_cast<GtkCellRenderer *>(g_object_new(GQV_TYPE_CELL_RENDERER_ICON, nullptr));
552 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
554 const GdkRectangle *cell_area,
560 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
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, nullptr, &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 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
635 const GdkRectangle *,
636 const GdkRectangle *cell_area,
637 GtkCellRendererState flags)
640 GtkStyleContext *context = gtk_widget_get_style_context(widget);
641 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
644 GdkRectangle cell_rect;
650 pixbuf = cellicon->pixbuf;
651 text = cellicon->text;
652 if (!pixbuf && !text)
657 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
659 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
660 &cell_rect.x, &cell_rect.y,
661 &cell_rect.width, &cell_rect.height);
665 cell_rect.width -= xpad * 2;
666 cell_rect.height -= ypad * 2;
668 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
670 if (gtk_widget_has_focus(widget))
671 state = GTK_STATE_FLAG_SELECTED;
673 state = GTK_STATE_FLAG_ACTIVE;
677 if (!gtk_widget_is_sensitive(widget))
678 state = GTK_STATE_FLAG_INSENSITIVE;
680 state = GTK_STATE_FLAG_NORMAL;
682 gtk_style_context_set_state(context, state);
686 GdkRectangle pix_rect;
687 GdkRectangle draw_rect;
689 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
690 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
692 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
694 if (cellicon->fixed_height > 0)
696 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
700 pix_rect.y = cell_area->y + cell_rect.y;
703 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
705 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
716 if (cellicon->show_text && text)
719 PangoRectangle text_rect;
720 GdkRectangle pix_rect;
721 GdkRectangle draw_rect;
722 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
723 pango_layout_get_pixel_extents(layout, nullptr, &text_rect);
725 pix_rect.width = text_rect.width;
726 pix_rect.height = text_rect.height;
727 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
728 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
730 if (cellicon->show_marks)
732 pix_rect.y -= TOGGLE_SPACING;
735 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
737 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
739 g_object_unref(layout);
742 if (cellicon->show_marks)
744 GdkRectangle pix_rect;
745 GdkRectangle draw_rect;
748 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
749 pix_rect.height = TOGGLE_SPACING;
750 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
751 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
753 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
755 for (i = 0; i < cellicon->num_marks; i++)
757 state = static_cast<GtkStateFlags>(state & ~GTK_STATE_FLAG_CHECKED);
759 if ((cellicon->marks & (1 << i)))
760 state = static_cast<GtkStateFlags>(state | GTK_STATE_FLAG_CHECKED);
764 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
766 TOGGLE_WIDTH, TOGGLE_WIDTH);
769 gtk_style_context_save(context);
770 gtk_style_context_set_state(context, state);
772 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
774 gtk_style_context_add_class(context, "marks");
776 if (state & GTK_STATE_FLAG_CHECKED)
778 gtk_render_check(context, cr,
779 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
781 TOGGLE_WIDTH, TOGGLE_WIDTH);
783 gtk_render_frame(context, cr,
784 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
786 TOGGLE_WIDTH, TOGGLE_WIDTH);
788 if (cellicon->focused && gtk_widget_has_focus(widget))
790 gtk_render_focus(context, cr,
791 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
792 pix_rect.y, TOGGLE_WIDTH, TOGGLE_WIDTH);
794 gtk_style_context_restore(context);
801 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
805 const GdkRectangle *,
806 const GdkRectangle *cell_area,
807 GtkCellRendererState)
809 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
810 GdkEventButton *bevent = &event->button;
812 if (cellicon->show_marks &&
813 event->type == GDK_BUTTON_PRESS &&
814 !(bevent->state & GDK_SHIFT_MASK ) &&
815 !(bevent->state & GDK_CONTROL_MASK ))
818 GdkRectangle cell_rect;
823 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
825 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
826 &cell_rect.x, &cell_rect.y,
827 &cell_rect.width, &cell_rect.height);
831 cell_rect.width -= xpad * 2;
832 cell_rect.height -= ypad * 2;
834 rect.width = TOGGLE_WIDTH;
835 rect.height = TOGGLE_WIDTH;
836 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
837 for (i = 0; i < cellicon->num_marks; i++)
839 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
841 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
842 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
844 cellicon->toggled_mark = i;
845 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
852 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */