make position fader a "line" style rather than bar
[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                                 _position_fader->set_style (BarController::Line);
496                                 ARDOUR_UI::instance()->set_tip (_position_fader, _("Pan Position"));                                
497                                 _position_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &PannerUI::position_adjusted));
498                                 _panner->direction_control()->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::show_position, this), gui_context());
499                                 show_position();
500
501                                 _width_fader = new BarController (_width_adjustment, _panner->width_control());
502                                 _width_fader->set_size_request (-1, pan_bar_height/2);
503                                 _width_fader->set_name ("PanSlider");
504                                 _width_fader->set_style (BarController::CenterOut);
505                                 ARDOUR_UI::instance()->set_tip (_width_fader, _("Stereo Image Width"));                                
506                                 _width_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &PannerUI::width_adjusted));
507                                  _panner->width_control()->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::show_width, this), gui_context());
508                                 show_width();
509                                 
510                                 poswidth_box.pack_start (*_position_fader, true, true);
511                                 poswidth_box.pack_start (*_width_fader, true, true);
512                         }
513                         pan_vbox.pack_start (poswidth_box, false, false);
514                         poswidth_box.show_all ();
515                         cerr << "Packed poswidth and mde it visible\n";
516                 } else {
517                         if (_position_fader) {
518                                 pan_vbox.remove (poswidth_box);
519                                 cerr << "Hid poswidth\n";
520                         }
521                 }
522
523         } else {
524
525                 if (!twod_panner) {
526                         twod_panner = new Panner2d (_panner, 61);
527                         twod_panner->set_name ("MixerPanZone");
528                         twod_panner->show ();
529
530                         twod_panner->signal_button_press_event().connect
531                                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) 0), false);
532                 }
533
534                 update_pan_sensitive ();
535                 twod_panner->reset (npans);
536                 if (big_window) {
537                         big_window->reset (npans);
538                 }
539                 twod_panner->set_size_request (-1, 61);
540
541                 /* and finally, add it to the panner frame */
542
543                 panning_viewport.remove ();
544                 panning_viewport.add (*twod_panner);
545                 panning_viewport.show_all ();
546         }
547 }
548
549 void
550 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
551 {
552         boost::shared_ptr<AutomationControl> ac = wac.lock();
553         if (!ac) {
554                 return;
555         }
556         ac->start_touch (ac->session().transport_frame());
557 }
558
559 void
560 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
561 {
562         boost::shared_ptr<AutomationControl> ac = wac.lock();
563         if (!ac) {
564                 return;
565         }
566         ac->stop_touch (false, ac->session().transport_frame());
567 }
568
569 bool
570 PannerUI::pan_button_event (GdkEventButton* ev, uint32_t which)
571 {
572         switch (ev->button) {
573         case 1:
574                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
575                         if (!big_window) {
576                                 big_window = new Panner2dWindow (_panner, 400, _panner->npanners());
577                         }
578                         big_window->show ();
579                         return true;
580                 }
581                 break;
582
583         case 3:
584                 if (pan_menu == 0) {
585                         pan_menu = manage (new Menu);
586                         pan_menu->set_name ("ArdourContextMenu");
587                 }
588                 build_pan_menu (which);
589                 pan_menu->popup (1, ev->time);
590                 return true;
591                 break;
592         default:
593                 return false;
594         }
595
596         return false; // what's wrong with gcc?
597 }
598
599 void
600 PannerUI::build_pan_menu (uint32_t which)
601 {
602         using namespace Menu_Helpers;
603         MenuList& items (pan_menu->items());
604
605         items.clear ();
606
607         items.push_back (CheckMenuElem (_("Mute")));
608
609         /* set state first, connect second */
610
611         (dynamic_cast<CheckMenuItem*> (&items.back()))->set_active (_panner->streampanner(which).muted());
612         (dynamic_cast<CheckMenuItem*> (&items.back()))->signal_toggled().connect
613                 (sigc::bind (sigc::mem_fun(*this, &PannerUI::pan_mute), which));
614
615         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
616         bypass_menu_item = static_cast<CheckMenuItem*> (&items.back());
617
618         /* set state first, connect second */
619
620         bypass_menu_item->set_active (_panner->bypassed());
621         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
622
623         items.push_back (MenuElem (_("Reset"), sigc::bind (sigc::mem_fun (*this, &PannerUI::pan_reset), which)));
624         items.push_back (SeparatorElem());
625         items.push_back (MenuElem (_("Reset all"), sigc::mem_fun (*this, &PannerUI::pan_reset_all)));
626 }
627
628 void
629 PannerUI::pan_mute (uint32_t which)
630 {
631         StreamPanner& sp = _panner->streampanner(which);
632         sp.set_muted (!sp.muted());
633 }
634
635 void
636 PannerUI::pan_bypass_toggle ()
637 {
638         if (bypass_menu_item && (_panner->bypassed() != bypass_menu_item->get_active())) {
639                 _panner->set_bypassed (!_panner->bypassed());
640         }
641 }
642
643 void
644 PannerUI::pan_reset (uint32_t which)
645 {
646         _panner->reset_streampanner (which);
647 }
648
649 void
650 PannerUI::pan_reset_all ()
651 {
652         _panner->reset_to_default ();
653 }
654
655 void
656 PannerUI::effective_pan_display ()
657 {
658         if (_panner->empty()) {
659                 return;
660         }
661
662         switch (_panner->nouts()) {
663         case 0:
664         case 1:
665                 /* relax */
666                 break;
667
668         case 2:
669                 update_pan_bars (true);
670                 break;
671
672         default:
673                 //panner->move_puck (pan_value (v, right), 0.5);
674                 break;
675         }
676 }
677
678 void
679 PannerUI::pan_adjustment_changed (uint32_t which)
680 {
681         if (!in_pan_update && which < _panner->npanners()) {
682
683                 float val = pan_adjustments[which]->get_value ();
684                 float const xpos = _panner->pan_control(which)->get_value();
685
686                 /* add a kinda-sorta detent for the middle */
687
688                 if (val != 0.5 && Panner::equivalent (val, 0.5)) {
689                         /* this is going to be reentrant, so just
690                            return after it.
691                         */
692
693                         in_pan_update = true;
694                         pan_adjustments[which]->set_value (0.5);
695                         in_pan_update = false;
696                         return;
697                 }
698
699                 if (!Panner::equivalent (val, xpos)) {
700
701                         _panner->pan_control(which)->set_value (val);
702                         /* XXX
703                            the panner objects have no access to the session,
704                            so do this here. ick.
705                         */
706                         _session->set_dirty();
707                 }
708         }
709 }
710
711 void
712 PannerUI::pan_value_changed (uint32_t which)
713 {
714         ENSURE_GUI_THREAD (*this, &PannerUI::pan_value_changed, which)
715
716         if (twod_panner) {
717
718                 in_pan_update = true;
719                 twod_panner->move_puck (which, _panner->streampanner(which).get_position());
720                 in_pan_update = false;
721
722         } else if (_panner->npanners() > 0 && which < _panner->npanners()) {
723                 AngularVector model = _panner->streampanner(which).get_position();
724                 double fract = pan_adjustments[which]->get_value();
725                 AngularVector view (BaseStereoPanner::lr_fract_to_azimuth (fract), 0.0);
726
727                 if (!Panner::equivalent (model, view)) {
728                         in_pan_update = true;
729                         pan_adjustments[which]->set_value (BaseStereoPanner::azimuth_to_lr_fract (model.azi));
730                         in_pan_update = false;
731                 }
732         }
733 }
734
735 void
736 PannerUI::update_pan_bars (bool only_if_aplay)
737 {
738         uint32_t n;
739         vector<Adjustment*>::iterator i;
740
741         in_pan_update = true;
742
743         /* this runs during automation playback, and moves the bar controllers
744            and/or pucks around.
745         */
746
747         for (i = pan_adjustments.begin(), n = 0; i != pan_adjustments.end(); ++i, ++n) {
748
749                 if (only_if_aplay) {
750                         boost::shared_ptr<AutomationList> alist (_panner->streampanner(n).pan_control()->alist());
751
752                         if (!alist->automation_playback()) {
753                                 continue;
754                         }
755                 }
756
757                 AngularVector model = _panner->streampanner(n).get_effective_position();
758                 double fract = (*i)->get_value();
759                 AngularVector view (BaseStereoPanner::lr_fract_to_azimuth (fract), 0.0);
760
761                 if (!Panner::equivalent (model, view)) {
762                         (*i)->set_value (BaseStereoPanner::azimuth_to_lr_fract (model.azi));
763                 }
764         }
765
766         in_pan_update = false;
767 }
768
769 void
770 PannerUI::update_pan_sensitive ()
771 {
772         bool const sensitive = !(_panner->mono()) && !(_panner->automation_state() & Play);
773
774         switch (_panner->nouts()) {
775         case 0:
776         case 1:
777                 break;
778         case 2:
779                 for (vector<PannerBar*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
780                         (*i)->set_sensitive (sensitive);
781                 }
782                 break;
783         default:
784                 if (twod_panner) {
785                         twod_panner->set_sensitive (sensitive);
786                 }
787                 if (big_window) {
788                         big_window->set_sensitive (sensitive);
789                 }
790                 break;
791         }
792 }
793
794 gint
795 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
796 {
797         using namespace Menu_Helpers;
798
799         if (ev->type == GDK_BUTTON_RELEASE) {
800                 return TRUE;
801         }
802
803         switch (ev->button) {
804         case 1:
805                 if (pan_astate_menu == 0) {
806                         build_astate_menu ();
807                 }
808                 pan_astate_menu->popup (1, ev->time);
809                 break;
810         default:
811                 break;
812         }
813
814         return TRUE;
815 }
816
817 gint
818 PannerUI::pan_automation_style_button_event (GdkEventButton *ev)
819 {
820         if (ev->type == GDK_BUTTON_RELEASE) {
821                 return TRUE;
822         }
823
824         switch (ev->button) {
825         case 1:
826                 if (pan_astyle_menu == 0) {
827                         build_astyle_menu ();
828                 }
829                 pan_astyle_menu->popup (1, ev->time);
830                 break;
831         default:
832                 break;
833         }
834         return TRUE;
835 }
836
837 void
838 PannerUI::pan_automation_style_changed ()
839 {
840         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_style_changed)
841
842         switch (_width) {
843         case Wide:
844                 pan_automation_style_button.set_label (astyle_string(_panner->automation_style()));
845                 break;
846         case Narrow:
847                 pan_automation_style_button.set_label (short_astyle_string(_panner->automation_style()));
848                 break;
849         }
850 }
851
852 void
853 PannerUI::pan_automation_state_changed ()
854 {
855         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_state_changed)
856
857         bool x;
858
859         switch (_width) {
860         case Wide:
861           pan_automation_state_button.set_label (astate_string(_panner->automation_state()));
862                 break;
863         case Narrow:
864           pan_automation_state_button.set_label (short_astate_string(_panner->automation_state()));
865                 break;
866         }
867
868         /* when creating a new session, we get to create busses (and
869            sometimes tracks) with no outputs by the time they get
870            here.
871         */
872
873         if (_panner->empty()) {
874                 return;
875         }
876
877         x = (_panner->streampanner(0).pan_control()->alist()->automation_state() != Off);
878
879         if (pan_automation_state_button.get_active() != x) {
880         ignore_toggle = true;
881                 pan_automation_state_button.set_active (x);
882                 ignore_toggle = false;
883         }
884
885         update_pan_sensitive ();
886
887         /* start watching automation so that things move */
888
889         pan_watching.disconnect();
890
891         if (x) {
892                 pan_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &PannerUI::effective_pan_display));
893         }
894 }
895
896 string
897 PannerUI::astate_string (AutoState state)
898 {
899         return _astate_string (state, false);
900 }
901
902 string
903 PannerUI::short_astate_string (AutoState state)
904 {
905         return _astate_string (state, true);
906 }
907
908 string
909 PannerUI::_astate_string (AutoState state, bool shrt)
910 {
911         string sstr;
912
913         switch (state) {
914         case Off:
915                 sstr = (shrt ? "M" : _("M"));
916                 break;
917         case Play:
918                 sstr = (shrt ? "P" : _("P"));
919                 break;
920         case Touch:
921                 sstr = (shrt ? "T" : _("T"));
922                 break;
923         case Write:
924                 sstr = (shrt ? "W" : _("W"));
925                 break;
926         }
927
928         return sstr;
929 }
930
931 string
932 PannerUI::astyle_string (AutoStyle style)
933 {
934         return _astyle_string (style, false);
935 }
936
937 string
938 PannerUI::short_astyle_string (AutoStyle style)
939 {
940         return _astyle_string (style, true);
941 }
942
943 string
944 PannerUI::_astyle_string (AutoStyle style, bool shrt)
945 {
946         if (style & Trim) {
947                 return _("Trim");
948         } else {
949                 /* XXX it might different in different languages */
950
951                 return (shrt ? _("Abs") : _("Abs"));
952         }
953 }
954
955 void
956 PannerUI::set_mono (bool yn)
957 {
958         _panner->set_mono (yn);
959         update_pan_sensitive ();
960 }
961
962
963 void
964 PannerUI::connect_to_pan_control (uint32_t i)
965 {
966         _panner->pan_control(i)->Changed.connect (
967                 _pan_control_connections, invalidator (*this), boost::bind (&PannerUI::pan_value_changed, this, i), gui_context ()
968                 );
969 }
970
971 void
972 PannerUI::bar_spinner_activate (bool a)
973 {
974         _bar_spinner_active = a;
975 }
976
977 void
978 PannerUI::setup_slider_pix ()
979 {
980         _poswidth_slider = ::get_icon ("fader_belt_h_thin");
981         assert (_poswidth_slider);
982 }
983
984 void
985 PannerUI::show_width ()
986 {
987         float const value = _panner->width_control()->get_value ();
988         
989         if (_width_adjustment.get_value() != value) {
990                 _ignore_width_change = true;
991                 _width_adjustment.set_value (value);
992                 _ignore_width_change = false;
993         }
994 }
995
996 void
997 PannerUI::width_adjusted ()
998 {
999         if (_ignore_width_change) {
1000                 return;
1001         }
1002
1003         _panner->width_control()->set_value (_width_adjustment.get_value());
1004 }
1005
1006 void
1007 PannerUI::show_position ()
1008 {
1009         float const value = _panner->direction_control()->get_value ();
1010         
1011         if (_position_adjustment.get_value() != value) {
1012                 _ignore_position_change = true;
1013                 _position_adjustment.set_value (value);
1014                 _ignore_position_change = false;
1015         }
1016 }
1017
1018 void
1019 PannerUI::position_adjusted ()
1020 {
1021         if (_ignore_position_change) {
1022                 return;
1023         }
1024
1025         _panner->direction_control()->set_value (_position_adjustment.get_value());
1026 }