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