Allow right-click on region list to select the region. Fixes #3129.
[ardour.git] / gtk2_ardour / port_group.cc
index 11e75c1fc34ef79c2ac839c6937b2e0f8ed31c57..8dc91edb3b46ee7af4db96c42c42626ddc36b802 100644 (file)
@@ -30,6 +30,7 @@
 #include "ardour/session.h"
 #include "ardour/auditioner.h"
 
+#include "gui_thread.h"
 #include "port_group.h"
 #include "port_matrix.h"
 #include "time_axis_view.h"
@@ -116,7 +117,7 @@ PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<I
        }
 
        BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
-       br->changed_connection = b->Changed.connect (boost::bind (&PortGroup::bundle_changed, this, _1));
+       b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
        _bundles.push_back (br);
 
        Changed ();     
@@ -206,6 +207,55 @@ PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
        return (*i)->io;
 }
 
+/** Remove bundles whose channels are already represented by other, larger bundles */
+void
+PortGroup::remove_duplicates ()
+{
+       BundleList::iterator i = _bundles.begin();
+       while (i != _bundles.end()) {
+
+               BundleList::iterator tmp = i;
+               ++tmp;
+
+               bool remove = false;
+
+               for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
+
+                       if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
+                               /* this bundle is larger */
+
+                               uint32_t k = 0;
+                               while (k < (*i)->bundle->nchannels()) {
+                                       /* see if this channel on *i has an equivalent on *j */
+                                       uint32_t l = 0;
+                                       while (l < (*j)->bundle->nchannels() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
+                                               ++l;
+                                       }
+
+                                       if (l == (*j)->bundle->nchannels()) {
+                                               /* it does not */
+                                               break;
+                                       }
+
+                                       ++k;
+                               }
+
+                               if (k == (*i)->bundle->nchannels ()) {
+                                       /* all channels on *i are represented by the larger bundle *j, so remove *i */
+                                       remove = true;
+                                       break;
+                               }
+                       }
+               }
+               
+               if (remove) {
+                       _bundles.erase (i);
+               }
+               
+               i = tmp;
+       }
+}
+
 
 /** PortGroupList constructor.
  */
@@ -397,11 +447,15 @@ PortGroupList::gather (ARDOUR::Session* session, bool inputs, bool allow_dups)
                other->add_bundle (make_bundle_from_ports (extra_other, inputs));
        }
 
-       add_group_if_not_empty (system);
+       if (!allow_dups) {
+               system->remove_duplicates ();
+       }
+
+       add_group_if_not_empty (other);
        add_group_if_not_empty (bus);
        add_group_if_not_empty (track);
        add_group_if_not_empty (ardour);
-       add_group_if_not_empty (other);
+       add_group_if_not_empty (system);
 
        emit_changed ();
 }
@@ -521,9 +575,8 @@ PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
 {
        _groups.push_back (g);
 
-       g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
-
-       _bundle_changed_connections.add_connection (g->BundleChanged.connect (sigc::mem_fun (*this, &PortGroupList::emit_bundle_changed)));
+       g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
+       g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
 
        emit_changed ();
 }