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