Sort headers using clang-tidy
[geeqie.git] / src / image-overlay.cc
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "image-overlay.h"
23
24 #include <config.h>
25
26 #include "collect.h"
27 #include "debug.h"
28 #include "filedata.h"
29 #include "histogram.h"
30 #include "image-load.h"
31 #include "image.h"
32 #include "img-view.h"
33 #include "intl.h"
34 #include "layout.h"
35 #include "main-defines.h"
36 #include "osd.h"
37 #include "pixbuf-renderer.h"
38 #include "pixbuf-util.h"
39 #include "slideshow.h"
40 #include "ui-fileops.h"
41
42 /*
43  *----------------------------------------------------------------------------
44  * image overlay
45  *----------------------------------------------------------------------------
46  */
47
48
49 struct OverlayStateData {
50         ImageWindow *imd;
51         ImageState changed_states;
52         NotifyType notify;
53
54         Histogram *histogram;
55
56         OsdShowFlags show;
57         OverlayRendererFlags origin;
58
59         gint ovl_info;
60
61         gint x;
62         gint y;
63
64         gint icon_time[IMAGE_OSD_COUNT];
65         gint icon_id[IMAGE_OSD_COUNT];
66
67         guint idle_id; /* event source id */
68         guint timer_id; /* event source id */
69         gulong destroy_id;
70 };
71
72
73 struct OSDIcon {
74         gboolean reset; /* reset on new image */
75         gint x;         /* x, y offset */
76         gint y;
77         gchar *key;     /* inline pixbuf */
78 };
79
80 static OSDIcon osd_icons[] = {
81         {  TRUE,   0,   0, nullptr },                   /* none */
82         {  TRUE, -10, -10, nullptr },                   /* auto rotated */
83         {  TRUE, -10, -10, nullptr },                   /* user rotated */
84         {  TRUE, -40, -10, nullptr },                   /* color embedded */
85         {  TRUE, -70, -10, nullptr },                   /* first image */
86         {  TRUE, -70, -10, nullptr },                   /* last image */
87         { FALSE, -70, -10, nullptr },                   /* osd enabled */
88         { FALSE, 0, 0, nullptr }
89 };
90
91 #define OSD_DATA "overlay-data"
92
93 enum {
94         IMAGE_OSD_DEFAULT_DURATION = 30
95 };
96
97 enum {
98         HISTOGRAM_HEIGHT = 140,
99         HISTOGRAM_WIDTH =  256
100 };
101
102 static void image_osd_timer_schedule(OverlayStateData *osd);
103
104 void set_image_overlay_template_string(gchar **template_string, const gchar *value)
105 {
106         g_assert(template_string);
107
108         g_free(*template_string);
109         *template_string = g_strdup(value);
110 }
111
112
113 void set_default_image_overlay_template_string(gchar **template_string)
114 {
115         set_image_overlay_template_string(template_string, DEFAULT_OVERLAY_INFO);
116 }
117
118 void set_image_overlay_font_string(gchar **font_string, const gchar *value)
119 {
120         g_assert(font_string);
121
122         g_free(*font_string);
123         *font_string = g_strdup(value);
124 }
125
126 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
127 {
128         OverlayStateData *osd;
129
130         if (!imd) return nullptr;
131
132         g_assert(imd->pr);
133
134         osd = static_cast<OverlayStateData *>(g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA"));
135         return osd;
136 }
137
138 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
139 {
140         g_assert(imd);
141         g_assert(imd->pr);
142         g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
143 }
144
145 /*
146  *----------------------------------------------------------------------------
147  * image histogram
148  *----------------------------------------------------------------------------
149  */
150
151
152 void image_osd_histogram_toggle_channel(ImageWindow *imd)
153 {
154         OverlayStateData *osd = image_get_osd_data(imd);
155
156         if (!osd || !osd->histogram) return;
157
158         histogram_toggle_channel(osd->histogram);
159         image_osd_update(imd);
160 }
161
162 void image_osd_histogram_toggle_mode(ImageWindow *imd)
163 {
164         OverlayStateData *osd = image_get_osd_data(imd);
165
166         if (!osd || !osd->histogram) return;
167
168         histogram_toggle_mode(osd->histogram);
169         image_osd_update(imd);
170 }
171
172 void image_osd_histogram_set_channel(ImageWindow *imd, gint chan)
173 {
174         OverlayStateData *osd = image_get_osd_data(imd);
175
176         if (!osd || !osd->histogram) return;
177
178         histogram_set_channel(osd->histogram, chan);
179         image_osd_update(imd);
180 }
181
182 void image_osd_histogram_set_mode(ImageWindow *imd, gint mode)
183 {
184         OverlayStateData *osd = image_get_osd_data(imd);
185
186         if (!osd || !osd->histogram) return;
187
188         histogram_set_mode(osd->histogram, mode);
189         image_osd_update(imd);
190 }
191
192 gint image_osd_histogram_get_channel(ImageWindow *imd)
193 {
194         OverlayStateData *osd = image_get_osd_data(imd);
195
196         if (!osd || !osd->histogram) return HCHAN_DEFAULT;
197
198         return histogram_get_channel(osd->histogram);
199 }
200
201 gint image_osd_histogram_get_mode(ImageWindow *imd)
202 {
203         OverlayStateData *osd = image_get_osd_data(imd);
204
205         if (!osd || !osd->histogram) return 0;
206
207         return histogram_get_mode(osd->histogram);
208 }
209
210 void image_osd_toggle(ImageWindow *imd)
211 {
212         OsdShowFlags show;
213         if (!imd) return;
214
215         show = image_osd_get(imd);
216         if (show == OSD_SHOW_NOTHING)
217                 {
218                 image_osd_set(imd, static_cast<OsdShowFlags>(OSD_SHOW_INFO | OSD_SHOW_STATUS));
219                 return;
220                 }
221
222         if (show & OSD_SHOW_HISTOGRAM)
223                 {
224                 image_osd_set(imd, OSD_SHOW_NOTHING);
225                 }
226         else
227                 {
228                 image_osd_set(imd, static_cast<OsdShowFlags>(show | OSD_SHOW_HISTOGRAM));
229                 }
230 }
231
232 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
233 {
234         GdkPixbuf *pixbuf = nullptr;
235         gint width;
236         gint height;
237         PangoLayout *layout;
238         const gchar *name;
239         gchar *text;
240         gboolean with_hist;
241         const HistMap *histmap = nullptr;
242         ImageWindow *imd = osd->imd;
243         FileData *fd = image_get_fd(imd);
244         PangoFontDescription *font_desc;
245
246         if (!fd) return nullptr;
247
248         name = image_get_name(imd);
249         if (name)
250                 {
251                 gint n;
252                 gint t;
253                 CollectionData *cd;
254                 CollectInfo *info;
255                 GHashTable *vars;
256
257                 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
258
259                 cd = image_get_collection(imd, &info);
260                 if (cd)
261                         {
262                         t = g_list_length(cd->list);
263                         n = g_list_index(cd->list, info) + 1;
264                         if (cd->name)
265                                 {
266                                 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
267                                         osd_template_insert(vars, "collection", remove_extension_from_path(cd->name), OSDT_FREE);
268                                 else
269                                         osd_template_insert(vars, "collection", cd->name, OSDT_NONE);
270                                 }
271                         else
272                                 {
273                                 osd_template_insert(vars, "collection", _("Untitled"), OSDT_NONE);
274                                 }
275                         }
276                 else
277                         {
278                         LayoutWindow *lw = layout_find_by_image(imd);
279                         if (lw)
280                                 {
281                                 if (lw->slideshow)
282                                         {
283                                         n = g_list_length(lw->slideshow->list_done);
284                                         t = n + g_list_length(lw->slideshow->list);
285                                         if (n == 0) n = t;
286                                         }
287                                 else
288                                         {
289                                         t = layout_list_count(lw, nullptr);
290                                         n = layout_list_get_index(lw, image_get_fd(lw->image)) + 1;
291                                         }
292                                 }
293                         else if (view_window_find_image(imd, &n, &t))
294                                 {
295                                 n++;
296                                 }
297                         else
298                                 {
299                                 t = 1;
300                                 n = 1;
301                                 }
302
303                         if (n < 1) n = 1;
304                         if (t < 1) t = 1;
305
306                         osd_template_insert(vars, "collection", nullptr, OSDT_NONE);
307                         }
308
309                 osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
310                 osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
311                 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
312                 osd_template_insert(vars, "path", const_cast<gchar *>(image_get_path(imd)), OSDT_NONE);
313                 osd_template_insert(vars, "date", imd->image_fd ? (const_cast<gchar *>(text_from_time(imd->image_fd->date))) : "", OSDT_NONE);
314                 osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
315                 osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
316
317                 if (!imd->unknown)
318                         {
319                         gint w;
320                         gint h;
321                         GdkPixbuf *load_pixbuf = image_loader_get_pixbuf(imd->il);
322
323                         if (imd->delay_flip &&
324                             imd->il && load_pixbuf &&
325                             image_get_pixbuf(imd) != load_pixbuf)
326                                 {
327                                 w = gdk_pixbuf_get_width(load_pixbuf);
328                                 h = gdk_pixbuf_get_height(load_pixbuf);
329                                 }
330                         else
331                                 {
332                                 image_get_image_size(imd, &w, &h);
333                                 }
334
335
336                         osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
337                         osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
338                         osd_template_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h), OSDT_FREE);
339                         }
340                 else
341                         {
342                         osd_template_insert(vars, "width", nullptr, OSDT_NONE);
343                         osd_template_insert(vars, "height", nullptr, OSDT_NONE);
344                         osd_template_insert(vars, "res", nullptr, OSDT_NONE);
345                         }
346
347                 text = image_osd_mkinfo(options->image_overlay.template_string, imd->image_fd, vars);
348                 g_hash_table_destroy(vars);
349
350         } else {
351                 /* When does this occur ?? */
352                 text = g_markup_escape_text(_("Untitled"), -1);
353         }
354
355         with_hist = ((osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram);
356         if (with_hist)
357                 {
358                 histmap = histmap_get(imd->image_fd);
359                 if (!histmap)
360                         {
361                         histmap_start_idle(imd->image_fd);
362                         with_hist = FALSE;
363                         }
364                 }
365
366
367         {
368                 gint active_marks = 0;
369                 gint mark;
370                 gchar *text2;
371
372                 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
373                         {
374                         active_marks += file_data_get_mark(fd, mark);
375                         }
376
377                 if (active_marks > 0)
378                         {
379                         GString *buf = g_string_sized_new(strlen(text) + 1 + FILEDATA_MARKS_SIZE * 2);
380
381                         if (*text)
382                                 {
383                                 g_string_append_printf(buf, "%s\n", text);
384                                 }
385
386                         for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
387                                 {
388                                 g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + (mark < 9 ? mark : -1) );
389                                 }
390
391                         g_free(text);
392                         text = g_string_free(buf, FALSE);
393                         }
394
395                 if (with_hist)
396                         {
397                         gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
398                         if (*text)
399                                 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
400                         else
401                                 text2 = g_strdup(escaped_histogram_label);
402                         g_free(escaped_histogram_label);
403                         g_free(text);
404                         text = text2;
405                         }
406         }
407
408         font_desc = pango_font_description_from_string(options->image_overlay.font);
409         layout = gtk_widget_create_pango_layout(imd->pr, nullptr);
410         pango_layout_set_font_description(layout, font_desc);
411
412         pango_layout_set_markup(layout, text, -1);
413         g_free(text);
414
415         pango_layout_get_pixel_size(layout, &width, &height);
416         /* with empty text width is set to 0, but not height) */
417         if (width == 0)
418                 height = 0;
419         else if (height == 0)
420                 width = 0;
421         if (width > 0) width += 10;
422         if (height > 0) height += 10;
423
424         if (with_hist)
425                 {
426                 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
427                 height += HISTOGRAM_HEIGHT + 5;
428                 }
429
430         if (width > 0 && height > 0)
431                 {
432                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
433                 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, options->image_overlay.background_red, options->image_overlay.background_green,
434                                                                                                                         options->image_overlay.background_blue, options->image_overlay.background_alpha);
435                 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
436                 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
437                 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
438                 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
439                 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
440                 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
441                 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
442
443                 if (with_hist)
444                         {
445                         gint x = 5;
446                         gint y = height - HISTOGRAM_HEIGHT - 5;
447                         gint w = width - 10;
448
449                         pixbuf_set_rect_fill(pixbuf, x, y, w, HISTOGRAM_HEIGHT, 220, 220, 220, 210);
450                         histogram_draw(osd->histogram, histmap, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
451                         }
452                 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, options->image_overlay.text_red, options->image_overlay.text_green,
453                                                                                                                         options->image_overlay.text_blue, options->image_overlay.text_alpha);
454         }
455
456         g_object_unref(G_OBJECT(layout));
457
458         return pixbuf;
459 }
460
461 /**
462  * @brief Create non-standard icons for the OSD
463  * @param flag
464  * @returns
465  *
466  * IMAGE_OSD_COLOR
467  * \image html image-osd-color.png
468  * IMAGE_OSD_FIRST
469  * \image html image-osd-first.png
470  * IMAGE_OSD_ICON
471  * \image html image-osd-icon.png
472  * IMAGE_OSD_LAST
473  * \image html image-osd-last.png
474  * IMAGE_OSD_ROTATE_AUTO
475  * \image html image-osd-rotate-auto.png
476  *
477  */
478 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
479 {
480         static GdkPixbuf **icons = nullptr;
481         GdkPixbuf *icon = nullptr;
482
483         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
484         if (icons[flag]) return icons[flag];
485
486         if (osd_icons[flag].key)
487                 {
488                 icon = pixbuf_inline(osd_icons[flag].key);
489                 }
490
491         if (!icon)
492                 {
493                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
494                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
495                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
496                 switch (flag)
497                         {
498                         case IMAGE_OSD_ROTATE_AUTO:
499                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
500                                                 0, 0, 0, 255,
501                                                 3, 0, 3, 0);
502                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
503                                                      20, 9, 14, 15, 14, 3,
504                                                      0, 0, 0, 255);
505                                 break;
506                         case IMAGE_OSD_ROTATE_USER:
507                                 break;
508                         case IMAGE_OSD_COLOR:
509                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
510                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
511                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
512                                 break;
513                         case IMAGE_OSD_FIRST:
514                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
515                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
516                                                      12, 5, 18, 11, 6, 11,
517                                                      0, 0, 0, 255);
518                                 break;
519                         case IMAGE_OSD_LAST:
520                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
521                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
522                                                      12, 18, 6, 12, 18, 12,
523                                                      0, 0, 0, 255);
524                                 break;
525                         case IMAGE_OSD_ICON:
526                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
527                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
528                                 break;
529                         default:
530                                 break;
531                         }
532                 }
533
534         icons[flag] = icon;
535
536         return icon;
537 }
538
539 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
540                               OverlayRendererFlags flags)
541 {
542         return pixbuf_renderer_overlay_add(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, x, y, flags);
543 }
544
545 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
546 {
547         pixbuf_renderer_overlay_set(reinterpret_cast<PixbufRenderer *>(imd->pr), id, pixbuf, x, y);
548 }
549
550 static void image_overlay_remove(ImageWindow *imd, gint id)
551 {
552         pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), id);
553 }
554
555 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
556 {
557         GdkPixbuf *pixbuf;
558
559         if (osd->icon_id[flag]) return;
560
561         pixbuf = image_osd_icon_pixbuf(flag);
562         if (!pixbuf) return;
563
564         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
565                                                osd_icons[flag].x, osd_icons[flag].y,
566                                                OVL_RELATIVE);
567 }
568
569 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
570 {
571         if (osd->icon_id[flag])
572                 {
573                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
574                 osd->icon_id[flag] = 0;
575                 }
576 }
577
578 static void image_osd_icons_reset_time(OverlayStateData *osd)
579 {
580         gint i;
581
582         for (i = 0; i < IMAGE_OSD_COUNT; i++)
583                 {
584                 if (osd_icons[i].reset)
585                         {
586                         osd->icon_time[i] = 0;
587                         }
588                 }
589 }
590
591 static void image_osd_icons_update(OverlayStateData *osd)
592 {
593         gint i;
594
595         for (i = 0; i < IMAGE_OSD_COUNT; i++)
596                 {
597                 if (osd->icon_time[i] > 0)
598                         {
599                         image_osd_icon_show(osd, static_cast<ImageOSDFlag>(i));
600                         }
601                 else
602                         {
603                         image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
604                         }
605                 }
606 }
607
608 static void image_osd_icons_hide(OverlayStateData *osd)
609 {
610         gint i;
611
612         for (i = 0; i < IMAGE_OSD_COUNT; i++)
613                 {
614                 image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
615                 }
616 }
617
618 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
619 {
620         if (osd->ovl_info == 0)
621                 {
622                 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, osd->origin);
623                 }
624         else
625                 {
626                 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
627                 }
628 }
629
630 static void image_osd_info_hide(OverlayStateData *osd)
631 {
632         if (osd->ovl_info == 0) return;
633
634         image_overlay_remove(osd->imd, osd->ovl_info);
635         osd->ovl_info = 0;
636 }
637
638 static gboolean image_osd_update_cb(gpointer data)
639 {
640         auto osd = static_cast<OverlayStateData *>(data);
641
642         if (osd->show & OSD_SHOW_INFO)
643                 {
644                 /* redraw when the image was changed,
645                    with histogram we have to redraw also when loading is finished */
646                 if (osd->changed_states & IMAGE_STATE_IMAGE ||
647                     (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
648                     osd->notify & NOTIFY_HISTMAP)
649                         {
650                         GdkPixbuf *pixbuf;
651
652                         pixbuf = image_osd_info_render(osd);
653                         if (pixbuf)
654                                 {
655                                 image_osd_info_show(osd, pixbuf);
656                                 g_object_unref(pixbuf);
657                                 }
658                         else
659                                 {
660                                 image_osd_info_hide(osd);
661                                 }
662                         }
663                 }
664         else
665                 {
666                 image_osd_info_hide(osd);
667                 }
668
669         if (osd->show & OSD_SHOW_STATUS)
670                 {
671                 if (osd->changed_states & IMAGE_STATE_IMAGE)
672                         image_osd_icons_reset_time(osd);
673
674                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
675                         {
676                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
677                         image_osd_timer_schedule(osd);
678                         }
679
680                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
681                         {
682                         gint n = 0;
683
684                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
685                                 {
686                                 n = 1;
687                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
688                                 }
689
690                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
691                         image_osd_timer_schedule(osd);
692                         }
693
694                 image_osd_icons_update(osd);
695                 }
696         else
697                 {
698                 image_osd_icons_hide(osd);
699                 }
700
701         osd->changed_states = IMAGE_STATE_NONE;
702         osd->notify = static_cast<NotifyType>(0);
703         osd->idle_id = 0;
704         return G_SOURCE_REMOVE;
705 }
706
707 static void image_osd_update_schedule(OverlayStateData *osd, gboolean force)
708 {
709         if (force) osd->changed_states = static_cast<ImageState>(osd->changed_states | IMAGE_STATE_IMAGE);
710
711         if (!osd->idle_id)
712                 {
713                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, nullptr);
714                 }
715 }
716
717 void image_osd_update(ImageWindow *imd)
718 {
719         OverlayStateData *osd = image_get_osd_data(imd);
720
721         if (!osd) return;
722
723         image_osd_update_schedule(osd, TRUE);
724 }
725
726 static gboolean image_osd_timer_cb(gpointer data)
727 {
728         auto osd = static_cast<OverlayStateData *>(data);
729         gboolean done = TRUE;
730         gboolean changed = FALSE;
731         gint i;
732
733         for (i = 0; i < IMAGE_OSD_COUNT; i++)
734                 {
735                 if (osd->icon_time[i] > 1)
736                         {
737                         osd->icon_time[i]--;
738                         if (osd->icon_time[i] < 2)
739                                 {
740                                 osd->icon_time[i] = 0;
741                                 changed = TRUE;
742                                 }
743                         else
744                                 {
745                                 done = FALSE;
746                                 }
747                         }
748                 }
749
750         if (changed) image_osd_update_schedule(osd, FALSE);
751
752         if (done)
753                 {
754                 osd->timer_id = 0;
755                 return FALSE;
756                 }
757
758         return TRUE;
759 }
760
761 static void image_osd_timer_schedule(OverlayStateData *osd)
762 {
763         if (!osd->timer_id)
764                 {
765                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
766                 }
767 }
768
769 static void image_osd_state_cb(ImageWindow *, ImageState state, gpointer data)
770 {
771         auto osd = static_cast<OverlayStateData *>(data);
772
773         osd->changed_states = static_cast<ImageState>(osd->changed_states | state);
774         image_osd_update_schedule(osd, FALSE);
775 }
776
777 static void image_osd_notify_cb(FileData *fd, NotifyType type, gpointer data)
778 {
779         auto osd = static_cast<OverlayStateData *>(data);
780
781         if ((type & (NOTIFY_HISTMAP)) && osd->imd && fd == osd->imd->image_fd)
782                 {
783                 DEBUG_1("Notify osd: %s %04x", fd->path, type);
784                 osd->notify = static_cast<NotifyType>(osd->notify | type);
785                 image_osd_update_schedule(osd, FALSE);
786                 }
787 }
788
789
790 static void image_osd_free(OverlayStateData *osd)
791 {
792         if (!osd) return;
793
794         if (osd->idle_id) g_source_remove(osd->idle_id);
795         if (osd->timer_id) g_source_remove(osd->timer_id);
796
797         file_data_unregister_notify_func(image_osd_notify_cb, osd);
798
799         if (osd->imd)
800                 {
801                 image_set_osd_data(osd->imd, nullptr);
802                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
803
804                 image_set_state_func(osd->imd, nullptr, nullptr);
805
806                 image_osd_info_hide(osd);
807                 image_osd_icons_hide(osd);
808                 }
809
810         if (osd->histogram) histogram_free(osd->histogram);
811
812         g_free(osd);
813 }
814
815 static void image_osd_destroy_cb(GtkWidget *, gpointer data)
816 {
817         auto osd = static_cast<OverlayStateData *>(data);
818
819         osd->imd = nullptr;
820         image_osd_free(osd);
821 }
822
823 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
824 {
825         OverlayStateData *osd = image_get_osd_data(imd);
826
827         if (!osd)
828                 {
829                 osd = g_new0(OverlayStateData, 1);
830                 osd->imd = imd;
831                 osd->show = OSD_SHOW_NOTHING;
832                 osd->x = options->image_overlay.x;
833                 osd->y = options->image_overlay.y;
834                 osd->origin = OVL_RELATIVE;
835
836                 osd->histogram = histogram_new();
837
838                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
839                                                    G_CALLBACK(image_osd_destroy_cb), osd);
840                 image_set_osd_data(imd, osd);
841
842                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
843                 file_data_register_notify_func(image_osd_notify_cb, osd, NOTIFY_PRIORITY_LOW);
844                 }
845
846         if (show & OSD_SHOW_STATUS)
847                 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
848
849         if (show != osd->show)
850                 image_osd_update_schedule(osd, TRUE);
851
852         osd->show = show;
853 }
854
855 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
856 {
857         if (!imd) return;
858
859         image_osd_enable(imd, show);
860 }
861
862 OsdShowFlags image_osd_get(ImageWindow *imd)
863 {
864         OverlayStateData *osd = image_get_osd_data(imd);
865
866         return osd ? osd->show : OSD_SHOW_NOTHING;
867 }
868
869 Histogram *image_osd_get_histogram(ImageWindow *imd)
870 {
871         OverlayStateData *osd = image_get_osd_data(imd);
872
873         return osd ? osd->histogram : nullptr;
874 }
875
876 void image_osd_copy_status(ImageWindow *src, ImageWindow *dest)
877 {
878         Histogram *h_src;
879         Histogram *h_dest;
880         image_osd_set(dest, image_osd_get(src));
881
882         h_src = image_osd_get_histogram(src);
883         h_dest = image_osd_get_histogram(dest);
884
885         h_dest->histogram_mode = h_src->histogram_mode;
886         h_dest->histogram_channel = h_src->histogram_channel;
887
888 }
889
890 /* duration:
891     0 = hide
892     1 = show
893    2+ = show for duration tenths of a second
894    -1 = use default duration
895  */
896 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
897 {
898         OverlayStateData *osd = image_get_osd_data(imd);
899
900         if (!osd) return;
901
902         if (flag >= IMAGE_OSD_COUNT) return;
903         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
904         if (duration > 1) duration += 1;
905
906         osd->icon_time[flag] = duration;
907
908         image_osd_update_schedule(osd, FALSE);
909         image_osd_timer_schedule(osd);
910 }
911 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */