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