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!
13 #include "image-overlay.h"
21 #include "pixbuf-renderer.h"
22 #include "pixbuf_util.h"
26 *----------------------------------------------------------------------------
28 *----------------------------------------------------------------------------
31 typedef struct _OverlayStateData OverlayStateData;
32 struct _OverlayStateData {
34 ImageState changed_states;
41 gint icon_time[IMAGE_OSD_COUNT];
42 gint icon_id[IMAGE_OSD_COUNT];
50 typedef struct _OSDIcon OSDIcon;
52 gint reset; /* reset on new image */
53 gint x; /* x, y offset */
55 gchar *key; /* inline pixbuf */
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 */
69 #define OSD_DATA "overlay-data"
72 #define OSD_INFO_Y -10
74 #define IMAGE_OSD_DEFAULT_DURATION 30
77 static void image_osd_timer_schedule(OverlayStateData *osd);
79 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
81 gchar delim = '%', imp = '|', sep[] = " - ";
86 GString *new = g_string_new(str);
90 exif = exif_read_fd(imd->image_fd, FALSE);
96 start = strchr(new->str, delim);
99 end = strchr(start+1, delim);
103 name = g_strndup(start+1, end-start-1);
104 pos = start-new->str;
105 data = g_strdup(g_hash_table_lookup(vars, name));
107 data = exif_get_data_as_text(exif, name);
109 g_string_erase(new, pos, end-start+1);
111 g_string_insert(new, pos, data);
112 if (pos-prev == 2 && new->str[pos-1] == imp)
114 g_string_erase(new, --pos, 1);
117 g_string_insert(new, pos, sep);
122 prev = data ? pos+strlen(data)-1 : pos-1;
123 last = data ? TRUE : last;
128 /* search and destroy empty lines */
130 while ((start = strchr(end, '\n')))
133 while (*++(end) == '\n')
135 g_string_erase(new, start-new->str, end-start-1);
138 g_strchomp(new->str);
141 g_string_free(new, FALSE);
146 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
162 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
164 name = image_get_name(imd);
167 name_escaped = g_markup_escape_text(name, -1);
174 cd = image_get_collection(imd, &info);
179 t = g_list_length(cd->list);
180 n = g_list_index(cd->list, info) + 1;
181 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
182 ct = g_strdup_printf("<i>%s</i>\n", buf);
189 lw = layout_find_by_image(imd);
194 n = g_list_length(lw->slideshow->list_done);
195 t = n + g_list_length(lw->slideshow->list);
200 t = layout_list_count(lw, NULL);
201 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
204 else if (view_window_find_image(imd, &n, &t))
220 size = text_from_size_abrev(imd->size);
223 if (imd->delay_flip &&
224 imd->il && imd->il->pixbuf &&
225 image_get_pixbuf(imd) != imd->il->pixbuf)
227 w = gdk_pixbuf_get_width(imd->il->pixbuf);
228 h = gdk_pixbuf_get_height(imd->il->pixbuf);
232 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
235 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
236 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
237 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
240 g_hash_table_insert(vars, "collection", g_strdup(ct));
241 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
242 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
243 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
244 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
245 g_hash_table_insert(vars, "size", g_strdup(size));
249 text = g_strdup_printf(_("Untitled"));
253 text = image_osd_mkinfo(fullscreen_info, imd, vars);
258 g_free(name_escaped);
259 g_hash_table_destroy(vars);
263 FileData *fd = image_get_fd(imd);
265 if (fd) /* fd may be null after file deletion */
267 gint active_marks = 0;
270 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
272 active_marks += fd->marks[mark];
275 if (active_marks > 0)
277 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
279 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
281 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
284 text2 = g_strdup_printf("%s\n%s", text, buf->str);
285 g_string_free(buf, TRUE);
291 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
292 pango_layout_set_markup(layout, text2, -1);
295 pango_layout_get_pixel_size(layout, &width, &height);
300 /* TODO: make osd color configurable --Zas */
301 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
302 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
303 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
304 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
305 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
306 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
307 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
308 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
309 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
311 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
313 g_object_unref(G_OBJECT(layout));
318 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
320 static GdkPixbuf **icons = NULL;
321 GdkPixbuf *icon = NULL;
323 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
324 if (icons[flag]) return icons[flag];
326 if (osd_icons[flag].key)
328 icon = pixbuf_inline(osd_icons[flag].key);
333 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
334 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
335 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
338 case IMAGE_OSD_ROTATE_AUTO:
339 pixbuf_set_rect(icon, 3, 8, 11, 12,
342 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
343 20, 9, 14, 15, 14, 3,
346 case IMAGE_OSD_ROTATE_USER:
348 case IMAGE_OSD_COLOR:
349 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
350 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
351 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
353 case IMAGE_OSD_FIRST:
354 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
355 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
356 12, 5, 18, 11, 6, 11,
360 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
361 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
362 12, 18, 6, 12, 18, 12,
366 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
367 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
379 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
383 if (osd->icon_id[flag]) return;
385 pixbuf = image_osd_icon_pixbuf(flag);
388 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
389 osd_icons[flag].x, osd_icons[flag].y,
393 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
395 if (osd->icon_id[flag])
397 image_overlay_remove(osd->imd, osd->icon_id[flag]);
398 osd->icon_id[flag] = 0;
402 static gint image_osd_update_cb(gpointer data)
404 OverlayStateData *osd = data;
408 if (osd->changed_states & IMAGE_STATE_IMAGE)
412 pixbuf = image_osd_info_render(osd->imd);
413 if (osd->ovl_info == 0)
415 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
416 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
420 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
422 g_object_unref(pixbuf);
429 image_overlay_remove(osd->imd, osd->ovl_info);
434 if (osd->show_status)
438 if (osd->changed_states & IMAGE_STATE_IMAGE)
440 for (i = 0; i < IMAGE_OSD_COUNT; i++)
442 if (osd_icons[i].reset) osd->icon_time[i] = 0;
446 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
448 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
449 image_osd_timer_schedule(osd);
452 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
456 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
459 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
462 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
463 image_osd_timer_schedule(osd);
466 for (i = 0; i < IMAGE_OSD_COUNT; i++)
468 if (osd->icon_time[i] > 0)
470 image_osd_icon_show(osd, i);
474 image_osd_icon_hide(osd, i);
482 for (i = 0; i < IMAGE_OSD_COUNT; i++)
484 image_osd_icon_hide(osd, i);
488 osd->changed_states = IMAGE_STATE_NONE;
493 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
495 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
497 if (osd->idle_id == -1)
499 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
503 void image_osd_update(ImageWindow *imd)
505 OverlayStateData *osd;
509 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
512 image_osd_update_schedule(osd, TRUE);
515 static gint image_osd_timer_cb(gpointer data)
517 OverlayStateData *osd = data;
519 gint changed = FALSE;
522 for (i = 0; i < IMAGE_OSD_COUNT; i++)
524 if (osd->icon_time[i] > 1)
527 if (osd->icon_time[i] < 2)
529 osd->icon_time[i] = 0;
539 if (changed) image_osd_update_schedule(osd, FALSE);
550 static void image_osd_timer_schedule(OverlayStateData *osd)
552 if (osd->timer_id == -1)
554 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
558 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
560 OverlayStateData *osd = data;
562 osd->changed_states |= state;
563 image_osd_update_schedule(osd, FALSE);
566 static void image_osd_free(OverlayStateData *osd)
570 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
571 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
577 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
578 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
580 image_set_state_func(osd->imd, NULL, NULL);
581 image_overlay_remove(osd->imd, osd->ovl_info);
583 for (i = 0; i < IMAGE_OSD_COUNT; i++)
585 image_osd_icon_hide(osd, i);
592 static void image_osd_remove(ImageWindow *imd)
594 OverlayStateData *osd;
596 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
600 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
602 OverlayStateData *osd = data;
608 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
610 OverlayStateData *osd;
612 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
615 osd = g_new0(OverlayStateData, 1);
620 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
621 G_CALLBACK(image_osd_destroy_cb), osd);
622 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
624 image_set_state_func(osd->imd, image_osd_state_cb, osd);
627 if (osd->show_info != info ||
628 osd->show_status != status)
630 osd->show_info = info;
631 osd->show_status = status;
633 image_osd_update_schedule(osd, TRUE);
637 void image_osd_set(ImageWindow *imd, gint info, gint status)
641 if (!info && !status)
643 image_osd_remove(imd);
647 image_osd_enable(imd, info, status);
650 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
652 OverlayStateData *osd;
654 if (!imd) return FALSE;
656 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
657 if (!osd) return FALSE;
659 if (info) *info = osd->show_info;
660 if (status) *status = osd->show_status;
668 2+ = show for duration tenths of a second
669 -1 = use default duration
671 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
673 OverlayStateData *osd;
677 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
680 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
681 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
682 if (duration > 1) duration += 1;
684 osd->icon_time[flag] = duration;
686 image_osd_update_schedule(osd, FALSE);
687 image_osd_timer_schedule(osd);