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