fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / port_matrix_grid.cc
blobfdfe45b2248066a90ce0eba2e496c4f6bb9f2042
1 /*
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.
20 #include <iostream>
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"
27 #include "keyboard.h"
29 using namespace std;
30 using Gtkmm2ext::Keyboard;
32 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
33 : PortMatrixComponent (m, b),
34 _dragging (false),
35 _drag_valid (false),
36 _moved (false)
41 void
42 PortMatrixGrid::compute_dimensions ()
44 if (_matrix->visible_columns()) {
45 _width = group_size (_matrix->visible_columns()) * grid_spacing ();
46 } else {
47 _width = 0;
50 if (_matrix->visible_rows()) {
51 _height = group_size (_matrix->visible_rows()) * grid_spacing ();
52 } else {
53 _height = 0;
58 void
59 PortMatrixGrid::render (cairo_t* cr)
61 set_source_rgb (cr, background_colour());
62 cairo_rectangle (cr, 0, 0, _width, _height);
63 cairo_fill (cr);
65 PortGroup::BundleList const & row_bundles = _matrix->visible_rows()->bundles();
66 PortGroup::BundleList const & column_bundles = _matrix->visible_columns()->bundles();
68 uint32_t x = 0;
70 /* VERTICAL GRID LINES */
72 set_source_rgb (cr, grid_colour());
73 uint32_t N = 0;
75 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
77 cairo_set_line_width (cr, thick_grid_line_width());
78 cairo_move_to (cr, x, 0);
79 cairo_line_to (cr, x, _height);
80 cairo_stroke (cr);
82 if (!_matrix->show_only_bundles()) {
83 cairo_set_line_width (cr, thin_grid_line_width());
84 for (uint32_t j = 0; j < (*i)->bundle->nchannels(); ++j) {
85 x += grid_spacing ();
86 cairo_move_to (cr, x, 0);
87 cairo_line_to (cr, x, _height);
88 cairo_stroke (cr);
91 } else {
93 x += grid_spacing ();
97 ++N;
100 uint32_t y = 0;
102 /* HORIZONTAL GRID LINES */
104 N = 0;
105 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
107 cairo_set_line_width (cr, thick_grid_line_width());
108 cairo_move_to (cr, 0, y);
109 cairo_line_to (cr, _width, y);
110 cairo_stroke (cr);
112 if (!_matrix->show_only_bundles()) {
113 cairo_set_line_width (cr, thin_grid_line_width());
114 for (uint32_t j = 0; j < (*i)->bundle->nchannels(); ++j) {
115 y += grid_spacing ();
116 cairo_move_to (cr, 0, y);
117 cairo_line_to (cr, _width, y);
118 cairo_stroke (cr);
121 } else {
123 y += grid_spacing ();
127 ++N;
130 /* ASSOCIATION INDICATORS */
132 uint32_t bx = 0;
133 uint32_t by = 0;
135 if (_matrix->show_only_bundles()) {
137 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
138 by = 0;
140 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
142 PortMatrixNode::State s = get_association (PortMatrixNode (
143 ARDOUR::BundleChannel ((*i)->bundle, 0),
144 ARDOUR::BundleChannel ((*j)->bundle, 0)
146 switch (s) {
147 case PortMatrixNode::ASSOCIATED:
148 draw_association_indicator (cr, bx, by);
149 break;
150 case PortMatrixNode::PARTIAL:
151 draw_association_indicator (cr, bx, by, 0.5);
152 break;
153 default:
154 break;
157 by += grid_spacing();
160 bx += grid_spacing();
164 } else {
166 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
167 by = 0;
169 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
171 x = bx;
172 for (uint32_t k = 0; k < (*i)->bundle->nchannels (); ++k) {
174 y = by;
175 for (uint32_t l = 0; l < (*j)->bundle->nchannels (); ++l) {
177 ARDOUR::BundleChannel c[2];
178 c[_matrix->column_index()] = ARDOUR::BundleChannel ((*i)->bundle, k);
179 c[_matrix->row_index()] = ARDOUR::BundleChannel ((*j)->bundle, l);
181 PortMatrixNode::State const s = _matrix->get_state (c);
183 switch (s) {
184 case PortMatrixNode::ASSOCIATED:
185 draw_association_indicator (cr, x, y);
186 break;
188 case PortMatrixNode::NOT_ASSOCIATED:
189 break;
191 default:
192 break;
195 y += grid_spacing();
198 x += grid_spacing();
201 by += (*j)->bundle->nchannels () * grid_spacing();
204 bx += (*i)->bundle->nchannels () * grid_spacing();
209 void
210 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
212 set_source_rgba (cr, association_colour(), 0.5);
214 cairo_arc (
216 x + grid_spacing() / 2,
217 y + grid_spacing() / 2,
218 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
220 p * 2 * M_PI
223 cairo_fill (cr);
226 void
227 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
229 set_source_rgb (cr, background_colour());
230 cairo_rectangle (
232 x + thick_grid_line_width(),
233 y + thick_grid_line_width(),
234 grid_spacing() - 2 * thick_grid_line_width(),
235 grid_spacing() - 2 * thick_grid_line_width()
237 cairo_fill (cr);
240 PortMatrixNode
241 PortMatrixGrid::position_to_node (double x, double y) const
243 return PortMatrixNode (
244 position_to_channel (y, x, _matrix->visible_rows()),
245 position_to_channel (x, y, _matrix->visible_columns())
249 void
250 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t, guint)
252 ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
253 ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
255 if (b == 1) {
257 _dragging = true;
258 _drag_valid = (px.bundle && py.bundle);
260 _moved = false;
261 _drag_start_x = x / grid_spacing ();
262 _drag_start_y = y / grid_spacing ();
264 } else if (b == 3) {
266 _matrix->popup_menu (px, py, t);
271 PortMatrixNode::State
272 PortMatrixGrid::get_association (PortMatrixNode node) const
274 if (_matrix->show_only_bundles()) {
276 bool have_off_diagonal_association = false;
277 bool have_diagonal_association = false;
278 bool have_diagonal_not_association = false;
280 for (uint32_t i = 0; i < node.row.bundle->nchannels (); ++i) {
282 for (uint32_t j = 0; j < node.column.bundle->nchannels (); ++j) {
284 ARDOUR::BundleChannel c[2];
285 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
286 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
288 PortMatrixNode::State const s = _matrix->get_state (c);
290 switch (s) {
291 case PortMatrixNode::ASSOCIATED:
292 if (i == j) {
293 have_diagonal_association = true;
294 } else {
295 have_off_diagonal_association = true;
297 break;
299 case PortMatrixNode::NOT_ASSOCIATED:
300 if (i == j) {
301 have_diagonal_not_association = true;
303 break;
305 default:
306 break;
311 if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
312 return PortMatrixNode::ASSOCIATED;
313 } else if (!have_diagonal_association && !have_off_diagonal_association) {
314 return PortMatrixNode::NOT_ASSOCIATED;
317 return PortMatrixNode::PARTIAL;
319 } else {
321 ARDOUR::BundleChannel c[2];
322 c[_matrix->column_index()] = node.column;
323 c[_matrix->row_index()] = node.row;
324 return _matrix->get_state (c);
328 /* NOTREACHED */
329 return PortMatrixNode::NOT_ASSOCIATED;
332 void
333 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
335 if (_matrix->show_only_bundles()) {
337 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
338 for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
340 ARDOUR::BundleChannel c[2];
341 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
342 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
343 _matrix->set_state (c, s && (i == j));
347 } else {
349 if (node.row.bundle && node.column.bundle) {
351 ARDOUR::BundleChannel c[2];
352 c[_matrix->row_index()] = node.row;
353 c[_matrix->column_index()] = node.column;
354 _matrix->set_state (c, s);
359 void
360 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/, guint s)
362 if (b == 1) {
364 if (x != -1) {
366 if (_dragging && _moved) {
368 if (_drag_valid) {
369 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
371 if (!p.empty()) {
372 PortMatrixNode::State const s = get_association (p.front());
373 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
374 set_association (*i, toggle_state (s));
379 } else {
381 if (Keyboard::modifier_state_equals (s, Keyboard::PrimaryModifier)) {
382 /* associate/disassociate things diagonally down and right until we run out */
383 PortMatrixNode::State s = (PortMatrixNode::State) 0;
384 while (1) {
385 PortMatrixNode const n = position_to_node (x, y);
386 if (n.row.bundle && n.column.bundle) {
387 if (s == (PortMatrixNode::State) 0) {
388 s = get_association (n);
390 set_association (n, toggle_state (s));
391 } else {
392 break;
394 x += grid_spacing ();
395 y += grid_spacing ();
398 } else {
400 PortMatrixNode const n = position_to_node (x, y);
401 if (n.row.bundle && n.column.bundle) {
402 PortMatrixNode::State const s = get_association (n);
403 set_association (n, toggle_state (s));
408 require_render ();
411 _body->queue_draw ();
414 _dragging = false;
418 void
419 PortMatrixGrid::draw_extra (cairo_t* cr)
421 set_source_rgba (cr, mouseover_line_colour(), 0.3);
422 cairo_set_line_width (cr, mouseover_line_width());
424 list<PortMatrixNode> const m = _body->mouseover ();
426 for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
428 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
429 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
431 if (i->row.bundle && i->column.bundle) {
433 cairo_move_to (cr, x, y);
434 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
435 cairo_line_to (cr, component_to_parent_x (0), y);
436 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
437 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
439 cairo_stroke (cr);
441 cairo_move_to (cr, x, y);
442 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
443 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
444 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
445 cairo_line_to (cr, x, component_to_parent_y (0));
447 cairo_stroke (cr);
451 if (_dragging && _drag_valid && _moved) {
453 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
455 if (!p.empty()) {
457 bool const s = toggle_state (get_association (p.front()));
459 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
460 if (s) {
461 draw_association_indicator (
463 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
464 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
466 } else {
467 draw_empty_square (
469 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
470 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
476 set_source_rgba (cr, association_colour (), 0.3);
478 cairo_move_to (
480 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
481 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
484 cairo_line_to (
486 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
487 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
490 cairo_stroke (cr);
495 void
496 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
498 queue_draw_for (old);
499 queue_draw_for (_body->mouseover());
502 void
503 PortMatrixGrid::motion (double x, double y)
505 _body->set_mouseover (position_to_node (x, y));
507 int const px = x / grid_spacing ();
508 int const py = y / grid_spacing ();
510 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
511 _moved = true;
514 if (_dragging && _drag_valid && _moved) {
515 _drag_x = px;
516 _drag_y = py;
517 _body->queue_draw ();
521 void
522 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
524 for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
526 if (i->row.bundle) {
528 double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
529 _body->queue_draw_area (
530 _parent_rectangle.get_x(),
531 component_to_parent_y (y),
532 _parent_rectangle.get_width(),
533 grid_spacing()
537 if (i->column.bundle) {
539 double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
541 _body->queue_draw_area (
542 component_to_parent_x (x),
543 _parent_rectangle.get_y(),
544 grid_spacing(),
545 _parent_rectangle.get_height()
551 double
552 PortMatrixGrid::component_to_parent_x (double x) const
554 return x - _body->xoffset() + _parent_rectangle.get_x();
557 double
558 PortMatrixGrid::parent_to_component_x (double x) const
560 return x + _body->xoffset() - _parent_rectangle.get_x();
563 double
564 PortMatrixGrid::component_to_parent_y (double y) const
566 return y - _body->yoffset() + _parent_rectangle.get_y();
569 double
570 PortMatrixGrid::parent_to_component_y (double y) const
572 return y + _body->yoffset() - _parent_rectangle.get_y();
575 list<PortMatrixNode>
576 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
578 list<PortMatrixNode> p;
580 bool const steep = abs (y1 - y0) > abs (x1 - x0);
581 if (steep) {
582 int tmp = x0;
583 x0 = y0;
584 y0 = tmp;
586 tmp = y1;
587 y1 = x1;
588 x1 = tmp;
591 if (x0 > x1) {
592 int tmp = x0;
593 x0 = x1;
594 x1 = tmp;
596 tmp = y0;
597 y0 = y1;
598 y1 = tmp;
601 int dx = x1 - x0;
602 int dy = abs (y1 - y0);
604 double err = 0;
605 double derr = double (dy) / dx;
607 int y = y0;
608 int const ystep = y0 < y1 ? 1 : -1;
610 for (int x = x0; x <= x1; ++x) {
611 if (steep) {
612 PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
613 if (n.row.bundle && n.column.bundle) {
614 p.push_back (n);
616 } else {
617 PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
618 if (n.row.bundle && n.column.bundle) {
619 p.push_back (n);
623 err += derr;
625 if (err >= 0.5) {
626 y += ystep;
627 err -= 1;
631 return p;
634 bool
635 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
637 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);