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"
28 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
29 : PortMatrixComponent (m, b)
35 PortMatrixRowLabels::compute_dimensions ()
37 GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
38 gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
39 cairo_t* cr = gdk_cairo_create (pm);
41 _longest_port_name = 0;
42 _longest_bundle_name = 0;
44 ARDOUR::BundleList const r = _matrix->rows()->bundles();
45 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
46 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
47 cairo_text_extents_t ext;
48 cairo_text_extents (cr, (*i)->channel_name(j).c_str(), &ext);
49 if (ext.width > _longest_port_name) {
50 _longest_port_name = ext.width;
54 cairo_text_extents_t ext;
55 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
56 if (ext.width > _longest_bundle_name) {
57 _longest_bundle_name = ext.width;
60 _height += (*i)->nchannels() * row_height();
63 _highest_group_name = 0;
64 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
65 if ((*i)->visible()) {
66 cairo_text_extents_t ext;
67 cairo_text_extents (cr, (*i)->name.c_str(), &ext);
68 if (ext.height > _highest_group_name) {
69 _highest_group_name = ext.height;
75 gdk_pixmap_unref (pm);
77 _width = _highest_group_name +
79 _longest_bundle_name +
85 PortMatrixRowLabels::render (cairo_t* cr)
89 set_source_rgb (cr, background_colour());
90 cairo_rectangle (cr, 0, 0, _width, _height);
93 /* PORT GROUP NAMES */
96 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
99 x = _width - _highest_group_name - 2 * name_pad();
104 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
106 if (!(*i)->visible() || ((*i)->bundles().empty() && (*i)->ports.empty()) ) {
110 /* compute height of this group */
112 for (ARDOUR::BundleList::const_iterator j = (*i)->bundles().begin(); j != (*i)->bundles().end(); ++j) {
113 h += (*j)->nchannels() * row_height();
115 h += (*i)->ports.size() * row_height();
118 set_source_rgb (cr, get_a_group_colour (g));
119 double const rw = _highest_group_name + 2 * name_pad();
120 cairo_rectangle (cr, x, y, rw, h);
123 /* hence what abbreviation (or not) we need for the group name */
124 std::pair<std::string, double> display = display_port_name (cr, (*i)->name, h);
127 set_source_rgb (cr, text_colour());
128 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
130 cairo_rotate (cr, - M_PI / 2);
131 cairo_show_text (cr, display.first.c_str());
141 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
142 x = _highest_group_name + 2 * name_pad();
144 x = _longest_port_name + name_pad() * 2;
148 ARDOUR::BundleList const r = _matrix->rows()->bundles();
149 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
151 Gdk::Color const colour = get_a_bundle_colour (i - r.begin ());
152 set_source_rgb (cr, colour);
153 cairo_rectangle (cr, x, y, _longest_bundle_name + name_pad() * 2, row_height() * (*i)->nchannels());
154 cairo_fill_preserve (cr);
155 set_source_rgb (cr, background_colour());
156 cairo_set_line_width (cr, label_border_width ());
160 if ((*i)->nchannels () > 0) {
161 /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
162 cairo_text_extents_t ext;
163 cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
164 off = (row_height() - ext.height) / 2;
166 off = row_height() / 2;
169 set_source_rgb (cr, text_colour());
170 cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
171 cairo_show_text (cr, (*i)->name().c_str());
173 y += row_height() * (*i)->nchannels ();
180 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
181 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
182 render_port_name (cr, get_a_bundle_colour (i - r.begin()), 0, y, ARDOUR::BundleChannel (*i, j));
189 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
195 if (!_matrix->can_rename_channels (_matrix->row_index()) &&
196 !_matrix->can_remove_channels (_matrix->row_index())) {
200 if ( (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x > (_longest_bundle_name + name_pad() * 2)) ||
201 (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x < (_longest_port_name + name_pad() * 2))
204 _matrix->popup_channel_context_menu (_matrix->row_index(), y / row_height(), t);
210 PortMatrixRowLabels::component_to_parent_x (double x) const
212 return x + _parent_rectangle.get_x();
216 PortMatrixRowLabels::parent_to_component_x (double x) const
218 return x - _parent_rectangle.get_x();
222 PortMatrixRowLabels::component_to_parent_y (double y) const
224 return y - _body->yoffset() + _parent_rectangle.get_y();
228 PortMatrixRowLabels::parent_to_component_y (double y) const
230 return y + _body->yoffset() - _parent_rectangle.get_y();
234 PortMatrixRowLabels::port_name_x () const
236 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
237 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
246 PortMatrixRowLabels::render_port_name (
247 cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
250 set_source_rgb (cr, colour);
251 cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, row_height());
252 cairo_fill_preserve (cr);
253 set_source_rgb (cr, background_colour());
254 cairo_set_line_width (cr, label_border_width ());
257 cairo_text_extents_t ext;
258 cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
259 double const off = (row_height() - ext.height) / 2;
261 set_source_rgb (cr, text_colour());
262 cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
263 cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
267 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
271 ARDOUR::BundleList::const_iterator i = _matrix->rows()->bundles().begin();
272 while (i != _matrix->rows()->bundles().end() && *i != bc.bundle) {
273 n += (*i)->nchannels ();
278 return n * row_height();
282 PortMatrixRowLabels::queue_draw_for (PortMatrixNode const& n)
286 _body->queue_draw_area (
287 component_to_parent_x (port_name_x()),
288 component_to_parent_y (channel_y (n.row)),
289 _longest_port_name + name_pad() * 2,
297 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const& old)
299 queue_draw_for (old);
300 queue_draw_for (_body->mouseover());
304 PortMatrixRowLabels::draw_extra (cairo_t* cr)
306 if (_body->mouseover().row.bundle) {
309 mouseover_port_colour (),
310 component_to_parent_x (0),
311 component_to_parent_y (channel_y (_body->mouseover().row)),
312 _body->mouseover().row