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