Make Bundles work a bit better. A few include optimisations.
[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/event_type_map.h"
25 #include "ardour/automatable.h"
26 #include "ardour_ui.h"
27 #include "utils.h"
28 #include "automation_controller.h"
29 #include "gui_thread.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace Gtk;
35
36
37 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
38         : BarController(*adj, ac)
39         , _ignore_change(false)
40         , _controllable(ac)
41         , _adjustment(adj)
42 {
43         set_name (X_("PluginSlider")); // FIXME: get yer own name!
44         set_style (BarController::LeftToRight);
45         set_use_parent (true);
46         
47         label_callback = sigc::mem_fun(this, &AutomationController::update_label);
48         
49         StartGesture.connect (mem_fun(*this, &AutomationController::start_touch));
50         StopGesture.connect (mem_fun(*this, &AutomationController::end_touch));
51         
52         _adjustment->signal_value_changed().connect (
53                         mem_fun(*this, &AutomationController::value_adjusted));
54                 
55         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
56                         mem_fun (*this, &AutomationController::display_effective_value));
57         
58         ac->Changed.connect (mem_fun(*this, &AutomationController::value_changed));
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 void
82 AutomationController::update_label(char* label, int label_len)
83 {
84         if (label && label_len) {
85                 // Hack to display CC rounded to int
86                 if (_controllable->parameter().type() == MidiCCAutomation)
87                         snprintf(label, label_len, "%d", (int)_controllable->get_value());
88                 else
89                         snprintf(label, label_len, "%.3f", _controllable->get_value());
90         }
91 }
92
93 void
94 AutomationController::display_effective_value()
95 {
96         //if ( ! _controllable->list()->automation_playback())
97         //      return;
98
99         float value = _controllable->get_value();
100         
101         if (_adjustment->get_value() != value) {
102                 _ignore_change = true; 
103                 _adjustment->set_value (value);
104                 _ignore_change = false;
105         }
106 }
107
108 void
109 AutomationController::value_adjusted()
110 {
111         if (!_ignore_change) {
112                 _controllable->set_value(_adjustment->get_value());
113         }
114 }
115
116 void
117 AutomationController::start_touch()
118 {
119         _controllable->start_touch();
120 }
121
122 void
123 AutomationController::end_touch()
124 {
125         _controllable->stop_touch();
126 }
127
128 void
129 AutomationController::automation_state_changed ()
130 {
131         ENSURE_GUI_THREAD(mem_fun(*this, &AutomationController::automation_state_changed));
132
133         bool x = (_controllable->automation_state() != Off);
134         
135         /* start watching automation so that things move */
136         
137         _screen_update_connection.disconnect();
138
139         if (x) {
140                 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
141                                 mem_fun (*this, &AutomationController::display_effective_value));
142         }
143 }
144
145 void
146 AutomationController::value_changed ()
147 {
148         Gtkmm2ext::UI::instance()->call_slot (
149                         mem_fun(*this, &AutomationController::display_effective_value));
150 }
151