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 ARDOUR::BundleList const c = _matrix->columns()->bundles();
39 if (_matrix->show_only_bundles()) {
40 _width = c.size() * column_width();
42 for (ARDOUR::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
43 _width += (*i)->nchannels() * column_width();
48 ARDOUR::BundleList const r = _matrix->rows()->bundles();
49 if (_matrix->show_only_bundles()) {
50 _height = r.size() * column_width();
52 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
53 _height += (*i)->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 ARDOUR::BundleList const c = _matrix->columns()->bundles();
73 for (ARDOUR::BundleList::size_type i = 0; i < c.size(); ++i) {
75 if (!_matrix->show_only_bundles()) {
76 cairo_set_line_width (cr, thin_grid_line_width());
77 for (uint32_t j = 1; j < c[i]->nchannels(); ++j) {
79 cairo_move_to (cr, x, 0);
80 cairo_line_to (cr, x, _height);
85 if (i < (c.size() - 1)) {
87 cairo_set_line_width (cr, thick_grid_line_width());
88 cairo_move_to (cr, x, 0);
89 cairo_line_to (cr, x, _height);
94 uint32_t grid_width = x + column_width();
96 /* HORIZONTAL GRID LINES */
99 ARDOUR::BundleList const r = _matrix->rows()->bundles();
100 for (ARDOUR::BundleList::size_type i = 0; i < r.size(); ++i) {
102 if (!_matrix->show_only_bundles()) {
103 cairo_set_line_width (cr, thin_grid_line_width());
104 for (uint32_t j = 1; j < r[i]->nchannels(); ++j) {
106 cairo_move_to (cr, 0, y);
107 cairo_line_to (cr, grid_width, y);
112 if (i < (r.size() - 1)) {
114 cairo_set_line_width (cr, thick_grid_line_width());
115 cairo_move_to (cr, 0, y);
116 cairo_line_to (cr, grid_width, y);
121 /* ASSOCIATION INDICATORS */
126 if (_matrix->show_only_bundles()) {
128 for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
131 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
133 PortMatrixNode::State s = bundle_to_bundle_state (*i, *j);
135 case PortMatrixNode::UNKNOWN:
136 draw_unknown_indicator (cr, bx, by);
138 case PortMatrixNode::ASSOCIATED:
139 draw_association_indicator (cr, bx, by);
141 case PortMatrixNode::PARTIAL:
142 draw_association_indicator (cr, bx, by, 0.5);
151 bx += column_width();
156 for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
159 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
162 for (uint32_t k = 0; k < (*i)->nchannels (); ++k) {
165 for (uint32_t l = 0; l < (*j)->nchannels (); ++l) {
167 ARDOUR::BundleChannel c[2];
168 c[_matrix->column_index()] = ARDOUR::BundleChannel (*i, k);
169 c[_matrix->row_index()] = ARDOUR::BundleChannel (*j, l);
171 PortMatrixNode::State const s = _matrix->get_state (c);
174 case PortMatrixNode::ASSOCIATED:
175 draw_association_indicator (cr, x, y);
178 case PortMatrixNode::UNKNOWN:
179 draw_unknown_indicator (cr, x, y);
182 case PortMatrixNode::NOT_ASSOCIATED:
195 by += (*j)->nchannels () * row_height();
198 bx += (*i)->nchannels () * column_width();
204 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
206 set_source_rgba (cr, association_colour(), 0.5);
209 x + column_width() / 2,
210 y + column_width() / 2,
211 (column_width() - (2 * connection_indicator_pad())) / 2,
220 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
222 set_source_rgba (cr, unknown_colour(), 0.5);
225 x + thick_grid_line_width(),
226 y + thick_grid_line_width(),
227 column_width() - 2 * thick_grid_line_width(),
228 row_height() - 2 * thick_grid_line_width()
234 PortMatrixGrid::position_to_node (double x, double y) const
236 return PortMatrixNode (
237 position_to_channel (y, _matrix->rows()->bundles(), row_height()),
238 position_to_channel (x, _matrix->columns()->bundles(), column_width())
243 ARDOUR::BundleChannel
244 PortMatrixGrid::position_to_channel (double p, ARDOUR::BundleList const& bundles, double inc) const
246 uint32_t pos = p / inc;
248 if (_matrix->show_only_bundles()) {
250 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
252 return ARDOUR::BundleChannel (*i, 0);
260 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
261 if (pos < (*i)->nchannels()) {
262 return ARDOUR::BundleChannel (*i, pos);
264 pos -= (*i)->nchannels();
270 return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
275 PortMatrixGrid::channel_position (
276 ARDOUR::BundleChannel bc,
277 ARDOUR::BundleList const& bundles,
282 ARDOUR::BundleList::const_iterator i = bundles.begin ();
283 while (i != bundles.end() && *i != bc.bundle) {
285 if (_matrix->show_only_bundles()) {
288 p += inc * (*i)->nchannels();
294 if (i == bundles.end()) {
298 p += inc * bc.channel;
304 PortMatrixGrid::button_press (double x, double y, int b)
306 PortMatrixNode const node = position_to_node (x, y);
308 if (_matrix->show_only_bundles()) {
310 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
312 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
313 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
315 ARDOUR::BundleChannel c[2];
316 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
317 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
318 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
319 _matrix->set_state (c, i == j);
321 _matrix->set_state (c, false);
328 if (node.row.bundle && node.column.bundle) {
330 ARDOUR::BundleChannel c[2];
331 c[_matrix->row_index()] = node.row;
332 c[_matrix->column_index()] = node.column;
334 PortMatrixNode::State const s = _matrix->get_state (c);
336 if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
338 bool const n = !(s == PortMatrixNode::ASSOCIATED);
340 ARDOUR::BundleChannel c[2];
341 c[_matrix->row_index()] = node.row;
342 c[_matrix->column_index()] = node.column;
344 _matrix->set_state (c, n);
351 _body->queue_draw ();
355 PortMatrixGrid::draw_extra (cairo_t* cr)
357 set_source_rgba (cr, mouseover_line_colour(), 0.3);
358 cairo_set_line_width (cr, mouseover_line_width());
360 double const x = component_to_parent_x (
361 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
364 double const y = component_to_parent_y (
365 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
368 if (_body->mouseover().row.bundle) {
370 cairo_move_to (cr, x, y);
371 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
372 cairo_line_to (cr, component_to_parent_x (0), y);
373 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
374 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
379 if (_body->mouseover().column.bundle) {
381 cairo_move_to (cr, x, y);
382 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
383 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
384 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
385 cairo_line_to (cr, x, component_to_parent_y (0));
392 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
394 queue_draw_for (old);
395 queue_draw_for (_body->mouseover());
399 PortMatrixGrid::mouseover_event (double x, double y)
401 _body->set_mouseover (position_to_node (x, y));
405 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
409 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
410 _body->queue_draw_area (
411 _parent_rectangle.get_x(),
412 component_to_parent_y (y),
413 _parent_rectangle.get_width(),
418 if (n.column.bundle) {
420 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
422 _body->queue_draw_area (
423 component_to_parent_x (x),
424 _parent_rectangle.get_y(),
426 _parent_rectangle.get_height()
432 PortMatrixGrid::component_to_parent_x (double x) const
434 return x - _body->xoffset() + _parent_rectangle.get_x();
438 PortMatrixGrid::parent_to_component_x (double x) const
440 return x + _body->xoffset() - _parent_rectangle.get_x();
444 PortMatrixGrid::component_to_parent_y (double y) const
446 return y - _body->yoffset() + _parent_rectangle.get_y();
450 PortMatrixGrid::parent_to_component_y (double y) const
452 return y + _body->yoffset() - _parent_rectangle.get_y();
455 PortMatrixNode::State
456 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
458 bool have_unknown = false;
459 bool have_off_diagonal_association = false;
460 bool have_diagonal_association = false;
461 bool have_diagonal_not_association = false;
463 for (uint32_t i = 0; i < a->nchannels (); ++i) {
465 for (uint32_t j = 0; j < b->nchannels (); ++j) {
467 ARDOUR::BundleChannel c[2];
468 c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
469 c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
471 PortMatrixNode::State const s = _matrix->get_state (c);
474 case PortMatrixNode::ASSOCIATED:
476 have_diagonal_association = true;
478 have_off_diagonal_association = true;
482 case PortMatrixNode::UNKNOWN:
486 case PortMatrixNode::NOT_ASSOCIATED:
488 have_diagonal_not_association = true;
499 return PortMatrixNode::UNKNOWN;
500 } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
501 return PortMatrixNode::ASSOCIATED;
502 } else if (!have_diagonal_association && !have_off_diagonal_association) {
503 return PortMatrixNode::NOT_ASSOCIATED;
506 return PortMatrixNode::PARTIAL;