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