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