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);
149 bx += column_width();
154 for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
157 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
160 for (uint32_t k = 0; k < (*i)->nchannels (); ++k) {
163 for (uint32_t l = 0; l < (*j)->nchannels (); ++l) {
165 ARDOUR::BundleChannel c[2];
166 c[_matrix->column_index()] = ARDOUR::BundleChannel (*i, k);
167 c[_matrix->row_index()] = ARDOUR::BundleChannel (*j, l);
169 PortMatrixNode::State const s = _matrix->get_state (c);
172 case PortMatrixNode::ASSOCIATED:
173 draw_association_indicator (cr, x, y);
176 case PortMatrixNode::UNKNOWN:
177 draw_unknown_indicator (cr, x, y);
180 case PortMatrixNode::NOT_ASSOCIATED:
190 by += (*j)->nchannels () * row_height();
193 bx += (*i)->nchannels () * column_width();
199 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
201 set_source_rgba (cr, association_colour(), 0.5);
204 x + column_width() / 2,
205 y + column_width() / 2,
206 (column_width() - (2 * connection_indicator_pad())) / 2,
215 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
217 set_source_rgba (cr, unknown_colour(), 0.5);
220 x + thick_grid_line_width(),
221 y + thick_grid_line_width(),
222 column_width() - 2 * thick_grid_line_width(),
223 row_height() - 2 * thick_grid_line_width()
229 PortMatrixGrid::position_to_node (double x, double y) const
231 return PortMatrixNode (
232 position_to_channel (y, _matrix->rows()->bundles(), row_height()),
233 position_to_channel (x, _matrix->columns()->bundles(), column_width())
238 ARDOUR::BundleChannel
239 PortMatrixGrid::position_to_channel (double p, ARDOUR::BundleList const& bundles, double inc) const
241 uint32_t pos = p / inc;
243 if (_matrix->show_only_bundles()) {
245 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
247 return ARDOUR::BundleChannel (*i, 0);
255 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
256 if (pos < (*i)->nchannels()) {
257 return ARDOUR::BundleChannel (*i, pos);
259 pos -= (*i)->nchannels();
265 return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
270 PortMatrixGrid::channel_position (
271 ARDOUR::BundleChannel bc,
272 ARDOUR::BundleList const& bundles,
277 ARDOUR::BundleList::const_iterator i = bundles.begin ();
278 while (i != bundles.end() && *i != bc.bundle) {
280 if (_matrix->show_only_bundles()) {
283 p += inc * (*i)->nchannels();
289 if (i == bundles.end()) {
293 p += inc * bc.channel;
299 PortMatrixGrid::button_press (double x, double y, int b)
301 PortMatrixNode const node = position_to_node (x, y);
303 if (_matrix->show_only_bundles()) {
305 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
307 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
308 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
310 ARDOUR::BundleChannel c[2];
311 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
312 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
313 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
314 _matrix->set_state (c, i == j);
316 _matrix->set_state (c, false);
323 if (node.row.bundle && node.column.bundle) {
325 ARDOUR::BundleChannel c[2];
326 c[_matrix->row_index()] = node.row;
327 c[_matrix->column_index()] = node.column;
329 PortMatrixNode::State const s = _matrix->get_state (c);
331 if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
333 bool const n = !(s == PortMatrixNode::ASSOCIATED);
335 ARDOUR::BundleChannel c[2];
336 c[_matrix->row_index()] = node.row;
337 c[_matrix->column_index()] = node.column;
339 _matrix->set_state (c, n);
346 _body->queue_draw ();
350 PortMatrixGrid::draw_extra (cairo_t* cr)
352 set_source_rgba (cr, mouseover_line_colour(), 0.3);
353 cairo_set_line_width (cr, mouseover_line_width());
355 double const x = component_to_parent_x (
356 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
359 double const y = component_to_parent_y (
360 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
363 if (_body->mouseover().row.bundle) {
365 cairo_move_to (cr, x, y);
366 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
367 cairo_line_to (cr, component_to_parent_x (0), y);
368 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
369 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
374 if (_body->mouseover().column.bundle) {
376 cairo_move_to (cr, x, y);
377 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
378 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
379 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
380 cairo_line_to (cr, x, component_to_parent_y (0));
387 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
389 queue_draw_for (old);
390 queue_draw_for (_body->mouseover());
394 PortMatrixGrid::mouseover_event (double x, double y)
396 _body->set_mouseover (position_to_node (x, y));
400 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
404 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
405 _body->queue_draw_area (
406 _parent_rectangle.get_x(),
407 component_to_parent_y (y),
408 _parent_rectangle.get_width(),
413 if (n.column.bundle) {
415 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
417 _body->queue_draw_area (
418 component_to_parent_x (x),
419 _parent_rectangle.get_y(),
421 _parent_rectangle.get_height()
427 PortMatrixGrid::component_to_parent_x (double x) const
429 return x - _body->xoffset() + _parent_rectangle.get_x();
433 PortMatrixGrid::parent_to_component_x (double x) const
435 return x + _body->xoffset() - _parent_rectangle.get_x();
439 PortMatrixGrid::component_to_parent_y (double y) const
441 return y - _body->yoffset() + _parent_rectangle.get_y();
445 PortMatrixGrid::parent_to_component_y (double y) const
447 return y + _body->yoffset() - _parent_rectangle.get_y();
450 PortMatrixNode::State
451 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
453 bool have_unknown = false;
454 bool have_off_diagonal_association = false;
455 bool have_diagonal_association = false;
456 bool have_diagonal_not_association = false;
458 for (uint32_t i = 0; i < a->nchannels (); ++i) {
460 for (uint32_t j = 0; j < b->nchannels (); ++j) {
462 ARDOUR::BundleChannel c[2];
463 c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
464 c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
466 PortMatrixNode::State const s = _matrix->get_state (c);
469 case PortMatrixNode::ASSOCIATED:
471 have_diagonal_association = true;
473 have_off_diagonal_association = true;
477 case PortMatrixNode::UNKNOWN:
481 case PortMatrixNode::NOT_ASSOCIATED:
483 have_diagonal_not_association = true;
491 return PortMatrixNode::UNKNOWN;
492 } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
493 return PortMatrixNode::ASSOCIATED;
494 } else if (!have_diagonal_association && !have_off_diagonal_association) {
495 return PortMatrixNode::NOT_ASSOCIATED;
498 return PortMatrixNode::PARTIAL;