+
+void
+Playlist::shuffle (boost::shared_ptr<Region> region, int dir)
+{
+ bool moved = false;
+
+ if (region->locked()) {
+ return;
+ }
+
+ _shuffling = true;
+
+ {
+ RegionLock rlock (const_cast<Playlist*> (this));
+
+
+ if (dir > 0) {
+
+ RegionList::iterator next;
+
+ for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
+ if ((*i) == region) {
+ next = i;
+ ++next;
+
+ if (next != regions.end()) {
+
+ if ((*next)->locked()) {
+ break;
+ }
+
+ framepos_t new_pos;
+
+ if ((*next)->position() != region->last_frame() + 1) {
+ /* they didn't used to touch, so after shuffle,
+ just have them swap positions.
+ */
+ new_pos = (*next)->position();
+ } else {
+ /* they used to touch, so after shuffle,
+ make sure they still do. put the earlier
+ region where the later one will end after
+ it is moved.
+ */
+ new_pos = region->position() + (*next)->length();
+ }
+
+ (*next)->set_position (region->position(), this);
+ region->set_position (new_pos, this);
+
+ /* avoid a full sort */
+
+ regions.erase (i); // removes the region from the list */
+ next++;
+ regions.insert (next, region); // adds it back after next
+
+ moved = true;
+ }
+ break;
+ }
+ }
+ } else {
+
+ RegionList::iterator prev = regions.end();
+
+ for (RegionList::iterator i = regions.begin(); i != regions.end(); prev = i, ++i) {
+ if ((*i) == region) {
+
+ if (prev != regions.end()) {
+
+ if ((*prev)->locked()) {
+ break;
+ }
+
+ framepos_t new_pos;
+ if (region->position() != (*prev)->last_frame() + 1) {
+ /* they didn't used to touch, so after shuffle,
+ just have them swap positions.
+ */
+ new_pos = region->position();
+ } else {
+ /* they used to touch, so after shuffle,
+ make sure they still do. put the earlier
+ one where the later one will end after
+ */
+ new_pos = (*prev)->position() + region->length();
+ }
+
+ region->set_position ((*prev)->position(), this);
+ (*prev)->set_position (new_pos, this);
+
+ /* avoid a full sort */
+
+ regions.erase (i); // remove region
+ regions.insert (prev, region); // insert region before prev
+
+ moved = true;
+ }
+
+ break;
+ }
+ }
+ }
+ }
+
+ _shuffling = false;
+
+ if (moved) {
+
+ relayer ();
+ check_dependents (region, false);
+
+ notify_contents_changed();
+ }
+
+}
+
+bool
+Playlist::region_is_shuffle_constrained (boost::shared_ptr<Region>)
+{
+ RegionLock rlock (const_cast<Playlist*> (this));
+
+ if (regions.size() > 1) {
+ return true;
+ }
+
+ return false;
+}
+
+void
+Playlist::update_after_tempo_map_change ()
+{
+ RegionLock rlock (const_cast<Playlist*> (this));
+ RegionList copy (regions.rlist());
+
+ freeze ();
+
+ for (RegionList::iterator i = copy.begin(); i != copy.end(); ++i) {
+ (*i)->update_position_after_tempo_map_change ();
+ }
+
+ thaw ();
+}
+
+void
+Playlist::foreach_region (boost::function<void(boost::shared_ptr<Region>)> s)
+{
+ RegionLock rl (this, false);
+ for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
+ s (*i);
+ }
+}
+
+void
+Playlist::set_explicit_relayering (bool e)
+{
+ if (e == false && _explicit_relayering == true) {
+
+ /* We are changing from explicit to implicit relayering; layering may have been changed whilst
+ we were in explicit mode, and we don't want that to be undone next time an implicit relayer
+ occurs. Hence now we'll set up region last_layer_op values so that an implicit relayer
+ at this point would keep regions on the same layers.
+
+ From then on in, it's just you and your towel.
+ */
+
+ RegionLock rl (this);
+ for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
+ (*i)->set_last_layer_op ((*i)->layer ());
+ }
+ }
+
+ _explicit_relayering = e;
+}
+
+
+bool
+Playlist::has_region_at (framepos_t const p) const
+{
+ RegionLock (const_cast<Playlist *> (this));
+
+ RegionList::const_iterator i = regions.begin ();
+ while (i != regions.end() && !(*i)->covers (p)) {
+ ++i;
+ }
+
+ return (i != regions.end());
+}