819b991a5693d59dad3a0bb332a15aa19cc4c77b
[geeqie.git] / src / osd.cc
1 /*
2  * Copyright (C) 2018 The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /* Routines for creating the Overlay Screen Display text. Also
22  * used for the same purposes by the Print routines
23  */
24
25 #include "main.h"
26 #include "osd.h"
27
28 #include "dnd.h"
29 #include "exif.h"
30 #include "glua.h"
31 #include "metadata.h"
32 #include "ui-fileops.h"
33 #include "ui-misc.h"
34
35 #include <cmath>
36
37 static const gchar *predefined_tags[][2] = {
38         {"%name%",                                                      N_("Name")},
39         {"%path:60%",                                           N_("Path")},
40         {"%date%",                                                      N_("Date")},
41         {"%size%",                                                      N_("Size")},
42         {"%zoom%",                                                      N_("Zoom")},
43         {"%dimensions%",                                        N_("Dimensions")},
44         {"%collection%",                                        N_("Collection")},
45         {"%number%",                                            N_("Image index")},
46         {"%total%",                                                     N_("Images total")},
47         {"%comment%",                                           N_("Comment")},
48         {"%keywords%",                                          N_("Keywords")},
49         {"%file.ctime%",                                        N_("File ctime")},
50         {"%file.mode%",                                         N_("File mode")},
51         {"%file.owner%",                                        N_("File owner")},
52         {"%file.group%",                                        N_("File group")},
53         {"%file.link%",                                         N_("File link")},
54         {"%file.class%",                                        N_("File class")},
55         {"%file.page_no%",                                      N_("File page no.")},
56         {"%formatted.DateTime%",                        N_("Image date")},
57         {"%formatted.DateTimeDigitized%",       N_("Date digitized")},
58         {"%formatted.ShutterSpeed%",            N_("ShutterSpeed")},
59         {"%formatted.Aperture%",                        N_("Aperture")},
60         {"%formatted.ExposureBias%",            N_("Exposure bias")},
61         {"%formatted.Resolution%",                      N_("Resolution")},
62         {"%formatted.Camera%",                          N_("Camera")},
63         {"%lua.lensID%",                                        N_("Lens")},
64         {"%formatted.ISOSpeedRating%",          N_("ISO")},
65         {"%formatted.FocalLength%",                     N_("Focal length")},
66         {"%formatted.FocalLength35mmFilm%",     N_("Focal len. 35mm")},
67         {"%formatted.SubjectDistance%",         N_("Subject distance")},
68         {"%formatted.Flash%",                           N_("Flash")},
69         {"%formatted.ColorProfile%",            N_("Color profile")},
70         {"%formatted.GPSPosition%",                     N_("Lat, Long")},
71         {"%formatted.GPSAltitude%",                     N_("Altitude")},
72         {"%formatted.localtime%",                       N_("Local time")},
73         {"%formatted.timezone%",                        N_("Timezone")},
74         {"%formatted.countryname%",                     N_("Country name")},
75         {"%formatted.countrycode%",                     N_("Country code")},
76         {"%rating%",                                            N_("Rating")},
77         {"%formatted.star_rating%",                     N_("Star rating")},
78         {"%Xmp.dc.creator%",                            N_("© Creator")},
79         {"%Xmp.dc.contributor%",                        N_("© Contributor")},
80         {"%Xmp.dc.rights%",                                     N_("© Rights")},
81         {nullptr, nullptr}};
82
83 static GtkTargetEntry osd_drag_types[] = {
84         { const_cast<gchar *>("text/plain"), GTK_TARGET_SAME_APP, TARGET_TEXT_PLAIN }
85 };
86
87 struct TagData
88 {
89         gchar *key;
90         gchar *title;
91 };
92
93 static void tag_button_cb(GtkWidget *widget, gpointer data)
94 {
95         auto image_overlay_template_view = static_cast<GtkTextView *>(data);
96         GtkTextBuffer *buffer;
97         TagData *td;
98
99         buffer = gtk_text_view_get_buffer(image_overlay_template_view);
100         td = static_cast<TagData *>(g_object_get_data(G_OBJECT(widget), "tag_data"));
101         gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer), td->key, -1);
102
103         gtk_widget_grab_focus(GTK_WIDGET(image_overlay_template_view));
104 }
105
106 static void osd_dnd_get_cb(GtkWidget *btn, GdkDragContext *UNUSED(context),
107                                                                 GtkSelectionData *selection_data, guint UNUSED(info),
108                                                                 guint UNUSED(time), gpointer data)
109 {
110         TagData *td;
111         auto image_overlay_template_view = static_cast<GtkTextView *>(data);
112
113         td = static_cast<TagData *>(g_object_get_data(G_OBJECT(btn), "tag_data"));
114         gtk_selection_data_set_text(selection_data, td->key, -1);
115
116         gtk_widget_grab_focus(GTK_WIDGET(image_overlay_template_view));
117 }
118
119 static void osd_btn_destroy_cb(GtkWidget *btn, GdkDragContext *UNUSED(context),
120                                                                 GtkSelectionData *UNUSED(selection_data), guint UNUSED(info),
121                                                                 guint UNUSED(time), gpointer UNUSED(data))
122 {
123         TagData *td;
124
125         td = static_cast<TagData *>(g_object_get_data(G_OBJECT(btn), "tag_data"));
126         g_free(td->key);
127         g_free(td->title);
128 }
129
130 static void set_osd_button(GtkTable *table, const gint rows, const gint cols, const gchar *key, const gchar *title, GtkWidget *template_view)
131 {
132         GtkWidget *new_button;
133         TagData *td;
134
135         new_button = gtk_button_new_with_label(title);
136         g_signal_connect(G_OBJECT(new_button), "clicked", G_CALLBACK(tag_button_cb), template_view);
137         gtk_widget_show(new_button);
138
139         td = g_new0(TagData, 1);
140         td->key = g_strdup(key);
141         td->title = g_strdup(title);
142
143         g_object_set_data(G_OBJECT(new_button), "tag_data", td);
144
145         gtk_drag_source_set(new_button, GDK_BUTTON1_MASK, osd_drag_types, 1, GDK_ACTION_COPY);
146         g_signal_connect(G_OBJECT(new_button), "drag_data_get",
147                                                         G_CALLBACK(osd_dnd_get_cb), template_view);
148         g_signal_connect(G_OBJECT(new_button), "destroy",
149                                                         G_CALLBACK(osd_btn_destroy_cb), new_button);
150
151         gtk_table_attach_defaults(table, new_button, cols, cols+1, rows, rows+1);
152
153 }
154
155 GtkWidget *osd_new(gint max_cols, GtkWidget *template_view)
156 {
157         GtkWidget *vbox;
158         GtkWidget *scrolled;
159         gint i = 0;
160         gint rows = 0;
161         gint max_rows = 0;
162         gint cols = 0;
163         gdouble entries;
164         GtkWidget *viewport;
165
166         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
167
168         pref_label_new(vbox, _("To include predefined tags in the template, click a button or drag-and-drop"));
169
170         scrolled = gtk_scrolled_window_new(nullptr, nullptr);
171         gtk_box_pack_start(GTK_BOX(vbox), scrolled, FALSE, FALSE, 0);
172         gtk_container_set_border_width(GTK_CONTAINER(scrolled), PREF_PAD_BORDER);
173         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
174                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
175         gtk_widget_show(scrolled);
176         gtk_widget_set_size_request(scrolled, -1, 140);
177
178         viewport = gtk_viewport_new(nullptr, nullptr);
179         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
180         gtk_container_add(GTK_CONTAINER(scrolled), viewport);
181         gtk_widget_show(viewport);
182
183         entries = (sizeof(predefined_tags) / sizeof(predefined_tags[0])) - 1;
184         max_rows = ceil(entries / max_cols);
185
186         GtkTable *table;
187         table = GTK_TABLE(gtk_table_new(max_rows, max_cols, FALSE));
188         gtk_container_add(GTK_CONTAINER(viewport), GTK_WIDGET(table));
189         gtk_widget_show(GTK_WIDGET(table));
190
191         for (rows = 0; rows < max_rows; rows++)
192                 {
193                 cols = 0;
194
195                 while (cols < max_cols && predefined_tags[i][0])
196                         {
197                         set_osd_button(table, rows, cols, predefined_tags[i][0], predefined_tags[i][1], template_view);
198                         i = i + 1;
199                         cols++;
200                         }
201                 }
202         return vbox;
203 }
204 static gchar *keywords_to_string(FileData *fd)
205 {
206         GList *keywords;
207         GString *kwstr = nullptr;
208         gchar *ret = nullptr;
209
210         g_assert(fd);
211
212         keywords = metadata_read_list(fd, KEYWORD_KEY, METADATA_PLAIN);
213
214         if (keywords)
215                 {
216                 GList *work = keywords;
217
218                 while (work)
219                         {
220                         auto kw = static_cast<gchar *>(work->data);
221                         work = work->next;
222
223                         if (!kw) continue;
224                         if (!kwstr)
225                                 kwstr = g_string_new("");
226                         else
227                                 g_string_append(kwstr, ", ");
228
229                         g_string_append(kwstr, kw);
230                         }
231                 g_list_free_full(keywords, g_free);
232                 }
233
234         if (kwstr)
235                 {
236                 ret = g_strdup(kwstr->str);
237                 g_string_free(kwstr, TRUE);
238                 }
239
240         return ret;
241 }
242
243 gchar *image_osd_mkinfo(const gchar *str, FileData *fd, GHashTable *vars)
244 {
245         gchar delim = '%', imp = '|', sep[] = " - ";
246         gchar *start, *end;
247         guint pos, prev;
248         gboolean want_separator = FALSE;
249         gchar *name, *data;
250         GString *osd_info;
251         gchar *ret;
252
253         if (!str || !*str) return g_strdup("");
254
255         osd_info = g_string_new(str);
256
257         prev = -1;
258
259         while (TRUE)
260                 {
261                 guint limit = 0;
262                 gchar *trunc = nullptr;
263                 gchar *limpos = nullptr;
264                 gchar *extra = nullptr;
265                 gchar *extrapos = nullptr;
266                 gchar *p;
267
268                 start = strchr(osd_info->str + (prev + 1), delim);
269                 if (!start)
270                         break;
271                 end = strchr(start+1, delim);
272                 if (!end)
273                         break;
274
275                 /* Search for optional modifiers
276                  * %name:99:extra% -> name = "name", limit=99, extra = "extra"
277                  */
278                 for (p = start + 1; p < end; p++)
279                         {
280                         if (p[0] == ':')
281                                 {
282                                 if (g_ascii_isdigit(p[1]) && !limpos)
283                                         {
284                                         limpos = p + 1;
285                                         if (!trunc) trunc = p;
286                                         }
287                                 else
288                                         {
289                                         extrapos = p + 1;
290                                         if (!trunc) trunc = p;
291                                         break;
292                                         }
293                                 }
294                         }
295
296                 if (limpos)
297                         limit = static_cast<guint>(atoi(limpos));
298
299                 if (extrapos)
300                         extra = g_strndup(extrapos, end - extrapos);
301
302                 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
303                 pos = start - osd_info->str;
304                 data = nullptr;
305
306                 if (strcmp(name, "keywords") == 0)
307                         {
308                         data = keywords_to_string(fd);
309                         }
310                 else if (strcmp(name, "comment") == 0)
311                         {
312                         data = metadata_read_string(fd, COMMENT_KEY, METADATA_PLAIN);
313                         }
314                 else if (strcmp(name, "imagecomment") == 0)
315                         {
316                         data = exif_get_image_comment(fd);
317                         }
318                 else if (strcmp(name, "rating") == 0)
319                         {
320                         data = metadata_read_string(fd, RATING_KEY, METADATA_PLAIN);
321                         }
322 #ifdef HAVE_LUA
323                 else if (strncmp(name, "lua/", 4) == 0)
324                         {
325                         gchar *tmp;
326                         tmp = strchr(name+4, '/');
327                         if (!tmp)
328                                 break;
329                         *tmp = '\0';
330                         data = lua_callvalue(fd, name+4, tmp+1);
331                         }
332 #endif
333                 else
334                         {
335                         data = g_strdup(static_cast<const gchar *>(g_hash_table_lookup(vars, static_cast<gconstpointer>(name))));
336                         if (!data)
337                                 data = metadata_read_string(fd, name, METADATA_FORMATTED);
338                         }
339
340                 if (data && *data && limit > 0 && strlen(data) > limit + 3)
341                         {
342                         gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
343                         g_free(data);
344                         data = new_data;
345                         }
346
347                 if (data)
348                         {
349                         /* Since we use pango markup to display, we need to escape here */
350                         gchar *escaped = g_markup_escape_text(data, -1);
351                         g_free(data);
352                         data = escaped;
353                         }
354
355                 if (extra)
356                         {
357                         if (data && *data)
358                                 {
359                                 /* Display data between left and right parts of extra string
360                                  * the data is expressed by a '*' character. A '*' may be escaped
361                                  * by a \. You should escape all '*' characters, do not rely on the
362                                  * current implementation which only replaces the first unescaped '*'.
363                                  * If no "*" is present, the extra string is just appended to data string.
364                                  * Pango mark up is accepted in left and right parts.
365                                  * Any \n is replaced by a newline
366                                  * Examples:
367                                  * "<i>*</i>\n" -> data is displayed in italics ended with a newline
368                                  * "\n"         -> ended with newline
369                                  * "ISO *"      -> prefix data with "ISO " (ie. "ISO 100")
370                                  * "\**\*"      -> prefix data with a star, and append a star (ie. "*100*")
371                                  * "\\*"        -> prefix data with an anti slash (ie "\100")
372                                  * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
373                                  */
374                                 /** @FIXME using background / foreground colors lead to weird results.
375                                  */
376                                 gchar *new_data;
377                                 gchar *left = nullptr;
378                                 gchar *right = extra;
379                                 gchar *p;
380                                 guint len = strlen(extra);
381
382                                 /* Search for left and right parts and unescape characters */
383                                 for (p = extra; *p; p++, len--)
384                                         if (p[0] == '\\')
385                                                 {
386                                                 if (p[1] == 'n')
387                                                         {
388                                                         memmove(p+1, p+2, --len);
389                                                         p[0] = '\n';
390                                                         }
391                                                 else if (p[1] != '\0')
392                                                         memmove(p, p+1, len--); // includes \0
393                                                 }
394                                         else if (p[0] == '*' && !left)
395                                                 {
396                                                 right = p + 1;
397                                                 left = extra;
398                                                 }
399
400                                 if (left) right[-1] = '\0';
401
402                                 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
403                                 g_free(data);
404                                 data = new_data;
405                                 }
406                         g_free(extra);
407                         }
408
409                 g_string_erase(osd_info, pos, end-start+1);
410                 if (data && *data)
411                         {
412                         if (want_separator)
413                                 {
414                                 /* insert separator */
415                                 g_string_insert(osd_info, pos, sep);
416                                 pos += strlen(sep);
417                                 want_separator = FALSE;
418                                 }
419
420                         g_string_insert(osd_info, pos, data);
421                         pos += strlen(data);
422                 }
423
424                 if (pos-prev >= 1 && osd_info->str[pos] == imp)
425                         {
426                         /* pipe character is replaced by a separator, delete it
427                          * and raise a flag if needed */
428                         g_string_erase(osd_info, pos--, 1);
429                         want_separator |= (data && *data);
430                         }
431
432                 if (osd_info->str[pos] == '\n') want_separator = FALSE;
433
434                 prev = pos - 1;
435
436                 g_free(name);
437                 g_free(data);
438                 }
439
440         /* search and destroy empty lines */
441         end = osd_info->str;
442         while ((start = strchr(end, '\n')))
443                 {
444                 end = start;
445                 while (*++(end) == '\n')
446                         ;
447                 g_string_erase(osd_info, start-osd_info->str, end-start-1);
448                 }
449
450         g_strchomp(osd_info->str);
451
452         ret = g_strdup(osd_info->str);
453         g_string_free(osd_info, TRUE);
454
455         return ret;
456 }
457
458 void osd_template_insert(GHashTable *vars, const gchar *keyword, const gchar *value, OsdTemplateFlags flags)
459 {
460         if (!value)
461                 {
462                 g_hash_table_insert(vars, const_cast<gchar *>(keyword), g_strdup(""));
463                 return;
464                 }
465
466         if (flags & OSDT_NO_DUP)
467                 {
468                 g_hash_table_insert(vars, const_cast<gchar *>(keyword), const_cast<gchar *>(value));
469                 return;
470                 }
471         else
472                 {
473                 g_hash_table_insert(vars, const_cast<gchar *>(keyword), g_strdup(value));
474                 }
475
476         if (flags & OSDT_FREE) g_free(const_cast<gchar *>(value));
477 }
478 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */