Skip updating options for hidden file-dialog
[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/compose.h"
25 #include "pbd/error.h"
26
27 #include "ardour/automatable.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/session.h"
30 #include "ardour/tempo.h"
31
32 #include "widgets/ardour_button.h"
33 #include "widgets/ardour_knob.h"
34 #include "automation_controller.h"
35 #include "gui_thread.h"
36 #include "note_select_dialog.h"
37 #include "timers.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace Gtk;
43 using namespace ArdourWidgets;
44
45 using PBD::Controllable;
46
47 AutomationBarController::AutomationBarController (
48                 boost::shared_ptr<AutomationControl> ac,
49                 Adjustment*                          adj)
50         : ArdourWidgets::BarController (*adj, ac)
51         , _controllable (ac)
52 {
53 }
54
55 std::string
56 AutomationBarController::get_label (double& xpos)
57 {
58         xpos = 0.5;
59         return _controllable->get_user_string();
60 }
61
62 AutomationBarController::~AutomationBarController()
63 {
64 }
65
66 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
67                                            Adjustment*                          adj,
68                                            bool                                 use_knob)
69         : _widget(NULL)
70         , _controllable(ac)
71         , _adjustment(adj)
72         , _ignore_change(false)
73         , _grabbed(false)
74 {
75         if (ac->toggled()) {
76                 ArdourButton* but = manage(new ArdourButton());
77
78                 // Apply styles for special types
79                 if (ac->parameter().type() == MuteAutomation) {
80                         but->set_name("mute button");
81                 } else if (ac->parameter().type() == SoloAutomation) {
82                         but->set_name("solo button");
83                 } else {
84                         but->set_name("generic button");
85                 }
86                 but->set_fallthrough_to_parent(true);
87                 but->set_controllable(ac);
88                 but->signal_button_press_event().connect(
89                         sigc::mem_fun(*this, &AutomationController::button_press));
90                 but->signal_button_release_event().connect(
91                         sigc::mem_fun(*this, &AutomationController::button_release));
92                 const bool active = _adjustment->get_value() >= 0.5;
93                 if (but->get_active() != active) {
94                         but->set_active(active);
95                 }
96                 _widget = but;
97         } else if (use_knob) {
98                 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
99                 knob->set_controllable (ac);
100                 knob->set_name("processor control knob");
101                 _widget = knob;
102                 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
103                 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
104         } else {
105                 AutomationBarController* bar = manage(new AutomationBarController(ac, adj));
106
107                 bar->set_name(X_("ProcessorControlSlider"));
108                 bar->StartGesture.connect(
109                         sigc::mem_fun(*this, &AutomationController::start_touch));
110                 bar->StopGesture.connect(
111                         sigc::mem_fun(*this, &AutomationController::end_touch));
112                 bar->signal_button_release_event().connect(
113                         sigc::mem_fun(*this, &AutomationController::on_button_release));
114
115                 _widget = bar;
116         }
117
118         _adjustment->signal_value_changed().connect(
119                 sigc::mem_fun(*this, &AutomationController::value_adjusted));
120
121         ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
122         display_effective_value ();
123
124         if (ac->alist ()) {
125                 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
126                 automation_state_changed ();
127         }
128
129         add(*_widget);
130         show_all();
131 }
132
133 AutomationController::~AutomationController()
134 {
135 }
136
137 boost::shared_ptr<AutomationController>
138 AutomationController::create(const Evoral::Parameter&             param,
139                              const ParameterDescriptor&           desc,
140                              boost::shared_ptr<AutomationControl> ac,
141                              bool use_knob)
142 {
143         const double lo        = ac->internal_to_interface(desc.lower);
144         const double up        = ac->internal_to_interface(desc.upper);
145         const double normal    = ac->internal_to_interface(desc.normal);
146         const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep) - lo;
147         const double largestep = ac->internal_to_interface(desc.lower + desc.largestep) - lo;
148
149         Gtk::Adjustment* adjustment = manage (
150                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
151
152         assert (ac);
153         assert(ac->parameter() == param);
154         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
155 }
156
157 void
158 AutomationController::automation_state_changed ()
159 {
160         bool x = _controllable->alist()->automation_state() & Play;
161         _widget->set_sensitive (!x);
162 }
163
164 void
165 AutomationController::display_effective_value ()
166 {
167         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
168
169         if (_grabbed) {
170                 /* we cannot use _controllable->touching() here
171                  * because that's only set in Write or Touch mode.
172                  * Besides ctrl-surfaces may also set touching()
173                  */
174                 return;
175         }
176         if (_adjustment->get_value () != interface_value) {
177                 _ignore_change = true;
178                 _adjustment->set_value (interface_value);
179                 _ignore_change = false;
180         }
181
182 }
183
184 void
185 AutomationController::value_adjusted ()
186 {
187         if (!_ignore_change) {
188                 const double new_val = _controllable->interface_to_internal(_adjustment->get_value());
189                 if (_controllable->user_double() != new_val) {
190                         _controllable->set_value (new_val, Controllable::NoGroup);
191                 }
192         }
193
194         /* A bar controller will automatically follow the adjustment, but for a
195            button we have to do it manually. */
196         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
197         if (but) {
198                 const bool active = _adjustment->get_value() >= 0.5;
199                 if (but->get_active() != active) {
200                         but->set_active(active);
201                 }
202         }
203 }
204
205 void
206 AutomationController::start_touch()
207 {
208         _grabbed = true;
209         _controllable->start_touch (_controllable->session().transport_sample());
210 }
211
212 void
213 AutomationController::end_touch ()
214 {
215         _controllable->stop_touch (_controllable->session().transport_sample());
216         if (_grabbed) {
217                 _grabbed = false;
218                 display_effective_value ();
219         }
220 }
221
222 bool
223 AutomationController::button_press (GdkEventButton*)
224 {
225         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
226         if (but) {
227                 start_touch ();
228                 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
229         }
230         return false;
231 }
232
233 bool
234 AutomationController::button_release (GdkEventButton*)
235 {
236         end_touch ();
237         return true;
238 }
239
240 static double
241 midi_note_to_hz(int note)
242 {
243         const double tuning = 440.0;
244         return tuning * pow(2, (note - 69.0) / 12.0);
245 }
246
247 static double
248 clamp(double val, double min, double max)
249 {
250         if (val < min) {
251                 return min;
252         } else if (val > max) {
253                 return max;
254         }
255         return val;
256 }
257
258 void
259 AutomationController::run_note_select_dialog()
260 {
261         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
262         NoteSelectDialog*                  dialog = new NoteSelectDialog();
263         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
264                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
265                                       ? midi_note_to_hz(dialog->note_number())
266                                       : dialog->note_number());
267                 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
268         }
269         delete dialog;
270 }
271
272 void
273 AutomationController::set_freq_beats(double beats)
274 {
275         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
276         const ARDOUR::Session&             session = _controllable->session();
277         const samplepos_t                   pos     = session.transport_sample();
278         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at_sample (pos);
279         const double                       bpm     = tempo.note_types_per_minute();
280         const double                       bps     = bpm / 60.0;
281         const double                       freq    = bps / beats;
282         _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
283 }
284
285 void
286 AutomationController::set_ratio(double ratio)
287 {
288         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
289         const double                       value = _controllable->get_value() * ratio;
290         _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
291 }
292
293 bool
294 AutomationController::on_button_release(GdkEventButton* ev)
295 {
296         using namespace Gtk::Menu_Helpers;
297
298         if (ev->button != 3) {
299                 return false;
300         }
301
302         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
303         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
304                 Gtk::Menu* menu  = manage(new Menu());
305                 MenuList&  items = menu->items();
306                 items.push_back(MenuElem(_("Select Note..."),
307                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
308                 menu->popup(1, ev->time);
309                 return true;
310         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
311                 Gtk::Menu* menu  = manage(new Menu());
312                 MenuList&  items = menu->items();
313                 items.push_back(MenuElem(_("Halve"),
314                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
315                                                     0.5)));
316                 items.push_back(MenuElem(_("Double"),
317                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
318                                                     2.0)));
319                 const bool is_audible = desc.upper > 40.0;
320                 const bool is_low     = desc.lower < 1.0;
321                 if (is_audible) {
322                         items.push_back(MenuElem(_("Select Note..."),
323                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
324                 }
325                 if (is_low) {
326                         for (int beats = 1; beats <= 16; ++beats) {
327                                 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
328                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
329                                                                     (double)beats)));
330                         }
331                 }
332                 menu->popup(1, ev->time);
333                 return true;
334         }
335
336         return false;
337 }
338
339 /** Stop updating our value from our controllable */
340 void
341 AutomationController::stop_updating ()
342 {
343         _screen_update_connection.disconnect ();
344 }
345
346 void
347 AutomationController::disable_vertical_scroll ()
348 {
349         AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
350         if (bar) {
351                 bar->set_tweaks (
352                         ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));
353         }
354 }