2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
6 * Copyright (C) 2010-2014, Rob Norris <rw_norris@hotmail.com>
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.
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.
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
29 #include "thumbnails.h"
30 #include "garminsymbols.h"
31 #include "degrees_converters.h"
33 #include "documenters.h"
37 #include "geotag_exif.h"
38 #include "vikdatetime_edit_dialog.h"
40 #include <glib/gi18n.h>
47 void a_dialog_msg ( GtkWindow *parent, gint type, const gchar *info, const gchar *extra )
49 GtkWidget *msgbox = gtk_message_dialog_new ( parent, GTK_DIALOG_DESTROY_WITH_PARENT, type, GTK_BUTTONS_OK, info, extra );
50 gtk_dialog_run ( GTK_DIALOG(msgbox) );
51 gtk_widget_destroy ( msgbox );
54 gboolean a_dialog_goto_latlon ( GtkWindow *parent, struct LatLon *ll, const struct LatLon *old )
56 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to Lat/Lon"),
58 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
64 GtkWidget *latlabel, *lonlabel;
66 gchar *tmp_lat, *tmp_lon;
68 latlabel = gtk_label_new (_("Latitude:"));
69 lat = gtk_entry_new ();
70 tmp_lat = g_strdup_printf ( "%f", old->lat );
71 gtk_entry_set_text ( GTK_ENTRY(lat), tmp_lat );
74 lonlabel = gtk_label_new (_("Longitude:"));
75 lon = gtk_entry_new ();
76 tmp_lon = g_strdup_printf ( "%f", old->lon );
77 gtk_entry_set_text ( GTK_ENTRY(lon), tmp_lon );
80 gtk_widget_show ( latlabel );
81 gtk_widget_show ( lonlabel );
82 gtk_widget_show ( lat );
83 gtk_widget_show ( lon );
85 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
86 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lat, FALSE, FALSE, 0);
87 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
88 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lon, FALSE, FALSE, 0);
90 // 'ok' when press return in the entry
91 g_signal_connect_swapped (lat, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
92 g_signal_connect_swapped (lon, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
94 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
96 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
98 ll->lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lat) ) );
99 ll->lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lon) ) );
100 gtk_widget_destroy ( dialog );
104 gtk_widget_destroy ( dialog );
108 gboolean a_dialog_goto_utm ( GtkWindow *parent, struct UTM *utm, const struct UTM *old )
110 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to UTM"),
112 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
118 GtkWidget *norlabel, *easlabel, *nor, *eas;
119 GtkWidget *zonehbox, *zonespin, *letterentry;
120 gchar *tmp_eas, *tmp_nor;
123 norlabel = gtk_label_new (_("Northing:"));
124 nor = gtk_entry_new ();
125 tmp_nor = g_strdup_printf("%ld", (long) old->northing );
126 gtk_entry_set_text ( GTK_ENTRY(nor), tmp_nor );
129 easlabel = gtk_label_new (_("Easting:"));
130 eas = gtk_entry_new ();
131 tmp_eas = g_strdup_printf("%ld", (long) old->easting );
132 gtk_entry_set_text ( GTK_ENTRY(eas), tmp_eas );
135 zonehbox = gtk_hbox_new ( FALSE, 0 );
136 gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Zone:") ), FALSE, FALSE, 5 );
137 zonespin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( old->zone, 1, 60, 1, 5, 0 ), 1, 0 );
138 gtk_box_pack_start ( GTK_BOX(zonehbox), zonespin, TRUE, TRUE, 5 );
139 gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Letter:") ), FALSE, FALSE, 5 );
140 letterentry = gtk_entry_new ();
141 gtk_entry_set_max_length ( GTK_ENTRY(letterentry), 1 );
142 gtk_entry_set_width_chars ( GTK_ENTRY(letterentry), 2 );
143 tmp_letter[0] = old->letter;
144 tmp_letter[1] = '\0';
145 gtk_entry_set_text ( GTK_ENTRY(letterentry), tmp_letter );
146 gtk_box_pack_start ( GTK_BOX(zonehbox), letterentry, FALSE, FALSE, 5 );
148 gtk_widget_show ( norlabel );
149 gtk_widget_show ( easlabel );
150 gtk_widget_show ( nor );
151 gtk_widget_show ( eas );
153 gtk_widget_show_all ( zonehbox );
155 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), norlabel, FALSE, FALSE, 0);
156 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nor, FALSE, FALSE, 0);
157 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), easlabel, FALSE, FALSE, 0);
158 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), eas, FALSE, FALSE, 0);
159 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), zonehbox, FALSE, FALSE, 0);
161 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
163 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
166 utm->northing = atof ( gtk_entry_get_text ( GTK_ENTRY(nor) ) );
167 utm->easting = atof ( gtk_entry_get_text ( GTK_ENTRY(eas) ) );
168 utm->zone = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(zonespin) );
169 letter = gtk_entry_get_text ( GTK_ENTRY(letterentry) );
171 utm->letter = toupper(*letter);
172 gtk_widget_destroy ( dialog );
176 gtk_widget_destroy ( dialog );
180 void a_dialog_response_accept ( GtkDialog *dialog )
182 gtk_dialog_response ( dialog, GTK_RESPONSE_ACCEPT );
185 static void update_time ( GtkWidget *widget, VikWaypoint *wp )
187 gchar *msg = vu_get_time_string ( &(wp->timestamp), "%c", &(wp->coord), NULL );
188 gtk_button_set_label ( GTK_BUTTON(widget), msg );
192 static VikWaypoint *edit_wp;
194 static void time_edit_click ( GtkWidget *widget, VikWaypoint *wp )
196 GTimeZone *gtz = g_time_zone_new_local ();
197 time_t mytime = vik_datetime_edit_dialog ( GTK_WINDOW(gtk_widget_get_toplevel(widget)),
201 g_time_zone_unref ( gtz );
203 // Was the dialog cancelled?
207 // Otherwise use new value in the edit buffer
208 edit_wp->timestamp = mytime;
210 // Clear the previous 'Add' image as now a time is set
211 if ( gtk_button_get_image ( GTK_BUTTON(widget) ) )
212 gtk_button_set_image ( GTK_BUTTON(widget), NULL );
214 update_time ( widget, edit_wp );
217 static void symbol_entry_changed_cb(GtkWidget *combo, GtkListStore *store)
222 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter))
225 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
226 /* Note: symm is NULL when "(none)" is select (first cell is empty) */
227 gtk_widget_set_tooltip_text(combo, sym);
231 /* Specify if a new waypoint or not */
232 /* If a new waypoint then it uses the default_name for the suggested name allowing the user to change it.
233 The name to use is returned
235 /* todo: less on this side, like add track */
236 gchar *a_dialog_waypoint ( GtkWindow *parent, gchar *default_name, VikTrwLayer *vtl, VikWaypoint *wp, VikCoordMode coord_mode, gboolean is_new, gboolean *updated )
238 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Waypoint Properties"),
240 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
247 GtkWidget *latlabel, *lonlabel, *namelabel, *latentry, *lonentry, *altentry, *altlabel, *nameentry=NULL;
248 GtkWidget *commentlabel, *commententry, *descriptionlabel, *descriptionentry, *imagelabel, *imageentry, *symbollabel, *symbolentry;
249 GtkWidget *timelabel = NULL;
250 GtkWidget *timevaluebutton = NULL;
251 GtkWidget *hasGeotagCB = NULL;
252 GtkWidget *consistentGeotagCB = NULL;
255 gchar *lat, *lon, *alt;
257 vik_coord_to_latlon ( &(wp->coord), &ll );
259 lat = g_strdup_printf ( "%f", ll.lat );
260 lon = g_strdup_printf ( "%f", ll.lon );
261 vik_units_height_t height_units = a_vik_get_units_height ();
262 switch (height_units) {
263 case VIK_UNITS_HEIGHT_METRES:
264 alt = g_strdup_printf ( "%f", wp->altitude );
266 case VIK_UNITS_HEIGHT_FEET:
267 alt = g_strdup_printf ( "%f", VIK_METERS_TO_FEET(wp->altitude) );
270 alt = g_strdup_printf ( "%f", wp->altitude );
271 g_critical("Houston, we've had a problem. height=%d", height_units);
276 namelabel = gtk_label_new (_("Name:"));
277 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), namelabel, FALSE, FALSE, 0);
278 // Name is now always changeable
279 nameentry = gtk_entry_new ();
281 gtk_entry_set_text( GTK_ENTRY(nameentry), default_name );
282 g_signal_connect_swapped ( nameentry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
283 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nameentry, FALSE, FALSE, 0);
285 latlabel = gtk_label_new (_("Latitude:"));
286 latentry = gtk_entry_new ();
287 gtk_entry_set_text ( GTK_ENTRY(latentry), lat );
290 lonlabel = gtk_label_new (_("Longitude:"));
291 lonentry = gtk_entry_new ();
292 gtk_entry_set_text ( GTK_ENTRY(lonentry), lon );
295 altlabel = gtk_label_new (_("Altitude:"));
296 altentry = gtk_entry_new ();
297 gtk_entry_set_text ( GTK_ENTRY(altentry), alt );
300 commentlabel = gtk_label_new (_("Comment:"));
301 commententry = gtk_entry_new ();
303 // Auto put in some kind of 'name' as a comment if one previously 'goto'ed this exact location
304 cmt = a_vik_goto_get_search_string_for_this_place(VIK_WINDOW(parent));
306 gtk_entry_set_text(GTK_ENTRY(commententry), cmt);
308 descriptionlabel = gtk_label_new (_("Description:"));
309 descriptionentry = gtk_entry_new ();
311 imagelabel = gtk_label_new (_("Image:"));
312 imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN, VF_FILTER_IMAGE);
316 symbollabel = gtk_label_new (_("Symbol:"));
319 store = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING);
320 symbolentry = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
321 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(symbolentry), 6);
323 g_signal_connect(symbolentry, "changed", G_CALLBACK(symbol_entry_changed_cb), store);
324 gtk_list_store_append (store, &iter);
325 gtk_list_store_set (store, &iter, 0, NULL, 1, NULL, 2, _("(none)"), -1);
326 a_populate_sym_list(store);
328 r = gtk_cell_renderer_pixbuf_new ();
329 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
330 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "pixbuf", 1, NULL);
332 r = gtk_cell_renderer_text_new ();
333 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
334 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "text", 2, NULL);
336 if ( !is_new && wp->symbol ) {
339 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)) {
340 gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
341 if (sym && !strcmp(sym, wp->symbol)) {
348 // Ensure is it a valid symbol in the given symbol set (large vs small)
349 // Not all symbols are available in both
350 // The check prevents a Gtk Critical message
352 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(symbolentry), &iter);
356 if ( !is_new && wp->comment )
357 gtk_entry_set_text ( GTK_ENTRY(commententry), wp->comment );
359 if ( !is_new && wp->description )
360 gtk_entry_set_text ( GTK_ENTRY(descriptionentry), wp->description );
362 if ( !is_new && wp->image ) {
363 vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), wp->image );
365 // Geotag Info [readonly]
366 hasGeotagCB = gtk_check_button_new_with_label ( _("Has Geotag") );
367 gtk_widget_set_sensitive ( hasGeotagCB, FALSE );
369 gchar *ignore = a_geotag_get_exif_date_from_file ( wp->image, &hasGeotag );
371 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(hasGeotagCB), hasGeotag );
373 consistentGeotagCB = gtk_check_button_new_with_label ( _("Consistent Position") );
374 gtk_widget_set_sensitive ( consistentGeotagCB, FALSE );
376 struct LatLon ll = a_geotag_get_position ( wp->image );
378 vik_coord_load_from_latlon ( &coord, coord_mode, &ll );
379 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(consistentGeotagCB), vik_coord_equals(&coord, &wp->coord) );
383 timelabel = gtk_label_new ( _("Time:") );
384 timevaluebutton = gtk_button_new();
385 gtk_button_set_relief ( GTK_BUTTON(timevaluebutton), GTK_RELIEF_NONE );
388 edit_wp = vik_waypoint_new ();
389 edit_wp = vik_waypoint_copy ( wp );
391 // TODO: Consider if there should be a remove time button...
393 if ( !is_new && wp->has_timestamp ) {
394 update_time ( timevaluebutton, wp );
397 GtkWidget *img = gtk_image_new_from_stock ( GTK_STOCK_ADD, GTK_ICON_SIZE_MENU );
398 gtk_button_set_image ( GTK_BUTTON(timevaluebutton), img );
399 // Initially use current time or otherwise whatever the last value used was
400 if ( edit_wp->timestamp == 0 ) {
401 time ( &edit_wp->timestamp );
404 g_signal_connect ( G_OBJECT(timevaluebutton), "clicked", G_CALLBACK(time_edit_click), edit_wp );
406 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
407 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latentry, FALSE, FALSE, 0);
408 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
409 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonentry, FALSE, FALSE, 0);
410 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timelabel, FALSE, FALSE, 0);
411 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timevaluebutton, FALSE, FALSE, 0);
412 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altlabel, FALSE, FALSE, 0);
413 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altentry, FALSE, FALSE, 0);
414 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commentlabel, FALSE, FALSE, 0);
415 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commententry, FALSE, FALSE, 0);
416 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionlabel, FALSE, FALSE, 0);
417 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionentry, FALSE, FALSE, 0);
418 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imagelabel, FALSE, FALSE, 0);
419 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imageentry, FALSE, FALSE, 0);
421 GtkWidget *hbox = gtk_hbox_new ( FALSE, 0 );
422 gtk_box_pack_start (GTK_BOX(hbox), hasGeotagCB, FALSE, FALSE, 0);
423 gtk_box_pack_start (GTK_BOX(hbox), consistentGeotagCB, FALSE, FALSE, 0);
424 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox, FALSE, FALSE, 0);
426 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), symbollabel, FALSE, FALSE, 0);
427 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(symbolentry), FALSE, FALSE, 0);
429 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
431 gtk_widget_show_all ( gtk_dialog_get_content_area(GTK_DIALOG(dialog)) );
434 // Shift left<->right to try not to obscure the waypoint.
435 trw_layer_dialog_shift ( vtl, GTK_WINDOW(dialog), &(wp->coord), FALSE );
438 while ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
440 if ( strlen((gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) )) == 0 ) /* TODO: other checks (isalpha or whatever ) */
441 a_dialog_info_msg ( parent, _("Please enter a name for the waypoint.") );
443 // NB: No check for unique names - this allows generation of same named entries.
444 gchar *entered_name = g_strdup ( (gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) ) );
447 ll.lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(latentry) ) );
448 ll.lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lonentry) ) );
449 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &ll );
450 // Always store in metres
451 switch (height_units) {
452 case VIK_UNITS_HEIGHT_METRES:
453 wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
455 case VIK_UNITS_HEIGHT_FEET:
456 wp->altitude = VIK_FEET_TO_METERS(atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) ));
459 wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
460 g_critical("Houston, we've had a problem. height=%d", height_units);
462 if ( g_strcmp0 ( wp->comment, gtk_entry_get_text ( GTK_ENTRY(commententry) ) ) )
463 vik_waypoint_set_comment ( wp, gtk_entry_get_text ( GTK_ENTRY(commententry) ) );
464 if ( g_strcmp0 ( wp->description, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) ) )
465 vik_waypoint_set_description ( wp, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) );
466 if ( g_strcmp0 ( wp->image, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) ) )
467 vik_waypoint_set_image ( wp, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) );
468 if ( wp->image && *(wp->image) && (!a_thumbnails_exists(wp->image)) )
469 a_thumbnails_create ( wp->image );
470 if ( edit_wp->timestamp ) {
471 wp->timestamp = edit_wp->timestamp;
472 wp->has_timestamp = TRUE;
475 GtkTreeIter iter, first;
476 gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(store), &first );
477 if ( !gtk_combo_box_get_active_iter ( GTK_COMBO_BOX(symbolentry), &iter ) || !memcmp(&iter, &first, sizeof(GtkTreeIter)) ) {
478 vik_waypoint_set_symbol ( wp, NULL );
481 gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
482 vik_waypoint_set_symbol ( wp, sym );
486 gtk_widget_destroy ( dialog );
491 // See if name has been changed
492 if ( g_strcmp0 (default_name, entered_name ) )
499 gtk_widget_destroy ( dialog );
503 static void get_selected_foreach_func(GtkTreeModel *model,
510 gtk_tree_model_get (model, iter, 0, &name, -1);
511 *list = g_list_prepend(*list, name);
514 GList *a_dialog_select_from_list ( GtkWindow *parent, GList *names, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg )
517 GtkCellRenderer *renderer;
520 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
522 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
528 /* When something is selected then OK */
529 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
530 GtkWidget *response_w = NULL;
531 #if GTK_CHECK_VERSION (2, 20, 0)
532 /* Default to not apply - as initially nothing is selected! */
533 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
535 GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
537 GtkWidget *scrolledwindow;
539 GList *runner = names;
542 gtk_list_store_append(store, &iter);
543 gtk_list_store_set(store, &iter, 0, runner->data, -1);
544 runner = g_list_next(runner);
547 view = gtk_tree_view_new();
548 renderer = gtk_cell_renderer_text_new();
549 // Use the column header to display the message text,
550 // this makes the overall widget allocation simple as treeview takes up all the space
551 GtkTreeViewColumn *column;
552 column = gtk_tree_view_column_new_with_attributes (msg, renderer, "text", 0, NULL );
553 gtk_tree_view_column_set_sort_column_id (column, 0);
554 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
556 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
557 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
558 multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
559 g_object_unref(store);
561 scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
562 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
563 gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
565 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0 );
566 // Ensure a reasonable number of items are shown, but let the width be automatically sized
567 gtk_widget_set_size_request ( dialog, -1, 400) ;
569 gtk_widget_show_all ( dialog );
572 gtk_widget_grab_focus ( response_w );
574 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
576 GList *names_selected = NULL;
577 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
578 gtk_tree_selection_selected_foreach(selection, get_selected_foreach_func, &names_selected);
581 gtk_widget_destroy ( dialog );
582 return names_selected;
584 a_dialog_error_msg(parent, _("Nothing was selected"));
586 gtk_widget_destroy ( dialog );
590 gchar *a_dialog_new_track ( GtkWindow *parent, gchar *default_name, gboolean is_route )
592 GtkWidget *dialog = gtk_dialog_new_with_buttons ( is_route ? _("Add Route") : _("Add Track"),
594 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
600 GtkWidget *label = gtk_label_new ( is_route ? _("Route Name:") : _("Track Name:") );
601 GtkWidget *entry = gtk_entry_new ();
604 gtk_entry_set_text ( GTK_ENTRY(entry), default_name );
606 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
607 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), entry, FALSE, FALSE, 0);
609 g_signal_connect_swapped ( entry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
611 gtk_widget_show ( label );
612 gtk_widget_show ( entry );
614 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
616 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
618 const gchar *constname = gtk_entry_get_text ( GTK_ENTRY(entry) );
619 if ( *constname == '\0' )
620 a_dialog_info_msg ( parent, _("Please enter a name for the track.") );
622 gchar *name = g_strdup ( constname );
623 gtk_widget_destroy ( dialog );
627 gtk_widget_destroy ( dialog );
631 static void today_clicked (GtkWidget *cal)
633 GDateTime *now = g_date_time_new_now_local ();
634 gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(now)-1, g_date_time_get_year(now) );
635 gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(now) );
636 g_date_time_unref ( now );
642 * Returns: a date as a string - always in ISO8601 format (YYYY-MM-DD)
643 * This string can be NULL (especially when the dialog is cancelled)
644 * Free the string after use
646 gchar *a_dialog_get_date ( GtkWindow *parent, const gchar *title )
648 GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
650 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
656 GtkWidget *cal = gtk_calendar_new ();
657 GtkWidget *today = gtk_button_new_with_label ( _("Today") );
659 static guint year = 0;
660 static guint month = 0;
661 static guint day = 0;
664 // restore the last selected date
665 gtk_calendar_select_month ( GTK_CALENDAR(cal), month, year );
666 gtk_calendar_select_day ( GTK_CALENDAR(cal), day );
669 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), today, FALSE, FALSE, 0);
670 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0);
672 g_signal_connect_swapped ( G_OBJECT(today), "clicked", G_CALLBACK(today_clicked), cal );
674 gtk_widget_show_all ( dialog );
676 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
678 gchar *date_str = NULL;
679 if ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
681 gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day );
682 date_str = g_strdup_printf ( "%d-%02d-%02d", year, month+1, day );
684 gtk_widget_destroy ( dialog );
688 /* creates a vbox full of labels */
689 GtkWidget *a_dialog_create_label_vbox ( gchar **texts, int label_count, gint spacing, gint padding )
691 GtkWidget *vbox, *label;
693 vbox = gtk_vbox_new( TRUE, spacing );
695 for ( i = 0; i < label_count; i++ )
697 label = gtk_label_new(NULL);
698 gtk_label_set_markup ( GTK_LABEL(label), _(texts[i]) );
699 gtk_box_pack_start ( GTK_BOX(vbox), label, FALSE, TRUE, padding );
704 gboolean a_dialog_yes_or_no ( GtkWindow *parent, const gchar *message, const gchar *extra )
707 dia = gtk_message_dialog_new ( parent,
708 GTK_DIALOG_DESTROY_WITH_PARENT,
709 GTK_MESSAGE_QUESTION,
713 if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_YES )
715 gtk_widget_destroy ( dia );
720 gtk_widget_destroy ( dia );
725 static void zoom_spin_changed ( GtkSpinButton *spin, GtkWidget *pass_along[3] )
727 if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(pass_along[2]) ) )
728 gtk_spin_button_set_value (
729 GTK_SPIN_BUTTON(pass_along[GTK_WIDGET(spin) == pass_along[0] ? 1 : 0]),
730 gtk_spin_button_get_value ( spin ) );
733 gboolean a_dialog_custom_zoom ( GtkWindow *parent, gdouble *xmpp, gdouble *ympp )
735 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Zoom Factors..."),
737 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
743 GtkWidget *table, *label, *xlabel, *xspin, *ylabel, *yspin, *samecheck;
744 GtkWidget *pass_along[3];
746 table = gtk_table_new ( 4, 2, FALSE );
747 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
749 label = gtk_label_new ( _("Zoom factor (in meters per pixel):") );
750 xlabel = gtk_label_new ( _("X (easting): "));
751 ylabel = gtk_label_new ( _("Y (northing): "));
753 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 );
754 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 );
756 pass_along[2] = samecheck = gtk_check_button_new_with_label ( _("X and Y zoom factors must be equal") );
757 /* TODO -- same factor */
758 /* samecheck = gtk_check_button_new_with_label ( "Same x/y zoom factor" ); */
760 if ( *xmpp == *ympp )
761 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(samecheck), TRUE );
763 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
764 gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 1, 2 );
765 gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 1, 2 );
766 gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 2, 3 );
767 gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 2, 3 );
768 gtk_table_attach_defaults ( GTK_TABLE(table), samecheck, 0, 2, 3, 4 );
770 gtk_widget_show_all ( table );
772 g_signal_connect ( G_OBJECT(xspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
773 g_signal_connect ( G_OBJECT(yspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
775 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
777 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
779 *xmpp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) );
780 *ympp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) );
781 gtk_widget_destroy ( dialog );
784 gtk_widget_destroy ( dialog );
788 static void split_spin_focused ( GtkSpinButton *spin, GtkWidget *pass_along[1] )
790 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(pass_along[0]), 1);
793 gboolean a_dialog_time_threshold ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint *thr )
795 GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
797 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
803 GtkWidget *table, *t1, *t2, *t3, *t4, *spin, *label;
804 GtkWidget *pass_along[1];
806 table = gtk_table_new ( 4, 2, FALSE );
807 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
809 label = gtk_label_new (label_text);
811 t1 = gtk_radio_button_new_with_label ( NULL, _("1 min") );
812 t2 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t1), _("1 hour") );
813 t3 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t2), _("1 day") );
814 t4 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t3), _("Custom (in minutes):") );
818 spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *thr, 0, 65536, 1, 5, 0 ), 1, 0 );
820 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
821 gtk_table_attach_defaults ( GTK_TABLE(table), t1, 0, 1, 1, 2 );
822 gtk_table_attach_defaults ( GTK_TABLE(table), t2, 0, 1, 2, 3 );
823 gtk_table_attach_defaults ( GTK_TABLE(table), t3, 0, 1, 3, 4 );
824 gtk_table_attach_defaults ( GTK_TABLE(table), t4, 0, 1, 4, 5 );
825 gtk_table_attach_defaults ( GTK_TABLE(table), spin, 1, 2, 4, 5 );
827 gtk_widget_show_all ( table );
829 g_signal_connect ( G_OBJECT(spin), "grab-focus", G_CALLBACK(split_spin_focused), pass_along );
831 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
833 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
835 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t1))) {
837 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t2))) {
839 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t3))) {
841 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t4))) {
842 *thr = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
844 gtk_widget_destroy ( dialog );
847 gtk_widget_destroy ( dialog );
852 * a_dialog_get_positive_number:
854 * Dialog to return a positive number via a spinbox within the supplied limits
856 * Returns: A value of zero indicates the dialog was cancelled
858 guint a_dialog_get_positive_number ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint default_num, guint min, guint max, guint step )
860 GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
862 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
868 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
869 GtkWidget *response_w = NULL;
870 #if GTK_CHECK_VERSION (2, 20, 0)
871 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
874 GtkWidget *table, *spin, *label;
875 guint result = default_num;
877 table = gtk_table_new ( 2, 1, FALSE );
878 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
880 label = gtk_label_new (label_text);
881 spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( default_num, min, max, step, 5, 0 ), 1, 0 );
883 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 1, 0, 1 );
884 gtk_table_attach_defaults ( GTK_TABLE(table), spin, 0, 1, 1, 2 );
887 gtk_widget_grab_focus ( response_w );
889 gtk_widget_show_all ( table );
891 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
893 result = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
894 gtk_widget_destroy ( dialog );
899 gtk_widget_destroy ( dialog );
903 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
904 static void about_url_hook (GtkAboutDialog *about,
908 open_url (GTK_WINDOW(about), link);
911 static void about_email_hook (GtkAboutDialog *about,
915 new_email (GTK_WINDOW(about), email);
919 void a_dialog_about ( GtkWindow *parent )
921 const gchar *program_name = PACKAGE_NAME;
922 const gchar *version = VIKING_VERSION;
923 const gchar *website = VIKING_URL;
924 const gchar *copyright = "2003-2008, Evan Battaglia\n2008-2013, Viking's contributors";
925 const gchar *comments = _("GPS Data and Topo Analyzer, Explorer, and Manager.");
926 const gchar *license = _("This program is free software; you can redistribute it and/or modify "
927 "it under the terms of the GNU General Public License as published by "
928 "the Free Software Foundation; either version 2 of the License, or "
929 "(at your option) any later version."
931 "This program is distributed in the hope that it will be useful, "
932 "but WITHOUT ANY WARRANTY; without even the implied warranty of "
933 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
934 "GNU General Public License for more details."
936 "You should have received a copy of the GNU General Public License "
937 "along with this program; if not, write to the Free Software "
938 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA");
940 // Would be nice to use gtk_about_dialog_add_credit_section (), but that requires gtk 3.4
941 // For now shove it in the 'artists' section so at least the information is easily visible
942 // Something more advanced might have proper version information too...
943 const gchar *libs[] = {
944 "Compiled in libraries:",
950 // Potentially optional libs (but probably couldn't build without them)
960 // Actually optional libs
976 #ifdef HAVE_LIBSQLITE3
981 // Newer versions of GTK 'just work', calling gtk_show_uri() on the URL or email and opens up the appropriate program
982 // This is the old method:
983 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
984 gtk_about_dialog_set_url_hook (about_url_hook, NULL, NULL);
985 gtk_about_dialog_set_email_hook (about_email_hook, NULL, NULL);
988 gtk_show_about_dialog (parent,
989 /* TODO do not set program-name and correctly set info for g_get_application_name */
990 "program-name", program_name,
993 "comments", comments,
994 "copyright", copyright,
996 "wrap-license", TRUE,
997 /* logo automatically retrieved via gtk_window_get_default_icon_list */
999 "documenters", DOCUMENTERS,
1000 "translator-credits", _("Translation is coordinated on http://launchpad.net/viking"),
1005 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)
1009 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 );
1010 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1011 GtkWidget *response_w = NULL;
1012 #if GTK_CHECK_VERSION (2, 20, 0)
1013 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1016 GtkWidget *map_label = gtk_label_new(_("Map type:"));
1017 GtkWidget *map_combo = vik_combo_box_text_new();
1018 for (s = mapnames; *s; s++)
1019 vik_combo_box_text_append (GTK_COMBO_BOX(map_combo), *s);
1020 gtk_combo_box_set_active (GTK_COMBO_BOX(map_combo), default_map);
1022 GtkWidget *zoom_label = gtk_label_new(_("Zoom level:"));
1023 GtkWidget *zoom_combo = vik_combo_box_text_new();
1024 for (s = zoom_list; *s; s++)
1025 vik_combo_box_text_append (GTK_COMBO_BOX(zoom_combo), *s);
1026 gtk_combo_box_set_active (GTK_COMBO_BOX(zoom_combo), default_zoom);
1028 GtkTable *box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
1029 gtk_table_attach_defaults(box, map_label, 0, 1, 0, 1);
1030 gtk_table_attach_defaults(box, map_combo, 1, 2, 0, 1);
1031 gtk_table_attach_defaults(box, zoom_label, 0, 1, 1, 2);
1032 gtk_table_attach_defaults(box, zoom_combo, 1, 2, 1, 2);
1034 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(box), FALSE, FALSE, 5 );
1037 gtk_widget_grab_focus ( response_w );
1039 gtk_widget_show_all ( dialog );
1040 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
1041 gtk_widget_destroy(dialog);
1045 *selected_map = gtk_combo_box_get_active(GTK_COMBO_BOX(map_combo));
1046 *selected_zoom = gtk_combo_box_get_active(GTK_COMBO_BOX(zoom_combo));
1048 gtk_widget_destroy(dialog);
1053 * Display a dialog presenting the license of a map.
1054 * Allow to read the license by launching a web browser.
1056 void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *license, const gchar *url)
1058 GtkWidget *dialog = gtk_message_dialog_new (parent,
1059 GTK_DIALOG_DESTROY_WITH_PARENT,
1062 _("The map data is licensed: %s."),
1064 gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1065 _("The data provided by '<b>%s</b>' are licensed under the following license: <b>%s</b>."),
1067 #define RESPONSE_OPEN_LICENSE 600
1069 gtk_dialog_add_button (GTK_DIALOG (dialog), _("Open license"), RESPONSE_OPEN_LICENSE);
1073 response = gtk_dialog_run (GTK_DIALOG (dialog));
1074 if (response == RESPONSE_OPEN_LICENSE) {
1075 open_url (parent, url);
1077 } while (response != GTK_RESPONSE_DELETE_EVENT && response != GTK_RESPONSE_OK);
1078 gtk_widget_destroy (dialog);