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