clang-tidy: modernize-redundant-void-arg
[geeqie.git] / src / cellrenderericon.cc
1 /*
2  * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "main.h"
21 #include "cellrenderericon.h"
22
23 #define FIXED_ICON_SIZE_MAX 512
24
25
26 static void gqv_cell_renderer_icon_get_property(GObject         *object,
27                                                 guint           param_id,
28                                                 GValue          *value,
29                                                 GParamSpec      *pspec);
30 static void gqv_cell_renderer_icon_set_property(GObject         *object,
31                                                 guint           param_id,
32                                                 const GValue    *value,
33                                                 GParamSpec      *pspec);
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,
40                                             GtkWidget          *widget,
41                                             const GdkRectangle *cell_area,
42                                             gint               *x_offset,
43                                             gint               *y_offset,
44                                             gint               *width,
45                                             gint               *height);
46
47 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
48                                            cairo_t *cr,
49                                            GtkWidget *widget,
50                                            const GdkRectangle *background_area,
51                                            const GdkRectangle *cell_area,
52                                            GtkCellRendererState flags);
53
54 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer      *cell,
55                                                 GdkEvent             *event,
56                                                 GtkWidget            *widget,
57                                                 const gchar          *path,
58                                                 const GdkRectangle   *background_area,
59                                                 const GdkRectangle   *cell_area,
60                                                 GtkCellRendererState  flags);
61 enum {
62   TOGGLED,
63   LAST_SIGNAL
64 };
65
66 enum {
67         PROP_ZERO,
68         PROP_PIXBUF,
69         PROP_TEXT,
70         PROP_BACKGROUND_GDK,
71         PROP_FOREGROUND_GDK,
72         PROP_FOCUSED,
73         PROP_FIXED_WIDTH,
74         PROP_FIXED_HEIGHT,
75
76         PROP_BACKGROUND_SET,
77         PROP_FOREGROUND_SET,
78         PROP_SHOW_TEXT,
79         PROP_SHOW_MARKS,
80         PROP_NUM_MARKS,
81         PROP_MARKS,
82         PROP_TOGGLED
83 };
84
85 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
86
87 static gpointer parent_class;
88
89 GType
90 gqv_cell_renderer_icon_get_type()
91 {
92         static GType cell_icon_type = 0;
93
94         if (!cell_icon_type)
95                 {
96                 static const GTypeInfo cell_icon_info =
97                         {
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 */
105                         0,              /* n_preallocs */
106                         reinterpret_cast<GInstanceInitFunc>(gqv_cell_renderer_icon_init_wrapper), /* instance_init */
107                         nullptr,                /* value_table */
108                         };
109
110                 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
111                                                         "GQvCellRendererIcon",
112                                                         &cell_icon_info, static_cast<GTypeFlags>(0));
113                 }
114
115         return cell_icon_type;
116 }
117
118 static void
119 gqv_cell_renderer_icon_init_wrapper(void *data, void *)
120 {
121         gqv_cell_renderer_icon_init(static_cast<GQvCellRendererIcon *>(data));
122 }
123
124 static void
125 gqv_cell_renderer_icon_init(GQvCellRendererIcon *cellicon)
126 {
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);
129 }
130
131 static void
132 gqv_cell_renderer_icon_class_init_wrapper(void *data, void *)
133 {
134         gqv_cell_renderer_icon_class_init(static_cast<GQvCellRendererIconClass *>(data));
135 }
136
137 static void
138 gqv_cell_renderer_icon_class_init(GQvCellRendererIconClass *icon_class)
139 {
140         GObjectClass *object_class = G_OBJECT_CLASS(icon_class);
141         GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(icon_class);
142
143         parent_class = g_type_class_peek_parent(icon_class);
144
145         object_class->finalize = gqv_cell_renderer_icon_finalize;
146
147         object_class->get_property = gqv_cell_renderer_icon_get_property;
148         object_class->set_property = gqv_cell_renderer_icon_set_property;
149
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;
153
154         g_object_class_install_property(object_class,
155                                         PROP_PIXBUF,
156                                         g_param_spec_object("pixbuf",
157                                                         "Pixbuf Object",
158                                                         "The pixbuf to render",
159                                                         GDK_TYPE_PIXBUF,
160                                                         G_PARAM_READWRITE));
161
162         g_object_class_install_property(object_class,
163                                         PROP_TEXT,
164                                         g_param_spec_string("text",
165                                                         "Text",
166                                                         "Text to render",
167                                                         nullptr,
168                                                         G_PARAM_READWRITE));
169
170         g_object_class_install_property(object_class,
171                                         PROP_BACKGROUND_GDK,
172                                         g_param_spec_boxed("background_rgba",
173                                                         "Background color",
174                                                         "Background color as a GdkRGBA",
175                                                         GDK_TYPE_RGBA,
176                                                         G_PARAM_READWRITE));
177
178         g_object_class_install_property(object_class,
179                                         PROP_FOREGROUND_GDK,
180                                         g_param_spec_boxed("foreground_rgba",
181                                                         "Foreground color",
182                                                         "Foreground color as a GdkRGBA",
183                                                         GDK_TYPE_RGBA,
184                                                         G_PARAM_READWRITE));
185
186         g_object_class_install_property(object_class,
187                                         PROP_FOCUSED,
188                                         g_param_spec_boolean("has_focus",
189                                                         "Focus",
190                                                         "Draw focus indicator",
191                                                         FALSE,
192                                                         G_PARAM_READWRITE));
193
194         g_object_class_install_property(object_class,
195                                         PROP_FIXED_WIDTH,
196                                         g_param_spec_int("fixed_width",
197                                                         "Fixed width",
198                                                         "Width of cell",
199                                                         -1, FIXED_ICON_SIZE_MAX,
200                                                         -1,
201                                                         G_PARAM_READWRITE));
202
203         g_object_class_install_property(object_class,
204                                         PROP_FIXED_HEIGHT,
205                                         g_param_spec_int("fixed_height",
206                                                         "Fixed height",
207                                                         "Height of icon excluding text",
208                                                         -1, FIXED_ICON_SIZE_MAX,
209                                                         -1,
210                                                         G_PARAM_READWRITE));
211
212         g_object_class_install_property(object_class,
213                                         PROP_BACKGROUND_SET,
214                                         g_param_spec_boolean("background_set",
215                                                         "Background set",
216                                                         "Whether this tag affects the background color",
217                                                         FALSE,
218                                                         G_PARAM_READWRITE));
219
220         g_object_class_install_property(object_class,
221                                         PROP_FOREGROUND_SET,
222                                         g_param_spec_boolean("foreground_set",
223                                                         "Foreground set",
224                                                         "Whether this tag affects the foreground color",
225                                                         FALSE,
226                                                         G_PARAM_READWRITE));
227
228         g_object_class_install_property(object_class,
229                                         PROP_SHOW_TEXT,
230                                         g_param_spec_boolean("show_text",
231                                                         "Show text",
232                                                         "Whether the text is displayed",
233                                                         TRUE,
234                                                         G_PARAM_READWRITE));
235
236         g_object_class_install_property(object_class,
237                                         PROP_SHOW_MARKS,
238                                         g_param_spec_boolean("show_marks",
239                                                         "Show marks",
240                                                         "Whether the marks are displayed",
241                                                         TRUE,
242                                                         G_PARAM_READWRITE));
243
244         g_object_class_install_property(object_class,
245                                         PROP_NUM_MARKS,
246                                         g_param_spec_int("num_marks",
247                                                         "Number of marks",
248                                                         "Number of marks",
249                                                         0, 32,
250                                                         6,
251                                                         G_PARAM_READWRITE));
252
253         g_object_class_install_property(object_class,
254                                         PROP_MARKS,
255                                         g_param_spec_uint("marks",
256                                                         "Marks",
257                                                         "Marks bit array",
258                                                         0, 0xffffffff,
259                                                         0,
260                                                         G_PARAM_READWRITE));
261
262         g_object_class_install_property(object_class,
263                                         PROP_TOGGLED,
264                                         g_param_spec_uint("toggled_mark",
265                                                         "Toggled mark",
266                                                         "Toggled mark",
267                                                         0, 32,
268                                                         0,
269                                                         G_PARAM_READWRITE));
270         toggle_cell_signals[TOGGLED] =
271                 g_signal_new("toggled",
272                 G_OBJECT_CLASS_TYPE (object_class),
273                 G_SIGNAL_RUN_LAST,
274                 G_STRUCT_OFFSET (GQvCellRendererIconClass, toggled),
275                 nullptr, nullptr,
276                 g_cclosure_marshal_VOID__STRING,
277                 G_TYPE_NONE, 1,
278                 G_TYPE_STRING);
279
280 }
281
282 static void
283 gqv_cell_renderer_icon_finalize(GObject *object)
284 {
285         GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
286
287         if (cellicon->pixbuf) g_object_unref(cellicon->pixbuf);
288
289         g_free(cellicon->text);
290
291         (*(G_OBJECT_CLASS(parent_class))->finalize)(object);
292 }
293
294 static void
295 gqv_cell_renderer_icon_get_property(GObject     *object,
296                                     guint       param_id,
297                                     GValue      *value,
298                                     GParamSpec  *pspec)
299 {
300         GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
301
302         switch (param_id)
303         {
304         case PROP_PIXBUF:
305                 g_value_set_object(value, cellicon->pixbuf ? G_OBJECT(cellicon->pixbuf) : nullptr);
306                 break;
307         case PROP_TEXT:
308                 g_value_set_string(value, cellicon->text);
309                 break;
310         case PROP_BACKGROUND_GDK:
311                 {
312                 GdkRGBA color;
313
314                 color.red = cellicon->background.red / 65535;
315                 color.green = cellicon->background.green / 65535;
316                 color.blue = cellicon->background.blue / 65535;
317
318                 g_value_set_boxed(value, &color);
319                 }
320                 break;
321         case PROP_FOREGROUND_GDK:
322                 {
323                 GdkRGBA color;
324
325                 color.red = cellicon->foreground.red / 65535;
326                 color.green = cellicon->foreground.green / 65535;
327                 color.blue = cellicon->foreground.blue / 65535;
328
329                 g_value_set_boxed(value, &color);
330                 }
331                 break;
332         case PROP_FOCUSED:
333                 g_value_set_boolean(value, cellicon->focused);
334                 break;
335         case PROP_FIXED_WIDTH:
336                 g_value_set_int(value, cellicon->fixed_width);
337                 break;
338         case PROP_FIXED_HEIGHT:
339                 g_value_set_int(value, cellicon->fixed_height);
340                 break;
341         case PROP_BACKGROUND_SET:
342                 g_value_set_boolean(value, cellicon->background_set);
343                 break;
344         case PROP_FOREGROUND_SET:
345                 g_value_set_boolean(value, cellicon->foreground_set);
346                 break;
347         case PROP_SHOW_TEXT:
348                 g_value_set_boolean(value, cellicon->show_text);
349                 break;
350         case PROP_SHOW_MARKS:
351                 g_value_set_boolean(value, cellicon->show_marks);
352                 break;
353         case PROP_NUM_MARKS:
354                 g_value_set_int(value, cellicon->num_marks);
355                 break;
356         case PROP_MARKS:
357                 g_value_set_uint(value, cellicon->marks);
358                 break;
359         case PROP_TOGGLED:
360                 g_value_set_uint(value, cellicon->toggled_mark);
361                 break;
362         default:
363                 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
364                 break;
365         }
366 }
367
368 static void set_bg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
369 {
370         if (color)
371                 {
372                 if (!cellicon->background_set)
373                         {
374                         cellicon->background_set = TRUE;
375                         g_object_notify(G_OBJECT(cellicon), "background_set");
376                         }
377
378                 cellicon->background.red = color->red * 65535;
379                 cellicon->background.green = color->green * 65535;
380                 cellicon->background.blue = color->blue * 65535;
381                 }
382         else
383                 {
384                 if (cellicon->background_set)
385                         {
386                         cellicon->background_set = FALSE;
387                         g_object_notify(G_OBJECT(cellicon), "background_set");
388                         }
389                 }
390 }
391
392 static void set_fg_color(GQvCellRendererIcon *cellicon, GdkRGBA *color)
393 {
394         if (color)
395                 {
396                 if (!cellicon->foreground_set)
397                         {
398                         cellicon->foreground_set = TRUE;
399                         g_object_notify(G_OBJECT(cellicon), "foreground_set");
400                         }
401                 cellicon->foreground.red = color->red * 65535;
402                 cellicon->foreground.green = color->green * 65535;
403                 cellicon->foreground.blue = color->blue * 65535;
404                 }
405         else
406                 {
407                 if (cellicon->foreground_set)
408                         {
409                         cellicon->foreground_set = FALSE;
410                         g_object_notify(G_OBJECT(cellicon), "foreground_set");
411                         }
412                 }
413 }
414
415 static void
416 gqv_cell_renderer_icon_set_property(GObject             *object,
417                                     guint               param_id,
418                                     const GValue        *value,
419                                     GParamSpec          *pspec)
420 {
421         GQvCellRendererIcon *cellicon = GQV_CELL_RENDERER_ICON(object);
422
423         switch (param_id)
424         {
425         case PROP_PIXBUF:
426                 {
427                 GdkPixbuf *pixbuf;
428
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;
433                 }
434                 break;
435         case PROP_TEXT:
436                 {
437                 gchar *text;
438
439                 text = cellicon->text;
440                 cellicon->text = g_strdup(g_value_get_string(value));
441                 g_free(text);
442
443                 g_object_notify(object, "text");
444                 }
445                 break;
446         case PROP_BACKGROUND_GDK:
447                 set_bg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
448                 break;
449         case PROP_FOREGROUND_GDK:
450                 set_fg_color(cellicon, static_cast<GdkRGBA *>(g_value_get_boxed(value)));
451                 break;
452         case PROP_FOCUSED:
453                 cellicon->focused = g_value_get_boolean(value);
454                 break;
455         case PROP_FIXED_WIDTH:
456                 cellicon->fixed_width = g_value_get_int(value);
457                 break;
458         case PROP_FIXED_HEIGHT:
459                 cellicon->fixed_height = g_value_get_int(value);
460                 break;
461         case PROP_BACKGROUND_SET:
462                 cellicon->background_set = g_value_get_boolean(value);
463                 break;
464         case PROP_FOREGROUND_SET:
465                 cellicon->foreground_set = g_value_get_boolean(value);
466                 break;
467         case PROP_SHOW_TEXT:
468                 cellicon->show_text = g_value_get_boolean(value);
469                 break;
470         case PROP_SHOW_MARKS:
471                 cellicon->show_marks = g_value_get_boolean(value);
472                 break;
473         case PROP_NUM_MARKS:
474                 cellicon->num_marks = g_value_get_int(value);
475                 break;
476         case PROP_MARKS:
477                 cellicon->marks = g_value_get_uint(value);
478                 break;
479         default:
480                 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
481                 break;
482         }
483 }
484
485 static PangoLayout *
486 gqv_cell_renderer_icon_get_layout(GQvCellRendererIcon *cellicon, GtkWidget *widget, gboolean will_render)
487 {
488         PangoLayout *layout;
489         gint width;
490
491         width = (cellicon->fixed_width > 0) ? cellicon->fixed_width * PANGO_SCALE : -1;
492
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);
497
498         if (will_render)
499                 {
500                 PangoAttrList *attr_list;
501
502                 attr_list = pango_attr_list_new();
503
504                 if (cellicon->foreground_set)
505                         {
506                         PangoColor color;
507                         PangoAttribute *attr;
508
509                         color = cellicon->foreground;
510
511                         attr = pango_attr_foreground_new(color.red, color.green, color.blue);
512
513                         attr->start_index = 0;
514                         attr->end_index = G_MAXINT;
515                         pango_attr_list_insert(attr_list, attr);
516                         }
517
518                 pango_layout_set_attributes(layout, attr_list);
519                 pango_attr_list_unref(attr_list);
520                 }
521
522         return layout;
523 }
524
525 /**
526  * gqv_cell_renderer_icon_new:
527  *
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
534  * #GtkTreeView.
535  *
536  * Return value: the new cell renderer
537  **/
538 GtkCellRenderer *
539 gqv_cell_renderer_icon_new()
540 {
541         return static_cast<GtkCellRenderer *>(g_object_new(GQV_TYPE_CELL_RENDERER_ICON, nullptr));
542 }
543
544 static void gqv_cell_renderer_icon_get_size(GtkCellRenderer    *cell,
545                                             GtkWidget          *widget,
546                                             const GdkRectangle *cell_area,
547                                             gint               *x_offset,
548                                             gint               *y_offset,
549                                             gint               *width,
550                                             gint               *height)
551 {
552         auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
553         gint calc_width;
554         gint calc_height;
555         gint xpad, ypad;
556         gfloat xalign, yalign;
557
558         gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
559         gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
560
561         if (cellicon->fixed_width > 0)
562                 {
563                 calc_width = cellicon->fixed_width;
564                 }
565         else
566                 {
567                 calc_width = (cellicon->pixbuf) ? gdk_pixbuf_get_width(cellicon->pixbuf) : 0;
568                 }
569
570         if (cellicon->fixed_height > 0)
571                 {
572                 calc_height = cellicon->fixed_height;
573                 }
574         else
575                 {
576                 calc_height = (cellicon->pixbuf) ? gdk_pixbuf_get_height(cellicon->pixbuf) : 0;
577                 }
578
579         if (cellicon->show_text && cellicon->text)
580                 {
581                 PangoLayout *layout;
582                 PangoRectangle rect;
583
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);
587
588                 calc_width = MAX(calc_width, rect.width);
589                 calc_height += rect.height;
590                 }
591
592         if (cellicon->show_marks)
593                 {
594                 calc_height += TOGGLE_SPACING;
595                 calc_width = MAX(calc_width, TOGGLE_SPACING * cellicon->num_marks);
596                 }
597
598         calc_width += xpad * 2;
599         calc_height += ypad * 2;
600
601         if (x_offset) *x_offset = 0;
602         if (y_offset) *y_offset = 0;
603
604         if (cell_area && calc_width > 0 && calc_height > 0)
605                 {
606                 if (x_offset)
607                         {
608                         *x_offset = (xalign * (cell_area->width - calc_width - 2 * xpad));
609                         *x_offset = MAX(*x_offset, 0) + xpad;
610                         }
611                 if (y_offset)
612                         {
613                         *y_offset = (yalign * (cell_area->height - calc_height - 2 * ypad));
614                         *y_offset = MAX(*y_offset, 0) + ypad;
615                         }
616                 }
617
618         if (width) *width = calc_width;
619         if (height) *height = calc_height;
620 }
621
622 static void gqv_cell_renderer_icon_render(GtkCellRenderer *cell,
623                                            cairo_t *cr,
624                                            GtkWidget *widget,
625                                            const GdkRectangle *,
626                                            const GdkRectangle *cell_area,
627                                            GtkCellRendererState flags)
628
629 {
630         GtkStyleContext *context = gtk_widget_get_style_context(widget);
631         auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
632         GdkPixbuf *pixbuf;
633         const gchar *text;
634         GdkRectangle cell_rect;
635         GtkStateFlags state;
636         gint xpad, ypad;
637
638
639         pixbuf = cellicon->pixbuf;
640         text = cellicon->text;
641         if (!pixbuf && !text)
642                 {
643                 return;
644                 }
645
646         gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
647
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);
651
652         cell_rect.x += xpad;
653         cell_rect.y += ypad;
654         cell_rect.width -= xpad * 2;
655         cell_rect.height -= ypad * 2;
656
657         if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
658                 {
659                 if (gtk_widget_has_focus(widget))
660                         state = GTK_STATE_FLAG_SELECTED;
661                 else
662                         state = GTK_STATE_FLAG_ACTIVE;
663                 }
664         else
665                 {
666                 if (!gtk_widget_is_sensitive(widget))
667                         state = GTK_STATE_FLAG_INSENSITIVE;
668                 else
669                         state = GTK_STATE_FLAG_NORMAL;
670                 }
671         gtk_style_context_set_state(context, state);
672
673         if (pixbuf)
674                 {
675                 GdkRectangle pix_rect;
676                 GdkRectangle draw_rect;
677
678                 pix_rect.width = gdk_pixbuf_get_width(pixbuf);
679                 pix_rect.height = gdk_pixbuf_get_height(pixbuf);
680
681                 pix_rect.x = cell_area->x + (cell_area->width - pix_rect.width) / 2;
682
683                 if (cellicon->fixed_height > 0)
684                         {
685                         pix_rect.y = cell_area->y + ypad + (cellicon->fixed_height - pix_rect.height) / 2;
686                         }
687                 else
688                         {
689                         pix_rect.y = cell_area->y + cell_rect.y;
690                         }
691
692                 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
693                         {
694                         gdk_cairo_set_source_pixbuf(cr, pixbuf, pix_rect.x, pix_rect.y);
695                         cairo_rectangle (cr,
696                                         draw_rect.x,
697                                         draw_rect.y,
698                                         draw_rect.width,
699                                         draw_rect.height);
700
701                         cairo_fill (cr);
702                         }
703                 }
704
705         if (cellicon->show_text && text)
706                 {
707                 PangoLayout *layout;
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);
713
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);
718
719                 if (cellicon->show_marks)
720                         {
721                         pix_rect.y -= TOGGLE_SPACING;
722                         }
723
724                 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
725                         {
726                         gtk_render_layout(context, cr, pix_rect.x - text_rect.x, pix_rect.y, layout);
727                         }
728                 g_object_unref(layout);
729                 }
730
731         if (cellicon->show_marks)
732                 {
733                 GdkRectangle pix_rect;
734                 GdkRectangle draw_rect;
735                 gint i;
736
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;
741
742                 if (gdk_rectangle_intersect(cell_area, &pix_rect, &draw_rect))
743                         {
744                         for (i = 0; i < cellicon->num_marks; i++)
745                                 {
746                                 state = static_cast<GtkStateFlags>(state & ~GTK_STATE_FLAG_CHECKED);
747
748                                 if ((cellicon->marks & (1 << i)))
749                                         state = static_cast<GtkStateFlags>(state | GTK_STATE_FLAG_CHECKED);
750                                 cairo_save (cr);
751
752                                 cairo_rectangle(cr,
753                                                 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
754                                                 pix_rect.y,
755                                                 TOGGLE_WIDTH, TOGGLE_WIDTH);
756                                 cairo_clip (cr);
757
758                                 gtk_style_context_save(context);
759                                 gtk_style_context_set_state(context, state);
760
761                                 gtk_style_context_add_class(context, GTK_STYLE_CLASS_CHECK);
762
763                                 gtk_style_context_add_class(context, "marks");
764
765                                 if (state & GTK_STATE_FLAG_CHECKED)
766                                         {
767                                         gtk_render_check(context, cr,
768                                                 pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
769                                                 pix_rect.y,
770                                                 TOGGLE_WIDTH, TOGGLE_WIDTH);
771                                         }
772                                 gtk_render_frame(context, cr,
773                                          pix_rect.x + i * TOGGLE_SPACING + (TOGGLE_WIDTH - TOGGLE_SPACING) / 2,
774                                          pix_rect.y,
775                                          TOGGLE_WIDTH, TOGGLE_WIDTH);
776
777                                 if (cellicon->focused && gtk_widget_has_focus(widget))
778                                         {
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);
782                                         }
783                                 gtk_style_context_restore(context);
784                                 cairo_restore(cr);
785                                 }
786                         }
787                 }
788 }
789
790 static gboolean gqv_cell_renderer_icon_activate(GtkCellRenderer      *cell,
791                                                 GdkEvent             *event,
792                                                 GtkWidget            *widget,
793                                                 const gchar          *path,
794                                                 const GdkRectangle   *,
795                                                 const GdkRectangle   *cell_area,
796                                                 GtkCellRendererState)
797 {
798         auto cellicon = reinterpret_cast<GQvCellRendererIcon *>(cell);
799         GdkEventButton *bevent = &event->button;
800
801         if (cellicon->show_marks &&
802             event->type == GDK_BUTTON_PRESS &&
803             !(bevent->state & GDK_SHIFT_MASK ) &&
804             !(bevent->state & GDK_CONTROL_MASK ))
805                 {
806                 GdkRectangle rect;
807                 GdkRectangle cell_rect;
808                 gint i;
809                 gint xpad, ypad;
810
811                 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
812
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);
816
817                 cell_rect.x += xpad;
818                 cell_rect.y += ypad;
819                 cell_rect.width -= xpad * 2;
820                 cell_rect.height -= ypad * 2;
821
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++)
826                         {
827                         rect.x = cell_area->x + xpad + (cell_rect.width - TOGGLE_SPACING * cellicon->num_marks + 1) / 2 + i * TOGGLE_SPACING;
828
829                         if (bevent->x >= rect.x && bevent->x < rect.x + rect.width &&
830                             bevent->y >= rect.y && bevent->y < rect.y + rect.height)
831                                 {
832                                 cellicon->toggled_mark = i;
833                                 g_signal_emit(cell, toggle_cell_signals[TOGGLED], 0, path);
834                                 break;
835                                 }
836                         }
837                 }
838         return FALSE;
839 }
840 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */