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