Image overlay configurable font
[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                 /* TODO: make osd color configurable --Zas */
690                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
691                 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
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, 0, 0, 0, 255);
710         }
711
712         g_object_unref(G_OBJECT(layout));
713
714         return pixbuf;
715 }
716
717 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
718 {
719         static GdkPixbuf **icons = NULL;
720         GdkPixbuf *icon = NULL;
721
722         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
723         if (icons[flag]) return icons[flag];
724
725         if (osd_icons[flag].key)
726                 {
727                 icon = pixbuf_inline(osd_icons[flag].key);
728                 }
729
730         if (!icon)
731                 {
732                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
733                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
734                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
735                 switch (flag)
736                         {
737                         case IMAGE_OSD_ROTATE_AUTO:
738                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
739                                                 0, 0, 0, 255,
740                                                 3, 0, 3, 0);
741                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
742                                                      20, 9, 14, 15, 14, 3,
743                                                      0, 0, 0, 255);
744                                 break;
745                         case IMAGE_OSD_ROTATE_USER:
746                                 break;
747                         case IMAGE_OSD_COLOR:
748                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
749                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
750                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
751                                 break;
752                         case IMAGE_OSD_FIRST:
753                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
754                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
755                                                      12, 5, 18, 11, 6, 11,
756                                                      0, 0, 0, 255);
757                                 break;
758                         case IMAGE_OSD_LAST:
759                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
760                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
761                                                      12, 18, 6, 12, 18, 12,
762                                                      0, 0, 0, 255);
763                                 break;
764                         case IMAGE_OSD_ICON:
765                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
766                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
767                                 break;
768                         default:
769                                 break;
770                         }
771                 }
772
773         icons[flag] = icon;
774
775         return icon;
776 }
777
778 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
779                               OverlayRendererFlags flags)
780 {
781         return pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, pixbuf, x, y, flags);
782 }
783
784 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
785 {
786         pixbuf_renderer_overlay_set((PixbufRenderer *)imd->pr, id, pixbuf, x, y);
787 }
788
789 static void image_overlay_remove(ImageWindow *imd, gint id)
790 {
791         pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, id);
792 }
793
794 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
795 {
796         GdkPixbuf *pixbuf;
797
798         if (osd->icon_id[flag]) return;
799
800         pixbuf = image_osd_icon_pixbuf(flag);
801         if (!pixbuf) return;
802
803         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
804                                                osd_icons[flag].x, osd_icons[flag].y,
805                                                OVL_RELATIVE);
806 }
807
808 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
809 {
810         if (osd->icon_id[flag])
811                 {
812                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
813                 osd->icon_id[flag] = 0;
814                 }
815 }
816
817 static void image_osd_icons_reset_time(OverlayStateData *osd)
818 {
819         gint i;
820
821         for (i = 0; i < IMAGE_OSD_COUNT; i++)
822                 {
823                 if (osd_icons[i].reset)
824                         {
825                         osd->icon_time[i] = 0;
826                         }
827                 }
828 }
829
830 static void image_osd_icons_update(OverlayStateData *osd)
831 {
832         gint i;
833
834         for (i = 0; i < IMAGE_OSD_COUNT; i++)
835                 {
836                 if (osd->icon_time[i] > 0)
837                         {
838                         image_osd_icon_show(osd, i);
839                         }
840                 else
841                         {
842                         image_osd_icon_hide(osd, i);
843                         }
844                 }
845 }
846
847 static void image_osd_icons_hide(OverlayStateData *osd)
848 {
849         gint i;
850
851         for (i = 0; i < IMAGE_OSD_COUNT; i++)
852                 {
853                 image_osd_icon_hide(osd, i);
854                 }
855 }
856
857 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
858 {
859         if (osd->ovl_info == 0)
860                 {
861                 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, OVL_RELATIVE);
862                 }
863         else
864                 {
865                 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
866                 }
867 }
868
869 static void image_osd_info_hide(OverlayStateData *osd)
870 {
871         if (osd->ovl_info == 0) return;
872
873         image_overlay_remove(osd->imd, osd->ovl_info);
874         osd->ovl_info = 0;
875 }
876
877 static gboolean image_osd_update_cb(gpointer data)
878 {
879         OverlayStateData *osd = data;
880
881         if (osd->show & OSD_SHOW_INFO)
882                 {
883                 /* redraw when the image was changed,
884                    with histogram we have to redraw also when loading is finished */
885                 if (osd->changed_states & IMAGE_STATE_IMAGE ||
886                     (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
887                     osd->notify & NOTIFY_HISTMAP)
888                         {
889                         GdkPixbuf *pixbuf;
890
891                         pixbuf = image_osd_info_render(osd);
892                         if (pixbuf)
893                                 {
894                                 image_osd_info_show(osd, pixbuf);
895                                 g_object_unref(pixbuf);
896                                 }
897                         else
898                                 {
899                                 image_osd_info_hide(osd);
900                                 }
901                         }
902                 }
903         else
904                 {
905                 image_osd_info_hide(osd);
906                 }
907
908         if (osd->show & OSD_SHOW_STATUS)
909                 {
910                 if (osd->changed_states & IMAGE_STATE_IMAGE)
911                         image_osd_icons_reset_time(osd);
912
913                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
914                         {
915                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
916                         image_osd_timer_schedule(osd);
917                         }
918
919                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
920                         {
921                         gint n = 0;
922
923                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
924                                 {
925                                 n = 1;
926                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
927                                 }
928
929                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
930                         image_osd_timer_schedule(osd);
931                         }
932
933                 image_osd_icons_update(osd);
934                 }
935         else
936                 {
937                 image_osd_icons_hide(osd);
938                 }
939
940         osd->changed_states = IMAGE_STATE_NONE;
941         osd->notify = 0;
942         osd->idle_id = 0;
943         return FALSE;
944 }
945
946 static void image_osd_update_schedule(OverlayStateData *osd, gboolean force)
947 {
948         if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
949
950         if (!osd->idle_id)
951                 {
952                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
953                 }
954 }
955
956 void image_osd_update(ImageWindow *imd)
957 {
958         OverlayStateData *osd = image_get_osd_data(imd);
959
960         if (!osd) return;
961
962         image_osd_update_schedule(osd, TRUE);
963 }
964
965 static gboolean image_osd_timer_cb(gpointer data)
966 {
967         OverlayStateData *osd = data;
968         gboolean done = TRUE;
969         gboolean changed = FALSE;
970         gint i;
971
972         for (i = 0; i < IMAGE_OSD_COUNT; i++)
973                 {
974                 if (osd->icon_time[i] > 1)
975                         {
976                         osd->icon_time[i]--;
977                         if (osd->icon_time[i] < 2)
978                                 {
979                                 osd->icon_time[i] = 0;
980                                 changed = TRUE;
981                                 }
982                         else
983                                 {
984                                 done = FALSE;
985                                 }
986                         }
987                 }
988
989         if (changed) image_osd_update_schedule(osd, FALSE);
990
991         if (done)
992                 {
993                 osd->timer_id = 0;
994                 return FALSE;
995                 }
996
997         return TRUE;
998 }
999
1000 static void image_osd_timer_schedule(OverlayStateData *osd)
1001 {
1002         if (!osd->timer_id)
1003                 {
1004                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
1005                 }
1006 }
1007
1008 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
1009 {
1010         OverlayStateData *osd = data;
1011
1012         osd->changed_states |= state;
1013         image_osd_update_schedule(osd, FALSE);
1014 }
1015
1016 static void image_osd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1017 {
1018         OverlayStateData *osd = data;
1019
1020         if ((type & (NOTIFY_HISTMAP)) && osd->imd && fd == osd->imd->image_fd)
1021                 {
1022                 DEBUG_1("Notify osd: %s %04x", fd->path, type);
1023                 osd->notify |= type;
1024                 image_osd_update_schedule(osd, FALSE);
1025                 }
1026 }
1027
1028
1029 static void image_osd_free(OverlayStateData *osd)
1030 {
1031         if (!osd) return;
1032
1033         if (osd->idle_id) g_source_remove(osd->idle_id);
1034         if (osd->timer_id) g_source_remove(osd->timer_id);
1035
1036         file_data_unregister_notify_func(image_osd_notify_cb, osd);
1037
1038         if (osd->imd)
1039                 {
1040                 image_set_osd_data(osd->imd, NULL);
1041                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
1042
1043                 image_set_state_func(osd->imd, NULL, NULL);
1044
1045                 image_osd_info_hide(osd);
1046                 image_osd_icons_hide(osd);
1047                 }
1048
1049         if (osd->histogram) histogram_free(osd->histogram);
1050
1051         g_free(osd);
1052 }
1053
1054 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
1055 {
1056         OverlayStateData *osd = data;
1057
1058         osd->imd = NULL;
1059         image_osd_free(osd);
1060 }
1061
1062 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
1063 {
1064         OverlayStateData *osd = image_get_osd_data(imd);
1065
1066         if (!osd)
1067                 {
1068                 osd = g_new0(OverlayStateData, 1);
1069                 osd->imd = imd;
1070                 osd->show = OSD_SHOW_NOTHING;
1071                 osd->x = options->image_overlay.x;
1072                 osd->y = options->image_overlay.y;
1073
1074                 osd->histogram = histogram_new();
1075
1076                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
1077                                                    G_CALLBACK(image_osd_destroy_cb), osd);
1078                 image_set_osd_data(imd, osd);
1079
1080                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
1081                 file_data_register_notify_func(image_osd_notify_cb, osd, NOTIFY_PRIORITY_LOW);
1082                 }
1083
1084         if (show & OSD_SHOW_STATUS)
1085                 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
1086
1087         if (show != osd->show)
1088                 image_osd_update_schedule(osd, TRUE);
1089
1090         osd->show = show;
1091 }
1092
1093 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
1094 {
1095         if (!imd) return;
1096
1097         image_osd_enable(imd, show);
1098 }
1099
1100 OsdShowFlags image_osd_get(ImageWindow *imd)
1101 {
1102         OverlayStateData *osd = image_get_osd_data(imd);
1103
1104         return osd ? osd->show : OSD_SHOW_NOTHING;
1105 }
1106
1107 Histogram *image_osd_get_histogram(ImageWindow *imd)
1108 {
1109         OverlayStateData *osd = image_get_osd_data(imd);
1110
1111         return osd ? osd->histogram : NULL;
1112 }
1113
1114 void image_osd_copy_status(ImageWindow *src, ImageWindow *dest)
1115 {
1116         Histogram *h_src, *h_dest;
1117         image_osd_set(dest, image_osd_get(src));
1118
1119         h_src = image_osd_get_histogram(src);
1120         h_dest = image_osd_get_histogram(dest);
1121
1122         h_dest->histogram_mode = h_src->histogram_mode;
1123         h_dest->histogram_channel = h_src->histogram_channel;
1124
1125 }
1126
1127 /* duration:
1128     0 = hide
1129     1 = show
1130    2+ = show for duration tenths of a second
1131    -1 = use default duration
1132  */
1133 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
1134 {
1135         OverlayStateData *osd = image_get_osd_data(imd);
1136
1137         if (!osd) return;
1138
1139         if (flag >= IMAGE_OSD_COUNT) return;
1140         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
1141         if (duration > 1) duration += 1;
1142
1143         osd->icon_time[flag] = duration;
1144
1145         image_osd_update_schedule(osd, FALSE);
1146         image_osd_timer_schedule(osd);
1147 }
1148 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */