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* p, PortMatrixBody* b)
28 : PortMatrixComponent (b),
35 PortMatrixGrid::compute_dimensions ()
38 ARDOUR::BundleList const c = _body->column_ports().bundles();
39 for (ARDOUR::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
40 _width += (*i)->nchannels() * column_width();
44 ARDOUR::BundleList const r = _body->row_ports().bundles();
45 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
46 _height += (*i)->nchannels() * row_height();
52 PortMatrixGrid::render (cairo_t* cr)
56 set_source_rgb (cr, background_colour());
57 cairo_rectangle (cr, 0, 0, _width, _height);
60 /* VERTICAL GRID LINES */
62 set_source_rgb (cr, grid_colour());
64 ARDOUR::BundleList const c = _body->column_ports().bundles();
65 for (ARDOUR::BundleList::size_type i = 0; i < c.size(); ++i) {
67 cairo_set_line_width (cr, thin_grid_line_width());
68 for (uint32_t j = 1; j < c[i]->nchannels(); ++j) {
70 cairo_move_to (cr, x, 0);
71 cairo_line_to (cr, x, _height);
75 if (i < (c.size() - 1)) {
77 cairo_set_line_width (cr, thick_grid_line_width());
78 cairo_move_to (cr, x, 0);
79 cairo_line_to (cr, x, _height);
84 uint32_t grid_width = x + column_width();
86 /* HORIZONTAL GRID LINES */
89 ARDOUR::BundleList const r = _body->row_ports().bundles();
90 for (ARDOUR::BundleList::size_type i = 0; i < r.size(); ++i) {
92 cairo_set_line_width (cr, thin_grid_line_width());
93 for (uint32_t j = 1; j < r[i]->nchannels(); ++j) {
95 cairo_move_to (cr, 0, y);
96 cairo_line_to (cr, grid_width, y);
100 if (i < (r.size() - 1)) {
102 cairo_set_line_width (cr, thick_grid_line_width());
103 cairo_move_to (cr, 0, y);
104 cairo_line_to (cr, grid_width, y);
109 /* ASSOCIATION INDICATORS */
114 for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
117 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
120 for (uint32_t k = 0; k < (*i)->nchannels (); k++) {
123 for (uint32_t l = 0; l < (*j)->nchannels (); ++l) {
125 PortMatrix::State const s = _port_matrix->get_state (*j, l, *i, k);
128 case PortMatrix::ASSOCIATED:
129 set_source_rgba (cr, association_colour(), 0.5);
132 x + column_width() / 2,
133 y + column_width() / 2,
134 (column_width() - (2 * connection_indicator_pad())) / 2,
142 case PortMatrix::UNKNOWN:
143 set_source_rgba (cr, unknown_colour(), 0.5);
146 x + thick_grid_line_width(),
147 y + thick_grid_line_width(),
148 column_width() - 2 * thick_grid_line_width(),
149 row_height() - 2 * thick_grid_line_width()
154 case PortMatrix::NOT_ASSOCIATED:
163 by += (*j)->nchannels () * row_height();
166 bx += (*i)->nchannels () * column_width();
172 PortMatrixGrid::position_to_node (double x, double y) const
174 return PortMatrixNode (
175 position_to_channel (y, _body->row_ports().bundles(), row_height()),
176 position_to_channel (x, _body->column_ports().bundles(), column_width())
181 PortMatrixBundleChannel
182 PortMatrixGrid::position_to_channel (double p, ARDOUR::BundleList const& bundles, double inc) const
184 uint32_t pos = p / inc;
186 for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
187 if (pos < (*i)->nchannels()) {
188 return PortMatrixBundleChannel (*i, pos);
190 pos -= (*i)->nchannels();
194 return PortMatrixBundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
199 PortMatrixGrid::channel_position (
200 PortMatrixBundleChannel bc,
201 ARDOUR::BundleList const& bundles,
206 ARDOUR::BundleList::const_iterator i = bundles.begin ();
207 while (i != bundles.end() && *i != bc.bundle) {
208 p += inc * (*i)->nchannels();
212 if (i == bundles.end()) {
216 p += inc * bc.channel;
222 PortMatrixGrid::button_press (double x, double y, int b)
224 PortMatrixNode const node = position_to_node (x, y);
226 if (node.row.bundle && node.column.bundle) {
228 PortMatrix::State const s = _port_matrix->get_state (
229 node.row.bundle, node.row.channel, node.column.bundle, node.column.channel
233 if (s == PortMatrix::ASSOCIATED || s == PortMatrix::NOT_ASSOCIATED) {
235 bool const n = !(s == PortMatrix::ASSOCIATED);
237 _port_matrix->set_state (
238 node.row.bundle, node.row.channel,
239 node.column.bundle, node.column.channel,
245 _body->queue_draw ();
250 PortMatrixGrid::draw_extra (cairo_t* cr)
252 set_source_rgba (cr, mouseover_line_colour(), 0.3);
253 cairo_set_line_width (cr, mouseover_line_width());
255 if (_body->mouseover().row.bundle) {
257 double const y = component_to_parent_y (
258 channel_position (_body->mouseover().row, _body->row_ports().bundles(), row_height()) + row_height() / 2
261 cairo_move_to (cr, _parent_rectangle.get_x(), y);
262 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
266 if (_body->mouseover().column.bundle) {
268 double const x = component_to_parent_x (
269 channel_position (_body->mouseover().column, _body->column_ports().bundles(), column_width()) + column_width() / 2
272 cairo_move_to (cr, x, _parent_rectangle.get_y());
273 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
279 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
281 queue_draw_for (old);
282 queue_draw_for (_body->mouseover());
286 PortMatrixGrid::mouseover_event (double x, double y)
288 _body->set_mouseover (position_to_node (x, y));
292 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
296 double const y = channel_position (n.row, _body->row_ports().bundles(), row_height());
297 _body->queue_draw_area (
298 _parent_rectangle.get_x(),
299 component_to_parent_y (y),
300 _parent_rectangle.get_width(),
305 if (n.column.bundle) {
307 double const x = channel_position (n.column, _body->column_ports().bundles(), column_width());
309 _body->queue_draw_area (
310 component_to_parent_x (x),
311 _parent_rectangle.get_y(),
313 _parent_rectangle.get_height()
319 PortMatrixGrid::component_to_parent_x (double x) const
321 return x - _body->xoffset() + _parent_rectangle.get_x();
325 PortMatrixGrid::parent_to_component_x (double x) const
327 return x + _body->xoffset() - _parent_rectangle.get_x();
331 PortMatrixGrid::component_to_parent_y (double y) const
333 return y - _body->yoffset() + _parent_rectangle.get_y();
337 PortMatrixGrid::parent_to_component_y (double y) const
339 return y + _body->yoffset() - _parent_rectangle.get_y();