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 <gtk/gtk.h> /* To define GTK_CHECK_VERSION */
23 #include "cellrenderericon.h"
27 #define FIXED_ICON_SIZE_MAX 512
30 static void gqv_cell_renderer_icon_get_property(GObject *object,
34 static void gqv_cell_renderer_icon_set_property(GObject *object,
38 static void gqv_cell_renderer_icon_init_wrapper(void *, void *);
39 static void gqv_cell_renderer_icon_init(GQvCellRendererIcon *celltext);
40 static void gqv_cell_renderer_icon_class_init_wrapper(void *, void *);
41 static void gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class);
42 static void gqv_cell_renderer_icon_finalize(GObject *object);
43 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
45 const GdkRectangle *rectangle,
51 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
54 const GdkRectangle *background_area,
55 const GdkRectangle *cell_area,
56 GtkCellRendererState flags);
58 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
62 const GdkRectangle *background_area,
63 const GdkRectangle *cell_area,
64 GtkCellRendererState flags);
89 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
91 static gpointer parent_class;
94 gqv_cell_renderer_icon_get_type(void)
96 static GType cell_icon_type = 0;
100 static const GTypeInfo cell_icon_info =
102 sizeof(GQvCellRendererIconClass), /* class_size */
103 NULL, /* base_init */
104 NULL, /* base_finalize */
105 (GClassInitFunc) gqv_cell_renderer_icon_class_init_wrapper, /* class_init */
106 NULL, /* class_finalize */
107 NULL, /* class_data */
108 sizeof(GQvCellRendererIcon), /* instance_size */
110 (GInstanceInitFunc) gqv_cell_renderer_icon_init_wrapper, /* instance_init */
111 NULL, /* value_table */
114 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
115 "GQvCellRendererIcon",
119 return cell_icon_type;
123 gqv_cell_renderer_icon_init_wrapper(void *data, void *UNUSED(user_data))
125 gqv_cell_renderer_icon_init(data);
129 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
131 g_object_set(G_OBJECT(cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
132 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellicon), 2, 2);
136 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *UNUSED(user_data))
138 gqv_cell_renderer_icon_class_init(data);
142 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *class)
144 GObjectClass *object_class = G_OBJECT_CLASS(class);
145 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
147 parent_class = g_type_class_peek_parent(class);
149 object_class->finalize = gqv_cell_renderer_icon_finalize;
151 object_class->get_property = gqv_cell_renderer_icon_get_property;
152 object_class->set_property = gqv_cell_renderer_icon_set_property;
154 cell_class->get_size = gqv_cell_renderer_icon_get_size;
155 cell_class->render = gqv_cell_renderer_icon_render;
156 cell_class->activate = gqv_cell_renderer_icon_activate;
158 g_object_class_install_property(object_class,
160 g_param_spec_object("pixbuf",
162 "The pixbuf to render",
166 g_object_class_install_property(object_class,
168 g_param_spec_string("text",
174 g_object_class_install_property(object_class,
176 g_param_spec_boxed("background_gdk",
178 "Background color as a GdkColor",
182 g_object_class_install_property(object_class,
184 g_param_spec_boxed("foreground_gdk",
186 "Foreground color as a GdkColor",
190 g_object_class_install_property(object_class,
192 g_param_spec_boolean("has_focus",
194 "Draw focus indicator",
198 g_object_class_install_property(object_class,
200 g_param_spec_int("fixed_width",
203 -1, FIXED_ICON_SIZE_MAX,
207 g_object_class_install_property(object_class,
209 g_param_spec_int("fixed_height",
211 "Height of icon excluding text",
212 -1, FIXED_ICON_SIZE_MAX,
216 g_object_class_install_property(object_class,
218 g_param_spec_boolean("background_set",
220 "Whether this tag affects the background color",
224 g_object_class_install_property(object_class,
226 g_param_spec_boolean("foreground_set",
228 "Whether this tag affects the foreground color",
232 g_object_class_install_property(object_class,
234 g_param_spec_boolean("show_text",
236 "Whether the text is displayed",
240 g_object_class_install_property(object_class,
242 g_param_spec_boolean("show_marks",
244 "Whether the marks are displayed",
248 g_object_class_install_property(object_class,
250 g_param_spec_int("num_marks",
257 g_object_class_install_property(object_class,
259 g_param_spec_uint("marks",
266 g_object_class_install_property(object_class,
268 g_param_spec_uint("toggled_mark",
274 toggle_cell_signals[TOGGLED] =
275 g_signal_new("toggled",
276 G_OBJECT_CLASS_TYPE (object_class),
278 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
280 g_cclosure_marshal_VOID__STRING,
287 gqv_cell_renderer_icon_finalize(GObject *object)
289 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
291 if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
293 g_free(cellicon->text);
295 (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
299 gqv_cell_renderer_icon_get_property(GObject *object,
304 GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
309 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : NULL);
312 g_value_set_string(value, cellicon->text);
314 case PROP_BACKGROUND_GDK:
318 color.red = cellicon->background.red;
319 color.green = cellicon->background.green;
320 color.blue = cellicon->background.blue;
322 g_value_set_boxed(value, &color);
325 case PROP_FOREGROUND_GDK:
329 color.red = cellicon->foreground.red;
330 color.green = cellicon->foreground.green;
331 color.blue = cellicon->foreground.blue;
333 g_value_set_boxed(value, &color);
337 g_value_set_boolean(value, cellicon->focused);
339 case PROP_FIXED_WIDTH:
340 g_value_set_int(value, cellicon->fixed_width);
342 case PROP_FIXED_HEIGHT:
343 g_value_set_int(value, cellicon->fixed_height);
345 case PROP_BACKGROUND_SET:
346 g_value_set_boolean(value, cellicon->background_set);
348 case PROP_FOREGROUND_SET:
349 g_value_set_boolean(value, cellicon->foreground_set);
352 g_value_set_boolean(value, cellicon->show_text);
354 case PROP_SHOW_MARKS:
355 g_value_set_boolean(value, cellicon->show_marks);
358 g_value_set_int(value, cellicon->num_marks);
361 g_value_set_uint(value, cellicon->marks);
364 g_value_set_uint(value, cellicon->toggled_mark);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
373 set_bg_color(GQvCellRendererIcon *cellicon,
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;
385 cellicon->background.green = color->green;
386 cellicon->background.blue = color->blue;
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,
403 if (!cellicon->foreground_set)
405 cellicon->foreground_set = TRUE;
406 g_object_notify(G_OBJECT(cellicon), "foreground_set");
409 cellicon->foreground.red = color->red;
410 cellicon->foreground.green = color->green;
411 cellicon->foreground.blue = color->blue;
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 = (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, g_value_get_boxed(value));
457 case PROP_FOREGROUND_GDK:
458 set_fg_color(cellicon, 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(void)
549 return g_object_new(GQV_TYPE_CELL_RENDERER_ICON, NULL);
552 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer *cell,
554 const GdkRectangle *cell_area,
560 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
564 gfloat xalign, yalign;
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, NULL, &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 *UNUSED(background_area),
634 const GdkRectangle *cell_area,
635 GtkCellRendererState flags)
638 GtkStyleContext *context = gtk_widget_get_style_context(widget);
639 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
642 GdkRectangle cell_rect;
647 pixbuf = cellicon->pixbuf;
648 text = cellicon->text;
650 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
652 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
653 &cell_rect.x, &cell_rect.y,
654 &cell_rect.width, &cell_rect.height);
658 cell_rect.width -= xpad * 2;
659 cell_rect.height -= ypad * 2;
661 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
663 if (gtk_widget_has_focus(widget))
664 state = GTK_STATE_FLAG_SELECTED;
666 state = GTK_STATE_FLAG_ACTIVE;
670 if (gtk_widget_get_state(widget) == GTK_STATE_INSENSITIVE)
671 state = GTK_STATE_FLAG_INSENSITIVE;
673 state = GTK_STATE_FLAG_NORMAL;
675 gtk_style_context_set_state(context, state);
679 GdkRectangle pix_rect;
680 GdkRectangle draw_rect;
682 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
683 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
685 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
687 if (cellicon->fixed_height > 0)
689 pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
693 pix_rect.y = cell_area->y + cell_rect.y;
696 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
698 gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
709 if (cellicon->show_text && text)
712 PangoRectangle text_rect;
713 GdkRectangle pix_rect;
714 GdkRectangle draw_rect;
715 layout = gqv_cell_renderer_icon_get_layout(cellicon, widget, TRUE);
716 pango_layout_get_pixel_extents(layout, NULL, &text_rect);
718 pix_rect.width = text_rect.width;
719 pix_rect.height = text_rect.height;
720 pix_rect.x = cell_area->x + xpad + (cell_rect.width - text_rect.width + 1) / 2;
721 pix_rect.y = cell_area->y + ypad + (cell_rect.height - text_rect.height);
723 if (cellicon->show_marks)
725 pix_rect.y -= TOGGLE_SPACING;
728 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
730 gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
732 g_object_unref(layout);
735 if (cellicon->show_marks)
737 GdkRectangle pix_rect;
738 GdkRectangle draw_rect;
741 pix_rect.width = TOGGLE_SPACING * cellicon->num_marks;
742 pix_rect.height = TOGGLE_SPACING;
743 pix_rect.x = cell_area->x + xpad + (cell_rect.width - pix_rect.width + 1) / 2 + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
744 pix_rect.y = cell_area->y + ypad + (cell_rect.height - pix_rect.height) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
746 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
748 for (i = 0; i < cellicon->num_marks; i++)
750 state &= ~(GTK_STATE_FLAG_CHECKED);
752 if ((cellicon->marks & (1 << i)))
753 state |= GTK_STATE_FLAG_CHECKED;
757 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
759 TOGGLE_WIDTH, TOGGLE_WIDTH);
762 gtk_style_context_save(context);
763 gtk_style_context_set_state(context, state);
765 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
767 gtk_style_context_add_class(context, "marks");
768 GtkStyleProvider *provider;
769 provider = (GtkStyleProvider *)gtk_css_provider_new();
770 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(provider),
772 "border-color: #808080;\n"
773 "border-style: solid;\n"
774 "border-width: 1px;\n"
775 "border-radius: 0px;\n"
778 gtk_style_context_add_provider(context, provider,
779 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
781 gtk_render_check(context, cr,
782 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
784 TOGGLE_WIDTH, TOGGLE_WIDTH);
785 gtk_render_frame(context, cr,
786 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
788 TOGGLE_WIDTH, TOGGLE_WIDTH);
790 if (cellicon->focused && gtk_widget_has_focus(widget))
792 gtk_render_focus(context, cr,
793 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
794 pix_rect.y, TOGGLE_WIDTH, TOGGLE_WIDTH);
796 gtk_style_context_restore(context);
803 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer *cell,
807 const GdkRectangle *UNUSED(background_area),
808 const GdkRectangle *cell_area,
809 GtkCellRendererState UNUSED(flags))
811 GQvCellRendererIcon *cellicon = (GQvCellRendererIcon *) cell;
812 GdkEventButton *bevent = &event->button;
814 if (cellicon->show_marks &&
815 event->type == GDK_BUTTON_PRESS &&
816 !(bevent->state & GDK_SHIFT_MASK ) &&
817 !(bevent->state & GDK_CONTROL_MASK ))
820 GdkRectangle cell_rect;
824 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
826 gqv_cell_renderer_icon_get_size(cell, widget, cell_area,
827 &cell_rect.x, &cell_rect.y,
828 &cell_rect.width, &cell_rect.height);
832 cell_rect.width -= xpad * 2;
833 cell_rect.height -= ypad * 2;
835 rect.width = TOGGLE_WIDTH;
836 rect.height = TOGGLE_WIDTH;
837 rect.y = cell_area->y + ypad + (cell_rect.height - TOGGLE_SPACING) + (TOGGLE_SPACING - TOGGLE_WIDTH) / 2;
838 for (i = 0; i < cellicon->num_marks; i++)
840 rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
842 if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
843 bevent->y >= rect.y && bevent->y < rect.y + rect.height)
845 cellicon->toggled_mark = i;
846 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
853 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */