Fix DSP load sorting with inactive plugins
[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 (GainMeterBase::astate_string (ARDOUR::Off),
155                         sigc::bind ( sigc::mem_fun (_panner.get(), &Panner::set_automation_state), (AutoState) ARDOUR::Off)));
156         pan_astate_menu->items().push_back (MenuElem (GainMeterBase::astate_string (ARDOUR::Play),
157                         sigc::bind ( sigc::mem_fun (_panner.get(), &Panner::set_automation_state), (AutoState) Play)));
158         pan_astate_menu->items().push_back (MenuElem (GainMeterBase::astate_string (ARDOUR::Write),
159                         sigc::bind ( sigc::mem_fun (_panner.get(), &Panner::set_automation_state), (AutoState) Write)));
160         pan_astate_menu->items().push_back (MenuElem (GainMeterBase::astate_string (ARDOUR::Touch),
161                         sigc::bind (sigc::mem_fun (_panner.get(), &Panner::set_automation_state), (AutoState) Touch)));
162         pan_astate_menu->items().push_back (MenuElem (GainMeterBase::astate_string (ARDOUR::Latch),
163                         sigc::bind ( sigc::mem_fun (_panner.get(), &Panner::set_automation_state), (AutoState) Latch)));
164
165 }
166
167 void
168 PannerUI::build_astyle_menu ()
169 {
170         using namespace Menu_Helpers;
171
172         if (pan_astyle_menu == 0) {
173                 pan_astyle_menu = new Menu;
174                 pan_astyle_menu->set_name ("ArdourContextMenu");
175         } else {
176                 pan_astyle_menu->items().clear();
177         }
178
179         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
180         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
181 }
182
183 void
184 PannerUI::on_size_allocate (Allocation& a)
185 {
186         HBox::on_size_allocate (a);
187 }
188
189 void
190 PannerUI::set_width (Width w)
191 {
192         _width = w;
193 }
194
195 PannerUI::~PannerUI ()
196 {
197         delete twod_panner;
198         delete big_window;
199         delete pan_menu;
200         delete pan_astyle_menu;
201         delete pan_astate_menu;
202         delete _stereo_panner;
203         delete _mono_panner;
204 }
205
206 void
207 PannerUI::panshell_changed ()
208 {
209         set_panner (_panshell, _panshell->panner());
210         setup_pan ();
211 }
212
213 void
214 PannerUI::setup_pan ()
215 {
216         int const nouts = _panner ? _panner->out().n_audio() : -1;
217         int const nins = _panner ? _panner->in().n_audio() : -1;
218
219         assert (_panshell);
220
221         if (nouts == _current_nouts
222                         && nins == _current_nins
223                         && _current_uri == _panshell->panner_gui_uri()
224                         )
225         {
226                 return;
227         }
228
229         _current_nins = nins;
230         _current_nouts = nouts;
231         _current_uri = _panshell->panner_gui_uri();
232
233         container_clear (pan_vbox);
234
235         delete twod_panner;
236         twod_panner = 0;
237         delete _stereo_panner;
238         _stereo_panner = 0;
239         delete _mono_panner;
240         _mono_panner = 0;
241
242         if (!_panner) {
243                 delete big_window;
244                 big_window = 0;
245                 return;
246         }
247
248         const float scale = std::max (1.f, UIConfiguration::instance().get_ui_scale());
249
250         if (_current_uri == "http://ardour.org/plugin/panner_2in2out#ui")
251         {
252                 delete big_window;
253                 big_window = 0;
254
255                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
256
257                 _stereo_panner = new StereoPanner (_panshell);
258                 _stereo_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
259                 _stereo_panner->set_send_drawing_mode (_send_mode);
260                 pan_vbox.pack_start (*_stereo_panner, false, false);
261
262                 boost::shared_ptr<AutomationControl> ac;
263
264                 ac = pannable->pan_azimuth_control;
265                 _stereo_panner->StartPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
266                                         boost::weak_ptr<AutomationControl> (ac)));
267                 _stereo_panner->StopPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
268                                         boost::weak_ptr<AutomationControl>(ac)));
269
270                 ac = pannable->pan_width_control;
271                 _stereo_panner->StartWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
272                                         boost::weak_ptr<AutomationControl> (ac)));
273                 _stereo_panner->StopWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
274                                         boost::weak_ptr<AutomationControl>(ac)));
275                 _stereo_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
276         }
277         else if (_current_uri == "http://ardour.org/plugin/panner_1in2out#ui"
278                         || _current_uri == "http://ardour.org/plugin/panner_balance#ui")
279         {
280                 delete big_window;
281                 big_window = 0;
282                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
283                 boost::shared_ptr<AutomationControl> ac = pannable->pan_azimuth_control;
284
285                 _mono_panner = new MonoPanner (_panshell);
286
287                 _mono_panner->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
288                                         boost::weak_ptr<AutomationControl> (ac)));
289                 _mono_panner->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
290                                         boost::weak_ptr<AutomationControl>(ac)));
291
292                 _mono_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
293
294                 _mono_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
295                 _mono_panner->set_send_drawing_mode (_send_mode);
296
297                 update_pan_sensitive ();
298                 pan_vbox.pack_start (*_mono_panner, false, false);
299         }
300         else if (_current_uri == "http://ardour.org/plugin/panner_vbap#ui")
301         {
302                 if (!twod_panner) {
303                         twod_panner = new Panner2d (_panshell, rintf(61.f * scale));
304                         twod_panner->set_name ("MixerPanZone");
305                         twod_panner->show ();
306                         twod_panner->signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event), false);
307                 }
308
309                 update_pan_sensitive ();
310                 twod_panner->reset (nins);
311                 if (big_window) {
312                         big_window->reset (nins);
313                 }
314                 twod_panner->set_size_request (-1, rintf(61.f * scale));
315                 twod_panner->set_send_drawing_mode (_send_mode);
316
317                 /* and finally, add it to the panner frame */
318
319                 pan_vbox.pack_start (*twod_panner, false, false);
320         }
321         else
322         {
323                 /* stick something into the panning viewport so that it redraws */
324                 EventBox* eb = manage (new EventBox());
325                 pan_vbox.pack_start (*eb, false, false);
326
327                 delete big_window;
328                 big_window = 0;
329         }
330
331         pan_vbox.show_all ();
332 }
333
334 void
335 PannerUI::set_send_drawing_mode (bool onoff)
336 {
337         if (_stereo_panner) {
338                 _stereo_panner->set_send_drawing_mode (onoff);
339         } else if (_mono_panner) {
340                 _mono_panner->set_send_drawing_mode (onoff);
341         } else if (twod_panner) {
342                 twod_panner->set_send_drawing_mode (onoff);
343         }
344         _send_mode = onoff;
345 }
346
347 void
348 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
349 {
350         boost::shared_ptr<AutomationControl> ac = wac.lock();
351         if (!ac) {
352                 return;
353         }
354         ac->start_touch (ac->session().transport_sample());
355 }
356
357 void
358 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
359 {
360         boost::shared_ptr<AutomationControl> ac = wac.lock();
361         if (!ac) {
362                 return;
363         }
364         ac->stop_touch (ac->session().transport_sample());
365 }
366
367 bool
368 PannerUI::pan_button_event (GdkEventButton* ev)
369 {
370         switch (ev->button) {
371         case 1:
372                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
373                         if (!big_window) {
374                                 big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
375                         }
376                         big_window->show ();
377                         return true;
378                 }
379                 break;
380
381         case 3:
382                 if (pan_menu == 0) {
383                         pan_menu = new Menu;
384                         pan_menu->set_name ("ArdourContextMenu");
385                 }
386                 build_pan_menu ();
387                 pan_menu->popup (1, ev->time);
388                 return true;
389                 break;
390         default:
391                 return false;
392         }
393
394         return false; // what's wrong with gcc?
395 }
396
397 void
398 PannerUI::build_pan_menu ()
399 {
400         using namespace Menu_Helpers;
401         MenuList& items (pan_menu->items());
402
403         items.clear ();
404
405         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
406         bypass_menu_item = static_cast<Gtk::CheckMenuItem*> (&items.back());
407
408         /* set state first, connect second */
409
410         bypass_menu_item->set_active (_panshell->bypassed());
411         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
412
413         if (!_panshell->bypassed()) {
414                 items.push_back (MenuElem (_("Reset"), sigc::mem_fun (*this, &PannerUI::pan_reset)));
415                 items.push_back (MenuElem (_("Edit..."), sigc::mem_fun (*this, &PannerUI::pan_edit)));
416         }
417
418         if (_panner_list.size() > 1 && !_panshell->bypassed()) {
419                 RadioMenuItem::Group group;
420                 items.push_back (SeparatorElem());
421
422                 _suspend_menu_callbacks = true;
423                 for (std::map<std::string,std::string>::const_iterator p = _panner_list.begin(); p != _panner_list.end(); ++p) {
424                         items.push_back (RadioMenuElem (group, p->second,
425                                                 sigc::bind(sigc::mem_fun (*this, &PannerUI::pan_set_custom_type), p->first)));
426                         RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&items.back ());
427                         i->set_active (_panshell->current_panner_uri() == p->first);
428                 }
429                 _suspend_menu_callbacks = false;
430         }
431 }
432
433 void
434 PannerUI::pan_bypass_toggle ()
435 {
436         if (bypass_menu_item && (_panshell->bypassed() != bypass_menu_item->get_active())) {
437                 _panshell->set_bypassed (!_panshell->bypassed());
438         }
439 }
440
441 void
442 PannerUI::pan_edit ()
443 {
444         if (_panshell->bypassed()) {
445                 return;
446         }
447         if (_mono_panner) {
448                 _mono_panner->edit ();
449         } else if (_stereo_panner) {
450                 _stereo_panner->edit ();
451         } else if (twod_panner) {
452                 if (!big_window) {
453                         big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
454                 }
455                 big_window->show ();
456         }
457 }
458
459 void
460 PannerUI::pan_reset ()
461 {
462         if (_panshell->bypassed()) {
463                 return;
464         }
465         _panner->reset ();
466 }
467
468 void
469 PannerUI::pan_set_custom_type (std::string uri) {
470         if (_suspend_menu_callbacks) return;
471         _panshell->select_panner_by_uri(uri);
472 }
473
474 void
475 PannerUI::effective_pan_display ()
476 {
477         if (_stereo_panner) {
478                 _stereo_panner->queue_draw ();
479         } else if (_mono_panner) {
480                 _mono_panner->queue_draw ();
481         } else if (twod_panner) {
482                 twod_panner->queue_draw ();
483         }
484 }
485
486 void
487 PannerUI::update_pan_sensitive ()
488 {
489         bool const sensitive = !(_panner->pannable()->automation_state() & Play);
490
491         pan_vbox.set_sensitive (sensitive);
492
493         if (big_window) {
494                 big_window->set_sensitive (sensitive);
495         }
496 }
497
498 gint
499 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
500 {
501         using namespace Menu_Helpers;
502
503         if (ev->type == GDK_BUTTON_RELEASE) {
504                 return TRUE;
505         }
506
507         switch (ev->button) {
508         case 1:
509                 if (pan_astate_menu == 0) {
510                         build_astate_menu ();
511                 }
512                 pan_astate_menu->popup (1, ev->time);
513                 break;
514         default:
515                 break;
516         }
517
518         return TRUE;
519 }
520
521 void
522 PannerUI::pan_automation_state_changed ()
523 {
524         boost::shared_ptr<Pannable> pannable (_panner->pannable());
525         pan_automation_state_button.set_label (GainMeterBase::short_astate_string(pannable->automation_state()));
526
527         bool x = (pannable->automation_state() != ARDOUR::Off);
528
529         if (pan_automation_state_button.get_active() != x) {
530                 ignore_toggle = true;
531                 pan_automation_state_button.set_active (x);
532                 ignore_toggle = false;
533         }
534
535         update_pan_sensitive ();
536 }
537
538 void
539 PannerUI::show_width ()
540 {
541 }
542
543 void
544 PannerUI::width_adjusted ()
545 {
546 }
547
548 void
549 PannerUI::show_position ()
550 {
551 }
552
553 void
554 PannerUI::position_adjusted ()
555 {
556 }
557
558 void
559 PannerUI::set_available_panners(std::map<std::string,std::string> p)
560 {
561         _panner_list = p;
562 }