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