Keep port matrix group labels on screen where possible.
[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 (PortMatrixNode const &)
219 {
220         clear_channel_highlights ();
221         if (_body->mouseover().column.bundle && _body->mouseover().row.bundle) {
222                 add_channel_highlight (_body->mouseover().column);
223         }
224 }
225
226 vector<pair<double, double> >
227 PortMatrixColumnLabels::port_name_shape (double xoff, double yoff) const
228 {
229         vector<pair<double, double> > shape;
230
231         double const lc = _longest_channel_name + name_pad();
232         double const w = grid_spacing();
233
234         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
235
236                 double x_ = xoff + slanted_height() / tan (angle()) + w;
237                 double y_ = yoff;
238                 shape.push_back (make_pair (x_, y_));
239                 x_ -= w;
240                 shape.push_back (make_pair (x_, y_));
241                 x_ -= lc * cos (angle());
242                 y_ += lc * sin (angle());
243                 shape.push_back (make_pair (x_, y_));
244                 x_ += w * pow (sin (angle()), 2);
245                 y_ += w * sin (angle()) * cos (angle());
246                 shape.push_back (make_pair (x_, y_));
247
248         } else {
249
250                 double x_ = xoff;
251                 double y_ = yoff + _height;
252                 shape.push_back (make_pair (x_, y_));
253                 x_ += w;
254                 shape.push_back (make_pair (x_, y_));
255                 x_ += lc * cos (angle());
256                 y_ -= lc * sin (angle());
257                 shape.push_back (make_pair (x_, y_));
258                 x_ -= grid_spacing() * pow (sin (angle()), 2);
259                 y_ -= grid_spacing() * sin (angle()) * cos (angle());
260                 shape.push_back (make_pair (x_, y_));
261         }
262
263         return shape;
264 }
265
266 void
267 PortMatrixColumnLabels::render_bundle_name (
268         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
269         )
270 {
271         set_source_rgb (cr, bg_colour);
272
273         double w = 0;
274         if (_matrix->show_only_bundles()) {
275                 w = grid_spacing ();
276         } else {
277                 w = b->nchannels() * grid_spacing();
278         }
279
280         double x_ = xoff;
281
282         uint32_t y = yoff;
283         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
284                 y += _height;
285         } else {
286                 y += slanted_height();
287         }
288
289         double y_ = y;
290         cairo_move_to (cr, x_, y_);
291         x_ += w;
292         cairo_line_to (cr, x_, y_);
293         x_ += slanted_height() / tan (angle ());
294         y_ -= slanted_height();
295         cairo_line_to (cr, x_, y_);
296         x_ -= w;
297         cairo_line_to (cr, x_, y_);
298         cairo_line_to (cr, xoff, y);
299         cairo_fill_preserve (cr);
300         set_source_rgb (cr, fg_colour);
301         cairo_set_line_width (cr, label_border_width());
302         cairo_stroke (cr);
303
304         set_source_rgb (cr, text_colour());
305
306         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
307
308                 double rl = 0;
309                 if (_matrix->show_only_bundles()) {
310                         rl = name_pad();
311                 } else {
312                         rl = 3 * name_pad() + _longest_channel_name;
313                 }
314                 cairo_move_to (
315                         cr,
316                         xoff + basic_text_x_pos (0) + rl * cos (angle()),
317                         yoff + _height - rl * sin (angle())
318                         );
319
320         } else {
321
322                 cairo_move_to (
323                         cr,
324                         xoff + basic_text_x_pos (0) + name_pad() * cos (angle ()),
325                         yoff + slanted_height() - name_pad() * sin (angle())
326                         );
327         }
328
329         cairo_save (cr);
330         cairo_rotate (cr, -angle());
331         cairo_show_text (cr, b->name().c_str());
332         cairo_restore (cr);
333 }
334
335 void
336 PortMatrixColumnLabels::render_channel_name (
337         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, ARDOUR::BundleChannel const &bc
338         )
339 {
340         vector<pair<double, double> > const shape = port_name_shape (xoff, yoff);
341
342         cairo_move_to (cr, shape[0].first, shape[0].second);
343         for (uint32_t i = 1; i < 4; ++i) {
344                 cairo_line_to (cr, shape[i].first, shape[i].second);
345         }
346         cairo_line_to (cr, shape[0].first, shape[0].second);
347
348         set_source_rgb (cr, bg_colour);
349         cairo_fill_preserve (cr);
350         set_source_rgb (cr, fg_colour);
351         cairo_set_line_width (cr, label_border_width());
352         cairo_stroke (cr);
353
354         set_source_rgb (cr, text_colour());
355
356         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
357
358                 cairo_move_to (
359                         cr,
360                         xoff + basic_text_x_pos(bc.channel),
361                         yoff + _height - name_pad() * sin (angle())
362                         );
363
364         } else {
365
366                 double const rl = 3 * name_pad() + _longest_bundle_name;
367                 cairo_move_to (
368                         cr,
369                         xoff + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
370                         yoff + slanted_height() - rl * sin (angle())
371                         );
372         }
373
374         if (bc.bundle->nchannels() > 1) {
375
376                 /* only plot the name if the bundle has more than one channel;
377                    the name of a single channel is assumed to be redundant */
378                 
379                 cairo_save (cr);
380                 cairo_rotate (cr, -angle());
381                 
382                 cairo_show_text (
383                         cr,
384                         bc.bundle->channel_name(bc.channel).c_str()
385                         );
386                 
387                 cairo_restore (cr);
388         }
389 }
390
391 double
392 PortMatrixColumnLabels::channel_x (ARDOUR::BundleChannel const &bc) const
393 {
394         return channel_to_position (bc, _matrix->columns()) * grid_spacing ();
395 }
396
397 double
398 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &) const
399 {
400         return 0;
401 }
402
403 void
404 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
405 {
406         if (!bc.bundle) {
407                 return;
408         }
409
410         if (_matrix->show_only_bundles()) {
411
412                 _body->queue_draw_area (
413                         component_to_parent_x (channel_x (bc)) - 1,
414                         component_to_parent_y (0) - 1,
415                         grid_spacing() + _height * tan (angle()) + 2,
416                         _height + 2
417                         );
418
419         } else {
420
421                 double const x = channel_x (bc);
422                 double const lc = _longest_channel_name + name_pad();
423                 double const h = lc * sin (angle ()) + grid_spacing() * sin (angle()) * cos (angle());
424
425                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
426
427                         _body->queue_draw_area (
428                                 component_to_parent_x (x) - 1,
429                                 component_to_parent_y (_height - h) - 1,
430                                 grid_spacing() + lc * cos (angle()) + 2,
431                                 h + 2
432                                 );
433
434                 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
435
436                         double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
437
438                         _body->queue_draw_area (
439                                 component_to_parent_x (x_) - 1,
440                                 component_to_parent_y (0) - 1,
441                                 grid_spacing() + lc * cos (angle()) + 2,
442                                 h + 2
443                                 );
444
445                 }
446
447         }
448 }
449
450 void
451 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
452 {
453         uint32_t cx = 0;
454         uint32_t const gh = _highest_group_name + 2 * name_pad();
455
456         bool group_name = false;
457         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
458                 if (y > (_height - gh)) {
459                         group_name = true;
460                         cx = x;
461                 } else {
462                         cx = x - (_height - gh - y) * tan (angle ());
463                 }
464         } else {
465                 if (y < gh) {
466                         group_name = true;
467                         cx = x - _overhang;
468                 } else {
469                         cx = x - (_height - y) * tan (angle ());
470                 }
471         }
472
473         pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> gc = position_to_group_and_channel (cx / grid_spacing(), _matrix->columns());
474
475         if (b == 1) {
476
477                 if (group_name && gc.first) {
478                         gc.first->set_visible (!gc.first->visible ());
479                 } else if (gc.second.bundle) {
480                         _body->highlight_associated_channels (_matrix->column_index(), gc.second);
481                 }
482
483         } else if (b == 3) {
484
485                         _matrix->popup_menu (
486                                 gc,
487                                 make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
488                                 t
489                                 );
490         }
491 }
492
493 void
494 PortMatrixColumnLabels::draw_extra (cairo_t* cr)
495 {
496         PortMatrixLabels::draw_extra (cr);
497
498         /* PORT GROUP NAME */
499         
500         double x = 0;
501         double y = 0;
502
503         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
504                 x = component_to_parent_x (slanted_height() / tan (angle()));
505                 y = component_to_parent_y ( _highest_group_name + name_pad());
506         } else {
507                 x = component_to_parent_x (0);
508                 y = component_to_parent_y (_height - name_pad());
509         }
510
511         int g = 0;
512         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
513
514                 /* compute width of this group */
515                 uint32_t w = 0;
516                 if (!(*i)->visible()) {
517                         w = grid_spacing ();
518                 } else {
519                         if (_matrix->show_only_bundles()) {
520                                 w = (*i)->bundles().size() * grid_spacing();
521                         } else {
522                                 w = (*i)->total_channels() * grid_spacing();
523                         }
524                 }
525
526                 if (w == 0) {
527                         continue;
528                 }
529
530                 /* rectangle */
531                 set_source_rgb (cr, get_a_group_colour (g));
532                 double const rh = _highest_group_name + 2 * name_pad();
533                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
534                         cairo_rectangle (cr, x, component_to_parent_y (0), w, rh);
535                 } else {
536                         cairo_rectangle (cr, x, component_to_parent_y (_height - rh), w, rh);
537                 }
538                 cairo_fill (cr);
539
540                 /* x area available to draw the label in (trying to keep it visible) */
541                 double const lx = max (x, double (_parent_rectangle.get_x ()));
542                 double const rx = min (x + w, double (_parent_rectangle.get_width()));
543
544                 /* hence what abbreviation (or not) we need for the group name */
545                 string const upper = Glib::ustring ((*i)->name).uppercase ();
546                 pair<string, double> const display = fit_to_pixels (cr, upper, rx - lx);
547
548                 /* plot it */
549                 set_source_rgb (cr, text_colour());
550                 cairo_move_to (cr, (lx + rx - display.second) / 2, y);
551                 cairo_show_text (cr, display.first.c_str());
552
553                 x += w;
554                 ++g;
555
556         }
557 }