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