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