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