Make range selection context menu work again.
[ardour.git] / gtk2_ardour / streamview.cc
index d77681b6d5c173d8ec32865c03dda0933ab3771f..c211804fc492d2e90e1e25cf17f0a4171d0dd598 100644 (file)
@@ -48,17 +48,17 @@ using namespace Editing;
 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* group)
        : _trackview (tv)
        , owns_canvas_group(group == 0)
-       , _background_group(new ArdourCanvas::Group(*_trackview.canvas_background))
-       , canvas_group(group ? group : new ArdourCanvas::Group(*_trackview.canvas_display))
-       , _samples_per_unit(_trackview.editor.get_current_zoom())
+       , _background_group (new ArdourCanvas::Group (*_trackview.canvas_background()))
+       , canvas_group(group ? group : new ArdourCanvas::Group(*_trackview.canvas_display()))
+       , _samples_per_unit (_trackview.editor().get_current_zoom ())
        , rec_updating(false)
        , rec_active(false)
-       , use_rec_regions(tv.editor.show_waveforms_recording())
+       , use_rec_regions (tv.editor().show_waveforms_recording ())
        , region_color(_trackview.color())
        , stream_base_color(0xFFFFFFFF)
-       , layers(1)
+       , _layers (1)
+       , _layer_display (Overlaid)
        , height(tv.height)
-       , layer_display(Overlaid)
        , last_rec_data_frame(0)
 {
        /* set_position() will position the group */
@@ -66,24 +66,27 @@ StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* group)
        canvas_rect = new ArdourCanvas::SimpleRect (*_background_group);
        canvas_rect->property_x1() = 0.0;
        canvas_rect->property_y1() = 0.0;
-       canvas_rect->property_x2() = _trackview.editor.get_physical_screen_width();
+       canvas_rect->property_x2() = _trackview.editor().get_physical_screen_width ();
        canvas_rect->property_y2() = (double) tv.current_height();
        canvas_rect->raise(1); // raise above tempo lines
 
-       // DR-way
        canvas_rect->property_outline_what() = (guint32) (0x2|0x8);  // outline RHS and bottom 
-       // 2.0 way
-       //canvas_rect->property_outline_what() = (guint32) (0x1|0x2|0x8);  // outline ends and bottom 
-       // (Fill/Outline colours set in derived classes)
 
-       canvas_rect->signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
+       canvas_rect->signal_event().connect (bind (
+                       mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
+                       canvas_rect, &_trackview));
 
        if (_trackview.is_track()) {
-               _trackview.track()->DiskstreamChanged.connect (mem_fun (*this, &StreamView::diskstream_changed));
-               _trackview.session().TransportStateChange.connect (mem_fun (*this, &StreamView::transport_changed));
-               _trackview.session().TransportLooped.connect (mem_fun (*this, &StreamView::transport_looped));
-               _trackview.get_diskstream()->RecordEnableChanged.connect (mem_fun (*this, &StreamView::rec_enable_changed));
-               _trackview.session().RecordStateChanged.connect (mem_fun (*this, &StreamView::sess_rec_enable_changed));
+               _trackview.track()->DiskstreamChanged.connect (
+                               mem_fun (*this, &StreamView::diskstream_changed));
+               _trackview.session().TransportStateChange.connect (
+                               mem_fun (*this, &StreamView::transport_changed));
+               _trackview.session().TransportLooped.connect (
+                               mem_fun (*this, &StreamView::transport_looped));
+               _trackview.get_diskstream()->RecordEnableChanged.connect (
+                               mem_fun (*this, &StreamView::rec_enable_changed));
+               _trackview.session().RecordStateChanged.connect (
+                               mem_fun (*this, &StreamView::sess_rec_enable_changed));
        } 
 
        ColorsChanged.connect (mem_fun (*this, &StreamView::color_handler));
@@ -151,13 +154,15 @@ StreamView::set_samples_per_unit (gdouble spp)
        for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
                RecBoxInfo &recbox = (*xi);
                
-               gdouble xstart = _trackview.editor.frame_to_pixel ( recbox.start );
-               gdouble xend = _trackview.editor.frame_to_pixel ( recbox.start + recbox.length );
+               gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
+               gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
 
                recbox.rectangle->property_x1() = xstart;
                recbox.rectangle->property_x2() = xend;
        }
 
+       update_coverage_frames ();
+
        return 0;
 }
 
@@ -209,18 +214,75 @@ StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
 {
        playlist_change_connection.disconnect();
        playlist_changed (ds);
-       playlist_change_connection = ds->PlaylistChanged.connect (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
+       playlist_change_connection = ds->PlaylistChanged.connect (bind (
+                       mem_fun (*this, &StreamView::playlist_changed_weak),
+                       boost::weak_ptr<Diskstream> (ds)));
+}
+
+void
+StreamView::layer_regions()
+{
+       // In one traversal of the region view list:
+       // - Build a list of region views sorted by layer
+       // - Remove invalid views from the actual region view list
+       RegionViewList copy;
+       list<RegionView*>::iterator i, tmp;
+       for (i = region_views.begin(); i != region_views.end(); ) {
+               tmp = i;
+               tmp++;
+
+               if (!(*i)->is_valid()) {
+                       delete *i;
+                       region_views.erase (i);
+                       i = tmp;
+                       continue;
+               } else {
+                       (*i)->enable_display(true);
+               }
+
+               if (copy.size() == 0) {
+                       copy.push_front((*i));
+                       i = tmp;
+                       continue;
+               }
+
+               RegionViewList::iterator k = copy.begin();
+               RegionViewList::iterator l = copy.end();
+               l--;
+
+               if ((*i)->region()->layer() <= (*k)->region()->layer()) {
+                       copy.push_front((*i));
+                       i = tmp;
+                       continue;
+               } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
+                       copy.push_back((*i));
+                       i = tmp;
+                       continue;
+               }
+
+               for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
+                       if ((*j)->region()->layer() >= (*i)->region()->layer()) {
+                               copy.insert(j, (*i));
+                               break;
+                       }
+               }
+
+               i = tmp;
+       }
+
+       // Fix canvas layering by raising each in the sorted list order
+       for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
+               region_layered (*i);
+       }
 }
 
 void
 StreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
 {
        boost::shared_ptr<Diskstream> sp (ds.lock());
-       if (!sp) {
-               return;
+       if (sp) {
+               playlist_modified (sp);
        }
-
-       playlist_modified (sp);
 }
 
 void
@@ -231,22 +293,33 @@ StreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
 
        /* update layers count and the y positions and heights of our regions */
        if (ds->playlist()) {
-               layers = ds->playlist()->top_layer() + 1;
+               _layers = ds->playlist()->top_layer() + 1;
                update_contents_height ();
                update_coverage_frames ();
                redisplay_diskstream ();
        }
 }
 
+void
+StreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
+{
+       boost::shared_ptr<Diskstream> sp (ds.lock());
+       if (sp) {
+               playlist_changed (sp);
+       }
+}
+
 void
 StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
 {
-       /* XXX: binding to a shared_ptr, is this ok? */
-       ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
+       ENSURE_GUI_THREAD (bind (
+                       mem_fun (*this, &StreamView::playlist_changed_weak),
+                       boost::weak_ptr<Diskstream> (ds)));
 
        /* disconnect from old playlist */
 
-       for (vector<sigc::connection>::iterator i = playlist_connections.begin(); i != playlist_connections.end(); ++i) {
+       for (vector<sigc::connection>::iterator i = playlist_connections.begin();
+                       i != playlist_connections.end(); ++i) {
                (*i).disconnect();
        }
        
@@ -254,7 +327,7 @@ StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
        undisplay_diskstream ();
 
        /* update layers count and the y positions and heights of our regions */
-       layers = ds->playlist()->top_layer() + 1;
+       _layers = ds->playlist()->top_layer() + 1;
        update_contents_height ();
 
        update_coverage_frames ();
@@ -264,7 +337,9 @@ StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
 
        /* catch changes */
 
-       playlist_connections.push_back (ds->playlist()->Modified.connect (bind (mem_fun (*this, &StreamView::playlist_modified_weak), ds)));
+       playlist_connections.push_back (ds->playlist()->Modified.connect (bind (
+                       mem_fun (*this, &StreamView::playlist_modified_weak),
+                       ds)));
 }
 
 void
@@ -273,7 +348,9 @@ StreamView::diskstream_changed ()
        boost::shared_ptr<Track> t;
 
        if ((t = _trackview.track()) != 0) {
-               Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun (*this, &StreamView::display_diskstream), t->diskstream()));
+               Gtkmm2ext::UI::instance()->call_slot (bind (
+                               mem_fun (*this, &StreamView::display_diskstream),
+                               t->diskstream()));
        } else {
                Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
        }
@@ -349,14 +426,14 @@ StreamView::update_rec_box ()
                switch (_trackview.track()->mode()) {
                case Normal:
                        rect.length = at - rect.start;
-                       xstart = _trackview.editor.frame_to_pixel (rect.start);
-                       xend = _trackview.editor.frame_to_pixel (at);
+                       xstart = _trackview.editor().frame_to_pixel (rect.start);
+                       xend = _trackview.editor().frame_to_pixel (at);
                        break;
                        
                case Destructive:
                        rect.length = 2;
-                       xstart = _trackview.editor.frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
-                       xend = _trackview.editor.frame_to_pixel (at);
+                       xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
+                       xend = _trackview.editor().frame_to_pixel (at);
                        break;
                }
                
@@ -397,6 +474,7 @@ StreamView::set_selected_regionviews (RegionSelection& regions)
                for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
                        if (*i == *ii) {
                                selected = true;
+                               break;
                        }
                }
 
@@ -428,8 +506,8 @@ StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results
 double
 StreamView::child_height () const
 {
-       if (layer_display == Stacked) {
-               return height / layers;
+       if (_layer_display == Stacked) {
+               return height / _layers;
        }
        
        return height;
@@ -443,7 +521,7 @@ StreamView::update_contents_height ()
        const double h = child_height ();
 
        for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
-               switch (layer_display) {
+               switch (_layer_display) {
                case Overlaid:
                        (*i)->set_y (0);
                        break;
@@ -463,7 +541,7 @@ StreamView::update_contents_height ()
 void
 StreamView::set_layer_display (LayerDisplay d)
 {
-       layer_display = d;
+       _layer_display = d;
        update_contents_height ();
        update_coverage_frames ();
 }
@@ -472,6 +550,6 @@ void
 StreamView::update_coverage_frames ()
 {
        for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
-               (*i)->update_coverage_frames (layer_display);
+               (*i)->update_coverage_frames (_layer_display);
        }
 }