fix log-scale for automation lanes.
[ardour.git] / gtk2_ardour / automation_line.cc
index eb6a54e1a3e1f8915f6611a0a6a69f65a2af58bf..7ea3ff027c81e20b0a7faaae76e7918f9d6116d8 100644 (file)
 */
 
 #include <cmath>
+
+#ifdef COMPILER_MSVC
+#include <float.h>
+
+// 'std::isnan()' is not available in MSVC.
+#define isnan_local(val) (bool)_isnan((double)val)
+#else
+#define isnan_local std::isnan
+#endif
+
 #include <climits>
 #include <vector>
 #include <fstream>
@@ -32,6 +42,8 @@
 #include "ardour/automation_list.h"
 #include "ardour/dB.h"
 #include "ardour/debug.h"
+#include "ardour/parameter_types.h"
+#include "ardour/tempo.h"
 
 #include "evoral/Curve.hpp"
 
@@ -43,7 +55,6 @@
 #include "rgb_macros.h"
 #include "ardour_ui.h"
 #include "public_editor.h"
-#include "utils.h"
 #include "selection.h"
 #include "time_axis_view.h"
 #include "point_selection.h"
@@ -51,6 +62,7 @@
 
 #include "ardour/event_type_map.h"
 #include "ardour/session.h"
+#include "ardour/value_as_string.h"
 
 #include "i18n.h"
 
@@ -62,9 +74,12 @@ using namespace Editing;
 /** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session frames.
  *  This will not be deleted by AutomationLine.
  */
-AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanvas::Group& parent,
-               boost::shared_ptr<AutomationList> al,
-               Evoral::TimeConverter<double, framepos_t>* converter)
+AutomationLine::AutomationLine (const string&                              name,
+                                TimeAxisView&                              tv,
+                                ArdourCanvas::Item&                        parent,
+                                boost::shared_ptr<AutomationList>          al,
+                                const ParameterDescriptor&                 desc,
+                                Evoral::TimeConverter<double, framepos_t>* converter)
        : trackview (tv)
        , _name (name)
        , alist (al)
@@ -72,12 +87,11 @@ AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanv
        , _parent_group (parent)
        , _offset (0)
        , _maximum_time (max_framepos)
+       , _desc (desc)
 {
        if (converter) {
-               _time_converter = converter;
                _our_time_converter = false;
        } else {
-               _time_converter = new Evoral::IdentityConverter<double, framepos_t>;
                _our_time_converter = true;
        }
        
@@ -91,19 +105,21 @@ AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanv
        terminal_points_can_slide = true;
        _height = 0;
 
-       group = new ArdourCanvas::Group (&parent);
+       group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
        CANVAS_DEBUG_NAME (group, "region gain envelope group");
 
-       line = new ArdourCanvas::Curve (group);
+       line = new ArdourCanvas::PolyLine (group);
        CANVAS_DEBUG_NAME (line, "region gain envelope line");
        line->set_data ("line", this);
        line->set_outline_width (2.0);
+       line->set_covers_threshold (4.0);
 
        line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
 
        trackview.session()->register_with_memento_command_factory(alist->id(), this);
 
        if (alist->parameter().type() == GainAutomation ||
+           alist->parameter().type() == TrimAutomation ||
            alist->parameter().type() == EnvelopeAutomation) {
                set_uses_gain_mapping (true);
        }
@@ -129,53 +145,71 @@ AutomationLine::event_handler (GdkEvent* event)
        return PublicEditor::instance().canvas_line_event (event, line, this);
 }
 
+bool
+AutomationLine::is_stepped() const
+{
+       return (_desc.toggled ||
+               (alist && alist->interpolation() == AutomationList::Discrete));
+}
+
 void
-AutomationLine::show ()
+AutomationLine::update_visibility ()
 {
        if (_visible & Line) {
-               /* Only show the line there are some points, otherwise we may show an out-of-date line
+               /* Only show the line when there are some points, otherwise we may show an out-of-date line
                   when automation points have been removed (the line will still follow the shape of the
                   old points).
                */
-               if (alist->interpolation() != AutomationList::Discrete && control_points.size() >= 2) {
+               if (control_points.size() >= 2) {
                        line->show();
                } else {
                        line->hide ();
                }
-       } else {
-               line->hide();
-       }
 
-       if (_visible & ControlPoints) {
-               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->set_visible (true);
-                       (*i)->show ();
-               }
-       } else if (_visible & SelectedControlPoints) {
-               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->set_visible ((*i)->get_selected());
+               if (_visible & ControlPoints) {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
+                               (*i)->show ();
+                       }
+               } else if (_visible & SelectedControlPoints) {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
+                               if ((*i)->get_selected()) {
+                                       (*i)->show ();
+                               } else {
+                                       (*i)->hide ();
+                               }
+                       }
+               } else {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
+                               (*i)->hide ();
+                       }
                }
+
        } else {
+               line->hide ();
                for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->set_visible (false);
+                       if (_visible & ControlPoints) {
+                               (*i)->show ();
+                       } else {
+                               (*i)->hide ();
+                       }
                }
        }
+
 }
 
 void
 AutomationLine::hide ()
 {
-       set_visibility (VisibleAspects (0));
+       /* leave control points setting unchanged, we are just hiding the
+          overall line 
+       */
+       
+       set_visibility (AutomationLine::VisibleAspects (_visible & ~Line));
 }
 
 double
 AutomationLine::control_point_box_size ()
 {
-       if (alist->interpolation() == AutomationList::Discrete) {
-               return max((_height*4.0) / (double)(alist->parameter().max() - alist->parameter().min()),
-                               4.0);
-       }
-
        if (_height > TimeAxisView::preset_height (HeightLarger)) {
                return 8.0;
        } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
@@ -249,30 +283,28 @@ AutomationLine::modify_point_y (ControlPoint& cp, double y)
 
        double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
 
-       trackview.editor().session()->begin_reversible_command (_("automation event move"));
+       trackview.editor().begin_reversible_command (_("automation event move"));
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0)
-               );
+               new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
 
        cp.move_to (x, y, ControlPoint::Full);
 
+       alist->freeze ();
+       sync_model_with_view_point (cp);
+       alist->thaw ();
+
        reset_line_coords (cp);
 
        if (line_points.size() > 1) {
-               line->set (line_points);
+               line->set_steps (line_points, is_stepped());
        }
 
-       alist->freeze ();
-       sync_model_with_view_point (cp);
-       alist->thaw ();
-
        update_pending = false;
 
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state())
-               );
+               new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state()));
 
-       trackview.editor().session()->commit_reversible_command ();
+       trackview.editor().commit_reversible_command ();
        trackview.editor().session()->set_dirty ();
 }
 
@@ -285,14 +317,17 @@ AutomationLine::reset_line_coords (ControlPoint& cp)
        }
 }
 
-void
+bool
 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp)
 {
        update_pending = true;
 
+       bool moved = false;
        for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
-               sync_model_with_view_point (**i);
+               moved = sync_model_with_view_point (**i) || moved;
        }
+
+       return moved;
 }
 
 string
@@ -338,24 +373,26 @@ AutomationLine::get_verbose_cursor_relative_string (double original, double frac
 string
 AutomationLine::fraction_to_string (double fraction) const
 {
-       char buf[32];
-
        if (_uses_gain_mapping) {
-               if (fraction == 0.0) {
-                       snprintf (buf, sizeof (buf), "-inf");
+               char buf[32];
+               if (_desc.type == GainAutomation || _desc.type == EnvelopeAutomation || _desc.lower == 0) {
+                       if (fraction == 0.0) {
+                               snprintf (buf, sizeof (buf), "-inf");
+                       } else {
+                               snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, _desc.upper)));
+                       }
                } else {
-                       snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, Config->get_max_gain())));
+                       const double lower_db = accurate_coefficient_to_dB (_desc.lower);
+                       const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
+                       snprintf (buf, sizeof (buf), "%.1f", lower_db + fraction * range_db);
                }
+               return buf;
        } else {
                view_to_model_coord_y (fraction);
-               if (EventTypeMap::instance().is_integer (alist->parameter())) {
-                       snprintf (buf, sizeof (buf), "%d", (int)fraction);
-               } else {
-                       snprintf (buf, sizeof (buf), "%.2f", fraction);
-               }
+               return ARDOUR::value_as_string (_desc, fraction);
        }
 
-       return buf;
+       return ""; /*NOTREACHED*/
 }
 
 /**
@@ -388,11 +425,7 @@ AutomationLine::fraction_to_relative_string (double original, double fraction) c
        } else {
                view_to_model_coord_y (original);
                view_to_model_coord_y (fraction);
-               if (EventTypeMap::instance().is_integer (alist->parameter())) {
-                       snprintf (buf, sizeof (buf), "%d", (int)fraction - (int)original);
-               } else {
-                       snprintf (buf, sizeof (buf), "%.2f", fraction - original);
-               }
+               return ARDOUR::value_as_string (_desc, fraction - original);
        }
 
        return buf;
@@ -432,10 +465,8 @@ AutomationLine::string_to_fraction (string const & s) const
 void
 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation event move"));
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0)
-               );
+               new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
 
        _drag_points.clear ();
        _drag_points.push_back (cp);
@@ -459,10 +490,8 @@ AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
 void
 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation range move"));
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0)
-               );
+               new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0));
 
        _drag_points.clear ();
 
@@ -480,10 +509,8 @@ AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
 void
 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation range move"));
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder(), state, 0)
-               );
+               new MementoCommand<AutomationList> (memento_command_binder(), state, 0));
 
        _drag_points = cp;
        start_drag_common (0, fraction);
@@ -505,11 +532,12 @@ AutomationLine::ContiguousControlPoints::ContiguousControlPoints (AutomationLine
 }
 
 void
-AutomationLine::ContiguousControlPoints::compute_x_bounds ()
+AutomationLine::ContiguousControlPoints::compute_x_bounds (PublicEditor& e)
 {
        uint32_t sz = size();
 
        if (sz > 0 && sz < line.npoints()) {
+               const TempoMap& map (e.session()->tempo_map());
 
                /* determine the limits on x-axis motion for this 
                   contiguous range of control points
@@ -517,14 +545,30 @@ AutomationLine::ContiguousControlPoints::compute_x_bounds ()
 
                if (front()->view_index() > 0) {
                        before_x = line.nth (front()->view_index() - 1)->get_x();
+
+                       const framepos_t pos = e.pixel_to_sample(before_x);
+                       const Meter& meter = map.meter_at (pos);
+                       const framecnt_t len = ceil (meter.frames_per_bar (map.tempo_at (pos), e.session()->frame_rate())
+                                       / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()) );
+                       const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
+
+                       before_x += one_tick_in_pixels;
                }
 
                /* if our last point has a point after it in the line,
                   we have an "after" bound
                */
 
-               if (back()->view_index() < (line.npoints() - 2)) {
+               if (back()->view_index() < (line.npoints() - 1)) {
                        after_x = line.nth (back()->view_index() + 1)->get_x();
+
+                       const framepos_t pos = e.pixel_to_sample(after_x);
+                       const Meter& meter = map.meter_at (pos);
+                       const framecnt_t len = ceil (meter.frames_per_bar (map.tempo_at (pos), e.session()->frame_rate())
+                                       / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()));
+                       const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
+
+                       after_x -= one_tick_in_pixels;
                }
        }
 }
@@ -628,8 +672,9 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
                }
 
                for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
-                       (*ccp)->compute_x_bounds ();
+                       (*ccp)->compute_x_bounds (trackview.editor());
                }
+               _drag_had_movement = true;
        }       
 
        /* OK, now on to the stuff related to *this* motion event. First, for
@@ -683,14 +728,13 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
                 */
 
                if (line_points.size() > 1) {
-                       line->set (line_points);
+                       line->set_steps (line_points, is_stepped());
                }
        }
        
        _drag_distance += dx;
        _drag_x += dx;
        _last_drag_fraction = fraction;
-       _drag_had_movement = true;
        did_push = with_push;
 
        return pair<double, float> (_drag_x + dx, _last_drag_fraction + dy);
@@ -705,13 +749,13 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
        }
 
        alist->freeze ();
-       sync_model_with_view_points (_drag_points);
+       bool moved = sync_model_with_view_points (_drag_points);
 
        if (with_push) {
                ControlPoint* p;
                uint32_t i = final_index;
                while ((p = nth (i)) != 0 && p->can_slide()) {
-                       sync_model_with_view_point (*p);
+                       moved = sync_model_with_view_point (*p) || moved;
                        ++i;
                }
        }
@@ -720,9 +764,14 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
 
        update_pending = false;
 
+       if (moved) {
+               /* A point has moved as a result of sync (clamped to integer or boolean
+                  value), update line accordingly. */
+               line->set_steps (line_points, is_stepped());
+       }
+
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state())
-               );
+               new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state()));
 
        trackview.editor().session()->set_dirty ();
        did_push = false;
@@ -730,7 +779,7 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
        contiguous_points.clear ();
 }
 
-void
+bool
 AutomationLine::sync_model_with_view_point (ControlPoint& cp)
 {
        /* find out where the visual control point is.
@@ -755,6 +804,17 @@ AutomationLine::sync_model_with_view_point (ControlPoint& cp)
        view_to_model_coord_y (view_y);
 
        alist->modify (cp.model(), view_x, view_y);
+
+       /* convert back from model to view y for clamping position (for integer/boolean/etc) */
+       model_to_view_coord_y (view_y);
+       const double point_y = _height - (view_y * _height);
+       if (point_y != cp.get_y()) {
+               cp.move_to (cp.get_x(), point_y, ControlPoint::Full);
+               reset_line_coords (cp);
+               return true;
+       }
+
+       return false;
 }
 
 bool
@@ -820,16 +880,15 @@ AutomationLine::is_first_point (ControlPoint& cp)
 void
 AutomationLine::remove_point (ControlPoint& cp)
 {
-       trackview.editor().session()->begin_reversible_command (_("remove control point"));
+       trackview.editor().begin_reversible_command (_("remove control point"));
        XMLNode &before = alist->get_state();
 
        alist->erase (cp.model());
        
        trackview.editor().session()->add_command(
-               new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state())
-               );
+               new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
 
-       trackview.editor().session()->commit_reversible_command ();
+       trackview.editor().commit_reversible_command ();
        trackview.editor().session()->set_dirty ();
 }
 
@@ -879,12 +938,19 @@ AutomationLine::set_selected_points (PointSelection const & points)
                (*i)->set_selected (true);
        }
 
+       if (points.empty()) {
+               remove_visibility (SelectedControlPoints);
+       } else {
+               add_visibility (SelectedControlPoints);
+       }
+
        set_colors ();
 }
 
-void AutomationLine::set_colors ()
+void
+AutomationLine::set_colors ()
 {
-       set_line_color (ARDOUR_UI::config()->get_canvasvar_AutomationLine());
+       set_line_color (ARDOUR_UI::config()->color ("automation line"));
        for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                (*i)->set_color ();
        }
@@ -937,9 +1003,9 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
 
                model_to_view_coord (tx, ty);
 
-               if (std::isnan (tx) || std::isnan (ty)) {
+               if (isnan_local (tx) || isnan_local (ty)) {
                        warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
-                                                  _name) << endmsg;
+                                                  _name) << endmsg;
                        continue;
                }
                
@@ -992,11 +1058,9 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
                        line_points[n].y = control_points[n]->get_y();
                }
 
-               line->set (line_points);
+               line->set_steps (line_points, is_stepped());
 
-               if (_visible && alist->interpolation() != AutomationList::Discrete) {
-                       line->show();
-               }
+               update_visibility ();
        }
 
        set_selected_points (trackview.editor().get_selection().points);
@@ -1045,13 +1109,7 @@ AutomationLine::clear ()
        alist->clear();
 
        trackview.editor().session()->add_command (
-               new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state())
-               );
-}
-
-void
-AutomationLine::change_model (AutomationList::iterator /*i*/, double /*x*/, double /*y*/)
-{
+               new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
 }
 
 void
@@ -1065,38 +1123,46 @@ AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
 void
 AutomationLine::add_visibility (VisibleAspects va)
 {
+       VisibleAspects old = _visible;
+
        _visible = VisibleAspects (_visible | va);
-       show ();
+
+       if (old != _visible) {
+               update_visibility ();
+       }
 }
 
 void
 AutomationLine::set_visibility (VisibleAspects va)
 {
-       _visible = va;
-       show ();
+       if (_visible != va) {
+               _visible = va;
+               update_visibility ();
+       }
 }
 
 void
 AutomationLine::remove_visibility (VisibleAspects va)
 {
+       VisibleAspects old = _visible;
+
        _visible = VisibleAspects (_visible & ~va);
-       show ();
+
+       if (old != _visible) {
+               update_visibility ();
+       }
 }
 
 void
 AutomationLine::track_entered()
 {
-       if (alist->interpolation() != AutomationList::Discrete) {
-               add_visibility (ControlPoints);
-       }
+       add_visibility (ControlPoints);
 }
 
 void
 AutomationLine::track_exited()
 {
-       if (alist->interpolation() != AutomationList::Discrete) {
-               remove_visibility (ControlPoints);
-       }
+       remove_visibility (ControlPoints);
 }
 
 XMLNode &
@@ -1123,41 +1189,64 @@ AutomationLine::view_to_model_coord (double& x, double& y) const
 void
 AutomationLine::view_to_model_coord_y (double& y) const
 {
-       /* TODO: This should be more generic ... */
-       if (alist->parameter().type() == GainAutomation ||
-           alist->parameter().type() == EnvelopeAutomation) {
-               y = slider_position_to_gain_with_max (y, Config->get_max_gain());
+       /* TODO: This should be more generic (use ParameterDescriptor)
+        * or better yet:  Controllable -> set_interface();
+        */
+       if (   alist->parameter().type() == GainAutomation
+           || alist->parameter().type() == EnvelopeAutomation
+           || (_desc.logarithmic && _desc.lower == 0. && _desc.upper > _desc.lower)) {
+               y = slider_position_to_gain_with_max (y, _desc.upper);
+               y = max ((double)_desc.lower, y);
+               y = min ((double)_desc.upper, y);
+       } else if (alist->parameter().type() == TrimAutomation
+                  || (_desc.logarithmic && _desc.lower * _desc.upper > 0 && _desc.upper > _desc.lower)) {
+               const double lower_db = accurate_coefficient_to_dB (_desc.lower);
+               const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
                y = max (0.0, y);
-               y = min (2.0, y);
+               y = min (1.0, y);
+               y = dB_to_coefficient (lower_db + y * range_db);
        } else if (alist->parameter().type() == PanAzimuthAutomation ||
-                   alist->parameter().type() == PanElevationAutomation ||
-                   alist->parameter().type() == PanWidthAutomation) {
+                  alist->parameter().type() == PanElevationAutomation) {
                y = 1.0 - y;
-       } else if (alist->parameter().type() == PluginAutomation) {
-               y = y * (double)(alist->get_max_y()- alist->get_min_y()) + alist->get_min_y();
+       } else if (alist->parameter().type() == PanWidthAutomation) {
+               y = 2.0 * y - 1.0;
        } else {
-               y = rint (y * alist->parameter().max());
+               y = y * (double)(alist->get_max_y() - alist->get_min_y()) + alist->get_min_y();
+               if (_desc.integer_step) {
+                       y = round(y);
+               } else if (_desc.toggled) {
+                       y = (y > 0.5) ? 1.0 : 0.0;
+               }
        }
 }
 
 void
-AutomationLine::model_to_view_coord (double& x, double& y) const
+AutomationLine::model_to_view_coord_y (double& y) const
 {
-       /* TODO: This should be more generic ... */
-       if (alist->parameter().type() == GainAutomation ||
-           alist->parameter().type() == EnvelopeAutomation) {
+       /* TODO: This should be more generic (use ParameterDescriptor) */
+       if (   alist->parameter().type() == GainAutomation
+           || alist->parameter().type() == EnvelopeAutomation
+           || (_desc.logarithmic && _desc.lower == 0. && _desc.upper > _desc.lower)) {
                y = gain_to_slider_position_with_max (y, Config->get_max_gain());
+       } else if (alist->parameter().type() == TrimAutomation
+                  || (_desc.logarithmic && _desc.lower * _desc.upper > 0 && _desc.upper > _desc.lower)) {
+               const double lower_db = accurate_coefficient_to_dB (_desc.lower);
+               const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
+               y = (accurate_coefficient_to_dB (y) - lower_db) / range_db;
        } else if (alist->parameter().type() == PanAzimuthAutomation ||
-                   alist->parameter().type() == PanElevationAutomation ||
-                   alist->parameter().type() == PanWidthAutomation) {
-               // vertical coordinate axis reversal
+                  alist->parameter().type() == PanElevationAutomation) {
                y = 1.0 - y;
-       } else if (alist->parameter().type() == PluginAutomation) {
-               y = (y - alist->get_min_y()) / (double)(alist->get_max_y()- alist->get_min_y());
+       } else if (alist->parameter().type() == PanWidthAutomation) {
+               y = .5 + y * .5;
        } else {
-               y = y / (double)alist->parameter().max(); /* ... like this */
+               y = (y - alist->get_min_y()) / (double)(alist->get_max_y() - alist->get_min_y());
        }
+}
 
+void
+AutomationLine::model_to_view_coord (double& x, double& y) const
+{
+       model_to_view_coord_y (y);
        x = _time_converter->to (x) - _offset;
 }
 
@@ -1165,17 +1254,14 @@ AutomationLine::model_to_view_coord (double& x, double& y) const
 void
 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
 {
-       if (style == AutomationList::Discrete) {
-               set_visibility (ControlPoints);
-               line->hide();
-       } else {
-               set_visibility (Line);
+       if (line_points.size() > 1) {
+               line->set_steps(line_points, is_stepped());
        }
 }
 
 void
 AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty, 
-                                          AutomationList::iterator model, uint32_t npoints)
+                                           AutomationList::iterator model, uint32_t npoints)
 {
        ControlPoint::ShapeType shape;
 
@@ -1215,9 +1301,8 @@ AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, dou
 
        if (_visible & ControlPoints) {
                control_points[view_index]->show ();
-               control_points[view_index]->set_visible (true);
        } else {
-               control_points[view_index]->set_visible (false);
+               control_points[view_index]->hide ();
        }
 }
 
@@ -1229,8 +1314,7 @@ AutomationLine::connect_to_list ()
        alist->StateChanged.connect (_list_connections, invalidator (*this), boost::bind (&AutomationLine::list_changed, this), gui_context());
 
        alist->InterpolationChanged.connect (
-               _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context()
-               );
+               _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context());
 }
 
 MementoCommandBinder<AutomationList>*