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)
37 PortMatrixGrid::compute_dimensions ()
40 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
41 _width += group_width (*i);
45 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
46 _height += group_height (*i);
52 PortMatrixGrid::render (cairo_t* cr)
54 set_source_rgb (cr, background_colour());
55 cairo_rectangle (cr, 0, 0, _width, _height);
59 for (PortGroupList::List::const_iterator c = _matrix->columns()->begin(); c != _matrix->columns()->end(); ++c) {
62 for (PortGroupList::List::const_iterator r = _matrix->rows()->begin(); r != _matrix->rows()->end(); ++r) {
64 if ((*c)->visible() && (*r)->visible()) {
65 render_group_pair (cr, *r, *c, x, y);
68 y += group_height (*r);
71 x += group_width (*c);
76 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)
78 PortGroup::BundleList const & row_bundles = row->bundles();
79 PortGroup::BundleList const & column_bundles = column->bundles();
81 /* unfortunately we need to compute the height of the row group here */
82 uint32_t height = group_height (row);
86 /* VERTICAL GRID LINES */
88 set_source_rgb (cr, grid_colour());
91 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
93 cairo_set_line_width (cr, thick_grid_line_width());
94 cairo_move_to (cr, tx, y);
95 cairo_line_to (cr, tx, y + height);
98 if (!_matrix->show_only_bundles()) {
99 cairo_set_line_width (cr, thin_grid_line_width());
100 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
101 tx += column_width ();
102 cairo_move_to (cr, tx, y);
103 cairo_line_to (cr, tx, y + height);
109 tx += column_width ();
116 uint32_t const width = tx - x;
120 /* HORIZONTAL GRID LINES */
123 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
125 cairo_set_line_width (cr, thick_grid_line_width());
126 cairo_move_to (cr, x, ty);
127 cairo_line_to (cr, x + width, ty);
130 if (!_matrix->show_only_bundles()) {
131 cairo_set_line_width (cr, thin_grid_line_width());
132 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
134 cairo_move_to (cr, x, ty);
135 cairo_line_to (cr, x + width, ty);
148 /* ASSOCIATION INDICATORS */
153 if (_matrix->show_only_bundles()) {
155 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
158 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
160 PortMatrixNode::State s = bundle_to_bundle_state (i->bundle, j->bundle);
162 case PortMatrixNode::UNKNOWN:
163 draw_unknown_indicator (cr, bx, by);
165 case PortMatrixNode::ASSOCIATED:
166 draw_association_indicator (cr, bx, by);
168 case PortMatrixNode::PARTIAL:
169 draw_association_indicator (cr, bx, by, 0.5);
178 bx += column_width();
184 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
187 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
190 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
193 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
195 ARDOUR::BundleChannel c[2];
196 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
197 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
199 PortMatrixNode::State const s = _matrix->get_state (c);
202 case PortMatrixNode::ASSOCIATED:
203 draw_association_indicator (cr, tx, ty);
206 case PortMatrixNode::UNKNOWN:
207 draw_unknown_indicator (cr, tx, ty);
210 case PortMatrixNode::NOT_ASSOCIATED:
220 tx += column_width();
223 by += j->bundle->nchannels () * row_height();
226 bx += i->bundle->nchannels () * column_width();
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);
237 x + column_width() / 2,
238 y + column_width() / 2,
239 (column_width() - (2 * connection_indicator_pad())) / 2,
248 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
250 set_source_rgba (cr, unknown_colour(), 0.5);
253 x + thick_grid_line_width(),
254 y + thick_grid_line_width(),
255 column_width() - 2 * thick_grid_line_width(),
256 row_height() - 2 * thick_grid_line_width()
262 PortMatrixGrid::position_to_node (double x, double y) const
264 return PortMatrixNode (
265 y_position_to_group_and_channel (y).second,
266 x_position_to_group_and_channel (x).second
271 pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel>
272 PortMatrixGrid::x_position_to_group_and_channel (double x) const
274 PortGroupList::List::const_iterator i = _matrix->columns()->begin();
276 while (i != _matrix->columns()->end()) {
278 uint32_t const gw = group_width (*i);
282 /* it's in this group */
284 PortGroup::BundleList const & bundles = (*i)->bundles ();
285 for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
287 if (_matrix->show_only_bundles()) {
289 if (x < column_width()) {
290 return make_pair (*i, ARDOUR::BundleChannel (j->bundle, 0));
292 x -= column_width ();
297 uint32_t const w = j->bundle->nchannels () * column_width ();
299 return make_pair (*i, ARDOUR::BundleChannel (j->bundle, x / column_width()));
317 return make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0));
323 PortMatrixGrid::channel_position (
324 ARDOUR::BundleChannel bc,
325 PortGroup::BundleList const& bundles,
330 PortGroup::BundleList::const_iterator i = bundles.begin ();
331 while (i != bundles.end() && i->bundle != bc.bundle) {
333 if (_matrix->show_only_bundles()) {
336 p += inc * i->bundle->nchannels();
342 if (i == bundles.end()) {
346 p += inc * bc.channel;
352 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
356 PortMatrixNode const node = position_to_node (x, y);
358 if (_matrix->show_only_bundles()) {
360 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
362 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
363 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
365 ARDOUR::BundleChannel c[2];
366 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
367 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
368 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
369 _matrix->set_state (c, i == j);
371 _matrix->set_state (c, false);
378 if (node.row.bundle && node.column.bundle) {
380 ARDOUR::BundleChannel c[2];
381 c[_matrix->row_index()] = node.row;
382 c[_matrix->column_index()] = node.column;
384 PortMatrixNode::State const s = _matrix->get_state (c);
386 if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
388 bool const n = !(s == PortMatrixNode::ASSOCIATED);
390 ARDOUR::BundleChannel c[2];
391 c[_matrix->row_index()] = node.row;
392 c[_matrix->column_index()] = node.column;
394 _matrix->set_state (c, n);
401 _body->queue_draw ();
405 _matrix->popup_menu (x_position_to_group_and_channel (x), y_position_to_group_and_channel (y), t);
411 PortMatrixGrid::draw_extra (cairo_t* cr)
413 set_source_rgba (cr, mouseover_line_colour(), 0.3);
414 cairo_set_line_width (cr, mouseover_line_width());
416 double const x = component_to_parent_x (
417 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
420 double const y = component_to_parent_y (
421 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
424 if (_body->mouseover().row.bundle) {
426 cairo_move_to (cr, x, y);
427 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
428 cairo_line_to (cr, component_to_parent_x (0), y);
429 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
430 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
435 if (_body->mouseover().column.bundle) {
437 cairo_move_to (cr, x, y);
438 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
439 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
440 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
441 cairo_line_to (cr, x, component_to_parent_y (0));
448 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
450 queue_draw_for (old);
451 queue_draw_for (_body->mouseover());
455 PortMatrixGrid::mouseover_event (double x, double y)
457 _body->set_mouseover (position_to_node (x, y));
461 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
465 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
466 _body->queue_draw_area (
467 _parent_rectangle.get_x(),
468 component_to_parent_y (y),
469 _parent_rectangle.get_width(),
474 if (n.column.bundle) {
476 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
478 _body->queue_draw_area (
479 component_to_parent_x (x),
480 _parent_rectangle.get_y(),
482 _parent_rectangle.get_height()
488 PortMatrixGrid::component_to_parent_x (double x) const
490 return x - _body->xoffset() + _parent_rectangle.get_x();
494 PortMatrixGrid::parent_to_component_x (double x) const
496 return x + _body->xoffset() - _parent_rectangle.get_x();
500 PortMatrixGrid::component_to_parent_y (double y) const
502 return y - _body->yoffset() + _parent_rectangle.get_y();
506 PortMatrixGrid::parent_to_component_y (double y) const
508 return y + _body->yoffset() - _parent_rectangle.get_y();
511 PortMatrixNode::State
512 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
514 bool have_unknown = false;
515 bool have_off_diagonal_association = false;
516 bool have_diagonal_association = false;
517 bool have_diagonal_not_association = false;
519 for (uint32_t i = 0; i < a->nchannels (); ++i) {
521 for (uint32_t j = 0; j < b->nchannels (); ++j) {
523 ARDOUR::BundleChannel c[2];
524 c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
525 c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
527 PortMatrixNode::State const s = _matrix->get_state (c);
530 case PortMatrixNode::ASSOCIATED:
532 have_diagonal_association = true;
534 have_off_diagonal_association = true;
538 case PortMatrixNode::UNKNOWN:
542 case PortMatrixNode::NOT_ASSOCIATED:
544 have_diagonal_not_association = true;
555 return PortMatrixNode::UNKNOWN;
556 } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
557 return PortMatrixNode::ASSOCIATED;
558 } else if (!have_diagonal_association && !have_off_diagonal_association) {
559 return PortMatrixNode::NOT_ASSOCIATED;
562 return PortMatrixNode::PARTIAL;