2 Copyright (C) 2002-2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <cairo/cairo.h>
22 #include "ardour/bundle.h"
23 #include "ardour/types.h"
24 #include "port_matrix_grid.h"
25 #include "port_matrix.h"
26 #include "port_matrix_body.h"
30 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
31 : PortMatrixComponent (m, b),
39 PortMatrixGrid::compute_dimensions ()
43 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
44 _width += group_size (*i) * grid_spacing ();
48 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
49 _height += group_size (*i) * grid_spacing ();
55 PortMatrixGrid::render (cairo_t* cr)
57 set_source_rgb (cr, background_colour());
58 cairo_rectangle (cr, 0, 0, _width, _height);
62 for (PortGroupList::List::const_iterator c = _matrix->columns()->begin(); c != _matrix->columns()->end(); ++c) {
65 for (PortGroupList::List::const_iterator r = _matrix->rows()->begin(); r != _matrix->rows()->end(); ++r) {
67 if ((*c)->visible() && (*r)->visible()) {
68 render_group_pair (cr, *r, *c, x, y);
71 y += group_size (*r) * grid_spacing ();
74 x += group_size (*c) * grid_spacing ();
79 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)
81 PortGroup::BundleList const & row_bundles = row->bundles();
82 PortGroup::BundleList const & column_bundles = column->bundles();
84 /* unfortunately we need to compute the height of the row group here */
85 uint32_t height = group_size (row) * grid_spacing ();
89 /* VERTICAL GRID LINES */
91 set_source_rgb (cr, grid_colour());
94 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
96 cairo_set_line_width (cr, thick_grid_line_width());
97 cairo_move_to (cr, tx, y);
98 cairo_line_to (cr, tx, y + height);
101 if (!_matrix->show_only_bundles()) {
102 cairo_set_line_width (cr, thin_grid_line_width());
103 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
104 tx += grid_spacing ();
105 cairo_move_to (cr, tx, y);
106 cairo_line_to (cr, tx, y + height);
112 tx += grid_spacing ();
119 uint32_t const width = tx - x;
123 /* HORIZONTAL GRID LINES */
126 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
128 cairo_set_line_width (cr, thick_grid_line_width());
129 cairo_move_to (cr, x, ty);
130 cairo_line_to (cr, x + width, ty);
133 if (!_matrix->show_only_bundles()) {
134 cairo_set_line_width (cr, thin_grid_line_width());
135 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
136 ty += grid_spacing ();
137 cairo_move_to (cr, x, ty);
138 cairo_line_to (cr, x + width, ty);
144 ty += grid_spacing ();
151 /* ASSOCIATION INDICATORS */
156 if (_matrix->show_only_bundles()) {
158 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
161 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
163 PortMatrixNode::State s = bundle_to_bundle_state (i->bundle, j->bundle);
165 case PortMatrixNode::UNKNOWN:
166 draw_unknown_indicator (cr, bx, by);
168 case PortMatrixNode::ASSOCIATED:
169 draw_association_indicator (cr, bx, by);
171 case PortMatrixNode::PARTIAL:
172 draw_association_indicator (cr, bx, by, 0.5);
178 by += grid_spacing();
181 bx += grid_spacing();
187 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
190 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
193 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
196 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
198 ARDOUR::BundleChannel c[2];
199 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
200 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
202 PortMatrixNode::State const s = _matrix->get_state (c);
205 case PortMatrixNode::ASSOCIATED:
206 draw_association_indicator (cr, tx, ty);
209 case PortMatrixNode::UNKNOWN:
210 draw_unknown_indicator (cr, tx, ty);
213 case PortMatrixNode::NOT_ASSOCIATED:
220 ty += grid_spacing();
223 tx += grid_spacing();
226 by += j->bundle->nchannels () * grid_spacing();
229 bx += i->bundle->nchannels () * grid_spacing();
235 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
237 set_source_rgba (cr, association_colour(), 0.5);
241 x + grid_spacing() / 2,
242 y + grid_spacing() / 2,
243 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
252 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
254 set_source_rgba (cr, unknown_colour(), 0.5);
257 x + thick_grid_line_width(),
258 y + thick_grid_line_width(),
259 grid_spacing() - 2 * thick_grid_line_width(),
260 grid_spacing() - 2 * thick_grid_line_width()
266 PortMatrixGrid::position_to_node (uint32_t x, uint32_t y) const
268 return PortMatrixNode (
269 position_to_group_and_channel (y, _matrix->rows()).second,
270 position_to_group_and_channel (x, _matrix->columns()).second
275 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
281 _drag_start_x = x / grid_spacing ();
282 _drag_start_y = y / grid_spacing ();
286 _matrix->popup_menu (
287 position_to_group_and_channel (x / grid_spacing(), _matrix->columns()),
288 position_to_group_and_channel (y / grid_spacing(), _matrix->rows()), t);
294 PortMatrixGrid::set_association (PortMatrixNode node)
296 if (_matrix->show_only_bundles()) {
298 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
299 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
301 ARDOUR::BundleChannel c[2];
302 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
303 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
304 _matrix->set_state (c, true);
310 if (node.row.bundle && node.column.bundle) {
312 ARDOUR::BundleChannel c[2];
313 c[_matrix->row_index()] = node.row;
314 c[_matrix->column_index()] = node.column;
315 _matrix->set_state (c, true);
321 PortMatrixGrid::toggle_association (PortMatrixNode node)
323 if (_matrix->show_only_bundles()) {
325 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
327 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
328 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
330 ARDOUR::BundleChannel c[2];
331 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
332 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
333 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
334 _matrix->set_state (c, i == j);
336 _matrix->set_state (c, false);
343 if (node.row.bundle && node.column.bundle) {
345 ARDOUR::BundleChannel c[2];
346 c[_matrix->row_index()] = node.row;
347 c[_matrix->column_index()] = node.column;
349 PortMatrixNode::State const s = _matrix->get_state (c);
351 if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
352 bool const n = !(s == PortMatrixNode::ASSOCIATED);
353 _matrix->set_state (c, n);
361 PortMatrixGrid::button_release (double x, double y, int b, uint32_t t)
365 if (_dragging && _moved) {
367 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
369 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
370 set_association (*i);
375 toggle_association (position_to_node (x / grid_spacing(), y / grid_spacing()));
379 _body->queue_draw ();
387 PortMatrixGrid::draw_extra (cairo_t* cr)
389 set_source_rgba (cr, mouseover_line_colour(), 0.3);
390 cairo_set_line_width (cr, mouseover_line_width());
392 double const x = component_to_parent_x (channel_to_position (_body->mouseover().column, _matrix->columns()) * grid_spacing()) + grid_spacing() / 2;
393 double const y = component_to_parent_y (channel_to_position (_body->mouseover().row, _matrix->rows()) * grid_spacing()) + grid_spacing() / 2;
395 if (_body->mouseover().row.bundle) {
397 cairo_move_to (cr, x, y);
398 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
399 cairo_line_to (cr, component_to_parent_x (0), y);
400 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
401 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
406 if (_body->mouseover().column.bundle) {
408 cairo_move_to (cr, x, y);
409 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
410 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
411 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
412 cairo_line_to (cr, x, component_to_parent_y (0));
417 if (_dragging && _moved) {
419 set_source_rgba (cr, association_colour (), 0.3);
423 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
424 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
429 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
430 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
435 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
437 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
438 draw_association_indicator (
440 component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
441 component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
448 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
450 queue_draw_for (old);
451 queue_draw_for (_body->mouseover());
455 PortMatrixGrid::motion (double x, double y)
457 _body->set_mouseover (position_to_node (x / grid_spacing(), y / grid_spacing()));
459 int const px = x / grid_spacing ();
460 int const py = y / grid_spacing ();
462 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
466 if (_dragging && _moved) {
469 _body->queue_draw ();
474 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
478 double const y = channel_to_position (n.row, _matrix->rows()) * grid_spacing ();
479 _body->queue_draw_area (
480 _parent_rectangle.get_x(),
481 component_to_parent_y (y),
482 _parent_rectangle.get_width(),
487 if (n.column.bundle) {
489 double const x = channel_to_position (n.column, _matrix->columns()) * grid_spacing ();
491 _body->queue_draw_area (
492 component_to_parent_x (x),
493 _parent_rectangle.get_y(),
495 _parent_rectangle.get_height()
501 PortMatrixGrid::component_to_parent_x (double x) const
503 return x - _body->xoffset() + _parent_rectangle.get_x();
507 PortMatrixGrid::parent_to_component_x (double x) const
509 return x + _body->xoffset() - _parent_rectangle.get_x();
513 PortMatrixGrid::component_to_parent_y (double y) const
515 return y - _body->yoffset() + _parent_rectangle.get_y();
519 PortMatrixGrid::parent_to_component_y (double y) const
521 return y + _body->yoffset() - _parent_rectangle.get_y();
524 PortMatrixNode::State
525 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
527 bool have_unknown = false;
528 bool have_off_diagonal_association = false;
529 bool have_diagonal_association = false;
530 bool have_diagonal_not_association = false;
532 for (uint32_t i = 0; i < a->nchannels (); ++i) {
534 for (uint32_t j = 0; j < b->nchannels (); ++j) {
536 ARDOUR::BundleChannel c[2];
537 c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
538 c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
540 PortMatrixNode::State const s = _matrix->get_state (c);
543 case PortMatrixNode::ASSOCIATED:
545 have_diagonal_association = true;
547 have_off_diagonal_association = true;
551 case PortMatrixNode::UNKNOWN:
555 case PortMatrixNode::NOT_ASSOCIATED:
557 have_diagonal_not_association = true;
568 return PortMatrixNode::UNKNOWN;
569 } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
570 return PortMatrixNode::ASSOCIATED;
571 } else if (!have_diagonal_association && !have_off_diagonal_association) {
572 return PortMatrixNode::NOT_ASSOCIATED;
575 return PortMatrixNode::PARTIAL;
579 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
581 list<PortMatrixNode> p;
583 bool const steep = abs (y1 - y0) > abs (x1 - x0);
605 int dy = abs (y1 - y0);
608 double derr = double (dy) / dx;
611 int const ystep = y0 < y1 ? 1 : -1;
613 for (int x = x0; x <= x1; ++x) {
615 p.push_back (position_to_node (y, x));
617 p.push_back (position_to_node (x, y));