first basic pass at a single widget 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         if (nouts == 0 || nouts == 1) {
387
388                 while (!pan_adjustments.empty()) {
389                         delete pan_bars.back();
390                         pan_bars.pop_back ();
391                         delete pan_adjustments.back();
392                         pan_adjustments.pop_back ();
393                 }
394
395                 delete twod_panner;
396                 twod_panner = 0;
397
398                 /* stick something into the panning viewport so that it redraws */
399
400                 EventBox* eb = manage (new EventBox());
401                 panning_viewport.remove ();
402                 panning_viewport.add (*eb);
403                 panning_viewport.show_all ();
404
405         } else if (nouts == 2) {
406
407                 vector<Adjustment*>::size_type asz;
408
409                 while (!pan_adjustments.empty()) {
410                         delete pan_bars.back();
411                         pan_bars.pop_back ();
412                         delete pan_adjustments.back();
413                         pan_adjustments.pop_back ();
414                 }
415
416                 delete twod_panner;
417                 twod_panner = 0;
418
419                 while ((asz = pan_adjustments.size()) < npans) {
420
421                         float x, rx;
422                         PannerBar* bc;
423
424                         /* initialize adjustment with 0.0 (L) or 1.0 (R) for the first and second panners,
425                            which serves as a default, otherwise use current value */
426
427                         rx = _panner->pan_control( asz)->get_value();
428
429                         if (npans == 1) {
430                                 x = 0.5;
431                         } else if (asz == 0) {
432                                 x = 0.0;
433                         } else if (asz == 1) {
434                                 x = 1.0;
435                         } else {
436                                 x = rx;
437                         }
438
439                         pan_adjustments.push_back (new Adjustment (x, 0, 1.0, 0.005, 0.05));
440                         bc = new PannerBar (*pan_adjustments[asz],
441                                 boost::static_pointer_cast<PBD::Controllable>( _panner->pan_control( asz )) );
442
443                         /* now set adjustment with current value of panner, then connect the signals */
444                         pan_adjustments.back()->set_value(rx);
445                         pan_adjustments.back()->signal_value_changed().connect (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_adjustment_changed), (uint32_t) asz));
446                         connect_to_pan_control (asz);
447
448                         bc->set_name ("PanSlider");
449                         bc->set_shadow_type (Gtk::SHADOW_NONE);
450
451                         boost::shared_ptr<AutomationControl> ac = _panner->pan_control (asz);
452
453                         if (asz) {
454                                 bc->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch), 
455                                                                       boost::weak_ptr<AutomationControl> (ac)));
456                                 bc->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch), 
457                                                                      boost::weak_ptr<AutomationControl>(ac)));
458                         }
459
460                         char buf[64];
461                         snprintf (buf, sizeof (buf), _("panner for channel %zu"), asz + 1);
462                         ARDOUR_UI::instance()->set_tip (bc->event_widget(), buf);
463
464                         bc->event_widget().signal_button_release_event().connect
465                                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) asz));
466
467                         bc->set_size_request (-1, pan_bar_height);
468                         bc->SpinnerActive.connect (sigc::mem_fun (*this, &PannerUI::bar_spinner_activate));
469
470                         pan_bars.push_back (bc);
471                         pan_bar_packer.pack_start (*bc, false, false);
472                 }
473
474                 /* now that we actually have the pan bars,
475                    set their sensitivity based on current
476                    automation state.
477                 */
478
479                 update_pan_sensitive ();
480
481                 panning_viewport.remove ();
482                 panning_viewport.add (pan_bar_packer);
483                 panning_viewport.show_all ();
484
485                 if (npans == 2) {
486                         /* add position and width controls */
487                         if (_stereo_panner == 0) {
488                                 _stereo_panner = new StereoPanner (_panner->direction_control(), 
489                                                                    _panner->width_control());
490                                 poswidth_box.pack_start (*_stereo_panner, true, true);
491                         }
492                         pan_vbox.pack_start (poswidth_box, false, false);
493                         poswidth_box.show_all ();
494                         cerr << "Packed poswidth and mde it visible\n";
495                 } else {
496                         if (_stereo_panner) {
497                                 pan_vbox.remove (poswidth_box);
498                                 cerr << "Hid poswidth\n";
499                         }
500                 }
501
502         } else {
503
504                 if (!twod_panner) {
505                         twod_panner = new Panner2d (_panner, 61);
506                         twod_panner->set_name ("MixerPanZone");
507                         twod_panner->show ();
508
509                         twod_panner->signal_button_press_event().connect
510                                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) 0), false);
511                 }
512
513                 update_pan_sensitive ();
514                 twod_panner->reset (npans);
515                 if (big_window) {
516                         big_window->reset (npans);
517                 }
518                 twod_panner->set_size_request (-1, 61);
519
520                 /* and finally, add it to the panner frame */
521
522                 panning_viewport.remove ();
523                 panning_viewport.add (*twod_panner);
524                 panning_viewport.show_all ();
525         }
526 }
527
528 void
529 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
530 {
531         boost::shared_ptr<AutomationControl> ac = wac.lock();
532         if (!ac) {
533                 return;
534         }
535         ac->start_touch (ac->session().transport_frame());
536 }
537
538 void
539 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
540 {
541         boost::shared_ptr<AutomationControl> ac = wac.lock();
542         if (!ac) {
543                 return;
544         }
545         ac->stop_touch (false, ac->session().transport_frame());
546 }
547
548 bool
549 PannerUI::pan_button_event (GdkEventButton* ev, uint32_t which)
550 {
551         switch (ev->button) {
552         case 1:
553                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
554                         if (!big_window) {
555                                 big_window = new Panner2dWindow (_panner, 400, _panner->npanners());
556                         }
557                         big_window->show ();
558                         return true;
559                 }
560                 break;
561
562         case 3:
563                 if (pan_menu == 0) {
564                         pan_menu = manage (new Menu);
565                         pan_menu->set_name ("ArdourContextMenu");
566                 }
567                 build_pan_menu (which);
568                 pan_menu->popup (1, ev->time);
569                 return true;
570                 break;
571         default:
572                 return false;
573         }
574
575         return false; // what's wrong with gcc?
576 }
577
578 void
579 PannerUI::build_pan_menu (uint32_t which)
580 {
581         using namespace Menu_Helpers;
582         MenuList& items (pan_menu->items());
583
584         items.clear ();
585
586         items.push_back (CheckMenuElem (_("Mute")));
587
588         /* set state first, connect second */
589
590         (dynamic_cast<CheckMenuItem*> (&items.back()))->set_active (_panner->streampanner(which).muted());
591         (dynamic_cast<CheckMenuItem*> (&items.back()))->signal_toggled().connect
592                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_mute), which));
593
594         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
595         bypass_menu_item = static_cast<CheckMenuItem*> (&items.back());
596
597         /* set state first, connect second */
598
599         bypass_menu_item->set_active (_panner->bypassed());
600         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
601
602         items.push_back (MenuElem (_("Reset"), sigc::bind (sigc::mem_fun (*this, &PannerUI::pan_reset), which)));
603         items.push_back (SeparatorElem());
604         items.push_back (MenuElem (_("Reset all"), sigc::mem_fun (*this, &PannerUI::pan_reset_all)));
605 }
606
607 void
608 PannerUI::pan_mute (uint32_t which)
609 {
610         StreamPanner& sp = _panner->streampanner(which);
611         sp.set_muted (!sp.muted());
612 }
613
614 void
615 PannerUI::pan_bypass_toggle ()
616 {
617         if (bypass_menu_item && (_panner->bypassed() != bypass_menu_item->get_active())) {
618                 _panner->set_bypassed (!_panner->bypassed());
619         }
620 }
621
622 void
623 PannerUI::pan_reset (uint32_t which)
624 {
625         _panner->reset_streampanner (which);
626 }
627
628 void
629 PannerUI::pan_reset_all ()
630 {
631         _panner->reset_to_default ();
632 }
633
634 void
635 PannerUI::effective_pan_display ()
636 {
637         if (_panner->empty()) {
638                 return;
639         }
640
641         switch (_panner->nouts()) {
642         case 0:
643         case 1:
644                 /* relax */
645                 break;
646
647         case 2:
648                 update_pan_bars (true);
649                 break;
650
651         default:
652                 //panner->move_puck (pan_value (v, right), 0.5);
653                 break;
654         }
655 }
656
657 void
658 PannerUI::pan_adjustment_changed (uint32_t which)
659 {
660         if (!in_pan_update && which < _panner->npanners()) {
661
662                 float val = pan_adjustments[which]->get_value ();
663                 float const xpos = _panner->pan_control(which)->get_value();
664
665                 /* add a kinda-sorta detent for the middle */
666
667                 if (val != 0.5 && Panner::equivalent (val, 0.5)) {
668                         /* this is going to be reentrant, so just
669                            return after it.
670                         */
671
672                         in_pan_update = true;
673                         pan_adjustments[which]->set_value (0.5);
674                         in_pan_update = false;
675                         return;
676                 }
677
678                 if (!Panner::equivalent (val, xpos)) {
679
680                         _panner->pan_control(which)->set_value (val);
681                         /* XXX
682                            the panner objects have no access to the session,
683                            so do this here. ick.
684                         */
685                         _session->set_dirty();
686                 }
687         }
688 }
689
690 void
691 PannerUI::pan_value_changed (uint32_t which)
692 {
693         ENSURE_GUI_THREAD (*this, &PannerUI::pan_value_changed, which)
694
695         if (twod_panner) {
696
697                 in_pan_update = true;
698                 twod_panner->move_puck (which, _panner->streampanner(which).get_position());
699                 in_pan_update = false;
700
701         } else if (_panner->npanners() > 0 && which < _panner->npanners()) {
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 }