Can't call the wrong function when there's only one of them: remove ARDOUR::Parameter...
[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 <pbd/error.h>
22 #include "ardour/automation_list.h"
23 #include "ardour/automation_control.h"
24 #include "ardour_ui.h"
25 #include "utils.h"
26 #include "automation_controller.h"
27 #include "gui_thread.h"
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32 using namespace Gtk;
33
34
35 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
36         : BarController(*adj, ac)
37         , _ignore_change(false)
38         , _controllable(ac)
39         , _adjustment(adj)
40 {
41         set_name (X_("PluginSlider")); // FIXME: get yer own name!
42         set_style (BarController::LeftToRight);
43         set_use_parent (true);
44         
45         label_callback = sigc::mem_fun(this, &AutomationController::update_label);
46         
47         StartGesture.connect (mem_fun(*this, &AutomationController::start_touch));
48         StopGesture.connect (mem_fun(*this, &AutomationController::end_touch));
49         
50         _adjustment->signal_value_changed().connect (
51                         mem_fun(*this, &AutomationController::value_adjusted));
52                 
53         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
54                         mem_fun (*this, &AutomationController::display_effective_value));
55         
56         ac->Changed.connect (mem_fun(*this, &AutomationController::value_changed));
57 }
58
59 AutomationController::~AutomationController()
60 {
61 }
62
63 boost::shared_ptr<AutomationController>
64 AutomationController::create(
65                 boost::shared_ptr<Automatable> parent,
66                 const Evoral::Parameter& param,
67                 boost::shared_ptr<AutomationControl> ac)
68 {
69         Gtk::Adjustment* adjustment = manage(new Gtk::Adjustment(param.normal(), param.min(), param.max()));
70         if (!ac) {
71                 PBD::warning << "Creating AutomationController for " << EventTypeMap::instance().to_symbol(param) << endmsg;
72                 ac = boost::dynamic_pointer_cast<AutomationControl>(parent->control_factory(param));
73         } else {
74                 assert(ac->parameter() == param);
75         }
76         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment));
77 }
78
79 void
80 AutomationController::update_label(char* label, int label_len)
81 {
82         if (label && label_len) {
83                 // Hack to display CC rounded to int
84                 if (_controllable->parameter().type() == MidiCCAutomation)
85                         snprintf(label, label_len, "%d", (int)_controllable->get_value());
86                 else
87                         snprintf(label, label_len, "%.3f", _controllable->get_value());
88         }
89 }
90
91 void
92 AutomationController::display_effective_value()
93 {
94         //if ( ! _controllable->list()->automation_playback())
95         //      return;
96
97         float value = _controllable->get_value();
98         
99         if (_adjustment->get_value() != value) {
100                 _ignore_change = true; 
101                 _adjustment->set_value (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();
118 }
119
120 void
121 AutomationController::end_touch()
122 {
123         _controllable->stop_touch();
124 }
125
126 void
127 AutomationController::automation_state_changed ()
128 {
129         ENSURE_GUI_THREAD(mem_fun(*this, &AutomationController::automation_state_changed));
130
131         bool x = (_controllable->automation_state() != Off);
132         
133         /* start watching automation so that things move */
134         
135         _screen_update_connection.disconnect();
136
137         if (x) {
138                 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
139                                 mem_fun (*this, &AutomationController::display_effective_value));
140         }
141 }
142
143 void
144 AutomationController::value_changed ()
145 {
146         Gtkmm2ext::UI::instance()->call_slot (
147                         mem_fun(*this, &AutomationController::display_effective_value));
148 }
149