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"
23 #include "histogram.h"
27 *----------------------------------------------------------------------------
29 *----------------------------------------------------------------------------
32 typedef struct _OverlayStateData OverlayStateData;
33 struct _OverlayStateData {
35 ImageState changed_states;
42 gint icon_time[IMAGE_OSD_COUNT];
43 gint icon_id[IMAGE_OSD_COUNT];
51 typedef struct _OSDIcon OSDIcon;
53 gint reset; /* reset on new image */
54 gint x; /* x, y offset */
56 gchar *key; /* inline pixbuf */
59 static OSDIcon osd_icons[] = {
60 { TRUE, 0, 0, NULL }, /* none */
61 { TRUE, -10, -10, NULL }, /* auto rotated */
62 { TRUE, -10, -10, NULL }, /* user rotated */
63 { TRUE, -40, -10, NULL }, /* color embedded */
64 { TRUE, -70, -10, NULL }, /* first image */
65 { TRUE, -70, -10, NULL }, /* last image */
66 { FALSE, -70, -10, NULL }, /* osd enabled */
70 #define OSD_DATA "overlay-data"
73 #define OSD_INFO_Y -10
75 #define IMAGE_OSD_DEFAULT_DURATION 30
77 #define HISTOGRAM_HEIGHT 140
79 *----------------------------------------------------------------------------
81 *----------------------------------------------------------------------------
85 #define HIST_PREPARE(imd, lw) \
86 LayoutWindow *lw = NULL; \
88 lw = layout_find_by_image(imd);
90 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
95 lw->histogram_enabled = !!(x);
96 if (lw->histogram_enabled && !lw->histogram)
97 lw->histogram = histogram_new();
101 gint image_osd_histogram_onoff_status(ImageWindow *imd)
103 HIST_PREPARE(imd, lw)
104 return lw ? lw->histogram_enabled : FALSE;
107 void image_osd_histogram_chan_toggle(ImageWindow *imd)
109 HIST_PREPARE(imd, lw)
110 if (lw && lw->histogram)
111 histogram_set_channel(lw->histogram, (histogram_get_channel(lw->histogram) +1)%HCHAN_COUNT);
114 void image_osd_histogram_log_toggle(ImageWindow *imd)
117 if (lw && lw->histogram)
118 histogram_set_mode(lw->histogram, !histogram_get_mode(lw->histogram));
123 static void image_osd_timer_schedule(OverlayStateData *osd);
125 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
127 gchar delim = '%', imp = '|', sep[] = " - ";
136 if (!str || !*str) return g_strdup("");
138 new = g_string_new(str);
140 exif = exif_read_fd(imd->image_fd);
151 start = strchr(new->str, delim);
154 end = strchr(start+1, delim);
158 for (p = end; p > start; p--)
160 if (*p == ':' && was_digit)
165 was_digit = g_ascii_isdigit(*p);
168 if (trunc) limit = atoi(trunc+1);
170 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
172 pos = start-new->str;
173 data = g_strdup(g_hash_table_lookup(vars, name));
174 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
176 data = exif_get_data_as_text(exif, name);
177 if (data && *data && limit > 0 && strlen(data) > limit + 3)
179 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
184 g_string_erase(new, pos, end-start+1);
186 g_string_insert(new, pos, data);
187 if (pos-prev == 2 && new->str[pos-1] == imp)
189 g_string_erase(new, --pos, 1);
192 g_string_insert(new, pos, sep);
197 prev = data ? pos+strlen(data)-1 : pos-1;
198 last = data ? TRUE : last;
203 /* search and destroy empty lines */
205 while ((start = strchr(end, '\n')))
208 while (*++(end) == '\n')
210 g_string_erase(new, start-new->str, end-start-1);
213 g_strchomp(new->str);
216 g_string_free(new, FALSE);
221 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
223 GdkPixbuf *pixbuf = NULL;
233 GdkPixbuf *imgpixbuf = NULL;
234 LayoutWindow *lw = NULL;
240 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
242 name = image_get_name(imd);
245 name_escaped = g_markup_escape_text(name, -1);
252 cd = image_get_collection(imd, &info);
257 t = g_list_length(cd->list);
258 n = g_list_index(cd->list, info) + 1;
259 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
260 ct = g_strdup_printf("<i>%s</i>\n", buf);
265 lw = layout_find_by_image(imd);
270 n = g_list_length(lw->slideshow->list_done);
271 t = n + g_list_length(lw->slideshow->list);
276 t = layout_list_count(lw, NULL);
277 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
280 else if (view_window_find_image(imd, &n, &t))
296 size = text_from_size_abrev(imd->size);
299 if (imd->delay_flip &&
300 imd->il && imd->il->pixbuf &&
301 image_get_pixbuf(imd) != imd->il->pixbuf)
303 w = gdk_pixbuf_get_width(imd->il->pixbuf);
304 h = gdk_pixbuf_get_height(imd->il->pixbuf);
305 imgpixbuf = imd->il->pixbuf;
309 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
310 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
313 lw = layout_find_by_image(imd);
315 if (imgpixbuf && lw && lw->histogram && lw->histogram_enabled
316 && (!imd->il || imd->il->done))
319 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
320 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
321 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
324 g_hash_table_insert(vars, "collection", g_strdup(ct));
325 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
326 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
327 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
328 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
329 g_hash_table_insert(vars, "size", g_strdup(size));
330 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
334 text = g_strdup_printf(_("Untitled"));
338 text = image_osd_mkinfo(options->fullscreen.info, imd, vars);
343 g_free(name_escaped);
344 g_hash_table_destroy(vars);
347 FileData *fd = image_get_fd(imd);
349 if (fd) /* fd may be null after file deletion */
351 gint active_marks = 0;
355 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
357 active_marks += fd->marks[mark];
361 if (active_marks > 0)
363 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
365 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
367 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
371 text2 = g_strdup_printf("%s\n%s", text, buf->str);
373 text2 = g_strdup(buf->str);
374 g_string_free(buf, TRUE);
382 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
384 text2 = g_strdup(histogram_label(lw->histogram));
391 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
392 pango_layout_set_markup(layout, text, -1);
395 pango_layout_get_pixel_size(layout, &width, &height);
396 /* with empty text width is set to 0, but not height) */
399 else if (height == 0)
401 if (width > 0) width += 10;
402 if (height > 0) height += 10;
406 histogram_read(lw->histogram, imgpixbuf);
407 if (width < 266) width = 266;
408 height += HISTOGRAM_HEIGHT + 5;
411 if (width > 0 && height > 0)
413 /* TODO: make osd color configurable --Zas */
414 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
415 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
416 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
417 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
418 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
419 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
420 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
421 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
422 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
425 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
427 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
430 g_object_unref(G_OBJECT(layout));
435 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
437 static GdkPixbuf **icons = NULL;
438 GdkPixbuf *icon = NULL;
440 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
441 if (icons[flag]) return icons[flag];
443 if (osd_icons[flag].key)
445 icon = pixbuf_inline(osd_icons[flag].key);
450 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
451 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
452 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
455 case IMAGE_OSD_ROTATE_AUTO:
456 pixbuf_set_rect(icon, 3, 8, 11, 12,
459 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
460 20, 9, 14, 15, 14, 3,
463 case IMAGE_OSD_ROTATE_USER:
465 case IMAGE_OSD_COLOR:
466 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
467 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
468 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
470 case IMAGE_OSD_FIRST:
471 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
472 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
473 12, 5, 18, 11, 6, 11,
477 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
478 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
479 12, 18, 6, 12, 18, 12,
483 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
484 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
496 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
500 if (osd->icon_id[flag]) return;
502 pixbuf = image_osd_icon_pixbuf(flag);
505 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
506 osd_icons[flag].x, osd_icons[flag].y,
510 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
512 if (osd->icon_id[flag])
514 image_overlay_remove(osd->imd, osd->icon_id[flag]);
515 osd->icon_id[flag] = 0;
519 static gint image_osd_update_cb(gpointer data)
521 OverlayStateData *osd = data;
523 osd->imd->overlay_show_zoom = FALSE;
527 if (osd->changed_states & IMAGE_STATE_IMAGE)
531 pixbuf = image_osd_info_render(osd->imd);
534 if (osd->ovl_info == 0)
536 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
537 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
541 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
543 g_object_unref(pixbuf);
545 else if (osd->ovl_info)
547 image_overlay_remove(osd->imd, osd->ovl_info);
556 image_overlay_remove(osd->imd, osd->ovl_info);
561 if (osd->show_status)
565 if (osd->changed_states & IMAGE_STATE_IMAGE)
567 for (i = 0; i < IMAGE_OSD_COUNT; i++)
569 if (osd_icons[i].reset) osd->icon_time[i] = 0;
573 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
575 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
576 image_osd_timer_schedule(osd);
579 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
583 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
586 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
589 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
590 image_osd_timer_schedule(osd);
593 for (i = 0; i < IMAGE_OSD_COUNT; i++)
595 if (osd->icon_time[i] > 0)
597 image_osd_icon_show(osd, i);
601 image_osd_icon_hide(osd, i);
609 for (i = 0; i < IMAGE_OSD_COUNT; i++)
611 image_osd_icon_hide(osd, i);
615 if (osd->imd->il && osd->imd->il->done)
616 osd->changed_states = IMAGE_STATE_NONE;
621 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
623 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
625 if (osd->idle_id == -1)
627 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
631 void image_osd_update(ImageWindow *imd)
633 OverlayStateData *osd;
637 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
640 image_osd_update_schedule(osd, TRUE);
643 static gint image_osd_timer_cb(gpointer data)
645 OverlayStateData *osd = data;
647 gint changed = FALSE;
650 for (i = 0; i < IMAGE_OSD_COUNT; i++)
652 if (osd->icon_time[i] > 1)
655 if (osd->icon_time[i] < 2)
657 osd->icon_time[i] = 0;
667 if (changed) image_osd_update_schedule(osd, FALSE);
678 static void image_osd_timer_schedule(OverlayStateData *osd)
680 if (osd->timer_id == -1)
682 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
686 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
688 OverlayStateData *osd = data;
690 osd->changed_states |= state;
691 image_osd_update_schedule(osd, FALSE);
694 static void image_osd_free(OverlayStateData *osd)
698 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
699 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
705 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
706 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
708 image_set_state_func(osd->imd, NULL, NULL);
709 image_overlay_remove(osd->imd, osd->ovl_info);
711 for (i = 0; i < IMAGE_OSD_COUNT; i++)
713 image_osd_icon_hide(osd, i);
720 static void image_osd_remove(ImageWindow *imd)
722 OverlayStateData *osd;
724 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
728 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
730 OverlayStateData *osd = data;
736 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
738 OverlayStateData *osd;
740 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
743 osd = g_new0(OverlayStateData, 1);
748 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
749 G_CALLBACK(image_osd_destroy_cb), osd);
750 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
752 image_set_state_func(osd->imd, image_osd_state_cb, osd);
755 if (osd->show_info != info ||
756 osd->show_status != status)
758 osd->show_info = info;
759 osd->show_status = status;
761 image_osd_update_schedule(osd, TRUE);
765 void image_osd_set(ImageWindow *imd, gint info, gint status)
769 if (!info && !status)
771 image_osd_remove(imd);
775 image_osd_enable(imd, info, status);
778 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
780 OverlayStateData *osd;
782 if (!imd) return FALSE;
784 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
785 if (!osd) return FALSE;
787 if (info) *info = osd->show_info;
788 if (status) *status = osd->show_status;
796 2+ = show for duration tenths of a second
797 -1 = use default duration
799 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
801 OverlayStateData *osd;
805 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
808 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
809 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
810 if (duration > 1) duration += 1;
812 osd->icon_time[flag] = duration;
814 image_osd_update_schedule(osd, FALSE);
815 image_osd_timer_schedule(osd);