d21b824cd4da66f5947f9f74402f8d13b8ae7341
[geeqie.git] / src / bar_gps.c
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 "rcfile.h"
34 #include "thumb.h"
35 #include "ui_menu.h"
36
37 #include <clutter-gtk/clutter-gtk.h>
38 #include <champlain/champlain.h>
39 #include <champlain-gtk/champlain-gtk.h>
40
41 #define MARKER_COLOUR 0x00, 0x00, 0xff, 0xff
42 #define TEXT_COLOUR 0x00, 0x00, 0x00, 0xff
43 #define THUMB_COLOUR 0xff, 0xff, 0xff, 0xff
44 #define THUMB_SIZE 100
45
46 #define DIRECTION_SIZE 300
47
48 /*
49  *-------------------------------------------------------------------
50  * GPS Map utils
51  *-------------------------------------------------------------------
52  */
53
54 typedef struct _PaneGPSData PaneGPSData;
55 struct _PaneGPSData
56 {
57         PaneData pane;
58         GtkWidget *widget;
59         gchar *map_source;
60         gint height;
61         FileData *fd;
62         ClutterActor *gps_view;
63         ChamplainMarkerLayer *icon_layer;
64         GList *selection_list;
65         GList *not_added;
66         ChamplainBoundingBox *bbox;
67         guint num_added;
68         guint create_markers_id;
69         GtkWidget *progress;
70         GtkWidget *slider;
71         GtkWidget *state;
72         gint selection_count;
73         gboolean centre_map_checked;
74         gboolean enable_markers_checked;
75 };
76
77 static gboolean bar_gps_draw_direction (ClutterCanvas *canvas,
78                                 cairo_t *cr, gpointer data)
79 {
80         cairo_set_source_rgb(cr, 255, 0, 0);
81
82         cairo_set_line_width(cr, 2);
83         cairo_move_to(cr, 0, 1);
84         cairo_line_to(cr, DIRECTION_SIZE, 1);
85
86         cairo_stroke(cr);
87
88         return TRUE;
89 }
90
91 static void bar_pane_gps_thumb_done_cb(ThumbLoader *tl, gpointer data)
92 {
93         FileData *fd;
94         ClutterActor *marker;
95         ClutterActor *actor;
96
97         marker = CLUTTER_ACTOR(data);
98         fd = g_object_get_data(G_OBJECT(marker), "file_fd");
99         if (fd->thumb_pixbuf != NULL)
100                 {
101                 actor = gtk_clutter_texture_new();
102                 gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor), fd->thumb_pixbuf, NULL);
103                 champlain_label_set_image(CHAMPLAIN_LABEL(marker), actor);
104                 }
105         thumb_loader_free(tl);
106 }
107
108 static void bar_pane_gps_thumb_error_cb(ThumbLoader *tl, gpointer data)
109 {
110         thumb_loader_free(tl);
111 }
112
113 static gboolean bar_pane_gps_marker_keypress_cb(GtkWidget *widget, ClutterButtonEvent *bevent, gpointer data)
114 {
115         //PaneGPSData *pgd = data;
116         FileData *fd;
117         ClutterActor *label_marker, *parent_marker;
118         ClutterColor marker_colour = { MARKER_COLOUR };
119         ClutterColor text_colour = { TEXT_COLOUR };
120         ClutterColor thumb_colour = { THUMB_COLOUR };
121         gchar *current_text;
122         ClutterActor *actor, *direction;
123         ClutterActor *current_image;
124         GString *text;
125         gint height, width, rotate;
126         gchar *altitude = NULL;
127         ThumbLoader *tl;
128
129         if (bevent->button == MOUSE_BUTTON_LEFT)
130                 {
131                 label_marker = CLUTTER_ACTOR(widget);
132                 fd = g_object_get_data(G_OBJECT(label_marker), "file_fd");
133
134                 /* If the marker is showing a thumbnail, delete it
135                  */
136                 current_image = champlain_label_get_image(CHAMPLAIN_LABEL(label_marker));
137                 if (current_image != NULL)
138                         {
139                         clutter_actor_destroy(CLUTTER_ACTOR(current_image));
140                         champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), NULL);
141                         }
142
143                 current_text = g_strdup(champlain_label_get_text(CHAMPLAIN_LABEL(label_marker)));
144
145                 /* If the marker is showing only the text character, replace it with a
146                  * thumbnail and date and altitude
147                  */
148                 if (g_strcmp0(current_text, "i") == 0)
149                         {
150                         /* If a thumbail has already been generated, use that. If not try the pixbuf of the full image.
151                          * If not, call the thumb_loader to generate a thumbnail and update the marker later in the
152                          * thumb_loader callback
153                          */
154                          if (fd->thumb_pixbuf != NULL)
155                                 {
156                                 actor = gtk_clutter_texture_new();
157                                 gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor), fd->thumb_pixbuf, NULL);
158                                 champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), actor);
159                                 }
160                         else if (fd->pixbuf != NULL)
161                                 {
162                                 actor = gtk_clutter_texture_new();
163                                 width = gdk_pixbuf_get_width (fd->pixbuf);
164                                 height = gdk_pixbuf_get_height (fd->pixbuf);
165                                 switch (fd->exif_orientation)
166                                         {
167                                         case 8:
168                                                 rotate = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
169                                                 break;
170                                         case 3:
171                                                 rotate = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
172                                                 break;
173                                         case 6:
174                                                 rotate = GDK_PIXBUF_ROTATE_CLOCKWISE;
175                                                 break;
176                                         default:
177                                                 rotate = GDK_PIXBUF_ROTATE_NONE;
178                                         }
179
180                                         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(actor),
181                                                                                 gdk_pixbuf_rotate_simple(gdk_pixbuf_scale_simple(fd->pixbuf, THUMB_SIZE, height * THUMB_SIZE / width,
182                                                                                 GDK_INTERP_NEAREST), rotate), NULL);
183                                         champlain_label_set_image(CHAMPLAIN_LABEL(label_marker), actor);
184                                 }
185                         else
186                                 {
187                                 tl = thumb_loader_new(THUMB_SIZE, THUMB_SIZE);
188                                 thumb_loader_set_callbacks(tl,
189                                                                                         bar_pane_gps_thumb_done_cb,
190                                                                                         bar_pane_gps_thumb_error_cb,
191                                                                                         NULL,
192                                                                                         label_marker);
193                                 thumb_loader_start(tl, fd);
194                                 }
195
196                         text = g_string_new(fd->name);
197                         g_string_append(text, "\n");
198                         g_string_append(text, text_from_time(fd->date));
199                         g_string_append(text, "\n");
200                         altitude = metadata_read_string(fd, "formatted.GPSAltitude", METADATA_FORMATTED);
201                         if (altitude != NULL)
202                                 {
203                                 g_string_append(text, altitude);
204                                 }
205
206                         champlain_label_set_text(CHAMPLAIN_LABEL(label_marker), text->str);
207                         champlain_label_set_font_name(CHAMPLAIN_LABEL(label_marker), "sans 8");
208                         champlain_marker_set_selection_color(&thumb_colour);
209                         champlain_marker_set_selection_text_color(&text_colour);
210
211                         g_free(altitude);
212                         g_string_free(text, TRUE);
213
214                         parent_marker = clutter_actor_get_parent(label_marker);
215                         direction = clutter_actor_get_child_at_index(parent_marker, 0);
216                         clutter_actor_set_opacity(direction, 255);
217                         }
218                 /* otherwise, revert to the hidden text marker
219                  */
220                 else
221                         {
222                         champlain_label_set_text(CHAMPLAIN_LABEL(label_marker), "i");
223                         champlain_label_set_font_name(CHAMPLAIN_LABEL(label_marker), "courier 5");
224                         champlain_marker_set_selection_color(&marker_colour);
225                         champlain_marker_set_selection_text_color(&marker_colour);
226
227                         parent_marker = clutter_actor_get_parent(label_marker);
228                         direction = clutter_actor_get_child_at_index(parent_marker, 0);
229                         clutter_actor_set_opacity(direction, 0);
230                         }
231
232                 g_free(current_text);
233
234                 return TRUE;
235                 }
236         return TRUE;
237 }
238
239 static gboolean bar_pane_gps_create_markers_cb(gpointer data)
240 {
241         PaneGPSData *pgd = data;
242         gdouble latitude;
243         gdouble longitude;
244         gdouble compass;
245         FileData *fd;
246         ClutterActor *parent_marker, *label_marker;
247         ClutterActor *direction;
248         ClutterColor marker_colour = { MARKER_COLOUR };
249         ClutterColor thumb_colour = { THUMB_COLOUR };
250         GString *message;
251         ClutterContent *canvas;
252
253         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgd->progress),
254                                                         (gdouble)(pgd->selection_count - g_list_length(pgd->not_added)) /
255                                                         (gdouble)pgd->selection_count);
256
257         message = g_string_new("");
258         g_string_printf(message, "%i/%i", (pgd->selection_count - g_list_length(pgd->not_added)),
259                                                                                                                                                         pgd->selection_count);
260         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), message->str);
261         g_string_free(message, TRUE);
262
263         if(pgd->not_added)
264                 {
265                 fd = pgd->not_added->data;
266                 pgd->not_added = pgd->not_added->next;
267
268                 latitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLatitude", 0);
269                 longitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLongitude", 0);
270                 compass = metadata_read_GPS_direction(fd, "Xmp.exif.GPSImgDirection", 1000);
271
272                 if (!(latitude == 0 && longitude == 0))
273                         {
274                         pgd->num_added++;
275
276                         parent_marker = champlain_marker_new();
277                         clutter_actor_set_reactive(parent_marker, FALSE);
278                         label_marker = champlain_label_new_with_text("i","courier 5", &marker_colour, &marker_colour);
279                         clutter_actor_set_reactive(label_marker, TRUE);
280                         champlain_marker_set_selection_color(&thumb_colour);
281
282                         if (compass != 1000)
283                                 {
284                                 canvas = clutter_canvas_new();
285                                 clutter_canvas_set_size(CLUTTER_CANVAS (canvas), DIRECTION_SIZE, 3);
286                                 g_signal_connect(canvas, "draw", G_CALLBACK(bar_gps_draw_direction), NULL);
287                                 direction = clutter_actor_new();
288                                 clutter_actor_set_size(direction, DIRECTION_SIZE, 3);
289                                 clutter_actor_set_position(direction, 0, 0);
290                                 clutter_actor_set_rotation_angle(direction, CLUTTER_Z_AXIS, compass -90.00);
291                                 clutter_actor_set_content(direction, canvas);
292                                 clutter_content_invalidate(canvas);
293                                 g_object_unref(canvas);
294
295                                 clutter_actor_add_child(parent_marker, direction);
296                                 clutter_actor_set_opacity(direction, 0);
297                                 }
298
299                         clutter_actor_add_child(parent_marker, label_marker);
300
301                         champlain_location_set_location(CHAMPLAIN_LOCATION(parent_marker), latitude, longitude);
302                         champlain_marker_layer_add_marker(pgd->icon_layer, CHAMPLAIN_MARKER(parent_marker));
303
304                         g_signal_connect(G_OBJECT(label_marker), "button_release_event",
305                                         G_CALLBACK(bar_pane_gps_marker_keypress_cb), pgd);
306
307                         g_object_set_data(G_OBJECT(label_marker), "file_fd", fd);
308
309                         champlain_bounding_box_extend(pgd->bbox, latitude, longitude);
310
311                         }
312                 return TRUE;
313                 }
314
315         if (pgd->centre_map_checked)
316                 {
317                 if (pgd->num_added == 1)
318                         {
319                         champlain_bounding_box_get_center(pgd->bbox, &latitude, &longitude);
320                         champlain_view_go_to(CHAMPLAIN_VIEW(pgd->gps_view), latitude, longitude);
321                         }
322                  else if (pgd->num_added > 1)
323                         {
324                         champlain_view_ensure_visible(CHAMPLAIN_VIEW(pgd->gps_view), pgd->bbox, TRUE);
325                         }
326                 }
327         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgd->progress), 0);
328         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), NULL);
329         pgd->create_markers_id = 0;
330
331         return FALSE;
332 }
333
334 static void bar_pane_gps_update(PaneGPSData *pgd)
335 {
336         GList *list;
337
338         /* The widget does not have a parent during bar_pane_gps_new, so calling gtk_widget_show_all there gives a
339          * "Gtk-CRITICAL **: gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed"
340          * error. gtk_widget_show_all can be given after it has been added to the bar.
341          */
342         if (gtk_widget_get_parent(pgd->widget) != NULL)
343                 gtk_widget_show_all(pgd->widget);
344
345         /* If a create-marker background process is running, kill it
346          * and start again
347          */
348         if (pgd->create_markers_id != 0)
349                 {
350                 if (g_idle_remove_by_data(pgd))
351                         {
352                         pgd->create_markers_id = 0;
353                         }
354                 else
355                         {
356                         return;
357                         }
358                 }
359
360         /* Delete any markers currently displayed
361          */
362
363         champlain_marker_layer_remove_all(pgd->icon_layer);
364
365         if (!pgd->enable_markers_checked)
366                 {
367                 return;
368                 }
369
370         /* For each selected photo that has GPS data, create a marker containing
371          * a single, small text character the same colour as the marker background.
372          * Use a background process in case the user selects a large number of files.
373          */
374         filelist_free(pgd->selection_list);
375         if (pgd->bbox) champlain_bounding_box_free(pgd->bbox);
376
377         list = layout_selection_list(pgd->pane.lw);
378         list = file_data_process_groups_in_selection(list, FALSE, NULL);
379
380         pgd->selection_list = list;
381         pgd->not_added = list;
382
383         pgd->bbox = champlain_bounding_box_new();
384         pgd->selection_count = g_list_length(pgd->selection_list);
385         pgd->create_markers_id = g_idle_add(bar_pane_gps_create_markers_cb, pgd);
386         pgd->num_added = 0;
387 }
388
389 void bar_pane_gps_set_map_source(PaneGPSData *pgd, const gchar *map_id)
390 {
391         ChamplainMapSource *map_source;
392         ChamplainMapSourceFactory *map_factory;
393
394         map_factory = champlain_map_source_factory_dup_default();
395         map_source = champlain_map_source_factory_create(map_factory, map_id);
396
397         if (map_source != NULL)
398                 {
399                 g_object_set(G_OBJECT(pgd->gps_view), "map-source", map_source, NULL);
400                 }
401
402         g_object_unref(map_factory);
403 }
404
405 void bar_pane_gps_enable_markers_checked_toggle_cb(GtkWidget *menu_widget, gpointer data)
406 {
407         PaneGPSData *pgd = data;
408
409         if (pgd->enable_markers_checked)
410                 {
411                 pgd->enable_markers_checked = FALSE;
412                 }
413         else
414                 {
415                 pgd->enable_markers_checked = TRUE;
416                 }
417 }
418
419 static void bar_pane_gps_centre_map_checked_toggle_cb(GtkWidget *menu_widget, gpointer data)
420 {
421         PaneGPSData *pgd = data;
422
423         if (pgd->centre_map_checked)
424                 {
425                 pgd->centre_map_checked = FALSE;
426                 }
427         else
428                 {
429                 pgd->centre_map_checked = TRUE;
430                 }
431 }
432
433 static void bar_pane_gps_change_map_cb(GtkWidget *widget, gpointer data)
434 {
435         PaneGPSData *pgd = data;
436         gchar *mapsource;
437
438         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
439                 return;
440
441         if (!pgd) return;
442
443         mapsource = g_object_get_data(G_OBJECT(widget), "menu_item_radio_data");
444         bar_pane_gps_set_map_source(pgd, mapsource);
445 }
446
447 static void bar_pane_gps_notify_selection(GtkWidget *bar, gint count)
448 {
449         PaneGPSData *pgd;
450
451         if (count == 0) return;
452
453         pgd = g_object_get_data(G_OBJECT(bar), "pane_data");
454         if (!pgd) return;
455
456         bar_pane_gps_update(pgd);
457 }
458
459 static void bar_pane_gps_set_fd(GtkWidget *bar, FileData *fd)
460 {
461         PaneGPSData *pgd;
462
463         pgd = g_object_get_data(G_OBJECT(bar), "pane_data");
464         if (!pgd) return;
465
466         file_data_unref(pgd->fd);
467         pgd->fd = file_data_ref(fd);
468
469         bar_pane_gps_update(pgd);
470 }
471
472 static gint bar_pane_gps_event(GtkWidget *bar, GdkEvent *event)
473 {
474         PaneGPSData *pgd;
475
476         pgd = g_object_get_data(G_OBJECT(bar), "pane_data");
477         if (!pgd) return FALSE;
478
479         if (gtk_widget_has_focus(pgd->widget)) return gtk_widget_event(GTK_WIDGET(pgd->widget), event);
480
481         return FALSE;
482 }
483
484 static void bar_pane_gps_write_config(GtkWidget *pane, GString *outstr, gint indent)
485 {
486         PaneGPSData *pgd;
487         gint zoom;
488         ChamplainMapSource *mapsource;
489         const gchar *map_id;
490         gchar *str = NULL;
491         GString *buffer = g_string_new(str);
492         gdouble position;
493         gint int_position;
494
495         pgd = g_object_get_data(G_OBJECT(pane), "pane_data");
496         if (!pgd) return;
497
498         WRITE_NL();
499         WRITE_STRING("<pane_gps ");
500         write_char_option(outstr, indent, "id", pgd->pane.id);
501         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pgd->pane.title)));
502         WRITE_BOOL(pgd->pane, expanded);
503         WRITE_INT(*pgd, height);
504         indent++;
505
506         g_object_get(G_OBJECT(pgd->gps_view), "map-source", &mapsource, NULL);
507         map_id = champlain_map_source_get_id(mapsource);
508         WRITE_NL();
509         write_char_option(outstr, indent, "map-id", map_id);
510
511         g_object_get(G_OBJECT(pgd->gps_view), "zoom-level", &zoom, NULL);
512         g_string_printf(buffer, "%d", zoom);
513         WRITE_NL();
514         write_char_option(outstr, indent, "zoom-level", buffer->str);
515
516         g_object_get(G_OBJECT(pgd->gps_view), "latitude", &position, NULL);
517         int_position = position * 1000000;
518         g_string_printf(buffer, "%i", int_position);
519         WRITE_NL();
520         write_char_option(outstr, indent, "latitude", buffer->str);
521
522         g_object_get(G_OBJECT(pgd->gps_view), "longitude", &position, NULL);
523         int_position = position * 1000000;
524         g_string_printf(buffer, "%i", int_position);
525         WRITE_NL();
526         write_char_option(outstr, indent, "longitude", buffer->str);
527
528         indent--;
529         WRITE_NL();
530         WRITE_STRING("/>");
531
532   g_object_unref(mapsource);
533
534 }
535
536 static void bar_pane_gps_slider_changed_cb(GtkScaleButton *slider,
537                                            gdouble zoom,
538                                            gpointer data)
539 {
540         PaneGPSData *pgd = data;
541         GString *message;
542
543         message = g_string_new("");
544         g_string_printf(message, _("Zoom %i"), (gint)zoom);
545
546         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "zoom-level", (gint)zoom, NULL);
547         gtk_widget_set_tooltip_text(GTK_WIDGET(slider), message->str);
548         g_string_free(message, TRUE);
549
550 }
551 static void bar_pane_gps_view_state_changed_cb(ChamplainView *view,
552                                                GParamSpec *gobject,
553                                                gpointer data)
554 {
555         PaneGPSData *pgd = data;
556         ChamplainState status;
557         gint zoom;
558         GString *message;
559
560         g_object_get(G_OBJECT(view), "zoom-level", &zoom, NULL);
561         message = g_string_new("");
562         g_string_printf(message, _("Zoom level %i"), zoom);
563
564         g_object_get(G_OBJECT(view), "state", &status, NULL);
565         if (status == CHAMPLAIN_STATE_LOADING)
566                 {
567                 gtk_label_set_text(GTK_LABEL(pgd->state), _("Loading map"));
568                 }
569         else
570                 {
571                 gtk_label_set_text(GTK_LABEL(pgd->state), message->str);
572                 }
573
574         gtk_widget_set_tooltip_text(GTK_WIDGET(pgd->slider), message->str);
575         gtk_scale_button_set_value(GTK_SCALE_BUTTON(pgd->slider), (gdouble)zoom);
576
577         g_string_free(message, TRUE);
578 }
579
580 static void bar_pane_gps_notify_cb(FileData *fd, NotifyType type, gpointer data)
581 {
582         PaneGPSData *pgd = data;
583
584         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) &&
585             g_list_find(pgd->selection_list, fd))
586                 {
587                 bar_pane_gps_update(pgd);
588                 }
589 }
590
591 const gchar *bar_pane_gps_get_map_id(PaneGPSData *pgd)
592 {
593         const gchar *map_id;
594         ChamplainMapSource *mapsource;
595
596         g_object_get(G_OBJECT(pgd->gps_view), "map-source", &mapsource, NULL);
597         map_id = champlain_map_source_get_id(mapsource);
598
599         g_object_unref(mapsource);
600
601         return map_id;
602 }
603
604 static GtkWidget *bar_pane_gps_menu(PaneGPSData *pgd)
605 {
606         GtkWidget *menu;
607         GtkWidget *map_centre;
608         ChamplainMapSourceFactory *map_factory;
609         GSList *map_list;
610         ChamplainMapSourceDesc *map_desc;
611         const gchar *current;
612
613         menu = popup_menu_short_lived();
614
615         map_factory = champlain_map_source_factory_dup_default();
616         map_list = champlain_map_source_factory_get_registered(map_factory);
617         current = bar_pane_gps_get_map_id(pgd);
618
619         while (map_list)
620                 {
621                 map_desc = (ChamplainMapSourceDesc *)(map_list->data);
622
623                 menu_item_add_radio(menu,
624                                     champlain_map_source_desc_get_name(map_desc),
625                                     (gpointer)champlain_map_source_desc_get_id(map_desc),
626                                     strcmp(champlain_map_source_desc_get_id(map_desc), current) == 0,
627                                     G_CALLBACK(bar_pane_gps_change_map_cb), pgd);
628
629                 map_list = g_slist_next(map_list);
630                 }
631
632         menu_item_add_divider(menu);
633         menu_item_add_check(menu, _("Enable markers"), pgd->enable_markers_checked,
634                             G_CALLBACK(bar_pane_gps_enable_markers_checked_toggle_cb), pgd);
635         map_centre = menu_item_add_check(menu, _("Centre map on marker"), pgd->centre_map_checked,
636                                          G_CALLBACK(bar_pane_gps_centre_map_checked_toggle_cb), pgd);
637         if (!pgd->enable_markers_checked)
638                 {
639                 gtk_widget_set_sensitive(map_centre, FALSE);
640                 }
641
642         g_slist_free(map_list);
643         g_object_unref(map_factory);
644
645         return menu;
646 }
647
648 /* Determine if the map is to be re-centred on the marker when another photo is selected
649  */
650 void bar_pane_gps_map_centreing(PaneGPSData *pgd)
651 {
652         GtkWidget *dialog;
653         GString *message = g_string_new("");
654
655         if (pgd->centre_map_checked)
656                 {
657                 message = g_string_append(message, _("Move map centre to marker\n is disabled"));
658                 pgd->centre_map_checked = FALSE;
659                 }
660         else
661                 {
662                 message = g_string_append(message, _("Move map centre to marker\n is enabled"));
663                 pgd->centre_map_checked = TRUE;
664                 }
665
666         dialog = gtk_message_dialog_new(NULL,
667                                                           GTK_DIALOG_DESTROY_WITH_PARENT,
668                                                           GTK_MESSAGE_INFO,
669                                                           GTK_BUTTONS_CLOSE,
670                                                           "%s", message->str);
671         gtk_window_set_title(GTK_WINDOW(dialog), _("Map Centreing"));
672         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
673         gtk_dialog_run(GTK_DIALOG(dialog));
674
675         gtk_widget_destroy(dialog);
676         g_string_free(message, TRUE);
677 }
678
679 static gboolean bar_pane_gps_map_keypress_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
680 {
681         PaneGPSData *pgd = data;
682         GtkWidget *menu;
683         GtkClipboard *clipboard;
684         gchar *geo_coords;
685
686         if (bevent->button == MOUSE_BUTTON_RIGHT)
687                 {
688                 menu = bar_pane_gps_menu(pgd);
689                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
690                 return TRUE;
691                 }
692         else if (bevent->button == MOUSE_BUTTON_MIDDLE)
693                 {
694                 bar_pane_gps_map_centreing(pgd);
695                 return TRUE;
696                 }
697         else if (bevent->button == MOUSE_BUTTON_LEFT)
698                 {
699                 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
700                 geo_coords = g_strdup_printf("%lf %lf",
701                                                         champlain_view_y_to_latitude(
702                                                                 CHAMPLAIN_VIEW(pgd->gps_view),bevent->y),
703                                                         champlain_view_x_to_longitude(
704                                                                 CHAMPLAIN_VIEW(pgd->gps_view),bevent->x));
705                 gtk_clipboard_set_text(clipboard, geo_coords, -1);
706
707                 g_free(geo_coords);
708
709                 return TRUE;
710                 }
711         else
712                 {
713                 return FALSE;
714                 }
715 }
716
717 static void bar_pane_gps_destroy(GtkWidget *widget, gpointer data)
718 {
719         PaneGPSData *pgd = data;
720
721         file_data_unregister_notify_func(bar_pane_gps_notify_cb, pgd);
722
723         g_idle_remove_by_data(pgd);
724
725         filelist_free(pgd->selection_list);
726         if (pgd->bbox) champlain_bounding_box_free(pgd->bbox);
727
728         file_data_unref(pgd->fd);
729         g_free(pgd->map_source);
730         g_free(pgd->pane.id);
731         clutter_actor_destroy(pgd->gps_view);
732         g_free(pgd);
733 }
734
735
736 GtkWidget *bar_pane_gps_new(const gchar *id, const gchar *title, const gchar *map_id,
737                                                 const gint zoom, const gdouble latitude, const gdouble longitude,
738                                         gboolean expanded, gint height)
739 {
740         PaneGPSData *pgd;
741         GtkWidget *vbox, *frame;
742         GtkWidget *gpswidget;
743         GtkWidget *status, *state, *progress, *slider;
744         ChamplainMarkerLayer *layer;
745         ChamplainView *view;
746         const gchar *slider_list[] = {"zoom-in", "zoom-out", NULL};
747         const gchar **slider_icons = slider_list;
748
749         pgd = g_new0(PaneGPSData, 1);
750
751         pgd->pane.pane_set_fd = bar_pane_gps_set_fd;
752         pgd->pane.pane_notify_selection = bar_pane_gps_notify_selection;
753         pgd->pane.pane_event = bar_pane_gps_event;
754         pgd->pane.pane_write_config = bar_pane_gps_write_config;
755         pgd->pane.title = bar_pane_expander_title(title);
756         pgd->pane.id = g_strdup(id);
757         pgd->pane.type = PANE_GPS;
758         pgd->pane.expanded = expanded;
759         pgd->height = height;
760
761         frame = gtk_frame_new(NULL);
762         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
763
764         gpswidget = gtk_champlain_embed_new();
765         view = gtk_champlain_embed_get_view(GTK_CHAMPLAIN_EMBED(gpswidget));
766
767         gtk_box_pack_start(GTK_BOX(vbox), gpswidget, TRUE, TRUE, 0);
768         gtk_container_add(GTK_CONTAINER(frame), vbox);
769
770         status = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
771         slider = gtk_scale_button_new(GTK_ICON_SIZE_SMALL_TOOLBAR, 1, 17, 1, slider_icons);
772         gtk_widget_set_tooltip_text(slider, "Zoom");
773         gtk_scale_button_set_value(GTK_SCALE_BUTTON(slider), (gdouble)zoom);
774
775         progress = gtk_progress_bar_new();
776 #if GTK_CHECK_VERSION(3,0,0)
777         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "");
778         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(progress), TRUE);
779 #endif
780         state = gtk_label_new("");
781         gtk_label_set_justify(GTK_LABEL(state), GTK_JUSTIFY_CENTER);
782
783         gtk_box_pack_start(GTK_BOX(status), GTK_WIDGET(slider), FALSE, FALSE, 0);
784         gtk_box_pack_start(GTK_BOX(status), GTK_WIDGET(state), FALSE, FALSE, 5);
785         gtk_box_pack_end(GTK_BOX(status), GTK_WIDGET(progress), FALSE, FALSE, 0);
786         gtk_box_pack_end(GTK_BOX(vbox),GTK_WIDGET(status), FALSE, FALSE, 0);
787
788         layer = champlain_marker_layer_new();
789         champlain_view_add_layer(view, CHAMPLAIN_LAYER(layer));
790
791         pgd->icon_layer = layer;
792         pgd->gps_view = CLUTTER_ACTOR(view);
793         pgd->widget = frame;
794         pgd->progress = progress;
795         pgd->slider = slider;
796         pgd->state = state;
797
798         bar_pane_gps_set_map_source(pgd, map_id);
799
800         g_object_set(G_OBJECT(view), "kinetic-mode", TRUE,
801                                      "zoom-level", zoom,
802                                      "keep-center-on-resize", TRUE,
803                                      "deceleration", 1.1,
804                                      "zoom-on-double-click", FALSE,
805                                      "max-zoom-level", 17,
806                                      "min-zoom-level", 1,
807                                      NULL);
808         champlain_view_center_on(view, latitude, longitude);
809         pgd->centre_map_checked = TRUE;
810         g_object_set_data(G_OBJECT(pgd->widget), "pane_data", pgd);
811         g_signal_connect(G_OBJECT(pgd->widget), "destroy", G_CALLBACK(bar_pane_gps_destroy), pgd);
812
813         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
814
815         gtk_widget_set_size_request(pgd->widget, -1, height);
816
817         g_signal_connect(G_OBJECT(gpswidget), "button_press_event", G_CALLBACK(bar_pane_gps_map_keypress_cb), pgd);
818         g_signal_connect(pgd->gps_view, "notify::state", G_CALLBACK(bar_pane_gps_view_state_changed_cb), pgd);
819         g_signal_connect(pgd->gps_view, "notify::zoom-level", G_CALLBACK(bar_pane_gps_view_state_changed_cb), pgd);
820         g_signal_connect(G_OBJECT(slider), "value-changed", G_CALLBACK(bar_pane_gps_slider_changed_cb), pgd);
821
822         file_data_register_notify_func(bar_pane_gps_notify_cb, pgd, NOTIFY_PRIORITY_LOW);
823
824         pgd->create_markers_id = 0;
825         pgd->enable_markers_checked = TRUE;
826         pgd->centre_map_checked = TRUE;
827
828         return pgd->widget;
829 }
830
831 GtkWidget *bar_pane_gps_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
832 {
833         gchar *title = g_strdup(_("GPS Map"));
834         gchar *map_id = NULL;
835         gboolean expanded = TRUE;
836         gint height = 350;
837         gint zoom = 7;
838         gdouble latitude;
839         gdouble longitude;
840         /* Latitude and longitude are stored in the config file as an integer of
841          * (actual value * 1,000,000). There is no READ_DOUBLE utilty function.
842          */
843         gint int_latitude = 54000000;
844         gint int_longitude = -4000000;
845         gchar *id = g_strdup("gps");
846         GtkWidget *ret;
847
848         while (*attribute_names)
849                 {
850                 const gchar *option = *attribute_names++;
851                 const gchar *value = *attribute_values++;
852
853                 if (READ_CHAR_FULL("title", title))
854                         continue;
855                 if (READ_CHAR_FULL("map-id", map_id))
856                         continue;
857                 if (READ_INT_CLAMP_FULL("zoom-level", zoom, 1, 20))
858                         continue;
859                 if (READ_INT_CLAMP_FULL("latitude", int_latitude, -90000000, +90000000))
860                         continue;
861                 if (READ_INT_CLAMP_FULL("longitude", int_longitude, -90000000, +90000000))
862                         continue;
863                 if (READ_BOOL_FULL("expanded", expanded))
864                         continue;
865                 if (READ_INT_FULL("height", height))
866                         continue;
867                 if (READ_CHAR_FULL("id", id))
868                         continue;
869
870                 log_printf("unknown attribute %s = %s\n", option, value);
871                 }
872
873         bar_pane_translate_title(PANE_COMMENT, id, &title);
874         latitude = int_latitude / 1000000;
875         longitude = int_longitude / 1000000;
876         ret = bar_pane_gps_new(id, title, map_id, zoom, latitude, longitude, expanded, height);
877         g_free(title);
878         g_free(map_id);
879         g_free(id);
880         return ret;
881 }
882
883 void bar_pane_gps_update_from_config(GtkWidget *pane, const gchar **attribute_names,
884                                                                                 const gchar **attribute_values)
885 {
886         PaneGPSData *pgd;
887         gint zoom;
888         gint int_longitude, int_latitude;
889         gdouble longitude, latitude;
890
891         pgd = g_object_get_data(G_OBJECT(pane), "pane_data");
892         if (!pgd)
893                 return;
894
895         gchar *title = NULL;
896
897         while (*attribute_names)
898         {
899                 const gchar *option = *attribute_names++;
900                 const gchar *value = *attribute_values++;
901
902                 if (READ_CHAR_FULL("title", title))
903                         continue;
904                 if (READ_CHAR_FULL("map-id", pgd->map_source))
905                         continue;
906                 if (READ_BOOL_FULL("expanded", pgd->pane.expanded))
907                         continue;
908                 if (READ_INT_FULL("height", pgd->height))
909                         continue;
910                 if (READ_CHAR_FULL("id", pgd->pane.id))
911                         continue;
912                 if (READ_INT_CLAMP_FULL("zoom-level", zoom, 1, 8))
913                         {
914                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "zoom-level", zoom, NULL);
915                         continue;
916                         }
917                 if (READ_INT_CLAMP_FULL("longitude", int_longitude, -90000000, +90000000))
918                         {
919                         longitude = int_longitude / 1000000;
920                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "longitude", longitude, NULL);
921                         continue;
922                         }
923                 if (READ_INT_CLAMP_FULL("latitude", int_latitude, -90000000, +90000000))
924                         {
925                         latitude = int_latitude / 1000000;
926                         g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "latitude", latitude, NULL);
927                         continue;
928                         }
929                 log_printf("unknown attribute %s = %s\n", option, value);
930         }
931
932         if (title)
933                 {
934                 bar_pane_translate_title(PANE_COMMENT, pgd->pane.id, &title);
935                 gtk_label_set_text(GTK_LABEL(pgd->pane.title), title);
936                 g_free(title);
937                 }
938
939         gtk_widget_set_size_request(pgd->widget, -1, pgd->height);
940         bar_update_expander(pane);
941 }
942
943 #endif
944 #endif
945
946 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */