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