401d707b28b88242e044e190e06daec93314696f
[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                 static_cast<Gtk::Label*> (panning_link_button.get_child())->set_text (_("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                 static_cast<Gtk::Label*> (panning_link_button.get_child())->set_text (_("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                         snprintf (buf, sizeof (buf), _("panner for channel %u"), asz + 1);
297                         ARDOUR_UI::instance()->tooltips().set_tip (bc->event_widget(), buf);
298
299                         bc->event_widget().signal_button_release_event().connect
300                                 (bind (mem_fun(*this, &PannerUI::pan_button_event), (uint32_t) asz));
301
302                         pan_bars.push_back (bc);
303                         switch (_width) {
304                         case Wide:
305                                 pan_bars.back()->set_size_request (61, 15);
306                                 break;
307                         case Narrow:
308                                 pan_bars.back()->set_size_request (31, 15);
309                                 break;
310                         }
311
312                         pan_bar_packer.pack_start (*pan_bars.back(), false, false);
313                 }
314
315                 /* now that we actually have the pan bars,
316                    set their sensitivity based on current
317                    automation state.
318                 */
319
320                 update_pan_sensitive ();
321
322                 panning_viewport.remove ();
323                 panning_viewport.add (pan_bar_packer);
324                 panning_viewport.show_all ();
325
326         } else {
327
328                 int w = 0;
329
330                 switch (_width) {
331                 case Wide:
332                         w = 61;
333                         break;
334                 case Narrow:
335                         w = 31;
336                         break;
337                 }
338
339                 if (panner == 0) {
340                         panner = new Panner2d (_io.panner(), w, 61);
341                         panner->set_name ("MixerPanZone");
342                         panner->show ();
343                 }
344                 
345                 update_pan_sensitive ();
346                 panner->reset (_io.n_inputs());
347                 panner->set_size_request (w, 61);
348
349                 /* and finally, add it to the panner frame */
350
351                 panning_viewport.remove ();
352                 panning_viewport.add (*panner);
353                 panning_viewport.show_all ();
354         }
355 }
356
357 gint
358 PannerUI::pan_button_event (GdkEventButton* ev, uint32_t which)
359 {
360         switch (ev->button) {
361         case 3:
362                 if (pan_menu == 0) {
363                         pan_menu = manage (new Menu);
364                         pan_menu->set_name ("ArdourContextMenu");
365                 }
366                 build_pan_menu (which);
367                 pan_menu->popup (1, ev->time);
368                 return TRUE;
369                 break;
370         default:
371                 return FALSE;
372         }
373
374         return FALSE; // what's wrong with gcc?
375 }
376
377 void
378 PannerUI::build_pan_menu (uint32_t which)
379 {
380         using namespace Menu_Helpers;
381         MenuList& items (pan_menu->items());
382
383         items.clear ();
384
385         items.push_back (CheckMenuElem (_("Mute")));
386         
387         /* set state first, connect second */
388
389         (dynamic_cast<CheckMenuItem*> (&items.back()))->set_active (_io.panner()[which]->muted());
390         (dynamic_cast<CheckMenuItem*> (&items.back()))->signal_toggled().connect
391                 (bind (mem_fun(*this, &PannerUI::pan_mute), which));
392
393         items.push_back (CheckMenuElem (_("Bypass"), mem_fun(*this, &PannerUI::pan_bypass_toggle)));
394         bypass_menu_item = static_cast<CheckMenuItem*> (&items.back());
395
396         /* set state first, connect second */
397
398         bypass_menu_item->set_active (_io.panner().bypassed());
399         bypass_menu_item->signal_toggled().connect (mem_fun(*this, &PannerUI::pan_bypass_toggle));
400
401         items.push_back (MenuElem (_("Reset"), mem_fun(*this, &PannerUI::pan_reset)));
402         items.push_back (SeparatorElem());
403         items.push_back (MenuElem (_("Reset all")));
404 }
405
406 void
407 PannerUI::pan_mute (uint32_t which)
408 {
409         StreamPanner* sp = _io.panner()[which];
410         sp->set_muted (!sp->muted());
411 }
412
413 void
414 PannerUI::pan_bypass_toggle ()
415 {
416         if (bypass_menu_item && (_io.panner().bypassed() != bypass_menu_item->get_active())) {
417                 _io.panner().set_bypassed (!_io.panner().bypassed());
418         }
419 }
420
421 void
422 PannerUI::pan_reset ()
423 {
424 }
425
426 void
427 PannerUI::effective_pan_display ()
428 {
429         if (_io.panner().empty()) {
430                 return;
431         }
432
433         switch (_io.n_outputs()) {
434         case 0: 
435         case 1:
436                 /* relax */
437                 break;
438
439         case 2:
440                 update_pan_bars (true);
441                 break;
442
443         default:
444                 //panner->move_puck (pan_value (v, right), 0.5);
445                 break;
446         }
447 }
448
449 void
450 PannerUI::pan_changed (void *src)
451 {
452         if (src == this) {
453                 return;
454         }
455
456         switch (_io.panner().size()) {
457         case 0:
458                 panning_link_box.set_sensitive (false);
459                 return;
460         case 1:
461                 panning_link_box.set_sensitive (false);
462                 break;
463         default:
464                 panning_link_box.set_sensitive (true);
465         }
466
467         uint32_t nouts = _io.n_outputs();
468
469         switch (nouts) {
470         case 0:
471         case 1:
472                 /* relax */
473                 break;
474
475         case 2:
476                 update_pan_bars (false);
477                 break;
478
479         default:
480                 // panner->move_puck (pan_value (pans[0], pans[1]), 0.5);
481                 break;
482         }
483 }
484
485 void
486 PannerUI::pan_adjustment_changed (uint32_t which)
487 {
488         if (!in_pan_update && which < _io.panner().size()) {
489
490                 float xpos;
491                 float val = pan_adjustments[which]->get_value ();
492                 _io.panner()[which]->get_position (xpos);
493
494                 /* add a kinda-sorta detent for the middle */
495                 
496                 if (val != 0.5 && Panner::equivalent (val, 0.5)) {
497                         /* this is going to be reentrant, so just 
498                            return after it.
499                         */
500
501                         in_pan_update = true;
502                         pan_adjustments[which]->set_value (0.5);
503                         in_pan_update = false;
504                         return;
505                 }
506                 
507                 if (!Panner::equivalent (val, xpos)) {
508
509                         _io.panner()[which]->set_position (val);
510                         /* XXX 
511                            the panner objects have no access to the session,
512                            so do this here. ick.
513                         */
514                         _session.set_dirty();
515                 }
516         }
517 }
518
519 void
520 PannerUI::pan_value_changed (uint32_t which)
521 {
522         ENSURE_GUI_THREAD (bind (mem_fun(*this, &PannerUI::pan_value_changed), which));
523                                                            
524         if (_io.n_outputs() > 1 && which < _io.panner().size()) {
525                 float xpos;
526                 float val = pan_adjustments[which]->get_value ();
527
528                 _io.panner()[which]->get_position (xpos);
529
530                 if (!Panner::equivalent (val, xpos)) {
531                         in_pan_update = true;
532                         pan_adjustments[which]->set_value (xpos);
533                         in_pan_update = false;
534                 }
535         }
536 }       
537
538 void
539 PannerUI::update_pan_bars (bool only_if_aplay)
540 {
541         uint32_t n;
542         vector<Adjustment*>::iterator i;
543
544         in_pan_update = true;
545
546         /* this runs during automation playback, and moves the bar controllers
547            and/or pucks around.
548         */
549
550         for (i = pan_adjustments.begin(), n = 0; i != pan_adjustments.end(); ++i, ++n) {
551                 float xpos, val;
552
553                 if (only_if_aplay) {
554                         AutomationList& alist (_io.panner()[n]->automation());
555                         
556                         if (!alist.automation_playback()) {
557                                 continue;
558                         }
559                 }
560
561                 _io.panner()[n]->get_effective_position (xpos);
562                 val = (*i)->get_value ();
563                 
564                 if (!Panner::equivalent (val, xpos)) {
565                         (*i)->set_value (xpos);
566                 }
567         }
568
569         in_pan_update = false;
570 }
571
572 void
573 PannerUI::pan_printer (char *buf, uint32_t len, Adjustment* adj)
574 {
575         float val = adj->get_value();
576
577         if (val == 0.0f) {
578                 snprintf (buf, len, X_("L"));
579         } else if (val == 1.0f) {
580                 snprintf (buf, len, X_("R"));
581         } else if (Panner::equivalent (val, 0.5f)) {
582                 snprintf (buf, len, X_("C"));
583         } else {
584                 /* don't print anything */
585                 buf[0] = '\0';
586         }
587 }
588
589 void
590 PannerUI::update_pan_sensitive ()
591 {
592         bool sensitive = !(_io.panner().automation_state() & Play);
593
594         switch (_io.n_outputs()) {
595         case 0:
596         case 1:
597                 break;
598         case 2:
599                 for (vector<BarController*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
600                         (*i)->set_sensitive (sensitive);
601                 }
602                 break;
603         default:
604                 if (panner) {
605                         panner->set_sensitive (sensitive);
606                 }
607                 break;
608         }
609 }
610