add icons for cut mouse mode button and cut cursor (both need improving)
[ardour.git] / gtk2_ardour / automation_line.cc
index dbab83d6b9dac96d731ab8352db442d40ca2a392..1cfd7eeb48d4a7c657d27d0142d7b35bb9a53bf6 100644 (file)
@@ -39,6 +39,7 @@
 #include "ardour/automation_list.h"
 #include "ardour/dB.h"
 #include "ardour/debug.h"
+#include "ardour/tempo.h"
 
 #include "evoral/Curve.hpp"
 
@@ -50,7 +51,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"
@@ -69,7 +69,7 @@ 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,
+AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanvas::Item& parent,
                                 boost::shared_ptr<AutomationList> al,
                                 Evoral::TimeConverter<double, framepos_t>* converter)
        : trackview (tv)
@@ -98,7 +98,7 @@ 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);
        CANVAS_DEBUG_NAME (group, "region gain envelope group");
 
        line = new ArdourCanvas::PolyLine (group);
@@ -138,10 +138,9 @@ AutomationLine::event_handler (GdkEvent* event)
 }
 
 void
-AutomationLine::show ()
+AutomationLine::update_visibility ()
 {
        if (_visible & Line) {
-               cerr << "AL visibile with vis = " << (hex) << _visible << dec << endl;
                /* Only show the line 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).
@@ -153,12 +152,10 @@ AutomationLine::show ()
                }
 
                if (_visible & ControlPoints) {
-                       cerr << "AL visibile (just cp's with vis = " << (hex) << _visible << dec << endl;
                        for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                                (*i)->show ();
                        }
                } else if (_visible & SelectedControlPoints) {
-                       cerr << "AL visibile (just selected cp's with vis = " << (hex) << _visible << dec << endl;
                        for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                                if ((*i)->get_selected()) {
                                        (*i)->show ();
@@ -167,7 +164,6 @@ AutomationLine::show ()
                                }
                        }
                } else {
-                       cerr << "AL visibile (no cp's with vis = " << (hex) << _visible << dec << endl;
                        for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                                (*i)->hide ();
                        }
@@ -175,6 +171,9 @@ AutomationLine::show ()
 
        } else {
                line->hide ();
+               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
+                       (*i)->hide ();
+               }
        }
 
 }
@@ -521,11 +520,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
@@ -533,14 +533,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;
                }
        }
 }
@@ -644,7 +660,7 @@ 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());
                }
        }       
 
@@ -1008,9 +1024,7 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
 
                line->set (line_points);
 
-               /* despite the name, this may show or hide things */
-
-               show();
+               update_visibility ();
        }
 
        set_selected_points (trackview.editor().get_selection().points);
@@ -1083,7 +1097,7 @@ AutomationLine::add_visibility (VisibleAspects va)
        _visible = VisibleAspects (_visible | va);
 
        if (old != _visible) {
-               show ();
+               update_visibility ();
        }
 }
 
@@ -1092,7 +1106,7 @@ AutomationLine::set_visibility (VisibleAspects va)
 {
        if (_visible != va) {
                _visible = va;
-               show ();
+               update_visibility ();
        }
 }
 
@@ -1104,7 +1118,7 @@ AutomationLine::remove_visibility (VisibleAspects va)
        _visible = VisibleAspects (_visible & ~va);
 
        if (old != _visible) {
-               show ();
+               update_visibility ();
        }
 }