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