Update GPL boilerplate and (C)
[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 "panner_ui.h"
41 #include "panner2d.h"
42 #include "gui_thread.h"
43 #include "stereo_panner.h"
44 #include "timers.h"
45 #include "mono_panner.h"
46 #include "ui_config.h"
47
48 #include "pbd/i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtkmm2ext;
54 using namespace Gtk;
55
56 PannerUI::PannerUI (Session* s)
57         : _current_nouts (-1)
58         , _current_nins (-1)
59         , _current_uri ("")
60         , _send_mode (false)
61         , pan_automation_state_button ("")
62         , _panner_list()
63 {
64         set_session (s);
65
66         ignore_toggle = false;
67         pan_menu = 0;
68         pan_astate_menu = 0;
69         pan_astyle_menu = 0;
70         in_pan_update = false;
71         _stereo_panner = 0;
72         _mono_panner = 0;
73         _ignore_width_change = false;
74         _ignore_position_change = false;
75
76         pan_automation_state_button.set_name ("MixerAutomationPlaybackButton");
77
78         ArdourWidgets::set_tooltip (pan_automation_state_button, _("Pan automation mode"));
79
80         //set_size_request_to_display_given_text (pan_automation_state_button, X_("O"), 2, 2);
81
82         pan_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
83
84         pan_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_state_button_event), false);
85
86         pan_vbox.set_spacing (2);
87         pack_start (pan_vbox, true, true);
88
89         twod_panner = 0;
90         big_window = 0;
91
92         set_width(Narrow);
93 }
94
95 void
96 PannerUI::set_panner (boost::shared_ptr<PannerShell> ps, boost::shared_ptr<Panner> p)
97 {
98         /* note that the panshell might not change here (i.e. ps == _panshell)
99          */
100
101         connections.drop_connections ();
102
103         delete pan_astyle_menu;
104         pan_astyle_menu = 0;
105
106         delete pan_astate_menu;
107         pan_astate_menu = 0;
108
109         _panshell = ps;
110         _panner = p;
111
112         delete twod_panner;
113         twod_panner = 0;
114
115         delete _stereo_panner;
116         _stereo_panner = 0;
117
118         delete _mono_panner;
119         _mono_panner = 0;
120
121         if (!_panner) {
122                 return;
123         }
124
125         _panshell->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::panshell_changed, this), gui_context());
126
127         /* new panner object, force complete reset of panner GUI
128          */
129
130         _current_nouts = 0;
131         _current_nins = 0;
132
133         setup_pan ();
134         update_pan_sensitive ();
135         pan_automation_state_changed ();
136 }
137
138 void
139 PannerUI::build_astate_menu ()
140 {
141         using namespace Menu_Helpers;
142
143         if (pan_astate_menu == 0) {
144                 pan_astate_menu = new Menu;
145                 pan_astate_menu->set_name ("ArdourContextMenu");
146         } else {
147                 pan_astate_menu->items().clear ();
148         }
149
150         /** TRANSLATORS: this is `Manual' in the sense of automation not being played,
151             so that changes to pan must be done by hand.
152         */
153         pan_astate_menu->items().push_back (MenuElem (S_("Automation|Manual"), sigc::bind (
154                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
155                         (AutoState) ARDOUR::Off)));
156         pan_astate_menu->items().push_back (MenuElem (_("Play"), sigc::bind (
157                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
158                         (AutoState) Play)));
159         pan_astate_menu->items().push_back (MenuElem (_("Write"), sigc::bind (
160                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
161                         (AutoState) Write)));
162         pan_astate_menu->items().push_back (MenuElem (_("Touch"), sigc::bind (
163                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
164                         (AutoState) Touch)));
165         pan_astate_menu->items().push_back (MenuElem (_("Latch"), sigc::bind (
166                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
167                         (AutoState) Latch)));
168
169 }
170
171 void
172 PannerUI::build_astyle_menu ()
173 {
174         using namespace Menu_Helpers;
175
176         if (pan_astyle_menu == 0) {
177                 pan_astyle_menu = new Menu;
178                 pan_astyle_menu->set_name ("ArdourContextMenu");
179         } else {
180                 pan_astyle_menu->items().clear();
181         }
182
183         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
184         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
185 }
186
187 void
188 PannerUI::on_size_allocate (Allocation& a)
189 {
190         HBox::on_size_allocate (a);
191 }
192
193 void
194 PannerUI::set_width (Width w)
195 {
196         _width = w;
197 }
198
199 PannerUI::~PannerUI ()
200 {
201         delete twod_panner;
202         delete big_window;
203         delete pan_menu;
204         delete pan_astyle_menu;
205         delete pan_astate_menu;
206         delete _stereo_panner;
207         delete _mono_panner;
208 }
209
210 void
211 PannerUI::panshell_changed ()
212 {
213         set_panner (_panshell, _panshell->panner());
214         setup_pan ();
215 }
216
217 void
218 PannerUI::setup_pan ()
219 {
220         int const nouts = _panner ? _panner->out().n_audio() : -1;
221         int const nins = _panner ? _panner->in().n_audio() : -1;
222
223         if (nouts == _current_nouts
224                         && nins == _current_nins
225                         && _current_uri == _panshell->panner_gui_uri()
226                         )
227         {
228                 return;
229         }
230
231         _current_nins = nins;
232         _current_nouts = nouts;
233         _current_uri = _panshell->panner_gui_uri();
234
235         container_clear (pan_vbox);
236
237         delete twod_panner;
238         twod_panner = 0;
239         delete _stereo_panner;
240         _stereo_panner = 0;
241         delete _mono_panner;
242         _mono_panner = 0;
243
244         if (!_panner) {
245                 delete big_window;
246                 big_window = 0;
247                 return;
248         }
249
250         const float scale = std::max (1.f, UIConfiguration::instance().get_ui_scale());
251
252         if (_current_uri == "http://ardour.org/plugin/panner_2in2out#ui")
253         {
254                 delete big_window;
255                 big_window = 0;
256
257                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
258
259                 _stereo_panner = new StereoPanner (_panshell);
260                 _stereo_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
261                 _stereo_panner->set_send_drawing_mode (_send_mode);
262                 pan_vbox.pack_start (*_stereo_panner, false, false);
263
264                 boost::shared_ptr<AutomationControl> ac;
265
266                 ac = pannable->pan_azimuth_control;
267                 _stereo_panner->StartPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
268                                         boost::weak_ptr<AutomationControl> (ac)));
269                 _stereo_panner->StopPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
270                                         boost::weak_ptr<AutomationControl>(ac)));
271
272                 ac = pannable->pan_width_control;
273                 _stereo_panner->StartWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
274                                         boost::weak_ptr<AutomationControl> (ac)));
275                 _stereo_panner->StopWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
276                                         boost::weak_ptr<AutomationControl>(ac)));
277                 _stereo_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
278         }
279         else if (_current_uri == "http://ardour.org/plugin/panner_1in2out#ui"
280                         || _current_uri == "http://ardour.org/plugin/panner_balance#ui")
281         {
282                 delete big_window;
283                 big_window = 0;
284                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
285                 boost::shared_ptr<AutomationControl> ac = pannable->pan_azimuth_control;
286
287                 _mono_panner = new MonoPanner (_panshell);
288
289                 _mono_panner->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
290                                         boost::weak_ptr<AutomationControl> (ac)));
291                 _mono_panner->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
292                                         boost::weak_ptr<AutomationControl>(ac)));
293
294                 _mono_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
295
296                 _mono_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
297                 _mono_panner->set_send_drawing_mode (_send_mode);
298
299                 update_pan_sensitive ();
300                 pan_vbox.pack_start (*_mono_panner, false, false);
301         }
302         else if (_current_uri == "http://ardour.org/plugin/panner_vbap#ui")
303         {
304                 if (!twod_panner) {
305                         twod_panner = new Panner2d (_panshell, rintf(61.f * scale));
306                         twod_panner->set_name ("MixerPanZone");
307                         twod_panner->show ();
308                         twod_panner->signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event), false);
309                 }
310
311                 update_pan_sensitive ();
312                 twod_panner->reset (nins);
313                 if (big_window) {
314                         big_window->reset (nins);
315                 }
316                 twod_panner->set_size_request (-1, rintf(61.f * scale));
317                 twod_panner->set_send_drawing_mode (_send_mode);
318
319                 /* and finally, add it to the panner frame */
320
321                 pan_vbox.pack_start (*twod_panner, false, false);
322         }
323         else
324         {
325                 /* stick something into the panning viewport so that it redraws */
326                 EventBox* eb = manage (new EventBox());
327                 pan_vbox.pack_start (*eb, false, false);
328
329                 delete big_window;
330                 big_window = 0;
331         }
332
333         pan_vbox.show_all ();
334 }
335
336 void
337 PannerUI::set_send_drawing_mode (bool onoff)
338 {
339         if (_stereo_panner) {
340                 _stereo_panner->set_send_drawing_mode (onoff);
341         } else if (_mono_panner) {
342                 _mono_panner->set_send_drawing_mode (onoff);
343         } else if (twod_panner) {
344                 twod_panner->set_send_drawing_mode (onoff);
345         }
346         _send_mode = onoff;
347 }
348
349 void
350 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
351 {
352         boost::shared_ptr<AutomationControl> ac = wac.lock();
353         if (!ac) {
354                 return;
355         }
356         ac->start_touch (ac->session().transport_sample());
357 }
358
359 void
360 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
361 {
362         boost::shared_ptr<AutomationControl> ac = wac.lock();
363         if (!ac) {
364                 return;
365         }
366         ac->stop_touch (ac->session().transport_sample());
367 }
368
369 bool
370 PannerUI::pan_button_event (GdkEventButton* ev)
371 {
372         switch (ev->button) {
373         case 1:
374                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
375                         if (!big_window) {
376                                 big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
377                         }
378                         big_window->show ();
379                         return true;
380                 }
381                 break;
382
383         case 3:
384                 if (pan_menu == 0) {
385                         pan_menu = new Menu;
386                         pan_menu->set_name ("ArdourContextMenu");
387                 }
388                 build_pan_menu ();
389                 pan_menu->popup (1, ev->time);
390                 return true;
391                 break;
392         default:
393                 return false;
394         }
395
396         return false; // what's wrong with gcc?
397 }
398
399 void
400 PannerUI::build_pan_menu ()
401 {
402         using namespace Menu_Helpers;
403         MenuList& items (pan_menu->items());
404
405         items.clear ();
406
407         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
408         bypass_menu_item = static_cast<Gtk::CheckMenuItem*> (&items.back());
409
410         /* set state first, connect second */
411
412         bypass_menu_item->set_active (_panshell->bypassed());
413         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
414
415         if (!_panshell->bypassed()) {
416                 items.push_back (MenuElem (_("Reset"), sigc::mem_fun (*this, &PannerUI::pan_reset)));
417                 items.push_back (MenuElem (_("Edit..."), sigc::mem_fun (*this, &PannerUI::pan_edit)));
418         }
419
420         if (_panner_list.size() > 1 && !_panshell->bypassed()) {
421                 RadioMenuItem::Group group;
422                 items.push_back (SeparatorElem());
423
424                 _suspend_menu_callbacks = true;
425                 for (std::map<std::string,std::string>::const_iterator p = _panner_list.begin(); p != _panner_list.end(); ++p) {
426                         items.push_back (RadioMenuElem (group, p->second,
427                                                 sigc::bind(sigc::mem_fun (*this, &PannerUI::pan_set_custom_type), p->first)));
428                         RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&items.back ());
429                         i->set_active (_panshell->current_panner_uri() == p->first);
430                 }
431                 _suspend_menu_callbacks = false;
432         }
433 }
434
435 void
436 PannerUI::pan_bypass_toggle ()
437 {
438         if (bypass_menu_item && (_panshell->bypassed() != bypass_menu_item->get_active())) {
439                 _panshell->set_bypassed (!_panshell->bypassed());
440         }
441 }
442
443 void
444 PannerUI::pan_edit ()
445 {
446         if (_panshell->bypassed()) {
447                 return;
448         }
449         if (_mono_panner) {
450                 _mono_panner->edit ();
451         } else if (_stereo_panner) {
452                 _stereo_panner->edit ();
453         } else if (twod_panner) {
454                 if (!big_window) {
455                         big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
456                 }
457                 big_window->show ();
458         }
459 }
460
461 void
462 PannerUI::pan_reset ()
463 {
464         if (_panshell->bypassed()) {
465                 return;
466         }
467         _panner->reset ();
468 }
469
470 void
471 PannerUI::pan_set_custom_type (std::string uri) {
472         if (_suspend_menu_callbacks) return;
473         _panshell->select_panner_by_uri(uri);
474 }
475
476 void
477 PannerUI::effective_pan_display ()
478 {
479         if (_stereo_panner) {
480                 _stereo_panner->queue_draw ();
481         } else if (_mono_panner) {
482                 _mono_panner->queue_draw ();
483         } else if (twod_panner) {
484                 twod_panner->queue_draw ();
485         }
486 }
487
488 void
489 PannerUI::update_pan_sensitive ()
490 {
491         bool const sensitive = !(_panner->pannable()->automation_state() & Play);
492
493         pan_vbox.set_sensitive (sensitive);
494
495         if (big_window) {
496                 big_window->set_sensitive (sensitive);
497         }
498 }
499
500 gint
501 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
502 {
503         using namespace Menu_Helpers;
504
505         if (ev->type == GDK_BUTTON_RELEASE) {
506                 return TRUE;
507         }
508
509         switch (ev->button) {
510         case 1:
511                 if (pan_astate_menu == 0) {
512                         build_astate_menu ();
513                 }
514                 pan_astate_menu->popup (1, ev->time);
515                 break;
516         default:
517                 break;
518         }
519
520         return TRUE;
521 }
522
523 void
524 PannerUI::pan_automation_state_changed ()
525 {
526         boost::shared_ptr<Pannable> pannable (_panner->pannable());
527
528         switch (_width) {
529         case Wide:
530                 pan_automation_state_button.set_label (astate_string(pannable->automation_state()));
531                 break;
532         case Narrow:
533                 pan_automation_state_button.set_label (short_astate_string(pannable->automation_state()));
534                 break;
535         }
536
537         bool x = (pannable->automation_state() != ARDOUR::Off);
538
539         if (pan_automation_state_button.get_active() != x) {
540                 ignore_toggle = true;
541                 pan_automation_state_button.set_active (x);
542                 ignore_toggle = false;
543         }
544
545         update_pan_sensitive ();
546 }
547
548 string
549 PannerUI::astate_string (AutoState state)
550 {
551         return _astate_string (state, false);
552 }
553
554 string
555 PannerUI::short_astate_string (AutoState state)
556 {
557         return _astate_string (state, true);
558 }
559
560 string
561 PannerUI::_astate_string (AutoState state, bool shrt)
562 {
563         string sstr;
564
565         switch (state) {
566         case ARDOUR::Off:
567                 sstr = (shrt ? "M" : S_("Manual|M"));
568                 break;
569         case Play:
570                 sstr = (shrt ? "P" : S_("Play|P"));
571                 break;
572         case Touch:
573                 sstr = (shrt ? "T" : S_("Touch|T"));
574                 break;
575         case Latch:
576                 sstr = (shrt ? "L" : S_("Latch|L"));
577                 break;
578         case Write:
579                 sstr = (shrt ? "W" : S_("Write|W"));
580                 break;
581         }
582
583         return sstr;
584 }
585
586 void
587 PannerUI::show_width ()
588 {
589 }
590
591 void
592 PannerUI::width_adjusted ()
593 {
594 }
595
596 void
597 PannerUI::show_position ()
598 {
599 }
600
601 void
602 PannerUI::position_adjusted ()
603 {
604 }
605
606 void
607 PannerUI::set_available_panners(std::map<std::string,std::string> p)
608 {
609         _panner_list = p;
610 }