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