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