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"
27 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
28 : PortMatrixComponent (m, b)
34 PortMatrixGrid::compute_dimensions ()
37 ARDOUR::BundleList const c = _matrix->columns()->bundles();
38 for (ARDOUR::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
39 _width += (*i)->nchannels() * column_width();
43 ARDOUR::BundleList const r = _matrix->rows()->bundles();
44 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
45 _height += (*i)->nchannels() * row_height();
51 PortMatrixGrid::render (cairo_t* cr)
55 set_source_rgb (cr, background_colour());
56 cairo_rectangle (cr, 0, 0, _width, _height);
59 /* VERTICAL GRID LINES */
61 set_source_rgb (cr, grid_colour());
63 ARDOUR::BundleList const c = _matrix->columns()->bundles();
64 for (ARDOUR::BundleList::size_type i = 0; i < c.size(); ++i) {
66 cairo_set_line_width (cr, thin_grid_line_width());
67 for (uint32_t j = 1; j < c[i]->nchannels(); ++j) {
69 cairo_move_to (cr, x, 0);
70 cairo_line_to (cr, x, _height);
74 if (i < (c.size() - 1)) {
76 cairo_set_line_width (cr, thick_grid_line_width());
77 cairo_move_to (cr, x, 0);
78 cairo_line_to (cr, x, _height);
83 uint32_t grid_width = x + column_width();
85 /* HORIZONTAL GRID LINES */
88 ARDOUR::BundleList const r = _matrix->rows()->bundles();
89 for (ARDOUR::BundleList::size_type i = 0; i < r.size(); ++i) {
91 cairo_set_line_width (cr, thin_grid_line_width());
92 for (uint32_t j = 1; j < r[i]->nchannels(); ++j) {
94 cairo_move_to (cr, 0, y);
95 cairo_line_to (cr, grid_width, y);
99 if (i < (r.size() - 1)) {
101 cairo_set_line_width (cr, thick_grid_line_width());
102 cairo_move_to (cr, 0, y);
103 cairo_line_to (cr, grid_width, y);
108 /* ASSOCIATION INDICATORS */
113 for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
116 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
119 for (uint32_t k = 0; k < (*i)->nchannels (); k++) {
122 for (uint32_t l = 0; l < (*j)->nchannels (); ++l) {
124 ARDOUR::BundleChannel c[2];
125 c[_matrix->column_index()] = ARDOUR::BundleChannel (*i, k);
126 c[_matrix->row_index()] = ARDOUR::BundleChannel (*j, l);
128 PortMatrix::State const s = _matrix->get_state (c);
131 case PortMatrix::ASSOCIATED:
132 set_source_rgba (cr, association_colour(), 0.5);
135 x + column_width() / 2,
136 y + column_width() / 2,
137 (column_width() - (2 * connection_indicator_pad())) / 2,
145 case PortMatrix::UNKNOWN:
146 set_source_rgba (cr, unknown_colour(), 0.5);
149 x + thick_grid_line_width(),
150 y + thick_grid_line_width(),
151 column_width() - 2 * thick_grid_line_width(),
152 row_height() - 2 * thick_grid_line_width()
157 case PortMatrix::NOT_ASSOCIATED:
166 by += (*j)->nchannels () * row_height();
169 bx += (*i)->nchannels () * column_width();
175 PortMatrixGrid::position_to_node (double x, double y) const
177 return PortMatrixNode (
178 position_to_channel (y, _matrix->rows()->bundles(), row_height()),
179 position_to_channel (x, _matrix->columns()->bundles(), column_width())
184 ARDOUR::BundleChannel
185 PortMatrixGrid::position_to_channel (double p, ARDOUR::BundleList const& bundles, double inc) const
187 uint32_t pos = p / inc;
189 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
190 if (pos < (*i)->nchannels()) {
191 return ARDOUR::BundleChannel (*i, pos);
193 pos -= (*i)->nchannels();
197 return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
202 PortMatrixGrid::channel_position (
203 ARDOUR::BundleChannel bc,
204 ARDOUR::BundleList const& bundles,
209 ARDOUR::BundleList::const_iterator i = bundles.begin ();
210 while (i != bundles.end() && *i != bc.bundle) {
211 p += inc * (*i)->nchannels();
215 if (i == bundles.end()) {
219 p += inc * bc.channel;
225 PortMatrixGrid::button_press (double x, double y, int b)
227 PortMatrixNode const node = position_to_node (x, y);
229 if (node.row.bundle && node.column.bundle) {
231 ARDOUR::BundleChannel c[2];
232 c[_matrix->row_index()] = node.row;
233 c[_matrix->column_index()] = node.column;
235 PortMatrix::State const s = _matrix->get_state (c);
237 if (s == PortMatrix::ASSOCIATED || s == PortMatrix::NOT_ASSOCIATED) {
239 bool const n = !(s == PortMatrix::ASSOCIATED);
241 ARDOUR::BundleChannel c[2];
242 c[_matrix->row_index()] = node.row;
243 c[_matrix->column_index()] = node.column;
245 _matrix->set_state (c, n);
249 _body->queue_draw ();
254 PortMatrixGrid::draw_extra (cairo_t* cr)
256 set_source_rgba (cr, mouseover_line_colour(), 0.3);
257 cairo_set_line_width (cr, mouseover_line_width());
259 double const x = component_to_parent_x (
260 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
263 double const y = component_to_parent_y (
264 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
267 if (_body->mouseover().row.bundle) {
269 cairo_move_to (cr, x, y);
270 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
271 cairo_line_to (cr, component_to_parent_x (0), y);
272 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
273 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
278 if (_body->mouseover().column.bundle) {
280 cairo_move_to (cr, x, y);
281 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
282 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
283 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
284 cairo_line_to (cr, x, component_to_parent_y (0));
291 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
293 queue_draw_for (old);
294 queue_draw_for (_body->mouseover());
298 PortMatrixGrid::mouseover_event (double x, double y)
300 _body->set_mouseover (position_to_node (x, y));
304 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
308 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
309 _body->queue_draw_area (
310 _parent_rectangle.get_x(),
311 component_to_parent_y (y),
312 _parent_rectangle.get_width(),
317 if (n.column.bundle) {
319 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
321 _body->queue_draw_area (
322 component_to_parent_x (x),
323 _parent_rectangle.get_y(),
325 _parent_rectangle.get_height()
331 PortMatrixGrid::component_to_parent_x (double x) const
333 return x - _body->xoffset() + _parent_rectangle.get_x();
337 PortMatrixGrid::parent_to_component_x (double x) const
339 return x + _body->xoffset() - _parent_rectangle.get_x();
343 PortMatrixGrid::component_to_parent_y (double y) const
345 return y - _body->yoffset() + _parent_rectangle.get_y();
349 PortMatrixGrid::parent_to_component_y (double y) const
351 return y + _body->yoffset() - _parent_rectangle.get_y();