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