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