first part of MIDI cut/copy/paste ; fix for input/output_streams of an IOProcessor...
[ardour.git] / gtk2_ardour / editor_ops.cc
index a5dfb6b08e4f6c116c9722f0907d1922d8c279e8..0a7543b214a4ae6ce233a47bd7fbd5a06c253a07 100644 (file)
 #include <map>
 #include <set>
 
-#include <pbd/error.h>
-#include <pbd/basename.h>
-#include <pbd/pthread_utils.h>
-#include <pbd/memento_command.h>
-#include <pbd/whitespace.h>
+#include "pbd/error.h"
+#include "pbd/basename.h"
+#include "pbd/pthread_utils.h"
+#include "pbd/memento_command.h"
+#include "pbd/whitespace.h"
 
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/choice.h>
 #include <gtkmm2ext/window_title.h>
 #include <gtkmm2ext/popup.h>
 
-#include <ardour/audioengine.h>
-#include <ardour/session.h>
-#include <ardour/audioplaylist.h>
-#include <ardour/audioregion.h>
-#include <ardour/audio_diskstream.h>
-#include <ardour/utils.h>
-#include <ardour/location.h>
-#include <ardour/named_selection.h>
-#include <ardour/audio_track.h>
-#include <ardour/audiofilesource.h>
-#include <ardour/audioplaylist.h>
-#include <ardour/region_factory.h>
-#include <ardour/playlist_factory.h>
-#include <ardour/reverse.h>
-#include <ardour/transient_detector.h>
-#include <ardour/dB.h>
-#include <ardour/quantize.h>
+#include "ardour/audioengine.h"
+#include "ardour/session.h"
+#include "ardour/audioplaylist.h"
+#include "ardour/audioregion.h"
+#include "ardour/audio_diskstream.h"
+#include "ardour/utils.h"
+#include "ardour/location.h"
+#include "ardour/named_selection.h"
+#include "ardour/audio_track.h"
+#include "ardour/audioplaylist.h"
+#include "ardour/region_factory.h"
+#include "ardour/playlist_factory.h"
+#include "ardour/reverse.h"
+#include "ardour/transient_detector.h"
+#include "ardour/dB.h"
+#include "ardour/quantize.h"
+#include "ardour/strip_silence.h"
+#include "ardour/route_group.h"
 
 #include "ardour_ui.h"
 #include "editor.h"
 #include "gui_thread.h"
 #include "keyboard.h"
 #include "utils.h"
+#include "editor_drag.h"
+#include "strip_silence_dialog.h"
+#include "editor_routes.h"
+#include "editor_regions.h"
 
 #include "i18n.h"
 
@@ -107,17 +112,11 @@ Editor::redo (uint32_t n)
 
 void
 Editor::split_region ()
-{
-       split_region_at (get_preferred_edit_position());
-}
-
-void
-Editor::split_region_at (nframes64_t where)
 {
        RegionSelection rs;
 
-       get_regions_for_action (rs);
-       split_regions_at (where, selection->regions);
+       get_regions_for_action (rs, true);
+       split_regions_at (get_preferred_edit_position (), selection->regions);
 }
 
 void
@@ -174,100 +173,32 @@ Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
                }
 
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*a);
-
                if (arv) {
                        _new_regionviews_show_envelope = arv->envelope_visible();
                }
                
                if (pl) {
-                        XMLNode &before = pl->get_state();
+                       XMLNode &before = pl->get_state();
                        pl->split_region ((*a)->region(), where);
-                        XMLNode &after = pl->get_state();
-                        session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
+                       XMLNode &after = pl->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
                }
 
                a = tmp;
        }
-       while (used_playlists.size() > 0) {
 
+       while (used_playlists.size() > 0) {
                list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
                (*i)->thaw();
                used_playlists.pop_front();
        }
-       commit_reversible_command ();
-       _new_regionviews_show_envelope = false;
-}
-
-
-/** Remove `clicked_regionview' */
-void
-Editor::remove_clicked_region ()
-{
-       if (clicked_routeview == 0 || clicked_regionview == 0) {
-               return;
-       }
-
-       boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
-       
-       begin_reversible_command (_("remove region"));
-        XMLNode &before = playlist->get_state();
-       playlist->remove_region (clicked_regionview->region());
-        XMLNode &after = playlist->get_state();
-       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
-       commit_reversible_command ();
-}
-
-
-/** Remove the selected regions */
-void
-Editor::remove_selected_regions ()
-{
-       RegionSelection rs; 
-       get_regions_for_action (rs);
-       
-       if (!session) {
-               return;
-       }
-
-       if (rs.empty()) {
-               return;
-       }
-
-       begin_reversible_command (_("remove region"));
-
-       list<boost::shared_ptr<Region> > regions_to_remove;
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               // we can't just remove the region(s) in this loop because
-               // this removes them from the RegionSelection, and they thus
-               // disappear from underneath the iterator, and the ++i above
-               // SEGVs in a puzzling fashion.
-
-               // so, first iterate over the regions to be removed from rs and
-               // add them to the regions_to_remove list, and then
-               // iterate over the list to actually remove them.
-               
-               regions_to_remove.push_back ((*i)->region());
-       }
        
-       for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
-               boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
-               if (!playlist) {
-                       // is this check necessary?
-                       continue;
-               }
-
-               XMLNode &before = playlist->get_state();
-               playlist->remove_region (*rl);
-               XMLNode &after = playlist->get_state();
-               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
-       }
-
        commit_reversible_command ();
+       _new_regionviews_show_envelope = false;
 }
 
 boost::shared_ptr<Region>
-Editor::select_region_for_operation (int dir, TimeAxisView **tv)
+Editor::select_region_for_operation (int /*dir*/, TimeAxisView **tv)
 {
        RegionView* rv;
        boost::shared_ptr<Region> region;
@@ -711,7 +642,8 @@ Editor::build_region_boundary_cache ()
                                break;  
 
                        case SyncPoint:
-                               rpos = r->adjust_to_sync (r->first_frame());
+                               rpos = r->sync_position ();
+                               //r->adjust_to_sync (r->first_frame());
                                break;
 
                        default:
@@ -797,7 +729,8 @@ Editor::find_next_region (nframes64_t frame, RegionPoint point, int32_t dir, Tra
                        break;
 
                case SyncPoint:
-                       rpos = r->adjust_to_sync (r->first_frame());
+                       rpos = r->sync_position ();
+                       // r->adjust_to_sync (r->first_frame());
                        break;
                }
 
@@ -827,6 +760,7 @@ Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackView
        nframes64_t distance = max_frames;
        nframes64_t current_nearest = -1;
 
+
        for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
                nframes64_t contender;
                nframes64_t d;
@@ -852,10 +786,45 @@ Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackView
        return current_nearest;
 }
 
+nframes64_t
+Editor::get_region_boundary (nframes64_t pos, int32_t dir, bool with_selection, bool only_onscreen)
+{
+       nframes64_t target;
+       TrackViewList tvl;
+
+       if (with_selection && Config->get_region_boundaries_from_selected_tracks()) {
+
+               if (!selection->tracks.empty()) {
+                       
+                       target = find_next_region_boundary (pos, dir, selection->tracks);
+                       
+               } else {
+                       
+                       if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
+                               get_onscreen_tracks (tvl);
+                               target = find_next_region_boundary (pos, dir, tvl);
+                       } else {
+                               target = find_next_region_boundary (pos, dir, track_views);
+                       }
+               }
+               
+       } else {
+
+               if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
+                       get_onscreen_tracks (tvl);
+                       target = find_next_region_boundary (pos, dir, tvl);
+               } else {
+                       target = find_next_region_boundary (pos, dir, track_views);
+               }
+       }
+       
+       return target;
+}
+
 void
-Editor::cursor_to_region_boundary (Cursor* cursor, int32_t dir)
+Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
 {
-       nframes64_t pos = cursor->current_frame;
+       nframes64_t pos = playhead_cursor->current_frame;
        nframes64_t target;
 
        if (!session) {
@@ -867,41 +836,28 @@ Editor::cursor_to_region_boundary (Cursor* cursor, int32_t dir)
                pos += dir;
        }
 
-       if (!selection->tracks.empty()) {
-               
-               target = find_next_region_boundary (pos, dir, selection->tracks);
-               
-       } else {
-               
-               target = find_next_region_boundary (pos, dir, track_views);
-       }
-       
-       if (target < 0) {
+       if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
                return;
        }
 
 
-       if (cursor == playhead_cursor) {
-               session->request_locate (target);
-       } else {
-               cursor->set_position (target);
-       }
+       session->request_locate (target);
 }
 
 void
-Editor::cursor_to_next_region_boundary (Cursor* cursor)
+Editor::cursor_to_next_region_boundary (bool with_selection)
 {
-       cursor_to_region_boundary (cursor, 1);
+       cursor_to_region_boundary (with_selection, 1);
 }
 
 void
-Editor::cursor_to_previous_region_boundary (Cursor* cursor)
+Editor::cursor_to_previous_region_boundary (bool with_selection)
 {
-       cursor_to_region_boundary (cursor, -1);
+       cursor_to_region_boundary (with_selection, -1);
 }
 
 void
-Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
+Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
 {
        boost::shared_ptr<Region> r;
        nframes64_t pos = cursor->current_frame;
@@ -946,7 +902,8 @@ Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
                break;
 
        case SyncPoint:
-               pos = r->adjust_to_sync (r->first_frame());
+               pos = r->sync_position ();
+               // r->adjust_to_sync (r->first_frame());
                break;  
        }
        
@@ -969,19 +926,19 @@ Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
 }
 
 void
-Editor::cursor_to_next_region_point (Cursor* cursor, RegionPoint point)
+Editor::cursor_to_next_region_point (EditorCursor* cursor, RegionPoint point)
 {
        cursor_to_region_point (cursor, point, 1);
 }
 
 void
-Editor::cursor_to_previous_region_point (Cursor* cursor, RegionPoint point)
+Editor::cursor_to_previous_region_point (EditorCursor* cursor, RegionPoint point)
 {
        cursor_to_region_point (cursor, point, -1);
 }
 
 void
-Editor::cursor_to_selection_start (Cursor *cursor)
+Editor::cursor_to_selection_start (EditorCursor *cursor)
 {
        nframes64_t pos = 0;
        RegionSelection rs; 
@@ -1013,7 +970,7 @@ Editor::cursor_to_selection_start (Cursor *cursor)
 }
 
 void
-Editor::cursor_to_selection_end (Cursor *cursor)
+Editor::cursor_to_selection_end (EditorCursor *cursor)
 {
        nframes64_t pos = 0;
        RegionSelection rs; 
@@ -1045,7 +1002,7 @@ Editor::cursor_to_selection_end (Cursor *cursor)
 }
 
 void
-Editor::selected_marker_to_region_boundary (int32_t dir)
+Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
 {
        nframes64_t target;
        Location* loc;
@@ -1077,16 +1034,7 @@ Editor::selected_marker_to_region_boundary (int32_t dir)
                pos += dir;
        }
 
-       if (!selection->tracks.empty()) {
-               
-               target = find_next_region_boundary (pos, dir, selection->tracks);
-               
-       } else {
-               
-               target = find_next_region_boundary (pos, dir, track_views);
-       }
-       
-       if (target < 0) {
+       if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
                return;
        }
 
@@ -1094,15 +1042,15 @@ Editor::selected_marker_to_region_boundary (int32_t dir)
 }
 
 void
-Editor::selected_marker_to_next_region_boundary ()
+Editor::selected_marker_to_next_region_boundary (bool with_selection)
 {
-       selected_marker_to_region_boundary (1);
+       selected_marker_to_region_boundary (with_selection, 1);
 }
 
 void
-Editor::selected_marker_to_previous_region_boundary ()
+Editor::selected_marker_to_previous_region_boundary (bool with_selection)
 {
-       selected_marker_to_region_boundary (-1);
+       selected_marker_to_region_boundary (with_selection, -1);
 }
 
 void
@@ -1157,11 +1105,11 @@ Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
        }
        
        float speed = 1.0f;
-       AudioTimeAxisView *atav;
+       RouteTimeAxisView *rtav;
 
-       if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
+       if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0) {
+               if (rtav->get_diskstream() != 0) {
+                       speed = rtav->get_diskstream()->speed();
                }
        }
 
@@ -1465,7 +1413,7 @@ void
 Editor::scroll_backward (float pages)
 {
        nframes64_t frame;
-       nframes64_t one_page = (nframes64_t) rint (canvas_width * frames_per_unit);
+       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
        nframes64_t cnt;
@@ -1493,7 +1441,7 @@ void
 Editor::scroll_forward (float pages)
 {
        nframes64_t frame;
-       nframes64_t one_page = (nframes64_t) rint (canvas_width * frames_per_unit);
+       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
        nframes64_t cnt;
@@ -1532,8 +1480,8 @@ Editor::scroll_tracks_down ()
 
        double vert_value = vertical_adjustment.get_value() + (cnt *
                vertical_adjustment.get_page_size());
-       if (vert_value > vertical_adjustment.get_upper() - canvas_height) {
-               vert_value = vertical_adjustment.get_upper() - canvas_height;
+       if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
+               vert_value = vertical_adjustment.get_upper() - _canvas_height;
        }
        vertical_adjustment.set_value (vert_value);
 }
@@ -1561,8 +1509,8 @@ Editor::scroll_tracks_down_line ()
         Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
        double vert_value = adj->get_value() + 60;
 
-       if (vert_value>adj->get_upper() - canvas_height) {
-               vert_value = adj->get_upper() - canvas_height;
+       if (vert_value>adj->get_upper() - _canvas_height) {
+               vert_value = adj->get_upper() - _canvas_height;
        }
        adj->set_value (vert_value);
 }
@@ -1619,7 +1567,7 @@ Editor::temporal_zoom (gdouble fpu)
 
        nfpu = fpu;
        
-       new_page_size = (nframes64_t) floor (canvas_width * nfpu);
+       new_page_size = (nframes64_t) floor (_canvas_width * nfpu);
        half_page_size = new_page_size / 2;
 
        switch (zoom_focus) {
@@ -1762,7 +1710,7 @@ Editor::temporal_zoom_region (bool both_axes)
        }
 
        nframes64_t range = end - start;
-       double new_fpu = (double)range / (double)canvas_width;
+       double new_fpu = (double)range / (double)_canvas_width;
        nframes64_t extra_samples = (nframes64_t) floor (one_centimeter_in_pixels * new_fpu);
 
        if (start > extra_samples) {
@@ -1788,7 +1736,7 @@ Editor::temporal_zoom_region (bool both_axes)
        temporal_zoom_by_frame (start, end, "zoom to region");
 
        if (both_axes) {
-               uint32_t per_track_height = (uint32_t) floor ((canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
+               uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
                
                /* set visible track heights appropriately */
                
@@ -1797,8 +1745,8 @@ Editor::temporal_zoom_region (bool both_axes)
                }
                
                /* hide irrelevant tracks */
-               
-               no_route_list_redisplay = true;
+
+               _routes->suspend_redisplay ();
 
                for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                        if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
@@ -1806,8 +1754,7 @@ Editor::temporal_zoom_region (bool both_axes)
                        }
                }
 
-               no_route_list_redisplay = false;
-               redisplay_route_list ();
+               _routes->resume_redisplay ();
 
                vertical_adjustment.set_value (0.0);
                no_save_visual = false;
@@ -1853,7 +1800,7 @@ Editor::temporal_zoom_session ()
 }
 
 void
-Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string & op)
+Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string & /*op*/)
 {
        if (!session) return;
 
@@ -1863,9 +1810,9 @@ Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string
 
        nframes64_t range = end - start;
 
-       double new_fpu = (double)range / (double)canvas_width;
+       double new_fpu = (double)range / (double)_canvas_width;
        
-       nframes64_t new_page = (nframes64_t) floor (canvas_width * new_fpu);
+       nframes64_t new_page = (nframes64_t) floor (_canvas_width * new_fpu);
        nframes64_t middle = (nframes64_t) floor( (double)start + ((double)range / 2.0f ));
        nframes64_t new_leftmost = (nframes64_t) floor( (double)middle - ((double)new_page/2.0f));
 
@@ -2261,7 +2208,6 @@ Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
 {
        double wx, wy;
        double cx, cy;
-       TimeAxisView *tv;
        nframes64_t where;
        RouteTimeAxisView *rtv = 0;
        boost::shared_ptr<Playlist> playlist;
@@ -2281,12 +2227,13 @@ Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
                /* clearly outside canvas area */
                return;
        }
-       
-       if ((tv = trackview_by_y_position (cy)) == 0) {
+
+       std::pair<TimeAxisView*, int> tv = trackview_by_y_position (cy);
+       if (tv.first == 0) {
                return;
        }
        
-       if ((rtv = dynamic_cast<RouteTimeAxisView*>(tv)) == 0) {
+       if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
                return;
        }
 
@@ -2307,7 +2254,6 @@ void
 Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y) {
        double wx, wy;
        double cx, cy;
-       TimeAxisView *tv;
        nframes_t where;
        RouteTimeAxisView *dest_rtv = 0;
        RouteTimeAxisView *source_rtv = 0;
@@ -2323,11 +2269,12 @@ Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y) {
 
        where = event_frame (&event, &cx, &cy);
 
-       if ((tv = trackview_by_y_position (cy)) == 0) {
+       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
+       if (tv.first == 0) {
                return;
        }
 
-       if ((dest_rtv = dynamic_cast<RouteTimeAxisView*>(tv)) == 0) {
+       if ((dest_rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
                return;
        }
 
@@ -2368,29 +2315,17 @@ Editor::insert_region_list_selection (float times)
        if ((playlist = tv->playlist()) == 0) {
                return;
        }
-       
-       Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
-       
-       if (selected->count_selected_rows() != 1) {
+
+       boost::shared_ptr<Region> region = _regions->get_single_selection ();
+       if (region == 0) {
                return;
        }
-       
-       TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
-
-       /* only one row selected, so rows.begin() is it */
-
-       TreeIter iter;
-
-       if ((iter = region_list_model->get_iter (*rows.begin()))) {
-
-               boost::shared_ptr<Region> region = (*iter)[region_list_columns.region];
                
-               begin_reversible_command (_("insert region"));
-               XMLNode &before = playlist->get_state();
-               playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
-               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
-               commit_reversible_command ();
-       } 
+       begin_reversible_command (_("insert region"));
+       XMLNode &before = playlist->get_state();
+       playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+       commit_reversible_command ();
 }
 
 /* BUILT-IN EFFECTS */
@@ -2644,7 +2579,7 @@ Editor::rename_region()
                strip_whitespace_edges (str);
                if (!str.empty()) {
                        rs.front()->region()->set_name (str);
-                       redisplay_regions ();
+                       _regions->redisplay ();
                }
        }
 }
@@ -2758,13 +2693,13 @@ Editor::region_from_selection ()
        nframes64_t start = selection->time[clicked_selection].start;
        nframes64_t end = selection->time[clicked_selection].end;
 
+       TrackSelection tracks = get_tracks_for_range_action ();
+
        nframes64_t selection_cnt = end - start + 1;
        
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-               boost::shared_ptr<AudioRegion> current;
-               boost::shared_ptr<Region> current_r;
+       for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               boost::shared_ptr<Region> current;
                boost::shared_ptr<Playlist> pl;
-
                nframes64_t internal_start;
                string new_name;
 
@@ -2772,17 +2707,14 @@ Editor::region_from_selection ()
                        continue;
                }
 
-               if ((current_r = pl->top_region_at (start)) == 0) {
+               if ((current = pl->top_region_at (start)) == 0) {
                        continue;
                }
 
-               current = boost::dynamic_pointer_cast<AudioRegion> (current_r);
-               assert(current); // FIXME
-               if (current != 0) {
-                       internal_start = start - current->position();
-                       session->region_name (new_name, current->name(), true);
-                       boost::shared_ptr<Region> region (RegionFactory::create (current, internal_start, selection_cnt, new_name));
-               }
+               internal_start = start - current->position();
+               session->region_name (new_name, current->name(), true);
+               boost::shared_ptr<Region> region (RegionFactory::create (current,
+                               internal_start, selection_cnt, new_name));
        }
 }      
 
@@ -2799,9 +2731,7 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
        sort_track_selection ();
 
        for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               boost::shared_ptr<AudioRegion> current;
-               boost::shared_ptr<Region> current_r;
+               boost::shared_ptr<Region> current;
                boost::shared_ptr<Playlist> playlist;
                nframes64_t internal_start;
                string new_name;
@@ -2810,18 +2740,15 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
                        continue;
                }
 
-               if ((current_r = playlist->top_region_at(start)) == 0) {
+               if ((current = playlist->top_region_at(start)) == 0) {
                        continue;
                }
 
-               if ((current = boost::dynamic_pointer_cast<AudioRegion>(current_r)) == 0) {
-                       continue;
-               }
-       
                internal_start = start - current->position();
                session->region_name (new_name, current->name(), true);
-               
-               new_regions.push_back (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (current, internal_start, end - start + 1, new_name)));
+
+               new_regions.push_back (RegionFactory::create (current,
+                                       internal_start, end - start + 1, new_name));
        }
 }
 
@@ -2836,17 +2763,10 @@ Editor::split_multichannel_region ()
                return;
        }
 
-       vector<boost::shared_ptr<AudioRegion> > v;
+       vector< boost::shared_ptr<Region> > v;
 
        for (list<RegionView*>::iterator x = rs.begin(); x != rs.end(); ++x) {
-
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*x);
-               
-               if (!arv || arv->audio_region()->n_channels() < 2) {
-                       continue;
-               }
-
-               (arv)->audio_region()->separate_by_channel (*session, v);
+               (*x)->region()->separate_by_channel (*session, v);
        }
 }
 
@@ -2868,45 +2788,53 @@ add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
        }
 }
 
-void
-Editor::separate_regions_between (const TimeSelection& ts)
+/** Return either:
+ *    - selected tracks, or if there are none...
+ *    - tracks containing selected regions, or if there are none...
+ *    - all tracks
+ * @return tracks.
+ */
+TrackSelection
+Editor::get_tracks_for_range_action () const
 {
-       bool in_command = false;
-       boost::shared_ptr<Playlist> playlist;
-       RegionSelection new_selection;
-       TrackSelection tmptracks;
-
+       TrackSelection t;
+       
        if (selection->tracks.empty()) {
                
                /* use tracks with selected regions */
 
-               RegionSelection rs; 
-
-               get_regions_for_action (rs);
+               RegionSelection rs = selection->regions;
 
                for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                        TimeAxisView* tv = &(*i)->get_time_axis_view();
 
-                       if (find (tmptracks.begin(), tmptracks.end(), tv) == tmptracks.end()) {
-                               tmptracks.push_back (tv);
+                       if (!t.contains (tv)) {
+                               t.push_back (tv);
                        }
                }
 
-               if (tmptracks.empty()) {
-                       /* no regions selected: do nothing */
-                       return;
+               if (t.empty()) {
+                       /* no regions and no tracks: use all tracks */
+                       t = track_views;
                }
 
        } else {
 
-               tmptracks = selection->tracks;
-
+               t = selection->tracks;
        }
 
-       sort_track_selection (&tmptracks);
-
+       return t;
+}
 
+void
+Editor::separate_regions_between (const TimeSelection& ts)
+{
+       bool in_command = false;
+       boost::shared_ptr<Playlist> playlist;
+       RegionSelection new_selection;
 
+       TrackSelection tmptracks = get_tracks_for_range_action ();
+       sort_track_selection (&tmptracks);
 
        for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
 
@@ -2937,10 +2865,12 @@ Editor::separate_regions_between (const TimeSelection& ts)
 
                                        for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
 
-                                               sigc::connection c = rtv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+                                               sigc::connection c = rtv->view()->RegionViewAdded.connect (
+                                                               mem_fun(*this, &Editor::collect_new_region_view));
                                                latest_regionviews.clear ();
 
-                                               playlist->partition ((nframes64_t)((*t).start * speed), (nframes64_t)((*t).end * speed), true);
+                                               playlist->partition ((nframes64_t)((*t).start * speed),
+                                                               (nframes64_t)((*t).end * speed), true);
 
                                                c.disconnect ();
 
@@ -2948,15 +2878,17 @@ Editor::separate_regions_between (const TimeSelection& ts)
                                                        
                                                        got_some = true;
 
-                                                       rtv->view()->foreach_regionview (bind (sigc::ptr_fun (add_if_covered), &(*t), &new_selection));
+                                                       rtv->view()->foreach_regionview (bind (
+                                                                               sigc::ptr_fun (add_if_covered),
+                                                                               &(*t), &new_selection));
                                                        
                                                        if (!in_command) {
                                                                begin_reversible_command (_("separate"));
                                                                in_command = true;
                                                        }
                                                        
-                                                       session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
-                                                       
+                                                       session->add_command(new MementoCommand<Playlist>(
+                                                                       *playlist, before, &playlist->get_state()));
                                                } 
                                        }
 
@@ -2976,6 +2908,10 @@ Editor::separate_regions_between (const TimeSelection& ts)
        }
 }
 
+/** Take tracks from get_tracks_for_range_action and cut any regions
+ *  on those tracks so that the tracks are empty over the time
+ *  selection.
+ */
 void
 Editor::separate_region_from_selection ()
 {
@@ -3184,16 +3120,11 @@ Editor::region_fill_selection ()
                return;
        }
 
-
-       Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
-
-       if (selected->count_selected_rows() != 1) {
+       boost::shared_ptr<Region> region = _regions->get_single_selection ();
+       if (region == 0) {
                return;
        }
 
-       TreeModel::iterator i = region_list_display.get_selection()->get_selected();
-       boost::shared_ptr<Region> region = (*i)[region_list_columns.region];
-
        nframes64_t start = selection->time[clicked_selection].start;
        nframes64_t end = selection->time[clicked_selection].end;
 
@@ -3476,6 +3407,49 @@ Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint poi
        session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
 }      
 
+void
+Editor::trim_region_front ()
+{
+       trim_region (true);
+}
+
+void
+Editor::trim_region_back ()
+{
+       trim_region (false);
+}
+
+void
+Editor::trim_region (bool front)
+{
+       nframes64_t where = get_preferred_edit_position();
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (front ? _("trim front") : _("trim back"));
+
+       for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
+               if (!(*i)->region()->locked()) {
+                       boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
+                       XMLNode &before = pl->get_state();
+                       if (front) {
+                               (*i)->region()->trim_front (where, this);       
+                       } else {
+                               (*i)->region()->trim_end (where, this); 
+                       }
+                       XMLNode &after = pl->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+               }
+       }
+
+       commit_reversible_command ();
+}
+
 /** Trim the end of the selected regions to the position of the edit cursor */
 void
 Editor::trim_region_to_loop ()
@@ -3506,24 +3480,18 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
        begin_reversible_command (str);
 
        for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
-
-               if (!arv) {
-                       continue;
-               }
+               RegionView* rv = (*x);
 
                /* require region to span proposed trim */
-
-               switch (arv->region()->coverage (loc.start(), loc.end())) {
+               switch (rv->region()->coverage (loc.start(), loc.end())) {
                case OverlapInternal:
                        break;
                default:
                        continue;
                }
                                
-               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
-
-               if (!atav) {
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
                        return;
                }
 
@@ -3531,17 +3499,18 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
                nframes64_t start;
                nframes64_t end;
 
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
                }
                
                start = session_frame_to_track_frame (loc.start(), speed);
                end = session_frame_to_track_frame (loc.end(), speed);
 
-               XMLNode &before = arv->region()->playlist()->get_state();
-               arv->region()->trim_to (start, (end - start), this);
-               XMLNode &after = arv->region()->playlist()->get_state();
-               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_to (start, (end - start), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
        }
 
        commit_reversible_command ();
@@ -3559,34 +3528,29 @@ Editor::trim_region_to_edit_point ()
        begin_reversible_command (_("trim region start to edit point"));
 
        for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
-
-               if (!arv) {
-                       continue;
-               }
+               RegionView* rv = (*x);
 
                /* require region to cover trim */
-
-               if (!arv->region()->covers (where)) {
+               if (!rv->region()->covers (where)) {
                        continue;
                }
 
-               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
-
-               if (!atav) {
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
                        return;
                }
 
                float speed = 1.0;
 
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
                }
 
-               XMLNode &before = arv->region()->playlist()->get_state();
-               arv->region()->trim_end( session_frame_to_track_frame(where, speed), this);
-               XMLNode &after = arv->region()->playlist()->get_state();
-               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_end( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
        }
                
        commit_reversible_command ();
@@ -3604,15 +3568,62 @@ Editor::trim_region_from_edit_point ()
        begin_reversible_command (_("trim region end to edit point"));
 
        for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+               RegionView* rv = (*x);
 
-               if (!arv) {
+               /* require region to cover trim */
+               if (!rv->region()->covers (where)) {
                        continue;
                }
 
-               /* require region to cover trim */
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
+                       return;
+               }
+
+               float speed = 1.0;
+
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
+               }
+
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_front ( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
+       }
+               
+       commit_reversible_command ();
+}
 
-               if (!arv->region()->covers (where)) {
+void
+Editor::trim_region_to_previous_region_end ()
+{
+       return trim_to_region(false);
+}
+
+void
+Editor::trim_region_to_next_region_start ()
+{
+       return trim_to_region(true);
+}
+
+void
+Editor::trim_to_region(bool forward)
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       begin_reversible_command (_("trim to region"));
+
+       boost::shared_ptr<Region> next_region;
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
                        continue;
                }
 
@@ -3628,23 +3639,50 @@ Editor::trim_region_from_edit_point ()
                        speed = atav->get_diskstream()->speed();
                }
 
-               XMLNode &before = arv->region()->playlist()->get_state();
-               arv->region()->trim_front ( session_frame_to_track_frame(where, speed), this);
-               XMLNode &after = arv->region()->playlist()->get_state();
-               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
-       }
                
+               boost::shared_ptr<Region> region = arv->region();
+               boost::shared_ptr<Playlist> playlist (region->playlist());
+               
+               XMLNode &before = playlist->get_state();
+
+               if(forward){
+
+                   next_region = playlist->find_next_region (region->first_frame(), Start, 1);
+
+                   if(!next_region){
+                       continue;
+                   }
+
+                   region->trim_end((nframes64_t) (next_region->first_frame() * speed), this);
+                   arv->region_changed (Change (LengthChanged));
+               }
+               else {
+
+                   next_region = playlist->find_next_region (region->first_frame(), Start, 0);
+
+                   if(!next_region){
+                       continue;
+                   }
+
+                   region->trim_front((nframes64_t) ((next_region->last_frame() + 1) * speed), this);              
+                   arv->region_changed (Change (LengthChanged|PositionChanged|StartChanged));
+               }
+
+               XMLNode &after = playlist->get_state();
+               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+       }
+
        commit_reversible_command ();
 }
 
 void
 Editor::unfreeze_route ()
 {
-       if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
+       if (clicked_routeview == 0 || !clicked_routeview->is_track()) {
                return;
        }
        
-       clicked_routeview->audio_track()->unfreeze ();
+       clicked_routeview->track()->unfreeze ();
 }
 
 void*
@@ -3658,13 +3696,14 @@ void*
 Editor::freeze_thread ()
 {
        clicked_routeview->audio_track()->freeze (*current_interthread_info);
+       current_interthread_info->done = true;
        return 0;
 }
 
 gint
-Editor::freeze_progress_timeout (void *arg)
+Editor::freeze_progress_timeout (void */*arg*/)
 {
-       interthread_progress_bar.set_fraction (current_interthread_info->progress/100);
+       interthread_progress_bar.set_fraction (current_interthread_info->progress);
        return !(current_interthread_info->done || current_interthread_info->cancel);
 }
 
@@ -3702,7 +3741,7 @@ Editor::freeze_route ()
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 500000);
 
-       pthread_create (&itt.thread, &attr, _freeze_thread, this);
+       pthread_create_and_store (X_("freezer"), &itt.thread, &attr, _freeze_thread, this);
 
        pthread_attr_destroy(&attr);
 
@@ -3719,7 +3758,7 @@ Editor::freeze_route ()
 }
 
 void
-Editor::bounce_range_selection (bool replace)
+Editor::bounce_range_selection (bool replace, bool enable_processing)
 {
        if (selection->time.empty()) {
                return;
@@ -3754,7 +3793,7 @@ Editor::bounce_range_selection (bool replace)
                itt.progress = false;
 
                 XMLNode &before = playlist->get_state();
-               boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt);
+               boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
                
                if (replace) {
                        list<AudioRange> ranges;
@@ -3840,9 +3879,10 @@ Editor::cut_copy (CutCopyOp op)
        */
 
        if (op == Cut || op == Clear) {
-               if (drag_info.item) {
-                       drag_info.item->ungrab (0);
-                       drag_info.item = 0;
+               if (_drag) {
+                       _drag->item()->ungrab (0);
+                       delete _drag;
+                       _drag = 0;
                }
        }
        
@@ -3860,75 +3900,92 @@ Editor::cut_copy (CutCopyOp op)
                }
 
                break_drag ();
+               delete _drag;
+               _drag = 0;
 
                return;
        }
 
-       RegionSelection rs; 
+       if (internal_editing()) {
 
-       /* we only want to cut regions if some are selected */
-
-       if (!selection->regions.empty()) {
-               get_regions_for_action (rs);
-       }
-
-       switch (current_mouse_mode()) {
-       case MouseObject: 
-               if (!rs.empty() || !selection->points.empty()) {
+               switch (current_mouse_mode()) {
+               case MouseObject:
+               case MouseRange:
+                       cut_copy_midi (op);
+                       break;
+               default:
+                       break;
+               }
+       } else {
+               
+               RegionSelection rs; 
+               
+               /* we only want to cut regions if some are selected */
+               
+               if (!selection->regions.empty()) {
+                       get_regions_for_action (rs);
+               }
+               
+               switch (current_mouse_mode()) {
+               case MouseObject: 
+                       if (!rs.empty() || !selection->points.empty()) {
 
-                       begin_reversible_command (opname + _(" objects"));
+                               begin_reversible_command (opname + _(" objects"));
 
-                       if (!rs.empty()) {
-                               cut_copy_regions (op, rs);
+                               if (!rs.empty()) {
+                                       cut_copy_regions (op, rs);
                                
-                               if (op == Cut) {
-                                       selection->clear_regions ();
+                                       if (op == Cut) {
+                                               selection->clear_regions ();
+                                       }
                                }
-                       }
 
-                       if (!selection->points.empty()) {
-                               cut_copy_points (op);
+                               if (!selection->points.empty()) {
+                                       cut_copy_points (op);
 
-                               if (op == Cut) {
-                                       selection->clear_points ();
+                                       if (op == Cut) {
+                                               selection->clear_points ();
+                                       }
                                }
-                       }
 
-                       commit_reversible_command ();   
-                       break; // terminate case statement here
-               } 
-               if (!selection->time.empty()) {
-                       /* don't cause suprises */
-                       break;
-               }
-               // fall thru if there was nothing selected
+                               commit_reversible_command ();   
+                               break; // terminate case statement here
+                       
+                       if (!selection->time.empty()) {
+                               /* don't cause suprises */
+                               break;
+                       }
+                       // fall thru if there was nothing selected
                
-       case MouseRange:
-               if (selection->time.empty()) {
-                       nframes64_t start, end;
-                       if (!get_edit_op_range (start, end)) {
-                               return;
+               case MouseRange:
+                       if (selection->time.empty()) {
+                               nframes64_t start, end;
+                               if (!get_edit_op_range (start, end)) {
+                                       return;
+                               }
+                               selection->set ((TimeAxisView*) 0, start, end);
                        }
-                       selection->set ((TimeAxisView*) 0, start, end);
-               }
                        
-               begin_reversible_command (opname + _(" range"));
-               cut_copy_ranges (op);
-               commit_reversible_command ();
+                       begin_reversible_command (opname + _(" range"));
+                       cut_copy_ranges (op);
+                       commit_reversible_command ();
                
-               if (op == Cut) {
-                       selection->clear_time ();
-               }
+                       if (op == Cut) {
+                               selection->clear_time ();
+                       }
 
-               break;
+                       break;
                
-       default:
-               break;
+               default:
+                       break;
+               }
        }
-
-
+               
        if (op == Cut || op == Clear) {
                break_drag ();
+               delete _drag;
+               _drag = 0;
        }
 }
 
@@ -3948,6 +4005,20 @@ Editor::cut_copy_points (CutCopyOp op)
        }
 }
 
+/** Cut, copy or clear selected automation points.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_midi (CutCopyOp op)
+{
+       cerr << "CCM: there are " << selection->midi.size() << " MRV's to work on\n";
+
+       for (MidiSelection::iterator i = selection->midi.begin(); i != selection->midi.end(); ++i) {
+               MidiRegionView* mrv = *i;
+               mrv->cut_copy_clear (op);
+       }
+}
+
 struct PlaylistState {
     boost::shared_ptr<Playlist> playlist;
     XMLNode*  before;
@@ -3966,13 +4037,106 @@ struct PlaylistMapping {
     PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
 };
 
+/** Remove `clicked_regionview' */
+void
+Editor::remove_clicked_region ()
+{
+       if (clicked_routeview == 0 || clicked_regionview == 0) {
+               return;
+       }
+
+       boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
+       
+       begin_reversible_command (_("remove region"));
+        XMLNode &before = playlist->get_state();
+       playlist->remove_region (clicked_regionview->region());
+        XMLNode &after = playlist->get_state();
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+       commit_reversible_command ();
+}
+
+
+/** Remove the selected regions */
+void
+Editor::remove_selected_regions ()
+{
+       RegionSelection rs; 
+       get_regions_for_action (rs);
+       
+       if (!session) {
+               return;
+       }
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("remove region"));
+
+       list<boost::shared_ptr<Region> > regions_to_remove;
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               // we can't just remove the region(s) in this loop because
+               // this removes them from the RegionSelection, and they thus
+               // disappear from underneath the iterator, and the ++i above
+               // SEGVs in a puzzling fashion.
+
+               // so, first iterate over the regions to be removed from rs and
+               // add them to the regions_to_remove list, and then
+               // iterate over the list to actually remove them.
+               
+               regions_to_remove.push_back ((*i)->region());
+       }
+
+       vector<PlaylistState> playlists;
+       
+       for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
+
+               boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
+
+               if (!playlist) {
+                       // is this check necessary?
+                       continue;
+               }
+
+               vector<PlaylistState>::iterator i;
+
+               //only take state if this is a new playlist.
+               for (i = playlists.begin(); i != playlists.end(); ++i) {
+                       if ((*i).playlist == playlist) {
+                               break;
+                       }
+               }
+
+               if (i == playlists.end()) {
+
+                       PlaylistState before;
+                       before.playlist = playlist;
+                       before.before = &playlist->get_state();
+
+                       playlist->freeze ();
+                       playlists.push_back(before);
+               }
+
+               playlist->remove_region (*rl);          
+       }
+
+       vector<PlaylistState>::iterator pl;
+
+       for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
+               (*pl).playlist->thaw ();
+               session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+       }
+
+       commit_reversible_command ();
+}
 
 /** Cut, copy or clear selected regions.
  * @param op Operation (Cut, Copy or Clear)
  */
 void
 Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
-{      
+{
        /* we can't use a std::map here because the ordering is important, and we can't trivially sort
           a map when we want ordered access to both elements. i think.
        */
@@ -3996,15 +4160,21 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                        boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
 
                        if (pl) {
+                               set<PlaylistState, lt_playlist>::iterator fl;
 
-                               PlaylistState before;
-                               before.playlist = pl;
-                               before.before = &pl->get_state();
-                               
-                               insert_result = freezelist.insert (before);
-                               
-                               if (insert_result.second) {
+                               //only take state if this is a new playlist.
+                               for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
+                                       if ((*fl).playlist == pl) {
+                                               break;
+                                       }
+                               }
+               
+                               if (fl == freezelist.end()) {
+                                       PlaylistState before;
+                                       before.playlist = pl;
+                                       before.before = &pl->get_state();
                                        pl->freeze ();
+                                       insert_result = freezelist.insert (before);
                                }
                        }
                }
@@ -4164,7 +4334,6 @@ Editor::paste_internal (nframes64_t position, float times)
 
        /* get everything in the correct order */
 
-
        if (!selection->tracks.empty()) {
                sort_track_selection ();
                ts = selection->tracks;
@@ -4336,14 +4505,14 @@ Editor::reset_point_selection ()
 void
 Editor::center_playhead ()
 {
-       float page = canvas_width * frames_per_unit;
+       float page = _canvas_width * frames_per_unit;
        center_screen_internal (playhead_cursor->current_frame, page);
 }
 
 void
 Editor::center_edit_point ()
 {
-       float page = canvas_width * frames_per_unit;
+       float page = _canvas_width * frames_per_unit;
        center_screen_internal (get_preferred_edit_position(), page);
 }
 
@@ -4428,17 +4597,35 @@ Editor::remove_last_capture ()
 void
 Editor::normalize_region ()
 {
-       RegionSelection rs; 
-
-       get_regions_for_action (rs);
-       
        if (!session) {
                return;
        }
 
+       RegionSelection rs; 
+       get_regions_for_action (rs);
+
        if (rs.empty()) {
                return;
        }
+       
+       Dialog dialog (rs.size() > 1 ? _("Normalize regions") : _("Normalize region"));
+       HBox hbox;
+       hbox.pack_start (*manage (new Label (_("Normalize to:"))));
+       SpinButton spin (0.2, 2);
+       spin.set_range (-112, 0);
+       spin.set_increments (0.1, 1);
+       spin.set_value (0);
+       hbox.pack_start (spin);
+       spin.set_value (_last_normalization_value);
+       hbox.pack_start (*manage (new Label (_("dbFS"))));
+       hbox.show_all ();
+       dialog.get_vbox()->pack_start (hbox);
+       dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
+       dialog.add_button (_("Normalize"), RESPONSE_ACCEPT);
+
+       if (dialog.run () == RESPONSE_CANCEL) {
+               return;
+       }
 
        begin_reversible_command (_("normalize"));
 
@@ -4450,12 +4637,14 @@ Editor::normalize_region ()
                if (!arv)
                        continue;
                XMLNode &before = arv->region()->get_state();
-               arv->audio_region()->normalize_to (0.0f);
+               arv->audio_region()->normalize_to (spin.get_value());
                session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
        }
 
        commit_reversible_command ();
        track_canvas->get_window()->set_cursor (*current_canvas_cursor);
+
+       _last_normalization_value = spin.get_value ();
 }
 
 
@@ -4552,6 +4741,38 @@ Editor::reverse_region ()
        apply_filter (rev, _("reverse regions"));
 }
 
+void
+Editor::strip_region_silence ()
+{
+       if (!session) {
+               return;
+       }
+
+       RegionSelection rs;
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       std::list<boost::shared_ptr<AudioRegion> > ar;
+       
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*i);
+               if (arv) {
+                       ar.push_back (arv->audio_region ());
+               }
+       }
+       
+       StripSilenceDialog d (ar);
+       int const r = d.run ();
+
+       if (r == Gtk::RESPONSE_OK) {
+               StripSilence s (*session, d.threshold (), d.minimum_length (), d.fade_length ());
+               apply_filter (s, _("strip silence"));
+       }
+}
+
 
 void
 Editor::quantize_region ()
@@ -4599,7 +4820,28 @@ Editor::apply_filter (Filter& filter, string command)
                        if (arv->audio_region()->apply (filter) == 0) {
 
                                XMLNode &before = playlist->get_state();
-                               playlist->replace_region (arv->region(), filter.results.front(), arv->region()->position());
+
+                               if (filter.results.empty ()) {
+
+                                       /* no regions returned; remove the old one */
+                                       playlist->remove_region (arv->region ());
+                                       
+                               } else {
+
+                                       std::vector<boost::shared_ptr<Region> >::iterator res = filter.results.begin ();
+
+                                       /* first region replaces the old one */
+                                       playlist->replace_region (arv->region(), *res, (*res)->position());
+                                       ++res;
+
+                                       /* add the rest */
+                                       while (res != filter.results.end()) {
+                                               playlist->add_region (*res, (*res)->position());
+                                               ++res;
+                                       }
+                                       
+                               }
+
                                XMLNode &after = playlist->get_state();
                                session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
                        } else {
@@ -4673,9 +4915,7 @@ Editor::brush (nframes64_t pos)
 void
 Editor::reset_region_gain_envelopes ()
 {
-       RegionSelection rs; 
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
        if (!session || rs.empty()) {
                return;
@@ -4700,80 +4940,134 @@ Editor::reset_region_gain_envelopes ()
 void
 Editor::toggle_gain_envelope_visibility ()
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+
+       session->begin_reversible_command (_("region gain envelope visible"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
                if (arv) {
+                       XMLNode &before = arv->region()->get_state ();
                        arv->set_envelope_visible (!arv->envelope_visible());
+                       XMLNode &after = arv->region()->get_state ();
+                       session->add_command (new MementoCommand<Region> (*(arv->region().get()), &before, &after));
                }
        }
+
+       session->commit_reversible_command ();
 }
 
 void
 Editor::toggle_gain_envelope_active ()
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+       
+       session->begin_reversible_command (_("region gain envelope active"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
                if (arv) {
+                       XMLNode &before = arv->region()->get_state ();
                        arv->audio_region()->set_envelope_active (!arv->audio_region()->envelope_active());
+                       XMLNode &after = arv->region()->get_state ();
+                       session->add_command (new MementoCommand<Region> (*(arv->region().get()), &before, &after));
                }
        }
+
+       session->commit_reversible_command ();
 }
 
 void
 Editor::toggle_region_lock ()
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+       
+       session->begin_reversible_command (_("region lock"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
                (*i)->region()->set_locked (!(*i)->region()->locked());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
+
+       session->commit_reversible_command ();
 }
 
 void
 Editor::set_region_lock_style (Region::PositionLockStyle ps)
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+       
+       session->begin_reversible_command (_("region lock style"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
                (*i)->region()->set_position_lock_style (ps);
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
+
+       session->commit_reversible_command ();
 }
 
 
 void
 Editor::toggle_region_mute ()
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+       
+       session->begin_reversible_command (_("region mute"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
                (*i)->region()->set_muted (!(*i)->region()->muted());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
+
+       session->commit_reversible_command ();
 }
 
 void
 Editor::toggle_region_opaque ()
 {
-       RegionSelection rs
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       get_regions_for_action (rs);
+       if (!session || rs.empty()) {
+               return;
+       }
+       
+       session->begin_reversible_command (_("region opacity"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
                (*i)->region()->set_opaque (!(*i)->region()->opaque());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
+
+       session->commit_reversible_command ();
 }
 
 void
@@ -4803,20 +5097,16 @@ Editor::set_fade_length (bool in)
 {
        RegionSelection rs; 
 
-       get_regions_for_action (rs);
-
-       /* we need a region to measure the offset from the start */
+       get_regions_for_action (rs, true);
 
-       RegionView* rv;
-
-       if (!rs.empty()) {
-               rv = rs.front();
-       } else if (entered_regionview) {
-               rv = entered_regionview;
-       } else {
+       if (rs.empty()) {
                return;
        }
 
+       /* we need a region to measure the offset from the start */
+
+       RegionView* rv = rs.front ();
+
        nframes64_t pos = get_preferred_edit_position();
        nframes64_t len;
        char* cmd;
@@ -5107,7 +5397,7 @@ Editor::toggle_selected_region_fades (int dir)
 void
 Editor::update_region_fade_visibility ()
 {
-       bool _fade_visibility = Config->get_show_region_fades ();
+       bool _fade_visibility = session->config.get_show_region_fades ();
 
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
@@ -5125,7 +5415,7 @@ Editor::update_region_fade_visibility ()
 void
 Editor::update_xfade_visibility ()
 {
-       _xfade_visibility = Config->get_xfades_visible ();
+       _xfade_visibility = session->config.get_xfades_visible ();
        
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
@@ -5192,7 +5482,7 @@ Editor::split ()
 {
        RegionSelection rs; 
        
-       get_regions_for_action (rs);
+       get_regions_for_action (rs, true);
 
        nframes64_t where = get_preferred_edit_position();
 
@@ -5221,49 +5511,6 @@ Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_ar
        }
 }
 
-void
-Editor::trim_region_front ()
-{
-       trim_region (true);
-}
-
-void
-Editor::trim_region_back ()
-{
-       trim_region (false);
-}
-
-void
-Editor::trim_region (bool front)
-{
-       nframes64_t where = get_preferred_edit_position();
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       if (rs.empty()) {
-               return;
-       }
-
-       begin_reversible_command (front ? _("trim front") : _("trim back"));
-
-       for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
-               if (!(*i)->region()->locked()) {
-                       boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
-                       XMLNode &before = pl->get_state();
-                       if (front) {
-                               (*i)->region()->trim_front (where, this);       
-                       } else {
-                               (*i)->region()->trim_end (where, this); 
-                       }
-                       XMLNode &after = pl->get_state();
-                       session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
-               }
-       }
-
-       commit_reversible_command ();
-}
-
 struct EditorOrderRouteSorter {
     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
            /* use of ">" forces the correct sort order */
@@ -5340,11 +5587,11 @@ Editor::ensure_track_visible(TimeAxisView *track)
        if (track->hidden())
                return;
 
-       double current_view_min_y = vertical_adjustment.get_value();
-       double current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
+       double const current_view_min_y = vertical_adjustment.get_value();
+       double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
 
-       double track_min_y = track->y_position;
-       double track_max_y = track->y_position + (double)track->effective_height;
+       double const track_min_y = track->y_position ();
+       double const track_max_y = track->y_position () + track->effective_height ();
 
        if (track_min_y >= current_view_min_y &&
             track_max_y <= current_view_max_y) {
@@ -5358,7 +5605,7 @@ Editor::ensure_track_visible(TimeAxisView *track)
                new_value = track_min_y;
        } else {
                // Track is below the current view
-               new_value = track->y_position + (double)track->effective_height + canvas_timebars_vsize - vertical_adjustment.get_page_size();
+               new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
        }
 
        vertical_adjustment.set_value(new_value);
@@ -5889,46 +6136,11 @@ Editor::set_track_height (uint32_t h)
 {
        TrackSelection& ts (selection->tracks);
 
-       if (ts.empty()) {
-               return;
-       }
-
        for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
                (*x)->set_height (h);
        }
 }
 
-void
-Editor::set_track_height_largest ()
-{
-       set_track_height (TimeAxisView::hLargest);
-}
-void
-Editor::set_track_height_large ()
-{
-       set_track_height (TimeAxisView::hLarge);
-}
-void
-Editor::set_track_height_larger ()
-{
-       set_track_height (TimeAxisView::hLarger);
-}
-void
-Editor::set_track_height_normal ()
-{
-       set_track_height (TimeAxisView::hNormal);
-}
-void
-Editor::set_track_height_smaller ()
-{
-       set_track_height (TimeAxisView::hSmaller);
-}
-void
-Editor::set_track_height_small ()
-{
-       set_track_height (TimeAxisView::hSmall);
-}
-
 void
 Editor::toggle_tracks_active ()
 {
@@ -6033,23 +6245,6 @@ Editor::remove_tracks ()
        }
 }
 
-void
-Editor::set_waveform_scale (WaveformScale ws)
-{
-       TrackSelection& ts (selection->tracks);
-
-       if (ts.empty()) {
-               return;
-       }
-
-       for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
-               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (*x);
-               if (atv) {
-                       atv->set_waveform_scale (ws);
-               }
-       }
-}      
-
 void
 Editor::do_insert_time ()
 {
@@ -6057,61 +6252,54 @@ Editor::do_insert_time ()
                return;
        }
 
-       nframes64_t pos = get_preferred_edit_position ();
        ArdourDialog d (*this, _("Insert Time"));
-       VButtonBox button_box;
-       VBox option_box;
-       RadioButtonGroup group;
-       RadioButton leave_button (group, _("Stay in position"));
-       RadioButton move_button (group, _("Move"));
-       RadioButton split_button (group, _("Split & Later Section Moves"));
-       Label intersect_option_label (_("Intersected regions should:"));
-       CheckButton glue_button (_("Move Glued Regions"));
-       CheckButton marker_button (_("Move Markers"));
-       AudioClock clock ("insertTimeClock", true, X_("InsertTimeClock"), true, true, true);
-       HBox clock_box;
 
-       clock.set (0);
-       clock.set_session (session);
-       clock.set_bbt_reference (pos);
-
-       clock_box.pack_start (clock, false, true);
+       nframes64_t const pos = get_preferred_edit_position ();
+       
+       d.get_vbox()->set_border_width (12);
+       d.get_vbox()->set_spacing (4);
 
-       option_box.set_spacing (6);
-       option_box.pack_start (intersect_option_label, false, false);
-       option_box.pack_start (button_box, false, false);
-       option_box.pack_start (glue_button, false, false);
-       option_box.pack_start (marker_button, false, false);
+       Table table (2, 2);
+       table.set_spacings (4);
 
-       button_box.pack_start (leave_button, false, false);
-       button_box.pack_start (move_button, false, false);
-       button_box.pack_start (split_button, false, false);
-                                     
-       d.get_vbox()->set_border_width (12);
-       d.get_vbox()->pack_start (clock_box, false, false);
-       d.get_vbox()->pack_start (option_box, false, false);
+       Label time_label (_("Time to insert:"));
+       time_label.set_alignment (1, 0.5);
+       table.attach (time_label, 0, 1, 0, 1, FILL | EXPAND);
+       AudioClock clock ("insertTimeClock", true, X_("InsertTimeClock"), true, true, true);
+       clock.set (0);
+       clock.set_session (session);
+       clock.set_bbt_reference (pos);  
+       table.attach (clock, 1, 2, 0, 1);
+
+       Label intersected_label (_("Intersected regions should:"));
+       intersected_label.set_alignment (1, 0.5);
+       table.attach (intersected_label, 0, 1, 1, 2, FILL | EXPAND);
+       ComboBoxText intersected_combo;
+       intersected_combo.append_text (_("stay in position"));
+       intersected_combo.append_text (_("move"));
+       intersected_combo.append_text (_("be split"));
+       intersected_combo.set_active (0);
+       table.attach (intersected_combo, 1, 2, 1, 2);
+
+       d.get_vbox()->pack_start (table);
+
+       CheckButton move_glued (_("Move glued regions"));
+       d.get_vbox()->pack_start (move_glued);
+       CheckButton move_markers (_("Move markers"));
+       d.get_vbox()->pack_start (move_markers);
+       CheckButton move_tempos (_("Move tempo and meter changes"));
+       d.get_vbox()->pack_start (move_tempos);
        
-       leave_button.show ();
-       move_button.show ();
-       split_button.show ();
-       intersect_option_label.show ();
-       option_box.show ();
-       button_box.show ();
-       glue_button.show ();
-       clock.show_all();
-       clock_box.show ();
-       marker_button.show ();
-
        d.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-       d.add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
-       d.show ();
+       d.add_button (_("Insert time"), Gtk::RESPONSE_OK);
+       d.show_all ();
 
        int response = d.run ();
 
        if (response != RESPONSE_OK) {
                return;
        }
-       
+
        nframes64_t distance = clock.current_duration (pos);
 
        if (distance == 0) {
@@ -6120,20 +6308,24 @@ Editor::do_insert_time ()
 
        InsertTimeOption opt;
 
-       if (leave_button.get_active()) {
+       switch (intersected_combo.get_active_row_number ()) {
+       case 0:
                opt = LeaveIntersected;
-       } else if (move_button.get_active()) {
+               break;
+       case 1:
                opt = MoveIntersected;
-       } else {
+               break;
+       case 2:
                opt = SplitIntersected;
+               break;
        }
 
-       insert_time (pos, distance, opt, glue_button.get_active(), marker_button.get_active());
+       insert_time (pos, distance, opt, move_glued.get_active(), move_markers.get_active(), move_tempos.get_active());
 }
 
 void
 Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt, 
-                    bool ignore_music_glue, bool markers_too)
+                    bool ignore_music_glue, bool markers_too, bool tempo_too)
 {
        bool commit = false;
 
@@ -6196,34 +6388,44 @@ Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
                }
        }
 
+       if (tempo_too) {
+               session->tempo_map().insert_time (pos, frames);
+       }
+
        if (commit) {
                commit_reversible_command ();
        }
 }
 
 void
-Editor::fit_tracks ()
+Editor::fit_selected_tracks ()
 {
-       if (selection->tracks.empty()) {
+       fit_tracks (selection->tracks);
+}
+
+void
+Editor::fit_tracks (TrackSelection & tracks)
+{
+       if (tracks.empty()) {
                return;
        }
 
        uint32_t child_heights = 0;
 
-       for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
+       for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
 
                if (!(*t)->marked_for_display()) {
                        continue;
                }
 
-               child_heights += ((*t)->effective_height - (*t)->current_height());
+               child_heights += (*t)->effective_height() - (*t)->current_height();
        }
 
-       uint32_t h = (uint32_t) floor ((canvas_height - child_heights - canvas_timebars_vsize)/selection->tracks.size());
+       uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / tracks.size());
        double first_y_pos = DBL_MAX;
 
        if (h < TimeAxisView::hSmall) {
-               MessageDialog msg (*this, _("There are too many selected tracks to fit in the current window"));
+               MessageDialog msg (*this, _("There are too many tracks to fit in the current window"));
                /* too small to be displayed */
                return;
        }
@@ -6233,7 +6435,7 @@ Editor::fit_tracks ()
        /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
        
        bool prev_was_selected = false;
-       bool is_selected = selection->selected (track_views.front());
+       bool is_selected = tracks.contains (track_views.front());
        bool next_is_selected;
 
        for (TrackViewList::iterator t = track_views.begin(); t != track_views.end(); ++t) {
@@ -6244,14 +6446,14 @@ Editor::fit_tracks ()
                ++next;
                
                if (next != track_views.end()) {
-                       next_is_selected = selection->selected (*next);
+                       next_is_selected = tracks.contains (*next);
                } else {
                        next_is_selected = false;
                }
 
                if (is_selected) {
                        (*t)->set_height (h);
-                       first_y_pos = std::min ((*t)->y_position, first_y_pos);
+                       first_y_pos = std::min ((*t)->y_position (), first_y_pos);
                } else {
                        if (prev_was_selected && next_is_selected) {
                                hide_track_in_display (**t);
@@ -6280,9 +6482,7 @@ Editor::save_visual_state (uint32_t n)
                visual_states.push_back (0);
        }
 
-       if (visual_states[n] != 0) {
-               delete visual_states[n];
-       }
+       delete visual_states[n];
 
        visual_states[n] = current_visual_state (true);
        gdk_beep ();