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