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