Allow markers to be glued to bar/beat time. Fixes #1815.
[ardour.git] / libs / ardour / session_command.cc
1 /*
2     Copyright (C) 2000-2007 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 */
19
20 #include "ardour/session.h"
21 #include "ardour/route.h"
22 #include "pbd/memento_command.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/playlist.h"
25 #include "ardour/audioplaylist.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/midi_playlist.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/tempo.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/session_playlists.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/midi_automation_list_binder.h"
37 #include "pbd/error.h"
38 #include "pbd/id.h"
39 #include "pbd/statefuldestructible.h"
40 #include "pbd/failed_constructor.h"
41 #include "pbd/stateful_diff_command.h"
42 #include "evoral/Curve.hpp"
43
44 using namespace PBD;
45 using namespace ARDOUR;
46
47 #include "i18n.h"
48
49 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulDestructible *ptr)
50 {
51     registry[id] = ptr;
52 }
53
54 Command *
55 Session::memento_command_factory(XMLNode *n)
56 {
57     PBD::ID id;
58     XMLNode *before = 0, *after = 0;
59     XMLNode *child = 0;
60
61     /* get id */
62
63     /* XXX: HACK! */
64     bool have_id = n->property("obj-id") != 0;
65     if (have_id) {
66             id = PBD::ID(n->property("obj-id")->value());
67     }
68
69     /* get before/after */
70
71     if (n->name() == "MementoCommand") {
72             before = new XMLNode(*n->children().front());
73             after = new XMLNode(*n->children().back());
74             child = before;
75     } else if (n->name() == "MementoUndoCommand") {
76             before = new XMLNode(*n->children().front());
77             child = before;
78     } else if (n->name() == "MementoRedoCommand") {
79             after = new XMLNode(*n->children().front());
80             child = after;
81     } else if (n->name() == "PlaylistCommand") {
82             before = new XMLNode(*n->children().front());
83             after = new XMLNode(*n->children().back());
84             child = before;
85     }
86
87     if (!child) {
88         error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
89         return 0;
90     }
91
92     /* create command */
93     string obj_T = n->property ("type-name")->value();
94
95     if (obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion" || obj_T == "ARDOUR::Region") {
96             boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
97             if (r) {
98                     return new MementoCommand<Region>(*r, before, after);
99             }
100
101     } else if (obj_T == "ARDOUR::AudioSource" || obj_T == "ARDOUR::MidiSource") {
102             if (sources.count(id))
103                     return new MementoCommand<Source>(*sources[id], before, after);
104
105     } else if (obj_T == "ARDOUR::Location") {
106             Location* loc = _locations->get_location_by_id(id);
107             if (loc) {
108                     return new MementoCommand<Location>(*loc, before, after);
109             }
110
111     } else if (obj_T == "ARDOUR::Locations") {
112             return new MementoCommand<Locations>(*_locations, before, after);
113
114     } else if (obj_T == "ARDOUR::TempoMap") {
115             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
116
117     } else if (obj_T == "ARDOUR::Playlist" || obj_T == "ARDOUR::AudioPlaylist" || obj_T == "ARDOUR::MidiPlaylist") {
118             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
119                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
120             }
121
122     } else if (obj_T == "ARDOUR::Route" || obj_T == "ARDOUR::AudioTrack" || obj_T == "ARDOUR::MidiTrack") {
123                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
124                         return new MementoCommand<Route>(*r, before, after);
125                 } else {
126                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
127                 }
128
129     } else if (obj_T == "Evoral::Curve" || obj_T == "ARDOUR::AutomationList") {
130             if (have_id) {
131                     std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
132                     if (i != automation_lists.end()) {
133                             return new MementoCommand<AutomationList>(*i->second, before, after);
134                     }
135             } else {
136                     return new MementoCommand<AutomationList> (
137                             new MidiAutomationListBinder (n, sources),
138                             before, after
139                             );
140             }
141             
142             cerr << "Alist not found\n";
143             
144     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
145             return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
146     }
147
148     /* we failed */
149     error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
150
151     return 0 ;
152 }
153
154 Command *
155 Session::stateful_diff_command_factory (XMLNode* n)
156 {
157         PBD::ID const id (n->property("obj-id")->value ());
158
159         string const obj_T = n->property ("type-name")->value ();
160         if ((obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion")) {
161                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
162                 if (r) {
163                         return new StatefulDiffCommand (r, *n);
164                 }
165
166         } else if (obj_T == "ARDOUR::AudioPlaylist" ||  obj_T == "ARDOUR::MidiPlaylist") {
167                 boost::shared_ptr<Playlist> p = playlists->by_id (id);
168                 if (p) {
169                         return new StatefulDiffCommand (p, *n);
170                 } else {
171                         cerr << "Playlist with ID = " << id << " not found\n";
172                 }
173         }
174
175         /* we failed */
176         
177         error << string_compose (
178                 _("could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s())
179               << endmsg;
180         
181         return 0;
182 }