Add some comments to the RegionSelection class.
[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/midi_events.h>
33 #include <ardour/smf_source.h>
34 #include <ardour/region_factory.h>
35
36 #include "automation_streamview.h"
37 #include "region_view.h"
38 #include "automation_region_view.h"
39 #include "automation_time_axis.h"
40 #include "canvas-simplerect.h"
41 #include "region_selection.h"
42 #include "selection.h"
43 #include "public_editor.h"
44 #include "ardour_ui.h"
45 #include "rgb_macros.h"
46 #include "gui_thread.h"
47 #include "utils.h"
48 #include "simplerect.h"
49 #include "simpleline.h"
50
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55
56 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
57         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
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         use_rec_regions = tv.editor.show_waveforms_recording ();
66 }
67
68 AutomationStreamView::~AutomationStreamView ()
69 {
70 }
71
72
73 RegionView*
74 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wfd)
75 {
76         if ( ! region) {
77                 cerr << "No region" << endl;
78                 return NULL;
79         }
80
81         if (wfd) {
82                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
83                 if (mr)
84                         mr->midi_source()->load_model();
85         }
86
87         const boost::shared_ptr<AutomationControl> control = region->control(_controller->controllable()->parameter());
88
89         boost::shared_ptr<AutomationList> list;
90         if (control)
91                 list = control->list();
92
93         AutomationRegionView *region_view;
94         std::list<RegionView *>::iterator i;
95
96         for (i = region_views.begin(); i != region_views.end(); ++i) {
97                 if ((*i)->region() == region) {
98                         
99                         /* great. we already have a MidiRegionView for this Region. use it again. */
100
101                         (*i)->set_valid (true);
102                         (*i)->enable_display(wfd);
103                         display_region(dynamic_cast<AutomationRegionView*>(*i));
104
105                         return NULL;
106                 }
107         }
108         
109         region_view = new AutomationRegionView (canvas_group, _automation_view, region, list,
110                         _samples_per_unit, region_color);
111                 
112         region_view->init (region_color, false);
113         region_views.push_front (region_view);
114         
115         /* follow global waveform setting */
116
117         if (wfd) {
118                 region_view->enable_display(true);
119                 //region_view->midi_region()->midi_source(0)->load_model();
120         }
121
122         display_region(region_view);
123
124         /* catch regionview going away */
125         region->GoingAway.connect (bind (mem_fun (*this, &AutomationStreamView::remove_region_view), region));
126         
127         RegionViewAdded (region_view);
128
129         return region_view;
130 }
131
132 void
133 AutomationStreamView::display_region(AutomationRegionView* region_view)
134 {
135         region_view->line().reset();
136 }
137
138 void
139 AutomationStreamView::redisplay_diskstream ()
140 {
141         list<RegionView *>::iterator i, tmp;
142
143         for (i = region_views.begin(); i != region_views.end(); ++i)
144                 (*i)->set_valid (false);
145         
146         if (_trackview.is_track()) {
147                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
148         }
149
150         for (i = region_views.begin(); i != region_views.end(); ) {
151                 tmp = i;
152                 tmp++;
153
154                 if (!(*i)->is_valid()) {
155                         delete *i;
156                         region_views.erase (i);
157                 } else {
158                         (*i)->enable_display(true);
159                         (*i)->set_y_position_and_height(0, height);
160                 }
161
162                 i = tmp;
163         }
164         
165         /* now fix layering */
166
167         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
168                 region_layered (*i);
169         }
170 }
171
172
173 void
174 AutomationStreamView::setup_rec_box ()
175 {
176 }
177
178 void
179 AutomationStreamView::update_rec_regions (nframes_t start, nframes_t dur)
180 {
181 }
182
183 void
184 AutomationStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur)
185 {
186         // this is called from the butler thread for now
187         
188         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AutomationStreamView::rec_data_range_ready), start, dur));
189         
190         this->update_rec_regions (start, dur);
191 }
192
193 void
194 AutomationStreamView::color_handler ()
195 {
196         /*if (_trackview.is_midi_track()) {
197                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
198         } 
199
200         if (!_trackview.is_midi_track()) {
201                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
202         }*/
203 }
204