Allow port matrix to show both audio and midi ports at the same time, and use that...
[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 */
131
132         uint32_t bx = 0;
133         uint32_t by = 0;
134
135         if (_matrix->show_only_bundles()) {
136
137                 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
138                         by = 0;
139
140                         for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
141
142                                 PortMatrixNode::State s = get_association (PortMatrixNode (
143                                                                                    ARDOUR::BundleChannel ((*i)->bundle, 0),
144                                                                                    ARDOUR::BundleChannel ((*j)->bundle, 0)
145                                                                                    ));
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;
155                                 }
156
157                                 by += grid_spacing();
158                         }
159
160                         bx += grid_spacing();
161
162                 }
163
164         } else {
165
166                 for (PortGroup::BundleList::const_iterator i = column_bundles.begin(); i != column_bundles.end(); ++i) {
167                         by = 0;
168
169                         for (PortGroup::BundleList::const_iterator j = row_bundles.begin(); j != row_bundles.end(); ++j) {
170
171                                 x = bx;
172                                 for (uint32_t k = 0; k < (*i)->bundle->nchannels().n_total(); ++k) {
173
174                                         y = by;
175                                         for (uint32_t l = 0; l < (*j)->bundle->nchannels().n_total(); ++l) {
176
177                                                 if (!_matrix->should_show ((*i)->bundle->channel_type(k)) || !_matrix->should_show ((*j)->bundle->channel_type(l))) {
178                                                         continue;
179                                                 }
180
181                                                 ARDOUR::BundleChannel c[2];
182                                                 c[_matrix->column_index()] = ARDOUR::BundleChannel ((*i)->bundle, k);
183                                                 c[_matrix->row_index()] = ARDOUR::BundleChannel ((*j)->bundle, l);
184
185                                                 PortMatrixNode::State const s = _matrix->get_state (c);
186
187                                                 switch (s) {
188                                                 case PortMatrixNode::ASSOCIATED:
189                                                         draw_association_indicator (cr, x, y);
190                                                         break;
191
192                                                 case PortMatrixNode::NOT_ASSOCIATED:
193                                                         break;
194
195                                                 default:
196                                                         break;
197                                                 }
198
199                                                 y += grid_spacing();
200                                         }
201
202                                         x += grid_spacing();
203                                 }
204
205                                 by += _matrix->count_of_our_type ((*j)->bundle->nchannels()) * grid_spacing();
206                         }
207
208                         bx += _matrix->count_of_our_type ((*i)->bundle->nchannels()) * grid_spacing();
209                 }
210         }
211 }
212
213 void
214 PortMatrixGrid::draw_association_indicator (cairo_t* cr, uint32_t x, uint32_t y, double p)
215 {
216         set_source_rgba (cr, association_colour(), 0.5);
217
218         cairo_arc (
219                 cr,
220                 x + grid_spacing() / 2,
221                 y + grid_spacing() / 2,
222                 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
223                 0,
224                 p * 2 * M_PI
225                 );
226
227         cairo_fill (cr);
228 }
229
230 void
231 PortMatrixGrid::draw_empty_square (cairo_t* cr, uint32_t x, uint32_t y)
232 {
233         set_source_rgb (cr, background_colour());
234         cairo_rectangle (
235                 cr,
236                 x + thick_grid_line_width(),
237                 y + thick_grid_line_width(),
238                 grid_spacing() - 2 * thick_grid_line_width(),
239                 grid_spacing() - 2 * thick_grid_line_width()
240                 );
241         cairo_fill (cr);
242 }
243
244 PortMatrixNode
245 PortMatrixGrid::position_to_node (double x, double y) const
246 {
247         return PortMatrixNode (
248                 position_to_channel (y, x, _matrix->visible_rows()),
249                 position_to_channel (x, y, _matrix->visible_columns())
250                 );
251 }
252
253 void
254 PortMatrixGrid::button_press (double x, double y, int b, uint32_t t, guint)
255 {
256         ARDOUR::BundleChannel const px = position_to_channel (x, y, _matrix->visible_columns());
257         ARDOUR::BundleChannel const py = position_to_channel (y, x, _matrix->visible_rows());
258
259         if (b == 1) {
260                 
261                 _dragging = true;
262                 _drag_valid = (px.bundle && py.bundle);
263                 
264                 _moved = false;
265                 _drag_start_x = x / grid_spacing ();
266                 _drag_start_y = y / grid_spacing ();
267
268         } else if (b == 3) {
269
270                 _matrix->popup_menu (px, py, t);
271
272         }
273 }
274
275 PortMatrixNode::State
276 PortMatrixGrid::get_association (PortMatrixNode node) const
277 {
278         if (_matrix->show_only_bundles()) {
279
280                 bool have_off_diagonal_association = false;
281                 bool have_diagonal_association = false;
282                 bool have_diagonal_not_association = false;
283
284                 for (uint32_t i = 0; i < node.row.bundle->nchannels().n_total(); ++i) {
285
286                         for (uint32_t j = 0; j < node.column.bundle->nchannels().n_total(); ++j) {
287
288                                 if (!_matrix->should_show (node.row.bundle->channel_type(i)) || !_matrix->should_show (node.column.bundle->channel_type(j))) {
289                                         continue;
290                                 }
291
292                                 ARDOUR::BundleChannel c[2];
293                                 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.row.bundle, i);
294                                 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.column.bundle, j);
295
296                                 PortMatrixNode::State const s = _matrix->get_state (c);
297
298                                 switch (s) {
299                                 case PortMatrixNode::ASSOCIATED:
300                                         if (i == j) {
301                                                 have_diagonal_association = true;
302                                         } else {
303                                                 have_off_diagonal_association = true;
304                                         }
305                                         break;
306
307                                 case PortMatrixNode::NOT_ASSOCIATED:
308                                         if (i == j) {
309                                                 have_diagonal_not_association = true;
310                                         }
311                                         break;
312
313                                 default:
314                                         break;
315                                 }
316                         }
317                 }
318
319                 if (have_diagonal_association && !have_off_diagonal_association && !have_diagonal_not_association) {
320                         return PortMatrixNode::ASSOCIATED;
321                 } else if (!have_diagonal_association && !have_off_diagonal_association) {
322                         return PortMatrixNode::NOT_ASSOCIATED;
323                 }
324
325                 return PortMatrixNode::PARTIAL;
326
327         } else {
328
329                 ARDOUR::BundleChannel c[2];
330                 c[_matrix->column_index()] = node.column;
331                 c[_matrix->row_index()] = node.row;
332                 return _matrix->get_state (c);
333
334         }
335
336         /* NOTREACHED */
337         return PortMatrixNode::NOT_ASSOCIATED;
338 }
339
340 void
341 PortMatrixGrid::set_association (PortMatrixNode node, bool s)
342 {
343         if (_matrix->show_only_bundles()) {
344
345                 for (uint32_t i = 0; i < node.column.bundle->nchannels().n_total(); ++i) {
346                         for (uint32_t j = 0; j < node.row.bundle->nchannels().n_total(); ++j) {
347
348                                 if (!_matrix->should_show (node.column.bundle->channel_type(i)) || !_matrix->should_show (node.row.bundle->channel_type(j))) {
349                                         continue;
350                                 }
351                                 
352                                 ARDOUR::BundleChannel c[2];
353                                 c[_matrix->column_index()] = ARDOUR::BundleChannel (node.column.bundle, i);
354                                 c[_matrix->row_index()] = ARDOUR::BundleChannel (node.row.bundle, j);
355                                 _matrix->set_state (c, s && (i == j));
356                         }
357                 }
358
359         } else {
360
361                 if (node.row.bundle && node.column.bundle) {
362
363                         ARDOUR::BundleChannel c[2];
364                         c[_matrix->row_index()] = node.row;
365                         c[_matrix->column_index()] = node.column;
366                         _matrix->set_state (c, s);
367                 }
368         }
369 }
370
371 void
372 PortMatrixGrid::button_release (double x, double y, int b, uint32_t /*t*/, guint s)
373 {
374         if (b == 1) {
375
376                 if (x != -1) {
377                         
378                         if (_dragging && _moved) {
379                                 
380                                 if (_drag_valid) {
381                                         list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
382                                         
383                                         if (!p.empty()) {
384                                                 PortMatrixNode::State const s = get_association (p.front());
385                                                 for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
386                                                         set_association (*i, toggle_state (s));
387                                                 }
388                                         }
389                                 }
390                                 
391                         } else {
392                                 
393                                 if (Keyboard::modifier_state_equals (s, Keyboard::PrimaryModifier)) {
394                                         /* associate/disassociate things diagonally down and right until we run out */
395                                         PortMatrixNode::State s = (PortMatrixNode::State) 0;
396                                         while (1) {
397                                                 PortMatrixNode const n = position_to_node (x, y);
398                                                 if (n.row.bundle && n.column.bundle) {
399                                                         if (s == (PortMatrixNode::State) 0) {
400                                                                 s = get_association (n);
401                                                         }
402                                                         set_association (n, toggle_state (s));
403                                                 } else {
404                                                         break;
405                                                 }
406                                                 x += grid_spacing ();
407                                                 y += grid_spacing ();
408                                         }
409                                         
410                                 } else {
411                                         
412                                         PortMatrixNode const n = position_to_node (x, y);
413                                         if (n.row.bundle && n.column.bundle) {
414                                                 PortMatrixNode::State const s = get_association (n);
415                                                 set_association (n, toggle_state (s));
416                                         }
417                                 }
418                         }
419
420                         require_render ();
421                 }
422                 
423                 _body->queue_draw ();
424         }
425
426         _dragging = false;
427 }
428
429
430 void
431 PortMatrixGrid::draw_extra (cairo_t* cr)
432 {
433         set_source_rgba (cr, mouseover_line_colour(), 0.3);
434         cairo_set_line_width (cr, mouseover_line_width());
435
436         list<PortMatrixNode> const m = _body->mouseover ();
437
438         for (list<PortMatrixNode>::const_iterator i = m.begin(); i != m.end(); ++i) {
439         
440                 double const x = component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
441                 double const y = component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
442
443                 if (i->row.bundle && i->column.bundle) {
444
445                         cairo_move_to (cr, x, y);
446                         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
447                                 cairo_line_to (cr, component_to_parent_x (0), y);
448                         } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
449                                 cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
450                         }
451                         cairo_stroke (cr);
452                         
453                         cairo_move_to (cr, x, y);
454                         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
455                                 cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
456                         } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
457                                 cairo_line_to (cr, x, component_to_parent_y (0));
458                         }
459                         cairo_stroke (cr);
460                 }
461         }
462
463         if (_dragging && _drag_valid && _moved) {
464
465                 list<PortMatrixNode> const p = nodes_on_line (_drag_start_x, _drag_start_y, _drag_x, _drag_y);
466
467                 if (!p.empty()) {
468
469                         bool const s = toggle_state (get_association (p.front()));
470
471                         for (list<PortMatrixNode>::const_iterator i = p.begin(); i != p.end(); ++i) {
472                                 if (s) {
473                                         draw_association_indicator (
474                                                 cr,
475                                                 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
476                                                 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
477                                                 );
478                                 } else {
479                                         draw_empty_square (
480                                                 cr,
481                                                 component_to_parent_x (channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ()),
482                                                 component_to_parent_y (channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ())
483                                                 );
484                                 }
485                         }
486                 }
487
488                 set_source_rgba (cr, association_colour (), 0.3);
489
490                 cairo_move_to (
491                         cr,
492                         component_to_parent_x (_drag_start_x * grid_spacing() + grid_spacing() / 2),
493                         component_to_parent_y (_drag_start_y * grid_spacing() + grid_spacing() / 2)
494                         );
495
496                 cairo_line_to (
497                         cr,
498                         component_to_parent_x (_drag_x * grid_spacing() + grid_spacing() / 2),
499                         component_to_parent_y (_drag_y * grid_spacing() + grid_spacing() / 2)
500                         );
501
502                 cairo_stroke (cr);
503
504         }
505 }
506
507 void
508 PortMatrixGrid::mouseover_changed (list<PortMatrixNode> const & old)
509 {
510         queue_draw_for (old);
511         queue_draw_for (_body->mouseover());
512 }
513
514 void
515 PortMatrixGrid::motion (double x, double y)
516 {
517         _body->set_mouseover (position_to_node (x, y));
518
519         int const px = x / grid_spacing ();
520         int const py = y / grid_spacing ();
521
522         if (_dragging && !_moved && ( (px != _drag_start_x || py != _drag_start_x) )) {
523                 _moved = true;
524         }
525
526         if (_dragging && _drag_valid && _moved) {
527                 _drag_x = px;
528                 _drag_y = py;
529                 _body->queue_draw ();
530         }
531 }
532
533 void
534 PortMatrixGrid::queue_draw_for (list<PortMatrixNode> const &n)
535 {
536         for (list<PortMatrixNode>::const_iterator i = n.begin(); i != n.end(); ++i) {
537                 
538                 if (i->row.bundle) {
539
540                         double const y = channel_to_position (i->row, _matrix->visible_rows()) * grid_spacing ();
541                         _body->queue_draw_area (
542                                 _parent_rectangle.get_x(),
543                                 component_to_parent_y (y),
544                                 _parent_rectangle.get_width(),
545                                 grid_spacing()
546                                 );
547                 }
548
549                 if (i->column.bundle) {
550
551                         double const x = channel_to_position (i->column, _matrix->visible_columns()) * grid_spacing ();
552                         
553                         _body->queue_draw_area (
554                                 component_to_parent_x (x),
555                                 _parent_rectangle.get_y(),
556                                 grid_spacing(),
557                                 _parent_rectangle.get_height()
558                                 );
559                 }
560         }
561 }
562
563 double
564 PortMatrixGrid::component_to_parent_x (double x) const
565 {
566         return x - _body->xoffset() + _parent_rectangle.get_x();
567 }
568
569 double
570 PortMatrixGrid::parent_to_component_x (double x) const
571 {
572         return x + _body->xoffset() - _parent_rectangle.get_x();
573 }
574
575 double
576 PortMatrixGrid::component_to_parent_y (double y) const
577 {
578         return y - _body->yoffset() + _parent_rectangle.get_y();
579 }
580
581 double
582 PortMatrixGrid::parent_to_component_y (double y) const
583 {
584         return y + _body->yoffset() - _parent_rectangle.get_y();
585 }
586
587 list<PortMatrixNode>
588 PortMatrixGrid::nodes_on_line (int x0, int y0, int x1, int y1) const
589 {
590         list<PortMatrixNode> p;
591
592         bool const steep = abs (y1 - y0) > abs (x1 - x0);
593         if (steep) {
594                 int tmp = x0;
595                 x0 = y0;
596                 y0 = tmp;
597
598                 tmp = y1;
599                 y1 = x1;
600                 x1 = tmp;
601         }
602
603         if (x0 > x1) {
604                 int tmp = x0;
605                 x0 = x1;
606                 x1 = tmp;
607
608                 tmp = y0;
609                 y0 = y1;
610                 y1 = tmp;
611         }
612
613         int dx = x1 - x0;
614         int dy = abs (y1 - y0);
615
616         double err = 0;
617         double derr = double (dy) / dx;
618
619         int y = y0;
620         int const ystep = y0 < y1 ? 1 : -1;
621
622         for (int x = x0; x <= x1; ++x) {
623                 if (steep) {
624                         PortMatrixNode n = position_to_node (y * grid_spacing (), x * grid_spacing ());
625                         if (n.row.bundle && n.column.bundle) {
626                                 p.push_back (n);
627                         }
628                 } else {
629                         PortMatrixNode n = position_to_node (x * grid_spacing (), y * grid_spacing ());
630                         if (n.row.bundle && n.column.bundle) {
631                                 p.push_back (n);
632                         }
633                 }
634
635                 err += derr;
636
637                 if (err >= 0.5) {
638                         y += ystep;
639                         err -= 1;
640                 }
641         }
642
643         return p;
644 }
645
646 bool
647 PortMatrixGrid::toggle_state (PortMatrixNode::State s) const
648 {
649         return (s == PortMatrixNode::NOT_ASSOCIATED || s == PortMatrixNode::PARTIAL);
650 }