Support for the port matrix working at the bundle level and hiding details of ports.
[ardour.git] / gtk2_ardour / port_matrix_row_labels.cc
1 /*
2     Copyright (C) 2002-2009 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
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
29 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
30         : PortMatrixLabels (m, b)
31 {
32         
33 }
34
35 void
36 PortMatrixRowLabels::compute_dimensions ()
37 {
38         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
39         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
40         cairo_t* cr = gdk_cairo_create (pm);
41         
42         _longest_port_name = 0;
43         _longest_bundle_name = 0;
44         _height = 0;
45         ARDOUR::BundleList const r = _matrix->rows()->bundles();
46         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
47                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
48                         cairo_text_extents_t ext;
49                         cairo_text_extents (cr, (*i)->channel_name(j).c_str(), &ext);
50                         if (ext.width > _longest_port_name) {
51                                 _longest_port_name = ext.width;
52                         }
53                 }
54
55                 cairo_text_extents_t ext;
56                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
57                 if (ext.width > _longest_bundle_name) {
58                         _longest_bundle_name = ext.width;
59                 }
60
61                 if (_matrix->show_only_bundles()) {
62                         _height += row_height ();
63                 } else {
64                         _height += (*i)->nchannels() * row_height();
65                 }
66         }
67
68         _highest_group_name = 0;
69         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
70                 if ((*i)->visible()) {
71                         cairo_text_extents_t ext;
72                         cairo_text_extents (cr, (*i)->name.c_str(), &ext);
73                         if (ext.height > _highest_group_name) {
74                                 _highest_group_name = ext.height;
75                         }
76                 }
77         }
78                         
79         cairo_destroy (cr);
80         gdk_pixmap_unref (pm);
81
82         _width = _highest_group_name +
83                 _longest_bundle_name +
84                 name_pad() * 4;
85
86         if (!_matrix->show_only_bundles()) {
87                 _width += _longest_port_name;
88                 _width += name_pad() * 2;
89         }
90 }
91
92
93 void
94 PortMatrixRowLabels::render (cairo_t* cr)
95 {
96         /* BACKGROUND */
97         
98         set_source_rgb (cr, background_colour());
99         cairo_rectangle (cr, 0, 0, _width, _height);
100         cairo_fill (cr);
101
102         /* PORT GROUP NAMES */
103
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();
109         }
110
111         double y = 0;
112         int g = 0;
113         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
114
115                 if (!(*i)->visible() || (*i)->bundles().empty()) {
116                         continue;
117                 }
118                         
119                 /* compute height of this group */
120                 double h = 0;
121                 if (_matrix->show_only_bundles()) {
122                         h = (*i)->bundles().size() * row_height();
123                 } else {
124                         h = (*i)->total_channels () * row_height();
125                 }
126
127                 /* rectangle */
128                 set_source_rgb (cr, get_a_group_colour (g));
129                 double const rw = _highest_group_name + 2 * name_pad();
130                 cairo_rectangle (cr, x, y, rw, h);
131                 cairo_fill (cr);
132                     
133                 /* hence what abbreviation (or not) we need for the group name */
134                 std::pair<std::string, double> display = display_port_name (cr, (*i)->name, h);
135
136                 /* plot it */
137                 set_source_rgb (cr, text_colour());
138                 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
139                 cairo_save (cr);
140                 cairo_rotate (cr, - M_PI / 2);
141                 cairo_show_text (cr, display.first.c_str());
142                 cairo_restore (cr);
143
144                 y += h;
145                 ++g;
146         }
147
148         /* BUNDLE NAMES */
149
150         y = 0;
151         ARDOUR::BundleList const r = _matrix->rows()->bundles();
152         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
153                 render_bundle_name (cr, get_a_bundle_colour (i - r.begin ()), 0, y, *i);
154                 int const n = _matrix->show_only_bundles() ? 1 : (*i)->nchannels();
155                 y += row_height() * n;
156         }
157         
158
159         /* PORT NAMES */
160
161         if (!_matrix->show_only_bundles()) {
162                 y = 0;
163                 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
164                         for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
165                                 render_channel_name (cr, get_a_bundle_colour (i - r.begin()), 0, y, ARDOUR::BundleChannel (*i, j));
166                                 y += row_height();
167                         }
168                 }
169         }
170 }
171
172 void
173 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
174 {
175         switch (b) {
176         case 1:
177                 _body->highlight_associated_channels (_matrix->row_index(), y / row_height ());
178                 break;
179         case 3:
180                 maybe_popup_context_menu (x, y, t);
181                 break;
182         }
183 }
184
185 void
186 PortMatrixRowLabels::maybe_popup_context_menu (double x, double y, uint32_t t)
187 {
188         if ( (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x > (_longest_bundle_name + name_pad() * 2)) ||
189              (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x < (_longest_port_name + name_pad() * 2))
190                 ) {
191
192                 _matrix->popup_channel_context_menu (_matrix->row_index(), y / row_height(), t);
193                 
194         }
195 }
196
197 double
198 PortMatrixRowLabels::component_to_parent_x (double x) const
199 {
200         return x + _parent_rectangle.get_x();
201 }
202
203 double
204 PortMatrixRowLabels::parent_to_component_x (double x) const
205 {
206         return x - _parent_rectangle.get_x();
207 }
208
209 double
210 PortMatrixRowLabels::component_to_parent_y (double y) const
211 {
212         return y - _body->yoffset() + _parent_rectangle.get_y();
213 }
214
215 double
216 PortMatrixRowLabels::parent_to_component_y (double y) const
217 {
218         return y + _body->yoffset() - _parent_rectangle.get_y();
219 }
220
221
222 double
223 PortMatrixRowLabels::bundle_name_x () const
224 {
225         double x = 0;
226         
227         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
228                 x = _highest_group_name + 2 * name_pad();
229         } else {
230                 if (_matrix->show_only_bundles()) {
231                         x = 0;
232                 } else {
233                         x = _longest_port_name + name_pad() * 2;
234                 }
235         }
236
237         return x;
238 }
239
240 double
241 PortMatrixRowLabels::port_name_x () const
242 {
243         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
244                 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
245         } else {
246                 return 0;
247         }
248
249         return 0;
250 }
251
252 void
253 PortMatrixRowLabels::render_bundle_name (
254         cairo_t* cr, Gdk::Color colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
255         )
256 {
257         double const x = bundle_name_x ();
258         
259         int const n = _matrix->show_only_bundles() ? 1 : b->nchannels();
260         set_source_rgb (cr, colour);
261         cairo_rectangle (cr, xoff + x, yoff, _longest_bundle_name + name_pad() * 2, row_height() * n);
262         cairo_fill_preserve (cr);
263         set_source_rgb (cr, background_colour());
264         cairo_set_line_width (cr, label_border_width ());
265         cairo_stroke (cr);
266
267         double const off = row_height() / 2;
268
269 //      if ((*i)->nchannels () > 0 && !_matrix->show_only_bundles()) {
270 //              /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
271 //              cairo_text_extents_t ext;
272 //              cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
273 //              off = (row_height() - ext.height) / 2;
274 //      }
275
276         set_source_rgb (cr, text_colour());
277         cairo_move_to (cr, xoff + x + name_pad(), yoff + name_pad() + off);
278         cairo_show_text (cr, b->name().c_str());
279 }
280
281 void
282 PortMatrixRowLabels::render_channel_name (
283         cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
284         )
285 {
286         set_source_rgb (cr, colour);
287         cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, row_height());
288         cairo_fill_preserve (cr);
289         set_source_rgb (cr, background_colour());
290         cairo_set_line_width (cr, label_border_width ());
291         cairo_stroke (cr);
292         
293         cairo_text_extents_t ext;
294         cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
295         double const off = (row_height() - ext.height) / 2;
296         
297         set_source_rgb (cr, text_colour());
298         cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
299         cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
300 }
301
302 double
303 PortMatrixRowLabels::channel_x (ARDOUR::BundleChannel const& bc) const
304 {
305         return 0;
306 }
307
308 double
309 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
310 {
311         uint32_t n = 0;
312
313         ARDOUR::BundleList::const_iterator i = _matrix->rows()->bundles().begin();
314         while (i != _matrix->rows()->bundles().end() && *i != bc.bundle) {
315                 if (_matrix->show_only_bundles()) {
316                         n += 1;
317                 } else {
318                         n += (*i)->nchannels ();
319                 }
320                 ++i;
321         }
322
323         if (!_matrix->show_only_bundles()) {
324                 n += bc.channel;
325         }
326         
327         return n * row_height();
328 }
329
330 void
331 PortMatrixRowLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
332 {
333         if (bc.bundle) {
334
335                 if (_matrix->show_only_bundles()) {
336                         _body->queue_draw_area (
337                                 component_to_parent_x (bundle_name_x()),
338                                 component_to_parent_y (channel_y (bc)),
339                                 _longest_bundle_name + name_pad() * 2,
340                                 row_height()
341                                 );
342                 } else {
343                         _body->queue_draw_area (
344                                 component_to_parent_x (port_name_x()),
345                                 component_to_parent_y (channel_y (bc)),
346                                 _longest_port_name + name_pad() * 2,
347                                 row_height()
348                                 );
349                 }
350         }
351
352 }
353
354 void
355 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const &)
356 {
357         clear_channel_highlights ();
358         if (_body->mouseover().row.bundle) {
359                 add_channel_highlight (_body->mouseover().row);
360         }
361 }