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