+
+struct RegionSortByTrack {
+ bool operator() (const RegionView* a, const RegionView* b) const {
+
+ /* really, track and position */
+
+ if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
+ return a->region()->position() < b->region()->position();
+ } else {
+ return a->get_time_axis_view().order() < b->get_time_axis_view().order();
+ }
+ }
+};
+
+
+/**
+ * @param List which will be filled with the selection's regions
+ * sorted by track and position.
+ */
+void
+RegionSelection::by_track (list<RegionView*>& foo) const
+{
+ list<RegionView*>::const_iterator i;
+ RegionSortByTrack sorter;
+
+ for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
+ foo.push_back (*i);
+ }
+
+ foo.sort (sorter);
+ return;
+}
+
+/**
+ * @param Sort the selection by position and track.
+ */
+void
+RegionSelection::sort_by_position_and_track ()
+{
+ RegionSortByTrack sorter;
+ sort (sorter);
+}
+
+/**
+ * @param tv Track.
+ * @return true if any of the selection's regions are on tv.
+ */
+bool
+RegionSelection::involves (const TimeAxisView& tv) const
+{
+ for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
+ if (&(*i)->get_time_axis_view() == &tv) {
+ return true;
+ }
+ }
+ return false;
+}
+
+framepos_t
+RegionSelection::start () const
+{
+ framepos_t s = max_framepos;
+ for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
+ s = min (s, (*i)->region()->position ());
+ }
+
+ if (s == max_framepos) {
+ return 0;
+ }
+
+ return s;
+}
+
+framepos_t
+RegionSelection::end_frame () const
+{
+ framepos_t e = 0;
+ for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
+ e = max (e, (*i)->region()->last_frame ());
+ }
+
+ return e;
+}
+
+/** @return the playlists that the regions in the selection are on */
+set<boost::shared_ptr<Playlist> >
+RegionSelection::playlists () const
+{
+ set<boost::shared_ptr<Playlist> > pl;
+ for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
+ pl.insert ((*i)->region()->playlist ());
+ }
+
+ return pl;
+}
+
+size_t
+RegionSelection::n_midi_regions () const
+{
+ size_t count = 0;
+
+ for (const_iterator r = begin(); r != end(); ++r) {
+ MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
+ if (mrv) {
+ ++count;
+ }
+ }
+
+ return count;
+}
+
+ARDOUR::RegionList
+RegionSelection::regionlist () const
+{
+ ARDOUR::RegionList rl;
+ for (const_iterator r = begin (); r != end (); ++r) {
+ rl.push_back ((*r)->region ());
+ }
+ return rl;
+}