2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
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"
30 using Gtkmm2ext::Keyboard
;
32 PortMatrixGrid::PortMatrixGrid (PortMatrix
* m
, PortMatrixBody
* b
)
33 : PortMatrixComponent (m
, b
),
42 PortMatrixGrid::compute_dimensions ()
44 if (_matrix
->visible_columns()) {
45 _width
= group_size (_matrix
->visible_columns()) * grid_spacing ();
50 if (_matrix
->visible_rows()) {
51 _height
= group_size (_matrix
->visible_rows()) * grid_spacing ();
59 PortMatrixGrid::render (cairo_t
* cr
)
61 set_source_rgb (cr
, background_colour());
62 cairo_rectangle (cr
, 0, 0, _width
, _height
);
65 PortGroup::BundleList
const & row_bundles
= _matrix
->visible_rows()->bundles();
66 PortGroup::BundleList
const & column_bundles
= _matrix
->visible_columns()->bundles();
70 /* VERTICAL GRID LINES */
72 set_source_rgb (cr
, grid_colour());
75 for (PortGroup::BundleList::const_iterator i
= column_bundles
.begin(); i
!= column_bundles
.end(); ++i
) {
77 cairo_set_line_width (cr
, thick_grid_line_width());
78 cairo_move_to (cr
, x
, 0);
79 cairo_line_to (cr
, x
, _height
);
82 if (!_matrix
->show_only_bundles()) {
83 cairo_set_line_width (cr
, thin_grid_line_width());
84 for (uint32_t j
= 0; j
< _matrix
->count_of_our_type ((*i
)->bundle
->nchannels()); ++j
) {
86 cairo_move_to (cr
, x
, 0);
87 cairo_line_to (cr
, x
, _height
);
102 /* HORIZONTAL GRID LINES */
105 for (PortGroup::BundleList::const_iterator i
= row_bundles
.begin(); i
!= row_bundles
.end(); ++i
) {
107 cairo_set_line_width (cr
, thick_grid_line_width());
108 cairo_move_to (cr
, 0, y
);
109 cairo_line_to (cr
, _width
, y
);
112 if (!_matrix
->show_only_bundles()) {
113 cairo_set_line_width (cr
, thin_grid_line_width());
114 for (uint32_t j
= 0; j
< _matrix
->count_of_our_type ((*i
)->bundle
->nchannels()); ++j
) {
115 y
+= grid_spacing ();
116 cairo_move_to (cr
, 0, y
);
117 cairo_line_to (cr
, _width
, y
);
123 y
+= grid_spacing ();
130 /* ASSOCIATION INDICATORS and NON-CONNECTABLE INDICATORS */
132 /* we draw a grey square in a matrix box if the two ports that intersect at that box
133 cannot be connected because they are of different types (MIDI vs. audio)
139 if (_matrix
->show_only_bundles()) {
141 for (PortGroup::BundleList::const_iterator i
= column_bundles
.begin(); i
!= column_bundles
.end(); ++i
) {
144 for (PortGroup::BundleList::const_iterator j
= row_bundles
.begin(); j
!= row_bundles
.end(); ++j
) {
146 PortMatrixNode::State s
= _matrix
->get_association (PortMatrixNode (
147 ARDOUR::BundleChannel ((*j
)->bundle
, 0),
148 ARDOUR::BundleChannel ((*i
)->bundle
, 0)
151 case PortMatrixNode::ASSOCIATED
:
152 draw_association_indicator (cr
, bx
, by
);
154 case PortMatrixNode::PARTIAL
:
155 draw_association_indicator (cr
, bx
, by
, 0.5);
161 by
+= grid_spacing();
164 bx
+= grid_spacing();
170 for (PortGroup::BundleList::const_iterator i
= column_bundles
.begin(); i
!= column_bundles
.end(); ++i
) {
173 for (PortGroup::BundleList::const_iterator j
= row_bundles
.begin(); j
!= row_bundles
.end(); ++j
) {
176 for (uint32_t k
= 0; k
< (*i
)->bundle
->nchannels().n_total(); ++k
) {
179 for (uint32_t l
= 0; l
< (*j
)->bundle
->nchannels().n_total(); ++l
) {
181 if (!_matrix
->should_show ((*i
)->bundle
->channel_type(k
)) || !_matrix
->should_show ((*j
)->bundle
->channel_type(l
))) {
185 ARDOUR::BundleChannel c
[2];
186 c
[_matrix
->column_index()] = ARDOUR::BundleChannel ((*i
)->bundle
, k
);
187 c
[_matrix
->row_index()] = ARDOUR::BundleChannel ((*j
)->bundle
, l
);
189 if (c
[0].bundle
->channel_type (c
[0].channel
) != c
[1].bundle
->channel_type (c
[1].channel
)) {
190 /* these two channels are of different types */
191 draw_non_connectable_indicator (cr
, x
, y
);
193 /* these two channels might be associated */
194 PortMatrixNode::State
const s
= _matrix
->get_state (c
);
197 case PortMatrixNode::ASSOCIATED
:
198 draw_association_indicator (cr
, x
, y
);
201 case PortMatrixNode::NOT_ASSOCIATED
:
215 by
+= _matrix
->count_of_our_type ((*j
)->bundle
->nchannels()) * grid_spacing();
218 bx
+= _matrix
->count_of_our_type ((*i
)->bundle
->nchannels()) * grid_spacing();
224 PortMatrixGrid::draw_association_indicator (cairo_t
* cr
, uint32_t x
, uint32_t y
, double p
)
226 set_source_rgba (cr
, association_colour(), 0.5);
230 x
+ grid_spacing() / 2,
231 y
+ grid_spacing() / 2,
232 (grid_spacing() - (2 * connection_indicator_pad())) / 2,
241 PortMatrixGrid::draw_empty_square (cairo_t
* cr
, uint32_t x
, uint32_t y
)
243 set_source_rgb (cr
, background_colour());
246 x
+ thick_grid_line_width(),
247 y
+ thick_grid_line_width(),
248 grid_spacing() - 2 * thick_grid_line_width(),
249 grid_spacing() - 2 * thick_grid_line_width()
254 /** Draw a square to indicate that two channels in a matrix cannot be associated
258 PortMatrixGrid::draw_non_connectable_indicator (cairo_t
* cr
, uint32_t x
, uint32_t y
)
260 set_source_rgb (cr
, non_connectable_colour ());
263 x
+ thick_grid_line_width(),
264 y
+ thick_grid_line_width(),
265 grid_spacing() - 2 * thick_grid_line_width(),
266 grid_spacing() - 2 * thick_grid_line_width()
272 PortMatrixGrid::position_to_node (double x
, double y
) const
274 return PortMatrixNode (
275 position_to_channel (y
, x
, _matrix
->visible_rows()),
276 position_to_channel (x
, y
, _matrix
->visible_columns())
281 PortMatrixGrid::button_press (double x
, double y
, int b
, uint32_t t
, guint
)
283 ARDOUR::BundleChannel
const px
= position_to_channel (x
, y
, _matrix
->visible_columns());
284 ARDOUR::BundleChannel
const py
= position_to_channel (y
, x
, _matrix
->visible_rows());
289 _drag_valid
= (px
.bundle
&& py
.bundle
);
292 _drag_start_x
= x
/ grid_spacing ();
293 _drag_start_y
= y
/ grid_spacing ();
297 _matrix
->popup_menu (px
, py
, t
);
303 PortMatrixGrid::set_association (PortMatrixNode node
, bool s
)
305 if (_matrix
->show_only_bundles()) {
307 for (uint32_t i
= 0; i
< node
.column
.bundle
->nchannels().n_total(); ++i
) {
308 for (uint32_t j
= 0; j
< node
.row
.bundle
->nchannels().n_total(); ++j
) {
310 if (!_matrix
->should_show (node
.column
.bundle
->channel_type(i
)) || !_matrix
->should_show (node
.row
.bundle
->channel_type(j
))) {
314 ARDOUR::BundleChannel c
[2];
315 c
[_matrix
->column_index()] = ARDOUR::BundleChannel (node
.column
.bundle
, i
);
316 c
[_matrix
->row_index()] = ARDOUR::BundleChannel (node
.row
.bundle
, j
);
317 _matrix
->set_state (c
, s
&& (i
== j
));
323 if (node
.row
.bundle
&& node
.column
.bundle
) {
325 ARDOUR::BundleChannel c
[2];
326 c
[_matrix
->row_index()] = node
.row
;
327 c
[_matrix
->column_index()] = node
.column
;
328 _matrix
->set_state (c
, s
);
334 PortMatrixGrid::button_release (double x
, double y
, int b
, uint32_t /*t*/, guint s
)
340 if (_dragging
&& _moved
) {
343 list
<PortMatrixNode
> const p
= nodes_on_line (_drag_start_x
, _drag_start_y
, _drag_x
, _drag_y
);
346 PortMatrixNode::State
const s
= _matrix
->get_association (p
.front());
347 for (list
<PortMatrixNode
>::const_iterator i
= p
.begin(); i
!= p
.end(); ++i
) {
348 set_association (*i
, toggle_state (s
));
355 if (Keyboard::modifier_state_equals (s
, Keyboard::PrimaryModifier
)) {
356 /* associate/disassociate things diagonally down and right until we run out */
357 PortMatrixNode::State s
= (PortMatrixNode::State
) 0;
359 PortMatrixNode
const n
= position_to_node (x
, y
);
360 if (n
.row
.bundle
&& n
.column
.bundle
) {
361 if (s
== (PortMatrixNode::State
) 0) {
362 s
= _matrix
->get_association (n
);
364 set_association (n
, toggle_state (s
));
368 x
+= grid_spacing ();
369 y
+= grid_spacing ();
374 PortMatrixNode
const n
= position_to_node (x
, y
);
375 if (n
.row
.bundle
&& n
.column
.bundle
) {
376 PortMatrixNode::State
const s
= _matrix
->get_association (n
);
377 set_association (n
, toggle_state (s
));
385 _body
->queue_draw ();
393 PortMatrixGrid::draw_extra (cairo_t
* cr
)
395 set_source_rgba (cr
, mouseover_line_colour(), 0.3);
396 cairo_set_line_width (cr
, mouseover_line_width());
398 list
<PortMatrixNode
> const m
= _body
->mouseover ();
400 for (list
<PortMatrixNode
>::const_iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
402 double const x
= component_to_parent_x (channel_to_position (i
->column
, _matrix
->visible_columns()) * grid_spacing()) + grid_spacing() / 2;
403 double const y
= component_to_parent_y (channel_to_position (i
->row
, _matrix
->visible_rows()) * grid_spacing()) + grid_spacing() / 2;
405 if (i
->row
.bundle
&& i
->column
.bundle
) {
407 cairo_move_to (cr
, x
, y
);
408 if (_matrix
->arrangement() == PortMatrix::LEFT_TO_BOTTOM
) {
409 cairo_line_to (cr
, component_to_parent_x (0), y
);
410 } else if (_matrix
->arrangement() == PortMatrix::TOP_TO_RIGHT
) {
411 cairo_line_to (cr
, _parent_rectangle
.get_x() + _parent_rectangle
.get_width(), y
);
415 cairo_move_to (cr
, x
, y
);
416 if (_matrix
->arrangement() == PortMatrix::LEFT_TO_BOTTOM
) {
417 cairo_line_to (cr
, x
, _parent_rectangle
.get_y() + _parent_rectangle
.get_height());
418 } else if (_matrix
->arrangement() == PortMatrix::TOP_TO_RIGHT
) {
419 cairo_line_to (cr
, x
, component_to_parent_y (0));
425 if (_dragging
&& _drag_valid
&& _moved
) {
427 list
<PortMatrixNode
> const p
= nodes_on_line (_drag_start_x
, _drag_start_y
, _drag_x
, _drag_y
);
431 bool const s
= toggle_state (_matrix
->get_association (p
.front()));
433 for (list
<PortMatrixNode
>::const_iterator i
= p
.begin(); i
!= p
.end(); ++i
) {
435 draw_association_indicator (
437 component_to_parent_x (channel_to_position (i
->column
, _matrix
->visible_columns()) * grid_spacing ()),
438 component_to_parent_y (channel_to_position (i
->row
, _matrix
->visible_rows()) * grid_spacing ())
443 component_to_parent_x (channel_to_position (i
->column
, _matrix
->visible_columns()) * grid_spacing ()),
444 component_to_parent_y (channel_to_position (i
->row
, _matrix
->visible_rows()) * grid_spacing ())
450 set_source_rgba (cr
, association_colour (), 0.3);
454 component_to_parent_x (_drag_start_x
* grid_spacing() + grid_spacing() / 2),
455 component_to_parent_y (_drag_start_y
* grid_spacing() + grid_spacing() / 2)
460 component_to_parent_x (_drag_x
* grid_spacing() + grid_spacing() / 2),
461 component_to_parent_y (_drag_y
* grid_spacing() + grid_spacing() / 2)
470 PortMatrixGrid::mouseover_changed (list
<PortMatrixNode
> const & old
)
472 queue_draw_for (old
);
473 queue_draw_for (_body
->mouseover());
477 PortMatrixGrid::motion (double x
, double y
)
479 _body
->set_mouseover (position_to_node (x
, y
));
481 int const px
= x
/ grid_spacing ();
482 int const py
= y
/ grid_spacing ();
484 if (_dragging
&& !_moved
&& ( (px
!= _drag_start_x
|| py
!= _drag_start_x
) )) {
488 if (_dragging
&& _drag_valid
&& _moved
) {
491 _body
->queue_draw ();
496 PortMatrixGrid::queue_draw_for (list
<PortMatrixNode
> const &n
)
498 for (list
<PortMatrixNode
>::const_iterator i
= n
.begin(); i
!= n
.end(); ++i
) {
502 double const y
= channel_to_position (i
->row
, _matrix
->visible_rows()) * grid_spacing ();
503 _body
->queue_draw_area (
504 _parent_rectangle
.get_x(),
505 component_to_parent_y (y
),
506 _parent_rectangle
.get_width(),
511 if (i
->column
.bundle
) {
513 double const x
= channel_to_position (i
->column
, _matrix
->visible_columns()) * grid_spacing ();
515 _body
->queue_draw_area (
516 component_to_parent_x (x
),
517 _parent_rectangle
.get_y(),
519 _parent_rectangle
.get_height()
526 PortMatrixGrid::component_to_parent_x (double x
) const
528 return x
- _body
->xoffset() + _parent_rectangle
.get_x();
532 PortMatrixGrid::parent_to_component_x (double x
) const
534 return x
+ _body
->xoffset() - _parent_rectangle
.get_x();
538 PortMatrixGrid::component_to_parent_y (double y
) const
540 return y
- _body
->yoffset() + _parent_rectangle
.get_y();
544 PortMatrixGrid::parent_to_component_y (double y
) const
546 return y
+ _body
->yoffset() - _parent_rectangle
.get_y();
550 PortMatrixGrid::nodes_on_line (int x0
, int y0
, int x1
, int y1
) const
552 list
<PortMatrixNode
> p
;
554 bool const steep
= abs (y1
- y0
) > abs (x1
- x0
);
576 int dy
= abs (y1
- y0
);
579 double derr
= double (dy
) / dx
;
582 int const ystep
= y0
< y1
? 1 : -1;
584 for (int x
= x0
; x
<= x1
; ++x
) {
586 PortMatrixNode n
= position_to_node (y
* grid_spacing (), x
* grid_spacing ());
587 if (n
.row
.bundle
&& n
.column
.bundle
) {
591 PortMatrixNode n
= position_to_node (x
* grid_spacing (), y
* grid_spacing ());
592 if (n
.row
.bundle
&& n
.column
.bundle
) {
609 PortMatrixGrid::toggle_state (PortMatrixNode::State s
) const
611 return (s
== PortMatrixNode::NOT_ASSOCIATED
|| s
== PortMatrixNode::PARTIAL
);