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