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