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