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