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