+ if (need_separator) {
+ items.push_back (SeparatorElem ());
+ }
+
+ items.push_back (MenuElem (_("Rescan"), mem_fun (*this, &PortMatrix::setup_all_ports)));
+ items.push_back (CheckMenuElem (_("Show individual ports"), mem_fun (*this, &PortMatrix::toggle_show_only_bundles)));
+ CheckMenuItem* i = dynamic_cast<CheckMenuItem*> (&items.back());
+ _inhibit_toggle_show_only_bundles = true;
+ i->set_active (!_show_only_bundles);
+ _inhibit_toggle_show_only_bundles = false;
+
+ _menu->popup (1, t);
+}
+
+void
+PortMatrix::remove_channel_proxy (boost::weak_ptr<Bundle> b, uint32_t c)
+{
+ boost::shared_ptr<Bundle> sb = b.lock ();
+ if (!sb) {
+ return;
+ }
+
+ remove_channel (BundleChannel (sb, c));
+
+}
+
+void
+PortMatrix::rename_channel_proxy (boost::weak_ptr<Bundle> b, uint32_t c)
+{
+ boost::shared_ptr<Bundle> sb = b.lock ();
+ if (!sb) {
+ return;
+ }
+
+ rename_channel (BundleChannel (sb, c));
+}
+
+void
+PortMatrix::disassociate_all_on_channel (boost::weak_ptr<Bundle> bundle, uint32_t channel, int dim)
+{
+ boost::shared_ptr<Bundle> sb = bundle.lock ();
+ if (!sb) {
+ return;
+ }
+
+ PortGroup::BundleList a = _ports[1-dim].bundles ();
+
+ for (PortGroup::BundleList::iterator i = a.begin(); i != a.end(); ++i) {
+ for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
+
+ BundleChannel c[2];
+ c[dim] = BundleChannel (sb, channel);
+ c[1-dim] = BundleChannel (i->bundle, j);
+
+ if (get_state (c) == PortMatrixNode::ASSOCIATED) {
+ set_state (c, false);
+ }
+ }
+ }
+
+ _body->rebuild_and_draw_grid ();
+}
+
+void
+PortMatrix::setup_global_ports ()
+{
+ for (int i = 0; i < 2; ++i) {
+ if (list_is_global (i)) {
+ setup_ports (i);
+ }
+ }
+}
+
+void
+PortMatrix::setup_all_ports ()
+{
+ setup_ports (0);
+ setup_ports (1);
+}
+
+void
+PortMatrix::toggle_show_only_bundles ()
+{
+ if (_inhibit_toggle_show_only_bundles) {
+ return;
+ }
+
+ _show_only_bundles = !_show_only_bundles;
+ _body->setup ();
+ setup_scrollbars ();
+ queue_draw ();
+}
+
+void
+PortMatrix::hide_group (boost::weak_ptr<PortGroup> w)
+{
+ boost::shared_ptr<PortGroup> g = w.lock ();
+ if (!g) {
+ return;
+ }
+
+ g->set_visible (false);
+}
+
+void
+PortMatrix::show_group (boost::weak_ptr<PortGroup> w)
+{
+ boost::shared_ptr<PortGroup> g = w.lock ();
+ if (!g) {
+ return;
+ }
+
+ g->set_visible (true);
+}
+
+pair<uint32_t, uint32_t>
+PortMatrix::max_size () const
+{
+ pair<uint32_t, uint32_t> m = _body->max_size ();
+
+ m.first += _vscroll.get_width ();
+ m.second += _hscroll.get_height ();
+
+ return m;
+}
+
+void
+PortMatrix::setup_max_size ()
+{
+ if (!_parent) {
+ return;
+ }
+
+ pair<uint32_t, uint32_t> const m = max_size ();
+
+ GdkGeometry g;
+ g.max_width = m.first;
+ g.max_height = m.second;
+
+ _parent->set_geometry_hints (*this, g, Gdk::HINT_MAX_SIZE);
+}
+
+bool
+PortMatrix::on_scroll_event (GdkEventScroll* ev)
+{
+ double const h = _hscroll.get_value ();
+ double const v = _vscroll.get_value ();
+
+ switch (ev->direction) {
+ case GDK_SCROLL_UP:
+ _vscroll.set_value (v - PortMatrixComponent::grid_spacing ());
+ break;
+ case GDK_SCROLL_DOWN:
+ _vscroll.set_value (v + PortMatrixComponent::grid_spacing ());
+ break;
+ case GDK_SCROLL_LEFT:
+ _hscroll.set_value (h - PortMatrixComponent::grid_spacing ());
+ break;
+ case GDK_SCROLL_RIGHT:
+ _hscroll.set_value (h + PortMatrixComponent::grid_spacing ());
+ break;
+ }
+
+ return true;
+}
+
+boost::shared_ptr<IO>
+PortMatrix::io_from_bundle (boost::shared_ptr<Bundle> b) const
+{
+ boost::shared_ptr<IO> io = _ports[0].io_from_bundle (b);
+ if (!io) {
+ io = _ports[1].io_from_bundle (b);
+ }
+
+ return io;
+}
+
+bool
+PortMatrix::can_add_channel (boost::shared_ptr<Bundle> b) const
+{
+ return io_from_bundle (b);
+}
+
+void
+PortMatrix::add_channel (boost::shared_ptr<Bundle> b)
+{
+ boost::shared_ptr<IO> io = io_from_bundle (b);
+
+ if (io) {
+ io->add_port ("", this, _type);
+ }
+}
+
+bool
+PortMatrix::can_remove_channels (boost::shared_ptr<Bundle> b) const
+{
+ return io_from_bundle (b);
+}
+
+void
+PortMatrix::remove_channel (ARDOUR::BundleChannel b)
+{
+ boost::shared_ptr<IO> io = io_from_bundle (b.bundle);
+
+ if (io) {
+ Port* p = io->nth (b.channel);
+ if (p) {
+ io->remove_port (p, this);
+ }
+ }
+}
+
+void
+PortMatrix::add_channel_proxy (boost::weak_ptr<Bundle> w)
+{
+ boost::shared_ptr<Bundle> b = w.lock ();
+ if (!b) {
+ return;
+ }
+
+ add_channel (b);