]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrwlayer.c
Merge pull request #20 from huobos/zh_CN
[andy/viking.git] / src / viktrwlayer.c
index 97ff7d47bbb41a51dbf8bdb5be8dbfb4b4f08b4c..7a1aab78bd19a521df524b0396352081b054fc62 100644 (file)
@@ -180,7 +180,6 @@ struct _VikTrwLayer {
   GdkGC *waypoint_text_gc; GdkColor waypoint_text_color;
   GdkGC *waypoint_bg_gc; GdkColor waypoint_bg_color;
   gboolean wpbgand;
-  GdkFont *waypoint_font;
   VikTrack *current_track; // ATM shared between new tracks and new routes
   guint16 ct_x1, ct_y1, ct_x2, ct_y2;
   gboolean draw_sync_done;
@@ -370,6 +369,8 @@ static void trw_layer_cancel_current_tp ( VikTrwLayer *vtl, gboolean destroy );
 static void trw_layer_tpwin_response ( VikTrwLayer *vtl, gint response );
 static void trw_layer_tpwin_init ( VikTrwLayer *vtl );
 
+static void trw_layer_sort_all ( VikTrwLayer *vtl );
+
 static gpointer tool_edit_trackpoint_create ( VikWindow *vw, VikViewport *vvp);
 static void tool_edit_trackpoint_destroy ( tool_ed_t *t );
 static gboolean tool_edit_trackpoint_click ( VikTrwLayer *vtl, GdkEventButton *event, gpointer data );
@@ -825,15 +826,17 @@ static void vik_trwlayer_class_init ( VikTrwLayerClass *klass )
       else if ( mystderr )
         tokens = g_strsplit(mystderr, " ", 0);
 
-      gint num = 0;
-      gchar *token = tokens[num];
-      while ( token && num < 2 ) {
-        if (num == 1) {
-          if ( viking_version_to_number(token) >= viking_version_to_number("1.7.3") )
-            have_diary_program = TRUE;
+      if ( tokens ) {
+        gint num = 0;
+        gchar *token = tokens[num];
+        while ( token && num < 2 ) {
+          if (num == 1) {
+            if ( viking_version_to_number(token) >= viking_version_to_number("1.7.3") )
+              have_diary_program = TRUE;
+          }
+          num++;
+          token = tokens[num];
         }
-        num++;
-        token = tokens[num];
       }
       g_strfreev ( tokens );
     }
@@ -914,8 +917,8 @@ typedef struct {
   const gchar *date_str;
   const VikTrack *trk;
   const VikWaypoint *wpt;
-  gpointer *trk_id;
-  gpointer *wpt_id;
+  gpointer trk_id;
+  gpointer wpt_id;
 } date_finder_type;
 
 static gboolean trw_layer_find_date_track ( const gpointer id, const VikTrack *trk, date_finder_type *df )
@@ -1027,7 +1030,7 @@ static void trw_layer_copy_item_cb ( menu_array_sublayer values)
 {
   VikTrwLayer *vtl = VIK_TRW_LAYER(values[MA_VTL]);
   gint subtype = GPOINTER_TO_INT (values[MA_SUBTYPE]);
-  gpointer sublayer = values[MA_SUBLAYER_ID];
+  gpointer sublayer = values[MA_SUBLAYER_ID];
   guint8 *data = NULL;
   guint len;
 
@@ -1170,6 +1173,12 @@ static void trw_layer_free_copied_item ( gint subtype, gpointer item )
   }
 }
 
+static void image_cache_free ( VikTrwLayer *vtl )
+{
+  g_list_foreach ( vtl->image_cache->head, (GFunc)cached_pixbuf_free, NULL );
+  g_queue_free ( vtl->image_cache );
+}
+
 static gboolean trw_layer_set_param ( VikTrwLayer *vtl, guint16 id, VikLayerParamData data, VikViewport *vp, gboolean is_file_operation )
 {
   switch ( id )
@@ -1241,12 +1250,17 @@ static gboolean trw_layer_set_param ( VikTrwLayer *vtl, guint16 id, VikLayerPara
     case PARAM_IS: if ( data.u != vtl->image_size )
       {
         vtl->image_size = data.u;
-        g_list_foreach ( vtl->image_cache->head, (GFunc) cached_pixbuf_free, NULL );
-        g_queue_free ( vtl->image_cache );
+        image_cache_free ( vtl );
+        vtl->image_cache = g_queue_new ();
+      }
+      break;
+    case PARAM_IA: if ( data.u != vtl->image_alpha )
+      {
+        vtl->image_alpha = data.u;
+        image_cache_free ( vtl );
         vtl->image_cache = g_queue_new ();
       }
       break;
-    case PARAM_IA: vtl->image_alpha = data.u; break;
     case PARAM_ICS: vtl->image_cache_size = data.u;
       while ( vtl->image_cache->length > vtl->image_cache_size ) /* if shrinking cache_size, free pixbuf ASAP */
           cached_pixbuf_free ( g_queue_pop_tail ( vtl->image_cache ) );
@@ -1523,23 +1537,27 @@ static VikTrwLayer *trw_layer_unmarshall( guint8 *data, gint len, VikViewport *v
       // Reuse pl to read the subtype from the data stream
       memcpy(&pl, data+sizeof(gint), sizeof(pl));
 
+      // Also remember to (attempt to) convert each coordinate in case this is pasted into a different drawmode
       if ( pl == VIK_TRW_LAYER_SUBLAYER_TRACK ) {
         VikTrack *trk = vik_track_unmarshall ( data + sizeof_len_and_subtype, 0 );
         gchar *name = g_strdup ( trk->name );
         vik_trw_layer_add_track ( vtl, name, trk );
         g_free ( name );
+        vik_track_convert (trk, vtl->coord_mode);
       }
       if ( pl == VIK_TRW_LAYER_SUBLAYER_WAYPOINT ) {
         VikWaypoint *wp = vik_waypoint_unmarshall ( data + sizeof_len_and_subtype, 0 );
         gchar *name = g_strdup ( wp->name );
         vik_trw_layer_add_waypoint ( vtl, name, wp );
         g_free ( name );
+        waypoint_convert (NULL, wp, &vtl->coord_mode);
       }
       if ( pl == VIK_TRW_LAYER_SUBLAYER_ROUTE ) {
         VikTrack *trk = vik_track_unmarshall ( data + sizeof_len_and_subtype, 0 );
         gchar *name = g_strdup ( trk->name );
         vik_trw_layer_add_route ( vtl, name, trk );
         g_free ( name );
+        vik_track_convert (trk, vtl->coord_mode);
       }
     }
     consumed_length += tlm_size + sizeof_len_and_subtype;
@@ -1615,6 +1633,7 @@ static VikTrwLayer* trw_layer_new1 ( VikViewport *vvp )
   rv->metadata = vik_trw_metadata_new ();
   rv->draw_sync_done = TRUE;
   rv->draw_sync_do = TRUE;
+  rv->coord_mode = VIK_COORD_LATLON;
   // Everything else is 0, FALSE or NULL
 
   return rv;
@@ -1663,8 +1682,7 @@ static void trw_layer_free ( VikTrwLayer *trwlayer )
   if ( trwlayer->tracks_analysis_dialog != NULL )
     gtk_widget_destroy ( GTK_WIDGET(trwlayer->tracks_analysis_dialog) );
 
-  g_list_foreach ( trwlayer->image_cache->head, (GFunc) cached_pixbuf_free, NULL );
-  g_queue_free ( trwlayer->image_cache );
+  image_cache_free ( trwlayer );
 }
 
 static void init_drawing_params ( struct DrawingParams *dp, VikTrwLayer *vtl, VikViewport *vp, gboolean highlight )
@@ -2012,6 +2030,40 @@ static void trw_layer_draw_track_name_labels ( struct DrawingParams *dp, VikTrac
   g_free ( ename );
 }
 
+
+/**
+ * trw_layer_draw_point_names:
+ *
+ * Draw a point labels along a track
+ * This might slow things down if there's many tracks being displayed with this on.
+ */
+static void trw_layer_draw_point_names ( struct DrawingParams *dp, VikTrack *trk, gboolean drawing_highlight )
+{
+  GList *list = trk->trackpoints;
+  if (!list) return;
+  VikTrackpoint *tp = VIK_TRACKPOINT(list->data);
+  gchar *fgcolour;
+  if ( dp->vtl->drawmode == DRAWMODE_BY_TRACK )
+    fgcolour = gdk_color_to_string ( &(trk->color) );
+  else
+    fgcolour = gdk_color_to_string ( &(dp->vtl->track_color) );
+  gchar *bgcolour;
+  if ( drawing_highlight )
+    bgcolour = g_strdup ( vik_viewport_get_highlight_color ( dp->vp ) );
+  else
+    bgcolour = gdk_color_to_string ( &(dp->vtl->track_bg_color) );
+  if ( tp->name )
+    trw_layer_draw_track_label ( tp->name, fgcolour, bgcolour, dp, &tp->coord );
+  while ((list = g_list_next(list)))
+  {
+    tp = VIK_TRACKPOINT(list->data);
+    if ( tp->name )
+      trw_layer_draw_track_label ( tp->name, fgcolour, bgcolour, dp, &tp->coord );
+  };
+  g_free ( fgcolour );
+  g_free ( bgcolour );
+}
+
 static void trw_layer_draw_track ( const gpointer id, VikTrack *track, struct DrawingParams *dp, gboolean draw_track_outline )
 {
   if ( ! track->visible )
@@ -2138,7 +2190,7 @@ static void trw_layer_draw_track ( const gpointer id, VikTrack *track, struct Dr
          // Still need to process points to ensure 'stops' are drawn if required
          if ( drawstops && drawpoints && ! draw_track_outline && list->next &&
               (VIK_TRACKPOINT(list->next->data)->timestamp - VIK_TRACKPOINT(list->data)->timestamp > dp->vtl->stop_length) )
-           vik_viewport_draw_arc ( dp->vp, g_array_index(dp->vtl->track_gc, GdkGC *, 11), TRUE, x-(3*tp_size), y-(3*tp_size), 6*tp_size, 6*tp_size, 0, 360*64 );
+           vik_viewport_draw_arc ( dp->vp, g_array_index(dp->vtl->track_gc, GdkGC *, VIK_TRW_LAYER_TRACK_GC_STOP), TRUE, x-(3*tp_size), y-(3*tp_size), 6*tp_size, 6*tp_size, 0, 360*64 );
 
          goto skip;
        }
@@ -2280,6 +2332,7 @@ static void trw_layer_draw_track ( const gpointer id, VikTrack *track, struct Dr
       if ( track->max_number_dist_labels > 0 ) {
         trw_layer_draw_dist_labels ( dp, track, drawing_highlight );
       }
+      trw_layer_draw_point_names (dp, track, drawing_highlight );
 
       if ( track->draw_name_mode != TRACK_DRAWNAME_NO ) {
         trw_layer_draw_track_name_labels ( dp, track, drawing_highlight );
@@ -2352,6 +2405,10 @@ static void trw_layer_draw_waypoint ( const gpointer id, VikWaypoint *wp, struct
           }
           cp->image = g_strdup ( image );
 
+          // Apply alpha setting to the image before the pixbuf gets stored in the cache
+          if ( dp->vtl->image_alpha != 255 )
+            cp->pixbuf = ui_pixbuf_set_alpha ( cp->pixbuf, dp->vtl->image_alpha );
+
           /* needed so 'click picture' tool knows how big the pic is; we don't
            * store it in cp because they may have been freed already. */
           wp->image_width = gdk_pixbuf_get_width ( cp->pixbuf );
@@ -2385,10 +2442,7 @@ static void trw_layer_draw_waypoint ( const gpointer id, VikWaypoint *wp, struct
                                          x - (w/2) - 2, y - (h/2) - 2, w + 4, h + 4 );
           }
 
-          if ( dp->vtl->image_alpha == 255 )
-            vik_viewport_draw_pixbuf ( dp->vp, pixbuf, 0, 0, x - (w/2), y - (h/2), w, h );
-          else
-            vik_viewport_draw_pixbuf_with_alpha ( dp->vp, pixbuf, dp->vtl->image_alpha, 0, 0, x - (w/2), y - (h/2), w, h );
+          vik_viewport_draw_pixbuf ( dp->vp, pixbuf, 0, 0, x - (w/2), y - (h/2), w, h );
         }
         return; /* if failed to draw picture, default to drawing regular waypoint (below) */
       }
@@ -2772,6 +2826,8 @@ static void trw_layer_realize ( VikTrwLayer *vtl, VikTreeview *vt, GtkTreeIter *
   }
 
   trw_layer_verify_thumbnails ( vtl, NULL );
+
+  trw_layer_sort_all ( vtl );
 }
 
 static gboolean trw_layer_sublayer_toggle_visible ( VikTrwLayer *l, gint subtype, gpointer sublayer )
@@ -2939,9 +2995,9 @@ static const gchar* trw_layer_layer_tooltip ( VikTrwLayer *vtl )
       // Timing information if available
       tbuf1[0] = '\0';
       if ( tt.duration > 0 ) {
-       g_snprintf (tbuf1, sizeof(tbuf1),
-                   _(" in %d:%02d hrs:mins"),
-                   (int)round(tt.duration/3600), (int)round((tt.duration/60)%60));
+        g_snprintf (tbuf1, sizeof(tbuf1),
+                    _(" in %d:%02d hrs:mins"),
+                    (int)(tt.duration/3600), (int)round(tt.duration/60.0)%60);
       }
       g_snprintf (tbuf2, sizeof(tbuf2),
                  _("\n%sTotal Length %.1f %s%s"),
@@ -3030,9 +3086,9 @@ static const gchar* trw_layer_sublayer_tooltip ( VikTrwLayer *l, gint subtype, g
        if ( tr->trackpoints && vik_track_get_tp_first(tr)->has_timestamp ) {
          // %x     The preferred date representation for the current locale without the time.
          strftime (time_buf1, sizeof(time_buf1), "%x: ", gmtime(&(vik_track_get_tp_first(tr)->timestamp)));
-         time_t dur = vik_track_get_duration ( tr );
+         time_t dur = vik_track_get_duration ( tr, TRUE );
          if ( dur > 0 )
-           g_snprintf ( time_buf2, sizeof(time_buf2), _("- %d:%02d hrs:mins"), (int)round(dur/3600), (int)round((dur/60)%60) );
+           g_snprintf ( time_buf2, sizeof(time_buf2), _("- %d:%02d hrs:mins"), (int)(dur/3600), (int)round(dur/60.0)%60 );
        }
        // Get length and consider the appropriate distance units
        gdouble tr_len = vik_track_get_length(tr);
@@ -3267,7 +3323,7 @@ GHashTable *vik_trw_layer_get_routes_iters ( VikTrwLayer *vtl )
 
 GHashTable *vik_trw_layer_get_waypoints_iters ( VikTrwLayer *vtl )
 {
-  return vtl->waypoints;
+  return vtl->waypoints_iters;
 }
 
 gboolean vik_trw_layer_is_empty ( VikTrwLayer *vtl )
@@ -3394,51 +3450,7 @@ static void trw_layer_centerize ( menu_array_layer values )
 
 void trw_layer_zoom_to_show_latlons ( VikTrwLayer *vtl, VikViewport *vvp, struct LatLon maxmin[2] )
 {
-  /* First set the center [in case previously viewing from elsewhere] */
-  /* Then loop through zoom levels until provided positions are in view */
-  /* This method is not particularly fast - but should work well enough */
-  struct LatLon average = { (maxmin[0].lat+maxmin[1].lat)/2, (maxmin[0].lon+maxmin[1].lon)/2 };
-  VikCoord coord;
-  vik_coord_load_from_latlon ( &coord, vtl->coord_mode, &average );
-  vik_viewport_set_center_coord ( vvp, &coord, TRUE );
-
-  /* Convert into definite 'smallest' and 'largest' positions */
-  struct LatLon minmin;
-  if ( maxmin[0].lat < maxmin[1].lat )
-    minmin.lat = maxmin[0].lat;
-  else
-    minmin.lat = maxmin[1].lat;
-
-  struct LatLon maxmax;
-  if ( maxmin[0].lon > maxmin[1].lon )
-    maxmax.lon = maxmin[0].lon;
-  else
-    maxmax.lon = maxmin[1].lon;
-
-  /* Never zoom in too far - generally not that useful, as too close ! */
-  /* Always recalculate the 'best' zoom level */
-  gdouble zoom = 1.0;
-  vik_viewport_set_zoom ( vvp, zoom );
-
-  gdouble min_lat, max_lat, min_lon, max_lon;
-  /* Should only be a maximum of about 18 iterations from min to max zoom levels */
-  while ( zoom <= VIK_VIEWPORT_MAX_ZOOM ) {
-    vik_viewport_get_min_max_lat_lon ( vvp, &min_lat, &max_lat, &min_lon, &max_lon );
-    /* NB I think the logic used in this test to determine if the bounds is within view
-       fails if track goes across 180 degrees longitude.
-       Hopefully that situation is not too common...
-       Mind you viking doesn't really do edge locations to well anyway */
-    if ( min_lat < minmin.lat &&
-        max_lat > minmin.lat &&
-        min_lon < maxmax.lon &&
-        max_lon > maxmax.lon )
-      /* Found within zoom level */
-      break;
-
-    /* Try next */
-    zoom = zoom * 2;
-    vik_viewport_set_zoom ( vvp, zoom );
-  }
+  vu_zoom_to_show_latlons ( vtl->coord_mode, vvp, maxmin );
 }
 
 gboolean vik_trw_layer_auto_set_view ( VikTrwLayer *vtl, VikViewport *vvp )
@@ -3575,13 +3587,13 @@ static void trw_layer_goto_wp ( menu_array_layer values )
 
   GtkWidget *label, *entry;
   label = gtk_label_new(_("Waypoint Name:"));
-  entry = gtk_entry_new();
+  entry = ui_entry_new ( NULL, GTK_ENTRY_ICON_SECONDARY );
 
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), entry, FALSE, FALSE, 0);
-  gtk_widget_show_all ( label );
-  gtk_widget_show_all ( entry );
-
+  gtk_widget_show_all ( dia );
+  // 'ok' when press return in the entry
+  g_signal_connect_swapped ( entry, "activate", G_CALLBACK(a_dialog_response_accept), dia );
   gtk_dialog_set_default_response ( GTK_DIALOG(dia), GTK_RESPONSE_ACCEPT );
 
   while ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT )
@@ -3603,7 +3615,7 @@ static void trw_layer_goto_wp ( menu_array_layer values )
       udata.uuid = NULL;
 
       // Hmmm, want key of it
-      gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
+      gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
 
       if ( wpf && udata.uuid ) {
         GtkTreeIter *it = g_hash_table_lookup ( vtl->waypoints_iters, udata.uuid );
@@ -3933,9 +3945,10 @@ static void trw_layer_new_wp ( menu_array_layer values )
   VikLayersPanel *vlp = VIK_LAYERS_PANEL(values[MA_VLP]);
   /* TODO longone: okay, if layer above (aggregate) is invisible but vtl->visible is true, this redraws for no reason.
      instead return true if you want to update. */
-  if ( vik_trw_layer_new_waypoint ( vtl, VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_viewport_get_center(vik_layers_panel_get_viewport(vlp))) && VIK_LAYER(vtl)->visible ) {
+  if ( vik_trw_layer_new_waypoint ( vtl, VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_viewport_get_center(vik_layers_panel_get_viewport(vlp))) ) {
     trw_layer_calculate_bounds_waypoints ( vtl );
-    vik_layers_panel_emit_update ( vlp );
+    if ( VIK_LAYER(vtl)->visible )
+      vik_layers_panel_emit_update ( vlp );
   }
 }
 
@@ -4168,10 +4181,12 @@ static void trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer
   gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
   gtk_widget_show ( item );
 
-  item = gtk_menu_item_new_with_mnemonic ( _("Export as _KML...") );
-  g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_export_kml), pass_along );
-  gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
-  gtk_widget_show ( item );
+  if ( a_babel_available () ) {
+    item = gtk_menu_item_new_with_mnemonic ( _("Export as _KML...") );
+    g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_export_kml), pass_along );
+    gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
+    gtk_widget_show ( item );
+  }
 
   if ( have_geojson_export ) {
     item = gtk_menu_item_new_with_mnemonic ( _("Export as GEO_JSON...") );
@@ -4180,10 +4195,12 @@ static void trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer
     gtk_widget_show ( item );
   }
 
-  item = gtk_menu_item_new_with_mnemonic ( _("Export via GPSbabel...") );
-  g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_export_babel), pass_along );
-  gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
-  gtk_widget_show ( item );
+  if ( a_babel_available () ) {
+    item = gtk_menu_item_new_with_mnemonic ( _("Export via GPSbabel...") );
+    g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_export_babel), pass_along );
+    gtk_menu_shell_append (GTK_MENU_SHELL (export_submenu), item);
+    gtk_widget_show ( item );
+  }
 
   gchar* external1 = g_strdup_printf ( _("Open with External Program_1: %s"), a_vik_get_external_gpx_program_1() );
   item = gtk_menu_item_new_with_mnemonic ( external1 );
@@ -4305,11 +4322,13 @@ static void trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer
   gtk_widget_show ( item );
 #endif
 
-  item = gtk_menu_item_new_with_mnemonic ( _("From _File...") );
-  g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_acquire_file_cb), pass_along );
-  gtk_menu_shell_append (GTK_MENU_SHELL (acquire_submenu), item);
-  gtk_widget_set_tooltip_text (item, _("Import File With GPS_Babel..."));
-  gtk_widget_show ( item );
+  if ( a_babel_available () ) {
+    item = gtk_menu_item_new_with_mnemonic ( _("From _File...") );
+    g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(trw_layer_acquire_file_cb), pass_along );
+    gtk_menu_shell_append (GTK_MENU_SHELL (acquire_submenu), item);
+    gtk_widget_set_tooltip_text (item, _("Import File With GPS_Babel..."));
+    gtk_widget_show ( item );
+  }
 
   vik_ext_tool_datasources_add_menu_items_to_menu ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vtl)), GTK_MENU (acquire_submenu) );
 
@@ -4576,7 +4595,19 @@ gchar *trw_layer_new_unique_sublayer_name (VikTrwLayer *vtl, gint sublayer_type,
     }
     // If found a name already in use try adding 1 to it and we try again
     if ( id ) {
-      gchar *new_newname = g_strdup_printf("%s#%d", name, i);
+      const gchar *corename = newname;
+      gint newi = i;
+      // If name is already of the form text#N
+      //  set name to text and i to N+1
+      gchar **tokens = g_regex_split_simple ( "#(\\d+)", newname, G_REGEX_CASELESS, 0 );
+      if ( tokens ) {
+        corename = tokens[0];
+        if ( tokens[1] ) {
+          newi = atoi ( tokens[1] ) + 1;
+        }
+      }
+      gchar *new_newname = g_strdup_printf("%s#%d", corename, newi);
+      g_strfreev ( tokens );
       g_free(newname);
       newname = new_newname;
       i++;
@@ -4601,10 +4632,12 @@ void vik_trw_layer_filein_add_track ( VikTrwLayer *vtl, gchar *name, VikTrack *t
     // enforce end of current track equal to start of tr
     VikTrackpoint *cur_end = vik_track_get_tp_last ( vtl->current_track );
     VikTrackpoint *new_start = vik_track_get_tp_first ( tr );
-    if ( ! vik_coord_equals ( &cur_end->coord, &new_start->coord ) ) {
-        vik_track_add_trackpoint ( vtl->current_track,
-                                   vik_trackpoint_copy ( cur_end ),
-                                   FALSE );
+    if ( cur_end && new_start ) {
+      if ( ! vik_coord_equals ( &cur_end->coord, &new_start->coord ) ) {
+          vik_track_add_trackpoint ( vtl->current_track,
+                                     vik_trackpoint_copy ( cur_end ),
+                                     FALSE );
+      }
     }
 
     vik_track_steal_and_append_trackpoints ( vtl->current_track, tr );
@@ -4635,19 +4668,17 @@ static void trw_layer_enum_item ( gpointer id, GList **tr, GList **l )
  */
 static void trw_layer_move_item ( VikTrwLayer *vtl_src, VikTrwLayer *vtl_dest, gpointer id, gint type )
 {
+  // When an item is moved the name is checked to see if it clashes with an existing name
+  //  in the destination layer and if so then it is allocated a new name
+
   // TODO reconsider strategy when moving within layer (if anything...)
-  gboolean rename = ( vtl_src != vtl_dest );
-  if ( ! rename )
+  if ( vtl_src == vtl_dest )
     return;
 
   if (type == VIK_TRW_LAYER_SUBLAYER_TRACK) {
     VikTrack *trk = g_hash_table_lookup ( vtl_src->tracks, id );
 
-    gchar *newname;
-    if ( rename )
-      newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, trk->name );
-    else
-      newname = g_strdup ( trk->name );
+    gchar *newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, trk->name );
 
     VikTrack *trk2 = vik_track_copy ( trk, TRUE );
     vik_trw_layer_add_track ( vtl_dest, newname, trk2 );
@@ -4661,11 +4692,7 @@ static void trw_layer_move_item ( VikTrwLayer *vtl_src, VikTrwLayer *vtl_dest, g
   if (type == VIK_TRW_LAYER_SUBLAYER_ROUTE) {
     VikTrack *trk = g_hash_table_lookup ( vtl_src->routes, id );
 
-    gchar *newname;
-    if ( rename )
-      newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, trk->name );
-    else
-      newname = g_strdup ( trk->name );
+    gchar *newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, trk->name );
 
     VikTrack *trk2 = vik_track_copy ( trk, TRUE );
     vik_trw_layer_add_route ( vtl_dest, newname, trk2 );
@@ -4676,11 +4703,7 @@ static void trw_layer_move_item ( VikTrwLayer *vtl_src, VikTrwLayer *vtl_dest, g
   if (type == VIK_TRW_LAYER_SUBLAYER_WAYPOINT) {
     VikWaypoint *wp = g_hash_table_lookup ( vtl_src->waypoints, id );
 
-    gchar *newname;
-    if ( rename )
-      newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, wp->name );
-    else
-      newname = g_strdup ( wp->name );
+    gchar *newname = trw_layer_new_unique_sublayer_name ( vtl_dest, type, wp->name );
 
     VikWaypoint *wp2 = vik_waypoint_copy ( wp );
     vik_trw_layer_add_waypoint ( vtl_dest, newname, wp2 );
@@ -4767,7 +4790,7 @@ gboolean vik_trw_layer_delete_track ( VikTrwLayer *vtl, VikTrack *trk )
     udata.uuid = NULL;
 
     // Hmmm, want key of it
-    gpointer *trkf = g_hash_table_find ( vtl->tracks, (GHRFunc) trw_layer_track_find_uuid, &udata );
+    gpointer trkf = g_hash_table_find ( vtl->tracks, (GHRFunc) trw_layer_track_find_uuid, &udata );
 
     if ( trkf && udata.uuid ) {
       /* could be current_tp, so we have to check */
@@ -4815,7 +4838,7 @@ gboolean vik_trw_layer_delete_route ( VikTrwLayer *vtl, VikTrack *trk )
     udata.uuid = NULL;
 
     // Hmmm, want key of it
-    gpointer *trkf = g_hash_table_find ( vtl->routes, (GHRFunc) trw_layer_track_find_uuid, &udata );
+    gpointer trkf = g_hash_table_find ( vtl->routes, (GHRFunc) trw_layer_track_find_uuid, &udata );
 
     if ( trkf && udata.uuid ) {
       /* could be current_tp, so we have to check */
@@ -4859,7 +4882,7 @@ static gboolean trw_layer_delete_waypoint ( VikTrwLayer *vtl, VikWaypoint *wp )
     udata.uuid = NULL;
 
     // Hmmm, want key of it
-    gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
+    gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
 
     if ( wpf && udata.uuid ) {
       GtkTreeIter *it = g_hash_table_lookup ( vtl->waypoints_iters, udata.uuid );
@@ -4911,7 +4934,7 @@ static gboolean trw_layer_delete_waypoint_by_name ( VikTrwLayer *vtl, const gcha
   udata.uuid = NULL;
 
   // Hmmm, want key of it
-  gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid_by_name, (gpointer) &udata );
+  gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid_by_name, (gpointer) &udata );
 
   vik_waypoint_free (udata.wp);
 
@@ -4952,7 +4975,7 @@ static gboolean trw_layer_delete_track_by_name ( VikTrwLayer *vtl, const gchar *
   udata.uuid = NULL;
 
   // Hmmm, want key of it
-  gpointer *trkf = g_hash_table_find ( ht_tracks, (GHRFunc) trw_layer_track_find_uuid_by_name, &udata );
+  gpointer trkf = g_hash_table_find ( ht_tracks, (GHRFunc) trw_layer_track_find_uuid_by_name, &udata );
 
   vik_track_free (udata.trk);
 
@@ -5120,7 +5143,7 @@ void trw_layer_waypoint_rename ( VikTrwLayer *vtl, VikWaypoint *wp, const gchar
   udataU.uuid = NULL;
 
   // Need key of it for treeview update
-  gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, &udataU );
+  gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, &udataU );
 
   if ( wpf && udataU.uuid ) {
     GtkTreeIter *it = g_hash_table_lookup ( vtl->waypoints_iters, udataU.uuid );
@@ -5143,7 +5166,7 @@ void trw_layer_waypoint_reset_icon ( VikTrwLayer *vtl, VikWaypoint *wp )
   udataU.uuid = NULL;
 
   // Need key of it for treeview update
-  gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, &udataU );
+  gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, &udataU );
 
   if ( wpf && udataU.uuid ) {
     GtkTreeIter *it = g_hash_table_lookup ( vtl->waypoints_iters, udataU.uuid );
@@ -5230,7 +5253,7 @@ void trw_layer_update_treeview ( VikTrwLayer *vtl, VikTrack *trk )
   udata.trk  = trk;
   udata.uuid = NULL;
 
-  gpointer *trkf = NULL;
+  gpointer trkf = NULL;
   if ( trk->is_route )
     trkf = g_hash_table_find ( vtl->routes, (GHRFunc) trw_layer_track_find_uuid, &udata );
   else
@@ -6336,7 +6359,7 @@ static void trw_layer_split_at_selected_trackpoint ( VikTrwLayer *vtl, gint subt
       udata.uuid = NULL;
 
       // Also need id of newly created track
-      gpointer *trkf;
+      gpointer trkf;
       if ( tr->is_route )
          trkf = g_hash_table_find ( vtl->routes, (GHRFunc) trw_layer_track_find_uuid, &udata );
       else
@@ -7068,7 +7091,7 @@ static void vik_trw_layer_uniquify_tracks ( VikTrwLayer *vtl, VikLayersPanel *vl
     udataU.uuid = NULL;
 
     // Need want key of it for treeview update
-    gpointer *trkf = g_hash_table_find ( track_table, (GHRFunc) trw_layer_track_find_uuid, &udataU );
+    gpointer trkf = g_hash_table_find ( track_table, (GHRFunc) trw_layer_track_find_uuid, &udataU );
 
     if ( trkf && udataU.uuid ) {
 
@@ -7086,6 +7109,7 @@ static void vik_trw_layer_uniquify_tracks ( VikTrwLayer *vtl, VikLayersPanel *vl
           vik_treeview_sort_children ( VIK_LAYER(vtl)->vt, &(vtl->routes_iter), vtl->track_sort_order );
       }
     }
+    g_free ( newname );
 
     // Start trying to find same names again...
     track_names = NULL;
@@ -7344,6 +7368,8 @@ static void vik_trw_layer_uniquify_waypoints ( VikTrwLayer *vtl, VikLayersPanel
 
     trw_layer_waypoint_rename ( vtl, waypoint, newname );
 
+    g_free (newname);
+
     // Start trying to find same names again...
     waypoint_names = NULL;
     g_hash_table_foreach ( vtl->waypoints, (GHFunc) trw_layer_sorted_wp_id_by_name_list, &waypoint_names );
@@ -7856,7 +7882,7 @@ static void trw_layer_google_route_webpage ( menu_array_sublayer values )
 {
   VikTrack *tr = g_hash_table_lookup ( VIK_TRW_LAYER(values[MA_VTL])->routes, values[MA_SUBLAYER_ID] );
   if ( tr ) {
-    gchar *escaped = uri_escape ( tr->comment );
+    gchar *escaped = g_uri_escape_string ( tr->comment, NULL, TRUE );
     gchar *webpage = g_strdup_printf("http://maps.google.com/maps?f=q&hl=en&q=%s", escaped );
     open_url(VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(values[MA_VTL])), webpage);
     g_free ( escaped );
@@ -9043,7 +9069,7 @@ typedef struct {
   gint x, y;
   gint closest_x, closest_y;
   gboolean draw_images;
-  gpointer *closest_wp_id;
+  gpointer closest_wp_id;
   VikWaypoint *closest_wp;
   VikViewport *vvp;
 } WPSearchParams;
@@ -9438,7 +9464,7 @@ static gboolean trw_layer_show_selected_viewport_menu ( VikTrwLayer *vtl, GdkEve
       udataU.trk  = track;
       udataU.uuid = NULL;
 
-      gpointer *trkf;
+      gpointer trkf;
       if ( track->is_route )
         trkf = g_hash_table_find ( vtl->routes, (GHRFunc) trw_layer_track_find_uuid, &udataU );
       else
@@ -9481,7 +9507,7 @@ static gboolean trw_layer_show_selected_viewport_menu ( VikTrwLayer *vtl, GdkEve
       udata.wp   = waypoint;
       udata.uuid = NULL;
 
-      gpointer *wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
+      gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc) trw_layer_waypoint_find_uuid, (gpointer) &udata );
 
       if ( wpf && udata.uuid ) {
         GtkTreeIter *iter = g_hash_table_lookup ( vtl->waypoints_iters, udata.uuid );
@@ -10153,9 +10179,10 @@ static gboolean tool_new_waypoint_click ( VikTrwLayer *vtl, GdkEventButton *even
   if (!vtl || vtl->vl.type != VIK_LAYER_TRW)
     return FALSE;
   vik_viewport_screen_to_coord ( vvp, event->x, event->y, &coord );
-  if (vik_trw_layer_new_waypoint ( vtl, VIK_GTK_WINDOW_FROM_LAYER(vtl), &coord ) && VIK_LAYER(vtl)->visible) {
+  if ( vik_trw_layer_new_waypoint (vtl, VIK_GTK_WINDOW_FROM_LAYER(vtl), &coord) ) {
     trw_layer_calculate_bounds_waypoints ( vtl );
-    vik_layer_emit_update ( VIK_LAYER(vtl) );
+    if ( VIK_LAYER(vtl)->visible )
+      vik_layer_emit_update ( VIK_LAYER(vtl) );
   }
   return TRUE;
 }
@@ -10325,7 +10352,8 @@ static gboolean tool_edit_trackpoint_release ( VikTrwLayer *vtl, GdkEventButton
 
     /* diff dist is diff from orig */
     if ( vtl->tpwin )
-      my_tpwin_set_tp ( vtl );
+      if ( vtl->current_tp_track )
+        my_tpwin_set_tp ( vtl );
 
     vik_layer_emit_update ( VIK_LAYER(vtl) );
     return TRUE;