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)
163 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
165 name = image_get_name(imd);
168 name_escaped = g_markup_escape_text(name, -1);
175 cd = image_get_collection(imd, &info);
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);
190 lw = layout_find_by_image(imd);
195 n = g_list_length(lw->slideshow->list_done);
196 t = n + g_list_length(lw->slideshow->list);
201 t = layout_list_count(lw, NULL);
202 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
205 else if (view_window_find_image(imd, &n, &t))
221 size = text_from_size_abrev(imd->size);
224 if (imd->delay_flip &&
225 imd->il && imd->il->pixbuf &&
226 image_get_pixbuf(imd) != imd->il->pixbuf)
228 w = gdk_pixbuf_get_width(imd->il->pixbuf);
229 h = gdk_pixbuf_get_height(imd->il->pixbuf);
233 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
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));
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));
250 text = g_strdup_printf(_("Untitled"));
254 text = image_osd_mkinfo(fullscreen_info, imd, vars);
259 g_free(name_escaped);
260 g_hash_table_destroy(vars);
263 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
264 FileData *fd = image_get_fd(imd);
266 for (i=0; i < FILEDATA_MARKS_SIZE; i++)
269 g_string_append_printf(buf, fd->marks[i] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + i);
271 text2 = g_strdup_printf("%s\n%s", text, buf->str);
274 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
275 pango_layout_set_markup(layout, text2, -1);
279 pango_layout_get_pixel_size(layout, &width, &height);
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);
295 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
297 g_object_unref(G_OBJECT(layout));
302 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
304 static GdkPixbuf **icons = NULL;
305 GdkPixbuf *icon = NULL;
307 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
308 if (icons[flag]) return icons[flag];
310 if (osd_icons[flag].key)
312 icon = pixbuf_inline(osd_icons[flag].key);
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);
322 case IMAGE_OSD_ROTATE_AUTO:
323 pixbuf_set_rect(icon, 3, 8, 11, 12,
326 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
327 20, 9, 14, 15, 14, 3,
330 case IMAGE_OSD_ROTATE_USER:
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);
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,
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,
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);
363 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
367 if (osd->icon_id[flag]) return;
369 pixbuf = image_osd_icon_pixbuf(flag);
372 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
373 osd_icons[flag].x, osd_icons[flag].y,
377 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
379 if (osd->icon_id[flag])
381 image_overlay_remove(osd->imd, osd->icon_id[flag]);
382 osd->icon_id[flag] = 0;
386 static gint image_osd_update_cb(gpointer data)
388 OverlayStateData *osd = data;
392 if (osd->changed_states & IMAGE_STATE_IMAGE)
396 pixbuf = image_osd_info_render(osd->imd);
397 if (osd->ovl_info == 0)
399 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
400 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
404 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
406 g_object_unref(pixbuf);
413 image_overlay_remove(osd->imd, osd->ovl_info);
418 if (osd->show_status)
422 if (osd->changed_states & IMAGE_STATE_IMAGE)
424 for (i = 0; i < IMAGE_OSD_COUNT; i++)
426 if (osd_icons[i].reset) osd->icon_time[i] = 0;
430 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
432 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
433 image_osd_timer_schedule(osd);
436 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
440 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
443 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
446 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
447 image_osd_timer_schedule(osd);
450 for (i = 0; i < IMAGE_OSD_COUNT; i++)
452 if (osd->icon_time[i] > 0)
454 image_osd_icon_show(osd, i);
458 image_osd_icon_hide(osd, i);
466 for (i = 0; i < IMAGE_OSD_COUNT; i++)
468 image_osd_icon_hide(osd, i);
472 osd->changed_states = IMAGE_STATE_NONE;
477 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
479 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
481 if (osd->idle_id == -1)
483 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
487 void image_osd_update(ImageWindow *imd)
489 OverlayStateData *osd;
493 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
496 image_osd_update_schedule(osd, TRUE);
499 static gint image_osd_timer_cb(gpointer data)
501 OverlayStateData *osd = data;
503 gint changed = FALSE;
506 for (i = 0; i < IMAGE_OSD_COUNT; i++)
508 if (osd->icon_time[i] > 1)
511 if (osd->icon_time[i] < 2)
513 osd->icon_time[i] = 0;
523 if (changed) image_osd_update_schedule(osd, FALSE);
534 static void image_osd_timer_schedule(OverlayStateData *osd)
536 if (osd->timer_id == -1)
538 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
542 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
544 OverlayStateData *osd = data;
546 osd->changed_states |= state;
547 image_osd_update_schedule(osd, FALSE);
550 static void image_osd_free(OverlayStateData *osd)
554 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
555 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
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);
564 image_set_state_func(osd->imd, NULL, NULL);
565 image_overlay_remove(osd->imd, osd->ovl_info);
567 for (i = 0; i < IMAGE_OSD_COUNT; i++)
569 image_osd_icon_hide(osd, i);
576 static void image_osd_remove(ImageWindow *imd)
578 OverlayStateData *osd;
580 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
584 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
586 OverlayStateData *osd = data;
592 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
594 OverlayStateData *osd;
596 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
599 osd = g_new0(OverlayStateData, 1);
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);
608 image_set_state_func(osd->imd, image_osd_state_cb, osd);
611 if (osd->show_info != info ||
612 osd->show_status != status)
614 osd->show_info = info;
615 osd->show_status = status;
617 image_osd_update_schedule(osd, TRUE);
621 void image_osd_set(ImageWindow *imd, gint info, gint status)
625 if (!info && !status)
627 image_osd_remove(imd);
631 image_osd_enable(imd, info, status);
634 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
636 OverlayStateData *osd;
638 if (!imd) return FALSE;
640 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
641 if (!osd) return FALSE;
643 if (info) *info = osd->show_info;
644 if (status) *status = osd->show_status;
652 2+ = show for duration tenths of a second
653 -1 = use default duration
655 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
657 OverlayStateData *osd;
661 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
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;
668 osd->icon_time[flag] = duration;
670 image_osd_update_schedule(osd, FALSE);
671 image_osd_timer_schedule(osd);