Fix untranslated text
[geeqie.git] / src / bar-gps.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Colin Clark
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <config.h>
23
24 #if HAVE_LIBCHAMPLAIN
25 #if HAVE_LIBCHAMPLAIN_GTK
26
27 #include "bar-gps.h"
28
29 #include <cstring>
30
31 #include <cairo.h>
32 #include <champlain-gtk/champlain-gtk.h>
33 #include <champlain/champlain.h>
34 #include <clutter-gtk/clutter-gtk.h>
35 #include <clutter/clutter.h>
36 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #include <gdk/gdk.h>
38 #include <glib-object.h>
39 #include <pango/pango.h>
40
41 #include "bar.h"
42 #include "compat.h"
43 #include "debug.h"
44 #include "filedata.h"
45 #include "intl.h"
46 #include "layout.h"
47 #include "main-defines.h"
48 #include "metadata.h"
49 #include "misc.h"
50 #include "rcfile.h"
51 #include "thumb.h"
52 #include "typedefs.h"
53 #include "ui-menu.h"
54 #include "ui-utildlg.h"
55 #include "uri-utils.h"
56
57 #define MARKER_COLOUR 0x00, 0x00, 0xff, 0xff
58 #define TEXT_COLOUR 0x00, 0x00, 0x00, 0xff
59 #define THUMB_COLOUR 0xff, 0xff, 0xff, 0xff
60 #define THUMB_SIZE 100
61
62 #define DIRECTION_SIZE 300
63
64 /*
65  *-------------------------------------------------------------------
66  * GPS Map utils
67  *-------------------------------------------------------------------
68  */
69
70 struct PaneGPSData
71 {
72         PaneData pane;
73         GtkWidget *widget;
74         gchar *map_source;
75         gint height;
76         FileData *fd;
77         ClutterActor *gps_view;
78         ChamplainMarkerLayer *icon_layer;
79         GList *selection_list;
80         GList *not_added;
81         ChamplainBoundingBox *bbox;
82         guint num_added;
83         guint create_markers_id;
84         GtkWidget *progress;
85         GtkWidget *slider;
86         GtkWidget *state;
87         gint selection_count;
88         gboolean centre_map_checked;
89         gboolean enable_markers_checked;
90         gdouble dest_latitude;
91         gdouble dest_longitude;
92         GList *geocode_list;
93 };
94
95 /*
96  *-------------------------------------------------------------------
97  * drag-and-drop
98  *-------------------------------------------------------------------
99  */
100 enum {
101         TARGET_APP_COLLECTION_MEMBER,
102         TARGET_APP_EXIF_ENTRY,
103         TARGET_APP_KEYWORD_PATH,
104         TARGET_URI_LIST,
105         TARGET_TEXT_PLAIN
106 };
107
108 static GtkTargetEntry bar_pane_gps_drop_types[] = {
109         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
110         { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
111 };
112 static gint n_gps_entry_drop_types = 2;
113
114 static void bar_pane_gps_close_cancel_cb(GenericDialog *, gpointer data)
115 {
116         auto pgd = static_cast<PaneGPSData *>(data);
117
118         g_list_free(pgd->geocode_list);
119 }
120
121 static void bar_pane_gps_close_save_cb(GenericDialog *, gpointer data)
122 {
123         auto pgd = static_cast<PaneGPSData *>(data);
124         FileData *fd;
125         GList *work;
126
127         work = g_list_first(pgd->geocode_list);
128         while (work)
129                 {
130                 fd = static_cast<FileData *>(work->data);
131                 if (fd->name && !fd->parent)
132                         {
133                         work = work->next;
134                         metadata_write_GPS_coord(fd, "Xmp.exif.GPSLatitude", pgd->dest_latitude);
135                         metadata_write_GPS_coord(fd, "Xmp.exif.GPSLongitude", pgd->dest_longitude);
136                         }
137                 }
138         g_list_free(pgd->geocode_list);
139 }
140
141  static void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
142                                                                           gint x, gint y,
143                                                                           GtkSelectionData *selection_data, guint info,
144                                                                           guint, gpointer)
145 {
146         PaneGPSData *pgd;
147         GenericDialog *gd;
148         FileData *fd;
149         FileData *fd_found;
150         GList *work;
151         GList *list;
152         gint count;
153         gint geocoded_count;
154         gdouble latitude;
155         gdouble longitude;
156         GString *message;
157         gchar *location;
158         gchar **latlong;
159
160         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
161         if (!pgd) return;
162
163         if (info == TARGET_URI_LIST)
164                 {
165                 pgd->dest_longitude = champlain_view_x_to_longitude(CHAMPLAIN_VIEW(pgd->gps_view), x);
166                 pgd->dest_latitude = champlain_view_y_to_latitude(CHAMPLAIN_VIEW(pgd->gps_view), y);
167
168                 count = 0;
169                 geocoded_count = 0;
170                 pgd->geocode_list = nullptr;
171
172                 list = uri_filelist_from_gtk_selection_data(selection_data);
173
174                 if (list)
175                         {
176                         work = list;
177                         while (work)
178                                 {
179                                 fd = static_cast<FileData *>(work->data);
180                                 work = work->next;
181                                 if (fd->name && !fd->parent)
182                                         {
183                                         count++;
184                                         pgd->geocode_list = g_list_append(pgd->geocode_list, fd);
185                                         latitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLatitude", 1000);
186                                         longitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLongitude", 1000);
187                                         if (latitude != 1000 && longitude != 1000)
188                                                 {
189                                                 geocoded_count++;
190                                                 }
191                                         }
192                                 }
193                         g_list_free(list);
194
195                         if(count)
196                                 {
197                                 message = g_string_new("");
198                                 if (count == 1)
199                                         {
200                                         fd_found = static_cast<FileData *>(g_list_first(pgd->geocode_list)->data);
201                                         g_string_append_printf(message,
202                                                         _("\nDo you want to geocode image %s?"), fd_found->name);
203                                         }
204                                 else
205                                         {
206                                         g_string_append_printf(message,
207                                                         _("\nDo you want to geocode %i images?"), count);
208                                         }
209                                 if (geocoded_count == 1 && count == 1)
210                                         {
211                                         g_string_append(message,
212                                                         _("\nThis image is already geocoded!"));
213                                         }
214                                 else if (geocoded_count == 1 && count > 1)
215                                         {
216                                         g_string_append(message,
217                                                         _("\nOne image is already geocoded!"));
218                                         }
219                                 else if (geocoded_count > 1 && count > 1)
220                                         {
221                                         g_string_append_printf(message,
222                                                         _("\n%i Images are already geocoded!"), geocoded_count);
223                                         }
224
225                                 g_string_append_printf(message, _("\n\nPosition: %lf %lf \n"), pgd->dest_latitude, pgd->dest_longitude);
226
227                                 gd = generic_dialog_new(_("Geocode images"),
228                                                         "geocode_images", nullptr, TRUE,
229                                                         bar_pane_gps_close_cancel_cb, pgd);
230                                 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
231                                                         _("Write lat/long to meta-data?"),
232                                                         message->str, TRUE);
233
234                                 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"),
235                                                                                                 bar_pane_gps_close_save_cb, TRUE);
236
237                                 gtk_widget_show(gd->dialog);
238                                 g_string_free(message, TRUE);
239                                 }
240                         }
241                 }
242
243         if (info == TARGET_TEXT_PLAIN)
244                 {
245                 location = decode_geo_parameters(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)));
246                 if (!(g_strstr_len(location,-1,"Error")))
247                         {
248                         latlong = g_strsplit(location, " ", 2);
249                         champlain_view_center_on(CHAMPLAIN_VIEW(pgd->gps_view),
250                                                         g_ascii_strtod(latlong[0],nullptr),
251                                                         g_ascii_strtod(latlong[1],nullptr));
252                         g_strfreev(latlong);
253                         }
254                 g_free(location);
255                 }
256 }
257
258 static void bar_pane_gps_dnd_init(gpointer data)
259 {
260         auto pgd = static_cast<PaneGPSData *>(data);
261
262         gtk_drag_dest_set(pgd->widget,
263                           static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
264                           bar_pane_gps_drop_types, n_gps_entry_drop_types,
265                           static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE));
266         g_signal_connect(G_OBJECT(pgd->widget), "drag_data_received",
267                          G_CALLBACK(bar_pane_gps_dnd_receive), NULL);
268
269 }
270
271 static gboolean bar_gps_draw_direction (ClutterCanvas *, cairo_t *cr, gpointer)
272 {
273         cairo_set_source_rgb(cr, 255, 0, 0);
274
275         cairo_set_line_width(cr, 2);
276         cairo_move_to(cr, 0, 1);
277         cairo_line_to(cr, DIRECTION_SIZE, 1);
278
279         cairo_stroke(cr);
280
281         return TRUE;
282 }
283
284 static void bar_pane_gps_thumb_done_cb(ThumbLoader *tl, gpointer data)
285 {
286         FileData *fd;
287         ClutterActor *marker;
288         ClutterActor *actor;
289
290         marker = CLUTTER_ACTOR(data);
291         fd = static_cast<FileData *>(g_object_get_data(G_OBJECT(marker), "file_fd"));
292         if (fd->thumb_pixbuf != nullptr)
293                 {
294                 actor = gtk_clutter_texture_new();
295                 gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor), fd->thumb_pixbuf, nullptr);
296                 champlain_label_set_image(CHAMPLAIN_LABEL(marker), actor);
297                 }
298         thumb_loader_free(tl);
299 }
300
301 static void bar_pane_gps_thumb_error_cb(ThumbLoader *tl, gpointer)
302 {
303         thumb_loader_free(tl);
304 }
305
306 static gboolean bar_pane_gps_marker_keypress_cb(GtkWidget *widget, ClutterButtonEvent *bevent, gpointer)
307 {
308         FileData *fd;
309         ClutterActor *label_marker;
310         ClutterActor *parent_marker;
311         ClutterColor marker_colour = { MARKER_COLOUR };
312         ClutterColor text_colour = { TEXT_COLOUR };
313         ClutterColor thumb_colour = { THUMB_COLOUR };
314         gchar *current_text;
315         ClutterActor *actor;
316         ClutterActor *direction;
317         ClutterActor *current_image;
318         GString *text;
319         gint height;
320         gint width;
321         GdkPixbufRotation rotate;
322         gchar *altitude = nullptr;
323         ThumbLoader *tl;
324
325         if (bevent->button == MOUSE_BUTTON_LEFT)
326                 {
327                 label_marker = CLUTTER_ACTOR(widget);
328                 fd = static_cast<FileData *>(g_object_get_data(G_OBJECT(label_marker), "file_fd"));
329
330                 /* If the marker is showing a thumbnail, delete it
331                  */
332                 current_image = champlain_label_get_image(CHAMPLAIN_LABEL(label_marker));
333                 if (current_image != nullptr)
334                         {
335                         clutter_actor_destroy(CLUTTER_ACTOR(current_image));
336                         champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), nullptr);
337                         }
338
339                 current_text = g_strdup(champlain_label_get_text(CHAMPLAIN_LABEL(label_marker)));
340
341                 /* If the marker is showing only the text character, replace it with a
342                  * thumbnail and date and altitude
343                  */
344                 if (g_strcmp0(current_text, "i") == 0)
345                         {
346                         /* If a thumbail has already been generated, use that. If not try the pixbuf of the full image.
347                          * If not, call the thumb_loader to generate a thumbnail and update the marker later in the
348                          * thumb_loader callback
349                          */
350                         if (fd->thumb_pixbuf != nullptr)
351                                 {
352                                 actor = gtk_clutter_texture_new();
353                                 gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor), fd->thumb_pixbuf, nullptr);
354                                 champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), actor);
355                                 }
356                         else if (fd->pixbuf != nullptr)
357                                 {
358                                 actor = gtk_clutter_texture_new();
359                                 width = gdk_pixbuf_get_width (fd->pixbuf);
360                                 height = gdk_pixbuf_get_height (fd->pixbuf);
361                                 switch (fd->exif_orientation)
362                                         {
363                                         case 8:
364                                                 rotate = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
365                                                 break;
366                                         case 3:
367                                                 rotate = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
368                                                 break;
369                                         case 6:
370                                                 rotate = GDK_PIXBUF_ROTATE_CLOCKWISE;
371                                                 break;
372                                         default:
373                                                 rotate = GDK_PIXBUF_ROTATE_NONE;
374                                         }
375
376                                         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor),
377                                                                                 gdk_pixbuf_rotate_simple(gdk_pixbuf_scale_simple(fd->pixbuf, THUMB_SIZE, height * THUMB_SIZE / width,
378                                                                                 GDK_INTERP_NEAREST), rotate), nullptr);
379                                         champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), actor);
380                                 }
381                         else
382                                 {
383                                 tl = thumb_loader_new(THUMB_SIZE, THUMB_SIZE);
384                                 thumb_loader_set_callbacks(tl,
385                                                                                         bar_pane_gps_thumb_done_cb,
386                                                                                         bar_pane_gps_thumb_error_cb,
387                                                                                         nullptr,
388                                                                                         label_marker);
389                                 thumb_loader_start(tl, fd);
390                                 }
391
392                         text = g_string_new(fd->name);
393                         g_string_append(text, "\n");
394                         g_string_append(text, text_from_time(fd->date));
395                         g_string_append(text, "\n");
396                         altitude = metadata_read_string(fd, "formatted.GPSAltitude", METADATA_FORMATTED);
397                         if (altitude != nullptr)
398                                 {
399                                 g_string_append(text, altitude);
400                                 }
401
402                         champlain_label_set_text(CHAMPLAIN_LABEL(label_marker), text->str);
403                         champlain_label_set_font_name(CHAMPLAIN_LABEL(label_marker), "sans 8");
404                         champlain_marker_set_selection_color(&thumb_colour);
405                         champlain_marker_set_selection_text_color(&text_colour);
406
407                         g_free(altitude);
408                         g_string_free(text, TRUE);
409
410                         parent_marker = clutter_actor_get_parent(label_marker);
411                         if (clutter_actor_get_n_children(parent_marker ) > 1 )
412                                 {
413                                 direction = clutter_actor_get_child_at_index(parent_marker, 0);
414                                 clutter_actor_set_opacity(direction, 255);
415                                 }
416                         }
417                 /* otherwise, revert to the hidden text marker
418                  */
419                 else
420                         {
421                         champlain_label_set_text(CHAMPLAIN_LABEL(label_marker), "i");
422                         champlain_label_set_font_name(CHAMPLAIN_LABEL(label_marker), "courier 5");
423                         champlain_marker_set_selection_color(&marker_colour);
424                         champlain_marker_set_selection_text_color(&marker_colour);
425
426                         parent_marker = clutter_actor_get_parent(label_marker);
427                         if (clutter_actor_get_n_children(parent_marker ) > 1 )
428                                 {
429                                 direction = clutter_actor_get_child_at_index(parent_marker, 0);
430                                 clutter_actor_set_opacity(direction, 0);
431                                 }
432                         }
433
434                 g_free(current_text);
435
436                 return TRUE;
437                 }
438         return TRUE;
439 }
440
441 static gboolean bar_pane_gps_create_markers_cb(gpointer data)
442 {
443         auto pgd = static_cast<PaneGPSData *>(data);
444         gdouble latitude;
445         gdouble longitude;
446         gdouble compass;
447         FileData *fd;
448         ClutterActor *parent_marker;
449         ClutterActor *label_marker;
450         ClutterActor *direction;
451         ClutterColor marker_colour = { MARKER_COLOUR };
452         ClutterColor thumb_colour = { THUMB_COLOUR };
453         GString *message;
454         ClutterContent *canvas;
455
456         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgd->progress),
457                                                         static_cast<gdouble>(pgd->selection_count - g_list_length(pgd->not_added)) /
458                                                         static_cast<gdouble>(pgd->selection_count));
459
460         message = g_string_new("");
461         g_string_printf(message, "%i/%i", (pgd->selection_count - g_list_length(pgd->not_added)),
462                                                                                                                                                         pgd->selection_count);
463         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), message->str);
464         g_string_free(message, TRUE);
465
466         if(pgd->not_added)
467                 {
468                 fd = static_cast<FileData *>(pgd->not_added->data);
469                 pgd->not_added = pgd->not_added->next;
470
471                 latitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLatitude", 0);
472                 longitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLongitude", 0);
473                 compass = metadata_read_GPS_direction(fd, "Xmp.exif.GPSImgDirection", 1000);
474
475                 if (!(latitude == 0 && longitude == 0))
476                         {
477                         pgd->num_added++;
478
479                         parent_marker = champlain_marker_new();
480                         clutter_actor_set_reactive(parent_marker, FALSE);
481                         label_marker = champlain_label_new_with_text("i","courier 5", &marker_colour, &marker_colour);
482                         clutter_actor_set_reactive(label_marker, TRUE);
483                         champlain_marker_set_selection_color(&thumb_colour);
484
485                         if (compass != 1000)
486                                 {
487                                 canvas = clutter_canvas_new();
488                                 clutter_canvas_set_size(CLUTTER_CANVAS (canvas), DIRECTION_SIZE, 3);
489                                 g_signal_connect(canvas, "draw", G_CALLBACK(bar_gps_draw_direction), NULL);
490                                 direction = clutter_actor_new();
491                                 clutter_actor_set_size(direction, DIRECTION_SIZE, 3);
492                                 clutter_actor_set_position(direction, 0, 0);
493                                 clutter_actor_set_rotation_angle(direction, CLUTTER_Z_AXIS, compass -90.00);
494                                 clutter_actor_set_content(direction, canvas);
495                                 clutter_content_invalidate(canvas);
496                                 g_object_unref(canvas);
497
498                                 clutter_actor_add_child(parent_marker, direction);
499                                 clutter_actor_set_opacity(direction, 0);
500                                 }
501
502                         clutter_actor_add_child(parent_marker, label_marker);
503
504                         champlain_location_set_location(CHAMPLAIN_LOCATION(parent_marker), latitude, longitude);
505                         champlain_marker_layer_add_marker(pgd->icon_layer, CHAMPLAIN_MARKER(parent_marker));
506
507                         g_signal_connect(G_OBJECT(label_marker), "button_release_event",
508                                         G_CALLBACK(bar_pane_gps_marker_keypress_cb), pgd);
509
510                         g_object_set_data(G_OBJECT(label_marker), "file_fd", fd);
511
512                         champlain_bounding_box_extend(pgd->bbox, latitude, longitude);
513
514                         }
515                 return G_SOURCE_CONTINUE;
516                 }
517
518         if (pgd->centre_map_checked)
519                 {
520                 if (pgd->num_added == 1)
521                         {
522                         champlain_bounding_box_get_center(pgd->bbox, &latitude, &longitude);
523                         champlain_view_go_to(CHAMPLAIN_VIEW(pgd->gps_view), latitude, longitude);
524                         }
525                 else if (pgd->num_added > 1)
526                         {
527                         champlain_view_ensure_visible(CHAMPLAIN_VIEW(pgd->gps_view), pgd->bbox, TRUE);
528                         }
529                 }
530         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgd->progress), 0);
531         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), nullptr);
532         pgd->create_markers_id = 0;
533
534         return G_SOURCE_REMOVE;
535 }
536
537 static void bar_pane_gps_update(PaneGPSData *pgd)
538 {
539         GList *list;
540
541         /* If a create-marker background process is running, kill it
542          * and start again
543          */
544         if (pgd->create_markers_id != 0)
545                 {
546                 if (g_idle_remove_by_data(pgd))
547                         {
548                         pgd->create_markers_id = 0;
549                         }
550                 else
551                         {
552                         return;
553                         }
554                 }
555
556         /* Delete any markers currently displayed
557          */
558
559         champlain_marker_layer_remove_all(pgd->icon_layer);
560
561         if (!pgd->enable_markers_checked)
562                 {
563                 return;
564                 }
565
566         /* For each selected photo that has GPS data, create a marker containing
567          * a single, small text character the same colour as the marker background.
568          * Use a background process in case the user selects a large number of files.
569          */
570         filelist_free(pgd->selection_list);
571         if (pgd->bbox) champlain_bounding_box_free(pgd->bbox);
572
573         list = layout_selection_list(pgd->pane.lw);
574         list = file_data_process_groups_in_selection(list, FALSE, nullptr);
575
576         pgd->selection_list = list;
577         pgd->not_added = list;
578
579         pgd->bbox = champlain_bounding_box_new();
580         pgd->selection_count = g_list_length(pgd->selection_list);
581         pgd->create_markers_id = g_idle_add(bar_pane_gps_create_markers_cb, pgd);
582         pgd->num_added = 0;
583 }
584
585 static void bar_pane_gps_set_map_source(PaneGPSData *pgd, const gchar *map_id)
586 {
587         ChamplainMapSource *map_source;
588         ChamplainMapSourceFactory *map_factory;
589
590         map_factory = champlain_map_source_factory_dup_default();
591         map_source = champlain_map_source_factory_create(map_factory, map_id);
592
593         if (map_source != nullptr)
594                 {
595                 g_object_set(G_OBJECT(pgd->gps_view), "map-source", map_source, NULL);
596                 }
597
598         g_object_unref(map_factory);
599 }
600
601 static void bar_pane_gps_enable_markers_checked_toggle_cb(GtkWidget *, gpointer data)
602 {
603         auto pgd = static_cast<PaneGPSData *>(data);
604
605         if (pgd->enable_markers_checked)
606                 {
607                 pgd->enable_markers_checked = FALSE;
608                 }
609         else
610                 {
611                 pgd->enable_markers_checked = TRUE;
612                 }
613 }
614
615 static void bar_pane_gps_centre_map_checked_toggle_cb(GtkWidget *, gpointer data)
616 {
617         auto pgd = static_cast<PaneGPSData *>(data);
618
619         if (pgd->centre_map_checked)
620                 {
621                 pgd->centre_map_checked = FALSE;
622                 }
623         else
624                 {
625                 pgd->centre_map_checked = TRUE;
626                 }
627 }
628
629 static void bar_pane_gps_change_map_cb(GtkWidget *widget, gpointer data)
630 {
631         auto pgd = static_cast<PaneGPSData *>(data);
632         gchar *mapsource;
633
634         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
635                 return;
636
637         if (!pgd) return;
638
639         mapsource = static_cast<gchar *>(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
640         bar_pane_gps_set_map_source(pgd, mapsource);
641 }
642
643 static void bar_pane_gps_notify_selection(GtkWidget *bar, gint count)
644 {
645         PaneGPSData *pgd;
646
647         if (count == 0) return;
648
649         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(bar), "pane_data"));
650         if (!pgd) return;
651
652         bar_pane_gps_update(pgd);
653 }
654
655 static void bar_pane_gps_set_fd(GtkWidget *bar, FileData *fd)
656 {
657         PaneGPSData *pgd;
658
659         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(bar), "pane_data"));
660         if (!pgd) return;
661
662         file_data_unref(pgd->fd);
663         pgd->fd = file_data_ref(fd);
664
665         bar_pane_gps_update(pgd);
666 }
667
668 static gint bar_pane_gps_event(GtkWidget *bar, GdkEvent *event)
669 {
670         PaneGPSData *pgd;
671
672         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(bar), "pane_data"));
673         if (!pgd) return FALSE;
674
675         if (gtk_widget_has_focus(pgd->widget)) return gtk_widget_event(GTK_WIDGET(pgd->widget), event);
676
677         return FALSE;
678 }
679
680 static void bar_pane_gps_write_config(GtkWidget *pane, GString *outstr, gint indent)
681 {
682         PaneGPSData *pgd;
683         gint zoom;
684         ChamplainMapSource *mapsource;
685         const gchar *map_id;
686         gdouble position;
687         gint int_position;
688         gint w;
689         gint h;
690
691         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
692         if (!pgd) return;
693
694         WRITE_NL();
695         WRITE_STRING("<pane_gps ");
696         write_char_option(outstr, indent, "id", pgd->pane.id);
697         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pgd->pane.title)));
698         WRITE_BOOL(pgd->pane, expanded);
699
700         gtk_widget_get_size_request(GTK_WIDGET(pane), &w, &h);
701         pgd->height = h;
702
703         WRITE_INT(*pgd, height);
704         indent++;
705
706         g_object_get(G_OBJECT(pgd->gps_view), "map-source", &mapsource, NULL);
707         map_id = champlain_map_source_get_id(mapsource);
708         WRITE_NL();
709         write_char_option(outstr, indent, "map-id", map_id);
710
711         GString *buffer = g_string_new(nullptr);
712
713         g_object_get(G_OBJECT(pgd->gps_view), "zoom-level", &zoom, NULL);
714         g_string_printf(buffer, "%d", zoom);
715         WRITE_NL();
716         write_char_option(outstr, indent, "zoom-level", buffer->str);
717
718         g_object_get(G_OBJECT(pgd->gps_view), "latitude", &position, NULL);
719         int_position = position * 1000000;
720         g_string_printf(buffer, "%i", int_position);
721         WRITE_NL();
722         write_char_option(outstr, indent, "latitude", buffer->str);
723
724         g_object_get(G_OBJECT(pgd->gps_view), "longitude", &position, NULL);
725         int_position = position * 1000000;
726         g_string_printf(buffer, "%i", int_position);
727         WRITE_NL();
728         write_char_option(outstr, indent, "longitude", buffer->str);
729
730         indent--;
731         WRITE_NL();
732         WRITE_STRING("/>");
733
734         g_string_free(buffer, TRUE);
735         g_object_unref(mapsource);
736 }
737
738 static void bar_pane_gps_slider_changed_cb(GtkScaleButton *slider,
739                                            gdouble zoom,
740                                            gpointer data)
741 {
742         auto pgd = static_cast<PaneGPSData *>(data);
743         GString *message;
744
745         message = g_string_new("");
746         g_string_printf(message, _("Zoom %i"), static_cast<gint>(zoom));
747
748         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "zoom-level", static_cast<gint>(zoom), NULL);
749         gtk_widget_set_tooltip_text(GTK_WIDGET(slider), message->str);
750         g_string_free(message, TRUE);
751
752 }
753 static void bar_pane_gps_view_state_changed_cb(ChamplainView *view, GParamSpec *, gpointer data)
754 {
755         auto pgd = static_cast<PaneGPSData *>(data);
756         ChamplainState status;
757         gint zoom;
758         GString *message;
759
760         g_object_get(G_OBJECT(view), "zoom-level", &zoom, NULL);
761         message = g_string_new("");
762         g_string_printf(message, _("Zoom level %i"), zoom);
763
764         g_object_get(G_OBJECT(view), "state", &status, NULL);
765         if (status == CHAMPLAIN_STATE_LOADING)
766                 {
767                 gtk_label_set_text(GTK_LABEL(pgd->state), _("Loading map"));
768                 }
769         else
770                 {
771                 gtk_label_set_text(GTK_LABEL(pgd->state), message->str);
772                 }
773
774         gtk_widget_set_tooltip_text(GTK_WIDGET(pgd->slider), message->str);
775         gtk_scale_button_set_value(GTK_SCALE_BUTTON(pgd->slider), static_cast<gdouble>(zoom));
776
777         g_string_free(message, TRUE);
778 }
779
780 static void bar_pane_gps_notify_cb(FileData *fd, NotifyType type, gpointer data)
781 {
782         auto pgd = static_cast<PaneGPSData *>(data);
783
784         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) &&
785             g_list_find(pgd->selection_list, fd))
786                 {
787                 bar_pane_gps_update(pgd);
788                 }
789 }
790
791 static const gchar *bar_pane_gps_get_map_id(PaneGPSData *pgd)
792 {
793         const gchar *map_id;
794         ChamplainMapSource *mapsource;
795
796         g_object_get(G_OBJECT(pgd->gps_view), "map-source", &mapsource, NULL);
797         map_id = champlain_map_source_get_id(mapsource);
798
799         g_object_unref(mapsource);
800
801         return map_id;
802 }
803
804 static GtkWidget *bar_pane_gps_menu(PaneGPSData *pgd)
805 {
806         GtkWidget *menu;
807         GtkWidget *map_centre;
808         ChamplainMapSourceFactory *map_factory;
809         GSList *map_list;
810         ChamplainMapSourceDesc *map_desc;
811         const gchar *current;
812
813         menu = popup_menu_short_lived();
814
815         map_factory = champlain_map_source_factory_dup_default();
816         map_list = champlain_map_source_factory_get_registered(map_factory);
817         current = bar_pane_gps_get_map_id(pgd);
818
819         while (map_list)
820                 {
821                 map_desc = static_cast<ChamplainMapSourceDesc *>(map_list->data);
822
823                 menu_item_add_radio(menu,
824                                     champlain_map_source_desc_get_name(map_desc),
825                                     const_cast<gchar *>(champlain_map_source_desc_get_id(map_desc)),
826                                     strcmp(champlain_map_source_desc_get_id(map_desc), current) == 0,
827                                     G_CALLBACK(bar_pane_gps_change_map_cb), pgd);
828
829                 map_list = g_slist_next(map_list);
830                 }
831
832         menu_item_add_divider(menu);
833         menu_item_add_check(menu, _("Enable markers"), pgd->enable_markers_checked,
834                             G_CALLBACK(bar_pane_gps_enable_markers_checked_toggle_cb), pgd);
835         map_centre = menu_item_add_check(menu, _("Centre map on marker"), pgd->centre_map_checked,
836                                          G_CALLBACK(bar_pane_gps_centre_map_checked_toggle_cb), pgd);
837         if (!pgd->enable_markers_checked)
838                 {
839                 gtk_widget_set_sensitive(map_centre, FALSE);
840                 }
841
842         g_slist_free(map_list);
843         g_object_unref(map_factory);
844
845         return menu;
846 }
847
848 /* Determine if the map is to be re-centred on the marker when another photo is selected
849  */
850 static void bar_pane_gps_map_centreing(PaneGPSData *pgd)
851 {
852         GenericDialog *gd;
853         GString *message = g_string_new("");
854
855         if (pgd->centre_map_checked)
856                 {
857                 message = g_string_append(message, _("Move map centre to marker\n is disabled"));
858                 pgd->centre_map_checked = FALSE;
859                 }
860         else
861                 {
862                 message = g_string_append(message, _("Move map centre to marker\n is enabled"));
863                 pgd->centre_map_checked = TRUE;
864                 }
865
866         gd = generic_dialog_new(_("Map centering"),
867                                 "map_centering", nullptr, TRUE, nullptr, pgd);
868         generic_dialog_add_message(gd, GQ_ICON_DIALOG_INFO, _("Map Centering"), message->str, TRUE);
869         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", nullptr, TRUE);
870
871         gtk_widget_show(gd->dialog);
872
873         g_string_free(message, TRUE);
874 }
875
876 #if HAVE_GTK4
877 static gboolean bar_pane_gps_map_keypress_cb(GtkWidget *, GdkEventButton *bevent, gpointer data)
878 {
879 /* @FIXME GTK4 stub */
880         return FALSE;
881 }
882 #else
883 static gboolean bar_pane_gps_map_keypress_cb(GtkWidget *, GdkEventButton *bevent, gpointer data)
884 {
885         auto pgd = static_cast<PaneGPSData *>(data);
886         GtkWidget *menu;
887         GtkClipboard *clipboard;
888         gchar *geo_coords;
889
890         if (bevent->button == MOUSE_BUTTON_RIGHT)
891                 {
892                 menu = bar_pane_gps_menu(pgd);
893                 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
894                 return TRUE;
895                 }
896
897         if (bevent->button == MOUSE_BUTTON_MIDDLE)
898                 {
899                 bar_pane_gps_map_centreing(pgd);
900                 return TRUE;
901                 }
902
903         if (bevent->button == MOUSE_BUTTON_LEFT)
904                 {
905                 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
906                 geo_coords = g_strdup_printf("%lf %lf",
907                                                         champlain_view_y_to_latitude(
908                                                                 CHAMPLAIN_VIEW(pgd->gps_view),bevent->y),
909                                                         champlain_view_x_to_longitude(
910                                                                 CHAMPLAIN_VIEW(pgd->gps_view),bevent->x));
911                 gtk_clipboard_set_text(clipboard, geo_coords, -1);
912
913                 g_free(geo_coords);
914
915                 return TRUE;
916                 }
917
918         return FALSE;
919 }
920 #endif
921
922 static void bar_pane_gps_destroy(GtkWidget *, gpointer data)
923 {
924         auto pgd = static_cast<PaneGPSData *>(data);
925
926         file_data_unregister_notify_func(bar_pane_gps_notify_cb, pgd);
927
928         g_idle_remove_by_data(pgd);
929
930         filelist_free(pgd->selection_list);
931         if (pgd->bbox) champlain_bounding_box_free(pgd->bbox);
932
933         file_data_unref(pgd->fd);
934         g_free(pgd->map_source);
935         g_free(pgd->pane.id);
936         clutter_actor_destroy(pgd->gps_view);
937         g_free(pgd);
938 }
939
940
941 GtkWidget *bar_pane_gps_new(const gchar *id, const gchar *title, const gchar *map_id,
942                                                 const gint zoom, const gdouble latitude, const gdouble longitude,
943                                         gboolean expanded, gint height)
944 {
945         PaneGPSData *pgd;
946         GtkWidget *vbox;
947         GtkWidget *frame;
948         GtkWidget *gpswidget;
949         GtkWidget *status;
950         GtkWidget *state;
951         GtkWidget *progress;
952         GtkWidget *slider;
953         ChamplainMarkerLayer *layer;
954         ChamplainView *view;
955         const gchar *slider_list[] = {GQ_ICON_ZOOM_IN, GQ_ICON_ZOOM_OUT, nullptr};
956         const gchar **slider_icons = slider_list;
957
958         pgd = g_new0(PaneGPSData, 1);
959
960         pgd->pane.pane_set_fd = bar_pane_gps_set_fd;
961         pgd->pane.pane_notify_selection = bar_pane_gps_notify_selection;
962         pgd->pane.pane_event = bar_pane_gps_event;
963         pgd->pane.pane_write_config = bar_pane_gps_write_config;
964         pgd->pane.title = bar_pane_expander_title(title);
965         pgd->pane.id = g_strdup(id);
966         pgd->pane.type = PANE_GPS;
967         pgd->pane.expanded = expanded;
968         pgd->height = height;
969
970         frame = gtk_frame_new(nullptr);
971         DEBUG_NAME(frame);
972         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
973
974         gpswidget = gtk_champlain_embed_new();
975         view = gtk_champlain_embed_get_view(GTK_CHAMPLAIN_EMBED(gpswidget));
976
977         gq_gtk_box_pack_start(GTK_BOX(vbox), gpswidget, TRUE, TRUE, 0);
978         gtk_container_add(GTK_CONTAINER(frame), vbox);
979
980         status = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
981 #if HAVE_GTK4
982         slider = gtk_scale_button_new(1, 17, 1, slider_icons);
983 #else
984         slider = gtk_scale_button_new(GTK_ICON_SIZE_SMALL_TOOLBAR, 1, 17, 1, slider_icons);
985 #endif
986         gtk_widget_set_tooltip_text(slider, _("Zoom"));
987         gtk_scale_button_set_value(GTK_SCALE_BUTTON(slider), static_cast<gdouble>(zoom));
988
989         progress = gtk_progress_bar_new();
990         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "");
991         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(progress), TRUE);
992
993         state = gtk_label_new("");
994         gtk_label_set_justify(GTK_LABEL(state), GTK_JUSTIFY_LEFT);
995         gtk_label_set_ellipsize(GTK_LABEL(state), PANGO_ELLIPSIZE_START);
996         gtk_widget_set_tooltip_text(state, _("Zoom level"));
997
998         gq_gtk_box_pack_start(GTK_BOX(status), GTK_WIDGET(slider), FALSE, FALSE, 0);
999         gq_gtk_box_pack_start(GTK_BOX(status), GTK_WIDGET(state), FALSE, FALSE, 5);
1000         gq_gtk_box_pack_end(GTK_BOX(status), GTK_WIDGET(progress), FALSE, FALSE, 0);
1001         gq_gtk_box_pack_end(GTK_BOX(vbox),GTK_WIDGET(status), FALSE, FALSE, 0);
1002
1003         layer = champlain_marker_layer_new();
1004         champlain_view_add_layer(view, CHAMPLAIN_LAYER(layer));
1005
1006         pgd->icon_layer = layer;
1007         pgd->gps_view = CLUTTER_ACTOR(view);
1008         pgd->widget = frame;
1009         pgd->progress = progress;
1010         pgd->slider = slider;
1011         pgd->state = state;
1012
1013         bar_pane_gps_set_map_source(pgd, map_id);
1014
1015         g_object_set(G_OBJECT(view), "kinetic-mode", TRUE,
1016                                      "zoom-level", zoom,
1017                                      "keep-center-on-resize", TRUE,
1018                                      "deceleration", 1.1,
1019                                      "zoom-on-double-click", FALSE,
1020                                      "max-zoom-level", 17,
1021                                      "min-zoom-level", 1,
1022                                      NULL);
1023         champlain_view_center_on(view, latitude, longitude);
1024         pgd->centre_map_checked = TRUE;
1025         g_object_set_data(G_OBJECT(pgd->widget), "pane_data", pgd);
1026         g_signal_connect(G_OBJECT(pgd->widget), "destroy", G_CALLBACK(bar_pane_gps_destroy), pgd);
1027
1028         gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1029
1030         gtk_widget_set_size_request(pgd->widget, -1, height);
1031
1032         g_signal_connect(G_OBJECT(gpswidget), "button_press_event", G_CALLBACK(bar_pane_gps_map_keypress_cb), pgd);
1033         g_signal_connect(pgd->gps_view, "notify::state", G_CALLBACK(bar_pane_gps_view_state_changed_cb), pgd);
1034         g_signal_connect(pgd->gps_view, "notify::zoom-level", G_CALLBACK(bar_pane_gps_view_state_changed_cb), pgd);
1035         g_signal_connect(G_OBJECT(slider), "value-changed", G_CALLBACK(bar_pane_gps_slider_changed_cb), pgd);
1036
1037         bar_pane_gps_dnd_init(pgd);
1038
1039         file_data_register_notify_func(bar_pane_gps_notify_cb, pgd, NOTIFY_PRIORITY_LOW);
1040
1041         pgd->create_markers_id = 0;
1042         pgd->enable_markers_checked = TRUE;
1043         pgd->centre_map_checked = TRUE;
1044
1045         return pgd->widget;
1046 }
1047
1048 GtkWidget *bar_pane_gps_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
1049 {
1050         gchar *title = g_strdup(_("GPS Map"));
1051         gchar *map_id = nullptr;
1052         gboolean expanded = TRUE;
1053         gint height = 350;
1054         gint zoom = 7;
1055         gdouble latitude;
1056         gdouble longitude;
1057         /* Latitude and longitude are stored in the config file as an integer of
1058          * (actual value * 1,000,000). There is no READ_DOUBLE utility function.
1059          */
1060         gint int_latitude = 54000000;
1061         gint int_longitude = -4000000;
1062         gchar *id = g_strdup("gps");
1063         GtkWidget *ret;
1064
1065         while (*attribute_names)
1066                 {
1067                 const gchar *option = *attribute_names++;
1068                 const gchar *value = *attribute_values++;
1069
1070                 if (READ_CHAR_FULL("title", title))
1071                         continue;
1072                 if (READ_CHAR_FULL("map-id", map_id))
1073                         continue;
1074                 if (READ_INT_CLAMP_FULL("zoom-level", zoom, 1, 20))
1075                         continue;
1076                 if (READ_INT_CLAMP_FULL("latitude", int_latitude, -90000000, +90000000))
1077                         continue;
1078                 if (READ_INT_CLAMP_FULL("longitude", int_longitude, -90000000, +90000000))
1079                         continue;
1080                 if (READ_BOOL_FULL("expanded", expanded))
1081                         continue;
1082                 if (READ_INT_FULL("height", height))
1083                         continue;
1084                 if (READ_CHAR_FULL("id", id))
1085                         continue;
1086
1087                 log_printf("unknown attribute %s = %s\n", option, value);
1088                 }
1089
1090         bar_pane_translate_title(PANE_COMMENT, id, &title);
1091         latitude = static_cast<gdouble>(int_latitude) / 1000000;
1092         longitude = static_cast<gdouble>(int_longitude) / 1000000;
1093         ret = bar_pane_gps_new(id, title, map_id, zoom, latitude, longitude, expanded, height);
1094         g_free(title);
1095         g_free(map_id);
1096         g_free(id);
1097         return ret;
1098 }
1099
1100 void bar_pane_gps_update_from_config(GtkWidget *pane, const gchar **attribute_names,
1101                                                                                 const gchar **attribute_values)
1102 {
1103         PaneGPSData *pgd;
1104         gint zoom;
1105         gint int_longitude;
1106         gint int_latitude;
1107         gdouble longitude;
1108         gdouble latitude;
1109
1110         pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
1111         if (!pgd)
1112                 return;
1113
1114         gchar *title = nullptr;
1115
1116         while (*attribute_names)
1117         {
1118                 const gchar *option = *attribute_names++;
1119                 const gchar *value = *attribute_values++;
1120
1121                 if (READ_CHAR_FULL("title", title))
1122                         continue;
1123                 if (READ_CHAR_FULL("map-id", pgd->map_source))
1124                         continue;
1125                 if (READ_BOOL_FULL("expanded", pgd->pane.expanded))
1126                         continue;
1127                 if (READ_INT_FULL("height", pgd->height))
1128                         continue;
1129                 if (READ_CHAR_FULL("id", pgd->pane.id))
1130                         continue;
1131                 if (READ_INT_CLAMP_FULL("zoom-level", zoom, 1, 8))
1132                         {
1133                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "zoom-level", zoom, NULL);
1134                         continue;
1135                         }
1136                 if (READ_INT_CLAMP_FULL("longitude", int_longitude, -90000000, +90000000))
1137                         {
1138                         longitude = int_longitude / 1000000.0;
1139                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "longitude", longitude, NULL);
1140                         continue;
1141                         }
1142                 if (READ_INT_CLAMP_FULL("latitude", int_latitude, -90000000, +90000000))
1143                         {
1144                         latitude = int_latitude / 1000000.0;
1145                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "latitude", latitude, NULL);
1146                         continue;
1147                         }
1148                 log_printf("unknown attribute %s = %s\n", option, value);
1149         }
1150
1151         if (title)
1152                 {
1153                 bar_pane_translate_title(PANE_COMMENT, pgd->pane.id, &title);
1154                 gtk_label_set_text(GTK_LABEL(pgd->pane.title), title);
1155                 g_free(title);
1156                 }
1157
1158         gtk_widget_set_size_request(pgd->widget, -1, pgd->height);
1159         bar_update_expander(pane);
1160 }
1161
1162 #endif // HAVE_LIBCHAMPLAIN_GTK
1163 #endif // HAVE_LIBCHAMPLAIN
1164
1165 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */