Fix crash after deleting automation region views (#4718).
[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->Event.connect (sigc::mem_fun (this, &AutomationRegionView::canvas_event));
58         group->raise_to_top();
59 }
60
61 AutomationRegionView::~AutomationRegionView ()
62 {
63         RegionViewGoingAway (this); /* EMIT_SIGNAL */
64 }
65
66 void
67 AutomationRegionView::init (bool /*wfd*/)
68 {
69         _enable_display = false;
70
71         RegionView::init (false);
72
73         reset_width_dependent_items ((double) _region->length() / samples_per_pixel);
74
75         set_height (trackview.current_height());
76
77         set_colors ();
78
79         _enable_display = true;
80 }
81
82 void
83 AutomationRegionView::create_line (boost::shared_ptr<ARDOUR::AutomationList> list)
84 {
85         _line = boost::shared_ptr<AutomationLine> (new MidiAutomationLine(
86                                 ARDOUR::EventTypeMap::instance().to_symbol(list->parameter()),
87                                 trackview, *get_canvas_group(), list,
88                                 boost::dynamic_pointer_cast<ARDOUR::MidiRegion> (_region),
89                                 _parameter,
90                                 &_source_relative_time_converter));
91         _line->set_colors();
92         _line->set_height ((uint32_t)rint(trackview.current_height() - NAME_HIGHLIGHT_SIZE));
93         _line->set_visibility (AutomationLine::VisibleAspects (AutomationLine::Line|AutomationLine::ControlPoints));
94         _line->set_maximum_time (_region->length());
95         _line->set_offset (_region->start ());
96 }
97
98 bool
99 AutomationRegionView::canvas_event (GdkEvent* ev)
100 {
101         PublicEditor& e = trackview.editor ();
102
103         if (ev->type == GDK_BUTTON_PRESS && e.current_mouse_mode() == Editing::MouseObject) {
104
105                 /* XXX: icky dcast to Editor */
106                 e.drags()->set (new EditorRubberbandSelectDrag (dynamic_cast<Editor*> (&e), group), ev);
107
108         } else if (ev->type == GDK_BUTTON_RELEASE) {
109
110                 if (trackview.editor().drags()->active() && trackview.editor().drags()->end_grab (ev)) {
111                         return true;
112                 }
113
114                 double x = ev->button.x;
115                 double y = ev->button.y;
116
117                 /* convert to item coordinates in the time axis view */
118                 automation_view()->canvas_display()->canvas_to_item (x, y);
119
120                 /* clamp y */
121                 y = std::max (y, 0.0);
122                 y = std::min (y, _height - NAME_HIGHLIGHT_SIZE);
123
124                 /* guard points only if primary modifier is used */
125                 bool with_guard_points = Gtkmm2ext::Keyboard::modifier_state_equals (ev->button.state, Gtkmm2ext::Keyboard::PrimaryModifier);
126                 add_automation_event (ev, trackview.editor().pixel_to_sample (x) - _region->position() + _region->start(), y, with_guard_points);
127         }
128
129         return false;
130 }
131
132 /** @param when Position in frames, where 0 is the start of the region.
133  *  @param y y position, relative to our TimeAxisView.
134  */
135 void
136 AutomationRegionView::add_automation_event (GdkEvent *, framepos_t when, double y, bool with_guard_points)
137 {
138         if (!_line) {
139                 boost::shared_ptr<Evoral::Control> c = _region->control(_parameter, true);
140                 boost::shared_ptr<ARDOUR::AutomationControl> ac
141                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(c);
142                 assert(ac);
143                 create_line(ac->alist());
144         }
145         assert(_line);
146
147         AutomationTimeAxisView* const view = automation_view ();
148
149         /* compute vertical fractional position */
150
151         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
152         y = 1.0 - (y / h);
153
154         /* snap frame */
155
156         when = snap_frame_to_frame (when - _region->start ()) + _region->start ();
157
158         /* map using line */
159
160         double when_d = when;
161         _line->view_to_model_coord (when_d, y);
162
163         view->session()->begin_reversible_command (_("add automation event"));
164         XMLNode& before = _line->the_list()->get_state();
165
166         _line->the_list()->add (when_d, y, with_guard_points);
167
168         XMLNode& after = _line->the_list()->get_state();
169
170         /* XXX: hack! */
171         boost::shared_ptr<ARDOUR::MidiRegion> mr = boost::dynamic_pointer_cast<ARDOUR::MidiRegion> (_region);
172         assert (mr);
173
174         view->session()->commit_reversible_command (
175                 new MementoCommand<ARDOUR::AutomationList> (new ARDOUR::MidiAutomationListBinder (mr->midi_source(), _parameter), &before, &after)
176                 );
177
178
179         view->session()->set_dirty ();
180 }
181
182 void
183 AutomationRegionView::set_height (double h)
184 {
185         RegionView::set_height(h);
186
187         if (_line) {
188                 _line->set_height ((uint32_t)rint(h - NAME_HIGHLIGHT_SIZE));
189         }
190 }
191
192 bool
193 AutomationRegionView::set_position (framepos_t pos, void* src, double* ignored)
194 {
195         if (_line) {
196                 _line->set_maximum_time (_region->length ());
197         }
198
199         return RegionView::set_position(pos, src, ignored);
200 }
201
202
203 void
204 AutomationRegionView::reset_width_dependent_items (double pixel_width)
205 {
206         RegionView::reset_width_dependent_items(pixel_width);
207
208         if (_line) {
209                 _line->reset();
210         }
211 }
212
213
214 void
215 AutomationRegionView::region_resized (const PBD::PropertyChange& what_changed)
216 {
217         RegionView::region_resized (what_changed);
218
219         if (!_line) {
220                 return;
221         }
222
223         if (what_changed.contains (ARDOUR::Properties::start)) {
224                 _line->set_offset (_region->start ());
225         }
226
227         if (what_changed.contains (ARDOUR::Properties::length)) {
228                 _line->set_maximum_time (_region->length());
229         }
230 }
231
232
233 void
234 AutomationRegionView::entered (bool)
235 {
236         if (_line) {
237                 _line->track_entered();
238         }
239 }
240
241
242 void
243 AutomationRegionView::exited ()
244 {
245         if (_line) {
246                 _line->track_exited();
247         }
248 }