Make stuff in the automation menu apply to the track selection.
[ardour.git] / gtk2_ardour / gain_meter.cc
index 3c3b70c7cfacbc96e6314730ec966f7451aa0edd..8ef83cfad5b4e2e98446089992e17c1bcad4f3e4 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"
@@ -81,6 +82,7 @@ GainMeterBase::GainMeterBase (Session* s,
        : gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1)  // 0.781787 is the value needed for gain to be set to 0.
        , gain_automation_style_button ("")
        , gain_automation_state_button ("")
+       , style_changed (false)
        , dpi_changed (false)
        , _is_midi (false)
 
@@ -92,7 +94,6 @@ GainMeterBase::GainMeterBase (Session* s,
        ignore_toggle = false;
        meter_menu = 0;
        next_release_selects = false;
-       style_changed = true;
        _width = Wide;
 
        if (horizontal) {
@@ -109,8 +110,8 @@ GainMeterBase::GainMeterBase (Session* s,
 
        level_meter = new LevelMeter(_session);
 
-       gain_slider->signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeter::start_gain_touch));
-       gain_slider->signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeter::end_gain_touch));
+       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");
@@ -131,8 +132,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);
@@ -226,13 +227,13 @@ GainMeterBase::set_controls (boost::shared_ptr<Route> r,
 
                boost::shared_ptr<AutomationControl> gc = amp->gain_control();
                
-               gc->alist()->automation_state_changed.connect (model_connections, boost::bind (&GainMeter::gain_automation_state_changed, this));
-               gc->alist()->automation_style_changed.connect (model_connections, boost::bind (&GainMeter::gain_automation_style_changed, this));
+               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 ();
        }
        
-       amp->gain_control()->Changed.connect (model_connections, boost::bind (&GainMeterBase::gain_changed, this));
+       amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
 
        gain_changed ();
        show_gain ();
@@ -397,16 +398,14 @@ void
 GainMeterBase::gain_adjusted ()
 {
        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);
-                               }
+               if (_route && _route->amp() == _amp) {
+                       if (_is_midi) {
+                               _route->set_gain (gain_adjustment.get_value(), this);
                        } else {
-                               _amp->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
+                               _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);
                }
        }
 
@@ -438,7 +437,7 @@ GainMeterBase::effective_gain_display ()
 void
 GainMeterBase::gain_changed ()
 {
-       Gtkmm2ext::UI::instance()->call_slot (boost::bind (&GainMeterBase::effective_gain_display, this));
+       Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
 }
 
 void
@@ -450,7 +449,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
@@ -473,6 +475,10 @@ next_meter_point (MeterPoint mp)
                break;
 
        case MeterPostFader:
+               return MeterOutput;
+               break;
+
+       case MeterOutput:
                return MeterCustom;
                break;
 
@@ -529,7 +535,7 @@ GainMeterBase::meter_press(GdkEventButton* ev)
                                        */
 
                                        if (ev->button == 1) {
-                                               set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
+                                               set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
                                        }
 
                                } else {
@@ -538,7 +544,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()));
                                }
                        }
                }
@@ -551,8 +557,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) {
@@ -567,18 +573,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);
        }
 }
 
@@ -590,18 +596,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* ev)
 {
-       _amp->gain_control()->stop_touch ();
-       return FALSE;
+       _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
+       return true;
 }
 
 gint
@@ -796,13 +809,13 @@ GainMeter::GainMeter (Session* s, int fader_length)
        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);
@@ -810,15 +823,12 @@ GainMeter::GainMeter (Session* s, int fader_length)
        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, false, false, 0);
 
        hbox.set_spacing (2);
-       hbox.pack_start (*fader_vbox, true, true);
+        hbox.pack_start (*fader_vbox, false, false, 4);
 
        set_spacing (2);
 
@@ -847,13 +857,22 @@ 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 (*level_meter, true, true);
 
        if (r && !r->is_hidden()) {
                fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
@@ -872,15 +891,12 @@ GainMeter::get_gm_width ()
 }
 
 Glib::RefPtr<Gdk::Pixmap>
-GainMeter::render_metrics (Gtk::Widget& w)
+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);
@@ -889,24 +905,88 @@ GainMeter::render_metrics (Gtk::Widget& w)
 
        pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
 
-       Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
-
-       for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
-
-               float fraction = log_meter (db_points[i]);
-               gint pos = height - (gint) floor (height * fraction);
+       Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout ("");
 
-               snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
+       for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
 
-               layout->set_text (buf);
-
-               /* we want logical extents, not ink extents here */
-
-               int width, height;
-               layout->get_pixel_size (width, height);
+               Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
+               
+               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.set_rgb_p (1, 1, 1);
+                               break;
+                       case DataType::MIDI:
+                               c.set_rgb_p (0.2, 0.2, 0.5);
+                               break;
+                       }
+                       
+                       fg_gc->set_rgb_fg_color (c);
+               }
 
-               pixmap->draw_line (fg_gc, 0, pos, 4, pos);
-               pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
+               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;
+               }
+               
+               char buf[32];
+               
+               for (vector<int>::const_iterator j = points.begin(); j != points.end(); ++j) {
+                       
+                       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);
+                       
+                       snprintf (buf, sizeof (buf), "%d", abs (*j));
+                       
+                       layout->set_text (buf);
+                       
+                       /* we want logical extents, not ink extents here */
+                       
+                       int tw, th;
+                       layout->get_pixel_size (tw, th);
+                       
+                       pixmap->draw_line (fg_gc, 0, pos, 4, pos);
+                       
+                       int p = pos - (th / 2);
+                       p = min (p, height - th);
+                       p = max (p, 0);
+                       
+                       pixmap->draw_layout (fg_gc, 6, p, layout);
+               }
        }
 
        return pixmap;
@@ -915,12 +995,8 @@ GainMeter::render_metrics (Gtk::Widget& w)
 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));
+       Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_INSENSITIVE));
        GdkRectangle base_rect;
        GdkRectangle draw_rect;
        gint width, height;
@@ -936,7 +1012,7 @@ GainMeter::meter_metrics_expose (GdkEventExpose *ev)
        std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
 
        if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
-               pixmap = render_metrics (meter_metric_area);
+               pixmap = render_metrics (meter_metric_area, _types);
        } else {
                pixmap = i->second;
        }
@@ -957,4 +1033,17 @@ GainMeterBase::get_controllable()
        }
 }
 
+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 ();
+}