modified fix from carl for region copy-moves-original-to-start bug; change verbose...
[ardour.git] / gtk2_ardour / editor_ops.cc
index 7a7101d831c0ad7059ef9147a1744a490d7f6c38..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>
 #include <string>
 #include <map>
 
-#include <sndfile.h>
-
 #include <pbd/error.h>
 #include <pbd/basename.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/memento_command.h>
 
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/choice.h>
+#include <gtkmm2ext/window_title.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/session.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/audioregion.h>
-#include <ardour/diskstream.h>
-#include <ardour/filesource.h>
-#include <ardour/sndfilesource.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_ui.h"
 #include "audio_time_axis.h"
 #include "automation_time_axis.h"
 #include "streamview.h"
-#include "regionview.h"
+#include "audio_region_view.h"
 #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"
 
 #include "i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Editing;
 
 /***********************************************************************
@@ -91,25 +91,8 @@ Editor::redo (uint32_t n)
        }
 }
 
-void
-Editor::set_meter_hold (int32_t cnt)
-{
-       if (session) {
-               session->set_meter_hold (cnt);
-       }
-}
-
-void
-Editor::set_meter_falloff (float val)
-{
-       if (session) {
-               session->set_meter_falloff (val);
-       }
-}
-
-
 int
-Editor::ensure_cursor (jack_nframes_t *pos)
+Editor::ensure_cursor (nframes_t *pos)
 {
        *pos = edit_cursor->current_frame;
        return 0;
@@ -122,32 +105,35 @@ Editor::split_region ()
 }
 
 void
-Editor::split_region_at (jack_nframes_t where)
+Editor::split_region_at (nframes_t where)
 {
-       split_regions_at (where, selection->audio_regions);
+       split_regions_at (where, selection->regions);
 }
 
 void
-Editor::split_regions_at (jack_nframes_t where, AudioRegionSelection& regions)
+Editor::split_regions_at (nframes_t where, RegionSelection& regions)
 {
        begin_reversible_command (_("split"));
 
        snap_to (where);
-       for (AudioRegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
+       for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
 
-               AudioRegionSelection::iterator tmp;
+               RegionSelection::iterator tmp;
                
                tmp = a;
                ++tmp;
 
-               Playlist* pl = (*a)->region.playlist();
+               boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
 
-               _new_regionviews_show_envelope = (*a)->envelope_visible();
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*a);
+               if (arv)
+                       _new_regionviews_show_envelope = arv->envelope_visible();
                
                if (pl) {
-                       session->add_undo (pl->get_memento());
-                       pl->split_region ((*a)->region, where);
-                       session->add_redo_no_execute (pl->get_memento());
+                        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));
                }
 
                a = tmp;
@@ -164,21 +150,22 @@ 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"));
-       session->add_undo (playlist->get_memento());
-       playlist->remove_region (&clicked_regionview->region);
-       session->add_redo_no_execute (playlist->get_memento());
+        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 ();
 }
 
 void
 Editor::destroy_clicked_region ()
 {
-       int32_t selected = selection->audio_regions.size();
+       uint32_t selected = selection->regions.size();
 
-       if (!session || clicked_regionview == 0 && selected == 0) {
+       if (!session || !selected) {
                return;
        }
 
@@ -191,78 +178,68 @@ Do you really want to destroy %1 ?"),
                           (selected > 1 ? 
                            _("these regions") : _("this region")));
 
+       choices.push_back (_("No, do nothing."));
+
        if (selected > 1) {
                choices.push_back (_("Yes, destroy them."));
        } else {
                choices.push_back (_("Yes, destroy it."));
        }
 
-       choices.push_back (_("No, do nothing."));
-
        Gtkmm2ext::Choice prompter (prompt, choices);
-
-       prompter.chosen.connect (ptr_fun (Main::quit));
-       prompter.show_all ();
-
-       Main::run ();
-               
-       if (prompter.get_choice() != 0) {
+       
+       if (prompter.run() == 0) { /* first choice */
                return;
        }
 
-       if (selected > 0) {
-               list<Region*> r;
+       if (selected) {
+               list<boost::shared_ptr<Region> > r;
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       r.push_back (&(*i)->region);
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       r.push_back ((*i)->region());
                }
 
                session->destroy_regions (r);
-
-       } else if (clicked_regionview) {
-               session->destroy_region (&clicked_regionview->region);
        } 
 }
 
-AudioRegion *
+boost::shared_ptr<Region>
 Editor::select_region_for_operation (int dir, TimeAxisView **tv)
 {
-       AudioRegionView* rv;
-       AudioRegion *region;
-       jack_nframes_t start = 0;
+       RegionView* rv;
+       boost::shared_ptr<Region> region;
+       nframes_t start = 0;
 
        if (selection->time.start () == selection->time.end_frame ()) {
                
                /* no current selection-> is there a selected regionview? */
 
-               if (selection->audio_regions.empty()) {
-                       return 0;
+               if (selection->regions.empty()) {
+                       return region;
                }
 
        } 
 
-       region = 0;
+       if (!selection->regions.empty()) {
 
-       if (!selection->audio_regions.empty()) {
-
-               rv = *(selection->audio_regions.begin());
+               rv = *(selection->regions.begin());
                (*tv) = &rv->get_time_axis_view();
-               region = &rv->region;
+               region = rv->region();
 
        } else if (!selection->tracks.empty()) {
 
                (*tv) = selection->tracks.front();
 
-               AudioTimeAxisView* atv;
+               RouteTimeAxisView* rtv;
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> (*tv)) != 0) {
-                       Playlist *pl;
+               if ((rtv = dynamic_cast<RouteTimeAxisView*> (*tv)) != 0) {
+                       boost::shared_ptr<Playlist> pl;
                        
-                       if ((pl = atv->playlist()) == 0) {
-                               return 0;
+                       if ((pl = rtv->playlist()) == 0) {
+                               return region;
                        }
                        
-                       region = dynamic_cast<AudioRegion*> (pl->top_region_at (start));
+                       region = pl->top_region_at (start);
                }
        } 
        
@@ -273,8 +250,8 @@ void
 Editor::extend_selection_to_end_of_region (bool next)
 {
        TimeAxisView *tv;
-       Region *region;
-       jack_nframes_t start;
+       boost::shared_ptr<Region> region;
+       nframes_t start;
 
        if ((region = select_region_for_operation (next ? 1 : 0, &tv)) == 0) {
                return;
@@ -301,8 +278,8 @@ void
 Editor::extend_selection_to_start_of_region (bool previous)
 {
        TimeAxisView *tv;
-       Region *region;
-       jack_nframes_t end;
+       boost::shared_ptr<Region> region;
+       nframes_t end;
 
        if ((region = select_region_for_operation (previous ? -1 : 0, &tv)) == 0) {
                return;
@@ -329,27 +306,28 @@ Editor::extend_selection_to_start_of_region (bool previous)
 void
 Editor::nudge_forward (bool next)
 {
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
+       nframes_t distance;
+       nframes_t next_distance;
 
        if (!session) return;
        
-       if (!selection->audio_regions.empty()) {
+       if (!selection->regions.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
                        
-                       distance = get_nudge_distance (r.position(), next_distance);
+                       distance = get_nudge_distance (r->position(), next_distance);
 
                        if (next) {
                                distance = next_distance;
                        }
 
-                       session->add_undo (r.playlist()->get_memento());
-                       r.set_position (r.position() + distance, this);
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                        XMLNode &before = r->playlist()->get_state();
+                       r->set_position (r->position() + distance, this);
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command (new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
@@ -363,32 +341,33 @@ Editor::nudge_forward (bool next)
 void
 Editor::nudge_backward (bool next)
 {
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
+       nframes_t distance;
+       nframes_t next_distance;
 
        if (!session) return;
        
-       if (!selection->audio_regions.empty()) {
+       if (!selection->regions.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
 
-                       distance = get_nudge_distance (r.position(), next_distance);
+                       distance = get_nudge_distance (r->position(), next_distance);
                        
                        if (next) {
                                distance = next_distance;
                        }
 
-                       session->add_undo (r.playlist()->get_memento());
+                        XMLNode &before = r->playlist()->get_state();
                        
-                       if (r.position() > distance) {
-                               r.set_position (r.position() - distance, this);
+                       if (r->position() > distance) {
+                               r->set_position (r->position() - distance, this);
                        } else {
-                               r.set_position (0, this);
+                               r->set_position (0, this);
                        }
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
@@ -400,7 +379,7 @@ Editor::nudge_backward (bool next)
                if (playhead_cursor->current_frame > distance) {
                        session->request_locate (playhead_cursor->current_frame - distance);
                } else {
-                       session->request_locate (0);
+                       session->goto_start();
                }
        }
 }
@@ -408,22 +387,23 @@ Editor::nudge_backward (bool next)
 void
 Editor::nudge_forward_capture_offset ()
 {
-       jack_nframes_t distance;
+       nframes_t distance;
 
        if (!session) return;
        
-       if (!selection->audio_regions.empty()) {
+       if (!selection->regions.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
                distance = session->worst_output_latency();
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
                        
-                       session->add_undo (r.playlist()->get_memento());
-                       r.set_position (r.position() + distance, this);
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                       XMLNode &before = r->playlist()->get_state();
+                       r->set_position (r->position() + distance, this);
+                       XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
@@ -434,27 +414,28 @@ Editor::nudge_forward_capture_offset ()
 void
 Editor::nudge_backward_capture_offset ()
 {
-       jack_nframes_t distance;
+       nframes_t distance;
 
        if (!session) return;
        
-       if (!selection->audio_regions.empty()) {
+       if (!selection->regions.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
                distance = session->worst_output_latency();
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
 
-                       session->add_undo (r.playlist()->get_memento());
+                        XMLNode &before = r->playlist()->get_state();
                        
-                       if (r.position() > distance) {
-                               r.set_position (r.position() - distance, this);
+                       if (r->position() > distance) {
+                               r->set_position (r->position() - distance, this);
                        } else {
-                               r.set_position (0, this);
+                               r->set_position (0, this);
                        }
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
@@ -466,7 +447,7 @@ Editor::nudge_backward_capture_offset ()
 void
 Editor::move_to_start ()
 {
-       session->request_locate (0);
+       session->goto_start ();
 }
 
 void
@@ -479,9 +460,9 @@ Editor::move_to_end ()
 void
 Editor::build_region_boundary_cache ()
 {
-       jack_nframes_t pos = 0;
+       nframes_t pos = 0;
        RegionPoint point;
-       Region *r;
+       boost::shared_ptr<Region> r;
        TrackViewList tracks;
 
        region_boundary_cache.clear ();
@@ -535,7 +516,7 @@ Editor::build_region_boundary_cache ()
                        }
                }
 
-               jack_nframes_t rpos;
+               nframes_t rpos;
                
                switch (snap_type) {
                case SnapToRegionStart:
@@ -577,23 +558,23 @@ Editor::build_region_boundary_cache ()
        }
 }
 
-Region*
-Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
+boost::shared_ptr<Region>
+Editor::find_next_region (nframes_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
 {
        TrackViewList::iterator i;
-       jack_nframes_t closest = max_frames;
-       Region* ret = 0;
-       jack_nframes_t rpos = 0;
+       nframes_t closest = max_frames;
+       boost::shared_ptr<Region> ret;
+       nframes_t rpos = 0;
 
        float track_speed;
-       jack_nframes_t track_frame;
+       nframes_t track_frame;
        AudioTimeAxisView *atav;
 
        for (i = tracks.begin(); i != tracks.end(); ++i) {
 
-               jack_nframes_t distance;
-               Region* r;
-
+               nframes_t distance;
+               boost::shared_ptr<Region> r;
+               
                track_speed = 1.0f;
                if ( (atav = dynamic_cast<AudioTimeAxisView*>(*i)) != 0 ) {
                        if (atav->get_diskstream()!=0)
@@ -642,8 +623,8 @@ Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir,
 void
 Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
 {
-       Region* r;
-       jack_nframes_t pos = cursor->current_frame;
+       boost::shared_ptr<Region> r;
+       nframes_t pos = cursor->current_frame;
 
        if (!session) {
                return;
@@ -722,11 +703,11 @@ Editor::cursor_to_previous_region_point (Cursor* cursor, RegionPoint point)
 void
 Editor::cursor_to_selection_start (Cursor *cursor)
 {
-       jack_nframes_t pos = 0;
+       nframes_t pos = 0;
        switch (mouse_mode) {
        case MouseObject:
-               if (!selection->audio_regions.empty()) {
-                       pos = selection->audio_regions.start();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.start();
                }
                break;
 
@@ -750,12 +731,12 @@ Editor::cursor_to_selection_start (Cursor *cursor)
 void
 Editor::cursor_to_selection_end (Cursor *cursor)
 {
-       jack_nframes_t pos = 0;
+       nframes_t pos = 0;
 
        switch (mouse_mode) {
        case MouseObject:
-               if (!selection->audio_regions.empty()) {
-                       pos = selection->audio_regions.end_frame();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.end_frame();
                }
                break;
 
@@ -776,11 +757,44 @@ 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 ()
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
+       nframes_t pos;
+       nframes_t cnt;
        float prefix;
        bool was_floating;
 
@@ -788,15 +802,15 @@ Editor::playhead_backward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
                } else {
-                       cnt = (jack_nframes_t) prefix;
+                       cnt = (nframes_t) prefix;
                }
        }
 
        pos = playhead_cursor->current_frame;
 
-       if ((jack_nframes_t) pos < cnt) {
+       if ((nframes_t) pos < cnt) {
                pos = 0;
        } else {
                pos -= cnt;
@@ -813,8 +827,8 @@ Editor::playhead_backward ()
 void
 Editor::playhead_forward ()
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
+       nframes_t pos;
+       nframes_t cnt;
        bool was_floating;
        float prefix;
 
@@ -822,9 +836,9 @@ Editor::playhead_forward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
                } else {
-                       cnt = (jack_nframes_t) floor (prefix);
+                       cnt = (nframes_t) floor (prefix);
                }
        }
 
@@ -853,8 +867,8 @@ Editor::cursor_align (bool playhead_to_edit)
 void
 Editor::edit_cursor_backward ()
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
+       nframes_t pos;
+       nframes_t cnt;
        float prefix;
        bool was_floating;
 
@@ -862,15 +876,15 @@ Editor::edit_cursor_backward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
                } else {
-                       cnt = (jack_nframes_t) prefix;
+                       cnt = (nframes_t) prefix;
                }
        }
 
        pos = edit_cursor->current_frame;
 
-       if ((jack_nframes_t) pos < cnt) {
+       if ((nframes_t) pos < cnt) {
                pos = 0;
        } else {
                pos -= cnt;
@@ -882,8 +896,8 @@ Editor::edit_cursor_backward ()
 void
 Editor::edit_cursor_forward ()
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
+       nframes_t pos;
+       nframes_t cnt;
        bool was_floating;
        float prefix;
 
@@ -891,9 +905,9 @@ Editor::edit_cursor_forward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
                } else {
-                       cnt = (jack_nframes_t) floor (prefix);
+                       cnt = (nframes_t) floor (prefix);
                }
        }
 
@@ -906,16 +920,16 @@ Editor::goto_frame ()
 {
        float prefix;
        bool was_floating;
-       jack_nframes_t frame;
+       nframes_t frame;
 
        if (get_prefix (prefix, was_floating)) {
                return;
        }
 
        if (was_floating) {
-               frame = (jack_nframes_t) floor (prefix * session->frame_rate());
+               frame = (nframes_t) floor (prefix * session->frame_rate());
        } else {
-               frame = (jack_nframes_t) floor (prefix);
+               frame = (nframes_t) floor (prefix);
        }
 
        session->request_locate (frame);
@@ -924,19 +938,19 @@ Editor::goto_frame ()
 void
 Editor::scroll_backward (float pages)
 {
-       jack_nframes_t frame;
-       jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
+       nframes_t frame;
+       nframes_t one_page = (nframes_t) rint (canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
-       jack_nframes_t cnt;
+       nframes_t cnt;
        
        if (get_prefix (prefix, was_floating)) {
-               cnt = (jack_nframes_t) floor (pages * one_page);
+               cnt = (nframes_t) floor (pages * one_page);
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate());
                } else {
-                       cnt = (jack_nframes_t) floor (prefix * one_page);
+                       cnt = (nframes_t) floor (prefix * one_page);
                }
        }
 
@@ -946,35 +960,35 @@ Editor::scroll_backward (float pages)
                frame = leftmost_frame - cnt;
        }
 
-       reposition_x_origin (frame);
+       reset_x_origin (frame);
 }
 
 void
 Editor::scroll_forward (float pages)
 {
-       jack_nframes_t frame;
-       jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
+       nframes_t frame;
+       nframes_t one_page = (nframes_t) rint (canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
-       jack_nframes_t cnt;
+       nframes_t cnt;
        
        if (get_prefix (prefix, was_floating)) {
-               cnt = (jack_nframes_t) floor (pages * one_page);
+               cnt = (nframes_t) floor (pages * one_page);
        } else {
                if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
+                       cnt = (nframes_t) floor (prefix * session->frame_rate());
                } else {
-                       cnt = (jack_nframes_t) floor (prefix * one_page);
+                       cnt = (nframes_t) floor (prefix * one_page);
                }
        }
 
-       if (ULONG_MAX - cnt < leftmost_frame) {
-               frame = ULONG_MAX - cnt;
+       if (max_frames - cnt < leftmost_frame) {
+               frame = max_frames - cnt;
        } else {
                frame = leftmost_frame + cnt;
        }
 
-       reposition_x_origin (frame);
+       reset_x_origin (frame);
 }
 
 void
@@ -990,7 +1004,12 @@ Editor::scroll_tracks_down ()
                cnt = (int) floor (prefix);
        }
 
-       vertical_adjustment.set_value (vertical_adjustment.get_value() + (cnt * vertical_adjustment.get_page_size()));
+       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;
+       }
+       vertical_adjustment.set_value (vert_value);
 }
 
 void
@@ -1012,15 +1031,21 @@ Editor::scroll_tracks_up ()
 void
 Editor::scroll_tracks_down_line ()
 {
+
         Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
-       adj->set_value (adj->get_value() + 10);
+       double vert_value = adj->get_value() + 20;
+
+       if (vert_value>adj->get_upper() - canvas_height) {
+               vert_value = adj->get_upper() - canvas_height;
+       }
+       adj->set_value (vert_value);
 }
 
 void
 Editor::scroll_tracks_up_line ()
 {
         Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
-       adj->set_value (adj->get_value() - 10);
+       adj->set_value (adj->get_value() - 20);
 }
 
 /* ZOOM */
@@ -1028,14 +1053,16 @@ Editor::scroll_tracks_up_line ()
 void
 Editor::temporal_zoom_step (bool coarser)
 {
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::temporal_zoom_step), coarser));
+
        double nfpu;
 
        nfpu = frames_per_unit;
        
        if (coarser) { 
-               nfpu *= 2.0;
+               nfpu *= 1.61803399;
        } else { 
-               nfpu = max(1.0,(nfpu/2.0));
+               nfpu = max(1.0,(nfpu/1.61803399));
        }
 
        temporal_zoom (nfpu);
@@ -1046,17 +1073,17 @@ Editor::temporal_zoom (gdouble fpu)
 {
        if (!session) return;
        
-       jack_nframes_t current_page = current_page_frames();
-       jack_nframes_t current_leftmost = leftmost_frame;
-       jack_nframes_t current_rightmost;
-       jack_nframes_t current_center;
-       jack_nframes_t new_page;
-       jack_nframes_t leftmost_after_zoom = 0;
+       nframes_t current_page = current_page_frames();
+       nframes_t current_leftmost = leftmost_frame;
+       nframes_t current_rightmost;
+       nframes_t current_center;
+       nframes_t new_page;
+       nframes_t leftmost_after_zoom = 0;
        double nfpu;
 
        nfpu = fpu;
        
-       new_page = (jack_nframes_t) floor (canvas_width * nfpu);
+       new_page = (nframes_t) floor (canvas_width * nfpu);
 
        switch (zoom_focus) {
        case ZoomFocusLeft:
@@ -1092,7 +1119,7 @@ Editor::temporal_zoom (gdouble fpu)
 
        case ZoomFocusEdit:
                /* try to keep the edit cursor in the center */
-               if (edit_cursor->current_frame > leftmost_frame + (new_page/2)) {
+               if (edit_cursor->current_frame > new_page/2) {
                        leftmost_after_zoom = edit_cursor->current_frame - (new_page/2);
                } else {
                        leftmost_after_zoom = 0;
@@ -1120,8 +1147,8 @@ Editor::temporal_zoom_selection ()
                return;
        }
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
 
        temporal_zoom_by_frame (start, end, "zoom to selection");
 }
@@ -1129,13 +1156,15 @@ Editor::temporal_zoom_selection ()
 void
 Editor::temporal_zoom_session ()
 {
+       ENSURE_GUI_THREAD (mem_fun (*this, &Editor::temporal_zoom_session));
+
        if (session) {
-               temporal_zoom_by_frame (0, session->current_end_frame(), "zoom to session");
+               temporal_zoom_by_frame (session->current_start_frame(), session->current_end_frame(), "zoom to session");
        }
 }
 
 void
-Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const string & op)
+Editor::temporal_zoom_by_frame (nframes_t start, nframes_t end, const string & op)
 {
        if (!session) return;
 
@@ -1143,7 +1172,7 @@ Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const
                return;
        }
 
-       jack_nframes_t range = end - start;
+       nframes_t range = end - start;
 
        double new_fpu = (double)range / (double)canvas_width;
 //     double p2 = 1.0;
@@ -1153,9 +1182,9 @@ Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const
 //     }
 //     new_fpu = p2;
        
-       jack_nframes_t new_page = (jack_nframes_t) floor (canvas_width * new_fpu);
-       jack_nframes_t middle = (jack_nframes_t) floor( (double)start + ((double)range / 2.0f ));
-       jack_nframes_t new_leftmost = (jack_nframes_t) floor( (double)middle - ((double)new_page/2.0f));
+       nframes_t new_page = (nframes_t) floor (canvas_width * new_fpu);
+       nframes_t middle = (nframes_t) floor( (double)start + ((double)range / 2.0f ));
+       nframes_t new_leftmost = (nframes_t) floor( (double)middle - ((double)new_page/2.0f));
 
        if (new_leftmost > middle) new_leftmost = 0;
 
@@ -1168,26 +1197,26 @@ Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const
 }
 
 void 
-Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
+Editor::temporal_zoom_to_frame (bool coarser, nframes_t frame)
 {
        if (!session) return;
        
-       jack_nframes_t range_before = frame - leftmost_frame;
+       double range_before = frame - leftmost_frame;
        double new_fpu;
        
        new_fpu = frames_per_unit;
        
        if (coarser) { 
-               new_fpu *= 2.0;
-               range_before *= 2;
+               new_fpu *= 1.61803399;
+               range_before *= 1.61803399;
        } else { 
-               new_fpu = max(1.0,(new_fpu/2.0));
-               range_before /= 2;
+               new_fpu = max(1.0,(new_fpu/1.61803399));
+               range_before /= 1.61803399;
        }
 
        if (new_fpu == frames_per_unit) return;
 
-       jack_nframes_t new_leftmost = frame - range_before;
+       nframes_t new_leftmost = frame - (nframes_t)range_before;
 
        if (new_leftmost > frame) new_leftmost = 0;
 
@@ -1200,233 +1229,66 @@ Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
 }
 
 void
-Editor::select_all_in_track (bool add)
+Editor::add_location_from_selection ()
 {
-       list<Selectable *> touched;
+       string rangename;
 
-       if (!clicked_trackview) {
+       if (selection->time.empty()) {
                return;
        }
-       
-       clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
-
-       if (add) {
-               selection->add (touched);
-       } else {
-               selection->set (touched);
-       }
-}
-
-void
-Editor::select_all (bool add)
-{
-       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"));
-       if (add) {
-               selection->add (touched);
-       } else {
-               selection->set (touched);
-       }
-       commit_reversible_command ();
-}
 
-void
-Editor::invert_selection_in_track ()
-{
-       list<Selectable *> touched;
-
-       if (!clicked_trackview) {
+       if (session == 0 || clicked_trackview == 0) {
                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 (jack_nframes_t start, jack_nframes_t end, double top, double bot, bool add)
-{
-       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);
-       }
-       begin_reversible_command (_("select all within"));
-       if (add) {
-               selection->add (touched);
-       } else {
-               selection->set (touched);
-       }
-       commit_reversible_command ();
-       return !touched.empty();
-}
 
-void
-Editor::set_selection_from_punch()
-{
-       Location* location;
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
 
-       if ((location = session->locations()->auto_punch_location()) == 0)  {
-               return;
-       }
+       session->locations()->next_available_name(rangename,"selection");
+       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
 
-       set_selection_from_range (*location);
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       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::set_selection_from_loop()
+Editor::add_location_from_playhead_cursor ()
 {
-       Location* location;
-
-       if ((location = session->locations()->auto_loop_location()) == 0)  {
-               return;
-       }
-       set_selection_from_range (*location);
-}
+       string markername;
 
-void
-Editor::set_selection_from_range (Location& loc)
-{
-        if (clicked_trackview == 0) {
-               return;
-       }
+       nframes_t where = session->audible_frame();
        
-       begin_reversible_command (_("set selection from range"));
-       selection->set (0, loc.start(), loc.end());
-       commit_reversible_command ();
-}
-
-void
-Editor::select_all_selectables_using_time_selection ()
-{
-
-       list<Selectable *> touched;
-
-       if (clicked_trackview == 0) {
-               return;
-       }
-
-       if (selection->time.empty()) {
-               return;
-       }
-
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_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 ();
-
+       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);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
 }
 
 void
-Editor::select_all_selectables_using_loop()
+Editor::add_location_from_audio_region ()
 {
-       Location* location = session->locations()->auto_loop_location();
-       list<Selectable *> touched;
-
-       if (location == 0 || (location->end() - location->start() <= 1))  {
+       if (selection->regions.empty()) {
                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)
-{
-        jack_nframes_t start;
-       jack_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 ();
+       RegionView* rv = *(selection->regions.begin());
+       boost::shared_ptr<Region> region = rv->region();
+       
+       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);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
 }
 
 void
@@ -1512,28 +1374,30 @@ Editor::jump_backward_to_mark ()
        if (location) {
                session->request_locate (location->start(), session->transport_rolling());
        } else {
-               session->request_locate (0);
+               session->goto_start ();
        }
 }
 
 void
 Editor::set_mark ()
 {
-       jack_nframes_t pos;
+       nframes_t pos;
        float prefix;
        bool was_floating;
+       string markername;
 
        if (get_prefix (prefix, was_floating)) {
                pos = session->audible_frame ();
        } else {
                if (was_floating) {
-                       pos = (jack_nframes_t) floor (prefix * session->frame_rate ());
+                       pos = (nframes_t) floor (prefix * session->frame_rate ());
                } else {
-                       pos = (jack_nframes_t) floor (prefix);
+                       pos = (nframes_t) floor (prefix);
                }
        }
 
-       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
@@ -1541,9 +1405,10 @@ Editor::clear_markers ()
 {
        if (session) {
                session->begin_reversible_command (_("clear markers"));
-               session->add_undo (session->locations()->get_memento());
+                XMLNode &before = session->locations()->get_state();
                session->locations()->clear_markers ();
-               session->add_redo_no_execute (session->locations()->get_memento());
+                XMLNode &after = session->locations()->get_state();
+               session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
                session->commit_reversible_command ();
        }
 }
@@ -1553,7 +1418,7 @@ Editor::clear_ranges ()
 {
        if (session) {
                session->begin_reversible_command (_("clear ranges"));
-               session->add_undo (session->locations()->get_memento());
+                XMLNode &before = session->locations()->get_state();
                
                Location * looploc = session->locations()->auto_loop_location();
                Location * punchloc = session->locations()->auto_punch_location();
@@ -1563,7 +1428,8 @@ Editor::clear_ranges ()
                if (looploc) session->locations()->add (looploc);
                if (punchloc) session->locations()->add (punchloc);
                
-               session->add_redo_no_execute (session->locations()->get_memento());
+                XMLNode &after = session->locations()->get_state();
+               session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
                session->commit_reversible_command ();
        }
 }
@@ -1572,24 +1438,47 @@ void
 Editor::clear_locations ()
 {
        session->begin_reversible_command (_("clear locations"));
-       session->add_undo (session->locations()->get_memento());
+        XMLNode &before = session->locations()->get_state();
        session->locations()->clear ();
-       session->add_redo_no_execute (session->locations()->get_memento());
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
        session->commit_reversible_command ();
        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
-Editor::insert_region_list_drag (AudioRegion& region, int x, int y)
+Editor::insert_region_list_drag (boost::shared_ptr<AudioRegion> region, int x, int y)
 {
        double wx, wy;
        double cx, cy;
        TimeAxisView *tv;
-       jack_nframes_t where;
+       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();
@@ -1622,22 +1511,22 @@ Editor::insert_region_list_drag (AudioRegion& region, int x, int y)
        snap_to (where);
        
        begin_reversible_command (_("insert dragged region"));
-       session->add_undo (playlist->get_memento());
-       playlist->add_region (*(new AudioRegion (region)), where, 1.0);
-       session->add_redo_no_execute (playlist->get_memento());
+        XMLNode &before = playlist->get_state();
+       playlist->add_region (RegionFactory::create (region), where, 1.0);
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
        commit_reversible_command ();
 }
 
 void
 Editor::insert_region_list_selection (float times)
 {
-       AudioTimeAxisView *tv = 0;
-       Playlist *playlist;
+       RouteTimeAxisView *tv = 0;
+       boost::shared_ptr<Playlist> playlist;
 
        if (clicked_audio_trackview != 0) {
                tv = clicked_audio_trackview;
        } else if (!selection->tracks.empty()) {
-               if ((tv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front())) == 0) {
+               if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
                        return;
                }
        } else {
@@ -1654,16 +1543,23 @@ Editor::insert_region_list_selection (float times)
                return;
        }
        
-       TreeModel::iterator i = region_list_display.get_selection()->get_selected();
-       Region* region = (*i)[region_list_columns.region];
+       TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
 
-       begin_reversible_command (_("insert region"));
-       session->add_undo (playlist->get_memento());
-       playlist->add_region (*(createRegion (*region)), edit_cursor->current_frame, times);
-       session->add_redo_no_execute (playlist->get_memento());
-       commit_reversible_command ();
-}
+       /* 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)), edit_cursor->current_frame, times);
+               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+               commit_reversible_command ();
+       } 
+}
 
 /* BUILT-IN EFFECTS */
 
@@ -1682,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)
 {
@@ -1689,9 +1609,9 @@ Editor::toggle_playback (bool with_abort)
                return;
        }
 
-       switch (session->slave_source()) {
-       case Session::None:
-       case Session::JACK:
+       switch (Config->get_slave_source()) {
+       case None:
+       case JACK:
                break;
        default:
                /* transport controlled by the master */
@@ -1705,8 +1625,8 @@ Editor::toggle_playback (bool with_abort)
        
        if (session->transport_rolling()) {
                session->request_stop (with_abort);
-               if (session->get_auto_loop()) {
-                       session->request_auto_loop (false);
+               if (session->get_play_loop()) {
+                       session->request_play_loop (false);
                }
        } else {
                session->request_transport_speed (1.0f);
@@ -1716,7 +1636,13 @@ Editor::toggle_playback (bool with_abort)
 void
 Editor::play_from_start ()
 {
-       session->request_locate (0, true);
+       session->request_locate (session->current_start_frame(), true);
+}
+
+void
+Editor::play_from_edit_cursor ()
+{
+       session->request_locate (edit_cursor->current_frame, true);
 }
 
 void
@@ -1732,27 +1658,27 @@ Editor::play_selection ()
 void
 Editor::play_selected_region ()
 {
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView *rv = *(selection->audio_regions.begin());
+       if (!selection->regions.empty()) {
+               RegionView *rv = *(selection->regions.begin());
 
-               session->request_bounded_roll (rv->region.position(), rv->region.last_frame()); 
+               session->request_bounded_roll (rv->region()->position(), rv->region()->last_frame());   
        }
 }
 
 void
 Editor::loop_selected_region ()
 {
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView *rv = *(selection->audio_regions.begin());
+       if (!selection->regions.empty()) {
+               RegionView *rv = *(selection->regions.begin());
                Location* tll;
 
                if ((tll = transport_loop_location()) != 0)  {
 
-                       tll->set (rv->region.position(), rv->region.last_frame());
+                       tll->set (rv->region()->position(), rv->region()->last_frame());
                        
                        // enable looping, reposition and start rolling
 
-                       session->request_auto_loop (true);
+                       session->request_play_loop (true);
                        session->request_locate (tll->start(), false);
                        session->request_transport_speed (1.0f);
                }
@@ -1782,55 +1708,33 @@ Editor::loop_location (Location& location)
                tll->set (location.start(), location.end());
 
                // enable looping, reposition and start rolling
-               session->request_auto_loop (true);
+               session->request_play_loop (true);
                session->request_locate (tll->start(), true);
        }
 }
 
-void 
-Editor::toggle_region_mute ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region.set_muted (!clicked_regionview->region.muted());
-       } else if (!selection->audio_regions.empty()) {
-               bool yn = ! (*selection->audio_regions.begin())->region.muted();
-               selection->foreach_audio_region (&AudioRegion::set_muted, yn);
-       }
-}
-
-void
-Editor::toggle_region_opaque ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region.set_opaque (!clicked_regionview->region.opaque());
-       } else if (!selection->audio_regions.empty()) {
-               bool yn = ! (*selection->audio_regions.begin())->region.opaque();
-               selection->foreach_audio_region (&Region::set_opaque, yn);
-       }
-}
-
 void
 Editor::raise_region ()
 {
-       selection->foreach_audio_region (&Region::raise);
+       selection->foreach_region (&Region::raise);
 }
 
 void
 Editor::raise_region_to_top ()
 {
-       selection->foreach_audio_region (&Region::raise_to_top);
+       selection->foreach_region (&Region::raise_to_top);
 }
 
 void
 Editor::lower_region ()
 {
-       selection->foreach_audio_region (&Region::lower);
+       selection->foreach_region (&Region::lower);
 }
 
 void
 Editor::lower_region_to_bottom ()
 {
-       selection->foreach_audio_region (&Region::lower_to_bottom);
+       selection->foreach_region (&Region::lower_to_bottom);
 }
 
 void
@@ -1851,11 +1755,14 @@ Editor::rename_region ()
        Button ok_button (_("OK"));
        Button cancel_button (_("Cancel"));
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                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);
@@ -1882,7 +1789,7 @@ Editor::rename_region ()
        Main::run ();
 
        if (region_renamed) {
-               (*selection->audio_regions.begin())->region.set_name (entry.get_text());
+               (*selection->regions.begin())->region()->set_name (entry.get_text());
                redisplay_regions ();
        }
 }
@@ -1896,7 +1803,7 @@ Editor::rename_region_finished (bool status)
 }
 
 void
-Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
+Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Route& route)
 {
        if (session->is_auditioning()) {
                session->cancel_audition ();
@@ -1909,7 +1816,7 @@ Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
 
        route.set_solo (true, this);
        
-       session->request_bounded_roll (region.position(), region.position() + region.length());
+       session->request_bounded_roll (region->position(), region->position() + region->length());
        
        /* XXX how to unset the solo state ? */
 }
@@ -1917,14 +1824,14 @@ Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
 void
 Editor::audition_selected_region ()
 {
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView* rv = *(selection->audio_regions.begin());
-               session->audition_region (rv->region);
+       if (!selection->regions.empty()) {
+               RegionView* rv = *(selection->regions.begin());
+               session->audition_region (rv->region());
        }
 }
 
 void
-Editor::audition_playlist_region_standalone (AudioRegion& region)
+Editor::audition_playlist_region_standalone (boost::shared_ptr<Region> region)
 {
        session->audition_region (region);
 }
@@ -1957,521 +1864,28 @@ Editor::interthread_cancel_clicked ()
        }
 }
 
-void *
-Editor::_import_thread (void *arg)
-{
-       PBD::ThreadCreated (pthread_self(), X_("Import"));
-
-       Editor *ed = (Editor *) arg;
-       return ed->import_thread ();
-}
-
-void *
-Editor::import_thread ()
-{
-       session->import_audiofile (import_status);
-       return 0;
-}
-
-gint
-Editor::import_progress_timeout (void *arg)
-{
-       interthread_progress_label.set_text (import_status.doing_what);
-
-       if (import_status.freeze) {
-               interthread_cancel_button.set_sensitive(false);
-       } else {
-               interthread_cancel_button.set_sensitive(true);
-       }
-
-       if (import_status.doing_what == "building peak files") {
-               interthread_progress_bar.pulse ();
-               return FALSE;
-       } else {
-               interthread_progress_bar.set_fraction (import_status.progress/100);
-       }
-
-       return !(import_status.done || import_status.cancel);
-}
-
 void
-Editor::import_audio (bool as_tracks)
+Editor::region_from_selection ()
 {
-       if (session == 0) {
-               warning << _("You can't import an audiofile until you have a session loaded.") << endmsg;
+       if (clicked_trackview == 0) {
                return;
        }
 
-       string str;
-
-       if (as_tracks) {
-               str =_("Import selected as tracks");
-       } else {
-               str = _("Import selected to region list");
+       if (selection->time.empty()) {
+               return;
        }
 
-       SoundFileOmega sfdb (str);
-       sfdb.Imported.connect (bind (mem_fun (*this, &Editor::do_import), as_tracks));
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
 
-       sfdb.run();
-}
+       nframes_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;
+               boost::shared_ptr<Playlist> pl;
 
-void
-Editor::catch_new_audio_region (AudioRegion* ar)
-{
-       last_audio_region = ar;
-}
-
-void
-Editor::do_import (vector<string> paths, bool split, bool as_tracks)
-{
-       sigc::connection c;
-       
-       /* SFDB sets "multichan" to true to indicate "split channels"
-          so reverse the setting to match the way libardour
-          interprets it.
-       */
-       
-       import_status.multichan = !split;
-
-       if (interthread_progress_window == 0) {
-               build_interthread_progress_window ();
-       }
-       
-       interthread_progress_window->set_title (_("ardour: audio import in progress"));
-       interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
-       interthread_progress_window->show_all ();
-       interthread_progress_bar.set_fraction (0.0f);
-       interthread_cancel_label.set_text (_("Cancel Import"));
-       current_interthread_info = &import_status;
-
-       c = session->AudioRegionAdded.connect (mem_fun(*this, &Editor::catch_new_audio_region));
-
-       for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i ) {
-
-               interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), (*i)));
-       
-               import_status.pathname = (*i);
-               import_status.done = false;
-               import_status.cancel = false;
-               import_status.freeze = false;
-               import_status.done = 0.0;
-               
-               interthread_progress_connection = 
-                 Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
-               
-               last_audio_region = 0;
-               
-               pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
-               pthread_detach (import_status.thread);
-               
-               while (!(import_status.done || import_status.cancel)) {
-                       gtk_main_iteration ();
-               }
-               
-               import_status.done = true;
-               interthread_progress_connection.disconnect ();
-
-               if (as_tracks && last_audio_region != 0) {
-                       uint32_t channels = last_audio_region->n_channels();
-
-                       AudioTrack* at = session->new_audio_track (channels, channels);
-                       AudioRegion* copy = new AudioRegion (*last_audio_region);
-                       at->disk_stream().playlist()->add_region (*copy, 0);
-               }
-       }
-
-       c.disconnect ();
-       interthread_progress_window->hide_all ();
-}
-
-int
-Editor::reject_because_rate_differs (const string & path, SF_INFO& finfo, const string & action, bool multiple_pending)
-{
-       if (!session) {
-               return 1;
-       }
-
-       if (finfo.samplerate != (int) session->frame_rate()) {
-               vector<string> choices;
-
-               choices.push_back (string_compose (_("%1 it anyway"), action));
-
-               if (multiple_pending) {
-                       /* XXX assumptions about sentence structure
-                          here for translators. Sorry.
-                       */
-                       choices.push_back (string_compose (_("Don't %1 it"), action));
-                       choices.push_back (string_compose (_("%1 all without questions"), action));
-                       choices.push_back (_("Cancel entire import"));
-               } else {
-                       choices.push_back (_("Cancel"));
-               }
-
-               Gtkmm2ext::Choice rate_choice (
-                       string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
-                       choices);
-
-               rate_choice.chosen.connect (ptr_fun (Main::quit));
-               rate_choice.show_all ();
-
-               Main::run ();
-
-               switch (rate_choice.get_choice()) {
-               case 0: /* do it anyway */
-                       return 0;
-               case 1: /* don't import this one */
-                       return 1;
-               case 2: /* do the rest without asking */
-                       return -1;
-               case 3: /* stop a multi-file import */
-               default:
-                       return -2;
-               }
-       }
-
-       return 0;
-}
-
-void 
-Editor::embed_audio ()
-{
-       if (session == 0) {
-               warning << _("You can't embed an audiofile until you have a session loaded.") << endmsg;
-               return;
-       }
-
-       SoundFileOmega sfdb (_("Add to External Region list"));
-       sfdb.Embedded.connect (mem_fun (*this, &Editor::do_embed_sndfiles));
-
-       sfdb.run ();
-}
-
-void
-Editor::do_embed_sndfiles (vector<string> paths, bool split)
-{
-       bool multiple_files = paths.size() > 1;
-       bool check_sample_rate = true;
-
-       for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i) {
-               embed_sndfile (*i, split, multiple_files, check_sample_rate);
-       }
-
-       session->save_state ("");
-}
-
-void
-Editor::embed_sndfile (string path, bool split, bool multiple_files, bool& check_sample_rate)
-{
-       SndFileSource *source = 0; /* keep g++ quiet */
-       AudioRegion::SourceList sources;
-       string idspec;
-       string linked_path;
-       SNDFILE *sf;
-       SF_INFO finfo;
-
-       /* lets see if we can link it into the session */
-       
-       linked_path = session->sound_dir();
-       linked_path += PBD::basename (path);
-
-       if (link (path.c_str(), linked_path.c_str()) == 0) {
-
-               /* there are many reasons why link(2) might have failed.
-                  but if it succeeds, we now have a link in the
-                  session sound dir that will protect against
-                  unlinking of the original path. nice.
-               */
-
-               path = linked_path;
-       }
-
-       memset (&finfo, 0, sizeof(finfo));
-
-       /* note that we temporarily truncated _id at the colon */
-       
-       if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
-               char errbuf[256];
-               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-               error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), selection, errbuf) << endmsg;
-               return;
-       }
-       sf_close (sf);
-       sf = 0;
-       
-       if (check_sample_rate) {
-               switch (reject_because_rate_differs (path, finfo, "Embed", multiple_files)) {
-               case 0:
-                       break;
-               case 1:
-                       return;
-               case -1:
-                       check_sample_rate = false;
-                       break;
-                       
-               case -2:
-               default:
-                       return;
-               }
-       }
-
-       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
-       ARDOUR_UI::instance()->flush_pending ();
-
-       /* make the proper number of channels in the region */
-
-       for (int n=0; n < finfo.channels; ++n)
-       {
-               idspec = path;
-               idspec += string_compose(":%1", n);
-               
-               try {
-                       source = new SndFileSource (idspec.c_str());
-                       sources.push_back(source);
-               } 
-
-               catch (failed_constructor& err) {
-                       error << string_compose(_("could not open %1"), path) << endmsg;
-                       goto out;
-               }
-
-               ARDOUR_UI::instance()->flush_pending ();
-       }
-
-       if (sources.size() > 0) {
-
-               string region_name = PBD::basename_nosuffix (path);
-               region_name += "-0";
-
-               /* The created region isn't dropped.  It emits a signal
-                  that is picked up by the session. 
-               */
-
-               new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
-                                Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
-               
-               /* make sure we can see it in the list */
-
-                /* its the second node, always */
-
-               // GTK2FIX ?? is it still always the 2nd node
-
-               TreeModel::Path path ("2");
-               region_list_display.expand_row (path, true);
-
-               ARDOUR_UI::instance()->flush_pending ();
-       }
-
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
-}
-
-void
-Editor::insert_sndfile (bool as_tracks)
-{
-//     SoundFileSelector& sfdb (ARDOUR_UI::instance()->get_sfdb_window());
-       sigc::connection c;
-       string str;
-
-       if (as_tracks) {
-
-//             c = sfdb.Action.connect (mem_fun(*this, &Editor::insert_paths_as_new_tracks));
-               str = _("Insert selected as new tracks");
-
-       } else {
-
-               jack_nframes_t pos;
-
-               if (clicked_audio_trackview == 0) {
-                       return;
-               }
-
-               if (ensure_cursor (&pos)) {
-                       return;
-               }
-
-//             c = sfdb.Action.connect (bind (mem_fun(*this, &Editor::do_insert_sndfile), pos));
-               str = _("Insert selected");
-       }
-
-//     sfdb.run (str, false);
-//     c.disconnect ();
-}
-
-void
-Editor::insert_paths_as_new_tracks (vector<string> paths, bool split)
-{
-       SNDFILE *sf;
-       SF_INFO finfo;
-       bool multiple_files;
-       bool check_sample_rate = true;
-
-       multiple_files = paths.size() > 1;      
-
-       for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
-               
-               memset (&finfo, 0, sizeof(finfo));
-               
-               if ((sf = sf_open ((*p).c_str(), SFM_READ, &finfo)) == 0) {
-                       char errbuf[256];
-                       sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-                       error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), (*p), errbuf) << endmsg;
-                       continue;
-               }
-               
-               sf_close (sf);
-               sf = 0;
-               
-               /* add a new track */
-               
-               if (check_sample_rate) {
-                       switch (reject_because_rate_differs (*p, finfo, "Insert", multiple_files)) {
-                       case 0:
-                               break;
-                       case 1:
-                               continue;
-                       case -1:
-                               check_sample_rate = false;
-                               break;
-                               
-                       case -2:
-                               return;
-                       }
-               }
-               
-               uint32_t input_chan = finfo.channels;
-               uint32_t output_chan;
-               
-               if (session->get_output_auto_connect() & Session::AutoConnectMaster) {
-                       output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
-               } else {
-                       output_chan = input_chan;
-               }
-               
-               (void) session->new_audio_track (input_chan, output_chan);
-
-
-               /* get the last (most recently added) track view */
-       
-               AudioTimeAxisView* tv;
-       
-               if ((tv = dynamic_cast<AudioTimeAxisView*>(track_views.back())) == 0) {
-                       fatal << _("programming error: ")
-                             << X_("last trackview after new_audio_track is not an audio track!")
-                             << endmsg;
-                       /*NOTREACHED*/
-               }
-               
-               jack_nframes_t pos = 0;
-               insert_sndfile_into (*p, true, tv, pos, false);
-       }
-}
-
-void
-Editor::do_insert_sndfile (vector<string> paths, bool split, jack_nframes_t pos)
-{
-       for (vector<string>::iterator x = paths.begin(); x != paths.end(); ++x) {
-               insert_sndfile_into (*x, !split, clicked_audio_trackview, pos);
-       }
-}
-
-void
-Editor::insert_sndfile_into (const string & path, bool multi, AudioTimeAxisView* tv, jack_nframes_t& pos, bool prompt)
-{
-       SndFileSource *source = 0; /* keep g++ quiet */
-       AudioRegion::SourceList sources;
-       string idspec;
-       SNDFILE *sf;
-       SF_INFO finfo;
-
-       memset (&finfo, 0, sizeof(finfo));
-
-       /* note that we temporarily truncated _id at the colon */
-       
-       if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
-               char errbuf[256];
-               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-               error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), path, errbuf) << endmsg;
-               return;
-       }
-       sf_close (sf);
-       sf = 0;
-       
-       if (prompt && (reject_because_rate_differs (path, finfo, "Insert", false) != 0)) {
-               return;
-       }
-
-       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
-       ARDOUR_UI::instance()->flush_pending ();
-
-       /* make the proper number of channels in the region */
-
-       for (int n=0; n < finfo.channels; ++n)
-       {
-               idspec = path;
-               idspec += string_compose(":%1", n);
-
-               try {
-                       source = new SndFileSource (idspec.c_str());
-                       sources.push_back(source);
-               } 
-
-               catch (failed_constructor& err) {
-                       error << string_compose(_("could not open %1"), path) << endmsg;
-                       goto out;
-               }
-
-               ARDOUR_UI::instance()->flush_pending ();
-       }
-
-       if (sources.size() > 0) {
-
-               string region_name = region_name_from_path (PBD::basename (path));
-               
-               AudioRegion *region = new AudioRegion (sources, 0, sources[0]->length(), region_name, 
-                                                      0, /* irrelevant these days */
-                                                      Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
-
-               begin_reversible_command (_("insert sndfile"));
-               session->add_undo (tv->playlist()->get_memento());
-               tv->playlist()->add_region (*region, pos);
-               session->add_redo_no_execute (tv->playlist()->get_memento());
-               commit_reversible_command ();
-               
-               pos += sources[0]->length();
-
-               ARDOUR_UI::instance()->flush_pending ();
-       }
-
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
-       return;
-}
-
-void
-Editor::region_from_selection ()
-{
-       if (clicked_trackview == 0) {
-               return;
-       }
-
-       if (selection->time.empty()) {
-               return;
-       }
-
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
-
-       jack_nframes_t selection_cnt = end - start + 1;
-       
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               AudioRegion *region;
-               AudioRegion *current;
-               Region* current_r;
-               Playlist *pl;
-
-               jack_nframes_t internal_start;
+               nframes_t internal_start;
                string new_name;
 
                if ((pl = (*i)->playlist()) == 0) {
@@ -2482,30 +1896,34 @@ Editor::region_from_selection ()
                        continue;
                }
 
-               if ((current = dynamic_cast<AudioRegion*> (current_r)) != 0) {
+               current = boost::dynamic_pointer_cast<AudioRegion> (current_r);
+               // FIXME: audio only
+               if (current != 0) {
                        internal_start = start - current->position();
                        session->region_name (new_name, current->name(), true);
-                       region = new AudioRegion (*current, internal_start, selection_cnt, new_name);
+                       boost::shared_ptr<Region> region (RegionFactory::create (current, internal_start, selection_cnt, new_name));
                }
        }
 }      
 
 void
-Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
+Editor::create_region_from_selection (vector<boost::shared_ptr<AudioRegion> >& new_regions)
 {
        if (selection->time.empty() || selection->tracks.empty()) {
                return;
        }
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
+       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) {
 
-               AudioRegion* current;
-               Region* current_r;
-               Playlist* playlist;
-               jack_nframes_t internal_start;
+               boost::shared_ptr<AudioRegion> current;
+               boost::shared_ptr<Region> current_r;
+               boost::shared_ptr<Playlist> playlist;
+               nframes_t internal_start;
                string new_name;
 
                if ((playlist = (*i)->playlist()) == 0) {
@@ -2516,29 +1934,36 @@ Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
                        continue;
                }
 
-               if ((current = dynamic_cast<AudioRegion*>(current_r)) == 0) {
+               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 (new AudioRegion (*current, internal_start, end - start + 1, new_name));
+               new_regions.push_back (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (current, internal_start, end - start + 1, new_name)));
        }
 }
 
 void
 Editor::split_multichannel_region ()
 {
-       vector<AudioRegion*> v;
-
-       if (!clicked_regionview || clicked_regionview->region.n_channels() < 2) {
+       if (selection->regions.empty()) {
                return;
        }
 
-       clicked_regionview->region.separate_by_channel (*session, v);
+       vector<boost::shared_ptr<AudioRegion> > v;
 
-       /* nothing else to do, really */
+       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;
+               }
+
+               (arv)->audio_region()->separate_by_channel (*session, v);
+       }
 }
 
 void
@@ -2557,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;
@@ -2566,23 +1993,32 @@ 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) {
                                                begin_reversible_command (_("separate"));
                                                doing_undo = true;
                                        }
-                                       if (doing_undo) session->add_undo ((playlist)->get_memento());
+                                        XMLNode *before;
+                                       if (doing_undo) 
+                                            before = &(playlist->get_state());
                        
                                        /* XXX need to consider musical time selections here at some point */
 
                                        double speed = atv->get_diskstream()->speed();
 
                                        for (list<AudioRange>::iterator t = selection->time.begin(); t != selection->time.end(); ++t) {
-                                               playlist->partition ((jack_nframes_t)((*t).start * speed), (jack_nframes_t)((*t).end * speed), true);
+                                               playlist->partition ((nframes_t)((*t).start * speed), (nframes_t)((*t).end * speed), true);
                                        }
 
-                                       if (doing_undo) session->add_redo_no_execute (playlist->get_memento());
+                                       if (doing_undo) 
+                                            session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
                                }
                        }
                }
@@ -2592,82 +2028,143 @@ Editor::separate_region_from_selection ()
 }
 
 void
-Editor::crop_region_to_selection ()
+Editor::separate_regions_using_location (Location& loc)
 {
-       if (selection->time.empty()) {
+       bool doing_undo = false;
+
+       if (loc.is_mark()) {
                return;
        }
 
-       vector<Playlist*> playlists;
-       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 
+          (currently set for all tracks)
+       */
 
-       if (clicked_trackview != 0) {
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {    
+       //for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
-               if ((playlist = clicked_trackview->playlist()) == 0) {
-                       return;
+               AudioTimeAxisView* atv;
+
+               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) {
+                                        XMLNode *before;
+                                       if (!doing_undo) {
+                                               begin_reversible_command (_("separate"));
+                                               doing_undo = true;
+                                       }
+                                       if (doing_undo) 
+                                            before = &(playlist->get_state());
+                                            
+                       
+                                       /* XXX need to consider musical time selections here at some point */
+
+                                       double speed = atv->get_diskstream()->speed();
+
+
+                                       playlist->partition ((nframes_t)(loc.start() * speed), (nframes_t)(loc.end() * speed), true);
+                                       if (doing_undo) 
+                                            session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
+                               }
+                       }
                }
+       }
 
-               playlists.push_back (playlist);
+       if (doing_undo) commit_reversible_command ();
+}
 
-       } else {
-               
-               for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+void
+Editor::crop_region_to_selection ()
+{
+       if (selection->time.empty() || selection->tracks.empty()) {
+               return;
+       }
+
+       vector<boost::shared_ptr<Playlist> > playlists;
+       boost::shared_ptr<Playlist> playlist;
 
-                       AudioTimeAxisView* atv;
+       sort_track_selection ();
+       
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               
+               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()) {
-
-               jack_nframes_t start;
-               jack_nframes_t end;
-               jack_nframes_t cnt;
-
-               begin_reversible_command (_("trim to selection"));
-
-               for (vector<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-                       
-                       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;
-
-                       session->add_undo ((*i)->get_memento());
-                       region->trim_to (start, cnt, this);
-                       session->add_redo_no_execute ((*i)->get_memento());
+       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
 Editor::region_fill_track ()
 {
-       jack_nframes_t end;
+       nframes_t end;
 
-       if (!session || selection->audio_regions.empty()) {
+       if (!session || selection->regions.empty()) {
                return;
        }
 
@@ -2675,24 +2172,30 @@ Editor::region_fill_track ()
 
        begin_reversible_command (_("region fill"));
 
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+
+               boost::shared_ptr<Region> region ((*i)->region());
+               
+               // FIXME
+               boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
+               if (!ar)
+                       continue;
 
-               AudioRegion& region ((*i)->region);
-               Playlist* pl = region.playlist();
+               boost::shared_ptr<Playlist> pl = region->playlist();
 
-               if (end <= region.last_frame()) {
+               if (end <= region->last_frame()) {
                        return;
                }
 
-               double times = (double) (end - region.last_frame()) / (double) region.length();
+               double times = (double) (end - region->last_frame()) / (double) region->length();
 
                if (times == 0) {
                        return;
                }
 
-               session->add_undo (pl->get_memento());
-               pl->add_region (*(new AudioRegion (region)), region.last_frame(), times);
-               session->add_redo_no_execute (pl->get_memento());
+                XMLNode &before = pl->get_state();
+               pl->add_region (RegionFactory::create (ar), ar->last_frame(), times);
+               session->add_command (new MementoCommand<Playlist>(*pl, &before, &pl->get_state()));
        }
 
        commit_reversible_command ();
@@ -2709,7 +2212,6 @@ Editor::region_fill_selection ()
                return;
        }
 
-       Region *region;
 
        Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
 
@@ -2718,18 +2220,18 @@ Editor::region_fill_selection ()
        }
 
        TreeModel::iterator i = region_list_display.get_selection()->get_selected();
-       region = (*i)[region_list_columns.region];
+       boost::shared_ptr<Region> region = (*i)[region_list_columns.region];
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
+       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;
        }
 
-       jack_nframes_t selection_length = end - start;
+       nframes_t selection_length = end - start;
        float times = (float)selection_length / region->length();
        
        begin_reversible_command (_("fill selection"));
@@ -2740,26 +2242,27 @@ Editor::region_fill_selection ()
                        continue;
                }               
                
-               session->add_undo (playlist->get_memento());
-               playlist->add_region (*(createRegion (*region)), start, times);
-               session->add_redo_no_execute (playlist->get_memento());
+                XMLNode &before = playlist->get_state();
+               playlist->add_region (RegionFactory::create (region), start, times);
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
        }
        
        commit_reversible_command ();                   
 }
 
 void
-Editor::set_a_regions_sync_position (Region& region, jack_nframes_t position)
+Editor::set_a_regions_sync_position (boost::shared_ptr<Region> region, nframes_t position)
 {
 
-       if (!region.covers (position)) {
+       if (!region->covers (position)) {
          error << _("Programming error. that region doesn't cover that position") << __FILE__ << " +" << __LINE__ << endmsg;
                return;
        }
        begin_reversible_command (_("set region sync position"));
-       session->add_undo (region.playlist()->get_memento());
-       region.set_sync_position (position);
-       session->add_redo_no_execute (region.playlist()->get_memento());
+        XMLNode &before = region->playlist()->get_state();
+       region->set_sync_position (position);
+        XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
        commit_reversible_command ();
 }
 
@@ -2770,16 +2273,17 @@ Editor::set_region_sync_from_edit_cursor ()
                return;
        }
 
-       if (!clicked_regionview->region.covers (edit_cursor->current_frame)) {
+       if (!clicked_regionview->region()->covers (edit_cursor->current_frame)) {
                error << _("Place the edit cursor at the desired sync point") << endmsg;
                return;
        }
 
-       Region& region (clicked_regionview->region);
+       boost::shared_ptr<Region> region (clicked_regionview->region());
        begin_reversible_command (_("set sync from edit cursor"));
-       session->add_undo (region.playlist()->get_memento());
-       region.set_sync_position (edit_cursor->current_frame);
-       session->add_redo_no_execute (region.playlist()->get_memento());
+        XMLNode &before = region->playlist()->get_state();
+       region->set_sync_position (edit_cursor->current_frame);
+        XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
        commit_reversible_command ();
 }
 
@@ -2787,11 +2291,12 @@ void
 Editor::remove_region_sync ()
 {
        if (clicked_regionview) {
-               Region& region (clicked_regionview->region);
+               boost::shared_ptr<Region> region (clicked_regionview->region());
                begin_reversible_command (_("remove sync"));
-               session->add_undo (region.playlist()->get_memento());
-               region.clear_sync_position ();
-               session->add_redo_no_execute (region.playlist()->get_memento());
+                XMLNode &before = region->playlist()->get_state();
+               region->clear_sync_position ();
+                XMLNode &after = region->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
                commit_reversible_command ();
        }
 }
@@ -2799,14 +2304,15 @@ Editor::remove_region_sync ()
 void
 Editor::naturalize ()
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
        begin_reversible_command (_("naturalize"));
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               session->add_undo ((*i)->region.get_memento());
-               (*i)->region.move_to_natural_position (this);
-               session->add_redo_no_execute ((*i)->region.get_memento());
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                XMLNode &before = (*i)->region()->get_state();
+               (*i)->region()->move_to_natural_position (this);
+                XMLNode &after = (*i)->region()->get_state();
+               session->add_command (new MementoCommand<Region>(*((*i)->region().get()), &before, &after));
        }
        commit_reversible_command ();
 }
@@ -2825,36 +2331,36 @@ Editor::align_relative (RegionPoint what)
 
 struct RegionSortByTime {
     bool operator() (const AudioRegionView* a, const AudioRegionView* b) {
-           return a->region.position() < b->region.position();
+           return a->region()->position() < b->region()->position();
     }
 };
 
 void
-Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
+Editor::align_selection_relative (RegionPoint point, nframes_t position)
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
-       jack_nframes_t distance;
-       jack_nframes_t pos = 0;
+       nframes_t distance;
+       nframes_t pos = 0;
        int dir;
 
-       list<AudioRegionView*> sorted;
-       selection->audio_regions.by_position (sorted);
-       Region& r ((*sorted.begin())->region);
+       list<RegionView*> sorted;
+       selection->regions.by_position (sorted);
+       boost::shared_ptr<Region> r ((*sorted.begin())->region());
 
        switch (point) {
        case Start:
-               pos = r.first_frame ();
+               pos = r->first_frame ();
                break;
 
        case End:
-               pos = r.last_frame();
+               pos = r->last_frame();
                break;
 
        case SyncPoint:
-               pos = r.adjust_to_sync (r.first_frame());
+               pos = r->adjust_to_sync (r->first_frame());
                break;  
        }
 
@@ -2868,19 +2374,20 @@ Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
 
        begin_reversible_command (_("align selection (relative)"));
 
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
 
-               Region& region ((*i)->region);
+               boost::shared_ptr<Region> region ((*i)->region());
 
-               session->add_undo (region.playlist()->get_memento());
+                XMLNode &before = region->playlist()->get_state();
                
                if (dir > 0) {
-                       region.set_position (region.position() + distance, this);
+                       region->set_position (region->position() + distance, this);
                } else {
-                       region.set_position (region.position() - distance, this);
+                       region->set_position (region->position() - distance, this);
                }
 
-               session->add_redo_no_execute (region.playlist()->get_memento());
+                XMLNode &after = region->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
 
        }
 
@@ -2888,23 +2395,23 @@ Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
 }
 
 void
-Editor::align_selection (RegionPoint point, jack_nframes_t position)
+Editor::align_selection (RegionPoint point, nframes_t position)
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
        begin_reversible_command (_("align selection"));
 
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               align_region_internal ((*i)->region, point, position);
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               align_region_internal ((*i)->region(), point, position);
        }
 
        commit_reversible_command ();
 }
 
 void
-Editor::align_region (Region& region, RegionPoint point, jack_nframes_t position)
+Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, nframes_t position)
 {
        begin_reversible_command (_("align region"));
        align_region_internal (region, point, position);
@@ -2912,27 +2419,28 @@ Editor::align_region (Region& region, RegionPoint point, jack_nframes_t position
 }
 
 void
-Editor::align_region_internal (Region& region, RegionPoint point, jack_nframes_t position)
+Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, nframes_t position)
 {
-       session->add_undo (region.playlist()->get_memento());
+       XMLNode &before = region->playlist()->get_state();
 
        switch (point) {
        case SyncPoint:
-               region.set_position (region.adjust_to_sync (position), this);
+               region->set_position (region->adjust_to_sync (position), this);
                break;
 
        case End:
-               if (position > region.length()) {
-                       region.set_position (position - region.length(), this);
+               if (position > region->length()) {
+                       region->set_position (position - region->length(), this);
                }
                break;
 
        case Start:
-               region.set_position (position, this);
+               region->set_position (position, this);
                break;
        }
 
-       session->add_redo_no_execute (region.playlist()->get_memento());
+       XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
 }      
 
 void
@@ -2942,7 +2450,7 @@ Editor::trim_region_to_edit_cursor ()
                return;
        }
 
-       Region& region (clicked_regionview->region);
+       boost::shared_ptr<Region> region (clicked_regionview->region());
 
        float speed = 1.0f;
        AudioTimeAxisView *atav;
@@ -2954,9 +2462,10 @@ Editor::trim_region_to_edit_cursor ()
        }
 
        begin_reversible_command (_("trim to edit"));
-       session->add_undo (region.playlist()->get_memento());
-       region.trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-       session->add_redo_no_execute (region.playlist()->get_memento());
+        XMLNode &before = region->playlist()->get_state();
+       region->trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
+        XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
        commit_reversible_command ();
 }
 
@@ -2967,7 +2476,7 @@ Editor::trim_region_from_edit_cursor ()
                return;
        }
 
-       Region& region (clicked_regionview->region);
+       boost::shared_ptr<Region> region (clicked_regionview->region());
 
        float speed = 1.0f;
        AudioTimeAxisView *atav;
@@ -2979,9 +2488,10 @@ Editor::trim_region_from_edit_cursor ()
        }
 
        begin_reversible_command (_("trim to edit"));
-       session->add_undo (region.playlist()->get_memento());
-       region.trim_front ( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-       session->add_redo_no_execute (region.playlist()->get_memento());
+        XMLNode &before = region->playlist()->get_state();
+       region->trim_front ( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
+        XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
        commit_reversible_command ();
 }
 
@@ -3028,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);
@@ -3043,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));
 
@@ -3065,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;
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
-       jack_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;
 
@@ -3081,7 +2599,7 @@ Editor::bounce_range_selection ()
                        continue;
                }
                
-               Playlist* playlist;
+               boost::shared_ptr<Playlist> playlist;
                
                if ((playlist = atv->playlist()) == 0) {
                        return;
@@ -3092,15 +2610,14 @@ Editor::bounce_range_selection ()
                itt.done = false;
                itt.cancel = false;
                itt.progress = false;
-               
-               session->add_undo (playlist->get_memento());
+
+                XMLNode &before = playlist->get_state();
                atv->audio_track()->bounce_range (start, cnt, itt);
-               session->add_redo_no_execute (playlist->get_memento());
+                XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist> (*playlist, &before, &after));
        }
        
        commit_reversible_command ();
-       
-       delete views;
 }
 
 void
@@ -3138,16 +2655,16 @@ Editor::cut_copy (CutCopyOp op)
 
        switch (current_mouse_mode()) {
        case MouseObject: 
-               if (!selection->audio_regions.empty() || !selection->points.empty()) {
+               if (!selection->regions.empty() || !selection->points.empty()) {
 
                        begin_reversible_command (opname + _(" objects"));
 
-                       if (!selection->audio_regions.empty()) {
+                       if (!selection->regions.empty()) {
                                
                                cut_copy_regions (op);
                                
                                if (op == Cut) {
-                                       selection->clear_audio_regions ();
+                                       selection->clear_regions ();
                                }
                        }
 
@@ -3195,83 +2712,155 @@ Editor::cut_copy_points (CutCopyOp op)
        }
 }
 
+struct PlaylistState {
+    boost::shared_ptr<Playlist> playlist;
+    XMLNode*  before;
+};
+
+struct lt_playlist {
+    bool operator () (const PlaylistState& a, const PlaylistState& b) {
+           return a.playlist < b.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;
-       jack_nframes_t first_position = max_frames;
-       set<Playlist*> freezelist;
-       pair<set<Playlist*>::iterator,bool> insert_result;
+       /* 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 (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ++x) {
-               first_position = min ((*x)->region.position(), first_position);
+       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) {
-                               insert_result = freezelist.insert (pl);
+
+                               PlaylistState before;
+                               before.playlist = pl;
+                               before.before = &pl->get_state();
+                               
+                               insert_result = freezelist.insert (before);
+                               
                                if (insert_result.second) {
                                        pl->freeze ();
-                                       session->add_undo (pl->get_memento());
                                }
                        }
                }
-       }
 
-       for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ) {
+               TimeAxisView* tv = &(*x)->get_trackview();
+               vector<PlaylistMapping>::iterator z;
 
-               AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
-               AudioPlaylist* npl;
-               AudioRegionSelection::iterator tmp;
+               for (z = pmap.begin(); z != pmap.end(); ++z) {
+                       if ((*z).tv == tv) {
+                               break;
+                       }
+               }
                
-               tmp = x;
-               ++tmp;
-
-               if (pl) {
+               if (z == pmap.end()) {
+                       pmap.push_back (PlaylistMapping (tv));
+               }
+       }
 
-                       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;
-                       }
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ) {
 
-                       switch (op) {
-                       case Cut:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
-                               pl->remove_region (&((*x)->region));
-                               break;
+               boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist>((*x)->region()->playlist());
+               
+               if (!pl) {
+                       /* impossible, but this handles it for the future */
+                       continue;
+               }
 
-                       case Copy:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
-                               break;
+               TimeAxisView& tv = (*x)->get_trackview();
+               boost::shared_ptr<AudioPlaylist> npl;
+               RegionSelection::iterator tmp;
+               
+               tmp = x;
+               ++tmp;
 
-                       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<Playlist*>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
-               (*pl)->thaw ();
-               session->add_redo_no_execute ((*pl)->get_memento());
+
+       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()));
        }
 }
 
@@ -3305,13 +2894,13 @@ Editor::mouse_paste ()
        event.button.x = wx;
        event.button.y = wy;
        
-       jack_nframes_t where = event_frame (&event, 0, 0);
+       nframes_t where = event_frame (&event, 0, 0);
        snap_to (where);
        paste_internal (where, 1);
 }
 
 void
-Editor::paste_internal (jack_nframes_t position, float times)
+Editor::paste_internal (nframes_t position, float times)
 {
        bool commit = false;
 
@@ -3328,15 +2917,19 @@ Editor::paste_internal (jack_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 ();
        }
@@ -3356,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;
@@ -3376,17 +2971,17 @@ 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;
                }
 
                tmp = chunk;
                ++tmp;
 
-               session->add_undo (apl->get_memento());
-               apl->paste (**chunk, edit_cursor->current_frame, times);
-               session->add_redo_no_execute (apl->get_memento());
+                XMLNode &before = apl->get_state();
+               apl->paste (*chunk, edit_cursor->current_frame, times);
+               session->add_command(new MementoCommand<AudioPlaylist>(*apl, &before, &apl->get_state()));
 
                if (tmp != ns->playlists.end()) {
                        chunk = tmp;
@@ -3397,27 +2992,27 @@ Editor::paste_named_selection (float times)
 }
 
 void
-Editor::duplicate_some_regions (AudioRegionSelection& regions, float times)
+Editor::duplicate_some_regions (RegionSelection& regions, float times)
 {
-       Playlist *playlist; 
-       AudioRegionSelection sel = regions; // clear (below) will clear the argument list
+       boost::shared_ptr<Playlist> playlist; 
+       RegionSelection sel = regions; // clear (below) will clear the argument list
                
        begin_reversible_command (_("duplicate region"));
 
-       selection->clear_audio_regions ();
+       selection->clear_regions ();
 
-       for (AudioRegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
+       for (RegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
 
-               Region& r ((*i)->region);
+               boost::shared_ptr<Region> r ((*i)->region());
 
                TimeAxisView& tv = (*i)->get_time_axis_view();
                AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
-               sigc::connection c = atv->view->AudioRegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+               sigc::connection c = atv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
                
-               playlist = (*i)->region.playlist();
-               session->add_undo (playlist->get_memento());
-               playlist->duplicate (r, r.last_frame(), times);
-               session->add_redo_no_execute (playlist->get_memento());
+               playlist = (*i)->region()->playlist();
+                XMLNode &before = playlist->get_state();
+               playlist->duplicate (r, r->last_frame(), times);
+               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
 
                c.disconnect ();
 
@@ -3437,9 +3032,9 @@ Editor::duplicate_selection (float times)
                return;
        }
 
-       Playlist *playlist; 
-       vector<AudioRegion*> new_regions;
-       vector<AudioRegion*>::iterator ri;
+       boost::shared_ptr<Playlist> playlist; 
+       vector<boost::shared_ptr<AudioRegion> > new_regions;
+       vector<boost::shared_ptr<AudioRegion> >::iterator ri;
                
        create_region_from_selection (new_regions);
 
@@ -3455,9 +3050,10 @@ Editor::duplicate_selection (float times)
                if ((playlist = (*i)->playlist()) == 0) {
                        continue;
                }
-               session->add_undo (playlist->get_memento());
-               playlist->duplicate (**ri, selection->time[clicked_selection].end, times);
-               session->add_redo_no_execute (playlist->get_memento());
+                XMLNode &before = playlist->get_state();
+               playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
+                XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
 
                ++ri;
                if (ri == new_regions.end()) {
@@ -3468,6 +3064,21 @@ Editor::duplicate_selection (float times)
        commit_reversible_command ();
 }
 
+void
+Editor::reset_point_selection ()
+{
+       /* reset all selected points to the relevant default value */
+
+       for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
+               
+               AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
+               
+               if (atv) {
+                       atv->reset_objects (selection->points);
+               } 
+       }
+}
+
 void
 Editor::center_playhead ()
 {
@@ -3485,22 +3096,23 @@ Editor::center_edit_cursor ()
 }
 
 void
-Editor::clear_playlist (Playlist& playlist)
+Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
 {
        begin_reversible_command (_("clear playlist"));
-       session->add_undo (playlist.get_memento());
-       playlist.clear ();
-       session->add_redo_no_execute (playlist.get_memento());
+        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; 
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
-       jack_nframes_t start;
+       boost::shared_ptr<Playlist> playlist; 
+       nframes_t distance;
+       nframes_t next_distance;
+       nframes_t start;
 
        if (use_edit_cursor) {
                start = edit_cursor->current_frame;
@@ -3524,34 +3136,15 @@ Editor::nudge_track (bool use_edit_cursor, bool forwards)
                        continue;
                }               
                
-               session->add_undo (playlist->get_memento());
+                XMLNode &before = playlist->get_state();
                playlist->nudge_after (start, distance, forwards);
-               session->add_redo_no_execute (playlist->get_memento());
+                XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
        }
        
        commit_reversible_command ();                   
 }
 
-void
-Editor::toggle_xfades_active ()
-{
-       if (session) {
-               session->set_crossfades_active (!session->get_crossfades_active());
-       }
-}
-
-void
-Editor::set_xfade_visibility (bool yn)
-{
-       
-}
-
-void
-Editor::toggle_xfade_visibility ()
-{
-       set_xfade_visibility (!xfade_visibility());
-}
-
 void
 Editor::remove_last_capture ()
 {
@@ -3566,16 +3159,12 @@ Editor::remove_last_capture ()
                prompt  = _("Do you really want to destroy the last capture?"
                            "\n(This is destructive and cannot be undone)");
 
-               choices.push_back (_("Yes, destroy it."));
                choices.push_back (_("No, do nothing."));
+               choices.push_back (_("Yes, destroy it."));
                
                Gtkmm2ext::Choice prompter (prompt, choices);
-               prompter.chosen.connect (ptr_fun (Main::quit));
-               prompter.show_all ();
-
-               Main::run ();
                
-               if (prompter.get_choice() == 0) {
+               if (prompter.run () == 1) {
                        session->remove_last_capture ();
                }
 
@@ -3591,7 +3180,7 @@ Editor::normalize_region ()
                return;
        }
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
@@ -3600,10 +3189,13 @@ Editor::normalize_region ()
        track_canvas.get_window()->set_cursor (*wait_cursor);
        gdk_flush ();
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
-               session->add_undo ((*r)->region.get_memento());
-               (*r)->region.normalize_to (0.0f);
-               session->add_redo_no_execute ((*r)->region.get_memento());
+       for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
+               XMLNode &before = arv->region()->get_state();
+               arv->audio_region()->normalize_to (0.0f);
+               session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
        }
 
        commit_reversible_command ();
@@ -3618,16 +3210,19 @@ Editor::denormalize_region ()
                return;
        }
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
        begin_reversible_command ("denormalize");
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
-               session->add_undo ((*r)->region.get_memento());
-               (*r)->region.set_scale_amplitude (1.0f);
-               session->add_redo_no_execute ((*r)->region.get_memento());
+       for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
+               XMLNode &before = arv->region()->get_state();
+               arv->audio_region()->set_scale_amplitude (1.0f);
+               session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
        }
 
        commit_reversible_command ();
@@ -3648,7 +3243,7 @@ Editor::reverse_region ()
 void
 Editor::apply_filter (AudioFilter& filter, string command)
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
@@ -3657,21 +3252,24 @@ Editor::apply_filter (AudioFilter& filter, string command)
        track_canvas.get_window()->set_cursor (*wait_cursor);
        gdk_flush ();
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ) {
+       for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
 
-               AudioRegion& region ((*r)->region);
-               Playlist* playlist = region.playlist();
+               boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
 
-               AudioRegionSelection::iterator tmp;
+               RegionSelection::iterator tmp;
                
                tmp = r;
                ++tmp;
 
-               if (region.apply (filter) == 0) {
+               if (arv->audio_region()->apply (filter) == 0) {
 
-                       session->add_undo (playlist->get_memento());
-                       playlist->replace_region (region, *(filter.results.front()), region.position());
-                       session->add_redo_no_execute (playlist->get_memento());
+                        XMLNode &before = playlist->get_state();
+                       playlist->replace_region (arv->region(), filter.results.front(), arv->region()->position());
+                        XMLNode &after = playlist->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
                } else {
                        goto out;
                }
@@ -3680,7 +3278,7 @@ Editor::apply_filter (AudioFilter& filter, string command)
        }
 
        commit_reversible_command ();
-       selection->audio_regions.clear ();
+       selection->regions.clear ();
 
   out:
        track_canvas.get_window()->set_cursor (*current_canvas_cursor);
@@ -3689,8 +3287,9 @@ Editor::apply_filter (AudioFilter& filter, string command)
 void
 Editor::region_selection_op (void (Region::*pmf)(void))
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)();
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)();
        }
 }
 
@@ -3698,16 +3297,18 @@ Editor::region_selection_op (void (Region::*pmf)(void))
 void
 Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)(arg);
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)(arg);
        }
 }
 
 void
 Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)(yn);
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)(yn);
        }
 }
 
@@ -3722,41 +3323,211 @@ Editor::external_edit_region ()
 }
 
 void
-Editor::brush (jack_nframes_t pos)
+Editor::brush (nframes_t pos)
 {
-       AudioRegionSelection sel;
+       RegionSelection sel;
        snap_to (pos);
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                /* XXX get selection from region list */
        } else { 
-               sel = selection->audio_regions;
+               sel = selection->regions;
        }
 
        if (sel.empty()) {
                return;
        }
 
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
                mouse_brush_insert_region ((*i), 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 (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               (*i)->set_envelope_visible (!(*i)->envelope_visible());
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_envelope_visible_item->get_active();
+                       if (x != arv->envelope_visible()) {
+                               arv->set_envelope_visible (x);
+                       }
+               }
        }
 }
 
 void
 Editor::toggle_gain_envelope_active ()
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               AudioRegion* ar = dynamic_cast<AudioRegion*>(&(*i)->region);
-               if (ar) {
-                       ar->set_envelope_active (true);
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               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));
        }
 }
+