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