2 Copyright (C) 2000 Paul Davis
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.
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.
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.
23 #include <cstdio> /* for sprintf */
27 #include <sigc++/bind.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/xml++.h>
32 #include <ardour/location.h>
37 using namespace ARDOUR;
40 Location::Location (const Location& other)
41 : _name (other._name),
42 _start (other._start),
49 Location::operator= (const Location& other)
56 _start = other._start;
58 _flags = other._flags;
60 /* "changed" not emitted on purpose */
66 Location::set_start (jack_nframes_t s)
72 start_changed(this); /* EMIT SIGNAL */
77 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
83 start_changed(this); /* EMIT SIGNAL */
90 Location::set_end (jack_nframes_t e)
96 end_changed(this); /* EMIT SIGNAL */
101 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
107 end_changed(this); /* EMIT SIGNAL */
113 Location::set (jack_nframes_t start, jack_nframes_t end)
115 if (is_mark() && start != end) {
117 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
121 if (_start != start) {
123 start_changed(this); /* EMIT SIGNAL */
128 end_changed(this); /* EMIT SIGNAL */
134 Location::set_hidden (bool yn, void *src)
136 if (set_flag_internal (yn, IsHidden)) {
137 FlagsChanged (this, src); /* EMIT SIGNAL */
142 Location::set_cd (bool yn, void *src)
144 if (set_flag_internal (yn, IsCDMarker)) {
145 FlagsChanged (this, src); /* EMIT SIGNAL */
150 Location::set_is_end (bool yn, void *src)
152 if (set_flag_internal (yn, IsEnd)) {
153 FlagsChanged (this, src); /* EMIT SIGNAL */
158 Location::set_auto_punch (bool yn, void *src)
160 if (is_mark() || _start == _end) {
164 if (set_flag_internal (yn, IsAutoPunch)) {
165 FlagsChanged (this, src); /* EMIT SIGNAL */
170 Location::set_auto_loop (bool yn, void *src)
172 if (is_mark() || _start == _end) {
176 if (set_flag_internal (yn, IsAutoLoop)) {
177 FlagsChanged (this, src); /* EMIT SIGNAL */
182 Location::set_flag_internal (bool yn, Flags flag)
185 if (!(_flags & flag)) {
199 Location::set_mark (bool yn)
201 /* This function is private, and so does not emit signals */
203 if (_start != _end) {
207 set_flag_internal (yn, IsMark);
212 Location::cd_info_node(const string & name, const string & value)
214 XMLNode* root = new XMLNode("CD-Info");
216 root->add_property("name", name);
217 root->add_property("value", value);
224 Location::get_state (void)
226 XMLNode *node = new XMLNode ("Location");
229 typedef map<string, string>::const_iterator CI;
230 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
231 node->add_child_nocopy(cd_info_node(m->first, m->second));
234 node->add_property ("name", name());
235 snprintf (buf, sizeof (buf), "%u", start());
236 node->add_property ("start", buf);
237 snprintf (buf, sizeof (buf), "%u", end());
238 node->add_property ("end", buf);
239 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
240 node->add_property ("flags", buf);
246 Location::set_state (const XMLNode& node)
248 XMLPropertyList plist;
249 const XMLProperty *prop;
251 XMLNodeList cd_list = node.children();
252 XMLNodeConstIterator cd_iter;
259 if (node.name() != "Location") {
260 error << _("incorrect XML node passed to Location::set_state") << endmsg;
264 plist = node.properties();
266 if ((prop = node.property ("name")) == 0) {
267 error << _("XML node for Location has no name information") << endmsg;
271 set_name (prop->value());
273 if ((prop = node.property ("start")) == 0) {
274 error << _("XML node for Location has no start information") << endmsg;
278 /* can't use set_start() here, because _end
279 may make the value of _start illegal.
282 _start = atoi (prop->value().c_str());
284 if ((prop = node.property ("end")) == 0) {
285 error << _("XML node for Location has no end information") << endmsg;
289 _end = atoi (prop->value().c_str());
293 if ((prop = node.property ("flags")) == 0) {
294 error << _("XML node for Location has no flags information") << endmsg;
298 _flags = Flags (atoi (prop->value().c_str()));
300 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
304 if (cd_node->name() != "CD-Info") {
308 if ((prop = cd_node->property ("name")) != 0) {
309 cd_name = prop->value();
311 throw failed_constructor ();
314 if ((prop = cd_node->property ("value")) != 0) {
315 cd_value = prop->value();
317 throw failed_constructor ();
321 cd_info[cd_name] = cd_value;
325 changed(this); /* EMIT SIGNAL */
330 /*---------------------------------------------------------------------- */
332 Locations::Locations ()
335 current_location = 0;
336 save_state (_("initial"));
339 Locations::~Locations ()
341 std::set<Location*> all_locations;
343 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
345 State* lstate = dynamic_cast<State*> (*siter);
347 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
348 all_locations.insert (*liter);
351 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
352 all_locations.insert (*siter);
356 set_delete (&all_locations);
360 Locations::set_current (Location *loc, bool want_lock)
366 LockMonitor lm (lock, __LINE__, __FILE__);
367 ret = set_current_unlocked (loc);
369 ret = set_current_unlocked (loc);
373 current_changed (current_location); /* EMIT SIGNAL */
379 Locations::set_current_unlocked (Location *loc)
381 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
382 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
386 current_location = loc;
394 LockMonitor lm (lock, __LINE__, __FILE__);
395 LocationList::iterator tmp;
396 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
399 if (!(*i)->is_end()) {
406 current_location = 0;
409 save_state (_("clear"));
411 changed (); /* EMIT SIGNAL */
412 current_changed (0); /* EMIT SIGNAL */
416 Locations::clear_markers ()
419 LockMonitor lm (lock, __LINE__, __FILE__);
420 LocationList::iterator tmp;
422 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
426 if ((*i)->is_mark() && !(*i)->is_end()) {
434 save_state (_("clear markers"));
436 changed (); /* EMIT SIGNAL */
440 Locations::clear_ranges ()
443 LockMonitor lm (lock, __LINE__, __FILE__);
444 LocationList::iterator tmp;
446 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
451 if (!(*i)->is_mark()) {
459 current_location = 0;
462 save_state (_("clear ranges"));
464 changed (); /* EMIT SIGNAL */
465 current_changed (0); /* EMIT SIGNAL */
469 Locations::add (Location *loc, bool make_current)
472 LockMonitor lm (lock, __LINE__, __FILE__);
473 locations.push_back (loc);
476 current_location = loc;
480 save_state (_("add"));
482 added (loc); /* EMIT SIGNAL */
485 current_changed (current_location); /* EMIT SIGNAL */
490 Locations::remove (Location *loc)
493 bool was_removed = false;
494 bool was_current = false;
495 LocationList::iterator i;
502 LockMonitor lm (lock, __LINE__, __FILE__);
504 for (i = locations.begin(); i != locations.end(); ++i) {
508 if (current_location == loc) {
509 current_location = 0;
518 save_state (_("remove"));
520 removed (loc); /* EMIT SIGNAL */
523 current_changed (0); /* EMIT SIGNAL */
526 changed (); /* EMIT_SIGNAL */
531 Locations::location_changed (Location* loc)
533 save_state (X_("location changed"));
534 changed (); /* EMIT SIGNAL */
538 Locations::get_state ()
540 XMLNode *node = new XMLNode ("Locations");
541 LocationList::iterator iter;
542 LockMonitor lm (lock, __LINE__, __FILE__);
544 for (iter = locations.begin(); iter != locations.end(); ++iter) {
545 node->add_child_nocopy ((*iter)->get_state ());
552 Locations::set_state (const XMLNode& node)
555 XMLNodeConstIterator niter;
557 if (node.name() != "Locations") {
558 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
562 nlist = node.children();
565 LockMonitor lm (lock, __LINE__, __FILE__);
567 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
568 Location *loc = new Location;
570 if (loc->set_state (**niter)) {
573 locations.push_back (loc);
577 if (locations.size()) {
578 current_location = locations.front();
580 current_location = 0;
584 changed (); /* EMIT SIGNAL */
589 struct LocationStartEarlierComparison
591 bool operator() (Location *a, Location *b) {
592 return a->start() < b->start();
596 struct LocationStartLaterComparison
598 bool operator() (Location *a, Location *b) {
599 return a->start() > b->start();
604 Locations::first_location_before (jack_nframes_t frame)
609 LockMonitor lm (lock, __LINE__, __FILE__);
613 LocationStartLaterComparison cmp;
616 /* locs is now sorted latest..earliest */
618 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
619 if (!(*i)->is_hidden() && (*i)->start() < frame) {
628 Locations::first_location_after (jack_nframes_t frame)
633 LockMonitor lm (lock, __LINE__, __FILE__);
637 LocationStartEarlierComparison cmp;
640 /* locs is now sorted earliest..latest */
642 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
643 if (!(*i)->is_hidden() && (*i)->start() > frame) {
652 Locations::end_location () const
654 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
655 if ((*i)->is_end()) {
656 return const_cast<Location*> (*i);
663 Locations::auto_loop_location () const
665 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
666 if ((*i)->is_auto_loop()) {
667 return const_cast<Location*> (*i);
674 Locations::auto_punch_location () const
676 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
677 if ((*i)->is_auto_punch()) {
678 return const_cast<Location*> (*i);
685 Locations::state_factory (std::string why) const
687 State* state = new State (why);
689 state->locations = locations;
691 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
692 state->states.push_back (new Location (**i));
699 Locations::restore_state (StateManager::State& state)
702 LockMonitor lm (lock, __LINE__, __FILE__);
703 State* lstate = dynamic_cast<State*> (&state);
705 locations = lstate->locations;
706 LocationList& states = lstate->states;
707 LocationList::iterator l, s;
709 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
718 Locations::get_memento () const
720 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);