Fix font sizing in the big clock.
[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         , _pending_automation_state (Off)
62 {
63         //canvas_rect->property_fill_color_rgba() = stream_base_color;
64         canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
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 = boost::dynamic_pointer_cast<AutomationControl> (
87                 region->control (_controller->controllable()->parameter(), true)
88                 );
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->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
133
134         /* setup automation state for this region */
135         boost::shared_ptr<AutomationLine> line = region_view->line ();
136         if (line && line->the_list()) {
137                 line->the_list()->set_automation_state (automation_state ());
138         }
139         
140         RegionViewAdded (region_view);
141
142         return region_view;
143 }
144
145 void
146 AutomationStreamView::display_region(AutomationRegionView* region_view)
147 {
148         region_view->line().reset();
149 }
150
151 void
152 AutomationStreamView::set_automation_state (AutoState state)
153 {
154         /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
155         
156         if (region_views.empty()) {
157                 _pending_automation_state = state;
158         } else {
159                 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
160
161                 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
162                         if ((*i)->the_list()) {
163                                 (*i)->the_list()->set_automation_state (state);
164                         }
165                 }
166         }
167 }
168
169 void
170 AutomationStreamView::redisplay_track ()
171 {
172         list<RegionView *>::iterator i, tmp;
173
174         // Flag region views as invalid and disable drawing
175         for (i = region_views.begin(); i != region_views.end(); ++i) {
176                 (*i)->set_valid (false);
177                 (*i)->enable_display(false);
178         }
179
180         // Add and display region views, and flag them as valid
181         if (_trackview.is_track()) {
182                 _trackview.track()->playlist()->foreach_region (
183                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
184                         );
185         }
186
187         // Stack regions by layer, and remove invalid regions
188         layer_regions();
189 }
190
191
192 void
193 AutomationStreamView::setup_rec_box ()
194 {
195 }
196
197 void
198 AutomationStreamView::color_handler ()
199 {
200         /*if (_trackview.is_midi_track()) {
201                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
202         }
203
204         if (!_trackview.is_midi_track()) {
205                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
206         }*/
207 }
208
209 AutoState
210 AutomationStreamView::automation_state () const
211 {
212         if (region_views.empty()) {
213                 return _pending_automation_state;
214         }
215
216         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
217         if (!line || !line->the_list()) {
218                 return Off;
219         }
220
221         return line->the_list()->automation_state ();
222 }
223
224 bool
225 AutomationStreamView::has_automation () const
226 {
227         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
228         
229         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
230                 if ((*i)->npoints() > 0) {
231                         return true;
232                 }
233         }
234
235         return false;
236 }
237
238 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
239  *  InterpolationStyle; tell the AutomationLists in our regions.
240  */
241 void
242 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
243 {
244         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
245         
246         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
247                 (*i)->the_list()->set_interpolation (s);
248         }
249 }
250
251 AutomationList::InterpolationStyle
252 AutomationStreamView::interpolation () const
253 {
254         if (region_views.empty()) {
255                 return AutomationList::Linear;
256         }
257
258         AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
259         assert (v);
260
261         return v->line()->the_list()->interpolation ();
262 }
263
264 /** Clear all automation displayed in this view */
265 void
266 AutomationStreamView::clear ()
267 {
268         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
269         
270         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
271                 (*i)->clear ();
272         }
273 }
274
275 /** @param start Start position in session frames.
276  *  @param end End position in session frames.
277  *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
278  *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
279  *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
280  *  confusing.
281  */
282 void
283 AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
284 {
285         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
286                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
287                 assert (arv);
288                 arv->line()->get_selectables (start, end, botfrac, topfrac, results);
289         }
290 }
291
292 void
293 AutomationStreamView::set_selected_points (PointSelection& ps)
294 {
295         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
296         
297         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
298                 (*i)->set_selected_points (ps);
299         }
300 }
301
302 list<boost::shared_ptr<AutomationLine> >
303 AutomationStreamView::get_lines () const
304 {
305         list<boost::shared_ptr<AutomationLine> > lines;
306         
307         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
308                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
309                 assert (arv);
310                 lines.push_back (arv->line());
311         }
312
313         return lines;
314 }
315
316 struct RegionPositionSorter {
317         bool operator() (RegionView* a, RegionView* b) {
318                 return a->region()->position() < b->region()->position();
319         }
320 };
321         
322
323 /** @param pos Position, in session frames.
324  *  @return AutomationLine to paste to for that position, or 0 if there is none appropriate.
325  */
326 boost::shared_ptr<AutomationLine>
327 AutomationStreamView::paste_line (framepos_t pos)
328 {
329         /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
330
331         if (region_views.empty()) {
332                 return boost::shared_ptr<AutomationLine> ();
333         }
334
335         region_views.sort (RegionPositionSorter ());
336
337         list<RegionView*>::const_iterator prev = region_views.begin ();
338
339         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
340                 if ((*i)->region()->position() > pos) {
341                         break;
342                 }
343                 prev = i;
344         }
345
346         boost::shared_ptr<Region> r = (*prev)->region ();
347
348         /* If *prev doesn't cover pos, it's no good */
349         if (r->position() > pos || ((r->position() + r->length()) < pos)) {
350                 return boost::shared_ptr<AutomationLine> ();
351         }
352
353         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
354         assert (arv);
355
356         return arv->line ();
357 }