Reinstate mouse scroll actions (audio clocks, shuttle wheel, faders), clearing select...
[ardour.git] / gtk2_ardour / editor_ops.cc
index 2f8d3a8cfd6fbf8e0ce7e6a9e3c136c6044c1873..192269eb15bc3d6ff909d2b5b177491b918150a2 100644 (file)
@@ -25,8 +25,6 @@
 #include <string>
 #include <map>
 
-#include <sndfile.h>
-
 #include <pbd/error.h>
 #include <pbd/basename.h>
 #include <pbd/pthread_utils.h>
@@ -40,7 +38,7 @@
 #include <ardour/audioregion.h>
 #include <ardour/diskstream.h>
 #include <ardour/filesource.h>
-#include <ardour/sndfilesource.h>
+#include <ardour/externalsource.h>
 #include <ardour/utils.h>
 #include <ardour/location.h>
 #include <ardour/named_selection.h>
@@ -62,6 +60,7 @@
 #include "sfdb_ui.h"
 #include "editing.h"
 #include "gtk-custom-hruler.h"
+#include "gui_thread.h"
 
 #include "i18n.h"
 
@@ -91,6 +90,23 @@ Editor::redo (uint32_t n)
        }
 }
 
+void
+Editor::set_meter_hold (int32_t cnt)
+{
+       if (session) {
+               session->set_meter_hold (cnt);
+       }
+}
+
+void
+Editor::set_meter_falloff (float val)
+{
+       if (session) {
+               session->set_meter_falloff (val);
+       }
+}
+
+
 int
 Editor::ensure_cursor (jack_nframes_t *pos)
 {
@@ -183,13 +199,8 @@ Do you really want to destroy %1 ?"),
        choices.push_back (_("No, do nothing."));
 
        Gtkmm2ext::Choice prompter (prompt, choices);
-
-       prompter.chosen.connect (Gtk::Main::quit.slot());
-       prompter.show_all ();
-
-       Gtk::Main::run ();
-
-       if (prompter.get_choice() != 0) {
+       
+       if (prompter.run() != 0) { /* first choice */
                return;
        }
 
@@ -383,7 +394,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();
                }
        }
 }
@@ -449,7 +460,7 @@ Editor::nudge_backward_capture_offset ()
 void
 Editor::move_to_start ()
 {
-       session->request_locate (0);
+       session->goto_start ();
 }
 
 void
@@ -973,8 +984,12 @@ Editor::scroll_tracks_down ()
                cnt = (int) floor (prefix);
        }
 
-       Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
-       adj->set_value (adj->get_value() + (cnt * adj->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
@@ -990,22 +1005,27 @@ Editor::scroll_tracks_up ()
                cnt = (int) floor (prefix);
        }
 
-       Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
-       adj->set_value (adj->get_value() - (cnt * adj->get_page_size()));
+       vertical_adjustment.set_value (vertical_adjustment.get_value() - (cnt * vertical_adjustment.get_page_size()));
 }
 
 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 */
@@ -1013,6 +1033,8 @@ 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;
@@ -1114,13 +1136,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, string op)
+Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const string & op)
 {
        if (!session) return;
 
@@ -1185,7 +1209,61 @@ Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
 }
 
 void
-Editor::select_all_in_track (bool add)
+Editor::add_location_from_selection ()
+{
+       if (selection->time.empty()) {
+               return;
+       }
+
+       if (session == 0 || clicked_trackview == 0) {
+               return;
+       }
+
+       jack_nframes_t start = selection->time[clicked_selection].start;
+       jack_nframes_t end = selection->time[clicked_selection].end;
+
+       Location *location = new Location (start, end, "selection");
+
+       session->begin_reversible_command (_("add marker"));
+       session->add_undo (session->locations()->get_memento());
+       session->locations()->add (location, true);
+       session->add_redo_no_execute (session->locations()->get_memento());
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_playhead_cursor ()
+{
+       jack_nframes_t where = session->audible_frame();
+       
+       Location *location = new Location (where, where, "mark", Location::IsMark);
+       session->begin_reversible_command (_("add marker"));
+       session->add_undo (session->locations()->get_memento());
+       session->locations()->add (location, true);
+       session->add_redo_no_execute (session->locations()->get_memento());
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_audio_region ()
+{
+       if (selection->audio_regions.empty()) {
+               return;
+       }
+
+       AudioRegionView* rv = *(selection->audio_regions.begin());
+       Region& region = rv->region;
+       
+       Location *location = new Location (region.position(), region.last_frame(), region.name());
+       session->begin_reversible_command (_("add marker"));
+       session->add_undo (session->locations()->get_memento());
+       session->locations()->add (location, true);
+       session->add_redo_no_execute (session->locations()->get_memento());
+       session->commit_reversible_command ();
+}
+
+void
+Editor::select_all_in_track (Selection::Operation op)
 {
        list<Selectable *> touched;
 
@@ -1195,15 +1273,21 @@ 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;
        }
 }
 
 void
-Editor::select_all (bool add)
+Editor::select_all (Selection::Operation op)
 {
        list<Selectable *> touched;
        
@@ -1213,13 +1297,19 @@ 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::Toggle:
                selection->add (touched);
-       } else {
+               break;
+       case Selection::Set:
                selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* not defined yet */
+               break;
        }
-
+       commit_reversible_command ();
 }
 
 void
@@ -1251,7 +1341,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 (jack_nframes_t start, jack_nframes_t end, double top, double bot, Selection::Operation op)
 {
        list<Selectable *> touched;
        
@@ -1261,16 +1351,39 @@ Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top,
                }
                (*iter)->get_selectables (start, end, top, bot, touched);
        }
-
-       if (add) {
+       begin_reversible_command (_("select all within"));
+       switch (op) {
+       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 ();
        return !touched.empty();
 }
 
+void
+Editor::set_selection_from_audio_region ()
+{
+       if (selection->audio_regions.empty()) {
+               return;
+       }
+
+       AudioRegionView* rv = *(selection->audio_regions.begin());
+       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()
 {
@@ -1291,19 +1404,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;
+       }
+
+       jack_nframes_t start = selection->time[clicked_selection].start;
+       jack_nframes_t end = selection->time[clicked_selection].end;
+
+       if (end - start < 1)  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
+       }
+
+       begin_reversible_command (_("select all from range"));
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+
+void
+Editor::select_all_selectables_using_punch()
+{
+       Location* location = session->locations()->auto_punch_location();
+       list<Selectable *> touched;
+
+       if (location == 0 || (location->end() - location->start() <= 1))  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all from punch"));
+       selection->set (touched);
+       commit_reversible_command ();
+
+}
+
+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)
+{
+        jack_nframes_t start;
+       jack_nframes_t end;
+       list<Selectable *> touched;
+
+       if (after) {
+               begin_reversible_command (_("select all after cursor"));
+               start = cursor->current_frame ;
+               end = session->current_end_frame();
+       } else {
+               if (cursor->current_frame > 0) {
+                       begin_reversible_command (_("select all before cursor"));
+                       start = 0;
+                       end = cursor->current_frame - 1;
+               } else {
+                       return;
+               }
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
+       }
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+void
+Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
+{
+        jack_nframes_t start;
+       jack_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 ();
 }
 
@@ -1390,7 +1636,7 @@ Editor::jump_backward_to_mark ()
        if (location) {
                session->request_locate (location->start(), session->transport_rolling());
        } else {
-               session->request_locate (0);
+               session->goto_start ();
        }
 }
 
@@ -1460,9 +1706,8 @@ Editor::clear_locations ()
 /* INSERT/REPLACE */
 
 void
-Editor::insert_region_list_drag (AudioRegion& region)
+Editor::insert_region_list_drag (AudioRegion& region, int x, int y)
 {
-       double x, y;
        double wx, wy;
        double cx, cy;
        TimeAxisView *tv;
@@ -1470,9 +1715,9 @@ Editor::insert_region_list_drag (AudioRegion& region)
        AudioTimeAxisView *atv = 0;
        Playlist *playlist;
        
-       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;
@@ -1529,7 +1774,7 @@ Editor::insert_region_list_selection (float times)
        
        Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
        
-       if (selected.count_selected_rows() != 1) {
+       if (selected->count_selected_rows() != 1) {
                return;
        }
        
@@ -1595,7 +1840,7 @@ 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
@@ -1618,14 +1863,6 @@ Editor::play_selected_region ()
        }
 }
 
-void
-Editor::toggle_loop_playback ()
-{
-       if (session) {
-               session->request_auto_loop (true);
-       }
-}
-
 void
 Editor::loop_selected_region ()
 {
@@ -1819,23 +2056,21 @@ Editor::audition_playlist_region_standalone (AudioRegion& region)
 void
 Editor::build_interthread_progress_window ()
 {
-       interthread_progress_window = new ArdourDialog (X_("interthread progress"));
+       interthread_progress_window = new ArdourDialog (X_("interthread progress"), true);
 
        interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
        
-       interthread_progress_vbox.set_border_width (10);
-       interthread_progress_vbox.set_spacing (5);
-       interthread_progress_vbox.pack_start (interthread_progress_label, false, false);
-       interthread_progress_vbox.pack_start (interthread_progress_bar,false, false);
-       interthread_progress_vbox.pack_start (interthread_cancel_button,false, false);
+       interthread_progress_window->get_vbox()->pack_start (interthread_progress_label, false, false);
+       interthread_progress_window->get_vbox()->pack_start (interthread_progress_bar,false, false);
+
+       // GTK2FIX: this button needs a modifiable label
+
+       Button* b = interthread_progress_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
+       b->signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
 
        interthread_cancel_button.add (interthread_cancel_label);
 
-       interthread_cancel_button.signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
-       
-       interthread_progress_window->set_modal (true);
        interthread_progress_window->set_default_size (200, 100);
-       interthread_progress_window->add (interthread_progress_vbox);
 }
 
 void
@@ -1846,543 +2081,52 @@ Editor::interthread_cancel_clicked ()
        }
 }
 
-void *
-Editor::_import_thread (void *arg)
+void
+Editor::region_from_selection ()
 {
-       PBD::ThreadCreated (pthread_self(), X_("Import"));
+       if (clicked_trackview == 0) {
+               return;
+       }
 
-       Editor *ed = (Editor *) arg;
-       return ed->import_thread ();
-}
+       if (selection->time.empty()) {
+               return;
+       }
 
-void *
-Editor::import_thread ()
-{
-       session->import_audiofile (import_status);
-       return 0;
-}
+       jack_nframes_t start = selection->time[clicked_selection].start;
+       jack_nframes_t end = selection->time[clicked_selection].end;
 
-gint
-Editor::import_progress_timeout (void *arg)
-{
-       interthread_progress_label.set_text (import_status.doing_what);
+       jack_nframes_t selection_cnt = end - start + 1;
+       
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
-       if (import_status.freeze) {
-               interthread_cancel_button.set_sensitive(false);
-       } else {
-               interthread_cancel_button.set_sensitive(true);
-       }
+               AudioRegion *region;
+               AudioRegion *current;
+               Region* current_r;
+               Playlist *pl;
 
-       if (import_status.doing_what == "building peak files") {
-               interthread_progress_bar.pulse ();
-               return FALSE;
-       } else {
-               interthread_progress_bar.set_fraction (import_status.progress/100);
-       }
+               jack_nframes_t internal_start;
+               string new_name;
 
-       return !(import_status.done || import_status.cancel);
-}
+               if ((pl = (*i)->playlist()) == 0) {
+                       continue;
+               }
+
+               if ((current_r = pl->top_region_at (start)) == 0) {
+                       continue;
+               }
+
+               if ((current = dynamic_cast<AudioRegion*> (current_r)) != 0) {
+                       internal_start = start - current->position();
+                       session->region_name (new_name, current->name(), true);
+                       region = new AudioRegion (*current, internal_start, selection_cnt, new_name);
+               }
+       }
+}      
 
 void
-Editor::import_audio (bool as_tracks)
+Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
 {
-       if (session == 0) {
-               warning << _("You can't import an audiofile until you have a session loaded.") << endmsg;
-               return;
-       }
-
-       string str;
-
-       if (as_tracks) {
-               str =_("Import selected as tracks");
-       } else {
-               str = _("Import selected to region list");
-       }
-
-       SoundFileOmega sfdb (str);
-       sfdb.Imported.connect (bind (mem_fun (*this, &Editor::do_import), as_tracks));
-
-       sfdb.run();
-}
-
-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 (string path, SF_INFO& finfo, 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 (Main::quit.slot());
-               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_scroller.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_scroller.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 (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_scroller.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_scroller.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;
-
-               if ((pl = (*i)->playlist()) == 0) {
-                       continue;
-               }
-
-               if ((current_r = pl->top_region_at (start)) == 0) {
-                       continue;
-               }
-
-               if ((current = dynamic_cast<AudioRegion*> (current_r)) != 0) {
-                       internal_start = start - current->position();
-                       session->region_name (new_name, current->name(), true);
-                       region = new AudioRegion (*current, internal_start, selection_cnt, new_name);
-               }
-       }
-}      
-
-void
-Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
-{
-       if (selection->time.empty() || selection->tracks.empty()) {
+       if (selection->time.empty() || selection->tracks.empty()) {
                return;
        }
 
@@ -2480,6 +2224,53 @@ Editor::separate_region_from_selection ()
        if (doing_undo) commit_reversible_command ();
 }
 
+void
+Editor::separate_regions_using_location (Location& loc)
+{
+       bool doing_undo = false;
+
+       if (loc.is_mark()) {
+               return;
+       }
+
+       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) {
+                                       if (!doing_undo) {
+                                               begin_reversible_command (_("separate"));
+                                               doing_undo = true;
+                                       }
+                                       if (doing_undo) session->add_undo ((playlist)->get_memento());
+                       
+                                       /* XXX need to consider musical time selections here at some point */
+
+                                       double speed = atv->get_diskstream()->speed();
+
+
+                                       playlist->partition ((jack_nframes_t)(loc.start() * speed), (jack_nframes_t)(loc.end() * speed), true);
+                                       if (doing_undo) session->add_redo_no_execute (playlist->get_memento());
+                               }
+                       }
+               }
+       }
+
+       if (doing_undo) commit_reversible_command ();
+}
+
 void
 Editor::crop_region_to_selection ()
 {
@@ -2602,7 +2393,7 @@ Editor::region_fill_selection ()
 
        Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
 
-       if (selected.count_selected_rows() != 1) {
+       if (selected->count_selected_rows() != 1) {
                return;
        }
 
@@ -2636,7 +2427,22 @@ Editor::region_fill_selection ()
        
        commit_reversible_command ();                   
 }
-       
+
+void
+Editor::set_a_regions_sync_position (Region& region, jack_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"));
+       session->add_undo (region.playlist()->get_memento());
+       region.set_sync_position (position);
+       session->add_redo_no_execute (region.playlist()->get_memento());
+       commit_reversible_command ();
+}
+
 void
 Editor::set_region_sync_from_edit_cursor ()
 {
@@ -2650,7 +2456,6 @@ Editor::set_region_sync_from_edit_cursor ()
        }
 
        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);
@@ -2855,7 +2660,7 @@ 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);
+       region.trim_front ( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
        session->add_redo_no_execute (region.playlist()->get_memento());
        commit_reversible_command ();
 }
@@ -2921,7 +2726,7 @@ Editor::freeze_route ()
 
        pthread_create (&itt.thread, 0, _freeze_thread, this);
 
-       track_canvas_scroller.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
+       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
 
        while (!itt.done && !itt.cancel) {
                gtk_main_iteration ();
@@ -2930,7 +2735,7 @@ Editor::freeze_route ()
        interthread_progress_connection.disconnect ();
        interthread_progress_window->hide_all ();
        current_interthread_info = 0;
-       track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
+       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
 }
 
 void
@@ -3167,10 +2972,14 @@ Editor::paste (float times)
 void
 Editor::mouse_paste ()
 {
-       double x, y;
+       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;
@@ -3216,11 +3025,11 @@ Editor::paste_internal (jack_nframes_t position, float times)
 void
 Editor::paste_named_selection (float times)
 {
-       TrackSelection::iterator i;
+       TrackSelection::iterator t;
 
        Glib::RefPtr<TreeSelection> selected = named_selection_display.get_selection();
 
-       if (selected.count_selected_rows() == 0 || selection->tracks.empty()) {
+       if (selected->count_selected_rows() != 1 || selection->tracks.empty()) {
                return;
        }
 
@@ -3234,13 +3043,13 @@ Editor::paste_named_selection (float times)
                
        begin_reversible_command (_("paste chunk"));
 
-       for (i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       for (t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
                
                AudioTimeAxisView* atv;
                Playlist* pl;
                AudioPlaylist* apl;
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
+               if ((atv = dynamic_cast<AudioTimeAxisView*> (*t)) == 0) {
                        continue;
                }
 
@@ -3271,19 +3080,32 @@ void
 Editor::duplicate_some_regions (AudioRegionSelection& regions, float times)
 {
        Playlist *playlist; 
-       
+       AudioRegionSelection 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_audio_regions ();
+
+       for (AudioRegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
 
                Region& r ((*i)->region);
+
+               TimeAxisView& tv = (*i)->get_time_axis_view();
+               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
+               sigc::connection c = atv->view->AudioRegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
                
                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());
 
+               c.disconnect ();
+
+               if (latest_regionview) {
+                       selection->add (latest_regionview);
+               }
        }
+               
 
        commit_reversible_command ();
 }
@@ -3390,32 +3212,6 @@ Editor::nudge_track (bool use_edit_cursor, bool forwards)
        commit_reversible_command ();                   
 }
 
-void
-Editor::toggle_xfades_active ()
-{
-       if (session) {
-               session->set_crossfades_active (!session->get_crossfades_active());
-       }
-}
-
-void
-Editor::toggle_follow_playhead ()
-{
-       set_follow_playhead (!_follow_playhead);
-}
-
-void
-Editor::set_xfade_visibility (bool yn)
-{
-       
-}
-
-void
-Editor::toggle_xfade_visibility ()
-{
-       set_xfade_visibility (!xfade_visibility());
-}
-
 void
 Editor::remove_last_capture ()
 {
@@ -3432,16 +3228,13 @@ Editor::remove_last_capture ()
 
                choices.push_back (_("Yes, destroy it."));
                choices.push_back (_("No, do nothing."));
-
+               
                Gtkmm2ext::Choice prompter (prompt, choices);
-               prompter.done.connect (Gtk::Main::quit.slot());
-               prompter.show_all ();
-
-               Gtk::Main::run ();
-
-               if (prompter.get_choice() == 0) {
+               
+               if (prompter.run () == 0) {
                        session->remove_last_capture ();
                }
+
        } else {
                session->remove_last_capture();
        }
@@ -3460,7 +3253,7 @@ Editor::normalize_region ()
 
        begin_reversible_command (_("normalize"));
 
-       track_canvas_scroller.get_window()->set_cursor (*wait_cursor);
+       track_canvas.get_window()->set_cursor (*wait_cursor);
        gdk_flush ();
 
        for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
@@ -3470,7 +3263,7 @@ Editor::normalize_region ()
        }
 
        commit_reversible_command ();
-       track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
+       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
 }
 
 
@@ -3517,7 +3310,7 @@ Editor::apply_filter (AudioFilter& filter, string command)
 
        begin_reversible_command (command);
 
-       track_canvas_scroller.get_window()->set_cursor (*wait_cursor);
+       track_canvas.get_window()->set_cursor (*wait_cursor);
        gdk_flush ();
 
        for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ) {
@@ -3546,7 +3339,7 @@ Editor::apply_filter (AudioFilter& filter, string command)
        selection->audio_regions.clear ();
 
   out:
-       track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
+       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
 }
 
 void