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"
28 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
29 : PortMatrixComponent (m, b)
35 PortMatrixGrid::compute_dimensions ()
38 PortGroup::BundleList const c = _matrix->columns()->bundles();
39 if (_matrix->show_only_bundles()) {
40 _width = c.size() * column_width();
42 for (PortGroup::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
43 _width += i->bundle->nchannels() * column_width();
48 PortGroup::BundleList const r = _matrix->rows()->bundles();
49 if (_matrix->show_only_bundles()) {
50 _height = r.size() * column_width();
52 for (PortGroup::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
53 _height += i->bundle->nchannels() * row_height();
60 PortMatrixGrid::render (cairo_t* cr)
64 set_source_rgb (cr, background_colour());
65 cairo_rectangle (cr, 0, 0, _width, _height);
68 /* VERTICAL GRID LINES */
70 set_source_rgb (cr, grid_colour());
72 PortGroup::BundleList const c = _matrix->columns()->bundles();
74 for (PortGroup::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
76 if (!_matrix->show_only_bundles()) {
77 cairo_set_line_width (cr, thin_grid_line_width());
78 for (uint32_t j = 1; j < i->bundle->nchannels(); ++j) {
80 cairo_move_to (cr, x, 0);
81 cairo_line_to (cr, x, _height);
86 if (N < (c.size() - 1)) {
88 cairo_set_line_width (cr, thick_grid_line_width());
89 cairo_move_to (cr, x, 0);
90 cairo_line_to (cr, x, _height);
97 uint32_t grid_width = x + column_width();
99 /* HORIZONTAL GRID LINES */
103 PortGroup::BundleList const r = _matrix->rows()->bundles();
104 for (PortGroup::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
106 if (!_matrix->show_only_bundles()) {
107 cairo_set_line_width (cr, thin_grid_line_width());
108 for (uint32_t j = 1; j < i->bundle->nchannels(); ++j) {
110 cairo_move_to (cr, 0, y);
111 cairo_line_to (cr, grid_width, y);
116 if (N < (r.size() - 1)) {
118 cairo_set_line_width (cr, thick_grid_line_width());
119 cairo_move_to (cr, 0, y);
120 cairo_line_to (cr, grid_width, y);
127 /* ASSOCIATION INDICATORS */
132 if (_matrix->show_only_bundles()) {
134 for (PortGroup::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
137 for (PortGroup::BundleList::const_iterator j = r.begin(); j != r.end(); ++j) {
139 PortMatrixNode::State s = bundle_to_bundle_state (i->bundle, j->bundle);
141 case PortMatrixNode::UNKNOWN:
142 draw_unknown_indicator (cr, bx, by);
144 case PortMatrixNode::ASSOCIATED:
145 draw_association_indicator (cr, bx, by);
147 case PortMatrixNode::PARTIAL:
148 draw_association_indicator (cr, bx, by, 0.5);
157 bx += column_width();
162 for (PortGroup::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
165 for (PortGroup::BundleList::const_iterator j = r.begin(); j != r.end(); ++j) {
168 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
171 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
173 ARDOUR::BundleChannel c[2];
174 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
175 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
177 PortMatrixNode::State const s = _matrix->get_state (c);
180 case PortMatrixNode::ASSOCIATED:
181 draw_association_indicator (cr, x, y);
184 case PortMatrixNode::UNKNOWN:
185 draw_unknown_indicator (cr, x, y);
188 case PortMatrixNode::NOT_ASSOCIATED:
201 by += j->bundle->nchannels () * row_height();
204 bx += i->bundle->nchannels () * column_width();
210 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
212 set_source_rgba (cr, association_colour(), 0.5);
215 x + column_width() / 2,
216 y + column_width() / 2,
217 (column_width() - (2 * connection_indicator_pad())) / 2,
226 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
228 set_source_rgba (cr, unknown_colour(), 0.5);
231 x + thick_grid_line_width(),
232 y + thick_grid_line_width(),
233 column_width() - 2 * thick_grid_line_width(),
234 row_height() - 2 * thick_grid_line_width()
240 PortMatrixGrid::position_to_node (double x, double y) const
242 return PortMatrixNode (
243 position_to_channel (y, _matrix->rows()->bundles(), row_height()),
244 position_to_channel (x, _matrix->columns()->bundles(), column_width())
249 ARDOUR::BundleChannel
250 PortMatrixGrid::position_to_channel (double p, PortGroup::BundleList const & bundles, double inc) const
252 uint32_t pos = p / inc;
254 if (_matrix->show_only_bundles()) {
256 for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
258 return ARDOUR::BundleChannel (i->bundle, 0);
266 for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
267 if (pos < i->bundle->nchannels()) {
268 return ARDOUR::BundleChannel (i->bundle, pos);
270 pos -= i->bundle->nchannels();
276 return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
281 PortMatrixGrid::channel_position (
282 ARDOUR::BundleChannel bc,
283 PortGroup::BundleList const& bundles,
288 PortGroup::BundleList::const_iterator i = bundles.begin ();
289 while (i != bundles.end() && i->bundle != bc.bundle) {
291 if (_matrix->show_only_bundles()) {
294 p += inc * i->bundle->nchannels();
300 if (i == bundles.end()) {
304 p += inc * bc.channel;
310 PortMatrixGrid::button_press (double x, double y, int b)
312 PortMatrixNode const node = position_to_node (x, y);
314 if (_matrix->show_only_bundles()) {
316 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
318 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
319 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
321 ARDOUR::BundleChannel c[2];
322 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
323 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
324 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
325 _matrix->set_state (c, i == j);
327 _matrix->set_state (c, false);
334 if (node.row.bundle && node.column.bundle) {
336 ARDOUR::BundleChannel c[2];
337 c[_matrix->row_index()] = node.row;
338 c[_matrix->column_index()] = node.column;
340 PortMatrixNode::State const s = _matrix->get_state (c);
342 if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
344 bool const n = !(s == PortMatrixNode::ASSOCIATED);
346 ARDOUR::BundleChannel c[2];
347 c[_matrix->row_index()] = node.row;
348 c[_matrix->column_index()] = node.column;
350 _matrix->set_state (c, n);
357 _body->queue_draw ();
361 PortMatrixGrid::draw_extra (cairo_t* cr)
363 set_source_rgba (cr, mouseover_line_colour(), 0.3);
364 cairo_set_line_width (cr, mouseover_line_width());
366 double const x = component_to_parent_x (
367 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
370 double const y = component_to_parent_y (
371 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
374 if (_body->mouseover().row.bundle) {
376 cairo_move_to (cr, x, y);
377 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
378 cairo_line_to (cr, component_to_parent_x (0), y);
379 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
380 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
385 if (_body->mouseover().column.bundle) {
387 cairo_move_to (cr, x, y);
388 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
389 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
390 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
391 cairo_line_to (cr, x, component_to_parent_y (0));
398 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
400 queue_draw_for (old);
401 queue_draw_for (_body->mouseover());
405 PortMatrixGrid::mouseover_event (double x, double y)
407 _body->set_mouseover (position_to_node (x, y));
411 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
415 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
416 _body->queue_draw_area (
417 _parent_rectangle.get_x(),
418 component_to_parent_y (y),
419 _parent_rectangle.get_width(),
424 if (n.column.bundle) {
426 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
428 _body->queue_draw_area (
429 component_to_parent_x (x),
430 _parent_rectangle.get_y(),
432 _parent_rectangle.get_height()
438 PortMatrixGrid::component_to_parent_x (double x) const
440 return x - _body->xoffset() + _parent_rectangle.get_x();
444 PortMatrixGrid::parent_to_component_x (double x) const
446 return x + _body->xoffset() - _parent_rectangle.get_x();
450 PortMatrixGrid::component_to_parent_y (double y) const
452 return y - _body->yoffset() + _parent_rectangle.get_y();
456 PortMatrixGrid::parent_to_component_y (double y) const
458 return y + _body->yoffset() - _parent_rectangle.get_y();
461 PortMatrixNode::State
462 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
464 bool have_unknown = false;
465 bool have_off_diagonal_association = false;
466 bool have_diagonal_association = false;
467 bool have_diagonal_not_association = false;
469 for (uint32_t i = 0; i < a->nchannels (); ++i) {
471 for (uint32_t j = 0; j < b->nchannels (); ++j) {
473 ARDOUR::BundleChannel c[2];
474 c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
475 c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
477 PortMatrixNode::State const s = _matrix->get_state (c);
480 case PortMatrixNode::ASSOCIATED:
482 have_diagonal_association = true;
484 have_off_diagonal_association = true;
488 case PortMatrixNode::UNKNOWN:
492 case PortMatrixNode::NOT_ASSOCIATED:
494 have_diagonal_not_association = true;
505 return PortMatrixNode::UNKNOWN;
506 } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
507 return PortMatrixNode::ASSOCIATED;
508 } else if (!have_diagonal_association && !have_off_diagonal_association) {
509 return PortMatrixNode::NOT_ASSOCIATED;
512 return PortMatrixNode::PARTIAL;