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