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