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 ()
42 if (_matrix->visible_columns() == 0) {
45 _width = group_size (_matrix->visible_columns()) * grid_spacing ();
48 if (_matrix->visible_rows() == 0) {
51 _height = group_size (_matrix->visible_rows()) * grid_spacing ();
57 PortMatrixGrid::render (cairo_t* cr)
59 set_source_rgb (cr, background_colour());
60 cairo_rectangle (cr, 0, 0, _width, _height);
63 PortGroup::BundleList const & row_bundles = _matrix->visible_rows()->bundles();
64 PortGroup::BundleList const & column_bundles = _matrix->visible_columns()->bundles();
68 /* VERTICAL GRID LINES */
70 set_source_rgb (cr, grid_colour());
73 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
75 cairo_set_line_width (cr, thick_grid_line_width());
76 cairo_move_to (cr, x, 0);
77 cairo_line_to (cr, x, _height);
80 if (!_matrix->show_only_bundles()) {
81 cairo_set_line_width (cr, thin_grid_line_width());
82 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
84 cairo_move_to (cr, x, 0);
85 cairo_line_to (cr, x, _height);
100 /* HORIZONTAL GRID LINES */
103 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
105 cairo_set_line_width (cr, thick_grid_line_width());
106 cairo_move_to (cr, 0, y);
107 cairo_line_to (cr, _width, y);
110 if (!_matrix->show_only_bundles()) {
111 cairo_set_line_width (cr, thin_grid_line_width());
112 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
113 y += grid_spacing ();
114 cairo_move_to (cr, 0, y);
115 cairo_line_to (cr, _width, y);
121 y += grid_spacing ();
128 /* ASSOCIATION INDICATORS */
133 if (_matrix->show_only_bundles()) {
135 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
138 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
140 PortMatrixNode::State s = get_association (PortMatrixNode (
141 ARDOUR::BundleChannel (i->bundle, 0),
142 ARDOUR::BundleChannel (j->bundle, 0)
145 case PortMatrixNode::ASSOCIATED:
146 draw_association_indicator (cr, bx, by);
148 case PortMatrixNode::PARTIAL:
149 draw_association_indicator (cr, bx, by, 0.5);
155 by += grid_spacing();
158 bx += grid_spacing();
164 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
167 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
170 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
173 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
175 ARDOUR::BundleChannel c[2];
176 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
177 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
179 PortMatrixNode::State const s = _matrix->get_state (c);
182 case PortMatrixNode::ASSOCIATED:
183 draw_association_indicator (cr, x, y);
186 case PortMatrixNode::NOT_ASSOCIATED:
199 by += j->bundle->nchannels () * grid_spacing();
202 bx += i->bundle->nchannels () * grid_spacing();
208 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
210 set_source_rgba (cr, association_colour(), 0.5);
214 x + grid_spacing() / 2,
215 y + grid_spacing() / 2,
216 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
225 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
227 set_source_rgb (cr, background_colour());
230 x + thick_grid_line_width(),
231 y + thick_grid_line_width(),
232 grid_spacing() - 2 * thick_grid_line_width(),
233 grid_spacing() - 2 * thick_grid_line_width()
239 PortMatrixGrid::position_to_node (double x, double y) const
241 return PortMatrixNode (
242 position_to_channel (y, x, _matrix->visible_rows()),
243 position_to_channel (x, y, _matrix->visible_columns())
248 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
250 ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
251 ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
256 _drag_valid = (px.bundle && py.bundle);
259 _drag_start_x = x / grid_spacing ();
260 _drag_start_y = y / grid_spacing ();
264 _matrix->popup_menu (px, py, t);
269 PortMatrixNode::State
270 PortMatrixGrid::get_association (PortMatrixNode node) const
272 if (_matrix->show_only_bundles()) {
274 bool have_off_diagonal_association = false;
275 bool have_diagonal_association = false;
276 bool have_diagonal_not_association = false;
278 for (uint32_t i = 0; i < node.row.bundle->nchannels (); ++i) {
280 for (uint32_t j = 0; j < node.column.bundle->nchannels (); ++j) {
282 ARDOUR::BundleChannel c[2];
283 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
284 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
286 PortMatrixNode::State const s = _matrix->get_state (c);
289 case PortMatrixNode::ASSOCIATED:
291 have_diagonal_association = true;
293 have_off_diagonal_association = true;
297 case PortMatrixNode::NOT_ASSOCIATED:
299 have_diagonal_not_association = true;
309 if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
310 return PortMatrixNode::ASSOCIATED;
311 } else if (!have_diagonal_association && !have_off_diagonal_association) {
312 return PortMatrixNode::NOT_ASSOCIATED;
315 return PortMatrixNode::PARTIAL;
319 ARDOUR::BundleChannel c[2];
320 c[_matrix->column_index()] = node.column;
321 c[_matrix->row_index()] = node.row;
322 return _matrix->get_state (c);
327 return PortMatrixNode::NOT_ASSOCIATED;
331 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
333 if (_matrix->show_only_bundles()) {
335 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
336 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
338 ARDOUR::BundleChannel c[2];
339 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
340 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
341 _matrix->set_state (c, s && (i == j));
347 if (node.row.bundle && node.column.bundle) {
349 ARDOUR::BundleChannel c[2];
350 c[_matrix->row_index()] = node.row;
351 c[_matrix->column_index()] = node.column;
352 _matrix->set_state (c, s);
358 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/)
364 if (_dragging && _moved) {
367 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
370 PortMatrixNode::State const s = get_association (p.front());
371 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
372 set_association (*i, toggle_state (s));
379 PortMatrixNode const n = position_to_node (x, y);
380 if (n.row.bundle && n.column.bundle) {
381 PortMatrixNode::State const s = get_association (n);
382 set_association (n, toggle_state (s));
389 _body->queue_draw ();
397 PortMatrixGrid::draw_extra (cairo_t* cr)
399 set_source_rgba (cr, mouseover_line_colour(), 0.3);
400 cairo_set_line_width (cr, mouseover_line_width());
402 list<PortMatrixNode> const m = _body->mouseover ();
404 for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
406 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
407 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
409 if (i->row.bundle && i->column.bundle) {
411 cairo_move_to (cr, x, y);
412 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
413 cairo_line_to (cr, component_to_parent_x (0), y);
414 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
415 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
419 cairo_move_to (cr, x, y);
420 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
421 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
422 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
423 cairo_line_to (cr, x, component_to_parent_y (0));
429 if (_dragging && _drag_valid && _moved) {
431 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
435 bool const s = toggle_state (get_association (p.front()));
437 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
439 draw_association_indicator (
441 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
442 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
447 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
448 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
454 set_source_rgba (cr, association_colour (), 0.3);
458 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
459 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
464 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
465 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
474 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
476 queue_draw_for (old);
477 queue_draw_for (_body->mouseover());
481 PortMatrixGrid::motion (double x, double y)
483 _body->set_mouseover (position_to_node (x, y));
485 int const px = x / grid_spacing ();
486 int const py = y / grid_spacing ();
488 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
492 if (_dragging && _drag_valid && _moved) {
495 _body->queue_draw ();
500 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
502 for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
506 double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
507 _body->queue_draw_area (
508 _parent_rectangle.get_x(),
509 component_to_parent_y (y),
510 _parent_rectangle.get_width(),
515 if (i->column.bundle) {
517 double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
519 _body->queue_draw_area (
520 component_to_parent_x (x),
521 _parent_rectangle.get_y(),
523 _parent_rectangle.get_height()
530 PortMatrixGrid::component_to_parent_x (double x) const
532 return x - _body->xoffset() + _parent_rectangle.get_x();
536 PortMatrixGrid::parent_to_component_x (double x) const
538 return x + _body->xoffset() - _parent_rectangle.get_x();
542 PortMatrixGrid::component_to_parent_y (double y) const
544 return y - _body->yoffset() + _parent_rectangle.get_y();
548 PortMatrixGrid::parent_to_component_y (double y) const
550 return y + _body->yoffset() - _parent_rectangle.get_y();
554 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
556 list<PortMatrixNode> p;
558 bool const steep = abs (y1 - y0) > abs (x1 - x0);
580 int dy = abs (y1 - y0);
583 double derr = double (dy) / dx;
586 int const ystep = y0 < y1 ? 1 : -1;
588 for (int x = x0; x <= x1; ++x) {
590 PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
591 if (n.row.bundle && n.column.bundle) {
595 PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
596 if (n.row.bundle && n.column.bundle) {
613 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
615 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);