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