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