modified fix from carl for region copy-moves-original-to-start bug; change verbose...
[ardour.git] / gtk2_ardour / editor_ops.cc
index 6d006f94bf5a54e16a42eb4637cbb9ae4d4523bc..f80ec708ebee70178fc003a5c0493ab44989ce7c 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <unistd.h>
@@ -32,6 +31,7 @@
 
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/choice.h>
+#include <gtkmm2ext/window_title.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/session.h>
@@ -44,6 +44,7 @@
 #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_ui.h"
@@ -56,7 +57,6 @@
 #include "rgb_macros.h"
 #include "selection_templates.h"
 #include "selection.h"
-#include "sfdb_ui.h"
 #include "editing.h"
 #include "gtk-custom-hruler.h"
 #include "gui_thread.h"
@@ -68,6 +68,7 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Editing;
 
 /***********************************************************************
@@ -122,7 +123,7 @@ Editor::split_regions_at (nframes_t where, RegionSelection& regions)
                tmp = a;
                ++tmp;
 
-               Playlist* pl = (*a)->region()->playlist();
+               boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
 
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*a);
                if (arv)
@@ -149,7 +150,7 @@ Editor::remove_clicked_region ()
                return;
        }
 
-       Playlist* playlist = clicked_audio_trackview->playlist();
+       boost::shared_ptr<Playlist> playlist = clicked_audio_trackview->playlist();
        
        begin_reversible_command (_("remove region"));
         XMLNode &before = playlist->get_state();
@@ -162,9 +163,9 @@ Editor::remove_clicked_region ()
 void
 Editor::destroy_clicked_region ()
 {
-       int32_t selected = selection->regions.size();
+       uint32_t selected = selection->regions.size();
 
-       if (!session || clicked_regionview == 0 && selected == 0) {
+       if (!session || !selected) {
                return;
        }
 
@@ -191,7 +192,7 @@ Do you really want to destroy %1 ?"),
                return;
        }
 
-       if (selected > 0) {
+       if (selected) {
                list<boost::shared_ptr<Region> > r;
 
                for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
@@ -199,9 +200,6 @@ Do you really want to destroy %1 ?"),
                }
 
                session->destroy_regions (r);
-
-       } else if (clicked_regionview) {
-               session->destroy_region (clicked_regionview->region());
        } 
 }
 
@@ -235,7 +233,7 @@ Editor::select_region_for_operation (int dir, TimeAxisView **tv)
                RouteTimeAxisView* rtv;
 
                if ((rtv = dynamic_cast<RouteTimeAxisView*> (*tv)) != 0) {
-                       Playlist *pl;
+                       boost::shared_ptr<Playlist> pl;
                        
                        if ((pl = rtv->playlist()) == 0) {
                                return region;
@@ -759,6 +757,39 @@ Editor::cursor_to_selection_end (Cursor *cursor)
        }
 }
 
+void
+Editor::scroll_playhead (bool forward)
+{
+       nframes_t pos = playhead_cursor->current_frame;
+       nframes_t delta = (nframes_t) floor (current_page_frames() / 0.8);
+
+       if (forward) {
+               if (pos == max_frames) {
+                       return;
+               }
+
+               if (pos < max_frames - delta) {
+                       pos += delta ;
+               } else {
+                       pos = max_frames;
+               } 
+
+       } else {
+
+               if (pos == 0) {
+                       return;
+               } 
+
+               if (pos > delta) {
+                       pos -= delta;
+               } else {
+                       pos = 0;
+               }
+       }
+
+       session->request_locate (pos);
+}
+
 void
 Editor::playhead_backward ()
 {
@@ -929,7 +960,7 @@ Editor::scroll_backward (float pages)
                frame = leftmost_frame - cnt;
        }
 
-       reposition_x_origin (frame);
+       reset_x_origin (frame);
 }
 
 void
@@ -957,7 +988,7 @@ Editor::scroll_forward (float pages)
                frame = leftmost_frame + cnt;
        }
 
-       reposition_x_origin (frame);
+       reset_x_origin (frame);
 }
 
 void
@@ -1200,6 +1231,8 @@ Editor::temporal_zoom_to_frame (bool coarser, nframes_t frame)
 void
 Editor::add_location_from_selection ()
 {
+       string rangename;
+
        if (selection->time.empty()) {
                return;
        }
@@ -1211,7 +1244,8 @@ Editor::add_location_from_selection ()
        nframes_t start = selection->time[clicked_selection].start;
        nframes_t end = selection->time[clicked_selection].end;
 
-       Location *location = new Location (start, end, "selection");
+       session->locations()->next_available_name(rangename,"selection");
+       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
 
        session->begin_reversible_command (_("add marker"));
         XMLNode &before = session->locations()->get_state();
@@ -1224,9 +1258,12 @@ Editor::add_location_from_selection ()
 void
 Editor::add_location_from_playhead_cursor ()
 {
+       string markername;
+
        nframes_t where = session->audible_frame();
        
-       Location *location = new Location (where, where, "mark", Location::IsMark);
+       session->locations()->next_available_name(markername,"mark");
+       Location *location = new Location (where, where, markername, Location::IsMark);
        session->begin_reversible_command (_("add marker"));
         XMLNode &before = session->locations()->get_state();
        session->locations()->add (location, true);
@@ -1245,7 +1282,7 @@ Editor::add_location_from_audio_region ()
        RegionView* rv = *(selection->regions.begin());
        boost::shared_ptr<Region> region = rv->region();
        
-       Location *location = new Location (region->position(), region->last_frame(), region->name());
+       Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
        session->begin_reversible_command (_("add marker"));
         XMLNode &before = session->locations()->get_state();
        session->locations()->add (location, true);
@@ -1254,306 +1291,6 @@ Editor::add_location_from_audio_region ()
        session->commit_reversible_command ();
 }
 
-void
-Editor::select_all_in_track (Selection::Operation op)
-{
-       list<Selectable *> touched;
-
-       if (!clicked_trackview) {
-               return;
-       }
-       
-       clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
-
-       switch (op) {
-       case Selection::Toggle:
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               /* not defined yet */
-               break;
-       }
-}
-
-void
-Editor::select_all (Selection::Operation op)
-{
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all"));
-       switch (op) {
-       case Selection::Toggle:
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               /* not defined yet */
-               break;
-       }
-       commit_reversible_command ();
-}
-
-void
-Editor::invert_selection_in_track ()
-{
-       list<Selectable *> touched;
-
-       if (!clicked_trackview) {
-               return;
-       }
-       
-       clicked_trackview->get_inverted_selectables (*selection, touched);
-       selection->set (touched);
-}
-
-void
-Editor::invert_selection ()
-{
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_inverted_selectables (*selection, touched);
-       }
-
-       selection->set (touched);
-}
-
-bool
-Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, Selection::Operation op)
-{
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, top, bot, touched);
-       }
-
-       cerr << "select all within found " << touched.size() << endl;
-
-       begin_reversible_command (_("select all within"));
-       switch (op) {
-       case Selection::Toggle:
-               cerr << "toggle\n";
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               cerr << "set\n";
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               cerr << "extend\n";
-               /* not defined yet */
-               break;
-       }
-
-       cerr << "selection now has " << selection->points.size() << endl;
-
-       commit_reversible_command ();
-       return !touched.empty();
-}
-
-void
-Editor::set_selection_from_audio_region ()
-{
-       if (selection->regions.empty()) {
-               return;
-       }
-
-       RegionView* rv = *(selection->regions.begin());
-       boost::shared_ptr<Region> region = rv->region();
-       
-       begin_reversible_command (_("set selection from region"));
-       selection->set (0, region->position(), region->last_frame());
-       commit_reversible_command ();
-
-       set_mouse_mode (Editing::MouseRange, false);
-}
-
-void
-Editor::set_selection_from_punch()
-{
-       Location* location;
-
-       if ((location = session->locations()->auto_punch_location()) == 0)  {
-               return;
-       }
-
-       set_selection_from_range (*location);
-}
-
-void
-Editor::set_selection_from_loop()
-{
-       Location* location;
-
-       if ((location = session->locations()->auto_loop_location()) == 0)  {
-               return;
-       }
-       set_selection_from_range (*location);
-}
-
-void
-Editor::set_selection_from_range (Location& loc)
-{
-       begin_reversible_command (_("set selection from range"));
-       selection->set (0, loc.start(), loc.end());
-       commit_reversible_command ();
-
-       set_mouse_mode (Editing::MouseRange, false);
-}
-
-void
-Editor::select_all_selectables_using_time_selection ()
-{
-       list<Selectable *> touched;
-
-       if (selection->time.empty()) {
-               return;
-       }
-
-       nframes_t start = selection->time[clicked_selection].start;
-       nframes_t end = selection->time[clicked_selection].end;
-
-       if (end - start < 1)  {
-               return;
-       }
-
-       for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
-       }
-
-       begin_reversible_command (_("select all from range"));
-       selection->set (touched);
-       commit_reversible_command ();
-}
-
-
-void
-Editor::select_all_selectables_using_punch()
-{
-       Location* location = session->locations()->auto_punch_location();
-       list<Selectable *> touched;
-
-       if (location == 0 || (location->end() - location->start() <= 1))  {
-               return;
-       }
-
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all from punch"));
-       selection->set (touched);
-       commit_reversible_command ();
-
-}
-
-void
-Editor::select_all_selectables_using_loop()
-{
-       Location* location = session->locations()->auto_loop_location();
-       list<Selectable *> touched;
-
-       if (location == 0 || (location->end() - location->start() <= 1))  {
-               return;
-       }
-
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all from loop"));
-       selection->set (touched);
-       commit_reversible_command ();
-
-}
-
-void
-Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
-{
-        nframes_t start;
-       nframes_t end;
-       list<Selectable *> touched;
-
-       if (after) {
-               begin_reversible_command (_("select all after cursor"));
-               start = cursor->current_frame ;
-               end = session->current_end_frame();
-       } else {
-               if (cursor->current_frame > 0) {
-                       begin_reversible_command (_("select all before cursor"));
-                       start = 0;
-                       end = cursor->current_frame - 1;
-               } else {
-                       return;
-               }
-       }
-
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
-       }
-       selection->set (touched);
-       commit_reversible_command ();
-}
-
-void
-Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
-{
-        nframes_t start;
-       nframes_t end;
-       list<Selectable *> touched;
-       bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
-
-       if (cursor->current_frame == other_cursor->current_frame) {
-               return;
-       }
-
-       begin_reversible_command (_("select all between cursors"));
-       if (other_cursor_is_first) {
-               start = other_cursor->current_frame;
-               end = cursor->current_frame - 1;
-               
-       } else {
-               start = cursor->current_frame;
-               end = other_cursor->current_frame - 1;
-       }
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
-       }
-       selection->set (touched);
-       commit_reversible_command ();
-}
-
 void
 Editor::amplitude_zoom_step (bool in)
 {
@@ -1647,6 +1384,7 @@ Editor::set_mark ()
        nframes_t pos;
        float prefix;
        bool was_floating;
+       string markername;
 
        if (get_prefix (prefix, was_floating)) {
                pos = session->audible_frame ();
@@ -1658,7 +1396,8 @@ Editor::set_mark ()
                }
        }
 
-       session->locations()->add (new Location (pos, 0, "mark", Location::IsMark), true);
+       session->locations()->next_available_name(markername,"mark");
+       session->locations()->add (new Location (pos, 0, markername, Location::IsMark), true);
 }
 
 void
@@ -1707,6 +1446,28 @@ Editor::clear_locations ()
        session->locations()->clear ();
 }
 
+void
+Editor::unhide_markers ()
+{
+       for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
+               Location *l = (*i).first;
+               if (l->is_hidden() && l->is_mark()) {
+                       l->set_hidden(false, this);
+               }
+       }
+}
+
+void
+Editor::unhide_ranges ()
+{
+       for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
+               Location *l = (*i).first;
+               if (l->is_hidden() && l->is_range_marker()) { 
+                       l->set_hidden(false, this);
+               }
+       }
+}
+
 /* INSERT/REPLACE */
 
 void
@@ -1717,7 +1478,7 @@ Editor::insert_region_list_drag (boost::shared_ptr<AudioRegion> region, int x, i
        TimeAxisView *tv;
        nframes_t where;
        AudioTimeAxisView *atv = 0;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
        
        track_canvas.window_to_world (x, y, wx, wy);
        wx += horizontal_adjustment.get_value();
@@ -1760,7 +1521,7 @@ void
 Editor::insert_region_list_selection (float times)
 {
        RouteTimeAxisView *tv = 0;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        if (clicked_audio_trackview != 0) {
                tv = clicked_audio_trackview;
@@ -1817,6 +1578,30 @@ Editor::edit_envelope ()
 
 /* PLAYBACK */
 
+void
+Editor::transition_to_rolling (bool fwd)
+{
+       if (!session) {
+               return;
+       }
+
+       switch (Config->get_slave_source()) {
+       case None:
+       case JACK:
+               break;
+       default:
+               /* transport controlled by the master */
+               return;
+       }
+
+       if (session->is_auditioning()) {
+               session->cancel_audition ();
+               return;
+       }
+       
+       session->request_transport_speed (fwd ? 1.0f : -1.0f);
+}
+
 void
 Editor::toggle_playback (bool with_abort)
 {
@@ -1928,28 +1713,6 @@ Editor::loop_location (Location& location)
        }
 }
 
-void 
-Editor::toggle_region_mute ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region()->set_muted (!clicked_regionview->region()->muted());
-       } else if (!selection->regions.empty()) {
-               bool yn = ! (*selection->regions.begin())->region()->muted();
-               selection->foreach_region (&Region::set_muted, yn);
-       }
-}
-
-void
-Editor::toggle_region_opaque ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region()->set_opaque (!clicked_regionview->region()->opaque());
-       } else if (!selection->regions.empty()) {
-               bool yn = ! (*selection->regions.begin())->region()->opaque();
-               selection->foreach_region (&Region::set_opaque, yn);
-       }
-}
-
 void
 Editor::raise_region ()
 {
@@ -1996,7 +1759,10 @@ Editor::rename_region ()
                return;
        }
 
-       dialog.set_title (_("ardour: rename region"));
+       WindowTitle title(Glib::get_application_name());
+       title += _("Rename Region");
+
+       dialog.set_title (title.get_string());
        dialog.set_name ("RegionRenameWindow");
        dialog.set_size_request (300, -1);
        dialog.set_position (Gtk::WIN_POS_MOUSE);
@@ -2117,7 +1883,7 @@ Editor::region_from_selection ()
        for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
                boost::shared_ptr<AudioRegion> current;
                boost::shared_ptr<Region> current_r;
-               Playlist *pl;
+               boost::shared_ptr<Playlist> pl;
 
                nframes_t internal_start;
                string new_name;
@@ -2150,11 +1916,13 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<AudioRegion> >& n
        nframes_t start = selection->time[clicked_selection].start;
        nframes_t end = selection->time[clicked_selection].end;
        
+       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;
-               Playlist* playlist;
+               boost::shared_ptr<Playlist> playlist;
                nframes_t internal_start;
                string new_name;
 
@@ -2180,17 +1948,22 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<AudioRegion> >& n
 void
 Editor::split_multichannel_region ()
 {
-       vector<AudioRegion*> v;
-
-       AudioRegionView* clicked_arv = dynamic_cast<AudioRegionView*>(clicked_regionview);
-       
-       if (!clicked_arv || clicked_arv->audio_region()->n_channels() < 2) {
+       if (selection->regions.empty()) {
                return;
        }
 
-       clicked_arv->audio_region()->separate_by_channel (*session, v);
+       vector<boost::shared_ptr<AudioRegion> > v;
+
+       for (list<RegionView*>::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*x);
+               
+               if (!arv || arv->audio_region()->n_channels() < 2) {
+                       continue;
+               }
 
-       /* nothing else to do, really */
+               (arv)->audio_region()->separate_by_channel (*session, v);
+       }
 }
 
 void
@@ -2209,8 +1982,10 @@ Editor::separate_region_from_selection ()
                return;
        }
 
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
                
+       sort_track_selection ();
+
        for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
                AudioTimeAxisView* atv;
@@ -2218,6 +1993,12 @@ Editor::separate_region_from_selection ()
                if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
 
                        if (atv->is_audio_track()) {
+
+                               /* no edits to destructive tracks */
+
+                               if (atv->audio_track()->audio_diskstream()->destructive()) {
+                                       continue;
+                               }
                                        
                                if ((playlist = atv->playlist()) != 0) {
                                        if (!doing_undo) {
@@ -2255,7 +2036,7 @@ Editor::separate_regions_using_location (Location& loc)
                return;
        }
 
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        /* XXX i'm unsure as to whether this should operate on selected tracks only 
           or the entire enchillada. uncomment the below line to correct the behaviour 
@@ -2271,6 +2052,12 @@ Editor::separate_regions_using_location (Location& loc)
 
                        if (atv->is_audio_track()) {
                                        
+                               /* no edits to destructive tracks */
+
+                               if (atv->audio_track()->audio_diskstream()->destructive()) {
+                                       continue;
+                               }
+
                                if ((playlist = atv->playlist()) != 0) {
                                         XMLNode *before;
                                        if (!doing_undo) {
@@ -2300,73 +2087,76 @@ Editor::separate_regions_using_location (Location& loc)
 void
 Editor::crop_region_to_selection ()
 {
-       if (selection->time.empty()) {
+       if (selection->time.empty() || selection->tracks.empty()) {
                return;
        }
 
-       vector<Playlist*> playlists;
-       Playlist *playlist;
-
-       if (clicked_trackview != 0) {
-
-               if ((playlist = clicked_trackview->playlist()) == 0) {
-                       return;
-               }
-
-               playlists.push_back (playlist);
+       vector<boost::shared_ptr<Playlist> > playlists;
+       boost::shared_ptr<Playlist> playlist;
 
-       } else {
+       sort_track_selection ();
+       
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
                
-               for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-                       AudioTimeAxisView* atv;
+               AudioTimeAxisView* atv;
+               
+               if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+                       
+                       if (atv->is_audio_track()) {
+                               
+                               /* no edits to destructive tracks */
 
-                       if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+                               if (atv->audio_track()->audio_diskstream()->destructive()) {
+                                       continue;
+                               }
 
-                               if (atv->is_audio_track()) {
-                                       
-                                       if ((playlist = atv->playlist()) != 0) {
-                                               playlists.push_back (playlist);
-                                       }
+                               if ((playlist = atv->playlist()) != 0) {
+                                       playlists.push_back (playlist);
                                }
                        }
                }
        }
 
-       if (!playlists.empty()) {
-
-               nframes_t start;
-               nframes_t end;
-               nframes_t cnt;
-
-               begin_reversible_command (_("trim to selection"));
-
-               for (vector<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-                       
-                       boost::shared_ptr<Region> region;
-                       
-                       start = selection->time.start();
-
-                       if ((region = (*i)->top_region_at(start)) == 0) {
-                               continue;
-                       }
-                       
-                       /* now adjust lengths to that we do the right thing
-                          if the selection extends beyond the region
-                       */
-                       
-                       start = max (start, region->position());
-                       end = min (selection->time.end_frame(), start + region->length() - 1);
-                       cnt = end - start + 1;
-
-                        XMLNode &before = (*i)->get_state();
-                       region->trim_to (start, cnt, this);
-                        XMLNode &after = (*i)->get_state();
-                       session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
+       if (playlists.empty()) {
+               return;
+       }
+               
+       nframes_t start;
+       nframes_t end;
+       nframes_t cnt;
+       
+       begin_reversible_command (_("trim to selection"));
+       
+       for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               
+               boost::shared_ptr<Region> region;
+               
+               start = selection->time.start();
+               
+               if ((region = (*i)->top_region_at(start)) == 0) {
+                       continue;
                }
-
-               commit_reversible_command ();
+               
+               /* now adjust lengths to that we do the right thing
+                  if the selection extends beyond the region
+               */
+               
+               start = max (start, region->position());
+               if (max_frames - start < region->length()) {
+                       end = start + region->length() - 1;
+               } else {
+                       end = max_frames;
+               }
+               end = min (selection->time.end_frame(), end);
+               cnt = end - start + 1;
+               
+               XMLNode &before = (*i)->get_state();
+               region->trim_to (start, cnt, this);
+               XMLNode &after = (*i)->get_state();
+               session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
        }
+       
+       commit_reversible_command ();
 }              
 
 void
@@ -2391,7 +2181,7 @@ Editor::region_fill_track ()
                if (!ar)
                        continue;
 
-               Playlist* pl = region->playlist();
+               boost::shared_ptr<Playlist> pl = region->playlist();
 
                if (end <= region->last_frame()) {
                        return;
@@ -2435,7 +2225,7 @@ Editor::region_fill_selection ()
        nframes_t start = selection->time[clicked_selection].start;
        nframes_t end = selection->time[clicked_selection].end;
 
-       Playlist *playlist; 
+       boost::shared_ptr<Playlist> playlist; 
 
        if (selection->tracks.empty()) {
                return;
@@ -2748,8 +2538,10 @@ Editor::freeze_route ()
        if (interthread_progress_window == 0) {
                build_interthread_progress_window ();
        }
-       
-       interthread_progress_window->set_title (_("ardour: freeze"));
+
+       WindowTitle title(Glib::get_application_name());
+       title += _("Freeze");
+       interthread_progress_window->set_title (title.get_string());
        interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
        interthread_progress_window->show_all ();
        interthread_progress_bar.set_fraction (0.0f);
@@ -2763,8 +2555,14 @@ Editor::freeze_route ()
        itt.done = false;
        itt.cancel = false;
        itt.progress = 0.0f;
+       
+       pthread_attr_t attr;
+       pthread_attr_init(&attr);
+       pthread_attr_setstacksize(&attr, 500000);
 
-       pthread_create (&itt.thread, 0, _freeze_thread, this);
+       pthread_create (&itt.thread, &attr, _freeze_thread, this);
+
+       pthread_attr_destroy(&attr);
 
        track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
 
@@ -2785,15 +2583,15 @@ Editor::bounce_range_selection ()
                return;
        }
 
-       TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
+       TrackSelection views = selection->tracks;
 
        nframes_t start = selection->time[clicked_selection].start;
        nframes_t end = selection->time[clicked_selection].end;
        nframes_t cnt = end - start + 1;
-       
+
        begin_reversible_command (_("bounce range"));
 
-       for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
+       for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
 
                AudioTimeAxisView* atv;
 
@@ -2801,7 +2599,7 @@ Editor::bounce_range_selection ()
                        continue;
                }
                
-               Playlist* playlist;
+               boost::shared_ptr<Playlist> playlist;
                
                if ((playlist = atv->playlist()) == 0) {
                        return;
@@ -2812,7 +2610,7 @@ Editor::bounce_range_selection ()
                itt.done = false;
                itt.cancel = false;
                itt.progress = false;
-               
+
                 XMLNode &before = playlist->get_state();
                atv->audio_track()->bounce_range (start, cnt, itt);
                 XMLNode &after = playlist->get_state();
@@ -2820,8 +2618,6 @@ Editor::bounce_range_selection ()
        }
        
        commit_reversible_command ();
-       
-       delete views;
 }
 
 void
@@ -2917,7 +2713,7 @@ Editor::cut_copy_points (CutCopyOp op)
 }
 
 struct PlaylistState {
-    Playlist* playlist;
+    boost::shared_ptr<Playlist> playlist;
     XMLNode*  before;
 };
 
@@ -2927,21 +2723,38 @@ struct lt_playlist {
     }
 };
        
+struct PlaylistMapping { 
+    TimeAxisView* tv;
+    boost::shared_ptr<AudioPlaylist> pl;
+
+    PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
+};
+
 void
 Editor::cut_copy_regions (CutCopyOp op)
 {
-        typedef std::map<AudioPlaylist*,AudioPlaylist*> PlaylistMapping;
-       PlaylistMapping pmap;
-       nframes_t first_position = max_frames;
+       /* 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.
+       */
+
+       vector<PlaylistMapping> pmap;
 
+       nframes_t first_position = max_frames;
+       
        set<PlaylistState, lt_playlist> freezelist;
        pair<set<PlaylistState, lt_playlist>::iterator,bool> insert_result;
+       
+       /* get ordering correct before we cut/copy */
+       
+       selection->regions.sort_by_position_and_track ();
 
        for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+
                first_position = min ((*x)->region()->position(), first_position);
 
                if (op == Cut || op == Clear) {
-                       AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region()->playlist());
+                       boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist>((*x)->region()->playlist());
+
                        if (pl) {
 
                                PlaylistState before;
@@ -2949,70 +2762,102 @@ Editor::cut_copy_regions (CutCopyOp op)
                                before.before = &pl->get_state();
                                
                                insert_result = freezelist.insert (before);
-
+                               
                                if (insert_result.second) {
                                        pl->freeze ();
                                }
                        }
                }
+
+               TimeAxisView* tv = &(*x)->get_trackview();
+               vector<PlaylistMapping>::iterator z;
+
+               for (z = pmap.begin(); z != pmap.end(); ++z) {
+                       if ((*z).tv == tv) {
+                               break;
+                       }
+               }
+               
+               if (z == pmap.end()) {
+                       pmap.push_back (PlaylistMapping (tv));
+               }
        }
 
        for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ) {
 
-               AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region()->playlist());
-               AudioPlaylist* npl;
+               boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist>((*x)->region()->playlist());
+               
+               if (!pl) {
+                       /* impossible, but this handles it for the future */
+                       continue;
+               }
+
+               TimeAxisView& tv = (*x)->get_trackview();
+               boost::shared_ptr<AudioPlaylist> npl;
                RegionSelection::iterator tmp;
                
                tmp = x;
                ++tmp;
 
-               if (pl) {
-
-                       PlaylistMapping::iterator pi = pmap.find (pl);
-                       
-                       if (pi == pmap.end()) {
-                               npl = new AudioPlaylist (*session, "cutlist", true);
-                               npl->freeze();
-                               pmap[pl] = npl;
-                       } else {
-                               npl = pi->second;
-                       }
-
-                       // FIXME
-                       boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>((*x)->region());
-                       switch (op) {
-                       case Cut:
-                               if (!ar) break;
-
-                               npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
-                               pl->remove_region (((*x)->region()));
-                               break;
-
-                       case Copy:
-                               if (!ar) break;
-
-                               npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
-                               break;
-
-                       case Clear:
-                               pl->remove_region (((*x)->region()));
+               vector<PlaylistMapping>::iterator z;
+               
+               for (z = pmap.begin(); z != pmap.end(); ++z) {
+                       if ((*z).tv == &tv) {
                                break;
                        }
                }
+               
+               assert (z != pmap.end());
+               
+               if (!(*z).pl) {
+                       npl = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (*session, "cutlist", true));
+                       npl->freeze();
+                       (*z).pl = npl;
+               } else {
+                       npl = (*z).pl;
+               }
+               
+               boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>((*x)->region());
+               boost::shared_ptr<Region> _xx;
+               
+               switch (op) {
+               case Cut:
+                       if (!ar) break;
+                       
+                       _xx = RegionFactory::create ((*x)->region());
+                       npl->add_region (_xx, (*x)->region()->position() - first_position);
+                       pl->remove_region (((*x)->region()));
+                       break;
+                       
+               case Copy:
+                       if (!ar) break;
+
+                       /* copy region before adding, so we're not putting same object into two different playlists */
+                       npl->add_region (RegionFactory::create ((*x)->region()), (*x)->region()->position() - first_position);
+                       break;
+                       
+               case Clear:
+                       pl->remove_region (((*x)->region()));
+                       break;
+               }
 
                x = tmp;
        }
-
-       list<Playlist*> foo;
-
-       for (PlaylistMapping::iterator i = pmap.begin(); i != pmap.end(); ++i) {
-               foo.push_back (i->second);
+       
+       list<boost::shared_ptr<Playlist> > foo;
+       
+       /* the pmap is in the same order as the tracks in which selected regions occured */
+       
+       for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
+               (*i).pl->thaw();
+               foo.push_back ((*i).pl);
        }
+       
 
        if (!foo.empty()) {
                cut_buffer->set (foo);
        }
-       
+
        for (set<PlaylistState, lt_playlist>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
                (*pl).playlist->thaw ();
                session->add_command (new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
@@ -3072,15 +2917,19 @@ Editor::paste_internal (nframes_t position, float times)
        TrackSelection::iterator i;
        size_t nth;
 
+       /* get everything in the correct order */
+
+       sort_track_selection ();
+
        for (nth = 0, i = selection->tracks.begin(); i != selection->tracks.end(); ++i, ++nth) {
-               
+
                /* undo/redo is handled by individual tracks */
 
                if ((*i)->paste (position, times, *cut_buffer, nth)) {
                        commit = true;
                }
        }
-
+       
        if (commit) {
                commit_reversible_command ();
        }
@@ -3100,18 +2949,20 @@ Editor::paste_named_selection (float times)
        TreeModel::iterator i = selected->get_selected();
        NamedSelection* ns = (*i)[named_selection_columns.selection];
 
-       list<Playlist*>::iterator chunk;
-       list<Playlist*>::iterator tmp;
+       list<boost::shared_ptr<Playlist> >::iterator chunk;
+       list<boost::shared_ptr<Playlist> >::iterator tmp;
 
        chunk = ns->playlists.begin();
                
        begin_reversible_command (_("paste chunk"));
+       
+       sort_track_selection ();
 
        for (t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
                
                AudioTimeAxisView* atv;
-               Playlist* pl;
-               AudioPlaylist* apl;
+               boost::shared_ptr<Playlist> pl;
+               boost::shared_ptr<AudioPlaylist> apl;
 
                if ((atv = dynamic_cast<AudioTimeAxisView*> (*t)) == 0) {
                        continue;
@@ -3120,8 +2971,8 @@ Editor::paste_named_selection (float times)
                if ((pl = atv->playlist()) == 0) {
                        continue;
                }
-
-               if ((apl = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
+               
+               if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
                        continue;
                }
 
@@ -3129,7 +2980,7 @@ Editor::paste_named_selection (float times)
                ++tmp;
 
                 XMLNode &before = apl->get_state();
-               apl->paste (**chunk, edit_cursor->current_frame, times);
+               apl->paste (*chunk, edit_cursor->current_frame, times);
                session->add_command(new MementoCommand<AudioPlaylist>(*apl, &before, &apl->get_state()));
 
                if (tmp != ns->playlists.end()) {
@@ -3143,7 +2994,7 @@ Editor::paste_named_selection (float times)
 void
 Editor::duplicate_some_regions (RegionSelection& regions, float times)
 {
-       Playlist *playlist; 
+       boost::shared_ptr<Playlist> playlist; 
        RegionSelection sel = regions; // clear (below) will clear the argument list
                
        begin_reversible_command (_("duplicate region"));
@@ -3181,7 +3032,7 @@ Editor::duplicate_selection (float times)
                return;
        }
 
-       Playlist *playlist; 
+       boost::shared_ptr<Playlist> playlist; 
        vector<boost::shared_ptr<AudioRegion> > new_regions;
        vector<boost::shared_ptr<AudioRegion> >::iterator ri;
                
@@ -3218,8 +3069,6 @@ Editor::reset_point_selection ()
 {
        /* reset all selected points to the relevant default value */
 
-       cerr << "point selection has " << selection->points.size() << " entries\n";
-       
        for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
                
                AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
@@ -3247,20 +3096,20 @@ Editor::center_edit_cursor ()
 }
 
 void
-Editor::clear_playlist (Playlist& playlist)
+Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
 {
        begin_reversible_command (_("clear playlist"));
-        XMLNode &before = playlist.get_state();
-       playlist.clear ();
-        XMLNode &after = playlist.get_state();
-       session->add_command (new MementoCommand<Playlist>(playlist, &before, &after));
+        XMLNode &before = playlist->get_state();
+       playlist->clear ();
+        XMLNode &after = playlist->get_state();
+       session->add_command (new MementoCommand<Playlist>(*playlist.get(), &before, &after));
        commit_reversible_command ();
 }
 
 void
 Editor::nudge_track (bool use_edit_cursor, bool forwards)
 {
-       Playlist *playlist; 
+       boost::shared_ptr<Playlist> playlist; 
        nframes_t distance;
        nframes_t next_distance;
        nframes_t start;
@@ -3408,7 +3257,7 @@ Editor::apply_filter (AudioFilter& filter, string command)
                if (!arv)
                        continue;
 
-               Playlist* playlist = arv->region()->playlist();
+               boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
 
                RegionSelection::iterator tmp;
                
@@ -3494,13 +3343,40 @@ Editor::brush (nframes_t pos)
        }
 }
 
+void
+Editor::reset_region_gain_envelopes ()
+{
+       if (!session || selection->regions.empty()) {
+               return;
+       }
+
+       session->begin_reversible_command (_("reset region gain"));
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       AutomationList& alist (arv->audio_region()->envelope());
+                       XMLNode& before (alist.get_state());
+
+                       arv->audio_region()->set_default_envelope ();
+                       session->add_command (new MementoCommand<AutomationList>(arv->audio_region()->envelope(), &before, &alist.get_state()));
+               }
+       }
+
+       session->commit_reversible_command ();
+}
+
 void
 Editor::toggle_gain_envelope_visibility ()
 {
        for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
-               if (arv)
-                       arv->set_envelope_visible (!arv->envelope_visible());
+               if (arv) {
+                       bool x = region_envelope_visible_item->get_active();
+                       if (x != arv->envelope_visible()) {
+                               arv->set_envelope_visible (x);
+                       }
+               }
        }
 }
 
@@ -3509,7 +3385,149 @@ Editor::toggle_gain_envelope_active ()
 {
        for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
-               if (arv)
-                       arv->audio_region()->set_envelope_active (true);
+               if (arv) {
+                       bool x = region_envelope_active_item->get_active();
+                       if (x != arv->audio_region()->envelope_active()) {
+                               arv->audio_region()->set_envelope_active (x);
+                       }
+               }
+       }
+}
+
+void
+Editor::toggle_region_lock ()
+{
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_lock_item->get_active();
+                       if (x != arv->audio_region()->locked()) {
+                               arv->audio_region()->set_locked (x);
+                       }
+               }
+       }
+}
+
+void
+Editor::toggle_region_mute ()
+{
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_mute_item->get_active();
+                       if (x != arv->audio_region()->muted()) {
+                               arv->audio_region()->set_muted (x);
+                       }
+               }
        }
 }
+
+void
+Editor::toggle_region_opaque ()
+{
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_opaque_item->get_active();
+                       if (x != arv->audio_region()->opaque()) {
+                               arv->audio_region()->set_opaque (x);
+                       }
+               }
+       }
+}
+
+void
+Editor::set_fade_in_shape (AudioRegion::FadeShape shape)
+{
+       begin_reversible_command (_("set fade in shape"));
+
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
+               }
+
+               AutomationList& alist = tmp->audio_region()->fade_in();
+               XMLNode &before = alist.get_state();
+
+               tmp->audio_region()->set_fade_in_shape (shape);
+               
+               XMLNode &after = alist.get_state();
+               session->add_command(new MementoCommand<AutomationList>(alist, &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::set_fade_out_shape (AudioRegion::FadeShape shape)
+{
+       begin_reversible_command (_("set fade out shape"));
+
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
+               }
+
+               AutomationList& alist = tmp->audio_region()->fade_out();
+               XMLNode &before = alist.get_state();
+
+               tmp->audio_region()->set_fade_out_shape (shape);
+               
+               XMLNode &after = alist.get_state();
+               session->add_command(new MementoCommand<AutomationList>(alist, &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::set_fade_in_active (bool yn)
+{
+       begin_reversible_command (_("set fade in active"));
+
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
+               }
+
+
+               boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
+
+               XMLNode &before = ar->get_state();
+
+               ar->set_fade_in_active (yn);
+               
+               XMLNode &after = ar->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
+       }
+}
+
+void
+Editor::set_fade_out_active (bool yn)
+{
+       begin_reversible_command (_("set fade out active"));
+
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
+               }
+
+               boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
+
+               XMLNode &before = ar->get_state();
+
+               ar->set_fade_out_active (yn);
+               
+               XMLNode &after = ar->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
+       }
+}
+