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 <boost/weak_ptr.hpp>
22 #include <cairo/cairo.h>
23 #include "ardour/bundle.h"
24 #include "port_matrix_row_labels.h"
25 #include "port_matrix.h"
26 #include "port_matrix_body.h"
30 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
31 : PortMatrixLabels (m, b)
37 PortMatrixRowLabels::compute_dimensions ()
39 GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
40 gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
41 cairo_t* cr = gdk_cairo_create (pm);
43 _longest_port_name = 0;
44 _longest_bundle_name = 0;
46 PortGroup::BundleList const r = _matrix->rows()->bundles();
47 for (PortGroup::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
48 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
49 cairo_text_extents_t ext;
50 cairo_text_extents (cr, i->bundle->channel_name(j).c_str(), &ext);
51 if (ext.width > _longest_port_name) {
52 _longest_port_name = ext.width;
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;
62 if (_matrix->show_only_bundles()) {
63 _height += row_height ();
65 _height += i->bundle->nchannels() * row_height();
69 _highest_group_name = 0;
70 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
71 if ((*i)->visible()) {
72 cairo_text_extents_t ext;
73 cairo_text_extents (cr, (*i)->name.c_str(), &ext);
74 if (ext.height > _highest_group_name) {
75 _highest_group_name = ext.height;
81 gdk_pixmap_unref (pm);
83 _width = _highest_group_name +
84 _longest_bundle_name +
87 if (!_matrix->show_only_bundles()) {
88 _width += _longest_port_name;
89 _width += name_pad() * 2;
95 PortMatrixRowLabels::render (cairo_t* cr)
99 set_source_rgb (cr, background_colour());
100 cairo_rectangle (cr, 0, 0, _width, _height);
103 /* PORT GROUP NAMES */
106 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
109 x = _width - _highest_group_name - 2 * name_pad();
114 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
116 if (!(*i)->visible() || (*i)->bundles().empty()) {
120 /* compute height of this group */
122 if (_matrix->show_only_bundles()) {
123 h = (*i)->bundles().size() * row_height();
125 h = (*i)->total_channels () * row_height();
129 set_source_rgb (cr, get_a_group_colour (g));
130 double const rw = _highest_group_name + 2 * name_pad();
131 cairo_rectangle (cr, x, y, rw, h);
134 /* hence what abbreviation (or not) we need for the group name */
135 std::string const upper = Glib::ustring ((*i)->name).uppercase ();
136 std::pair<std::string, double> display = fit_to_pixels (cr, upper, h);
139 set_source_rgb (cr, text_colour());
140 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
142 cairo_rotate (cr, - M_PI / 2);
143 cairo_show_text (cr, display.first.c_str());
154 PortGroup::BundleList const r = _matrix->rows()->bundles();
155 for (PortGroup::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
156 render_bundle_name (cr, i->has_colour ? i->colour : get_a_bundle_colour (N), 0, y, i->bundle);
157 int const n = _matrix->show_only_bundles() ? 1 : i->bundle->nchannels();
158 y += row_height() * n;
165 if (!_matrix->show_only_bundles()) {
168 for (PortGroup::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
169 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
170 render_channel_name (cr, i->has_colour ? i->colour : get_a_bundle_colour (N), 0, y, ARDOUR::BundleChannel (i->bundle, j));
179 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
183 _body->highlight_associated_channels (_matrix->row_index(), y / row_height ());
186 maybe_popup_context_menu (x, y, t);
192 PortMatrixRowLabels::maybe_popup_context_menu (double x, double y, uint32_t t)
194 if ( (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x > (_longest_bundle_name + name_pad() * 2)) ||
195 (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x < (_longest_port_name + name_pad() * 2))
198 _matrix->popup_channel_context_menu (_matrix->row_index(), y / row_height(), t);
204 PortMatrixRowLabels::component_to_parent_x (double x) const
206 return x + _parent_rectangle.get_x();
210 PortMatrixRowLabels::parent_to_component_x (double x) const
212 return x - _parent_rectangle.get_x();
216 PortMatrixRowLabels::component_to_parent_y (double y) const
218 return y - _body->yoffset() + _parent_rectangle.get_y();
222 PortMatrixRowLabels::parent_to_component_y (double y) const
224 return y + _body->yoffset() - _parent_rectangle.get_y();
229 PortMatrixRowLabels::bundle_name_x () const
233 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
234 x = _highest_group_name + 2 * name_pad();
236 if (_matrix->show_only_bundles()) {
239 x = _longest_port_name + name_pad() * 2;
247 PortMatrixRowLabels::port_name_x () const
249 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
250 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
259 PortMatrixRowLabels::render_bundle_name (
260 cairo_t* cr, Gdk::Color colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
263 double const x = bundle_name_x ();
265 int const n = _matrix->show_only_bundles() ? 1 : b->nchannels();
266 set_source_rgb (cr, colour);
267 cairo_rectangle (cr, xoff + x, yoff, _longest_bundle_name + name_pad() * 2, row_height() * n);
268 cairo_fill_preserve (cr);
269 set_source_rgb (cr, background_colour());
270 cairo_set_line_width (cr, label_border_width ());
273 double const off = row_height() / 2;
275 // if ((*i)->nchannels () > 0 && !_matrix->show_only_bundles()) {
276 // /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
277 // cairo_text_extents_t ext;
278 // cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
279 // off = (row_height() - ext.height) / 2;
282 set_source_rgb (cr, text_colour());
283 cairo_move_to (cr, xoff + x + name_pad(), yoff + name_pad() + off);
284 cairo_show_text (cr, b->name().c_str());
288 PortMatrixRowLabels::render_channel_name (
289 cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
292 set_source_rgb (cr, colour);
293 cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, row_height());
294 cairo_fill_preserve (cr);
295 set_source_rgb (cr, background_colour());
296 cairo_set_line_width (cr, label_border_width ());
299 cairo_text_extents_t ext;
300 cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
301 double const off = (row_height() - ext.height) / 2;
303 set_source_rgb (cr, text_colour());
304 cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
305 cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
309 PortMatrixRowLabels::channel_x (ARDOUR::BundleChannel const& bc) const
315 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
319 PortGroup::BundleList const & bundles = _matrix->rows()->bundles();
320 PortGroup::BundleList::const_iterator i = bundles.begin ();
321 while (i != bundles.end() && i->bundle != bc.bundle) {
322 if (_matrix->show_only_bundles()) {
325 n += i->bundle->nchannels ();
330 if (!_matrix->show_only_bundles()) {
334 return n * row_height();
338 PortMatrixRowLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
342 if (_matrix->show_only_bundles()) {
343 _body->queue_draw_area (
344 component_to_parent_x (bundle_name_x()),
345 component_to_parent_y (channel_y (bc)),
346 _longest_bundle_name + name_pad() * 2,
350 _body->queue_draw_area (
351 component_to_parent_x (port_name_x()),
352 component_to_parent_y (channel_y (bc)),
353 _longest_port_name + name_pad() * 2,
362 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const &)
364 clear_channel_highlights ();
365 if (_body->mouseover().row.bundle) {
366 add_channel_highlight (_body->mouseover().row);