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),
40 PortMatrixGrid::compute_dimensions ()
44 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
45 _width += group_size (*i) * grid_spacing ();
49 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
50 _height += group_size (*i) * grid_spacing ();
56 PortMatrixGrid::render (cairo_t* cr)
58 set_source_rgb (cr, background_colour());
59 cairo_rectangle (cr, 0, 0, _width, _height);
63 for (PortGroupList::List::const_iterator c = _matrix->columns()->begin(); c != _matrix->columns()->end(); ++c) {
66 for (PortGroupList::List::const_iterator r = _matrix->rows()->begin(); r != _matrix->rows()->end(); ++r) {
68 if ((*c)->visible() && (*r)->visible()) {
69 render_group_pair (cr, *r, *c, x, y);
72 y += group_size (*r) * grid_spacing ();
75 x += group_size (*c) * grid_spacing ();
80 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)
82 PortGroup::BundleList const & row_bundles = row->bundles();
83 PortGroup::BundleList const & column_bundles = column->bundles();
85 /* unfortunately we need to compute the height of the row group here */
86 uint32_t height = group_size (row) * grid_spacing ();
90 /* VERTICAL GRID LINES */
92 set_source_rgb (cr, grid_colour());
95 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
97 cairo_set_line_width (cr, thick_grid_line_width());
98 cairo_move_to (cr, tx, y);
99 cairo_line_to (cr, tx, y + height);
102 if (!_matrix->show_only_bundles()) {
103 cairo_set_line_width (cr, thin_grid_line_width());
104 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
105 tx += grid_spacing ();
106 cairo_move_to (cr, tx, y);
107 cairo_line_to (cr, tx, y + height);
113 tx += grid_spacing ();
120 uint32_t const width = tx - x;
124 /* HORIZONTAL GRID LINES */
127 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
129 cairo_set_line_width (cr, thick_grid_line_width());
130 cairo_move_to (cr, x, ty);
131 cairo_line_to (cr, x + width, ty);
134 if (!_matrix->show_only_bundles()) {
135 cairo_set_line_width (cr, thin_grid_line_width());
136 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
137 ty += grid_spacing ();
138 cairo_move_to (cr, x, ty);
139 cairo_line_to (cr, x + width, ty);
145 ty += grid_spacing ();
152 /* ASSOCIATION INDICATORS */
157 if (_matrix->show_only_bundles()) {
159 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
162 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
164 PortMatrixNode::State s = get_association (PortMatrixNode (
165 ARDOUR::BundleChannel (i->bundle, 0),
166 ARDOUR::BundleChannel (j->bundle, 0)
169 case PortMatrixNode::ASSOCIATED:
170 draw_association_indicator (cr, bx, by);
172 case PortMatrixNode::PARTIAL:
173 draw_association_indicator (cr, bx, by, 0.5);
179 by += grid_spacing();
182 bx += grid_spacing();
188 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
191 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
194 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
197 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
199 ARDOUR::BundleChannel c[2];
200 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
201 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
203 PortMatrixNode::State const s = _matrix->get_state (c);
206 case PortMatrixNode::ASSOCIATED:
207 draw_association_indicator (cr, tx, ty);
210 case PortMatrixNode::NOT_ASSOCIATED:
217 ty += grid_spacing();
220 tx += grid_spacing();
223 by += j->bundle->nchannels () * grid_spacing();
226 bx += i->bundle->nchannels () * grid_spacing();
232 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
234 set_source_rgba (cr, association_colour(), 0.5);
238 x + grid_spacing() / 2,
239 y + grid_spacing() / 2,
240 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
249 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
251 set_source_rgb (cr, background_colour());
254 x + thick_grid_line_width(),
255 y + thick_grid_line_width(),
256 grid_spacing() - 2 * thick_grid_line_width(),
257 grid_spacing() - 2 * thick_grid_line_width()
263 PortMatrixGrid::position_to_node (uint32_t x, uint32_t y) const
265 return PortMatrixNode (
266 position_to_group_and_channel (y, _matrix->rows()).second,
267 position_to_group_and_channel (x, _matrix->columns()).second
272 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
274 pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> px = position_to_group_and_channel (x / grid_spacing(), _matrix->columns());
275 pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> py = position_to_group_and_channel (y / grid_spacing(), _matrix->rows());
280 _drag_valid = (px.second.bundle && py.second.bundle);
283 _drag_start_x = x / grid_spacing ();
284 _drag_start_y = y / grid_spacing ();
288 _matrix->popup_menu (px, py, t);
293 PortMatrixNode::State
294 PortMatrixGrid::get_association (PortMatrixNode node) const
296 if (_matrix->show_only_bundles()) {
298 bool have_unknown = false;
299 bool have_off_diagonal_association = false;
300 bool have_diagonal_association = false;
301 bool have_diagonal_not_association = false;
303 for (uint32_t i = 0; i < node.row.bundle->nchannels (); ++i) {
305 for (uint32_t j = 0; j < node.column.bundle->nchannels (); ++j) {
307 ARDOUR::BundleChannel c[2];
308 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
309 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
311 PortMatrixNode::State const s = _matrix->get_state (c);
314 case PortMatrixNode::ASSOCIATED:
316 have_diagonal_association = true;
318 have_off_diagonal_association = true;
322 case PortMatrixNode::NOT_ASSOCIATED:
324 have_diagonal_not_association = true;
334 if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
335 return PortMatrixNode::ASSOCIATED;
336 } else if (!have_diagonal_association && !have_off_diagonal_association) {
337 return PortMatrixNode::NOT_ASSOCIATED;
340 return PortMatrixNode::PARTIAL;
344 ARDOUR::BundleChannel c[2];
345 c[_matrix->column_index()] = node.column;
346 c[_matrix->row_index()] = node.row;
347 return _matrix->get_state (c);
352 return PortMatrixNode::NOT_ASSOCIATED;
356 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
358 if (_matrix->show_only_bundles()) {
360 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
361 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
363 ARDOUR::BundleChannel c[2];
364 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
365 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
366 _matrix->set_state (c, s && (i == j));
372 if (node.row.bundle && node.column.bundle) {
374 ARDOUR::BundleChannel c[2];
375 c[_matrix->row_index()] = node.row;
376 c[_matrix->column_index()] = node.column;
377 _matrix->set_state (c, s);
383 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/)
387 if (_dragging && _moved) {
390 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
393 PortMatrixNode::State const s = get_association (p.front());
394 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
395 set_association (*i, toggle_state (s));
402 PortMatrixNode const n = position_to_node (x / grid_spacing(), y / grid_spacing());
403 if (n.row.bundle && n.column.bundle) {
404 PortMatrixNode::State const s = get_association (n);
405 set_association (n, toggle_state (s));
410 _body->queue_draw ();
418 PortMatrixGrid::draw_extra (cairo_t* cr)
420 set_source_rgba (cr, mouseover_line_colour(), 0.3);
421 cairo_set_line_width (cr, mouseover_line_width());
423 double const x = component_to_parent_x (channel_to_position (_body->mouseover().column, _matrix->columns()) * grid_spacing()) + grid_spacing() / 2;
424 double const y = component_to_parent_y (channel_to_position (_body->mouseover().row, _matrix->rows()) * grid_spacing()) + grid_spacing() / 2;
426 if (_body->mouseover().row.bundle && _body->mouseover().column.bundle) {
428 cairo_move_to (cr, x, y);
429 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
430 cairo_line_to (cr, component_to_parent_x (0), y);
431 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
432 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
436 cairo_move_to (cr, x, y);
437 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
438 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
439 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
440 cairo_line_to (cr, x, component_to_parent_y (0));
445 if (_dragging && _drag_valid && _moved) {
447 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
451 bool const s = toggle_state (get_association (p.front()));
453 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
455 draw_association_indicator (
457 component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
458 component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
463 component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
464 component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
470 set_source_rgba (cr, association_colour (), 0.3);
474 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
475 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
480 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
481 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
490 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
492 queue_draw_for (old);
493 queue_draw_for (_body->mouseover());
497 PortMatrixGrid::motion (double x, double y)
499 _body->set_mouseover (position_to_node (x / grid_spacing(), y / grid_spacing()));
501 int const px = x / grid_spacing ();
502 int const py = y / grid_spacing ();
504 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
508 if (_dragging && _drag_valid && _moved) {
511 _body->queue_draw ();
516 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
520 double const y = channel_to_position (n.row, _matrix->rows()) * grid_spacing ();
521 _body->queue_draw_area (
522 _parent_rectangle.get_x(),
523 component_to_parent_y (y),
524 _parent_rectangle.get_width(),
529 if (n.column.bundle) {
531 double const x = channel_to_position (n.column, _matrix->columns()) * grid_spacing ();
533 _body->queue_draw_area (
534 component_to_parent_x (x),
535 _parent_rectangle.get_y(),
537 _parent_rectangle.get_height()
543 PortMatrixGrid::component_to_parent_x (double x) const
545 return x - _body->xoffset() + _parent_rectangle.get_x();
549 PortMatrixGrid::parent_to_component_x (double x) const
551 return x + _body->xoffset() - _parent_rectangle.get_x();
555 PortMatrixGrid::component_to_parent_y (double y) const
557 return y - _body->yoffset() + _parent_rectangle.get_y();
561 PortMatrixGrid::parent_to_component_y (double y) const
563 return y + _body->yoffset() - _parent_rectangle.get_y();
567 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
569 list<PortMatrixNode> p;
571 bool const steep = abs (y1 - y0) > abs (x1 - x0);
593 int dy = abs (y1 - y0);
596 double derr = double (dy) / dx;
599 int const ystep = y0 < y1 ? 1 : -1;
601 for (int x = x0; x <= x1; ++x) {
603 PortMatrixNode n = position_to_node (y, x);
604 if (n.row.bundle && n.column.bundle) {
608 PortMatrixNode n = position_to_node (x, y);
609 if (n.row.bundle && n.column.bundle) {
626 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
628 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);