Add menu option to insert program changes.
[ardour.git] / gtk2_ardour / automation_streamview.cc
index 76e565d4143d5f290a9128eb085802da47ff00e0..883f0449b3368f0f23d678a0099a24faeece9e1a 100644 (file)
 
 #include <gtkmm2ext/gtk_ui.h>
 
-#include "ardour/midi_playlist.h"
 #include "ardour/midi_region.h"
 #include "ardour/midi_source.h"
-#include "ardour/midi_diskstream.h"
-#include "ardour/midi_track.h"
-#include "ardour/smf_source.h"
 #include "ardour/region_factory.h"
 
 #include "automation_streamview.h"
@@ -79,8 +75,9 @@ AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region
 
        if (wfd) {
                boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
-               if (mr)
+               if (mr) {
                        mr->midi_source()->load_model();
+               }
        }
 
        const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
@@ -151,17 +148,16 @@ AutomationStreamView::display_region(AutomationRegionView* region_view)
 void
 AutomationStreamView::set_automation_state (AutoState state)
 {
-       /* XXX: not sure if this is right, but for now the automation state is basically held by
-          the regions' AutomationLists.  Each region is always set to have the same AutoState.
-       */
+       /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
        
        if (region_views.empty()) {
                _pending_automation_state = state;
        } else {
-               for (std::list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
-                       boost::shared_ptr<AutomationLine> line = dynamic_cast<AutomationRegionView*>(*i)->line();
-                       if (line && line->the_list()) {
-                               line->the_list()->set_automation_state (state);
+               list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
+
+               for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
+                       if ((*i)->the_list()) {
+                               (*i)->the_list()->set_automation_state (state);
                        }
                }
        }
@@ -225,13 +221,12 @@ AutomationStreamView::automation_state () const
 bool
 AutomationStreamView::has_automation () const
 {
-       list<RegionView*>::const_iterator i = region_views.begin ();
-       while (i != region_views.end()) {
-               AutomationRegionView* rv = static_cast<AutomationRegionView*> (*i);
-               if (rv->line() && rv->line()->npoints() > 0) {
+       list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
+       
+       for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
+               if ((*i)->npoints() > 0) {
                        return true;
                }
-               ++i;
        }
 
        return false;
@@ -243,10 +238,10 @@ AutomationStreamView::has_automation () const
 void
 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
 {
-       for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
-               AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
-               assert (arv);
-               arv->line()->the_list()->set_interpolation (s);
+       list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
+       
+       for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
+               (*i)->the_list()->set_interpolation (s);
        }
 }
 
@@ -266,10 +261,94 @@ AutomationStreamView::interpolation () const
 /** Clear all automation displayed in this view */
 void
 AutomationStreamView::clear ()
+{
+       list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
+       
+       for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
+               (*i)->clear ();
+       }
+}
+
+/** @param start Start position in session frames.
+ *  @param end End position in session frames.
+ *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
+ *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
+ *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
+ *  confusing.
+ */
+void
+AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
 {
        for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
                AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
                assert (arv);
-               arv->line()->clear ();
+               arv->line()->get_selectables (start, end, botfrac, topfrac, results);
+       }
+}
+
+void
+AutomationStreamView::set_selected_points (PointSelection& ps)
+{
+       list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
+       
+       for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
+               (*i)->set_selected_points (ps);
+       }
+}
+
+list<boost::shared_ptr<AutomationLine> >
+AutomationStreamView::get_lines () const
+{
+       list<boost::shared_ptr<AutomationLine> > lines;
+       
+       for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
+               AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
+               assert (arv);
+               lines.push_back (arv->line());
+       }
+
+       return lines;
+}
+
+struct RegionPositionSorter {
+       bool operator() (RegionView* a, RegionView* b) {
+               return a->region()->position() < b->region()->position();
        }
+};
+       
+
+/** @param pos Position, in session frames.
+ *  @return AutomationLine to paste to for that position, or 0 if there is none appropriate.
+ */
+boost::shared_ptr<AutomationLine>
+AutomationStreamView::paste_line (framepos_t pos)
+{
+       /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
+
+       if (region_views.empty()) {
+               return boost::shared_ptr<AutomationLine> ();
+       }
+
+       region_views.sort (RegionPositionSorter ());
+
+       list<RegionView*>::const_iterator prev = region_views.begin ();
+
+       for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
+               if ((*i)->region()->position() > pos) {
+                       break;
+               }
+               prev = i;
+       }
+
+       boost::shared_ptr<Region> r = (*prev)->region ();
+
+       /* If *prev doesn't cover pos, it's no good */
+       if (r->position() > pos || ((r->position() + r->length()) < pos)) {
+               return boost::shared_ptr<AutomationLine> ();
+       }
+
+       AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
+       assert (arv);
+
+       return arv->line ();
 }