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