c086ce177ca5fd83b1ae6b39ac7ee5f53eef6e7a
[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
19 #include <limits.h>
20
21 #include "ardour/io.h"
22 #include "ardour/dB.h"
23 #include <gtkmm2ext/utils.h>
24 #include <gtkmm2ext/barcontroller.h>
25 #include "midi++/manager.h"
26 #include "pbd/fastlog.h"
27
28 #include "ardour_ui.h"
29 #include "panner_ui.h"
30 #include "panner2d.h"
31 #include "utils.h"
32 #include "gui_thread.h"
33 #include "stereo_panner.h"
34 #include "mono_panner.h"
35
36 #include "ardour/delivery.h"
37 #include "ardour/session.h"
38 #include "ardour/panner.h"
39 #include "ardour/pannable.h"
40 #include "ardour/panner_shell.h"
41 #include "ardour/route.h"
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47 using namespace PBD;
48 using namespace Gtkmm2ext;
49 using namespace Gtk;
50
51 const int PannerUI::pan_bar_height = 35;
52
53 PannerUI::PannerUI (Session* s)
54         : _current_nouts (-1)
55         , _current_nins (-1)
56         , pan_automation_style_button ("")
57         , pan_automation_state_button ("")
58 {
59         set_session (s);
60
61         ignore_toggle = false;
62         pan_menu = 0;
63         pan_astate_menu = 0;
64         pan_astyle_menu = 0;
65         in_pan_update = false;
66         _stereo_panner = 0;
67         _mono_panner = 0;
68         _ignore_width_change = false;
69         _ignore_position_change = false;
70
71         pan_automation_style_button.set_name ("MixerAutomationModeButton");
72         pan_automation_state_button.set_name ("MixerAutomationPlaybackButton");
73
74         ARDOUR_UI::instance()->set_tip (pan_automation_state_button, _("Pan automation mode"));
75         ARDOUR_UI::instance()->set_tip (pan_automation_style_button, _("Pan automation type"));
76
77         //set_size_request_to_display_given_text (pan_automation_state_button, X_("O"), 2, 2);
78         //set_size_request_to_display_given_text (pan_automation_style_button, X_("0"), 2, 2);
79
80         pan_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
81         pan_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
82
83         pan_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_style_button_event), false);
84         pan_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_automation_state_button_event), false);
85
86         pan_vbox.set_spacing (2);
87         pack_start (pan_vbox, true, true);
88
89         twod_panner = 0;
90         big_window = 0;
91
92         set_width(Narrow);
93 }
94
95 void
96 PannerUI::set_panner (boost::shared_ptr<PannerShell> ps, boost::shared_ptr<Panner> p)
97 {
98         /* note that the panshell might not change here (i.e. ps == _panshell)
99          */
100
101         connections.drop_connections ();
102
103         delete pan_astyle_menu;
104         pan_astyle_menu = 0;
105
106         delete pan_astate_menu;
107         pan_astate_menu = 0;
108
109         _panshell = ps;
110         _panner = p;
111
112         delete twod_panner;
113         twod_panner = 0;
114
115         delete _stereo_panner;
116         _stereo_panner = 0;
117
118         if (!_panner) {
119                 return;
120         }
121
122         _panshell->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::panshell_changed, this), gui_context());
123
124         /* new panner object, force complete reset of panner GUI
125          */
126
127         _current_nouts = 0;
128         _current_nins = 0;
129
130         setup_pan ();
131         update_pan_sensitive ();
132         pan_automation_state_changed ();
133 }
134
135 void
136 PannerUI::build_astate_menu ()
137 {
138         using namespace Menu_Helpers;
139
140         if (pan_astate_menu == 0) {
141                 pan_astate_menu = new Menu;
142                 pan_astate_menu->set_name ("ArdourContextMenu");
143         } else {
144                 pan_astate_menu->items().clear ();
145         }
146
147         pan_astate_menu->items().push_back (MenuElem (_("Manual"), sigc::bind (
148                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
149                         (AutoState) Off)));
150         pan_astate_menu->items().push_back (MenuElem (_("Play"), sigc::bind (
151                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
152                         (AutoState) Play)));
153         pan_astate_menu->items().push_back (MenuElem (_("Write"), sigc::bind (
154                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
155                         (AutoState) Write)));
156         pan_astate_menu->items().push_back (MenuElem (_("Touch"), sigc::bind (
157                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
158                         (AutoState) Touch)));
159
160 }
161
162 void
163 PannerUI::build_astyle_menu ()
164 {
165         using namespace Menu_Helpers;
166
167         if (pan_astyle_menu == 0) {
168                 pan_astyle_menu = new Menu;
169                 pan_astyle_menu->set_name ("ArdourContextMenu");
170         } else {
171                 pan_astyle_menu->items().clear();
172         }
173
174         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
175         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
176 }
177
178 boost::shared_ptr<PBD::Controllable>
179 PannerUI::get_controllable()
180 {
181         assert (!pan_bars.empty());
182         return pan_bars[0]->get_controllable();
183 }
184
185 void
186 PannerUI::on_size_allocate (Allocation& a)
187 {
188         HBox::on_size_allocate (a);
189 }
190
191 void
192 PannerUI::set_width (Width w)
193 {
194         _width = w;
195 }
196
197 PannerUI::~PannerUI ()
198 {
199         for (vector<MonoPanner*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
200                 delete (*i);
201         }
202
203         delete twod_panner;
204         delete big_window;
205         delete pan_menu;
206         delete pan_astyle_menu;
207         delete pan_astate_menu;
208         delete _stereo_panner;
209         delete _mono_panner;
210 }
211
212 void
213 PannerUI::panshell_changed ()
214 {
215         set_panner (_panshell, _panshell->panner());
216         setup_pan ();
217 }
218
219 void
220 PannerUI::setup_pan ()
221 {
222         int const nouts = _panner ? _panner->out().n_audio() : -1;
223         int const nins = _panner ? _panner->in().n_audio() : -1;
224
225         if (nouts == _current_nouts && nins == _current_nins) {
226                 return;
227         }
228
229         _current_nins = nins;
230         _current_nouts = nouts;
231
232         container_clear (pan_vbox);
233
234         delete twod_panner;
235         twod_panner = 0;
236         delete _stereo_panner;
237         _stereo_panner = 0;
238         delete _mono_panner;
239         _mono_panner = 0;
240
241         if (!_panner) {
242                 return;
243         }
244
245         if (nouts == 0 || nouts == 1) {
246
247                 /* stick something into the panning viewport so that it redraws */
248
249                 EventBox* eb = manage (new EventBox());
250                 pan_vbox.pack_start (*eb, false, false);
251
252                 delete big_window;
253                 big_window = 0;
254
255         } else if (nouts == 2) {
256
257                 if (nins == 2) {
258
259                         /* add integrated 2in/2out panner GUI */
260
261                         boost::shared_ptr<Pannable> pannable = _panner->pannable();
262
263                         _stereo_panner = new StereoPanner (_panner);
264                         _stereo_panner->set_size_request (-1, pan_bar_height);
265                         pan_vbox.pack_start (*_stereo_panner, false, false);
266
267                         boost::shared_ptr<AutomationControl> ac;
268
269                         ac = pannable->pan_azimuth_control;
270                         _stereo_panner->StartPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
271                                                                                   boost::weak_ptr<AutomationControl> (ac)));
272                         _stereo_panner->StopPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
273                                                                                  boost::weak_ptr<AutomationControl>(ac)));
274
275                         ac = pannable->pan_width_control;
276                         _stereo_panner->StartWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
277                                                                                boost::weak_ptr<AutomationControl> (ac)));
278                         _stereo_panner->StopWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
279                                                                               boost::weak_ptr<AutomationControl>(ac)));
280                         _stereo_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
281
282                 } else if (nins == 1) {
283                         /* 1-in/2out */
284
285                         boost::shared_ptr<Pannable> pannable = _panner->pannable();
286                         boost::shared_ptr<AutomationControl> ac = pannable->pan_azimuth_control;
287
288                         _mono_panner = new MonoPanner (_panner);
289                         
290                         _mono_panner->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
291                                                                       boost::weak_ptr<AutomationControl> (ac)));
292                         _mono_panner->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
293                                                              boost::weak_ptr<AutomationControl>(ac)));
294
295                         _mono_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
296
297                         _mono_panner->set_size_request (-1, pan_bar_height);
298
299                         update_pan_sensitive ();
300                         pan_vbox.pack_start (*_mono_panner, false, false);
301
302                 } else {
303                         warning << string_compose (_("No panner user interface is currently available for %1-in/2out tracks/busses"),
304                                                    nins) << endmsg;
305                 }
306
307                 delete big_window;
308                 big_window = 0;
309
310         } else {
311
312                 if (!twod_panner) {
313                         twod_panner = new Panner2d (_panshell, 61);
314                         twod_panner->set_name ("MixerPanZone");
315                         twod_panner->show ();
316                         twod_panner->signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event), false);
317                 }
318
319                 update_pan_sensitive ();
320                 twod_panner->reset (nins);
321                 if (big_window) {
322                         big_window->reset (nins);
323                 }
324                 twod_panner->set_size_request (-1, 61);
325
326                 /* and finally, add it to the panner frame */
327
328                 pan_vbox.pack_start (*twod_panner, false, false);
329         }
330
331         pan_vbox.show_all ();
332 }
333
334 void
335 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
336 {
337         boost::shared_ptr<AutomationControl> ac = wac.lock();
338         if (!ac) {
339                 return;
340         }
341         ac->start_touch (ac->session().transport_frame());
342 }
343
344 void
345 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
346 {
347         boost::shared_ptr<AutomationControl> ac = wac.lock();
348         if (!ac) {
349                 return;
350         }
351         ac->stop_touch (false, ac->session().transport_frame());
352 }
353
354 bool
355 PannerUI::pan_button_event (GdkEventButton* ev)
356 {
357         switch (ev->button) {
358         case 1:
359                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
360                         if (!big_window) {
361                                 big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
362                         }
363                         big_window->show ();
364                         return true;
365                 }
366                 break;
367
368         case 3:
369                 if (pan_menu == 0) {
370                         pan_menu = manage (new Menu);
371                         pan_menu->set_name ("ArdourContextMenu");
372                 }
373                 build_pan_menu ();
374                 pan_menu->popup (1, ev->time);
375                 return true;
376                 break;
377         default:
378                 return false;
379         }
380
381         return false; // what's wrong with gcc?
382 }
383
384 void
385 PannerUI::build_pan_menu ()
386 {
387         using namespace Menu_Helpers;
388         MenuList& items (pan_menu->items());
389
390         items.clear ();
391
392         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
393         bypass_menu_item = static_cast<CheckMenuItem*> (&items.back());
394
395         /* set state first, connect second */
396
397         bypass_menu_item->set_active (_panshell->bypassed());
398         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
399
400         items.push_back (MenuElem (_("Reset"), sigc::mem_fun (*this, &PannerUI::pan_reset)));
401 }
402
403 void
404 PannerUI::pan_bypass_toggle ()
405 {
406         if (bypass_menu_item && (_panshell->bypassed() != bypass_menu_item->get_active())) {
407                 _panshell->set_bypassed (!_panshell->bypassed());
408         }
409 }
410
411 void
412 PannerUI::pan_reset ()
413 {
414         _panner->reset ();
415 }
416
417 void
418 PannerUI::effective_pan_display ()
419 {
420         if (_stereo_panner) {
421                 _stereo_panner->queue_draw ();
422         } else if (twod_panner) {
423                 twod_panner->queue_draw ();
424         } else {
425                 for (vector<MonoPanner*>::iterator i = pan_bars.begin(); i != pan_bars.end(); ++i) {
426                         (*i)->queue_draw ();
427                 }
428         }
429 }
430
431 void
432 PannerUI::update_pan_sensitive ()
433 {
434         bool const sensitive = !(_panner->pannable()->automation_state() & Play);
435
436         pan_vbox.set_sensitive (sensitive);
437
438         if (big_window) {
439                 big_window->set_sensitive (sensitive);
440         }
441 }
442
443 gint
444 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
445 {
446         using namespace Menu_Helpers;
447
448         if (ev->type == GDK_BUTTON_RELEASE) {
449                 return TRUE;
450         }
451
452         switch (ev->button) {
453         case 1:
454                 if (pan_astate_menu == 0) {
455                         build_astate_menu ();
456                 }
457                 pan_astate_menu->popup (1, ev->time);
458                 break;
459         default:
460                 break;
461         }
462
463         return TRUE;
464 }
465
466 gint
467 PannerUI::pan_automation_style_button_event (GdkEventButton *ev)
468 {
469         if (ev->type == GDK_BUTTON_RELEASE) {
470                 return TRUE;
471         }
472
473         switch (ev->button) {
474         case 1:
475                 if (pan_astyle_menu == 0) {
476                         build_astyle_menu ();
477                 }
478                 pan_astyle_menu->popup (1, ev->time);
479                 break;
480         default:
481                 break;
482         }
483         return TRUE;
484 }
485
486 void
487 PannerUI::pan_automation_style_changed ()
488 {
489         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_style_changed)
490
491         switch (_width) {
492         case Wide:
493                 pan_automation_style_button.set_label (astyle_string(_panner->automation_style()));
494                 break;
495         case Narrow:
496                 pan_automation_style_button.set_label (short_astyle_string(_panner->automation_style()));
497                 break;
498         }
499 }
500
501 void
502 PannerUI::pan_automation_state_changed ()
503 {
504         boost::shared_ptr<Pannable> pannable (_panner->pannable());
505
506         switch (_width) {
507         case Wide:
508                 pan_automation_state_button.set_label (astate_string(pannable->automation_state()));
509                 break;
510         case Narrow:
511                 pan_automation_state_button.set_label (short_astate_string(pannable->automation_state()));
512                 break;
513         }
514
515         bool x = (pannable->automation_state() != Off);
516
517         if (pan_automation_state_button.get_active() != x) {
518                 ignore_toggle = true;
519                 pan_automation_state_button.set_active (x);
520                 ignore_toggle = false;
521         }
522
523         update_pan_sensitive ();
524
525         /* start watching automation so that things move */
526
527         pan_watching.disconnect();
528
529         if (x) {
530                 pan_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &PannerUI::effective_pan_display));
531         }
532 }
533
534 string
535 PannerUI::astate_string (AutoState state)
536 {
537         return _astate_string (state, false);
538 }
539
540 string
541 PannerUI::short_astate_string (AutoState state)
542 {
543         return _astate_string (state, true);
544 }
545
546 string
547 PannerUI::_astate_string (AutoState state, bool shrt)
548 {
549         string sstr;
550
551         switch (state) {
552         case Off:
553                 sstr = (shrt ? "M" : _("M"));
554                 break;
555         case Play:
556                 sstr = (shrt ? "P" : _("P"));
557                 break;
558         case Touch:
559                 sstr = (shrt ? "T" : _("T"));
560                 break;
561         case Write:
562                 sstr = (shrt ? "W" : _("W"));
563                 break;
564         }
565
566         return sstr;
567 }
568
569 string
570 PannerUI::astyle_string (AutoStyle style)
571 {
572         return _astyle_string (style, false);
573 }
574
575 string
576 PannerUI::short_astyle_string (AutoStyle style)
577 {
578         return _astyle_string (style, true);
579 }
580
581 string
582 PannerUI::_astyle_string (AutoStyle style, bool shrt)
583 {
584         if (style & Trim) {
585                 return _("Trim");
586         } else {
587                 /* XXX it might different in different languages */
588
589                 return (shrt ? _("Abs") : _("Abs"));
590         }
591 }
592
593 void
594 PannerUI::show_width ()
595 {
596 }
597
598 void
599 PannerUI::width_adjusted ()
600 {
601 }
602
603 void
604 PannerUI::show_position ()
605 {
606 }
607
608 void
609 PannerUI::position_adjusted ()
610 {
611 }