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