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"
31 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
32 : PortMatrixComponent (m, b),
41 PortMatrixGrid::compute_dimensions ()
43 if (_matrix->visible_columns() == 0) {
46 _width = group_size (_matrix->visible_columns()) * grid_spacing ();
49 if (_matrix->visible_rows() == 0) {
52 _height = group_size (_matrix->visible_rows()) * grid_spacing ();
58 PortMatrixGrid::render (cairo_t* cr)
60 set_source_rgb (cr, background_colour());
61 cairo_rectangle (cr, 0, 0, _width, _height);
64 PortGroup::BundleList const & row_bundles = _matrix->visible_rows()->bundles();
65 PortGroup::BundleList const & column_bundles = _matrix->visible_columns()->bundles();
69 /* VERTICAL GRID LINES */
71 set_source_rgb (cr, grid_colour());
74 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
76 cairo_set_line_width (cr, thick_grid_line_width());
77 cairo_move_to (cr, x, 0);
78 cairo_line_to (cr, x, _height);
81 if (!_matrix->show_only_bundles()) {
82 cairo_set_line_width (cr, thin_grid_line_width());
83 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
85 cairo_move_to (cr, x, 0);
86 cairo_line_to (cr, x, _height);
101 /* HORIZONTAL GRID LINES */
104 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
106 cairo_set_line_width (cr, thick_grid_line_width());
107 cairo_move_to (cr, 0, y);
108 cairo_line_to (cr, _width, y);
111 if (!_matrix->show_only_bundles()) {
112 cairo_set_line_width (cr, thin_grid_line_width());
113 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
114 y += grid_spacing ();
115 cairo_move_to (cr, 0, y);
116 cairo_line_to (cr, _width, y);
122 y += grid_spacing ();
129 /* ASSOCIATION INDICATORS */
134 if (_matrix->show_only_bundles()) {
136 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
139 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
141 PortMatrixNode::State s = get_association (PortMatrixNode (
142 ARDOUR::BundleChannel (i->bundle, 0),
143 ARDOUR::BundleChannel (j->bundle, 0)
146 case PortMatrixNode::ASSOCIATED:
147 draw_association_indicator (cr, bx, by);
149 case PortMatrixNode::PARTIAL:
150 draw_association_indicator (cr, bx, by, 0.5);
156 by += grid_spacing();
159 bx += grid_spacing();
165 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
168 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
171 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
174 for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
176 ARDOUR::BundleChannel c[2];
177 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
178 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
180 PortMatrixNode::State const s = _matrix->get_state (c);
183 case PortMatrixNode::ASSOCIATED:
184 draw_association_indicator (cr, x, y);
187 case PortMatrixNode::NOT_ASSOCIATED:
200 by += j->bundle->nchannels () * grid_spacing();
203 bx += i->bundle->nchannels () * grid_spacing();
209 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
211 set_source_rgba (cr, association_colour(), 0.5);
215 x + grid_spacing() / 2,
216 y + grid_spacing() / 2,
217 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
226 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
228 set_source_rgb (cr, background_colour());
231 x + thick_grid_line_width(),
232 y + thick_grid_line_width(),
233 grid_spacing() - 2 * thick_grid_line_width(),
234 grid_spacing() - 2 * thick_grid_line_width()
240 PortMatrixGrid::position_to_node (double x, double y) const
242 return PortMatrixNode (
243 position_to_channel (y, x, _matrix->visible_rows()),
244 position_to_channel (x, y, _matrix->visible_columns())
249 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t, guint)
251 ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
252 ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
257 _drag_valid = (px.bundle && py.bundle);
260 _drag_start_x = x / grid_spacing ();
261 _drag_start_y = y / grid_spacing ();
265 _matrix->popup_menu (px, py, t);
270 PortMatrixNode::State
271 PortMatrixGrid::get_association (PortMatrixNode node) const
273 if (_matrix->show_only_bundles()) {
275 bool have_off_diagonal_association = false;
276 bool have_diagonal_association = false;
277 bool have_diagonal_not_association = false;
279 for (uint32_t i = 0; i < node.row.bundle->nchannels (); ++i) {
281 for (uint32_t j = 0; j < node.column.bundle->nchannels (); ++j) {
283 ARDOUR::BundleChannel c[2];
284 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
285 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
287 PortMatrixNode::State const s = _matrix->get_state (c);
290 case PortMatrixNode::ASSOCIATED:
292 have_diagonal_association = true;
294 have_off_diagonal_association = true;
298 case PortMatrixNode::NOT_ASSOCIATED:
300 have_diagonal_not_association = true;
310 if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
311 return PortMatrixNode::ASSOCIATED;
312 } else if (!have_diagonal_association && !have_off_diagonal_association) {
313 return PortMatrixNode::NOT_ASSOCIATED;
316 return PortMatrixNode::PARTIAL;
320 ARDOUR::BundleChannel c[2];
321 c[_matrix->column_index()] = node.column;
322 c[_matrix->row_index()] = node.row;
323 return _matrix->get_state (c);
328 return PortMatrixNode::NOT_ASSOCIATED;
332 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
334 if (_matrix->show_only_bundles()) {
336 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
337 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
339 ARDOUR::BundleChannel c[2];
340 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
341 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
342 _matrix->set_state (c, s && (i == j));
348 if (node.row.bundle && node.column.bundle) {
350 ARDOUR::BundleChannel c[2];
351 c[_matrix->row_index()] = node.row;
352 c[_matrix->column_index()] = node.column;
353 _matrix->set_state (c, s);
359 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/, guint s)
365 if (_dragging && _moved) {
368 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
371 PortMatrixNode::State const s = get_association (p.front());
372 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
373 set_association (*i, toggle_state (s));
380 if (Keyboard::modifier_state_equals (s, Keyboard::PrimaryModifier)) {
381 /* associate/disassociate things diagonally down and right until we run out */
382 PortMatrixNode::State s = (PortMatrixNode::State) 0;
384 PortMatrixNode const n = position_to_node (x, y);
385 if (n.row.bundle && n.column.bundle) {
386 if (s == (PortMatrixNode::State) 0) {
387 s = get_association (n);
389 set_association (n, toggle_state (s));
393 x += grid_spacing ();
394 y += grid_spacing ();
399 PortMatrixNode const n = position_to_node (x, y);
400 if (n.row.bundle && n.column.bundle) {
401 PortMatrixNode::State const s = get_association (n);
402 set_association (n, toggle_state (s));
410 _body->queue_draw ();
418 PortMatrixGrid::draw_extra (cairo_t* cr)
420 set_source_rgba (cr, mouseover_line_colour(), 0.3);
421 cairo_set_line_width (cr, mouseover_line_width());
423 list<PortMatrixNode> const m = _body->mouseover ();
425 for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
427 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
428 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
430 if (i->row.bundle && i->column.bundle) {
432 cairo_move_to (cr, x, y);
433 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
434 cairo_line_to (cr, component_to_parent_x (0), y);
435 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
436 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
440 cairo_move_to (cr, x, y);
441 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
442 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
443 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
444 cairo_line_to (cr, x, component_to_parent_y (0));
450 if (_dragging && _drag_valid && _moved) {
452 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
456 bool const s = toggle_state (get_association (p.front()));
458 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
460 draw_association_indicator (
462 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
463 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
468 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
469 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
475 set_source_rgba (cr, association_colour (), 0.3);
479 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
480 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
485 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
486 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
495 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
497 queue_draw_for (old);
498 queue_draw_for (_body->mouseover());
502 PortMatrixGrid::motion (double x, double y)
504 _body->set_mouseover (position_to_node (x, y));
506 int const px = x / grid_spacing ();
507 int const py = y / grid_spacing ();
509 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
513 if (_dragging && _drag_valid && _moved) {
516 _body->queue_draw ();
521 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
523 for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
527 double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
528 _body->queue_draw_area (
529 _parent_rectangle.get_x(),
530 component_to_parent_y (y),
531 _parent_rectangle.get_width(),
536 if (i->column.bundle) {
538 double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
540 _body->queue_draw_area (
541 component_to_parent_x (x),
542 _parent_rectangle.get_y(),
544 _parent_rectangle.get_height()
551 PortMatrixGrid::component_to_parent_x (double x) const
553 return x - _body->xoffset() + _parent_rectangle.get_x();
557 PortMatrixGrid::parent_to_component_x (double x) const
559 return x + _body->xoffset() - _parent_rectangle.get_x();
563 PortMatrixGrid::component_to_parent_y (double y) const
565 return y - _body->yoffset() + _parent_rectangle.get_y();
569 PortMatrixGrid::parent_to_component_y (double y) const
571 return y + _body->yoffset() - _parent_rectangle.get_y();
575 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
577 list<PortMatrixNode> p;
579 bool const steep = abs (y1 - y0) > abs (x1 - x0);
601 int dy = abs (y1 - y0);
604 double derr = double (dy) / dx;
607 int const ystep = y0 < y1 ? 1 : -1;
609 for (int x = x0; x <= x1; ++x) {
611 PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
612 if (n.row.bundle && n.column.bundle) {
616 PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
617 if (n.row.bundle && n.column.bundle) {
634 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
636 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);