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