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