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);
262 FileData *fd = image_get_fd(imd);
263 gint active_marks = 0;
266 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
268 active_marks += fd->marks[mark];
271 if (active_marks > 0)
273 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
275 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
277 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
280 text2 = g_strdup_printf("%s\n%s", text, buf->str);
281 g_string_free(buf, TRUE);
290 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
291 pango_layout_set_markup(layout, text2, -1);
294 pango_layout_get_pixel_size(layout, &width, &height);
299 /* TODO: make osd color configurable --Zas */
300 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
301 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
302 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
303 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
304 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
305 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
306 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
307 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
308 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
310 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
312 g_object_unref(G_OBJECT(layout));
317 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
319 static GdkPixbuf **icons = NULL;
320 GdkPixbuf *icon = NULL;
322 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
323 if (icons[flag]) return icons[flag];
325 if (osd_icons[flag].key)
327 icon = pixbuf_inline(osd_icons[flag].key);
332 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
333 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
334 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
337 case IMAGE_OSD_ROTATE_AUTO:
338 pixbuf_set_rect(icon, 3, 8, 11, 12,
341 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
342 20, 9, 14, 15, 14, 3,
345 case IMAGE_OSD_ROTATE_USER:
347 case IMAGE_OSD_COLOR:
348 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
349 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
350 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
352 case IMAGE_OSD_FIRST:
353 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
354 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
355 12, 5, 18, 11, 6, 11,
359 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
360 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
361 12, 18, 6, 12, 18, 12,
365 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
366 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
378 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
382 if (osd->icon_id[flag]) return;
384 pixbuf = image_osd_icon_pixbuf(flag);
387 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
388 osd_icons[flag].x, osd_icons[flag].y,
392 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
394 if (osd->icon_id[flag])
396 image_overlay_remove(osd->imd, osd->icon_id[flag]);
397 osd->icon_id[flag] = 0;
401 static gint image_osd_update_cb(gpointer data)
403 OverlayStateData *osd = data;
407 if (osd->changed_states & IMAGE_STATE_IMAGE)
411 pixbuf = image_osd_info_render(osd->imd);
412 if (osd->ovl_info == 0)
414 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
415 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
419 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
421 g_object_unref(pixbuf);
428 image_overlay_remove(osd->imd, osd->ovl_info);
433 if (osd->show_status)
437 if (osd->changed_states & IMAGE_STATE_IMAGE)
439 for (i = 0; i < IMAGE_OSD_COUNT; i++)
441 if (osd_icons[i].reset) osd->icon_time[i] = 0;
445 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
447 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
448 image_osd_timer_schedule(osd);
451 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
455 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
458 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
461 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
462 image_osd_timer_schedule(osd);
465 for (i = 0; i < IMAGE_OSD_COUNT; i++)
467 if (osd->icon_time[i] > 0)
469 image_osd_icon_show(osd, i);
473 image_osd_icon_hide(osd, i);
481 for (i = 0; i < IMAGE_OSD_COUNT; i++)
483 image_osd_icon_hide(osd, i);
487 osd->changed_states = IMAGE_STATE_NONE;
492 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
494 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
496 if (osd->idle_id == -1)
498 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
502 void image_osd_update(ImageWindow *imd)
504 OverlayStateData *osd;
508 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
511 image_osd_update_schedule(osd, TRUE);
514 static gint image_osd_timer_cb(gpointer data)
516 OverlayStateData *osd = data;
518 gint changed = FALSE;
521 for (i = 0; i < IMAGE_OSD_COUNT; i++)
523 if (osd->icon_time[i] > 1)
526 if (osd->icon_time[i] < 2)
528 osd->icon_time[i] = 0;
538 if (changed) image_osd_update_schedule(osd, FALSE);
549 static void image_osd_timer_schedule(OverlayStateData *osd)
551 if (osd->timer_id == -1)
553 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
557 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
559 OverlayStateData *osd = data;
561 osd->changed_states |= state;
562 image_osd_update_schedule(osd, FALSE);
565 static void image_osd_free(OverlayStateData *osd)
569 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
570 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
576 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
577 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
579 image_set_state_func(osd->imd, NULL, NULL);
580 image_overlay_remove(osd->imd, osd->ovl_info);
582 for (i = 0; i < IMAGE_OSD_COUNT; i++)
584 image_osd_icon_hide(osd, i);
591 static void image_osd_remove(ImageWindow *imd)
593 OverlayStateData *osd;
595 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
599 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
601 OverlayStateData *osd = data;
607 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
609 OverlayStateData *osd;
611 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
614 osd = g_new0(OverlayStateData, 1);
619 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
620 G_CALLBACK(image_osd_destroy_cb), osd);
621 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
623 image_set_state_func(osd->imd, image_osd_state_cb, osd);
626 if (osd->show_info != info ||
627 osd->show_status != status)
629 osd->show_info = info;
630 osd->show_status = status;
632 image_osd_update_schedule(osd, TRUE);
636 void image_osd_set(ImageWindow *imd, gint info, gint status)
640 if (!info && !status)
642 image_osd_remove(imd);
646 image_osd_enable(imd, info, status);
649 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
651 OverlayStateData *osd;
653 if (!imd) return FALSE;
655 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
656 if (!osd) return FALSE;
658 if (info) *info = osd->show_info;
659 if (status) *status = osd->show_status;
667 2+ = show for duration tenths of a second
668 -1 = use default duration
670 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
672 OverlayStateData *osd;
676 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
679 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
680 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
681 if (duration > 1) duration += 1;
683 osd->icon_time[flag] = duration;
685 image_osd_update_schedule(osd, FALSE);
686 image_osd_timer_schedule(osd);