Include a Other Software section in Help file
[geeqie.git] / src / image-overlay.c
index cb272f0..90d4c78 100644 (file)
 #include "main.h"
 #include "image-overlay.h"
 
-#include "collect.h"
-#include "exif.h"
 #include "filedata.h"
 #include "histogram.h"
 #include "image.h"
 #include "img-view.h"
 #include "layout.h"
-#include "metadata.h"
+#include "osd.h"
 #include "pixbuf-renderer.h"
 #include "pixbuf_util.h"
 #include "ui_fileops.h"
 #include "image-load.h"
-#include "glua.h"
 
 /*
  *----------------------------------------------------------------------------
@@ -52,6 +49,7 @@ struct _OverlayStateData {
        Histogram *histogram;
 
        OsdShowFlags show;
+       OverlayRendererFlags origin;
 
        gint ovl_info;
 
@@ -204,7 +202,6 @@ gint image_osd_histogram_get_mode(ImageWindow *imd)
 void image_osd_toggle(ImageWindow *imd)
 {
        OsdShowFlags show;
-
        if (!imd) return;
 
        show = image_osd_get(imd);
@@ -215,296 +212,20 @@ void image_osd_toggle(ImageWindow *imd)
                }
        else
                {
-               if (show & OSD_SHOW_HISTOGRAM)
+               if (show & OSD_SHOW_GUIDELINES)
                        {
                        image_osd_set(imd, OSD_SHOW_NOTHING);
                        }
-               else
-                       {
-                       image_osd_set(imd, show | OSD_SHOW_HISTOGRAM);
-                       }
-               }
-}
-
-static gchar *keywords_to_string(FileData *fd)
-{
-       GList *keywords;
-       GString *kwstr = NULL;
-       gchar *ret = NULL;
-
-       g_assert(fd);
-
-       keywords = metadata_read_list(fd, KEYWORD_KEY, METADATA_PLAIN);
-
-       if (keywords)
-               {
-               GList *work = keywords;
-
-               while (work)
-                       {
-                       gchar *kw = work->data;
-                       work = work->next;
-
-                       if (!kw) continue;
-                       if (!kwstr)
-                               kwstr = g_string_new("");
-                       else
-                               g_string_append(kwstr, ", ");
-
-                       g_string_append(kwstr, kw);
-                       }
-               string_list_free(keywords);
-               }
-
-       if (kwstr)
-               {
-               ret = kwstr->str;
-               g_string_free(kwstr, FALSE);
-               }
-
-       return ret;
-}
-
-static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
-{
-       gchar delim = '%', imp = '|', sep[] = " - ";
-       gchar *start, *end;
-       guint pos, prev;
-       gboolean want_separator = FALSE;
-       gchar *name, *data;
-       GString *new;
-       gchar *ret;
-
-       if (!str || !*str) return g_strdup("");
-
-       new = g_string_new(str);
-
-       prev = -1;
-
-       while (TRUE)
-               {
-               guint limit = 0;
-               gchar *trunc = NULL;
-               gchar *limpos = NULL;
-               gchar *extra = NULL;
-               gchar *extrapos = NULL;
-               gchar *p;
-
-               start = strchr(new->str + (prev + 1), delim);
-               if (!start)
-                       break;
-               end = strchr(start+1, delim);
-               if (!end)
-                       break;
-
-               /* Search for optionnal modifiers
-                * %name:99:extra% -> name = "name", limit=99, extra = "extra"
-                */
-               for (p = start + 1; p < end; p++)
-                       {
-                       if (p[0] == ':')
-                               {
-                               if (g_ascii_isdigit(p[1]) && !limpos)
-                                       {
-                                       limpos = p + 1;
-                                       if (!trunc) trunc = p;
-                                       }
-                               else
-                                       {
-                                       extrapos = p + 1;
-                                       if (!trunc) trunc = p;
-                                       break;
-                                       }
-                               }
-                       }
-
-               if (limpos)
-                       limit = (guint) atoi(limpos);
-
-               if (extrapos)
-                       extra = g_strndup(extrapos, end - extrapos);
-
-               name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
-               pos = start - new->str;
-               data = NULL;
-
-               if (strcmp(name, "keywords") == 0)
-                       {
-                       data = keywords_to_string(imd->image_fd);
-                       }
-               else if (strcmp(name, "comment") == 0)
-                       {
-                       data = metadata_read_string(imd->image_fd, COMMENT_KEY, METADATA_PLAIN);
-                       }
-               else if (strcmp(name, "imagecomment") == 0)
-                       {
-                       data = exif_get_image_comment(imd->image_fd);
-                       }
-               else if (strcmp(name, "rating") == 0)
+               else if (show & OSD_SHOW_HISTOGRAM)
                        {
-                       data = metadata_read_string(imd->image_fd, RATING_KEY, METADATA_PLAIN);
+                       image_osd_set(imd, OSD_SHOW_GUIDELINES);
+                       image_osd_set(imd, show | ~OSD_SHOW_HISTOGRAM);
                        }
-#ifdef HAVE_LUA
-               else if (strncmp(name, "lua/", 4) == 0)
-                       {
-                       gchar *tmp;
-                       tmp = strchr(name+4, '/');
-                       if (!tmp)
-                               break;
-                       *tmp = '\0';
-                       data = lua_callvalue(imd->image_fd, name+4, tmp+1);
-                       }
-#endif
                else
                        {
-                       data = g_strdup(g_hash_table_lookup(vars, name));
-                       if (!data)
-                               data = metadata_read_string(imd->image_fd, name, METADATA_FORMATTED);
-                       }
-
-               if (data && *data && limit > 0 && strlen(data) > limit + 3)
-                       {
-                       gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
-                       g_free(data);
-                       data = new_data;
-                       }
-
-               if (data)
-                       {
-                       /* Since we use pango markup to display, we need to escape here */
-                       gchar *escaped = g_markup_escape_text(data, -1);
-                       g_free(data);
-                       data = escaped;
-                       }
-
-               if (extra)
-                       {
-                       if (data && *data)
-                               {
-                               /* Display data between left and right parts of extra string
-                                * the data is expressed by a '*' character. A '*' may be escaped
-                                * by a \. You should escape all '*' characters, do not rely on the
-                                * current implementation which only replaces the first unescaped '*'.
-                                * If no "*" is present, the extra string is just appended to data string.
-                                * Pango mark up is accepted in left and right parts.
-                                * Any \n is replaced by a newline
-                                * Examples:
-                                * "<i>*</i>\n" -> data is displayed in italics ended with a newline
-                                * "\n"         -> ended with newline
-                                * "ISO *"      -> prefix data with "ISO " (ie. "ISO 100")
-                                * "\**\*"      -> prefix data with a star, and append a star (ie. "*100*")
-                                * "\\*"        -> prefix data with an anti slash (ie "\100")
-                                * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
-                                *
-                                * FIXME: using background / foreground colors lead to weird results.
-                                */
-                               gchar *new_data;
-                               gchar *left = NULL;
-                               gchar *right = extra;
-                               gchar *p;
-                               guint len = strlen(extra);
-
-                               /* Search for left and right parts and unescape characters */
-                               for (p = extra; *p; p++, len--)
-                                       if (p[0] == '\\')
-                                               {
-                                               if (p[1] == 'n')
-                                                       {
-                                                       memmove(p+1, p+2, --len);
-                                                       p[0] = '\n';
-                                                       }
-                                               else if (p[1] != '\0')
-                                                       memmove(p, p+1, len--); // includes \0
-                                               }
-                                       else if (p[0] == '*' && !left)
-                                               {
-                                               right = p + 1;
-                                               left = extra;
-                                               }
-
-                               if (left) right[-1] = '\0';
-
-                               new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
-                               g_free(data);
-                               data = new_data;
-                               }
-                       g_free(extra);
-                       }
-
-               g_string_erase(new, pos, end-start+1);
-               if (data && *data)
-                       {
-                       if (want_separator)
-                               {
-                               /* insert separator */
-                               g_string_insert(new, pos, sep);
-                               pos += strlen(sep);
-                               want_separator = FALSE;
-                               }
-
-                       g_string_insert(new, pos, data);
-                       pos += strlen(data);
-               }
-
-               if (pos-prev >= 1 && new->str[pos] == imp)
-                       {
-                       /* pipe character is replaced by a separator, delete it
-                        * and raise a flag if needed */
-                       g_string_erase(new, pos--, 1);
-                       want_separator |= (data && *data);
+                       image_osd_set(imd, show | OSD_SHOW_HISTOGRAM);
                        }
-
-               if (new->str[pos] == '\n') want_separator = FALSE;
-
-               prev = pos - 1;
-
-               g_free(name);
-               g_free(data);
-               }
-
-       /* search and destroy empty lines */
-       end = new->str;
-       while ((start = strchr(end, '\n')))
-               {
-               end = start;
-               while (*++(end) == '\n')
-                       ;
-               g_string_erase(new, start-new->str, end-start-1);
                }
-
-       g_strchomp(new->str);
-
-       ret = new->str;
-       g_string_free(new, FALSE);
-
-       return ret;
-}
-
-typedef enum {
-       OSDT_NONE       = 0,
-       OSDT_FREE       = 1 << 0,
-       OSDT_NO_DUP     = 1 << 1
-} OsdTemplateFlags;
-
-static void osd_template_insert(GHashTable *vars, gchar *keyword, gchar *value, OsdTemplateFlags flags)
-{
-       if (!value)
-               {
-               g_hash_table_insert(vars, keyword, g_strdup(""));
-               return;
-               }
-
-       if (flags & OSDT_NO_DUP)
-               {
-               g_hash_table_insert(vars, keyword, value);
-               return;
-               }
-       else
-               {
-               g_hash_table_insert(vars, keyword, g_strdup(value));
-               }
-
-       if (flags & OSDT_FREE) g_free((gpointer) value);
 }
 
 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
@@ -585,6 +306,7 @@ static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
                osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
                osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
                osd_template_insert(vars, "name", (gchar *) name, OSDT_NONE);
+               osd_template_insert(vars, "path", (gchar *) image_get_path(imd), OSDT_NONE);
                osd_template_insert(vars, "date", imd->image_fd ? ((gchar *) text_from_time(imd->image_fd->date)) : "", OSDT_NONE);
                osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
                osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
@@ -618,7 +340,7 @@ static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
                        osd_template_insert(vars, "res", NULL, OSDT_NONE);
                        }
 
-               text = image_osd_mkinfo(options->image_overlay.template_string, imd, vars);
+               text = image_osd_mkinfo(options->image_overlay.template_string, imd->image_fd, vars);
                g_hash_table_destroy(vars);
 
        } else {
@@ -793,6 +515,42 @@ static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
        return icon;
 }
 
+static GdkPixbuf *image_osd_guidelines_render(OverlayStateData *osd)
+{
+       gint width, height;
+       GdkPixbuf *rectangles;
+       ImageWindow *imd = osd->imd;
+
+/** @FIXME guidelines does not work with revised draw signal handling
+ */
+       //~ pixbuf_renderer_get_scaled_size((PixbufRenderer *)imd->pr, &width, &height);
+
+       //~ if (width && height)
+               //~ {
+               //~ rectangles = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
+               //~ if (rectangles)
+                       //~ {
+                       //~ pixbuf_set_rect_fill(rectangles, 0, 0, width, height, 255, 255, 255, 0);
+                       //~ pixbuf_set_rect(rectangles, 0, 0 + (height / 3), width, height / 3,
+                                                               //~ 0, 0, 0, 255,
+                                                               //~ 1, 1, 1, 1);
+                       //~ pixbuf_set_rect(rectangles, 0, 0 + (height / 3 + 1), width, height / 3 - 2,
+                                                               //~ 255, 255, 255, 255,
+                                                               //~ 1, 1, 1, 1);
+
+                       //~ pixbuf_set_rect(rectangles, 0 + width / 3, 0 , width / 3, height,
+                                                               //~ 0, 0, 0, 255,
+                                                               //~ 1, 1, 1, 1);
+                       //~ pixbuf_set_rect(rectangles, 0 + width / 3 + 1, 0, width / 3 - 2, height,
+                                                               //~ 255, 255, 255, 255,
+                                                               //~ 1, 1, 1, 1);
+                       //~ return rectangles;
+                       //~ }
+               //~ }
+
+       return NULL;
+}
+
 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
                              OverlayRendererFlags flags)
 {
@@ -876,7 +634,7 @@ static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
 {
        if (osd->ovl_info == 0)
                {
-               osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, OVL_RELATIVE);
+               osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, osd->origin);
                }
        else
                {
@@ -902,19 +660,41 @@ static gboolean image_osd_update_cb(gpointer data)
                   with histogram we have to redraw also when loading is finished */
                if (osd->changed_states & IMAGE_STATE_IMAGE ||
                    (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
+                   (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_GUIDELINES) ||
                    osd->notify & NOTIFY_HISTMAP)
                        {
                        GdkPixbuf *pixbuf;
 
-                       pixbuf = image_osd_info_render(osd);
-                       if (pixbuf)
+                       if (osd->show & OSD_SHOW_GUIDELINES)
                                {
-                               image_osd_info_show(osd, pixbuf);
-                               g_object_unref(pixbuf);
+                               ImageWindow *imd = osd->imd;
+                               osd->x = ((PixbufRenderer *)imd->pr)->x_offset;
+                               osd->y = ((PixbufRenderer *)imd->pr)->y_offset;
+                               osd->origin = OVL_NORMAL;
+
+                               pixbuf = image_osd_guidelines_render(osd);
+                               if (pixbuf)
+                                       {
+                                       image_osd_info_show(osd, pixbuf);
+                                       g_object_unref(pixbuf);
+                                       }
+
+                               osd->x = options->image_overlay.x;
+                               osd->y = options->image_overlay.y;
+                               osd->origin = OVL_RELATIVE;
                                }
                        else
                                {
-                               image_osd_info_hide(osd);
+                               pixbuf = image_osd_info_render(osd);
+                               if (pixbuf)
+                                       {
+                                       image_osd_info_show(osd, pixbuf);
+                                       g_object_unref(pixbuf);
+                                       }
+                               else
+                                       {
+                                       image_osd_info_hide(osd);
+                                       }
                                }
                        }
                }
@@ -1088,6 +868,7 @@ static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
                osd->show = OSD_SHOW_NOTHING;
                osd->x = options->image_overlay.x;
                osd->y = options->image_overlay.y;
+               osd->origin = OVL_RELATIVE;
 
                osd->histogram = histogram_new();