Fix "Crop Region to Range" -- second attempt.
[ardour.git] / gtk2_ardour / editor_ops.cc
index ed364367bcb197a5a740e456374be4cf1035c37c..7402d231471d555bda48647b2bd2e5ff1dd1b607 100644 (file)
@@ -3256,9 +3256,10 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
                return;
        }
 
-       framepos_t the_start;
-       framepos_t the_end;
-       framepos_t cnt;
+       framepos_t pos;
+       framepos_t new_start;
+       framepos_t new_end;
+       framecnt_t new_length;
        bool in_command = false;
 
        for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
@@ -3267,7 +3268,7 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
                boost::shared_ptr<Region> region_at_start = (*i)->top_region_at(start);
                boost::shared_ptr<Region> region_at_end = (*i)->top_region_at(end);
 
-               vector<boost::shared_ptr<Region>> regions;
+               vector<boost::shared_ptr<Region> > regions;
 
                if (region_at_start != 0) {
                        regions.push_back (region_at_start);
@@ -3277,23 +3278,24 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
                }
 
                /* now adjust lengths */
-               for (vector<boost::shared_ptr<Region>>::iterator i = regions.begin(); i != regions.end(); ++i) {
+               for (vector<boost::shared_ptr<Region> >::iterator i = regions.begin(); i != regions.end(); ++i) {
 
-                       the_start = max (start, (framepos_t) (*i)->position());
-                       if (max_framepos - the_start < (*i)->length()) {
-                               the_end = the_start + (*i)->length() - 1;
+                       pos = (*i)->position();
+                       new_start = max (start, pos);
+                       if (max_framepos - pos > (*i)->length()) {
+                               new_end = pos + (*i)->length() - 1;
                        } else {
-                               the_end = max_framepos;
+                               new_end = max_framepos;
                        }
-                       the_end = min (end, the_end);
-                       cnt = the_end - the_start + 1;
+                       new_end = min (end, new_end);
+                       new_length = new_end - new_start + 1;
 
                        if(!in_command) {
                                begin_reversible_command (_("trim to selection"));
                                in_command = true;
                        }
                        (*i)->clear_changes ();
-                       (*i)->trim_to (the_start, cnt);
+                       (*i)->trim_to (new_start, new_length);
                        _session->add_command (new StatefulDiffCommand (*i));
                }
        }
@@ -3306,56 +3308,49 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
 void
 Editor::region_fill_track ()
 {
-       RegionSelection rs = get_regions_from_selection_and_entered ();
-
-       if (!_session || rs.empty()) {
-               return;
-       }
+       boost::shared_ptr<Playlist> playlist;
+       RegionSelection regions = get_regions_from_selection_and_entered ();
+       RegionSelection foo;
 
        framepos_t const end = _session->current_end_frame ();
-       RegionSelection foo;
-       bool in_command = false;
 
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+       if (regions.empty () || regions.end_frame () + 1 >= end) {
+               return;
+       }
 
-               boost::shared_ptr<Region> region ((*i)->region());
+       framepos_t const start_frame = regions.start ();
+       framepos_t const end_frame = regions.end_frame ();
+       framecnt_t const gap = end_frame - start_frame + 1;
 
-               boost::shared_ptr<Playlist> pl = region->playlist();
+       begin_reversible_command (Operations::region_fill);
 
-               if (end <= region->last_frame()) {
-                       continue;
-               }
+       selection->clear_regions ();
 
-               double times = (double) (end - region->last_frame()) / (double) region->length();
+       for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
 
-               if (times == 0) {
-                       continue;
-               }
+               boost::shared_ptr<Region> r ((*i)->region());
 
-               if (!in_command) {
-                       begin_reversible_command (Operations::region_fill);
-                       in_command = true;
-               }
                TimeAxisView& tv = (*i)->get_time_axis_view();
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
                latest_regionviews.clear ();
                sigc::connection c = rtv->view()->RegionViewAdded.connect (sigc::mem_fun(*this, &Editor::collect_new_region_view));
 
-               pl->clear_changes ();
-               pl->add_region (RegionFactory::create (region, true), region->last_frame(), times);
-               _session->add_command (new StatefulDiffCommand (pl));
+               framepos_t const position = end_frame + (r->first_frame() - start_frame + 1);
+               playlist = (*i)->region()->playlist();
+               playlist->clear_changes ();
+               playlist->duplicate_until (r, position, gap, end);
+               _session->add_command(new StatefulDiffCommand (playlist));
 
                c.disconnect ();
 
                foo.insert (foo.end(), latest_regionviews.begin(), latest_regionviews.end());
        }
 
-       if (in_command) {
-               if (!foo.empty()) {
-                       selection->set (foo);
-               }
-               commit_reversible_command ();
+       if (!foo.empty()) {
+               selection->set (foo);
        }
+
+       commit_reversible_command ();
 }
 
 void