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