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