Make send automation work (#4734).
[ardour.git] / gtk2_ardour / automation_controller.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
21 #include <iomanip>
22 #include <cmath>
23
24 #include "pbd/error.h"
25
26 #include "ardour/automatable.h"
27 #include "ardour/automation_control.h"
28 #include "ardour/session.h"
29
30 #include "ardour_ui.h"
31 #include "utils.h"
32 #include "automation_controller.h"
33 #include "gui_thread.h"
34
35 #include "i18n.h"
36
37 using namespace ARDOUR;
38 using namespace Gtk;
39
40 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer, boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
41         : BarController (*adj, ac)
42         , _ignore_change(false)
43         , _printer (printer)
44         , _controllable(ac)
45         , _adjustment(adj)
46 {
47         assert (_printer);
48
49         set_name (X_("PluginSlider")); // FIXME: get yer own name!
50         set_style (BarController::LeftToRight);
51         set_use_parent (true);
52
53         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
54         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
55
56         _adjustment->signal_value_changed().connect (
57                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
58
59         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
60                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
61
62         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
63 }
64
65 AutomationController::~AutomationController()
66 {
67 }
68
69 boost::shared_ptr<AutomationController>
70 AutomationController::create(
71                 boost::shared_ptr<Automatable> printer,
72                 const Evoral::Parameter& param,
73                 boost::shared_ptr<AutomationControl> ac)
74 {
75         Gtk::Adjustment* adjustment = manage (
76                 new Gtk::Adjustment (
77                         ac->internal_to_interface (param.normal()),
78                         ac->internal_to_interface (param.min()),
79                         ac->internal_to_interface (param.max()),
80                         (param.max() - param.min()) / 100.0,
81                         (param.max() - param.min()) / 10.0
82                         )
83                 );
84
85         assert (ac);
86         assert(ac->parameter() == param);
87         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
88 }
89
90 std::string
91 AutomationController::get_label (double& xpos)
92 {
93         xpos = 0.5;
94         return _printer->value_as_string (_controllable);
95 }
96
97 void
98 AutomationController::display_effective_value()
99 {
100         double const interface_value = _controllable->internal_to_interface (_controllable->get_value());
101
102         if (_adjustment->get_value () != interface_value) {
103                 _ignore_change = true;
104                 _adjustment->set_value (interface_value);
105                 _ignore_change = false;
106         }
107 }
108
109 void
110 AutomationController::value_adjusted ()
111 {
112         if (!_ignore_change) {
113                 _controllable->set_value (_controllable->interface_to_internal (_adjustment->get_value()));
114         }
115 }
116
117 void
118 AutomationController::start_touch()
119 {
120         _controllable->start_touch (_controllable->session().transport_frame());
121 }
122
123 void
124 AutomationController::end_touch ()
125 {
126         if (_controllable->automation_state() == Touch) {
127
128                 bool mark = false;
129                 double when = 0;
130
131                 if (_controllable->session().transport_rolling()) {
132                         mark = true;
133                         when = _controllable->session().transport_frame();
134                 }
135
136                 _controllable->stop_touch (mark, when);
137         }
138 }
139
140 void
141 AutomationController::value_changed ()
142 {
143         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
144 }
145
146 /** Stop updating our value from our controllable */
147 void
148 AutomationController::stop_updating ()
149 {
150         _screen_update_connection.disconnect ();
151 }