Merge branch 'master' into cairocanvas
[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 "pbd/compose.h"
28 #include "canvas/debug.h"
29
30 #include "ardour/midi_region.h"
31 #include "ardour/midi_source.h"
32
33 #include "automation_streamview.h"
34 #include "region_view.h"
35 #include "automation_region_view.h"
36 #include "automation_time_axis.h"
37 #include "region_selection.h"
38 #include "selection.h"
39 #include "public_editor.h"
40 #include "ardour_ui.h"
41 #include "rgb_macros.h"
42 #include "gui_thread.h"
43 #include "utils.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47 using namespace PBD;
48 using namespace Editing;
49
50 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
51         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
52                       tv.canvas_display())
53         , _automation_view(tv)
54         , _pending_automation_state (Off)
55 {
56         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group auto %1", tv.name()));
57         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle auto %1", tv.name()));
58
59         canvas_rect->set_fill (false);
60         canvas_rect->set_outline_color (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 wait_for_data, bool /*recording*/)
70 {
71         assert (region);
72
73         if (wait_for_data) {
74                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
75                 if (mr) {
76                         mr->midi_source()->load_model();
77                 }
78         }
79
80         const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
81                 region->control (_automation_view.parameter(), true)
82                 );
83
84         boost::shared_ptr<AutomationList> list;
85         if (control) {
86                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
87                 assert(!control->list() || list);
88         }
89
90         AutomationRegionView *region_view;
91         std::list<RegionView *>::iterator i;
92
93         for (i = region_views.begin(); i != region_views.end(); ++i) {
94                 if ((*i)->region() == region) {
95
96                         /* great. we already have an AutomationRegionView for this Region. use it again. */
97                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
98
99                         if (arv->line()) {
100                                 arv->line()->set_list (list);
101                         }
102                         (*i)->set_valid (true);
103                         (*i)->enable_display (wait_for_data);
104                         display_region(arv);
105
106                         return 0;
107                 }
108         }
109
110         region_view = new AutomationRegionView (
111                 _canvas_group, _automation_view, region,
112                 _automation_view.parameter (), list,
113                 _samples_per_pixel, region_color
114                 );
115
116         region_view->init (region_color, false);
117         region_views.push_front (region_view);
118
119         /* follow global waveform setting */
120
121         if (wait_for_data) {
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         // Flag region views as invalid and disable drawing
170         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
171                 (*i)->set_valid (false);
172                 (*i)->enable_display(false);
173         }
174
175         // Add and display region views, and flag them as valid
176         if (_trackview.is_track()) {
177                 _trackview.track()->playlist()->foreach_region (
178                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
179                         );
180         }
181
182         // Stack regions by layer, and remove invalid regions
183         layer_regions();
184 }
185
186
187 void
188 AutomationStreamView::setup_rec_box ()
189 {
190 }
191
192 void
193 AutomationStreamView::color_handler ()
194 {
195         /*if (_trackview.is_midi_track()) {
196                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->get_canvasvar_MidiTrackBase();
197         }
198
199         if (!_trackview.is_midi_track()) {
200                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->get_canvasvar_MidiBusBase();;
201         }*/
202 }
203
204 AutoState
205 AutomationStreamView::automation_state () const
206 {
207         if (region_views.empty()) {
208                 return _pending_automation_state;
209         }
210
211         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
212         if (!line || !line->the_list()) {
213                 return Off;
214         }
215
216         return line->the_list()->automation_state ();
217 }
218
219 bool
220 AutomationStreamView::has_automation () const
221 {
222         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
223
224         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
225                 if ((*i)->npoints() > 0) {
226                         return true;
227                 }
228         }
229
230         return false;
231 }
232
233 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
234  *  InterpolationStyle; tell the AutomationLists in our regions.
235  */
236 void
237 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
238 {
239         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
240
241         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
242                 (*i)->the_list()->set_interpolation (s);
243         }
244 }
245
246 AutomationList::InterpolationStyle
247 AutomationStreamView::interpolation () const
248 {
249         if (region_views.empty()) {
250                 return AutomationList::Linear;
251         }
252
253         AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
254         if (v) {
255                 return v->line()->the_list()->interpolation ();
256         }
257         return AutomationList::Linear;
258 }
259
260 /** Clear all automation displayed in this view */
261 void
262 AutomationStreamView::clear ()
263 {
264         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
265
266         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
267                 (*i)->clear ();
268         }
269 }
270
271 /** @param start Start position in session frames.
272  *  @param end End position in session frames.
273  *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
274  *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
275  *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
276  *  confusing.
277  */
278 void
279 AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
280 {
281         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
282                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
283                 assert (arv);
284                 arv->line()->get_selectables (start, end, botfrac, topfrac, results);
285         }
286 }
287
288 void
289 AutomationStreamView::set_selected_points (PointSelection& ps)
290 {
291         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
292
293         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
294                 (*i)->set_selected_points (ps);
295         }
296 }
297
298 list<boost::shared_ptr<AutomationLine> >
299 AutomationStreamView::get_lines () const
300 {
301         list<boost::shared_ptr<AutomationLine> > lines;
302
303         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
304                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
305                 assert (arv);
306                 lines.push_back (arv->line());
307         }
308
309         return lines;
310 }
311
312 struct RegionPositionSorter {
313         bool operator() (RegionView* a, RegionView* b) {
314                 return a->region()->position() < b->region()->position();
315         }
316 };
317
318
319 /** @param pos Position, in session frames.
320  *  @return AutomationLine to paste to for that position, or 0 if there is none appropriate.
321  */
322 boost::shared_ptr<AutomationLine>
323 AutomationStreamView::paste_line (framepos_t pos)
324 {
325         /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
326
327         if (region_views.empty()) {
328                 return boost::shared_ptr<AutomationLine> ();
329         }
330
331         region_views.sort (RegionPositionSorter ());
332
333         list<RegionView*>::const_iterator prev = region_views.begin ();
334
335         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
336                 if ((*i)->region()->position() > pos) {
337                         break;
338                 }
339                 prev = i;
340         }
341
342         boost::shared_ptr<Region> r = (*prev)->region ();
343
344         /* If *prev doesn't cover pos, it's no good */
345         if (r->position() > pos || ((r->position() + r->length()) < pos)) {
346                 return boost::shared_ptr<AutomationLine> ();
347         }
348
349         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
350         assert (arv);
351
352         return arv->line ();
353 }