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