+ }
+
+ _body->rebuild_and_draw_grid ();
+}
+
+/* Decide how to arrange the components of the matrix */
+void
+PortMatrix::select_arrangement ()
+{
+ uint32_t const N[2] = {
+ count_of_our_type_min_1 (_ports[0].total_channels()),
+ count_of_our_type_min_1 (_ports[1].total_channels())
+ };
+
+ /* XXX: shirley there's an easier way than this */
+
+ if (_vspacer.get_parent()) {
+ _vbox.remove (_vspacer);
+ }
+
+ if (_vnotebook.get_parent()) {
+ _vbox.remove (_vnotebook);
+ }
+
+ if (_vlabel.get_parent()) {
+ _vbox.remove (_vlabel);
+ }
+
+ /* The list with the most channels goes on left or right, so that the most channel
+ names are printed horizontally and hence more readable. However we also
+ maintain notional `signal flow' vaguely from left to right. Subclasses
+ should choose where to put ports based on signal flowing from _ports[0]
+ to _ports[1] */
+
+ if (N[0] > N[1]) {
+
+ _row_index = 0;
+ _column_index = 1;
+ _arrangement = LEFT_TO_BOTTOM;
+ _vlabel.set_label (_("<b>Sources</b>"));
+ _hlabel.set_label (_("<b>Destinations</b>"));
+ _vlabel.set_angle (90);
+
+ _vbox.pack_end (_vlabel, false, false);
+ _vbox.pack_end (_vnotebook, false, false);
+ _vbox.pack_end (_vspacer, true, true);
+
+#define REMOVE_FROM_GTK_PARENT(WGT) if ((WGT).get_parent()) { (WGT).get_parent()->remove(WGT);}
+ REMOVE_FROM_GTK_PARENT(*_body)
+ REMOVE_FROM_GTK_PARENT(_vscroll)
+ REMOVE_FROM_GTK_PARENT(_hscroll)
+ REMOVE_FROM_GTK_PARENT(_vbox)
+ REMOVE_FROM_GTK_PARENT(_hbox)
+
+ attach (*_body, 2, 3, 1, 2, FILL | EXPAND, FILL | EXPAND);
+ attach (_vscroll, 3, 4, 1, 2, SHRINK);
+ attach (_hscroll, 2, 3, 3, 4, FILL | EXPAND, SHRINK);
+ attach (_vbox, 1, 2, 1, 2, SHRINK);
+ attach (_hbox, 2, 3, 2, 3, FILL | EXPAND, SHRINK);
+
+ } else {
+
+ _row_index = 1;
+ _column_index = 0;
+ _arrangement = TOP_TO_RIGHT;
+ _hlabel.set_label (_("<b>Sources</b>"));
+ _vlabel.set_label (_("<b>Destinations</b>"));
+ _vlabel.set_angle (-90);
+
+ _vbox.pack_end (_vspacer, true, true);
+ _vbox.pack_end (_vnotebook, false, false);
+ _vbox.pack_end (_vlabel, false, false);
+
+ REMOVE_FROM_GTK_PARENT(*_body)
+ REMOVE_FROM_GTK_PARENT(_vscroll)
+ REMOVE_FROM_GTK_PARENT(_hscroll)
+ REMOVE_FROM_GTK_PARENT(_vbox)
+ REMOVE_FROM_GTK_PARENT(_hbox)
+
+ attach (*_body, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
+ attach (_vscroll, 3, 4, 2, 3, SHRINK);
+ attach (_hscroll, 1, 2, 3, 4, FILL | EXPAND, SHRINK);
+ attach (_vbox, 2, 3, 2, 3, SHRINK);
+ attach (_hbox, 1, 2, 1, 2, FILL | EXPAND, SHRINK);
+ }
+}
+
+/** @return columns list */
+PortGroupList const *
+PortMatrix::columns () const
+{
+ return &_ports[_column_index];
+}
+
+boost::shared_ptr<const PortGroup>
+PortMatrix::visible_columns () const
+{
+ return visible_ports (_column_index);
+}
+
+/* @return rows list */
+PortGroupList const *
+PortMatrix::rows () const
+{
+ return &_ports[_row_index];
+}
+
+boost::shared_ptr<const PortGroup>
+PortMatrix::visible_rows () const
+{
+ return visible_ports (_row_index);
+}
+
+/** @param column Column; its bundle may be 0 if we are over a row heading.
+ * @param row Row; its bundle may be 0 if we are over a column heading.
+ */
+void
+PortMatrix::popup_menu (BundleChannel column, BundleChannel row, uint32_t t)
+{
+ using namespace Menu_Helpers;
+
+ delete _menu;
+
+ _menu = new Menu;
+ _menu->set_name ("ArdourContextMenu");
+
+ MenuList& items = _menu->items ();
+
+ BundleChannel bc[2];
+ bc[_column_index] = column;
+ bc[_row_index] = row;
+
+ char buf [64];
+ bool need_separator = false;
+
+ for (int dim = 0; dim < 2; ++dim) {
+
+ if (bc[dim].bundle) {
+
+ Menu* m = manage (new Menu);
+ MenuList& sub = m->items ();
+
+ boost::weak_ptr<Bundle> w (bc[dim].bundle);
+
+ if (can_add_channels (bc[dim].bundle)) {
+ /* Start off with options for the `natural' port type */
+ for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+ if (should_show (*i)) {
+ snprintf (buf, sizeof (buf), _("Add %s %s"), (*i).to_i18n_string(), channel_noun().c_str());
+ sub.push_back (MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::add_channel_proxy), w, *i)));
+ }
+ }
+
+ /* Now add other ones */
+ for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+ if (!should_show (*i)) {
+ snprintf (buf, sizeof (buf), _("Add %s %s"), (*i).to_i18n_string(), channel_noun().c_str());
+ sub.push_back (MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::add_channel_proxy), w, *i)));
+ }
+ }
+ }
+
+ if (can_rename_channels (bc[dim].bundle) && bc[dim].channel != -1) {
+ snprintf (
+ buf, sizeof (buf), _("Rename '%s'..."),
+ escape_underscores (bc[dim].bundle->channel_name (bc[dim].channel)).c_str()
+ );
+ sub.push_back (
+ MenuElem (
+ buf,
+ sigc::bind (sigc::mem_fun (*this, &PortMatrix::rename_channel_proxy), w, bc[dim].channel)
+ )
+ );
+ }
+
+ if (can_remove_channels (bc[dim].bundle) && bc[dim].bundle->nchannels() != ARDOUR::ChanCount::ZERO) {
+ if (bc[dim].channel != -1) {
+ add_remove_option (sub, w, bc[dim].channel);
+ } else {
+ sub.push_back (
+ MenuElem (_("Remove all"), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_all_channels), w))
+ );
+
+ if (bc[dim].bundle->nchannels().n_total() > 1) {
+ for (uint32_t i = 0; i < bc[dim].bundle->nchannels().n_total(); ++i) {
+ if (should_show (bc[dim].bundle->channel_type(i))) {
+ add_remove_option (sub, w, i);
+ }
+ }
+ }
+ }
+ }
+
+ uint32_t c = count_of_our_type (bc[dim].bundle->nchannels ());
+ if ((_show_only_bundles && c > 0) || c == 1) {
+
+ /* we're looking just at bundles, or our bundle has only one channel, so just offer
+ to disassociate all on the bundle.
+ */
+
+ snprintf (buf, sizeof (buf), _("%s all"), disassociation_verb().c_str());
+ sub.push_back (
+ MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::disassociate_all_on_bundle), w, dim))
+ );
+
+ } else if (c != 0) {
+
+ if (bc[dim].channel != -1) {
+ /* specific channel under the menu, so just offer to disassociate that */
+ add_disassociate_option (sub, w, dim, bc[dim].channel);
+ } else {
+ /* no specific channel; offer to disassociate all, or any one in particular */
+ snprintf (buf, sizeof (buf), _("%s all"), disassociation_verb().c_str());
+ sub.push_back (
+ MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PortMatrix::disassociate_all_on_bundle), w, dim))
+ );
+
+ for (uint32_t i = 0; i < bc[dim].bundle->nchannels().n_total(); ++i) {
+ if (should_show (bc[dim].bundle->channel_type(i))) {
+ add_disassociate_option (sub, w, dim, i);
+ }
+ }
+ }
+ }
+
+ items.push_back (MenuElem (escape_underscores (bc[dim].bundle->name()).c_str(), *m));
+ need_separator = true;