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