feaf6b1222dcd7d2bab8f82119eef960ca2a68b7
[ardour.git] / gtk2_ardour / port_matrix_grid.cc
1 /*
2     Copyright (C) 2002-2009 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
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
28 using namespace std;
29
30 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
31         : PortMatrixComponent (m, b),
32           _dragging (false),
33           _moved (false)
34 {
35         
36 }
37
38 void
39 PortMatrixGrid::compute_dimensions ()
40 {
41         _width = 0;
42
43         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
44                 _width += group_size (*i) * grid_spacing ();
45         }
46
47         _height = 0;
48         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
49                 _height += group_size (*i) * grid_spacing ();
50         }
51 }
52
53
54 void
55 PortMatrixGrid::render (cairo_t* cr)
56 {
57         set_source_rgb (cr, background_colour());
58         cairo_rectangle (cr, 0, 0, _width, _height);
59         cairo_fill (cr);
60
61         uint32_t x = 0;
62         for (PortGroupList::List::const_iterator c = _matrix->columns()->begin(); c != _matrix->columns()->end(); ++c) {
63
64                 uint32_t y = 0;
65                 for (PortGroupList::List::const_iterator r = _matrix->rows()->begin(); r != _matrix->rows()->end(); ++r) {
66
67                         if ((*c)->visible() && (*r)->visible()) {
68                                 render_group_pair (cr, *r, *c, x, y);
69                         }
70
71                         y += group_size (*r) * grid_spacing ();
72                 }
73
74                 x += group_size (*c) * grid_spacing ();
75         }
76 }
77
78 void
79 PortMatrixGrid::render_group_pair (cairo_t* cr, boost::shared_ptr<const PortGroup> row, boost::shared_ptr<const PortGroup> column, uint32_t const x, uint32_t const y)
80 {
81         PortGroup::BundleList const & row_bundles = row->bundles();
82         PortGroup::BundleList const & column_bundles = column->bundles();
83
84         /* unfortunately we need to compute the height of the row group here */
85         uint32_t height = group_size (row) * grid_spacing ();
86         
87         uint32_t tx = x;
88
89         /* VERTICAL GRID LINES */
90         
91         set_source_rgb (cr, grid_colour());
92         uint32_t N = 0;
93         
94         for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
95
96                 cairo_set_line_width (cr, thick_grid_line_width());
97                 cairo_move_to (cr, tx, y);
98                 cairo_line_to (cr, tx, y + height);
99                 cairo_stroke (cr);
100                 
101                 if (!_matrix->show_only_bundles()) {
102                         cairo_set_line_width (cr, thin_grid_line_width());
103                         for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
104                                 tx += grid_spacing ();
105                                 cairo_move_to (cr, tx, y);
106                                 cairo_line_to (cr, tx, y + height);
107                                 cairo_stroke (cr);
108                         }
109                         
110                 } else {
111                         
112                         tx += grid_spacing ();
113                         
114                 }
115                 
116                 ++N;
117         }
118
119         uint32_t const width = tx - x;
120
121         uint32_t ty = y;
122         
123         /* HORIZONTAL GRID LINES */
124         
125         N = 0;
126         for (PortGroup::BundleList::const_iterator i = row_bundles.begin(); i != row_bundles.end(); ++i) {
127                 
128                 cairo_set_line_width (cr, thick_grid_line_width());
129                 cairo_move_to (cr, x, ty);
130                 cairo_line_to (cr, x + width, ty);
131                 cairo_stroke (cr);
132                 
133                 if (!_matrix->show_only_bundles()) {
134                         cairo_set_line_width (cr, thin_grid_line_width());
135                         for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
136                                 ty += grid_spacing ();
137                                 cairo_move_to (cr, x, ty);
138                                 cairo_line_to (cr, x + width, ty);
139                                 cairo_stroke (cr);
140                         }
141
142                 } else {
143
144                         ty += grid_spacing ();
145
146                 }
147                 
148                 ++N;
149         }
150         
151         /* ASSOCIATION INDICATORS */
152
153         uint32_t bx = x;
154         uint32_t by = y;
155
156         if (_matrix->show_only_bundles()) {
157
158                 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
159                         by = y;
160                         
161                         for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
162                                 
163                                 PortMatrixNode::State s = bundle_to_bundle_state (i->bundle, j->bundle);
164                                 switch (s) {
165                                 case PortMatrixNode::UNKNOWN:
166                                         draw_unknown_indicator (cr, bx, by);
167                                         break;
168                                 case PortMatrixNode::ASSOCIATED:
169                                         draw_association_indicator (cr, bx, by);
170                                         break;
171                                 case PortMatrixNode::PARTIAL:
172                                         draw_association_indicator (cr, bx, by, 0.5);
173                                         break;
174                                 default:
175                                         break;
176                                 }
177                                 
178                                 by += grid_spacing();
179                         }
180                         
181                         bx += grid_spacing();
182                         
183                 }
184
185         } else {
186
187                 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
188                         by = y;
189                         
190                         for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
191                                 
192                                 tx = bx;
193                                 for (uint32_t k = 0; k < i->bundle->nchannels (); ++k) {
194                                         
195                                         ty = by;
196                                         for (uint32_t l = 0; l < j->bundle->nchannels (); ++l) {
197                                                 
198                                                 ARDOUR::BundleChannel c[2];
199                                                 c[_matrix->column_index()] = ARDOUR::BundleChannel (i->bundle, k);
200                                                 c[_matrix->row_index()] = ARDOUR::BundleChannel (j->bundle, l);
201                                                 
202                                                 PortMatrixNode::State const s = _matrix->get_state (c);
203                                                 
204                                                 switch (s) {
205                                                 case PortMatrixNode::ASSOCIATED:
206                                                         draw_association_indicator (cr, tx, ty);
207                                                         break;
208                                                         
209                                                 case PortMatrixNode::UNKNOWN:
210                                                         draw_unknown_indicator (cr, tx, ty);
211                                                         break;
212                                                         
213                                                 case PortMatrixNode::NOT_ASSOCIATED:
214                                                         break;
215                                                         
216                                                 default:
217                                                         break;
218                                                 }
219                                                 
220                                                 ty += grid_spacing();
221                                         }
222                                         
223                                         tx += grid_spacing();
224                                 }
225                                 
226                                 by += j->bundle->nchannels () * grid_spacing();
227                         }
228                         
229                         bx += i->bundle->nchannels () * grid_spacing();
230                 }
231         }
232 }
233
234 void
235 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
236 {
237         set_source_rgba (cr, association_colour(), 0.5);
238
239         cairo_arc (
240                 cr,
241                 x + grid_spacing() / 2,
242                 y + grid_spacing() / 2,
243                 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
244                 0,
245                 p * 2 * M_PI
246                 );
247         
248         cairo_fill (cr);
249 }
250
251 void
252 PortMatrixGrid::draw_unknown_indicator (cairo_t* cr, uint32_t x, uint32_t y)
253 {
254         set_source_rgba (cr, unknown_colour(), 0.5);
255         cairo_rectangle (
256                 cr,
257                 x + thick_grid_line_width(),
258                 y + thick_grid_line_width(),
259                 grid_spacing() - 2 * thick_grid_line_width(),
260                 grid_spacing() - 2 * thick_grid_line_width()
261                 );
262         cairo_fill (cr);
263 }
264
265 PortMatrixNode
266 PortMatrixGrid::position_to_node (uint32_t x, uint32_t y) const
267 {
268         return PortMatrixNode (
269                 position_to_group_and_channel (y, _matrix->rows()).second,
270                 position_to_group_and_channel (x, _matrix->columns()).second
271                 );
272 }
273
274 void
275 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t)
276 {
277         if (b == 1) {
278
279                 _dragging = true;
280                 _moved = false;
281                 _drag_start_x = x / grid_spacing ();
282                 _drag_start_y = y / grid_spacing ();
283
284         } else if (b == 3) {
285
286                 _matrix->popup_menu (
287                         position_to_group_and_channel (x / grid_spacing(), _matrix->columns()),
288                         position_to_group_and_channel (y / grid_spacing(), _matrix->rows()), t);
289                 
290         }
291 }
292
293 void
294 PortMatrixGrid::set_association (PortMatrixNode node)
295 {
296         if (_matrix->show_only_bundles()) {
297                                 
298                 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
299                         for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
300                                 
301                                 ARDOUR::BundleChannel c[2];
302                                 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
303                                 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
304                                 _matrix->set_state (c, true);
305                         }
306                 }
307                 
308         } else {
309                 
310                 if (node.row.bundle && node.column.bundle) {
311                         
312                         ARDOUR::BundleChannel c[2];
313                         c[_matrix->row_index()] = node.row;
314                         c[_matrix->column_index()] = node.column;
315                         _matrix->set_state (c, true);
316                 }
317         }
318 }
319
320 void
321 PortMatrixGrid::toggle_association (PortMatrixNode node)
322 {
323         if (_matrix->show_only_bundles()) {
324                                 
325                 PortMatrixNode::State const s = bundle_to_bundle_state (node.column.bundle, node.row.bundle);
326                 
327                 for (uint32_t i = 0; i < node.column.bundle->nchannels(); ++i) {
328                         for (uint32_t j = 0; j < node.row.bundle->nchannels(); ++j) {
329                                 
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                                 if (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL) {
334                                         _matrix->set_state (c, i == j);
335                                 } else {
336                                         _matrix->set_state (c, false);
337                                 }
338                         }
339                 }
340                 
341         } else {
342                 
343                 if (node.row.bundle && node.column.bundle) {
344                         
345                         ARDOUR::BundleChannel c[2];
346                         c[_matrix->row_index()] = node.row;
347                         c[_matrix->column_index()] = node.column;
348                         
349                         PortMatrixNode::State const s = _matrix->get_state (c);
350                         
351                         if (s == PortMatrixNode::ASSOCIATED || s == PortMatrixNode::NOT_ASSOCIATED) {
352                                 bool const n = !(s == PortMatrixNode::ASSOCIATED);
353                                 _matrix->set_state (c, n);
354                         }
355                         
356                 }
357         }
358 }
359
360 void
361 PortMatrixGrid::button_release (double x, double y, int b, uint32_t t)
362 {
363         if (b == 1) {
364
365                 if (_dragging && _moved) {
366
367                         list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
368
369                         for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
370                                 set_association (*i);
371                         }
372
373                 } else {
374
375                         toggle_association (position_to_node (x / grid_spacing(), y / grid_spacing()));
376                 }
377
378                 require_render ();
379                 _body->queue_draw ();
380         }
381
382         _dragging = false;
383 }
384
385
386 void
387 PortMatrixGrid::draw_extra (cairo_t* cr)
388 {
389         set_source_rgba (cr, mouseover_line_colour(), 0.3);
390         cairo_set_line_width (cr, mouseover_line_width());
391
392         double const x = component_to_parent_x (channel_to_position (_body->mouseover().column, _matrix->columns()) * grid_spacing()) + grid_spacing() / 2;
393         double const y = component_to_parent_y (channel_to_position (_body->mouseover().row, _matrix->rows()) * grid_spacing()) + grid_spacing() / 2;
394         
395         if (_body->mouseover().row.bundle) {
396
397                 cairo_move_to (cr, x, y);
398                 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
399                         cairo_line_to (cr, component_to_parent_x (0), y);
400                 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
401                         cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
402                 }
403                 cairo_stroke (cr);
404         }
405
406         if (_body->mouseover().column.bundle) {
407
408                 cairo_move_to (cr, x, y);
409                 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
410                         cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
411                 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
412                         cairo_line_to (cr, x, component_to_parent_y (0));
413                 }
414                 cairo_stroke (cr);
415         }
416
417         if (_dragging && _moved) {
418                 
419                 set_source_rgba (cr, association_colour (), 0.3);
420
421                 cairo_move_to (
422                         cr,
423                         component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
424                         component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
425                         );
426                 
427                 cairo_line_to (
428                         cr,
429                         component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
430                         component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
431                         );
432                 
433                 cairo_stroke (cr);
434
435                 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
436
437                 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
438                         draw_association_indicator (
439                                 cr,
440                                 component_to_parent_x (channel_to_position (i->column, _matrix->columns()) * grid_spacing ()),
441                                 component_to_parent_y (channel_to_position (i->row, _matrix->rows()) * grid_spacing ())
442                                 );
443                 }
444         }
445 }
446
447 void
448 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
449 {
450         queue_draw_for (old);
451         queue_draw_for (_body->mouseover());
452 }
453
454 void
455 PortMatrixGrid::motion (double x, double y)
456 {
457         _body->set_mouseover (position_to_node (x / grid_spacing(), y / grid_spacing()));
458
459         int const px = x / grid_spacing ();
460         int const py = y / grid_spacing ();
461
462         if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
463                 _moved = true;
464         }
465
466         if (_dragging && _moved) {
467                 _drag_x = px;
468                 _drag_y = py;
469                 _body->queue_draw ();
470         }
471 }
472
473 void
474 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
475 {
476         if (n.row.bundle) {
477
478                 double const y = channel_to_position (n.row, _matrix->rows()) * grid_spacing ();
479                 _body->queue_draw_area (
480                         _parent_rectangle.get_x(),
481                         component_to_parent_y (y),
482                         _parent_rectangle.get_width(),
483                         grid_spacing()
484                         );
485         }
486
487         if (n.column.bundle) {
488
489                 double const x = channel_to_position (n.column, _matrix->columns()) * grid_spacing ();
490                 
491                 _body->queue_draw_area (
492                         component_to_parent_x (x),
493                         _parent_rectangle.get_y(),
494                         grid_spacing(),
495                         _parent_rectangle.get_height()
496                         );
497         }
498 }
499
500 double
501 PortMatrixGrid::component_to_parent_x (double x) const
502 {
503         return x - _body->xoffset() + _parent_rectangle.get_x();
504 }
505
506 double
507 PortMatrixGrid::parent_to_component_x (double x) const
508 {
509         return x + _body->xoffset() - _parent_rectangle.get_x();
510 }
511
512 double
513 PortMatrixGrid::component_to_parent_y (double y) const
514 {
515         return y - _body->yoffset() + _parent_rectangle.get_y();
516 }
517
518 double
519 PortMatrixGrid::parent_to_component_y (double y) const
520 {
521         return y + _body->yoffset() - _parent_rectangle.get_y();
522 }
523
524 PortMatrixNode::State
525 PortMatrixGrid::bundle_to_bundle_state (boost::shared_ptr<ARDOUR::Bundle> a, boost::shared_ptr<ARDOUR::Bundle> b) const
526 {
527         bool have_unknown = false;
528         bool have_off_diagonal_association = false;
529         bool have_diagonal_association = false;
530         bool have_diagonal_not_association = false;
531                                 
532         for (uint32_t i = 0; i < a->nchannels (); ++i) {
533                                         
534                 for (uint32_t j = 0; j < b->nchannels (); ++j) {
535                                                 
536                         ARDOUR::BundleChannel c[2];
537                         c[_matrix->column_index()] = ARDOUR::BundleChannel (a, i);
538                         c[_matrix->row_index()] = ARDOUR::BundleChannel (b, j);
539                         
540                         PortMatrixNode::State const s = _matrix->get_state (c);
541
542                         switch (s) {
543                         case PortMatrixNode::ASSOCIATED:
544                                 if (i == j) {
545                                         have_diagonal_association = true;
546                                 } else {
547                                         have_off_diagonal_association = true;
548                                 }
549                                 break;
550                                 
551                         case PortMatrixNode::UNKNOWN:
552                                 have_unknown = true;
553                                 break;
554                                 
555                         case PortMatrixNode::NOT_ASSOCIATED:
556                                 if (i == j) {
557                                         have_diagonal_not_association = true;
558                                 }
559                                 break;
560
561                         default:
562                                 break;
563                         }
564                 }
565         }
566         
567         if (have_unknown) {
568                 return PortMatrixNode::UNKNOWN;
569         } else if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
570                 return PortMatrixNode::ASSOCIATED;
571         } else if (!have_diagonal_association && !have_off_diagonal_association) {
572                 return PortMatrixNode::NOT_ASSOCIATED;
573         }
574
575         return PortMatrixNode::PARTIAL;
576 }
577
578 list<PortMatrixNode>
579 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
580 {
581         list<PortMatrixNode> p;
582
583         bool const steep = abs (y1 - y0) > abs (x1 - x0);
584         if (steep) {
585                 int tmp = x0;
586                 x0 = y0;
587                 y0 = tmp;
588
589                 tmp = y1;
590                 y1 = x1;
591                 x1 = tmp;
592         }
593
594         if (x0 > x1) {
595                 int tmp = x0;
596                 x0 = x1;
597                 x1 = tmp;
598
599                 tmp = y0;
600                 y0 = y1;
601                 y1 = tmp;
602         }
603
604         int dx = x1 - x0;
605         int dy = abs (y1 - y0);
606         
607         double err = 0;
608         double derr = double (dy) / dx;
609
610         int y = y0;
611         int const ystep = y0 < y1 ? 1 : -1;
612
613         for (int x = x0; x <= x1; ++x) {
614                 if (steep) {
615                         p.push_back (position_to_node (y, x));
616                 } else {
617                         p.push_back (position_to_node (x, y));
618                 }
619
620                 err += derr;
621
622                 if (err >= 0.5) {
623                         y += ystep;
624                         err -= 1;
625                 }
626         }
627
628         return p;
629 }