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_is_start (bool yn, void *src)
160 if (set_flag_internal (yn, IsStart)) {
161 FlagsChanged (this, src); /* EMIT SIGNAL */
166 Location::set_auto_punch (bool yn, void *src)
168 if (is_mark() || _start == _end) {
172 if (set_flag_internal (yn, IsAutoPunch)) {
173 FlagsChanged (this, src); /* EMIT SIGNAL */
178 Location::set_auto_loop (bool yn, void *src)
180 if (is_mark() || _start == _end) {
184 if (set_flag_internal (yn, IsAutoLoop)) {
185 FlagsChanged (this, src); /* EMIT SIGNAL */
190 Location::set_flag_internal (bool yn, Flags flag)
193 if (!(_flags & flag)) {
207 Location::set_mark (bool yn)
209 /* This function is private, and so does not emit signals */
211 if (_start != _end) {
215 set_flag_internal (yn, IsMark);
220 Location::cd_info_node(const string & name, const string & value)
222 XMLNode* root = new XMLNode("CD-Info");
224 root->add_property("name", name);
225 root->add_property("value", value);
232 Location::get_state (void)
234 XMLNode *node = new XMLNode ("Location");
237 typedef map<string, string>::const_iterator CI;
238 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
239 node->add_child_nocopy(cd_info_node(m->first, m->second));
242 node->add_property ("name", name());
243 snprintf (buf, sizeof (buf), "%u", start());
244 node->add_property ("start", buf);
245 snprintf (buf, sizeof (buf), "%u", end());
246 node->add_property ("end", buf);
247 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
248 node->add_property ("flags", buf);
254 Location::set_state (const XMLNode& node)
256 XMLPropertyList plist;
257 const XMLProperty *prop;
259 XMLNodeList cd_list = node.children();
260 XMLNodeConstIterator cd_iter;
267 if (node.name() != "Location") {
268 error << _("incorrect XML node passed to Location::set_state") << endmsg;
272 plist = node.properties();
274 if ((prop = node.property ("name")) == 0) {
275 error << _("XML node for Location has no name information") << endmsg;
279 set_name (prop->value());
281 if ((prop = node.property ("start")) == 0) {
282 error << _("XML node for Location has no start information") << endmsg;
286 /* can't use set_start() here, because _end
287 may make the value of _start illegal.
290 _start = atoi (prop->value().c_str());
292 if ((prop = node.property ("end")) == 0) {
293 error << _("XML node for Location has no end information") << endmsg;
297 _end = atoi (prop->value().c_str());
301 if ((prop = node.property ("flags")) == 0) {
302 error << _("XML node for Location has no flags information") << endmsg;
306 _flags = Flags (atoi (prop->value().c_str()));
308 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
312 if (cd_node->name() != "CD-Info") {
316 if ((prop = cd_node->property ("name")) != 0) {
317 cd_name = prop->value();
319 throw failed_constructor ();
322 if ((prop = cd_node->property ("value")) != 0) {
323 cd_value = prop->value();
325 throw failed_constructor ();
329 cd_info[cd_name] = cd_value;
333 changed(this); /* EMIT SIGNAL */
338 /*---------------------------------------------------------------------- */
340 Locations::Locations ()
343 current_location = 0;
344 save_state (_("initial"));
347 Locations::~Locations ()
349 std::set<Location*> all_locations;
351 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
353 State* lstate = dynamic_cast<State*> (*siter);
355 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
356 all_locations.insert (*liter);
359 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
360 all_locations.insert (*siter);
364 set_delete (&all_locations);
368 Locations::set_current (Location *loc, bool want_lock)
374 LockMonitor lm (lock, __LINE__, __FILE__);
375 ret = set_current_unlocked (loc);
377 ret = set_current_unlocked (loc);
381 current_changed (current_location); /* EMIT SIGNAL */
387 Locations::set_current_unlocked (Location *loc)
389 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
390 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
394 current_location = loc;
402 LockMonitor lm (lock, __LINE__, __FILE__);
403 LocationList::iterator tmp;
404 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
407 if (!(*i)->is_end() && !(*i)->is_start()) {
414 current_location = 0;
417 save_state (_("clear"));
419 changed (); /* EMIT SIGNAL */
420 current_changed (0); /* EMIT SIGNAL */
424 Locations::clear_markers ()
427 LockMonitor lm (lock, __LINE__, __FILE__);
428 LocationList::iterator tmp;
430 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
434 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
442 save_state (_("clear markers"));
444 changed (); /* EMIT SIGNAL */
448 Locations::clear_ranges ()
451 LockMonitor lm (lock, __LINE__, __FILE__);
452 LocationList::iterator tmp;
454 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
459 if (!(*i)->is_mark()) {
467 current_location = 0;
470 save_state (_("clear ranges"));
472 changed (); /* EMIT SIGNAL */
473 current_changed (0); /* EMIT SIGNAL */
477 Locations::add (Location *loc, bool make_current)
480 LockMonitor lm (lock, __LINE__, __FILE__);
481 locations.push_back (loc);
484 current_location = loc;
488 save_state (_("add"));
490 added (loc); /* EMIT SIGNAL */
493 current_changed (current_location); /* EMIT SIGNAL */
498 Locations::remove (Location *loc)
501 bool was_removed = false;
502 bool was_current = false;
503 LocationList::iterator i;
505 if (loc->is_end() || loc->is_start()) {
510 LockMonitor lm (lock, __LINE__, __FILE__);
512 for (i = locations.begin(); i != locations.end(); ++i) {
516 if (current_location == loc) {
517 current_location = 0;
526 save_state (_("remove"));
528 removed (loc); /* EMIT SIGNAL */
531 current_changed (0); /* EMIT SIGNAL */
534 changed (); /* EMIT_SIGNAL */
539 Locations::location_changed (Location* loc)
541 save_state (X_("location changed"));
542 changed (); /* EMIT SIGNAL */
546 Locations::get_state ()
548 XMLNode *node = new XMLNode ("Locations");
549 LocationList::iterator iter;
550 LockMonitor lm (lock, __LINE__, __FILE__);
552 for (iter = locations.begin(); iter != locations.end(); ++iter) {
553 node->add_child_nocopy ((*iter)->get_state ());
560 Locations::set_state (const XMLNode& node)
563 XMLNodeConstIterator niter;
565 if (node.name() != "Locations") {
566 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
570 nlist = node.children();
573 LockMonitor lm (lock, __LINE__, __FILE__);
575 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
576 Location *loc = new Location;
578 if (loc->set_state (**niter)) {
581 locations.push_back (loc);
585 if (locations.size()) {
586 current_location = locations.front();
588 current_location = 0;
592 changed (); /* EMIT SIGNAL */
597 struct LocationStartEarlierComparison
599 bool operator() (Location *a, Location *b) {
600 return a->start() < b->start();
604 struct LocationStartLaterComparison
606 bool operator() (Location *a, Location *b) {
607 return a->start() > b->start();
612 Locations::first_location_before (jack_nframes_t frame)
617 LockMonitor lm (lock, __LINE__, __FILE__);
621 LocationStartLaterComparison cmp;
624 /* locs is now sorted latest..earliest */
626 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
627 if (!(*i)->is_hidden() && (*i)->start() < frame) {
636 Locations::first_location_after (jack_nframes_t frame)
641 LockMonitor lm (lock, __LINE__, __FILE__);
645 LocationStartEarlierComparison cmp;
648 /* locs is now sorted earliest..latest */
650 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
651 if (!(*i)->is_hidden() && (*i)->start() > frame) {
660 Locations::first_mark_before (jack_nframes_t frame)
665 LockMonitor lm (lock, __LINE__, __FILE__);
669 LocationStartLaterComparison cmp;
672 /* locs is now sorted latest..earliest */
674 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
675 if (!(*i)->is_hidden()) {
676 if ((*i)->is_mark()) {
677 /* MARK: start == end */
678 if ((*i)->start() < frame) {
679 return (*i)->start();
682 /* RANGE: start != end, compare start and end */
683 if ((*i)->end() < frame) {
686 if ((*i)->start () < frame) {
687 return (*i)->start();
697 Locations::first_mark_after (jack_nframes_t frame)
702 LockMonitor lm (lock, __LINE__, __FILE__);
706 LocationStartEarlierComparison cmp;
709 /* locs is now sorted earliest..latest */
711 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
712 if (!(*i)->is_hidden()) {
713 if ((*i)->is_mark()) {
714 /* MARK, start == end so just compare start */
715 if ((*i)->start() > frame) {
716 return (*i)->start();
719 /* RANGE, start != end, compare start and end */
720 if ((*i)->start() > frame ) {
721 return (*i)->start ();
723 if ((*i)->end() > frame) {
734 Locations::end_location () const
736 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
737 if ((*i)->is_end()) {
738 return const_cast<Location*> (*i);
745 Locations::start_location () const
747 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
748 if ((*i)->is_start()) {
749 return const_cast<Location*> (*i);
756 Locations::auto_loop_location () const
758 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
759 if ((*i)->is_auto_loop()) {
760 return const_cast<Location*> (*i);
767 Locations::auto_punch_location () const
769 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
770 if ((*i)->is_auto_punch()) {
771 return const_cast<Location*> (*i);
778 Locations::state_factory (std::string why) const
780 State* state = new State (why);
782 state->locations = locations;
784 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
785 state->states.push_back (new Location (**i));
792 Locations::restore_state (StateManager::State& state)
795 LockMonitor lm (lock, __LINE__, __FILE__);
796 State* lstate = dynamic_cast<State*> (&state);
798 locations = lstate->locations;
799 LocationList& states = lstate->states;
800 LocationList::iterator l, s;
802 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
811 Locations::get_memento () const
813 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
817 Locations::num_range_markers () const
820 LockMonitor lm (lock, __LINE__, __FILE__);
821 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
822 if ((*i)->is_range_marker()) {