Reinstate command line option to hide splash screen.
[ardour.git] / gtk2_ardour / gain_meter.cc
index 7dd7f88d7c778172950e1a333ee89e41a0def6b3..0324f3d93cfe39eb232b5e922e950d6e9bb2e5ee 100644 (file)
@@ -63,7 +63,7 @@ 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;
 
 
@@ -79,12 +79,12 @@ GainMeterBase::GainMeterBase (Session* s,
                              const Glib::RefPtr<Gdk::Pixbuf>& pix,
                              bool horizontal,
                              int fader_length)
-       : 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_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;
@@ -97,7 +97,7 @@ GainMeterBase::GainMeterBase (Session* s,
        _width = Wide;
 
        if (horizontal) {
-               gain_slider = manage (new HSliderController (pix,       
+               gain_slider = manage (new HSliderController (pix,
                                                             &gain_adjustment,
                                                             fader_length,
                                                             false));
@@ -110,21 +110,22 @@ 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));
+       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 (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);
@@ -189,27 +190,21 @@ 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"),
+               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"),
@@ -226,13 +221,13 @@ GainMeterBase::set_controls (boost::shared_ptr<Route> r,
                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();
-               
+
                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, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
 
        gain_changed ();
@@ -240,6 +235,42 @@ GainMeterBase::set_controls (boost::shared_ptr<Route> r,
        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 ()
 {
@@ -320,7 +351,7 @@ GainMeterBase::reset_group_peak_display (RouteGroup* group)
 {
        if (_route && group == _route->route_group()) {
                reset_peak_display ();
-               }
+       }
 }
 
 void
@@ -381,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);
@@ -397,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);
                }
        }
 
@@ -417,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);
@@ -451,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
@@ -474,11 +515,15 @@ next_meter_point (MeterPoint mp)
                break;
 
        case MeterPostFader:
+               return MeterOutput;
+               break;
+
+       case MeterOutput:
                return MeterCustom;
                break;
 
        case MeterCustom:
-               return MeterInput;              
+               return MeterInput;
                break;
        }
 
@@ -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) {
@@ -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 (_amp->session().transport_frame());
-       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 (false, _amp->session().transport_frame());
-       return FALSE;
+       return true;
 }
 
 gint
@@ -791,9 +843,9 @@ GainMeterBase::on_theme_changed()
 
 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");
@@ -811,22 +863,25 @@ 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, true, true);
+
+       fader_alignment.set (0.5, 0.5, 0.0, 1.0);
+       fader_alignment.add (*fader_vbox);
 
-       hbox.set_spacing (2);
-       hbox.pack_start (*fader_vbox, true, true);
+       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 (sigc::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()) {
@@ -852,18 +907,18 @@ GainMeter::set_controls (boost::shared_ptr<Route> r,
                _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 && !r->is_hidden()) {
                fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
@@ -881,44 +936,50 @@ GainMeter::get_gm_width ()
        return sz.width;
 }
 
-Glib::RefPtr<Gdk::Pixmap>
+cairo_pattern_t*
 GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
 {
        Glib::RefPtr<Gdk::Window> win (w.get_window());
-       Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
 
        gint width, height;
        win->get_size (width, height);
 
-       Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
-
-       metric_pixmaps[w.get_name()] = pixmap;
-
-       pixmap->draw_rectangle (bg_gc, true, 0, 0, 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(), "");
 
-       Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout ("");
+       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) {
 
-               Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
-               
+               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.set_rgb_p (1, 1, 1);
+                               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.set_rgb_p (0.2, 0.2, 0.5);
+                               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;
                        }
-                       
-                       fg_gc->set_rgb_fg_color (c);
+               } 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);
@@ -930,13 +991,13 @@ GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
                        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 */                         
+                               /* tweak so as not to overlay the -30dB mark */
                                points.push_back (48);
                        }
                        points.push_back (64);
@@ -944,11 +1005,11 @@ GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
                        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:
@@ -958,59 +1019,83 @@ GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
                                fraction = *j / 127.0;
                                break;
                        }
-                       
+
                        gint const pos = height - (gint) floor (height * fraction);
+
+                       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));
-                       
-                       layout->set_text (buf);
-                       
+                       pango_layout_set_text (layout, buf, strlen (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);
-                       
+                       pango_layout_get_pixel_size (layout, &tw, &th);
+
                        int p = pos - (th / 2);
                        p = min (p, height - th);
                        p = max (p, 0);
-                       
-                       pixmap->draw_layout (fg_gc, 6, p, layout);
+
+                       cairo_move_to (cr, 6, p);
+                       pango_cairo_show_layout (cr, layout);
                }
        }
 
-       return pixmap;
+       cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
+       MetricPatterns::iterator p;
+
+       if ((p = metric_patterns.find (w.get_name())) != metric_patterns.end()) {
+               cairo_pattern_destroy (p->second);
+       }
+
+       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)
 {
        Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
-       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;
+       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, _types);
+       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;
 }
 
@@ -1024,6 +1109,12 @@ GainMeterBase::get_controllable()
        }
 }
 
+bool
+GainMeterBase::level_meter_button_press (GdkEventButton* ev)
+{
+       return LevelMeterButtonPress (ev); /* EMIT SIGNAL */
+}
+
 void
 GainMeter::meter_configuration_changed (ChanCount c)
 {
@@ -1038,3 +1129,4 @@ GainMeter::meter_configuration_changed (ChanCount c)
        style_changed = true;
        meter_metric_area.queue_draw ();
 }
+