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