Reinstate command line option to hide splash screen.
[ardour.git] / gtk2_ardour / gain_meter.cc
index 1b251ba3182c3168d3d45ad903902f3618f62ead..0324f3d93cfe39eb232b5e922e950d6e9bb2e5ee 100644 (file)
 #include "ardour/session.h"
 #include "ardour/session_route.h"
 #include "ardour/dB.h"
+#include "ardour/utils.h"
 
 #include <gtkmm/style.h>
 #include <gdkmm/color.h>
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/fastmeter.h>
-#include <gtkmm2ext/stop_signal.h>
 #include <gtkmm2ext/barcontroller.h>
 #include <gtkmm2ext/gtk_ui.h>
 #include "midi++/manager.h"
 
 #include "ardour_ui.h"
 #include "gain_meter.h"
-#include "utils.h"
+#include "global_signals.h"
 #include "logmeter.h"
 #include "gui_thread.h"
 #include "keyboard.h"
 #include "public_editor.h"
+#include "utils.h"
 
 #include "ardour/session.h"
 #include "ardour/route.h"
@@ -56,13 +57,13 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace Gtkmm2ext;
 using namespace Gtk;
-using namespace sigc;
 using namespace std;
+using Gtkmm2ext::Keyboard;
 
 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
 
-map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
+GainMeter::MetricPatterns GainMeter::metric_patterns;
 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
 
 
@@ -74,53 +75,57 @@ GainMeter::setup_slider_pix ()
        }
 }
 
-GainMeterBase::GainMeterBase (Session& s,
+GainMeterBase::GainMeterBase (Session* s,
                              const Glib::RefPtr<Gdk::Pixbuf>& pix,
-                             bool horizontal)
-       : _session (s)
-         // 0.781787 is the value needed for gain to be set to 0.
-       , gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1)
+                             bool horizontal,
+                             int fader_length)
+       : gain_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0.0, 1.0, 0.01, 0.1)
        , gain_automation_style_button ("")
        , gain_automation_state_button ("")
+       , style_changed (false)
        , dpi_changed (false)
-       , _is_midi (false)
+       , _data_type (DataType::AUDIO)
 
 {
        using namespace Menu_Helpers;
 
+       set_session (s);
+
        ignore_toggle = false;
        meter_menu = 0;
        next_release_selects = false;
-       style_changed = true;
        _width = Wide;
 
        if (horizontal) {
                gain_slider = manage (new HSliderController (pix,
                                                             &gain_adjustment,
+                                                            fader_length,
                                                             false));
        } else {
                gain_slider = manage (new VSliderController (pix,
                                                             &gain_adjustment,
+                                                            fader_length,
                                                             false));
        }
 
        level_meter = new LevelMeter(_session);
 
-       gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
-       gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
+       level_meter->ButtonPress.connect_same_thread (_level_meter_connection, boost::bind (&GainMeterBase::level_meter_button_press, this, _1));
+       meter_metric_area.signal_button_press_event().connect (sigc::mem_fun (*this, &GainMeterBase::level_meter_button_press));
+       meter_metric_area.add_events (Gdk::BUTTON_PRESS_MASK);
+
+       gain_slider->signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_press));
+       gain_slider->signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_release));
        gain_slider->set_name ("GainFader");
 
        gain_display.set_name ("MixerStripGainDisplay");
-       gain_display.set_has_frame (false);
        set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
-       gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
-       gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
-       gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
+       gain_display.signal_activate().connect (sigc::mem_fun (*this, &GainMeter::gain_activated));
+       gain_display.signal_focus_in_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
+       gain_display.signal_focus_out_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
 
        peak_display.set_name ("MixerStripPeakDisplay");
-//     peak_display.set_has_frame (false);
-//     peak_display.set_editable (false);
-       set_size_request_to_display_given_text  (peak_display, "-80.g", 2, 6); /* note the descender */
+       set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
        max_peak = minus_infinity();
        peak_display.set_label (_("-inf"));
        peak_display.unset_flags (Gtk::CAN_FOCUS);
@@ -128,8 +133,8 @@ GainMeterBase::GainMeterBase (Session& s,
        gain_automation_style_button.set_name ("MixerAutomationModeButton");
        gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
 
-       ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
-       ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
+       ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
+       ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
 
        gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
        gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
@@ -143,16 +148,16 @@ GainMeterBase::GainMeterBase (Session& s,
        gain_astate_menu.set_name ("ArdourContextMenu");
        gain_astyle_menu.set_name ("ArdourContextMenu");
 
-       gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeterBase::gain_adjusted));
-       peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeterBase::peak_button_release), false);
-       gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeterBase::gain_key_press), false);
+       gain_adjustment.signal_value_changed().connect (sigc::mem_fun(*this, &GainMeterBase::gain_adjusted));
+       peak_display.signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeterBase::peak_button_release), false);
+       gain_display.signal_key_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_key_press), false);
 
-       ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_peak_display));
-       ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_group_peak_display));
+       ResetAllPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_peak_display));
+       ResetGroupPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_group_peak_display));
 
-       UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeterBase::on_theme_changed));
-       ColorsChanged.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), false));
-       DPIReset.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), true));
+       UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &GainMeterBase::on_theme_changed));
+       ColorsChanged.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), false));
+       DPIReset.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), true));
 }
 
 GainMeterBase::~GainMeterBase ()
@@ -167,6 +172,7 @@ GainMeterBase::set_controls (boost::shared_ptr<Route> r,
                             boost::shared_ptr<Amp> amp)
 {
        connections.clear ();
+       model_connections.drop_connections ();
 
        if (!pm && !amp) {
                level_meter->set_meter (0);
@@ -184,57 +190,87 @@ GainMeterBase::set_controls (boost::shared_ptr<Route> r,
        level_meter->set_meter (pm.get());
        gain_slider->set_controllable (amp->gain_control());
 
-       if (!_route || _route->output()->n_ports().n_midi() == 0) {
-               _is_midi = false;
-               gain_adjustment.set_lower (0.0);
-               gain_adjustment.set_upper (1.0);
-               gain_adjustment.set_step_increment (0.01);
-               gain_adjustment.set_page_increment (0.1);
-       } else {
-               _is_midi = true;
-               gain_adjustment.set_lower (0.0);
-               gain_adjustment.set_upper (2.0);
-               gain_adjustment.set_step_increment (0.05);
-               gain_adjustment.set_page_increment (0.1);
+       if (amp) {
+               amp->ConfigurationChanged.connect (
+                       model_connections, invalidator (*this), ui_bind (&GainMeterBase::setup_gain_adjustment, this), gui_context ()
+                       );
        }
 
+       setup_gain_adjustment ();
+
        if (!_route || !_route->is_hidden()) {
 
                using namespace Menu_Helpers;
 
                gain_astate_menu.items().clear ();
 
-               gain_astate_menu.items().push_back (MenuElem (_("Manual"),
-                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+               gain_astate_menu.items().push_back (MenuElem (S_("Automation|Manual"),
+                                                             sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
                                                                    Evoral::Parameter(GainAutomation), (AutoState) Off)));
                gain_astate_menu.items().push_back (MenuElem (_("Play"),
-                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                             sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
                                                                    Evoral::Parameter(GainAutomation), (AutoState) Play)));
                gain_astate_menu.items().push_back (MenuElem (_("Write"),
-                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                             sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
                                                                    Evoral::Parameter(GainAutomation), (AutoState) Write)));
                gain_astate_menu.items().push_back (MenuElem (_("Touch"),
-                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                             sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
                                                                    Evoral::Parameter(GainAutomation), (AutoState) Touch)));
 
-               connections.push_back (gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
-               connections.push_back (gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
+               connections.push_back (gain_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
+               connections.push_back (gain_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
 
                boost::shared_ptr<AutomationControl> gc = amp->gain_control();
 
-               connections.push_back (gc->alist()->automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed)));
-               connections.push_back (gc->alist()->automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed)));
+               gc->alist()->automation_state_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_state_changed, this), gui_context());
+               gc->alist()->automation_style_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_style_changed, this), gui_context());
 
                gain_automation_state_changed ();
        }
 
-       connections.push_back (amp->gain_control()->Changed.connect (mem_fun (*this, &GainMeterBase::gain_changed)));
+       amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
 
        gain_changed ();
        show_gain ();
        update_gain_sensitive ();
 }
 
+void
+GainMeterBase::setup_gain_adjustment ()
+{
+       if (!_amp) {
+               return;
+       }
+
+       if (_previous_amp_output_streams == _amp->output_streams ()) {
+               return;
+       }
+
+       ignore_toggle = true;
+
+       if (_amp->output_streams().n_midi() == 0) {
+               _data_type = DataType::AUDIO;
+               gain_adjustment.set_lower (0.0);
+               gain_adjustment.set_upper (1.0);
+               gain_adjustment.set_step_increment (0.01);
+               gain_adjustment.set_page_increment (0.1);
+               gain_slider->set_default_value (gain_to_slider_position (1));
+       } else {
+               _data_type = DataType::MIDI;
+               gain_adjustment.set_lower (0.0);
+               gain_adjustment.set_upper (2.0);
+               gain_adjustment.set_step_increment (0.05);
+               gain_adjustment.set_page_increment (0.1);
+               gain_slider->set_default_value (1);
+       }
+
+       ignore_toggle = false;
+
+       effective_gain_display ();
+       
+       _previous_amp_output_streams = _amp->output_streams ();
+}
+
 void
 GainMeterBase::hide_all_meters ()
 {
@@ -315,7 +351,7 @@ GainMeterBase::reset_group_peak_display (RouteGroup* group)
 {
        if (_route && group == _route->route_group()) {
                reset_peak_display ();
-               }
+       }
 }
 
 void
@@ -376,14 +412,17 @@ GainMeterBase::show_gain ()
 
        float v = gain_adjustment.get_value();
 
-       if (!_is_midi) {
+       switch (_data_type) {
+       case DataType::AUDIO:
                if (v == 0.0) {
                        strcpy (buf, _("-inf"));
                } else {
-                       snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain (v)));
+                       snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (v, Config->get_max_gain())));
                }
-       } else {
+               break;
+       case DataType::MIDI:
                snprintf (buf, sizeof (buf), "%.1f", v);
+               break;
        }
 
        gain_display.set_text (buf);
@@ -392,17 +431,22 @@ GainMeterBase::show_gain ()
 void
 GainMeterBase::gain_adjusted ()
 {
+       gain_t value = 0;
+
+       switch (_data_type) {
+       case DataType::AUDIO:
+               value = slider_position_to_gain_with_max (gain_adjustment.get_value(), Config->get_max_gain());
+               break;
+       case DataType::MIDI:
+               value = gain_adjustment.get_value ();
+               break;
+       }
+       
        if (!ignore_toggle) {
-               if (_route) {
-                       if (_route->amp() == _amp) {
-                               if (_is_midi) {
-                                       _route->set_gain (gain_adjustment.get_value(), this);
-                               } else {
-                                       _route->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
-                               }
-                       } else {
-                               _amp->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
-                       }
+               if (_route && _route->amp() == _amp) {
+                       _route->set_gain (value, this);
+               } else {
+                       _amp->set_gain (value, this);
                }
        }
 
@@ -412,18 +456,17 @@ GainMeterBase::gain_adjusted ()
 void
 GainMeterBase::effective_gain_display ()
 {
-       gfloat value;
+       float value = 0.0;
 
-       if (!_route || _route->output()->n_ports().n_midi() == 0) {
-               value = gain_to_slider_position (_amp->gain());
-       } else {
+       switch (_data_type) {
+       case DataType::AUDIO:
+               value = gain_to_slider_position_with_max (_amp->gain(), Config->get_max_gain());
+               break;
+       case DataType::MIDI:
                value = _amp->gain ();
+               break;
        }
 
-       //cerr << this << " for " << _io->name() << " EGAIN = " << value
-       //              << " AGAIN = " << gain_adjustment.get_value () << endl;
-       // stacktrace (cerr, 20);
-
        if (gain_adjustment.get_value() != value) {
                ignore_toggle = true;
                gain_adjustment.set_value (value);
@@ -434,7 +477,7 @@ GainMeterBase::effective_gain_display ()
 void
 GainMeterBase::gain_changed ()
 {
-       Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeterBase::effective_gain_display));
+       Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
 }
 
 void
@@ -446,7 +489,10 @@ GainMeterBase::set_meter_strip_name (const char * name)
 void
 GainMeterBase::set_fader_name (const char * name)
 {
-       gain_slider->set_name (name);
+        uint32_t rgb_active = rgba_from_style (name, 0xff, 0, 0xff, 0, "bg", STATE_ACTIVE, false);
+        uint32_t rgb_normal = rgba_from_style (name, 0xff, 0xff, 0, 0, "bg", STATE_NORMAL, false);
+
+       gain_slider->set_border_colors (rgb_normal, rgb_active);
 }
 
 void
@@ -469,9 +515,18 @@ next_meter_point (MeterPoint mp)
                break;
 
        case MeterPostFader:
+               return MeterOutput;
+               break;
+
+       case MeterOutput:
+               return MeterCustom;
+               break;
+
+       case MeterCustom:
                return MeterInput;
                break;
        }
+
        /*NOTREACHED*/
        return MeterInput;
 }
@@ -510,12 +565,7 @@ GainMeterBase::meter_press(GdkEventButton* ev)
 
                                        /* Primary+Tertiary-click applies change to all routes */
 
-                                       _session.begin_reversible_command (_("meter point change"));
-                                        Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
-                                       _session.foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
-                                        cmd->mark();
-                                       _session.add_command (cmd);
-                                       _session.commit_reversible_command ();
+                                       _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
 
 
                                } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
@@ -525,12 +575,7 @@ GainMeterBase::meter_press(GdkEventButton* ev)
                                        */
 
                                        if (ev->button == 1) {
-                                               _session.begin_reversible_command (_("meter point change"));
-                                               Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
-                                               set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
-                                               cmd->mark();
-                                               _session.add_command (cmd);
-                                               _session.commit_reversible_command ();
+                                               set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
                                        }
 
                                } else {
@@ -539,7 +584,7 @@ GainMeterBase::meter_press(GdkEventButton* ev)
 
                                        // XXX no undo yet
 
-                                       _route->set_meter_point (next_meter_point (_route->meter_point()), this);
+                                       _route->set_meter_point (next_meter_point (_route->meter_point()));
                                }
                        }
                }
@@ -552,8 +597,8 @@ GainMeterBase::meter_press(GdkEventButton* ev)
 gint
 GainMeterBase::meter_release(GdkEventButton*)
 {
-       if(!ignore_toggle){
-               if (wait_for_release){
+       if (!ignore_toggle) {
+               if (wait_for_release) {
                        wait_for_release = false;
 
                        if (_route) {
@@ -568,18 +613,18 @@ GainMeterBase::meter_release(GdkEventButton*)
 void
 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
 {
-       route.set_meter_point (mp, this);
+       route.set_meter_point (mp);
 }
 
 void
-GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
+GainMeterBase::set_route_group_meter_point (Route& route, MeterPoint mp)
 {
-       RouteGroup* mix_group;
+       RouteGroup* route_group;
 
-       if((mix_group = route.route_group()) != 0){
-               mix_group->apply (&Route::set_meter_point, mp, this);
+       if ((route_group = route.route_group ()) != 0) {
+               route_group->foreach_route (boost::bind (&Route::set_meter_point, _1, mp, false));
        } else {
-               route.set_meter_point (mp, this);
+               route.set_meter_point (mp);
        }
 }
 
@@ -591,18 +636,25 @@ GainMeterBase::meter_point_clicked ()
        }
 }
 
-gint
-GainMeterBase::start_gain_touch (GdkEventButton*)
+bool
+GainMeterBase::gain_slider_button_press (GdkEventButton* ev)
 {
-       _amp->gain_control()->start_touch ();
-       return FALSE;
+       switch (ev->type) {
+       case GDK_BUTTON_PRESS:
+               _amp->gain_control()->start_touch (_amp->session().transport_frame());
+               break;
+       default:
+               return false;
+       }
+
+       return true;
 }
 
-gint
-GainMeterBase::end_gain_touch (GdkEventButton*)
+bool
+GainMeterBase::gain_slider_button_release (GdkEventButton*)
 {
-       _amp->gain_control()->stop_touch ();
-       return FALSE;
+       _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
+       return true;
 }
 
 gint
@@ -715,7 +767,7 @@ GainMeterBase::gain_automation_style_changed ()
 void
 GainMeterBase::gain_automation_state_changed ()
 {
-       ENSURE_GUI_THREAD(mem_fun(*this, &GainMeterBase::gain_automation_state_changed));
+       ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
 
        bool x;
 
@@ -743,7 +795,7 @@ GainMeterBase::gain_automation_state_changed ()
        gain_watching.disconnect();
 
        if (x) {
-               gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeterBase::effective_gain_display));
+               gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
        }
 }
 
@@ -789,21 +841,21 @@ GainMeterBase::on_theme_changed()
        style_changed = true;
 }
 
-GainMeter::GainMeter (Session& s)
-       : GainMeterBase (s, slider, false)
+GainMeter::GainMeter (Session* s, int fader_length)
+       : GainMeterBase (s, slider, false, fader_length)
+       , gain_display_box(true, 0)
+       , hbox(true, 2)
 {
-       gain_display_box.set_homogeneous (true);
-       gain_display_box.set_spacing (2);
        gain_display_box.pack_start (gain_display, true, true);
 
        meter_metric_area.set_name ("AudioTrackMetrics");
-       set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
+       set_size_request_to_display_given_text (meter_metric_area, "-127", 0, 0);
 
        gain_automation_style_button.set_name ("MixerAutomationModeButton");
        gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
 
-       ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
-       ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
+       ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
+       ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
 
        gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
        gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
@@ -811,22 +863,25 @@ GainMeter::GainMeter (Session& s)
        gain_automation_state_button.set_size_request(15, 15);
        gain_automation_style_button.set_size_request(15, 15);
 
-       HBox* fader_centering_box = manage (new HBox);
-       fader_centering_box->pack_start (*gain_slider, true, false);
-
        fader_vbox = manage (new Gtk::VBox());
        fader_vbox->set_spacing (0);
-       fader_vbox->pack_start (*fader_centering_box, false, false, 0);
+       fader_vbox->pack_start (*gain_slider, true, true);
 
-       hbox.set_spacing (2);
-       hbox.pack_start (*fader_vbox, true, true);
+       fader_alignment.set (0.5, 0.5, 0.0, 1.0);
+       fader_alignment.add (*fader_vbox);
+
+       hbox.pack_start (fader_alignment, true, true);
 
        set_spacing (2);
 
        pack_start (gain_display_box, Gtk::PACK_SHRINK);
        pack_start (hbox, Gtk::PACK_SHRINK);
 
-       meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
+       meter_alignment.set (0.5, 0.5, 0.0, 1.0);
+       meter_alignment.add (*level_meter);
+
+       meter_metric_area.signal_expose_event().connect (
+               sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
 }
 
 void
@@ -834,8 +889,8 @@ GainMeter::set_controls (boost::shared_ptr<Route> r,
                         boost::shared_ptr<PeakMeter> meter,
                         boost::shared_ptr<Amp> amp)
 {
-       if (level_meter->get_parent()) {
-               hbox.remove (*level_meter);
+       if (meter_alignment.get_parent()) {
+               hbox.remove (meter_alignment);
        }
 
        if (peak_display.get_parent()) {
@@ -848,15 +903,24 @@ GainMeter::set_controls (boost::shared_ptr<Route> r,
 
        GainMeterBase::set_controls (r, meter, amp);
 
+       if (_meter) {
+               _meter->ConfigurationChanged.connect (
+                       model_connections, invalidator (*this), ui_bind (&GainMeter::meter_configuration_changed, this, _1), gui_context()
+                       );
+
+               meter_configuration_changed (_meter->input_streams ());
+       }
+
+
        /*
           if we have a non-hidden route (ie. we're not the click or the auditioner),
           pack some route-dependent stuff.
        */
 
        gain_display_box.pack_end (peak_display, true, true);
-       hbox.pack_end (*level_meter, true, true);
+       hbox.pack_start (meter_alignment, true, true);
 
-       if (!r->is_hidden()) {
+       if (r && !r->is_hidden()) {
                fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
        }
 
@@ -872,79 +936,166 @@ GainMeter::get_gm_width ()
        return sz.width;
 }
 
-Glib::RefPtr<Gdk::Pixmap>
-GainMeter::render_metrics (Gtk::Widget& w)
+cairo_pattern_t*
+GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
 {
        Glib::RefPtr<Gdk::Window> win (w.get_window());
-       Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
-       Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
-       gint width, height;
-       int  db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
-       char buf[32];
 
+       gint width, height;
        win->get_size (width, height);
 
-       Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
+       cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
+       cairo_t* cr = cairo_create (surface);
+       PangoLayout* layout = gtk_widget_create_pango_layout (w.gobj(), "");
+
+       cairo_move_to (cr, 0, 0);
+       cairo_rectangle (cr, 0, 0, width, height);
+       {
+               Gdk::Color c = w.get_style()->get_bg (Gtk::STATE_NORMAL);
+               cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
+       }
+       cairo_fill (cr);
+
+       for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
+
+               Gdk::Color c;
+
+               if (types.size() > 1) {
+                       /* we're overlaying more than 1 set of marks, so use different colours */
+                       Gdk::Color c;
+                       switch (*i) {
+                       case DataType::AUDIO:
+                               c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
+                               cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
+                               break;
+                       case DataType::MIDI:
+                               c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
+                               cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
+                               break;
+                       }
+               } else {
+                       c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
+                       cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
+               }
+
+               vector<int> points;
+
+               switch (*i) {
+               case DataType::AUDIO:
+                       points.push_back (-50);
+                       points.push_back (-40);
+                       points.push_back (-30);
+                       points.push_back (-20);
+                       points.push_back (-10);
+                       points.push_back (-3);
+                       points.push_back (0);
+                       points.push_back (4);
+                       break;
+
+               case DataType::MIDI:
+                       points.push_back (0);
+                       if (types.size() == 1) {
+                               points.push_back (32);
+                       } else {
+                               /* tweak so as not to overlay the -30dB mark */
+                               points.push_back (48);
+                       }
+                       points.push_back (64);
+                       points.push_back (96);
+                       points.push_back (127);
+                       break;
+               }
 
-       metric_pixmaps[w.get_name()] = pixmap;
+               char buf[32];
 
-       pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
+               for (vector<int>::const_iterator j = points.begin(); j != points.end(); ++j) {
 
-       Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
+                       float fraction = 0;
+                       switch (*i) {
+                       case DataType::AUDIO:
+                               fraction = log_meter (*j);
+                               break;
+                       case DataType::MIDI:
+                               fraction = *j / 127.0;
+                               break;
+                       }
+
+                       gint const pos = height - (gint) floor (height * fraction);
 
-       for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
+                       cairo_set_line_width (cr, 1.0);
+                       cairo_move_to (cr, 0, pos);
+                       cairo_line_to (cr, 4, pos);
+                       cairo_stroke (cr);
+                       
+                       snprintf (buf, sizeof (buf), "%d", abs (*j));
+                       pango_layout_set_text (layout, buf, strlen (buf));
 
-               float fraction = log_meter (db_points[i]);
-               gint pos = height - (gint) floor (height * fraction);
+                       /* we want logical extents, not ink extents here */
 
-               snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
+                       int tw, th;
+                       pango_layout_get_pixel_size (layout, &tw, &th);
 
-               layout->set_text (buf);
+                       int p = pos - (th / 2);
+                       p = min (p, height - th);
+                       p = max (p, 0);
 
-               /* we want logical extents, not ink extents here */
+                       cairo_move_to (cr, 6, p);
+                       pango_cairo_show_layout (cr, layout);
+               }
+       }
 
-               int width, height;
-               layout->get_pixel_size (width, height);
+       cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
+       MetricPatterns::iterator p;
 
-               pixmap->draw_line (fg_gc, 0, pos, 4, pos);
-               pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
+       if ((p = metric_patterns.find (w.get_name())) != metric_patterns.end()) {
+               cairo_pattern_destroy (p->second);
        }
 
-       return pixmap;
+       metric_patterns[w.get_name()] = pattern;
+       
+       cairo_destroy (cr);
+       cairo_surface_destroy (surface);
+       g_object_unref (layout);
+
+       return pattern;
 }
 
 gint
 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
 {
-       static Glib::RefPtr<Gtk::Style> meter_style;
-       if (style_changed) {
-               meter_style = meter_metric_area.get_style();
-       }
        Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
-       Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
-       GdkRectangle base_rect;
-       GdkRectangle draw_rect;
-       gint width, height;
+       cairo_t* cr;
 
-       win->get_size (width, height);
+       cr = gdk_cairo_create (win->gobj());
+       
+       /* clip to expose area */
 
-       base_rect.width = width;
-       base_rect.height = height;
-       base_rect.x = 0;
-       base_rect.y = 0;
+       gdk_cairo_rectangle (cr, &ev->area);
+       cairo_clip (cr);
 
-       Glib::RefPtr<Gdk::Pixmap> pixmap;
-       std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
+       cairo_pattern_t* pattern;
+       MetricPatterns::iterator i = metric_patterns.find (meter_metric_area.get_name());
 
-       if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
-               pixmap = render_metrics (meter_metric_area);
+       if (i == metric_patterns.end() || style_changed || dpi_changed) {
+               pattern = render_metrics (meter_metric_area, _types);
        } else {
-               pixmap = i->second;
+               pattern = i->second;
        }
 
-       gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
-       win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
+       cairo_move_to (cr, 0, 0);
+       cairo_set_source (cr, pattern);
+
+       gint width, height;
+       win->get_size (width, height);
+
+       cairo_rectangle (cr, 0, 0, width, height);
+       cairo_fill (cr);
+
        style_changed = false;
+       dpi_changed = false;
+
+       cairo_destroy (cr);
+
        return true;
 }
 
@@ -958,4 +1109,24 @@ GainMeterBase::get_controllable()
        }
 }
 
+bool
+GainMeterBase::level_meter_button_press (GdkEventButton* ev)
+{
+       return LevelMeterButtonPress (ev); /* EMIT SIGNAL */
+}
+
+void
+GainMeter::meter_configuration_changed (ChanCount c)
+{
+       _types.clear ();
+
+       for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+               if (c.get (*i) > 0) {
+                       _types.push_back (*i);
+               }
+       }
+
+       style_changed = true;
+       meter_metric_area.queue_draw ();
+}