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