refactor MidiRegionView event handling to avoid too much spaghetti as we move along
[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_playlist.h"
28 #include "ardour/midi_region.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_diskstream.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/smf_source.h"
33 #include "ardour/region_factory.h"
34
35 #include "automation_streamview.h"
36 #include "region_view.h"
37 #include "automation_region_view.h"
38 #include "automation_time_axis.h"
39 #include "canvas-simplerect.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
46 #include "utils.h"
47 #include "simplerect.h"
48 #include "simpleline.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Editing;
54
55 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
56         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
57                       new ArdourCanvas::Group(*tv.canvas_background()),
58                       new ArdourCanvas::Group(*tv.canvas_display()))
59         , _controller(tv.controller())
60         , _automation_view(tv)
61 {
62         //canvas_rect->property_fill_color_rgba() = stream_base_color;
63         canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
64 }
65
66 AutomationStreamView::~AutomationStreamView ()
67 {
68 }
69
70
71 RegionView*
72 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wfd, bool /*recording*/)
73 {
74         if ( ! region) {
75                 cerr << "No region" << endl;
76                 return NULL;
77         }
78
79         if (wfd) {
80                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
81                 if (mr)
82                         mr->midi_source()->load_model();
83         }
84
85         const boost::shared_ptr<AutomationControl> control
86                 = boost::dynamic_pointer_cast<AutomationControl>(
87                                 region->control(_controller->controllable()->parameter()));
88
89         boost::shared_ptr<AutomationList> list;
90         if (control) {
91                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
92                 assert(!control->list() || list);
93         }
94
95         AutomationRegionView *region_view;
96         std::list<RegionView *>::iterator i;
97
98         for (i = region_views.begin(); i != region_views.end(); ++i) {
99                 if ((*i)->region() == region) {
100
101                         /* great. we already have an AutomationRegionView for this Region. use it again. */
102                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
103
104                         if (arv->line())
105                                 arv->line()->set_list (list);
106                         (*i)->set_valid (true);
107                         (*i)->enable_display(wfd);
108                         display_region(arv);
109
110                         return NULL;
111                 }
112         }
113
114         region_view = new AutomationRegionView (_canvas_group, _automation_view, region,
115                         _controller->controllable()->parameter(), list,
116                         _samples_per_unit, region_color);
117
118         region_view->init (region_color, false);
119         region_views.push_front (region_view);
120
121         /* follow global waveform setting */
122
123         if (wfd) {
124                 region_view->enable_display(true);
125                 //region_view->midi_region()->midi_source(0)->load_model();
126         }
127
128         display_region(region_view);
129
130         /* catch regionview going away */
131         region->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
132
133         RegionViewAdded (region_view);
134
135         return region_view;
136 }
137
138 void
139 AutomationStreamView::display_region(AutomationRegionView* region_view)
140 {
141         region_view->line().reset();
142 }
143
144 void
145 AutomationStreamView::set_automation_state (AutoState state)
146 {
147         std::list<RegionView *>::iterator i;
148         for (i = region_views.begin(); i != region_views.end(); ++i) {
149                 boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*)(*i))->line();
150                 if (line && line->the_list()) {
151                         line->the_list()->set_automation_state (state);
152                 }
153         }
154 }
155
156 void
157 AutomationStreamView::redisplay_track ()
158 {
159         list<RegionView *>::iterator i, tmp;
160
161         // Flag region views as invalid and disable drawing
162         for (i = region_views.begin(); i != region_views.end(); ++i) {
163                 (*i)->set_valid (false);
164                 (*i)->enable_display(false);
165         }
166
167         // Add and display region views, and flag them as valid
168         if (_trackview.is_track()) {
169                 _trackview.track()->playlist()->foreach_region (
170                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
171                         );
172         }
173
174         // Stack regions by layer, and remove invalid regions
175         layer_regions();
176 }
177
178
179 void
180 AutomationStreamView::setup_rec_box ()
181 {
182 }
183
184 void
185 AutomationStreamView::color_handler ()
186 {
187         /*if (_trackview.is_midi_track()) {
188                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
189         }
190
191         if (!_trackview.is_midi_track()) {
192                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
193         }*/
194 }
195
196 AutoState
197 AutomationStreamView::automation_state () const
198 {
199         /* XXX: bit of a hack: just return the state of our first RegionView */
200
201         if (region_views.empty()) {
202                 return Off;
203         }
204
205         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
206         if (!line || !line->the_list()) {
207                 return Off;
208         }
209
210         return line->the_list()->automation_state ();
211 }
212
213 bool
214 AutomationStreamView::has_automation () const
215 {
216         list<RegionView*>::const_iterator i = region_views.begin ();
217         while (i != region_views.end()) {
218                 AutomationRegionView* rv = static_cast<AutomationRegionView*> (*i);
219                 if (rv->line() && rv->line()->npoints() > 0) {
220                         return true;
221                 }
222                 ++i;
223         }
224
225         return false;
226 }
227
228 void
229 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
230 {
231         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
232                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
233                 assert (arv);
234                 if (arv->line()) {
235                         arv->line()->set_interpolation (s);
236                 }
237         }
238 }