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 "port_matrix_grid.h"
24 #include "port_matrix.h"
25 #include "port_matrix_body.h"
29 using Gtkmm2ext::Keyboard;
31 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
32 : PortMatrixComponent (m, b),
41 PortMatrixGrid::compute_dimensions ()
43 if (_matrix->visible_columns()) {
44 _width = group_size (_matrix->visible_columns()) * grid_spacing ();
49 if (_matrix->visible_rows()) {
50 _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 < _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels()); ++j) {
85 cairo_move_to (cr, x, 0);
86 cairo_line_to (cr, x, _height);
99 if (_matrix->show_only_bundles ()) {
100 cairo_move_to (cr, x, 0);
101 cairo_line_to (cr, x, _height);
107 /* HORIZONTAL GRID LINES */
110 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
112 cairo_set_line_width (cr, thick_grid_line_width());
113 cairo_move_to (cr, 0, y);
114 cairo_line_to (cr, _width, y);
117 if (!_matrix->show_only_bundles()) {
118 cairo_set_line_width (cr, thin_grid_line_width());
119 for (uint32_t j = 0; j < _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels()); ++j) {
120 y += grid_spacing ();
121 cairo_move_to (cr, 0, y);
122 cairo_line_to (cr, _width, y);
128 y += grid_spacing ();
135 if (_matrix->show_only_bundles ()) {
136 cairo_move_to (cr, 0, y);
137 cairo_line_to (cr, _width, y);
141 /* ASSOCIATION INDICATORS and NON-CONNECTABLE INDICATORS */
143 /* we draw a grey square in a matrix box if the two ports that intersect at that box
144 cannot be connected because they are of different types (MIDI vs. audio)
150 if (_matrix->show_only_bundles()) {
152 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
155 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
157 PortMatrixNode::State s = _matrix->get_association (PortMatrixNode (
158 ARDOUR::BundleChannel ((*j)->bundle, 0),
159 ARDOUR::BundleChannel ((*i)->bundle, 0)
162 case PortMatrixNode::ASSOCIATED:
163 draw_association_indicator (cr, bx, by);
165 case PortMatrixNode::PARTIAL:
166 draw_association_indicator (cr, bx, by, 0.5);
172 by += grid_spacing();
175 bx += grid_spacing();
181 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
184 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
187 for (uint32_t k = 0; k < _matrix->count_of_our_type ((*i)->bundle->nchannels()); ++k) {
190 for (uint32_t l = 0; l < _matrix->count_of_our_type ((*j)->bundle->nchannels()); ++l) {
192 ARDOUR::BundleChannel c[2];
194 c[_matrix->column_index()] = ARDOUR::BundleChannel (
196 (*i)->bundle->type_channel_to_overall (_matrix->type (), k)
199 c[_matrix->row_index()] = ARDOUR::BundleChannel (
201 (*j)->bundle->type_channel_to_overall (_matrix->type (), l)
204 if (c[0].bundle->channel_type (c[0].channel) != c[1].bundle->channel_type (c[1].channel)) {
205 /* these two channels are of different types */
206 draw_non_connectable_indicator (cr, x, y);
208 /* these two channels might be associated */
209 PortMatrixNode::State const s = _matrix->get_state (c);
212 case PortMatrixNode::ASSOCIATED:
213 draw_association_indicator (cr, x, y);
216 case PortMatrixNode::NOT_ASSOCIATED:
227 if (_matrix->count_of_our_type ((*j)->bundle->nchannels()) == 0) {
228 /* the *j bundle has no channels of our type, so it will have a dummy
229 one which needs to be marked non-connectable.
231 draw_non_connectable_indicator (cr, x, y);
237 if (_matrix->count_of_our_type ((*i)->bundle->nchannels()) == 0) {
238 /* draw non-connectable indicators for the case where the *i bundle
239 has no channels of our type (and hence has 1 dummy channel)
242 for (uint32_t l = 0; l < _matrix->count_of_our_type_min_1 ((*j)->bundle->nchannels()); ++l) {
243 draw_non_connectable_indicator (cr, x, y);
244 y += grid_spacing ();
248 by += _matrix->count_of_our_type_min_1 ((*j)->bundle->nchannels()) * grid_spacing();
251 bx += _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels()) * grid_spacing();
257 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
259 set_source_rgba (cr, association_colour(), 0.5);
263 x + grid_spacing() / 2,
264 y + grid_spacing() / 2,
265 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
274 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
276 set_source_rgb (cr, background_colour());
279 x + thick_grid_line_width(),
280 y + thick_grid_line_width(),
281 grid_spacing() - 2 * thick_grid_line_width(),
282 grid_spacing() - 2 * thick_grid_line_width()
287 /** Draw a square to indicate that two channels in a matrix cannot be associated
291 PortMatrixGrid::draw_non_connectable_indicator (cairo_t* cr, uint32_t x, uint32_t y)
293 set_source_rgb (cr, non_connectable_colour ());
296 x + thick_grid_line_width(),
297 y + thick_grid_line_width(),
298 grid_spacing() - 2 * thick_grid_line_width(),
299 grid_spacing() - 2 * thick_grid_line_width()
305 PortMatrixGrid::position_to_node (double x, double y) const
307 return PortMatrixNode (
308 position_to_channel (y, x, _matrix->visible_rows()),
309 position_to_channel (x, y, _matrix->visible_columns())
314 PortMatrixGrid::button_press (double x, double y, GdkEventButton* ev)
316 ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
317 ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
319 if (ev->button == 1) {
322 _drag_valid = (px.bundle && py.bundle);
325 _drag_start_x = x / grid_spacing ();
326 _drag_start_y = y / grid_spacing ();
328 } else if (ev->button == 3) {
330 _matrix->popup_menu (px, py, ev->time);
336 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
338 if (_matrix->show_only_bundles()) {
340 for (uint32_t i = 0; i < node.column.bundle->nchannels().n_total(); ++i) {
341 for (uint32_t j = 0; j < node.row.bundle->nchannels().n_total(); ++j) {
343 if (!_matrix->should_show (node.column.bundle->channel_type(i)) || !_matrix->should_show (node.row.bundle->channel_type(j))) {
347 ARDOUR::BundleChannel c[2];
348 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
349 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
350 _matrix->set_state (c, s && (i == j));
356 if (node.row.bundle && node.column.bundle) {
358 ARDOUR::BundleChannel c[2];
359 c[_matrix->row_index()] = node.row;
360 c[_matrix->column_index()] = node.column;
362 _matrix->set_state (c, s);
368 PortMatrixGrid::button_release (double x, double y, GdkEventButton* ev)
370 if (ev->button == 1) {
374 if (_dragging && _moved) {
377 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
380 PortMatrixNode::State const s = _matrix->get_association (p.front());
381 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
382 set_association (*i, toggle_state (s));
389 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
390 /* associate/disassociate things diagonally down and right until we run out */
391 PortMatrixNode::State s = (PortMatrixNode::State) 0;
393 PortMatrixNode const n = position_to_node (x, y);
394 if (n.row.bundle && n.column.bundle) {
395 if (s == (PortMatrixNode::State) 0) {
396 s = _matrix->get_association (n);
398 set_association (n, toggle_state (s));
402 x += grid_spacing ();
403 y += grid_spacing ();
408 PortMatrixNode const n = position_to_node (x, y);
409 if (n.row.bundle && n.column.bundle) {
410 PortMatrixNode::State const s = _matrix->get_association (n);
411 set_association (n, toggle_state (s));
419 _body->queue_draw ();
427 PortMatrixGrid::draw_extra (cairo_t* cr)
429 set_source_rgba (cr, mouseover_line_colour(), 0.3);
430 cairo_set_line_width (cr, mouseover_line_width());
432 list<PortMatrixNode> const m = _body->mouseover ();
434 for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
436 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
437 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
439 if (PortMatrix::bundle_with_channels (i->row.bundle) && PortMatrix::bundle_with_channels (i->column.bundle)) {
441 cairo_move_to (cr, x, y);
442 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
443 cairo_line_to (cr, component_to_parent_x (0), y);
444 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
445 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
449 cairo_move_to (cr, x, y);
450 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
451 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
452 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
453 cairo_line_to (cr, x, component_to_parent_y (0));
459 if (_dragging && _drag_valid && _moved) {
461 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
465 bool const s = toggle_state (_matrix->get_association (p.front()));
467 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
469 draw_association_indicator (
471 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
472 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
477 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
478 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
484 set_source_rgba (cr, association_colour (), 0.3);
488 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
489 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
494 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
495 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
504 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
506 queue_draw_for (old);
507 queue_draw_for (_body->mouseover());
511 PortMatrixGrid::motion (double x, double y)
513 _body->set_mouseover (position_to_node (x, y));
515 int const px = x / grid_spacing ();
516 int const py = y / grid_spacing ();
518 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
522 if (_dragging && _drag_valid && _moved) {
525 _body->queue_draw ();
530 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
532 for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
536 double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
537 _body->queue_draw_area (
538 _parent_rectangle.get_x(),
539 component_to_parent_y (y),
540 _parent_rectangle.get_width(),
545 if (i->column.bundle) {
547 double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
549 _body->queue_draw_area (
550 component_to_parent_x (x),
551 _parent_rectangle.get_y(),
553 _parent_rectangle.get_height()
560 PortMatrixGrid::component_to_parent_x (double x) const
562 return x - _body->xoffset() + _parent_rectangle.get_x();
566 PortMatrixGrid::parent_to_component_x (double x) const
568 return x + _body->xoffset() - _parent_rectangle.get_x();
572 PortMatrixGrid::component_to_parent_y (double y) const
574 return y - _body->yoffset() + _parent_rectangle.get_y();
578 PortMatrixGrid::parent_to_component_y (double y) const
580 return y + _body->yoffset() - _parent_rectangle.get_y();
584 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
586 list<PortMatrixNode> p;
588 bool const steep = abs (y1 - y0) > abs (x1 - x0);
610 int dy = abs (y1 - y0);
613 double derr = double (dy) / dx;
616 int const ystep = y0 < y1 ? 1 : -1;
618 for (int x = x0; x <= x1; ++x) {
620 PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
621 if (n.row.bundle && n.column.bundle) {
625 PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
626 if (n.row.bundle && n.column.bundle) {
643 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
645 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);