change the implementation of lower_region_to_bottom() and raise_region_to_top() to...
[ardour.git] / libs / ardour / playlist.cc
index b54963cf3c513985a92a8352fb162b15b9c6c1fb..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)
 {
@@ -293,6 +325,8 @@ Playlist::set_name (string str)
        }
 
        _name = name; 
+       _set_sort_id();
+       
        NameChanged(); /* EMIT SIGNAL */
 }
 
@@ -757,11 +791,11 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                                new_regions.push_back (region);
                                
                                /* "front" ***** */
-                               
+                       
                                current->freeze ();
                                thawlist.push_back (current);
-                               current->trim_end (pos2, this);
-                               
+                               current->cut_end (pos2, this);
+
                        } else if (overlap == OverlapEnd) {
                                
                                /*
@@ -789,7 +823,7 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                                
                                current->freeze ();
                                thawlist.push_back (current);
-                               current->trim_end (pos2, this);
+                               current->cut_end (pos2, this);
                                
                        } else if (overlap == OverlapStart) {
                                
@@ -850,10 +884,6 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                        }
                }
                
-               if (current->first_frame() >= current->last_frame()) {
-                       PBD::stacktrace (cerr);
-               }
-
                in_partition = false;
        }
 
@@ -1017,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;
        }
@@ -1518,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;
                }
 
@@ -1676,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());
                }
        }
 
@@ -1928,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
@@ -2124,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);
 }