enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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
23 #include "pbd/fastlog.h"
24
25 #include "ardour/pannable.h"
26 #include "ardour/panner.h"
27 #include "ardour/panner_shell.h"
28 #include "ardour/session.h"
29
30 #include "panner_ui.h"
31 #include "panner2d.h"
32 #include "gui_thread.h"
33 #include "stereo_panner.h"
34 #include "timers.h"
35 #include "tooltips.h"
36 #include "mono_panner.h"
37 #include "ui_config.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace Gtkmm2ext;
45 using namespace Gtk;
46 using namespace ARDOUR_UI_UTILS;
47
48 PannerUI::PannerUI (Session* s)
49         : _current_nouts (-1)
50         , _current_nins (-1)
51         , _current_uri ("")
52         , _send_mode (false)
53         , pan_automation_style_button ("")
54         , pan_automation_state_button ("")
55         , _panner_list()
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         set_tooltip (pan_automation_state_button, _("Pan automation mode"));
73         set_tooltip (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         delete _mono_panner;
117         _mono_panner = 0;
118
119         if (!_panner) {
120                 return;
121         }
122
123         _panshell->Changed.connect (connections, invalidator (*this), boost::bind (&PannerUI::panshell_changed, this), gui_context());
124
125         /* new panner object, force complete reset of panner GUI
126          */
127
128         _current_nouts = 0;
129         _current_nins = 0;
130
131         setup_pan ();
132         update_pan_sensitive ();
133         pan_automation_state_changed ();
134 }
135
136 void
137 PannerUI::build_astate_menu ()
138 {
139         using namespace Menu_Helpers;
140
141         if (pan_astate_menu == 0) {
142                 pan_astate_menu = new Menu;
143                 pan_astate_menu->set_name ("ArdourContextMenu");
144         } else {
145                 pan_astate_menu->items().clear ();
146         }
147
148         /** TRANSLATORS: this is `Manual' in the sense of automation not being played,
149             so that changes to pan must be done by hand.
150         */
151         pan_astate_menu->items().push_back (MenuElem (S_("Automation|Manual"), sigc::bind (
152                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
153                         (AutoState) ARDOUR::Off)));
154         pan_astate_menu->items().push_back (MenuElem (_("Play"), sigc::bind (
155                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
156                         (AutoState) Play)));
157         pan_astate_menu->items().push_back (MenuElem (_("Write"), sigc::bind (
158                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
159                         (AutoState) Write)));
160         pan_astate_menu->items().push_back (MenuElem (_("Touch"), sigc::bind (
161                         sigc::mem_fun (_panner.get(), &Panner::set_automation_state),
162                         (AutoState) Touch)));
163
164 }
165
166 void
167 PannerUI::build_astyle_menu ()
168 {
169         using namespace Menu_Helpers;
170
171         if (pan_astyle_menu == 0) {
172                 pan_astyle_menu = new Menu;
173                 pan_astyle_menu->set_name ("ArdourContextMenu");
174         } else {
175                 pan_astyle_menu->items().clear();
176         }
177
178         pan_astyle_menu->items().push_back (MenuElem (_("Trim")));
179         pan_astyle_menu->items().push_back (MenuElem (_("Abs")));
180 }
181
182 void
183 PannerUI::on_size_allocate (Allocation& a)
184 {
185         HBox::on_size_allocate (a);
186 }
187
188 void
189 PannerUI::set_width (Width w)
190 {
191         _width = w;
192 }
193
194 PannerUI::~PannerUI ()
195 {
196         delete twod_panner;
197         delete big_window;
198         delete pan_menu;
199         delete pan_astyle_menu;
200         delete pan_astate_menu;
201         delete _stereo_panner;
202         delete _mono_panner;
203 }
204
205 void
206 PannerUI::panshell_changed ()
207 {
208         set_panner (_panshell, _panshell->panner());
209         setup_pan ();
210 }
211
212 void
213 PannerUI::setup_pan ()
214 {
215         int const nouts = _panner ? _panner->out().n_audio() : -1;
216         int const nins = _panner ? _panner->in().n_audio() : -1;
217
218         if (nouts == _current_nouts
219                         && nins == _current_nins
220                         && _current_uri == _panshell->panner_gui_uri()
221                         )
222         {
223                 return;
224         }
225
226         _current_nins = nins;
227         _current_nouts = nouts;
228         _current_uri = _panshell->panner_gui_uri();
229
230         container_clear (pan_vbox);
231
232         delete twod_panner;
233         twod_panner = 0;
234         delete _stereo_panner;
235         _stereo_panner = 0;
236         delete _mono_panner;
237         _mono_panner = 0;
238
239         if (!_panner) {
240                 delete big_window;
241                 big_window = 0;
242                 return;
243         }
244
245         const float scale = std::max (1.f, UIConfiguration::instance().get_ui_scale());
246
247         if (_current_uri == "http://ardour.org/plugin/panner_2in2out#ui")
248         {
249                 delete big_window;
250                 big_window = 0;
251
252                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
253
254                 _stereo_panner = new StereoPanner (_panshell);
255                 _stereo_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
256                 _stereo_panner->set_send_drawing_mode (_send_mode);
257                 pan_vbox.pack_start (*_stereo_panner, false, false);
258
259                 boost::shared_ptr<AutomationControl> ac;
260
261                 ac = pannable->pan_azimuth_control;
262                 _stereo_panner->StartPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
263                                         boost::weak_ptr<AutomationControl> (ac)));
264                 _stereo_panner->StopPositionGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
265                                         boost::weak_ptr<AutomationControl>(ac)));
266
267                 ac = pannable->pan_width_control;
268                 _stereo_panner->StartWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
269                                         boost::weak_ptr<AutomationControl> (ac)));
270                 _stereo_panner->StopWidthGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
271                                         boost::weak_ptr<AutomationControl>(ac)));
272                 _stereo_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
273         }
274         else if (_current_uri == "http://ardour.org/plugin/panner_1in2out#ui"
275                         || _current_uri == "http://ardour.org/plugin/panner_balance#ui")
276         {
277                 delete big_window;
278                 big_window = 0;
279                 boost::shared_ptr<Pannable> pannable = _panner->pannable();
280                 boost::shared_ptr<AutomationControl> ac = pannable->pan_azimuth_control;
281
282                 _mono_panner = new MonoPanner (_panshell);
283
284                 _mono_panner->StartGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::start_touch),
285                                         boost::weak_ptr<AutomationControl> (ac)));
286                 _mono_panner->StopGesture.connect (sigc::bind (sigc::mem_fun (*this, &PannerUI::stop_touch),
287                                         boost::weak_ptr<AutomationControl>(ac)));
288
289                 _mono_panner->signal_button_release_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event));
290
291                 _mono_panner->set_size_request (-1, 5 * ceilf(7.f * scale));
292                 _mono_panner->set_send_drawing_mode (_send_mode);
293
294                 update_pan_sensitive ();
295                 pan_vbox.pack_start (*_mono_panner, false, false);
296         }
297         else if (_current_uri == "http://ardour.org/plugin/panner_vbap#ui")
298         {
299                 if (!twod_panner) {
300                         twod_panner = new Panner2d (_panshell, rintf(61.f * scale));
301                         twod_panner->set_name ("MixerPanZone");
302                         twod_panner->show ();
303                         twod_panner->signal_button_press_event().connect (sigc::mem_fun(*this, &PannerUI::pan_button_event), false);
304                 }
305
306                 update_pan_sensitive ();
307                 twod_panner->reset (nins);
308                 if (big_window) {
309                         big_window->reset (nins);
310                 }
311                 twod_panner->set_size_request (-1, rintf(61.f * scale));
312                 twod_panner->set_send_drawing_mode (_send_mode);
313
314                 /* and finally, add it to the panner frame */
315
316                 pan_vbox.pack_start (*twod_panner, false, false);
317         }
318         else
319         {
320                 /* stick something into the panning viewport so that it redraws */
321                 EventBox* eb = manage (new EventBox());
322                 pan_vbox.pack_start (*eb, false, false);
323
324                 delete big_window;
325                 big_window = 0;
326         }
327
328         pan_vbox.show_all ();
329 }
330
331 void
332 PannerUI::set_send_drawing_mode (bool onoff)
333 {
334         if (_stereo_panner) {
335                 _stereo_panner->set_send_drawing_mode (onoff);
336         } else if (_mono_panner) {
337                 _mono_panner->set_send_drawing_mode (onoff);
338         } else if (twod_panner) {
339                 twod_panner->set_send_drawing_mode (onoff);
340         }
341         _send_mode = onoff;
342 }
343
344 void
345 PannerUI::start_touch (boost::weak_ptr<AutomationControl> wac)
346 {
347         boost::shared_ptr<AutomationControl> ac = wac.lock();
348         if (!ac) {
349                 return;
350         }
351         ac->start_touch (ac->session().transport_frame());
352 }
353
354 void
355 PannerUI::stop_touch (boost::weak_ptr<AutomationControl> wac)
356 {
357         boost::shared_ptr<AutomationControl> ac = wac.lock();
358         if (!ac) {
359                 return;
360         }
361         ac->stop_touch (false, ac->session().transport_frame());
362 }
363
364 bool
365 PannerUI::pan_button_event (GdkEventButton* ev)
366 {
367         switch (ev->button) {
368         case 1:
369                 if (twod_panner && ev->type == GDK_2BUTTON_PRESS) {
370                         if (!big_window) {
371                                 big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
372                         }
373                         big_window->show ();
374                         return true;
375                 }
376                 break;
377
378         case 3:
379                 if (pan_menu == 0) {
380                         pan_menu = manage (new Menu);
381                         pan_menu->set_name ("ArdourContextMenu");
382                 }
383                 build_pan_menu ();
384                 pan_menu->popup (1, ev->time);
385                 return true;
386                 break;
387         default:
388                 return false;
389         }
390
391         return false; // what's wrong with gcc?
392 }
393
394 void
395 PannerUI::build_pan_menu ()
396 {
397         using namespace Menu_Helpers;
398         MenuList& items (pan_menu->items());
399
400         items.clear ();
401
402         items.push_back (CheckMenuElem (_("Bypass"), sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle)));
403         bypass_menu_item = static_cast<Gtk::CheckMenuItem*> (&items.back());
404
405         /* set state first, connect second */
406
407         bypass_menu_item->set_active (_panshell->bypassed());
408         bypass_menu_item->signal_toggled().connect (sigc::mem_fun(*this, &PannerUI::pan_bypass_toggle));
409
410         if (!_panshell->bypassed()) {
411                 items.push_back (MenuElem (_("Reset"), sigc::mem_fun (*this, &PannerUI::pan_reset)));
412                 items.push_back (MenuElem (_("Edit..."), sigc::mem_fun (*this, &PannerUI::pan_edit)));
413         }
414
415         if (_panner_list.size() > 1 && !_panshell->bypassed()) {
416                 RadioMenuItem::Group group;
417                 items.push_back (SeparatorElem());
418
419                 _suspend_menu_callbacks = true;
420                 for (std::map<std::string,std::string>::const_iterator p = _panner_list.begin(); p != _panner_list.end(); ++p) {
421                         items.push_back (RadioMenuElem (group, p->second,
422                                                 sigc::bind(sigc::mem_fun (*this, &PannerUI::pan_set_custom_type), p->first)));
423                         RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&items.back ());
424                         i->set_active (_panshell->current_panner_uri() == p->first);
425                 }
426                 _suspend_menu_callbacks = false;
427         }
428 }
429
430 void
431 PannerUI::pan_bypass_toggle ()
432 {
433         if (bypass_menu_item && (_panshell->bypassed() != bypass_menu_item->get_active())) {
434                 _panshell->set_bypassed (!_panshell->bypassed());
435         }
436 }
437
438 void
439 PannerUI::pan_edit ()
440 {
441         if (_panshell->bypassed()) {
442                 return;
443         }
444         if (_mono_panner) {
445                 _mono_panner->edit ();
446         } else if (_stereo_panner) {
447                 _stereo_panner->edit ();
448         } else if (twod_panner) {
449                 if (!big_window) {
450                         big_window = new Panner2dWindow (_panshell, 400, _panner->in().n_audio());
451                 }
452                 big_window->show ();
453         }
454 }
455
456 void
457 PannerUI::pan_reset ()
458 {
459         if (_panshell->bypassed()) {
460                 return;
461         }
462         _panner->reset ();
463 }
464
465 void
466 PannerUI::pan_set_custom_type (std::string uri) {
467         if (_suspend_menu_callbacks) return;
468         _panshell->select_panner_by_uri(uri);
469 }
470
471 void
472 PannerUI::effective_pan_display ()
473 {
474         if (_stereo_panner) {
475                 _stereo_panner->queue_draw ();
476         } else if (_mono_panner) {
477                 _mono_panner->queue_draw ();
478         } else if (twod_panner) {
479                 twod_panner->queue_draw ();
480         }
481 }
482
483 void
484 PannerUI::update_pan_sensitive ()
485 {
486         bool const sensitive = !(_panner->pannable()->automation_state() & Play);
487
488         pan_vbox.set_sensitive (sensitive);
489
490         if (big_window) {
491                 big_window->set_sensitive (sensitive);
492         }
493 }
494
495 gint
496 PannerUI::pan_automation_state_button_event (GdkEventButton *ev)
497 {
498         using namespace Menu_Helpers;
499
500         if (ev->type == GDK_BUTTON_RELEASE) {
501                 return TRUE;
502         }
503
504         switch (ev->button) {
505         case 1:
506                 if (pan_astate_menu == 0) {
507                         build_astate_menu ();
508                 }
509                 pan_astate_menu->popup (1, ev->time);
510                 break;
511         default:
512                 break;
513         }
514
515         return TRUE;
516 }
517
518 gint
519 PannerUI::pan_automation_style_button_event (GdkEventButton *ev)
520 {
521         if (ev->type == GDK_BUTTON_RELEASE) {
522                 return TRUE;
523         }
524
525         switch (ev->button) {
526         case 1:
527                 if (pan_astyle_menu == 0) {
528                         build_astyle_menu ();
529                 }
530                 pan_astyle_menu->popup (1, ev->time);
531                 break;
532         default:
533                 break;
534         }
535         return TRUE;
536 }
537
538 void
539 PannerUI::pan_automation_style_changed ()
540 {
541         ENSURE_GUI_THREAD (*this, &PannerUI::pan_automation_style_changed)
542
543         switch (_width) {
544         case Wide:
545                 pan_automation_style_button.set_label (astyle_string(_panner->automation_style()));
546                 break;
547         case Narrow:
548                 pan_automation_style_button.set_label (short_astyle_string(_panner->automation_style()));
549                 break;
550         }
551 }
552
553 void
554 PannerUI::pan_automation_state_changed ()
555 {
556         boost::shared_ptr<Pannable> pannable (_panner->pannable());
557
558         switch (_width) {
559         case Wide:
560                 pan_automation_state_button.set_label (astate_string(pannable->automation_state()));
561                 break;
562         case Narrow:
563                 pan_automation_state_button.set_label (short_astate_string(pannable->automation_state()));
564                 break;
565         }
566
567         bool x = (pannable->automation_state() != ARDOUR::Off);
568
569         if (pan_automation_state_button.get_active() != x) {
570                 ignore_toggle = true;
571                 pan_automation_state_button.set_active (x);
572                 ignore_toggle = false;
573         }
574
575         update_pan_sensitive ();
576
577         /* start watching automation so that things move */
578
579         pan_watching.disconnect();
580
581         if (x) {
582                 pan_watching = Timers::rapid_connect (sigc::mem_fun (*this, &PannerUI::effective_pan_display));
583         }
584 }
585
586 string
587 PannerUI::astate_string (AutoState state)
588 {
589         return _astate_string (state, false);
590 }
591
592 string
593 PannerUI::short_astate_string (AutoState state)
594 {
595         return _astate_string (state, true);
596 }
597
598 string
599 PannerUI::_astate_string (AutoState state, bool shrt)
600 {
601         string sstr;
602
603         switch (state) {
604         case ARDOUR::Off:
605                 sstr = (shrt ? "M" : S_("Manual|M"));
606                 break;
607         case Play:
608                 sstr = (shrt ? "P" : S_("Play|P"));
609                 break;
610         case Touch:
611                 sstr = (shrt ? "T" : S_("Touch|T"));
612                 break;
613         case Write:
614                 sstr = (shrt ? "W" : S_("Write|W"));
615                 break;
616         }
617
618         return sstr;
619 }
620
621 string
622 PannerUI::astyle_string (AutoStyle style)
623 {
624         return _astyle_string (style, false);
625 }
626
627 string
628 PannerUI::short_astyle_string (AutoStyle style)
629 {
630         return _astyle_string (style, true);
631 }
632
633 string
634 PannerUI::_astyle_string (AutoStyle style, bool shrt)
635 {
636         if (style & Trim) {
637                 return _("Trim");
638         } else {
639                 /* XXX it might different in different languages */
640
641                 return (shrt ? _("Abs") : _("Abs"));
642         }
643 }
644
645 void
646 PannerUI::show_width ()
647 {
648 }
649
650 void
651 PannerUI::width_adjusted ()
652 {
653 }
654
655 void
656 PannerUI::show_position ()
657 {
658 }
659
660 void
661 PannerUI::position_adjusted ()
662 {
663 }
664
665 void
666 PannerUI::set_available_panners(std::map<std::string,std::string> p)
667 {
668         _panner_list = p;
669 }