Support for the port matrix working at the bundle level and hiding details of ports.
[ardour.git] / gtk2_ardour / port_matrix_column_labels.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 "ardour/bundle.h"
22 #include "ardour/types.h"
23 #include "port_matrix_column_labels.h"
24 #include "port_matrix.h"
25 #include "port_matrix_body.h"
26
27 PortMatrixColumnLabels::PortMatrixColumnLabels (PortMatrix* m, PortMatrixBody* b)
28         : PortMatrixLabels (m, b)
29 {
30
31 }
32
33 void
34 PortMatrixColumnLabels::compute_dimensions ()
35 {
36         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
37         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
38         cairo_t* cr = gdk_cairo_create (pm);
39
40         /* width of the longest bundle name */
41         _longest_bundle_name = 0;
42         /* width of the longest channel name */
43         _longest_channel_name = 0;
44         /* height of highest bit of text (apart from group names) */
45         _highest_text = 0;
46         /* width of the whole thing */
47         _width = 0;
48
49         ARDOUR::BundleList const c = _matrix->columns()->bundles();
50         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
51
52                 cairo_text_extents_t ext;
53                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
54                 if (ext.width > _longest_bundle_name) {
55                         _longest_bundle_name = ext.width;
56                 }
57                 if (ext.height > _highest_text) {
58                         _highest_text = ext.height;
59                 }
60
61                 for (uint32_t j = 0; j < (*i)->nchannels (); ++j) {
62                         
63                         cairo_text_extents (
64                                 cr,
65                                 (*i)->channel_name (j).c_str(),
66                                 &ext
67                                 );
68                         
69                         if (ext.width > _longest_channel_name) {
70                                 _longest_channel_name = ext.width;
71                         }
72                         if (ext.height > _highest_text) {
73                                 _highest_text = ext.height;
74                         }
75                 }
76
77                 if (_matrix->show_only_bundles()) {
78                         _width += column_width();
79                 } else {
80                         _width += (*i)->nchannels() * column_width();
81                 }
82         }
83
84         _highest_group_name = 0;
85         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
86                 if ((*i)->visible()) {
87                         cairo_text_extents_t ext;
88                         cairo_text_extents (cr, (*i)->name.c_str(), &ext);
89                         if (ext.height > _highest_group_name) {
90                                 _highest_group_name = ext.height;
91                         }
92                 }
93         }
94
95         cairo_destroy (cr);
96         gdk_pixmap_unref (pm);
97
98         /* height of the whole thing */
99
100         int a = _longest_bundle_name + 4 * name_pad();
101         if (!_matrix->show_only_bundles()) {
102                 a += _longest_channel_name;
103         }
104
105         double const parallelogram_height =  a * sin (angle()) + _highest_text * cos (angle());
106
107         _height = parallelogram_height + _highest_group_name + 2 * name_pad();
108
109         _width += parallelogram_height / tan (angle ());
110 }
111
112 double
113 PortMatrixColumnLabels::basic_text_x_pos (int c) const
114 {
115         return column_width() / 2 +
116                 _highest_text / (2 * sin (angle ()));
117 }
118
119 void
120 PortMatrixColumnLabels::render (cairo_t* cr)
121 {
122         /* BACKGROUND */
123
124         set_source_rgb (cr, background_colour());
125         cairo_rectangle (cr, 0, 0, _width, _height);
126         cairo_fill (cr);
127
128         /* PORT GROUP NAME */
129         
130         double x = 0;
131         double y = 0;
132         
133         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
134                 x = slanted_height() / tan (angle());
135                 y = _highest_group_name + name_pad();
136         } else {
137                 x = 0;
138                 y = _height - name_pad();
139         }
140
141         int g = 0;
142         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
143
144                 if (!(*i)->visible() || (*i)->bundles().empty()) {
145                         continue;
146                 }
147
148                 /* compute width of this group */
149                 uint32_t w = 0;
150                 if (_matrix->show_only_bundles()) {
151                         w = (*i)->bundles().size() * column_width();
152                 } else {
153                         w = (*i)->total_channels() * column_width();
154                 }
155
156                 /* rectangle */
157                 set_source_rgb (cr, get_a_group_colour (g));
158                 double const rh = _highest_group_name + 2 * name_pad();
159                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
160                         cairo_rectangle (cr, x, 0, w, rh);
161                 } else {
162                         cairo_rectangle (cr, x, _height - rh, w, rh);
163                 }
164                 cairo_fill (cr);
165                 
166                 std::pair<std::string, double> const display = display_port_name (cr, (*i)->name, w);
167
168                 /* plot it */
169                 set_source_rgb (cr, text_colour());
170                 cairo_move_to (cr, x + (w - display.second) / 2, y);
171                 cairo_show_text (cr, display.first.c_str());
172
173                 x += w;
174                 ++g;
175         }
176
177         /* BUNDLE PARALLELOGRAM-TYPE-THING AND NAME */
178
179         x = 0;
180         ARDOUR::BundleList const c = _matrix->columns()->bundles();
181         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
182
183                 render_bundle_name (cr, get_a_bundle_colour (i - c.begin ()), x, 0, *i);
184
185                 if (_matrix->show_only_bundles()) {
186                         x += column_width();
187                 } else {
188                         x += (*i)->nchannels () * column_width();
189                 }
190         }
191         
192
193         /* PORT NAMES */
194
195         if (!_matrix->show_only_bundles()) {
196                 x = 0;
197                 for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
198                         
199                         for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
200                                 
201                                 render_channel_name (cr, get_a_bundle_colour (i - c.begin()), x, 0, ARDOUR::BundleChannel (*i, j));
202                                 x += column_width();
203                         }
204                 }
205         }
206 }
207
208 double
209 PortMatrixColumnLabels::component_to_parent_x (double x) const
210 {
211         return x - _body->xoffset() + _parent_rectangle.get_x();
212 }
213
214 double
215 PortMatrixColumnLabels::parent_to_component_x (double x) const
216 {
217         return x + _body->xoffset() - _parent_rectangle.get_x();
218 }
219
220 double
221 PortMatrixColumnLabels::component_to_parent_y (double y) const
222 {
223         return y + _parent_rectangle.get_y();
224 }
225
226 double
227 PortMatrixColumnLabels::parent_to_component_y (double y) const
228 {
229         return y - _parent_rectangle.get_y();
230 }
231
232 void
233 PortMatrixColumnLabels::mouseover_changed (PortMatrixNode const &)
234 {
235         clear_channel_highlights ();
236         if (_body->mouseover().column.bundle) {
237                 add_channel_highlight (_body->mouseover().column);
238         }
239 }
240
241 std::vector<std::pair<double, double> >
242 PortMatrixColumnLabels::port_name_shape (double xoff, double yoff) const
243 {
244         std::vector<std::pair<double, double> > shape;
245         
246         double const lc = _longest_channel_name + name_pad();
247         double const w = column_width();
248         
249         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
250
251                 double x_ = xoff + slanted_height() / tan (angle()) + w;
252                 double y_ = yoff;
253                 shape.push_back (std::make_pair (x_, y_));
254                 x_ -= w;
255                 shape.push_back (std::make_pair (x_, y_));
256                 x_ -= lc * cos (angle());
257                 y_ += lc * sin (angle());
258                 shape.push_back (std::make_pair (x_, y_));
259                 x_ += w * pow (sin (angle()), 2);
260                 y_ += w * sin (angle()) * cos (angle());
261                 shape.push_back (std::make_pair (x_, y_));
262                 
263         } else {
264                 
265                 double x_ = xoff;
266                 double y_ = yoff + _height;
267                 shape.push_back (std::make_pair (x_, y_));
268                 x_ += w;
269                 shape.push_back (std::make_pair (x_, y_));
270                 x_ += lc * cos (angle());
271                 y_ -= lc * sin (angle());
272                 shape.push_back (std::make_pair (x_, y_));
273                 x_ -= column_width() * pow (sin (angle()), 2);
274                 y_ -= column_width() * sin (angle()) * cos (angle());
275                 shape.push_back (std::make_pair (x_, y_));
276         }
277
278         return shape;
279 }
280
281 void
282 PortMatrixColumnLabels::render_bundle_name (
283         cairo_t* cr, Gdk::Color colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
284         )
285 {
286         set_source_rgb (cr, colour);
287
288         double w = 0;
289         if (_matrix->show_only_bundles()) {
290                 w = column_width ();
291         } else {
292                 w = b->nchannels() * column_width();
293         }
294         
295         double x_ = xoff;
296
297         uint32_t y = yoff;
298         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
299                 y += _height;
300         } else {
301                 y += slanted_height();
302         }
303
304         double y_ = y;
305         cairo_move_to (cr, x_, y_);
306         x_ += w;
307         cairo_line_to (cr, x_, y_);
308         x_ += slanted_height() / tan (angle ());
309         y_ -= slanted_height();
310         cairo_line_to (cr, x_, y_);
311         x_ -= w;
312         cairo_line_to (cr, x_, y_);
313         cairo_line_to (cr, xoff, y);
314         cairo_fill_preserve (cr);
315         set_source_rgb (cr, background_colour());
316         cairo_set_line_width (cr, label_border_width());
317         cairo_stroke (cr);
318
319         set_source_rgb (cr, text_colour());
320         
321         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
322
323                 double rl = 0;
324                 if (_matrix->show_only_bundles()) {
325                         rl = name_pad();
326                 } else {
327                         rl = 3 * name_pad() + _longest_channel_name;
328                 }
329                 cairo_move_to (
330                         cr,
331                         xoff + basic_text_x_pos (0) + rl * cos (angle()),
332                         yoff + _height - rl * sin (angle())
333                         );
334                 
335         } else {
336                 
337                 cairo_move_to (
338                         cr,
339                         xoff + basic_text_x_pos (0),
340                         yoff + slanted_height() - name_pad() * sin (angle())
341                         );
342         }
343         
344         cairo_save (cr);
345         cairo_rotate (cr, -angle());
346         cairo_show_text (cr, b->name().c_str());
347         cairo_restore (cr);
348 }
349
350 void
351 PortMatrixColumnLabels::render_channel_name (
352         cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const &bc
353         )
354 {
355         std::vector<std::pair<double, double> > const shape = port_name_shape (xoff, yoff);
356
357         cairo_move_to (cr, shape[0].first, shape[0].second);
358         for (uint32_t i = 1; i < 4; ++i) {
359                 cairo_line_to (cr, shape[i].first, shape[i].second);
360         }
361         cairo_line_to (cr, shape[0].first, shape[0].second);
362         
363         set_source_rgb (cr, colour);
364         cairo_fill_preserve (cr);
365         set_source_rgb (cr, background_colour());
366         cairo_set_line_width (cr, label_border_width());
367         cairo_stroke (cr);
368         
369         set_source_rgb (cr, text_colour());
370         
371         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
372
373                 cairo_move_to (
374                         cr,
375                         xoff + basic_text_x_pos(bc.channel),
376                         yoff + _height - name_pad() * sin (angle())
377                         );
378                 
379         } else { 
380
381                 double const rl = 3 * name_pad() + _longest_bundle_name;
382                 cairo_move_to (
383                         cr,
384                         xoff + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
385                         yoff + slanted_height() - rl * sin (angle())
386                         );
387         }
388         
389         cairo_save (cr);
390         cairo_rotate (cr, -angle());
391         
392         cairo_show_text (
393                 cr,
394                 bc.bundle->channel_name(bc.channel).c_str()
395                 );
396         
397         cairo_restore (cr);
398 }
399
400 double
401 PortMatrixColumnLabels::channel_x (ARDOUR::BundleChannel const &bc) const
402 {
403         uint32_t n = 0;
404
405         ARDOUR::BundleList::const_iterator i = _matrix->columns()->bundles().begin();
406         while (i != _matrix->columns()->bundles().end() && *i != bc.bundle) {
407                 if (_matrix->show_only_bundles()) {
408                         n += 1;
409                 } else {
410                         n += (*i)->nchannels ();
411                 }
412                 ++i;
413         }
414
415         if (!_matrix->show_only_bundles()) {
416                 n += bc.channel;
417         }
418         
419         return n * column_width();
420 }
421
422 double
423 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &bc) const
424 {
425         return 0;
426 }
427
428 void
429 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
430 {
431         if (!bc.bundle) {
432                 return;
433         }
434
435         if (_matrix->show_only_bundles()) {
436
437                 _body->queue_draw_area (
438                         component_to_parent_x (channel_x (bc)),
439                         component_to_parent_y (0),
440                         column_width() + _height * tan (angle()),
441                         _height
442                         );
443                 
444         } else {
445                 
446                 double const x = channel_x (bc);
447                 double const lc = _longest_channel_name + name_pad();
448                 double const h = lc * sin (angle ()) + column_width() * sin (angle()) * cos (angle());
449                 
450                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
451
452                         _body->queue_draw_area (
453                                 component_to_parent_x (x),
454                                 component_to_parent_y (_height - h),
455                                 column_width() + lc * cos (angle()),
456                                 h
457                                 );
458                         
459                 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
460                         
461                         double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
462                         
463                         _body->queue_draw_area (
464                                 component_to_parent_x (x_),
465                                 component_to_parent_y (0),
466                                 column_width() + lc * cos (angle()),
467                                 h
468                                 );
469                         
470                 }
471                 
472         }
473 }
474
475 void
476 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
477 {
478         uint32_t N = _matrix->columns()->total_visible_channels ();
479         uint32_t i = 0;
480         for (; i < N; ++i) {
481                 
482                 std::vector<std::pair<double, double> > const shape = port_name_shape (i * column_width(), 0);
483
484                 uint32_t j = 0;
485                 for (; j < 4; ++j) {
486                         uint32_t k = (j + 1) % 4;
487
488                         double const P = (y - shape[j].second) * (shape[k].first - shape[j].first) -
489                                 (x - shape[j].first) * (shape[k].second - shape[j].second);
490
491                         if (P > 0) {
492                                 break;
493                         }
494                 }
495
496                 if (j == 4) {
497                         break;
498                 }
499         }
500
501         if (i == N) {
502                 return;
503         }
504         
505         switch (b) {
506         case 1:
507                 _body->highlight_associated_channels (_matrix->column_index(), i);
508                 break;
509         case 3:
510                 _matrix->popup_channel_context_menu (_matrix->column_index(), i, t);
511                 break;
512         }
513 }
514