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