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