b02d511d07e266df2f48e70e9f559b954ec3f798
[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_display()))
58         , _controller(tv.controller())
59         , _automation_view(tv)
60 {
61         //canvas_rect->property_fill_color_rgba() = stream_base_color;
62         canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
63
64         use_rec_regions = tv.editor().show_waveforms_recording ();
65 }
66
67 AutomationStreamView::~AutomationStreamView ()
68 {
69 }
70
71
72 RegionView*
73 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wfd, bool recording)
74 {
75         if ( ! region) {
76                 cerr << "No region" << endl;
77                 return NULL;
78         }
79
80         if (wfd) {
81                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
82                 if (mr)
83                         mr->midi_source()->load_model();
84         }
85
86         const boost::shared_ptr<AutomationControl> control
87                 = boost::dynamic_pointer_cast<AutomationControl>(
88                                 region->control(_controller->controllable()->parameter()));
89
90         boost::shared_ptr<AutomationList> list;
91         if (control) {
92                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
93                 assert(!control->list() || list);
94         }
95
96         AutomationRegionView *region_view;
97         std::list<RegionView *>::iterator i;
98
99         for (i = region_views.begin(); i != region_views.end(); ++i) {
100                 if ((*i)->region() == region) {
101                         
102                         /* great. we already have an AutomationRegionView for this Region. use it again. */
103                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
104
105                         if (arv->line())
106                                 arv->line()->set_list (list);
107                         (*i)->set_valid (true);
108                         (*i)->enable_display(wfd);
109                         display_region(arv);
110
111                         return NULL;
112                 }
113         }
114         
115         region_view = new AutomationRegionView (canvas_group, _automation_view, region,
116                         _controller->controllable()->parameter(), list,
117                         _samples_per_unit, region_color);
118                 
119         region_view->init (region_color, false);
120         region_views.push_front (region_view);
121         
122         /* follow global waveform setting */
123
124         if (wfd) {
125                 region_view->enable_display(true);
126                 //region_view->midi_region()->midi_source(0)->load_model();
127         }
128
129         display_region(region_view);
130
131         /* catch regionview going away */
132         region->GoingAway.connect (bind (mem_fun (*this, &AutomationStreamView::remove_region_view), region));
133         
134         RegionViewAdded (region_view);
135
136         return region_view;
137 }
138
139 void
140 AutomationStreamView::display_region(AutomationRegionView* region_view)
141 {
142         region_view->line().reset();
143 }
144
145 void
146 AutomationStreamView::set_automation_state (AutoState state)
147 {
148         std::list<RegionView *>::iterator i;
149         for (i = region_views.begin(); i != region_views.end(); ++i) {
150                 boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*)(*i))->line();
151                 if (line && line->the_list())
152                         line->the_list()->set_automation_state (state);
153         }
154 }
155
156 void
157 AutomationStreamView::redisplay_diskstream ()
158 {
159         list<RegionView *>::iterator i, tmp;
160
161         for (i = region_views.begin(); i != region_views.end(); ++i)
162                 (*i)->set_valid (false);
163         
164         if (_trackview.is_track()) {
165                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
166         }
167
168         for (i = region_views.begin(); i != region_views.end(); ) {
169                 tmp = i;
170                 tmp++;
171
172                 if (!(*i)->is_valid()) {
173                         delete *i;
174                         region_views.erase (i);
175                 } else {
176                         (*i)->enable_display(true);
177                         (*i)->set_height(height);
178                 }
179
180                 i = tmp;
181         }
182         
183         /* now fix layering */
184
185         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
186                 region_layered (*i);
187         }
188 }
189
190
191 void
192 AutomationStreamView::setup_rec_box ()
193 {
194 }
195
196 void
197 AutomationStreamView::update_rec_regions (nframes_t start, nframes_t dur)
198 {
199 }
200
201 void
202 AutomationStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur)
203 {
204         // this is called from the butler thread for now
205         
206         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AutomationStreamView::rec_data_range_ready), start, dur));
207         
208         this->update_rec_regions (start, dur);
209 }
210
211 void
212 AutomationStreamView::color_handler ()
213 {
214         /*if (_trackview.is_midi_track()) {
215                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
216         } 
217
218         if (!_trackview.is_midi_track()) {
219                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
220         }*/
221 }
222