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