display results of a bounce in the region list
[ardour.git] / libs / ardour / region.cc
index 8c27a3bebc699732449943c0cf7bf9d6a959a0b9..a13ddd77cdd85589d89438c408d43757f1fcf8b3 100644 (file)
@@ -32,6 +32,7 @@
 #include <ardour/region.h>
 #include <ardour/playlist.h>
 #include <ardour/session.h>
+#include <ardour/region_factory.h>
 
 #include "i18n.h"
 
@@ -39,21 +40,18 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-Change Region::FadeChanged = ARDOUR::new_change ();
+Change Region::FadeChanged       = ARDOUR::new_change ();
 Change Region::SyncOffsetChanged = ARDOUR::new_change ();
-Change Region::MuteChanged = ARDOUR::new_change ();
-Change Region::OpacityChanged = ARDOUR::new_change ();
-Change Region::LockChanged = ARDOUR::new_change ();
-Change Region::LayerChanged = ARDOUR::new_change ();
-Change Region::HiddenChanged = ARDOUR::new_change ();
+Change Region::MuteChanged       = ARDOUR::new_change ();
+Change Region::OpacityChanged    = ARDOUR::new_change ();
+Change Region::LockChanged       = ARDOUR::new_change ();
+Change Region::LayerChanged      = ARDOUR::new_change ();
+Change Region::HiddenChanged     = ARDOUR::new_change ();
 
-sigc::signal<void,Region *> Region::CheckNewRegion;
-
-Region::Region (jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Region::Flag flags)
+Region::Region (nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags)
 {
        /* basic Region constructor */
 
-       _id = ARDOUR::new_id();
        _flags = flags;
        _playlist = 0;
        _read_data_count = 0;
@@ -72,19 +70,18 @@ Region::Region (jack_nframes_t start, jack_nframes_t length, const string& name,
        _last_layer_op = 0;
 }
 
-Region::Region (const Region& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
+Region::Region (boost::shared_ptr<const Region> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
 {
        /* create a new Region from part of an existing one */
 
-       _id = ARDOUR::new_id();
        _frozen = 0;
        pending_changed = Change (0);
        _playlist = 0;
        _read_data_count = 0;
 
-       _start = other._start + offset; 
-       if (other._sync_position < offset) {
-               _sync_position = other._sync_position;
+       _start = other->_start + offset; 
+       if (other->_sync_position < offset) {
+               _sync_position = other->_sync_position;
        } else {
                _sync_position = _start;
        }
@@ -98,39 +95,37 @@ Region::Region (const Region& other, jack_nframes_t offset, jack_nframes_t lengt
        _last_layer_op = 0;
 }
 
-Region::Region (const Region &other)
+Region::Region (boost::shared_ptr<const Region> other)
 {
        /* Pure copy constructor */
 
-       _id = ARDOUR::new_id();
        _frozen = 0;
        pending_changed = Change (0);
        _playlist = 0;
        _read_data_count = 0;
 
        _first_edit = EditChangesID;
-       other._first_edit = EditChangesName;
+       other->_first_edit = EditChangesName;
 
-       if (other._extra_xml) {
-               _extra_xml = new XMLNode (*other._extra_xml);
+       if (other->_extra_xml) {
+               _extra_xml = new XMLNode (*other->_extra_xml);
        } else {
                _extra_xml = 0;
        }
 
-       _start = other._start;
-       _sync_position = other._sync_position;
-       _length = other._length; 
-       _name = other._name;
-       _position = other._position; 
-       _layer = other._layer; 
-       _flags = Flag (other._flags & ~Locked);
+       _start = other->_start;
+       _sync_position = other->_sync_position;
+       _length = other->_length; 
+       _name = other->_name;
+       _position = other->_position; 
+       _layer = other->_layer; 
+       _flags = Flag (other->_flags & ~Locked);
        _current_state_id = 0;
-       _last_layer_op = other._last_layer_op;
+       _last_layer_op = other->_last_layer_op;
 }
 
 Region::Region (const XMLNode& node)
 {
-       _id = 0;
        _frozen = 0;
        pending_changed = Change (0);
        _playlist = 0;
@@ -152,6 +147,9 @@ Region::Region (const XMLNode& node)
 
 Region::~Region ()
 {
+       notify_callbacks ();
+
+       /* derived classes must emit GoingAway */
 }
 
 void
@@ -232,7 +230,7 @@ Region::set_name (string str)
 }
 
 void
-Region::set_length (jack_nframes_t len, void *src)
+Region::set_length (nframes_t len, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -240,6 +238,14 @@ Region::set_length (jack_nframes_t len, void *src)
 
        if (_length != len && len != 0) {
 
+               /* check that the current _position wouldn't make the new 
+                  length impossible.
+               */
+
+               if (max_frames - len < _position) {
+                       return;
+               }
+
                if (!verify_length (len)) {
                        return;
                }
@@ -277,7 +283,7 @@ Region::first_edit ()
                _first_edit = EditChangesNothing;
 
                send_change (NameChanged);
-               CheckNewRegion (this);
+               RegionFactory::CheckNewRegion (shared_from_this());
        }
 }
 
@@ -288,7 +294,7 @@ Region::move_to_natural_position (void *src)
                return;
        }
 
-       Region* whole_file_region = get_parent();
+       boost::shared_ptr<Region> whole_file_region = get_parent();
 
        if (whole_file_region) {
                set_position (whole_file_region->position() + _start, src);
@@ -296,7 +302,7 @@ Region::move_to_natural_position (void *src)
 }
        
 void
-Region::special_set_position (jack_nframes_t pos)
+Region::special_set_position (nframes_t pos)
 {
        /* this is used when creating a whole file region as 
           a way to store its "natural" or "captured" position.
@@ -306,7 +312,7 @@ Region::special_set_position (jack_nframes_t pos)
 }
 
 void
-Region::set_position (jack_nframes_t pos, void *src)
+Region::set_position (nframes_t pos, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -315,6 +321,16 @@ Region::set_position (jack_nframes_t pos, void *src)
        if (_position != pos) {
                _position = pos;
 
+               /* check that the new _position wouldn't make the current
+                  length impossible - if so, change the length. 
+
+                  XXX is this the right thing to do?
+               */
+
+               if (max_frames - _length < _position) {
+                       _length = max_frames - _position;
+               }
+
                if (!_frozen) {
                        char buf[64];
                        snprintf (buf, sizeof (buf), "position set to %u", pos);
@@ -330,7 +346,7 @@ Region::set_position (jack_nframes_t pos, void *src)
 }
 
 void
-Region::set_position_on_top (jack_nframes_t pos, void *src)
+Region::set_position_on_top (nframes_t pos, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -346,7 +362,7 @@ Region::set_position_on_top (jack_nframes_t pos, void *src)
                }
        }
 
-       _playlist->raise_region_to_top (*this);
+       _playlist->raise_region_to_top (boost::shared_ptr<Region>(this));
 
        /* do this even if the position is the same. this helps out
           a GUI that has moved its representation already.
@@ -373,7 +389,7 @@ Region::nudge_position (long n, void *src)
                        _position += n;
                }
        } else {
-               if (_position < (jack_nframes_t) -n) {
+               if (_position < (nframes_t) -n) {
                        _position = 0;
                } else {
                        _position += n;
@@ -390,7 +406,7 @@ Region::nudge_position (long n, void *src)
 }
 
 void
-Region::set_start (jack_nframes_t pos, void *src)
+Region::set_start (nframes_t pos, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -421,12 +437,12 @@ Region::set_start (jack_nframes_t pos, void *src)
 }
 
 void
-Region::trim_start (jack_nframes_t new_position, void *src)
+Region::trim_start (nframes_t new_position, void *src)
 {
        if (_flags & Locked) {
                return;
        }
-       jack_nframes_t new_start;
+       nframes_t new_start;
        int32_t start_shift;
        
        if (new_position > _position) {
@@ -449,7 +465,7 @@ Region::trim_start (jack_nframes_t new_position, void *src)
 
        } else if (start_shift < 0) {
 
-               if (_start < (jack_nframes_t) -start_shift) {
+               if (_start < (nframes_t) -start_shift) {
                        new_start = 0;
                } else {
                        new_start = _start + start_shift;
@@ -476,14 +492,14 @@ Region::trim_start (jack_nframes_t new_position, void *src)
 }
 
 void
-Region::trim_front (jack_nframes_t new_position, void *src)
+Region::trim_front (nframes_t new_position, void *src)
 {
        if (_flags & Locked) {
                return;
        }
 
-       jack_nframes_t end = _position + _length - 1;
-       jack_nframes_t source_zero;
+       nframes_t end = last_frame();
+       nframes_t source_zero;
 
        if (_position > _start) {
                source_zero = _position - _start;
@@ -493,7 +509,7 @@ Region::trim_front (jack_nframes_t new_position, void *src)
 
        if (new_position < end) { /* can't trim it zero or negative length */
                
-               jack_nframes_t newlen;
+               nframes_t newlen;
 
                /* can't trim it back passed where source position zero is located */
                
@@ -514,7 +530,7 @@ Region::trim_front (jack_nframes_t new_position, void *src)
 }
 
 void
-Region::trim_end (jack_nframes_t new_endpoint, void *src)
+Region::trim_end (nframes_t new_endpoint, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -529,7 +545,7 @@ Region::trim_end (jack_nframes_t new_endpoint, void *src)
 }
 
 void
-Region::trim_to (jack_nframes_t position, jack_nframes_t length, void *src)
+Region::trim_to (nframes_t position, nframes_t length, void *src)
 {
        if (_flags & Locked) {
                return;
@@ -544,10 +560,10 @@ Region::trim_to (jack_nframes_t position, jack_nframes_t length, void *src)
 }
 
 void
-Region::trim_to_internal (jack_nframes_t position, jack_nframes_t length, void *src)
+Region::trim_to_internal (nframes_t position, nframes_t length, void *src)
 {
        int32_t start_shift;
-       jack_nframes_t new_start;
+       nframes_t new_start;
 
        if (_flags & Locked) {
                return;
@@ -570,7 +586,7 @@ Region::trim_to_internal (jack_nframes_t position, jack_nframes_t length, void *
 
        } else if (start_shift < 0) {
 
-               if (_start < (jack_nframes_t) -start_shift) {
+               if (_start < (nframes_t) -start_shift) {
                        new_start = 0;
                } else {
                        new_start = _start + start_shift;
@@ -695,9 +711,9 @@ Region::set_locked (bool yn)
 }
 
 void
-Region::set_sync_position (jack_nframes_t absolute_pos)
+Region::set_sync_position (nframes_t absolute_pos)
 {
-       jack_nframes_t file_pos;
+       nframes_t file_pos;
 
        file_pos = _start + (absolute_pos - _position);
 
@@ -730,7 +746,7 @@ Region::clear_sync_position ()
        }
 }
 
-jack_nframes_t
+nframes_t
 Region::sync_offset (int& dir) const
 {
        /* returns the sync point relative the first frame of the region */
@@ -749,11 +765,11 @@ Region::sync_offset (int& dir) const
        }
 }
 
-jack_nframes_t 
-Region::adjust_to_sync (jack_nframes_t pos)
+nframes_t 
+Region::adjust_to_sync (nframes_t pos)
 {
        int sync_dir;
-       jack_nframes_t offset = sync_offset (sync_dir);
+       nframes_t offset = sync_offset (sync_dir);
        
        if (sync_dir > 0) {
                if (max_frames - pos > offset) {
@@ -770,7 +786,7 @@ Region::adjust_to_sync (jack_nframes_t pos)
        return pos;
 }
 
-jack_nframes_t
+nframes_t
 Region::sync_position() const
 {
        if (_flags & SyncMarked) {
@@ -788,7 +804,7 @@ Region::raise ()
                return;
        }
 
-       _playlist->raise_region (*this);
+       _playlist->raise_region (boost::shared_ptr<Region>(this));
 }
 
 void
@@ -798,7 +814,7 @@ Region::lower ()
                return;
        }
 
-       _playlist->lower_region (*this);
+       _playlist->lower_region (boost::shared_ptr<Region>(this));
 }
 
 void
@@ -809,7 +825,7 @@ Region::raise_to_top ()
                return;
        }
 
-       _playlist->raise_region_to_top (*this);
+       _playlist->raise_region_to_top (boost::shared_ptr<Region>(this));
 }
 
 void
@@ -819,7 +835,7 @@ Region::lower_to_bottom ()
                return;
        }
 
-       _playlist->lower_region_to_bottom (*this);
+       _playlist->lower_region_to_bottom (boost::shared_ptr<Region>(this));
 }
 
 void
@@ -844,7 +860,7 @@ Region::state (bool full_state)
        XMLNode *node = new XMLNode ("Region");
        char buf[64];
        
-       snprintf (buf, sizeof (buf), "%" PRIu64, _id);
+       _id.print (buf, sizeof (buf));
        node->add_property ("id", buf);
        node->add_property ("name", _name);
        snprintf (buf, sizeof (buf), "%u", _start);
@@ -886,7 +902,7 @@ Region::set_state (const XMLNode& node)
                return -1;
        }
 
-       sscanf (prop->value().c_str(), "%" PRIu64, &_id);
+       _id = prop->value();
 
        if ((prop = node.property ("name")) == 0) {
                error << _("Session: XMLNode describing a Region is incomplete (no name)") << endmsg;
@@ -896,15 +912,15 @@ Region::set_state (const XMLNode& node)
        _name = prop->value();
 
        if ((prop = node.property ("start")) != 0) {
-               _start = (jack_nframes_t) atoi (prop->value().c_str());
+               sscanf (prop->value().c_str(), "%" PRIu32, &_start);
        }
 
        if ((prop = node.property ("length")) != 0) {
-               _length = (jack_nframes_t) atoi (prop->value().c_str());
+               sscanf (prop->value().c_str(), "%" PRIu32, &_length);
        }
 
        if ((prop = node.property ("position")) != 0) {
-               _position = (jack_nframes_t) atoi (prop->value().c_str());
+               sscanf (prop->value().c_str(), "%" PRIu32, &_position);
        }
 
        if ((prop = node.property ("layer")) != 0) {
@@ -914,7 +930,7 @@ Region::set_state (const XMLNode& node)
        /* note: derived classes set flags */
 
        if ((prop = node.property ("sync-position")) != 0) {
-               _sync_position = (jack_nframes_t) atoi (prop->value().c_str());
+               sscanf (prop->value().c_str(), "%" PRIu32, &_sync_position);
        } else {
                _sync_position = _start;
        }
@@ -994,3 +1010,30 @@ Region::set_last_layer_op (uint64_t when)
 {
        _last_layer_op = when;
 }
+
+bool
+Region::overlap_equivalent (boost::shared_ptr<const Region> other) const
+{
+       return coverage (other->first_frame(), other->last_frame()) != OverlapNone;
+}
+
+bool
+Region::equivalent (boost::shared_ptr<const Region> other) const
+{
+       return _start == other->_start &&
+               _position == other->_position &&
+               _length == other->_length;
+}
+
+bool
+Region::size_equivalent (boost::shared_ptr<const Region> other) const
+{
+       return _start == other->_start &&
+               _length == other->_length;
+}
+
+bool
+Region::region_list_equivalent (boost::shared_ptr<const Region> other) const
+{
+       return size_equivalent (other) && source_equivalent (other) && _name == other->_name;
+}