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