Fly my pretties!
[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   $Id$
19 */
20
21 #include <limits.h>
22
23 #include <ardour/io.h>
24 #include <ardour/dB.h>
25 #include <gtkmm2ext/utils.h>
26 #include <gtkmm2ext/stop_signal.h>
27 #include <gtkmm2ext/barcontroller.h>
28 #include <midi++/manager.h>
29 #include <pbd/fastlog.h>
30
31 #include "ardour_ui.h"
32 #include "panner_ui.h"
33 #include "panner2d.h"
34 #include "utils.h"
35 #include "gui_thread.h"
36
37 #include <ardour/session.h>
38 #include <ardour/route.h>
39 #include <ardour/panner.h>
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace Gtkmm2ext;
45 using namespace Gtk;
46 using namespace sigc;
47
48 /* XPM */
49 static const gchar * forwdblarrow_xpm[] = {
50 "20 11 3 1",
51 "       c None",
52 ".      c #000000",
53 "+      c #FFFFFF",
54 "              ..    ",
55 "              .+.   ",
56 "              .++.  ",
57 " ..............+++. ",
58 " .+++++++++++++++++.",
59 " ...................",
60 " .+++++++++++++++++.",
61 " ..............+++. ",
62 "              .++.  ",
63 "              .+.   ",
64 "              ..    "};
65
66 /* XPM */
67 static const gchar * revdblarrow_xpm[] = {
68 "20 11 3 1",
69 "       c None",
70 ".      c #000000",
71 "+      c #FFFFFF",
72 "              ..    ",
73 "              .+.   ",
74 "              .++.  ",
75 " ..............+++. ",
76 " .+++++++++++++++++.",
77 " ...................",
78 " .+++++++++++++++++.",
79 "  .+++..............",
80 "   .++.             ",
81 "    .+.             ",
82 "     ..             "};
83
84
85 PannerUI::PannerUI (IO& io, Session& s)
86         : _io (io),
87           _session (s),
88           panning_up_arrow (GTK_ARROW_UP, Gtk::SHADOW_OUT),
89           panning_down_arrow (GTK_ARROW_DOWN, Gtk::SHADOW_OUT),
90           panning_link_button (_("link"))
91         
92 {
93         ignore_toggle = false;
94         pan_menu = 0;
95         in_pan_update = false;
96
97         pan_bar_packer.set_size_request (-1, 61);
98         panning_viewport.set_size_request (61, 61);
99
100         panning_viewport.set_name (X_("BaseFrame"));
101
102         ARDOUR_UI::instance()->tooltips().set_tip (panning_link_button,
103                                                    _("panning link control"));
104         ARDOUR_UI::instance()->tooltips().set_tip (panning_link_direction_button,
105                                                    _("panning link direction"));
106
107         panning_link_box.pack_start (panning_link_button, true, true);
108         panning_link_box.pack_start (panning_link_direction_button, true, true);
109
110         panning_link_button.set_name (X_("PanningLinkButton"));
111         panning_link_direction_button.set_name (X_("PanningLinkDirectionButton"));
112
113         /* the pixmap will be reset at some point, but the key thing is that
114            we need a pixmap in the button just to get started.
115         */
116
117         panning_link_direction_button.add (*(manage (new Pixmap (forwdblarrow_xpm))));
118
119         panning_link_direction_button.clicked.connect
120                 (slot (*this, &PannerUI::panning_link_direction_clicked));
121
122         panning_link_button.button_press_event.connect
123                 (slot (*this, &PannerUI::panning_link_button_press));
124         panning_link_button.button_release_event.connect
125                 (slot (*this, &PannerUI::panning_link_button_release));
126
127         panning_up.set_border_width (3);
128         panning_down.set_border_width (3);
129         panning_up.add (panning_up_arrow);
130         panning_down.add (panning_down_arrow);
131         panning_up.set_name (X_("PanScrollerBase"));
132         panning_down.set_name (X_("PanScrollerBase"));
133         panning_up_arrow.set_name (X_("PanScrollerArrow"));
134         panning_down_arrow.set_name (X_("PanScrollerArrow"));
135
136         pan_vbox.set_spacing (4);
137         pan_vbox.pack_start (panning_viewport, false, false);
138         pan_vbox.pack_start (panning_link_box, false, false);
139
140         pack_start (pan_vbox, true, false);
141         pan_vbox.show_all ();
142         show ();
143
144         panner = 0;
145
146         _io.panner().Changed.connect (slot (*this, &PannerUI::panner_changed));
147         _io.panner().LinkStateChanged.connect (slot (*this, &PannerUI::update_pan_linkage));
148         _io.panner().StateChanged.connect (slot (*this, &PannerUI::update_pan_state));
149
150         pan_changed (0);
151         update_pan_sensitive ();
152         update_pan_linkage ();
153 }
154
155 gint
156 PannerUI::panning_link_button_press (GdkEventButton* ev)
157 {
158         return stop_signal (panning_link_button, "button-press-event");
159 }
160
161 gint
162 PannerUI::panning_link_button_release (GdkEventButton* ev)
163 {
164         if (!ignore_toggle) {
165                 _io.panner().set_linked (!_io.panner().linked());
166         }
167         return TRUE;
168 }
169
170 void
171 PannerUI::panning_link_direction_clicked()
172 {
173         switch (_io.panner().link_direction()) {
174         case Panner::SameDirection:
175                 _io.panner().set_link_direction (Panner::OppositeDirection);
176                 break;
177         default:
178                 _io.panner().set_link_direction (Panner::SameDirection);
179                 break;
180         }
181 }
182
183 void
184 PannerUI::update_pan_linkage ()
185 {
186         ENSURE_GUI_THREAD(slot (*this, &PannerUI::update_pan_linkage));
187         
188         bool x = _io.panner().linked();
189         bool bx = panning_link_button.get_active();
190         
191         if (x != bx) {
192                 
193                 ignore_toggle = true;
194                 panning_link_button.set_active (x);
195                 ignore_toggle = false;
196         }
197
198         panning_link_direction_button.set_sensitive (x);
199
200         switch (_io.panner().link_direction()) {
201         case Panner::SameDirection:
202                 static_cast<Pixmap*> (panning_link_direction_button.get_child())->set (forwdblarrow_xpm);
203                 break;
204         default:
205                 static_cast<Pixmap*> (panning_link_direction_button.get_child())->set (revdblarrow_xpm);
206                 break;
207         }
208 }
209
210 void
211 PannerUI::set_width (Width w)
212 {
213         switch (w) {
214         case Wide:
215                 panning_viewport.set_size_request (61, 61);
216                 if (panner) {
217                         panner->set_size_request (61, 61);
218                 }
219                 for (vector<BarController*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
220                                 (*i)->set_size_request (61, 15);
221                 }
222                 static_cast<Gtk::Label*> (panning_link_button.get_child())->set_text (_("link"));
223                 break;
224         case Narrow:
225                 panning_viewport.set_size_request (31, 61);
226                 if (panner) {
227                         panner->set_size_request (31, 61);
228                 }
229                 for (vector<BarController*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
230                                 (*i)->set_size_request (31, 15);
231                 }
232                 static_cast<Gtk::Label*> (panning_link_button.get_child())->set_text (_("L"));
233                 break;
234         }
235
236         _width = w;
237 }
238
239
240 PannerUI::~PannerUI ()
241 {
242         for (vector<Adjustment*>::iterator i = pan_adjustments.begin(); i != pan_adjustments.end(); ++i) {
243                 delete (*i);
244         }
245         
246         for (vector<BarController*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
247                 delete (*i);
248         }
249
250         if (panner) {
251                 delete panner;
252         }
253
254         if (pan_menu) {
255                 delete pan_menu;
256         }
257
258 }
259
260
261 gint
262 PannerUI::entry_focus_event (GdkEventFocus* ev)
263 {
264         if (ev->in) {
265                 ARDOUR_UI::instance()->allow_focus (true);
266         } else {
267                 ARDOUR_UI::instance()->allow_focus (false);
268         }
269         return TRUE;
270 }
271
272 void
273 PannerUI::panner_changed ()
274 {
275         ENSURE_GUI_THREAD (slot (*this, &PannerUI::panner_changed));
276         setup_pan ();
277 }
278
279 void
280 PannerUI::update_pan_state ()
281 {
282         /* currently nothing to do */
283         // ENSURE_GUI_THREAD (slot (*this, &PannerUI::update_panner_state));
284 }
285
286 void
287 PannerUI::setup_pan ()
288 {
289         uint32_t nouts = _io.n_outputs ();
290
291         if (nouts == 0 || nouts == 1) {
292
293                 while (!pan_adjustments.empty()) {
294                         delete pan_bars.back();
295                         pan_bars.pop_back ();
296                         delete pan_adjustments.back();
297                         pan_adjustments.pop_back ();
298                 }
299
300         } else if (nouts == 2) {
301
302                 vector<Adjustment*>::size_type asz;
303                 uint32_t npans = _io.panner().size();
304
305                 while (!pan_adjustments.empty()) {
306                         delete pan_bars.back();
307                         pan_bars.pop_back ();
308                         delete pan_adjustments.back();
309                         pan_adjustments.pop_back ();
310                 }
311
312                 while ((asz = pan_adjustments.size()) < npans) {
313
314                         float x;
315                         BarController* bc;
316
317                         /* initialize adjustment with current value of panner */
318
319                         _io.panner()[asz]->get_position (x);
320
321                         pan_adjustments.push_back (new Adjustment (x, 0, 1.0, 0.05, 0.1));
322                         pan_adjustments.back()->value_changed.connect (bind (slot (*this, &PannerUI::pan_adjustment_changed), (uint32_t) asz));
323
324                         _io.panner()[asz]->Changed.connect (bind (slot (*this, &PannerUI::pan_value_changed), (uint32_t) asz));
325
326                         bc = new BarController (*pan_adjustments[asz], 
327                                                 &_io.panner()[asz]->midi_control(),
328                                                 bind (slot (*this, &PannerUI::pan_printer), pan_adjustments[asz]));
329                         
330                         if (_session.midi_port()) {
331                                 _io.panner()[asz]->reset_midi_control (_session.midi_port(), true);
332                         }
333                         
334                         bc->set_name ("PanSlider");
335                         bc->set_shadow_type (GTK_SHADOW_NONE);
336                         bc->set_style (BarController::Line);
337                         bc->get_spin_button().signal_focus_in_event().connect (slot (*this, &PannerUI::entry_focus_event));
338                         bc->get_spin_button().signal_focus_out_event().connect (slot (*this, &PannerUI::entry_focus_event));
339
340                         bc->StartGesture.connect (bind (slot (_io, &IO::start_pan_touch), (uint32_t) asz));
341                         bc->StopGesture.connect (bind (slot (_io, &IO::end_pan_touch), (uint32_t) asz));
342
343                         char buf[64];
344                         snprintf (buf, sizeof (buf), _("panner for channel %u"), asz + 1);
345                         ARDOUR_UI::instance()->tooltips().set_tip (bc->event_widget(), buf);
346
347                         bc->event_widget().button_release_event.connect
348                                 (bind (slot (*this, &PannerUI::pan_button_event), (uint32_t) asz));
349
350                         pan_bars.push_back (bc);
351                         switch (_width) {
352                         case Wide:
353                                 pan_bars.back()->set_size_request (61, 15);
354                                 break;
355                         case Narrow:
356                                 pan_bars.back()->set_size_request (31, 15);
357                                 break;
358                         }
359
360                         pan_bar_packer.pack_start (*pan_bars.back(), false, false);
361                 }
362
363                 /* now that we actually have the pan bars,
364                    set their sensitivity based on current
365                    automation state.
366                 */
367
368                 update_pan_sensitive ();
369
370                 panning_viewport.remove ();
371                 panning_viewport.add (pan_bar_packer);
372                 panning_viewport.show_all ();
373
374         } else {
375
376                 int w = 0;
377
378                 switch (_width) {
379                 case Wide:
380                         w = 61;
381                         break;
382                 case Narrow:
383                         w = 31;
384                         break;
385                 }
386
387                 if (panner == 0) {
388                         panner = new Panner2d (_io.panner(), w, 61);
389                         panner->set_name ("MixerPanZone");
390                         panner->show ();
391                 }
392                 
393                 update_pan_sensitive ();
394                 panner->reset (_io.n_inputs());
395                 panner->set_size_request (w, 61);
396
397                 /* and finally, add it to the panner frame */
398
399                 panning_viewport.remove ();
400                 panning_viewport.add (*panner);
401                 panning_viewport.show_all ();
402         }
403 }
404
405 gint
406 PannerUI::pan_button_event (GdkEventButton* ev, uint32_t which)
407 {
408         switch (ev->button) {
409         case 3:
410                 if (pan_menu == 0) {
411                         pan_menu = manage (new Menu);
412                         pan_menu->set_name ("ArdourContextMenu");
413                 }
414                 build_pan_menu (which);
415                 pan_menu->popup (1, ev->time);
416                 return TRUE;
417                 break;
418         default:
419                 return FALSE;
420         }
421
422         return FALSE; // what's wrong with gcc?
423 }
424
425 void
426 PannerUI::build_pan_menu (uint32_t which)
427 {
428         using namespace Menu_Helpers;
429         MenuList& items (pan_menu->items());
430
431         items.clear ();
432
433         items.push_back (CheckMenuElem (_("Mute")));
434         
435         /* set state first, connect second */
436
437         (dynamic_cast<CheckMenuItem*> (items.back()))->set_active (_io.panner()[which]->muted());
438         (dynamic_cast<CheckMenuItem*> (items.back()))->toggled.connect
439                 (bind (slot (*this, &PannerUI::pan_mute), which));
440
441         items.push_back (CheckMenuElem (_("Bypass"), slot (*this, &PannerUI::pan_bypass_toggle)));
442         bypass_menu_item = static_cast<CheckMenuItem*> (items.back());
443
444         /* set state first, connect second */
445
446         bypass_menu_item->set_active (_io.panner().bypassed());
447         bypass_menu_item->toggled.connect (slot (*this, &PannerUI::pan_bypass_toggle));
448
449         items.push_back (MenuElem (_("Reset"), slot (*this, &PannerUI::pan_reset)));
450         items.push_back (SeparatorElem());
451         items.push_back (MenuElem (_("Reset all")));
452 }
453
454 void
455 PannerUI::pan_mute (uint32_t which)
456 {
457         StreamPanner* sp = _io.panner()[which];
458         sp->set_muted (!sp->muted());
459 }
460
461 void
462 PannerUI::pan_bypass_toggle ()
463 {
464         if (bypass_menu_item && (_io.panner().bypassed() != bypass_menu_item->get_active())) {
465                 _io.panner().set_bypassed (!_io.panner().bypassed());
466         }
467 }
468
469 void
470 PannerUI::pan_reset ()
471 {
472 }
473
474 void
475 PannerUI::effective_pan_display ()
476 {
477         if (_io.panner().empty()) {
478                 return;
479         }
480
481         switch (_io.n_outputs()) {
482         case 0: 
483         case 1:
484                 /* relax */
485                 break;
486
487         case 2:
488                 update_pan_bars (true);
489                 break;
490
491         default:
492                 //panner->move_puck (pan_value (v, right), 0.5);
493                 break;
494         }
495 }
496
497 void
498 PannerUI::pan_changed (void *src)
499 {
500         if (src == this) {
501                 return;
502         }
503
504         switch (_io.panner().size()) {
505         case 0:
506                 panning_link_box.set_sensitive (false);
507                 return;
508         case 1:
509                 panning_link_box.set_sensitive (false);
510                 break;
511         default:
512                 panning_link_box.set_sensitive (true);
513         }
514
515         uint32_t nouts = _io.n_outputs();
516
517         switch (nouts) {
518         case 0:
519         case 1:
520                 /* relax */
521                 break;
522
523         case 2:
524                 update_pan_bars (false);
525                 break;
526
527         default:
528                 // panner->move_puck (pan_value (pans[0], pans[1]), 0.5);
529                 break;
530         }
531 }
532
533 void
534 PannerUI::pan_adjustment_changed (uint32_t which)
535 {
536         if (!in_pan_update && which < _io.panner().size()) {
537
538                 float xpos;
539                 float val = pan_adjustments[which]->get_value ();
540                 _io.panner()[which]->get_position (xpos);
541
542                 /* add a kinda-sorta detent for the middle */
543                 
544                 if (val != 0.5 && Panner::equivalent (val, 0.5)) {
545                         /* this is going to be reentrant, so just 
546                            return after it.
547                         */
548
549                         in_pan_update = true;
550                         pan_adjustments[which]->set_value (0.5);
551                         in_pan_update = false;
552                         return;
553                 }
554                 
555                 if (!Panner::equivalent (val, xpos)) {
556
557                         _io.panner()[which]->set_position (val);
558                         /* XXX 
559                            the panner objects have no access to the session,
560                            so do this here. ick.
561                         */
562                         _session.set_dirty();
563                 }
564         }
565 }
566
567 void
568 PannerUI::pan_value_changed (uint32_t which)
569 {
570         ENSURE_GUI_THREAD (bind (slot (*this, &PannerUI::pan_value_changed), which));
571                                                            
572         if (which < _io.panner().size()) {
573                 float xpos;
574                 float val = pan_adjustments[which]->get_value ();
575
576                 _io.panner()[which]->get_position (xpos);
577
578                 if (!Panner::equivalent (val, xpos)) {
579                         in_pan_update = true;
580                         pan_adjustments[which]->set_value (xpos);
581                         in_pan_update = false;
582                 }
583         }
584 }       
585
586 void
587 PannerUI::update_pan_bars (bool only_if_aplay)
588 {
589         uint32_t n;
590         vector<Adjustment*>::iterator i;
591
592         in_pan_update = true;
593
594         /* this runs during automation playback, and moves the bar controllers
595            and/or pucks around.
596         */
597
598         for (i = pan_adjustments.begin(), n = 0; i != pan_adjustments.end(); ++i, ++n) {
599                 float xpos, val;
600
601                 if (only_if_aplay) {
602                         AutomationList& alist (_io.panner()[n]->automation());
603                         
604                         if (!alist.automation_playback()) {
605                                 continue;
606                         }
607                 }
608
609                 _io.panner()[n]->get_effective_position (xpos);
610                 val = (*i)->get_value ();
611                 
612                 if (!Panner::equivalent (val, xpos)) {
613                         (*i)->set_value (xpos);
614                 }
615         }
616
617         in_pan_update = false;
618 }
619
620 void
621 PannerUI::pan_printer (char *buf, uint32_t len, Adjustment* adj)
622 {
623         float val = adj->get_value();
624
625         if (val == 0.0f) {
626                 snprintf (buf, len, X_("L"));
627         } else if (val == 1.0f) {
628                 snprintf (buf, len, X_("R"));
629         } else if (Panner::equivalent (val, 0.5f)) {
630                 snprintf (buf, len, X_("C"));
631         } else {
632                 /* don't print anything */
633                 buf[0] = '\0';
634         }
635 }
636
637 void
638 PannerUI::update_pan_sensitive ()
639 {
640         bool sensitive = !(_io.panner().automation_state() & Play);
641
642         switch (_io.n_outputs()) {
643         case 0:
644         case 1:
645                 break;
646         case 2:
647                 for (vector<BarController*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
648                         (*i)->set_sensitive (sensitive);
649                 }
650                 break;
651         default:
652                 if (panner) {
653                         panner->set_sensitive (sensitive);
654                 }
655                 break;
656         }
657 }
658