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