change the implementation of lower_region_to_bottom() and raise_region_to_top() to...
[ardour.git] / libs / ardour / playlist.cc
index 3aa14a055850f42ec09c6494881b502a569133bc..b8dc905e84e170ac5e601c7a026873a39d589b3e 100644 (file)
@@ -39,6 +39,8 @@
 #include <ardour/playlist_factory.h>
 #include <ardour/transient_detector.h>
 
+#include <boost/lexical_cast.hpp>
+
 #include "i18n.h"
 
 using namespace std;
@@ -78,7 +80,7 @@ Playlist::Playlist (Session& sess, string nom, bool hide)
        init (hide);
        first_set_state = false;
        _name = nom;
-       
+       _set_sort_id();
 }
 
 Playlist::Playlist (Session& sess, const XMLNode& node, bool hide)
@@ -86,6 +88,7 @@ Playlist::Playlist (Session& sess, const XMLNode& node, bool hide)
 {
        init (hide);
        _name = "unnamed"; /* reset by set_state */
+       _set_sort_id();
 
        /* set state called by derived class */
 }
@@ -269,6 +272,35 @@ Playlist::~Playlist ()
        /* GoingAway must be emitted by derived classes */
 }
 
+void
+Playlist::_set_sort_id ()
+{
+    /* 
+        Playlists are given names like <track name>.<id> 
+        or <track name>.<edit group name>.<id> where id 
+        is an integer. We extract the id and sort by that.
+    */
+
+    size_t dot_position = _name.find_last_of(".");
+    if (dot_position == string::npos)
+    {
+        _sort_id = 0;
+    }
+    else
+    {
+        string t = _name.substr(dot_position + 1);
+        
+        try
+        {
+            _sort_id = boost::lexical_cast<int>(t);
+        }
+        catch (boost::bad_lexical_cast e)
+        {
+            _sort_id = 0;
+        }
+    }
+}    
+
 void
 Playlist::set_name (string str)
 {
@@ -282,7 +314,19 @@ Playlist::set_name (string str)
                return;
        }
 
-       _name = str; 
+       if (str == _name) {
+               return;
+       }
+
+       string name = str;
+
+       while (_session.playlist_by_name(name) != 0) {
+               name = bump_name_once(name);
+       }
+
+       _name = name; 
+       _set_sort_id();
+       
        NameChanged(); /* EMIT SIGNAL */
 }
 
@@ -324,7 +368,6 @@ Playlist::release_notifications ()
        } 
 }
 
-
 void
 Playlist::notify_modified ()
 {
@@ -659,53 +702,64 @@ Playlist::partition (nframes_t start, nframes_t end, bool just_top_level)
 void
 Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, RegionList& thawlist)
 {
-       RegionLock rlock (this);
-       boost::shared_ptr<Region> region;
-       boost::shared_ptr<Region> current;
-       string new_name;
-       RegionList::iterator tmp;
-       OverlapType overlap;
-       nframes_t pos1, pos2, pos3, pos4;
        RegionList new_regions;
 
-       in_partition = true;
-
-       /* need to work from a copy, because otherwise the regions we add during the process
-          get operated on as well.
-       */
-
-       RegionList copy = regions;
-
-       for (RegionList::iterator i = copy.begin(); i != copy.end(); i = tmp) {
+       {
+               RegionLock rlock (this);
+               boost::shared_ptr<Region> region;
+               boost::shared_ptr<Region> current;
+               string new_name;
+               RegionList::iterator tmp;
+               OverlapType overlap;
+               nframes_t pos1, pos2, pos3, pos4;
                
-               tmp = i;
-               ++tmp;
-
-               current = *i;
+               in_partition = true;
                
-               if (current->first_frame() == start && current->last_frame() == end) {
-                       if (cutting) {
-                               remove_region_internal (current);
-                       }
-                       continue;
-               }
+               /* need to work from a copy, because otherwise the regions we add during the process
+                  get operated on as well.
+               */
                
-               if ((overlap = current->coverage (start, end)) == OverlapNone) {
-                       continue;
-               }
+               RegionList copy = regions;
                
-               pos1 = current->position();
-               pos2 = start;
-               pos3 = end;
-               pos4 = current->last_frame();
+               for (RegionList::iterator i = copy.begin(); i != copy.end(); i = tmp) {
+                       
+                       tmp = i;
+                       ++tmp;
+                       
+                       current = *i;
 
-               if (overlap == OverlapInternal) {
+                       if (current->first_frame() >= start && current->last_frame() < end) {
+                               if (cutting) {
+                                       remove_region_internal (current);
+                               }
+                               continue;
+                       }
                        
-                       /* split: we need 3 new regions, the front, middle and end.
-                          cut:   we need 2 regions, the front and end.
+                       /* coverage will return OverlapStart if the start coincides
+                          with the end point. we do not partition such a region,
+                          so catch this special case.
                        */
+
+                       if (current->first_frame() >= end) {
+                               continue;
+                       }
+
+                       if ((overlap = current->coverage (start, end)) == OverlapNone) {
+                               continue;
+                       }
+
+                       pos1 = current->position();
+                       pos2 = start;
+                       pos3 = end;
+                       pos4 = current->last_frame();
                        
-                       /*
+                       if (overlap == OverlapInternal) {
+                       
+                               /* split: we need 3 new regions, the front, middle and end.
+                                  cut:   we need 2 regions, the front and end.
+                               */
+                               
+                               /*
                                         start                 end
                          ---------------*************************------------
                                         P1  P2              P3  P4
@@ -714,37 +768,37 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                          CUT
                          ---------------*****----------------====------------
                          
-                       */
+                               */
 
-                       if (!cutting) {
+                               if (!cutting) {
                                
-                               /* "middle" ++++++ */
+                                       /* "middle" ++++++ */
+                                       
+                                       _session.region_name (new_name, current->name(), false);
+                                       region = RegionFactory::create (current, pos2 - pos1, pos3 - pos2, new_name,
+                                                                       regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit|Region::RightOfSplit));
+                                       add_region_internal (region, start);
+                                       new_regions.push_back (region);
+                               }
                                
+                               /* "end" ====== */
+                       
                                _session.region_name (new_name, current->name(), false);
-                               region = RegionFactory::create (current, pos2 - pos1, pos3 - pos2, new_name,
-                                                      regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit|Region::RightOfSplit));
-                               add_region_internal (region, start);
+                               region = RegionFactory::create (current, pos3 - pos1, pos4 - pos3, new_name, 
+                                                               regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
+                               
+                               add_region_internal (region, end);
                                new_regions.push_back (region);
-                       }
-                       
-                       /* "end" ====== */
+                               
+                               /* "front" ***** */
                        
-                       _session.region_name (new_name, current->name(), false);
-                       region = RegionFactory::create (current, pos3 - pos1, pos4 - pos3, new_name, 
-                                              regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
+                               current->freeze ();
+                               thawlist.push_back (current);
+                               current->cut_end (pos2, this);
 
-                       add_region_internal (region, end);
-                       new_regions.push_back (region);
-
-                       /* "front" ***** */
+                       } else if (overlap == OverlapEnd) {
                                
-                       current->freeze ();
-                       thawlist.push_back (current);
-                       current->trim_end (pos2, this);
-
-               } else if (overlap == OverlapEnd) {
-
-                       /*
+                               /*
                                                              start           end
                                    ---------------*************************------------
                                                   P1           P2         P4   P3
@@ -752,33 +806,32 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                                    ---------------**************+++++++++++------------
                                     CUT:                                                  
                                    ---------------**************-----------------------
-
-                       */
-
-                       if (!cutting) {
+                               */
                                
-                               /* end +++++ */
+                               if (!cutting) {
+                                       
+                                       /* end +++++ */
+                                       
+                                       _session.region_name (new_name, current->name(), false);
+                                       region = RegionFactory::create (current, pos2 - pos1, pos4 - pos2, new_name, (layer_t) regions.size(),
+                                                                       Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit));
+                                       add_region_internal (region, start);
+                                       new_regions.push_back (region);
+                               }
                                
-                               _session.region_name (new_name, current->name(), false);
-                               region = RegionFactory::create (current, pos2 - pos1, pos4 - pos2, new_name, (layer_t) regions.size(),
-                                                      Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit));
-                               add_region_internal (region, start);
-                               new_regions.push_back (region);
-                       }
-
-                       /* front ****** */
-
-                       current->freeze ();
-                       thawlist.push_back (current);
-                       current->trim_end (pos2, this);
-
-               } else if (overlap == OverlapStart) {
-
-                       /* split: we need 2 regions: the front and the end.
-                          cut: just trim current to skip the cut area
-                       */
+                               /* front ****** */
+                               
+                               current->freeze ();
+                               thawlist.push_back (current);
+                               current->cut_end (pos2, this);
+                               
+                       } else if (overlap == OverlapStart) {
                                
-                       /*
+                               /* split: we need 2 regions: the front and the end.
+                                  cut: just trim current to skip the cut area
+                               */
+                               
+                               /*
                                                        start           end
                                    ---------------*************************------------
                                       P2          P1 P3                   P4          
@@ -788,31 +841,31 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                                    CUT:
                                    -------------------*********************------------
                                    
-                       */
+                               */
 
-                       if (!cutting) {
+                               if (!cutting) {
                                
-                               /* front **** */
-                                _session.region_name (new_name, current->name(), false);
-                                region = RegionFactory::create (current, 0, pos3 - pos1, new_name,
-                                                       regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
-                                add_region_internal (region, pos1);
-                                new_regions.push_back (region);
-                       } 
-                       
-                       /* end */
-                       
-                       current->freeze ();
-                       thawlist.push_back (current);
-                       current->trim_front (pos3, this);
-
-               } else if (overlap == OverlapExternal) {
-
-                       /* split: no split required.
-                          cut: remove the region.
-                       */
+                                       /* front **** */
+                                       _session.region_name (new_name, current->name(), false);
+                                       region = RegionFactory::create (current, 0, pos3 - pos1, new_name,
+                                                                       regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
+                                       add_region_internal (region, pos1);
+                                       new_regions.push_back (region);
+                               } 
                                
-                       /*
+                               /* end */
+                               
+                               current->freeze ();
+                               thawlist.push_back (current);
+                               current->trim_front (pos3, this);
+                               
+                       } else if (overlap == OverlapExternal) {
+                               
+                               /* split: no split required.
+                                  cut: remove the region.
+                               */
+                               
+                               /*
                                       start                                      end
                                    ---------------*************************------------
                                       P2          P1 P3                   P4          
@@ -822,17 +875,18 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                                    CUT:
                                    ----------------------------------------------------
                                    
-                       */
-
-                       if (cutting) {
-                               remove_region_internal (current);
+                               */
+                               
+                               if (cutting) {
+                                       remove_region_internal (current);
+                               }
+                               new_regions.push_back (current);
                        }
-                       new_regions.push_back (current);
                }
+               
+               in_partition = false;
        }
 
-       in_partition = false;
-
        for (RegionList::iterator i = new_regions.begin(); i != new_regions.end(); ++i) {
                check_dependents (*i, false);
        }
@@ -993,11 +1047,68 @@ Playlist::duplicate (boost::shared_ptr<Region> region, nframes_t position, float
        }
 }
 
+void
+Playlist::shift (nframes64_t at, nframes64_t distance, bool move_intersected, bool ignore_music_glue)
+{
+       RegionLock rlock (this);
+       RegionList copy (regions);
+       RegionList fixup;
+
+       for (RegionList::iterator r = copy.begin(); r != copy.end(); ++r) {
+
+               if ((*r)->last_frame() < at) {
+                       /* too early */
+                       continue;
+               }
+               
+               if (at > (*r)->first_frame() && at < (*r)->last_frame()) {
+                       /* intersected region */
+                       if (!move_intersected) {
+                               continue;
+                       }
+               }
+               
+               /* do not move regions glued to music time - that
+                  has to be done separately.
+               */
+
+               if (!ignore_music_glue && (*r)->positional_lock_style() != Region::AudioTime) {
+                       fixup.push_back (*r);
+                       continue;
+               }
+
+               (*r)->set_position ((*r)->position() + distance, this);
+       }
+
+       for (RegionList::iterator r = fixup.begin(); r != fixup.end(); ++r) {
+               (*r)->recompute_position_from_lock_style ();
+       }
+}
+
+void
+Playlist::split (nframes64_t at)
+{
+       RegionLock rlock (this);
+       RegionList copy (regions);
+
+       /* use a copy since this operation can modify the region list
+        */
+
+       for (RegionList::iterator r = copy.begin(); r != copy.end(); ++r) {
+               _split_region (*r, at);
+       }
+}
+
 void
 Playlist::split_region (boost::shared_ptr<Region> region, nframes_t playlist_position)
 {
        RegionLock rl (this);
+       _split_region (region, playlist_position);
+}
 
+void
+Playlist::_split_region (boost::shared_ptr<Region> region, nframes_t playlist_position)
+{
        if (!region->covers (playlist_position)) {
                return;
        }
@@ -1494,7 +1605,8 @@ Playlist::find_next_region (nframes_t frame, RegionPoint point, int dir)
                        pos = r->last_frame ();
                        break;
                case SyncPoint:
-                       pos = r->adjust_to_sync (r->first_frame());
+                       pos = r->sync_position ();
+                       // r->adjust_to_sync (r->first_frame());
                        break;
                }
 
@@ -1652,10 +1764,11 @@ Playlist::set_state (const XMLNode& node)
                
                if (prop->name() == X_("name")) {
                        _name = prop->value();
+                       _set_sort_id();
                } else if (prop->name() == X_("orig_diskstream_id")) {
                        _orig_diskstream_id = prop->value ();
                } else if (prop->name() == X_("frozen")) {
-                       _frozen = (prop->value() == X_("yes"));
+                       _frozen = string_is_affirmative (prop->value());
                }
        }
 
@@ -1904,22 +2017,44 @@ void
 Playlist::raise_region_to_top (boost::shared_ptr<Region> region)
 {
        /* does nothing useful if layering mode is later=higher */
-       if ((Config->get_layer_model() == MoveAddHigher) ||
-           (Config->get_layer_model() == AddHigher)) {
-               timestamp_layer_op (region);
-               relayer ();
+       switch (Config->get_layer_model()) {
+       case LaterHigher:
+               return;
        }
+
+       RegionList::size_type sz = regions.size();
+
+       if (region->layer() >= (sz - 1)) {
+               /* already on the top */
+               return;
+       }
+
+       move_region_to_layer (sz, region, 1);
+       /* mark the region's last_layer_op as now, so that it remains on top when
+          doing future relayers (until something else takes over)
+        */
+       timestamp_layer_op (region);
 }
 
 void
 Playlist::lower_region_to_bottom (boost::shared_ptr<Region> region)
 {
        /* does nothing useful if layering mode is later=higher */
-       if ((Config->get_layer_model() == MoveAddHigher) ||
-           (Config->get_layer_model() == AddHigher)) {
-               region->set_last_layer_op (0);
-               relayer ();
+       switch (Config->get_layer_model()) {
+       case LaterHigher:
+               return;
        }
+
+       if (region->layer() == 0) {
+               /* already on the bottom */
+               return;
+       }
+
+       move_region_to_layer (0, region, -1);
+       /* force region's last layer op to zero so that it stays at the bottom
+          when doing future relayers
+       */
+       region->set_last_layer_op (0);
 }
 
 int
@@ -2100,8 +2235,6 @@ Playlist::set_frozen (bool yn)
 void
 Playlist::timestamp_layer_op (boost::shared_ptr<Region> region)
 {
-//     struct timeval tv;
-//     gettimeofday (&tv, 0);
        region->set_last_layer_op (++layer_op_counter);
 }