remove cruft
[ardour.git] / gtk2_ardour / gain_meter.cc
1 /*
2   Copyright (C) 2002 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
20 #include <limits.h>
21
22 #include "ardour/amp.h"
23 #include "ardour/route_group.h"
24 #include "ardour/session_route.h"
25 #include "ardour/dB.h"
26 #include "ardour/utils.h"
27
28 #include <pangomm.h>
29 #include <gtkmm/style.h>
30 #include <gdkmm/color.h>
31 #include <gtkmm2ext/utils.h>
32 #include <gtkmm2ext/fastmeter.h>
33 #include <gtkmm2ext/barcontroller.h>
34 #include <gtkmm2ext/gtk_ui.h>
35 #include "midi++/manager.h"
36 #include "pbd/fastlog.h"
37 #include "pbd/stacktrace.h"
38
39 #include "ardour_ui.h"
40 #include "gain_meter.h"
41 #include "global_signals.h"
42 #include "logmeter.h"
43 #include "gui_thread.h"
44 #include "keyboard.h"
45 #include "public_editor.h"
46 #include "utils.h"
47 #include "meter_patterns.h"
48
49 #include "ardour/session.h"
50 #include "ardour/route.h"
51 #include "ardour/meter.h"
52 #include "ardour/audio_track.h"
53 #include "ardour/midi_track.h"
54
55 #include "i18n.h"
56
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtkmm2ext;
60 using namespace Gtk;
61 using namespace std;
62 using Gtkmm2ext::Keyboard;
63
64 GainMeterBase::GainMeterBase (Session* s, bool horizontal, int fader_length, int fader_girth)
65         : gain_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0.0, 1.0, 0.01, 0.1)
66         , gain_automation_style_button ("")
67         , gain_automation_state_button ("")
68         , _data_type (DataType::AUDIO)
69
70 {
71         using namespace Menu_Helpers;
72
73         set_session (s);
74
75         ignore_toggle = false;
76         meter_menu = 0;
77         next_release_selects = false;
78         _width = Wide;
79
80         if (horizontal) {
81                 gain_slider = manage (new HSliderController (&gain_adjustment, fader_length, fader_girth, false));
82         } else {
83                 gain_slider = manage (new VSliderController (&gain_adjustment, fader_length, fader_girth, false));
84         }
85
86         level_meter = new LevelMeter(_session);
87
88         level_meter->ButtonPress.connect_same_thread (_level_meter_connection, boost::bind (&GainMeterBase::level_meter_button_press, this, _1));
89         meter_metric_area.signal_button_press_event().connect (sigc::mem_fun (*this, &GainMeterBase::level_meter_button_press));
90         meter_metric_area.add_events (Gdk::BUTTON_PRESS_MASK);
91
92         gain_slider->signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_press), false);
93         gain_slider->signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_release), false);
94         gain_slider->set_name ("GainFader");
95
96         gain_display.set_name ("MixerStripGainDisplay");
97         set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
98         gain_display.signal_activate().connect (sigc::mem_fun (*this, &GainMeter::gain_activated));
99         gain_display.signal_focus_in_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
100         gain_display.signal_focus_out_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
101
102         peak_display.set_name ("MixerStripPeakDisplay");
103         set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
104         max_peak = minus_infinity();
105         peak_display.set_label (_("-inf"));
106         peak_display.unset_flags (Gtk::CAN_FOCUS);
107
108         gain_automation_style_button.set_name ("mixer strip button");
109         gain_automation_state_button.set_name ("mixer strip button");
110
111         ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
112         ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
113
114         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
115         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
116
117         gain_automation_state_button.set_size_request(15, 15);
118         gain_automation_style_button.set_size_request(15, 15);
119
120         gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
121         gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
122
123         gain_astate_menu.set_name ("ArdourContextMenu");
124         gain_astyle_menu.set_name ("ArdourContextMenu");
125
126         gain_adjustment.signal_value_changed().connect (sigc::mem_fun(*this, &GainMeterBase::gain_adjusted));
127         peak_display.signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeterBase::peak_button_release), false);
128         gain_display.signal_key_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_key_press), false);
129
130         ResetAllPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_peak_display));
131         ResetRoutePeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_route_peak_display));
132         ResetGroupPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_group_peak_display));
133         RedrawMetrics.connect (sigc::mem_fun(*this, &GainMeterBase::redraw_metrics));
134
135         UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &GainMeterBase::on_theme_changed));
136         ColorsChanged.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), false));
137         DPIReset.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), true));
138         
139 //      PBD::ScopedConnection _config_connection;
140 //      Config->ParameterChanged.connect ( _config_connection, MISSING_INVALIDATOR, boost::bind(&GainMeterBase::set_flat_buttons, this, _1), gui_context() );
141 }
142
143 void
144 GainMeterBase::set_flat_buttons ()
145 {
146 printf("set_flat_butt\n");
147 //      gain_slider->set_flat_buttons( ARDOUR_UI::config()->flat_buttons.get() );
148 }
149
150 GainMeterBase::~GainMeterBase ()
151 {
152         delete meter_menu;
153         delete level_meter;
154 }
155
156 void
157 GainMeterBase::set_controls (boost::shared_ptr<Route> r,
158                              boost::shared_ptr<PeakMeter> pm,
159                              boost::shared_ptr<Amp> amp)
160 {
161         connections.clear ();
162         model_connections.drop_connections ();
163
164         if (!pm && !amp) {
165                 level_meter->set_meter (0);
166                 gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
167                 _meter.reset ();
168                 _amp.reset ();
169                 _route.reset ();
170                 return;
171         }
172
173         _meter = pm;
174         _amp = amp;
175         _route = r;
176
177         level_meter->set_meter (pm.get());
178         gain_slider->set_controllable (amp->gain_control());
179
180         if (amp) {
181                 amp->ConfigurationChanged.connect (
182                         model_connections, invalidator (*this), boost::bind (&GainMeterBase::setup_gain_adjustment, this), gui_context ()
183                         );
184         }
185
186         setup_gain_adjustment ();
187
188         if (!_route || !_route->is_auditioner()) {
189
190                 using namespace Menu_Helpers;
191
192                 gain_astate_menu.items().clear ();
193
194                 gain_astate_menu.items().push_back (MenuElem (S_("Automation|Manual"),
195                                                               sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
196                                                                           Evoral::Parameter(GainAutomation), (AutoState) ARDOUR::Off)));
197                 gain_astate_menu.items().push_back (MenuElem (_("Play"),
198                                                               sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
199                                                                     Evoral::Parameter(GainAutomation), (AutoState) Play)));
200                 gain_astate_menu.items().push_back (MenuElem (_("Write"),
201                                                               sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
202                                                                     Evoral::Parameter(GainAutomation), (AutoState) Write)));
203                 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
204                                                               sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
205                                                                     Evoral::Parameter(GainAutomation), (AutoState) Touch)));
206
207                 connections.push_back (gain_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
208                 connections.push_back (gain_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
209
210                 boost::shared_ptr<AutomationControl> gc = amp->gain_control();
211
212                 gc->alist()->automation_state_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_state_changed, this), gui_context());
213                 gc->alist()->automation_style_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_style_changed, this), gui_context());
214
215                 gain_automation_state_changed ();
216         }
217
218         amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
219
220         gain_changed ();
221         show_gain ();
222         update_gain_sensitive ();
223 }
224
225 void
226 GainMeterBase::setup_gain_adjustment ()
227 {
228         if (!_amp) {
229                 return;
230         }
231
232         if (_previous_amp_output_streams == _amp->output_streams ()) {
233                 return;
234         }
235
236         ignore_toggle = true;
237
238         if (_amp->output_streams().n_midi() <=  _amp->output_streams().n_audio()) {
239                 _data_type = DataType::AUDIO;
240                 gain_adjustment.set_lower (0.0);
241                 gain_adjustment.set_upper (1.0);
242                 gain_adjustment.set_step_increment (0.01);
243                 gain_adjustment.set_page_increment (0.1);
244                 gain_slider->set_default_value (gain_to_slider_position (1));
245         } else {
246                 _data_type = DataType::MIDI;
247                 gain_adjustment.set_lower (0.0);
248                 gain_adjustment.set_upper (2.0);
249                 gain_adjustment.set_step_increment (1.0/128.0);
250                 gain_adjustment.set_page_increment (10.0/128.0);
251                 gain_slider->set_default_value (1.0);
252         }
253
254         ignore_toggle = false;
255
256         effective_gain_display ();
257         
258         _previous_amp_output_streams = _amp->output_streams ();
259 }
260
261 void
262 GainMeterBase::hide_all_meters ()
263 {
264         level_meter->hide_meters();
265 }
266
267 void
268 GainMeter::hide_all_meters ()
269 {
270         GainMeterBase::hide_all_meters ();
271 }
272
273 void
274 GainMeterBase::setup_meters (int len)
275 {
276         int meter_width = 5;
277         if (_width == Wide && _route && _route->shared_peak_meter()->input_streams().n_total() == 1) {
278                 meter_width = 10;
279         }
280         level_meter->setup_meters(len, meter_width);
281
282         switch (_width) {
283         case Wide:
284                 //meter_metric_area.show();
285                 meter_ticks1_area.show();
286                 meter_ticks2_area.show();
287                 break;
288         case Narrow:
289                 //meter_metric_area.hide();
290                 meter_ticks1_area.hide();
291                 meter_ticks2_area.hide();
292                 break;
293         }
294 }
295
296 void
297 GainMeterBase::set_type (MeterType t)
298 {
299         level_meter->set_type(t);
300 }
301
302 void
303 GainMeter::setup_meters (int len)
304 {
305         GainMeterBase::setup_meters (len);
306 }
307
308 void
309 GainMeter::set_type (MeterType t)
310 {
311         GainMeterBase::set_type (t);
312 }
313
314 bool
315 GainMeterBase::gain_key_press (GdkEventKey* ev)
316 {
317         if (key_is_legal_for_numeric_entry (ev->keyval)) {
318                 /* drop through to normal handling */
319                 return false;
320         }
321         /* illegal key for gain entry */
322         return true;
323 }
324
325 bool
326 GainMeterBase::peak_button_release (GdkEventButton* ev)
327 {
328         /* reset peak label */
329
330         if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
331                 ResetAllPeakDisplays ();
332         } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
333                 if (_route) {
334                         ResetGroupPeakDisplays (_route->route_group());
335                 }
336         } else {
337                 ResetRoutePeakDisplays (_route.get());
338         }
339
340         return true;
341 }
342
343 void
344 GainMeterBase::reset_peak_display ()
345 {
346         _meter->reset_max();
347         level_meter->clear_meters();
348         max_peak = -INFINITY;
349         peak_display.set_label (_("-inf"));
350         peak_display.set_name ("MixerStripPeakDisplay");
351 }
352
353 void
354 GainMeterBase::reset_route_peak_display (Route* route)
355 {
356         if (_route && _route.get() == route) {
357                 reset_peak_display ();
358         }
359 }
360
361 void
362 GainMeterBase::reset_group_peak_display (RouteGroup* group)
363 {
364         if (_route && group == _route->route_group()) {
365                 reset_peak_display ();
366         }
367 }
368
369 void
370 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
371 {
372         using namespace Menu_Helpers;
373
374         if (meter_menu == 0) {
375                 meter_menu = new Gtk::Menu;
376                 MenuList& items = meter_menu->items();
377
378                 items.push_back (MenuElem ("-inf .. +0dBFS"));
379                 items.push_back (MenuElem ("-10dB .. +0dBFS"));
380                 items.push_back (MenuElem ("-4 .. +0dBFS"));
381                 items.push_back (SeparatorElem());
382                 items.push_back (MenuElem ("-inf .. -2dBFS"));
383                 items.push_back (MenuElem ("-10dB .. -2dBFS"));
384                 items.push_back (MenuElem ("-4 .. -2dBFS"));
385         }
386
387         meter_menu->popup (1, ev->time);
388 }
389
390 bool
391 GainMeterBase::gain_focused (GdkEventFocus* ev)
392 {
393         if (ev->in) {
394                 gain_display.select_region (0, -1);
395         } else {
396                 gain_display.select_region (0, 0);
397         }
398         return false;
399 }
400
401 void
402 GainMeterBase::gain_activated ()
403 {
404         float f;
405
406         {
407                 // Switch to user's preferred locale so that
408                 // if they use different LC_NUMERIC conventions,
409                 // we will honor them.
410
411                 PBD::LocaleGuard lg ("");
412                 if (sscanf (gain_display.get_text().c_str(), "%f", &f) != 1) {
413                         return;
414                 }
415         }
416
417         /* clamp to displayable values */
418         if (_data_type == DataType::AUDIO) {
419                 f = min (f, 6.0f);
420                 _amp->set_gain (dB_to_coefficient(f), this);
421         } else {
422                 f = min (fabs (f), 2.0f);
423                 _amp->set_gain (f, this);
424         }
425
426         if (gain_display.has_focus()) {
427                 Gtk::Widget* w = gain_display.get_toplevel();
428                 if (w) {
429                         Gtk::Window* win = dynamic_cast<Gtk::Window*> (w);
430
431                         /* sigh. gtkmm doesn't wrap get_default_widget() */
432
433                         if (win) {
434                                 GtkWidget* f = gtk_window_get_default_widget (win->gobj());
435                                 if (f) {
436                                         gtk_widget_grab_focus (f);
437                                         return;
438                                 }
439                         }
440                 }
441         }
442 }
443
444 void
445 GainMeterBase::show_gain ()
446 {
447         char buf[32];
448
449         float v = gain_adjustment.get_value();
450
451         switch (_data_type) {
452         case DataType::AUDIO:
453                 if (v == 0.0) {
454                         strcpy (buf, _("-inf"));
455                 } else {
456                         snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (v, Config->get_max_gain())));
457                 }
458                 break;
459         case DataType::MIDI:
460                 snprintf (buf, sizeof (buf), "%.1f", v);
461                 break;
462         }
463
464         gain_display.set_text (buf);
465 }
466
467 void
468 GainMeterBase::gain_adjusted ()
469 {
470         gain_t value;
471
472         /* convert from adjustment range (0..1) to gain coefficient */
473
474         if (_data_type == DataType::AUDIO) {
475                 value = slider_position_to_gain_with_max (gain_adjustment.get_value(), Config->get_max_gain());
476         } else {
477                 value = gain_adjustment.get_value();
478         }
479         
480         if (!ignore_toggle) {
481                 if (_route && _route->amp() == _amp) {
482                         _route->set_gain (value, this);
483                 } else {
484                         _amp->set_gain (value, this);
485                 }
486         }
487
488         show_gain ();
489 }
490
491 void
492 GainMeterBase::effective_gain_display ()
493 {
494         float value = 0.0;
495
496         switch (_data_type) {
497         case DataType::AUDIO:
498                 value = gain_to_slider_position_with_max (_amp->gain(), Config->get_max_gain());
499                 break;
500         case DataType::MIDI:
501                 value = _amp->gain ();
502                 break;
503         }
504
505         if (gain_adjustment.get_value() != value) {
506                 ignore_toggle = true;
507                 gain_adjustment.set_value (value);
508                 ignore_toggle = false;
509         }
510 }
511
512 void
513 GainMeterBase::gain_changed ()
514 {
515         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
516 }
517
518 void
519 GainMeterBase::set_meter_strip_name (const char * name)
520 {
521         meter_metric_area.set_name (name);
522         meter_ticks1_area.set_name (name);
523         meter_ticks2_area.set_name (name);
524 }
525
526 void
527 GainMeterBase::set_fader_name (const char * name)
528 {
529         gain_slider->set_name (name);
530 }
531
532 void
533 GainMeterBase::update_gain_sensitive ()
534 {
535         bool x = !(_amp->gain_control()->alist()->automation_state() & Play);
536         static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (x);
537 }
538
539 static MeterPoint
540 next_meter_point (MeterPoint mp)
541 {
542         switch (mp) {
543         case MeterInput:
544                 return MeterPreFader;
545                 break;
546
547         case MeterPreFader:
548                 return MeterPostFader;
549                 break;
550
551         case MeterPostFader:
552                 return MeterOutput;
553                 break;
554
555         case MeterOutput:
556                 return MeterCustom;
557                 break;
558
559         case MeterCustom:
560                 return MeterInput;
561                 break;
562         }
563
564         /*NOTREACHED*/
565         return MeterInput;
566 }
567
568 gint
569 GainMeterBase::meter_press(GdkEventButton* ev)
570 {
571         wait_for_release = false;
572
573         if (!_route) {
574                 return FALSE;
575         }
576
577         if (!ignore_toggle) {
578
579                 if (Keyboard::is_context_menu_event (ev)) {
580
581                         // no menu at this time.
582
583                 } else {
584
585                         if (Keyboard::is_button2_event(ev)) {
586
587                                 // Primary-button2 click is the midi binding click
588                                 // button2-click is "momentary"
589
590                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
591                                         wait_for_release = true;
592                                         old_meter_point = _route->meter_point ();
593                                 }
594                         }
595
596                         if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
597
598                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
599
600                                         /* Primary+Tertiary-click applies change to all routes */
601
602                                         _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
603
604
605                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
606
607                                         /* Primary-click: solo mix group.
608                                            NOTE: Primary-button2 is MIDI learn.
609                                         */
610
611                                         if (ev->button == 1) {
612                                                 set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
613                                         }
614
615                                 } else {
616
617                                         /* click: change just this route */
618
619                                         // XXX no undo yet
620
621                                         _route->set_meter_point (next_meter_point (_route->meter_point()));
622                                 }
623                         }
624                 }
625         }
626
627         return true;
628
629 }
630
631 gint
632 GainMeterBase::meter_release(GdkEventButton*)
633 {
634         if (!ignore_toggle) {
635                 if (wait_for_release) {
636                         wait_for_release = false;
637
638                         if (_route) {
639                                 set_meter_point (*_route, old_meter_point);
640                         }
641                 }
642         }
643
644         return true;
645 }
646
647 void
648 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
649 {
650         route.set_meter_point (mp);
651 }
652
653 void
654 GainMeterBase::set_route_group_meter_point (Route& route, MeterPoint mp)
655 {
656         RouteGroup* route_group;
657
658         if ((route_group = route.route_group ()) != 0) {
659                 route_group->foreach_route (boost::bind (&Route::set_meter_point, _1, mp, false));
660         } else {
661                 route.set_meter_point (mp);
662         }
663 }
664
665 void
666 GainMeterBase::meter_point_clicked ()
667 {
668         if (_route) {
669                 /* WHAT? */
670         }
671 }
672
673 bool
674 GainMeterBase::gain_slider_button_press (GdkEventButton* ev)
675 {
676         switch (ev->type) {
677         case GDK_BUTTON_PRESS:
678                 _amp->gain_control()->start_touch (_amp->session().transport_frame());
679                 break;
680         default:
681                 return false;
682         }
683
684         return false;
685 }
686
687 bool
688 GainMeterBase::gain_slider_button_release (GdkEventButton*)
689 {
690         _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
691         return false;
692 }
693
694 gint
695 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
696 {
697         if (ev->type == GDK_BUTTON_RELEASE) {
698                 return TRUE;
699         }
700
701         switch (ev->button) {
702                 case 1:
703                         gain_astate_menu.popup (1, ev->time);
704                         break;
705                 default:
706                         break;
707         }
708
709         return TRUE;
710 }
711
712 gint
713 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
714 {
715         if (ev->type == GDK_BUTTON_RELEASE) {
716                 return TRUE;
717         }
718
719         switch (ev->button) {
720         case 1:
721                 gain_astyle_menu.popup (1, ev->time);
722                 break;
723         default:
724                 break;
725         }
726         return TRUE;
727 }
728
729 string
730 GainMeterBase::astate_string (AutoState state)
731 {
732         return _astate_string (state, false);
733 }
734
735 string
736 GainMeterBase::short_astate_string (AutoState state)
737 {
738         return _astate_string (state, true);
739 }
740
741 string
742 GainMeterBase::_astate_string (AutoState state, bool shrt)
743 {
744         string sstr;
745
746         switch (state) {
747         case ARDOUR::Off:
748                 sstr = (shrt ? "M" : _("M"));
749                 break;
750         case Play:
751                 sstr = (shrt ? "P" : _("P"));
752                 break;
753         case Touch:
754                 sstr = (shrt ? "T" : _("T"));
755                 break;
756         case Write:
757                 sstr = (shrt ? "W" : _("W"));
758                 break;
759         }
760
761         return sstr;
762 }
763
764 string
765 GainMeterBase::astyle_string (AutoStyle style)
766 {
767         return _astyle_string (style, false);
768 }
769
770 string
771 GainMeterBase::short_astyle_string (AutoStyle style)
772 {
773         return _astyle_string (style, true);
774 }
775
776 string
777 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
778 {
779         if (style & Trim) {
780                 return _("Trim");
781         } else {
782                 /* XXX it might different in different languages */
783
784                 return (shrt ? _("Abs") : _("Abs"));
785         }
786 }
787
788 void
789 GainMeterBase::gain_automation_style_changed ()
790 {
791         switch (_width) {
792         case Wide:
793                 gain_automation_style_button.set_text (astyle_string(_amp->gain_control()->alist()->automation_style()));
794                 break;
795         case Narrow:
796                 gain_automation_style_button.set_text  (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
797                 break;
798         }
799 }
800
801 void
802 GainMeterBase::gain_automation_state_changed ()
803 {
804         ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
805
806         bool x;
807
808         switch (_width) {
809         case Wide:
810                 gain_automation_state_button.set_text (astate_string(_amp->gain_control()->alist()->automation_state()));
811                 break;
812         case Narrow:
813                 gain_automation_state_button.set_text (short_astate_string(_amp->gain_control()->alist()->automation_state()));
814                 break;
815         }
816
817         x = (_amp->gain_control()->alist()->automation_state() != ARDOUR::Off);
818
819         if (gain_automation_state_button.get_active() != x) {
820                 ignore_toggle = true;
821                 gain_automation_state_button.set_active (x);
822                 ignore_toggle = false;
823         }
824
825         update_gain_sensitive ();
826
827         /* start watching automation so that things move */
828
829         gain_watching.disconnect();
830
831         if (x) {
832                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
833         }
834 }
835
836 void
837 GainMeterBase::update_meters()
838 {
839         char buf[32];
840         float mpeak = level_meter->update_meters();
841
842         if (mpeak > max_peak) {
843                 max_peak = mpeak;
844                 if (mpeak <= -200.0f) {
845                         peak_display.set_label (_("-inf"));
846                 } else {
847                         snprintf (buf, sizeof(buf), "%.1f", mpeak);
848                         peak_display.set_label (buf);
849                 }
850         }
851         if (mpeak >= Config->get_meter_peak()) {
852                 peak_display.set_name ("MixerStripPeakDisplayPeak");
853         }
854 }
855
856 void GainMeterBase::color_handler(bool dpi)
857 {
858         setup_meters();
859 }
860
861 void
862 GainMeterBase::set_width (Width w, int len)
863 {
864         _width = w;
865         int meter_width = 5;
866         if (_width == Wide && _route && _route->shared_peak_meter()->input_streams().n_total() == 1) {
867                 meter_width = 10;
868         }
869         level_meter->setup_meters(len, meter_width);
870 }
871
872
873 void
874 GainMeterBase::on_theme_changed()
875 {
876 }
877
878 void
879 GainMeterBase::redraw_metrics()
880 {
881         meter_metric_area.queue_draw ();
882         meter_ticks1_area.queue_draw ();
883         meter_ticks2_area.queue_draw ();
884 }
885
886 GainMeter::GainMeter (Session* s, int fader_length)
887         : GainMeterBase (s, false, fader_length, 24)
888         , gain_display_box(true, 0)
889         , hbox(true, 2)
890 {
891         if (gain_display.get_parent()) {
892                 gain_display.get_parent()->remove (gain_display);
893         }
894         gain_display_box.pack_start (gain_display, true, true);
895
896         meter_metric_area.set_name ("AudioTrackMetrics");
897         meter_metric_area.set_size_request(25, 10);
898
899         gain_automation_style_button.set_name ("mixer strip button");
900         gain_automation_state_button.set_name ("mixer strip button");
901
902         ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
903         ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
904
905         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
906         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
907
908         gain_automation_state_button.set_size_request(15, 15);
909         gain_automation_style_button.set_size_request(15, 15);
910
911         fader_vbox = manage (new Gtk::VBox());
912         fader_vbox->set_spacing (0);
913         fader_vbox->pack_start (*gain_slider, true, true);
914
915         fader_alignment.set (0.5, 0.5, 0.0, 1.0);
916         fader_alignment.add (*fader_vbox);
917
918         hbox.pack_start (fader_alignment, true, true);
919
920         set_spacing (2);
921
922         pack_start (gain_display_box, Gtk::PACK_SHRINK);
923         pack_start (hbox, Gtk::PACK_SHRINK);
924
925         meter_alignment.set (0.5, 0.5, 0.0, 1.0);
926         meter_alignment.add (*level_meter);
927
928         meter_metric_area.signal_expose_event().connect (
929                 sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
930
931         meter_ticks1_area.set_size_request(3,-1);
932         meter_ticks2_area.set_size_request(3,-1);
933
934         meter_ticks1_area.signal_expose_event().connect (
935                         sigc::mem_fun(*this, &GainMeter::meter_ticks1_expose));
936         meter_ticks2_area.signal_expose_event().connect (
937                         sigc::mem_fun(*this, &GainMeter::meter_ticks2_expose));
938
939         meter_hbox.pack_start (meter_ticks1_area, false, false);
940         meter_hbox.pack_start (meter_alignment, true, true);
941         meter_hbox.pack_start (meter_ticks2_area, false, false);
942         meter_hbox.pack_start (meter_metric_area, false, false);
943 }
944
945 void
946 GainMeter::set_controls (boost::shared_ptr<Route> r,
947                          boost::shared_ptr<PeakMeter> meter,
948                          boost::shared_ptr<Amp> amp)
949 {
950         if (meter_hbox.get_parent()) {
951                 hbox.remove (meter_hbox);
952         }
953
954 //      if (gain_automation_state_button.get_parent()) {
955 //              fader_vbox->remove (gain_automation_state_button);
956 //      }
957
958         GainMeterBase::set_controls (r, meter, amp);
959
960         if (_meter) {
961                 _meter->ConfigurationChanged.connect (
962                         model_connections, invalidator (*this), boost::bind (&GainMeter::meter_configuration_changed, this, _1), gui_context()
963                         );
964                 _meter->TypeChanged.connect (
965                         model_connections, invalidator (*this), boost::bind (&GainMeter::meter_type_changed, this, _1), gui_context()
966                         );
967
968                 meter_configuration_changed (_meter->input_streams ());
969         }
970
971
972         /*
973            if we have a non-hidden route (ie. we're not the click or the auditioner),
974            pack some route-dependent stuff.
975         */
976
977         hbox.pack_start (meter_hbox, true, true);
978
979 //      if (r && !r->is_auditioner()) {
980 //              fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
981 //      }
982
983         hbox.show_all ();
984         setup_meters ();
985 }
986
987 int
988 GainMeter::get_gm_width ()
989 {
990         Gtk::Requisition sz;
991         hbox.size_request (sz);
992         return sz.width;
993 }
994
995 gint
996 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
997 {
998         return meter_expose_metrics(ev, _types, &meter_metric_area);
999 }
1000
1001 gint
1002 GainMeter::meter_ticks1_expose (GdkEventExpose *ev)
1003 {
1004         return meter_expose_ticks(ev, _types, &meter_ticks1_area);
1005 }
1006
1007 gint
1008 GainMeter::meter_ticks2_expose (GdkEventExpose *ev)
1009 {
1010         return meter_expose_ticks(ev, _types, &meter_ticks2_area);
1011 }
1012
1013 boost::shared_ptr<PBD::Controllable>
1014 GainMeterBase::get_controllable()
1015 {
1016         if (_amp) {
1017                 return _amp->gain_control();
1018         } else {
1019                 return boost::shared_ptr<PBD::Controllable>();
1020         }
1021 }
1022
1023 bool
1024 GainMeterBase::level_meter_button_press (GdkEventButton* ev)
1025 {
1026         return LevelMeterButtonPress (ev); /* EMIT SIGNAL */
1027 }
1028
1029 void
1030 GainMeter::meter_configuration_changed (ChanCount c)
1031 {
1032         int type = 0;
1033         _types.clear ();
1034
1035         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1036                 if (c.get (*i) > 0) {
1037                         _types.push_back (*i);
1038                         type |= 1 << (*i);
1039                 }
1040         }
1041
1042         if (_route
1043                         && boost::dynamic_pointer_cast<AudioTrack>(_route) == 0
1044                         && boost::dynamic_pointer_cast<MidiTrack>(_route) == 0
1045                         ) {
1046                 if (_route->active()) {
1047                         set_meter_strip_name ("AudioBusMetrics");
1048                 } else {
1049                         set_meter_strip_name ("AudioBusMetricsInactive");
1050                 }
1051         }
1052         else if (type == (1 << DataType::AUDIO)) {
1053                 if (!_route || _route->active()) {
1054                         set_meter_strip_name ("AudioTrackMetrics");
1055                 } else {
1056                         set_meter_strip_name ("AudioTrackMetricsInactive");
1057                 }
1058         }
1059         else if (type == (1 << DataType::MIDI)) {
1060                 if (!_route || _route->active()) {
1061                         set_meter_strip_name ("MidiTrackMetrics");
1062                 } else {
1063                         set_meter_strip_name ("MidiTrackMetricsInactive");
1064                 }
1065         } else {
1066                 if (!_route || _route->active()) {
1067                         set_meter_strip_name ("AudioMidiTrackMetrics");
1068                 } else {
1069                         set_meter_strip_name ("AudioMidiTrackMetricsInactive");
1070                 }
1071         }
1072         meter_clear_pattern_cache(); // XXX only once
1073 }
1074
1075 void
1076 GainMeter::meter_type_changed (MeterType t)
1077 {
1078         _route->set_meter_type(t);
1079 }