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