Reduce header dependencies.
[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() && (*i)->ports.empty()) ) {
138                         continue;
139                 }
140
141                 /* compute width of this group */
142                 uint32_t w = 0;
143                 for (ARDOUR::BundleList::const_iterator j = (*i)->bundles().begin(); j != (*i)->bundles().end(); ++j) {
144                         w += (*j)->nchannels() * column_width();
145                 }
146                 w += (*i)->ports.size() * column_width();
147
148                 /* rectangle */
149                 set_source_rgb (cr, get_a_group_colour (g));
150                 double const rh = _highest_group_name + 2 * name_pad();
151                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
152                         cairo_rectangle (cr, x, 0, w, rh);
153                 } else {
154                         cairo_rectangle (cr, x, _height - rh, w, rh);
155                 }
156                 cairo_fill (cr);
157                 
158                 std::pair<std::string, double> const display = display_port_name (cr, (*i)->name, w);
159
160                 /* plot it */
161                 set_source_rgb (cr, text_colour());
162                 cairo_move_to (cr, x + (w - display.second) / 2, y);
163                 cairo_show_text (cr, display.first.c_str());
164
165                 x += w;
166                 ++g;
167         }
168
169         /* BUNDLE PARALLELOGRAM-TYPE-THING AND NAME */
170
171         x = 0;
172         ARDOUR::BundleList const c = _matrix->columns()->bundles();
173         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
174
175                 Gdk::Color colour = get_a_bundle_colour (i - c.begin ());
176                 set_source_rgb (cr, colour);
177
178                 double const w = (*i)->nchannels() * column_width();
179
180                 double x_ = x;
181                 
182                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
183                         y = _height;
184                 } else {
185                         y = slanted_height();
186                 }
187
188                 double y_ = y;
189                 cairo_move_to (cr, x_, y_);
190                 x_ += w;
191                 cairo_line_to (cr, x_, y_);
192                 x_ += slanted_height() / tan (angle ());
193                 y_ -= slanted_height();
194                 cairo_line_to (cr, x_, y_);
195                 x_ -= w;
196                 cairo_line_to (cr, x_, y_);
197                 cairo_line_to (cr, x, y);
198                 cairo_fill_preserve (cr);
199                 set_source_rgb (cr, background_colour());
200                 cairo_set_line_width (cr, label_border_width());
201                 cairo_stroke (cr);
202
203                 set_source_rgb (cr, text_colour());
204
205                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
206                         
207                         double const rl = 3 * name_pad() + _longest_channel_name;
208                         cairo_move_to (
209                                 cr,
210                                 x + basic_text_x_pos (0) + rl * cos (angle()),
211                                 _height - rl * sin (angle())
212                                 );
213                         
214                 } else {
215
216                         cairo_move_to (
217                                 cr,
218                                 x + basic_text_x_pos (0),
219                                 slanted_height() - name_pad() * sin (angle())
220                                 );
221                 }
222                         
223                 cairo_save (cr);
224                 cairo_rotate (cr, -angle());
225                 cairo_show_text (cr, (*i)->name().c_str());
226                 cairo_restore (cr);
227                 
228                 x += (*i)->nchannels () * column_width();
229         }
230         
231
232         /* PORT NAMES */
233
234         x = 0;
235         for (ARDOUR::BundleList::const_iterator i = c.begin (); i != c.end(); ++i) {
236                 
237                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
238
239                         render_channel_name (cr, get_a_bundle_colour (i - c.begin()), x, 0, ARDOUR::BundleChannel (*i, j));
240                         x += column_width();
241                 }
242         }
243 }
244
245 double
246 PortMatrixColumnLabels::component_to_parent_x (double x) const
247 {
248         return x - _body->xoffset() + _parent_rectangle.get_x();
249 }
250
251 double
252 PortMatrixColumnLabels::parent_to_component_x (double x) const
253 {
254         return x + _body->xoffset() - _parent_rectangle.get_x();
255 }
256
257 double
258 PortMatrixColumnLabels::component_to_parent_y (double y) const
259 {
260         return y + _parent_rectangle.get_y();
261 }
262
263 double
264 PortMatrixColumnLabels::parent_to_component_y (double y) const
265 {
266         return y - _parent_rectangle.get_y();
267 }
268
269 void
270 PortMatrixColumnLabels::mouseover_changed (PortMatrixNode const &)
271 {
272         clear_channel_highlights ();
273         if (_body->mouseover().column.bundle) {
274                 add_channel_highlight (_body->mouseover().column);
275         }
276 }
277
278 std::vector<std::pair<double, double> >
279 PortMatrixColumnLabels::port_name_shape (double xoff, double yoff) const
280 {
281         std::vector<std::pair<double, double> > shape;
282         
283         double const lc = _longest_channel_name + name_pad();
284         double const w = column_width();
285         
286         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
287
288                 double x_ = xoff + slanted_height() / tan (angle()) + w;
289                 double y_ = yoff;
290                 shape.push_back (std::make_pair (x_, y_));
291                 x_ -= w;
292                 shape.push_back (std::make_pair (x_, y_));
293                 x_ -= lc * cos (angle());
294                 y_ += lc * sin (angle());
295                 shape.push_back (std::make_pair (x_, y_));
296                 x_ += w * pow (sin (angle()), 2);
297                 y_ += w * sin (angle()) * cos (angle());
298                 shape.push_back (std::make_pair (x_, y_));
299                 
300         } else {
301                 
302                 double x_ = xoff;
303                 double y_ = yoff + _height;
304                 shape.push_back (std::make_pair (x_, y_));
305                 x_ += w;
306                 shape.push_back (std::make_pair (x_, y_));
307                 x_ += lc * cos (angle());
308                 y_ -= lc * sin (angle());
309                 shape.push_back (std::make_pair (x_, y_));
310                 x_ -= column_width() * pow (sin (angle()), 2);
311                 y_ -= column_width() * sin (angle()) * cos (angle());
312                 shape.push_back (std::make_pair (x_, y_));
313         }
314
315         return shape;
316 }
317
318 void
319 PortMatrixColumnLabels::render_channel_name (cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const &bc)
320 {
321         std::vector<std::pair<double, double> > const shape = port_name_shape (xoff, yoff);
322
323         cairo_move_to (cr, shape[0].first, shape[0].second);
324         for (uint32_t i = 1; i < 4; ++i) {
325                 cairo_line_to (cr, shape[i].first, shape[i].second);
326         }
327         cairo_line_to (cr, shape[0].first, shape[0].second);
328         
329         set_source_rgb (cr, colour);
330         cairo_fill_preserve (cr);
331         set_source_rgb (cr, background_colour());
332         cairo_set_line_width (cr, label_border_width());
333         cairo_stroke (cr);
334         
335         set_source_rgb (cr, text_colour());
336         
337         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
338
339                 cairo_move_to (
340                         cr,
341                         xoff + basic_text_x_pos(bc.channel),
342                         yoff + _height - name_pad() * sin (angle())
343                         );
344                 
345         } else { 
346
347                 double const rl = 3 * name_pad() + _longest_bundle_name;
348                 cairo_move_to (
349                         cr,
350                         xoff + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
351                         yoff + slanted_height() - rl * sin (angle())
352                         );
353         }
354         
355         cairo_save (cr);
356         cairo_rotate (cr, -angle());
357         
358         cairo_show_text (
359                 cr,
360                 bc.bundle->channel_name(bc.channel).c_str()
361                 );
362         
363         cairo_restore (cr);
364 }
365
366 double
367 PortMatrixColumnLabels::channel_x (ARDOUR::BundleChannel const &bc) const
368 {
369         uint32_t n = 0;
370
371         ARDOUR::BundleList::const_iterator i = _matrix->columns()->bundles().begin();
372         while (i != _matrix->columns()->bundles().end() && *i != bc.bundle) {
373                 n += (*i)->nchannels ();
374                 ++i;
375         }
376
377         n += bc.channel;
378         return n * column_width();
379 }
380
381 double
382 PortMatrixColumnLabels::channel_y (ARDOUR::BundleChannel const &bc) const
383 {
384         return 0;
385 }
386
387 void
388 PortMatrixColumnLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
389 {
390         if (bc.bundle) {
391                 
392                 double const x = channel_x (bc);
393                 double const lc = _longest_channel_name + name_pad();
394                 double const h = lc * sin (angle ()) + column_width() * sin (angle()) * cos (angle());
395                 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
396
397                         _body->queue_draw_area (
398                                 component_to_parent_x (x),
399                                 component_to_parent_y (_height - h),
400                                 column_width() + lc * cos (angle()),
401                                 h
402                                 );
403                         
404                 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
405                         
406                         double const x_ = x + slanted_height() / tan (angle()) - lc * cos (angle());
407                         
408                         _body->queue_draw_area (
409                                 component_to_parent_x (x_),
410                                 component_to_parent_y (0),
411                                 column_width() + lc * cos (angle()),
412                                 h
413                                 );
414                         
415                 }
416                         
417                                 
418         }
419 }
420
421 void
422 PortMatrixColumnLabels::button_press (double x, double y, int b, uint32_t t)
423 {
424         uint32_t N = _matrix->columns()->total_visible_ports ();
425         uint32_t i = 0;
426         for (; i < N; ++i) {
427                 
428                 std::vector<std::pair<double, double> > const shape = port_name_shape (i * column_width(), 0);
429
430                 uint32_t j = 0;
431                 for (; j < 4; ++j) {
432                         uint32_t k = (j + 1) % 4;
433
434                         double const P = (y - shape[j].second) * (shape[k].first - shape[j].first) -
435                                 (x - shape[j].first) * (shape[k].second - shape[j].second);
436
437                         if (P > 0) {
438                                 break;
439                         }
440                 }
441
442                 if (j == 4) {
443                         break;
444                 }
445         }
446
447         if (i == N) {
448                 return;
449         }
450         
451         switch (b) {
452         case 1:
453                 _body->highlight_associated_channels (_matrix->column_index(), i);
454                 break;
455         case 3:
456                 maybe_popup_context_menu (i, t);
457                 break;
458         }
459 }
460
461
462 void
463 PortMatrixColumnLabels::maybe_popup_context_menu (int i, uint32_t t)
464 {
465         if (!_matrix->can_rename_channels (_matrix->column_index()) &&
466             !_matrix->can_remove_channels (_matrix->column_index())) {
467                 return;
468         }
469
470         _matrix->popup_channel_context_menu (_matrix->column_index(), i, t);
471 }