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