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