Remove idiocy.
[ardour2.git] / gtk2_ardour / port_matrix_row_labels.cc
blob47c791afb38c98ae71bc40c0f28ba7acd39dc13c
1 /*
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.
20 #include <iostream>
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"
27 #include "i18n.h"
28 #include "utils.h"
30 using namespace std;
32 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
33 : PortMatrixLabels (m, b)
38 void
39 PortMatrixRowLabels::compute_dimensions ()
41 GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
42 gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
43 cairo_t* cr = gdk_cairo_create (pm);
45 _longest_port_name = 0;
46 _longest_bundle_name = 0;
47 _height = 0;
48 _highest_group_name = 0;
50 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
52 PortGroup::BundleList const r = (*i)->bundles ();
53 for (PortGroup::BundleList::const_iterator j = r.begin(); j != r.end(); ++j) {
55 for (uint32_t k = 0; k < j->bundle->nchannels(); ++k) {
56 cairo_text_extents_t ext;
57 cairo_text_extents (cr, j->bundle->channel_name(k).c_str(), &ext);
58 if (ext.width > _longest_port_name) {
59 _longest_port_name = ext.width;
63 cairo_text_extents_t ext;
64 cairo_text_extents (cr, j->bundle->name().c_str(), &ext);
65 if (ext.width > _longest_bundle_name) {
66 _longest_bundle_name = ext.width;
70 _height += group_size (*i) * grid_spacing ();
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;
79 cairo_destroy (cr);
80 gdk_pixmap_unref (pm);
82 _width = _highest_group_name +
83 _longest_bundle_name +
84 name_pad() * 4;
86 if (!_matrix->show_only_bundles()) {
87 _width += _longest_port_name;
88 _width += name_pad() * 2;
93 void
94 PortMatrixRowLabels::render (cairo_t* cr)
96 /* BACKGROUND */
98 set_source_rgb (cr, background_colour());
99 cairo_rectangle (cr, 0, 0, _width, _height);
100 cairo_fill (cr);
102 /* PORT GROUP NAMES */
104 double x = 0;
105 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
106 x = 0;
107 } else {
108 x = _width - _highest_group_name - 2 * name_pad();
111 double y = 0;
112 int g = 0;
113 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
115 /* compute height of this group */
116 double h = 0;
117 if (!(*i)->visible()) {
118 h = grid_spacing ();
119 } else {
120 if (_matrix->show_only_bundles()) {
121 h = (*i)->bundles().size() * grid_spacing();
122 } else {
123 h = (*i)->total_channels () * grid_spacing();
127 if (h == 0) {
128 continue;
131 /* rectangle */
132 set_source_rgb (cr, get_a_group_colour (g));
133 double const rw = _highest_group_name + 2 * name_pad();
134 cairo_rectangle (cr, x, y, rw, h);
135 cairo_fill (cr);
137 /* hence what abbreviation (or not) we need for the group name */
138 string const upper = Glib::ustring ((*i)->name).uppercase ();
139 pair<string, double> display = fit_to_pixels (cr, upper, h);
141 /* plot it */
142 set_source_rgb (cr, text_colour());
143 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
144 cairo_save (cr);
145 cairo_rotate (cr, - M_PI / 2);
146 cairo_show_text (cr, display.first.c_str());
147 cairo_restore (cr);
149 y += h;
150 ++g;
153 /* BUNDLE AND PORT NAMES */
155 y = 0;
156 int N = 0;
157 int M = 0;
158 for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
160 if ((*i)->visible ()) {
162 PortGroup::BundleList const & bundles = (*i)->bundles ();
163 for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
164 render_bundle_name (cr, background_colour (), j->has_colour ? j->colour : get_a_bundle_colour (N), 0, y, j->bundle);
166 if (!_matrix->show_only_bundles()) {
167 for (uint32_t k = 0; k < j->bundle->nchannels(); ++k) {
168 Gdk::Color c = j->has_colour ? j->colour : get_a_bundle_colour (M);
169 render_channel_name (cr, background_colour (), c, 0, y, ARDOUR::BundleChannel (j->bundle, k));
170 y += grid_spacing();
171 ++M;
173 } else {
174 y += grid_spacing();
177 ++N;
180 } else {
182 y += grid_spacing ();
187 void
188 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
190 uint32_t const gw = (_highest_group_name + 2 * name_pad());
192 pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> w = position_to_group_and_channel (y / grid_spacing (), _matrix->rows());
194 if (
195 (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x < gw) ||
196 (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x > (_width - gw))
199 w.second.bundle.reset ();
202 if (b == 1) {
204 if (w.second.bundle) {
205 _body->highlight_associated_channels (_matrix->row_index(), w.second);
206 } else {
207 if (w.first) {
208 w.first->set_visible (!w.first->visible());
212 } else if (b == 3) {
214 _matrix->popup_menu (
215 make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
222 double
223 PortMatrixRowLabels::component_to_parent_x (double x) const
225 return x + _parent_rectangle.get_x();
228 double
229 PortMatrixRowLabels::parent_to_component_x (double x) const
231 return x - _parent_rectangle.get_x();
234 double
235 PortMatrixRowLabels::component_to_parent_y (double y) const
237 return y - _body->yoffset() + _parent_rectangle.get_y();
240 double
241 PortMatrixRowLabels::parent_to_component_y (double y) const
243 return y + _body->yoffset() - _parent_rectangle.get_y();
247 double
248 PortMatrixRowLabels::bundle_name_x () const
250 double x = 0;
252 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
253 x = _highest_group_name + 2 * name_pad();
254 } else {
255 if (_matrix->show_only_bundles()) {
256 x = 0;
257 } else {
258 x = _longest_port_name + name_pad() * 2;
262 return x;
265 double
266 PortMatrixRowLabels::port_name_x () const
268 if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
269 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
270 } else {
271 return 0;
274 return 0;
277 void
278 PortMatrixRowLabels::render_bundle_name (
279 cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
282 double const x = bundle_name_x ();
284 int const n = _matrix->show_only_bundles() ? 1 : b->nchannels();
285 set_source_rgb (cr, bg_colour);
286 cairo_rectangle (cr, xoff + x, yoff, _longest_bundle_name + name_pad() * 2, grid_spacing() * n);
287 cairo_fill_preserve (cr);
288 set_source_rgb (cr, fg_colour);
289 cairo_set_line_width (cr, label_border_width ());
290 cairo_stroke (cr);
292 double const off = grid_spacing() / 2;
294 // if ((*i)->nchannels () > 0 && !_matrix->show_only_bundles()) {
295 // /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
296 // cairo_text_extents_t ext;
297 // cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
298 // off = (grid_spacing() - ext.height) / 2;
299 // }
301 set_source_rgb (cr, text_colour());
302 cairo_move_to (cr, xoff + x + name_pad(), yoff + name_pad() + off);
303 cairo_show_text (cr, b->name().c_str());
306 void
307 PortMatrixRowLabels::render_channel_name (
308 cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
311 set_source_rgb (cr, bg_colour);
312 cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, grid_spacing());
313 cairo_fill_preserve (cr);
314 set_source_rgb (cr, fg_colour);
315 cairo_set_line_width (cr, label_border_width ());
316 cairo_stroke (cr);
318 cairo_text_extents_t ext;
319 cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
320 double const off = (grid_spacing() - ext.height) / 2;
322 set_source_rgb (cr, text_colour());
323 cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
324 cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
327 double
328 PortMatrixRowLabels::channel_x (ARDOUR::BundleChannel const &) const
330 return 0;
333 double
334 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
336 return channel_to_position (bc, _matrix->rows()) * grid_spacing ();
339 void
340 PortMatrixRowLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
342 if (bc.bundle) {
344 if (_matrix->show_only_bundles()) {
345 _body->queue_draw_area (
346 component_to_parent_x (bundle_name_x()) - 1,
347 component_to_parent_y (channel_y (bc)) - 1,
348 _longest_bundle_name + name_pad() * 2 + 2,
349 grid_spacing() + 2
351 } else {
352 _body->queue_draw_area (
353 component_to_parent_x (port_name_x()) - 1,
354 component_to_parent_y (channel_y (bc)) - 1,
355 _longest_port_name + name_pad() * 2 + 2,
356 grid_spacing() + 2
363 void
364 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const &)
366 clear_channel_highlights ();
367 if (_body->mouseover().column.bundle && _body->mouseover().row.bundle) {
368 add_channel_highlight (_body->mouseover().row);