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