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