Make editor route list rec button respect group settings.
[ardour.git] / gtk2_ardour / editor_ops.cc
index df2752bd939e91fb45deed4e2a539e423c37a40c..55382bbacaed10f2821ad28e01364dbb72248ad4 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"
@@ -64,6 +64,7 @@
 #include "audio_time_axis.h"
 #include "automation_time_axis.h"
 #include "streamview.h"
+#include "audio_streamview.h"
 #include "audio_region_view.h"
 #include "midi_region_view.h"
 #include "rgb_macros.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
@@ -147,8 +146,6 @@ Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
                snap_to (where);
        }
 
-       cerr << "Split " << regions.size() << " at " << where << endl;
-
        for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
 
                RegionSelection::iterator tmp;
@@ -176,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;
@@ -713,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:
@@ -799,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;
                }
 
@@ -829,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;
@@ -854,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) {
@@ -869,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;
@@ -948,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;  
        }
        
@@ -971,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; 
@@ -1015,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; 
@@ -1047,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;
@@ -1079,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;
        }
 
@@ -1096,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
@@ -1159,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();
                }
        }
 
@@ -1467,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;
@@ -1495,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;
@@ -1534,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);
 }
@@ -1563,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);
 }
@@ -1621,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) {
@@ -1728,7 +1674,6 @@ Editor::temporal_zoom_region (bool both_axes)
        nframes64_t end = 0;
        RegionSelection rs; 
        set<TimeAxisView*> tracks;
-       double top_y_position = DBL_MAX;
 
        get_regions_for_action (rs);
 
@@ -1747,10 +1692,6 @@ Editor::temporal_zoom_region (bool both_axes)
                }
 
                tracks.insert (&((*i)->get_time_axis_view()));
-
-               if ((*i)->get_time_axis_view().y_position < top_y_position) {
-                       top_y_position = (*i)->get_time_axis_view().y_position;
-               }
        }
 
        /* now comes an "interesting" hack ... make sure we leave a little space
@@ -1769,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) {
@@ -1795,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 - 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 */
                
@@ -1804,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()) {
@@ -1813,10 +1754,9 @@ Editor::temporal_zoom_region (bool both_axes)
                        }
                }
 
-               no_route_list_redisplay = false;
-               redisplay_route_list ();
+               _routes->resume_redisplay ();
 
-               vertical_adjustment.set_value (std::max (top_y_position - 5.0, 0.0));
+               vertical_adjustment.set_value (0.0);
                no_save_visual = false;
        }
 
@@ -1855,13 +1795,12 @@ Editor::temporal_zoom_session ()
        ENSURE_GUI_THREAD (mem_fun (*this, &Editor::temporal_zoom_session));
 
        if (session) {
-               last_canvas_frame = ((session->current_end_frame() - session->current_start_frame()) + (current_page_frames() / 24));
                temporal_zoom_by_frame (session->current_start_frame(), session->current_end_frame(), "zoom to 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;
 
@@ -1871,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));
 
@@ -2014,7 +1953,7 @@ Editor::add_location_from_playhead_cursor ()
 }
 
 void
-Editor::add_location_from_audio_region ()
+Editor::add_locations_from_audio_region ()
 {
        RegionSelection rs; 
 
@@ -2024,15 +1963,61 @@ Editor::add_location_from_audio_region ()
                return;
        }
 
-       RegionView* rv = *(rs.begin());
-       boost::shared_ptr<Region> region = rv->region();
+       session->begin_reversible_command (rs.size () > 1 ? _("add markers") : _("add marker"));
+       XMLNode &before = session->locations()->get_state();
+       
+       cerr << "Add locations\n";
+
+       for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
+               
+               boost::shared_ptr<Region> region = (*i)->region ();
        
-       Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+               Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+               
+               session->locations()->add (location, true);
+       }
+
+       XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_audio_region ()
+{
+       RegionSelection rs; 
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
        session->begin_reversible_command (_("add marker"));
-        XMLNode &before = session->locations()->get_state();
+       XMLNode &before = session->locations()->get_state();
+
+       string markername;
+
+       if (rs.size() > 1) {            // more than one region selected
+               session->locations()->next_available_name(markername, "regions");
+       } else {
+               RegionView* rv = *(rs.begin());
+               boost::shared_ptr<Region> region = rv->region();
+               markername = region->name();
+       }
+               
+       if (!choose_new_marker_name(markername)) {
+               return;
+       }
+
+       cerr << "Add location\n";
+
+       // single range spanning all selected 
+       Location *location = new Location (rs.start(), rs.end_frame(), markername, Location::IsRangeMarker);
        session->locations()->add (location, true);
-        XMLNode &after = session->locations()->get_state();
-       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+
+       XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
        session->commit_reversible_command ();
 }
 
@@ -2223,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;
@@ -2243,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;
        }
 
@@ -2269,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;
@@ -2285,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;
        }
 
@@ -2330,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 */
@@ -2606,7 +2579,7 @@ Editor::rename_region()
                strip_whitespace_edges (str);
                if (!str.empty()) {
                        rs.front()->region()->set_name (str);
-                       redisplay_regions ();
+                       _regions->redisplay ();
                }
        }
 }
@@ -2720,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;
 
@@ -2734,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));
        }
 }      
 
@@ -2761,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;
@@ -2772,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));
        }
 }
 
@@ -2798,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);
        }
 }
 
@@ -2830,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) {
 
@@ -2899,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 ();
 
@@ -2910,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()));
                                                } 
                                        }
 
@@ -2938,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 ()
 {
@@ -3146,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;
 
@@ -3310,9 +3279,9 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
                return;
        }
 
-       nframes64_t distance;
+       nframes64_t distance = 0;
        nframes64_t pos = 0;
-       int dir = 0;
+       int dir = 1;
 
        list<RegionView*> sorted;
        rs.by_position (sorted);
@@ -3324,7 +3293,6 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
                pos = position;
                if (position > r->position()) {
                        distance = position - r->position();
-                       dir = 1;
                } else {
                        distance = r->position() - position;
                        dir = -1;
@@ -3335,7 +3303,6 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
                if (position > r->last_frame()) {
                        distance = position - r->last_frame();
                        pos = r->position() + distance;
-                       dir = 1;
                } else {
                        distance = r->last_frame() - position;
                        pos = r->position() - distance;
@@ -3347,7 +3314,6 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
                pos = r->adjust_to_sync (position);
                if (pos > r->position()) {
                        distance = pos - r->position();
-                       dir = 1;
                } else {
                        distance = r->position() - pos;
                        dir = -1;
@@ -3441,54 +3407,91 @@ Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint poi
        session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
 }      
 
-/** Trim the end of the selected regions to the position of the edit cursor */
 void
-Editor::trim_region_to_loop ()
+Editor::trim_region_front ()
 {
-       Location* loc = session->locations()->auto_loop_location();
-       if (!loc) {
-               return;
-       }
-       trim_region_to_location (*loc, _("trim to loop"));
+       trim_region (true);
 }
 
 void
-Editor::trim_region_to_punch ()
+Editor::trim_region_back ()
 {
-       Location* loc = session->locations()->auto_punch_location();
-       if (!loc) {
-               return;
-       }
-       trim_region_to_location (*loc, _("trim to punch"));
+       trim_region (false);
 }
+
 void
-Editor::trim_region_to_location (const Location& loc, const char* str)
+Editor::trim_region (bool front)
 {
+       nframes64_t where = get_preferred_edit_position();
        RegionSelection rs;
 
        get_regions_for_action (rs);
 
-       begin_reversible_command (str);
+       if (rs.empty()) {
+               return;
+       }
 
-       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+       begin_reversible_command (front ? _("trim front") : _("trim back"));
 
-               if (!arv) {
-                       continue;
+       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));
                }
+       }
 
-               /* require region to span proposed trim */
+       commit_reversible_command ();
+}
+
+/** Trim the end of the selected regions to the position of the edit cursor */
+void
+Editor::trim_region_to_loop ()
+{
+       Location* loc = session->locations()->auto_loop_location();
+       if (!loc) {
+               return;
+       }
+       trim_region_to_location (*loc, _("trim to loop"));
+}
+
+void
+Editor::trim_region_to_punch ()
+{
+       Location* loc = session->locations()->auto_punch_location();
+       if (!loc) {
+               return;
+       }
+       trim_region_to_location (*loc, _("trim to punch"));
+}
+void
+Editor::trim_region_to_location (const Location& loc, const char* str)
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       begin_reversible_command (str);
 
-               switch (arv->region()->coverage (loc.start(), loc.end())) {
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               RegionView* rv = (*x);
+
+               /* require region to span proposed trim */
+               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;
                }
 
@@ -3496,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 ();
@@ -3524,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 ();
@@ -3569,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 ();
+}
+
+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) {
 
-               if (!arv->region()->covers (where)) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
                        continue;
                }
 
@@ -3593,29 +3639,56 @@ 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*
 Editor::_freeze_thread (void* arg)
 {
-       PBD::ThreadCreated (pthread_self(), X_("Freeze"));
+       PBD::notify_gui_about_thread_creation (pthread_self(), X_("Freeze"));
        return static_cast<Editor*>(arg)->freeze_thread ();
 }
 
@@ -3623,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);
 }
 
@@ -3667,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);
 
@@ -3684,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;
@@ -3719,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;
@@ -3805,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;
                }
        }
        
@@ -3825,6 +3900,8 @@ Editor::cut_copy (CutCopyOp op)
                }
 
                break_drag ();
+               delete _drag;
+               _drag = 0;
 
                return;
        }
@@ -3894,6 +3971,8 @@ Editor::cut_copy (CutCopyOp op)
 
        if (op == Cut || op == Clear) {
                break_drag ();
+               delete _drag;
+               _drag = 0;
        }
 }
 
@@ -3931,13 +4010,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.
        */
@@ -3961,15 +4133,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);
                                }
                        }
                }
@@ -4301,14 +4479,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);
 }
 
@@ -4393,17 +4571,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"));
 
@@ -4415,12 +4611,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 ();
 }
 
 
@@ -4517,6 +4715,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 ()
@@ -4564,7 +4794,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 {
@@ -4638,9 +4889,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;
@@ -4665,101 +4914,173 @@ 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 = get_equivalent_regions (selection->regions, RouteGroup::Edit);
+
+       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_mute ()
+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) {
-               (*i)->region()->set_muted (!(*i)->region()->muted());
+               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
-Editor::toggle_region_opaque ()
+Editor::toggle_record_enable ()
 {
-       RegionSelection rs; 
+       bool new_state = false;
+       bool first = true;
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
+               if (!rtav)
+                       continue;
+               if (!rtav->is_track())
+                       continue;
 
-       get_regions_for_action (rs);
+               if (first) {
+                       new_state = !rtav->track()->record_enabled();
+                       first = false;
+               }
 
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               (*i)->region()->set_opaque (!(*i)->region()->opaque());
+               rtav->track()->set_record_enable(new_state, this);
        }
 }
 
+
 void
 Editor::set_fade_length (bool in)
 {
        RegionSelection rs; 
 
-       get_regions_for_action (rs);
-
-       /* we need a region to measure the offset from the start */
-
-       RegionView* rv;
+       get_regions_for_action (rs, true);
 
-       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;
@@ -5000,12 +5321,75 @@ Editor::set_fade_out_active (bool yn)
        commit_reversible_command ();
 }
 
+void
+Editor::toggle_selected_region_fades (int dir)
+{
+       RegionSelection rs;
+       RegionSelection::iterator i;
+       boost::shared_ptr<AudioRegion> ar;
+       bool yn;
+
+       get_regions_for_action (rs);
+       
+       if (rs.empty()) {
+               return;
+       }
+
+       for (i = rs.begin(); i != rs.end(); ++i) {
+               if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
+                       if (dir == -1) {
+                               yn = ar->fade_out_active ();
+                       } else {
+                               yn = ar->fade_in_active ();
+                       }
+                       break;
+               }
+       }
+
+       if (i == rs.end()) {
+               return;
+       }
+
+       /* XXX should this undo-able? */
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) == 0) {
+                       continue;
+               }
+               if (dir == 1 || dir == 0) {
+                       ar->set_fade_in_active (!yn);
+               }
+
+               if (dir == -1 || dir == 0) {
+                       ar->set_fade_out_active (!yn);
+               }
+       }
+}
+
+
+/** Update region fade visibility after its configuration has been changed */
+void
+Editor::update_region_fade_visibility ()
+{
+       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);
+               if (v) {
+                       if (_fade_visibility) {
+                               v->audio_view()->show_all_fades ();
+                       } else {
+                               v->audio_view()->hide_all_fades ();
+                       }
+               }
+       }
+}
 
 /** Update crossfade visibility after its configuration has been changed */
 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);
@@ -5072,7 +5456,7 @@ Editor::split ()
 {
        RegionSelection rs; 
        
-       get_regions_for_action (rs);
+       get_regions_for_action (rs, true);
 
        nframes64_t where = get_preferred_edit_position();
 
@@ -5101,49 +5485,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 */
@@ -5161,17 +5502,26 @@ Editor::select_next_route()
 
        TimeAxisView* current = selection->tracks.front();
 
-       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
-               if (*i == current) {
-                       ++i;
-                       if (i != track_views.end()) {
-                               selection->set (*i);
-                       } else {
-                               selection->set (*(track_views.begin()));
+       RouteUI *rui;
+       do {
+               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+                       if (*i == current) {
+                               ++i;
+                               if (i != track_views.end()) {
+                                       current = (*i);
+                               } else {
+                                       current = (*(track_views.begin()));
+                                       //selection->set (*(track_views.begin()));
+                               }
+                               break;
                        }
-                       break;
                }
-       }
+               rui = dynamic_cast<RouteUI *>(current);
+       } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
+
+       selection->set(current);
+
+       ensure_track_visible(current);
 }
 
 void
@@ -5184,17 +5534,55 @@ Editor::select_prev_route()
 
        TimeAxisView* current = selection->tracks.front();
 
-       for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
-               if (*i == current) {
-                       ++i;
-                       if (i != track_views.rend()) {
-                               selection->set (*i);
-                       } else {
-                               selection->set (*(track_views.rbegin()));
+       RouteUI *rui;
+       do {
+               for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
+                       if (*i == current) {
+                               ++i;
+                               if (i != track_views.rend()) {
+                                       current = (*i);
+                               } else {
+                                       current = *(track_views.rbegin());
+                               }
+                               break;
                        }
-                       break;
                }
+               rui = dynamic_cast<RouteUI *>(current);
+       } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
+
+       selection->set (current);
+
+       ensure_track_visible(current);
+}
+
+void
+Editor::ensure_track_visible(TimeAxisView *track)
+{
+       if (track->hidden())
+               return;
+
+       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 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) {
+               return;
+       }
+
+       double new_value;
+
+       if (track_min_y < current_view_min_y) {
+               // Track is above the current view
+               new_value = track_min_y;
+       } else {
+               // Track is below the current view
+               new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
        }
+
+       vertical_adjustment.set_value(new_value);
 }
 
 void
@@ -5722,46 +6110,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 ()
 {
@@ -5866,23 +6219,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 ()
 {
@@ -5890,61 +6226,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) {
@@ -5953,20 +6282,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;
 
@@ -5977,26 +6310,34 @@ Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
        begin_reversible_command (_("insert time"));
 
        for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
+               /* regions */
                boost::shared_ptr<Playlist> pl = (*x)->playlist();
                
-               if (!pl) {
-                       continue;
-               }
-
-               XMLNode &before = pl->get_state();
+               if (pl) {
 
-               if (opt == SplitIntersected) {
-                       pl->split (pos);
+                       XMLNode &before = pl->get_state();
+                       
+                       if (opt == SplitIntersected) {
+                               pl->split (pos);
+                       }
+                       
+                       pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
+                       
+                       XMLNode &after = pl->get_state();
+                       
+                       session->add_command (new MementoCommand<Playlist> (*pl, &before, &after));
+                       commit = true;
+               }
+                       
+               /* automation */
+               RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
+               if (rtav) {
+                       rtav->route ()->shift (pos, frames);
+                       commit = true;
                }
-               
-               pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
-
-               XMLNode &after = pl->get_state();
-
-               session->add_command (new MementoCommand<Playlist> (*pl, &before, &after));
-               commit = true;
        }
 
+       /* markers */
        if (markers_too) {
                bool moved = false;
                XMLNode& before (session->locations()->get_state());
@@ -6021,39 +6362,88 @@ 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)/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 tracks to fit in the current window"));
+               /* too small to be displayed */
+               return;
+       }
+
        undo_visual_stack.push_back (current_visual_state());
        
-       for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
-               (*t)->set_height (h);
-               first_y_pos = std::min ((*t)->y_position, first_y_pos);
+       /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
+       
+       bool prev_was_selected = false;
+       bool is_selected = tracks.contains (track_views.front());
+       bool next_is_selected;
+
+       for (TrackViewList::iterator t = track_views.begin(); t != track_views.end(); ++t) {
+
+               TrackViewList::iterator next;
+               
+               next = t;
+               ++next;
+               
+               if (next != track_views.end()) {
+                       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);
+               } else {
+                       if (prev_was_selected && next_is_selected) {
+                               hide_track_in_display (**t);
+                       }
+               }
+
+               prev_was_selected = is_selected;
+               is_selected = next_is_selected;
        }
 
+       /* 
+          set the controls_layout height now, because waiting for its size 
+          request signal handler will cause the vertical adjustment setting to fail 
+       */ 
+
+       controls_layout.property_height () = full_canvas_height - canvas_timebars_vsize;
        vertical_adjustment.set_value (first_y_pos);
 
        redo_visual_stack.push_back (current_visual_state());
@@ -6066,9 +6456,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 ();
@@ -6091,22 +6479,23 @@ Editor::goto_visual_state (uint32_t n)
 void
 Editor::start_visual_state_op (uint32_t n)
 {
-       cerr << "Start\n";
+       cerr << "Start visual op\n";
        if (visual_state_op_connection.empty()) {
-               cerr << "\tqueue\n";
                visual_state_op_connection = Glib::signal_timeout().connect (bind (mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
+               cerr << "\tqueued new timeout\n";
        }
 }
 
 void
 Editor::cancel_visual_state_op (uint32_t n)
 {
-       cerr << "Cancel\n";
        if (!visual_state_op_connection.empty()) {
-               cerr << "\tgoto\n";
+               cerr << "cancel visual op, time to goto\n";
                visual_state_op_connection.disconnect();
                goto_visual_state (n);
-       } 
+       } else {
+               cerr << "cancel visual op, do nothing\n";
+       }
 }
 
 bool
@@ -6123,3 +6512,4 @@ Editor::end_visual_state_op (uint32_t n)
 
        return false; // do not call again
 }
+