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