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>
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.
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.
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.
25 #include "pbd/memento_command.h"
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"
33 #include "gtkmm2ext/keyboard.h"
35 #include "automation_region_view.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"
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,
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())
58 TimeAxisViewItem::set_position (_region->position(), this);
61 assert(list->parameter() == param);
65 group->raise_to_top();
67 trackview.editor().MouseModeChanged.connect(_mouse_mode_connection, invalidator (*this),
68 boost::bind (&AutomationRegionView::mouse_mode_changed, this),
72 AutomationRegionView::~AutomationRegionView ()
75 RegionViewGoingAway (this); /* EMIT_SIGNAL */
79 AutomationRegionView::init (bool /*wfd*/)
81 _enable_display = false;
83 RegionView::init (false);
85 reset_width_dependent_items ((double) _region->length() / samples_per_pixel);
87 set_height (trackview.current_height());
89 fill_color_name = "midi frame base";
92 _enable_display = true;
96 AutomationRegionView::create_line (boost::shared_ptr<ARDOUR::AutomationList> list)
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),
103 &_source_relative_time_converter));
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 ());
112 AutomationRegionView::get_fill_color() const
114 const std::string mod_name = (_dragging ? "dragging region" :
115 trackview.editor().internal_editing() ? "editable region" :
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);
122 return UIConfiguration::instance().color_mod (fill_color, mod_name);
126 AutomationRegionView::mouse_mode_changed ()
128 /* Adjust frame colour (become more transparent for internal tools) */
133 AutomationRegionView::canvas_group_event (GdkEvent* ev)
139 PublicEditor& e = trackview.editor ();
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()) {
147 double x = ev->button.x;
148 double y = ev->button.y;
150 /* convert to item coordinates in the time axis view */
151 automation_view()->canvas_display()->canvas_to_item (x, y);
154 y = std::max (y, 0.0);
155 y = std::min (y, _height - NAME_HIGHLIGHT_SIZE);
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);
163 return RegionView::canvas_group_event (ev);
166 /** @param when Position in samples, where 0 is the start of the region.
167 * @param y y position, relative to our TimeAxisView.
170 AutomationRegionView::add_automation_event (GdkEvent *, samplepos_t when, double y, bool with_guard_points)
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);
177 create_line(ac->alist());
181 AutomationTimeAxisView* const view = automation_view ();
183 /* compute vertical fractional position */
185 const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
190 when = snap_sample_to_sample (when - _region->start ()).sample + _region->start ();
194 double when_d = when;
195 _line->view_to_model_coord (when_d, y);
197 XMLNode& before = _line->the_list()->get_state();
199 if (_line->the_list()->editor_add (when_d, y, with_guard_points)) {
200 view->editor().begin_reversible_command (_("add automation event"));
202 XMLNode& after = _line->the_list()->get_state();
204 view->session()->add_command (new MementoCommand<ARDOUR::AutomationList> (_line->memento_command_binder(), &before, &after));
205 view->editor().commit_reversible_command ();
207 view->session()->set_dirty ();
212 AutomationRegionView::paste (samplepos_t pos,
213 unsigned paste_count,
215 boost::shared_ptr<const ARDOUR::AutomationList> slist)
217 using namespace ARDOUR;
219 AutomationTimeAxisView* const view = automation_view();
220 boost::shared_ptr<ARDOUR::AutomationList> my_list = _line->the_list();
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 */
227 AutomationType src_type = (AutomationType)slist->parameter().type ();
228 double len = slist->length();
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);
236 pos += view->editor ().get_paste_offset (pos, paste_count, len);
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());
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()));
252 AutomationRegionView::set_height (double h)
254 RegionView::set_height(h);
257 _line->set_height ((uint32_t)rint(h - 2.5 - NAME_HIGHLIGHT_SIZE));
262 AutomationRegionView::set_position (samplepos_t pos, void* src, double* ignored)
265 _line->set_maximum_time (_region->length ());
268 return RegionView::set_position(pos, src, ignored);
273 AutomationRegionView::reset_width_dependent_items (double pixel_width)
275 RegionView::reset_width_dependent_items(pixel_width);
284 AutomationRegionView::region_resized (const PBD::PropertyChange& what_changed)
286 RegionView::region_resized (what_changed);
288 if (what_changed.contains (ARDOUR::Properties::position)) {
289 _region_relative_time_converter.set_origin_b(_region->position());
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());
301 if (what_changed.contains (ARDOUR::Properties::start)) {
302 _line->set_offset (_region->start ());
305 if (what_changed.contains (ARDOUR::Properties::length)) {
306 _line->set_maximum_time (_region->length());
312 AutomationRegionView::entered ()
315 _line->track_entered();
321 AutomationRegionView::exited ()
324 _line->track_exited();