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