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