Add parenthesis around affection to clear a gcc warning.
[geeqie.git] / src / image-overlay.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "gqview.h"
13 #include "image-overlay.h"
14
15 #include "collect.h"
16 #include "exif.h"
17 #include "filelist.h"
18 #include "image.h"
19 #include "img-view.h"
20 #include "layout.h"
21 #include "pixbuf-renderer.h"
22 #include "pixbuf_util.h"
23
24
25 /*
26  *----------------------------------------------------------------------------
27  * image overlay
28  *----------------------------------------------------------------------------
29  */
30
31 typedef struct _OverlayStateData OverlayStateData;
32 struct _OverlayStateData {
33         ImageWindow *imd;
34         ImageState changed_states;
35
36         gint show_info;
37         gint show_status;
38
39         gint ovl_info;
40
41         gint icon_time[IMAGE_OSD_COUNT];
42         gint icon_id[IMAGE_OSD_COUNT];
43
44         gint idle_id;
45         gint timer_id;
46         gulong destroy_id;
47 };
48
49
50 typedef struct _OSDIcon OSDIcon;
51 struct _OSDIcon {
52         gint reset;     /* reset on new image */
53         gint x;         /* x, y offset */
54         gint y;
55         gchar *key;     /* inline pixbuf */
56 };
57
58 static OSDIcon osd_icons[] = {
59         {  TRUE,   0,   0, NULL },                      /* none */
60         {  TRUE, -10, -10, NULL },                      /* auto rotated */
61         {  TRUE, -10, -10, NULL },                      /* user rotated */
62         {  TRUE, -40, -10, NULL },                      /* color embedded */
63         {  TRUE, -70, -10, NULL },                      /* first image */
64         {  TRUE, -70, -10, NULL },                      /* last image */
65         { FALSE, -70, -10, NULL },                      /* osd enabled */
66         { FALSE, 0, 0, NULL }
67 };
68
69 #define OSD_DATA "overlay-data"
70
71 #define OSD_INFO_X 10
72 #define OSD_INFO_Y -10
73
74 #define IMAGE_OSD_DEFAULT_DURATION 30
75
76
77 static void image_osd_timer_schedule(OverlayStateData *osd);
78
79 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
80 {
81         gchar delim = '%', imp = '|', sep[] = " - ";
82         gchar *start, *end;
83         gint pos, prev;
84         gint last;
85         gchar *name, *data;
86         GString *new = g_string_new(str);
87         gchar *ret;
88         ExifData *exif;
89
90         exif = exif_read_fd(imd->image_fd, FALSE);
91         prev = 0;
92         last = FALSE;
93
94         while (TRUE)
95                 {
96                 start = strchr(new->str, delim);
97                 if (!start)
98                         break;
99                 end = strchr(start+1, delim);
100                 if (!end)
101                         break;
102
103                 name = g_strndup(start+1, end-start-1);
104                 pos = start-new->str;
105                 data = g_strdup(g_hash_table_lookup(vars, name));
106                 if (!data && exif)
107                         data = exif_get_data_as_text(exif, name);
108
109                 g_string_erase(new, pos, end-start+1);
110                 if (data)
111                         g_string_insert(new, pos, data);
112                 if (pos-prev == 2 && new->str[pos-1] == imp)
113                         {
114                         g_string_erase(new, --pos, 1);
115                         if (last && data)
116                                 {
117                                 g_string_insert(new, pos, sep);
118                                 pos += strlen(sep);
119                                 }
120                         }
121
122                 prev = data ? pos+strlen(data)-1 : pos-1;
123                 last = data ? TRUE : last;
124                 g_free(name);
125                 g_free(data);
126                 }
127         
128         /* search and destroy empty lines */
129         end = new->str;
130         while ((start = strchr(end, '\n')))
131                 {
132                 end = start;
133                 while (*++(end) == '\n')
134                         ;
135                 g_string_erase(new, start-new->str, end-start-1);
136                 }
137
138         g_strchomp(new->str);
139
140         ret = new->str;
141         g_string_free(new, FALSE);
142
143         return ret;
144 }
145
146 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
147 {
148         GdkPixbuf *pixbuf;
149         gint width, height;
150         PangoLayout *layout;
151         const gchar *name;
152         gchar *name_escaped;
153         gchar *text, *text2;
154         gchar *size;
155         gint n, t;
156         CollectionData *cd;
157         CollectInfo *info;
158         gchar *ct;
159         int i;
160         gint w, h;
161         GHashTable *vars;
162
163         vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
164
165         name = image_get_name(imd);
166         if (name)
167                 {
168                 name_escaped = g_markup_escape_text(name, -1);
169                 }
170         else
171                 {
172                 name_escaped = NULL;
173                 }
174
175         cd = image_get_collection(imd, &info);
176         if (cd)
177                 {
178                 gchar *buf;
179
180                 t = g_list_length(cd->list);
181                 n = g_list_index(cd->list, info) + 1;
182                 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
183                 ct = g_strdup_printf("<i>%s</i>\n", buf);
184                 g_free(buf);
185                 }
186         else
187                 {
188                 LayoutWindow *lw;
189
190                 lw = layout_find_by_image(imd);
191                 if (lw)
192                         {
193                         if (lw->slideshow)
194                                 {
195                                 n = g_list_length(lw->slideshow->list_done);
196                                 t = n + g_list_length(lw->slideshow->list);
197                                 if (n == 0) n = t;
198                                 }
199                         else
200                                 {
201                                 t = layout_list_count(lw, NULL);
202                                 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
203                                 }
204                         }
205                 else if (view_window_find_image(imd, &n, &t))
206                         {
207                         n++;
208                         }
209                 else
210                         {
211                         t = 1;
212                         n = 1;
213                         }
214
215                 if (n < 1) n = 1;
216                 if (t < 1) t = 1;
217
218                 ct = g_strdup("");
219                 }
220
221         size = text_from_size_abrev(imd->size);
222         if (!imd->unknown)
223                 {
224                 if (imd->delay_flip &&
225                     imd->il && imd->il->pixbuf &&
226                     image_get_pixbuf(imd) != imd->il->pixbuf)
227                         {
228                         w = gdk_pixbuf_get_width(imd->il->pixbuf);
229                         h = gdk_pixbuf_get_height(imd->il->pixbuf);
230                         }
231                 else
232                         {
233                         pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
234                         }
235
236                 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
237                 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
238                 g_hash_table_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h));
239                 }
240   
241         g_hash_table_insert(vars, "collection", g_strdup(ct));
242         g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
243         g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
244         g_hash_table_insert(vars, "name", g_strdup(name_escaped));
245         g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
246         g_hash_table_insert(vars, "size", g_strdup(size));
247   
248         if (!name_escaped)
249                 {
250                 text = g_strdup_printf(_("Untitled"));
251                 }
252         else
253                 {
254                 text = image_osd_mkinfo(fullscreen_info, imd, vars);
255                 }
256
257         g_free(size);
258         g_free(ct);
259         g_free(name_escaped);
260         g_hash_table_destroy(vars);
261
262         {
263         GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
264         FileData *fd = image_get_fd(imd);
265         
266         for (i=0; i < FILEDATA_MARKS_SIZE; i++) 
267                 {
268                         
269                 g_string_append_printf(buf, fd->marks[i] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + i);
270                 }
271         text2 = g_strdup_printf("%s\n%s", text, buf->str);
272         }
273         
274         layout = gtk_widget_create_pango_layout(imd->pr, NULL);
275         pango_layout_set_markup(layout, text2, -1);
276         g_free(text2);
277         g_free(text);
278     
279         pango_layout_get_pixel_size(layout, &width, &height);
280
281         width += 10;
282         height += 10;
283
284         /* TODO: make osd color configurable --Zas */
285         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
286         pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
287         pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
288         pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
289         pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
290         pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
291         pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
292         pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
293         pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
294
295         pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
296
297         g_object_unref(G_OBJECT(layout));
298
299         return pixbuf;
300 }
301
302 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
303 {
304         static GdkPixbuf **icons = NULL;
305         GdkPixbuf *icon = NULL;
306
307         if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
308         if (icons[flag]) return icons[flag];
309
310         if (osd_icons[flag].key)
311                 {
312                 icon = pixbuf_inline(osd_icons[flag].key);
313                 }
314
315         if (!icon)
316                 {
317                 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
318                 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
319                 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
320                 switch (flag)
321                         {
322                         case IMAGE_OSD_ROTATE_AUTO:
323                                 pixbuf_set_rect(icon, 3, 8, 11, 12,
324                                                 0, 0, 0, 255,
325                                                 3, 0, 3, 0);
326                                 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
327                                                      20, 9, 14, 15, 14, 3,
328                                                      0, 0, 0, 255);
329                                 break;
330                         case IMAGE_OSD_ROTATE_USER:
331                                 break;
332                         case IMAGE_OSD_COLOR:
333                                 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
334                                 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
335                                 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
336                                 break;
337                         case IMAGE_OSD_FIRST:
338                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
339                                 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
340                                                      12, 5, 18, 11, 6, 11,
341                                                      0, 0, 0, 255);
342                                 break;
343                         case IMAGE_OSD_LAST:
344                                 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
345                                 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
346                                                      12, 18, 6, 12, 18, 12,
347                                                      0, 0, 0, 255);
348                                 break;
349                         case IMAGE_OSD_ICON:
350                                 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
351                                 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
352                                 break;
353                         default:
354                                 break;
355                         }
356                 }
357
358         icons[flag] = icon;
359
360         return icon;
361 }
362
363 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
364 {
365         GdkPixbuf *pixbuf;
366
367         if (osd->icon_id[flag]) return;
368
369         pixbuf = image_osd_icon_pixbuf(flag);
370         if (!pixbuf) return;
371
372         osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
373                                                osd_icons[flag].x, osd_icons[flag].y,
374                                                TRUE, FALSE);
375 }
376
377 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
378 {
379         if (osd->icon_id[flag])
380                 {
381                 image_overlay_remove(osd->imd, osd->icon_id[flag]);
382                 osd->icon_id[flag] = 0;
383                 }
384 }
385
386 static gint image_osd_update_cb(gpointer data)
387 {
388         OverlayStateData *osd = data;
389
390         if (osd->show_info)
391                 {
392                 if (osd->changed_states & IMAGE_STATE_IMAGE)
393                         {
394                         GdkPixbuf *pixbuf;
395
396                         pixbuf = image_osd_info_render(osd->imd);
397                         if (osd->ovl_info == 0)
398                                 {
399                                 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
400                                                                   OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
401                                 }
402                         else
403                                 {
404                                 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
405                                 }
406                         g_object_unref(pixbuf);
407                         }
408                 }
409         else
410                 {
411                 if (osd->ovl_info)
412                         {
413                         image_overlay_remove(osd->imd, osd->ovl_info);
414                         osd->ovl_info = 0;
415                         }
416                 }
417
418         if (osd->show_status)
419                 {
420                 gint i;
421
422                 if (osd->changed_states & IMAGE_STATE_IMAGE)
423                         {
424                         for (i = 0; i < IMAGE_OSD_COUNT; i++)
425                                 {
426                                 if (osd_icons[i].reset) osd->icon_time[i] = 0;
427                                 }
428                         }
429
430                 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
431                         {
432                         osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
433                         image_osd_timer_schedule(osd);
434                         }
435
436                 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
437                         {
438                         gint n = 0;
439
440                         if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
441                                 {
442                                 n = 1;
443                                 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
444                                 }
445
446                         osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
447                         image_osd_timer_schedule(osd);
448                         }
449
450                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
451                         {
452                         if (osd->icon_time[i] > 0)
453                                 {
454                                 image_osd_icon_show(osd, i);
455                                 }
456                         else
457                                 {
458                                 image_osd_icon_hide(osd, i);
459                                 }
460                         }
461                 }
462         else
463                 {
464                 gint i;
465
466                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
467                         {
468                         image_osd_icon_hide(osd, i);
469                         }
470                 }
471
472         osd->changed_states = IMAGE_STATE_NONE;
473         osd->idle_id = -1;
474         return FALSE;
475 }
476
477 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
478 {
479         if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
480
481         if (osd->idle_id == -1)
482                 {
483                 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
484                 }
485 }
486
487 void image_osd_update(ImageWindow *imd)
488 {
489         OverlayStateData *osd;
490
491         if (!imd) return;
492
493         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
494         if (!osd) return;
495
496         image_osd_update_schedule(osd, TRUE);
497 }
498
499 static gint image_osd_timer_cb(gpointer data)
500 {
501         OverlayStateData *osd = data;
502         gint done = TRUE;
503         gint changed = FALSE;
504         gint i;
505
506         for (i = 0; i < IMAGE_OSD_COUNT; i++)
507                 {
508                 if (osd->icon_time[i] > 1)
509                         {
510                         osd->icon_time[i]--;
511                         if (osd->icon_time[i] < 2)
512                                 {
513                                 osd->icon_time[i] = 0;
514                                 changed = TRUE;
515                                 }
516                         else
517                                 {
518                                 done = FALSE;
519                                 }
520                         }
521                 }
522
523         if (changed) image_osd_update_schedule(osd, FALSE);
524
525         if (done)
526                 {
527                 osd->timer_id = -1;
528                 return FALSE;
529                 }
530
531         return TRUE;
532 }
533
534 static void image_osd_timer_schedule(OverlayStateData *osd)
535 {
536         if (osd->timer_id == -1)
537                 {
538                 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
539                 }
540 }
541
542 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
543 {
544         OverlayStateData *osd = data;
545
546         osd->changed_states |= state;
547         image_osd_update_schedule(osd, FALSE);
548 }
549
550 static void image_osd_free(OverlayStateData *osd)
551 {
552         if (!osd) return;
553
554         if (osd->idle_id != -1) g_source_remove(osd->idle_id);
555         if (osd->timer_id != -1) g_source_remove(osd->timer_id);
556
557         if (osd->imd)
558                 {
559                 gint i;
560
561                 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
562                 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
563
564                 image_set_state_func(osd->imd, NULL, NULL);
565                 image_overlay_remove(osd->imd, osd->ovl_info);
566
567                 for (i = 0; i < IMAGE_OSD_COUNT; i++)
568                         {
569                         image_osd_icon_hide(osd, i);
570                         }
571                 }
572
573         g_free(osd);
574 }
575
576 static void image_osd_remove(ImageWindow *imd)
577 {
578         OverlayStateData *osd;
579
580         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
581         image_osd_free(osd);
582 }
583
584 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
585 {
586         OverlayStateData *osd = data;
587
588         osd->imd = NULL;
589         image_osd_free(osd);
590 }
591
592 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
593 {
594         OverlayStateData *osd;
595
596         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
597         if (!osd)
598                 {
599                 osd = g_new0(OverlayStateData, 1);
600                 osd->imd = imd;
601                 osd->idle_id = -1;
602                 osd->timer_id = -1;
603
604                 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
605                                                    G_CALLBACK(image_osd_destroy_cb), osd);
606                 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
607
608                 image_set_state_func(osd->imd, image_osd_state_cb, osd);
609                 }
610
611         if (osd->show_info != info ||
612             osd->show_status != status)
613                 {
614                 osd->show_info = info;
615                 osd->show_status = status;
616
617                 image_osd_update_schedule(osd, TRUE);
618                 }
619 }
620
621 void image_osd_set(ImageWindow *imd, gint info, gint status)
622 {
623         if (!imd) return;
624
625         if (!info && !status)
626                 {
627                 image_osd_remove(imd);
628                 return;
629                 }
630
631         image_osd_enable(imd, info, status);
632 }
633
634 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
635 {
636         OverlayStateData *osd;
637
638         if (!imd) return FALSE;
639
640         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
641         if (!osd) return FALSE;
642
643         if (info) *info = osd->show_info;
644         if (status) *status = osd->show_status;
645
646         return TRUE;
647 }
648
649 /* duration:
650     0 = hide
651     1 = show
652    2+ = show for duration tenths of a second
653    -1 = use default duration
654  */
655 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
656 {
657         OverlayStateData *osd;
658
659         if (!imd) return;
660
661         osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
662         if (!osd) return;
663
664         if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
665         if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
666         if (duration > 1) duration += 1;
667
668         osd->icon_time[flag] = duration;
669
670         image_osd_update_schedule(osd, FALSE);
671         image_osd_timer_schedule(osd);
672 }