a72aa0ba7d02e270c41e8eb54bd2d9af8c22b7b0
[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 "automation_controller.h"
23 #include "ardour/automation_event.h"
24 #include "ardour/automation_control.h"
25 #include "ardour_ui.h"
26 #include "utils.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace Gtk;
32
33
34 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
35         : BarController(*adj, *ac)
36         , _ignore_change(false)
37         , _controllable(ac)
38         , _adjustment(adj)
39 {
40         set_name (X_("PluginSlider")); // FIXME: get yer own name!
41         set_style (BarController::LeftToRight);
42         set_use_parent (true);
43         
44         label_callback = sigc::mem_fun(this, &AutomationController::update_label);
45         
46         StartGesture.connect (mem_fun(*this, &AutomationController::start_touch));
47         StopGesture.connect (mem_fun(*this, &AutomationController::end_touch));
48         
49         _adjustment->signal_value_changed().connect (
50                         mem_fun(*this, &AutomationController::value_adjusted));
51                 
52         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
53                         mem_fun (*this, &AutomationController::display_effective_value));
54 }
55
56 AutomationController::~AutomationController()
57 {
58 }
59
60 boost::shared_ptr<AutomationController>
61 AutomationController::create(Session& s, boost::shared_ptr<AutomationList> al, boost::shared_ptr<AutomationControl> ac)
62 {
63         Gtk::Adjustment* adjustment = manage(new Gtk::Adjustment(al->default_value(), al->get_min_y(), al->get_max_y()));
64         if (!ac) {
65                 PBD::warning << "Creating AutomationController for " << al->param_id().to_string() << endmsg;
66                 ac = boost::shared_ptr<AutomationControl>(new AutomationControl(s, al));
67         }
68         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment));
69 }
70
71 void
72 AutomationController::update_label(char* label, int label_len)
73 {
74         //cerr << "Controller label: " << label << endl;
75 }
76
77 void
78 AutomationController::display_effective_value()
79 {
80         if ( ! _controllable->list()->automation_playback())
81                 return;
82
83         float value = _controllable->get_value();
84         
85         if (_adjustment->get_value() != value) {
86                 _ignore_change = true; 
87                 _adjustment->set_value (value);
88                 _ignore_change = false;
89         }
90 }
91
92 void
93 AutomationController::value_adjusted()
94 {
95         if (!_ignore_change) {
96                 _controllable->set_value(_adjustment->get_value());
97         }
98 }
99
100 void
101 AutomationController::start_touch()
102 {
103         _controllable->list()->start_touch();
104 }
105
106 void
107 AutomationController::end_touch()
108 {
109         _controllable->list()->stop_touch();
110 }