4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
14 #include "image-overlay.h"
22 #include "pixbuf-renderer.h"
23 #include "pixbuf_util.h"
24 #include "histogram.h"
28 *----------------------------------------------------------------------------
30 *----------------------------------------------------------------------------
33 typedef struct _OverlayStateData OverlayStateData;
34 struct _OverlayStateData {
36 ImageState changed_states;
43 gint icon_time[IMAGE_OSD_COUNT];
44 gint icon_id[IMAGE_OSD_COUNT];
52 typedef struct _OSDIcon OSDIcon;
54 gint reset; /* reset on new image */
55 gint x; /* x, y offset */
57 gchar *key; /* inline pixbuf */
60 static OSDIcon osd_icons[] = {
61 { TRUE, 0, 0, NULL }, /* none */
62 { TRUE, -10, -10, NULL }, /* auto rotated */
63 { TRUE, -10, -10, NULL }, /* user rotated */
64 { TRUE, -40, -10, NULL }, /* color embedded */
65 { TRUE, -70, -10, NULL }, /* first image */
66 { TRUE, -70, -10, NULL }, /* last image */
67 { FALSE, -70, -10, NULL }, /* osd enabled */
71 #define OSD_DATA "overlay-data"
74 #define OSD_INFO_Y -10
76 #define IMAGE_OSD_DEFAULT_DURATION 30
78 #define HISTOGRAM_HEIGHT 140
80 void set_default_image_overlay_template_string(ConfOptions *options)
82 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
83 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
87 *----------------------------------------------------------------------------
89 *----------------------------------------------------------------------------
93 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
95 imd->histogram_enabled = !!(x);
96 if (imd->histogram_enabled && !imd->histogram)
97 imd->histogram = histogram_new();
100 gint image_osd_histogram_onoff_status(ImageWindow *imd)
102 return imd->histogram_enabled;
105 void image_osd_histogram_chan_toggle(ImageWindow *imd)
108 histogram_set_channel(imd->histogram, (histogram_get_channel(imd->histogram) +1)%HCHAN_COUNT);
111 void image_osd_histogram_log_toggle(ImageWindow *imd)
114 histogram_set_mode(imd->histogram, !histogram_get_mode(imd->histogram));
119 static void image_osd_timer_schedule(OverlayStateData *osd);
121 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
123 gchar delim = '%', imp = '|', sep[] = " - ";
132 if (!str || !*str) return g_strdup("");
134 new = g_string_new(str);
136 exif = exif_read_fd(imd->image_fd);
147 start = strchr(new->str, delim);
150 end = strchr(start+1, delim);
154 for (p = end; p > start; p--)
156 if (*p == ':' && was_digit)
161 was_digit = g_ascii_isdigit(*p);
164 if (trunc) limit = atoi(trunc+1);
166 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
168 pos = start-new->str;
169 data = g_strdup(g_hash_table_lookup(vars, name));
170 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
172 data = exif_get_data_as_text(exif, name);
173 if (data && *data && limit > 0 && strlen(data) > limit + 3)
175 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
180 g_string_erase(new, pos, end-start+1);
182 g_string_insert(new, pos, data);
183 if (pos-prev == 2 && new->str[pos-1] == imp)
185 g_string_erase(new, --pos, 1);
188 g_string_insert(new, pos, sep);
193 prev = data ? pos+strlen(data)-1 : pos-1;
194 last = data ? TRUE : last;
199 /* search and destroy empty lines */
201 while ((start = strchr(end, '\n')))
204 while (*++(end) == '\n')
206 g_string_erase(new, start-new->str, end-start-1);
209 g_strchomp(new->str);
212 g_string_free(new, FALSE);
217 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
219 GdkPixbuf *pixbuf = NULL;
229 GdkPixbuf *imgpixbuf = NULL;
230 LayoutWindow *lw = NULL;
236 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
238 name = image_get_name(imd);
241 name_escaped = g_markup_escape_text(name, -1);
248 cd = image_get_collection(imd, &info);
253 t = g_list_length(cd->list);
254 n = g_list_index(cd->list, info) + 1;
255 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
256 ct = g_strdup_printf("<i>%s</i>\n", buf);
261 lw = layout_find_by_image(imd);
266 n = g_list_length(lw->slideshow->list_done);
267 t = n + g_list_length(lw->slideshow->list);
272 t = layout_list_count(lw, NULL);
273 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
276 else if (view_window_find_image(imd, &n, &t))
292 size = text_from_size_abrev(imd->size);
295 if (imd->delay_flip &&
296 imd->il && imd->il->pixbuf &&
297 image_get_pixbuf(imd) != imd->il->pixbuf)
299 w = gdk_pixbuf_get_width(imd->il->pixbuf);
300 h = gdk_pixbuf_get_height(imd->il->pixbuf);
301 imgpixbuf = imd->il->pixbuf;
305 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
306 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
309 if (imgpixbuf && imd->histogram_enabled && imd->histogram
310 && (!imd->il || imd->il->done))
313 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
314 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
315 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
318 g_hash_table_insert(vars, "collection", g_strdup(ct));
319 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
320 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
321 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
322 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
323 g_hash_table_insert(vars, "size", g_strdup(size));
324 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
328 text = g_strdup_printf(_("Untitled"));
332 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
337 g_free(name_escaped);
338 g_hash_table_destroy(vars);
341 FileData *fd = image_get_fd(imd);
343 if (fd) /* fd may be null after file deletion */
345 gint active_marks = 0;
349 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
351 active_marks += fd->marks[mark];
355 if (active_marks > 0)
357 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
359 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
361 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
365 text2 = g_strdup_printf("%s\n%s", text, buf->str);
367 text2 = g_strdup(buf->str);
368 g_string_free(buf, TRUE);
376 text2 = g_strdup_printf("%s\n%s", text, histogram_label(imd->histogram));
378 text2 = g_strdup(histogram_label(imd->histogram));
385 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
386 pango_layout_set_markup(layout, text, -1);
389 pango_layout_get_pixel_size(layout, &width, &height);
390 /* with empty text width is set to 0, but not height) */
393 else if (height == 0)
395 if (width > 0) width += 10;
396 if (height > 0) height += 10;
400 histogram_read(imd->histogram, imgpixbuf);
401 if (width < 266) width = 266;
402 height += HISTOGRAM_HEIGHT + 5;
405 if (width > 0 && height > 0)
407 /* TODO: make osd color configurable --Zas */
408 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
409 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
410 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
411 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
412 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
413 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
414 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
415 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
416 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
419 histogram_draw(imd->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
421 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
424 g_object_unref(G_OBJECT(layout));
429 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
431 static GdkPixbuf **icons = NULL;
432 GdkPixbuf *icon = NULL;
434 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
435 if (icons[flag]) return icons[flag];
437 if (osd_icons[flag].key)
439 icon = pixbuf_inline(osd_icons[flag].key);
444 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
445 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
446 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
449 case IMAGE_OSD_ROTATE_AUTO:
450 pixbuf_set_rect(icon, 3, 8, 11, 12,
453 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
454 20, 9, 14, 15, 14, 3,
457 case IMAGE_OSD_ROTATE_USER:
459 case IMAGE_OSD_COLOR:
460 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
461 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
462 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
464 case IMAGE_OSD_FIRST:
465 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
466 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
467 12, 5, 18, 11, 6, 11,
471 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
472 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
473 12, 18, 6, 12, 18, 12,
477 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
478 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
490 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
494 if (osd->icon_id[flag]) return;
496 pixbuf = image_osd_icon_pixbuf(flag);
499 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
500 osd_icons[flag].x, osd_icons[flag].y,
504 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
506 if (osd->icon_id[flag])
508 image_overlay_remove(osd->imd, osd->icon_id[flag]);
509 osd->icon_id[flag] = 0;
513 static gint image_osd_update_cb(gpointer data)
515 OverlayStateData *osd = data;
517 osd->imd->overlay_show_zoom = FALSE;
521 if (osd->changed_states & IMAGE_STATE_IMAGE)
525 pixbuf = image_osd_info_render(osd->imd);
528 if (osd->ovl_info == 0)
530 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
531 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
535 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
537 g_object_unref(pixbuf);
539 else if (osd->ovl_info)
541 image_overlay_remove(osd->imd, osd->ovl_info);
550 image_overlay_remove(osd->imd, osd->ovl_info);
555 if (osd->show_status)
559 if (osd->changed_states & IMAGE_STATE_IMAGE)
561 for (i = 0; i < IMAGE_OSD_COUNT; i++)
563 if (osd_icons[i].reset) osd->icon_time[i] = 0;
567 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
569 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
570 image_osd_timer_schedule(osd);
573 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
577 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
580 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
583 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
584 image_osd_timer_schedule(osd);
587 for (i = 0; i < IMAGE_OSD_COUNT; i++)
589 if (osd->icon_time[i] > 0)
591 image_osd_icon_show(osd, i);
595 image_osd_icon_hide(osd, i);
603 for (i = 0; i < IMAGE_OSD_COUNT; i++)
605 image_osd_icon_hide(osd, i);
609 if (osd->imd->il && osd->imd->il->done)
610 osd->changed_states = IMAGE_STATE_NONE;
615 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
617 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
619 if (osd->idle_id == -1)
621 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
625 void image_osd_update(ImageWindow *imd)
627 OverlayStateData *osd;
631 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
634 image_osd_update_schedule(osd, TRUE);
637 static gint image_osd_timer_cb(gpointer data)
639 OverlayStateData *osd = data;
641 gint changed = FALSE;
644 for (i = 0; i < IMAGE_OSD_COUNT; i++)
646 if (osd->icon_time[i] > 1)
649 if (osd->icon_time[i] < 2)
651 osd->icon_time[i] = 0;
661 if (changed) image_osd_update_schedule(osd, FALSE);
672 static void image_osd_timer_schedule(OverlayStateData *osd)
674 if (osd->timer_id == -1)
676 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
680 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
682 OverlayStateData *osd = data;
684 osd->changed_states |= state;
685 image_osd_update_schedule(osd, FALSE);
688 static void image_osd_free(OverlayStateData *osd)
692 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
693 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
699 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
700 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
702 image_set_state_func(osd->imd, NULL, NULL);
703 image_overlay_remove(osd->imd, osd->ovl_info);
705 for (i = 0; i < IMAGE_OSD_COUNT; i++)
707 image_osd_icon_hide(osd, i);
714 static void image_osd_remove(ImageWindow *imd)
716 OverlayStateData *osd;
718 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
722 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
724 OverlayStateData *osd = data;
730 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
732 OverlayStateData *osd;
734 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
737 osd = g_new0(OverlayStateData, 1);
742 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
743 G_CALLBACK(image_osd_destroy_cb), osd);
744 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
746 image_set_state_func(osd->imd, image_osd_state_cb, osd);
749 if (osd->show_info != info ||
750 osd->show_status != status)
752 osd->show_info = info;
753 osd->show_status = status;
755 image_osd_update_schedule(osd, TRUE);
759 void image_osd_set(ImageWindow *imd, gint info, gint status)
763 if (!info && !status)
765 image_osd_remove(imd);
769 image_osd_enable(imd, info, status);
772 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
774 OverlayStateData *osd;
776 if (!imd) return FALSE;
778 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
779 if (!osd) return FALSE;
781 if (info) *info = osd->show_info;
782 if (status) *status = osd->show_status;
790 2+ = show for duration tenths of a second
791 -1 = use default duration
793 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
795 OverlayStateData *osd;
799 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
802 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
803 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
804 if (duration > 1) duration += 1;
806 osd->icon_time[flag] = duration;
808 image_osd_update_schedule(osd, FALSE);
809 image_osd_timer_schedule(osd);