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