Fix some confusion about handling of route extra XML. Fixes #3574.
[ardour.git] / gtk2_ardour / automation_streamview.cc
1 /*
2     Copyright (C) 2001-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 #include <cmath>
20 #include <cassert>
21 #include <utility>
22
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/midi_region.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/region_factory.h"
30
31 #include "automation_streamview.h"
32 #include "region_view.h"
33 #include "automation_region_view.h"
34 #include "automation_time_axis.h"
35 #include "canvas-simplerect.h"
36 #include "region_selection.h"
37 #include "selection.h"
38 #include "public_editor.h"
39 #include "ardour_ui.h"
40 #include "rgb_macros.h"
41 #include "gui_thread.h"
42 #include "utils.h"
43 #include "simplerect.h"
44 #include "simpleline.h"
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
49 using namespace Editing;
50
51 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
52         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
53                       new ArdourCanvas::Group(*tv.canvas_background()),
54                       new ArdourCanvas::Group(*tv.canvas_display()))
55         , _controller(tv.controller())
56         , _automation_view(tv)
57         , _pending_automation_state (Off)
58 {
59         //canvas_rect->property_fill_color_rgba() = stream_base_color;
60         canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
61 }
62
63 AutomationStreamView::~AutomationStreamView ()
64 {
65 }
66
67
68 RegionView*
69 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wfd, bool /*recording*/)
70 {
71         if ( ! region) {
72                 cerr << "No region" << endl;
73                 return NULL;
74         }
75
76         if (wfd) {
77                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
78                 if (mr) {
79                         mr->midi_source()->load_model();
80                 }
81         }
82
83         const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
84                 region->control (_controller->controllable()->parameter(), true)
85                 );
86
87         boost::shared_ptr<AutomationList> list;
88         if (control) {
89                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
90                 assert(!control->list() || list);
91         }
92
93         AutomationRegionView *region_view;
94         std::list<RegionView *>::iterator i;
95
96         for (i = region_views.begin(); i != region_views.end(); ++i) {
97                 if ((*i)->region() == region) {
98
99                         /* great. we already have an AutomationRegionView for this Region. use it again. */
100                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
101
102                         if (arv->line())
103                                 arv->line()->set_list (list);
104                         (*i)->set_valid (true);
105                         (*i)->enable_display(wfd);
106                         display_region(arv);
107
108                         return NULL;
109                 }
110         }
111
112         region_view = new AutomationRegionView (_canvas_group, _automation_view, region,
113                         _controller->controllable()->parameter(), list,
114                         _samples_per_unit, region_color);
115
116         region_view->init (region_color, false);
117         region_views.push_front (region_view);
118
119         /* follow global waveform setting */
120
121         if (wfd) {
122                 region_view->enable_display(true);
123                 //region_view->midi_region()->midi_source(0)->load_model();
124         }
125
126         display_region(region_view);
127
128         /* catch regionview going away */
129         region->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
130
131         /* setup automation state for this region */
132         boost::shared_ptr<AutomationLine> line = region_view->line ();
133         if (line && line->the_list()) {
134                 line->the_list()->set_automation_state (automation_state ());
135         }
136         
137         RegionViewAdded (region_view);
138
139         return region_view;
140 }
141
142 void
143 AutomationStreamView::display_region(AutomationRegionView* region_view)
144 {
145         region_view->line().reset();
146 }
147
148 void
149 AutomationStreamView::set_automation_state (AutoState state)
150 {
151         /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
152         
153         if (region_views.empty()) {
154                 _pending_automation_state = state;
155         } else {
156                 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
157
158                 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
159                         if ((*i)->the_list()) {
160                                 (*i)->the_list()->set_automation_state (state);
161                         }
162                 }
163         }
164 }
165
166 void
167 AutomationStreamView::redisplay_track ()
168 {
169         list<RegionView *>::iterator i, tmp;
170
171         // Flag region views as invalid and disable drawing
172         for (i = region_views.begin(); i != region_views.end(); ++i) {
173                 (*i)->set_valid (false);
174                 (*i)->enable_display(false);
175         }
176
177         // Add and display region views, and flag them as valid
178         if (_trackview.is_track()) {
179                 _trackview.track()->playlist()->foreach_region (
180                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
181                         );
182         }
183
184         // Stack regions by layer, and remove invalid regions
185         layer_regions();
186 }
187
188
189 void
190 AutomationStreamView::setup_rec_box ()
191 {
192 }
193
194 void
195 AutomationStreamView::color_handler ()
196 {
197         /*if (_trackview.is_midi_track()) {
198                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
199         }
200
201         if (!_trackview.is_midi_track()) {
202                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
203         }*/
204 }
205
206 AutoState
207 AutomationStreamView::automation_state () const
208 {
209         if (region_views.empty()) {
210                 return _pending_automation_state;
211         }
212
213         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
214         if (!line || !line->the_list()) {
215                 return Off;
216         }
217
218         return line->the_list()->automation_state ();
219 }
220
221 bool
222 AutomationStreamView::has_automation () const
223 {
224         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
225         
226         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
227                 if ((*i)->npoints() > 0) {
228                         return true;
229                 }
230         }
231
232         return false;
233 }
234
235 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
236  *  InterpolationStyle; tell the AutomationLists in our regions.
237  */
238 void
239 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
240 {
241         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
242         
243         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
244                 (*i)->the_list()->set_interpolation (s);
245         }
246 }
247
248 AutomationList::InterpolationStyle
249 AutomationStreamView::interpolation () const
250 {
251         if (region_views.empty()) {
252                 return AutomationList::Linear;
253         }
254
255         AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
256         assert (v);
257
258         return v->line()->the_list()->interpolation ();
259 }
260
261 /** Clear all automation displayed in this view */
262 void
263 AutomationStreamView::clear ()
264 {
265         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
266         
267         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
268                 (*i)->clear ();
269         }
270 }
271
272 /** @param start Start position in session frames.
273  *  @param end End position in session frames.
274  *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
275  *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
276  *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
277  *  confusing.
278  */
279 void
280 AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
281 {
282         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
283                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
284                 assert (arv);
285                 arv->line()->get_selectables (start, end, botfrac, topfrac, results);
286         }
287 }
288
289 void
290 AutomationStreamView::set_selected_points (PointSelection& ps)
291 {
292         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
293         
294         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
295                 (*i)->set_selected_points (ps);
296         }
297 }
298
299 list<boost::shared_ptr<AutomationLine> >
300 AutomationStreamView::get_lines () const
301 {
302         list<boost::shared_ptr<AutomationLine> > lines;
303         
304         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
305                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
306                 assert (arv);
307                 lines.push_back (arv->line());
308         }
309
310         return lines;
311 }
312
313 struct RegionPositionSorter {
314         bool operator() (RegionView* a, RegionView* b) {
315                 return a->region()->position() < b->region()->position();
316         }
317 };
318         
319
320 /** @param pos Position, in session frames.
321  *  @return AutomationLine to paste to for that position, or 0 if there is none appropriate.
322  */
323 boost::shared_ptr<AutomationLine>
324 AutomationStreamView::paste_line (framepos_t pos)
325 {
326         /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
327
328         if (region_views.empty()) {
329                 return boost::shared_ptr<AutomationLine> ();
330         }
331
332         region_views.sort (RegionPositionSorter ());
333
334         list<RegionView*>::const_iterator prev = region_views.begin ();
335
336         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
337                 if ((*i)->region()->position() > pos) {
338                         break;
339                 }
340                 prev = i;
341         }
342
343         boost::shared_ptr<Region> r = (*prev)->region ();
344
345         /* If *prev doesn't cover pos, it's no good */
346         if (r->position() > pos || ((r->position() + r->length()) < pos)) {
347                 return boost::shared_ptr<AutomationLine> ();
348         }
349
350         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
351         assert (arv);
352
353         return arv->line ();
354 }