Fixes to bundle manager to make it vaguely usable.
[ardour.git] / gtk2_ardour / port_matrix_column_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 "ardour/bundle.h"
22 #include "ardour/types.h"
23 #include "port_matrix_column_labels.h"
24 #include "port_matrix.h"
25 #include "port_matrix_body.h"
26
27 PortMatrixColumnLabels::PortMatrixColumnLabels (PortMatrix* m, PortMatrixBody* b)
28         : PortMatrixLabels (m, b)
29 {
30
31 }
32
33 void
34 PortMatrixColumnLabels::compute_dimensions ()
35 {
36         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
37         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
38         cairo_t* cr = gdk_cairo_create (pm);
39
40         /* width of the longest bundle name */
41         _longest_bundle_name = 0;
42         /* width of the longest channel name */
43         _longest_channel_name = 0;
44         /* height of highest bit of text (apart from group names) */
45         _highest_text = 0;
46         /* width of the whole thing */
47         _width = 0;
48
49         ARDOUR::BundleList const c = _matrix->columns()->bundles();
50         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
51
52                 cairo_text_extents_t ext;
53                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
54                 if (ext.width > _longest_bundle_name) {
55                         _longest_bundle_name = ext.width;
56                 }
57                 if (ext.height > _highest_text) {
58                         _highest_text = ext.height;
59                 }
60
61                 for (uint32_t j = 0; j < (*i)->nchannels (); ++j) {
62                         
63                         cairo_text_extents (
64                                 cr,
65                                 (*i)->channel_name (j).c_str(),
66                                 &ext
67                                 );
68                         
69                         if (ext.width > _longest_channel_name) {
70                                 _longest_channel_name = ext.width;
71                         }
72                         if (ext.height > _highest_text) {
73                                 _highest_text = ext.height;
74                         }
75                 }
76
77                 _width += (*i)->nchannels() * column_width();
78         }
79
80         _highest_group_name = 0;
81         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
82                 if ((*i)->visible()) {
83                         cairo_text_extents_t ext;
84                         cairo_text_extents (cr, (*i)->name.c_str(), &ext);
85                         if (ext.height > _highest_group_name) {
86                                 _highest_group_name = ext.height;
87                         }
88                 }
89         }
90
91         cairo_destroy (cr);
92         gdk_pixmap_unref (pm);
93
94         /* height of the whole thing */
95
96         double const parallelogram_height = 
97                 (_longest_bundle_name + _longest_channel_name + 4 * name_pad()) * sin (angle())
98                 + _highest_text * cos (angle());
99
100         _height = parallelogram_height + _highest_group_name + 2 * name_pad();
101
102         _width += parallelogram_height / tan (angle ());
103 }
104
105 double
106 PortMatrixColumnLabels::basic_text_x_pos (int c) const
107 {
108         return column_width() / 2 +
109                 _highest_text / (2 * sin (angle ()));
110 }
111
112 void
113 PortMatrixColumnLabels::render (cairo_t* cr)
114 {
115         /* BACKGROUND */
116
117         set_source_rgb (cr, background_colour());
118         cairo_rectangle (cr, 0, 0, _width, _height);
119         cairo_fill (cr);
120
121         /* PORT GROUP NAME */
122         
123         double x = 0;
124         double y = 0;
125         
126         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
127                 x = slanted_height() / tan (angle());
128                 y = _highest_group_name + name_pad();
129         } else {
130                 x = 0;
131                 y = _height - name_pad();
132         }
133
134         int g = 0;
135         for (PortGroupList::List::const_iterator i = _matrix->columns()->begin(); i != _matrix->columns()->end(); ++i) {
136
137                 if (!(*i)->visible() || (*i)->bundles().empty()) {
138                         continue;
139                 }
140
141                 /* compute width of this group */
142                 uint32_t w = (*i)->total_channels() * column_width();
143
144                 /* rectangle */
145                 set_source_rgb (cr, get_a_group_colour (g));
146                 double const rh = _highest_group_name + 2 * name_pad();
147                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
148                         cairo_rectangle (cr, x, 0, w, rh);
149                 } else {
150                         cairo_rectangle (cr, x, _height - rh, w, rh);
151                 }
152                 cairo_fill (cr);
153                 
154                 std::pair<std::string, double> const display = display_port_name (cr, (*i)->name, w);
155
156                 /* plot it */
157                 set_source_rgb (cr, text_colour());
158                 cairo_move_to (cr, x + (w - display.second) / 2, y);
159                 cairo_show_text (cr, display.first.c_str());
160
161                 x += w;
162                 ++g;
163         }
164
165         /* BUNDLE PARALLELOGRAM-TYPE-THING AND NAME */
166
167         x = 0;
168         ARDOUR::BundleList const c = _matrix->columns()->bundles();
169         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
170
171                 Gdk::Color colour = get_a_bundle_colour (i - c.begin ());
172                 set_source_rgb (cr, colour);
173
174                 double const w = (*i)->nchannels() * column_width();
175
176                 double x_ = x;
177                 
178                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
179                         y = _height;
180                 } else {
181                         y = slanted_height();
182                 }
183
184                 double y_ = y;
185                 cairo_move_to (cr, x_, y_);
186                 x_ += w;
187                 cairo_line_to (cr, x_, y_);
188                 x_ += slanted_height() / tan (angle ());
189                 y_ -= slanted_height();
190                 cairo_line_to (cr, x_, y_);
191                 x_ -= w;
192                 cairo_line_to (cr, x_, y_);
193                 cairo_line_to (cr, x, y);
194                 cairo_fill_preserve (cr);
195                 set_source_rgb (cr, background_colour());
196                 cairo_set_line_width (cr, label_border_width());
197                 cairo_stroke (cr);
198
199                 set_source_rgb (cr, text_colour());
200
201                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
202                         
203                         double const rl = 3 * name_pad() + _longest_channel_name;
204                         cairo_move_to (
205                                 cr,
206                                 x + basic_text_x_pos (0) + rl * cos (angle()),
207                                 _height - rl * sin (angle())
208                                 );
209                         
210                 } else {
211
212                         cairo_move_to (
213                                 cr,
214                                 x + basic_text_x_pos (0),
215                                 slanted_height() - name_pad() * sin (angle())
216                                 );
217                 }
218                         
219                 cairo_save (cr);
220                 cairo_rotate (cr, -angle());
221                 cairo_show_text (cr, (*i)->name().c_str());
222                 cairo_restore (cr);
223                 
224                 x += (*i)->nchannels () * column_width();
225         }
226         
227
228         /* PORT NAMES */
229
230         x = 0;
231         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
232                 
233                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
234
235                         render_channel_name (cr, get_a_bundle_colour (i - c.begin()), x, 0, ARDOUR::BundleChannel (*i, j));
236                         x += column_width();
237                 }
238         }
239 }
240
241 double
242 PortMatrixColumnLabels::component_to_parent_x (double x) const
243 {
244         return x - _body->xoffset() + _parent_rectangle.get_x();
245 }
246
247 double
248 PortMatrixColumnLabels::parent_to_component_x (double x) const
249 {
250         return x + _body->xoffset() - _parent_rectangle.get_x();
251 }
252
253 double
254 PortMatrixColumnLabels::component_to_parent_y (double y) const
255 {
256         return y + _parent_rectangle.get_y();
257 }
258
259 double
260 PortMatrixColumnLabels::parent_to_component_y (double y) const
261 {
262         return y - _parent_rectangle.get_y();
263 }
264
265 void
266 PortMatrixColumnLabels::mouseover_changed (PortMatrixNode const &)
267 {
268         clear_channel_highlights ();
269         if (_body->mouseover().column.bundle) {
270                 add_channel_highlight (_body->mouseover().column);
271         }
272 }
273
274 std::vector<std::pair<double, double> >
275 PortMatrixColumnLabels::port_name_shape (double xoff, double yoff) const
276 {
277         std::vector<std::pair<double, double> > shape;
278         
279         double const lc = _longest_channel_name + name_pad();
280         double const w = column_width();
281         
282         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
283
284                 double x_ = xoff + slanted_height() / tan (angle()) + w;
285                 double y_ = yoff;
286                 shape.push_back (std::make_pair (x_, y_));
287                 x_ -= w;
288                 shape.push_back (std::make_pair (x_, y_));
289                 x_ -= lc * cos (angle());
290                 y_ += lc * sin (angle());
291                 shape.push_back (std::make_pair (x_, y_));
292                 x_ += w * pow (sin (angle()), 2);
293                 y_ += w * sin (angle()) * cos (angle());
294                 shape.push_back (std::make_pair (x_, y_));
295                 
296         } else {
297                 
298                 double x_ = xoff;
299                 double y_ = yoff + _height;
300                 shape.push_back (std::make_pair (x_, y_));
301                 x_ += w;
302                 shape.push_back (std::make_pair (x_, y_));
303                 x_ += lc * cos (angle());
304                 y_ -= lc * sin (angle());
305                 shape.push_back (std::make_pair (x_, y_));
306                 x_ -= column_width() * pow (sin (angle()), 2);
307                 y_ -= column_width() * sin (angle()) * cos (angle());
308                 shape.push_back (std::make_pair (x_, y_));
309         }
310
311         return shape;
312 }
313
314 void
315 PortMatrixColumnLabels::render_channel_name (cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const &bc)
316 {
317         std::vector<std::pair<double, double> > const shape = port_name_shape (xoff, yoff);
318
319         cairo_move_to (cr, shape[0].first, shape[0].second);
320         for (uint32_t i = 1; i < 4; ++i) {
321                 cairo_line_to (cr, shape[i].first, shape[i].second);
322         }
323         cairo_line_to (cr, shape[0].first, shape[0].second);
324         
325         set_source_rgb (cr, colour);
326         cairo_fill_preserve (cr);
327         set_source_rgb (cr, background_colour());
328         cairo_set_line_width (cr, label_border_width());
329         cairo_stroke (cr);
330         
331         set_source_rgb (cr, text_colour());
332         
333         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
334
335                 cairo_move_to (
336                         cr,
337                         xoff + basic_text_x_pos(bc.channel),
338                         yoff + _height - name_pad() * sin (angle())
339                         );
340                 
341         } else { 
342
343                 double const rl = 3 * name_pad() + _longest_bundle_name;
344                 cairo_move_to (
345                         cr,
346                         xoff + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
347                         yoff + slanted_height() - rl * sin (angle())
348                         );
349         }
350         
351         cairo_save (cr);
352         cairo_rotate (cr, -angle());
353         
354         cairo_show_text (
355                 cr,
356                 bc.bundle->channel_name(bc.channel).c_str()
357                 );
358         
359         cairo_restore (cr);
360 }
361
362 double
363 PortMatrixColumnLabels::channel_x (ARDOUR::BundleChannel const &bc) const
364 {
365         uint32_t n = 0;
366
367         ARDOUR::BundleList::const_iterator i = _matrix->columns()->bundles().begin();
368         while (i != _matrix->columns()->bundles().end() && *i != bc.bundle) {
369                 n += (*i)->nchannels ();
370                 ++i;
371         }
372
373         n += bc.channel;
374         return n * column_width();
375 }
376
377 double
378 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &bc) const
379 {
380         return 0;
381 }
382
383 void
384 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
385 {
386         if (bc.bundle) {
387                 
388                 double const x = channel_x (bc);
389                 double const lc = _longest_channel_name + name_pad();
390                 double const h = lc * sin (angle ()) + column_width() * sin (angle()) * cos (angle());
391                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
392
393                         _body->queue_draw_area (
394                                 component_to_parent_x (x),
395                                 component_to_parent_y (_height - h),
396                                 column_width() + lc * cos (angle()),
397                                 h
398                                 );
399                         
400                 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
401                         
402                         double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
403                         
404                         _body->queue_draw_area (
405                                 component_to_parent_x (x_),
406                                 component_to_parent_y (0),
407                                 column_width() + lc * cos (angle()),
408                                 h
409                                 );
410                         
411                 }
412                 
413         }
414 }
415
416 void
417 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
418 {
419         uint32_t N = _matrix->columns()->total_visible_channels ();
420         uint32_t i = 0;
421         for (; i < N; ++i) {
422                 
423                 std::vector<std::pair<double, double> > const shape = port_name_shape (i * column_width(), 0);
424
425                 uint32_t j = 0;
426                 for (; j < 4; ++j) {
427                         uint32_t k = (j + 1) % 4;
428
429                         double const P = (y - shape[j].second) * (shape[k].first - shape[j].first) -
430                                 (x - shape[j].first) * (shape[k].second - shape[j].second);
431
432                         if (P > 0) {
433                                 break;
434                         }
435                 }
436
437                 if (j == 4) {
438                         break;
439                 }
440         }
441
442         if (i == N) {
443                 return;
444         }
445         
446         switch (b) {
447         case 1:
448                 _body->highlight_associated_channels (_matrix->column_index(), i);
449                 break;
450         case 3:
451                 _matrix->popup_channel_context_menu (_matrix->column_index(), i, t);
452                 break;
453         }
454 }
455