+ /* Remove channels backwards so that we don't renumber channels
+ that we are about to remove.
+ */
+ for (int i = (b->nchannels().n_total() - 1); i >= 0; --i) {
+ if (should_show (b->channel_type(i))) {
+ remove_channel (ARDOUR::BundleChannel (b, i));
+ }
+ }
+}
+
+void
+PortMatrix::add_channel_proxy (boost::weak_ptr<Bundle> w, DataType t)
+{
+ boost::shared_ptr<Bundle> b = w.lock ();
+ if (!b) {
+ return;
+ }
+
+ add_channel (b, t);
+}
+
+void
+PortMatrix::setup_notebooks ()
+{
+ int const h_current_page = _hnotebook.get_current_page ();
+ int const v_current_page = _vnotebook.get_current_page ();
+
+ /* for some reason best known to GTK, erroneous switch_page signals seem to be generated
+ when adding or removing pages to or from notebooks, so ignore them */
+
+ _ignore_notebook_page_selected = true;
+
+ remove_notebook_pages (_hnotebook);
+ remove_notebook_pages (_vnotebook);
+
+ for (PortGroupList::List::const_iterator i = _ports[_row_index].begin(); i != _ports[_row_index].end(); ++i) {
+ HBox* dummy = manage (new HBox);
+ dummy->show ();
+ Label* label = manage (new Label ((*i)->name));
+ label->set_angle (_arrangement == LEFT_TO_BOTTOM ? 90 : -90);
+ label->set_use_markup ();
+ label->show ();
+ if (_arrangement == LEFT_TO_BOTTOM) {
+ _vnotebook.prepend_page (*dummy, *label);
+ } else {
+ /* Reverse the order of vertical tabs when they are on the right hand side
+ so that from top to bottom it is the same order as that from left to right
+ for the top tabs.
+ */
+ _vnotebook.append_page (*dummy, *label);
+ }
+ }
+
+ for (PortGroupList::List::const_iterator i = _ports[_column_index].begin(); i != _ports[_column_index].end(); ++i) {
+ HBox* dummy = manage (new HBox);
+ dummy->show ();
+ Label* label = manage (new Label ((*i)->name));
+ label->set_use_markup ();
+ label->show ();
+ _hnotebook.append_page (*dummy, *label);
+ }
+
+ _ignore_notebook_page_selected = false;
+
+ if (_arrangement == TOP_TO_RIGHT) {
+ _vnotebook.set_tab_pos (POS_RIGHT);
+ _hnotebook.set_tab_pos (POS_TOP);
+ } else {
+ _vnotebook.set_tab_pos (POS_LEFT);
+ _hnotebook.set_tab_pos (POS_BOTTOM);
+ }
+
+ if (h_current_page != -1 && _hnotebook.get_n_pages() > h_current_page) {
+ _hnotebook.set_current_page (h_current_page);
+ } else {
+ _hnotebook.set_current_page (0);
+ }
+
+ if (v_current_page != -1 && _vnotebook.get_n_pages() > v_current_page) {
+ _vnotebook.set_current_page (v_current_page);
+ } else {
+ _vnotebook.set_current_page (0);
+ }
+
+ if (_hnotebook.get_n_pages() <= 1) {
+ _hbox.hide ();
+ } else {
+ _hbox.show ();
+ }
+
+ if (_vnotebook.get_n_pages() <= 1) {
+ _vbox.hide ();
+ } else {
+ _vbox.show ();
+ }
+}
+
+void
+PortMatrix::remove_notebook_pages (Notebook& n)
+{
+ int const N = n.get_n_pages ();
+
+ for (int i = 0; i < N; ++i) {
+ n.remove_page ();
+ }
+}
+
+void
+PortMatrix::notebook_page_selected (GtkNotebookPage *, guint)
+{
+ if (_ignore_notebook_page_selected) {
+ return;
+ }
+
+ _body->setup ();
+ setup_scrollbars ();
+ queue_draw ();
+}
+
+void
+PortMatrix::session_going_away ()
+{
+ _session = 0;
+}
+
+void
+PortMatrix::body_dimensions_changed ()
+{
+ _hspacer.set_size_request (_body->column_labels_border_x (), -1);
+ if (_arrangement == TOP_TO_RIGHT) {
+ _vspacer.set_size_request (-1, _body->column_labels_height ());
+ _vspacer.show ();
+ } else {
+ _vspacer.hide ();
+ }
+
+ int curr_width;
+ int curr_height;
+ _parent->get_size (curr_width, curr_height);
+
+ pair<uint32_t, uint32_t> m = max_size ();
+
+ /* Don't shrink the window */
+ m.first = max (int (m.first), curr_width);
+ m.second = max (int (m.second), curr_height);
+
+ resize_window_to_proportion_of_monitor (_parent, m.first, m.second);
+}
+
+/** @return The PortGroup that is currently visible (ie selected by
+ * the notebook) along a given axis.
+ */
+boost::shared_ptr<const PortGroup>
+PortMatrix::visible_ports (int d) const
+{
+ PortGroupList const & p = _ports[d];
+ PortGroupList::List::const_iterator j = p.begin ();
+
+ /* The logic to compute the index here is a bit twisty because for
+ the TOP_TO_RIGHT arrangement we reverse the order of the vertical
+ tabs in setup_notebooks ().
+ */
+
+ int n = 0;
+ if (d == _row_index) {
+ if (_arrangement == LEFT_TO_BOTTOM) {
+ n = p.size() - _vnotebook.get_current_page () - 1;
+ } else {
+ n = _vnotebook.get_current_page ();
+ }
+ } else {
+ n = _hnotebook.get_current_page ();
+ }
+
+ int i = 0;
+ while (i != int (n) && j != p.end ()) {
+ ++i;
+ ++j;
+ }
+
+ if (j == p.end()) {
+ return boost::shared_ptr<const PortGroup> ();
+ }
+
+ return *j;
+}
+
+void
+PortMatrix::add_remove_option (Menu_Helpers::MenuList& m, boost::weak_ptr<Bundle> w, int c)
+{
+ using namespace Menu_Helpers;
+
+ boost::shared_ptr<Bundle> b = w.lock ();
+ if (!b) {
+ return;
+ }
+
+ char buf [64];
+ snprintf (buf, sizeof (buf), _("Remove '%s'"), escape_underscores (b->channel_name (c)).c_str());
+ m.push_back (MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_channel_proxy), w, c)));
+}
+
+void
+PortMatrix::add_disassociate_option (Menu_Helpers::MenuList& m, boost::weak_ptr<Bundle> w, int d, int c)
+{
+ using namespace Menu_Helpers;
+
+ boost::shared_ptr<Bundle> b = w.lock ();
+ if (!b) {
+ return;
+ }
+
+ char buf [64];
+ snprintf (buf, sizeof (buf), _("%s all from '%s'"), disassociation_verb().c_str(), escape_underscores (b->channel_name (c)).c_str());
+ m.push_back (MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::disassociate_all_on_channel), w, c, d)));
+}
+
+void
+PortMatrix::port_connected_or_disconnected ()
+{
+ _body->rebuild_and_draw_grid ();
+ update_tab_highlighting ();
+}
+
+/** Update the highlighting of tab names to reflect which ones
+ * have connections. This is pretty inefficient, unfortunately,
+ * but maybe that doesn't matter too much.
+ */
+void
+PortMatrix::update_tab_highlighting ()
+{
+ if (!_session) {
+ return;
+ }
+
+ for (int i = 0; i < 2; ++i) {
+
+ Gtk::Notebook* notebook = row_index() == i ? &_vnotebook : &_hnotebook;
+
+ PortGroupList const * gl = ports (i);
+ int p = 0;
+ for (PortGroupList::List::const_iterator j = gl->begin(); j != gl->end(); ++j) {
+ bool has_connection = false;
+ PortGroup::BundleList const & bl = (*j)->bundles ();
+ PortGroup::BundleList::const_iterator k = bl.begin ();
+ while (k != bl.end()) {
+ if ((*k)->bundle->connected_to_anything (_session->engine())) {
+ has_connection = true;
+ break;
+ }
+ ++k;
+ }
+
+ /* Find the page index that we should update; this is backwards
+ for the vertical tabs in the LEFT_TO_BOTTOM arrangement.
+ */
+ int page = p;
+ if (i == row_index() && _arrangement == LEFT_TO_BOTTOM) {
+ page = notebook->get_n_pages() - p - 1;
+ }
+
+ Gtk::Label* label = dynamic_cast<Gtk::Label*> (notebook->get_tab_label(*notebook->get_nth_page (page)));
+ string c = label->get_label ();
+ if (c.length() && c[0] == '<' && !has_connection) {
+ /* this label is marked up with <b> but shouldn't be */
+ label->set_text ((*j)->name);
+ } else if (c.length() && c[0] != '<' && has_connection) {
+ /* this label is not marked up with <b> but should be */
+ label->set_markup (string_compose ("<b>%1</b>", Gtkmm2ext::markup_escape_text ((*j)->name)));
+ }
+
+ ++p;
+ }
+ }
+}
+
+string
+PortMatrix::channel_noun () const
+{
+ return _("channel");
+}
+
+/** @return true if this matrix should show bundles / ports of type \t */
+bool
+PortMatrix::should_show (DataType t) const
+{
+ return (_type == DataType::NIL || t == _type);
+}
+
+uint32_t
+PortMatrix::count_of_our_type (ChanCount c) const
+{
+ if (_type == DataType::NIL) {
+ return c.n_total ();
+ }
+
+ return c.get (_type);
+}
+
+/** @return The number of ports of our type in the given channel count,
+ * but returning 1 if there are no ports.
+ */
+uint32_t
+PortMatrix::count_of_our_type_min_1 (ChanCount c) const
+{
+ uint32_t n = count_of_our_type (c);
+ if (n == 0) {
+ n = 1;
+ }
+
+ return n;
+}
+
+PortMatrixNode::State
+PortMatrix::get_association (PortMatrixNode node) const
+{
+ if (show_only_bundles ()) {
+
+ bool have_off_diagonal_association = false;
+ bool have_diagonal_association = false;
+ bool have_diagonal_not_association = false;
+
+ for (uint32_t i = 0; i < node.row.bundle->nchannels().n_total(); ++i) {
+
+ for (uint32_t j = 0; j < node.column.bundle->nchannels().n_total(); ++j) {
+
+ if (!should_show (node.row.bundle->channel_type(i)) || !should_show (node.column.bundle->channel_type(j))) {
+ continue;
+ }
+
+ ARDOUR::BundleChannel c[2];
+ c[row_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
+ c[column_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
+
+ PortMatrixNode::State const s = get_state (c);
+
+ switch (s) {
+ case PortMatrixNode::ASSOCIATED:
+ if (i == j) {
+ have_diagonal_association = true;
+ } else {
+ have_off_diagonal_association = true;
+ }
+ break;
+
+ case PortMatrixNode::NOT_ASSOCIATED:
+ if (i == j) {
+ have_diagonal_not_association = true;
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+ }
+
+ if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
+ return PortMatrixNode::ASSOCIATED;
+ } else if (!have_diagonal_association && !have_off_diagonal_association) {
+ return PortMatrixNode::NOT_ASSOCIATED;
+ }
+
+ return PortMatrixNode::PARTIAL;
+
+ } else {
+
+ ARDOUR::BundleChannel c[2];
+ c[column_index()] = node.column;
+ c[row_index()] = node.row;
+ return get_state (c);
+
+ }
+
+ abort(); /* NOTREACHED */
+ return PortMatrixNode::NOT_ASSOCIATED;
+}
+
+/** @return true if b is a non-zero pointer and the bundle it points to has some channels */
+bool
+PortMatrix::bundle_with_channels (boost::shared_ptr<ARDOUR::Bundle> b)
+{
+ return b && b->nchannels() != ARDOUR::ChanCount::ZERO;
+}
+
+/** See if a `flip' is possible.
+ * @return If flip is possible, the new (row, column) notebook indices that
+ * should be selected; otherwise, (-1, -1)
+ */
+pair<int, int>
+PortMatrix::check_flip () const
+{
+ /* Look for the row's port group name in the columns */
+
+ int new_column = 0;
+ boost::shared_ptr<const PortGroup> r = visible_ports (_row_index);
+ PortGroupList::List::const_iterator i = _ports[_column_index].begin();
+ while (i != _ports[_column_index].end() && (*i)->name != r->name) {
+ ++i;
+ ++new_column;
+ }
+
+ if (i == _ports[_column_index].end ()) {
+ return make_pair (-1, -1);
+ }
+
+ /* Look for the column's port group name in the rows */
+
+ int new_row = 0;
+ boost::shared_ptr<const PortGroup> c = visible_ports (_column_index);
+ i = _ports[_row_index].begin();
+ while (i != _ports[_row_index].end() && (*i)->name != c->name) {
+ ++i;
+ ++new_row;
+ }
+
+ if (i == _ports[_row_index].end ()) {
+ return make_pair (-1, -1);
+ }
+
+ if (_arrangement == LEFT_TO_BOTTOM) {
+ new_row = _ports[_row_index].size() - new_row - 1;
+ }
+
+ return make_pair (new_row, new_column);
+}
+
+bool
+PortMatrix::can_flip () const
+{
+ return check_flip().first != -1;
+}
+
+/** Flip the column and row pages around, if possible */
+void
+PortMatrix::flip ()
+{
+ pair<int, int> n = check_flip ();
+ if (n.first == -1) {
+ return;
+ }
+
+ _vnotebook.set_current_page (n.first);
+ _hnotebook.set_current_page (n.second);
+}
+
+bool
+PortMatrix::key_press (GdkEventKey* k)
+{
+ if (k->keyval == GDK_f) {
+ flip ();
+ return true;
+ }
+
+ return false;