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