Note selector dialog for note controls.
[ardour.git] / gtk2_ardour / automation_controller.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David 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 <cmath>
23
24 #include "pbd/error.h"
25
26 #include "ardour/automatable.h"
27 #include "ardour/automation_control.h"
28 #include "ardour/session.h"
29
30 #include "ardour_ui.h"
31 #include "automation_controller.h"
32 #include "gui_thread.h"
33 #include "note_select_dialog.h"
34
35 #include "i18n.h"
36
37 using namespace ARDOUR;
38 using namespace Gtk;
39
40 AutomationController::AutomationController(boost::shared_ptr<Automatable>       printer,
41                                            boost::shared_ptr<AutomationControl> ac,
42                                            Adjustment*                          adj)
43         : BarController (*adj, ac)
44         , _ignore_change(false)
45         , _printer (printer)
46         , _controllable(ac)
47         , _adjustment(adj)
48 {
49         assert (_printer);
50
51         set_name (X_("ProcessorControlSlider"));
52
53         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
54         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
55
56         signal_button_release_event().connect(
57                 sigc::mem_fun(*this, &AutomationController::on_button_release));
58
59         _adjustment->signal_value_changed().connect (
60                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
61
62         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
63                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
64
65         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
66 }
67
68 AutomationController::~AutomationController()
69 {
70 }
71
72 boost::shared_ptr<AutomationController>
73 AutomationController::create(boost::shared_ptr<Automatable>       printer,
74                              const Evoral::Parameter&             param,
75                              const ParameterDescriptor&           desc,
76                              boost::shared_ptr<AutomationControl> ac)
77 {
78         const double lo        = ac->internal_to_interface(desc.lower);
79         const double up        = ac->internal_to_interface(desc.upper);
80         const double normal    = ac->internal_to_interface(desc.normal);
81         double       smallstep = desc.smallstep;
82         double       largestep = desc.largestep;
83         if (smallstep == 0.0) {
84                 smallstep = (up - lo) / 100;
85         }
86         if (largestep == 0.0) {
87                 largestep = (up - lo) / 10;
88         }
89         smallstep = ac->internal_to_interface(smallstep);
90         largestep = ac->internal_to_interface(largestep);
91
92         Gtk::Adjustment* adjustment = manage (
93                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
94
95         assert (ac);
96         assert(ac->parameter() == param);
97         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
98 }
99
100 std::string
101 AutomationController::get_label (double& xpos)
102 {
103         xpos = 0.5;
104         return _printer->value_as_string (_controllable);
105 }
106
107 void
108 AutomationController::display_effective_value()
109 {
110         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
111
112         if (_adjustment->get_value () != interface_value) {
113                 _ignore_change = true;
114                 _adjustment->set_value (interface_value);
115                 _ignore_change = false;
116         }
117 }
118
119 void
120 AutomationController::value_adjusted ()
121 {
122         if (!_ignore_change) {
123                 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
124         }
125 }
126
127 void
128 AutomationController::start_touch()
129 {
130         _controllable->start_touch (_controllable->session().transport_frame());
131 }
132
133 void
134 AutomationController::end_touch ()
135 {
136         if (!_controllable->alist()) return;
137         if (_controllable->automation_state() == Touch) {
138
139                 bool mark = false;
140                 double when = 0;
141
142                 if (_controllable->session().transport_rolling()) {
143                         mark = true;
144                         when = _controllable->session().transport_frame();
145                 }
146
147                 _controllable->stop_touch (mark, when);
148         }
149 }
150
151 void
152 AutomationController::run_note_select_dialog()
153 {
154         NoteSelectDialog* dialog = new NoteSelectDialog();
155         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
156                 _controllable->set_value(dialog->note_number());
157         }
158         delete dialog;
159 }
160
161 bool
162 AutomationController::on_button_release(GdkEventButton* ev)
163 {
164         using namespace Gtk::Menu_Helpers;
165
166         if (ev->button == 3 && _controllable->desc().unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
167                 Gtk::Menu* menu  = manage(new Menu());
168                 MenuList&  items = menu->items();
169                 items.push_back(MenuElem(_("Select Note..."),
170                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
171                 menu->popup(1, ev->time);
172                 return true;
173         }
174         return false;
175 }
176
177 void
178 AutomationController::value_changed ()
179 {
180         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
181 }
182
183 /** Stop updating our value from our controllable */
184 void
185 AutomationController::stop_updating ()
186 {
187         _screen_update_connection.disconnect ();
188 }