ab13a99c16034819b243c41f62627e268ff3e4fc
[ardour.git] / gtk2_ardour / automation_region_view.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <utility>
21
22 #include "pbd/memento_command.h"
23
24 #include "ardour/automation_control.h"
25 #include "ardour/event_type_map.h"
26 #include "ardour/midi_automation_list_binder.h"
27 #include "ardour/midi_region.h"
28 #include "ardour/session.h"
29
30 #include "gtkmm2ext/keyboard.h"
31
32 #include "automation_region_view.h"
33 #include "editing.h"
34 #include "editor.h"
35 #include "editor_drag.h"
36 #include "gui_thread.h"
37 #include "midi_automation_line.h"
38 #include "public_editor.h"
39
40 #include "i18n.h"
41
42 AutomationRegionView::AutomationRegionView (ArdourCanvas::Container*                      parent,
43                                             AutomationTimeAxisView&                   time_axis,
44                                             boost::shared_ptr<ARDOUR::Region>         region,
45                                             const Evoral::Parameter&                  param,
46                                             boost::shared_ptr<ARDOUR::AutomationList> list,
47                                             double                                    spu,
48                                             uint32_t                                  basic_color)
49         : RegionView(parent, time_axis, region, spu, basic_color, true)
50         , _parameter(param)
51 {
52         if (list) {
53                 assert(list->parameter() == param);
54                 create_line(list);
55         }
56
57         group->raise_to_top();
58 }
59
60 AutomationRegionView::~AutomationRegionView ()
61 {
62         RegionViewGoingAway (this); /* EMIT_SIGNAL */
63 }
64
65 void
66 AutomationRegionView::init (bool /*wfd*/)
67 {
68         _enable_display = false;
69
70         RegionView::init (false);
71
72         reset_width_dependent_items ((double) _region->length() / samples_per_pixel);
73
74         set_height (trackview.current_height());
75
76         set_colors ();
77
78         _enable_display = true;
79 }
80
81 void
82 AutomationRegionView::create_line (boost::shared_ptr<ARDOUR::AutomationList> list)
83 {
84         _line = boost::shared_ptr<AutomationLine> (new MidiAutomationLine(
85                                 ARDOUR::EventTypeMap::instance().to_symbol(list->parameter()),
86                                 trackview, *get_canvas_group(), list,
87                                 boost::dynamic_pointer_cast<ARDOUR::MidiRegion> (_region),
88                                 _parameter,
89                                 &_source_relative_time_converter));
90         _line->set_colors();
91         _line->set_height ((uint32_t)rint(trackview.current_height() - NAME_HIGHLIGHT_SIZE));
92         _line->set_visibility (AutomationLine::VisibleAspects (AutomationLine::Line|AutomationLine::ControlPoints));
93         _line->set_maximum_time (_region->length());
94         _line->set_offset (_region->start ());
95 }
96
97 bool
98 AutomationRegionView::canvas_group_event (GdkEvent* ev)
99 {
100         if (in_destructor) {
101                 return false;
102         }
103
104         PublicEditor& e = trackview.editor ();
105
106         if (ev->type == GDK_BUTTON_PRESS && e.current_mouse_mode() == Editing::MouseObject) {
107
108                 /* XXX: icky dcast to Editor */
109                 e.drags()->set (new EditorRubberbandSelectDrag (dynamic_cast<Editor*> (&e), group), ev);
110                 e.drags()->start_grab (ev);
111                 return true;
112
113         } else if (ev->type == GDK_MOTION_NOTIFY && e.drags()->active()) {
114                 /* we probably shouldn't have to handle this here, but... */
115                 e.drags()->motion_handler(ev, false);
116                 return true;
117
118         } else if (ev->type == GDK_BUTTON_RELEASE) {
119                 if (e.drags()->end_grab (ev)) {
120                         return true;
121                 } else if (e.current_mouse_mode() != Editing::MouseObject &&
122                            e.current_mouse_mode() == Editing::MouseDraw) {
123                         return false;
124                 }
125
126                 double x = ev->button.x;
127                 double y = ev->button.y;
128
129                 /* convert to item coordinates in the time axis view */
130                 automation_view()->canvas_display()->canvas_to_item (x, y);
131
132                 /* clamp y */
133                 y = std::max (y, 0.0);
134                 y = std::min (y, _height - NAME_HIGHLIGHT_SIZE);
135
136                 /* guard points only if primary modifier is used */
137                 bool with_guard_points = Gtkmm2ext::Keyboard::modifier_state_equals (ev->button.state, Gtkmm2ext::Keyboard::PrimaryModifier);
138                 add_automation_event (ev, e.pixel_to_sample (x) - _region->position() + _region->start(), y, with_guard_points);
139                 return true;
140         }
141
142         return false;
143 }
144
145 /** @param when Position in frames, where 0 is the start of the region.
146  *  @param y y position, relative to our TimeAxisView.
147  */
148 void
149 AutomationRegionView::add_automation_event (GdkEvent *, framepos_t when, double y, bool with_guard_points)
150 {
151         if (!_line) {
152                 boost::shared_ptr<Evoral::Control> c = _region->control(_parameter, true);
153                 boost::shared_ptr<ARDOUR::AutomationControl> ac
154                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(c);
155                 assert(ac);
156                 create_line(ac->alist());
157         }
158         assert(_line);
159
160         AutomationTimeAxisView* const view = automation_view ();
161
162         /* compute vertical fractional position */
163
164         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
165         y = 1.0 - (y / h);
166
167         /* snap frame */
168
169         when = snap_frame_to_frame (when - _region->start ()) + _region->start ();
170
171         /* map using line */
172
173         double when_d = when;
174         _line->view_to_model_coord (when_d, y);
175
176         view->session()->begin_reversible_command (_("add automation event"));
177         XMLNode& before = _line->the_list()->get_state();
178
179         _line->the_list()->add (when_d, y, with_guard_points, false);
180
181         XMLNode& after = _line->the_list()->get_state();
182
183         view->session()->commit_reversible_command (
184                 new MementoCommand<ARDOUR::AutomationList> (_line->memento_command_binder(), &before, &after));
185
186         view->session()->set_dirty ();
187 }
188
189 bool
190 AutomationRegionView::paste (framepos_t                                      pos,
191                              unsigned                                        paste_count,
192                              float                                           times,
193                              boost::shared_ptr<const ARDOUR::AutomationList> slist)
194 {
195         AutomationTimeAxisView* const             view    = automation_view();
196         boost::shared_ptr<ARDOUR::AutomationList> my_list = _line->the_list();
197
198         if (view->session()->transport_rolling() && my_list->automation_write()) {
199                 /* do not paste if this control is in write mode and we're rolling */
200                 return false;
201         }
202
203         /* add multi-paste offset if applicable */
204         pos += view->editor().get_paste_offset(
205                 pos, paste_count, _line->time_converter().to(slist->length()));
206
207         const double model_pos = _line->time_converter().from(pos - _line->time_converter().origin_b());
208
209         XMLNode& before = my_list->get_state();
210         my_list->paste(*slist, model_pos, times);
211         view->session()->add_command(
212                 new MementoCommand<ARDOUR::AutomationList>(_line->memento_command_binder(), &before, &my_list->get_state()));
213
214         return true;
215 }
216
217 void
218 AutomationRegionView::set_height (double h)
219 {
220         RegionView::set_height(h);
221
222         if (_line) {
223                 _line->set_height ((uint32_t)rint(h - NAME_HIGHLIGHT_SIZE));
224         }
225 }
226
227 bool
228 AutomationRegionView::set_position (framepos_t pos, void* src, double* ignored)
229 {
230         if (_line) {
231                 _line->set_maximum_time (_region->length ());
232         }
233
234         return RegionView::set_position(pos, src, ignored);
235 }
236
237
238 void
239 AutomationRegionView::reset_width_dependent_items (double pixel_width)
240 {
241         RegionView::reset_width_dependent_items(pixel_width);
242
243         if (_line) {
244                 _line->reset();
245         }
246 }
247
248
249 void
250 AutomationRegionView::region_resized (const PBD::PropertyChange& what_changed)
251 {
252         RegionView::region_resized (what_changed);
253
254         if (!_line) {
255                 return;
256         }
257
258         if (what_changed.contains (ARDOUR::Properties::start)) {
259                 _line->set_offset (_region->start ());
260         }
261
262         if (what_changed.contains (ARDOUR::Properties::length)) {
263                 _line->set_maximum_time (_region->length());
264         }
265 }
266
267
268 void
269 AutomationRegionView::entered (bool)
270 {
271         if (_line) {
272                 _line->track_entered();
273         }
274 }
275
276
277 void
278 AutomationRegionView::exited ()
279 {
280         if (_line) {
281                 _line->track_exited();
282         }
283 }