Stop bundles disappearing from the port matrix when they
[ardour2.git] / gtk2_ardour / port_matrix_grid.cc
blobb87f560984539c0a5b560f2d0d0a82bd51e7ff21
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 < _matrix->count_of_our_type_min_1 ((*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 if (_matrix->show_only_bundles ()) {
101 cairo_move_to (cr, x, 0);
102 cairo_line_to (cr, x, _height);
103 cairo_stroke (cr);
106 uint32_t y = 0;
108 /* HORIZONTAL GRID LINES */
110 N = 0;
111 for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
113 cairo_set_line_width (cr, thick_grid_line_width());
114 cairo_move_to (cr, 0, y);
115 cairo_line_to (cr, _width, y);
116 cairo_stroke (cr);
118 if (!_matrix->show_only_bundles()) {
119 cairo_set_line_width (cr, thin_grid_line_width());
120 for (uint32_t j = 0; j < _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels()); ++j) {
121 y += grid_spacing ();
122 cairo_move_to (cr, 0, y);
123 cairo_line_to (cr, _width, y);
124 cairo_stroke (cr);
127 } else {
129 y += grid_spacing ();
133 ++N;
136 if (_matrix->show_only_bundles ()) {
137 cairo_move_to (cr, 0, y);
138 cairo_line_to (cr, _width, y);
139 cairo_stroke (cr);
142 /* ASSOCIATION INDICATORS and NON-CONNECTABLE INDICATORS */
144 /* we draw a grey square in a matrix box if the two ports that intersect at that box
145 cannot be connected because they are of different types (MIDI vs. audio)
148 uint32_t bx = 0;
149 uint32_t by = 0;
151 if (_matrix->show_only_bundles()) {
153 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
154 by = 0;
156 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
158 PortMatrixNode::State s = _matrix->get_association (PortMatrixNode (
159 ARDOUR::BundleChannel ((*j)->bundle, 0),
160 ARDOUR::BundleChannel ((*i)->bundle, 0)
162 switch (s) {
163 case PortMatrixNode::ASSOCIATED:
164 draw_association_indicator (cr, bx, by);
165 break;
166 case PortMatrixNode::PARTIAL:
167 draw_association_indicator (cr, bx, by, 0.5);
168 break;
169 default:
170 break;
173 by += grid_spacing();
176 bx += grid_spacing();
180 } else {
182 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
183 by = 0;
185 for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
187 x = bx;
188 for (uint32_t k = 0; k < _matrix->count_of_our_type ((*i)->bundle->nchannels()); ++k) {
190 y = by;
191 for (uint32_t l = 0; l < _matrix->count_of_our_type ((*j)->bundle->nchannels()); ++l) {
193 ARDOUR::BundleChannel c[2];
194 c[_matrix->column_index()] = ARDOUR::BundleChannel ((*i)->bundle, k);
195 c[_matrix->row_index()] = ARDOUR::BundleChannel ((*j)->bundle, l);
197 if (c[0].bundle->channel_type (c[0].channel) != c[1].bundle->channel_type (c[1].channel)) {
198 /* these two channels are of different types */
199 draw_non_connectable_indicator (cr, x, y);
200 } else {
201 /* these two channels might be associated */
202 PortMatrixNode::State const s = _matrix->get_state (c);
204 switch (s) {
205 case PortMatrixNode::ASSOCIATED:
206 draw_association_indicator (cr, x, y);
207 break;
209 case PortMatrixNode::NOT_ASSOCIATED:
210 break;
212 default:
213 break;
217 y += grid_spacing();
220 if ((*j)->bundle->nchannels() == ARDOUR::ChanCount::ZERO) {
221 draw_non_connectable_indicator (cr, x, y);
224 x += grid_spacing();
227 if ((*i)->bundle->nchannels() == ARDOUR::ChanCount::ZERO) {
228 draw_non_connectable_indicator (cr, x, y);
231 by += _matrix->count_of_our_type_min_1 ((*j)->bundle->nchannels()) * grid_spacing();
234 bx += _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels()) * grid_spacing();
239 void
240 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
242 set_source_rgba (cr, association_colour(), 0.5);
244 cairo_arc (
246 x + grid_spacing() / 2,
247 y + grid_spacing() / 2,
248 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
250 p * 2 * M_PI
253 cairo_fill (cr);
256 void
257 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
259 set_source_rgb (cr, background_colour());
260 cairo_rectangle (
262 x + thick_grid_line_width(),
263 y + thick_grid_line_width(),
264 grid_spacing() - 2 * thick_grid_line_width(),
265 grid_spacing() - 2 * thick_grid_line_width()
267 cairo_fill (cr);
270 /** Draw a square to indicate that two channels in a matrix cannot be associated
271 * with each other.
273 void
274 PortMatrixGrid::draw_non_connectable_indicator (cairo_t* cr, uint32_t x, uint32_t y)
276 set_source_rgb (cr, non_connectable_colour ());
277 cairo_rectangle (
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()
284 cairo_fill (cr);
287 PortMatrixNode
288 PortMatrixGrid::position_to_node (double x, double y) const
290 return PortMatrixNode (
291 position_to_channel (y, x, _matrix->visible_rows()),
292 position_to_channel (x, y, _matrix->visible_columns())
296 void
297 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t, guint)
299 ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
300 ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
302 if (b == 1) {
304 _dragging = true;
305 _drag_valid = (px.bundle && py.bundle);
307 _moved = false;
308 _drag_start_x = x / grid_spacing ();
309 _drag_start_y = y / grid_spacing ();
311 } else if (b == 3) {
313 _matrix->popup_menu (px, py, t);
318 void
319 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
321 if (_matrix->show_only_bundles()) {
323 for (uint32_t i = 0; i < node.column.bundle->nchannels().n_total(); ++i) {
324 for (uint32_t j = 0; j < node.row.bundle->nchannels().n_total(); ++j) {
326 if (!_matrix->should_show (node.column.bundle->channel_type(i)) || !_matrix->should_show (node.row.bundle->channel_type(j))) {
327 continue;
330 ARDOUR::BundleChannel c[2];
331 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
332 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
333 _matrix->set_state (c, s && (i == j));
337 } else {
339 if (node.row.bundle && node.column.bundle) {
341 ARDOUR::BundleChannel c[2];
342 c[_matrix->row_index()] = node.row;
343 c[_matrix->column_index()] = node.column;
344 _matrix->set_state (c, s);
349 void
350 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/, guint s)
352 if (b == 1) {
354 if (x != -1) {
356 if (_dragging && _moved) {
358 if (_drag_valid) {
359 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
361 if (!p.empty()) {
362 PortMatrixNode::State const s = _matrix->get_association (p.front());
363 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
364 set_association (*i, toggle_state (s));
369 } else {
371 if (Keyboard::modifier_state_equals (s, Keyboard::PrimaryModifier)) {
372 /* associate/disassociate things diagonally down and right until we run out */
373 PortMatrixNode::State s = (PortMatrixNode::State) 0;
374 while (1) {
375 PortMatrixNode const n = position_to_node (x, y);
376 if (n.row.bundle && n.column.bundle) {
377 if (s == (PortMatrixNode::State) 0) {
378 s = _matrix->get_association (n);
380 set_association (n, toggle_state (s));
381 } else {
382 break;
384 x += grid_spacing ();
385 y += grid_spacing ();
388 } else {
390 PortMatrixNode const n = position_to_node (x, y);
391 if (n.row.bundle && n.column.bundle) {
392 PortMatrixNode::State const s = _matrix->get_association (n);
393 set_association (n, toggle_state (s));
398 require_render ();
401 _body->queue_draw ();
404 _dragging = false;
408 void
409 PortMatrixGrid::draw_extra (cairo_t* cr)
411 set_source_rgba (cr, mouseover_line_colour(), 0.3);
412 cairo_set_line_width (cr, mouseover_line_width());
414 list<PortMatrixNode> const m = _body->mouseover ();
416 for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
418 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
419 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
421 if (PortMatrix::bundle_with_channels (i->row.bundle) && PortMatrix::bundle_with_channels (i->column.bundle)) {
423 cairo_move_to (cr, x, y);
424 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
425 cairo_line_to (cr, component_to_parent_x (0), y);
426 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
427 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
429 cairo_stroke (cr);
431 cairo_move_to (cr, x, y);
432 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
433 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
434 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
435 cairo_line_to (cr, x, component_to_parent_y (0));
437 cairo_stroke (cr);
441 if (_dragging && _drag_valid && _moved) {
443 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
445 if (!p.empty()) {
447 bool const s = toggle_state (_matrix->get_association (p.front()));
449 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
450 if (s) {
451 draw_association_indicator (
453 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
454 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
456 } else {
457 draw_empty_square (
459 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
460 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
466 set_source_rgba (cr, association_colour (), 0.3);
468 cairo_move_to (
470 component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
471 component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
474 cairo_line_to (
476 component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
477 component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
480 cairo_stroke (cr);
485 void
486 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
488 queue_draw_for (old);
489 queue_draw_for (_body->mouseover());
492 void
493 PortMatrixGrid::motion (double x, double y)
495 _body->set_mouseover (position_to_node (x, y));
497 int const px = x / grid_spacing ();
498 int const py = y / grid_spacing ();
500 if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
501 _moved = true;
504 if (_dragging && _drag_valid && _moved) {
505 _drag_x = px;
506 _drag_y = py;
507 _body->queue_draw ();
511 void
512 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
514 for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
516 if (i->row.bundle) {
518 double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
519 _body->queue_draw_area (
520 _parent_rectangle.get_x(),
521 component_to_parent_y (y),
522 _parent_rectangle.get_width(),
523 grid_spacing()
527 if (i->column.bundle) {
529 double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
531 _body->queue_draw_area (
532 component_to_parent_x (x),
533 _parent_rectangle.get_y(),
534 grid_spacing(),
535 _parent_rectangle.get_height()
541 double
542 PortMatrixGrid::component_to_parent_x (double x) const
544 return x - _body->xoffset() + _parent_rectangle.get_x();
547 double
548 PortMatrixGrid::parent_to_component_x (double x) const
550 return x + _body->xoffset() - _parent_rectangle.get_x();
553 double
554 PortMatrixGrid::component_to_parent_y (double y) const
556 return y - _body->yoffset() + _parent_rectangle.get_y();
559 double
560 PortMatrixGrid::parent_to_component_y (double y) const
562 return y + _body->yoffset() - _parent_rectangle.get_y();
565 list<PortMatrixNode>
566 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
568 list<PortMatrixNode> p;
570 bool const steep = abs (y1 - y0) > abs (x1 - x0);
571 if (steep) {
572 int tmp = x0;
573 x0 = y0;
574 y0 = tmp;
576 tmp = y1;
577 y1 = x1;
578 x1 = tmp;
581 if (x0 > x1) {
582 int tmp = x0;
583 x0 = x1;
584 x1 = tmp;
586 tmp = y0;
587 y0 = y1;
588 y1 = tmp;
591 int dx = x1 - x0;
592 int dy = abs (y1 - y0);
594 double err = 0;
595 double derr = double (dy) / dx;
597 int y = y0;
598 int const ystep = y0 < y1 ? 1 : -1;
600 for (int x = x0; x <= x1; ++x) {
601 if (steep) {
602 PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
603 if (n.row.bundle && n.column.bundle) {
604 p.push_back (n);
606 } else {
607 PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
608 if (n.row.bundle && n.column.bundle) {
609 p.push_back (n);
613 err += derr;
615 if (err >= 0.5) {
616 y += ystep;
617 err -= 1;
621 return p;
624 bool
625 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
627 return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);