Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / time_selection.cc
1 /*
2  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <algorithm>
22
23 #include "pbd/error.h"
24 #include "ardour/types.h"
25
26 #include "time_selection.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 AudioRange&
34 TimeSelection::operator[] (uint32_t which)
35 {
36         for (std::list<AudioRange>::iterator i = begin(); i != end(); ++i) {
37                 if ((*i).id == which) {
38                         return *i;
39                 }
40         }
41         fatal << string_compose (_("programming error: request for non-existent audio range (%1)!"), which) << endmsg;
42         abort(); /*NOTREACHED*/
43         return *(new AudioRange(0,0,0)); /* keep the compiler happy; never called */
44 }
45
46 bool
47 TimeSelection::consolidate ()
48 {
49         bool changed = false;
50
51   restart:
52         for (std::list<AudioRange>::iterator a = begin(); a != end(); ++a) {
53                 for (std::list<AudioRange>::iterator b = begin(); b != end(); ++b) {
54
55                         if (&(*a) == &(*b)) {
56                                 continue;
57                         }
58
59                         if (a->coverage (b->start, b->end) != Evoral::OverlapNone) {
60                                 a->start = std::min (a->start, b->start);
61                                 a->end = std::max (a->end, b->end);
62                                 erase (b);
63                                 changed = true;
64                                 goto restart;
65                         }
66                 }
67         }
68
69         return changed;
70 }
71
72 samplepos_t
73 TimeSelection::start ()
74 {
75         if (empty()) {
76                 return 0;
77         }
78
79         samplepos_t first = max_samplepos;
80
81         for (std::list<AudioRange>::iterator i = begin(); i != end(); ++i) {
82                 if ((*i).start < first) {
83                         first = (*i).start;
84                 }
85         }
86         return first;
87 }
88
89 samplepos_t
90 TimeSelection::end_sample ()
91 {
92         samplepos_t last = 0;
93
94         /* XXX make this work like RegionSelection: no linear search needed */
95
96         for (std::list<AudioRange>::iterator i = begin(); i != end(); ++i) {
97                 if ((*i).end > last) {
98                         last = (*i).end;
99                 }
100         }
101         return last;
102 }
103
104 samplecnt_t
105 TimeSelection::length()
106 {
107         if (empty()) {
108                 return 0;
109         }
110
111         return end_sample() - start() + 1;
112 }