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 static void image_osd_timer_schedule(OverlayStateData *osd);
83 void set_default_image_overlay_template_string(ConfOptions *options)
85 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
86 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
90 *----------------------------------------------------------------------------
92 *----------------------------------------------------------------------------
96 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
98 imd->histogram_enabled = !!(x);
99 if (imd->histogram_enabled && !imd->histogram)
100 imd->histogram = histogram_new();
103 gint image_osd_histogram_onoff_status(ImageWindow *imd)
105 return imd->histogram_enabled;
108 void image_osd_histogram_chan_toggle(ImageWindow *imd)
111 histogram_set_channel(imd->histogram, (histogram_get_channel(imd->histogram) +1)%HCHAN_COUNT);
114 void image_osd_histogram_log_toggle(ImageWindow *imd)
117 histogram_set_mode(imd->histogram, !histogram_get_mode(imd->histogram));
120 void image_osd_toggle(ImageWindow *imd)
122 if (image_osd_get(imd, NULL, NULL))
124 if (image_osd_histogram_onoff_status(imd))
126 image_osd_histogram_onoff_toggle(imd, 0);
127 image_osd_update(imd);
131 image_osd_set(imd, FALSE, FALSE);
136 image_osd_set(imd, TRUE, TRUE);
137 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
138 image_osd_histogram_onoff_toggle(imd, 1);
139 image_osd_update(imd);
143 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
145 gchar delim = '%', imp = '|', sep[] = " - ";
154 if (!str || !*str) return g_strdup("");
156 new = g_string_new(str);
158 exif = exif_read_fd(imd->image_fd);
169 start = strchr(new->str, delim);
172 end = strchr(start+1, delim);
176 for (p = end; p > start; p--)
178 if (*p == ':' && was_digit)
183 was_digit = g_ascii_isdigit(*p);
186 if (trunc) limit = atoi(trunc+1);
188 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
190 pos = start-new->str;
191 data = g_strdup(g_hash_table_lookup(vars, name));
192 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
194 data = exif_get_data_as_text(exif, name);
195 if (data && *data && limit > 0 && strlen(data) > limit + 3)
197 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
202 g_string_erase(new, pos, end-start+1);
204 g_string_insert(new, pos, data);
205 if (pos-prev == 2 && new->str[pos-1] == imp)
207 g_string_erase(new, --pos, 1);
210 g_string_insert(new, pos, sep);
215 prev = data ? pos+strlen(data)-1 : pos-1;
216 last = data ? TRUE : last;
221 /* search and destroy empty lines */
223 while ((start = strchr(end, '\n')))
226 while (*++(end) == '\n')
228 g_string_erase(new, start-new->str, end-start-1);
231 g_strchomp(new->str);
234 g_string_free(new, FALSE);
239 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
241 GdkPixbuf *pixbuf = NULL;
251 GdkPixbuf *imgpixbuf = NULL;
252 LayoutWindow *lw = NULL;
258 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
260 name = image_get_name(imd);
263 name_escaped = g_markup_escape_text(name, -1);
270 cd = image_get_collection(imd, &info);
275 t = g_list_length(cd->list);
276 n = g_list_index(cd->list, info) + 1;
277 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
278 ct = g_strdup_printf("<i>%s</i>\n", buf);
283 lw = layout_find_by_image(imd);
288 n = g_list_length(lw->slideshow->list_done);
289 t = n + g_list_length(lw->slideshow->list);
294 t = layout_list_count(lw, NULL);
295 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
298 else if (view_window_find_image(imd, &n, &t))
314 size = text_from_size_abrev(imd->size);
317 if (imd->delay_flip &&
318 imd->il && imd->il->pixbuf &&
319 image_get_pixbuf(imd) != imd->il->pixbuf)
321 w = gdk_pixbuf_get_width(imd->il->pixbuf);
322 h = gdk_pixbuf_get_height(imd->il->pixbuf);
323 imgpixbuf = imd->il->pixbuf;
327 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
328 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
331 if (imgpixbuf && imd->histogram_enabled && imd->histogram
332 && (!imd->il || imd->il->done))
335 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
336 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
337 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
340 g_hash_table_insert(vars, "collection", g_strdup(ct));
341 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
342 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
343 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
344 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
345 g_hash_table_insert(vars, "size", g_strdup(size));
346 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
350 text = g_strdup_printf(_("Untitled"));
354 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
359 g_free(name_escaped);
360 g_hash_table_destroy(vars);
363 FileData *fd = image_get_fd(imd);
365 if (fd) /* fd may be null after file deletion */
367 gint active_marks = 0;
371 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
373 active_marks += fd->marks[mark];
377 if (active_marks > 0)
379 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
381 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
383 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
387 text2 = g_strdup_printf("%s\n%s", text, buf->str);
389 text2 = g_strdup(buf->str);
390 g_string_free(buf, TRUE);
398 text2 = g_strdup_printf("%s\n%s", text, histogram_label(imd->histogram));
400 text2 = g_strdup(histogram_label(imd->histogram));
407 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
408 pango_layout_set_markup(layout, text, -1);
411 pango_layout_get_pixel_size(layout, &width, &height);
412 /* with empty text width is set to 0, but not height) */
415 else if (height == 0)
417 if (width > 0) width += 10;
418 if (height > 0) height += 10;
422 histogram_read(imd->histogram, imgpixbuf);
423 if (width < 266) width = 266;
424 height += HISTOGRAM_HEIGHT + 5;
427 if (width > 0 && height > 0)
429 /* TODO: make osd color configurable --Zas */
430 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
431 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
432 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
433 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
434 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
435 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
436 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
437 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
438 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
441 histogram_draw(imd->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
443 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
446 g_object_unref(G_OBJECT(layout));
451 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
453 static GdkPixbuf **icons = NULL;
454 GdkPixbuf *icon = NULL;
456 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
457 if (icons[flag]) return icons[flag];
459 if (osd_icons[flag].key)
461 icon = pixbuf_inline(osd_icons[flag].key);
466 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
467 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
468 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
471 case IMAGE_OSD_ROTATE_AUTO:
472 pixbuf_set_rect(icon, 3, 8, 11, 12,
475 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
476 20, 9, 14, 15, 14, 3,
479 case IMAGE_OSD_ROTATE_USER:
481 case IMAGE_OSD_COLOR:
482 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
483 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
484 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
486 case IMAGE_OSD_FIRST:
487 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
488 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
489 12, 5, 18, 11, 6, 11,
493 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
494 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
495 12, 18, 6, 12, 18, 12,
499 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
500 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
512 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
516 if (osd->icon_id[flag]) return;
518 pixbuf = image_osd_icon_pixbuf(flag);
521 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
522 osd_icons[flag].x, osd_icons[flag].y,
526 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
528 if (osd->icon_id[flag])
530 image_overlay_remove(osd->imd, osd->icon_id[flag]);
531 osd->icon_id[flag] = 0;
535 static gint image_osd_update_cb(gpointer data)
537 OverlayStateData *osd = data;
539 osd->imd->overlay_show_zoom = FALSE;
543 if (osd->changed_states & IMAGE_STATE_IMAGE)
547 pixbuf = image_osd_info_render(osd->imd);
550 if (osd->ovl_info == 0)
552 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
553 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
557 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
559 g_object_unref(pixbuf);
561 else if (osd->ovl_info)
563 image_overlay_remove(osd->imd, osd->ovl_info);
572 image_overlay_remove(osd->imd, osd->ovl_info);
577 if (osd->show_status)
581 if (osd->changed_states & IMAGE_STATE_IMAGE)
583 for (i = 0; i < IMAGE_OSD_COUNT; i++)
585 if (osd_icons[i].reset) osd->icon_time[i] = 0;
589 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
591 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
592 image_osd_timer_schedule(osd);
595 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
599 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
602 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
605 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
606 image_osd_timer_schedule(osd);
609 for (i = 0; i < IMAGE_OSD_COUNT; i++)
611 if (osd->icon_time[i] > 0)
613 image_osd_icon_show(osd, i);
617 image_osd_icon_hide(osd, i);
625 for (i = 0; i < IMAGE_OSD_COUNT; i++)
627 image_osd_icon_hide(osd, i);
631 if (osd->imd->il && osd->imd->il->done)
632 osd->changed_states = IMAGE_STATE_NONE;
637 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
639 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
641 if (osd->idle_id == -1)
643 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
647 void image_osd_update(ImageWindow *imd)
649 OverlayStateData *osd;
653 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
656 image_osd_update_schedule(osd, TRUE);
659 static gint image_osd_timer_cb(gpointer data)
661 OverlayStateData *osd = data;
663 gint changed = FALSE;
666 for (i = 0; i < IMAGE_OSD_COUNT; i++)
668 if (osd->icon_time[i] > 1)
671 if (osd->icon_time[i] < 2)
673 osd->icon_time[i] = 0;
683 if (changed) image_osd_update_schedule(osd, FALSE);
694 static void image_osd_timer_schedule(OverlayStateData *osd)
696 if (osd->timer_id == -1)
698 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
702 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
704 OverlayStateData *osd = data;
706 osd->changed_states |= state;
707 image_osd_update_schedule(osd, FALSE);
710 static void image_osd_free(OverlayStateData *osd)
714 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
715 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
721 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
722 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
724 image_set_state_func(osd->imd, NULL, NULL);
725 image_overlay_remove(osd->imd, osd->ovl_info);
727 for (i = 0; i < IMAGE_OSD_COUNT; i++)
729 image_osd_icon_hide(osd, i);
736 static void image_osd_remove(ImageWindow *imd)
738 OverlayStateData *osd;
740 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
744 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
746 OverlayStateData *osd = data;
752 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
754 OverlayStateData *osd;
756 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
759 osd = g_new0(OverlayStateData, 1);
764 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
765 G_CALLBACK(image_osd_destroy_cb), osd);
766 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
768 image_set_state_func(osd->imd, image_osd_state_cb, osd);
771 if (osd->show_info != info ||
772 osd->show_status != status)
774 osd->show_info = info;
775 osd->show_status = status;
777 image_osd_update_schedule(osd, TRUE);
781 void image_osd_set(ImageWindow *imd, gint info, gint status)
785 if (!info && !status)
787 image_osd_remove(imd);
791 image_osd_enable(imd, info, status);
794 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
796 OverlayStateData *osd;
798 if (!imd) return FALSE;
800 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
801 if (!osd) return FALSE;
803 if (info) *info = osd->show_info;
804 if (status) *status = osd->show_status;
812 2+ = show for duration tenths of a second
813 -1 = use default duration
815 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
817 OverlayStateData *osd;
821 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
824 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
825 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
826 if (duration > 1) duration += 1;
828 osd->icon_time[flag] = duration;
830 image_osd_update_schedule(osd, FALSE);
831 image_osd_timer_schedule(osd);