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.
21 #include "cellrenderericon.h"
23 #define FIXED_ICON_SIZE_MAX 512
26 static void gqv_cell_renderer_icon_get_property(GObject *object,
30 static void gqv_cell_renderer_icon_set_property(GObject *object,
34 static void gqv_cell_renderer_icon_init_wrapper(void *, void *);
35 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon);
36 static void gqv_cell_renderer_icon_class_init_wrapper(void *, void *);
37 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class);
38 static void gqv_cell_renderer_icon_finalize(GObject *object);
39 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
41 const GdkRectangle *cell_area,
47 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
50 const GdkRectangle *background_area,
51 const GdkRectangle *cell_area,
52 GtkCellRendererState flags);
54 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
58 const GdkRectangle *background_area,
59 const GdkRectangle *cell_area,
60 GtkCellRendererState flags);
85 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
87 static gpointer parent_class;
90 gqv_cell_renderer_icon_get_type()
92 static GType cell_icon_type = 0;
96 static const GTypeInfo cell_icon_info =
98 sizeof(GQvCellRendererIconClass), /* class_size */
99 nullptr, /* base_init */
100 nullptr, /* base_finalize */
101 static_cast<GClassInitFunc>(gqv_cell_renderer_icon_class_init_wrapper), /* class_init */
102 nullptr, /* class_finalize */
103 nullptr, /* class_data */
104 sizeof(GQvCellRendererIcon), /* instance_size */
106 reinterpret_cast<GInstanceInitFunc>(gqv_cell_renderer_icon_init_wrapper), /* instance_init */
107 nullptr, /* value_table */
110 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
111 "GQvCellRendererIcon",
112 &cell_icon_info, static_cast<GTypeFlags>(0));
115 return cell_icon_type;
119 gqv_cell_renderer_icon_init_wrapper(void *data, void *)
121 gqv_cell_renderer_icon_init(static_cast<GQvCellRendererIcon *>(data));
125 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
127 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
128 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
132 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *)
134 gqv_cell_renderer_icon_class_init(static_cast<GQvCellRendererIconClass *>(data));
138 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class)
140 GObjectClass *object_class = G_OBJECT_CLASS(icon_class);
141 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(icon_class);
143 parent_class = g_type_class_peek_parent(icon_class);
145 object_class->finalize = gqv_cell_renderer_icon_finalize;
147 object_class->get_property = gqv_cell_renderer_icon_get_property;
148 object_class->set_property = gqv_cell_renderer_icon_set_property;
150 cell_class->get_size = gqv_cell_renderer_icon_get_size;
151 cell_class->render = gqv_cell_renderer_icon_render;
152 cell_class->activate = gqv_cell_renderer_icon_activate;
154 g_object_class_install_property(object_class,
156 g_param_spec_object("pixbuf",
158 "The pixbuf to render",
162 g_object_class_install_property(object_class,
164 g_param_spec_string("text",
170 g_object_class_install_property(object_class,
172 g_param_spec_boxed("background_rgba",
174 "Background color as a GdkRGBA",
178 g_object_class_install_property(object_class,
180 g_param_spec_boxed("foreground_rgba",
182 "Foreground color as a GdkRGBA",
186 g_object_class_install_property(object_class,
188 g_param_spec_boolean("has_focus",
190 "Draw focus indicator",
194 g_object_class_install_property(object_class,
196 g_param_spec_int("fixed_width",
199 -1, FIXED_ICON_SIZE_MAX,
203 g_object_class_install_property(object_class,
205 g_param_spec_int("fixed_height",
207 "Height of icon excluding text",
208 -1, FIXED_ICON_SIZE_MAX,
212 g_object_class_install_property(object_class,
214 g_param_spec_boolean("background_set",
216 "Whether this tag affects the background color",
220 g_object_class_install_property(object_class,
222 g_param_spec_boolean("foreground_set",
224 "Whether this tag affects the foreground color",
228 g_object_class_install_property(object_class,
230 g_param_spec_boolean("show_text",
232 "Whether the text is displayed",
236 g_object_class_install_property(object_class,
238 g_param_spec_boolean("show_marks",
240 "Whether the marks are displayed",
244 g_object_class_install_property(object_class,
246 g_param_spec_int("num_marks",
253 g_object_class_install_property(object_class,
255 g_param_spec_uint("marks",
262 g_object_class_install_property(object_class,
264 g_param_spec_uint("toggled_mark",
270 toggle_cell_signals[TOGGLED] =
271 g_signal_new("toggled",
272 G_OBJECT_CLASS_TYPE (object_class),
274 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
276 g_cclosure_marshal_VOID__STRING,
283 gqv_cell_renderer_icon_finalize(GObject *object)
285 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
287 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
289 g_free(cellicon->text);
291 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
295 gqv_cell_renderer_icon_get_property(GObject *object,
300 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
305 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : nullptr);
308 g_value_set_string(value, cellicon->text);
310 case PROP_BACKGROUND_GDK:
314 color.red = cellicon->background.red / 65535;
315 color.green = cellicon->background.green / 65535;
316 color.blue = cellicon->background.blue / 65535;
318 g_value_set_boxed(value, &color);
321 case PROP_FOREGROUND_GDK:
325 color.red = cellicon->foreground.red / 65535;
326 color.green = cellicon->foreground.green / 65535;
327 color.blue = cellicon->foreground.blue / 65535;
329 g_value_set_boxed(value, &color);
333 g_value_set_boolean(value, cellicon->focused);
335 case PROP_FIXED_WIDTH:
336 g_value_set_int(value, cellicon->fixed_width);
338 case PROP_FIXED_HEIGHT:
339 g_value_set_int(value, cellicon->fixed_height);
341 case PROP_BACKGROUND_SET:
342 g_value_set_boolean(value, cellicon->background_set);
344 case PROP_FOREGROUND_SET:
345 g_value_set_boolean(value, cellicon->foreground_set);
348 g_value_set_boolean(value, cellicon->show_text);
350 case PROP_SHOW_MARKS:
351 g_value_set_boolean(value, cellicon->show_marks);
354 g_value_set_int(value, cellicon->num_marks);
357 g_value_set_uint(value, cellicon->marks);
360 g_value_set_uint(value, cellicon->toggled_mark);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
368 static void set_bg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
372 if (!cellicon->background_set)
374 cellicon->background_set = TRUE;
375 g_object_notify(G_OBJECT(cellicon), "background_set");
378 cellicon->background.red = color->red * 65535;
379 cellicon->background.green = color->green * 65535;
380 cellicon->background.blue = color->blue * 65535;
384 if (cellicon->background_set)
386 cellicon->background_set = FALSE;
387 g_object_notify(G_OBJECT(cellicon), "background_set");
392 static void set_fg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
396 if (!cellicon->foreground_set)
398 cellicon->foreground_set = TRUE;
399 g_object_notify(G_OBJECT(cellicon), "foreground_set");
401 cellicon->foreground.red = color->red * 65535;
402 cellicon->foreground.green = color->green * 65535;
403 cellicon->foreground.blue = color->blue * 65535;
407 if (cellicon->foreground_set)
409 cellicon->foreground_set = FALSE;
410 g_object_notify(G_OBJECT(cellicon), "foreground_set");
416 gqv_cell_renderer_icon_set_property(GObject *object,
421 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
429 pixbuf = static_cast<GdkPixbuf *>(g_value_get_object(value));
430 if (pixbuf) g_object_ref(pixbuf);
431 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
432 cellicon->pixbuf = pixbuf;
439 text = cellicon->text;
440 cellicon->text = g_strdup(g_value_get_string(value));
443 g_object_notify(object, "text");
446 case PROP_BACKGROUND_GDK:
447 set_bg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
449 case PROP_FOREGROUND_GDK:
450 set_fg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
453 cellicon->focused = g_value_get_boolean(value);
455 case PROP_FIXED_WIDTH:
456 cellicon->fixed_width = g_value_get_int(value);
458 case PROP_FIXED_HEIGHT:
459 cellicon->fixed_height = g_value_get_int(value);
461 case PROP_BACKGROUND_SET:
462 cellicon->background_set = g_value_get_boolean(value);
464 case PROP_FOREGROUND_SET:
465 cellicon->foreground_set = g_value_get_boolean(value);
468 cellicon->show_text = g_value_get_boolean(value);
470 case PROP_SHOW_MARKS:
471 cellicon->show_marks = g_value_get_boolean(value);
474 cellicon->num_marks = g_value_get_int(value);
477 cellicon->marks = g_value_get_uint(value);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
486 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
491 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
493 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
494 pango_layout_set_width(layout, width);
495 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
496 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
500 PangoAttrList *attr_list;
502 attr_list = pango_attr_list_new();
504 if (cellicon->foreground_set)
507 PangoAttribute *attr;
509 color = cellicon->foreground;
511 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
513 attr->start_index = 0;
514 attr->end_index = G_MAXINT;
515 pango_attr_list_insert(attr_list, attr);
518 pango_layout_set_attributes(layout, attr_list);
519 pango_attr_list_unref(attr_list);
526 * gqv_cell_renderer_icon_new:
528 * Creates a new #GQvCellRendererIcon. Adjust rendering
529 * parameters using object properties. Object properties can be set
530 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
531 * can bind a property to a value in a #GtkTreeModel. For example, you
532 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
533 * in the model, thus rendering a different image in each row of the
536 * Return value: the new cell renderer
539 gqv_cell_renderer_icon_new()
541 return static_cast<GtkCellRenderer *>(g_object_new(GQV_TYPE_CELL_RENDERER_ICON, nullptr));
544 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
546 const GdkRectangle *cell_area,
552 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
556 gfloat xalign, yalign;
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, nullptr, &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;
622 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
625 const GdkRectangle *,
626 const GdkRectangle *cell_area,
627 GtkCellRendererState flags)
630 GtkStyleContext *context = gtk_widget_get_style_context(widget);
631 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
634 GdkRectangle cell_rect;
639 pixbuf = cellicon->pixbuf;
640 text = cellicon->text;
641 if (!pixbuf && !text)
646 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
648 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
649 &cell_rect.x, &cell_rect.y,
650 &cell_rect.width, &cell_rect.height);
654 cell_rect.width -= xpad * 2;
655 cell_rect.height -= ypad * 2;
657 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
659 if (gtk_widget_has_focus(widget))
660 state = GTK_STATE_FLAG_SELECTED;
662 state = GTK_STATE_FLAG_ACTIVE;
666 if (!gtk_widget_is_sensitive(widget))
667 state = GTK_STATE_FLAG_INSENSITIVE;
669 state = GTK_STATE_FLAG_NORMAL;
671 gtk_style_context_set_state(context, state);
675 GdkRectangle pix_rect;
676 GdkRectangle draw_rect;
678 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
679 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
681 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
683 if (cellicon->fixed_height > 0)
685 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
689 pix_rect.y = cell_area->y + cell_rect.y;
692 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
694 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
705 if (cellicon->show_text && text)
708 PangoRectangle text_rect;
709 GdkRectangle pix_rect;
710 GdkRectangle draw_rect;
711 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
712 pango_layout_get_pixel_extents(layout, nullptr, &text_rect);
714 pix_rect.width = text_rect.width;
715 pix_rect.height = text_rect.height;
716 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
717 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
719 if (cellicon->show_marks)
721 pix_rect.y -= TOGGLE_SPACING;
724 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
726 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
728 g_object_unref(layout);
731 if (cellicon->show_marks)
733 GdkRectangle pix_rect;
734 GdkRectangle draw_rect;
737 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
738 pix_rect.height = TOGGLE_SPACING;
739 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
740 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
742 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
744 for (i = 0; i < cellicon->num_marks; i++)
746 state = static_cast<GtkStateFlags>(state & ~GTK_STATE_FLAG_CHECKED);
748 if ((cellicon->marks & (1 << i)))
749 state = static_cast<GtkStateFlags>(state | GTK_STATE_FLAG_CHECKED);
753 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
755 TOGGLE_WIDTH, TOGGLE_WIDTH);
758 gtk_style_context_save(context);
759 gtk_style_context_set_state(context, state);
761 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
763 gtk_style_context_add_class(context, "marks");
765 if (state & GTK_STATE_FLAG_CHECKED)
767 gtk_render_check(context, cr,
768 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
770 TOGGLE_WIDTH, TOGGLE_WIDTH);
772 gtk_render_frame(context, cr,
773 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
775 TOGGLE_WIDTH, TOGGLE_WIDTH);
777 if (cellicon->focused && gtk_widget_has_focus(widget))
779 gtk_render_focus(context, cr,
780 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
781 pix_rect.y, TOGGLE_WIDTH, TOGGLE_WIDTH);
783 gtk_style_context_restore(context);
790 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
794 const GdkRectangle *,
795 const GdkRectangle *cell_area,
796 GtkCellRendererState)
798 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
799 GdkEventButton *bevent = &event->button;
801 if (cellicon->show_marks &&
802 event->type == GDK_BUTTON_PRESS &&
803 !(bevent->state & GDK_SHIFT_MASK ) &&
804 !(bevent->state & GDK_CONTROL_MASK ))
807 GdkRectangle cell_rect;
811 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
813 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
814 &cell_rect.x, &cell_rect.y,
815 &cell_rect.width, &cell_rect.height);
819 cell_rect.width -= xpad * 2;
820 cell_rect.height -= ypad * 2;
822 rect.width = TOGGLE_WIDTH;
823 rect.height = TOGGLE_WIDTH;
824 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
825 for (i = 0; i < cellicon->num_marks; i++)
827 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
829 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
830 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
832 cellicon->toggled_mark = i;
833 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
840 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */