save and restore clock modes
[ardour.git] / gtk2_ardour / editor_ops.cc
index 68baba3061815c8170c29ebe2751fa323a5641c2..e4de1c1e2ea7d528d3247e2255f2266ec5a4b5a9 100644 (file)
 #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 <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 Editing;
@@ -92,7 +91,7 @@ Editor::redo (uint32_t n)
 }
 
 int
-Editor::ensure_cursor (jack_nframes_t *pos)
+Editor::ensure_cursor (nframes_t *pos)
 {
        *pos = edit_cursor->current_frame;
        return 0;
@@ -105,32 +104,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;
@@ -147,21 +149,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;
        }
 
@@ -174,78 +177,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->audio_regions.empty()) {
+       if (!selection->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);
                }
        } 
        
@@ -256,8 +249,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;
@@ -284,8 +277,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;
@@ -312,27 +305,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 ();
@@ -346,32 +340,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 ();
@@ -383,7 +378,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();
                }
        }
 }
@@ -391,22 +386,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 ();
@@ -417,27 +413,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 ();
@@ -449,7 +446,7 @@ Editor::nudge_backward_capture_offset ()
 void
 Editor::move_to_start ()
 {
-       session->request_locate (0);
+       session->goto_start ();
 }
 
 void
@@ -462,9 +459,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 ();
@@ -518,7 +515,7 @@ Editor::build_region_boundary_cache ()
                        }
                }
 
-               jack_nframes_t rpos;
+               nframes_t rpos;
                
                switch (snap_type) {
                case SnapToRegionStart:
@@ -560,23 +557,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)
@@ -625,8 +622,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;
@@ -705,11 +702,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;
 
@@ -733,12 +730,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;
 
@@ -762,8 +759,8 @@ Editor::cursor_to_selection_end (Cursor *cursor)
 void
 Editor::playhead_backward ()
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
+       nframes_t pos;
+       nframes_t cnt;
        float prefix;
        bool was_floating;
 
@@ -771,15 +768,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;
@@ -796,8 +793,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;
 
@@ -805,9 +802,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);
                }
        }
 
@@ -836,8 +833,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;
 
@@ -845,15 +842,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;
@@ -865,8 +862,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;
 
@@ -874,9 +871,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);
                }
        }
 
@@ -889,16 +886,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);
@@ -907,19 +904,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);
                }
        }
 
@@ -935,24 +932,24 @@ Editor::scroll_backward (float pages)
 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;
        }
@@ -973,7 +970,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
@@ -995,15 +997,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 */
@@ -1011,14 +1019,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);
@@ -1029,17 +1039,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:
@@ -1075,7 +1085,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;
@@ -1103,8 +1113,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");
 }
@@ -1112,13 +1122,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;
 
@@ -1126,7 +1138,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;
@@ -1136,9 +1148,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;
 
@@ -1151,26 +1163,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;
 
@@ -1183,7 +1195,70 @@ Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
 }
 
 void
-Editor::select_all_in_track (bool add)
+Editor::add_location_from_selection ()
+{
+       string rangename;
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       if (session == 0 || clicked_trackview == 0) {
+               return;
+       }
+
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+
+       session->locations()->next_available_name(rangename,"selection");
+       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
+
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_playhead_cursor ()
+{
+       string markername;
+
+       nframes_t where = session->audible_frame();
+       
+       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::add_location_from_audio_region ()
+{
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       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
+Editor::select_all_in_track (Selection::Operation op)
 {
        list<Selectable *> touched;
 
@@ -1193,15 +1268,24 @@ Editor::select_all_in_track (bool add)
        
        clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
 
-       if (add) {
+       switch (op) {
+       case Selection::Toggle:
                selection->add (touched);
-       } else {
+               break;
+       case Selection::Set:
                selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* not defined yet */
+               break;
+       case Selection::Add:
+               selection->add (touched);
+               break;
        }
 }
 
 void
-Editor::select_all (bool add)
+Editor::select_all (Selection::Operation op)
 {
        list<Selectable *> touched;
        
@@ -1211,13 +1295,20 @@ Editor::select_all (bool add)
                }
                (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
        }
-
-       if (add) {
+       begin_reversible_command (_("select all"));
+       switch (op) {
+       case Selection::Add:
+       case Selection::Toggle:
                selection->add (touched);
-       } else {
+               break;
+       case Selection::Set:
                selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* not defined yet */
+               break;
        }
-
+       commit_reversible_command ();
 }
 
 void
@@ -1249,7 +1340,7 @@ Editor::invert_selection ()
 }
 
 bool
-Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top, double bot, bool add)
+Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, Selection::Operation op)
 {
        list<Selectable *> touched;
        
@@ -1260,15 +1351,48 @@ Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top,
                (*iter)->get_selectables (start, end, top, bot, touched);
        }
 
-       if (add) {
+       cerr << "select all within found " << touched.size() << endl;
+
+       begin_reversible_command (_("select all within"));
+       switch (op) {
+       case Selection::Add:
+       case Selection::Toggle:
+               cerr << "toggle\n";
                selection->add (touched);
-       } else {
+               break;
+       case Selection::Set:
+               cerr << "set\n";
                selection->set (touched);
+               break;
+       case Selection::Extend:
+               cerr << "extend\n";
+               /* not defined yet */
+               break;
        }
 
+       cerr << "selection now has " << selection->points.size() << endl;
+
+       commit_reversible_command ();
        return !touched.empty();
 }
 
+void
+Editor::set_selection_from_audio_region ()
+{
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       RegionView* rv = *(selection->regions.begin());
+       boost::shared_ptr<Region> region = rv->region();
+       
+       begin_reversible_command (_("set selection from region"));
+       selection->set (0, region->position(), region->last_frame());
+       commit_reversible_command ();
+
+       set_mouse_mode (Editing::MouseRange, false);
+}
+
 void
 Editor::set_selection_from_punch()
 {
@@ -1289,19 +1413,152 @@ Editor::set_selection_from_loop()
        if ((location = session->locations()->auto_loop_location()) == 0)  {
                return;
        }
-
        set_selection_from_range (*location);
 }
 
 void
-Editor::set_selection_from_range (Location& range)
+Editor::set_selection_from_range (Location& loc)
 {
-       if (clicked_trackview == 0) {
+       begin_reversible_command (_("set selection from range"));
+       selection->set (0, loc.start(), loc.end());
+       commit_reversible_command ();
+
+       set_mouse_mode (Editing::MouseRange, false);
+}
+
+void
+Editor::select_all_selectables_using_time_selection ()
+{
+       list<Selectable *> touched;
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+
+       if (end - start < 1)  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
+       }
+
+       begin_reversible_command (_("select all from range"));
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+
+void
+Editor::select_all_selectables_using_punch()
+{
+       Location* location = session->locations()->auto_punch_location();
+       list<Selectable *> touched;
+
+       if (location == 0 || (location->end() - location->start() <= 1))  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all from punch"));
+       selection->set (touched);
+       commit_reversible_command ();
+
+}
+
+void
+Editor::select_all_selectables_using_loop()
+{
+       Location* location = session->locations()->auto_loop_location();
+       list<Selectable *> touched;
+
+       if (location == 0 || (location->end() - location->start() <= 1))  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all from loop"));
+       selection->set (touched);
+       commit_reversible_command ();
+
+}
+
+void
+Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
+{
+        nframes_t start;
+       nframes_t end;
+       list<Selectable *> touched;
+
+       if (after) {
+               begin_reversible_command (_("select all after cursor"));
+               start = cursor->current_frame ;
+               end = session->current_end_frame();
+       } else {
+               if (cursor->current_frame > 0) {
+                       begin_reversible_command (_("select all before cursor"));
+                       start = 0;
+                       end = cursor->current_frame - 1;
+               } else {
+                       return;
+               }
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
+       }
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+void
+Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
+{
+        nframes_t start;
+       nframes_t end;
+       list<Selectable *> touched;
+       bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
+
+       if (cursor->current_frame == other_cursor->current_frame) {
                return;
        }
+
+       begin_reversible_command (_("select all between cursors"));
+       if (other_cursor_is_first) {
+               start = other_cursor->current_frame;
+               end = cursor->current_frame - 1;
+               
+       } else {
+               start = cursor->current_frame;
+               end = other_cursor->current_frame - 1;
+       }
        
-       begin_reversible_command (_("set selection from range"));
-       selection->set (0, range.start(), range.end());
+       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 ();
 }
 
@@ -1388,28 +1645,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
@@ -1417,9 +1676,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 ();
        }
 }
@@ -1429,7 +1689,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();
@@ -1439,7 +1699,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 ();
        }
 }
@@ -1448,26 +1709,51 @@ 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();
+       wy += vertical_adjustment.get_value();
 
        GdkEvent event;
        event.type = GDK_BUTTON_RELEASE;
@@ -1493,25 +1779,31 @@ Editor::insert_region_list_drag (AudioRegion& region, int x, int y)
                return;
        }
        
+       cerr << "drop target playlist, UC  = " << playlist.use_count() << endl;
+
        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();
+       cerr << "pre add target playlist, UC  = " << playlist.use_count() << endl;
+       playlist->add_region (RegionFactory::create (region), where, 1.0);
+       cerr << "post add target playlist, UC  = " << playlist.use_count() << endl;
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
        commit_reversible_command ();
+
+       cerr << "post drop target playlist, UC  = " << playlist.use_count() << endl;
 }
 
 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 {
@@ -1528,16 +1820,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 */
 
@@ -1563,9 +1862,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 */
@@ -1579,8 +1878,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);
@@ -1590,7 +1889,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
@@ -1606,27 +1911,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);
                }
@@ -1656,55 +1961,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
@@ -1725,7 +2008,7 @@ Editor::rename_region ()
        Button ok_button (_("OK"));
        Button cancel_button (_("Cancel"));
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
@@ -1756,7 +2039,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 ();
        }
 }
@@ -1770,7 +2053,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 ();
@@ -1783,7 +2066,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 ? */
 }
@@ -1791,14 +2074,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);
 }
@@ -1831,522 +2114,29 @@ 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;
-               string new_name;
+               nframes_t internal_start;
+               string new_name;
 
                if ((pl = (*i)->playlist()) == 0) {
                        continue;
@@ -2356,30 +2146,32 @@ 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;
        
        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) {
@@ -2390,14 +2182,14 @@ 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)));
        }
 }
 
@@ -2406,11 +2198,13 @@ Editor::split_multichannel_region ()
 {
        vector<AudioRegion*> v;
 
-       if (!clicked_regionview || clicked_regionview->region.n_channels() < 2) {
+       AudioRegionView* clicked_arv = dynamic_cast<AudioRegionView*>(clicked_regionview);
+       
+       if (!clicked_arv || clicked_arv->audio_region()->n_channels() < 2) {
                return;
        }
 
-       clicked_regionview->region.separate_by_channel (*session, v);
+       clicked_arv->audio_region()->separate_by_channel (*session, v);
 
        /* nothing else to do, really */
 }
@@ -2431,7 +2225,7 @@ Editor::separate_region_from_selection ()
                return;
        }
 
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
                
        for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
@@ -2446,17 +2240,71 @@ Editor::separate_region_from_selection ()
                                                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()));
+                               }
+                       }
+               }
+       }
+
+       if (doing_undo) commit_reversible_command ();
+}
+
+void
+Editor::separate_regions_using_location (Location& loc)
+{
+       bool doing_undo = false;
+
+       if (loc.is_mark()) {
+               return;
+       }
+
+       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)
+       */
+
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {    
+       //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()) {
+                                       
+                               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()));
                                }
                        }
                }
@@ -2472,8 +2320,8 @@ Editor::crop_region_to_selection ()
                return;
        }
 
-       vector<Playlist*> playlists;
-       Playlist *playlist;
+       vector<boost::shared_ptr<Playlist> > playlists;
+       boost::shared_ptr<Playlist> playlist;
 
        if (clicked_trackview != 0) {
 
@@ -2503,15 +2351,15 @@ Editor::crop_region_to_selection ()
 
        if (!playlists.empty()) {
 
-               jack_nframes_t start;
-               jack_nframes_t end;
-               jack_nframes_t cnt;
+               nframes_t start;
+               nframes_t end;
+               nframes_t cnt;
 
                begin_reversible_command (_("trim to selection"));
 
-               for (vector<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
                        
-                       Region *region;
+                       boost::shared_ptr<Region> region;
                        
                        start = selection->time.start();
 
@@ -2527,9 +2375,10 @@ Editor::crop_region_to_selection ()
                        end = min (selection->time.end_frame(), start + region->length() - 1);
                        cnt = end - start + 1;
 
-                       session->add_undo ((*i)->get_memento());
+                        XMLNode &before = (*i)->get_state();
                        region->trim_to (start, cnt, this);
-                       session->add_redo_no_execute ((*i)->get_memento());
+                        XMLNode &after = (*i)->get_state();
+                       session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
                }
 
                commit_reversible_command ();
@@ -2539,9 +2388,9 @@ Editor::crop_region_to_selection ()
 void
 Editor::region_fill_track ()
 {
-       jack_nframes_t end;
+       nframes_t end;
 
-       if (!session || selection->audio_regions.empty()) {
+       if (!session || selection->regions.empty()) {
                return;
        }
 
@@ -2549,24 +2398,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 ();
@@ -2583,7 +2438,6 @@ Editor::region_fill_selection ()
                return;
        }
 
-       Region *region;
 
        Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
 
@@ -2592,18 +2446,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"));
@@ -2614,14 +2468,30 @@ 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 (boost::shared_ptr<Region> region, nframes_t 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"));
+        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 ();
+}
+
 void
 Editor::set_region_sync_from_edit_cursor ()
 {
@@ -2629,17 +2499,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 ();
 }
 
@@ -2647,11 +2517,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 ();
        }
 }
@@ -2659,14 +2530,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 ();
 }
@@ -2685,36 +2557,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;  
        }
 
@@ -2728,19 +2600,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));
 
        }
 
@@ -2748,23 +2621,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);
@@ -2772,27 +2645,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
@@ -2802,7 +2676,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;
@@ -2814,9 +2688,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 ();
 }
 
@@ -2827,7 +2702,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;
@@ -2839,9 +2714,10 @@ Editor::trim_region_from_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_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 ();
 }
 
@@ -2927,9 +2803,9 @@ Editor::bounce_range_selection ()
 
        TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
 
-       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;
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+       nframes_t cnt = end - start + 1;
        
        begin_reversible_command (_("bounce range"));
 
@@ -2941,7 +2817,7 @@ Editor::bounce_range_selection ()
                        continue;
                }
                
-               Playlist* playlist;
+               boost::shared_ptr<Playlist> playlist;
                
                if ((playlist = atv->playlist()) == 0) {
                        return;
@@ -2953,9 +2829,10 @@ Editor::bounce_range_selection ()
                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 ();
@@ -2998,16 +2875,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 ();
                                }
                        }
 
@@ -3055,35 +2932,53 @@ 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;
+    }
+};
+       
 void
 Editor::cut_copy_regions (CutCopyOp op)
 {
-        typedef std::map<AudioPlaylist*,AudioPlaylist*> PlaylistMapping;
+        typedef std::map<boost::shared_ptr<AudioPlaylist>,boost::shared_ptr<AudioPlaylist> > PlaylistMapping;
        PlaylistMapping pmap;
-       jack_nframes_t first_position = max_frames;
-       set<Playlist*> freezelist;
-       pair<set<Playlist*>::iterator,bool> insert_result;
+       nframes_t first_position = max_frames;
+
+       set<PlaylistState, lt_playlist> freezelist;
+       pair<set<PlaylistState, lt_playlist>::iterator,bool> insert_result;
 
-       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(); ) {
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ) {
 
-               AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
-               AudioPlaylist* npl;
-               AudioRegionSelection::iterator tmp;
+               boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist>((*x)->region()->playlist());
+               boost::shared_ptr<AudioPlaylist> npl;
+               RegionSelection::iterator tmp;
                
                tmp = x;
                ++tmp;
@@ -3093,25 +2988,31 @@ Editor::cut_copy_regions (CutCopyOp op)
                        PlaylistMapping::iterator pi = pmap.find (pl);
                        
                        if (pi == pmap.end()) {
-                               npl = new AudioPlaylist (*session, "cutlist", true);
+                               npl = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (*session, "cutlist", true));
                                npl->freeze();
                                pmap[pl] = npl;
                        } else {
                                npl = pi->second;
                        }
 
+                       // FIXME
+                       boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>((*x)->region());
                        switch (op) {
                        case Cut:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
-                               pl->remove_region (&((*x)->region));
+                               if (!ar) break;
+
+                               npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
+                               pl->remove_region (((*x)->region()));
                                break;
 
                        case Copy:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
+                               if (!ar) break;
+
+                               npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
                                break;
 
                        case Clear:
-                               pl->remove_region (&((*x)->region));
+                               pl->remove_region (((*x)->region()));
                                break;
                        }
                }
@@ -3119,7 +3020,7 @@ Editor::cut_copy_regions (CutCopyOp op)
                x = tmp;
        }
 
-       list<Playlist*> foo;
+       list<boost::shared_ptr<Playlist> > foo;
 
        for (PlaylistMapping::iterator i = pmap.begin(); i != pmap.end(); ++i) {
                foo.push_back (i->second);
@@ -3129,9 +3030,9 @@ Editor::cut_copy_regions (CutCopyOp op)
                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()));
        }
 }
 
@@ -3154,20 +3055,24 @@ Editor::mouse_paste ()
 {
        int x, y;
        double wx, wy;
+
        track_canvas.get_pointer (x, y);
        track_canvas.window_to_world (x, y, wx, wy);
+       wx += horizontal_adjustment.get_value();
+       wy += vertical_adjustment.get_value();
+
        GdkEvent event;
        event.type = GDK_BUTTON_RELEASE;
        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;
 
@@ -3212,8 +3117,8 @@ 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();
                
@@ -3222,8 +3127,8 @@ Editor::paste_named_selection (float times)
        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;
@@ -3232,17 +3137,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;
@@ -3253,22 +3158,35 @@ 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; 
-       
+       boost::shared_ptr<Playlist> playlist; 
+       RegionSelection sel = regions; // clear (below) will clear the argument list
+               
        begin_reversible_command (_("duplicate region"));
 
-       for (AudioRegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
+       selection->clear_regions ();
+
+       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()->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 ();
 
+               if (latest_regionview) {
+                       selection->add (latest_regionview);
+               }
        }
+               
 
        commit_reversible_command ();
 }
@@ -3280,9 +3198,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);
 
@@ -3298,9 +3216,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()) {
@@ -3311,6 +3230,23 @@ Editor::duplicate_selection (float times)
        commit_reversible_command ();
 }
 
+void
+Editor::reset_point_selection ()
+{
+       /* reset all selected points to the relevant default value */
+
+       cerr << "point selection has " << selection->points.size() << " entries\n";
+       
+       for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
+               
+               AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
+               
+               if (atv) {
+                       atv->reset_objects (selection->points);
+               } 
+       }
+}
+
 void
 Editor::center_playhead ()
 {
@@ -3328,22 +3264,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;
@@ -3367,34 +3304,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 ()
 {
@@ -3409,16 +3327,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 ();
                }
 
@@ -3434,7 +3348,7 @@ Editor::normalize_region ()
                return;
        }
 
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
@@ -3443,10 +3357,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 ();
@@ -3461,16 +3378,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 ();
@@ -3491,7 +3411,7 @@ Editor::reverse_region ()
 void
 Editor::apply_filter (AudioFilter& filter, string command)
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->regions.empty()) {
                return;
        }
 
@@ -3500,21 +3420,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;
                }
@@ -3523,7 +3446,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);
@@ -3532,8 +3455,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)();
        }
 }
 
@@ -3541,16 +3465,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);
        }
 }
 
@@ -3565,41 +3491,115 @@ 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);
+                       }
                }
        }
 }