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[] = " - ";
132 GString *new = g_string_new(str);
136 exif = exif_read_fd(imd->image_fd, FALSE);
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));
171 data = exif_get_data_as_text(exif, name);
172 if (data && *data && limit > 0 && strlen(data) > limit + 3)
174 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
179 g_string_erase(new, pos, end-start+1);
181 g_string_insert(new, pos, data);
182 if (pos-prev == 2 && new->str[pos-1] == imp)
184 g_string_erase(new, --pos, 1);
187 g_string_insert(new, pos, sep);
192 prev = data ? pos+strlen(data)-1 : pos-1;
193 last = data ? TRUE : last;
198 /* search and destroy empty lines */
200 while ((start = strchr(end, '\n')))
203 while (*++(end) == '\n')
205 g_string_erase(new, start-new->str, end-start-1);
208 g_strchomp(new->str);
211 g_string_free(new, FALSE);
216 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
228 GdkPixbuf *imgpixbuf = NULL;
229 LayoutWindow *lw = NULL;
235 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
237 name = image_get_name(imd);
240 name_escaped = g_markup_escape_text(name, -1);
247 cd = image_get_collection(imd, &info);
252 t = g_list_length(cd->list);
253 n = g_list_index(cd->list, info) + 1;
254 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
255 ct = g_strdup_printf("<i>%s</i>\n", buf);
260 lw = layout_find_by_image(imd);
265 n = g_list_length(lw->slideshow->list_done);
266 t = n + g_list_length(lw->slideshow->list);
271 t = layout_list_count(lw, NULL);
272 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
275 else if (view_window_find_image(imd, &n, &t))
291 size = text_from_size_abrev(imd->size);
294 if (imd->delay_flip &&
295 imd->il && imd->il->pixbuf &&
296 image_get_pixbuf(imd) != imd->il->pixbuf)
298 w = gdk_pixbuf_get_width(imd->il->pixbuf);
299 h = gdk_pixbuf_get_height(imd->il->pixbuf);
300 imgpixbuf = imd->il->pixbuf;
304 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
305 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
308 lw = layout_find_by_image(imd);
310 if (imgpixbuf && lw->histogram && lw->histogram_enabled
311 && (!imd->il || imd->il->done))
314 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
315 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
316 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
319 g_hash_table_insert(vars, "collection", g_strdup(ct));
320 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
321 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
322 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
323 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
324 g_hash_table_insert(vars, "size", g_strdup(size));
328 text = g_strdup_printf(_("Untitled"));
332 text = image_osd_mkinfo(fullscreen_info, 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);
364 text2 = g_strdup_printf("%s\n%s", text, buf->str);
365 g_string_free(buf, TRUE);
372 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
379 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
380 pango_layout_set_markup(layout, text, -1);
383 pango_layout_get_pixel_size(layout, &width, &height);
390 histogram_read(lw->histogram, imgpixbuf);
391 if (width < 266) width = 266;
392 height += HISTOGRAM_HEIGHT + 5;
396 /* TODO: make osd color configurable --Zas */
397 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
398 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
399 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
400 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
401 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
402 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
403 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
404 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
405 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
408 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
410 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
412 g_object_unref(G_OBJECT(layout));
417 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
419 static GdkPixbuf **icons = NULL;
420 GdkPixbuf *icon = NULL;
422 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
423 if (icons[flag]) return icons[flag];
425 if (osd_icons[flag].key)
427 icon = pixbuf_inline(osd_icons[flag].key);
432 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
433 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
434 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
437 case IMAGE_OSD_ROTATE_AUTO:
438 pixbuf_set_rect(icon, 3, 8, 11, 12,
441 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
442 20, 9, 14, 15, 14, 3,
445 case IMAGE_OSD_ROTATE_USER:
447 case IMAGE_OSD_COLOR:
448 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
449 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
450 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
452 case IMAGE_OSD_FIRST:
453 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
454 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
455 12, 5, 18, 11, 6, 11,
459 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
460 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
461 12, 18, 6, 12, 18, 12,
465 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
466 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
478 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
482 if (osd->icon_id[flag]) return;
484 pixbuf = image_osd_icon_pixbuf(flag);
487 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
488 osd_icons[flag].x, osd_icons[flag].y,
492 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
494 if (osd->icon_id[flag])
496 image_overlay_remove(osd->imd, osd->icon_id[flag]);
497 osd->icon_id[flag] = 0;
501 static gint image_osd_update_cb(gpointer data)
503 OverlayStateData *osd = data;
507 if (osd->changed_states & IMAGE_STATE_IMAGE)
511 pixbuf = image_osd_info_render(osd->imd);
512 if (osd->ovl_info == 0)
514 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
515 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
519 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
521 g_object_unref(pixbuf);
528 image_overlay_remove(osd->imd, osd->ovl_info);
533 if (osd->show_status)
537 if (osd->changed_states & IMAGE_STATE_IMAGE)
539 for (i = 0; i < IMAGE_OSD_COUNT; i++)
541 if (osd_icons[i].reset) osd->icon_time[i] = 0;
545 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
547 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
548 image_osd_timer_schedule(osd);
551 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
555 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
558 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
561 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
562 image_osd_timer_schedule(osd);
565 for (i = 0; i < IMAGE_OSD_COUNT; i++)
567 if (osd->icon_time[i] > 0)
569 image_osd_icon_show(osd, i);
573 image_osd_icon_hide(osd, i);
581 for (i = 0; i < IMAGE_OSD_COUNT; i++)
583 image_osd_icon_hide(osd, i);
587 if (osd->imd->il && osd->imd->il->done)
588 osd->changed_states = IMAGE_STATE_NONE;
593 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
595 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
597 if (osd->idle_id == -1)
599 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
603 void image_osd_update(ImageWindow *imd)
605 OverlayStateData *osd;
609 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
612 image_osd_update_schedule(osd, TRUE);
615 static gint image_osd_timer_cb(gpointer data)
617 OverlayStateData *osd = data;
619 gint changed = FALSE;
622 for (i = 0; i < IMAGE_OSD_COUNT; i++)
624 if (osd->icon_time[i] > 1)
627 if (osd->icon_time[i] < 2)
629 osd->icon_time[i] = 0;
639 if (changed) image_osd_update_schedule(osd, FALSE);
650 static void image_osd_timer_schedule(OverlayStateData *osd)
652 if (osd->timer_id == -1)
654 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
658 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
660 OverlayStateData *osd = data;
662 osd->changed_states |= state;
663 image_osd_update_schedule(osd, FALSE);
666 static void image_osd_free(OverlayStateData *osd)
670 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
671 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
677 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
678 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
680 image_set_state_func(osd->imd, NULL, NULL);
681 image_overlay_remove(osd->imd, osd->ovl_info);
683 for (i = 0; i < IMAGE_OSD_COUNT; i++)
685 image_osd_icon_hide(osd, i);
692 static void image_osd_remove(ImageWindow *imd)
694 OverlayStateData *osd;
696 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
700 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
702 OverlayStateData *osd = data;
708 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
710 OverlayStateData *osd;
712 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
715 osd = g_new0(OverlayStateData, 1);
720 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
721 G_CALLBACK(image_osd_destroy_cb), osd);
722 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
724 image_set_state_func(osd->imd, image_osd_state_cb, osd);
727 if (osd->show_info != info ||
728 osd->show_status != status)
730 osd->show_info = info;
731 osd->show_status = status;
733 image_osd_update_schedule(osd, TRUE);
737 void image_osd_set(ImageWindow *imd, gint info, gint status)
741 if (!info && !status)
743 image_osd_remove(imd);
747 image_osd_enable(imd, info, status);
750 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
752 OverlayStateData *osd;
754 if (!imd) return FALSE;
756 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
757 if (!osd) return FALSE;
759 if (info) *info = osd->show_info;
760 if (status) *status = osd->show_status;
768 2+ = show for duration tenths of a second
769 -1 = use default duration
771 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
773 OverlayStateData *osd;
777 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
780 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
781 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
782 if (duration > 1) duration += 1;
784 osd->icon_time[flag] = duration;
786 image_osd_update_schedule(osd, FALSE);
787 image_osd_timer_schedule(osd);