hack up the stereo 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 "ardour/io.h"
22 #include "ardour/dB.h"
23 #include <gtkmm2ext/utils.h>
24 #include <gtkmm2ext/barcontroller.h>
25 #include "midi++/manager.h"
26 #include "pbd/fastlog.h"
27
28 #include "ardour_ui.h"
29 #include "panner_ui.h"
30 #include "panner2d.h"
31 #include "utils.h"
32 #include "panner.h"
33 #include "gui_thread.h"
34 #include "stereo_panner.h"
35
36 #include "ardour/delivery.h"
37 #include "ardour/session.h"
38 #include "ardour/panner.h"
39 #include "ardour/route.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Gtkmm2ext;
47 using namespace Gtk;
48
49 const int PannerUI::pan_bar_height = 20;
50
51 PannerUI::PannerUI (Session* s)
52         : _current_nouts (-1)
53         , _current_npans (-1)
54         , hAdjustment(0.0, 0.0, 0.0)
55         , vAdjustment(0.0, 0.0, 0.0)
56         , panning_viewport(hAdjustment, vAdjustment)
57         , panning_up_arrow (Gtk::ARROW_UP, Gtk::SHADOW_OUT)
58         , panning_down_arrow (Gtk::ARROW_DOWN, Gtk::SHADOW_OUT)
59         , panning_link_button (_("link"))
60         , pan_automation_style_button ("")
61         , pan_automation_state_button ("")
62         , _bar_spinner_active (false)
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         _ignore_width_change = false;
73         _ignore_position_change = false;
74
75         pan_automation_style_button.set_name ("MixerAutomationModeButton");
76         pan_automation_state_button.set_name ("MixerAutomationPlaybackButton");
77
78         ARDOUR_UI::instance()->set_tip (pan_automation_state_button, _("Pan automation mode"));
79         ARDOUR_UI::instance()->set_tip (pan_automation_style_button, _("Pan automation type"));
80
81         //set_size_request_to_display_given_text (pan_automation_state_button, X_("O"), 2, 2);
82         //set_size_request_to_display_given_text (pan_automation_style_button, X_("0"), 2, 2);
83
84         panning_viewport.set_name (X_("BaseFrame"));
85
86         ARDOUR_UI::instance()->set_tip (panning_link_button,
87                                                    _("panning link control"));
88         ARDOUR_UI::instance()->set_tip (panning_link_direction_button,
89                                                    _("panning link direction"));
90
91         pan_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
92         pan_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
93
94         pan_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_style_button_event), false);
95         pan_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_state_button_event), false);
96
97         panning_link_button.set_name (X_("PanningLinkButton"));
98         panning_link_direction_button.set_name (X_("PanningLinkDirectionButton"));
99
100         panning_link_box.pack_start (panning_link_button, true, true);
101         panning_link_box.pack_start (panning_link_direction_button, true, true);
102         panning_link_box.pack_start (pan_automation_state_button, true, true);
103
104         /* the pixmap will be reset at some point, but the key thing is that
105            we need a pixmap in the button just to get started.
106         */
107         panning_link_direction_button.add (*(manage (new Image (get_xpm("forwardblarrow.xpm")))));
108
109         panning_link_direction_button.signal_clicked().connect
110                 (sigc::mem_fun(*this, &PannerUI::panning_link_direction_clicked));
111
112         panning_link_button.signal_button_press_event().connect
113                 (sigc::mem_fun(*this, &PannerUI::panning_link_button_press), false);
114         panning_link_button.signal_button_release_event().connect
115                 (sigc::mem_fun(*this, &PannerUI::panning_link_button_release), false);
116
117         panning_up.set_border_width (3);
118         panning_down.set_border_width (3);
119         panning_up.add (panning_up_arrow);
120         panning_down.add (panning_down_arrow);
121         panning_up.set_name (X_("PanScrollerBase"));
122         panning_down.set_name (X_("PanScrollerBase"));
123         panning_up_arrow.set_name (X_("PanScrollerArrow"));
124         panning_down_arrow.set_name (X_("PanScrollerArrow"));
125
126         pan_vbox.set_spacing (2);
127         pan_vbox.pack_start (panning_viewport, Gtk::PACK_SHRINK);
128         pan_vbox.pack_start (panning_link_box, Gtk::PACK_SHRINK);
129         
130         pack_start (pan_vbox, true, true);
131
132         twod_panner = 0;
133         big_window = 0;
134
135         set_width(Narrow);
136 }
137
138 void
139 PannerUI::set_panner (boost::shared_ptr<Panner> p)
140 {
141         connections.drop_connections ();
142
143         delete pan_astyle_menu;
144         pan_astyle_menu = 0;
145
146         delete pan_astate_menu;
147         pan_astate_menu = 0;
148
149         _panner = p;
150
151         delete twod_panner;
152         twod_panner = 0;
153
154         if (!_panner) {
155                 return;
156         }
157
158         _panner->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::panner_changed, this, this), gui_context());
159         _panner->LinkStateChanged.connect (connections, invalidator (*this), boost::bind (&PannerUI::update_pan_linkage, this), gui_context());
160         _panner->StateChanged.connect (connections, invalidator (*this), boost::bind (&PannerUI::update_pan_state, this), gui_context());
161
162         panner_changed (0);
163         update_pan_sensitive ();
164         update_pan_linkage ();
165         pan_automation_state_changed ();
166 }
167
168 void
169 PannerUI::build_astate_menu ()
170 {
171         using namespace Menu_Helpers;
172
173         if (pan_astate_menu == 0) {
174                 pan_astate_menu = new Menu;
175                 pan_astate_menu->set_name ("ArdourContextMenu");
176         } else {
177                 pan_astate_menu->items().clear ();
178         }
179
180         pan_astate_menu->items().push_back (MenuElem (_("Manual"), sigc::bind (
181                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
182                         (AutoState) Off)));
183         pan_astate_menu->items().push_back (MenuElem (_("Play"), sigc::bind (
184                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
185                         (AutoState) Play)));
186         pan_astate_menu->items().push_back (MenuElem (_("Write"), sigc::bind (
187                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
188                         (AutoState) Write)));
189         pan_astate_menu->items().push_back (MenuElem (_("Touch"), sigc::bind (
190                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
191                         (AutoState) Touch)));
192
193 }
194
195 void
196 PannerUI::build_astyle_menu ()
197 {
198         using namespace Menu_Helpers;
199
200         if (pan_astyle_menu == 0) {
201                 pan_astyle_menu = new Menu;
202                 pan_astyle_menu->set_name ("ArdourContextMenu");
203         } else {
204                 pan_astyle_menu->items().clear();
205         }
206
207         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
208         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
209 }
210
211 boost::shared_ptr<PBD::Controllable>
212 PannerUI::get_controllable()
213 {
214         return pan_bars[0]->get_controllable();
215 }
216
217 bool
218 PannerUI::panning_link_button_press (GdkEventButton*)
219 {
220         return true;
221 }
222
223 bool
224 PannerUI::panning_link_button_release (GdkEventButton*)
225 {
226         if (!ignore_toggle) {
227                 _panner->set_linked (!_panner->linked());
228         }
229         return true;
230 }
231
232 void
233 PannerUI::panning_link_direction_clicked()
234 {
235         switch (_panner->link_direction()) {
236         case Panner::SameDirection:
237                 _panner->set_link_direction (Panner::OppositeDirection);
238                 break;
239         default:
240                 _panner->set_link_direction (Panner::SameDirection);
241                 break;
242         }
243 }
244
245 void
246 PannerUI::update_pan_linkage ()
247 {
248         ENSURE_GUI_THREAD (*this, &PannerUI::update_pan_linkage)
249
250         bool const x = _panner->linked();
251         bool const bx = panning_link_button.get_active();
252
253         if (x != bx) {
254
255                 ignore_toggle = true;
256                 panning_link_button.set_active (x);
257                 ignore_toggle = false;
258         }
259
260         panning_link_direction_button.set_sensitive (x);
261
262         switch (_panner->link_direction()) {
263         case Panner::SameDirection:
264                 panning_link_direction_button.set_image (*(manage (new Image (get_xpm ("forwardblarrow.xpm")))));
265                 break;
266         default:
267                 panning_link_direction_button.set_image (*(manage (new Image (get_xpm("revdblarrow.xpm")))));
268                 break;
269         }
270 }
271
272 void
273 PannerUI::on_size_allocate (Allocation& a)
274 {
275         HBox::on_size_allocate (a);
276 }
277
278 void
279 PannerUI::set_width (Width w)
280 {
281         switch (w) {
282         case Wide:
283                 panning_link_button.set_label (_("link"));
284                 break;
285         case Narrow:
286                 panning_link_button.set_label (_("L"));
287                 break;
288         }
289
290         _width = w;
291 }
292
293
294 PannerUI::~PannerUI ()
295 {
296         for (vector<Adjustment*>::iterator i = pan_adjustments.begin(); i != pan_adjustments.end(); ++i) {
297                 delete (*i);
298         }
299
300         for (vector<PannerBar*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
301                 delete (*i);
302         }
303
304         delete twod_panner;
305         delete big_window;
306         delete pan_menu;
307         delete pan_astyle_menu;
308         delete pan_astate_menu;
309         delete _stereo_panner;
310 }
311
312
313 void
314 PannerUI::panner_changed (void* src)
315 {
316         ENSURE_GUI_THREAD (*this, &PannerUI::panner_changed)
317
318         setup_pan ();
319
320         if (src == this) {
321                 return;
322         }
323
324         switch (_panner->npanners()) {
325         case 0:
326                 panning_link_direction_button.set_sensitive (false);
327                 panning_link_button.set_sensitive (false);
328                 return;
329         case 1:
330                 panning_link_direction_button.set_sensitive (false);
331                 panning_link_button.set_sensitive (false);
332                 break;
333         default:
334                 panning_link_direction_button.set_sensitive (_panner->linked ());
335                 panning_link_button.set_sensitive (true);
336         }
337
338         uint32_t const nouts = _panner->nouts();
339
340         switch (nouts) {
341         case 0:
342         case 1:
343                 /* relax */
344                 break;
345
346         case 2:
347                 /* bring pan bar state up to date */
348                 update_pan_bars (false);
349                 break;
350
351         default:
352                 // panner->move_puck (pan_value (pans[0], pans[1]), 0.5);
353                 break;
354         }
355 }
356
357 void
358 PannerUI::update_pan_state ()
359 {
360         /* currently nothing to do */
361         // ENSURE_GUI_THREAD (*this, &PannerUI::update_panner_state)
362 }
363
364 void
365 PannerUI::setup_pan ()
366 {
367         if (!_panner) {
368                 return;
369         }
370
371         uint32_t const nouts = _panner->nouts();
372         uint32_t const npans = _panner->npanners();
373
374         if (int32_t (nouts) == _current_nouts && int32_t (npans) == _current_npans) {
375                 return;
376         }
377
378         _pan_control_connections.drop_connections ();
379         for (uint32_t i = 0; i < _panner->npanners(); ++i) {
380                 connect_to_pan_control (i);
381         }
382
383         _current_nouts = nouts;
384         _current_npans = npans;
385
386         panning_viewport.remove ();
387
388         delete twod_panner;
389         twod_panner = 0;
390         delete _stereo_panner;
391         _stereo_panner = 0;
392
393         if (nouts == 0 || nouts == 1) {
394
395                 while (!pan_adjustments.empty()) {
396                         delete pan_bars.back();
397                         pan_bars.pop_back ();
398                         delete pan_adjustments.back();
399                         pan_adjustments.pop_back ();
400                 }
401
402                 /* stick something into the panning viewport so that it redraws */
403
404                 EventBox* eb = manage (new EventBox());
405                 panning_viewport.add (*eb);
406
407         } else if (nouts == 2) {
408
409                 vector<Adjustment*>::size_type asz;
410
411                 while (!pan_adjustments.empty()) {
412                         delete pan_bars.back();
413                         pan_bars.pop_back ();
414                         delete pan_adjustments.back();
415                         pan_adjustments.pop_back ();
416                 }
417
418                 if (npans == 2) {
419
420                         /* add integrated 2in/2out panner GUI */
421
422                         _stereo_panner = new StereoPanner (_panner->direction_control(), 
423                                                            _panner->width_control());
424                         _stereo_panner->set_size_request (-1, 2 * pan_bar_height);
425                         panning_viewport.add (*_stereo_panner);
426
427                 } else {
428                         
429                         /* N-in/2out - just use a set of single-channel panners */
430
431                         while ((asz = pan_adjustments.size()) < npans) {
432                                 
433                                 float x, rx;
434                                 PannerBar* bc;
435                                 
436                                 /* initialize adjustment with 0.0 (L) or 1.0 (R) for the first and second panners,
437                                    which serves as a default, otherwise use current value */
438                                 
439                                 rx = _panner->pan_control( asz)->get_value();
440                                 
441                                 if (npans == 1) {
442                                         x = 0.5;
443                                 } else if (asz == 0) {
444                                         x = 0.0;
445                                 } else if (asz == 1) {
446                                         x = 1.0;
447                                 } else {
448                                         x = rx;
449                                 }
450                                 
451                                 pan_adjustments.push_back (new Adjustment (x, 0, 1.0, 0.005, 0.05));
452                                 bc = new PannerBar (*pan_adjustments[asz],
453                                                     boost::static_pointer_cast<PBD::Controllable>( _panner->pan_control( asz )) );
454                                 
455                                 /* now set adjustment with current value of panner, then connect the signals */
456                                 pan_adjustments.back()->set_value(rx);
457                                 pan_adjustments.back()->signal_value_changed().connect (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_adjustment_changed), (uint32_t) asz));
458                                 connect_to_pan_control (asz);
459                                 
460                                 bc->set_name ("PanSlider");
461                                 bc->set_shadow_type (Gtk::SHADOW_NONE);
462                                 
463                                 boost::shared_ptr<AutomationControl> ac = _panner->pan_control (asz);
464                                 
465                                 if (asz) {
466                                         bc->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch), 
467                                                                               boost::weak_ptr<AutomationControl> (ac)));
468                                         bc->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch), 
469                                                                              boost::weak_ptr<AutomationControl>(ac)));
470                                 }
471                                 
472                                 char buf[64];
473                                 snprintf (buf, sizeof (buf), _("panner for channel %zu"), asz + 1);
474                                 ARDOUR_UI::instance()->set_tip (bc->event_widget(), buf);
475                                 
476                                 bc->event_widget().signal_button_release_event().connect
477                                         (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) asz));
478                                 
479                                 bc->set_size_request (-1, pan_bar_height);
480                                 bc->SpinnerActive.connect (sigc::mem_fun (*this, &PannerUI::bar_spinner_activate));
481                                 
482                                 pan_bars.push_back (bc);
483                                 pan_bar_packer.pack_start (*bc, false, false);
484                         }
485                         
486                         /* now that we actually have the pan bars,
487                            set their sensitivity based on current
488                            automation state.
489                         */
490                         
491
492                         update_pan_sensitive ();
493                         panning_viewport.add (pan_bar_packer);
494                 }
495
496
497         } else {
498
499                 if (!twod_panner) {
500                         twod_panner = new Panner2d (_panner, 61);
501                         twod_panner->set_name ("MixerPanZone");
502                         twod_panner->show ();
503
504                         twod_panner->signal_button_press_event().connect
505                                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) 0), false);
506                 }
507
508                 update_pan_sensitive ();
509                 twod_panner->reset (npans);
510                 if (big_window) {
511                         big_window->reset (npans);
512                 }
513                 twod_panner->set_size_request (-1, 61);
514
515                 /* and finally, add it to the panner frame */
516
517                 panning_viewport.add (*twod_panner);
518         }
519
520         panning_viewport.show_all ();
521 }
522
523 void
524 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
525 {
526         boost::shared_ptr<AutomationControl> ac = wac.lock();
527         if (!ac) {
528                 return;
529         }
530         ac->start_touch (ac->session().transport_frame());
531 }
532
533 void
534 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
535 {
536         boost::shared_ptr<AutomationControl> ac = wac.lock();
537         if (!ac) {
538                 return;
539         }
540         ac->stop_touch (false, ac->session().transport_frame());
541 }
542
543 bool
544 PannerUI::pan_button_event (GdkEventButton* ev, uint32_t which)
545 {
546         switch (ev->button) {
547         case 1:
548                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
549                         if (!big_window) {
550                                 big_window = new Panner2dWindow (_panner, 400, _panner->npanners());
551                         }
552                         big_window->show ();
553                         return true;
554                 }
555                 break;
556
557         case 3:
558                 if (pan_menu == 0) {
559                         pan_menu = manage (new Menu);
560                         pan_menu->set_name ("ArdourContextMenu");
561                 }
562                 build_pan_menu (which);
563                 pan_menu->popup (1, ev->time);
564                 return true;
565                 break;
566         default:
567                 return false;
568         }
569
570         return false; // what's wrong with gcc?
571 }
572
573 void
574 PannerUI::build_pan_menu (uint32_t which)
575 {
576         using namespace Menu_Helpers;
577         MenuList& items (pan_menu->items());
578
579         items.clear ();
580
581         items.push_back (CheckMenuElem (_("Mute")));
582
583         /* set state first, connect second */
584
585         (dynamic_cast<CheckMenuItem*> (&items.back()))->set_active (_panner->streampanner(which).muted());
586         (dynamic_cast<CheckMenuItem*> (&items.back()))->signal_toggled().connect
587                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_mute), which));
588
589         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
590         bypass_menu_item = static_cast<CheckMenuItem*> (&items.back());
591
592         /* set state first, connect second */
593
594         bypass_menu_item->set_active (_panner->bypassed());
595         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
596
597         items.push_back (MenuElem (_("Reset"), sigc::bind (sigc::mem_fun (*this, &PannerUI::pan_reset), which)));
598         items.push_back (SeparatorElem());
599         items.push_back (MenuElem (_("Reset all"), sigc::mem_fun (*this, &PannerUI::pan_reset_all)));
600 }
601
602 void
603 PannerUI::pan_mute (uint32_t which)
604 {
605         StreamPanner& sp = _panner->streampanner(which);
606         sp.set_muted (!sp.muted());
607 }
608
609 void
610 PannerUI::pan_bypass_toggle ()
611 {
612         if (bypass_menu_item && (_panner->bypassed() != bypass_menu_item->get_active())) {
613                 _panner->set_bypassed (!_panner->bypassed());
614         }
615 }
616
617 void
618 PannerUI::pan_reset (uint32_t which)
619 {
620         _panner->reset_streampanner (which);
621 }
622
623 void
624 PannerUI::pan_reset_all ()
625 {
626         _panner->reset_to_default ();
627 }
628
629 void
630 PannerUI::effective_pan_display ()
631 {
632         if (_panner->empty()) {
633                 return;
634         }
635
636         switch (_panner->nouts()) {
637         case 0:
638         case 1:
639                 /* relax */
640                 break;
641
642         case 2:
643                 update_pan_bars (true);
644                 break;
645
646         default:
647                 //panner->move_puck (pan_value (v, right), 0.5);
648                 break;
649         }
650 }
651
652 void
653 PannerUI::pan_adjustment_changed (uint32_t which)
654 {
655         if (!in_pan_update && which < _panner->npanners()) {
656
657                 float val = pan_adjustments[which]->get_value ();
658                 float const xpos = _panner->pan_control(which)->get_value();
659
660                 /* add a kinda-sorta detent for the middle */
661
662                 if (val != 0.5 && Panner::equivalent (val, 0.5)) {
663                         /* this is going to be reentrant, so just
664                            return after it.
665                         */
666
667                         in_pan_update = true;
668                         pan_adjustments[which]->set_value (0.5);
669                         in_pan_update = false;
670                         return;
671                 }
672
673                 if (!Panner::equivalent (val, xpos)) {
674
675                         _panner->pan_control(which)->set_value (val);
676                         /* XXX
677                            the panner objects have no access to the session,
678                            so do this here. ick.
679                         */
680                         _session->set_dirty();
681                 }
682         }
683 }
684
685 void
686 PannerUI::pan_value_changed (uint32_t which)
687 {
688         ENSURE_GUI_THREAD (*this, &PannerUI::pan_value_changed, which)
689
690         if (twod_panner) {
691
692                 in_pan_update = true;
693                 twod_panner->move_puck (which, _panner->streampanner(which).get_position());
694                 in_pan_update = false;
695
696         } else if (_stereo_panner) {
697
698                 /* its taken care of */
699
700         } else if (_panner->npanners() > 0 && which < _panner->npanners()) {
701
702                 AngularVector model = _panner->streampanner(which).get_position();
703                 double fract = pan_adjustments[which]->get_value();
704                 AngularVector view (BaseStereoPanner::lr_fract_to_azimuth (fract), 0.0);
705
706                 if (!Panner::equivalent (model, view)) {
707                         in_pan_update = true;
708                         pan_adjustments[which]->set_value (BaseStereoPanner::azimuth_to_lr_fract (model.azi));
709                         in_pan_update = false;
710                 }
711         }
712 }
713
714 void
715 PannerUI::update_pan_bars (bool only_if_aplay)
716 {
717         uint32_t n;
718         vector<Adjustment*>::iterator i;
719
720         in_pan_update = true;
721
722         /* this runs during automation playback, and moves the bar controllers
723            and/or pucks around.
724         */
725
726         for (i = pan_adjustments.begin(), n = 0; i != pan_adjustments.end(); ++i, ++n) {
727
728                 if (only_if_aplay) {
729                         boost::shared_ptr<AutomationList> alist (_panner->streampanner(n).pan_control()->alist());
730
731                         if (!alist->automation_playback()) {
732                                 continue;
733                         }
734                 }
735
736                 AngularVector model = _panner->streampanner(n).get_effective_position();
737                 double fract = (*i)->get_value();
738                 AngularVector view (BaseStereoPanner::lr_fract_to_azimuth (fract), 0.0);
739
740                 if (!Panner::equivalent (model, view)) {
741                         (*i)->set_value (BaseStereoPanner::azimuth_to_lr_fract (model.azi));
742                 }
743         }
744
745         in_pan_update = false;
746 }
747
748 void
749 PannerUI::update_pan_sensitive ()
750 {
751         bool const sensitive = !(_panner->mono()) && !(_panner->automation_state() & Play);
752
753         switch (_panner->nouts()) {
754         case 0:
755         case 1:
756                 break;
757         case 2:
758                 for (vector<PannerBar*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
759                         (*i)->set_sensitive (sensitive);
760                 }
761                 break;
762         default:
763                 if (twod_panner) {
764                         twod_panner->set_sensitive (sensitive);
765                 }
766                 if (big_window) {
767                         big_window->set_sensitive (sensitive);
768                 }
769                 break;
770         }
771 }
772
773 gint
774 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
775 {
776         using namespace Menu_Helpers;
777
778         if (ev->type == GDK_BUTTON_RELEASE) {
779                 return TRUE;
780         }
781
782         switch (ev->button) {
783         case 1:
784                 if (pan_astate_menu == 0) {
785                         build_astate_menu ();
786                 }
787                 pan_astate_menu->popup (1, ev->time);
788                 break;
789         default:
790                 break;
791         }
792
793         return TRUE;
794 }
795
796 gint
797 PannerUI::pan_automation_style_button_event (GdkEventButton *ev)
798 {
799         if (ev->type == GDK_BUTTON_RELEASE) {
800                 return TRUE;
801         }
802
803         switch (ev->button) {
804         case 1:
805                 if (pan_astyle_menu == 0) {
806                         build_astyle_menu ();
807                 }
808                 pan_astyle_menu->popup (1, ev->time);
809                 break;
810         default:
811                 break;
812         }
813         return TRUE;
814 }
815
816 void
817 PannerUI::pan_automation_style_changed ()
818 {
819         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_style_changed)
820
821         switch (_width) {
822         case Wide:
823                 pan_automation_style_button.set_label (astyle_string(_panner->automation_style()));
824                 break;
825         case Narrow:
826                 pan_automation_style_button.set_label (short_astyle_string(_panner->automation_style()));
827                 break;
828         }
829 }
830
831 void
832 PannerUI::pan_automation_state_changed ()
833 {
834         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_state_changed)
835
836         bool x;
837
838         switch (_width) {
839         case Wide:
840           pan_automation_state_button.set_label (astate_string(_panner->automation_state()));
841                 break;
842         case Narrow:
843           pan_automation_state_button.set_label (short_astate_string(_panner->automation_state()));
844                 break;
845         }
846
847         /* when creating a new session, we get to create busses (and
848            sometimes tracks) with no outputs by the time they get
849            here.
850         */
851
852         if (_panner->empty()) {
853                 return;
854         }
855
856         x = (_panner->streampanner(0).pan_control()->alist()->automation_state() != Off);
857
858         if (pan_automation_state_button.get_active() != x) {
859         ignore_toggle = true;
860                 pan_automation_state_button.set_active (x);
861                 ignore_toggle = false;
862         }
863
864         update_pan_sensitive ();
865
866         /* start watching automation so that things move */
867
868         pan_watching.disconnect();
869
870         if (x) {
871                 pan_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &PannerUI::effective_pan_display));
872         }
873 }
874
875 string
876 PannerUI::astate_string (AutoState state)
877 {
878         return _astate_string (state, false);
879 }
880
881 string
882 PannerUI::short_astate_string (AutoState state)
883 {
884         return _astate_string (state, true);
885 }
886
887 string
888 PannerUI::_astate_string (AutoState state, bool shrt)
889 {
890         string sstr;
891
892         switch (state) {
893         case Off:
894                 sstr = (shrt ? "M" : _("M"));
895                 break;
896         case Play:
897                 sstr = (shrt ? "P" : _("P"));
898                 break;
899         case Touch:
900                 sstr = (shrt ? "T" : _("T"));
901                 break;
902         case Write:
903                 sstr = (shrt ? "W" : _("W"));
904                 break;
905         }
906
907         return sstr;
908 }
909
910 string
911 PannerUI::astyle_string (AutoStyle style)
912 {
913         return _astyle_string (style, false);
914 }
915
916 string
917 PannerUI::short_astyle_string (AutoStyle style)
918 {
919         return _astyle_string (style, true);
920 }
921
922 string
923 PannerUI::_astyle_string (AutoStyle style, bool shrt)
924 {
925         if (style & Trim) {
926                 return _("Trim");
927         } else {
928                 /* XXX it might different in different languages */
929
930                 return (shrt ? _("Abs") : _("Abs"));
931         }
932 }
933
934 void
935 PannerUI::set_mono (bool yn)
936 {
937         _panner->set_mono (yn);
938         update_pan_sensitive ();
939 }
940
941
942 void
943 PannerUI::connect_to_pan_control (uint32_t i)
944 {
945         _panner->pan_control(i)->Changed.connect (
946                 _pan_control_connections, invalidator (*this), boost::bind (&PannerUI::pan_value_changed, this, i), gui_context ()
947                 );
948 }
949
950 void
951 PannerUI::bar_spinner_activate (bool a)
952 {
953         _bar_spinner_active = a;
954 }
955
956 void
957 PannerUI::show_width ()
958 {
959 }
960
961 void
962 PannerUI::width_adjusted ()
963 {
964 }
965
966 void
967 PannerUI::show_position ()
968 {
969 }
970
971 void
972 PannerUI::position_adjusted ()
973 {
974 }