More port matrix re-working. Global matrix now has separate visibility buttons
[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
27 PortMatrixGrid::PortMatrixGrid (PortMatrix* m, PortMatrixBody* b)
28         : PortMatrixComponent (m, b)
29 {
30         
31 }
32
33 void
34 PortMatrixGrid::compute_dimensions ()
35 {
36         _width = 0;
37         ARDOUR::BundleList const c = _matrix->columns()->bundles();
38         for (ARDOUR::BundleList::const_iterator i = c.begin(); i != c.end(); ++i) {
39                 _width += (*i)->nchannels() * column_width();
40         }
41
42         _height = 0;
43         ARDOUR::BundleList const r = _matrix->rows()->bundles();
44         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
45                 _height += (*i)->nchannels() * row_height();
46         }
47 }
48
49
50 void
51 PortMatrixGrid::render (cairo_t* cr)
52 {
53         /* BACKGROUND */
54         
55         set_source_rgb (cr, background_colour());
56         cairo_rectangle (cr, 0, 0, _width, _height);
57         cairo_fill (cr);
58
59         /* VERTICAL GRID LINES */
60         
61         set_source_rgb (cr, grid_colour());
62         uint32_t x = 0;
63         ARDOUR::BundleList const c = _matrix->columns()->bundles();
64         for (ARDOUR::BundleList::size_type i = 0; i < c.size(); ++i) {
65
66                 cairo_set_line_width (cr, thin_grid_line_width());
67                 for (uint32_t j = 1; j < c[i]->nchannels(); ++j) {
68                         x += column_width();
69                         cairo_move_to (cr, x, 0);
70                         cairo_line_to (cr, x, _height);
71                         cairo_stroke (cr);
72                 }
73
74                 if (i < (c.size() - 1)) {
75                         x += column_width();
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         }
82                 
83         uint32_t grid_width = x + column_width();
84
85         /* HORIZONTAL GRID LINES */
86         
87         uint32_t y = 0;
88         ARDOUR::BundleList const r = _matrix->rows()->bundles();
89         for (ARDOUR::BundleList::size_type i = 0; i < r.size(); ++i) {
90
91                 cairo_set_line_width (cr, thin_grid_line_width());
92                 for (uint32_t j = 1; j < r[i]->nchannels(); ++j) {
93                         y += row_height();
94                         cairo_move_to (cr, 0, y);
95                         cairo_line_to (cr, grid_width, y);
96                         cairo_stroke (cr);
97                 }
98
99                 if (i < (r.size() - 1)) {
100                         y += row_height();
101                         cairo_set_line_width (cr, thick_grid_line_width());
102                         cairo_move_to (cr, 0, y);
103                         cairo_line_to (cr, grid_width, y);
104                         cairo_stroke (cr);
105                 }
106         }
107
108         /* ASSOCIATION INDICATORS */
109         
110         uint32_t bx = 0;
111         uint32_t by = 0;
112         
113         for (ARDOUR::BundleList::const_iterator i = c.begin(); i < c.end(); ++i) {
114                 by = 0;
115                 
116                 for (ARDOUR::BundleList::const_iterator j = r.begin(); j < r.end(); ++j) {
117
118                         x = bx;
119                         for (uint32_t k = 0; k < (*i)->nchannels (); k++) {
120
121                                 y = by;
122                                 for (uint32_t l = 0; l < (*j)->nchannels (); ++l) {
123
124                                         ARDOUR::BundleChannel c[2];
125                                         c[_matrix->column_index()] = ARDOUR::BundleChannel (*i, k);
126                                         c[_matrix->row_index()] = ARDOUR::BundleChannel (*j, l);
127                                         
128                                         PortMatrix::State const s = _matrix->get_state (c);
129                                                 
130                                         switch (s) {
131                                         case PortMatrix::ASSOCIATED:
132                                                 set_source_rgba (cr, association_colour(), 0.5);
133                                                 cairo_arc (
134                                                         cr,
135                                                         x + column_width() / 2,
136                                                         y + column_width() / 2,
137                                                         (column_width() - (2 * connection_indicator_pad())) / 2,
138                                                         0,
139                                                         2 * M_PI
140                                                         );
141
142                                                 cairo_fill (cr);
143                                                 break;
144
145                                         case PortMatrix::UNKNOWN:
146                                                 set_source_rgba (cr, unknown_colour(), 0.5);
147                                                 cairo_rectangle (
148                                                         cr,
149                                                         x + thick_grid_line_width(),
150                                                         y + thick_grid_line_width(),
151                                                         column_width() - 2 * thick_grid_line_width(),
152                                                         row_height() - 2 * thick_grid_line_width()
153                                                         );
154                                                 cairo_fill (cr);
155                                                 break;
156                                         
157                                         case PortMatrix::NOT_ASSOCIATED:
158                                                 break;
159                                         }
160
161                                         y += row_height();
162                                 }
163                                 x += column_width();
164                         }
165                         
166                         by += (*j)->nchannels () * row_height();
167                 }
168                 
169                 bx += (*i)->nchannels () * column_width();
170         }
171 }
172
173
174 PortMatrixNode
175 PortMatrixGrid::position_to_node (double x, double y) const
176 {
177         return PortMatrixNode (
178                 position_to_channel (y, _matrix->rows()->bundles(), row_height()),
179                 position_to_channel (x, _matrix->columns()->bundles(), column_width())
180                 );
181 }
182
183
184 ARDOUR::BundleChannel
185 PortMatrixGrid::position_to_channel (double p, ARDOUR::BundleList const& bundles, double inc) const
186 {
187         uint32_t pos = p / inc;
188
189         for (ARDOUR::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
190                 if (pos < (*i)->nchannels()) {
191                         return ARDOUR::BundleChannel (*i, pos);
192                 } else {
193                         pos -= (*i)->nchannels();
194                 }
195         }
196                         
197         return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0);
198 }
199
200
201 double
202 PortMatrixGrid::channel_position (
203         ARDOUR::BundleChannel bc,
204         ARDOUR::BundleList const& bundles,
205         double inc) const
206 {
207         double p = 0;
208         
209         ARDOUR::BundleList::const_iterator i = bundles.begin ();
210         while (i != bundles.end() && *i != bc.bundle) {
211                 p += inc * (*i)->nchannels();
212                 ++i;
213         }
214
215         if (i == bundles.end()) {
216                 return 0;
217         }
218
219         p += inc * bc.channel;
220
221         return p;
222 }
223
224 void
225 PortMatrixGrid::button_press (double x, double y, int b)
226 {
227         PortMatrixNode const node = position_to_node (x, y);
228         
229         if (node.row.bundle && node.column.bundle) {
230
231                 ARDOUR::BundleChannel c[2];
232                 c[_matrix->row_index()] = node.row;
233                 c[_matrix->column_index()] = node.column;
234                 
235                 PortMatrix::State const s = _matrix->get_state (c);
236                 
237                 if (s == PortMatrix::ASSOCIATED || s == PortMatrix::NOT_ASSOCIATED) {
238
239                         bool const n = !(s == PortMatrix::ASSOCIATED);
240
241                         ARDOUR::BundleChannel c[2];
242                         c[_matrix->row_index()] = node.row;
243                         c[_matrix->column_index()] = node.column;
244                         
245                         _matrix->set_state (c, n);
246                 }
247
248                 require_render ();
249                 _body->queue_draw ();
250         }
251 }
252
253 void
254 PortMatrixGrid::draw_extra (cairo_t* cr)
255 {
256         set_source_rgba (cr, mouseover_line_colour(), 0.3);
257         cairo_set_line_width (cr, mouseover_line_width());
258
259         double const x = component_to_parent_x (
260                 channel_position (_body->mouseover().column, _matrix->columns()->bundles(), column_width()) + column_width() / 2
261                 );
262         
263         double const y = component_to_parent_y (
264                 channel_position (_body->mouseover().row, _matrix->rows()->bundles(), row_height()) + row_height() / 2
265                 );
266         
267         if (_body->mouseover().row.bundle) {
268
269                 cairo_move_to (cr, x, y);
270                 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
271                         cairo_line_to (cr, component_to_parent_x (0), y);
272                 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
273                         cairo_line_to (cr, _parent_rectangle.get_x() + _parent_rectangle.get_width(), y);
274                 }
275                 cairo_stroke (cr);
276         }
277
278         if (_body->mouseover().column.bundle) {
279
280                 cairo_move_to (cr, x, y);
281                 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
282                         cairo_line_to (cr, x, _parent_rectangle.get_y() + _parent_rectangle.get_height());
283                 } else if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
284                         cairo_line_to (cr, x, component_to_parent_y (0));
285                 }
286                 cairo_stroke (cr);
287         }
288 }
289
290 void
291 PortMatrixGrid::mouseover_changed (PortMatrixNode const& old)
292 {
293         queue_draw_for (old);
294         queue_draw_for (_body->mouseover());
295 }
296
297 void
298 PortMatrixGrid::mouseover_event (double x, double y)
299 {
300         _body->set_mouseover (position_to_node (x, y));
301 }
302
303 void
304 PortMatrixGrid::queue_draw_for (PortMatrixNode const &n)
305 {
306         if (n.row.bundle) {
307
308                 double const y = channel_position (n.row, _matrix->rows()->bundles(), row_height());
309                 _body->queue_draw_area (
310                         _parent_rectangle.get_x(),
311                         component_to_parent_y (y),
312                         _parent_rectangle.get_width(),
313                         row_height()
314                         );
315         }
316
317         if (n.column.bundle) {
318
319                 double const x = channel_position (n.column, _matrix->columns()->bundles(), column_width());
320                 
321                 _body->queue_draw_area (
322                         component_to_parent_x (x),
323                         _parent_rectangle.get_y(),
324                         column_width(),
325                         _parent_rectangle.get_height()
326                         );
327         }
328 }
329
330 double
331 PortMatrixGrid::component_to_parent_x (double x) const
332 {
333         return x - _body->xoffset() + _parent_rectangle.get_x();
334 }
335
336 double
337 PortMatrixGrid::parent_to_component_x (double x) const
338 {
339         return x + _body->xoffset() - _parent_rectangle.get_x();
340 }
341
342 double
343 PortMatrixGrid::component_to_parent_y (double y) const
344 {
345         return y - _body->yoffset() + _parent_rectangle.get_y();
346 }
347
348 double
349 PortMatrixGrid::parent_to_component_y (double y) const
350 {
351         return y + _body->yoffset() - _parent_rectangle.get_y();
352 }
353