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"
28 FIXED_ICON_SIZE_MAX = 512
32 static void gqv_cell_renderer_icon_get_property(GObject *object,
36 static void gqv_cell_renderer_icon_set_property(GObject *object,
40 static void gqv_cell_renderer_icon_init_wrapper(void *, void *);
41 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon);
42 static void gqv_cell_renderer_icon_class_init_wrapper(void *, void *);
43 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_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 *cell_area,
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 gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
64 const GdkRectangle *background_area,
65 const GdkRectangle *cell_area,
66 GtkCellRendererState flags);
91 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
93 static gpointer parent_class;
96 gqv_cell_renderer_icon_get_type()
98 static GType cell_icon_type = 0;
102 static const GTypeInfo cell_icon_info =
104 sizeof(GQvCellRendererIconClass), /* class_size */
105 nullptr, /* base_init */
106 nullptr, /* base_finalize */
107 static_cast<GClassInitFunc>(gqv_cell_renderer_icon_class_init_wrapper), /* class_init */
108 nullptr, /* class_finalize */
109 nullptr, /* class_data */
110 sizeof(GQvCellRendererIcon), /* instance_size */
112 reinterpret_cast<GInstanceInitFunc>(gqv_cell_renderer_icon_init_wrapper), /* instance_init */
113 nullptr, /* value_table */
116 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
117 "GQvCellRendererIcon",
118 &cell_icon_info, static_cast<GTypeFlags>(0));
121 return cell_icon_type;
125 gqv_cell_renderer_icon_init_wrapper(void *data, void *)
127 gqv_cell_renderer_icon_init(static_cast<GQvCellRendererIcon *>(data));
131 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
133 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
134 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
138 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *)
140 gqv_cell_renderer_icon_class_init(static_cast<GQvCellRendererIconClass *>(data));
144 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class)
146 GObjectClass *object_class = G_OBJECT_CLASS(icon_class);
147 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(icon_class);
149 parent_class = g_type_class_peek_parent(icon_class);
151 object_class->finalize = gqv_cell_renderer_icon_finalize;
153 object_class->get_property = gqv_cell_renderer_icon_get_property;
154 object_class->set_property = gqv_cell_renderer_icon_set_property;
156 cell_class->get_size = gqv_cell_renderer_icon_get_size;
157 cell_class->render = gqv_cell_renderer_icon_render;
158 cell_class->activate = gqv_cell_renderer_icon_activate;
160 g_object_class_install_property(object_class,
162 g_param_spec_object("pixbuf",
164 "The pixbuf to render",
168 g_object_class_install_property(object_class,
170 g_param_spec_string("text",
176 g_object_class_install_property(object_class,
178 g_param_spec_boxed("background_rgba",
180 "Background color as a GdkRGBA",
184 g_object_class_install_property(object_class,
186 g_param_spec_boxed("foreground_rgba",
188 "Foreground color as a GdkRGBA",
192 g_object_class_install_property(object_class,
194 g_param_spec_boolean("has_focus",
196 "Draw focus indicator",
200 g_object_class_install_property(object_class,
202 g_param_spec_int("fixed_width",
205 -1, FIXED_ICON_SIZE_MAX,
209 g_object_class_install_property(object_class,
211 g_param_spec_int("fixed_height",
213 "Height of icon excluding text",
214 -1, FIXED_ICON_SIZE_MAX,
218 g_object_class_install_property(object_class,
220 g_param_spec_boolean("background_set",
222 "Whether this tag affects the background color",
226 g_object_class_install_property(object_class,
228 g_param_spec_boolean("foreground_set",
230 "Whether this tag affects the foreground color",
234 g_object_class_install_property(object_class,
236 g_param_spec_boolean("show_text",
238 "Whether the text is displayed",
242 g_object_class_install_property(object_class,
244 g_param_spec_boolean("show_marks",
246 "Whether the marks are displayed",
250 g_object_class_install_property(object_class,
252 g_param_spec_int("num_marks",
259 g_object_class_install_property(object_class,
261 g_param_spec_uint("marks",
268 g_object_class_install_property(object_class,
270 g_param_spec_uint("toggled_mark",
276 toggle_cell_signals[TOGGLED] =
277 g_signal_new("toggled",
278 G_OBJECT_CLASS_TYPE (object_class),
280 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
282 g_cclosure_marshal_VOID__STRING,
289 gqv_cell_renderer_icon_finalize(GObject *object)
291 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
293 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
295 g_free(cellicon->text);
297 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
301 gqv_cell_renderer_icon_get_property(GObject *object,
306 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
311 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : nullptr);
314 g_value_set_string(value, cellicon->text);
316 case PROP_BACKGROUND_GDK:
320 color.red = cellicon->background.red / 65535.0;
321 color.green = cellicon->background.green / 65535.0;
322 color.blue = cellicon->background.blue / 65535.0;
324 g_value_set_boxed(value, &color);
327 case PROP_FOREGROUND_GDK:
331 color.red = cellicon->foreground.red / 65535.0;
332 color.green = cellicon->foreground.green / 65535.0;
333 color.blue = cellicon->foreground.blue / 65535.0;
335 g_value_set_boxed(value, &color);
339 g_value_set_boolean(value, cellicon->focused);
341 case PROP_FIXED_WIDTH:
342 g_value_set_int(value, cellicon->fixed_width);
344 case PROP_FIXED_HEIGHT:
345 g_value_set_int(value, cellicon->fixed_height);
347 case PROP_BACKGROUND_SET:
348 g_value_set_boolean(value, cellicon->background_set);
350 case PROP_FOREGROUND_SET:
351 g_value_set_boolean(value, cellicon->foreground_set);
354 g_value_set_boolean(value, cellicon->show_text);
356 case PROP_SHOW_MARKS:
357 g_value_set_boolean(value, cellicon->show_marks);
360 g_value_set_int(value, cellicon->num_marks);
363 g_value_set_uint(value, cellicon->marks);
366 g_value_set_uint(value, cellicon->toggled_mark);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
374 static void set_bg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
378 if (!cellicon->background_set)
380 cellicon->background_set = TRUE;
381 g_object_notify(G_OBJECT(cellicon), "background_set");
384 cellicon->background.red = color->red * 65535;
385 cellicon->background.green = color->green * 65535;
386 cellicon->background.blue = color->blue * 65535;
390 if (cellicon->background_set)
392 cellicon->background_set = FALSE;
393 g_object_notify(G_OBJECT(cellicon), "background_set");
398 static void set_fg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
402 if (!cellicon->foreground_set)
404 cellicon->foreground_set = TRUE;
405 g_object_notify(G_OBJECT(cellicon), "foreground_set");
407 cellicon->foreground.red = color->red * 65535;
408 cellicon->foreground.green = color->green * 65535;
409 cellicon->foreground.blue = color->blue * 65535;
413 if (cellicon->foreground_set)
415 cellicon->foreground_set = FALSE;
416 g_object_notify(G_OBJECT(cellicon), "foreground_set");
422 gqv_cell_renderer_icon_set_property(GObject *object,
427 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
435 pixbuf = static_cast<GdkPixbuf *>(g_value_get_object(value));
436 if (pixbuf) g_object_ref(pixbuf);
437 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
438 cellicon->pixbuf = pixbuf;
445 text = cellicon->text;
446 cellicon->text = g_strdup(g_value_get_string(value));
449 g_object_notify(object, "text");
452 case PROP_BACKGROUND_GDK:
453 set_bg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
455 case PROP_FOREGROUND_GDK:
456 set_fg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
459 cellicon->focused = g_value_get_boolean(value);
461 case PROP_FIXED_WIDTH:
462 cellicon->fixed_width = g_value_get_int(value);
464 case PROP_FIXED_HEIGHT:
465 cellicon->fixed_height = g_value_get_int(value);
467 case PROP_BACKGROUND_SET:
468 cellicon->background_set = g_value_get_boolean(value);
470 case PROP_FOREGROUND_SET:
471 cellicon->foreground_set = g_value_get_boolean(value);
474 cellicon->show_text = g_value_get_boolean(value);
476 case PROP_SHOW_MARKS:
477 cellicon->show_marks = g_value_get_boolean(value);
480 cellicon->num_marks = g_value_get_int(value);
483 cellicon->marks = g_value_get_uint(value);
486 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
492 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
497 width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
499 layout = gtk_widget_create_pango_layout(widget, cellicon->text);
500 pango_layout_set_width(layout, width);
501 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
502 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
506 PangoAttrList *attr_list;
508 attr_list = pango_attr_list_new();
510 if (cellicon->foreground_set)
513 PangoAttribute *attr;
515 color = cellicon->foreground;
517 attr = pango_attr_foreground_new(color.red, color.green, color.blue);
519 attr->start_index = 0;
520 attr->end_index = G_MAXINT;
521 pango_attr_list_insert(attr_list, attr);
524 pango_layout_set_attributes(layout, attr_list);
525 pango_attr_list_unref(attr_list);
532 * gqv_cell_renderer_icon_new:
534 * Creates a new #GQvCellRendererIcon. Adjust rendering
535 * parameters using object properties. Object properties can be set
536 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
537 * can bind a property to a value in a #GtkTreeModel. For example, you
538 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
539 * in the model, thus rendering a different image in each row of the
542 * Return value: the new cell renderer
545 gqv_cell_renderer_icon_new()
547 return static_cast<GtkCellRenderer *>(g_object_new(GQV_TYPE_CELL_RENDERER_ICON, nullptr));
550 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
552 const GdkRectangle *cell_area,
558 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
566 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
567 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
569 if (cellicon->fixed_width > 0)
571 calc_width = cellicon->fixed_width;
575 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
578 if (cellicon->fixed_height > 0)
580 calc_height = cellicon->fixed_height;
584 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
587 if (cellicon->show_text && cellicon->text)
592 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, FALSE);
593 pango_layout_get_pixel_extents(layout, nullptr, &rect);
594 g_object_unref(layout);
596 calc_width = MAX(calc_width, rect.width);
597 calc_height += rect.height;
600 if (cellicon->show_marks)
602 calc_height += TOGGLE_SPACING;
603 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
606 calc_width += xpad * 2;
607 calc_height += ypad * 2;
609 if (x_offset) *x_offset = 0;
610 if (y_offset) *y_offset = 0;
612 if (cell_area && calc_width > 0 && calc_height > 0)
616 *x_offset = (xalign * (cell_area->width - calc_width - 2 * xpad));
617 *x_offset = MAX(*x_offset, 0) + xpad;
621 *y_offset = (yalign * (cell_area->height - calc_height - 2 * ypad));
622 *y_offset = MAX(*y_offset, 0) + ypad;
626 if (width) *width = calc_width;
627 if (height) *height = calc_height;
630 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
633 const GdkRectangle *,
634 const GdkRectangle *cell_area,
635 GtkCellRendererState flags)
638 GtkStyleContext *context = gtk_widget_get_style_context(widget);
639 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
642 GdkRectangle cell_rect;
648 pixbuf = cellicon->pixbuf;
649 text = cellicon->text;
650 if (!pixbuf && !text)
655 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
657 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
658 &cell_rect.x, &cell_rect.y,
659 &cell_rect.width, &cell_rect.height);
663 cell_rect.width -= xpad * 2;
664 cell_rect.height -= ypad * 2;
666 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
668 if (gtk_widget_has_focus(widget))
669 state = GTK_STATE_FLAG_SELECTED;
671 state = GTK_STATE_FLAG_ACTIVE;
675 if (!gtk_widget_is_sensitive(widget))
676 state = GTK_STATE_FLAG_INSENSITIVE;
678 state = GTK_STATE_FLAG_NORMAL;
680 gtk_style_context_set_state(context, state);
684 GdkRectangle pix_rect;
685 GdkRectangle draw_rect;
687 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
688 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
690 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
692 if (cellicon->fixed_height > 0)
694 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
698 pix_rect.y = cell_area->y + cell_rect.y;
701 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
703 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
714 if (cellicon->show_text && text)
717 PangoRectangle text_rect;
718 GdkRectangle pix_rect;
719 GdkRectangle draw_rect;
720 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
721 pango_layout_get_pixel_extents(layout, nullptr, &text_rect);
723 pix_rect.width = text_rect.width;
724 pix_rect.height = text_rect.height;
725 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
726 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
728 if (cellicon->show_marks)
730 pix_rect.y -= TOGGLE_SPACING;
733 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
735 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
737 g_object_unref(layout);
740 if (cellicon->show_marks)
742 GdkRectangle pix_rect;
743 GdkRectangle draw_rect;
746 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
747 pix_rect.height = TOGGLE_SPACING;
748 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
749 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
751 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
753 for (i = 0; i < cellicon->num_marks; i++)
755 state = static_cast<GtkStateFlags>(state & ~GTK_STATE_FLAG_CHECKED);
757 if ((cellicon->marks & (1 << i)))
758 state = static_cast<GtkStateFlags>(state | GTK_STATE_FLAG_CHECKED);
762 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
764 TOGGLE_WIDTH, TOGGLE_WIDTH);
767 gtk_style_context_save(context);
768 gtk_style_context_set_state(context, state);
770 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
772 gtk_style_context_add_class(context, "marks");
774 if (state & GTK_STATE_FLAG_CHECKED)
776 gtk_render_check(context, cr,
777 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
779 TOGGLE_WIDTH, TOGGLE_WIDTH);
781 gtk_render_frame(context, cr,
782 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
784 TOGGLE_WIDTH, TOGGLE_WIDTH);
786 if (cellicon->focused && gtk_widget_has_focus(widget))
788 gtk_render_focus(context, cr,
789 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2.0,
790 pix_rect.y, TOGGLE_WIDTH, TOGGLE_WIDTH);
792 gtk_style_context_restore(context);
799 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
803 const GdkRectangle *,
804 const GdkRectangle *cell_area,
805 GtkCellRendererState)
807 auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
808 GdkEventButton *bevent = &event->button;
810 if (cellicon->show_marks &&
811 event->type == GDK_BUTTON_PRESS &&
812 !(bevent->state & GDK_SHIFT_MASK ) &&
813 !(bevent->state & GDK_CONTROL_MASK ))
816 GdkRectangle cell_rect;
821 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
823 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
824 &cell_rect.x, &cell_rect.y,
825 &cell_rect.width, &cell_rect.height);
829 cell_rect.width -= xpad * 2;
830 cell_rect.height -= ypad * 2;
832 rect.width = TOGGLE_WIDTH;
833 rect.height = TOGGLE_WIDTH;
834 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
835 for (i = 0; i < cellicon->num_marks; i++)
837 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
839 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
840 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
842 cellicon->toggled_mark = i;
843 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
850 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */