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 #define HIST_PREPARE(imd, lw) \
94 LayoutWindow *lw = NULL; \
96 lw = layout_find_by_image(imd);
98 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
100 HIST_PREPARE(imd, lw)
103 lw->histogram_enabled = !!(x);
104 if (lw->histogram_enabled && !lw->histogram)
105 lw->histogram = histogram_new();
109 gint image_osd_histogram_onoff_status(ImageWindow *imd)
111 HIST_PREPARE(imd, lw)
112 return lw ? lw->histogram_enabled : FALSE;
115 void image_osd_histogram_chan_toggle(ImageWindow *imd)
117 HIST_PREPARE(imd, lw)
118 if (lw && lw->histogram)
119 histogram_set_channel(lw->histogram, (histogram_get_channel(lw->histogram) +1)%HCHAN_COUNT);
122 void image_osd_histogram_log_toggle(ImageWindow *imd)
125 if (lw && lw->histogram)
126 histogram_set_mode(lw->histogram, !histogram_get_mode(lw->histogram));
131 static void image_osd_timer_schedule(OverlayStateData *osd);
133 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
135 gchar delim = '%', imp = '|', sep[] = " - ";
144 if (!str || !*str) return g_strdup("");
146 new = g_string_new(str);
148 exif = exif_read_fd(imd->image_fd);
159 start = strchr(new->str, delim);
162 end = strchr(start+1, delim);
166 for (p = end; p > start; p--)
168 if (*p == ':' && was_digit)
173 was_digit = g_ascii_isdigit(*p);
176 if (trunc) limit = atoi(trunc+1);
178 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
180 pos = start-new->str;
181 data = g_strdup(g_hash_table_lookup(vars, name));
182 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
184 data = exif_get_data_as_text(exif, name);
185 if (data && *data && limit > 0 && strlen(data) > limit + 3)
187 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
192 g_string_erase(new, pos, end-start+1);
194 g_string_insert(new, pos, data);
195 if (pos-prev == 2 && new->str[pos-1] == imp)
197 g_string_erase(new, --pos, 1);
200 g_string_insert(new, pos, sep);
205 prev = data ? pos+strlen(data)-1 : pos-1;
206 last = data ? TRUE : last;
211 /* search and destroy empty lines */
213 while ((start = strchr(end, '\n')))
216 while (*++(end) == '\n')
218 g_string_erase(new, start-new->str, end-start-1);
221 g_strchomp(new->str);
224 g_string_free(new, FALSE);
229 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
231 GdkPixbuf *pixbuf = NULL;
241 GdkPixbuf *imgpixbuf = NULL;
242 LayoutWindow *lw = NULL;
248 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
250 name = image_get_name(imd);
253 name_escaped = g_markup_escape_text(name, -1);
260 cd = image_get_collection(imd, &info);
265 t = g_list_length(cd->list);
266 n = g_list_index(cd->list, info) + 1;
267 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
268 ct = g_strdup_printf("<i>%s</i>\n", buf);
273 lw = layout_find_by_image(imd);
278 n = g_list_length(lw->slideshow->list_done);
279 t = n + g_list_length(lw->slideshow->list);
284 t = layout_list_count(lw, NULL);
285 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
288 else if (view_window_find_image(imd, &n, &t))
304 size = text_from_size_abrev(imd->size);
307 if (imd->delay_flip &&
308 imd->il && imd->il->pixbuf &&
309 image_get_pixbuf(imd) != imd->il->pixbuf)
311 w = gdk_pixbuf_get_width(imd->il->pixbuf);
312 h = gdk_pixbuf_get_height(imd->il->pixbuf);
313 imgpixbuf = imd->il->pixbuf;
317 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
318 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
321 lw = layout_find_by_image(imd);
323 if (imgpixbuf && lw && lw->histogram && lw->histogram_enabled
324 && (!imd->il || imd->il->done))
327 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
328 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
329 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
332 g_hash_table_insert(vars, "collection", g_strdup(ct));
333 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
334 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
335 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
336 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
337 g_hash_table_insert(vars, "size", g_strdup(size));
338 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
342 text = g_strdup_printf(_("Untitled"));
346 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
351 g_free(name_escaped);
352 g_hash_table_destroy(vars);
355 FileData *fd = image_get_fd(imd);
357 if (fd) /* fd may be null after file deletion */
359 gint active_marks = 0;
363 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
365 active_marks += fd->marks[mark];
369 if (active_marks > 0)
371 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
373 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
375 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
379 text2 = g_strdup_printf("%s\n%s", text, buf->str);
381 text2 = g_strdup(buf->str);
382 g_string_free(buf, TRUE);
390 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
392 text2 = g_strdup(histogram_label(lw->histogram));
399 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
400 pango_layout_set_markup(layout, text, -1);
403 pango_layout_get_pixel_size(layout, &width, &height);
404 /* with empty text width is set to 0, but not height) */
407 else if (height == 0)
409 if (width > 0) width += 10;
410 if (height > 0) height += 10;
414 histogram_read(lw->histogram, imgpixbuf);
415 if (width < 266) width = 266;
416 height += HISTOGRAM_HEIGHT + 5;
419 if (width > 0 && height > 0)
421 /* TODO: make osd color configurable --Zas */
422 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
423 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
424 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
425 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
426 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
427 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
428 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
429 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
430 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
433 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
435 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
438 g_object_unref(G_OBJECT(layout));
443 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
445 static GdkPixbuf **icons = NULL;
446 GdkPixbuf *icon = NULL;
448 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
449 if (icons[flag]) return icons[flag];
451 if (osd_icons[flag].key)
453 icon = pixbuf_inline(osd_icons[flag].key);
458 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
459 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
460 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
463 case IMAGE_OSD_ROTATE_AUTO:
464 pixbuf_set_rect(icon, 3, 8, 11, 12,
467 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
468 20, 9, 14, 15, 14, 3,
471 case IMAGE_OSD_ROTATE_USER:
473 case IMAGE_OSD_COLOR:
474 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
475 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
476 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
478 case IMAGE_OSD_FIRST:
479 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
480 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
481 12, 5, 18, 11, 6, 11,
485 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
486 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
487 12, 18, 6, 12, 18, 12,
491 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
492 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
504 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
508 if (osd->icon_id[flag]) return;
510 pixbuf = image_osd_icon_pixbuf(flag);
513 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
514 osd_icons[flag].x, osd_icons[flag].y,
518 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
520 if (osd->icon_id[flag])
522 image_overlay_remove(osd->imd, osd->icon_id[flag]);
523 osd->icon_id[flag] = 0;
527 static gint image_osd_update_cb(gpointer data)
529 OverlayStateData *osd = data;
531 osd->imd->overlay_show_zoom = FALSE;
535 if (osd->changed_states & IMAGE_STATE_IMAGE)
539 pixbuf = image_osd_info_render(osd->imd);
542 if (osd->ovl_info == 0)
544 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
545 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
549 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
551 g_object_unref(pixbuf);
553 else if (osd->ovl_info)
555 image_overlay_remove(osd->imd, osd->ovl_info);
564 image_overlay_remove(osd->imd, osd->ovl_info);
569 if (osd->show_status)
573 if (osd->changed_states & IMAGE_STATE_IMAGE)
575 for (i = 0; i < IMAGE_OSD_COUNT; i++)
577 if (osd_icons[i].reset) osd->icon_time[i] = 0;
581 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
583 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
584 image_osd_timer_schedule(osd);
587 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
591 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
594 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
597 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
598 image_osd_timer_schedule(osd);
601 for (i = 0; i < IMAGE_OSD_COUNT; i++)
603 if (osd->icon_time[i] > 0)
605 image_osd_icon_show(osd, i);
609 image_osd_icon_hide(osd, i);
617 for (i = 0; i < IMAGE_OSD_COUNT; i++)
619 image_osd_icon_hide(osd, i);
623 if (osd->imd->il && osd->imd->il->done)
624 osd->changed_states = IMAGE_STATE_NONE;
629 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
631 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
633 if (osd->idle_id == -1)
635 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
639 void image_osd_update(ImageWindow *imd)
641 OverlayStateData *osd;
645 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
648 image_osd_update_schedule(osd, TRUE);
651 static gint image_osd_timer_cb(gpointer data)
653 OverlayStateData *osd = data;
655 gint changed = FALSE;
658 for (i = 0; i < IMAGE_OSD_COUNT; i++)
660 if (osd->icon_time[i] > 1)
663 if (osd->icon_time[i] < 2)
665 osd->icon_time[i] = 0;
675 if (changed) image_osd_update_schedule(osd, FALSE);
686 static void image_osd_timer_schedule(OverlayStateData *osd)
688 if (osd->timer_id == -1)
690 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
694 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
696 OverlayStateData *osd = data;
698 osd->changed_states |= state;
699 image_osd_update_schedule(osd, FALSE);
702 static void image_osd_free(OverlayStateData *osd)
706 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
707 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
713 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
714 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
716 image_set_state_func(osd->imd, NULL, NULL);
717 image_overlay_remove(osd->imd, osd->ovl_info);
719 for (i = 0; i < IMAGE_OSD_COUNT; i++)
721 image_osd_icon_hide(osd, i);
728 static void image_osd_remove(ImageWindow *imd)
730 OverlayStateData *osd;
732 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
736 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
738 OverlayStateData *osd = data;
744 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
746 OverlayStateData *osd;
748 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
751 osd = g_new0(OverlayStateData, 1);
756 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
757 G_CALLBACK(image_osd_destroy_cb), osd);
758 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
760 image_set_state_func(osd->imd, image_osd_state_cb, osd);
763 if (osd->show_info != info ||
764 osd->show_status != status)
766 osd->show_info = info;
767 osd->show_status = status;
769 image_osd_update_schedule(osd, TRUE);
773 void image_osd_set(ImageWindow *imd, gint info, gint status)
777 if (!info && !status)
779 image_osd_remove(imd);
783 image_osd_enable(imd, info, status);
786 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
788 OverlayStateData *osd;
790 if (!imd) return FALSE;
792 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
793 if (!osd) return FALSE;
795 if (info) *info = osd->show_info;
796 if (status) *status = osd->show_status;
804 2+ = show for duration tenths of a second
805 -1 = use default duration
807 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
809 OverlayStateData *osd;
813 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
816 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
817 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
818 if (duration > 1) duration += 1;
820 osd->icon_time[flag] = duration;
822 image_osd_update_schedule(osd, FALSE);
823 image_osd_timer_schedule(osd);