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