VKeybd: Pass on primary (Ctrl/Cmd) shortcuts
[ardour.git] / gtk2_ardour / automation_controller.cc
1 /*
2  * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2014 Ben Loftis <ben@harrisonconsoles.com>
8  * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <iomanip>
26 #include <cmath>
27
28 #include "pbd/compose.h"
29 #include "pbd/error.h"
30
31 #include "ardour/automatable.h"
32 #include "ardour/automation_control.h"
33 #include "ardour/session.h"
34 #include "ardour/tempo.h"
35
36 #include "widgets/ardour_button.h"
37 #include "widgets/ardour_knob.h"
38
39 #include "automation_controller.h"
40 #include "context_menu_helper.h"
41 #include "gui_thread.h"
42 #include "note_select_dialog.h"
43 #include "timers.h"
44
45 #include "pbd/i18n.h"
46
47 using namespace ARDOUR;
48 using namespace Gtk;
49 using namespace ArdourWidgets;
50
51 using PBD::Controllable;
52
53 AutomationBarController::AutomationBarController (
54                 boost::shared_ptr<AutomationControl> ac,
55                 Adjustment*                          adj)
56         : ArdourWidgets::BarController (*adj, ac)
57         , _controllable (ac)
58 {
59 }
60
61 std::string
62 AutomationBarController::get_label (double& xpos)
63 {
64         xpos = 0.5;
65         return _controllable->get_user_string();
66 }
67
68 AutomationBarController::~AutomationBarController()
69 {
70 }
71
72 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
73                                            Adjustment*                          adj,
74                                            bool                                 use_knob)
75         : _widget(NULL)
76         , _controllable(ac)
77         , _adjustment(adj)
78         , _ignore_change(false)
79         , _grabbed(false)
80 {
81         if (ac->toggled()) {
82                 ArdourButton* but = manage(new ArdourButton());
83
84                 // Apply styles for special types
85                 if (ac->parameter().type() == MuteAutomation) {
86                         but->set_name("mute button");
87                 } else if (ac->parameter().type() == SoloAutomation) {
88                         but->set_name("solo button");
89                 } else {
90                         but->set_name("generic button");
91                 }
92                 but->set_fallthrough_to_parent(true);
93                 but->set_controllable(ac);
94                 but->signal_button_press_event().connect(
95                         sigc::mem_fun(*this, &AutomationController::button_press));
96                 but->signal_button_release_event().connect(
97                         sigc::mem_fun(*this, &AutomationController::button_release));
98                 const bool active = _adjustment->get_value() >= 0.5;
99                 if (but->get_active() != active) {
100                         but->set_active(active);
101                 }
102                 _widget = but;
103         } else if (use_knob) {
104                 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
105                 knob->set_controllable (ac);
106                 knob->set_name("processor control knob");
107                 _widget = knob;
108                 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
109                 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
110         } else {
111                 AutomationBarController* bar = manage(new AutomationBarController(ac, adj));
112
113                 bar->set_name(X_("ProcessorControlSlider"));
114                 bar->StartGesture.connect(
115                         sigc::mem_fun(*this, &AutomationController::start_touch));
116                 bar->StopGesture.connect(
117                         sigc::mem_fun(*this, &AutomationController::end_touch));
118                 bar->signal_button_release_event().connect(
119                         sigc::mem_fun(*this, &AutomationController::on_button_release));
120
121                 _widget = bar;
122         }
123
124         _adjustment->signal_value_changed().connect(
125                 sigc::mem_fun(*this, &AutomationController::value_adjusted));
126
127         ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
128         display_effective_value ();
129
130         if (ac->alist ()) {
131                 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
132                 automation_state_changed ();
133         }
134
135         add(*_widget);
136         show_all();
137 }
138
139 AutomationController::~AutomationController()
140 {
141 }
142
143 boost::shared_ptr<AutomationController>
144 AutomationController::create(const Evoral::Parameter&             param,
145                              const ParameterDescriptor&           desc,
146                              boost::shared_ptr<AutomationControl> ac,
147                              bool use_knob)
148 {
149         const double lo        = ac->internal_to_interface(desc.lower, true);
150         const double normal    = ac->internal_to_interface(desc.normal, true);
151         const double smallstep = fabs (ac->internal_to_interface(desc.lower + desc.smallstep, true) - lo);
152         const double largestep = fabs (ac->internal_to_interface(desc.lower + desc.largestep, true) - lo);
153
154         /* even though internal_to_interface() may not generate the full range
155          * 0..1, the interface range is 0..1 by definition,  so just hard code
156          * that.
157          */
158
159         Gtk::Adjustment* adjustment = manage (new Gtk::Adjustment (normal, 0.0, 1.0, smallstep, largestep));
160
161         assert (ac);
162         assert(ac->parameter() == param);
163         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
164 }
165
166 void
167 AutomationController::automation_state_changed ()
168 {
169         bool x = _controllable->alist()->automation_state() & Play;
170         _widget->set_sensitive (!x);
171 }
172
173 void
174 AutomationController::display_effective_value ()
175 {
176         double const interface_value = _controllable->internal_to_interface(_controllable->get_value(), true);
177
178         if (_grabbed) {
179                 /* we cannot use _controllable->touching() here
180                  * because that's only set in Write or Touch mode.
181                  * Besides ctrl-surfaces may also set touching()
182                  */
183                 return;
184         }
185         if (_adjustment->get_value () != interface_value) {
186                 _ignore_change = true;
187                 _adjustment->set_value (interface_value);
188                 _ignore_change = false;
189         }
190
191 }
192
193 void
194 AutomationController::value_adjusted ()
195 {
196         if (!_ignore_change) {
197                 const double new_val = _controllable->interface_to_internal(_adjustment->get_value(), true);
198                 if (_controllable->user_double() != new_val) {
199                         _controllable->set_value (new_val, Controllable::NoGroup);
200                 }
201         }
202
203         /* A bar controller will automatically follow the adjustment, but for a
204            button we have to do it manually. */
205         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
206         if (but) {
207                 const bool active = _adjustment->get_value() >= 0.5;
208                 if (but->get_active() != active) {
209                         but->set_active(active);
210                 }
211         }
212 }
213
214 void
215 AutomationController::start_touch()
216 {
217         _grabbed = true;
218         _controllable->start_touch (_controllable->session().transport_sample());
219 }
220
221 void
222 AutomationController::end_touch ()
223 {
224         _controllable->stop_touch (_controllable->session().transport_sample());
225         if (_grabbed) {
226                 _grabbed = false;
227                 display_effective_value ();
228         }
229 }
230
231 bool
232 AutomationController::button_press (GdkEventButton*)
233 {
234         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
235         if (but) {
236                 start_touch ();
237                 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
238         }
239         return false;
240 }
241
242 bool
243 AutomationController::button_release (GdkEventButton*)
244 {
245         end_touch ();
246         return true;
247 }
248
249 static double
250 midi_note_to_hz(int note)
251 {
252         const double tuning = 440.0;
253         return tuning * pow(2, (note - 69.0) / 12.0);
254 }
255
256 static double
257 clamp(double val, double min, double max)
258 {
259         if (val < min) {
260                 return min;
261         } else if (val > max) {
262                 return max;
263         }
264         return val;
265 }
266
267 void
268 AutomationController::run_note_select_dialog()
269 {
270         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
271         NoteSelectDialog*                  dialog = new NoteSelectDialog();
272         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
273                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
274                                       ? midi_note_to_hz(dialog->note_number())
275                                       : dialog->note_number());
276                 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
277         }
278         delete dialog;
279 }
280
281 void
282 AutomationController::set_freq_beats(double beats)
283 {
284         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
285         const ARDOUR::Session&             session = _controllable->session();
286         const samplepos_t                  pos     = session.transport_sample();
287         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at_sample (pos);
288         const double                       bpm     = tempo.note_types_per_minute();
289         const double                       bps     = bpm / 60.0;
290         const double                       freq    = bps / beats;
291         _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
292 }
293
294 void
295 AutomationController::set_ratio(double ratio)
296 {
297         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
298         const double                       value = _controllable->get_value() * ratio;
299         _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
300 }
301
302 bool
303 AutomationController::on_button_release(GdkEventButton* ev)
304 {
305         using namespace Gtk::Menu_Helpers;
306
307         if (ev->button != 3) {
308                 return false;
309         }
310
311         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
312         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
313                 Gtk::Menu* menu  = ARDOUR_UI_UTILS::shared_popup_menu ();
314                 MenuList&  items = menu->items();
315                 items.push_back(MenuElem(_("Select Note..."),
316                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
317                 menu->popup(1, ev->time);
318                 return true;
319         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
320                 Gtk::Menu* menu  = ARDOUR_UI_UTILS::shared_popup_menu ();
321                 MenuList&  items = menu->items();
322                 items.push_back(MenuElem(_("Halve"),
323                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
324                                                     0.5)));
325                 items.push_back(MenuElem(_("Double"),
326                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
327                                                     2.0)));
328                 const bool is_audible = desc.upper > 40.0;
329                 const bool is_low     = desc.lower < 1.0;
330                 if (is_audible) {
331                         items.push_back(MenuElem(_("Select Note..."),
332                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
333                 }
334                 if (is_low) {
335                         for (int beats = 1; beats <= 16; ++beats) {
336                                 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
337                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
338                                                                     (double)beats)));
339                         }
340                 }
341                 menu->popup(1, ev->time);
342                 return true;
343         }
344
345         return false;
346 }
347
348 /** Stop updating our value from our controllable */
349 void
350 AutomationController::stop_updating ()
351 {
352         _screen_update_connection.disconnect ();
353 }
354
355 void
356 AutomationController::disable_vertical_scroll ()
357 {
358         AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
359         if (bar) {
360                 bar->set_tweaks (
361                         ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));
362         }
363 }