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