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