merge 3.0-panexp (pan experiments) branch, revisions 8534-8585 into 3.0, thus ending...
[ardour.git] / gtk2_ardour / automation_controller.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: Dave 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
23 #include "pbd/error.h"
24
25 #include "ardour/automation_list.h"
26 #include "ardour/automation_control.h"
27 #include "ardour/event_type_map.h"
28 #include "ardour/automatable.h"
29 #include "ardour/panner.h"
30 #include "ardour/pan_controllable.h"
31 #include "ardour/session.h"
32
33 #include "ardour_ui.h"
34 #include "utils.h"
35 #include "automation_controller.h"
36 #include "gui_thread.h"
37
38 #include "i18n.h"
39
40 using namespace ARDOUR;
41 using namespace Gtk;
42
43
44 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
45         : BarController (*adj, ac)
46         , _ignore_change(false)
47         , _controllable(ac)
48         , _adjustment(adj)
49 {
50         set_name (X_("PluginSlider")); // FIXME: get yer own name!
51         set_style (BarController::LeftToRight);
52         set_use_parent (true);
53
54         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
55         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
56
57         _adjustment->signal_value_changed().connect (
58                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
59
60         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
61                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
62
63         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
64 }
65
66 AutomationController::~AutomationController()
67 {
68 }
69
70 boost::shared_ptr<AutomationController>
71 AutomationController::create(
72                 boost::shared_ptr<Automatable> parent,
73                 const Evoral::Parameter& param,
74                 boost::shared_ptr<AutomationControl> ac)
75 {
76         Gtk::Adjustment* adjustment = manage(new Gtk::Adjustment(param.normal(), param.min(), param.max()));
77         if (!ac) {
78                 PBD::warning << "Creating AutomationController for " << EventTypeMap::instance().to_symbol(param) << endmsg;
79                 ac = boost::dynamic_pointer_cast<AutomationControl>(parent->control_factory(param));
80         } else {
81                 assert(ac->parameter() == param);
82         }
83         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment));
84 }
85
86 std::string
87 AutomationController::get_label (int&)
88 {
89         std::stringstream s;
90
91         // Hack to display CC rounded to int
92         if (_controllable->parameter().type() == MidiCCAutomation) {
93                 s << (int)_controllable->get_value();
94         } else {
95                 s << std::fixed << std::setprecision(3) << _controllable->get_value();
96         }
97
98         return s.str ();
99 }
100
101 void
102 AutomationController::display_effective_value()
103 {
104         //if ( ! _controllable->list()->automation_playback())
105         //      return;
106
107         float value = _controllable->get_value();
108
109         if (_adjustment->get_value() != value) {
110                 _ignore_change = true;
111                 _adjustment->set_value (value);
112                 _ignore_change = false;
113         }
114 }
115
116 void
117 AutomationController::value_adjusted()
118 {
119         if (!_ignore_change) {
120                 _controllable->set_value(_adjustment->get_value());
121         }
122 }
123
124 void
125 AutomationController::start_touch()
126 {
127         _controllable->start_touch (_controllable->session().transport_frame());
128 }
129
130 void
131 AutomationController::end_touch ()
132 {
133         if (_controllable->automation_state() == Touch) {
134
135                 bool mark = false;
136                 double when = 0;
137                 
138                 if (_controllable->session().transport_rolling()) {
139                         mark = true;
140                         when = _controllable->session().transport_frame();
141                 }
142
143                 _controllable->stop_touch (mark, when);
144         }
145 }
146
147 void
148 AutomationController::automation_state_changed ()
149 {
150         ENSURE_GUI_THREAD (*this, &AutomationController::automation_state_changed)
151
152         bool x = (_controllable->automation_state() != Off);
153
154         /* start watching automation so that things move */
155
156         _screen_update_connection.disconnect();
157
158         if (x) {
159                 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
160                                 sigc::mem_fun (*this, &AutomationController::display_effective_value));
161         }
162 }
163
164 void
165 AutomationController::value_changed ()
166 {
167         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
168 }
169
170 /** Stop updating our value from our controllable */
171 void
172 AutomationController::stop_updating ()
173 {
174         _screen_update_connection.disconnect ();
175 }