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