]> git.street.me.uk Git - andy/viking.git/blob - src/dialog.c
Add kdtree C code version 0.5.6 from https://code.google.com/p/kdtree/
[andy/viking.git] / src / dialog.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
6  * Copyright (C) 2010-2014, Rob Norris <rw_norris@hotmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "viking.h"
29 #include "thumbnails.h"
30 #include "garminsymbols.h"
31 #include "degrees_converters.h"
32 #include "authors.h"
33 #include "documenters.h"
34 #include "vikgoto.h"
35 #include "vikutils.h"
36 #include "util.h"
37 #include "geotag_exif.h"
38 #include "vikdatetime_edit_dialog.h"
39
40 #include <glib/gi18n.h>
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <time.h>
46
47 void a_dialog_msg ( GtkWindow *parent, gint type, const gchar *info, const gchar *extra )
48 {
49   GtkWidget *msgbox = gtk_message_dialog_new ( parent, GTK_DIALOG_DESTROY_WITH_PARENT, type, GTK_BUTTONS_OK, info, extra );
50   gtk_dialog_run ( GTK_DIALOG(msgbox) );
51   gtk_widget_destroy ( msgbox );
52 }
53
54 gboolean a_dialog_goto_latlon ( GtkWindow *parent, struct LatLon *ll, const struct LatLon *old )
55 {
56   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to Lat/Lon"),
57                                                   parent,
58                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
59                                                   GTK_STOCK_CANCEL,
60                                                   GTK_RESPONSE_REJECT,
61                                                   GTK_STOCK_OK,
62                                                   GTK_RESPONSE_ACCEPT,
63                                                   NULL);
64   GtkWidget *latlabel, *lonlabel;
65   GtkWidget *lat, *lon;
66   gchar *tmp_lat, *tmp_lon;
67
68   latlabel = gtk_label_new (_("Latitude:"));
69   lat = gtk_entry_new ();
70   tmp_lat = g_strdup_printf ( "%f", old->lat );
71   gtk_entry_set_text ( GTK_ENTRY(lat), tmp_lat );
72   g_free ( tmp_lat );
73
74   lonlabel = gtk_label_new (_("Longitude:"));
75   lon = gtk_entry_new ();
76   tmp_lon = g_strdup_printf ( "%f", old->lon );
77   gtk_entry_set_text ( GTK_ENTRY(lon), tmp_lon );
78   g_free ( tmp_lon );
79
80   gtk_widget_show ( latlabel );
81   gtk_widget_show ( lonlabel );
82   gtk_widget_show ( lat );
83   gtk_widget_show ( lon );
84
85   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel,  FALSE, FALSE, 0);
86   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lat, FALSE, FALSE, 0);
87   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel,  FALSE, FALSE, 0);
88   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lon,  FALSE, FALSE, 0);
89
90   // 'ok' when press return in the entry
91   g_signal_connect_swapped (lat, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
92   g_signal_connect_swapped (lon, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
93
94   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
95
96   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
97   {
98     ll->lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lat) ) );
99     ll->lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lon) ) );
100     gtk_widget_destroy ( dialog );
101     return TRUE;
102   }
103
104   gtk_widget_destroy ( dialog );
105   return FALSE;
106 }
107
108 gboolean a_dialog_goto_utm ( GtkWindow *parent, struct UTM *utm, const struct UTM *old )
109 {
110   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to UTM"),
111                                                   parent,
112                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
113                                                   GTK_STOCK_CANCEL,
114                                                   GTK_RESPONSE_REJECT,
115                                                   GTK_STOCK_OK,
116                                                   GTK_RESPONSE_ACCEPT,
117                                                   NULL);
118   GtkWidget *norlabel, *easlabel, *nor, *eas;
119   GtkWidget *zonehbox, *zonespin, *letterentry;
120   gchar *tmp_eas, *tmp_nor;
121   gchar tmp_letter[2];
122
123   norlabel = gtk_label_new (_("Northing:"));
124   nor = gtk_entry_new ();
125   tmp_nor = g_strdup_printf("%ld", (long) old->northing );
126   gtk_entry_set_text ( GTK_ENTRY(nor), tmp_nor );
127   g_free ( tmp_nor );
128
129   easlabel = gtk_label_new (_("Easting:"));
130   eas = gtk_entry_new ();
131   tmp_eas = g_strdup_printf("%ld", (long) old->easting );
132   gtk_entry_set_text ( GTK_ENTRY(eas), tmp_eas );
133   g_free ( tmp_eas );
134
135   zonehbox = gtk_hbox_new ( FALSE, 0 );
136   gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Zone:") ), FALSE, FALSE, 5 );
137   zonespin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( old->zone, 1, 60, 1, 5, 0 ), 1, 0 );
138   gtk_box_pack_start ( GTK_BOX(zonehbox), zonespin, TRUE, TRUE, 5 );
139   gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Letter:") ), FALSE, FALSE, 5 );
140   letterentry = gtk_entry_new ();
141   gtk_entry_set_max_length ( GTK_ENTRY(letterentry), 1 );
142   gtk_entry_set_width_chars ( GTK_ENTRY(letterentry), 2 );
143   tmp_letter[0] = old->letter;
144   tmp_letter[1] = '\0';
145   gtk_entry_set_text ( GTK_ENTRY(letterentry), tmp_letter );
146   gtk_box_pack_start ( GTK_BOX(zonehbox), letterentry, FALSE, FALSE, 5 );
147
148   gtk_widget_show ( norlabel );
149   gtk_widget_show ( easlabel );
150   gtk_widget_show ( nor );
151   gtk_widget_show ( eas );
152
153   gtk_widget_show_all ( zonehbox );
154
155   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), norlabel, FALSE, FALSE, 0);
156   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nor, FALSE, FALSE, 0);
157   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), easlabel,  FALSE, FALSE, 0);
158   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), eas,  FALSE, FALSE, 0);
159   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), zonehbox,  FALSE, FALSE, 0);
160
161   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
162
163   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
164   {
165     const gchar *letter;
166     utm->northing = atof ( gtk_entry_get_text ( GTK_ENTRY(nor) ) );
167     utm->easting = atof ( gtk_entry_get_text ( GTK_ENTRY(eas) ) );
168     utm->zone = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(zonespin) );
169     letter = gtk_entry_get_text ( GTK_ENTRY(letterentry) );
170     if (*letter)
171        utm->letter = toupper(*letter);
172     gtk_widget_destroy ( dialog );
173     return TRUE;
174   }
175
176   gtk_widget_destroy ( dialog );
177   return FALSE;
178 }
179
180 void a_dialog_response_accept ( GtkDialog *dialog )
181 {
182   gtk_dialog_response ( dialog, GTK_RESPONSE_ACCEPT );
183 }
184
185 static void update_time ( GtkWidget *widget, VikWaypoint *wp )
186 {
187   gchar *msg = vu_get_time_string ( &(wp->timestamp), "%c", &(wp->coord), NULL );
188   gtk_button_set_label ( GTK_BUTTON(widget), msg );
189   g_free ( msg );
190 }
191
192 static VikWaypoint *edit_wp;
193
194 static void time_edit_click ( GtkWidget *widget, VikWaypoint *wp )
195 {
196   GTimeZone *gtz = g_time_zone_new_local ();
197   time_t mytime = vik_datetime_edit_dialog ( GTK_WINDOW(gtk_widget_get_toplevel(widget)),
198                                              _("Date/Time Edit"),
199                                              wp->timestamp,
200                                              gtz );
201   g_time_zone_unref ( gtz );
202
203   // Was the dialog cancelled?
204   if ( mytime == 0 )
205     return;
206
207   // Otherwise use new value in the edit buffer
208   edit_wp->timestamp = mytime;
209
210   // Clear the previous 'Add' image as now a time is set
211   if ( gtk_button_get_image ( GTK_BUTTON(widget) ) )
212     gtk_button_set_image ( GTK_BUTTON(widget), NULL );
213
214   update_time ( widget, edit_wp );
215 }
216
217 static void symbol_entry_changed_cb(GtkWidget *combo, GtkListStore *store)
218 {
219   GtkTreeIter iter;
220   gchar *sym;
221
222   if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter))
223     return;
224
225   gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
226   /* Note: symm is NULL when "(none)" is select (first cell is empty) */
227   gtk_widget_set_tooltip_text(combo, sym);
228   g_free(sym);
229 }
230
231 /* Specify if a new waypoint or not */
232 /* If a new waypoint then it uses the default_name for the suggested name allowing the user to change it.
233     The name to use is returned
234  */
235 /* todo: less on this side, like add track */
236 gchar *a_dialog_waypoint ( GtkWindow *parent, gchar *default_name, VikTrwLayer *vtl, VikWaypoint *wp, VikCoordMode coord_mode, gboolean is_new, gboolean *updated )
237 {
238   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Waypoint Properties"),
239                                                    parent,
240                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
241                                                    GTK_STOCK_CANCEL,
242                                                    GTK_RESPONSE_REJECT,
243                                                    GTK_STOCK_OK,
244                                                    GTK_RESPONSE_ACCEPT,
245                                                    NULL);
246   struct LatLon ll;
247   GtkWidget *latlabel, *lonlabel, *namelabel, *latentry, *lonentry, *altentry, *altlabel, *nameentry=NULL;
248   GtkWidget *commentlabel, *commententry, *descriptionlabel, *descriptionentry, *imagelabel, *imageentry, *symbollabel, *symbolentry;
249   GtkWidget *timelabel = NULL;
250   GtkWidget *timevaluebutton = NULL;
251   GtkWidget *hasGeotagCB = NULL;
252   GtkWidget *consistentGeotagCB = NULL;
253   GtkListStore *store;
254
255   gchar *lat, *lon, *alt;
256
257   vik_coord_to_latlon ( &(wp->coord), &ll );
258
259   lat = g_strdup_printf ( "%f", ll.lat );
260   lon = g_strdup_printf ( "%f", ll.lon );
261   vik_units_height_t height_units = a_vik_get_units_height ();
262   switch (height_units) {
263   case VIK_UNITS_HEIGHT_METRES:
264     alt = g_strdup_printf ( "%f", wp->altitude );
265     break;
266   case VIK_UNITS_HEIGHT_FEET:
267     alt = g_strdup_printf ( "%f", VIK_METERS_TO_FEET(wp->altitude) );
268     break;
269   default:
270     alt = g_strdup_printf ( "%f", wp->altitude );
271     g_critical("Houston, we've had a problem. height=%d", height_units);
272   }
273
274   *updated = FALSE;
275
276   namelabel = gtk_label_new (_("Name:"));
277   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), namelabel, FALSE, FALSE, 0);
278   // Name is now always changeable
279   nameentry = gtk_entry_new ();
280   if ( default_name )
281     gtk_entry_set_text( GTK_ENTRY(nameentry), default_name );
282   g_signal_connect_swapped ( nameentry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
283   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nameentry, FALSE, FALSE, 0);
284
285   latlabel = gtk_label_new (_("Latitude:"));
286   latentry = gtk_entry_new ();
287   gtk_entry_set_text ( GTK_ENTRY(latentry), lat );
288   g_free ( lat );
289
290   lonlabel = gtk_label_new (_("Longitude:"));
291   lonentry = gtk_entry_new ();
292   gtk_entry_set_text ( GTK_ENTRY(lonentry), lon );
293   g_free ( lon );
294
295   altlabel = gtk_label_new (_("Altitude:"));
296   altentry = gtk_entry_new ();
297   gtk_entry_set_text ( GTK_ENTRY(altentry), alt );
298   g_free ( alt );
299
300   commentlabel = gtk_label_new (_("Comment:"));
301   commententry = gtk_entry_new ();
302   gchar *cmt =  NULL;
303   // Auto put in some kind of 'name' as a comment if one previously 'goto'ed this exact location
304   cmt = a_vik_goto_get_search_string_for_this_place(VIK_WINDOW(parent));
305   if (cmt)
306     gtk_entry_set_text(GTK_ENTRY(commententry), cmt);
307
308   descriptionlabel = gtk_label_new (_("Description:"));
309   descriptionentry = gtk_entry_new ();
310
311   imagelabel = gtk_label_new (_("Image:"));
312   imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN, VF_FILTER_IMAGE);
313
314   {
315     GtkCellRenderer *r;
316     symbollabel = gtk_label_new (_("Symbol:"));
317     GtkTreeIter iter;
318
319     store = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING);
320     symbolentry = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
321     gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(symbolentry), 6);
322
323     g_signal_connect(symbolentry, "changed", G_CALLBACK(symbol_entry_changed_cb), store);
324     gtk_list_store_append (store, &iter);
325     gtk_list_store_set (store, &iter, 0, NULL, 1, NULL, 2, _("(none)"), -1);
326     a_populate_sym_list(store);
327
328     r = gtk_cell_renderer_pixbuf_new ();
329     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
330     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "pixbuf", 1, NULL);
331
332     r = gtk_cell_renderer_text_new ();
333     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
334     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "text", 2, NULL);
335
336     if ( !is_new && wp->symbol ) {
337       gboolean ok;
338       gchar *sym;
339       for (ok = gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(store), &iter ); ok; ok = gtk_tree_model_iter_next ( GTK_TREE_MODEL(store), &iter)) {
340         gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
341         if (sym && !strcmp(sym, wp->symbol)) {
342           g_free(sym);
343           break;
344         } else {
345           g_free(sym);
346         }
347       }
348       // Ensure is it a valid symbol in the given symbol set (large vs small)
349       // Not all symbols are available in both
350       // The check prevents a Gtk Critical message
351       if ( iter.stamp )
352         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(symbolentry), &iter);
353     }
354   }
355
356   if ( !is_new && wp->comment )
357     gtk_entry_set_text ( GTK_ENTRY(commententry), wp->comment );
358
359   if ( !is_new && wp->description )
360     gtk_entry_set_text ( GTK_ENTRY(descriptionentry), wp->description );
361
362   if ( !is_new && wp->image ) {
363     vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), wp->image );
364
365     // Geotag Info [readonly]
366     hasGeotagCB = gtk_check_button_new_with_label ( _("Has Geotag") );
367     gtk_widget_set_sensitive ( hasGeotagCB, FALSE );
368     gboolean hasGeotag;
369     gchar *ignore = a_geotag_get_exif_date_from_file ( wp->image, &hasGeotag );
370     g_free ( ignore );
371     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(hasGeotagCB), hasGeotag );
372
373     consistentGeotagCB = gtk_check_button_new_with_label ( _("Consistent Position") );
374     gtk_widget_set_sensitive ( consistentGeotagCB, FALSE );
375     if ( hasGeotag ) {
376       struct LatLon ll = a_geotag_get_position ( wp->image );
377       VikCoord coord;
378       vik_coord_load_from_latlon ( &coord, coord_mode, &ll );
379       gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(consistentGeotagCB), vik_coord_equals(&coord, &wp->coord) );
380     }
381   }
382
383   timelabel = gtk_label_new ( _("Time:") );
384   timevaluebutton = gtk_button_new();
385   gtk_button_set_relief ( GTK_BUTTON(timevaluebutton), GTK_RELIEF_NONE );
386
387   if ( !edit_wp )
388     edit_wp = vik_waypoint_new ();
389   edit_wp = vik_waypoint_copy ( wp );
390
391   // TODO: Consider if there should be a remove time button...
392
393   if ( !is_new && wp->has_timestamp ) {
394     update_time ( timevaluebutton, wp );
395   }
396   else {
397     GtkWidget *img = gtk_image_new_from_stock ( GTK_STOCK_ADD, GTK_ICON_SIZE_MENU );
398     gtk_button_set_image ( GTK_BUTTON(timevaluebutton), img );
399     // Initially use current time or otherwise whatever the last value used was
400     if ( edit_wp->timestamp == 0 ) {
401       time ( &edit_wp->timestamp );
402     }
403   }
404   g_signal_connect ( G_OBJECT(timevaluebutton), "clicked", G_CALLBACK(time_edit_click), edit_wp );
405
406   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
407   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latentry, FALSE, FALSE, 0);
408   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
409   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonentry, FALSE, FALSE, 0);
410   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timelabel, FALSE, FALSE, 0);
411   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timevaluebutton, FALSE, FALSE, 0);
412   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altlabel, FALSE, FALSE, 0);
413   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altentry, FALSE, FALSE, 0);
414   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commentlabel, FALSE, FALSE, 0);
415   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commententry, FALSE, FALSE, 0);
416   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionlabel, FALSE, FALSE, 0);
417   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionentry, FALSE, FALSE, 0);
418   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imagelabel, FALSE, FALSE, 0);
419   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imageentry, FALSE, FALSE, 0);
420   if ( hasGeotagCB ) {
421     GtkWidget *hbox =  gtk_hbox_new ( FALSE, 0 );
422     gtk_box_pack_start (GTK_BOX(hbox), hasGeotagCB, FALSE, FALSE, 0);
423     gtk_box_pack_start (GTK_BOX(hbox), consistentGeotagCB, FALSE, FALSE, 0);
424     gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox, FALSE, FALSE, 0);
425   }
426   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), symbollabel, FALSE, FALSE, 0);
427   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(symbolentry), FALSE, FALSE, 0);
428
429   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
430
431   gtk_widget_show_all ( gtk_dialog_get_content_area(GTK_DIALOG(dialog)) );
432
433   if ( !is_new ) {
434     // Shift left<->right to try not to obscure the waypoint.
435     trw_layer_dialog_shift ( vtl, GTK_WINDOW(dialog), &(wp->coord), FALSE );
436   }
437
438   while ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
439   {
440     if ( strlen((gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) )) == 0 ) /* TODO: other checks (isalpha or whatever ) */
441       a_dialog_info_msg ( parent, _("Please enter a name for the waypoint.") );
442     else {
443       // NB: No check for unique names - this allows generation of same named entries.
444       gchar *entered_name = g_strdup ( (gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) ) );
445
446       /* Do It */
447       ll.lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(latentry) ) );
448       ll.lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lonentry) ) );
449       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &ll );
450       // Always store in metres
451       switch (height_units) {
452       case VIK_UNITS_HEIGHT_METRES:
453         wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
454         break;
455       case VIK_UNITS_HEIGHT_FEET:
456         wp->altitude = VIK_FEET_TO_METERS(atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) ));
457         break;
458       default:
459         wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
460         g_critical("Houston, we've had a problem. height=%d", height_units);
461       }
462       if ( g_strcmp0 ( wp->comment, gtk_entry_get_text ( GTK_ENTRY(commententry) ) ) )
463         vik_waypoint_set_comment ( wp, gtk_entry_get_text ( GTK_ENTRY(commententry) ) );
464       if ( g_strcmp0 ( wp->description, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) ) )
465         vik_waypoint_set_description ( wp, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) );
466       if ( g_strcmp0 ( wp->image, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) ) )
467         vik_waypoint_set_image ( wp, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) );
468       if ( wp->image && *(wp->image) && (!a_thumbnails_exists(wp->image)) )
469         a_thumbnails_create ( wp->image );
470       if ( edit_wp->timestamp ) {
471         wp->timestamp = edit_wp->timestamp;
472         wp->has_timestamp = TRUE;
473       }
474
475       GtkTreeIter iter, first;
476       gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(store), &first );
477       if ( !gtk_combo_box_get_active_iter ( GTK_COMBO_BOX(symbolentry), &iter ) || !memcmp(&iter, &first, sizeof(GtkTreeIter)) ) {
478         vik_waypoint_set_symbol ( wp, NULL );
479       } else {
480         gchar *sym;
481         gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
482         vik_waypoint_set_symbol ( wp, sym );
483         g_free(sym);
484       }
485
486       gtk_widget_destroy ( dialog );
487       if ( is_new )
488         return entered_name;
489       else {
490         *updated = TRUE;
491         // See if name has been changed
492         if ( g_strcmp0 (default_name, entered_name ) )
493           return entered_name;
494         else
495           return NULL;
496       }
497     }
498   }
499   gtk_widget_destroy ( dialog );
500   return NULL;
501 }
502
503 static void get_selected_foreach_func(GtkTreeModel *model,
504                                       GtkTreePath *path,
505                                       GtkTreeIter *iter,
506                                       gpointer data)
507 {
508   GList **list = data;
509   gchar *name;
510   gtk_tree_model_get (model, iter, 0, &name, -1);
511   *list = g_list_prepend(*list, name);
512 }
513
514 GList *a_dialog_select_from_list ( GtkWindow *parent, GList *names, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg )
515 {
516   GtkTreeIter iter;
517   GtkCellRenderer *renderer;
518   GtkWidget *view;
519
520   GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
521                                                   parent,
522                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
523                                                   GTK_STOCK_CANCEL,
524                                                   GTK_RESPONSE_REJECT,
525                                                   GTK_STOCK_OK,
526                                                   GTK_RESPONSE_ACCEPT,
527                                                   NULL);
528   /* When something is selected then OK */
529   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
530   GtkWidget *response_w = NULL;
531 #if GTK_CHECK_VERSION (2, 20, 0)
532   /* Default to not apply - as initially nothing is selected! */
533   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
534 #endif
535   GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
536
537   GtkWidget *scrolledwindow;
538
539   GList *runner = names;
540   while (runner)
541   {
542     gtk_list_store_append(store, &iter);
543     gtk_list_store_set(store, &iter, 0, runner->data, -1);
544     runner = g_list_next(runner);
545   }
546
547   view = gtk_tree_view_new();
548   renderer = gtk_cell_renderer_text_new();
549   // Use the column header to display the message text,
550   // this makes the overall widget allocation simple as treeview takes up all the space
551   GtkTreeViewColumn *column;
552   column = gtk_tree_view_column_new_with_attributes (msg, renderer, "text", 0, NULL );
553   gtk_tree_view_column_set_sort_column_id (column, 0);
554   gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
555
556   gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
557   gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
558       multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
559   g_object_unref(store);
560
561   scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
562   gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
563   gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
564
565   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0 );
566   // Ensure a reasonable number of items are shown, but let the width be automatically sized
567   gtk_widget_set_size_request ( dialog, -1, 400) ;
568
569   gtk_widget_show_all ( dialog );
570
571   if ( response_w )
572     gtk_widget_grab_focus ( response_w );
573
574   while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
575   {
576     GList *names_selected = NULL;
577     GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
578     gtk_tree_selection_selected_foreach(selection, get_selected_foreach_func, &names_selected);
579     if (names_selected)
580     {
581       gtk_widget_destroy ( dialog );
582       return names_selected;
583     }
584     a_dialog_error_msg(parent, _("Nothing was selected"));
585   }
586   gtk_widget_destroy ( dialog );
587   return NULL;
588 }
589
590 gchar *a_dialog_new_track ( GtkWindow *parent, gchar *default_name, gboolean is_route )
591 {
592   GtkWidget *dialog = gtk_dialog_new_with_buttons ( is_route ? _("Add Route") : _("Add Track"),
593                                                   parent,
594                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
595                                                   GTK_STOCK_CANCEL,
596                                                   GTK_RESPONSE_REJECT,
597                                                   GTK_STOCK_OK,
598                                                   GTK_RESPONSE_ACCEPT,
599                                                   NULL);
600   GtkWidget *label = gtk_label_new ( is_route ? _("Route Name:") : _("Track Name:") );
601   GtkWidget *entry = gtk_entry_new ();
602
603   if (default_name)
604     gtk_entry_set_text ( GTK_ENTRY(entry), default_name );
605
606   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
607   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), entry, FALSE, FALSE, 0);
608
609   g_signal_connect_swapped ( entry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
610
611   gtk_widget_show ( label );
612   gtk_widget_show ( entry );
613
614   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
615
616   while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
617   {
618     const gchar *constname = gtk_entry_get_text ( GTK_ENTRY(entry) );
619     if ( *constname == '\0' )
620       a_dialog_info_msg ( parent, _("Please enter a name for the track.") );
621     else {
622       gchar *name = g_strdup ( constname );
623       gtk_widget_destroy ( dialog );
624       return name;
625     }
626   }
627   gtk_widget_destroy ( dialog );
628   return NULL;
629 }
630
631 static void today_clicked (GtkWidget *cal)
632 {
633   GDateTime *now = g_date_time_new_now_local ();
634   gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(now)-1, g_date_time_get_year(now) );
635   gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(now) );
636   g_date_time_unref ( now );
637 }
638
639 /**
640  * a_dialog_get_date:
641  *
642  * Returns: a date as a string - always in ISO8601 format (YYYY-MM-DD)
643  *  This string can be NULL (especially when the dialog is cancelled)
644  *  Free the string after use
645  */
646 gchar *a_dialog_get_date ( GtkWindow *parent, const gchar *title )
647 {
648   GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
649                                                     parent,
650                                                     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
651                                                     GTK_STOCK_CANCEL,
652                                                     GTK_RESPONSE_REJECT,
653                                                     GTK_STOCK_OK,
654                                                     GTK_RESPONSE_ACCEPT,
655                                                     NULL);
656   GtkWidget *cal = gtk_calendar_new ();
657   GtkWidget *today = gtk_button_new_with_label ( _("Today") );
658
659   static guint year = 0;
660   static guint month = 0;
661   static guint day = 0;
662
663   if ( year != 0 ) {
664     // restore the last selected date
665     gtk_calendar_select_month ( GTK_CALENDAR(cal), month, year );
666     gtk_calendar_select_day ( GTK_CALENDAR(cal), day );
667   }
668
669   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), today, FALSE, FALSE, 0);
670   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0);
671
672   g_signal_connect_swapped ( G_OBJECT(today), "clicked", G_CALLBACK(today_clicked), cal );
673
674   gtk_widget_show_all ( dialog );
675
676   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
677
678   gchar *date_str = NULL;
679   if ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
680   {
681     gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day );
682     date_str = g_strdup_printf ( "%d-%02d-%02d", year, month+1, day );
683   }
684   gtk_widget_destroy ( dialog );
685   return date_str;
686 }
687
688 /* creates a vbox full of labels */
689 GtkWidget *a_dialog_create_label_vbox ( gchar **texts, int label_count, gint spacing, gint padding )
690 {
691   GtkWidget *vbox, *label;
692   int i;
693   vbox = gtk_vbox_new( TRUE, spacing );
694
695   for ( i = 0; i < label_count; i++ )
696   {
697     label = gtk_label_new(NULL);
698     gtk_label_set_markup ( GTK_LABEL(label), _(texts[i]) );
699     gtk_box_pack_start ( GTK_BOX(vbox), label, FALSE, TRUE, padding );
700   }
701   return vbox;
702 }
703
704 gboolean a_dialog_yes_or_no ( GtkWindow *parent, const gchar *message, const gchar *extra )
705 {
706   GtkWidget *dia;
707   dia = gtk_message_dialog_new ( parent,
708                                  GTK_DIALOG_DESTROY_WITH_PARENT,
709                                  GTK_MESSAGE_QUESTION,
710                                  GTK_BUTTONS_YES_NO,
711                                  message, extra );
712
713   if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_YES )
714   {
715     gtk_widget_destroy ( dia );
716     return TRUE;
717   }
718   else
719   {
720     gtk_widget_destroy ( dia );
721     return FALSE;
722   }
723 }
724
725 static void zoom_spin_changed ( GtkSpinButton *spin, GtkWidget *pass_along[3] )
726 {
727   if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(pass_along[2]) ) )
728     gtk_spin_button_set_value ( 
729         GTK_SPIN_BUTTON(pass_along[GTK_WIDGET(spin) == pass_along[0] ? 1 : 0]),
730         gtk_spin_button_get_value ( spin ) );
731 }
732
733 gboolean a_dialog_custom_zoom ( GtkWindow *parent, gdouble *xmpp, gdouble *ympp )
734 {
735   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Zoom Factors..."),
736                                                   parent,
737                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
738                                                   GTK_STOCK_CANCEL,
739                                                   GTK_RESPONSE_REJECT,
740                                                   GTK_STOCK_OK,
741                                                   GTK_RESPONSE_ACCEPT,
742                                                   NULL);
743   GtkWidget *table, *label, *xlabel, *xspin, *ylabel, *yspin, *samecheck;
744   GtkWidget *pass_along[3];
745
746   table = gtk_table_new ( 4, 2, FALSE );
747   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
748
749   label = gtk_label_new ( _("Zoom factor (in meters per pixel):") );
750   xlabel = gtk_label_new ( _("X (easting): "));
751   ylabel = gtk_label_new ( _("Y (northing): "));
752
753   pass_along[0] = xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *xmpp, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 );
754   pass_along[1] = yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *ympp, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 );
755
756   pass_along[2] = samecheck = gtk_check_button_new_with_label ( _("X and Y zoom factors must be equal") );
757   /* TODO -- same factor */
758   /*  samecheck = gtk_check_button_new_with_label ( "Same x/y zoom factor" ); */
759
760   if ( *xmpp == *ympp )
761     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(samecheck), TRUE );
762
763   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
764   gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 1, 2 );
765   gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 1, 2 );
766   gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 2, 3 );
767   gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 2, 3 );
768   gtk_table_attach_defaults ( GTK_TABLE(table), samecheck, 0, 2, 3, 4 );
769
770   gtk_widget_show_all ( table );
771
772   g_signal_connect ( G_OBJECT(xspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
773   g_signal_connect ( G_OBJECT(yspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
774
775   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
776
777   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
778   {
779     *xmpp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) );
780     *ympp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) );
781     gtk_widget_destroy ( dialog );
782     return TRUE;
783   }
784   gtk_widget_destroy ( dialog );
785   return FALSE;
786 }
787
788 static void split_spin_focused ( GtkSpinButton *spin, GtkWidget *pass_along[1] )
789 {
790   gtk_toggle_button_set_active    (GTK_TOGGLE_BUTTON(pass_along[0]), 1);
791 }
792
793 gboolean a_dialog_time_threshold ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint *thr )
794 {
795   GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text, 
796                                                   parent,
797                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
798                                                   GTK_STOCK_CANCEL,
799                                                   GTK_RESPONSE_REJECT,
800                                                   GTK_STOCK_OK,
801                                                   GTK_RESPONSE_ACCEPT,
802                                                   NULL);
803   GtkWidget *table, *t1, *t2, *t3, *t4, *spin, *label;
804   GtkWidget *pass_along[1];
805
806   table = gtk_table_new ( 4, 2, FALSE );
807   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
808
809   label = gtk_label_new (label_text);
810
811   t1 = gtk_radio_button_new_with_label ( NULL, _("1 min") );
812   t2 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t1), _("1 hour") );
813   t3 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t2), _("1 day") );
814   t4 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t3), _("Custom (in minutes):") );
815
816   pass_along[0] = t4;
817
818   spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *thr, 0, 65536, 1, 5, 0 ), 1, 0 );
819
820   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
821   gtk_table_attach_defaults ( GTK_TABLE(table), t1, 0, 1, 1, 2 );
822   gtk_table_attach_defaults ( GTK_TABLE(table), t2, 0, 1, 2, 3 );
823   gtk_table_attach_defaults ( GTK_TABLE(table), t3, 0, 1, 3, 4 );
824   gtk_table_attach_defaults ( GTK_TABLE(table), t4, 0, 1, 4, 5 );
825   gtk_table_attach_defaults ( GTK_TABLE(table), spin, 1, 2, 4, 5 );
826
827   gtk_widget_show_all ( table );
828
829   g_signal_connect ( G_OBJECT(spin), "grab-focus", G_CALLBACK(split_spin_focused), pass_along );
830
831   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
832
833   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
834   {
835     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t1))) {
836       *thr = 1;
837     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t2))) {
838       *thr = 60;
839     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t3))) {
840       *thr = 60 * 24;
841     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t4))) {
842       *thr = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
843     }
844     gtk_widget_destroy ( dialog );
845     return TRUE;
846   }
847   gtk_widget_destroy ( dialog );
848   return FALSE;
849 }
850
851 /**
852  * a_dialog_get_positive_number:
853  * 
854  * Dialog to return a positive number via a spinbox within the supplied limits
855  * 
856  * Returns: A value of zero indicates the dialog was cancelled
857  */
858 guint a_dialog_get_positive_number ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint default_num, guint min, guint max, guint step )
859 {
860   GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
861                                                    parent,
862                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
863                                                    GTK_STOCK_CANCEL,
864                                                    GTK_RESPONSE_REJECT,
865                                                    GTK_STOCK_OK,
866                                                    GTK_RESPONSE_ACCEPT,
867                                                    NULL);
868   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
869   GtkWidget *response_w = NULL;
870 #if GTK_CHECK_VERSION (2, 20, 0)
871   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
872 #endif
873
874   GtkWidget *table, *spin, *label;
875   guint result = default_num;
876
877   table = gtk_table_new ( 2, 1, FALSE );
878   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
879
880   label = gtk_label_new (label_text);
881   spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( default_num, min, max, step, 5, 0 ), 1, 0 );
882
883   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 1, 0, 1 );
884   gtk_table_attach_defaults ( GTK_TABLE(table), spin, 0, 1, 1, 2 );
885
886   if ( response_w )
887     gtk_widget_grab_focus ( response_w );
888
889   gtk_widget_show_all ( table );
890
891   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
892   {
893     result = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
894     gtk_widget_destroy ( dialog );
895     return result;
896   }
897
898   // Dialog cancelled
899   gtk_widget_destroy ( dialog );
900   return 0;
901 }
902
903 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
904 static void about_url_hook (GtkAboutDialog *about,
905                             const gchar    *link,
906                             gpointer        data)
907 {
908   open_url (GTK_WINDOW(about), link);
909 }
910
911 static void about_email_hook (GtkAboutDialog *about,
912                               const gchar    *email,
913                               gpointer        data)
914 {
915   new_email (GTK_WINDOW(about), email);
916 }
917 #endif
918
919 void a_dialog_about ( GtkWindow *parent )
920 {
921   const gchar *program_name = PACKAGE_NAME;
922   const gchar *version = VIKING_VERSION;
923   const gchar *website = VIKING_URL;
924   const gchar *copyright = "2003-2008, Evan Battaglia\n2008-2013, Viking's contributors";
925   const gchar *comments = _("GPS Data and Topo Analyzer, Explorer, and Manager.");
926   const gchar *license = _("This program is free software; you can redistribute it and/or modify "
927                         "it under the terms of the GNU General Public License as published by "
928                         "the Free Software Foundation; either version 2 of the License, or "
929                         "(at your option) any later version."
930                         "\n\n"
931                         "This program is distributed in the hope that it will be useful, "
932                         "but WITHOUT ANY WARRANTY; without even the implied warranty of "
933                         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
934                         "GNU General Public License for more details."
935                         "\n\n"
936                         "You should have received a copy of the GNU General Public License "
937                         "along with this program; if not, write to the Free Software "
938                         "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA");
939
940   // Would be nice to use gtk_about_dialog_add_credit_section (), but that requires gtk 3.4
941   // For now shove it in the 'artists' section so at least the information is easily visible
942   // Something more advanced might have proper version information too...
943   const gchar *libs[] = {
944     "Compiled in libraries:",
945     // Default libs
946     "libglib-2.0",
947     "libgthread-2.0",
948     "libgtk+-2.0",
949     "libgio-2.0",
950     // Potentially optional libs (but probably couldn't build without them)
951 #ifdef HAVE_LIBM
952     "libm",
953 #endif
954 #ifdef HAVE_LIBZ
955     "libz",
956 #endif
957 #ifdef HAVE_LIBCURL
958     "libcurl",
959 #endif
960     // Actually optional libs
961 #ifdef HAVE_LIBGPS
962     "libgps",
963 #endif
964 #ifdef HAVE_LIBEXIF
965     "libexif",
966 #endif
967 #ifdef HAVE_LIBX11
968     "libX11",
969 #endif
970 #ifdef HAVE_LIBMAGIC
971     "libmagic",
972 #endif
973 #ifdef HAVE_LIBBZ2
974     "libbz2",
975 #endif
976 #ifdef HAVE_LIBSQLITE3
977     "libsqlite3",
978 #endif
979     NULL
980   };
981   // Newer versions of GTK 'just work', calling gtk_show_uri() on the URL or email and opens up the appropriate program
982   // This is the old method:
983 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
984   gtk_about_dialog_set_url_hook (about_url_hook, NULL, NULL);
985   gtk_about_dialog_set_email_hook (about_email_hook, NULL, NULL);
986 #endif
987
988   gtk_show_about_dialog (parent,
989         /* TODO do not set program-name and correctly set info for g_get_application_name */
990         "program-name", program_name,
991         "version", version,
992         "website", website,
993         "comments", comments,
994         "copyright", copyright,
995         "license", license,
996         "wrap-license", TRUE,
997         /* logo automatically retrieved via gtk_window_get_default_icon_list */
998         "authors", AUTHORS,
999         "documenters", DOCUMENTERS,
1000         "translator-credits", _("Translation is coordinated on http://launchpad.net/viking"),
1001         "artists", libs,
1002         NULL);
1003 }
1004
1005 gboolean a_dialog_map_n_zoom(GtkWindow *parent, gchar *mapnames[], gint default_map, gchar *zoom_list[], gint default_zoom, gint *selected_map, gint *selected_zoom)
1006 {
1007   gchar **s;
1008
1009   GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Download along track"), parent, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
1010   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1011   GtkWidget *response_w = NULL;
1012 #if GTK_CHECK_VERSION (2, 20, 0)
1013   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1014 #endif
1015
1016   GtkWidget *map_label = gtk_label_new(_("Map type:"));
1017   GtkWidget *map_combo = vik_combo_box_text_new();
1018   for (s = mapnames; *s; s++)
1019     vik_combo_box_text_append (GTK_COMBO_BOX(map_combo), *s);
1020   gtk_combo_box_set_active (GTK_COMBO_BOX(map_combo), default_map);
1021
1022   GtkWidget *zoom_label = gtk_label_new(_("Zoom level:"));
1023   GtkWidget *zoom_combo = vik_combo_box_text_new();
1024   for (s = zoom_list; *s; s++)
1025     vik_combo_box_text_append (GTK_COMBO_BOX(zoom_combo), *s);
1026   gtk_combo_box_set_active (GTK_COMBO_BOX(zoom_combo), default_zoom);
1027
1028   GtkTable *box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
1029   gtk_table_attach_defaults(box, map_label, 0, 1, 0, 1);
1030   gtk_table_attach_defaults(box, map_combo, 1, 2, 0, 1);
1031   gtk_table_attach_defaults(box, zoom_label, 0, 1, 1, 2);
1032   gtk_table_attach_defaults(box, zoom_combo, 1, 2, 1, 2);
1033
1034   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(box), FALSE, FALSE, 5 );
1035
1036   if ( response_w )
1037     gtk_widget_grab_focus ( response_w );
1038
1039   gtk_widget_show_all ( dialog );
1040   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
1041     gtk_widget_destroy(dialog);
1042     return FALSE;
1043   }
1044
1045   *selected_map = gtk_combo_box_get_active(GTK_COMBO_BOX(map_combo));
1046   *selected_zoom = gtk_combo_box_get_active(GTK_COMBO_BOX(zoom_combo));
1047
1048   gtk_widget_destroy(dialog);
1049   return TRUE;
1050 }
1051
1052 /**
1053  * Display a dialog presenting the license of a map.
1054  * Allow to read the license by launching a web browser.
1055  */
1056 void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *license, const gchar *url)
1057 {
1058   GtkWidget *dialog = gtk_message_dialog_new (parent,
1059                                  GTK_DIALOG_DESTROY_WITH_PARENT,
1060                                  GTK_MESSAGE_INFO,
1061                                  GTK_BUTTONS_OK,
1062                                  _("The map data is licensed: %s."),
1063                                  license);
1064   gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1065     _("The data provided by '<b>%s</b>' are licensed under the following license: <b>%s</b>."),
1066     map, license);
1067 #define RESPONSE_OPEN_LICENSE 600
1068   if (url != NULL) {
1069     gtk_dialog_add_button (GTK_DIALOG (dialog), _("Open license"), RESPONSE_OPEN_LICENSE);
1070   }
1071   gint response;
1072   do {
1073     response = gtk_dialog_run (GTK_DIALOG (dialog));
1074     if (response == RESPONSE_OPEN_LICENSE) {
1075       open_url (parent, url);
1076     }
1077   } while (response != GTK_RESPONSE_DELETE_EVENT && response != GTK_RESPONSE_OK);
1078   gtk_widget_destroy (dialog);
1079 }