cefe7985cbdad8b24921d805f654c822b7493b71
[ardour.git] / gtk2_ardour / route_redirect_selection.cc
1 /*
2     Copyright (C) 2002 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <algorithm>
22 #include <sigc++/bind.h>
23 #include <pbd/error.h>
24
25 #include <ardour/playlist.h>
26 #include <ardour/redirect.h>
27 #include <ardour/route.h>
28
29 #include "route_redirect_selection.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace sigc;
35
36 RouteRedirectSelection&
37 RouteRedirectSelection::operator= (const RouteRedirectSelection& other)
38 {
39         if (&other != this) {
40                 redirects = other.redirects;
41                 routes = other.routes;
42         }
43         return *this;
44 }
45
46 bool
47 operator== (const RouteRedirectSelection& a, const RouteRedirectSelection& b)
48 {
49         return a.redirects == b.redirects &&
50                 a.routes == b.routes;
51 }
52
53 void
54 RouteRedirectSelection::clear ()
55 {
56         clear_redirects ();
57         clear_routes ();
58 }
59
60 void
61 RouteRedirectSelection::clear_redirects ()
62 {
63         for (RedirectSelection::iterator i = redirects.begin(); i != redirects.end(); ) {
64                 RedirectSelection::iterator tmp;
65
66                 tmp = i;
67                 ++tmp;
68
69                 delete *i;
70
71                 i = tmp;
72         }
73
74         redirects.clear ();
75         RedirectsChanged ();
76 }
77
78 void
79 RouteRedirectSelection::clear_routes ()
80 {
81         routes.clear ();
82         RoutesChanged ();
83 }
84
85 void
86 RouteRedirectSelection::add (Redirect* r)
87 {
88         if (find (redirects.begin(), redirects.end(), r) == redirects.end()) {
89                 redirects.push_back (r);
90                 
91                 void (RouteRedirectSelection::*pmf)(Redirect*) = &RouteRedirectSelection::remove;
92                 r->GoingAway.connect (mem_fun(*this, pmf));
93
94                 RedirectsChanged();
95         }
96 }
97
98 void
99 RouteRedirectSelection::add (const vector<Redirect*>& rlist)
100 {
101         bool changed = false;
102
103         for (vector<Redirect*>::const_iterator i = rlist.begin(); i != rlist.end(); ++i) {
104                 if (find (redirects.begin(), redirects.end(), *i) == redirects.end()) {
105                         redirects.push_back (*i);
106                         
107                         void (RouteRedirectSelection::*pmf)(Redirect*) = &RouteRedirectSelection::remove;
108                         (*i)->GoingAway.connect (mem_fun(*this, pmf));
109                         changed = true;
110                 }
111         }
112
113         if (changed) {
114                 RedirectsChanged();
115         }
116 }
117
118 void
119 RouteRedirectSelection::remove (Redirect* r)
120 {
121         list<Redirect*>::iterator i;
122         if ((i = find (redirects.begin(), redirects.end(), r)) != redirects.end()) {
123                 redirects.erase (i);
124                 RedirectsChanged ();
125         }
126 }
127
128 void
129 RouteRedirectSelection::set (Redirect *r)
130 {
131         clear_redirects ();
132         add (r);
133 }
134
135 void
136 RouteRedirectSelection::set (const vector<Redirect*>& rlist)
137 {
138         clear_redirects ();
139         add (rlist);
140 }
141
142 void
143 RouteRedirectSelection::add (Route* r)
144 {
145         if (find (routes.begin(), routes.end(), r) == routes.end()) {
146                 routes.push_back (r);
147
148                 void (RouteRedirectSelection::*pmf)(Route*) = &RouteRedirectSelection::remove;
149                 r->GoingAway.connect (bind (mem_fun(*this, pmf), r));
150
151                 RoutesChanged();
152         }
153 }
154
155 void
156 RouteRedirectSelection::remove (Route* r)
157 {
158         list<Route*>::iterator i;
159         if ((i = find (routes.begin(), routes.end(), r)) != routes.end()) {
160                 routes.erase (i);
161                 RoutesChanged ();
162         }
163 }
164
165 void
166 RouteRedirectSelection::set (Route *r)
167 {
168         clear_routes ();
169         add (r);
170 }
171
172 bool
173 RouteRedirectSelection::selected (Route* ms)
174 {
175         return find (routes.begin(), routes.end(), ms) != routes.end();
176 }
177
178 bool
179 RouteRedirectSelection::empty ()
180 {
181         return redirects.empty () && routes.empty ();
182 }
183