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