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"
36 #include "geotag_exif.h"
37 #include "vikdatetime_edit_dialog.h"
39 #include <glib/gi18n.h>
46 void a_dialog_msg ( GtkWindow *parent, gint type, const gchar *info, const gchar *extra )
48 GtkWidget *msgbox = gtk_message_dialog_new ( parent, GTK_DIALOG_DESTROY_WITH_PARENT, type, GTK_BUTTONS_OK, info, extra );
49 gtk_dialog_run ( GTK_DIALOG(msgbox) );
50 gtk_widget_destroy ( msgbox );
53 gboolean a_dialog_goto_latlon ( GtkWindow *parent, struct LatLon *ll, const struct LatLon *old )
55 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to Lat/Lon"),
57 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
63 GtkWidget *latlabel, *lonlabel;
65 gchar *tmp_lat, *tmp_lon;
67 latlabel = gtk_label_new (_("Latitude:"));
68 lat = gtk_entry_new ();
69 tmp_lat = g_strdup_printf ( "%f", old->lat );
70 gtk_entry_set_text ( GTK_ENTRY(lat), tmp_lat );
73 lonlabel = gtk_label_new (_("Longitude:"));
74 lon = gtk_entry_new ();
75 tmp_lon = g_strdup_printf ( "%f", old->lon );
76 gtk_entry_set_text ( GTK_ENTRY(lon), tmp_lon );
79 gtk_widget_show ( latlabel );
80 gtk_widget_show ( lonlabel );
81 gtk_widget_show ( lat );
82 gtk_widget_show ( lon );
84 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
85 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lat, FALSE, FALSE, 0);
86 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
87 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lon, FALSE, FALSE, 0);
89 // 'ok' when press return in the entry
90 g_signal_connect_swapped (lat, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
91 g_signal_connect_swapped (lon, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
93 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
95 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
97 ll->lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lat) ) );
98 ll->lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lon) ) );
99 gtk_widget_destroy ( dialog );
103 gtk_widget_destroy ( dialog );
107 gboolean a_dialog_goto_utm ( GtkWindow *parent, struct UTM *utm, const struct UTM *old )
109 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Go to UTM"),
111 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
117 GtkWidget *norlabel, *easlabel, *nor, *eas;
118 GtkWidget *zonehbox, *zonespin, *letterentry;
119 gchar *tmp_eas, *tmp_nor;
122 norlabel = gtk_label_new (_("Northing:"));
123 nor = gtk_entry_new ();
124 tmp_nor = g_strdup_printf("%ld", (long) old->northing );
125 gtk_entry_set_text ( GTK_ENTRY(nor), tmp_nor );
128 easlabel = gtk_label_new (_("Easting:"));
129 eas = gtk_entry_new ();
130 tmp_eas = g_strdup_printf("%ld", (long) old->easting );
131 gtk_entry_set_text ( GTK_ENTRY(eas), tmp_eas );
134 zonehbox = gtk_hbox_new ( FALSE, 0 );
135 gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Zone:") ), FALSE, FALSE, 5 );
136 zonespin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( old->zone, 1, 60, 1, 5, 0 ), 1, 0 );
137 gtk_box_pack_start ( GTK_BOX(zonehbox), zonespin, TRUE, TRUE, 5 );
138 gtk_box_pack_start ( GTK_BOX(zonehbox), gtk_label_new ( _("Letter:") ), FALSE, FALSE, 5 );
139 letterentry = gtk_entry_new ();
140 gtk_entry_set_max_length ( GTK_ENTRY(letterentry), 1 );
141 gtk_entry_set_width_chars ( GTK_ENTRY(letterentry), 2 );
142 tmp_letter[0] = old->letter;
143 tmp_letter[1] = '\0';
144 gtk_entry_set_text ( GTK_ENTRY(letterentry), tmp_letter );
145 gtk_box_pack_start ( GTK_BOX(zonehbox), letterentry, FALSE, FALSE, 5 );
147 gtk_widget_show ( norlabel );
148 gtk_widget_show ( easlabel );
149 gtk_widget_show ( nor );
150 gtk_widget_show ( eas );
152 gtk_widget_show_all ( zonehbox );
154 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), norlabel, FALSE, FALSE, 0);
155 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nor, FALSE, FALSE, 0);
156 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), easlabel, FALSE, FALSE, 0);
157 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), eas, FALSE, FALSE, 0);
158 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), zonehbox, FALSE, FALSE, 0);
160 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
162 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
165 utm->northing = atof ( gtk_entry_get_text ( GTK_ENTRY(nor) ) );
166 utm->easting = atof ( gtk_entry_get_text ( GTK_ENTRY(eas) ) );
167 utm->zone = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(zonespin) );
168 letter = gtk_entry_get_text ( GTK_ENTRY(letterentry) );
170 utm->letter = toupper(*letter);
171 gtk_widget_destroy ( dialog );
175 gtk_widget_destroy ( dialog );
179 void a_dialog_response_accept ( GtkDialog *dialog )
181 gtk_dialog_response ( dialog, GTK_RESPONSE_ACCEPT );
184 static void update_time ( GtkWidget *widget, time_t timestamp )
187 strftime ( tmp_str, sizeof(tmp_str), "%c", localtime(&(timestamp)) );
188 gtk_button_set_label ( GTK_BUTTON(widget), tmp_str );
191 static VikWaypoint *edit_wp;
193 static void time_edit_click ( GtkWidget *widget, VikWaypoint *wp )
195 GTimeZone *gtz = g_time_zone_new_local ();
196 time_t mytime = vik_datetime_edit_dialog ( GTK_WINDOW(gtk_widget_get_toplevel(widget)),
200 g_time_zone_unref ( gtz );
202 // Was the dialog cancelled?
206 // Otherwise use new value in the edit buffer
207 edit_wp->timestamp = mytime;
209 // Clear the previous 'Add' image as now a time is set
210 if ( gtk_button_get_image ( GTK_BUTTON(widget) ) )
211 gtk_button_set_image ( GTK_BUTTON(widget), NULL );
213 update_time ( widget, edit_wp->timestamp );
216 static void symbol_entry_changed_cb(GtkWidget *combo, GtkListStore *store)
221 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter))
224 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
225 /* Note: symm is NULL when "(none)" is select (first cell is empty) */
226 gtk_widget_set_tooltip_text(combo, sym);
230 /* Specify if a new waypoint or not */
231 /* If a new waypoint then it uses the default_name for the suggested name allowing the user to change it.
232 The name to use is returned
234 /* todo: less on this side, like add track */
235 gchar *a_dialog_waypoint ( GtkWindow *parent, gchar *default_name, VikTrwLayer *vtl, VikWaypoint *wp, VikCoordMode coord_mode, gboolean is_new, gboolean *updated )
237 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Waypoint Properties"),
239 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
246 GtkWidget *latlabel, *lonlabel, *namelabel, *latentry, *lonentry, *altentry, *altlabel, *nameentry=NULL;
247 GtkWidget *commentlabel, *commententry, *descriptionlabel, *descriptionentry, *imagelabel, *imageentry, *symbollabel, *symbolentry;
248 GtkWidget *timelabel = NULL;
249 GtkWidget *timevaluebutton = NULL;
250 GtkWidget *hasGeotagCB = NULL;
251 GtkWidget *consistentGeotagCB = NULL;
254 gchar *lat, *lon, *alt;
256 vik_coord_to_latlon ( &(wp->coord), &ll );
258 lat = g_strdup_printf ( "%f", ll.lat );
259 lon = g_strdup_printf ( "%f", ll.lon );
260 vik_units_height_t height_units = a_vik_get_units_height ();
261 switch (height_units) {
262 case VIK_UNITS_HEIGHT_METRES:
263 alt = g_strdup_printf ( "%f", wp->altitude );
265 case VIK_UNITS_HEIGHT_FEET:
266 alt = g_strdup_printf ( "%f", VIK_METERS_TO_FEET(wp->altitude) );
269 alt = g_strdup_printf ( "%f", wp->altitude );
270 g_critical("Houston, we've had a problem. height=%d", height_units);
275 namelabel = gtk_label_new (_("Name:"));
276 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), namelabel, FALSE, FALSE, 0);
277 // Name is now always changeable
278 nameentry = gtk_entry_new ();
280 gtk_entry_set_text( GTK_ENTRY(nameentry), default_name );
281 g_signal_connect_swapped ( nameentry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
282 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nameentry, FALSE, FALSE, 0);
284 latlabel = gtk_label_new (_("Latitude:"));
285 latentry = gtk_entry_new ();
286 gtk_entry_set_text ( GTK_ENTRY(latentry), lat );
289 lonlabel = gtk_label_new (_("Longitude:"));
290 lonentry = gtk_entry_new ();
291 gtk_entry_set_text ( GTK_ENTRY(lonentry), lon );
294 altlabel = gtk_label_new (_("Altitude:"));
295 altentry = gtk_entry_new ();
296 gtk_entry_set_text ( GTK_ENTRY(altentry), alt );
299 commentlabel = gtk_label_new (_("Comment:"));
300 commententry = gtk_entry_new ();
302 // Auto put in some kind of 'name' as a comment if one previously 'goto'ed this exact location
303 cmt = a_vik_goto_get_search_string_for_this_place(VIK_WINDOW(parent));
305 gtk_entry_set_text(GTK_ENTRY(commententry), cmt);
307 descriptionlabel = gtk_label_new (_("Description:"));
308 descriptionentry = gtk_entry_new ();
310 imagelabel = gtk_label_new (_("Image:"));
311 imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN, VF_FILTER_IMAGE);
315 symbollabel = gtk_label_new (_("Symbol:"));
318 store = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING);
319 symbolentry = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
320 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(symbolentry), 6);
322 g_signal_connect(symbolentry, "changed", G_CALLBACK(symbol_entry_changed_cb), store);
323 gtk_list_store_append (store, &iter);
324 gtk_list_store_set (store, &iter, 0, NULL, 1, NULL, 2, _("(none)"), -1);
325 a_populate_sym_list(store);
327 r = gtk_cell_renderer_pixbuf_new ();
328 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
329 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "pixbuf", 1, NULL);
331 r = gtk_cell_renderer_text_new ();
332 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
333 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "text", 2, NULL);
335 if ( !is_new && wp->symbol ) {
338 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)) {
339 gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
340 if (sym && !strcmp(sym, wp->symbol)) {
347 // Ensure is it a valid symbol in the given symbol set (large vs small)
348 // Not all symbols are available in both
349 // The check prevents a Gtk Critical message
351 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(symbolentry), &iter);
355 if ( !is_new && wp->comment )
356 gtk_entry_set_text ( GTK_ENTRY(commententry), wp->comment );
358 if ( !is_new && wp->description )
359 gtk_entry_set_text ( GTK_ENTRY(descriptionentry), wp->description );
361 if ( !is_new && wp->image ) {
362 vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), wp->image );
364 // Geotag Info [readonly]
365 hasGeotagCB = gtk_check_button_new_with_label ( _("Has Geotag") );
366 gtk_widget_set_sensitive ( hasGeotagCB, FALSE );
368 gchar *ignore = a_geotag_get_exif_date_from_file ( wp->image, &hasGeotag );
370 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(hasGeotagCB), hasGeotag );
372 consistentGeotagCB = gtk_check_button_new_with_label ( _("Consistent Position") );
373 gtk_widget_set_sensitive ( consistentGeotagCB, FALSE );
375 struct LatLon ll = a_geotag_get_position ( wp->image );
377 vik_coord_load_from_latlon ( &coord, coord_mode, &ll );
378 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(consistentGeotagCB), vik_coord_equals(&coord, &wp->coord) );
382 timelabel = gtk_label_new ( _("Time:") );
383 timevaluebutton = gtk_button_new();
384 gtk_button_set_relief ( GTK_BUTTON(timevaluebutton), GTK_RELIEF_NONE );
387 edit_wp = vik_waypoint_new ();
389 // TODO: Consider if there should be a remove time button...
391 if ( !is_new && wp->has_timestamp ) {
392 update_time ( timevaluebutton, wp->timestamp );
395 GtkWidget *img = gtk_image_new_from_stock ( GTK_STOCK_ADD, GTK_ICON_SIZE_MENU );
396 gtk_button_set_image ( GTK_BUTTON(timevaluebutton), img );
397 // Initially use current time or otherwise whatever the last value used was
398 if ( edit_wp->timestamp == 0 ) {
399 time ( &edit_wp->timestamp );
402 g_signal_connect ( G_OBJECT(timevaluebutton), "clicked", G_CALLBACK(time_edit_click), edit_wp );
404 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
405 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latentry, FALSE, FALSE, 0);
406 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
407 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonentry, FALSE, FALSE, 0);
408 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timelabel, FALSE, FALSE, 0);
409 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timevaluebutton, FALSE, FALSE, 0);
410 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altlabel, FALSE, FALSE, 0);
411 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altentry, FALSE, FALSE, 0);
412 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commentlabel, FALSE, FALSE, 0);
413 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commententry, FALSE, FALSE, 0);
414 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionlabel, FALSE, FALSE, 0);
415 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionentry, FALSE, FALSE, 0);
416 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imagelabel, FALSE, FALSE, 0);
417 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imageentry, FALSE, FALSE, 0);
419 GtkWidget *hbox = gtk_hbox_new ( FALSE, 0 );
420 gtk_box_pack_start (GTK_BOX(hbox), hasGeotagCB, FALSE, FALSE, 0);
421 gtk_box_pack_start (GTK_BOX(hbox), consistentGeotagCB, FALSE, FALSE, 0);
422 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox, FALSE, FALSE, 0);
424 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), symbollabel, FALSE, FALSE, 0);
425 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(symbolentry), FALSE, FALSE, 0);
427 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
429 gtk_widget_show_all ( gtk_dialog_get_content_area(GTK_DIALOG(dialog)) );
432 // Shift left<->right to try not to obscure the waypoint.
433 trw_layer_dialog_shift ( vtl, GTK_WINDOW(dialog), &(wp->coord), FALSE );
436 while ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
438 if ( strlen((gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) )) == 0 ) /* TODO: other checks (isalpha or whatever ) */
439 a_dialog_info_msg ( parent, _("Please enter a name for the waypoint.") );
441 // NB: No check for unique names - this allows generation of same named entries.
442 gchar *entered_name = g_strdup ( (gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) ) );
445 ll.lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(latentry) ) );
446 ll.lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lonentry) ) );
447 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &ll );
448 // Always store in metres
449 switch (height_units) {
450 case VIK_UNITS_HEIGHT_METRES:
451 wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
453 case VIK_UNITS_HEIGHT_FEET:
454 wp->altitude = VIK_FEET_TO_METERS(atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) ));
457 wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
458 g_critical("Houston, we've had a problem. height=%d", height_units);
460 if ( g_strcmp0 ( wp->comment, gtk_entry_get_text ( GTK_ENTRY(commententry) ) ) )
461 vik_waypoint_set_comment ( wp, gtk_entry_get_text ( GTK_ENTRY(commententry) ) );
462 if ( g_strcmp0 ( wp->description, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) ) )
463 vik_waypoint_set_description ( wp, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) );
464 if ( g_strcmp0 ( wp->image, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) ) )
465 vik_waypoint_set_image ( wp, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) );
466 if ( wp->image && *(wp->image) && (!a_thumbnails_exists(wp->image)) )
467 a_thumbnails_create ( wp->image );
468 if ( edit_wp->timestamp ) {
469 wp->timestamp = edit_wp->timestamp;
470 wp->has_timestamp = TRUE;
473 GtkTreeIter iter, first;
474 gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(store), &first );
475 if ( !gtk_combo_box_get_active_iter ( GTK_COMBO_BOX(symbolentry), &iter ) || !memcmp(&iter, &first, sizeof(GtkTreeIter)) ) {
476 vik_waypoint_set_symbol ( wp, NULL );
479 gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
480 vik_waypoint_set_symbol ( wp, sym );
484 gtk_widget_destroy ( dialog );
489 // See if name has been changed
490 if ( g_strcmp0 (default_name, entered_name ) )
497 gtk_widget_destroy ( dialog );
501 static void get_selected_foreach_func(GtkTreeModel *model,
508 gtk_tree_model_get (model, iter, 0, &name, -1);
509 *list = g_list_prepend(*list, name);
512 GList *a_dialog_select_from_list ( GtkWindow *parent, GList *names, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg )
515 GtkCellRenderer *renderer;
518 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
520 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
526 /* When something is selected then OK */
527 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
528 GtkWidget *response_w = NULL;
529 #if GTK_CHECK_VERSION (2, 20, 0)
530 /* Default to not apply - as initially nothing is selected! */
531 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
533 GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
535 GtkWidget *scrolledwindow;
537 GList *runner = names;
540 gtk_list_store_append(store, &iter);
541 gtk_list_store_set(store, &iter, 0, runner->data, -1);
542 runner = g_list_next(runner);
545 view = gtk_tree_view_new();
546 renderer = gtk_cell_renderer_text_new();
547 // Use the column header to display the message text,
548 // this makes the overall widget allocation simple as treeview takes up all the space
549 GtkTreeViewColumn *column;
550 column = gtk_tree_view_column_new_with_attributes (msg, renderer, "text", 0, NULL );
551 gtk_tree_view_column_set_sort_column_id (column, 0);
552 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
554 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
555 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
556 multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
557 g_object_unref(store);
559 scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
560 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
561 gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
563 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0 );
564 // Ensure a reasonable number of items are shown, but let the width be automatically sized
565 gtk_widget_set_size_request ( dialog, -1, 400) ;
567 gtk_widget_show_all ( dialog );
570 gtk_widget_grab_focus ( response_w );
572 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
574 GList *names_selected = NULL;
575 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
576 gtk_tree_selection_selected_foreach(selection, get_selected_foreach_func, &names_selected);
579 gtk_widget_destroy ( dialog );
580 return names_selected;
582 a_dialog_error_msg(parent, _("Nothing was selected"));
584 gtk_widget_destroy ( dialog );
588 gchar *a_dialog_new_track ( GtkWindow *parent, gchar *default_name, gboolean is_route )
590 GtkWidget *dialog = gtk_dialog_new_with_buttons ( is_route ? _("Add Route") : _("Add Track"),
592 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
598 GtkWidget *label = gtk_label_new ( is_route ? _("Route Name:") : _("Track Name:") );
599 GtkWidget *entry = gtk_entry_new ();
602 gtk_entry_set_text ( GTK_ENTRY(entry), default_name );
604 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
605 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), entry, FALSE, FALSE, 0);
607 g_signal_connect_swapped ( entry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
609 gtk_widget_show ( label );
610 gtk_widget_show ( entry );
612 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
614 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
616 const gchar *constname = gtk_entry_get_text ( GTK_ENTRY(entry) );
617 if ( *constname == '\0' )
618 a_dialog_info_msg ( parent, _("Please enter a name for the track.") );
620 gchar *name = g_strdup ( constname );
621 gtk_widget_destroy ( dialog );
625 gtk_widget_destroy ( dialog );
629 static void today_clicked (GtkWidget *cal)
631 GDateTime *now = g_date_time_new_now_local ();
632 gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(now)-1, g_date_time_get_year(now) );
633 gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(now) );
634 g_date_time_unref ( now );
640 * Returns: a date as a string - always in ISO8601 format (YYYY-MM-DD)
641 * This string can be NULL (especially when the dialog is cancelled)
642 * Free the string after use
644 gchar *a_dialog_get_date ( GtkWindow *parent, const gchar *title )
646 GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
648 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
654 GtkWidget *cal = gtk_calendar_new ();
655 GtkWidget *today = gtk_button_new_with_label ( _("Today") );
657 static guint year = 0;
658 static guint month = 0;
659 static guint day = 0;
662 // restore the last selected date
663 gtk_calendar_select_month ( GTK_CALENDAR(cal), month, year );
664 gtk_calendar_select_day ( GTK_CALENDAR(cal), day );
667 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), today, FALSE, FALSE, 0);
668 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0);
670 g_signal_connect_swapped ( G_OBJECT(today), "clicked", G_CALLBACK(today_clicked), cal );
672 gtk_widget_show_all ( dialog );
674 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
676 gchar *date_str = NULL;
677 if ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
679 gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day );
680 date_str = g_strdup_printf ( "%d-%02d-%02d", year, month+1, day );
682 gtk_widget_destroy ( dialog );
686 /* creates a vbox full of labels */
687 GtkWidget *a_dialog_create_label_vbox ( gchar **texts, int label_count, gint spacing, gint padding )
689 GtkWidget *vbox, *label;
691 vbox = gtk_vbox_new( TRUE, spacing );
693 for ( i = 0; i < label_count; i++ )
695 label = gtk_label_new(NULL);
696 gtk_label_set_markup ( GTK_LABEL(label), _(texts[i]) );
697 gtk_box_pack_start ( GTK_BOX(vbox), label, FALSE, TRUE, padding );
702 gboolean a_dialog_yes_or_no ( GtkWindow *parent, const gchar *message, const gchar *extra )
705 dia = gtk_message_dialog_new ( parent,
706 GTK_DIALOG_DESTROY_WITH_PARENT,
707 GTK_MESSAGE_QUESTION,
711 if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_YES )
713 gtk_widget_destroy ( dia );
718 gtk_widget_destroy ( dia );
723 static void zoom_spin_changed ( GtkSpinButton *spin, GtkWidget *pass_along[3] )
725 if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(pass_along[2]) ) )
726 gtk_spin_button_set_value (
727 GTK_SPIN_BUTTON(pass_along[GTK_WIDGET(spin) == pass_along[0] ? 1 : 0]),
728 gtk_spin_button_get_value ( spin ) );
731 gboolean a_dialog_custom_zoom ( GtkWindow *parent, gdouble *xmpp, gdouble *ympp )
733 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Zoom Factors..."),
735 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
741 GtkWidget *table, *label, *xlabel, *xspin, *ylabel, *yspin, *samecheck;
742 GtkWidget *pass_along[3];
744 table = gtk_table_new ( 4, 2, FALSE );
745 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
747 label = gtk_label_new ( _("Zoom factor (in meters per pixel):") );
748 xlabel = gtk_label_new ( _("X (easting): "));
749 ylabel = gtk_label_new ( _("Y (northing): "));
751 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 );
752 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 );
754 pass_along[2] = samecheck = gtk_check_button_new_with_label ( _("X and Y zoom factors must be equal") );
755 /* TODO -- same factor */
756 /* samecheck = gtk_check_button_new_with_label ( "Same x/y zoom factor" ); */
758 if ( *xmpp == *ympp )
759 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(samecheck), TRUE );
761 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
762 gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 1, 2 );
763 gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 1, 2 );
764 gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 2, 3 );
765 gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 2, 3 );
766 gtk_table_attach_defaults ( GTK_TABLE(table), samecheck, 0, 2, 3, 4 );
768 gtk_widget_show_all ( table );
770 g_signal_connect ( G_OBJECT(xspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
771 g_signal_connect ( G_OBJECT(yspin), "value-changed", G_CALLBACK(zoom_spin_changed), pass_along );
773 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
775 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
777 *xmpp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) );
778 *ympp = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) );
779 gtk_widget_destroy ( dialog );
782 gtk_widget_destroy ( dialog );
786 static void split_spin_focused ( GtkSpinButton *spin, GtkWidget *pass_along[1] )
788 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(pass_along[0]), 1);
791 gboolean a_dialog_time_threshold ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint *thr )
793 GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
795 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
801 GtkWidget *table, *t1, *t2, *t3, *t4, *spin, *label;
802 GtkWidget *pass_along[1];
804 table = gtk_table_new ( 4, 2, FALSE );
805 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
807 label = gtk_label_new (label_text);
809 t1 = gtk_radio_button_new_with_label ( NULL, _("1 min") );
810 t2 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t1), _("1 hour") );
811 t3 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t2), _("1 day") );
812 t4 = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(t3), _("Custom (in minutes):") );
816 spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( *thr, 0, 65536, 1, 5, 0 ), 1, 0 );
818 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 2, 0, 1 );
819 gtk_table_attach_defaults ( GTK_TABLE(table), t1, 0, 1, 1, 2 );
820 gtk_table_attach_defaults ( GTK_TABLE(table), t2, 0, 1, 2, 3 );
821 gtk_table_attach_defaults ( GTK_TABLE(table), t3, 0, 1, 3, 4 );
822 gtk_table_attach_defaults ( GTK_TABLE(table), t4, 0, 1, 4, 5 );
823 gtk_table_attach_defaults ( GTK_TABLE(table), spin, 1, 2, 4, 5 );
825 gtk_widget_show_all ( table );
827 g_signal_connect ( G_OBJECT(spin), "grab-focus", G_CALLBACK(split_spin_focused), pass_along );
829 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
831 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
833 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t1))) {
835 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t2))) {
837 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t3))) {
839 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(t4))) {
840 *thr = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
842 gtk_widget_destroy ( dialog );
845 gtk_widget_destroy ( dialog );
850 * a_dialog_get_positive_number:
852 * Dialog to return a positive number via a spinbox within the supplied limits
854 * Returns: A value of zero indicates the dialog was cancelled
856 guint a_dialog_get_positive_number ( GtkWindow *parent, gchar *title_text, gchar *label_text, guint default_num, guint min, guint max, guint step )
858 GtkWidget *dialog = gtk_dialog_new_with_buttons (title_text,
860 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
866 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
867 GtkWidget *response_w = NULL;
868 #if GTK_CHECK_VERSION (2, 20, 0)
869 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
872 GtkWidget *table, *spin, *label;
873 guint result = default_num;
875 table = gtk_table_new ( 2, 1, FALSE );
876 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 );
878 label = gtk_label_new (label_text);
879 spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( default_num, min, max, step, 5, 0 ), 1, 0 );
881 gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 1, 0, 1 );
882 gtk_table_attach_defaults ( GTK_TABLE(table), spin, 0, 1, 1, 2 );
885 gtk_widget_grab_focus ( response_w );
887 gtk_widget_show_all ( table );
889 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
891 result = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) );
892 gtk_widget_destroy ( dialog );
897 gtk_widget_destroy ( dialog );
901 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
902 static void about_url_hook (GtkAboutDialog *about,
906 open_url (GTK_WINDOW(about), link);
909 static void about_email_hook (GtkAboutDialog *about,
913 new_email (GTK_WINDOW(about), email);
917 void a_dialog_about ( GtkWindow *parent )
919 const gchar *program_name = PACKAGE_NAME;
920 const gchar *version = VIKING_VERSION;
921 const gchar *website = VIKING_URL;
922 const gchar *copyright = "2003-2008, Evan Battaglia\n2008-2013, Viking's contributors";
923 const gchar *comments = _("GPS Data and Topo Analyzer, Explorer, and Manager.");
924 const gchar *license = _("This program is free software; you can redistribute it and/or modify "
925 "it under the terms of the GNU General Public License as published by "
926 "the Free Software Foundation; either version 2 of the License, or "
927 "(at your option) any later version."
929 "This program is distributed in the hope that it will be useful, "
930 "but WITHOUT ANY WARRANTY; without even the implied warranty of "
931 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
932 "GNU General Public License for more details."
934 "You should have received a copy of the GNU General Public License "
935 "along with this program; if not, write to the Free Software "
936 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA");
938 // Would be nice to use gtk_about_dialog_add_credit_section (), but that requires gtk 3.4
939 // For now shove it in the 'artists' section so at least the information is easily visible
940 // Something more advanced might have proper version information too...
941 const gchar *libs[] = {
942 "Compiled in libraries:",
948 // Potentially optional libs (but probably couldn't build without them)
958 // Actually optional libs
974 #ifdef HAVE_LIBSQLITE3
979 // Newer versions of GTK 'just work', calling gtk_show_uri() on the URL or email and opens up the appropriate program
980 // This is the old method:
981 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 24)
982 gtk_about_dialog_set_url_hook (about_url_hook, NULL, NULL);
983 gtk_about_dialog_set_email_hook (about_email_hook, NULL, NULL);
986 gtk_show_about_dialog (parent,
987 /* TODO do not set program-name and correctly set info for g_get_application_name */
988 "program-name", program_name,
991 "comments", comments,
992 "copyright", copyright,
994 "wrap-license", TRUE,
995 /* logo automatically retrieved via gtk_window_get_default_icon_list */
997 "documenters", DOCUMENTERS,
998 "translator-credits", _("Translation is coordinated on http://launchpad.net/viking"),
1003 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)
1007 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 );
1008 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1009 GtkWidget *response_w = NULL;
1010 #if GTK_CHECK_VERSION (2, 20, 0)
1011 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1014 GtkWidget *map_label = gtk_label_new(_("Map type:"));
1015 GtkWidget *map_combo = vik_combo_box_text_new();
1016 for (s = mapnames; *s; s++)
1017 vik_combo_box_text_append (GTK_COMBO_BOX(map_combo), *s);
1018 gtk_combo_box_set_active (GTK_COMBO_BOX(map_combo), default_map);
1020 GtkWidget *zoom_label = gtk_label_new(_("Zoom level:"));
1021 GtkWidget *zoom_combo = vik_combo_box_text_new();
1022 for (s = zoom_list; *s; s++)
1023 vik_combo_box_text_append (GTK_COMBO_BOX(zoom_combo), *s);
1024 gtk_combo_box_set_active (GTK_COMBO_BOX(zoom_combo), default_zoom);
1026 GtkTable *box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
1027 gtk_table_attach_defaults(box, map_label, 0, 1, 0, 1);
1028 gtk_table_attach_defaults(box, map_combo, 1, 2, 0, 1);
1029 gtk_table_attach_defaults(box, zoom_label, 0, 1, 1, 2);
1030 gtk_table_attach_defaults(box, zoom_combo, 1, 2, 1, 2);
1032 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(box), FALSE, FALSE, 5 );
1035 gtk_widget_grab_focus ( response_w );
1037 gtk_widget_show_all ( dialog );
1038 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
1039 gtk_widget_destroy(dialog);
1043 *selected_map = gtk_combo_box_get_active(GTK_COMBO_BOX(map_combo));
1044 *selected_zoom = gtk_combo_box_get_active(GTK_COMBO_BOX(zoom_combo));
1046 gtk_widget_destroy(dialog);
1051 * Display a dialog presenting the license of a map.
1052 * Allow to read the license by launching a web browser.
1054 void a_dialog_license ( GtkWindow *parent, const gchar *map, const gchar *license, const gchar *url)
1056 GtkWidget *dialog = gtk_message_dialog_new (parent,
1057 GTK_DIALOG_DESTROY_WITH_PARENT,
1060 _("The map data is licensed: %s."),
1062 gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1063 _("The data provided by '<b>%s</b>' are licensed under the following license: <b>%s</b>."),
1065 #define RESPONSE_OPEN_LICENSE 600
1067 gtk_dialog_add_button (GTK_DIALOG (dialog), _("Open license"), RESPONSE_OPEN_LICENSE);
1071 response = gtk_dialog_run (GTK_DIALOG (dialog));
1072 if (response == RESPONSE_OPEN_LICENSE) {
1073 open_url (parent, url);
1075 } while (response != GTK_RESPONSE_DELETE_EVENT && response != GTK_RESPONSE_OK);
1076 gtk_widget_destroy (dialog);