Fix up state save/load of location lock/glue settings.
[ardour.git] / gtk2_ardour / port_matrix_grid.cc
index 3b0a98e2f3d2f22216edfc177917b81eb3eb3ee2..8a12df236e2450ee549803067a3afcefd4b4b51b 100644 (file)
 #include "port_matrix_grid.h"
 #include "port_matrix.h"
 #include "port_matrix_body.h"
+#include "keyboard.h"
 
 using namespace std;
+using Gtkmm2ext::Keyboard;
 
 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
        : PortMatrixComponent (m, b),
@@ -39,15 +41,16 @@ PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
 void
 PortMatrixGrid::compute_dimensions ()
 {
-       _width = 0;
-
-       for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
-               _width += group_size (*i) * grid_spacing ();
+       if (_matrix->visible_columns()) {
+               _width = group_size (_matrix->visible_columns()) * grid_spacing ();
+       } else {
+               _width = 0;
        }
 
-       _height = 0;
-       for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
-               _height += group_size (*i) * grid_spacing ();
+       if (_matrix->visible_rows()) {
+               _height = group_size (_matrix->visible_rows()) * grid_spacing ();
+       } else {
+               _height = 0;
        }
 }
 
@@ -59,33 +62,10 @@ PortMatrixGrid::render (cairo_t* cr)
        cairo_rectangle (cr, 0, 0, _width, _height);
        cairo_fill (cr);
 
-       uint32_t x = 0;
-       for (PortGroupList::List::const_iterator c = _matrix->columns()->begin(); c != _matrix->columns()->end(); ++c) {
-
-               uint32_t y = 0;
-               for (PortGroupList::List::const_iterator r = _matrix->rows()->begin(); r != _matrix->rows()->end(); ++r) {
-
-                       if ((*c)->visible() && (*r)->visible()) {
-                               render_group_pair (cr, *r, *c, x, y);
-                       }
-
-                       y += group_size (*r) * grid_spacing ();
-               }
-
-               x += group_size (*c) * grid_spacing ();
-       }
-}
-
-void
-PortMatrixGrid::render_group_pair (cairo_t* cr, boost::shared_ptr<const PortGroup> row, boost::shared_ptr<const PortGroup> column, uint32_t const x, uint32_t const y)
-{
-       PortGroup::BundleList const & row_bundles = row->bundles();
-       PortGroup::BundleList const & column_bundles = column->bundles();
-
-       /* unfortunately we need to compute the height of the row group here */
-       uint32_t height = group_size (row) * grid_spacing ();
+       PortGroup::BundleList const & row_bundles = _matrix->visible_rows()->bundles();
+       PortGroup::BundleList const & column_bundles = _matrix->visible_columns()->bundles();
 
-       uint32_t tx = x;
+       uint32_t x = 0;
 
        /* VERTICAL GRID LINES */
 
@@ -95,31 +75,29 @@ PortMatrixGrid::render_group_pair (cairo_t* cr, boost::shared_ptr<const PortGrou
        for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
 
                cairo_set_line_width (cr, thick_grid_line_width());
-               cairo_move_to (cr, tx, y);
-               cairo_line_to (cr, tx, y + height);
+               cairo_move_to (cr, x, 0);
+               cairo_line_to (cr, x, _height);
                cairo_stroke (cr);
 
                if (!_matrix->show_only_bundles()) {
                        cairo_set_line_width (cr, thin_grid_line_width());
-                       for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
-                               tx += grid_spacing ();
-                               cairo_move_to (cr, tx, y);
-                               cairo_line_to (cr, tx, y + height);
+                       for (uint32_t j = 0; j < _matrix->count_of_our_type ((*i)->bundle->nchannels()); ++j) {
+                               x += grid_spacing ();
+                               cairo_move_to (cr, x, 0);
+                               cairo_line_to (cr, x, _height);
                                cairo_stroke (cr);
                        }
 
                } else {
 
-                       tx += grid_spacing ();
+                       x += grid_spacing ();
 
                }
 
                ++N;
        }
 
-       uint32_t const width = tx - x;
-
-       uint32_t ty = y;
+       uint32_t y = 0;
 
        /* HORIZONTAL GRID LINES */
 
@@ -127,43 +105,47 @@ PortMatrixGrid::render_group_pair (cairo_t* cr, boost::shared_ptr<const PortGrou
        for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
 
                cairo_set_line_width (cr, thick_grid_line_width());
-               cairo_move_to (cr, x, ty);
-               cairo_line_to (cr, x + width, ty);
+               cairo_move_to (cr, 0, y);
+               cairo_line_to (cr, _width, y);
                cairo_stroke (cr);
 
                if (!_matrix->show_only_bundles()) {
                        cairo_set_line_width (cr, thin_grid_line_width());
-                       for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
-                               ty += grid_spacing ();
-                               cairo_move_to (cr, x, ty);
-                               cairo_line_to (cr, x + width, ty);
+                       for (uint32_t j = 0; j < _matrix->count_of_our_type ((*i)->bundle->nchannels()); ++j) {
+                               y += grid_spacing ();
+                               cairo_move_to (cr, 0, y);
+                               cairo_line_to (cr, _width, y);
                                cairo_stroke (cr);
                        }
 
                } else {
 
-                       ty += grid_spacing ();
+                       y += grid_spacing ();
 
                }
 
                ++N;
        }
 
-       /* ASSOCIATION INDICATORS */
+       /* ASSOCIATION INDICATORS and NON-CONNECTABLE INDICATORS */
 
-       uint32_t bx = x;
-       uint32_t by = y;
+       /* we draw a grey square in a matrix box if the two ports that intersect at that box
+          cannot be connected because they are of different types (MIDI vs. audio)
+       */
+
+       uint32_t bx = 0;
+       uint32_t by = 0;
 
        if (_matrix->show_only_bundles()) {
 
                for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
-                       by = y;
+                       by = 0;
 
                        for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
 
                                PortMatrixNode::State s = get_association (PortMatrixNode (
-                                                                                  ARDOUR::BundleChannel (i->bundle, 0),
-                                                                                  ARDOUR::BundleChannel (j->bundle, 0)
+                                                                                  ARDOUR::BundleChannel ((*i)->bundle, 0),
+                                                                                  ARDOUR::BundleChannel ((*j)->bundle, 0)
                                                                                   ));
                                switch (s) {
                                case PortMatrixNode::ASSOCIATED:
@@ -186,44 +168,54 @@ PortMatrixGrid::render_group_pair (cairo_t* cr, boost::shared_ptr<const PortGrou
        } else {
 
                for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
-                       by = y;
+                       by = 0;
 
                        for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
 
-                               tx = bx;
-                               for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
-
-                                       ty = by;
-                                       for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
+                               x = bx;
+                               for (uint32_t k = 0; k < (*i)->bundle->nchannels().n_total(); ++k) {
 
-                                               ARDOUR::BundleChannel c[2];
-                                               c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
-                                               c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
-
-                                               PortMatrixNode::State const s = _matrix->get_state (c);
-
-                                               switch (s) {
-                                               case PortMatrixNode::ASSOCIATED:
-                                                       draw_association_indicator (cr, tx, ty);
-                                                       break;
+                                       y = by;
+                                       for (uint32_t l = 0; l < (*j)->bundle->nchannels().n_total(); ++l) {
 
-                                               case PortMatrixNode::NOT_ASSOCIATED:
-                                                       break;
+                                               if (!_matrix->should_show ((*i)->bundle->channel_type(k)) || !_matrix->should_show ((*j)->bundle->channel_type(l))) {
+                                                       continue;
+                                               }
 
-                                               default:
-                                                       break;
+                                               ARDOUR::BundleChannel c[2];
+                                               c[_matrix->column_index()] = ARDOUR::BundleChannel ((*i)->bundle, k);
+                                               c[_matrix->row_index()] = ARDOUR::BundleChannel ((*j)->bundle, l);
+
+                                               if (c[0].bundle->channel_type (c[0].channel) != c[1].bundle->channel_type (c[1].channel)) {
+                                                       /* these two channels are of different types */
+                                                       draw_non_connectable_indicator (cr, x, y);
+                                               } else {
+                                                       /* these two channels might be associated */
+                                                       PortMatrixNode::State const s = _matrix->get_state (c);
+                                                       
+                                                       switch (s) {
+                                                       case PortMatrixNode::ASSOCIATED:
+                                                               draw_association_indicator (cr, x, y);
+                                                               break;
+                                                               
+                                                       case PortMatrixNode::NOT_ASSOCIATED:
+                                                               break;
+                                                               
+                                                       default:
+                                                               break;
+                                                       }
                                                }
 
-                                               ty += grid_spacing();
+                                               y += grid_spacing();
                                        }
 
-                                       tx += grid_spacing();
+                                       x += grid_spacing();
                                }
 
-                               by += j->bundle->nchannels () * grid_spacing();
+                               by += _matrix->count_of_our_type ((*j)->bundle->nchannels()) * grid_spacing();
                        }
 
-                       bx += i->bundle->nchannels () * grid_spacing();
+                       bx += _matrix->count_of_our_type ((*i)->bundle->nchannels()) * grid_spacing();
                }
        }
 }
@@ -259,26 +251,43 @@ PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
        cairo_fill (cr);
 }
 
+/** Draw a square to indicate that two channels in a matrix cannot be associated
+ *  with each other.
+ */
+void
+PortMatrixGrid::draw_non_connectable_indicator (cairo_t* cr, uint32_t x, uint32_t y)
+{
+       set_source_rgb (cr, non_connectable_colour ());
+       cairo_rectangle (
+               cr,
+               x + thick_grid_line_width(),
+               y + thick_grid_line_width(),
+               grid_spacing() - 2 * thick_grid_line_width(),
+               grid_spacing() - 2 * thick_grid_line_width()
+               );
+       cairo_fill (cr);
+}
+
 PortMatrixNode
 PortMatrixGrid::position_to_node (double x, double y) const
 {
        return PortMatrixNode (
-               position_to_group_and_channel (y, x, _matrix->rows()).second,
-               position_to_group_and_channel (x, y, _matrix->columns()).second
+               position_to_channel (y, x, _matrix->visible_rows()),
+               position_to_channel (x, y, _matrix->visible_columns())
                );
 }
 
 void
-PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
+PortMatrixGrid::button_press (double x, double y, int b, uint32_t t, guint)
 {
-       pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> px = position_to_group_and_channel (x, y, _matrix->columns());
-       pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> py = position_to_group_and_channel (y, x, _matrix->rows());
+       ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
+       ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
 
        if (b == 1) {
-
+               
                _dragging = true;
-               _drag_valid = (px.second.bundle && py.second.bundle);
-
+               _drag_valid = (px.bundle && py.bundle);
+               
                _moved = false;
                _drag_start_x = x / grid_spacing ();
                _drag_start_y = y / grid_spacing ();
@@ -299,9 +308,13 @@ PortMatrixGrid::get_association (PortMatrixNode node) const
                bool have_diagonal_association = false;
                bool have_diagonal_not_association = false;
 
-               for (uint32_t i = 0; i < node.row.bundle->nchannels (); ++i) {
+               for (uint32_t i = 0; i < node.row.bundle->nchannels().n_total(); ++i) {
 
-                       for (uint32_t j = 0; j < node.column.bundle->nchannels (); ++j) {
+                       for (uint32_t j = 0; j < node.column.bundle->nchannels().n_total(); ++j) {
+
+                               if (!_matrix->should_show (node.row.bundle->channel_type(i)) || !_matrix->should_show (node.column.bundle->channel_type(j))) {
+                                       continue;
+                               }
 
                                ARDOUR::BundleChannel c[2];
                                c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
@@ -356,9 +369,13 @@ PortMatrixGrid::set_association (PortMatrixNode node, bool s)
 {
        if (_matrix->show_only_bundles()) {
 
-               for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
-                       for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
+               for (uint32_t i = 0; i < node.column.bundle->nchannels().n_total(); ++i) {
+                       for (uint32_t j = 0; j < node.row.bundle->nchannels().n_total(); ++j) {
 
+                               if (!_matrix->should_show (node.column.bundle->channel_type(i)) || !_matrix->should_show (node.row.bundle->channel_type(j))) {
+                                       continue;
+                               }
+                               
                                ARDOUR::BundleChannel c[2];
                                c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
                                c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
@@ -379,33 +396,57 @@ PortMatrixGrid::set_association (PortMatrixNode node, bool s)
 }
 
 void
-PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/)
+PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/, guint s)
 {
        if (b == 1) {
 
-               if (_dragging && _moved) {
-
-                       if (_drag_valid) {
-                               list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
-
-                               if (!p.empty()) {
-                                       PortMatrixNode::State const s = get_association (p.front());
-                                       for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
-                                               set_association (*i, toggle_state (s));
+               if (x != -1) {
+                       
+                       if (_dragging && _moved) {
+                               
+                               if (_drag_valid) {
+                                       list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
+                                       
+                                       if (!p.empty()) {
+                                               PortMatrixNode::State const s = get_association (p.front());
+                                               for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
+                                                       set_association (*i, toggle_state (s));
+                                               }
+                                       }
+                               }
+                               
+                       } else {
+                               
+                               if (Keyboard::modifier_state_equals (s, Keyboard::PrimaryModifier)) {
+                                       /* associate/disassociate things diagonally down and right until we run out */
+                                       PortMatrixNode::State s = (PortMatrixNode::State) 0;
+                                       while (1) {
+                                               PortMatrixNode const n = position_to_node (x, y);
+                                               if (n.row.bundle && n.column.bundle) {
+                                                       if (s == (PortMatrixNode::State) 0) {
+                                                               s = get_association (n);
+                                                       }
+                                                       set_association (n, toggle_state (s));
+                                               } else {
+                                                       break;
+                                               }
+                                               x += grid_spacing ();
+                                               y += grid_spacing ();
+                                       }
+                                       
+                               } else {
+                                       
+                                       PortMatrixNode const n = position_to_node (x, y);
+                                       if (n.row.bundle && n.column.bundle) {
+                                               PortMatrixNode::State const s = get_association (n);
+                                               set_association (n, toggle_state (s));
                                        }
                                }
                        }
 
-               } else {
-
-                       PortMatrixNode const n = position_to_node (x, y);
-                       if (n.row.bundle && n.column.bundle) {
-                               PortMatrixNode::State const s = get_association (n);
-                               set_association (n, toggle_state (s));
-                       }
+                       require_render ();
                }
-
-               require_render ();
+               
                _body->queue_draw ();
        }
 
@@ -423,8 +464,8 @@ PortMatrixGrid::draw_extra (cairo_t* cr)
 
        for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
        
-               double const x = component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing()) + grid_spacing() / 2;
-               double const y = component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing()) + grid_spacing() / 2;
+               double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
+               double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
 
                if (i->row.bundle && i->column.bundle) {
 
@@ -458,14 +499,14 @@ PortMatrixGrid::draw_extra (cairo_t* cr)
                                if (s) {
                                        draw_association_indicator (
                                                cr,
-                                               component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
-                                               component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
+                                               component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
+                                               component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
                                                );
                                } else {
                                        draw_empty_square (
                                                cr,
-                                               component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
-                                               component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
+                                               component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
+                                               component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
                                                );
                                }
                        }
@@ -523,7 +564,7 @@ PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
                
                if (i->row.bundle) {
 
-                       double const y = channel_to_position (i->row, _matrix->rows()) * grid_spacing ();
+                       double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
                        _body->queue_draw_area (
                                _parent_rectangle.get_x(),
                                component_to_parent_y (y),
@@ -534,7 +575,7 @@ PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
 
                if (i->column.bundle) {
 
-                       double const x = channel_to_position (i->column, _matrix->columns()) * grid_spacing ();
+                       double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
                        
                        _body->queue_draw_area (
                                component_to_parent_x (x),