Merge branch 'master' into ke-lua
[geeqie.git] / src / image-overlay.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2012 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         gboolean with_hist;
493         const HistMap *histmap = NULL;
494         ImageWindow *imd = osd->imd;
495         FileData *fd = image_get_fd(imd);
496
497         if (!fd) return NULL;
498
499         name = image_get_name(imd);
500         if (name)
501                 {
502                 gint n, t;
503                 CollectionData *cd;
504                 CollectInfo *info;
505                 GHashTable *vars;
506
507                 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
508
509                 cd = image_get_collection(imd, &info);
510                 if (cd)
511                         {
512                         t = g_list_length(cd->list);
513                         n = g_list_index(cd->list, info) + 1;
514                         if (cd->name)
515                                 {
516                                 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
517                                         osd_template_insert(vars, "collection", remove_extension_from_path(cd->name), OSDT_FREE);
518                                 else
519                                         osd_template_insert(vars, "collection", cd->name, OSDT_NONE);
520                                 }
521                         else
522                                 {
523                                 osd_template_insert(vars, "collection", _("Untitled"), OSDT_NONE);
524                                 }
525                         }
526                 else
527                         {
528                         LayoutWindow *lw = layout_find_by_image(imd);
529                         if (lw)
530                                 {
531                                 if (lw->slideshow)
532                                         {
533                                         n = g_list_length(lw->slideshow->list_done);
534                                         t = n + g_list_length(lw->slideshow->list);
535                                         if (n == 0) n = t;
536                                         }
537                                 else
538                                         {
539                                         t = layout_list_count(lw, NULL);
540                                         n = layout_list_get_index(lw, image_get_fd(lw->image)) + 1;
541                                         }
542                                 }
543                         else if (view_window_find_image(imd, &n, &t))
544                                 {
545                                 n++;
546                                 }
547                         else
548                                 {
549                                 t = 1;
550                                 n = 1;
551                                 }
552
553                         if (n < 1) n = 1;
554                         if (t < 1) t = 1;
555
556                         osd_template_insert(vars, "collection", NULL, OSDT_NONE);
557                         }
558
559                 osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
560                 osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
561                 osd_template_insert(vars, "name", (gchar *) name, OSDT_NONE);
562                 osd_template_insert(vars, "date", imd->image_fd ? ((gchar *) text_from_time(imd->image_fd->date)) : "", OSDT_NONE);
563                 osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
564                 osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
565
566                 if (!imd->unknown)
567                         {
568                         gint w, h;
569                         GdkPixbuf *load_pixbuf = image_loader_get_pixbuf(imd->il);
570
571                         if (imd->delay_flip &&
572                             imd->il && load_pixbuf &&
573                             image_get_pixbuf(imd) != load_pixbuf)
574                                 {
575                                 w = gdk_pixbuf_get_width(load_pixbuf);
576                                 h = gdk_pixbuf_get_height(load_pixbuf);
577                                 }
578                         else
579                                 {
580                                 image_get_image_size(imd, &w, &h);
581                                 }
582
583
584                         osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
585                         osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
586                         osd_template_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h), OSDT_FREE);
587                         }
588                 else
589                         {
590                         osd_template_insert(vars, "width", NULL, OSDT_NONE);
591                         osd_template_insert(vars, "height", NULL, OSDT_NONE);
592                         osd_template_insert(vars, "res", NULL, OSDT_NONE);
593                         }
594
595                 text = image_osd_mkinfo(options->image_overlay.template_string, imd, vars);
596                 g_hash_table_destroy(vars);
597
598         } else {
599                 /* When does this occur ?? */
600                 text = g_markup_escape_text(_("Untitled"), -1);
601         }
602
603         with_hist = ((osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram);
604         if (with_hist)
605                 {
606                 histmap = histmap_get(imd->image_fd);
607                 if (!histmap)
608                         {
609                         histmap_start_idle(imd->image_fd);
610                         with_hist = FALSE;
611                         }
612                 }
613
614
615         {
616                 gint active_marks = 0;
617                 gint mark;
618                 gchar *text2;
619
620                 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
621                         {
622                         active_marks += file_data_get_mark(fd, mark);
623                         }
624
625                 if (active_marks > 0)
626                         {
627                         GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
628
629                         for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
630                                 {
631                                 g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
632                                 }
633
634                         if (*text)
635                                 text2 = g_strdup_printf("%s\n%s", text, buf->str);
636                         else
637                                 text2 = g_strdup(buf->str);
638                         g_string_free(buf, TRUE);
639                         g_free(text);
640                         text = text2;
641                         }
642
643                 if (with_hist)
644                         {
645                         gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
646                         if (*text)
647                                 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
648                         else
649                                 text2 = g_strdup(escaped_histogram_label);
650                         g_free(escaped_histogram_label);
651                         g_free(text);
652                         text = text2;
653                         }
654         }
655
656         layout = gtk_widget_create_pango_layout(imd->pr, NULL);
657         pango_layout_set_markup(layout, text, -1);
658         g_free(text);
659
660         pango_layout_get_pixel_size(layout, &width, &height);
661         /* with empty text width is set to 0, but not height) */
662         if (width == 0)
663                 height = 0;
664         else if (height == 0)
665                 width = 0;
666         if (width > 0) width += 10;
667         if (height > 0) height += 10;
668
669         if (with_hist)
670                 {
671                 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
672                 height += HISTOGRAM_HEIGHT + 5;
673                 }
674
675         if (width > 0 && height > 0)
676                 {
677                 /* TODO: make osd color configurable --Zas */
678                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
679                 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
680                 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
681                 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
682                 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
683                 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
684                 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
685                 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
686                 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
687
688                 if (with_hist)
689                         {
690                         gint x = 5;
691                         gint y = height - HISTOGRAM_HEIGHT - 5;
692                         gint w = width - 10;
693
694                         pixbuf_set_rect_fill(pixbuf, x, y, w, HISTOGRAM_HEIGHT, 220, 220, 220, 210);
695                         histogram_draw(osd->histogram, histmap, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
696                         }
697                 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
698         }
699
700         g_object_unref(G_OBJECT(layout));
701
702         return pixbuf;
703 }
704
705 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
706 {
707         static GdkPixbuf **icons = NULL;
708         GdkPixbuf *icon = NULL;
709
710         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
711         if (icons[flag]) return icons[flag];
712
713         if (osd_icons[flag].key)
714                 {
715                 icon = pixbuf_inline(osd_icons[flag].key);
716                 }
717
718         if (!icon)
719                 {
720                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
721                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
722                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
723                 switch (flag)
724                         {
725                         case IMAGE_OSD_ROTATE_AUTO:
726                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
727                                                 0, 0, 0, 255,
728                                                 3, 0, 3, 0);
729                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
730                                                      20, 9, 14, 15, 14, 3,
731                                                      0, 0, 0, 255);
732                                 break;
733                         case IMAGE_OSD_ROTATE_USER:
734                                 break;
735                         case IMAGE_OSD_COLOR:
736                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
737                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
738                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
739                                 break;
740                         case IMAGE_OSD_FIRST:
741                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
742                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
743                                                      12, 5, 18, 11, 6, 11,
744                                                      0, 0, 0, 255);
745                                 break;
746                         case IMAGE_OSD_LAST:
747                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
748                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
749                                                      12, 18, 6, 12, 18, 12,
750                                                      0, 0, 0, 255);
751                                 break;
752                         case IMAGE_OSD_ICON:
753                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
754                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
755                                 break;
756                         default:
757                                 break;
758                         }
759                 }
760
761         icons[flag] = icon;
762
763         return icon;
764 }
765
766 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
767                               OverlayRendererFlags flags)
768 {
769         return pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, pixbuf, x, y, flags);
770 }
771
772 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
773 {
774         pixbuf_renderer_overlay_set((PixbufRenderer *)imd->pr, id, pixbuf, x, y);
775 }
776
777 static void image_overlay_remove(ImageWindow *imd, gint id)
778 {
779         pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, id);
780 }
781
782 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
783 {
784         GdkPixbuf *pixbuf;
785
786         if (osd->icon_id[flag]) return;
787
788         pixbuf = image_osd_icon_pixbuf(flag);
789         if (!pixbuf) return;
790
791         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
792                                                osd_icons[flag].x, osd_icons[flag].y,
793                                                OVL_RELATIVE);
794 }
795
796 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
797 {
798         if (osd->icon_id[flag])
799                 {
800                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
801                 osd->icon_id[flag] = 0;
802                 }
803 }
804
805 static void image_osd_icons_reset_time(OverlayStateData *osd)
806 {
807         gint i;
808
809         for (i = 0; i < IMAGE_OSD_COUNT; i++)
810                 {
811                 if (osd_icons[i].reset)
812                         {
813                         osd->icon_time[i] = 0;
814                         }
815                 }
816 }
817
818 static void image_osd_icons_update(OverlayStateData *osd)
819 {
820         gint i;
821
822         for (i = 0; i < IMAGE_OSD_COUNT; i++)
823                 {
824                 if (osd->icon_time[i] > 0)
825                         {
826                         image_osd_icon_show(osd, i);
827                         }
828                 else
829                         {
830                         image_osd_icon_hide(osd, i);
831                         }
832                 }
833 }
834
835 static void image_osd_icons_hide(OverlayStateData *osd)
836 {
837         gint i;
838
839         for (i = 0; i < IMAGE_OSD_COUNT; i++)
840                 {
841                 image_osd_icon_hide(osd, i);
842                 }
843 }
844
845 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
846 {
847         if (osd->ovl_info == 0)
848                 {
849                 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, OVL_RELATIVE);
850                 }
851         else
852                 {
853                 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
854                 }
855 }
856
857 static void image_osd_info_hide(OverlayStateData *osd)
858 {
859         if (osd->ovl_info == 0) return;
860
861         image_overlay_remove(osd->imd, osd->ovl_info);
862         osd->ovl_info = 0;
863 }
864
865 static gboolean image_osd_update_cb(gpointer data)
866 {
867         OverlayStateData *osd = data;
868
869         if (osd->show & OSD_SHOW_INFO)
870                 {
871                 /* redraw when the image was changed,
872                    with histogram we have to redraw also when loading is finished */
873                 if (osd->changed_states & IMAGE_STATE_IMAGE ||
874                     (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
875                     osd->notify & NOTIFY_HISTMAP)
876                         {
877                         GdkPixbuf *pixbuf;
878
879                         pixbuf = image_osd_info_render(osd);
880                         if (pixbuf)
881                                 {
882                                 image_osd_info_show(osd, pixbuf);
883                                 g_object_unref(pixbuf);
884                                 }
885                         else
886                                 {
887                                 image_osd_info_hide(osd);
888                                 }
889                         }
890                 }
891         else
892                 {
893                 image_osd_info_hide(osd);
894                 }
895
896         if (osd->show & OSD_SHOW_STATUS)
897                 {
898                 if (osd->changed_states & IMAGE_STATE_IMAGE)
899                         image_osd_icons_reset_time(osd);
900
901                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
902                         {
903                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
904                         image_osd_timer_schedule(osd);
905                         }
906
907                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
908                         {
909                         gint n = 0;
910
911                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
912                                 {
913                                 n = 1;
914                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
915                                 }
916
917                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
918                         image_osd_timer_schedule(osd);
919                         }
920
921                 image_osd_icons_update(osd);
922                 }
923         else
924                 {
925                 image_osd_icons_hide(osd);
926                 }
927
928         osd->changed_states = IMAGE_STATE_NONE;
929         osd->notify = 0;
930         osd->idle_id = 0;
931         return FALSE;
932 }
933
934 static void image_osd_update_schedule(OverlayStateData *osd, gboolean force)
935 {
936         if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
937
938         if (!osd->idle_id)
939                 {
940                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
941                 }
942 }
943
944 void image_osd_update(ImageWindow *imd)
945 {
946         OverlayStateData *osd = image_get_osd_data(imd);
947
948         if (!osd) return;
949
950         image_osd_update_schedule(osd, TRUE);
951 }
952
953 static gboolean image_osd_timer_cb(gpointer data)
954 {
955         OverlayStateData *osd = data;
956         gboolean done = TRUE;
957         gboolean changed = FALSE;
958         gint i;
959
960         for (i = 0; i < IMAGE_OSD_COUNT; i++)
961                 {
962                 if (osd->icon_time[i] > 1)
963                         {
964                         osd->icon_time[i]--;
965                         if (osd->icon_time[i] < 2)
966                                 {
967                                 osd->icon_time[i] = 0;
968                                 changed = TRUE;
969                                 }
970                         else
971                                 {
972                                 done = FALSE;
973                                 }
974                         }
975                 }
976
977         if (changed) image_osd_update_schedule(osd, FALSE);
978
979         if (done)
980                 {
981                 osd->timer_id = 0;
982                 return FALSE;
983                 }
984
985         return TRUE;
986 }
987
988 static void image_osd_timer_schedule(OverlayStateData *osd)
989 {
990         if (!osd->timer_id)
991                 {
992                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
993                 }
994 }
995
996 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
997 {
998         OverlayStateData *osd = data;
999
1000         osd->changed_states |= state;
1001         image_osd_update_schedule(osd, FALSE);
1002 }
1003
1004 static void image_osd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1005 {
1006         OverlayStateData *osd = data;
1007
1008         if ((type & (NOTIFY_HISTMAP)) && osd->imd && fd == osd->imd->image_fd)
1009                 {
1010                 DEBUG_1("Notify osd: %s %04x", fd->path, type);
1011                 osd->notify |= type;
1012                 image_osd_update_schedule(osd, FALSE);
1013                 }
1014 }
1015
1016
1017 static void image_osd_free(OverlayStateData *osd)
1018 {
1019         if (!osd) return;
1020
1021         if (osd->idle_id) g_source_remove(osd->idle_id);
1022         if (osd->timer_id) g_source_remove(osd->timer_id);
1023
1024         file_data_unregister_notify_func(image_osd_notify_cb, osd);
1025
1026         if (osd->imd)
1027                 {
1028                 image_set_osd_data(osd->imd, NULL);
1029                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
1030
1031                 image_set_state_func(osd->imd, NULL, NULL);
1032
1033                 image_osd_info_hide(osd);
1034                 image_osd_icons_hide(osd);
1035                 }
1036
1037         if (osd->histogram) histogram_free(osd->histogram);
1038
1039         g_free(osd);
1040 }
1041
1042 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
1043 {
1044         OverlayStateData *osd = data;
1045
1046         osd->imd = NULL;
1047         image_osd_free(osd);
1048 }
1049
1050 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
1051 {
1052         OverlayStateData *osd = image_get_osd_data(imd);
1053
1054         if (!osd)
1055                 {
1056                 osd = g_new0(OverlayStateData, 1);
1057                 osd->imd = imd;
1058                 osd->show = OSD_SHOW_NOTHING;
1059                 osd->x = options->image_overlay.x;
1060                 osd->y = options->image_overlay.y;
1061
1062                 osd->histogram = histogram_new();
1063
1064                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
1065                                                    G_CALLBACK(image_osd_destroy_cb), osd);
1066                 image_set_osd_data(imd, osd);
1067
1068                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
1069                 file_data_register_notify_func(image_osd_notify_cb, osd, NOTIFY_PRIORITY_LOW);
1070                 }
1071
1072         if (show & OSD_SHOW_STATUS)
1073                 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
1074
1075         if (show != osd->show)
1076                 image_osd_update_schedule(osd, TRUE);
1077
1078         osd->show = show;
1079 }
1080
1081 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
1082 {
1083         if (!imd) return;
1084
1085         image_osd_enable(imd, show);
1086 }
1087
1088 OsdShowFlags image_osd_get(ImageWindow *imd)
1089 {
1090         OverlayStateData *osd = image_get_osd_data(imd);
1091
1092         return osd ? osd->show : OSD_SHOW_NOTHING;
1093 }
1094
1095 Histogram *image_osd_get_histogram(ImageWindow *imd)
1096 {
1097         OverlayStateData *osd = image_get_osd_data(imd);
1098
1099         return osd ? osd->histogram : NULL;
1100 }
1101
1102 void image_osd_copy_status(ImageWindow *src, ImageWindow *dest)
1103 {
1104         Histogram *h_src, *h_dest;
1105         image_osd_set(dest, image_osd_get(src));
1106
1107         h_src = image_osd_get_histogram(src);
1108         h_dest = image_osd_get_histogram(dest);
1109
1110         h_dest->histogram_mode = h_src->histogram_mode;
1111         h_dest->histogram_channel = h_src->histogram_channel;
1112
1113 }
1114
1115 /* duration:
1116     0 = hide
1117     1 = show
1118    2+ = show for duration tenths of a second
1119    -1 = use default duration
1120  */
1121 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
1122 {
1123         OverlayStateData *osd = image_get_osd_data(imd);
1124
1125         if (!osd) return;
1126
1127         if (flag >= IMAGE_OSD_COUNT) return;
1128         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
1129         if (duration > 1) duration += 1;
1130
1131         osd->icon_time[flag] = duration;
1132
1133         image_osd_update_schedule(osd, FALSE);
1134         image_osd_timer_schedule(osd);
1135 }
1136 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */