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)
112 histogram_set_channel(imd->histogram, (histogram_get_channel(imd->histogram) +1)%HCHAN_COUNT);
113 image_osd_update(imd);
117 void image_osd_histogram_log_toggle(ImageWindow *imd)
121 histogram_set_mode(imd->histogram, !histogram_get_mode(imd->histogram));
122 image_osd_update(imd);
126 void image_osd_toggle(ImageWindow *imd)
128 if (image_osd_get(imd, NULL, NULL))
130 if (image_osd_histogram_onoff_status(imd))
132 image_osd_histogram_onoff_toggle(imd, 0);
133 image_osd_update(imd);
137 image_osd_set(imd, FALSE, FALSE);
142 image_osd_set(imd, TRUE, TRUE);
143 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
144 image_osd_histogram_onoff_toggle(imd, 1);
145 image_osd_update(imd);
149 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
151 gchar delim = '%', imp = '|', sep[] = " - ";
160 if (!str || !*str) return g_strdup("");
162 new = g_string_new(str);
164 exif = exif_read_fd(imd->image_fd);
175 start = strchr(new->str, delim);
178 end = strchr(start+1, delim);
182 for (p = end; p > start; p--)
184 if (*p == ':' && was_digit)
189 was_digit = g_ascii_isdigit(*p);
192 if (trunc) limit = atoi(trunc+1);
194 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
196 pos = start-new->str;
197 data = g_strdup(g_hash_table_lookup(vars, name));
198 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
200 data = exif_get_data_as_text(exif, name);
201 if (data && *data && limit > 0 && strlen(data) > limit + 3)
203 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
209 /* Since we use pango markup to display, we need to escape here */
210 gchar *escaped = g_markup_escape_text(data, -1);
214 g_string_erase(new, pos, end-start+1);
216 g_string_insert(new, pos, data);
217 if (pos-prev == 2 && new->str[pos-1] == imp)
219 g_string_erase(new, --pos, 1);
222 g_string_insert(new, pos, sep);
227 prev = data ? pos+strlen(data)-1 : pos-1;
228 last = data ? TRUE : last;
233 /* search and destroy empty lines */
235 while ((start = strchr(end, '\n')))
238 while (*++(end) == '\n')
240 g_string_erase(new, start-new->str, end-start-1);
243 g_strchomp(new->str);
246 g_string_free(new, FALSE);
251 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
253 GdkPixbuf *pixbuf = NULL;
263 GdkPixbuf *imgpixbuf = NULL;
264 LayoutWindow *lw = NULL;
270 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
272 name = image_get_name(imd);
275 name_escaped = g_markup_escape_text(name, -1);
282 cd = image_get_collection(imd, &info);
287 t = g_list_length(cd->list);
288 n = g_list_index(cd->list, info) + 1;
289 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
290 ct = g_strdup_printf("<i>%s</i>\n", buf);
295 lw = layout_find_by_image(imd);
300 n = g_list_length(lw->slideshow->list_done);
301 t = n + g_list_length(lw->slideshow->list);
306 t = layout_list_count(lw, NULL);
307 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
310 else if (view_window_find_image(imd, &n, &t))
326 size = text_from_size_abrev(imd->size);
329 if (imd->delay_flip &&
330 imd->il && imd->il->pixbuf &&
331 image_get_pixbuf(imd) != imd->il->pixbuf)
333 w = gdk_pixbuf_get_width(imd->il->pixbuf);
334 h = gdk_pixbuf_get_height(imd->il->pixbuf);
335 imgpixbuf = imd->il->pixbuf;
339 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
340 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
343 if (imgpixbuf && imd->histogram_enabled && imd->histogram
344 && (!imd->il || imd->il->done))
347 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
348 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
349 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
352 g_hash_table_insert(vars, "collection", g_strdup(ct));
353 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
354 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
355 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
356 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
357 g_hash_table_insert(vars, "size", g_strdup(size));
358 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
362 text = g_strdup_printf(_("Untitled"));
366 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
371 g_free(name_escaped);
372 g_hash_table_destroy(vars);
375 FileData *fd = image_get_fd(imd);
377 if (fd) /* fd may be null after file deletion */
379 gint active_marks = 0;
383 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
385 active_marks += fd->marks[mark];
388 if (active_marks > 0)
390 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
392 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
394 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
398 text2 = g_strdup_printf("%s\n%s", text, buf->str);
400 text2 = g_strdup(buf->str);
401 g_string_free(buf, TRUE);
408 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(imd->histogram), -1);
410 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
412 text2 = g_strdup(escaped_histogram_label);
413 g_free(escaped_histogram_label);
420 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
421 pango_layout_set_markup(layout, text, -1);
424 pango_layout_get_pixel_size(layout, &width, &height);
425 /* with empty text width is set to 0, but not height) */
428 else if (height == 0)
430 if (width > 0) width += 10;
431 if (height > 0) height += 10;
435 histogram_read(imd->histogram, imgpixbuf);
436 if (width < 266) width = 266;
437 height += HISTOGRAM_HEIGHT + 5;
440 if (width > 0 && height > 0)
442 /* TODO: make osd color configurable --Zas */
443 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
444 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
445 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
446 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
447 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
448 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
449 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
450 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
451 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
454 histogram_draw(imd->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
456 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
459 g_object_unref(G_OBJECT(layout));
464 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
466 static GdkPixbuf **icons = NULL;
467 GdkPixbuf *icon = NULL;
469 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
470 if (icons[flag]) return icons[flag];
472 if (osd_icons[flag].key)
474 icon = pixbuf_inline(osd_icons[flag].key);
479 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
480 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
481 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
484 case IMAGE_OSD_ROTATE_AUTO:
485 pixbuf_set_rect(icon, 3, 8, 11, 12,
488 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
489 20, 9, 14, 15, 14, 3,
492 case IMAGE_OSD_ROTATE_USER:
494 case IMAGE_OSD_COLOR:
495 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
496 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
497 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
499 case IMAGE_OSD_FIRST:
500 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
501 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
502 12, 5, 18, 11, 6, 11,
506 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
507 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
508 12, 18, 6, 12, 18, 12,
512 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
513 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
525 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
529 if (osd->icon_id[flag]) return;
531 pixbuf = image_osd_icon_pixbuf(flag);
534 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
535 osd_icons[flag].x, osd_icons[flag].y,
539 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
541 if (osd->icon_id[flag])
543 image_overlay_remove(osd->imd, osd->icon_id[flag]);
544 osd->icon_id[flag] = 0;
548 static gint image_osd_update_cb(gpointer data)
550 OverlayStateData *osd = data;
552 osd->imd->overlay_show_zoom = FALSE;
556 if (osd->changed_states & IMAGE_STATE_IMAGE)
560 pixbuf = image_osd_info_render(osd->imd);
563 if (osd->ovl_info == 0)
565 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
566 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
570 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
572 g_object_unref(pixbuf);
574 else if (osd->ovl_info)
576 image_overlay_remove(osd->imd, osd->ovl_info);
585 image_overlay_remove(osd->imd, osd->ovl_info);
590 if (osd->show_status)
594 if (osd->changed_states & IMAGE_STATE_IMAGE)
596 for (i = 0; i < IMAGE_OSD_COUNT; i++)
598 if (osd_icons[i].reset) osd->icon_time[i] = 0;
602 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
604 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
605 image_osd_timer_schedule(osd);
608 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
612 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
615 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
618 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
619 image_osd_timer_schedule(osd);
622 for (i = 0; i < IMAGE_OSD_COUNT; i++)
624 if (osd->icon_time[i] > 0)
626 image_osd_icon_show(osd, i);
630 image_osd_icon_hide(osd, i);
638 for (i = 0; i < IMAGE_OSD_COUNT; i++)
640 image_osd_icon_hide(osd, i);
644 if (osd->imd->il && osd->imd->il->done)
645 osd->changed_states = IMAGE_STATE_NONE;
650 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
652 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
654 if (osd->idle_id == -1)
656 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
660 void image_osd_update(ImageWindow *imd)
662 OverlayStateData *osd;
666 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
669 image_osd_update_schedule(osd, TRUE);
672 static gint image_osd_timer_cb(gpointer data)
674 OverlayStateData *osd = data;
676 gint changed = FALSE;
679 for (i = 0; i < IMAGE_OSD_COUNT; i++)
681 if (osd->icon_time[i] > 1)
684 if (osd->icon_time[i] < 2)
686 osd->icon_time[i] = 0;
696 if (changed) image_osd_update_schedule(osd, FALSE);
707 static void image_osd_timer_schedule(OverlayStateData *osd)
709 if (osd->timer_id == -1)
711 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
715 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
717 OverlayStateData *osd = data;
719 osd->changed_states |= state;
720 image_osd_update_schedule(osd, FALSE);
723 static void image_osd_free(OverlayStateData *osd)
727 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
728 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
734 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
735 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
737 image_set_state_func(osd->imd, NULL, NULL);
738 image_overlay_remove(osd->imd, osd->ovl_info);
740 for (i = 0; i < IMAGE_OSD_COUNT; i++)
742 image_osd_icon_hide(osd, i);
749 static void image_osd_remove(ImageWindow *imd)
751 OverlayStateData *osd;
753 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
757 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
759 OverlayStateData *osd = data;
765 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
767 OverlayStateData *osd;
769 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
772 osd = g_new0(OverlayStateData, 1);
777 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
778 G_CALLBACK(image_osd_destroy_cb), osd);
779 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
781 image_set_state_func(osd->imd, image_osd_state_cb, osd);
784 if (osd->show_info != info ||
785 osd->show_status != status)
787 osd->show_info = info;
788 osd->show_status = status;
790 image_osd_update_schedule(osd, TRUE);
794 void image_osd_set(ImageWindow *imd, gint info, gint status)
798 if (!info && !status)
800 image_osd_remove(imd);
804 image_osd_enable(imd, info, status);
807 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
809 OverlayStateData *osd;
811 if (!imd) return FALSE;
813 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
814 if (!osd) return FALSE;
816 if (info) *info = osd->show_info;
817 if (status) *status = osd->show_status;
825 2+ = show for duration tenths of a second
826 -1 = use default duration
828 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
830 OverlayStateData *osd;
834 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
837 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
838 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
839 if (duration > 1) duration += 1;
841 osd->icon_time[flag] = duration;
843 image_osd_update_schedule(osd, FALSE);
844 image_osd_timer_schedule(osd);