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