First cut at mouseovers for the port matrix.
[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 (PortMatrixBody* b, Location l)
27         : PortMatrixComponent (b), _location (l)
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 = _body->column_ports().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::const_iterator i = _body->column_ports().begin(); i != _body->column_ports().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 (_location == TOP) {
126                 x = (_height - _highest_group_name - 2 * name_pad()) / 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::const_iterator i = _body->column_ports().begin(); i != _body->column_ports().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 (_location == TOP) {
151                         cairo_rectangle (cr, x, 0, w, rh);
152                 } else if (_location == BOTTOM) {
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 = _body->column_ports().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                 double const ph = _height - _highest_group_name - 2 * name_pad();
179
180                 double x_ = x;
181                 
182                 if (_location == TOP) {
183                         y = _height;
184                 } else if (_location == BOTTOM) {
185                         y = ph;
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_ += ph / tan (angle ());
193                 y_ -= ph;
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 (_location == TOP) {
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 if (_location == BOTTOM) {
215
216                         cairo_move_to (
217                                 cr,
218                                 x + basic_text_x_pos (0),
219                                 ph - 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_port_name (cr, get_a_bundle_colour (i - c.begin()), x, 0, PortMatrixBundleChannel (*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& old)
271 {
272         queue_draw_for (old);
273         queue_draw_for (_body->mouseover());
274 }
275
276 void
277 PortMatrixColumnLabels::draw_extra (cairo_t* cr)
278 {
279         if (_body->mouseover().column.bundle) {
280                 render_port_name (
281                         cr,
282                         mouseover_port_colour (),
283                         component_to_parent_x (channel_x (_body->mouseover().column)),
284                         component_to_parent_y (0),
285                         _body->mouseover().column
286                         );
287         }
288
289 }
290
291 void
292 PortMatrixColumnLabels::render_port_name (cairo_t* cr, Gdk::Color colour, double x, double y, PortMatrixBundleChannel const &bc)
293 {
294         double const lc = _longest_channel_name + name_pad();
295         double const w = column_width();
296         double const ph = _height - _highest_group_name - 2 * name_pad();
297
298         if (_location == BOTTOM) {
299
300                 double x_ = x + ph / tan (angle()) + w;
301                 double const ix = x_;
302                 double y_ = y;
303                 cairo_move_to (cr, x_, y_);
304                 x_ -= w;
305                 cairo_line_to (cr, x_, y_);
306                 x_ -= lc * cos (angle());
307                 y_ += lc * sin (angle());
308                 cairo_line_to (cr, x_, y_);
309                 x_ += w * pow (sin (angle()), 2);
310                 y_ += w * sin (angle()) * cos (angle());
311                 cairo_line_to (cr, x_, y_);
312                 cairo_line_to (cr, ix, y);
313                 
314         } else if (_location == TOP) {
315                 
316                 double x_ = x;
317                 double y_ = y + _height;
318                 cairo_move_to (cr, x_, y_);
319                 x_ += w;
320                 cairo_line_to (cr, x_, y_);
321                 x_ += lc * cos (angle());
322                 y_ -= lc * sin (angle());
323                 cairo_line_to (cr, x_, y_);
324                 x_ -= column_width() * pow (sin (angle()), 2);
325                 y_ -= column_width() * sin (angle()) * cos (angle());
326                 cairo_line_to (cr, x_, y_);
327                 cairo_line_to (cr, x, y + _height);
328                 
329         }
330         
331         set_source_rgb (cr, colour);
332         cairo_fill_preserve (cr);
333         set_source_rgb (cr, background_colour());
334         cairo_set_line_width (cr, label_border_width());
335         cairo_stroke (cr);
336         
337         set_source_rgb (cr, text_colour());
338         
339         if (_location == TOP) {
340
341                 cairo_move_to (
342                         cr,
343                         x + basic_text_x_pos(bc.channel),
344                         y + _height - name_pad() * sin (angle())
345                         );
346                 
347         } else if (_location == BOTTOM) {
348
349                 double const rl = 3 * name_pad() + _longest_bundle_name;
350                 cairo_move_to (
351                         cr,
352                         x + basic_text_x_pos(bc.channel) + rl * cos (angle ()),
353                         y + ph - rl * sin (angle())
354                         );
355         }
356         
357         cairo_save (cr);
358         cairo_rotate (cr, -angle());
359         
360         cairo_show_text (
361                 cr,
362                 bc.bundle->channel_name(bc.channel).c_str()
363                 );
364         
365         cairo_restore (cr);
366 }
367
368 double
369 PortMatrixColumnLabels::channel_x (PortMatrixBundleChannel const &bc) const
370 {
371         double x = 0;
372
373         ARDOUR::BundleList::const_iterator i = _body->column_ports().bundles().begin();
374         while (i != _body->column_ports().bundles().end() && *i != bc.bundle) {
375                 x += column_width() * (*i)->nchannels();
376                 ++i;
377         }
378
379         x += column_width() * bc.channel;
380
381         return x;
382 }
383
384 void
385 PortMatrixColumnLabels::queue_draw_for (PortMatrixNode const& n)
386 {
387         if (n.column.bundle) {
388                 
389                 double const x = channel_x (n.column);
390                 double const lc = _longest_channel_name + name_pad();
391                 double const h = lc * sin (angle ()) + column_width() * sin (angle()) * cos (angle());
392                 if (_location == TOP) {
393
394                         _body->queue_draw_area (
395                                 component_to_parent_x (x),
396                                 component_to_parent_y (_height - h),
397                                 column_width() + lc * cos (angle()),
398                                 h
399                                 );
400                         
401                 } else if (_location == BOTTOM) {
402                         
403                         double const ph = _height - _highest_group_name - 2 * name_pad();
404                         double const w = column_width() + lc * cos (angle());
405                         double const x_ = x + ph / 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 }