]> git.street.me.uk Git - andy/viking.git/blob - src/dialog.c
Put vikutils.h into viking.h
[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-2016, 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 "degrees_converters.h"
30 #include "authors.h"
31 #include "documenters.h"
32 #include "ui_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 /**
48  * a_dialog_goto_latlon:
49  *
50  * @parent:  Parent window
51  * @ll:      The returned #LatLon location
52  * @old:     Initialize the dialog with this #LatLon location
53  *
54  * A simple dialog to get a lat/lon location
55  *
56  * Returns: FALSE if the dialog was cancelled
57  */
58 gboolean a_dialog_goto_latlon ( GtkWindow *parent, struct LatLon *ll, const struct LatLon *old )
59 {
60   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to Lat/Lon"),
61                                                   parent,
62                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
63                                                   GTK_STOCK_CANCEL,
64                                                   GTK_RESPONSE_REJECT,
65                                                   GTK_STOCK_OK,
66                                                   GTK_RESPONSE_ACCEPT,
67                                                   NULL);
68
69   GtkWidget *latlabel = gtk_label_new (_("Latitude:"));
70   GtkWidget *lat = ui_spin_button_new ( (GtkAdjustment*)gtk_adjustment_new(old->lat,-90,90.0,0.05,0.1,0), 0.1, 6 );
71
72   GtkWidget *lonlabel = gtk_label_new (_("Longitude:"));
73   GtkWidget *lon = ui_spin_button_new ( (GtkAdjustment*)gtk_adjustment_new(old->lon,-180.0,180.0,0.05,0.1,0), 0.1, 6 );
74
75   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel,  FALSE, FALSE, 0);
76   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lat, FALSE, FALSE, 0);
77   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel,  FALSE, FALSE, 0);
78   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lon,  FALSE, FALSE, 0);
79
80   // 'ok' when press return on an entry
81   g_signal_connect_swapped (lat, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
82   g_signal_connect_swapped (lon, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
83
84   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
85
86   gtk_widget_show_all ( dialog );
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_letter[2];
113
114   norlabel = gtk_label_new (_("Northing:"));
115   nor = ui_spin_button_new ( (GtkAdjustment*)gtk_adjustment_new(old->northing,0,9999999,1,250,0), 1, 0 );
116
117   easlabel = gtk_label_new (_("Easting:"));
118   eas = ui_spin_button_new ( (GtkAdjustment*)gtk_adjustment_new(old->easting,0,9999999,1,250,0), 1, 0 );
119
120   zonehbox = gtk_hbox_new ( FALSE, 0 );
121   gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Zone:") ), FALSE, FALSE, 5 );
122   zonespin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( old->zone, 1, 60, 1, 5, 0 ), 1, 0 );
123   gtk_box_pack_start ( GTK_BOX(zonehbox), zonespin, TRUE, TRUE, 5 );
124   gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Letter:") ), FALSE, FALSE, 5 );
125   letterentry = gtk_entry_new ();
126   gtk_entry_set_max_length ( GTK_ENTRY(letterentry), 1 );
127   gtk_entry_set_width_chars ( GTK_ENTRY(letterentry), 2 );
128   tmp_letter[0] = old->letter;
129   tmp_letter[1] = '\0';
130   gtk_entry_set_text ( GTK_ENTRY(letterentry), tmp_letter );
131   gtk_box_pack_start ( GTK_BOX(zonehbox), letterentry, FALSE, FALSE, 5 );
132
133   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), norlabel, FALSE, FALSE, 0);
134   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nor, FALSE, FALSE, 0);
135   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), easlabel,  FALSE, FALSE, 0);
136   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), eas,  FALSE, FALSE, 0);
137   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), zonehbox,  FALSE, FALSE, 2);
138
139   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
140
141   gtk_widget_show_all ( dialog );
142
143   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
144   {
145     const gchar *letter;
146     utm->northing = atof ( gtk_entry_get_text ( GTK_ENTRY(nor) ) );
147     utm->easting = atof ( gtk_entry_get_text ( GTK_ENTRY(eas) ) );
148     utm->zone = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(zonespin) );
149     letter = gtk_entry_get_text ( GTK_ENTRY(letterentry) );
150     if (*letter)
151        utm->letter = toupper(*letter);
152     gtk_widget_destroy ( dialog );
153     return TRUE;
154   }
155
156   gtk_widget_destroy ( dialog );
157   return FALSE;
158 }
159
160 void a_dialog_response_accept ( GtkDialog *dialog )
161 {
162   gtk_dialog_response ( dialog, GTK_RESPONSE_ACCEPT );
163 }
164
165 static void get_selected_foreach_func(GtkTreeModel *model,
166                                       GtkTreePath *path,
167                                       GtkTreeIter *iter,
168                                       gpointer data)
169 {
170   GList **list = data;
171   gchar *name;
172   gtk_tree_model_get (model, iter, 0, &name, -1);
173   *list = g_list_prepend(*list, name);
174 }
175
176 GList *a_dialog_select_from_list ( GtkWindow *parent, GList *names, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg )
177 {
178   GtkTreeIter iter;
179   GtkCellRenderer *renderer;
180   GtkWidget *view;
181
182   GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
183                                                   parent,
184                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
185                                                   GTK_STOCK_CANCEL,
186                                                   GTK_RESPONSE_REJECT,
187                                                   GTK_STOCK_OK,
188                                                   GTK_RESPONSE_ACCEPT,
189                                                   NULL);
190   /* When something is selected then OK */
191   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
192   GtkWidget *response_w = NULL;
193 #if GTK_CHECK_VERSION (2, 20, 0)
194   /* Default to not apply - as initially nothing is selected! */
195   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
196 #endif
197   GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
198
199   GtkWidget *scrolledwindow;
200
201   GList *runner = names;
202   while (runner)
203   {
204     gtk_list_store_append(store, &iter);
205     gtk_list_store_set(store, &iter, 0, runner->data, -1);
206     runner = g_list_next(runner);
207   }
208
209   view = gtk_tree_view_new();
210   renderer = gtk_cell_renderer_text_new();
211   // Use the column header to display the message text,
212   // this makes the overall widget allocation simple as treeview takes up all the space
213   GtkTreeViewColumn *column;
214   column = gtk_tree_view_column_new_with_attributes (msg, renderer, "text", 0, NULL );
215   gtk_tree_view_column_set_sort_column_id (column, 0);
216   gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
217
218   gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
219   gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
220       multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
221   g_object_unref(store);
222
223   scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
224   gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
225   gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
226
227   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0 );
228   // Ensure a reasonable number of items are shown, but let the width be automatically sized
229   gtk_widget_set_size_request ( dialog, -1, 400) ;
230
231   gtk_widget_show_all ( dialog );
232
233   if ( response_w )
234     gtk_widget_grab_focus ( response_w );
235
236   while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
237   {
238     GList *names_selected = NULL;
239     GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
240     gtk_tree_selection_selected_foreach(selection, get_selected_foreach_func, &names_selected);
241     if (names_selected)
242     {
243       gtk_widget_destroy ( dialog );
244       return names_selected;
245     }
246     a_dialog_error_msg(parent, _("Nothing was selected"));
247   }
248   gtk_widget_destroy ( dialog );
249   return NULL;
250 }
251
252 gchar *a_dialog_new_track ( GtkWindow *parent, gchar *default_name, gboolean is_route )
253 {
254   GtkWidget *dialog = gtk_dialog_new_with_buttons ( is_route ? _("Add Route") : _("Add Track"),
255                                                   parent,
256                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
257                                                   GTK_STOCK_CANCEL,
258                                                   GTK_RESPONSE_REJECT,
259                                                   GTK_STOCK_OK,
260                                                   GTK_RESPONSE_ACCEPT,
261                                                   NULL);
262   GtkWidget *label = gtk_label_new ( is_route ? _("Route Name:") : _("Track Name:") );
263   GtkWidget *entry = gtk_entry_new ();
264
265   if (default_name)
266     gtk_entry_set_text ( GTK_ENTRY(entry), default_name );
267
268   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
269   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), entry, FALSE, FALSE, 0);
270
271   g_signal_connect_swapped ( entry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
272
273   gtk_widget_show ( label );
274   gtk_widget_show ( entry );
275
276   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
277
278   while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
279   {
280     const gchar *constname = gtk_entry_get_text ( GTK_ENTRY(entry) );
281     if ( *constname == '\0' )
282       a_dialog_info_msg ( parent, _("Please enter a name for the track.") );
283     else {
284       gchar *name = g_strdup ( constname );
285       gtk_widget_destroy ( dialog );
286       return name;
287     }
288   }
289   gtk_widget_destroy ( dialog );
290   return NULL;
291 }
292
293 static void today_clicked (GtkWidget *cal)
294 {
295   GDateTime *now = g_date_time_new_now_local ();
296   gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(now)-1, g_date_time_get_year(now) );
297   gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(now) );
298   g_date_time_unref ( now );
299 }
300
301 /**
302  * a_dialog_get_date:
303  *
304  * Returns: a date as a string - always in ISO8601 format (YYYY-MM-DD)
305  *  This string can be NULL (especially when the dialog is cancelled)
306  *  Free the string after use
307  */
308 gchar *a_dialog_get_date ( GtkWindow *parent, const gchar *title )
309 {
310   GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
311                                                     parent,
312                                                     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
313                                                     GTK_STOCK_CANCEL,
314                                                     GTK_RESPONSE_REJECT,
315                                                     GTK_STOCK_OK,
316                                                     GTK_RESPONSE_ACCEPT,
317                                                     NULL);
318   GtkWidget *cal = gtk_calendar_new ();
319   GtkWidget *today = gtk_button_new_with_label ( _("Today") );
320
321   static guint year = 0;
322   static guint month = 0;
323   static guint day = 0;
324
325   if ( year != 0 ) {
326     // restore the last selected date
327     gtk_calendar_select_month ( GTK_CALENDAR(cal), month, year );
328     gtk_calendar_select_day ( GTK_CALENDAR(cal), day );
329   }
330
331   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), today, FALSE, FALSE, 0);
332   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0);
333
334   g_signal_connect_swapped ( G_OBJECT(today), "clicked", G_CALLBACK(today_clicked), cal );
335
336   gtk_widget_show_all ( dialog );
337
338   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
339
340   gchar *date_str = NULL;
341   if ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
342   {
343     gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day );
344     date_str = g_strdup_printf ( "%d-%02d-%02d", year, month+1, day );
345   }
346   gtk_widget_destroy ( dialog );
347   return date_str;
348 }
349
350 /* creates a vbox full of labels */
351 GtkWidget *a_dialog_create_label_vbox ( gchar **texts, int label_count, gint spacing, gint padding )
352 {
353   GtkWidget *vbox, *label;
354   int i;
355   vbox = gtk_vbox_new( TRUE, spacing );
356
357   for ( i = 0; i < label_count; i++ )
358   {
359     label = gtk_label_new(NULL);
360     gtk_label_set_markup ( GTK_LABEL(label), _(texts[i]) );
361     if ( strchr(texts[i], ':') ) {
362       // Align label to the right
363       gtk_misc_set_alignment ( GTK_MISC(label), 1.0, 0.5 );
364     }
365     gtk_box_pack_start ( GTK_BOX(vbox), label, FALSE, TRUE, padding );
366   }
367   return vbox;
368 }
369
370 gboolean a_dialog_yes_or_no ( GtkWindow *parent, const gchar *message, const gchar *extra )
371 {
372   GtkWidget *dia;
373   dia = gtk_message_dialog_new ( parent,
374                                  GTK_DIALOG_DESTROY_WITH_PARENT,
375                                  GTK_MESSAGE_QUESTION,
376                                  GTK_BUTTONS_YES_NO,
377                                  message, extra );
378
379   if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_YES )
380   {
381     gtk_widget_destroy ( dia );
382     return TRUE;
383   }
384   else
385   {
386     gtk_widget_destroy ( dia );
387     return FALSE;
388   }
389 }
390
391 static void zoom_spin_changed ( GtkSpinButton *spin, GtkWidget *pass_along[3] )
392 {
393   if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(pass_along[2]) ) )
394     gtk_spin_button_set_value ( 
395         GTK_SPIN_BUTTON(pass_along[GTK_WIDGET(spin) == pass_along[0] ? 1 : 0]),
396         gtk_spin_button_get_value ( spin ) );
397 }
398
399 gboolean a_dialog_custom_zoom ( GtkWindow *parent, gdouble *xmpp, gdouble *ympp )
400 {
401   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Zoom Factors..."),
402                                                   parent,
403                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
404                                                   GTK_STOCK_CANCEL,
405                                                   GTK_RESPONSE_REJECT,
406                                                   GTK_STOCK_OK,
407                                                   GTK_RESPONSE_ACCEPT,
408                                                   NULL);
409   GtkWidget *table, *label, *xlabel, *xspin, *ylabel, *yspin, *samecheck;
410   GtkWidget *pass_along[3];
411
412   table = gtk_table_new ( 4, 2, FALSE );
413   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
414
415   label = gtk_label_new ( _("Zoom factor (in meters per pixel):") );
416   xlabel = gtk_label_new ( _("X (easting): "));
417   ylabel = gtk_label_new ( _("Y (northing): "));
418
419   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 );
420   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 );
421
422   pass_along[2] = samecheck = gtk_check_button_new_with_label ( _("X and Y zoom factors must be equal") );
423   /* TODO -- same factor */
424   /*  samecheck = gtk_check_button_new_with_label ( "Same x/y zoom factor" ); */
425
426   if ( *xmpp == *ympp )
427     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(samecheck), TRUE );
428
429   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
430   gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 1, 2 );
431   gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 1, 2 );
432   gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 2, 3 );
433   gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 2, 3 );
434   gtk_table_attach_defaults ( GTK_TABLE(table), samecheck, 0, 2, 3, 4 );
435
436   gtk_widget_show_all ( table );
437
438   g_signal_connect ( G_OBJECT(xspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
439   g_signal_connect ( G_OBJECT(yspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
440
441   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
442
443   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
444   {
445     *xmpp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) );
446     *ympp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) );
447     gtk_widget_destroy ( dialog );
448     return TRUE;
449   }
450   gtk_widget_destroy ( dialog );
451   return FALSE;
452 }
453
454 static void split_spin_focused ( GtkSpinButton *spin, GtkWidget *pass_along[1] )
455 {
456   gtk_toggle_button_set_active    (GTK_TOGGLE_BUTTON(pass_along[0]), 1);
457 }
458
459 gboolean a_dialog_time_threshold ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint *thr )
460 {
461   GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text, 
462                                                   parent,
463                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
464                                                   GTK_STOCK_CANCEL,
465                                                   GTK_RESPONSE_REJECT,
466                                                   GTK_STOCK_OK,
467                                                   GTK_RESPONSE_ACCEPT,
468                                                   NULL);
469   GtkWidget *table, *t1, *t2, *t3, *t4, *spin, *label;
470   GtkWidget *pass_along[1];
471
472   table = gtk_table_new ( 4, 2, FALSE );
473   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
474
475   label = gtk_label_new (label_text);
476
477   t1 = gtk_radio_button_new_with_label ( NULL, _("1 min") );
478   t2 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t1), _("1 hour") );
479   t3 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t2), _("1 day") );
480   t4 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t3), _("Custom (in minutes):") );
481
482   pass_along[0] = t4;
483
484   spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *thr, 0, 65536, 1, 5, 0 ), 1, 0 );
485
486   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
487   gtk_table_attach_defaults ( GTK_TABLE(table), t1, 0, 1, 1, 2 );
488   gtk_table_attach_defaults ( GTK_TABLE(table), t2, 0, 1, 2, 3 );
489   gtk_table_attach_defaults ( GTK_TABLE(table), t3, 0, 1, 3, 4 );
490   gtk_table_attach_defaults ( GTK_TABLE(table), t4, 0, 1, 4, 5 );
491   gtk_table_attach_defaults ( GTK_TABLE(table), spin, 1, 2, 4, 5 );
492
493   gtk_widget_show_all ( table );
494
495   g_signal_connect ( G_OBJECT(spin), "grab-focus", G_CALLBACK(split_spin_focused), pass_along );
496
497   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
498
499   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
500   {
501     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t1))) {
502       *thr = 1;
503     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t2))) {
504       *thr = 60;
505     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t3))) {
506       *thr = 60 * 24;
507     } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t4))) {
508       *thr = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
509     }
510     gtk_widget_destroy ( dialog );
511     return TRUE;
512   }
513   gtk_widget_destroy ( dialog );
514   return FALSE;
515 }
516
517 /**
518  * a_dialog_get_positive_number:
519  * 
520  * Dialog to return a positive number via a spinbox within the supplied limits
521  * 
522  * Returns: A value of zero indicates the dialog was cancelled
523  */
524 guint a_dialog_get_positive_number ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint default_num, guint min, guint max, guint step )
525 {
526   GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
527                                                    parent,
528                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
529                                                    GTK_STOCK_CANCEL,
530                                                    GTK_RESPONSE_REJECT,
531                                                    GTK_STOCK_OK,
532                                                    GTK_RESPONSE_ACCEPT,
533                                                    NULL);
534   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
535   GtkWidget *response_w = NULL;
536 #if GTK_CHECK_VERSION (2, 20, 0)
537   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
538 #endif
539
540   GtkWidget *table, *spin, *label;
541   guint result = default_num;
542
543   table = gtk_table_new ( 2, 1, FALSE );
544   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
545
546   label = gtk_label_new (label_text);
547   spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( default_num, min, max, step, 5, 0 ), 1, 0 );
548
549   gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 1, 0, 1 );
550   gtk_table_attach_defaults ( GTK_TABLE(table), spin, 0, 1, 1, 2 );
551
552   if ( response_w )
553     gtk_widget_grab_focus ( response_w );
554
555   gtk_widget_show_all ( table );
556
557   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
558   {
559     result = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
560     gtk_widget_destroy ( dialog );
561     return result;
562   }
563
564   // Dialog cancelled
565   gtk_widget_destroy ( dialog );
566   return 0;
567 }
568
569 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
570 static void about_url_hook (GtkAboutDialog *about,
571                             const gchar    *link,
572                             gpointer        data)
573 {
574   open_url (GTK_WINDOW(about), link);
575 }
576
577 static void about_email_hook (GtkAboutDialog *about,
578                               const gchar    *email,
579                               gpointer        data)
580 {
581   new_email (GTK_WINDOW(about), email);
582 }
583 #endif
584
585 /**
586  *  Creates a dialog with list of text
587  *  Mostly useful for longer messages that have several lines of information.
588  */
589 void a_dialog_list ( GtkWindow *parent, const gchar *title, GArray *array, gint padding )
590 {
591   GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
592                                                     parent,
593                                                     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
594                                                     GTK_STOCK_CLOSE,
595                                                     GTK_RESPONSE_CLOSE,
596                                                     NULL);
597
598   GtkBox *vbox = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
599   GtkWidget *label;
600
601   for ( int i = 0; i < array->len; i++ ) {
602     label = ui_label_new_selectable (NULL);
603     gtk_label_set_markup ( GTK_LABEL(label), g_array_index(array,gchar*,i) );
604     gtk_box_pack_start ( GTK_BOX(vbox), label, FALSE, TRUE, padding );
605   }
606
607   gtk_widget_show_all ( dialog );
608   gtk_dialog_run ( GTK_DIALOG(dialog) );
609   gtk_widget_destroy ( dialog );
610 }
611
612 void a_dialog_about ( GtkWindow *parent )
613 {
614   const gchar *version = VIKING_VERSION;
615   const gchar *website = VIKING_URL;
616   gchar *copyright = g_strdup_printf(_("2003-2008, Evan Battaglia\n2008-%s, Viking's contributors"), THEYEAR);
617   const gchar *comments = _("GPS Data and Topo Analyzer, Explorer, and Manager.");
618   const gchar *license = _("This program is free software; you can redistribute it and/or modify "
619                         "it under the terms of the GNU General Public License as published by "
620                         "the Free Software Foundation; either version 2 of the License, or "
621                         "(at your option) any later version."
622                         "\n\n"
623                         "This program is distributed in the hope that it will be useful, "
624                         "but WITHOUT ANY WARRANTY; without even the implied warranty of "
625                         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
626                         "GNU General Public License for more details."
627                         "\n\n"
628                         "You should have received a copy of the GNU General Public License "
629                         "along with this program; if not, write to the Free Software "
630                         "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA");
631
632   // Would be nice to use gtk_about_dialog_add_credit_section (), but that requires gtk 3.4
633   // For now shove it in the 'artists' section so at least the information is easily visible
634   // Something more advanced might have proper version information too...
635   const gchar *libs[] = {
636     "Compiled in libraries:",
637     // Default libs
638     "libglib-2.0",
639     "libgthread-2.0",
640     "libgtk+-2.0",
641     "libgio-2.0",
642     // Potentially optional libs (but probably couldn't build without them)
643 #ifdef HAVE_LIBM
644     "libm",
645 #endif
646 #ifdef HAVE_LIBZ
647     "libz",
648 #endif
649 #ifdef HAVE_LIBCURL
650     "libcurl",
651 #endif
652 #ifdef HAVE_EXPAT_H
653     "libexpat",
654 #endif
655     // Actually optional libs
656 #ifdef HAVE_LIBGPS
657     "libgps",
658 #endif
659 #ifdef HAVE_LIBGEXIV2
660     "libgexiv2",
661 #endif
662 #ifdef HAVE_LIBEXIF
663     "libexif",
664 #endif
665 #ifdef HAVE_LIBX11
666     "libX11",
667 #endif
668 #ifdef HAVE_LIBMAGIC
669     "libmagic",
670 #endif
671 #ifdef HAVE_LIBBZ2
672     "libbz2",
673 #endif
674 #ifdef HAVE_LIBZIP
675     "libzip",
676 #endif
677 #ifdef HAVE_LIBSQLITE3
678     "libsqlite3",
679 #endif
680 #ifdef HAVE_LIBMAPNIK
681     "libmapnik",
682 #endif
683 #ifdef HAVE_LIBNETTLE
684     "libnettle",
685 #endif
686     NULL
687   };
688   // Newer versions of GTK 'just work', calling gtk_show_uri() on the URL or email and opens up the appropriate program
689   // This is the old method:
690 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
691   gtk_about_dialog_set_url_hook (about_url_hook, NULL, NULL);
692   gtk_about_dialog_set_email_hook (about_email_hook, NULL, NULL);
693 #endif
694
695   gtk_show_about_dialog (parent,
696         "version", version,
697         "website", website,
698         "comments", comments,
699         "copyright", copyright,
700         "license", license,
701         "wrap-license", TRUE,
702         /* logo automatically retrieved via gtk_window_get_default_icon_list */
703         "authors", AUTHORS,
704         "documenters", DOCUMENTERS,
705         "translator-credits", _("Translation is coordinated on http://launchpad.net/viking"),
706         "artists", libs,
707         NULL);
708   g_free(copyright);
709 }
710
711 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)
712 {
713   gchar **s;
714
715   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 );
716   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
717   GtkWidget *response_w = NULL;
718 #if GTK_CHECK_VERSION (2, 20, 0)
719   response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
720 #endif
721
722   GtkWidget *map_label = gtk_label_new(_("Map type:"));
723   GtkWidget *map_combo = vik_combo_box_text_new();
724   for (s = mapnames; *s; s++)
725     vik_combo_box_text_append (GTK_COMBO_BOX(map_combo), *s);
726   gtk_combo_box_set_active (GTK_COMBO_BOX(map_combo), default_map);
727
728   GtkWidget *zoom_label = gtk_label_new(_("Zoom level:"));
729   GtkWidget *zoom_combo = vik_combo_box_text_new();
730   for (s = zoom_list; *s; s++)
731     vik_combo_box_text_append (GTK_COMBO_BOX(zoom_combo), *s);
732   gtk_combo_box_set_active (GTK_COMBO_BOX(zoom_combo), default_zoom);
733
734   GtkTable *box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
735   gtk_table_attach_defaults(box, map_label, 0, 1, 0, 1);
736   gtk_table_attach_defaults(box, map_combo, 1, 2, 0, 1);
737   gtk_table_attach_defaults(box, zoom_label, 0, 1, 1, 2);
738   gtk_table_attach_defaults(box, zoom_combo, 1, 2, 1, 2);
739
740   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(box), FALSE, FALSE, 5 );
741
742   if ( response_w )
743     gtk_widget_grab_focus ( response_w );
744
745   gtk_widget_show_all ( dialog );
746   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
747     gtk_widget_destroy(dialog);
748     return FALSE;
749   }
750
751   *selected_map = gtk_combo_box_get_active(GTK_COMBO_BOX(map_combo));
752   *selected_zoom = gtk_combo_box_get_active(GTK_COMBO_BOX(zoom_combo));
753
754   gtk_widget_destroy(dialog);
755   return TRUE;
756 }
757
758 /**
759  * Display a dialog presenting the license of a map.
760  * Allow to read the license by launching a web browser.
761  */
762 void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *license, const gchar *url)
763 {
764   GtkWidget *dialog = gtk_message_dialog_new (parent,
765                                  GTK_DIALOG_DESTROY_WITH_PARENT,
766                                  GTK_MESSAGE_INFO,
767                                  GTK_BUTTONS_OK,
768                                  _("The map data is licensed: %s."),
769                                  license);
770   gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
771     _("The data provided by '<b>%s</b>' are licensed under the following license: <b>%s</b>."),
772     map, license);
773 #define RESPONSE_OPEN_LICENSE 600
774   if (url != NULL) {
775     gtk_dialog_add_button (GTK_DIALOG (dialog), _("Open license"), RESPONSE_OPEN_LICENSE);
776   }
777   gint response;
778   do {
779     response = gtk_dialog_run (GTK_DIALOG (dialog));
780     if (response == RESPONSE_OPEN_LICENSE) {
781       open_url (parent, url);
782     }
783   } while (response != GTK_RESPONSE_DELETE_EVENT && response != GTK_RESPONSE_OK);
784   gtk_widget_destroy (dialog);
785 }