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