]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_wpwin.c
Remove not very helpful debug message since it can generate large volumes of messages.
[andy/viking.git] / src / viktrwlayer_wpwin.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) 2010-2014, Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #include <stdlib.h>
23 #include <glib/gi18n.h>
24
25 #include "viktrwlayer_wpwin.h"
26 #include "degrees_converters.h"
27 #include "garminsymbols.h"
28 #include "geotag_exif.h"
29 #include "thumbnails.h"
30 #include "viking.h"
31 #include "vikdatetime_edit_dialog.h"
32 #include "vikgoto.h"
33 #include "vikutils.h"
34
35 static void update_time ( GtkWidget *widget, VikWaypoint *wp )
36 {
37   gchar *msg = vu_get_time_string ( &(wp->timestamp), "%c", &(wp->coord), NULL );
38   gtk_button_set_label ( GTK_BUTTON(widget), msg );
39   g_free ( msg );
40 }
41
42 static VikWaypoint *edit_wp;
43
44 static void time_edit_click ( GtkWidget *widget, VikWaypoint *wp )
45 {
46   GTimeZone *gtz = g_time_zone_new_local ();
47   time_t mytime = vik_datetime_edit_dialog ( GTK_WINDOW(gtk_widget_get_toplevel(widget)),
48                                              _("Date/Time Edit"),
49                                              wp->timestamp,
50                                              gtz );
51   g_time_zone_unref ( gtz );
52
53   // Was the dialog cancelled?
54   if ( mytime == 0 )
55     return;
56
57   // Otherwise use new value in the edit buffer
58   edit_wp->timestamp = mytime;
59
60   // Clear the previous 'Add' image as now a time is set
61   if ( gtk_button_get_image ( GTK_BUTTON(widget) ) )
62     gtk_button_set_image ( GTK_BUTTON(widget), NULL );
63
64   update_time ( widget, edit_wp );
65 }
66
67 static void symbol_entry_changed_cb(GtkWidget *combo, GtkListStore *store)
68 {
69   GtkTreeIter iter;
70   gchar *sym;
71
72   if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter))
73     return;
74
75   gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
76   /* Note: symm is NULL when "(none)" is select (first cell is empty) */
77   gtk_widget_set_tooltip_text(combo, sym);
78   g_free(sym);
79 }
80
81 /* Specify if a new waypoint or not */
82 /* If a new waypoint then it uses the default_name for the suggested name allowing the user to change it.
83     The name to use is returned
84  */
85 /* todo: less on this side, like add track */
86 gchar *a_dialog_waypoint ( GtkWindow *parent, gchar *default_name, VikTrwLayer *vtl, VikWaypoint *wp, VikCoordMode coord_mode, gboolean is_new, gboolean *updated )
87 {
88   GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Waypoint Properties"),
89                                                    parent,
90                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
91                                                    GTK_STOCK_CANCEL,
92                                                    GTK_RESPONSE_REJECT,
93                                                    GTK_STOCK_OK,
94                                                    GTK_RESPONSE_ACCEPT,
95                                                    NULL);
96   struct LatLon ll;
97   GtkWidget *latlabel, *lonlabel, *namelabel, *latentry, *lonentry, *altentry, *altlabel, *nameentry=NULL;
98   GtkWidget *commentlabel, *commententry, *descriptionlabel, *descriptionentry, *imagelabel, *imageentry, *symbollabel, *symbolentry;
99   GtkWidget *timelabel = NULL;
100   GtkWidget *timevaluebutton = NULL;
101   GtkWidget *hasGeotagCB = NULL;
102   GtkWidget *consistentGeotagCB = NULL;
103   GtkListStore *store;
104
105   gchar *lat, *lon, *alt;
106
107   vik_coord_to_latlon ( &(wp->coord), &ll );
108
109   lat = g_strdup_printf ( "%f", ll.lat );
110   lon = g_strdup_printf ( "%f", ll.lon );
111   vik_units_height_t height_units = a_vik_get_units_height ();
112   switch (height_units) {
113   case VIK_UNITS_HEIGHT_METRES:
114     alt = g_strdup_printf ( "%f", wp->altitude );
115     break;
116   case VIK_UNITS_HEIGHT_FEET:
117     alt = g_strdup_printf ( "%f", VIK_METERS_TO_FEET(wp->altitude) );
118     break;
119   default:
120     alt = g_strdup_printf ( "%f", wp->altitude );
121     g_critical("Houston, we've had a problem. height=%d", height_units);
122   }
123
124   *updated = FALSE;
125
126   namelabel = gtk_label_new (_("Name:"));
127   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), namelabel, FALSE, FALSE, 0);
128   // Name is now always changeable
129   nameentry = gtk_entry_new ();
130   if ( default_name )
131     gtk_entry_set_text( GTK_ENTRY(nameentry), default_name );
132   g_signal_connect_swapped ( nameentry, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
133   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), nameentry, FALSE, FALSE, 0);
134
135   latlabel = gtk_label_new (_("Latitude:"));
136   latentry = gtk_entry_new ();
137   gtk_entry_set_text ( GTK_ENTRY(latentry), lat );
138   g_free ( lat );
139
140   lonlabel = gtk_label_new (_("Longitude:"));
141   lonentry = gtk_entry_new ();
142   gtk_entry_set_text ( GTK_ENTRY(lonentry), lon );
143   g_free ( lon );
144
145   altlabel = gtk_label_new (_("Altitude:"));
146   altentry = gtk_entry_new ();
147   gtk_entry_set_text ( GTK_ENTRY(altentry), alt );
148   g_free ( alt );
149
150   if ( wp->comment && !strncmp(wp->comment, "http", 4) )
151     commentlabel = gtk_link_button_new_with_label (wp->comment, _("Comment:") );
152   else
153     commentlabel = gtk_label_new (_("Comment:"));
154   commententry = gtk_entry_new ();
155   gchar *cmt =  NULL;
156   // Auto put in some kind of 'name' as a comment if one previously 'goto'ed this exact location
157   cmt = a_vik_goto_get_search_string_for_this_place(VIK_WINDOW(parent));
158   if (cmt)
159     gtk_entry_set_text(GTK_ENTRY(commententry), cmt);
160
161   if ( wp->description && !strncmp(wp->description, "http", 4) )
162     descriptionlabel = gtk_link_button_new_with_label (wp->description, _("Description:") );
163   else
164     descriptionlabel = gtk_label_new (_("Description:"));
165   descriptionentry = gtk_entry_new ();
166
167   imagelabel = gtk_label_new (_("Image:"));
168   imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN, VF_FILTER_IMAGE, NULL, NULL);
169
170   {
171     GtkCellRenderer *r;
172     symbollabel = gtk_label_new (_("Symbol:"));
173     GtkTreeIter iter;
174
175     store = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING);
176     symbolentry = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
177     gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(symbolentry), 6);
178
179     g_signal_connect(symbolentry, "changed", G_CALLBACK(symbol_entry_changed_cb), store);
180     gtk_list_store_append (store, &iter);
181     gtk_list_store_set (store, &iter, 0, NULL, 1, NULL, 2, _("(none)"), -1);
182     a_populate_sym_list(store);
183
184     r = gtk_cell_renderer_pixbuf_new ();
185     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
186     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "pixbuf", 1, NULL);
187
188     r = gtk_cell_renderer_text_new ();
189     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (symbolentry), r, FALSE);
190     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (symbolentry), r, "text", 2, NULL);
191
192     if ( !is_new && wp->symbol ) {
193       gboolean ok;
194       gchar *sym;
195       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)) {
196         gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
197         if (sym && !strcmp(sym, wp->symbol)) {
198           g_free(sym);
199           break;
200         } else {
201           g_free(sym);
202         }
203       }
204       // Ensure is it a valid symbol in the given symbol set (large vs small)
205       // Not all symbols are available in both
206       // The check prevents a Gtk Critical message
207       if ( iter.stamp )
208         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(symbolentry), &iter);
209     }
210   }
211
212   if ( !is_new && wp->comment )
213     gtk_entry_set_text ( GTK_ENTRY(commententry), wp->comment );
214
215   if ( !is_new && wp->description )
216     gtk_entry_set_text ( GTK_ENTRY(descriptionentry), wp->description );
217
218   if ( !is_new && wp->image ) {
219     vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), wp->image );
220
221     // Geotag Info [readonly]
222     hasGeotagCB = gtk_check_button_new_with_label ( _("Has Geotag") );
223     gtk_widget_set_sensitive ( hasGeotagCB, FALSE );
224     gboolean hasGeotag;
225     gchar *ignore = a_geotag_get_exif_date_from_file ( wp->image, &hasGeotag );
226     g_free ( ignore );
227     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(hasGeotagCB), hasGeotag );
228
229     consistentGeotagCB = gtk_check_button_new_with_label ( _("Consistent Position") );
230     gtk_widget_set_sensitive ( consistentGeotagCB, FALSE );
231     if ( hasGeotag ) {
232       struct LatLon ll = a_geotag_get_position ( wp->image );
233       VikCoord coord;
234       vik_coord_load_from_latlon ( &coord, coord_mode, &ll );
235       gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(consistentGeotagCB), vik_coord_equals(&coord, &wp->coord) );
236     }
237   }
238
239   timelabel = gtk_label_new ( _("Time:") );
240   timevaluebutton = gtk_button_new();
241   gtk_button_set_relief ( GTK_BUTTON(timevaluebutton), GTK_RELIEF_NONE );
242
243   if ( !edit_wp )
244     edit_wp = vik_waypoint_new ();
245   edit_wp = vik_waypoint_copy ( wp );
246
247   // TODO: Consider if there should be a remove time button...
248
249   if ( !is_new && wp->has_timestamp ) {
250     update_time ( timevaluebutton, wp );
251   }
252   else {
253     GtkWidget *img = gtk_image_new_from_stock ( GTK_STOCK_ADD, GTK_ICON_SIZE_MENU );
254     gtk_button_set_image ( GTK_BUTTON(timevaluebutton), img );
255     // Initially use current time or otherwise whatever the last value used was
256     if ( edit_wp->timestamp == 0 ) {
257       time ( &edit_wp->timestamp );
258     }
259   }
260   g_signal_connect ( G_OBJECT(timevaluebutton), "clicked", G_CALLBACK(time_edit_click), edit_wp );
261
262   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latlabel, FALSE, FALSE, 0);
263   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), latentry, FALSE, FALSE, 0);
264   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonlabel, FALSE, FALSE, 0);
265   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), lonentry, FALSE, FALSE, 0);
266   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timelabel, FALSE, FALSE, 0);
267   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), timevaluebutton, FALSE, FALSE, 0);
268   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altlabel, FALSE, FALSE, 0);
269   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), altentry, FALSE, FALSE, 0);
270   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commentlabel, FALSE, FALSE, 0);
271   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), commententry, FALSE, FALSE, 0);
272   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionlabel, FALSE, FALSE, 0);
273   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), descriptionentry, FALSE, FALSE, 0);
274   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imagelabel, FALSE, FALSE, 0);
275   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), imageentry, FALSE, FALSE, 0);
276   if ( hasGeotagCB ) {
277     GtkWidget *hbox =  gtk_hbox_new ( FALSE, 0 );
278     gtk_box_pack_start (GTK_BOX(hbox), hasGeotagCB, FALSE, FALSE, 0);
279     gtk_box_pack_start (GTK_BOX(hbox), consistentGeotagCB, FALSE, FALSE, 0);
280     gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox, FALSE, FALSE, 0);
281   }
282   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), symbollabel, FALSE, FALSE, 0);
283   gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GTK_WIDGET(symbolentry), FALSE, FALSE, 0);
284
285   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
286
287   gtk_widget_show_all ( gtk_dialog_get_content_area(GTK_DIALOG(dialog)) );
288
289   if ( !is_new ) {
290     // Shift left<->right to try not to obscure the waypoint.
291     trw_layer_dialog_shift ( vtl, GTK_WINDOW(dialog), &(wp->coord), FALSE );
292   }
293
294   while ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
295   {
296     if ( strlen((gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) )) == 0 ) /* TODO: other checks (isalpha or whatever ) */
297       a_dialog_info_msg ( parent, _("Please enter a name for the waypoint.") );
298     else {
299       // NB: No check for unique names - this allows generation of same named entries.
300       gchar *entered_name = g_strdup ( (gchar*)gtk_entry_get_text ( GTK_ENTRY(nameentry) ) );
301
302       /* Do It */
303       ll.lat = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(latentry) ) );
304       ll.lon = convert_dms_to_dec ( gtk_entry_get_text ( GTK_ENTRY(lonentry) ) );
305       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &ll );
306       // Always store in metres
307       switch (height_units) {
308       case VIK_UNITS_HEIGHT_METRES:
309         wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
310         break;
311       case VIK_UNITS_HEIGHT_FEET:
312         wp->altitude = VIK_FEET_TO_METERS(atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) ));
313         break;
314       default:
315         wp->altitude = atof ( gtk_entry_get_text ( GTK_ENTRY(altentry) ) );
316         g_critical("Houston, we've had a problem. height=%d", height_units);
317       }
318       if ( g_strcmp0 ( wp->comment, gtk_entry_get_text ( GTK_ENTRY(commententry) ) ) )
319         vik_waypoint_set_comment ( wp, gtk_entry_get_text ( GTK_ENTRY(commententry) ) );
320       if ( g_strcmp0 ( wp->description, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) ) )
321         vik_waypoint_set_description ( wp, gtk_entry_get_text ( GTK_ENTRY(descriptionentry) ) );
322       if ( g_strcmp0 ( wp->image, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) ) )
323         vik_waypoint_set_image ( wp, vik_file_entry_get_filename ( VIK_FILE_ENTRY(imageentry) ) );
324       if ( wp->image && *(wp->image) && (!a_thumbnails_exists(wp->image)) )
325         a_thumbnails_create ( wp->image );
326       if ( edit_wp->timestamp ) {
327         wp->timestamp = edit_wp->timestamp;
328         wp->has_timestamp = TRUE;
329       }
330
331       GtkTreeIter iter, first;
332       gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(store), &first );
333       if ( !gtk_combo_box_get_active_iter ( GTK_COMBO_BOX(symbolentry), &iter ) || !memcmp(&iter, &first, sizeof(GtkTreeIter)) ) {
334         vik_waypoint_set_symbol ( wp, NULL );
335       } else {
336         gchar *sym;
337         gtk_tree_model_get ( GTK_TREE_MODEL(store), &iter, 0, (void *)&sym, -1 );
338         vik_waypoint_set_symbol ( wp, sym );
339         g_free(sym);
340       }
341
342       gtk_widget_destroy ( dialog );
343       if ( is_new )
344         return entered_name;
345       else {
346         *updated = TRUE;
347         // See if name has been changed
348         if ( g_strcmp0 (default_name, entered_name ) )
349           return entered_name;
350         else
351           return NULL;
352       }
353     }
354   }
355   gtk_widget_destroy ( dialog );
356   return NULL;
357 }