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