comparison src/osd.c @ 2866:87242753ed2c

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