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