Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / automation_region_view.cc
1 /*
2  * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2014-2017 Nick Mainsbridge <mainsbridge@gmail.com>
6  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <utility>
24
25 #include "pbd/memento_command.h"
26
27 #include "ardour/automation_control.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/midi_automation_list_binder.h"
30 #include "ardour/midi_region.h"
31 #include "ardour/session.h"
32
33 #include "gtkmm2ext/keyboard.h"
34
35 #include "automation_region_view.h"
36 #include "editing.h"
37 #include "editor.h"
38 #include "editor_drag.h"
39 #include "gui_thread.h"
40 #include "midi_automation_line.h"
41 #include "public_editor.h"
42 #include "ui_config.h"
43
44 #include "pbd/i18n.h"
45
46 AutomationRegionView::AutomationRegionView (ArdourCanvas::Container*                  parent,
47                                             AutomationTimeAxisView&                   time_axis,
48                                             boost::shared_ptr<ARDOUR::Region>         region,
49                                             const Evoral::Parameter&                  param,
50                                             boost::shared_ptr<ARDOUR::AutomationList> list,
51                                             double                                    spu,
52                                             uint32_t                                  basic_color)
53         : RegionView(parent, time_axis, region, spu, basic_color, true)
54         , _region_relative_time_converter(region->session().tempo_map(), region->position())
55         , _source_relative_time_converter(region->session().tempo_map(), region->position() - region->start())
56         , _parameter(param)
57 {
58         TimeAxisViewItem::set_position (_region->position(), this);
59
60         if (list) {
61                 assert(list->parameter() == param);
62                 create_line(list);
63         }
64
65         group->raise_to_top();
66
67         trackview.editor().MouseModeChanged.connect(_mouse_mode_connection, invalidator (*this),
68                                                     boost::bind (&AutomationRegionView::mouse_mode_changed, this),
69                                                     gui_context ());
70 }
71
72 AutomationRegionView::~AutomationRegionView ()
73 {
74         in_destructor = true;
75         RegionViewGoingAway (this); /* EMIT_SIGNAL */
76 }
77
78 void
79 AutomationRegionView::init (bool /*wfd*/)
80 {
81         _enable_display = false;
82
83         RegionView::init (false);
84
85         reset_width_dependent_items ((double) _region->length() / samples_per_pixel);
86
87         set_height (trackview.current_height());
88
89         fill_color_name = "midi frame base";
90         set_colors ();
91
92         _enable_display = true;
93 }
94
95 void
96 AutomationRegionView::create_line (boost::shared_ptr<ARDOUR::AutomationList> list)
97 {
98         _line = boost::shared_ptr<AutomationLine> (new MidiAutomationLine(
99                                 ARDOUR::EventTypeMap::instance().to_symbol(list->parameter()),
100                                 trackview, *get_canvas_group(), list,
101                                 boost::dynamic_pointer_cast<ARDOUR::MidiRegion> (_region),
102                                 _parameter,
103                                 &_source_relative_time_converter));
104         _line->set_colors();
105         _line->set_height ((uint32_t)rint(trackview.current_height() - 2.5 - NAME_HIGHLIGHT_SIZE));
106         _line->set_visibility (AutomationLine::VisibleAspects (AutomationLine::Line|AutomationLine::ControlPoints));
107         _line->set_maximum_time (_region->length());
108         _line->set_offset (_region->start ());
109 }
110
111 uint32_t
112 AutomationRegionView::get_fill_color() const
113 {
114         const std::string mod_name = (_dragging ? "dragging region" :
115                                       trackview.editor().internal_editing() ? "editable region" :
116                                       "midi frame base");
117         if (_selected) {
118                 return UIConfiguration::instance().color_mod ("selected region base", mod_name);
119         } else if (high_enough_for_name || !UIConfiguration::instance().get_color_regions_using_track_color()) {
120                 return UIConfiguration::instance().color_mod ("midi frame base", mod_name);
121         }
122         return UIConfiguration::instance().color_mod (fill_color, mod_name);
123 }
124
125 void
126 AutomationRegionView::mouse_mode_changed ()
127 {
128         /* Adjust frame colour (become more transparent for internal tools) */
129         set_frame_color();
130 }
131
132 bool
133 AutomationRegionView::canvas_group_event (GdkEvent* ev)
134 {
135         if (in_destructor) {
136                 return false;
137         }
138
139         PublicEditor& e = trackview.editor ();
140
141         if (trackview.editor().internal_editing() &&
142             ev->type == GDK_BUTTON_RELEASE &&
143             ev->button.button == 1 &&
144             e.current_mouse_mode() == Editing::MouseDraw &&
145             !e.drags()->active()) {
146
147                 double x = ev->button.x;
148                 double y = ev->button.y;
149
150                 /* convert to item coordinates in the time axis view */
151                 automation_view()->canvas_display()->canvas_to_item (x, y);
152
153                 /* clamp y */
154                 y = std::max (y, 0.0);
155                 y = std::min (y, _height - NAME_HIGHLIGHT_SIZE);
156
157                 /* guard points only if primary modifier is used */
158                 bool with_guard_points = Gtkmm2ext::Keyboard::modifier_state_equals (ev->button.state, Gtkmm2ext::Keyboard::PrimaryModifier);
159                 add_automation_event (ev, e.pixel_to_sample (x) - _region->position() + _region->start(), y, with_guard_points);
160                 return true;
161         }
162
163         return RegionView::canvas_group_event (ev);
164 }
165
166 /** @param when Position in samples, where 0 is the start of the region.
167  *  @param y y position, relative to our TimeAxisView.
168  */
169 void
170 AutomationRegionView::add_automation_event (GdkEvent *, samplepos_t when, double y, bool with_guard_points)
171 {
172         if (!_line) {
173                 boost::shared_ptr<Evoral::Control> c = _region->control(_parameter, true);
174                 boost::shared_ptr<ARDOUR::AutomationControl> ac
175                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(c);
176                 assert(ac);
177                 create_line(ac->alist());
178         }
179         assert(_line);
180
181         AutomationTimeAxisView* const view = automation_view ();
182
183         /* compute vertical fractional position */
184
185         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
186         y = 1.0 - (y / h);
187
188         /* snap sample */
189
190         when = snap_sample_to_sample (when - _region->start ()).sample + _region->start ();
191
192         /* map using line */
193
194         double when_d = when;
195         _line->view_to_model_coord (when_d, y);
196
197         XMLNode& before = _line->the_list()->get_state();
198
199         if (_line->the_list()->editor_add (when_d, y, with_guard_points)) {
200                 view->editor().begin_reversible_command (_("add automation event"));
201
202                 XMLNode& after = _line->the_list()->get_state();
203
204                 view->session()->add_command (new MementoCommand<ARDOUR::AutomationList> (_line->memento_command_binder(), &before, &after));
205                 view->editor().commit_reversible_command ();
206
207                 view->session()->set_dirty ();
208         }
209 }
210
211 bool
212 AutomationRegionView::paste (samplepos_t                                     pos,
213                              unsigned                                        paste_count,
214                              float                                           times,
215                              boost::shared_ptr<const ARDOUR::AutomationList> slist)
216 {
217         using namespace ARDOUR;
218
219         AutomationTimeAxisView* const             view    = automation_view();
220         boost::shared_ptr<ARDOUR::AutomationList> my_list = _line->the_list();
221
222         if (view->session()->transport_rolling() && my_list->automation_write()) {
223                 /* do not paste if this control is in write mode and we're rolling */
224                 return false;
225         }
226
227         AutomationType src_type = (AutomationType)slist->parameter().type ();
228         double len = slist->length();
229
230         /* add multi-paste offset if applicable */
231         if (parameter_is_midi (src_type)) {
232                 // convert length to samples (incl tempo-ramps)
233                 len = DoubleBeatsSamplesConverter (view->session()->tempo_map(), pos).to (len * paste_count);
234                 pos += view->editor ().get_paste_offset (pos, paste_count > 0 ? 1 : 0, len);
235         } else {
236                 pos += view->editor ().get_paste_offset (pos, paste_count, len);
237         }
238
239         /* convert sample-position to model's unit and position */
240         const double model_pos = _source_relative_time_converter.from (
241                 pos - _source_relative_time_converter.origin_b());
242
243         XMLNode& before = my_list->get_state();
244         my_list->paste(*slist, model_pos, DoubleBeatsSamplesConverter (view->session()->tempo_map(), pos));
245         view->session()->add_command(
246                 new MementoCommand<ARDOUR::AutomationList>(_line->memento_command_binder(), &before, &my_list->get_state()));
247
248         return true;
249 }
250
251 void
252 AutomationRegionView::set_height (double h)
253 {
254         RegionView::set_height(h);
255
256         if (_line) {
257                 _line->set_height ((uint32_t)rint(h - 2.5 - NAME_HIGHLIGHT_SIZE));
258         }
259 }
260
261 bool
262 AutomationRegionView::set_position (samplepos_t pos, void* src, double* ignored)
263 {
264         if (_line) {
265                 _line->set_maximum_time (_region->length ());
266         }
267
268         return RegionView::set_position(pos, src, ignored);
269 }
270
271
272 void
273 AutomationRegionView::reset_width_dependent_items (double pixel_width)
274 {
275         RegionView::reset_width_dependent_items(pixel_width);
276
277         if (_line) {
278                 _line->reset();
279         }
280 }
281
282
283 void
284 AutomationRegionView::region_resized (const PBD::PropertyChange& what_changed)
285 {
286         RegionView::region_resized (what_changed);
287
288         if (what_changed.contains (ARDOUR::Properties::position)) {
289                 _region_relative_time_converter.set_origin_b(_region->position());
290         }
291
292         if (what_changed.contains (ARDOUR::Properties::start) ||
293             what_changed.contains (ARDOUR::Properties::position)) {
294                 _source_relative_time_converter.set_origin_b (_region->position() - _region->start());
295         }
296
297         if (!_line) {
298                 return;
299         }
300
301         if (what_changed.contains (ARDOUR::Properties::start)) {
302                 _line->set_offset (_region->start ());
303         }
304
305         if (what_changed.contains (ARDOUR::Properties::length)) {
306                 _line->set_maximum_time (_region->length());
307         }
308 }
309
310
311 void
312 AutomationRegionView::entered ()
313 {
314         if (_line) {
315                 _line->track_entered();
316         }
317 }
318
319
320 void
321 AutomationRegionView::exited ()
322 {
323         if (_line) {
324                 _line->track_exited();
325         }
326 }