7e1f6fda738d29227762c7d1fc92e6f61570c108
[geeqie.git] / src / osd.c
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 <math.h>
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         {NULL, NULL}};
82
83 static GtkTargetEntry osd_drag_types[] = {
84         { "text/plain", GTK_TARGET_SAME_APP, TARGET_TEXT_PLAIN }
85 };
86
87 typedef struct _TagData TagData;
88 struct _TagData
89 {
90         gchar *key;
91         gchar *title;
92 };
93
94 static void tag_button_cb(GtkWidget *widget, gpointer data)
95 {
96         GtkTextView *image_overlay_template_view = data;
97         GtkTextBuffer *buffer;
98         TagData *td;
99
100         buffer = gtk_text_view_get_buffer(image_overlay_template_view);
101         td = g_object_get_data(G_OBJECT(widget), "tag_data");
102         gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer), td->key, -1);
103
104         gtk_widget_grab_focus(GTK_WIDGET(image_overlay_template_view));
105 }
106
107 static void osd_dnd_get_cb(GtkWidget *btn, GdkDragContext *context,
108                                                                 GtkSelectionData *selection_data, guint info,
109                                                                 guint time, gpointer data)
110 {
111         TagData *td;
112         GtkTextView *image_overlay_template_view = data;
113
114         td = g_object_get_data(G_OBJECT(btn), "tag_data");
115         gtk_selection_data_set_text(selection_data, td->key, -1);
116
117         gtk_widget_grab_focus(GTK_WIDGET(image_overlay_template_view));
118 }
119
120 static void osd_btn_destroy_cb(GtkWidget *btn, GdkDragContext *context,
121                                                                 GtkSelectionData *selection_data, guint info,
122                                                                 guint time, gpointer data)
123 {
124         TagData *td;
125
126         td = g_object_get_data(G_OBJECT(btn), "tag_data");
127         g_free(td->key);
128         g_free(td->title);
129 }
130
131 static void set_osd_button(GtkTable *table, const gint rows, const gint cols, const gchar *key, const gchar *title, GtkWidget *template_view)
132 {
133         GtkWidget *new_button;
134         TagData *td;
135
136         new_button = gtk_button_new_with_label(title);
137         g_signal_connect(G_OBJECT(new_button), "clicked", G_CALLBACK(tag_button_cb), template_view);
138         gtk_widget_show(new_button);
139
140         td = g_new0(TagData, 1);
141         td->key = g_strdup(key);
142         td->title = g_strdup(title);
143
144         g_object_set_data(G_OBJECT(new_button), "tag_data", td);
145
146         gtk_drag_source_set(new_button, GDK_BUTTON1_MASK, osd_drag_types, 1, GDK_ACTION_COPY);
147         g_signal_connect(G_OBJECT(new_button), "drag_data_get",
148                                                         G_CALLBACK(osd_dnd_get_cb), template_view);
149         g_signal_connect(G_OBJECT(new_button), "destroy",
150                                                         G_CALLBACK(osd_btn_destroy_cb), new_button);
151
152         gtk_table_attach_defaults(table, new_button, cols, cols+1, rows, rows+1);
153
154 }
155
156 GtkWidget *osd_new(gint max_cols, GtkWidget *template_view)
157 {
158         GtkWidget *hbox;
159         GtkWidget *vbox;
160         GtkWidget *vbox_buttons;
161         GtkWidget *group;
162         GtkWidget *button;
163         GtkWidget *scrolled;
164         GtkTextBuffer *buffer;
165         GtkWidget *label;
166         GtkWidget *     subgroup;
167         gint i = 0;
168         gint rows = 0;
169         gint max_rows = 0;
170         gint col = 0;
171         gint cols = 0;
172         gdouble entries;
173         GtkWidget *viewport;
174
175         vbox = gtk_vbox_new(FALSE, 0);
176
177         pref_label_new(vbox, _("To include predefined tags in the template, click a button or drag-and-drop"));
178
179         scrolled = gtk_scrolled_window_new(NULL, NULL);
180         gtk_box_pack_start(GTK_BOX(vbox), scrolled, FALSE, FALSE, 0);
181         gtk_container_set_border_width(GTK_CONTAINER(scrolled), PREF_PAD_BORDER);
182         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
183                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
184         label = gtk_label_new("title");
185         gtk_widget_show(scrolled);
186         gtk_widget_set_size_request(scrolled, -1, 140);
187
188         viewport = gtk_viewport_new(NULL, NULL);
189         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
190         gtk_container_add(GTK_CONTAINER(scrolled), viewport);
191         gtk_widget_show(viewport);
192
193         entries = (sizeof(predefined_tags) / sizeof(predefined_tags[0])) - 1;
194         max_rows = ceil(entries / max_cols);
195
196         GtkTable *table;
197         table = GTK_TABLE(gtk_table_new(max_rows, max_cols, FALSE));
198         gtk_container_add(GTK_CONTAINER(viewport), GTK_WIDGET(table));
199         gtk_widget_show(GTK_WIDGET(table));
200
201         for (rows = 0; rows < max_rows; rows++)
202                 {
203                 cols = 0;
204
205                 while (cols < max_cols && predefined_tags[i][0])
206                         {
207                         set_osd_button(table, rows, cols, predefined_tags[i][0], predefined_tags[i][1], template_view);
208                         i = i + 1;
209                         cols++;
210                         }
211                 }
212         return vbox;
213 }
214 static gchar *keywords_to_string(FileData *fd)
215 {
216         GList *keywords;
217         GString *kwstr = NULL;
218         gchar *ret = NULL;
219
220         g_assert(fd);
221
222         keywords = metadata_read_list(fd, KEYWORD_KEY, METADATA_PLAIN);
223
224         if (keywords)
225                 {
226                 GList *work = keywords;
227
228                 while (work)
229                         {
230                         gchar *kw = work->data;
231                         work = work->next;
232
233                         if (!kw) continue;
234                         if (!kwstr)
235                                 kwstr = g_string_new("");
236                         else
237                                 g_string_append(kwstr, ", ");
238
239                         g_string_append(kwstr, kw);
240                         }
241                 string_list_free(keywords);
242                 }
243
244         if (kwstr)
245                 {
246                 ret = kwstr->str;
247                 g_string_free(kwstr, FALSE);
248                 }
249
250         return ret;
251 }
252
253 gchar *image_osd_mkinfo(const gchar *str, FileData *fd, GHashTable *vars)
254 {
255         gchar delim = '%', imp = '|', sep[] = " - ";
256         gchar *start, *end;
257         guint pos, prev;
258         gboolean want_separator = FALSE;
259         gchar *name, *data;
260         GString *new;
261         gchar *ret;
262
263         if (!str || !*str) return g_strdup("");
264
265         new = g_string_new(str);
266
267         prev = -1;
268
269         while (TRUE)
270                 {
271                 guint limit = 0;
272                 gchar *trunc = NULL;
273                 gchar *limpos = NULL;
274                 gchar *extra = NULL;
275                 gchar *extrapos = NULL;
276                 gchar *p;
277
278                 start = strchr(new->str + (prev + 1), delim);
279                 if (!start)
280                         break;
281                 end = strchr(start+1, delim);
282                 if (!end)
283                         break;
284
285                 /* Search for optionnal modifiers
286                  * %name:99:extra% -> name = "name", limit=99, extra = "extra"
287                  */
288                 for (p = start + 1; p < end; p++)
289                         {
290                         if (p[0] == ':')
291                                 {
292                                 if (g_ascii_isdigit(p[1]) && !limpos)
293                                         {
294                                         limpos = p + 1;
295                                         if (!trunc) trunc = p;
296                                         }
297                                 else
298                                         {
299                                         extrapos = p + 1;
300                                         if (!trunc) trunc = p;
301                                         break;
302                                         }
303                                 }
304                         }
305
306                 if (limpos)
307                         limit = (guint) atoi(limpos);
308
309                 if (extrapos)
310                         extra = g_strndup(extrapos, end - extrapos);
311
312                 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
313                 pos = start - new->str;
314                 data = NULL;
315
316                 if (strcmp(name, "keywords") == 0)
317                         {
318                         data = keywords_to_string(fd);
319                         }
320                 else if (strcmp(name, "comment") == 0)
321                         {
322                         data = metadata_read_string(fd, COMMENT_KEY, METADATA_PLAIN);
323                         }
324                 else if (strcmp(name, "imagecomment") == 0)
325                         {
326                         data = exif_get_image_comment(fd);
327                         }
328                 else if (strcmp(name, "rating") == 0)
329                         {
330                         data = metadata_read_string(fd, RATING_KEY, METADATA_PLAIN);
331                         }
332 #ifdef HAVE_LUA
333                 else if (strncmp(name, "lua/", 4) == 0)
334                         {
335                         gchar *tmp;
336                         tmp = strchr(name+4, '/');
337                         if (!tmp)
338                                 break;
339                         *tmp = '\0';
340                         data = lua_callvalue(fd, name+4, tmp+1);
341                         }
342 #endif
343                 else
344                         {
345                         data = g_strdup(g_hash_table_lookup(vars, name));
346                         if (!data)
347                                 data = metadata_read_string(fd, name, METADATA_FORMATTED);
348                         }
349
350                 if (data && *data && limit > 0 && strlen(data) > limit + 3)
351                         {
352                         gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
353                         g_free(data);
354                         data = new_data;
355                         }
356
357                 if (data)
358                         {
359                         /* Since we use pango markup to display, we need to escape here */
360                         gchar *escaped = g_markup_escape_text(data, -1);
361                         g_free(data);
362                         data = escaped;
363                         }
364
365                 if (extra)
366                         {
367                         if (data && *data)
368                                 {
369                                 /* Display data between left and right parts of extra string
370                                  * the data is expressed by a '*' character. A '*' may be escaped
371                                  * by a \. You should escape all '*' characters, do not rely on the
372                                  * current implementation which only replaces the first unescaped '*'.
373                                  * If no "*" is present, the extra string is just appended to data string.
374                                  * Pango mark up is accepted in left and right parts.
375                                  * Any \n is replaced by a newline
376                                  * Examples:
377                                  * "<i>*</i>\n" -> data is displayed in italics ended with a newline
378                                  * "\n"         -> ended with newline
379                                  * "ISO *"      -> prefix data with "ISO " (ie. "ISO 100")
380                                  * "\**\*"      -> prefix data with a star, and append a star (ie. "*100*")
381                                  * "\\*"        -> prefix data with an anti slash (ie "\100")
382                                  * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
383                                  *
384                                  * FIXME: using background / foreground colors lead to weird results.
385                                  */
386                                 gchar *new_data;
387                                 gchar *left = NULL;
388                                 gchar *right = extra;
389                                 gchar *p;
390                                 guint len = strlen(extra);
391
392                                 /* Search for left and right parts and unescape characters */
393                                 for (p = extra; *p; p++, len--)
394                                         if (p[0] == '\\')
395                                                 {
396                                                 if (p[1] == 'n')
397                                                         {
398                                                         memmove(p+1, p+2, --len);
399                                                         p[0] = '\n';
400                                                         }
401                                                 else if (p[1] != '\0')
402                                                         memmove(p, p+1, len--); // includes \0
403                                                 }
404                                         else if (p[0] == '*' && !left)
405                                                 {
406                                                 right = p + 1;
407                                                 left = extra;
408                                                 }
409
410                                 if (left) right[-1] = '\0';
411
412                                 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
413                                 g_free(data);
414                                 data = new_data;
415                                 }
416                         g_free(extra);
417                         }
418
419                 g_string_erase(new, pos, end-start+1);
420                 if (data && *data)
421                         {
422                         if (want_separator)
423                                 {
424                                 /* insert separator */
425                                 g_string_insert(new, pos, sep);
426                                 pos += strlen(sep);
427                                 want_separator = FALSE;
428                                 }
429
430                         g_string_insert(new, pos, data);
431                         pos += strlen(data);
432                 }
433
434                 if (pos-prev >= 1 && new->str[pos] == imp)
435                         {
436                         /* pipe character is replaced by a separator, delete it
437                          * and raise a flag if needed */
438                         g_string_erase(new, pos--, 1);
439                         want_separator |= (data && *data);
440                         }
441
442                 if (new->str[pos] == '\n') want_separator = FALSE;
443
444                 prev = pos - 1;
445
446                 g_free(name);
447                 g_free(data);
448                 }
449
450         /* search and destroy empty lines */
451         end = new->str;
452         while ((start = strchr(end, '\n')))
453                 {
454                 end = start;
455                 while (*++(end) == '\n')
456                         ;
457                 g_string_erase(new, start-new->str, end-start-1);
458                 }
459
460         g_strchomp(new->str);
461
462         ret = new->str;
463         g_string_free(new, FALSE);
464
465         return ret;
466 }
467
468 void osd_template_insert(GHashTable *vars, gchar *keyword, gchar *value, OsdTemplateFlags flags)
469 {
470         if (!value)
471                 {
472                 g_hash_table_insert(vars, keyword, g_strdup(""));
473                 return;
474                 }
475
476         if (flags & OSDT_NO_DUP)
477                 {
478                 g_hash_table_insert(vars, keyword, value);
479                 return;
480                 }
481         else
482                 {
483                 g_hash_table_insert(vars, keyword, g_strdup(value));
484                 }
485
486         if (flags & OSDT_FREE) g_free((gpointer) value);
487 }
488 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */