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