Merge view_overlay_toggle() and layout_image_overlay_toggle() into new image_osd_togg...
[geeqie.git] / src / image-overlay.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include "main.h"
14 #include "image-overlay.h"
15
16 #include "collect.h"
17 #include "exif.h"
18 #include "filelist.h"
19 #include "image.h"
20 #include "img-view.h"
21 #include "layout.h"
22 #include "pixbuf-renderer.h"
23 #include "pixbuf_util.h"
24 #include "histogram.h"
25
26
27 /*
28  *----------------------------------------------------------------------------
29  * image overlay
30  *----------------------------------------------------------------------------
31  */
32
33 typedef struct _OverlayStateData OverlayStateData;
34 struct _OverlayStateData {
35         ImageWindow *imd;
36         ImageState changed_states;
37
38         gint show_info;
39         gint show_status;
40
41         gint ovl_info;
42
43         gint icon_time[IMAGE_OSD_COUNT];
44         gint icon_id[IMAGE_OSD_COUNT];
45
46         gint idle_id;
47         gint timer_id;
48         gulong destroy_id;
49 };
50
51
52 typedef struct _OSDIcon OSDIcon;
53 struct _OSDIcon {
54         gint reset;     /* reset on new image */
55         gint x;         /* x, y offset */
56         gint y;
57         gchar *key;     /* inline pixbuf */
58 };
59
60 static OSDIcon osd_icons[] = {
61         {  TRUE,   0,   0, NULL },                      /* none */
62         {  TRUE, -10, -10, NULL },                      /* auto rotated */
63         {  TRUE, -10, -10, NULL },                      /* user rotated */
64         {  TRUE, -40, -10, NULL },                      /* color embedded */
65         {  TRUE, -70, -10, NULL },                      /* first image */
66         {  TRUE, -70, -10, NULL },                      /* last image */
67         { FALSE, -70, -10, NULL },                      /* osd enabled */
68         { FALSE, 0, 0, NULL }
69 };
70
71 #define OSD_DATA "overlay-data"
72
73 #define OSD_INFO_X 10
74 #define OSD_INFO_Y -10
75
76 #define IMAGE_OSD_DEFAULT_DURATION 30
77
78 #define HISTOGRAM_HEIGHT 140
79
80 static void image_osd_timer_schedule(OverlayStateData *osd);
81
82
83 void set_default_image_overlay_template_string(ConfOptions *options)
84 {
85         if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
86         options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
87 }
88
89 /*
90  *----------------------------------------------------------------------------
91  * image histogram
92  *----------------------------------------------------------------------------
93  */
94
95
96 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
97 {
98         imd->histogram_enabled = !!(x);
99         if (imd->histogram_enabled && !imd->histogram)
100                 imd->histogram = histogram_new();
101 }
102
103 gint image_osd_histogram_onoff_status(ImageWindow *imd)
104 {
105       return imd->histogram_enabled;
106 }
107
108 void image_osd_histogram_chan_toggle(ImageWindow *imd)
109 {
110         if (imd->histogram)
111                 histogram_set_channel(imd->histogram, (histogram_get_channel(imd->histogram) +1)%HCHAN_COUNT);
112 }
113
114 void image_osd_histogram_log_toggle(ImageWindow *imd)
115 {
116         if (imd->histogram)
117                 histogram_set_mode(imd->histogram, !histogram_get_mode(imd->histogram));
118 }
119
120 void image_osd_toggle(ImageWindow *imd)
121 {
122         if (image_osd_get(imd, NULL, NULL))
123                 {
124                 if (image_osd_histogram_onoff_status(imd))
125                         {
126                         image_osd_histogram_onoff_toggle(imd, 0);
127                         image_osd_update(imd);
128                         }
129                 else
130                         {
131                         image_osd_set(imd, FALSE, FALSE);
132                         }
133                 }
134         else
135                 {
136                 image_osd_set(imd, TRUE, TRUE);
137                 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
138                 image_osd_histogram_onoff_toggle(imd, 1);
139                 image_osd_update(imd);
140                 }
141 }
142
143 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
144 {
145         gchar delim = '%', imp = '|', sep[] = " - ";
146         gchar *start, *end;
147         gint pos, prev;
148         gint last;
149         gchar *name, *data;
150         GString *new;
151         gchar *ret;
152         ExifData *exif;
153
154         if (!str || !*str) return g_strdup("");
155
156         new = g_string_new(str);
157
158         exif = exif_read_fd(imd->image_fd);
159         prev = 0;
160         last = FALSE;
161
162         while (TRUE)
163                 {
164                 gint was_digit = 0;
165                 gint limit = 0;
166                 gchar *trunc = NULL;
167                 gchar *p;
168
169                 start = strchr(new->str, delim);
170                 if (!start)
171                         break;
172                 end = strchr(start+1, delim);
173                 if (!end)
174                         break;
175
176                 for (p = end; p > start; p--)
177                         {
178                         if (*p == ':' && was_digit)
179                                 {
180                                 trunc = p;
181                                 break;
182                                 }
183                         was_digit = g_ascii_isdigit(*p);
184                         }
185
186                 if (trunc) limit = atoi(trunc+1);
187
188                 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
189
190                 pos = start-new->str;
191                 data = g_strdup(g_hash_table_lookup(vars, name));
192                 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
193                 if (!data && exif)
194                         data = exif_get_data_as_text(exif, name);
195                 if (data && *data && limit > 0 && strlen(data) > limit + 3)
196                         {
197                         gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
198                         g_free(data);
199                         data = new_data;
200                         }
201
202                 g_string_erase(new, pos, end-start+1);
203                 if (data)
204                         g_string_insert(new, pos, data);
205                 if (pos-prev == 2 && new->str[pos-1] == imp)
206                         {
207                         g_string_erase(new, --pos, 1);
208                         if (last && data)
209                                 {
210                                 g_string_insert(new, pos, sep);
211                                 pos += strlen(sep);
212                                 }
213                         }
214
215                 prev = data ? pos+strlen(data)-1 : pos-1;
216                 last = data ? TRUE : last;
217                 g_free(name);
218                 g_free(data);
219                 }
220
221         /* search and destroy empty lines */
222         end = new->str;
223         while ((start = strchr(end, '\n')))
224                 {
225                 end = start;
226                 while (*++(end) == '\n')
227                         ;
228                 g_string_erase(new, start-new->str, end-start-1);
229                 }
230
231         g_strchomp(new->str);
232
233         ret = new->str;
234         g_string_free(new, FALSE);
235
236         return ret;
237 }
238
239 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
240 {
241         GdkPixbuf *pixbuf = NULL;
242         gint width, height;
243         PangoLayout *layout;
244         const gchar *name;
245         gchar *name_escaped;
246         gchar *text;
247         gchar *size;
248         gint n, t;
249         CollectionData *cd;
250         CollectInfo *info;
251         GdkPixbuf *imgpixbuf = NULL;
252         LayoutWindow *lw = NULL;
253         gint with_hist = 0;
254         gchar *ct;
255         gint w, h;
256         GHashTable *vars;
257
258         vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
259
260         name = image_get_name(imd);
261         if (name)
262                 {
263                 name_escaped = g_markup_escape_text(name, -1);
264                 }
265         else
266                 {
267                 name_escaped = NULL;
268                 }
269
270         cd = image_get_collection(imd, &info);
271         if (cd)
272                 {
273                 gchar *buf;
274
275                 t = g_list_length(cd->list);
276                 n = g_list_index(cd->list, info) + 1;
277                 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
278                 ct = g_strdup_printf("<i>%s</i>\n", buf);
279                 g_free(buf);
280                 }
281         else
282                 {
283                 lw = layout_find_by_image(imd);
284                 if (lw)
285                         {
286                         if (lw->slideshow)
287                                 {
288                                 n = g_list_length(lw->slideshow->list_done);
289                                 t = n + g_list_length(lw->slideshow->list);
290                                 if (n == 0) n = t;
291                                 }
292                         else
293                                 {
294                                 t = layout_list_count(lw, NULL);
295                                 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
296                                 }
297                         }
298                 else if (view_window_find_image(imd, &n, &t))
299                         {
300                         n++;
301                         }
302                 else
303                         {
304                         t = 1;
305                         n = 1;
306                         }
307
308                 if (n < 1) n = 1;
309                 if (t < 1) t = 1;
310
311                 ct = g_strdup("");
312                 }
313
314         size = text_from_size_abrev(imd->size);
315         if (!imd->unknown)
316                 {
317                 if (imd->delay_flip &&
318                     imd->il && imd->il->pixbuf &&
319                     image_get_pixbuf(imd) != imd->il->pixbuf)
320                         {
321                         w = gdk_pixbuf_get_width(imd->il->pixbuf);
322                         h = gdk_pixbuf_get_height(imd->il->pixbuf);
323                         imgpixbuf = imd->il->pixbuf;
324                         }
325                 else
326                         {
327                         pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
328                         imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
329                         }
330         
331                 if (imgpixbuf && imd->histogram_enabled && imd->histogram
332                               && (!imd->il || imd->il->done))
333                         with_hist=1;
334
335                 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
336                 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
337                 g_hash_table_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h));
338                 }
339
340         g_hash_table_insert(vars, "collection", g_strdup(ct));
341         g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
342         g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
343         g_hash_table_insert(vars, "name", g_strdup(name_escaped));
344         g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
345         g_hash_table_insert(vars, "size", g_strdup(size));
346         g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
347
348         if (!name_escaped)
349                 {
350                 text = g_strdup_printf(_("Untitled"));
351                 }
352         else
353                 {
354                 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
355                 }
356
357         g_free(size);
358         g_free(ct);
359         g_free(name_escaped);
360         g_hash_table_destroy(vars);
361
362         {
363         FileData *fd = image_get_fd(imd);
364
365         if (fd) /* fd may be null after file deletion */
366                 {
367                 gint active_marks = 0;
368                 gint mark;
369                 gchar *text2;
370
371                 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
372                         {
373                         active_marks += fd->marks[mark];
374                         }
375
376
377                 if (active_marks > 0)
378                         {
379                         GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
380
381                         for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
382                                 {
383                                 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
384                                 }
385
386                         if (*text)
387                                 text2 = g_strdup_printf("%s\n%s", text, buf->str);
388                         else
389                                 text2 = g_strdup(buf->str);
390                         g_string_free(buf, TRUE);
391                         g_free(text);
392                         text = text2;
393                         }
394
395                 if (with_hist)
396                         {
397                         if (*text)
398                                 text2 = g_strdup_printf("%s\n%s", text, histogram_label(imd->histogram));
399                         else
400                                 text2 = g_strdup(histogram_label(imd->histogram));
401                         g_free(text);
402                         text = text2;
403                         }
404                 }
405         }
406
407         layout = gtk_widget_create_pango_layout(imd->pr, NULL);
408         pango_layout_set_markup(layout, text, -1);
409         g_free(text);
410
411         pango_layout_get_pixel_size(layout, &width, &height);
412         /* with empty text width is set to 0, but not height) */
413         if (width == 0)
414                 height = 0;
415         else if (height == 0)
416                 width = 0;
417         if (width > 0) width += 10;
418         if (height > 0) height += 10;
419
420         if (with_hist)
421                 {
422                 histogram_read(imd->histogram, imgpixbuf);
423                 if (width < 266) width = 266;
424                 height += HISTOGRAM_HEIGHT + 5;
425                 }
426
427         if (width > 0 && height > 0)
428                 {
429                 /* TODO: make osd color configurable --Zas */
430                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
431                 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
432                 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
433                 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
434                 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
435                 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
436                 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
437                 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
438                 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
439
440                 if (with_hist)
441                         histogram_draw(imd->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
442
443                 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
444         }
445
446         g_object_unref(G_OBJECT(layout));
447
448         return pixbuf;
449 }
450
451 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
452 {
453         static GdkPixbuf **icons = NULL;
454         GdkPixbuf *icon = NULL;
455
456         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
457         if (icons[flag]) return icons[flag];
458
459         if (osd_icons[flag].key)
460                 {
461                 icon = pixbuf_inline(osd_icons[flag].key);
462                 }
463
464         if (!icon)
465                 {
466                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
467                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
468                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
469                 switch (flag)
470                         {
471                         case IMAGE_OSD_ROTATE_AUTO:
472                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
473                                                 0, 0, 0, 255,
474                                                 3, 0, 3, 0);
475                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
476                                                      20, 9, 14, 15, 14, 3,
477                                                      0, 0, 0, 255);
478                                 break;
479                         case IMAGE_OSD_ROTATE_USER:
480                                 break;
481                         case IMAGE_OSD_COLOR:
482                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
483                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
484                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
485                                 break;
486                         case IMAGE_OSD_FIRST:
487                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
488                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
489                                                      12, 5, 18, 11, 6, 11,
490                                                      0, 0, 0, 255);
491                                 break;
492                         case IMAGE_OSD_LAST:
493                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
494                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
495                                                      12, 18, 6, 12, 18, 12,
496                                                      0, 0, 0, 255);
497                                 break;
498                         case IMAGE_OSD_ICON:
499                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
500                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
501                                 break;
502                         default:
503                                 break;
504                         }
505                 }
506
507         icons[flag] = icon;
508
509         return icon;
510 }
511
512 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
513 {
514         GdkPixbuf *pixbuf;
515
516         if (osd->icon_id[flag]) return;
517
518         pixbuf = image_osd_icon_pixbuf(flag);
519         if (!pixbuf) return;
520
521         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
522                                                osd_icons[flag].x, osd_icons[flag].y,
523                                                TRUE, FALSE);
524 }
525
526 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
527 {
528         if (osd->icon_id[flag])
529                 {
530                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
531                 osd->icon_id[flag] = 0;
532                 }
533 }
534
535 static gint image_osd_update_cb(gpointer data)
536 {
537         OverlayStateData *osd = data;
538
539         osd->imd->overlay_show_zoom = FALSE;
540
541         if (osd->show_info)
542                 {
543                 if (osd->changed_states & IMAGE_STATE_IMAGE)
544                         {
545                         GdkPixbuf *pixbuf;
546
547                         pixbuf = image_osd_info_render(osd->imd);
548                         if (pixbuf)
549                                 {
550                                 if (osd->ovl_info == 0)
551                                         {
552                                         osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
553                                                                           OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
554                                         }
555                                 else
556                                         {
557                                         image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
558                                         }
559                                 g_object_unref(pixbuf);
560                                 }
561                         else if (osd->ovl_info)
562                                 {
563                                 image_overlay_remove(osd->imd, osd->ovl_info);
564                                 osd->ovl_info = 0;
565                                 }
566                         }
567                 }
568         else
569                 {
570                 if (osd->ovl_info)
571                         {
572                         image_overlay_remove(osd->imd, osd->ovl_info);
573                         osd->ovl_info = 0;
574                         }
575                 }
576
577         if (osd->show_status)
578                 {
579                 gint i;
580
581                 if (osd->changed_states & IMAGE_STATE_IMAGE)
582                         {
583                         for (i = 0; i < IMAGE_OSD_COUNT; i++)
584                                 {
585                                 if (osd_icons[i].reset) osd->icon_time[i] = 0;
586                                 }
587                         }
588
589                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
590                         {
591                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
592                         image_osd_timer_schedule(osd);
593                         }
594
595                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
596                         {
597                         gint n = 0;
598
599                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
600                                 {
601                                 n = 1;
602                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
603                                 }
604
605                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
606                         image_osd_timer_schedule(osd);
607                         }
608
609                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
610                         {
611                         if (osd->icon_time[i] > 0)
612                                 {
613                                 image_osd_icon_show(osd, i);
614                                 }
615                         else
616                                 {
617                                 image_osd_icon_hide(osd, i);
618                                 }
619                         }
620                 }
621         else
622                 {
623                 gint i;
624
625                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
626                         {
627                         image_osd_icon_hide(osd, i);
628                         }
629                 }
630
631         if (osd->imd->il && osd->imd->il->done)
632                 osd->changed_states = IMAGE_STATE_NONE;
633         osd->idle_id = -1;
634         return FALSE;
635 }
636
637 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
638 {
639         if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
640
641         if (osd->idle_id == -1)
642                 {
643                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
644                 }
645 }
646
647 void image_osd_update(ImageWindow *imd)
648 {
649         OverlayStateData *osd;
650
651         if (!imd) return;
652
653         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
654         if (!osd) return;
655
656         image_osd_update_schedule(osd, TRUE);
657 }
658
659 static gint image_osd_timer_cb(gpointer data)
660 {
661         OverlayStateData *osd = data;
662         gint done = TRUE;
663         gint changed = FALSE;
664         gint i;
665
666         for (i = 0; i < IMAGE_OSD_COUNT; i++)
667                 {
668                 if (osd->icon_time[i] > 1)
669                         {
670                         osd->icon_time[i]--;
671                         if (osd->icon_time[i] < 2)
672                                 {
673                                 osd->icon_time[i] = 0;
674                                 changed = TRUE;
675                                 }
676                         else
677                                 {
678                                 done = FALSE;
679                                 }
680                         }
681                 }
682
683         if (changed) image_osd_update_schedule(osd, FALSE);
684
685         if (done)
686                 {
687                 osd->timer_id = -1;
688                 return FALSE;
689                 }
690
691         return TRUE;
692 }
693
694 static void image_osd_timer_schedule(OverlayStateData *osd)
695 {
696         if (osd->timer_id == -1)
697                 {
698                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
699                 }
700 }
701
702 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
703 {
704         OverlayStateData *osd = data;
705
706         osd->changed_states |= state;
707         image_osd_update_schedule(osd, FALSE);
708 }
709
710 static void image_osd_free(OverlayStateData *osd)
711 {
712         if (!osd) return;
713
714         if (osd->idle_id != -1) g_source_remove(osd->idle_id);
715         if (osd->timer_id != -1) g_source_remove(osd->timer_id);
716
717         if (osd->imd)
718                 {
719                 gint i;
720
721                 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
722                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
723
724                 image_set_state_func(osd->imd, NULL, NULL);
725                 image_overlay_remove(osd->imd, osd->ovl_info);
726
727                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
728                         {
729                         image_osd_icon_hide(osd, i);
730                         }
731                 }
732
733         g_free(osd);
734 }
735
736 static void image_osd_remove(ImageWindow *imd)
737 {
738         OverlayStateData *osd;
739
740         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
741         image_osd_free(osd);
742 }
743
744 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
745 {
746         OverlayStateData *osd = data;
747
748         osd->imd = NULL;
749         image_osd_free(osd);
750 }
751
752 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
753 {
754         OverlayStateData *osd;
755
756         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
757         if (!osd)
758                 {
759                 osd = g_new0(OverlayStateData, 1);
760                 osd->imd = imd;
761                 osd->idle_id = -1;
762                 osd->timer_id = -1;
763
764                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
765                                                    G_CALLBACK(image_osd_destroy_cb), osd);
766                 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
767
768                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
769                 }
770
771         if (osd->show_info != info ||
772             osd->show_status != status)
773                 {
774                 osd->show_info = info;
775                 osd->show_status = status;
776
777                 image_osd_update_schedule(osd, TRUE);
778                 }
779 }
780
781 void image_osd_set(ImageWindow *imd, gint info, gint status)
782 {
783         if (!imd) return;
784
785         if (!info && !status)
786                 {
787                 image_osd_remove(imd);
788                 return;
789                 }
790
791         image_osd_enable(imd, info, status);
792 }
793
794 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
795 {
796         OverlayStateData *osd;
797
798         if (!imd) return FALSE;
799
800         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
801         if (!osd) return FALSE;
802
803         if (info) *info = osd->show_info;
804         if (status) *status = osd->show_status;
805
806         return TRUE;
807 }
808
809 /* duration:
810     0 = hide
811     1 = show
812    2+ = show for duration tenths of a second
813    -1 = use default duration
814  */
815 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
816 {
817         OverlayStateData *osd;
818
819         if (!imd) return;
820
821         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
822         if (!osd) return;
823
824         if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
825         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
826         if (duration > 1) duration += 1;
827
828         osd->icon_time[flag] = duration;
829
830         image_osd_update_schedule(osd, FALSE);
831         image_osd_timer_schedule(osd);
832 }