Consolidate automation and meter-point strings
[ardour.git] / gtk2_ardour / panner_ui.cc
1 /*
2  * Copyright (C) 2005-2006 Nick Mainsbridge <mainsbridge@gmail.com>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2006-2007 Doug McLain <doug@nostar.net>
6  * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
7  * Copyright (C) 2006 Sampo Savolainen <v2@iki.fi>
8  * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
9  * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
10  * Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include <limits.h>
28
29 #include <gtkmm2ext/utils.h>
30
31 #include "pbd/fastlog.h"
32
33 #include "ardour/pannable.h"
34 #include "ardour/panner.h"
35 #include "ardour/panner_shell.h"
36 #include "ardour/session.h"
37
38 #include "widgets/tooltips.h"
39
40 #include "gain_meter.h"
41 #include "panner_ui.h"
42 #include "panner2d.h"
43 #include "gui_thread.h"
44 #include "stereo_panner.h"
45 #include "timers.h"
46 #include "mono_panner.h"
47 #include "ui_config.h"
48
49 #include "pbd/i18n.h"
50
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtkmm2ext;
55 using namespace Gtk;
56
57 PannerUI::PannerUI (Session* s)
58         : _current_nouts (-1)
59         , _current_nins (-1)
60         , _current_uri ("")
61         , _send_mode (false)
62         , pan_automation_state_button ("")
63         , _panner_list()
64 {
65         set_session (s);
66
67         ignore_toggle = false;
68         pan_menu = 0;
69         pan_astate_menu = 0;
70         pan_astyle_menu = 0;
71         in_pan_update = false;
72         _stereo_panner = 0;
73         _mono_panner = 0;
74         _ignore_width_change = false;
75         _ignore_position_change = false;
76
77         pan_automation_state_button.set_name ("MixerAutomationPlaybackButton");
78
79         ArdourWidgets::set_tooltip (pan_automation_state_button, _("Pan automation mode"));
80
81         //set_size_request_to_display_given_text (pan_automation_state_button, X_("O"), 2, 2);
82
83         pan_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
84
85         pan_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_state_button_event), false);
86
87         pan_vbox.set_spacing (2);
88         pack_start (pan_vbox, true, true);
89
90         twod_panner = 0;
91         big_window = 0;
92
93         set_width(Narrow);
94 }
95
96 void
97 PannerUI::set_panner (boost::shared_ptr<PannerShell> ps, boost::shared_ptr<Panner> p)
98 {
99         /* note that the panshell might not change here (i.e. ps == _panshell)
100          */
101
102         connections.drop_connections ();
103
104         delete pan_astyle_menu;
105         pan_astyle_menu = 0;
106
107         delete pan_astate_menu;
108         pan_astate_menu = 0;
109
110         _panshell = ps;
111         _panner = p;
112
113         delete twod_panner;
114         twod_panner = 0;
115
116         delete _stereo_panner;
117         _stereo_panner = 0;
118
119         delete _mono_panner;
120         _mono_panner = 0;
121
122         if (!_panner) {
123                 return;
124         }
125
126         _panshell->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::panshell_changed, this), gui_context());
127
128         /* new panner object, force complete reset of panner GUI
129          */
130
131         _current_nouts = 0;
132         _current_nins = 0;
133
134         setup_pan ();
135         update_pan_sensitive ();
136         pan_automation_state_changed ();
137 }
138
139 void
140 PannerUI::build_astate_menu ()
141 {
142         using namespace Menu_Helpers;
143
144         if (pan_astate_menu == 0) {
145                 pan_astate_menu = new Menu;
146                 pan_astate_menu->set_name ("ArdourContextMenu");
147         } else {
148                 pan_astate_menu->items().clear ();
149         }
150
151         /** TRANSLATORS: this is `Manual' in the sense of automation not being played,
152             so that changes to pan must be done by hand.
153         */
154         pan_astate_menu->items().push_back (MenuElem (S_("Automation|Manual"), sigc::bind (
155                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
156                         (AutoState) ARDOUR::Off)));
157         pan_astate_menu->items().push_back (MenuElem (_("Play"), sigc::bind (
158                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
159                         (AutoState) Play)));
160         pan_astate_menu->items().push_back (MenuElem (_("Write"), sigc::bind (
161                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
162                         (AutoState) Write)));
163         pan_astate_menu->items().push_back (MenuElem (_("Touch"), sigc::bind (
164                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
165                         (AutoState) Touch)));
166         pan_astate_menu->items().push_back (MenuElem (_("Latch"), sigc::bind (
167                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
168                         (AutoState) Latch)));
169
170 }
171
172 void
173 PannerUI::build_astyle_menu ()
174 {
175         using namespace Menu_Helpers;
176
177         if (pan_astyle_menu == 0) {
178                 pan_astyle_menu = new Menu;
179                 pan_astyle_menu->set_name ("ArdourContextMenu");
180         } else {
181                 pan_astyle_menu->items().clear();
182         }
183
184         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
185         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
186 }
187
188 void
189 PannerUI::on_size_allocate (Allocation& a)
190 {
191         HBox::on_size_allocate (a);
192 }
193
194 void
195 PannerUI::set_width (Width w)
196 {
197         _width = w;
198 }
199
200 PannerUI::~PannerUI ()
201 {
202         delete twod_panner;
203         delete big_window;
204         delete pan_menu;
205         delete pan_astyle_menu;
206         delete pan_astate_menu;
207         delete _stereo_panner;
208         delete _mono_panner;
209 }
210
211 void
212 PannerUI::panshell_changed ()
213 {
214         set_panner (_panshell, _panshell->panner());
215         setup_pan ();
216 }
217
218 void
219 PannerUI::setup_pan ()
220 {
221         int const nouts = _panner ? _panner->out().n_audio() : -1;
222         int const nins = _panner ? _panner->in().n_audio() : -1;
223
224         assert (_panshell);
225
226         if (nouts == _current_nouts
227                         && nins == _current_nins
228                         && _current_uri == _panshell->panner_gui_uri()
229                         )
230         {
231                 return;
232         }
233
234         _current_nins = nins;
235         _current_nouts = nouts;
236         _current_uri = _panshell->panner_gui_uri();
237
238         container_clear (pan_vbox);
239
240         delete twod_panner;
241         twod_panner = 0;
242         delete _stereo_panner;
243         _stereo_panner = 0;
244         delete _mono_panner;
245         _mono_panner = 0;
246
247         if (!_panner) {
248                 delete big_window;
249                 big_window = 0;
250                 return;
251         }
252
253         const float scale = std::max (1.f, UIConfiguration::instance().get_ui_scale());
254
255         if (_current_uri == "http://ardour.org/plugin/panner_2in2out#ui")
256         {
257                 delete big_window;
258                 big_window = 0;
259
260                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
261
262                 _stereo_panner = new StereoPanner (_panshell);
263                 _stereo_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
264                 _stereo_panner->set_send_drawing_mode (_send_mode);
265                 pan_vbox.pack_start (*_stereo_panner, false, false);
266
267                 boost::shared_ptr<AutomationControl> ac;
268
269                 ac = pannable->pan_azimuth_control;
270                 _stereo_panner->StartPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
271                                         boost::weak_ptr<AutomationControl> (ac)));
272                 _stereo_panner->StopPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
273                                         boost::weak_ptr<AutomationControl>(ac)));
274
275                 ac = pannable->pan_width_control;
276                 _stereo_panner->StartWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
277                                         boost::weak_ptr<AutomationControl> (ac)));
278                 _stereo_panner->StopWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
279                                         boost::weak_ptr<AutomationControl>(ac)));
280                 _stereo_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
281         }
282         else if (_current_uri == "http://ardour.org/plugin/panner_1in2out#ui"
283                         || _current_uri == "http://ardour.org/plugin/panner_balance#ui")
284         {
285                 delete big_window;
286                 big_window = 0;
287                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
288                 boost::shared_ptr<AutomationControl> ac = pannable->pan_azimuth_control;
289
290                 _mono_panner = new MonoPanner (_panshell);
291
292                 _mono_panner->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
293                                         boost::weak_ptr<AutomationControl> (ac)));
294                 _mono_panner->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
295                                         boost::weak_ptr<AutomationControl>(ac)));
296
297                 _mono_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
298
299                 _mono_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
300                 _mono_panner->set_send_drawing_mode (_send_mode);
301
302                 update_pan_sensitive ();
303                 pan_vbox.pack_start (*_mono_panner, false, false);
304         }
305         else if (_current_uri == "http://ardour.org/plugin/panner_vbap#ui")
306         {
307                 if (!twod_panner) {
308                         twod_panner = new Panner2d (_panshell, rintf(61.f * scale));
309                         twod_panner->set_name ("MixerPanZone");
310                         twod_panner->show ();
311                         twod_panner->signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event), false);
312                 }
313
314                 update_pan_sensitive ();
315                 twod_panner->reset (nins);
316                 if (big_window) {
317                         big_window->reset (nins);
318                 }
319                 twod_panner->set_size_request (-1, rintf(61.f * scale));
320                 twod_panner->set_send_drawing_mode (_send_mode);
321
322                 /* and finally, add it to the panner frame */
323
324                 pan_vbox.pack_start (*twod_panner, false, false);
325         }
326         else
327         {
328                 /* stick something into the panning viewport so that it redraws */
329                 EventBox* eb = manage (new EventBox());
330                 pan_vbox.pack_start (*eb, false, false);
331
332                 delete big_window;
333                 big_window = 0;
334         }
335
336         pan_vbox.show_all ();
337 }
338
339 void
340 PannerUI::set_send_drawing_mode (bool onoff)
341 {
342         if (_stereo_panner) {
343                 _stereo_panner->set_send_drawing_mode (onoff);
344         } else if (_mono_panner) {
345                 _mono_panner->set_send_drawing_mode (onoff);
346         } else if (twod_panner) {
347                 twod_panner->set_send_drawing_mode (onoff);
348         }
349         _send_mode = onoff;
350 }
351
352 void
353 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
354 {
355         boost::shared_ptr<AutomationControl> ac = wac.lock();
356         if (!ac) {
357                 return;
358         }
359         ac->start_touch (ac->session().transport_sample());
360 }
361
362 void
363 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
364 {
365         boost::shared_ptr<AutomationControl> ac = wac.lock();
366         if (!ac) {
367                 return;
368         }
369         ac->stop_touch (ac->session().transport_sample());
370 }
371
372 bool
373 PannerUI::pan_button_event (GdkEventButton* ev)
374 {
375         switch (ev->button) {
376         case 1:
377                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
378                         if (!big_window) {
379                                 big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
380                         }
381                         big_window->show ();
382                         return true;
383                 }
384                 break;
385
386         case 3:
387                 if (pan_menu == 0) {
388                         pan_menu = new Menu;
389                         pan_menu->set_name ("ArdourContextMenu");
390                 }
391                 build_pan_menu ();
392                 pan_menu->popup (1, ev->time);
393                 return true;
394                 break;
395         default:
396                 return false;
397         }
398
399         return false; // what's wrong with gcc?
400 }
401
402 void
403 PannerUI::build_pan_menu ()
404 {
405         using namespace Menu_Helpers;
406         MenuList& items (pan_menu->items());
407
408         items.clear ();
409
410         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
411         bypass_menu_item = static_cast<Gtk::CheckMenuItem*> (&items.back());
412
413         /* set state first, connect second */
414
415         bypass_menu_item->set_active (_panshell->bypassed());
416         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
417
418         if (!_panshell->bypassed()) {
419                 items.push_back (MenuElem (_("Reset"), sigc::mem_fun (*this, &PannerUI::pan_reset)));
420                 items.push_back (MenuElem (_("Edit..."), sigc::mem_fun (*this, &PannerUI::pan_edit)));
421         }
422
423         if (_panner_list.size() > 1 && !_panshell->bypassed()) {
424                 RadioMenuItem::Group group;
425                 items.push_back (SeparatorElem());
426
427                 _suspend_menu_callbacks = true;
428                 for (std::map<std::string,std::string>::const_iterator p = _panner_list.begin(); p != _panner_list.end(); ++p) {
429                         items.push_back (RadioMenuElem (group, p->second,
430                                                 sigc::bind(sigc::mem_fun (*this, &PannerUI::pan_set_custom_type), p->first)));
431                         RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&items.back ());
432                         i->set_active (_panshell->current_panner_uri() == p->first);
433                 }
434                 _suspend_menu_callbacks = false;
435         }
436 }
437
438 void
439 PannerUI::pan_bypass_toggle ()
440 {
441         if (bypass_menu_item && (_panshell->bypassed() != bypass_menu_item->get_active())) {
442                 _panshell->set_bypassed (!_panshell->bypassed());
443         }
444 }
445
446 void
447 PannerUI::pan_edit ()
448 {
449         if (_panshell->bypassed()) {
450                 return;
451         }
452         if (_mono_panner) {
453                 _mono_panner->edit ();
454         } else if (_stereo_panner) {
455                 _stereo_panner->edit ();
456         } else if (twod_panner) {
457                 if (!big_window) {
458                         big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
459                 }
460                 big_window->show ();
461         }
462 }
463
464 void
465 PannerUI::pan_reset ()
466 {
467         if (_panshell->bypassed()) {
468                 return;
469         }
470         _panner->reset ();
471 }
472
473 void
474 PannerUI::pan_set_custom_type (std::string uri) {
475         if (_suspend_menu_callbacks) return;
476         _panshell->select_panner_by_uri(uri);
477 }
478
479 void
480 PannerUI::effective_pan_display ()
481 {
482         if (_stereo_panner) {
483                 _stereo_panner->queue_draw ();
484         } else if (_mono_panner) {
485                 _mono_panner->queue_draw ();
486         } else if (twod_panner) {
487                 twod_panner->queue_draw ();
488         }
489 }
490
491 void
492 PannerUI::update_pan_sensitive ()
493 {
494         bool const sensitive = !(_panner->pannable()->automation_state() & Play);
495
496         pan_vbox.set_sensitive (sensitive);
497
498         if (big_window) {
499                 big_window->set_sensitive (sensitive);
500         }
501 }
502
503 gint
504 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
505 {
506         using namespace Menu_Helpers;
507
508         if (ev->type == GDK_BUTTON_RELEASE) {
509                 return TRUE;
510         }
511
512         switch (ev->button) {
513         case 1:
514                 if (pan_astate_menu == 0) {
515                         build_astate_menu ();
516                 }
517                 pan_astate_menu->popup (1, ev->time);
518                 break;
519         default:
520                 break;
521         }
522
523         return TRUE;
524 }
525
526 void
527 PannerUI::pan_automation_state_changed ()
528 {
529         boost::shared_ptr<Pannable> pannable (_panner->pannable());
530         pan_automation_state_button.set_label (GainMeterBase::short_astate_string(pannable->automation_state()));
531
532         bool x = (pannable->automation_state() != ARDOUR::Off);
533
534         if (pan_automation_state_button.get_active() != x) {
535                 ignore_toggle = true;
536                 pan_automation_state_button.set_active (x);
537                 ignore_toggle = false;
538         }
539
540         update_pan_sensitive ();
541 }
542
543 void
544 PannerUI::show_width ()
545 {
546 }
547
548 void
549 PannerUI::width_adjusted ()
550 {
551 }
552
553 void
554 PannerUI::show_position ()
555 {
556 }
557
558 void
559 PannerUI::position_adjusted ()
560 {
561 }
562
563 void
564 PannerUI::set_available_panners(std::map<std::string,std::string> p)
565 {
566         _panner_list = p;
567 }