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