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