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