Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / region_gain_line.cc
1 /*
2  * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
6  * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2014-2015 Nick Mainsbridge <mainsbridge@gmail.com>
8  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "evoral/Curve.h"
26 #include "pbd/memento_command.h"
27 #include "pbd/stateful_diff_command.h"
28
29 #include "ardour/audioregion.h"
30 #include "ardour/session.h"
31
32 #include "control_point.h"
33 #include "region_gain_line.h"
34 #include "audio_region_view.h"
35
36 #include "time_axis_view.h"
37 #include "editor.h"
38 #include "gui_thread.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
45
46 AudioRegionGainLine::AudioRegionGainLine (const string & name, AudioRegionView& r, ArdourCanvas::Container& parent, boost::shared_ptr<AutomationList> l)
47         : AutomationLine (name, r.get_time_axis_view(), parent, l, l->parameter())
48         , rv (r)
49 {
50         // If this isn't true something is horribly wrong, and we'll get catastrophic gain values
51         assert(l->parameter().type() == EnvelopeAutomation);
52
53         _time_converter->set_origin_b (rv.region()->position());
54
55         r.region()->PropertyChanged.connect (_region_changed_connection, invalidator (*this), boost::bind (&AudioRegionGainLine::region_changed, this, _1), gui_context());
56
57         group->raise_to_top ();
58         group->set_y_position (2);
59         terminal_points_can_slide = false;
60 }
61
62 void
63 AudioRegionGainLine::start_drag_single (ControlPoint* cp, double x, float fraction)
64 {
65         AutomationLine::start_drag_single (cp, x, fraction);
66
67         // XXX Stateful need to capture automation curve data
68
69         if (!rv.audio_region()->envelope_active()) {
70                 trackview.session()->add_command(new MementoCommand<AudioRegion>(*(rv.audio_region().get()), &rv.audio_region()->get_state(), 0));
71                 rv.audio_region()->set_envelope_active(false);
72         }
73 }
74
75 // This is an extended copy from AutomationList
76 void
77 AudioRegionGainLine::remove_point (ControlPoint& cp)
78 {
79         trackview.editor().begin_reversible_command (_("remove control point"));
80         XMLNode &before = alist->get_state();
81
82         if (!rv.audio_region()->envelope_active()) {
83                 rv.audio_region()->clear_changes ();
84                 rv.audio_region()->set_envelope_active(true);
85                 trackview.session()->add_command(new StatefulDiffCommand (rv.audio_region()));
86         }
87
88         trackview.editor ().get_selection ().clear_points ();
89         alist->erase (cp.model());
90
91         trackview.editor().session()->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
92         trackview.editor().commit_reversible_command ();
93         trackview.editor().session()->set_dirty ();
94 }
95
96 void
97 AudioRegionGainLine::end_drag (bool with_push, uint32_t final_index)
98 {
99         if (!rv.audio_region()->envelope_active()) {
100                 rv.audio_region()->set_envelope_active(true);
101                 trackview.session()->add_command(new MementoCommand<AudioRegion>(*(rv.audio_region().get()), 0, &rv.audio_region()->get_state()));
102         }
103
104         AutomationLine::end_drag (with_push, final_index);
105 }
106
107 void
108 AudioRegionGainLine::region_changed (const PropertyChange& what_changed)
109 {
110         PropertyChange interesting_stuff;
111
112         interesting_stuff.add (ARDOUR::Properties::start);
113         interesting_stuff.add (ARDOUR::Properties::position);
114
115         if (what_changed.contains (interesting_stuff)) {
116                 _time_converter->set_origin_b (rv.region()->position());
117         }
118
119         interesting_stuff.clear ();
120         interesting_stuff.add (ARDOUR::Properties::start);
121         interesting_stuff.add (ARDOUR::Properties::length);
122
123         if (what_changed.contains (interesting_stuff)) {
124                 reset ();
125         }
126 }