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