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