+ vertical_adjustment.set_value (vert_value);
+}
+
+void
+Editor::scroll_tracks_up ()
+{
+ float prefix;
+ bool was_floating;
+ int cnt;
+
+ if (get_prefix (prefix, was_floating)) {
+ cnt = 1;
+ } else {
+ cnt = (int) floor (prefix);
+ }
+
+ 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();
+ double vert_value = adj->get_value() + 60;
+
+ 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() - 60);
+}
+
+/* ZOOM */
+
+void
+Editor::tav_zoom_step (bool coarser)
+{
+ ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::temporal_zoom_step), coarser));
+
+ _routes->suspend_redisplay ();
+
+ for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+ TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
+ tv->step_height (coarser);
+ }
+
+ _routes->resume_redisplay ();
+}
+
+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 *= 1.61803399;
+ } else {
+ nfpu = max(1.0,(nfpu/1.61803399));
+ }
+
+ temporal_zoom (nfpu);
+}
+
+void
+Editor::temporal_zoom (gdouble fpu)
+{
+ if (!session) return;
+
+ nframes64_t current_page = current_page_frames();
+ nframes64_t current_leftmost = leftmost_frame;
+ nframes64_t current_rightmost;
+ nframes64_t current_center;
+ nframes64_t new_page_size;
+ nframes64_t half_page_size;
+ nframes64_t leftmost_after_zoom = 0;
+ nframes64_t where;
+ bool in_track_canvas;
+ double nfpu;
+ double l;
+
+ /* XXX this limit is also in ::set_frames_per_unit() */
+
+ if (frames_per_unit <= 2.0 && fpu <= frames_per_unit) {
+ return;
+ }
+
+ nfpu = fpu;
+
+ new_page_size = (nframes64_t) floor (_canvas_width * nfpu);
+ half_page_size = new_page_size / 2;
+
+ switch (zoom_focus) {
+ case ZoomFocusLeft:
+ leftmost_after_zoom = current_leftmost;
+ break;
+
+ case ZoomFocusRight:
+ current_rightmost = leftmost_frame + current_page;
+ if (current_rightmost < new_page_size) {
+ leftmost_after_zoom = 0;
+ } else {
+ leftmost_after_zoom = current_rightmost - new_page_size;
+ }
+ break;
+
+ case ZoomFocusCenter:
+ current_center = current_leftmost + (current_page/2);
+ if (current_center < half_page_size) {
+ leftmost_after_zoom = 0;
+ } else {
+ leftmost_after_zoom = current_center - half_page_size;
+ }
+ break;
+
+ case ZoomFocusPlayhead:
+ /* try to keep the playhead in the same place */
+
+ where = playhead_cursor->current_frame;
+
+ l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+ if (l < 0) {
+ leftmost_after_zoom = 0;
+ } else if (l > max_frames) {
+ leftmost_after_zoom = max_frames - new_page_size;
+ } else {
+ leftmost_after_zoom = (nframes64_t) l;
+ }
+ break;
+
+ case ZoomFocusMouse:
+ /* try to keep the mouse over the same point in the display */
+
+ if (!mouse_frame (where, in_track_canvas)) {
+ /* use playhead instead */
+ where = playhead_cursor->current_frame;
+
+ if (where < half_page_size) {
+ leftmost_after_zoom = 0;
+ } else {
+ leftmost_after_zoom = where - half_page_size;
+ }
+
+ } else {
+
+ l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+ if (l < 0) {
+ leftmost_after_zoom = 0;
+ } else if (l > max_frames) {
+ leftmost_after_zoom = max_frames - new_page_size;
+ } else {
+ leftmost_after_zoom = (nframes64_t) l;
+ }
+ }
+
+ break;
+
+ case ZoomFocusEdit:
+ /* try to keep the edit point in the same place */
+ where = get_preferred_edit_position ();
+
+ if (where > 0) {
+
+ double l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+ if (l < 0) {
+ leftmost_after_zoom = 0;
+ } else if (l > max_frames) {
+ leftmost_after_zoom = max_frames - new_page_size;
+ } else {
+ leftmost_after_zoom = (nframes64_t) l;
+ }
+
+ } else {
+ /* edit point not defined */
+ return;
+ }
+ break;
+
+ }
+
+ // leftmost_after_zoom = min (leftmost_after_zoom, session->current_end_frame());
+
+ reposition_and_zoom (leftmost_after_zoom, nfpu);
+}
+
+void
+Editor::temporal_zoom_region (bool both_axes)
+{
+
+ nframes64_t start = max_frames;
+ nframes64_t end = 0;
+ RegionSelection rs;
+ set<TimeAxisView*> tracks;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+ if ((*i)->region()->position() < start) {
+ start = (*i)->region()->position();
+ }
+
+ if ((*i)->region()->last_frame() + 1 > end) {
+ end = (*i)->region()->last_frame() + 1;
+ }
+
+ tracks.insert (&((*i)->get_time_axis_view()));
+ }
+
+ /* now comes an "interesting" hack ... make sure we leave a little space
+ at each end of the editor so that the zoom doesn't fit the region
+ precisely to the screen.
+ */
+
+ GdkScreen* screen = gdk_screen_get_default ();
+ gint pixwidth = gdk_screen_get_width (screen);
+ gint mmwidth = gdk_screen_get_width_mm (screen);
+ double pix_per_mm = (double) pixwidth/ (double) mmwidth;
+ double one_centimeter_in_pixels = pix_per_mm * 10.0;
+
+ if ((start == 0 && end == 0) || end < start) {
+ return;
+ }
+
+ nframes64_t range = end - start;
+ double new_fpu = (double)range / (double)_canvas_width;
+ nframes64_t extra_samples = (nframes64_t) floor (one_centimeter_in_pixels * new_fpu);
+
+ if (start > extra_samples) {
+ start -= extra_samples;
+ } else {
+ start = 0;
+ }
+
+ if (max_frames - extra_samples > end) {
+ end += extra_samples;
+ } else {
+ end = max_frames;
+ }
+
+ if (both_axes) {
+ /* save visual state with track states included, and prevent
+ set_frames_per_unit() from doing it again.
+ */
+ undo_visual_stack.push_back (current_visual_state(true));
+ no_save_visual = true;
+ }
+
+ temporal_zoom_by_frame (start, end, "zoom to region");
+
+ if (both_axes) {
+ uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
+
+ /* set visible track heights appropriately */
+
+ for (set<TimeAxisView*>::iterator t = tracks.begin(); t != tracks.end(); ++t) {
+ (*t)->set_height (per_track_height);
+ }
+
+ /* hide irrelevant tracks */
+
+ _routes->suspend_redisplay ();
+
+ for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+ if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
+ hide_track_in_display (**i, true);
+ }
+ }
+
+ _routes->resume_redisplay ();
+
+ vertical_adjustment.set_value (0.0);
+ no_save_visual = false;
+ }
+
+ redo_visual_stack.push_back (current_visual_state());
+}
+
+void
+Editor::zoom_to_region (bool both_axes)
+{
+ temporal_zoom_region (both_axes);
+}
+
+void
+Editor::temporal_zoom_selection ()
+{
+ if (!selection) return;
+
+ if (selection->time.empty()) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ temporal_zoom_by_frame (start, end, "zoom to selection");
+}
+
+void
+Editor::temporal_zoom_session ()
+{
+ ENSURE_GUI_THREAD (mem_fun (*this, &Editor::temporal_zoom_session));
+
+ if (session) {
+ temporal_zoom_by_frame (session->current_start_frame(), session->current_end_frame(), "zoom to session");
+ }
+}
+
+void
+Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string & /*op*/)
+{
+ if (!session) return;
+
+ if ((start == 0 && end == 0) || end < start) {
+ return;
+ }
+
+ nframes64_t range = end - start;
+
+ double new_fpu = (double)range / (double)_canvas_width;
+
+ nframes64_t new_page = (nframes64_t) floor (_canvas_width * new_fpu);
+ nframes64_t middle = (nframes64_t) floor( (double)start + ((double)range / 2.0f ));
+ nframes64_t new_leftmost = (nframes64_t) floor( (double)middle - ((double)new_page/2.0f));
+
+ if (new_leftmost > middle) {
+ new_leftmost = 0;
+ }
+
+ reposition_and_zoom (new_leftmost, new_fpu);
+}
+
+void
+Editor::temporal_zoom_to_frame (bool coarser, nframes64_t frame)
+{
+ if (!session) {
+ return;
+ }
+ double range_before = frame - leftmost_frame;
+ double new_fpu;
+
+ new_fpu = frames_per_unit;
+
+ if (coarser) {
+ new_fpu *= 1.61803399;
+ range_before *= 1.61803399;
+ } else {
+ new_fpu = max(1.0,(new_fpu/1.61803399));
+ range_before /= 1.61803399;
+ }
+
+ if (new_fpu == frames_per_unit) {
+ return;
+ }
+
+ nframes64_t new_leftmost = frame - (nframes64_t)range_before;
+
+ if (new_leftmost > frame) {
+ new_leftmost = 0;
+ }
+// begin_reversible_command (_("zoom to frame"));
+// session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
+// session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
+// commit_reversible_command ();
+
+ reposition_and_zoom (new_leftmost, new_fpu);
+}
+
+
+bool
+Editor::choose_new_marker_name(string &name) {
+
+ if (!Config->get_name_new_markers()) {
+ /* don't prompt user for a new name */
+ return true;
+ }
+
+ ArdourPrompter dialog (true);
+
+ dialog.set_prompt (_("New Name:"));
+
+ dialog.set_title (_("New Location Marker"));
+
+ dialog.set_name ("MarkNameWindow");
+ dialog.set_size_request (250, -1);
+ dialog.set_position (Gtk::WIN_POS_MOUSE);
+
+ dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
+ dialog.set_initial_text (name);
+
+ dialog.show ();
+
+ switch (dialog.run ()) {
+ case RESPONSE_ACCEPT:
+ break;
+ default:
+ return false;
+ }
+
+ dialog.get_result(name);
+ return true;
+
+}
+
+
+void
+Editor::add_location_from_selection ()
+{
+ string rangename;
+
+ if (selection->time.empty()) {
+ return;
+ }
+
+ if (session == 0 || clicked_axisview == 0) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_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_mark (nframes64_t where)
+{
+ string markername;
+
+ select_new_marker = true;
+
+ session->locations()->next_available_name(markername,"mark");
+ if (!choose_new_marker_name(markername)) {
+ return;
+ }
+ 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_playhead_cursor ()
+{
+ add_location_mark (session->audible_frame());
+}
+
+void
+Editor::add_locations_from_audio_region ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ session->begin_reversible_command (rs.size () > 1 ? _("add markers") : _("add marker"));
+ XMLNode &before = session->locations()->get_state();
+
+ cerr << "Add locations\n";
+
+ for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
+
+ boost::shared_ptr<Region> region = (*i)->region ();
+
+ Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+
+ session->locations()->add (location, true);
+ }
+
+ XMLNode &after = session->locations()->get_state();
+ session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+ session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_audio_region ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ session->begin_reversible_command (_("add marker"));
+ XMLNode &before = session->locations()->get_state();
+
+ string markername;
+
+ if (rs.size() > 1) { // more than one region selected
+ session->locations()->next_available_name(markername, "regions");
+ } else {
+ RegionView* rv = *(rs.begin());
+ boost::shared_ptr<Region> region = rv->region();
+ markername = region->name();
+ }
+
+ if (!choose_new_marker_name(markername)) {
+ return;
+ }
+
+ cerr << "Add location\n";
+
+ // single range spanning all selected
+ Location *location = new Location (rs.start(), rs.end_frame(), markername, Location::IsRangeMarker);
+ session->locations()->add (location, true);
+
+ XMLNode &after = session->locations()->get_state();
+ session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+ session->commit_reversible_command ();
+}
+
+void
+Editor::amplitude_zoom_step (bool in)
+{
+ gdouble zoom = 1.0;
+
+ if (in) {
+ zoom *= 2.0;
+ } else {
+ if (zoom > 2.0) {
+ zoom /= 2.0;
+ } else {
+ zoom = 1.0;
+ }
+ }
+
+#ifdef FIX_FOR_CANVAS
+ /* XXX DO SOMETHING */
+#endif
+}
+
+
+/* DELETION */
+
+
+void
+Editor::delete_sample_forward ()
+{
+}
+
+void
+Editor::delete_sample_backward ()
+{
+}
+
+void
+Editor::delete_screen ()
+{
+}
+
+/* SEARCH */
+
+void
+Editor::search_backwards ()
+{
+ /* what ? */
+}
+
+void
+Editor::search_forwards ()
+{
+ /* what ? */
+}
+
+/* MARKS */
+
+void
+Editor::jump_forward_to_mark ()
+{
+ if (!session) {
+ return;
+ }
+
+ Location *location = session->locations()->first_location_after (playhead_cursor->current_frame);
+
+ if (location) {
+ session->request_locate (location->start(), session->transport_rolling());
+ } else {
+ session->request_locate (session->current_end_frame());
+ }
+}
+
+void
+Editor::jump_backward_to_mark ()
+{
+ if (!session) {
+ return;
+ }
+
+ Location *location = session->locations()->first_location_before (playhead_cursor->current_frame);
+
+ if (location) {
+ session->request_locate (location->start(), session->transport_rolling());
+ } else {
+ session->goto_start ();
+ }
+}
+
+void
+Editor::set_mark ()
+{
+ nframes64_t pos;
+ float prefix;
+ bool was_floating;
+ string markername;
+
+ if (get_prefix (prefix, was_floating)) {
+ pos = session->audible_frame ();
+ } else {
+ if (was_floating) {
+ pos = (nframes64_t) floor (prefix * session->frame_rate ());
+ } else {
+ pos = (nframes64_t) floor (prefix);
+ }
+ }
+
+ session->locations()->next_available_name(markername,"mark");
+ if (!choose_new_marker_name(markername)) {
+ return;
+ }
+ session->locations()->add (new Location (pos, 0, markername, Location::IsMark), true);
+}
+
+void
+Editor::clear_markers ()
+{
+ if (session) {
+ session->begin_reversible_command (_("clear markers"));
+ XMLNode &before = session->locations()->get_state();
+ session->locations()->clear_markers ();
+ XMLNode &after = session->locations()->get_state();
+ session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+ session->commit_reversible_command ();
+ }
+}
+
+void
+Editor::clear_ranges ()
+{
+ if (session) {
+ session->begin_reversible_command (_("clear ranges"));
+ XMLNode &before = session->locations()->get_state();
+
+ Location * looploc = session->locations()->auto_loop_location();
+ Location * punchloc = session->locations()->auto_punch_location();
+
+ session->locations()->clear_ranges ();
+ // re-add these
+ if (looploc) session->locations()->add (looploc);
+ if (punchloc) session->locations()->add (punchloc);
+
+ XMLNode &after = session->locations()->get_state();
+ session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+ session->commit_reversible_command ();
+ }
+}
+
+void
+Editor::clear_locations ()
+{
+ session->begin_reversible_command (_("clear locations"));
+ XMLNode &before = session->locations()->get_state();
+ session->locations()->clear ();
+ 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 (boost::shared_ptr<Region> region, int x, int y)
+{
+ double wx, wy;
+ double cx, cy;
+ nframes64_t where;
+ RouteTimeAxisView *rtv = 0;
+ 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;
+ event.button.x = wx;
+ event.button.y = wy;
+
+ where = event_frame (&event, &cx, &cy);
+
+ if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
+ /* clearly outside canvas area */
+ return;
+ }
+
+ std::pair<TimeAxisView*, int> tv = trackview_by_y_position (cy);
+ if (tv.first == 0) {
+ return;
+ }
+
+ if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
+ return;
+ }
+
+ if ((playlist = rtv->playlist()) == 0) {
+ return;
+ }
+
+ snap_to (where);
+
+ begin_reversible_command (_("insert dragged region"));
+ XMLNode &before = playlist->get_state();
+ playlist->add_region (RegionFactory::create (region), where, 1.0);
+ session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+ commit_reversible_command ();
+}
+
+void
+Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y) {
+ double wx, wy;
+ double cx, cy;
+ nframes_t where;
+ RouteTimeAxisView *dest_rtv = 0;
+ RouteTimeAxisView *source_rtv = 0;
+
+ 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;
+
+ where = event_frame (&event, &cx, &cy);
+
+ std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
+ if (tv.first == 0) {
+ return;
+ }
+
+ if ((dest_rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
+ return;
+ }
+
+ /* use this drag source to add underlay to a track. But we really don't care
+ about the Route, only the view of the route, so find it first */
+ for(TrackViewList::iterator it = track_views.begin(); it != track_views.end(); ++it) {
+ if((source_rtv = dynamic_cast<RouteTimeAxisView*>(*it)) == 0) {
+ continue;
+ }
+
+ if(source_rtv->route() == route && source_rtv != dest_rtv) {
+ dest_rtv->add_underlay(source_rtv->view());
+ break;
+ }
+ }
+}
+
+void
+Editor::insert_region_list_selection (float times)
+{
+ RouteTimeAxisView *tv = 0;
+ boost::shared_ptr<Playlist> playlist;
+
+ if (clicked_routeview != 0) {
+ tv = clicked_routeview;
+ } else if (!selection->tracks.empty()) {
+ if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
+ return;
+ }
+ } else if (entered_track != 0) {
+ if ((tv = dynamic_cast<RouteTimeAxisView*>(entered_track)) == 0) {
+ return;
+ }
+ } else {
+ return;
+ }
+
+ if ((playlist = tv->playlist()) == 0) {
+ return;
+ }
+
+ boost::shared_ptr<Region> region = _regions->get_single_selection ();
+ if (region == 0) {
+ return;
+ }
+
+ begin_reversible_command (_("insert region"));
+ XMLNode &before = playlist->get_state();
+ playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
+ session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+ commit_reversible_command ();
+}
+
+/* BUILT-IN EFFECTS */
+
+void
+Editor::reverse_selection ()
+{
+
+}
+
+/* GAIN ENVELOPE EDITING */
+
+void
+Editor::edit_envelope ()
+{
+}
+
+/* PLAYBACK */
+
+void
+Editor::transition_to_rolling (bool fwd)
+{
+ if (!session) {
+ return;
+ }
+
+ if (session->config.get_external_sync()) {
+ switch (session->config.get_sync_source()) {
+ case JACK:
+ break;
+ default:
+ /* transport controlled by the master */
+ return;
+ }
+ }
+
+ if (session->is_auditioning()) {
+ session->cancel_audition ();
+ return;
+ }
+
+ session->request_transport_speed (fwd ? 1.0f : -1.0f);
+}
+
+void
+Editor::play_from_start ()
+{
+ session->request_locate (session->current_start_frame(), true);
+}
+
+void
+Editor::play_from_edit_point ()
+{
+ session->request_locate (get_preferred_edit_position(), true);
+}
+
+void
+Editor::play_from_edit_point_and_return ()
+{
+ nframes64_t start_frame;
+ nframes64_t return_frame;
+
+ start_frame = get_preferred_edit_position (true);
+
+ if (session->transport_rolling()) {
+ session->request_locate (start_frame, false);
+ return;
+ }
+
+ /* don't reset the return frame if its already set */
+
+ if ((return_frame = session->requested_return_frame()) < 0) {
+ return_frame = session->audible_frame();
+ }
+
+ if (start_frame >= 0) {
+ session->request_roll_at_and_return (start_frame, return_frame);
+ }
+}
+
+void
+Editor::play_selection ()
+{
+ if (selection->time.empty()) {
+ return;
+ }
+
+ session->request_play_range (&selection->time, true);
+}
+
+void
+Editor::loop_selected_region ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (!rs.empty()) {
+ RegionView *rv = *(rs.begin());
+ Location* tll;
+
+ if ((tll = transport_loop_location()) != 0) {
+
+ tll->set (rv->region()->position(), rv->region()->last_frame());
+
+ // enable looping, reposition and start rolling
+
+ session->request_play_loop (true);
+ session->request_locate (tll->start(), false);
+ session->request_transport_speed (1.0f);
+ }
+ }
+}
+
+void
+Editor::play_location (Location& location)
+{
+ if (location.start() <= location.end()) {
+ return;
+ }
+
+ session->request_bounded_roll (location.start(), location.end());
+}
+
+void
+Editor::loop_location (Location& location)
+{
+ if (location.start() <= location.end()) {
+ return;
+ }
+
+ Location* tll;
+
+ if ((tll = transport_loop_location()) != 0) {
+ tll->set (location.start(), location.end());
+
+ // enable looping, reposition and start rolling
+ session->request_play_loop (true);
+ session->request_locate (tll->start(), true);
+ }
+}
+
+void
+Editor::raise_region ()
+{
+ selection->foreach_region (&Region::raise);
+}
+
+void
+Editor::raise_region_to_top ()
+{
+ selection->foreach_region (&Region::raise_to_top);
+}
+
+void
+Editor::lower_region ()
+{
+ selection->foreach_region (&Region::lower);
+}
+
+void
+Editor::lower_region_to_bottom ()
+{
+ selection->foreach_region (&Region::lower_to_bottom);
+}
+
+/** Show the region editor for the selected regions */
+void
+Editor::edit_region ()
+{
+ selection->foreach_regionview (&RegionView::show_region_editor);
+}
+
+/** Show the midi list editor for the selected MIDI regions */
+void
+Editor::show_midi_list_editor ()
+{
+ selection->foreach_midi_regionview (&MidiRegionView::show_list_editor);
+}
+
+void
+Editor::rename_region()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ ArdourDialog d (*this, _("Rename Region"), true, false);
+ Entry entry;
+ Label label (_("New name:"));
+ HBox hbox;
+
+ hbox.set_spacing (6);
+ hbox.pack_start (label, false, false);
+ hbox.pack_start (entry, true, true);
+
+ d.get_vbox()->set_border_width (12);
+ d.get_vbox()->pack_start (hbox, false, false);
+
+ d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
+ d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+
+ d.set_size_request (300, -1);
+ d.set_position (Gtk::WIN_POS_MOUSE);
+
+ entry.set_text (rs.front()->region()->name());
+ entry.select_region (0, -1);
+
+ entry.signal_activate().connect (bind (mem_fun (d, &Dialog::response), RESPONSE_OK));
+
+ d.show_all ();
+
+ entry.grab_focus();
+
+ int ret = d.run();
+
+ d.hide ();
+
+ if (ret == RESPONSE_OK) {
+ std::string str = entry.get_text();
+ strip_whitespace_edges (str);
+ if (!str.empty()) {
+ rs.front()->region()->set_name (str);
+ _regions->redisplay ();
+ }
+ }
+}
+
+void
+Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Route& route)
+{
+ if (session->is_auditioning()) {
+ session->cancel_audition ();
+ }
+
+ // note: some potential for creativity here, because region doesn't
+ // have to belong to the playlist that Route is handling
+
+ // bool was_soloed = route.soloed();
+
+ route.set_solo (true, this);
+
+ session->request_bounded_roll (region->position(), region->position() + region->length());
+
+ /* XXX how to unset the solo state ? */
+}
+
+/** Start an audition of the first selected region */
+void
+Editor::play_edit_range ()
+{
+ nframes64_t start, end;
+
+ if (get_edit_op_range (start, end)) {
+ session->request_bounded_roll (start, end);
+ }
+}
+
+void
+Editor::play_selected_region ()
+{
+ nframes64_t start = max_frames;
+ nframes64_t end = 0;
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ if ((*i)->region()->position() < start) {
+ start = (*i)->region()->position();
+ }
+ if ((*i)->region()->last_frame() + 1 > end) {
+ end = (*i)->region()->last_frame() + 1;
+ }
+ }
+
+ session->request_bounded_roll (start, end);
+}
+
+void
+Editor::audition_playlist_region_standalone (boost::shared_ptr<Region> region)
+{
+ session->audition_region (region);
+}
+
+void
+Editor::build_interthread_progress_window ()
+{
+ interthread_progress_window = new ArdourDialog (X_("interthread progress"), true);
+
+ interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
+
+ interthread_progress_window->set_border_width (12);
+ interthread_progress_window->get_vbox()->set_spacing (6);
+
+ interthread_progress_label.set_alignment (0.5, 0.5);
+
+ 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_progress_window->set_default_size (200, 100);
+}
+
+void
+Editor::interthread_cancel_clicked ()
+{
+ if (current_interthread_info) {
+ current_interthread_info->cancel = true;
+ }
+}
+
+void
+Editor::region_from_selection ()
+{
+ if (clicked_axisview == 0) {
+ return;
+ }
+
+ if (selection->time.empty()) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ TrackSelection tracks = get_tracks_for_range_action ();
+
+ nframes64_t selection_cnt = end - start + 1;
+
+ for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+ boost::shared_ptr<Region> current;
+ boost::shared_ptr<Playlist> pl;
+ nframes64_t internal_start;
+ string new_name;
+
+ if ((pl = (*i)->playlist()) == 0) {
+ continue;
+ }
+
+ if ((current = pl->top_region_at (start)) == 0) {
+ continue;
+ }
+
+ internal_start = start - current->position();
+ session->region_name (new_name, current->name(), true);
+ boost::shared_ptr<Region> region (RegionFactory::create (current,
+ internal_start, selection_cnt, new_name));
+ }
+}
+
+void
+Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_regions)
+{
+ if (selection->time.empty() || selection->tracks.empty()) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ sort_track_selection ();
+
+ for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+ boost::shared_ptr<Region> current;
+ boost::shared_ptr<Playlist> playlist;
+ nframes64_t internal_start;
+ string new_name;
+
+ if ((playlist = (*i)->playlist()) == 0) {
+ continue;
+ }
+
+ if ((current = playlist->top_region_at(start)) == 0) {
+ continue;
+ }
+
+ internal_start = start - current->position();
+ session->region_name (new_name, current->name(), true);
+
+ new_regions.push_back (RegionFactory::create (current,
+ internal_start, end - start + 1, new_name));
+ }
+}
+
+void
+Editor::split_multichannel_region ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ vector< boost::shared_ptr<Region> > v;
+
+ for (list<RegionView*>::iterator x = rs.begin(); x != rs.end(); ++x) {
+ (*x)->region()->separate_by_channel (*session, v);
+ }
+}
+
+void
+Editor::new_region_from_selection ()
+{
+ region_from_selection ();
+ cancel_selection ();
+}
+
+static void
+add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
+{
+ switch (rv->region()->coverage (ar->start, ar->end - 1)) {
+ case OverlapNone:
+ break;
+ default:
+ rs->push_back (rv);
+ }
+}
+
+/** Return either:
+ * - selected tracks, or if there are none...
+ * - tracks containing selected regions, or if there are none...
+ * - all tracks
+ * @return tracks.
+ */
+TrackSelection
+Editor::get_tracks_for_range_action () const
+{
+ TrackSelection t;
+
+ if (selection->tracks.empty()) {
+
+ /* use tracks with selected regions */
+
+ RegionSelection rs = selection->regions;
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ TimeAxisView* tv = &(*i)->get_time_axis_view();
+
+ if (!t.contains (tv)) {
+ t.push_back (tv);
+ }
+ }
+
+ if (t.empty()) {
+ /* no regions and no tracks: use all tracks */
+ t = track_views;
+ }
+
+ } else {
+
+ t = selection->tracks;
+ }
+
+ return t;
+}
+
+void
+Editor::separate_regions_between (const TimeSelection& ts)
+{
+ bool in_command = false;
+ boost::shared_ptr<Playlist> playlist;
+ RegionSelection new_selection;
+
+ TrackSelection tmptracks = get_tracks_for_range_action ();
+ sort_track_selection (&tmptracks);
+
+ for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
+
+ RouteTimeAxisView* rtv;
+
+ if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
+
+ if (rtv->is_track()) {
+
+ /* no edits to destructive tracks */
+
+ if (rtv->track()->diskstream()->destructive()) {
+ continue;
+ }
+
+ if ((playlist = rtv->playlist()) != 0) {
+
+ XMLNode *before;
+ bool got_some;
+
+ before = &(playlist->get_state());
+ got_some = false;
+
+ /* XXX need to consider musical time selections here at some point */
+
+ double speed = rtv->get_diskstream()->speed();
+
+
+ for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
+
+ sigc::connection c = rtv->view()->RegionViewAdded.connect (
+ mem_fun(*this, &Editor::collect_new_region_view));
+ latest_regionviews.clear ();
+
+ playlist->partition ((nframes64_t)((*t).start * speed),
+ (nframes64_t)((*t).end * speed), true);
+
+ c.disconnect ();
+
+ if (!latest_regionviews.empty()) {
+
+ got_some = true;
+
+ rtv->view()->foreach_regionview (bind (
+ sigc::ptr_fun (add_if_covered),
+ &(*t), &new_selection));
+
+ if (!in_command) {
+ begin_reversible_command (_("separate"));
+ in_command = true;
+ }
+
+ session->add_command(new MementoCommand<Playlist>(
+ *playlist, before, &playlist->get_state()));
+ }
+ }
+
+ if (!got_some) {
+ delete before;
+ }
+ }
+ }
+ }
+ }
+
+ if (in_command) {
+ selection->set (new_selection);
+ set_mouse_mode (MouseObject);
+
+ commit_reversible_command ();
+ }
+}
+
+/** Take tracks from get_tracks_for_range_action and cut any regions
+ * on those tracks so that the tracks are empty over the time
+ * selection.
+ */
+void
+Editor::separate_region_from_selection ()
+{
+ /* preferentially use *all* ranges in the time selection if we're in range mode
+ to allow discontiguous operation, since get_edit_op_range() currently
+ returns a single range.
+ */
+
+ if (mouse_mode == MouseRange && !selection->time.empty()) {
+
+ separate_regions_between (selection->time);
+
+ } else {
+
+ nframes64_t start;
+ nframes64_t end;
+
+ if (get_edit_op_range (start, end)) {
+
+ AudioRange ar (start, end, 1);
+ TimeSelection ts;
+ ts.push_back (ar);
+
+ separate_regions_between (ts);
+ }
+ }
+}
+
+void
+Editor::separate_region_from_punch ()
+{
+ Location* loc = session->locations()->auto_punch_location();
+ if (loc) {
+ separate_regions_using_location (*loc);
+ }
+}
+
+void
+Editor::separate_region_from_loop ()
+{
+ Location* loc = session->locations()->auto_loop_location();
+ if (loc) {
+ separate_regions_using_location (*loc);
+ }
+}
+
+void
+Editor::separate_regions_using_location (Location& loc)
+{
+ if (loc.is_mark()) {
+ return;
+ }
+
+ AudioRange ar (loc.start(), loc.end(), 1);
+ TimeSelection ts;
+
+ ts.push_back (ar);
+
+ separate_regions_between (ts);
+}
+
+void
+Editor::crop_region_to_selection ()
+{
+ if (!selection->time.empty()) {
+
+ crop_region_to (selection->time.start(), selection->time.end_frame());
+
+ } else {
+
+ nframes64_t start;
+ nframes64_t end;
+
+ if (get_edit_op_range (start, end)) {
+ crop_region_to (start, end);
+ }
+ }
+
+}
+
+void
+Editor::crop_region_to (nframes64_t start, nframes64_t end)
+{
+ vector<boost::shared_ptr<Playlist> > playlists;
+ boost::shared_ptr<Playlist> playlist;
+ TrackSelection* ts;
+
+ if (selection->tracks.empty()) {
+ ts = &track_views;
+ } else {
+ sort_track_selection ();
+ ts = &selection->tracks;
+ }
+
+ for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+
+ RouteTimeAxisView* rtv;
+
+ if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
+
+ boost::shared_ptr<Track> t = rtv->track();
+
+ if (t != 0 && ! t->diskstream()->destructive()) {
+
+ if ((playlist = rtv->playlist()) != 0) {
+ playlists.push_back (playlist);
+ }
+ }
+ }
+ }
+
+ if (playlists.empty()) {
+ return;
+ }
+
+ nframes64_t the_start;
+ nframes64_t the_end;
+ nframes64_t cnt;
+
+ begin_reversible_command (_("trim to selection"));
+
+ for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+
+ boost::shared_ptr<Region> region;
+
+ the_start = start;
+
+ if ((region = (*i)->top_region_at(the_start)) == 0) {
+ continue;
+ }
+
+ /* now adjust lengths to that we do the right thing
+ if the selection extends beyond the region
+ */
+
+ the_start = max (the_start, (nframes64_t) region->position());
+ if (max_frames - the_start < region->length()) {
+ the_end = the_start + region->length() - 1;
+ } else {
+ the_end = max_frames;
+ }
+ the_end = min (end, the_end);
+ cnt = the_end - the_start + 1;
+
+ XMLNode &before = (*i)->get_state();
+ region->trim_to (the_start, cnt, this);
+ XMLNode &after = (*i)->get_state();
+ session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::region_fill_track ()
+{
+ nframes64_t end;
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (!session || rs.empty()) {
+ return;
+ }
+
+ end = session->current_end_frame ();
+
+ begin_reversible_command (_("region fill"));
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+ boost::shared_ptr<Region> region ((*i)->region());
+
+ boost::shared_ptr<Playlist> pl = region->playlist();
+
+ if (end <= region->last_frame()) {
+ return;
+ }
+
+ double times = (double) (end - region->last_frame()) / (double) region->length();
+
+ if (times == 0) {
+ return;
+ }
+
+ XMLNode &before = pl->get_state();
+ pl->add_region (RegionFactory::create (region), region->last_frame(), times);
+ session->add_command (new MementoCommand<Playlist>(*pl, &before, &pl->get_state()));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::region_fill_selection ()
+{
+ if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
+ return;
+ }
+
+ if (selection->time.empty()) {
+ return;
+ }
+
+ boost::shared_ptr<Region> region = _regions->get_single_selection ();
+ if (region == 0) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ boost::shared_ptr<Playlist> playlist;
+
+ if (selection->tracks.empty()) {
+ return;
+ }
+
+ nframes64_t selection_length = end - start;
+ float times = (float)selection_length / region->length();
+
+ begin_reversible_command (_("fill selection"));
+
+ for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+
+ if ((playlist = (*i)->playlist()) == 0) {
+ continue;
+ }
+
+ 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_region_sync_from_edit_point ()
+{
+ nframes64_t where = get_preferred_edit_position ();
+ RegionSelection rs;
+ get_regions_for_action (rs);
+ set_sync_point (where, rs);
+}
+
+void
+Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
+{
+ bool in_command = false;
+
+ for (RegionSelection::const_iterator r = rs.begin(); r != rs.end(); ++r) {
+
+ if (!(*r)->region()->covers (where)) {
+ continue;
+ }
+
+ boost::shared_ptr<Region> region ((*r)->region());
+
+ if (!in_command) {
+ begin_reversible_command (_("set sync point"));
+ in_command = true;
+ }
+
+ XMLNode &before = region->playlist()->get_state();
+ region->set_sync_position (where);
+ XMLNode &after = region->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+ }
+
+ if (in_command) {
+ commit_reversible_command ();
+ }
+}
+
+/** Remove the sync positions of the selection */
+void
+Editor::remove_region_sync ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("remove sync"));
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+ XMLNode &before = (*i)->region()->playlist()->get_state();
+ (*i)->region()->clear_sync_position ();
+ XMLNode &after = (*i)->region()->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(*((*i)->region()->playlist()), &before, &after));
+ }
+ commit_reversible_command ();
+}
+
+void
+Editor::naturalize ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("naturalize"));
+ for (RegionSelection::iterator i = rs.begin(); i != rs.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 ();
+}
+
+void
+Editor::align (RegionPoint what)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+ nframes64_t where = get_preferred_edit_position();
+
+ if (!rs.empty()) {
+ align_selection (what, where, rs);
+ } else {
+
+ RegionSelection rs;
+ get_regions_at (rs, where, selection->tracks);
+ align_selection (what, where, rs);
+ }
+}
+
+void
+Editor::align_relative (RegionPoint what)
+{
+ nframes64_t where = get_preferred_edit_position();
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (!rs.empty()) {
+ align_selection_relative (what, where, rs);
+ }
+}
+
+struct RegionSortByTime {
+ bool operator() (const RegionView* a, const RegionView* b) {
+ return a->region()->position() < b->region()->position();
+ }
+};
+
+void
+Editor::align_selection_relative (RegionPoint point, nframes64_t position, const RegionSelection& rs)
+{
+ if (rs.empty()) {
+ return;
+ }
+
+ nframes64_t distance = 0;
+ nframes64_t pos = 0;
+ int dir = 1;
+
+ list<RegionView*> sorted;
+ rs.by_position (sorted);
+
+ boost::shared_ptr<Region> r ((*sorted.begin())->region());
+
+ switch (point) {
+ case Start:
+ pos = position;
+ if (position > r->position()) {
+ distance = position - r->position();
+ } else {
+ distance = r->position() - position;
+ dir = -1;
+ }
+ break;
+
+ case End:
+ if (position > r->last_frame()) {
+ distance = position - r->last_frame();
+ pos = r->position() + distance;
+ } else {
+ distance = r->last_frame() - position;
+ pos = r->position() - distance;
+ dir = -1;
+ }
+ break;
+
+ case SyncPoint:
+ pos = r->adjust_to_sync (position);
+ if (pos > r->position()) {
+ distance = pos - r->position();
+ } else {
+ distance = r->position() - pos;
+ dir = -1;
+ }
+ break;
+ }
+
+ if (pos == r->position()) {
+ return;
+ }
+
+ begin_reversible_command (_("align selection (relative)"));
+
+ /* move first one specially */
+
+ XMLNode &before = r->playlist()->get_state();
+ r->set_position (pos, this);
+ XMLNode &after = r->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
+
+ /* move rest by the same amount */
+
+ sorted.pop_front();
+
+ for (list<RegionView*>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
+
+ boost::shared_ptr<Region> region ((*i)->region());
+
+ XMLNode &before = region->playlist()->get_state();
+
+ if (dir > 0) {
+ region->set_position (region->position() + distance, this);
+ } else {
+ region->set_position (region->position() - distance, this);
+ }
+
+ XMLNode &after = region->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::align_selection (RegionPoint point, nframes64_t position, const RegionSelection& rs)
+{
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("align selection"));
+
+ for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
+ align_region_internal ((*i)->region(), point, position);
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+{
+ begin_reversible_command (_("align region"));
+ align_region_internal (region, point, position);
+ commit_reversible_command ();
+}
+
+void
+Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+{
+ XMLNode &before = region->playlist()->get_state();
+
+ switch (point) {
+ case SyncPoint:
+ region->set_position (region->adjust_to_sync (position), this);
+ break;
+
+ case End:
+ if (position > region->length()) {
+ region->set_position (position - region->length(), this);
+ }
+ break;
+
+ case Start:
+ region->set_position (position, this);
+ break;
+ }
+
+ XMLNode &after = region->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+}
+
+void
+Editor::trim_region_front ()
+{
+ trim_region (true);
+}
+
+void
+Editor::trim_region_back ()
+{
+ trim_region (false);
+}
+
+void
+Editor::trim_region (bool front)
+{
+ nframes64_t where = get_preferred_edit_position();
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (front ? _("trim front") : _("trim back"));
+
+ for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
+ if (!(*i)->region()->locked()) {
+ boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
+ XMLNode &before = pl->get_state();
+ if (front) {
+ (*i)->region()->trim_front (where, this);
+ } else {
+ (*i)->region()->trim_end (where, this);
+ }
+ XMLNode &after = pl->get_state();
+ session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+ }
+ }
+
+ commit_reversible_command ();
+}
+
+/** Trim the end of the selected regions to the position of the edit cursor */
+void
+Editor::trim_region_to_loop ()
+{
+ Location* loc = session->locations()->auto_loop_location();
+ if (!loc) {
+ return;
+ }
+ trim_region_to_location (*loc, _("trim to loop"));
+}
+
+void
+Editor::trim_region_to_punch ()
+{
+ Location* loc = session->locations()->auto_punch_location();
+ if (!loc) {
+ return;
+ }
+ trim_region_to_location (*loc, _("trim to punch"));
+}
+void
+Editor::trim_region_to_location (const Location& loc, const char* str)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ begin_reversible_command (str);
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ RegionView* rv = (*x);
+
+ /* require region to span proposed trim */
+ switch (rv->region()->coverage (loc.start(), loc.end())) {
+ case OverlapInternal:
+ break;
+ default:
+ continue;
+ }
+
+ RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+ if (!tav) {
+ return;
+ }
+
+ float speed = 1.0;
+ nframes64_t start;
+ nframes64_t end;
+
+ if (tav->get_diskstream() != 0) {
+ speed = tav->get_diskstream()->speed();
+ }
+
+ start = session_frame_to_track_frame (loc.start(), speed);
+ end = session_frame_to_track_frame (loc.end(), speed);
+
+ XMLNode &before = rv->region()->playlist()->get_state();
+ rv->region()->trim_to (start, (end - start), this);
+ XMLNode &after = rv->region()->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(
+ *(rv->region()->playlist()), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::trim_region_to_edit_point ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ nframes64_t where = get_preferred_edit_position();
+
+ begin_reversible_command (_("trim region start to edit point"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ RegionView* rv = (*x);
+
+ /* require region to cover trim */
+ if (!rv->region()->covers (where)) {
+ continue;
+ }
+
+ RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+ if (!tav) {
+ return;
+ }
+
+ float speed = 1.0;
+
+ if (tav->get_diskstream() != 0) {
+ speed = tav->get_diskstream()->speed();
+ }
+
+ XMLNode &before = rv->region()->playlist()->get_state();
+ rv->region()->trim_end( session_frame_to_track_frame(where, speed), this);
+ XMLNode &after = rv->region()->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(
+ *(rv->region()->playlist()), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::trim_region_from_edit_point ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ nframes64_t where = get_preferred_edit_position();
+
+ begin_reversible_command (_("trim region end to edit point"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ RegionView* rv = (*x);
+
+ /* require region to cover trim */
+ if (!rv->region()->covers (where)) {
+ continue;
+ }
+
+ RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+ if (!tav) {
+ return;
+ }
+
+ float speed = 1.0;
+
+ if (tav->get_diskstream() != 0) {
+ speed = tav->get_diskstream()->speed();
+ }
+
+ XMLNode &before = rv->region()->playlist()->get_state();
+ rv->region()->trim_front ( session_frame_to_track_frame(where, speed), this);
+ XMLNode &after = rv->region()->playlist()->get_state();
+ session->add_command(new MementoCommand<Playlist>(
+ *(rv->region()->playlist()), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::trim_region_to_previous_region_end ()
+{
+ return trim_to_region(false);
+}
+
+void
+Editor::trim_region_to_next_region_start ()
+{
+ return trim_to_region(true);
+}
+
+void
+Editor::trim_to_region(bool forward)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ begin_reversible_command (_("trim to region"));
+
+ boost::shared_ptr<Region> next_region;
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+
+ AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!arv) {
+ continue;
+ }
+
+ AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+ if (!atav) {
+ return;
+ }
+
+ float speed = 1.0;
+
+ if (atav->get_diskstream() != 0) {
+ speed = atav->get_diskstream()->speed();
+ }
+
+
+ boost::shared_ptr<Region> region = arv->region();
+ boost::shared_ptr<Playlist> playlist (region->playlist());
+
+ XMLNode &before = playlist->get_state();
+
+ if(forward){
+
+ next_region = playlist->find_next_region (region->first_frame(), Start, 1);
+
+ if(!next_region){
+ continue;
+ }
+
+ region->trim_end((nframes64_t) (next_region->first_frame() * speed), this);
+ arv->region_changed (Change (LengthChanged));
+ }
+ else {
+
+ next_region = playlist->find_next_region (region->first_frame(), Start, 0);
+
+ if(!next_region){
+ continue;
+ }
+
+ region->trim_front((nframes64_t) ((next_region->last_frame() + 1) * speed), this);
+ arv->region_changed (Change (LengthChanged|PositionChanged|StartChanged));
+ }
+
+ XMLNode &after = playlist->get_state();
+ session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::unfreeze_route ()
+{
+ if (clicked_routeview == 0 || !clicked_routeview->is_track()) {
+ return;
+ }
+
+ clicked_routeview->track()->unfreeze ();
+}
+
+void*
+Editor::_freeze_thread (void* arg)
+{
+ PBD::notify_gui_about_thread_creation (pthread_self(), X_("Freeze"));
+ return static_cast<Editor*>(arg)->freeze_thread ();
+}
+
+void*
+Editor::freeze_thread ()
+{
+ clicked_routeview->audio_track()->freeze (*current_interthread_info);
+ current_interthread_info->done = true;
+ return 0;
+}
+
+gint
+Editor::freeze_progress_timeout (void */*arg*/)
+{
+ interthread_progress_bar.set_fraction (current_interthread_info->progress);
+ return !(current_interthread_info->done || current_interthread_info->cancel);
+}
+
+void
+Editor::freeze_route ()
+{
+ if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
+ return;
+ }
+
+ InterThreadInfo itt;
+
+ if (interthread_progress_window == 0) {
+ build_interthread_progress_window ();
+ }
+
+ interthread_progress_window->set_title (_("Freeze"));
+ interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
+ interthread_progress_window->show_all ();
+ interthread_progress_bar.set_fraction (0.0f);
+ interthread_progress_label.set_text ("");
+ interthread_cancel_label.set_text (_("Cancel Freeze"));
+ current_interthread_info = &itt;
+
+ interthread_progress_connection =
+ Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::freeze_progress_timeout), (gpointer) 0), 100);
+
+ itt.done = false;
+ itt.cancel = false;
+ itt.progress = 0.0f;
+
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 500000);
+
+ pthread_create_and_store (X_("freezer"), &itt.thread, &attr, _freeze_thread, this);
+
+ pthread_attr_destroy(&attr);
+
+ track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
+
+ while (!itt.done && !itt.cancel) {
+ gtk_main_iteration ();
+ }
+
+ interthread_progress_connection.disconnect ();
+ interthread_progress_window->hide_all ();
+ current_interthread_info = 0;
+ track_canvas->get_window()->set_cursor (*current_canvas_cursor);
+}
+
+void
+Editor::bounce_range_selection (bool replace, bool enable_processing)
+{
+ if (selection->time.empty()) {
+ return;
+ }
+
+ TrackSelection views = selection->tracks;
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+ nframes64_t cnt = end - start + 1;
+
+ begin_reversible_command (_("bounce range"));
+
+ for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
+
+ RouteTimeAxisView* rtv;
+
+ if ((rtv = dynamic_cast<RouteTimeAxisView*> (*i)) == 0) {
+ continue;
+ }
+
+ boost::shared_ptr<Playlist> playlist;
+
+ if ((playlist = rtv->playlist()) == 0) {
+ return;
+ }
+
+ InterThreadInfo itt;
+
+ itt.done = false;
+ itt.cancel = false;
+ itt.progress = false;
+
+ XMLNode &before = playlist->get_state();
+ boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
+
+ if (replace) {
+ list<AudioRange> ranges;
+ ranges.push_back (AudioRange (start, start+cnt, 0));
+ playlist->cut (ranges); // discard result
+ playlist->add_region (r, start);
+ }
+
+ XMLNode &after = playlist->get_state();
+ session->add_command (new MementoCommand<Playlist> (*playlist, &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+/** Cut selected regions, automation points or a time range */
+void
+Editor::cut ()
+{
+ cut_copy (Cut);
+}
+
+/** Copy selected regions, automation points or a time range */
+void
+Editor::copy ()
+{
+ cut_copy (Copy);
+}
+
+
+/** @return true if a Cut, Copy or Clear is possible */
+bool
+Editor::can_cut_copy () const
+{
+ switch (current_mouse_mode()) {
+
+ case MouseObject:
+ if (!selection->regions.empty() || !selection->points.empty()) {
+ return true;
+ }
+ break;
+
+ case MouseRange:
+ if (!selection->time.empty()) {
+ return true;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return false;
+}
+
+
+/** Cut, copy or clear selected regions, automation points or a time range.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy (CutCopyOp op)
+{
+ /* only cancel selection if cut/copy is successful.*/
+
+ string opname;
+
+ switch (op) {
+ case Cut:
+ opname = _("cut");
+ break;
+ case Copy:
+ opname = _("copy");
+ break;
+ case Clear:
+ opname = _("clear");
+ break;
+ }
+
+ /* if we're deleting something, and the mouse is still pressed,
+ the thing we started a drag for will be gone when we release
+ the mouse button(s). avoid this. see part 2 at the end of
+ this function.
+ */
+
+ if (op == Cut || op == Clear) {
+ if (_drag) {
+ _drag->item()->ungrab (0);
+ delete _drag;
+ _drag = 0;
+ }
+ }
+
+ cut_buffer->clear ();
+
+ if (entered_marker) {
+
+ /* cut/delete op while pointing at a marker */
+
+ bool ignored;
+ Location* loc = find_location_from_marker (entered_marker, ignored);
+
+ if (session && loc) {
+ Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
+ }
+
+ break_drag ();
+ delete _drag;
+ _drag = 0;
+
+ return;
+ }
+
+ if (internal_editing()) {
+
+ switch (current_mouse_mode()) {
+ case MouseObject:
+ case MouseRange:
+ cut_copy_midi (op);
+ break;
+ default:
+ break;
+ }
+
+ } else {
+
+ RegionSelection rs;
+
+ /* we only want to cut regions if some are selected */
+
+ if (!selection->regions.empty()) {
+ get_regions_for_action (rs, false, false);
+ }
+
+ switch (current_mouse_mode()) {
+ case MouseObject:
+ if (!rs.empty() || !selection->points.empty()) {
+
+ begin_reversible_command (opname + _(" objects"));
+
+ if (!rs.empty()) {
+ cut_copy_regions (op, rs);
+
+ if (op == Cut) {
+ selection->clear_regions ();
+ }
+ }
+
+ if (!selection->points.empty()) {
+ cut_copy_points (op);
+
+ if (op == Cut) {
+ selection->clear_points ();
+ }
+ }
+
+ commit_reversible_command ();
+ break; // terminate case statement here
+ }
+ if (!selection->time.empty()) {
+ /* don't cause suprises */
+ break;
+ }
+ // fall thru if there was nothing selected
+
+ case MouseRange:
+ if (selection->time.empty()) {
+ nframes64_t start, end;
+ if (!get_edit_op_range (start, end)) {
+ return;
+ }
+ selection->set ((TimeAxisView*) 0, start, end);
+ }
+
+ begin_reversible_command (opname + _(" range"));
+ cut_copy_ranges (op);
+ commit_reversible_command ();
+
+ if (op == Cut) {
+ selection->clear_time ();
+ }
+
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ if (op == Cut || op == Clear) {
+ break_drag ();
+ delete _drag;
+ _drag = 0;
+ }
+}
+
+/** Cut, copy or clear selected automation points.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_points (CutCopyOp op)
+{
+ for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
+
+ AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
+
+ if (atv) {
+ atv->cut_copy_clear_objects (selection->points, op);
+ }
+ }
+}
+
+/** Cut, copy or clear selected automation points.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_midi (CutCopyOp op)
+{
+ for (MidiRegionSelection::iterator i = selection->midi_regions.begin(); i != selection->midi_regions.end(); ++i) {
+ MidiRegionView* mrv = *i;
+ mrv->cut_copy_clear (op);
+ }
+}
+
+struct PlaylistState {
+ boost::shared_ptr<Playlist> playlist;
+ XMLNode* before;
+};
+
+struct lt_playlist {
+ bool operator () (const PlaylistState& a, const PlaylistState& b) {
+ return a.playlist < b.playlist;
+ }
+};
+
+struct PlaylistMapping {
+ TimeAxisView* tv;
+ boost::shared_ptr<Playlist> pl;
+
+ PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
+};
+
+/** Remove `clicked_regionview' */
+void
+Editor::remove_clicked_region ()
+{
+ if (clicked_routeview == 0 || clicked_regionview == 0) {
+ return;
+ }
+
+ boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
+
+ begin_reversible_command (_("remove region"));
+ XMLNode &before = playlist->get_state();
+ playlist->remove_region (clicked_regionview->region());
+ XMLNode &after = playlist->get_state();
+ session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+ commit_reversible_command ();
+}
+
+
+/** Remove the selected regions */
+void
+Editor::remove_selected_regions ()
+{
+ RegionSelection rs;
+ get_regions_for_action (rs);
+
+ if (!session) {
+ return;
+ }
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("remove region"));
+
+ list<boost::shared_ptr<Region> > regions_to_remove;
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ // we can't just remove the region(s) in this loop because
+ // this removes them from the RegionSelection, and they thus
+ // disappear from underneath the iterator, and the ++i above
+ // SEGVs in a puzzling fashion.
+
+ // so, first iterate over the regions to be removed from rs and
+ // add them to the regions_to_remove list, and then
+ // iterate over the list to actually remove them.
+
+ regions_to_remove.push_back ((*i)->region());
+ }
+
+ vector<PlaylistState> playlists;
+
+ for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
+
+ boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
+
+ if (!playlist) {
+ // is this check necessary?
+ continue;
+ }
+
+ vector<PlaylistState>::iterator i;
+
+ //only take state if this is a new playlist.
+ for (i = playlists.begin(); i != playlists.end(); ++i) {
+ if ((*i).playlist == playlist) {
+ break;
+ }
+ }
+
+ if (i == playlists.end()) {
+
+ PlaylistState before;
+ before.playlist = playlist;
+ before.before = &playlist->get_state();
+
+ playlist->freeze ();
+ playlists.push_back(before);
+ }
+
+ playlist->remove_region (*rl);
+ }
+
+ vector<PlaylistState>::iterator pl;
+
+ for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
+ (*pl).playlist->thaw ();
+ session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+ }
+
+ commit_reversible_command ();
+}
+
+/** Cut, copy or clear selected regions.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
+{
+ /* we can't use a std::map here because the ordering is important, and we can't trivially sort
+ a map when we want ordered access to both elements. i think.
+ */
+
+ vector<PlaylistMapping> pmap;
+
+ nframes64_t first_position = max_frames;
+
+ set<PlaylistState, lt_playlist> freezelist;
+ pair<set<PlaylistState, lt_playlist>::iterator,bool> insert_result;
+
+ /* get ordering correct before we cut/copy */
+
+ rs.sort_by_position_and_track ();
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+
+ first_position = min ((nframes64_t) (*x)->region()->position(), first_position);
+
+ if (op == Cut || op == Clear) {
+ boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
+
+ if (pl) {
+ set<PlaylistState, lt_playlist>::iterator fl;
+
+ //only take state if this is a new playlist.
+ for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
+ if ((*fl).playlist == pl) {
+ break;
+ }
+ }
+
+ if (fl == freezelist.end()) {
+ PlaylistState before;
+ before.playlist = pl;
+ before.before = &pl->get_state();
+ pl->freeze ();
+ insert_result = freezelist.insert (before);
+ }
+ }
+ }
+
+ TimeAxisView* tv = &(*x)->get_trackview();
+ vector<PlaylistMapping>::iterator z;
+
+ for (z = pmap.begin(); z != pmap.end(); ++z) {
+ if ((*z).tv == tv) {
+ break;
+ }
+ }
+
+ if (z == pmap.end()) {
+ pmap.push_back (PlaylistMapping (tv));
+ }
+ }
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ) {
+
+ boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
+
+ if (!pl) {
+ /* impossible, but this handles it for the future */
+ continue;
+ }
+
+ TimeAxisView& tv = (*x)->get_trackview();
+ boost::shared_ptr<Playlist> npl;
+ RegionSelection::iterator tmp;
+
+ tmp = x;
+ ++tmp;
+
+ vector<PlaylistMapping>::iterator z;
+
+ for (z = pmap.begin(); z != pmap.end(); ++z) {
+ if ((*z).tv == &tv) {
+ break;
+ }
+ }
+
+ assert (z != pmap.end());
+
+ if (!(*z).pl) {
+ npl = PlaylistFactory::create (pl->data_type(), *session, "cutlist", true);
+ npl->freeze();
+ (*z).pl = npl;
+ } else {
+ npl = (*z).pl;
+ }
+
+ boost::shared_ptr<Region> r = (*x)->region();
+ boost::shared_ptr<Region> _xx;
+
+ assert (r != 0);
+
+ switch (op) {
+ case Cut:
+ _xx = RegionFactory::create (r);
+ npl->add_region (_xx, r->position() - first_position);
+ pl->remove_region (r);
+ break;
+
+ case Copy:
+ /* copy region before adding, so we're not putting same object into two different playlists */
+ npl->add_region (RegionFactory::create (r), r->position() - first_position);
+ break;
+
+ case Clear:
+ pl->remove_region (r);
+ break;
+ }
+
+ x = tmp;
+ }
+
+ list<boost::shared_ptr<Playlist> > foo;
+
+ /* the pmap is in the same order as the tracks in which selected regions occured */
+
+ for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
+ (*i).pl->thaw();
+ foo.push_back ((*i).pl);
+ }
+
+
+ if (!foo.empty()) {
+ cut_buffer->set (foo);
+ }
+
+ for (set<PlaylistState, lt_playlist>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
+ (*pl).playlist->thaw ();
+ session->add_command (new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+ }
+}
+
+void
+Editor::cut_copy_ranges (CutCopyOp op)
+{
+ TrackSelection* ts;
+ TrackSelection entered;
+
+ if (selection->tracks.empty()) {
+ if (!entered_track) {
+ return;
+ }
+ entered.push_back (entered_track);
+ ts = &entered;
+ } else {
+ ts = &selection->tracks;
+ }
+
+ for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+ (*i)->cut_copy_clear (*selection, op);
+ }
+}
+
+void
+Editor::paste (float times)
+{
+ paste_internal (get_preferred_edit_position(), times);
+}
+
+void
+Editor::mouse_paste ()
+{
+ nframes64_t where;
+ bool ignored;
+
+ if (!mouse_frame (where, ignored)) {
+ return;
+ }
+
+ snap_to (where);
+ paste_internal (where, 1);
+}
+
+void
+Editor::paste_internal (nframes64_t position, float times)
+{
+ bool commit = false;
+
+ if (internal_editing()) {
+ if (cut_buffer->midi_notes.empty()) {
+ return;
+ }
+ } else {
+ if (cut_buffer->empty()) {
+ return;
+ }
+ }
+
+ if (position == max_frames) {
+ position = get_preferred_edit_position();
+ }
+
+ begin_reversible_command (_("paste"));
+
+ TrackSelection ts;
+ TrackSelection::iterator i;
+ size_t nth;
+
+ /* get everything in the correct order */
+
+ if (!selection->tracks.empty()) {
+ sort_track_selection ();
+ ts = selection->tracks;
+ } else if (entered_track) {
+ ts.push_back (entered_track);
+ }
+
+ for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
+
+ /* undo/redo is handled by individual tracks/regions */
+
+ if (internal_editing()) {
+
+ RegionSelection rs;
+ RegionSelection::iterator r;
+ MidiNoteSelection::iterator cb;
+
+ get_regions_at (rs, position, ts);
+
+ for (cb = cut_buffer->midi_notes.begin(), r = rs.begin();
+ cb != cut_buffer->midi_notes.end() && r != rs.end(); ++r) {
+ MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*r);
+ if (mrv) {
+ mrv->paste (position, times, **cb);
+ ++cb;
+ }
+ }
+
+ } else {
+
+ if ((*i)->paste (position, times, *cut_buffer, nth)) {
+ commit = true;
+ }
+ }
+ }
+
+ if (commit) {
+ commit_reversible_command ();
+ }
+}
+
+void
+Editor::duplicate_some_regions (RegionSelection& regions, float times)
+{
+ boost::shared_ptr<Playlist> playlist;
+ RegionSelection sel = regions; // clear (below) may clear the argument list if its the current region selection
+ RegionSelection foo;
+
+ begin_reversible_command (_("duplicate region"));
+
+ selection->clear_regions ();
+
+ for (RegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
+
+ boost::shared_ptr<Region> r ((*i)->region());
+
+ TimeAxisView& tv = (*i)->get_time_axis_view();
+ RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
+ latest_regionviews.clear ();
+ sigc::connection c = rtv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+
+ 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()));