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 <gtkmm/menu.h>
23 #include <gtkmm/menushell.h>
24 #include <gtkmm/menu_elems.h>
25 #include <cairo/cairo.h>
26 #include "ardour/bundle.h"
27 #include "port_matrix_row_labels.h"
28 #include "port_matrix.h"
31 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* p, PortMatrixBody* b, Location l)
32 : PortMatrixComponent (b), _port_matrix (p), _menu (0), _location (l)
37 PortMatrixRowLabels::~PortMatrixRowLabels ()
43 PortMatrixRowLabels::compute_dimensions ()
45 GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
46 gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
47 cairo_t* cr = gdk_cairo_create (pm);
49 _longest_port_name = 0;
50 _longest_bundle_name = 0;
52 ARDOUR::BundleList const r = _body->row_ports().bundles();
53 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
54 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
55 cairo_text_extents_t ext;
56 cairo_text_extents (cr, (*i)->channel_name(j).c_str(), &ext);
57 if (ext.width > _longest_port_name) {
58 _longest_port_name = ext.width;
62 cairo_text_extents_t ext;
63 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
64 if (ext.width > _longest_bundle_name) {
65 _longest_bundle_name = ext.width;
68 _height += (*i)->nchannels() * row_height();
71 _highest_group_name = 0;
72 for (PortGroupList::const_iterator i = _body->row_ports().begin(); i != _body->row_ports().end(); ++i) {
73 if ((*i)->visible()) {
74 cairo_text_extents_t ext;
75 cairo_text_extents (cr, (*i)->name.c_str(), &ext);
76 if (ext.height > _highest_group_name) {
77 _highest_group_name = ext.height;
83 gdk_pixmap_unref (pm);
85 _width = _highest_group_name +
87 _longest_bundle_name +
93 PortMatrixRowLabels::render (cairo_t* cr)
97 set_source_rgb (cr, background_colour());
98 cairo_rectangle (cr, 0, 0, _width, _height);
101 /* PORT GROUP NAMES */
104 if (_location == LEFT) {
106 } else if (_location == RIGHT) {
107 x = _width - _highest_group_name - 2 * name_pad();
112 for (PortGroupList::const_iterator i = _body->row_ports().begin(); i != _body->row_ports().end(); ++i) {
114 if (!(*i)->visible() || ((*i)->bundles().empty() && (*i)->ports.empty()) ) {
118 /* compute height of this group */
120 for (ARDOUR::BundleList::const_iterator j = (*i)->bundles().begin(); j != (*i)->bundles().end(); ++j) {
121 h += (*j)->nchannels() * row_height();
123 h += (*i)->ports.size() * row_height();
126 set_source_rgb (cr, get_a_group_colour (g));
127 double const rw = _highest_group_name + 2 * name_pad();
128 cairo_rectangle (cr, x, y, rw, h);
131 /* hence what abbreviation (or not) we need for the group name */
132 std::pair<std::string, double> display = display_port_name (cr, (*i)->name, h);
135 set_source_rgb (cr, text_colour());
136 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
138 cairo_rotate (cr, - M_PI / 2);
139 cairo_show_text (cr, display.first.c_str());
149 if (_location == LEFT) {
150 x = _highest_group_name + 2 * name_pad();
151 } else if (_location == RIGHT) {
152 x = _longest_port_name + name_pad() * 2;
156 ARDOUR::BundleList const r = _body->row_ports().bundles();
157 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
159 Gdk::Color const colour = get_a_bundle_colour (i - r.begin ());
160 set_source_rgb (cr, colour);
161 cairo_rectangle (cr, x, y, _longest_bundle_name + name_pad() * 2, row_height() * (*i)->nchannels());
162 cairo_fill_preserve (cr);
163 set_source_rgb (cr, background_colour());
164 cairo_set_line_width (cr, label_border_width ());
168 if ((*i)->nchannels () > 0) {
169 /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
170 cairo_text_extents_t ext;
171 cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
172 off = (row_height() - ext.height) / 2;
174 off = row_height() / 2;
177 set_source_rgb (cr, text_colour());
178 cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
179 cairo_show_text (cr, (*i)->name().c_str());
181 y += row_height() * (*i)->nchannels ();
188 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
189 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
190 render_port_name (cr, get_a_bundle_colour (i - r.begin()), port_name_x(), y, PortMatrixBundleChannel (*i, j));
197 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
203 if ( (_location == LEFT && x > (_longest_bundle_name + name_pad() * 2)) ||
204 (_location == RIGHT && x < (_longest_port_name + name_pad() * 2))
209 _menu = new Gtk::Menu;
210 _menu->set_name ("ArdourContextMenu");
212 Gtk::Menu_Helpers::MenuList& items = _menu->items ();
214 uint32_t row = y / row_height ();
216 boost::shared_ptr<ARDOUR::Bundle> bundle;
217 uint32_t channel = 0;
219 ARDOUR::BundleList const r = _body->row_ports().bundles();
220 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
221 if (row < (*i)->nchannels ()) {
226 row -= (*i)->nchannels ();
233 if (_port_matrix->can_rename_channels ()) {
234 snprintf (buf, sizeof (buf), _("Rename '%s'..."), bundle->channel_name (channel).c_str());
236 Gtk::Menu_Helpers::MenuElem (
238 sigc::bind (sigc::mem_fun (*this, &PortMatrixRowLabels::rename_channel_proxy), bundle, channel)
243 snprintf (buf, sizeof (buf), _("Remove '%s'"), bundle->channel_name (channel).c_str());
245 Gtk::Menu_Helpers::MenuElem (
247 sigc::bind (sigc::mem_fun (*this, &PortMatrixRowLabels::remove_channel_proxy), bundle, channel)
258 PortMatrixRowLabels::remove_channel_proxy (boost::weak_ptr<ARDOUR::Bundle> b, uint32_t c)
260 boost::shared_ptr<ARDOUR::Bundle> sb = b.lock ();
265 _port_matrix->remove_channel (sb, c);
270 PortMatrixRowLabels::rename_channel_proxy (boost::weak_ptr<ARDOUR::Bundle> b, uint32_t c)
272 boost::shared_ptr<ARDOUR::Bundle> sb = b.lock ();
277 _port_matrix->rename_channel (sb, c);
282 PortMatrixRowLabels::component_to_parent_x (double x) const
284 return x + _parent_rectangle.get_x();
288 PortMatrixRowLabels::parent_to_component_x (double x) const
290 return x - _parent_rectangle.get_x();
294 PortMatrixRowLabels::component_to_parent_y (double y) const
296 return y - _body->yoffset() + _parent_rectangle.get_y();
300 PortMatrixRowLabels::parent_to_component_y (double y) const
302 return y + _body->yoffset() - _parent_rectangle.get_y();
306 PortMatrixRowLabels::port_name_x () const
308 if (_location == LEFT) {
309 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
310 } else if (_location == RIGHT) {
318 PortMatrixRowLabels::render_port_name (
319 cairo_t* cr, Gdk::Color colour, double x, double y, PortMatrixBundleChannel const& bc
322 set_source_rgb (cr, colour);
323 cairo_rectangle (cr, x, y, _longest_port_name + name_pad() * 2, row_height());
324 cairo_fill_preserve (cr);
325 set_source_rgb (cr, background_colour());
326 cairo_set_line_width (cr, label_border_width ());
329 cairo_text_extents_t ext;
330 cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
331 double const off = (row_height() - ext.height) / 2;
333 set_source_rgb (cr, text_colour());
334 cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
335 cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
339 PortMatrixRowLabels::channel_y (PortMatrixBundleChannel const& bc) const
343 ARDOUR::BundleList::const_iterator i = _body->row_ports().bundles().begin();
344 while (i != _body->row_ports().bundles().end() && *i != bc.bundle) {
345 y += row_height() * (*i)->nchannels();
349 y += row_height() * bc.channel;
355 PortMatrixRowLabels::queue_draw_for (PortMatrixNode const& n)
359 _body->queue_draw_area (
360 component_to_parent_x (port_name_x()),
361 component_to_parent_y (channel_y (n.row)),
362 _longest_port_name + name_pad() * 2,
370 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const& old)
372 queue_draw_for (old);
373 queue_draw_for (_body->mouseover());
377 PortMatrixRowLabels::draw_extra (cairo_t* cr)
379 if (_body->mouseover().row.bundle) {
382 mouseover_port_colour (),
383 component_to_parent_x (port_name_x()),
384 component_to_parent_y (channel_y (_body->mouseover().row)),
385 _body->mouseover().row