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