use metadata_read_* functions where possible
[geeqie.git] / src / image-overlay.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 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 "histogram.h"
20 #include "image.h"
21 #include "img-view.h"
22 #include "layout.h"
23 #include "metadata.h"
24 #include "pixbuf-renderer.h"
25 #include "pixbuf_util.h"
26 #include "ui_fileops.h"
27 #include "image-load.h"
28
29 /*
30  *----------------------------------------------------------------------------
31  * image overlay
32  *----------------------------------------------------------------------------
33  */
34
35
36 typedef struct _OverlayStateData OverlayStateData;
37 struct _OverlayStateData {
38         ImageWindow *imd;
39         ImageState changed_states;
40
41         Histogram *histogram;
42
43         OsdShowFlags show;
44
45         gint ovl_info;
46         
47         gint x;
48         gint y;
49
50         gint icon_time[IMAGE_OSD_COUNT];
51         gint icon_id[IMAGE_OSD_COUNT];
52
53         gint idle_id;
54         gint timer_id;
55         gulong destroy_id;
56 };
57
58
59 typedef struct _OSDIcon OSDIcon;
60 struct _OSDIcon {
61         gint reset;     /* reset on new image */
62         gint x;         /* x, y offset */
63         gint y;
64         gchar *key;     /* inline pixbuf */
65 };
66
67 static OSDIcon osd_icons[] = {
68         {  TRUE,   0,   0, NULL },                      /* none */
69         {  TRUE, -10, -10, NULL },                      /* auto rotated */
70         {  TRUE, -10, -10, NULL },                      /* user rotated */
71         {  TRUE, -40, -10, NULL },                      /* color embedded */
72         {  TRUE, -70, -10, NULL },                      /* first image */
73         {  TRUE, -70, -10, NULL },                      /* last image */
74         { FALSE, -70, -10, NULL },                      /* osd enabled */
75         { FALSE, 0, 0, NULL }
76 };
77
78 #define OSD_DATA "overlay-data"
79
80 #define IMAGE_OSD_DEFAULT_DURATION 30
81
82 #define HISTOGRAM_HEIGHT 140
83 #define HISTOGRAM_WIDTH  256
84
85 static void image_osd_timer_schedule(OverlayStateData *osd);
86
87 void set_image_overlay_template_string(gchar **template_string, const gchar *value)
88 {
89         g_assert(template_string);
90
91         g_free(*template_string);
92         *template_string = g_strdup(value);
93 }
94
95
96 void set_default_image_overlay_template_string(gchar **template_string)
97 {
98         set_image_overlay_template_string(template_string, DEFAULT_OVERLAY_INFO);
99 }
100
101 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
102 {
103         OverlayStateData *osd;
104
105         if (!imd) return NULL;
106
107         g_assert(imd->pr);
108
109         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
110         return osd;
111 }
112
113 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
114 {
115         g_assert(imd);
116         g_assert(imd->pr);
117         g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
118 }
119
120 /*
121  *----------------------------------------------------------------------------
122  * image histogram
123  *----------------------------------------------------------------------------
124  */
125
126
127 void image_osd_histogram_chan_toggle(ImageWindow *imd)
128 {
129         OverlayStateData *osd = image_get_osd_data(imd);
130
131         if (!osd || !osd->histogram) return;
132
133         histogram_set_channel(osd->histogram, (histogram_get_channel(osd->histogram) +1)%HCHAN_COUNT);
134         image_osd_update(imd);
135 }
136
137 void image_osd_histogram_log_toggle(ImageWindow *imd)
138 {
139         OverlayStateData *osd = image_get_osd_data(imd);
140
141         if (!osd || !osd->histogram) return;
142
143         histogram_set_mode(osd->histogram, !histogram_get_mode(osd->histogram));
144         image_osd_update(imd);
145 }
146
147 void image_osd_toggle(ImageWindow *imd)
148 {
149         OverlayStateData *osd;
150
151         if (!imd) return;
152
153         osd = image_get_osd_data(imd);
154         if (!osd)
155                 {
156                 image_osd_set(imd, OSD_SHOW_INFO | OSD_SHOW_STATUS);
157                 return;
158                 }
159
160         if (osd->show != OSD_SHOW_NOTHING)
161                 {
162                 if (osd->show & OSD_SHOW_HISTOGRAM)
163                         {
164                         image_osd_set(imd, OSD_SHOW_NOTHING);
165                         }
166                 else
167                         {
168                         image_osd_set(imd, osd->show | OSD_SHOW_HISTOGRAM);
169                         }
170                 }
171 }
172
173 static gchar *keywords_to_string(FileData *fd)
174 {
175         GList *keywords;
176         GString *kwstr = NULL;
177         gchar *ret = NULL;
178
179         g_assert(fd);
180
181         keywords = metadata_read_list(fd, KEYWORD_KEY, METADATA_PLAIN);
182
183         if (keywords)
184                 {
185                 GList *work = keywords;
186
187                 while (work)
188                         {
189                         gchar *kw = work->data;
190                         work = work->next;
191                         
192                         if (!kw) continue;
193                         if (!kwstr)
194                                 kwstr = g_string_new("");
195                         else
196                                 g_string_append(kwstr, ", ");
197                         
198                         g_string_append(kwstr, kw);
199                         }
200                 string_list_free(keywords);
201                 }
202
203         if (kwstr)
204                 {
205                 ret = kwstr->str;
206                 g_string_free(kwstr, FALSE);
207                 }
208
209         return ret;
210 }
211
212 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
213 {
214         gchar delim = '%', imp = '|', sep[] = " - ";
215         gchar *start, *end;
216         guint pos, prev;
217         gboolean want_separator = FALSE;
218         gchar *name, *data;
219         GString *new;
220         gchar *ret;
221
222         if (!str || !*str) return g_strdup("");
223
224         new = g_string_new(str);
225
226         prev = 0;
227
228         while (TRUE)
229                 {
230                 guint limit = 0;
231                 gchar *trunc = NULL;
232                 gchar *limpos = NULL;
233                 gchar *extra = NULL;
234                 gchar *extrapos = NULL;
235                 gchar *p;
236
237                 start = strchr(new->str, delim);
238                 if (!start)
239                         break;
240                 end = strchr(start+1, delim);
241                 if (!end)
242                         break;
243
244                 /* Search for optionnal modifiers
245                  * %name:99:extra% -> name = "name", limit=99, extra = "extra"
246                  */
247                 for (p = start + 1; p < end; p++)
248                         {
249                         if (p[0] == ':')
250                                 {
251                                 if (g_ascii_isdigit(p[1]) && !limpos)
252                                         {
253                                         limpos = p + 1;
254                                         if (!trunc) trunc = p;
255                                         }
256                                 else
257                                         {
258                                         extrapos = p + 1;
259                                         if (!trunc) trunc = p;
260                                         break;
261                                         }
262                                 }
263                         }
264
265                 if (limpos)
266                         limit = (guint) atoi(limpos);
267
268                 if (extrapos)
269                         extra = g_strndup(extrapos, end - extrapos);
270                                         
271                 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
272                 pos = start - new->str;
273                 data = NULL;
274
275                 if (strcmp(name, "keywords") == 0)
276                         {
277                         data = keywords_to_string(imd->image_fd);
278                         }
279                 else if (strcmp(name, "comment") == 0)
280                         {
281                         data = metadata_read_string(imd->image_fd, COMMENT_KEY, METADATA_PLAIN);
282                         }
283                 else
284                         {
285                         data = g_strdup(g_hash_table_lookup(vars, name));
286                         if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
287                         if (!data)
288                                 data = metadata_read_string(imd->image_fd, name, METADATA_FORMATTED);
289                         }
290         
291                 if (data && *data && limit > 0 && strlen(data) > limit + 3)
292                         {
293                         gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
294                         g_free(data);
295                         data = new_data;
296                         }
297         
298                 if (data)
299                         {
300                         /* Since we use pango markup to display, we need to escape here */
301                         gchar *escaped = g_markup_escape_text(data, -1);
302                         g_free(data);
303                         data = escaped;
304                         }
305
306                 if (extra)
307                         {
308                         if (data && *data)
309                                 {
310                                 /* Display data between left and right parts of extra string
311                                  * the data is expressed by a '*' character. A '*' may be escaped
312                                  * by a \. You should escape all '*' characters, do not rely on the
313                                  * current implementation which only replaces the first unescaped '*'.
314                                  * If no "*" is present, the extra string is just appended to data string.
315                                  * Pango mark up is accepted in left and right parts.
316                                  * Any \n is replaced by a newline
317                                  * Examples:
318                                  * "<i>*</i>\n" -> data is displayed in italics ended with a newline
319                                  * "\n"         -> ended with newline
320                                  * "ISO *"      -> prefix data with "ISO " (ie. "ISO 100")
321                                  * "\**\*"      -> prefix data with a star, and append a star (ie. "*100*")
322                                  * "\\*"        -> prefix data with an anti slash (ie "\100")
323                                  * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
324                                  *
325                                  * FIXME: using background / foreground colors lead to weird results.
326                                  */
327                                 gchar *new_data;
328                                 gchar *left = NULL;
329                                 gchar *right = extra;
330                                 gchar *p;
331                                 guint len = strlen(extra);
332                                 
333                                 /* Search for left and right parts and unescape characters */
334                                 for (p = extra; *p; p++, len--)
335                                         if (p[0] == '\\')
336                                                 {
337                                                 if (p[1] == 'n')
338                                                         {
339                                                         memmove(p+1, p+2, --len);
340                                                         p[0] = '\n';
341                                                         }
342                                                 else if (p[1] != '\0')
343                                                         memmove(p, p+1, len--); // includes \0
344                                                 }
345                                         else if (p[0] == '*' && !left)
346                                                 {
347                                                 right = p + 1;
348                                                 left = extra;
349                                                 }
350
351                                 if (left) right[-1] = '\0';
352
353                                 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
354                                 g_free(data);
355                                 data = new_data;
356                                 }
357                         g_free(extra);
358                         }
359
360                 g_string_erase(new, pos, end-start+1);
361                 if (data && *data)
362                         {
363                         if (want_separator)
364                                 {
365                                 /* insert separator */
366                                 g_string_insert(new, pos, sep);
367                                 pos += strlen(sep);
368                                 want_separator = FALSE;
369                                 }
370
371                         g_string_insert(new, pos, data);
372                         pos += strlen(data);
373                 }
374
375                 if (pos-prev >= 1 && new->str[pos] == imp)
376                         {
377                         /* pipe character is replaced by a separator, delete it
378                          * and raise a flag if needed */
379                         g_string_erase(new, pos--, 1);
380                         want_separator |= (data && *data);
381                         }
382
383                 if (new->str[pos] == '\n') want_separator = FALSE;
384
385                 prev = pos - 1;
386
387                 g_free(name);
388                 g_free(data);
389                 }
390
391         /* search and destroy empty lines */
392         end = new->str;
393         while ((start = strchr(end, '\n')))
394                 {
395                 end = start;
396                 while (*++(end) == '\n')
397                         ;
398                 g_string_erase(new, start-new->str, end-start-1);
399                 }
400
401         g_strchomp(new->str);
402
403         ret = new->str;
404         g_string_free(new, FALSE);
405
406         return ret;
407 }
408
409 typedef enum {
410         OSDT_NONE       = 0,
411         OSDT_FREE       = 1 << 0,
412         OSDT_NO_DUP     = 1 << 1
413 } OsdTemplateFlags;
414
415 static void osd_template_insert(GHashTable *vars, gchar *keyword, gchar *value, OsdTemplateFlags flags)
416 {
417         if (!value)
418                 {
419                 g_hash_table_insert(vars, keyword, g_strdup(""));
420                 return;
421                 }
422
423         if (flags & OSDT_NO_DUP)
424                 {
425                 g_hash_table_insert(vars, keyword, value);
426                 return;
427                 }
428         else
429                 {
430                 g_hash_table_insert(vars, keyword, g_strdup(value));
431                 }
432
433         if (flags & OSDT_FREE) g_free((gpointer) value);
434 }
435
436 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
437 {
438         GdkPixbuf *pixbuf = NULL;
439         gint width, height;
440         PangoLayout *layout;
441         const gchar *name;
442         gchar *text;
443         GdkPixbuf *imgpixbuf = NULL;
444         gboolean with_hist;
445         ImageWindow *imd = osd->imd;
446         FileData *fd = image_get_fd(imd);
447
448         if (!fd) return NULL;
449
450         name = image_get_name(imd);
451         if (name)
452                 {
453                 gint n, t;
454                 CollectionData *cd;
455                 CollectInfo *info;
456                 GHashTable *vars;
457
458                 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
459
460                 cd = image_get_collection(imd, &info);
461                 if (cd)
462                         {
463                         t = g_list_length(cd->list);
464                         n = g_list_index(cd->list, info) + 1;
465                         if (cd->name)
466                                 {
467                                 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
468                                         osd_template_insert(vars, "collection", remove_extension_from_path(cd->name), OSDT_FREE);
469                                 else
470                                         osd_template_insert(vars, "collection", cd->name, OSDT_NONE);
471                                 }
472                         else
473                                 {
474                                 osd_template_insert(vars, "collection", _("Untitled"), OSDT_NONE);
475                                 }
476                         }
477                 else
478                         {
479                         LayoutWindow *lw = layout_find_by_image(imd);
480                         if (lw)
481                                 {
482                                 if (lw->slideshow)
483                                         {
484                                         n = g_list_length(lw->slideshow->list_done);
485                                         t = n + g_list_length(lw->slideshow->list);
486                                         if (n == 0) n = t;
487                                         }
488                                 else
489                                         {
490                                         t = layout_list_count(lw, NULL);
491                                         n = layout_list_get_index(lw, image_get_fd(lw->image)) + 1;
492                                         }
493                                 }
494                         else if (view_window_find_image(imd, &n, &t))
495                                 {
496                                 n++;
497                                 }
498                         else
499                                 {
500                                 t = 1;
501                                 n = 1;
502                                 }
503         
504                         if (n < 1) n = 1;
505                         if (t < 1) t = 1;
506         
507                         osd_template_insert(vars, "collection", NULL, OSDT_NONE);
508                         }
509                 
510                 osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
511                 osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
512                 osd_template_insert(vars, "name", (gchar *) name, OSDT_NONE);
513                 osd_template_insert(vars, "date", imd->image_fd ? ((gchar *) text_from_time(imd->image_fd->date)) : "", OSDT_NONE);
514                 osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
515                 osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
516         
517                 if (!imd->unknown)
518                         {
519                         gint w, h;
520                         GdkPixbuf *load_pixbuf = image_loader_get_pixbuf(imd->il);
521
522                         if (imd->delay_flip &&
523                             imd->il && load_pixbuf &&
524                             image_get_pixbuf(imd) != load_pixbuf)
525                                 {
526                                 w = gdk_pixbuf_get_width(load_pixbuf);
527                                 h = gdk_pixbuf_get_height(load_pixbuf);
528                                 imgpixbuf = load_pixbuf;
529                                 }
530                         else
531                                 {
532                                 image_get_image_size(imd, &w, &h);
533                                 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
534                                 }
535                 
536                         
537                         osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
538                         osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
539                         osd_template_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h), OSDT_FREE);
540                         }
541                 else
542                         {
543                         osd_template_insert(vars, "width", NULL, OSDT_NONE);
544                         osd_template_insert(vars, "height", NULL, OSDT_NONE);
545                         osd_template_insert(vars, "res", NULL, OSDT_NONE);
546                         }
547
548                 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
549                 g_hash_table_destroy(vars);
550
551         } else {
552                 /* When does this occur ?? */
553                 text = g_markup_escape_text(_("Untitled"), -1);
554         }
555
556         with_hist = (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram && (!imd->il || image_loader_get_is_done(imd->il)));
557         
558         {
559                 gint active_marks = 0;
560                 gint mark;
561                 gchar *text2;
562
563                 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
564                         {
565                         active_marks += file_data_get_mark(fd, mark);
566                         }
567
568                 if (active_marks > 0)
569                         {
570                         GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
571
572                         for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
573                                 {
574                                 g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
575                                 }
576
577                         if (*text)
578                                 text2 = g_strdup_printf("%s\n%s", text, buf->str);
579                         else
580                                 text2 = g_strdup(buf->str);
581                         g_string_free(buf, TRUE);
582                         g_free(text);
583                         text = text2;
584                         }
585
586                 if (with_hist)
587                         {
588                         gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
589                         if (*text)
590                                 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
591                         else
592                                 text2 = g_strdup(escaped_histogram_label);
593                         g_free(escaped_histogram_label);
594                         g_free(text);
595                         text = text2;
596                         }
597         }
598
599         layout = gtk_widget_create_pango_layout(imd->pr, NULL);
600         pango_layout_set_markup(layout, text, -1);
601         g_free(text);
602
603         pango_layout_get_pixel_size(layout, &width, &height);
604         /* with empty text width is set to 0, but not height) */
605         if (width == 0)
606                 height = 0;
607         else if (height == 0)
608                 width = 0;
609         if (width > 0) width += 10;
610         if (height > 0) height += 10;
611
612         if (with_hist)
613                 {
614                 histogram_read(osd->histogram, imgpixbuf);
615                 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
616                 height += HISTOGRAM_HEIGHT + 5;
617                 }
618
619         if (width > 0 && height > 0)
620                 {
621                 /* TODO: make osd color configurable --Zas */
622                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
623                 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
624                 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
625                 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
626                 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
627                 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
628                 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
629                 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
630                 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
631
632                 if (with_hist)
633                         {
634                         gint x = 5;
635                         gint y = height - HISTOGRAM_HEIGHT - 5;
636                         gint w = width - 10;
637                         float xoffset = 0;
638                         gint subdiv = 5;
639                         gint c = 160;
640                         gint alpha = 250;
641                         gint i;
642                         float add = w / (float)subdiv;
643
644                         for (i = 0; i < subdiv; i++)
645                                 {
646                                 gint d = (i > 0 ? 0 : 1);
647
648                                 pixbuf_set_rect(pixbuf, x + xoffset + 0.5, y, add + d + 0.5, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
649                                 xoffset += add+d;
650                                 }
651                                                 
652                         histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
653                         }
654                 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
655         }
656
657         g_object_unref(G_OBJECT(layout));
658
659         return pixbuf;
660 }
661
662 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
663 {
664         static GdkPixbuf **icons = NULL;
665         GdkPixbuf *icon = NULL;
666
667         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
668         if (icons[flag]) return icons[flag];
669
670         if (osd_icons[flag].key)
671                 {
672                 icon = pixbuf_inline(osd_icons[flag].key);
673                 }
674
675         if (!icon)
676                 {
677                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
678                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
679                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
680                 switch (flag)
681                         {
682                         case IMAGE_OSD_ROTATE_AUTO:
683                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
684                                                 0, 0, 0, 255,
685                                                 3, 0, 3, 0);
686                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
687                                                      20, 9, 14, 15, 14, 3,
688                                                      0, 0, 0, 255);
689                                 break;
690                         case IMAGE_OSD_ROTATE_USER:
691                                 break;
692                         case IMAGE_OSD_COLOR:
693                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
694                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
695                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
696                                 break;
697                         case IMAGE_OSD_FIRST:
698                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
699                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
700                                                      12, 5, 18, 11, 6, 11,
701                                                      0, 0, 0, 255);
702                                 break;
703                         case IMAGE_OSD_LAST:
704                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
705                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
706                                                      12, 18, 6, 12, 18, 12,
707                                                      0, 0, 0, 255);
708                                 break;
709                         case IMAGE_OSD_ICON:
710                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
711                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
712                                 break;
713                         default:
714                                 break;
715                         }
716                 }
717
718         icons[flag] = icon;
719
720         return icon;
721 }
722
723 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
724                               OverlayRendererFlags flags)
725 {
726         return pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, pixbuf, x, y, flags);
727 }
728
729 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
730 {
731         pixbuf_renderer_overlay_set((PixbufRenderer *)imd->pr, id, pixbuf, x, y);
732 }
733
734 #if 0 /* unused for now */
735 static gint image_overlay_get(ImageWindow *imd, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
736 {
737         return pixbuf_renderer_overlay_get((PixbufRenderer *)imd->pr, id, pixbuf, x, y);
738 }
739 #endif
740
741 static void image_overlay_remove(ImageWindow *imd, gint id)
742 {
743         pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, id);
744 }
745
746 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
747 {
748         GdkPixbuf *pixbuf;
749
750         if (osd->icon_id[flag]) return;
751
752         pixbuf = image_osd_icon_pixbuf(flag);
753         if (!pixbuf) return;
754
755         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
756                                                osd_icons[flag].x, osd_icons[flag].y,
757                                                OVL_RELATIVE);
758 }
759
760 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
761 {
762         if (osd->icon_id[flag])
763                 {
764                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
765                 osd->icon_id[flag] = 0;
766                 }
767 }
768
769 static void image_osd_icons_reset_time(OverlayStateData *osd)
770 {
771         gint i;
772
773         for (i = 0; i < IMAGE_OSD_COUNT; i++)
774                 {
775                 if (osd_icons[i].reset)
776                         {
777                         osd->icon_time[i] = 0;
778                         }
779                 }
780 }
781
782 static void image_osd_icons_update(OverlayStateData *osd)
783 {
784         gint i;
785
786         for (i = 0; i < IMAGE_OSD_COUNT; i++)
787                 {
788                 if (osd->icon_time[i] > 0)
789                         {
790                         image_osd_icon_show(osd, i);
791                         }
792                 else
793                         {
794                         image_osd_icon_hide(osd, i);
795                         }
796                 }
797 }
798
799 static void image_osd_icons_hide(OverlayStateData *osd)
800 {
801         gint i;
802
803         for (i = 0; i < IMAGE_OSD_COUNT; i++)
804                 {
805                 image_osd_icon_hide(osd, i);
806                 }
807 }
808
809 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
810 {
811         if (osd->ovl_info == 0)
812                 {
813                 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, OVL_RELATIVE);
814                 }
815         else
816                 {
817                 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
818                 }
819 }
820
821 static void image_osd_info_hide(OverlayStateData *osd)
822 {
823         if (osd->ovl_info == 0) return;
824
825         image_overlay_remove(osd->imd, osd->ovl_info);
826         osd->ovl_info = 0;
827 }
828
829 static gint image_osd_update_cb(gpointer data)
830 {
831         OverlayStateData *osd = data;
832
833         osd->imd->overlay_show_zoom = FALSE;
834
835         if (osd->show & OSD_SHOW_INFO)
836                 {
837                 if (osd->changed_states & IMAGE_STATE_IMAGE)
838                         {
839                         GdkPixbuf *pixbuf;
840
841                         pixbuf = image_osd_info_render(osd);
842                         if (pixbuf)
843                                 {
844                                 image_osd_info_show(osd, pixbuf);
845                                 g_object_unref(pixbuf);
846                                 }
847                         else
848                                 {
849                                 image_osd_info_hide(osd);
850                                 }
851                         }
852                 }
853         else
854                 {
855                 image_osd_info_hide(osd);
856                 }
857
858         if (osd->show & OSD_SHOW_STATUS)
859                 {
860                 if (osd->changed_states & IMAGE_STATE_IMAGE)
861                         image_osd_icons_reset_time(osd);
862         
863                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
864                         {
865                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
866                         image_osd_timer_schedule(osd);
867                         }
868
869                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
870                         {
871                         gint n = 0;
872
873                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
874                                 {
875                                 n = 1;
876                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
877                                 }
878
879                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
880                         image_osd_timer_schedule(osd);
881                         }
882
883                 image_osd_icons_update(osd);
884                 }
885         else
886                 {
887                 image_osd_icons_hide(osd);
888                 }
889
890         if (osd->imd->il && image_loader_get_is_done(osd->imd->il))
891                 osd->changed_states = IMAGE_STATE_NONE;
892         osd->idle_id = -1;
893         return FALSE;
894 }
895
896 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
897 {
898         if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
899
900         if (osd->idle_id == -1)
901                 {
902                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
903                 }
904 }
905
906 void image_osd_update(ImageWindow *imd)
907 {
908         OverlayStateData *osd = image_get_osd_data(imd);
909
910         if (!osd) return;
911
912         image_osd_update_schedule(osd, TRUE);
913 }
914
915 static gint image_osd_timer_cb(gpointer data)
916 {
917         OverlayStateData *osd = data;
918         gint done = TRUE;
919         gint changed = FALSE;
920         gint i;
921
922         for (i = 0; i < IMAGE_OSD_COUNT; i++)
923                 {
924                 if (osd->icon_time[i] > 1)
925                         {
926                         osd->icon_time[i]--;
927                         if (osd->icon_time[i] < 2)
928                                 {
929                                 osd->icon_time[i] = 0;
930                                 changed = TRUE;
931                                 }
932                         else
933                                 {
934                                 done = FALSE;
935                                 }
936                         }
937                 }
938
939         if (changed) image_osd_update_schedule(osd, FALSE);
940
941         if (done)
942                 {
943                 osd->timer_id = -1;
944                 return FALSE;
945                 }
946
947         return TRUE;
948 }
949
950 static void image_osd_timer_schedule(OverlayStateData *osd)
951 {
952         if (osd->timer_id == -1)
953                 {
954                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
955                 }
956 }
957
958 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
959 {
960         OverlayStateData *osd = data;
961
962         osd->changed_states |= state;
963         image_osd_update_schedule(osd, FALSE);
964 }
965
966 static void image_osd_free(OverlayStateData *osd)
967 {
968         if (!osd) return;
969
970         if (osd->idle_id != -1) g_source_remove(osd->idle_id);
971         if (osd->timer_id != -1) g_source_remove(osd->timer_id);
972
973         if (osd->imd)
974                 {
975                 image_set_osd_data(osd->imd, NULL);
976                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
977
978                 image_set_state_func(osd->imd, NULL, NULL);
979
980                 image_osd_info_hide(osd);
981                 image_osd_icons_hide(osd);
982                 }
983
984         if (osd->histogram) histogram_free(osd->histogram);
985
986         g_free(osd);
987 }
988
989 static void image_osd_remove(ImageWindow *imd)
990 {
991         OverlayStateData *osd = image_get_osd_data(imd);
992
993         if (osd) image_osd_free(osd);
994 }
995
996 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
997 {
998         OverlayStateData *osd = data;
999
1000         osd->imd = NULL;
1001         image_osd_free(osd);
1002 }
1003
1004 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
1005 {
1006         OverlayStateData *osd = image_get_osd_data(imd);
1007
1008         if (!osd)
1009                 {
1010                 osd = g_new0(OverlayStateData, 1);
1011                 osd->imd = imd;
1012                 osd->idle_id = -1;
1013                 osd->timer_id = -1;
1014                 osd->show = OSD_SHOW_NOTHING;
1015                 osd->histogram = NULL;
1016                 osd->x = options->image_overlay.common.x;
1017                 osd->y = options->image_overlay.common.y;
1018                 
1019                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
1020                                                    G_CALLBACK(image_osd_destroy_cb), osd);
1021                 image_set_osd_data(imd, osd);
1022
1023                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
1024                 }
1025
1026         if (show & OSD_SHOW_HISTOGRAM)
1027                 osd->histogram = histogram_new();
1028         else if (osd->histogram)
1029                 {
1030                 histogram_free(osd->histogram);
1031                 osd->histogram = NULL;
1032                 }
1033
1034         if (show & OSD_SHOW_STATUS)
1035                 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
1036
1037         if (show != osd->show)
1038                 image_osd_update_schedule(osd, TRUE);
1039
1040         osd->show = show;
1041 }
1042
1043 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
1044 {
1045         if (!imd) return;
1046
1047         if (show == OSD_SHOW_NOTHING)
1048                 {
1049                 image_osd_remove(imd);
1050                 return;
1051                 }
1052
1053         image_osd_enable(imd, show);
1054 }
1055
1056 OsdShowFlags image_osd_get(ImageWindow *imd)
1057 {
1058         OverlayStateData *osd = image_get_osd_data(imd);
1059
1060         return osd ? osd->show : OSD_SHOW_NOTHING;
1061 }
1062
1063 /* duration:
1064     0 = hide
1065     1 = show
1066    2+ = show for duration tenths of a second
1067    -1 = use default duration
1068  */
1069 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
1070 {
1071         OverlayStateData *osd = image_get_osd_data(imd);
1072
1073         if (!osd) return;
1074
1075         if (flag >= IMAGE_OSD_COUNT) return;
1076         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
1077         if (duration > 1) duration += 1;
1078
1079         osd->icon_time[flag] = duration;
1080
1081         image_osd_update_schedule(osd, FALSE);
1082         image_osd_timer_schedule(osd);
1083 }
1084 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */