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