Drag a line in the port matrix to connect a group of things together in one go. ...
[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 += grid_spacing();
83                 } else {
84                         _width += i->bundle->nchannels() * grid_spacing();
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 += grid_spacing ();
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 grid_spacing() / 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 = grid_spacing ();
155                 } else {
156                         if (_matrix->show_only_bundles()) {
157                                 w = (*i)->bundles().size() * grid_spacing();
158                         } else {
159                                 w = (*i)->total_channels() * grid_spacing();
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 += grid_spacing();
203                                 } else {
204                                         x += j->bundle->nchannels () * grid_spacing();
205                                 }
206
207                                 ++N;
208                         }
209
210                 } else {
211
212                         x += grid_spacing ();
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 += grid_spacing();
234                                         }
235                                         
236                                         ++N;
237                                 }
238
239                         } else {
240
241                                 x += grid_spacing ();
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 = grid_spacing();
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_ -= grid_spacing() * pow (sin (angle()), 2);
314                 y_ -= grid_spacing() * 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 = grid_spacing ();
331         } else {
332                 w = b->nchannels() * grid_spacing();
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         return channel_to_position (bc, _matrix->columns()) * grid_spacing ();
444 }
445
446 double
447 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &bc) const
448 {
449         return 0;
450 }
451
452 void
453 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
454 {
455         if (!bc.bundle) {
456                 return;
457         }
458
459         if (_matrix->show_only_bundles()) {
460
461                 _body->queue_draw_area (
462                         component_to_parent_x (channel_x (bc)),
463                         component_to_parent_y (0),
464                         grid_spacing() + _height * tan (angle()),
465                         _height
466                         );
467                 
468         } else {
469                 
470                 double const x = channel_x (bc);
471                 double const lc = _longest_channel_name + name_pad();
472                 double const h = lc * sin (angle ()) + grid_spacing() * sin (angle()) * cos (angle());
473                 
474                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
475
476                         _body->queue_draw_area (
477                                 component_to_parent_x (x),
478                                 component_to_parent_y (_height - h),
479                                 grid_spacing() + lc * cos (angle()),
480                                 h
481                                 );
482                         
483                 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
484                         
485                         double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
486                         
487                         _body->queue_draw_area (
488                                 component_to_parent_x (x_),
489                                 component_to_parent_y (0),
490                                 grid_spacing() + lc * cos (angle()),
491                                 h
492                                 );
493                         
494                 }
495                 
496         }
497 }
498
499 void
500 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
501 {
502         uint32_t cx = 0;
503         uint32_t const gh = _highest_group_name + 2 * name_pad();
504
505         bool group_name = false;
506         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
507                 if (y > (_height - gh)) {
508                         group_name = true;
509                         cx = x;
510                 } else {
511                         cx = x - (_height - gh - y) * tan (angle ());
512                 }
513         } else {
514                 if (y < gh) {
515                         group_name = true;
516                         cx = x - _overhang;
517                 } else {
518                         cx = x - (_height - y) * tan (angle ());
519                 }
520         }
521
522         pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> gc = position_to_group_and_channel (cx / grid_spacing(), _matrix->columns());
523         
524         if (b == 1) {
525
526                 if (group_name && gc.first) {
527                         gc.first->set_visible (!gc.first->visible ());
528                 } else if (gc.second.bundle) {
529                         _body->highlight_associated_channels (_matrix->column_index(), gc.second);
530                 }
531                 
532         } else if (b == 3) {
533
534                         _matrix->popup_menu (
535                                 gc,
536                                 make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
537                                 t
538                                 );
539         }
540 }
541