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