fix invalid casts in prev commit.
[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         gain_slider->create_patterns();
542 }
543
544 static MeterPoint
545 next_meter_point (MeterPoint mp)
546 {
547         switch (mp) {
548         case MeterInput:
549                 return MeterPreFader;
550                 break;
551
552         case MeterPreFader:
553                 return MeterPostFader;
554                 break;
555
556         case MeterPostFader:
557                 return MeterOutput;
558                 break;
559
560         case MeterOutput:
561                 return MeterCustom;
562                 break;
563
564         case MeterCustom:
565                 return MeterInput;
566                 break;
567         }
568
569         /*NOTREACHED*/
570         return MeterInput;
571 }
572
573 gint
574 GainMeterBase::meter_press(GdkEventButton* ev)
575 {
576         wait_for_release = false;
577
578         if (!_route) {
579                 return FALSE;
580         }
581
582         if (!ignore_toggle) {
583
584                 if (Keyboard::is_context_menu_event (ev)) {
585
586                         // no menu at this time.
587
588                 } else {
589
590                         if (Keyboard::is_button2_event(ev)) {
591
592                                 // Primary-button2 click is the midi binding click
593                                 // button2-click is "momentary"
594
595                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
596                                         wait_for_release = true;
597                                         old_meter_point = _route->meter_point ();
598                                 }
599                         }
600
601                         if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
602
603                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
604
605                                         /* Primary+Tertiary-click applies change to all routes */
606
607                                         _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
608
609
610                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
611
612                                         /* Primary-click: solo mix group.
613                                            NOTE: Primary-button2 is MIDI learn.
614                                         */
615
616                                         if (ev->button == 1) {
617                                                 set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
618                                         }
619
620                                 } else {
621
622                                         /* click: change just this route */
623
624                                         // XXX no undo yet
625
626                                         _route->set_meter_point (next_meter_point (_route->meter_point()));
627                                 }
628                         }
629                 }
630         }
631
632         return true;
633
634 }
635
636 gint
637 GainMeterBase::meter_release(GdkEventButton*)
638 {
639         if (!ignore_toggle) {
640                 if (wait_for_release) {
641                         wait_for_release = false;
642
643                         if (_route) {
644                                 set_meter_point (*_route, old_meter_point);
645                         }
646                 }
647         }
648
649         return true;
650 }
651
652 void
653 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
654 {
655         route.set_meter_point (mp);
656 }
657
658 void
659 GainMeterBase::set_route_group_meter_point (Route& route, MeterPoint mp)
660 {
661         RouteGroup* route_group;
662
663         if ((route_group = route.route_group ()) != 0) {
664                 route_group->foreach_route (boost::bind (&Route::set_meter_point, _1, mp, false));
665         } else {
666                 route.set_meter_point (mp);
667         }
668 }
669
670 void
671 GainMeterBase::meter_point_clicked ()
672 {
673         if (_route) {
674                 /* WHAT? */
675         }
676 }
677
678 bool
679 GainMeterBase::gain_slider_button_press (GdkEventButton* ev)
680 {
681         switch (ev->type) {
682         case GDK_BUTTON_PRESS:
683                 _amp->gain_control()->start_touch (_amp->session().transport_frame());
684                 break;
685         default:
686                 return false;
687         }
688
689         return false;
690 }
691
692 bool
693 GainMeterBase::gain_slider_button_release (GdkEventButton*)
694 {
695         _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
696         return false;
697 }
698
699 gint
700 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
701 {
702         if (ev->type == GDK_BUTTON_RELEASE) {
703                 return TRUE;
704         }
705
706         switch (ev->button) {
707                 case 1:
708                         gain_astate_menu.popup (1, ev->time);
709                         break;
710                 default:
711                         break;
712         }
713
714         return TRUE;
715 }
716
717 gint
718 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
719 {
720         if (ev->type == GDK_BUTTON_RELEASE) {
721                 return TRUE;
722         }
723
724         switch (ev->button) {
725         case 1:
726                 gain_astyle_menu.popup (1, ev->time);
727                 break;
728         default:
729                 break;
730         }
731         return TRUE;
732 }
733
734 string
735 GainMeterBase::astate_string (AutoState state)
736 {
737         return _astate_string (state, false);
738 }
739
740 string
741 GainMeterBase::short_astate_string (AutoState state)
742 {
743         return _astate_string (state, true);
744 }
745
746 string
747 GainMeterBase::_astate_string (AutoState state, bool shrt)
748 {
749         string sstr;
750
751         switch (state) {
752         case ARDOUR::Off:
753                 sstr = (shrt ? "M" : _("M"));
754                 break;
755         case Play:
756                 sstr = (shrt ? "P" : _("P"));
757                 break;
758         case Touch:
759                 sstr = (shrt ? "T" : _("T"));
760                 break;
761         case Write:
762                 sstr = (shrt ? "W" : _("W"));
763                 break;
764         }
765
766         return sstr;
767 }
768
769 string
770 GainMeterBase::astyle_string (AutoStyle style)
771 {
772         return _astyle_string (style, false);
773 }
774
775 string
776 GainMeterBase::short_astyle_string (AutoStyle style)
777 {
778         return _astyle_string (style, true);
779 }
780
781 string
782 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
783 {
784         if (style & Trim) {
785                 return _("Trim");
786         } else {
787                 /* XXX it might different in different languages */
788
789                 return (shrt ? _("Abs") : _("Abs"));
790         }
791 }
792
793 void
794 GainMeterBase::gain_automation_style_changed ()
795 {
796         switch (_width) {
797         case Wide:
798                 gain_automation_style_button.set_text (astyle_string(_amp->gain_control()->alist()->automation_style()));
799                 break;
800         case Narrow:
801                 gain_automation_style_button.set_text  (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
802                 break;
803         }
804 }
805
806 void
807 GainMeterBase::gain_automation_state_changed ()
808 {
809         ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
810
811         bool x;
812
813         switch (_width) {
814         case Wide:
815                 gain_automation_state_button.set_text (astate_string(_amp->gain_control()->alist()->automation_state()));
816                 break;
817         case Narrow:
818                 gain_automation_state_button.set_text (short_astate_string(_amp->gain_control()->alist()->automation_state()));
819                 break;
820         }
821
822         x = (_amp->gain_control()->alist()->automation_state() != ARDOUR::Off);
823
824         if (gain_automation_state_button.get_active() != x) {
825                 ignore_toggle = true;
826                 gain_automation_state_button.set_active (x);
827                 ignore_toggle = false;
828         }
829
830         update_gain_sensitive ();
831
832         /* start watching automation so that things move */
833
834         gain_watching.disconnect();
835
836         if (x) {
837                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
838         }
839 }
840
841 void
842 GainMeterBase::update_meters()
843 {
844         char buf[32];
845         float mpeak = level_meter->update_meters();
846
847         if (mpeak > max_peak) {
848                 max_peak = mpeak;
849                 if (mpeak <= -200.0f) {
850                         peak_display.set_label (_("-inf"));
851                 } else {
852                         snprintf (buf, sizeof(buf), "%.1f", mpeak);
853                         peak_display.set_label (buf);
854                 }
855
856                 if (mpeak >= 0.0f) {
857                         peak_display.set_name ("MixerStripPeakDisplayPeak");
858                 }
859         }
860 }
861
862 void GainMeterBase::color_handler(bool dpi)
863 {
864         color_changed = true;
865         dpi_changed = (dpi) ? true : false;
866         setup_meters();
867 }
868
869 void
870 GainMeterBase::set_width (Width w, int len)
871 {
872         _width = w;
873         level_meter->setup_meters (len);
874 }
875
876
877 void
878 GainMeterBase::on_theme_changed()
879 {
880         style_changed = true;
881 }
882
883 GainMeter::GainMeter (Session* s, int fader_length)
884         : GainMeterBase (s, slider, slider_desensitised, false, fader_length)
885         , gain_display_box(true, 0)
886         , hbox(true, 2)
887 {
888 //      gain_display_box.pack_start (gain_display, true, true);
889
890         meter_metric_area.set_name ("AudioTrackMetrics");
891         set_size_request_to_display_given_text (meter_metric_area, "-127", 0, 0);
892
893         gain_automation_style_button.set_name ("mixer strip button");
894         gain_automation_state_button.set_name ("mixer strip button");
895
896         ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
897         ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
898
899         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
900         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
901
902         gain_automation_state_button.set_size_request(15, 15);
903         gain_automation_style_button.set_size_request(15, 15);
904
905         fader_vbox = manage (new Gtk::VBox());
906         fader_vbox->set_spacing (0);
907         fader_vbox->pack_start (*gain_slider, true, true);
908
909         fader_alignment.set (0.5, 0.5, 0.0, 1.0);
910         fader_alignment.add (*fader_vbox);
911
912         hbox.pack_start (fader_alignment, true, true);
913
914         set_spacing (2);
915
916         pack_start (gain_display_box, Gtk::PACK_SHRINK);
917         pack_start (hbox, Gtk::PACK_SHRINK);
918
919         meter_alignment.set (0.5, 0.5, 0.0, 1.0);
920         meter_alignment.add (*level_meter);
921
922         meter_metric_area.signal_expose_event().connect (
923                 sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
924 }
925
926 void
927 GainMeter::set_controls (boost::shared_ptr<Route> r,
928                          boost::shared_ptr<PeakMeter> meter,
929                          boost::shared_ptr<Amp> amp)
930 {
931         if (meter_alignment.get_parent()) {
932                 hbox.remove (meter_alignment);
933         }
934
935 //      if (peak_display.get_parent()) {
936 //              gain_display_box.remove (peak_display);
937 //      }
938
939 //      if (gain_automation_state_button.get_parent()) {
940 //              fader_vbox->remove (gain_automation_state_button);
941 //      }
942
943         GainMeterBase::set_controls (r, meter, amp);
944
945         if (_meter) {
946                 _meter->ConfigurationChanged.connect (
947                         model_connections, invalidator (*this), boost::bind (&GainMeter::meter_configuration_changed, this, _1), gui_context()
948                         );
949
950                 meter_configuration_changed (_meter->input_streams ());
951         }
952
953
954         /*
955            if we have a non-hidden route (ie. we're not the click or the auditioner),
956            pack some route-dependent stuff.
957         */
958
959 //      gain_display_box.pack_end (peak_display, true, true);
960         hbox.pack_start (meter_alignment, true, true);
961
962 //      if (r && !r->is_hidden()) {
963 //              fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
964 //      }
965
966         setup_meters ();
967         hbox.show_all ();
968 }
969
970 int
971 GainMeter::get_gm_width ()
972 {
973         Gtk::Requisition sz;
974         hbox.size_request (sz);
975         return sz.width;
976 }
977
978 cairo_pattern_t*
979 GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
980 {
981         Glib::RefPtr<Gdk::Window> win (w.get_window());
982
983         gint width, height;
984         win->get_size (width, height);
985
986         cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
987         cairo_t* cr = cairo_create (surface);
988         PangoLayout* layout = gtk_widget_create_pango_layout (w.gobj(), "");
989
990         cairo_move_to (cr, 0, 0);
991         cairo_rectangle (cr, 0, 0, width, height);
992         {
993                 Gdk::Color c = w.get_style()->get_bg (Gtk::STATE_NORMAL);
994                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
995         }
996         cairo_fill (cr);
997
998         for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
999
1000                 Gdk::Color c;
1001
1002                 if (types.size() > 1) {
1003                         /* we're overlaying more than 1 set of marks, so use different colours */
1004                         Gdk::Color c;
1005                         switch (*i) {
1006                         case DataType::AUDIO:
1007                                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
1008                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
1009                                 break;
1010                         case DataType::MIDI:
1011                                 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
1012                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
1013                                 break;
1014                         }
1015                 } else {
1016                         c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
1017                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
1018                 }
1019
1020                 vector<int> points;
1021
1022                 switch (*i) {
1023                 case DataType::AUDIO:
1024                         points.push_back (-50);
1025                         points.push_back (-40);
1026                         points.push_back (-30);
1027                         points.push_back (-20);
1028                         points.push_back (-10);
1029                         points.push_back (-3);
1030                         points.push_back (0);
1031                         points.push_back (4);
1032                         break;
1033
1034                 case DataType::MIDI:
1035                         points.push_back (0);
1036                         if (types.size() == 1) {
1037                                 points.push_back (32);
1038                         } else {
1039                                 /* tweak so as not to overlay the -30dB mark */
1040                                 points.push_back (48);
1041                         }
1042                         points.push_back (64);
1043                         points.push_back (96);
1044                         points.push_back (127);
1045                         break;
1046                 }
1047
1048                 char buf[32];
1049
1050                 for (vector<int>::const_iterator j = points.begin(); j != points.end(); ++j) {
1051
1052                         float fraction = 0;
1053                         switch (*i) {
1054                         case DataType::AUDIO:
1055                                 fraction = log_meter (*j);
1056                                 break;
1057                         case DataType::MIDI:
1058                                 fraction = *j / 127.0;
1059                                 break;
1060                         }
1061
1062                         gint const pos = height - (gint) floor (height * fraction);
1063
1064                         cairo_set_line_width (cr, 1.0);
1065                         cairo_move_to (cr, 0, pos);
1066                         cairo_line_to (cr, 4, pos);
1067                         cairo_stroke (cr);
1068                         
1069                         snprintf (buf, sizeof (buf), "%d", abs (*j));
1070                         pango_layout_set_text (layout, buf, strlen (buf));
1071
1072                         /* we want logical extents, not ink extents here */
1073
1074                         int tw, th;
1075                         pango_layout_get_pixel_size (layout, &tw, &th);
1076
1077                         int p = pos - (th / 2);
1078                         p = min (p, height - th);
1079                         p = max (p, 0);
1080
1081                         cairo_move_to (cr, 6, p);
1082                         pango_cairo_show_layout (cr, layout);
1083                 }
1084         }
1085
1086         cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
1087         MetricPatterns::iterator p;
1088
1089         if ((p = metric_patterns.find (w.get_name())) != metric_patterns.end()) {
1090                 cairo_pattern_destroy (p->second);
1091         }
1092
1093         metric_patterns[w.get_name()] = pattern;
1094         
1095         cairo_destroy (cr);
1096         cairo_surface_destroy (surface);
1097         g_object_unref (layout);
1098
1099         return pattern;
1100 }
1101
1102 gint
1103 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
1104 {
1105         Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
1106         cairo_t* cr;
1107
1108         cr = gdk_cairo_create (win->gobj());
1109         
1110         /* clip to expose area */
1111
1112         gdk_cairo_rectangle (cr, &ev->area);
1113         cairo_clip (cr);
1114
1115         cairo_pattern_t* pattern;
1116         MetricPatterns::iterator i = metric_patterns.find (meter_metric_area.get_name());
1117
1118         if (i == metric_patterns.end() || style_changed || dpi_changed) {
1119                 pattern = render_metrics (meter_metric_area, _types);
1120         } else {
1121                 pattern = i->second;
1122         }
1123
1124         cairo_move_to (cr, 0, 0);
1125         cairo_set_source (cr, pattern);
1126
1127         gint width, height;
1128         win->get_size (width, height);
1129
1130         cairo_rectangle (cr, 0, 0, width, height);
1131         cairo_fill (cr);
1132
1133         style_changed = false;
1134         dpi_changed = false;
1135
1136         cairo_destroy (cr);
1137
1138         return true;
1139 }
1140
1141 boost::shared_ptr<PBD::Controllable>
1142 GainMeterBase::get_controllable()
1143 {
1144         if (_amp) {
1145                 return _amp->gain_control();
1146         } else {
1147                 return boost::shared_ptr<PBD::Controllable>();
1148         }
1149 }
1150
1151 bool
1152 GainMeterBase::level_meter_button_press (GdkEventButton* ev)
1153 {
1154         return LevelMeterButtonPress (ev); /* EMIT SIGNAL */
1155 }
1156
1157 void
1158 GainMeter::meter_configuration_changed (ChanCount c)
1159 {
1160         _types.clear ();
1161
1162         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1163                 if (c.get (*i) > 0) {
1164                         _types.push_back (*i);
1165                 }
1166         }
1167
1168         style_changed = true;
1169         meter_metric_area.queue_draw ();
1170 }
1171