2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
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"
30 PortMatrixColumnLabels::PortMatrixColumnLabels (PortMatrix* m, PortMatrixBody* b)
31 : PortMatrixLabels (m, b),
38 PortMatrixColumnLabels::compute_dimensions ()
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);
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) */
50 /* width of the whole thing */
52 _highest_group_name = 0;
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) {
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;
64 if (ext.height > _highest_text) {
65 _highest_text = ext.height;
68 for (uint32_t k = 0; k < j->bundle->nchannels (); ++k) {
72 j->bundle->channel_name (k).c_str(),
76 if (ext.width > _longest_channel_name) {
77 _longest_channel_name = ext.width;
80 if (ext.height > _highest_text) {
81 _highest_text = ext.height;
86 _width += group_size (*i) * grid_spacing ();
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;
96 gdk_pixmap_unref (pm);
98 /* height of the whole thing */
100 int a = _longest_bundle_name + 4 * name_pad();
101 if (!_matrix->show_only_bundles()) {
102 a += _longest_channel_name;
105 double const parallelogram_height = a * sin (angle()) + _highest_text * cos (angle());
107 _height = parallelogram_height + _highest_group_name + 2 * name_pad();
109 _overhang = parallelogram_height / tan (angle ());
114 PortMatrixColumnLabels::basic_text_x_pos (int) const
116 return grid_spacing() / 2 +
117 _highest_text / (2 * sin (angle ()));
121 PortMatrixColumnLabels::render (cairo_t* cr)
125 set_source_rgb (cr, background_colour());
126 cairo_rectangle (cr, 0, 0, _width, _height);
129 /* BUNDLE PARALLELOGRAM-TYPE-THING AND NAME */
134 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
136 if ((*i)->visible ()) {
138 PortGroup::BundleList const & bundles = (*i)->bundles ();
139 for (PortGroup::BundleList::const_iterator j = bundles.begin (); j != bundles.end(); ++j) {
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);
144 if (_matrix->show_only_bundles()) {
147 x += j->bundle->nchannels () * grid_spacing();
155 x += grid_spacing ();
163 if (!_matrix->show_only_bundles()) {
166 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
168 if ((*i)->visible ()) {
170 PortGroup::BundleList const & bundles = (*i)->bundles ();
171 for (PortGroup::BundleList::const_iterator j = bundles.begin (); j != bundles.end(); ++j) {
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));
184 x += grid_spacing ();
192 PortMatrixColumnLabels::component_to_parent_x (double x) const
194 return x - _body->xoffset() + _parent_rectangle.get_x();
198 PortMatrixColumnLabels::parent_to_component_x (double x) const
200 return x + _body->xoffset() - _parent_rectangle.get_x();
204 PortMatrixColumnLabels::component_to_parent_y (double y) const
206 /* Column labels don't scroll vertically, so y conversion does not depend on yoffset */
207 return y + _parent_rectangle.get_y();
211 PortMatrixColumnLabels::parent_to_component_y (double y) const
213 /* Column labels don't scroll vertically, so y conversion does not depend on yoffset */
214 return y - _parent_rectangle.get_y();
218 PortMatrixColumnLabels::mouseover_changed (PortMatrixNode const &)
220 clear_channel_highlights ();
221 if (_body->mouseover().column.bundle && _body->mouseover().row.bundle) {
222 add_channel_highlight (_body->mouseover().column);
226 vector<pair<double, double> >
227 PortMatrixColumnLabels::port_name_shape (double xoff, double yoff) const
229 vector<pair<double, double> > shape;
231 double const lc = _longest_channel_name + name_pad();
232 double const w = grid_spacing();
234 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
236 double x_ = xoff + slanted_height() / tan (angle()) + w;
238 shape.push_back (make_pair (x_, y_));
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_));
251 double y_ = yoff + _height;
252 shape.push_back (make_pair (x_, y_));
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_));
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
271 set_source_rgb (cr, bg_colour);
274 if (_matrix->show_only_bundles()) {
277 w = b->nchannels() * grid_spacing();
283 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
286 y += slanted_height();
290 cairo_move_to (cr, x_, y_);
292 cairo_line_to (cr, x_, y_);
293 x_ += slanted_height() / tan (angle ());
294 y_ -= slanted_height();
295 cairo_line_to (cr, x_, y_);
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());
304 set_source_rgb (cr, text_colour());
306 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
309 if (_matrix->show_only_bundles()) {
312 rl = 3 * name_pad() + _longest_channel_name;
316 xoff + basic_text_x_pos (0) + rl * cos (angle()),
317 yoff + _height - rl * sin (angle())
324 xoff + basic_text_x_pos (0) + name_pad() * cos (angle ()),
325 yoff + slanted_height() - name_pad() * sin (angle())
330 cairo_rotate (cr, -angle());
331 cairo_show_text (cr, b->name().c_str());
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
340 vector<pair<double, double> > const shape = port_name_shape (xoff, yoff);
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);
346 cairo_line_to (cr, shape[0].first, shape[0].second);
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());
354 set_source_rgb (cr, text_colour());
356 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
360 xoff + basic_text_x_pos(bc.channel),
361 yoff + _height - name_pad() * sin (angle())
366 double const rl = 3 * name_pad() + _longest_bundle_name;
369 xoff + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
370 yoff + slanted_height() - rl * sin (angle())
374 if (bc.bundle->nchannels() > 1) {
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 */
380 cairo_rotate (cr, -angle());
384 bc.bundle->channel_name(bc.channel).c_str()
392 PortMatrixColumnLabels::channel_x (ARDOUR::BundleChannel const &bc) const
394 return channel_to_position (bc, _matrix->columns()) * grid_spacing ();
398 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &) const
404 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
410 if (_matrix->show_only_bundles()) {
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,
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());
425 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
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,
434 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
436 double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
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,
451 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
454 uint32_t const gh = _highest_group_name + 2 * name_pad();
456 bool group_name = false;
457 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
458 if (y > (_height - gh)) {
462 cx = x - (_height - gh - y) * tan (angle ());
469 cx = x - (_height - y) * tan (angle ());
473 pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> gc = position_to_group_and_channel (cx / grid_spacing(), _matrix->columns());
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);
485 _matrix->popup_menu (
487 make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
494 PortMatrixColumnLabels::draw_extra (cairo_t* cr)
496 PortMatrixLabels::draw_extra (cr);
498 /* PORT GROUP NAME */
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());
507 x = component_to_parent_x (0);
508 y = component_to_parent_y (_height - name_pad());
512 for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
514 /* compute width of this group */
516 if (!(*i)->visible()) {
519 if (_matrix->show_only_bundles()) {
520 w = (*i)->bundles().size() * grid_spacing();
522 w = (*i)->total_channels() * grid_spacing();
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);
536 cairo_rectangle (cr, x, component_to_parent_y (_height - rh), w, rh);
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()));
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);
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());