Keep port matrix group labels on screen where possible.
[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 using namespace std;
31
32 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
33         : PortMatrixLabels (m, b)
34 {
35
36 }
37
38 void
39 PortMatrixRowLabels::compute_dimensions ()
40 {
41         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
42         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
43         cairo_t* cr = gdk_cairo_create (pm);
44
45         _longest_port_name = 0;
46         _longest_bundle_name = 0;
47         _height = 0;
48         _highest_group_name = 0;
49
50         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
51
52                 PortGroup::BundleList const r = (*i)->bundles ();
53                 for (PortGroup::BundleList::const_iterator j = r.begin(); j != r.end(); ++j) {
54
55                         for (uint32_t k = 0; k < j->bundle->nchannels(); ++k) {
56                                 cairo_text_extents_t ext;
57                                 cairo_text_extents (cr, j->bundle->channel_name(k).c_str(), &ext);
58                                 if (ext.width > _longest_port_name) {
59                                         _longest_port_name = ext.width;
60                                 }
61                         }
62
63                         cairo_text_extents_t ext;
64                         cairo_text_extents (cr, j->bundle->name().c_str(), &ext);
65                         if (ext.width > _longest_bundle_name) {
66                                 _longest_bundle_name = ext.width;
67                         }
68                 }
69
70                 _height += group_size (*i) * grid_spacing ();
71
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         cairo_destroy (cr);
80         gdk_pixmap_unref (pm);
81
82         _width = _highest_group_name +
83                 _longest_bundle_name +
84                 name_pad() * 4;
85
86         if (!_matrix->show_only_bundles()) {
87                 _width += _longest_port_name;
88                 _width += name_pad() * 2;
89         }
90 }
91
92
93 void
94 PortMatrixRowLabels::render (cairo_t* cr)
95 {
96         /* BACKGROUND */
97
98         set_source_rgb (cr, background_colour());
99         cairo_rectangle (cr, 0, 0, _width, _height);
100         cairo_fill (cr);
101
102         /* BUNDLE AND PORT NAMES */
103
104         double y = 0;
105         int N = 0;
106         int M = 0;
107         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
108
109                 if ((*i)->visible ()) {
110
111                         PortGroup::BundleList const & bundles = (*i)->bundles ();
112                         for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
113                                 render_bundle_name (cr, background_colour (), j->has_colour ? j->colour : get_a_bundle_colour (N), 0, y, j->bundle);
114
115                                 if (!_matrix->show_only_bundles()) {
116                                         for (uint32_t k = 0; k < j->bundle->nchannels(); ++k) {
117                                                 Gdk::Color c = j->has_colour ? j->colour : get_a_bundle_colour (M);
118                                                 render_channel_name (cr, background_colour (), c, 0, y, ARDOUR::BundleChannel (j->bundle, k));
119                                                 y += grid_spacing();
120                                                 ++M;
121                                         }
122                                 } else {
123                                         y += grid_spacing();
124                                 }
125
126                                 ++N;
127                         }
128
129                 } else {
130
131                         y += grid_spacing ();
132                 }
133         }
134 }
135
136 void
137 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
138 {
139         uint32_t const gw = (_highest_group_name + 2 * name_pad());
140
141         pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> w = position_to_group_and_channel (y / grid_spacing (), _matrix->rows());
142
143         if (
144                 (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x < gw) ||
145                 (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x > (_width - gw))
146                 ) {
147
148                 w.second.bundle.reset ();
149         }
150
151         if (b == 1) {
152
153                 if (w.second.bundle) {
154                         _body->highlight_associated_channels (_matrix->row_index(), w.second);
155                 } else {
156                         if (w.first) {
157                                 w.first->set_visible (!w.first->visible());
158                         }
159                 }
160
161         } else if (b == 3) {
162
163                 _matrix->popup_menu (
164                         make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
165                         w,
166                         t
167                         );
168         }
169 }
170
171 double
172 PortMatrixRowLabels::component_to_parent_x (double x) const
173 {
174         /* Row labels don't scroll horizontally, so x conversion does not depend on xoffset */
175         return x + _parent_rectangle.get_x();
176 }
177
178 double
179 PortMatrixRowLabels::parent_to_component_x (double x) const
180 {
181         /* Row labels don't scroll horizontally, so x conversion does not depend on xoffset */
182         return x - _parent_rectangle.get_x();
183 }
184
185 double
186 PortMatrixRowLabels::component_to_parent_y (double y) const
187 {
188         return y - _body->yoffset() + _parent_rectangle.get_y();
189 }
190
191 double
192 PortMatrixRowLabels::parent_to_component_y (double y) const
193 {
194         return y + _body->yoffset() - _parent_rectangle.get_y();
195 }
196
197
198 double
199 PortMatrixRowLabels::bundle_name_x () const
200 {
201         double x = 0;
202
203         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
204                 x = _highest_group_name + 2 * name_pad();
205         } else {
206                 if (_matrix->show_only_bundles()) {
207                         x = 0;
208                 } else {
209                         x = _longest_port_name + name_pad() * 2;
210                 }
211         }
212
213         return x;
214 }
215
216 double
217 PortMatrixRowLabels::port_name_x () const
218 {
219         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
220                 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
221         } else {
222                 return 0;
223         }
224
225         return 0;
226 }
227
228 void
229 PortMatrixRowLabels::render_bundle_name (
230         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
231         )
232 {
233         double const x = bundle_name_x ();
234
235         int const n = _matrix->show_only_bundles() ? 1 : b->nchannels();
236         set_source_rgb (cr, bg_colour);
237         cairo_rectangle (cr, xoff + x, yoff, _longest_bundle_name + name_pad() * 2, grid_spacing() * n);
238         cairo_fill_preserve (cr);
239         set_source_rgb (cr, fg_colour);
240         cairo_set_line_width (cr, label_border_width ());
241         cairo_stroke (cr);
242
243         cairo_text_extents_t ext;
244         cairo_text_extents (cr, b->name().c_str(), &ext);
245         double const off = (grid_spacing() - ext.height) / 2;
246
247         set_source_rgb (cr, text_colour());
248         cairo_move_to (cr, xoff + x + name_pad(), yoff + name_pad() + off);
249         cairo_show_text (cr, b->name().c_str());
250 }
251
252 void
253 PortMatrixRowLabels::render_channel_name (
254         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
255         )
256 {
257         set_source_rgb (cr, bg_colour);
258         cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, grid_spacing());
259         cairo_fill_preserve (cr);
260         set_source_rgb (cr, fg_colour);
261         cairo_set_line_width (cr, label_border_width ());
262         cairo_stroke (cr);
263
264         cairo_text_extents_t ext;
265         cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
266         double const off = (grid_spacing() - ext.height) / 2;
267
268         if (bc.bundle->nchannels() > 1) {
269
270                 /* only plot the name if the bundle has more than one channel;
271                    the name of a single channel is assumed to be redundant */
272                 
273                 set_source_rgb (cr, text_colour());
274                 cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
275                 cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
276         }
277 }
278
279 double
280 PortMatrixRowLabels::channel_x (ARDOUR::BundleChannel const &) const
281 {
282         return 0;
283 }
284
285 double
286 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
287 {
288         return channel_to_position (bc, _matrix->rows()) * grid_spacing ();
289 }
290
291 void
292 PortMatrixRowLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
293 {
294         if (bc.bundle) {
295
296                 if (_matrix->show_only_bundles()) {
297                         _body->queue_draw_area (
298                                 component_to_parent_x (bundle_name_x()) - 1,
299                                 component_to_parent_y (channel_y (bc)) - 1,
300                                 _longest_bundle_name + name_pad() * 2 + 2,
301                                 grid_spacing() + 2
302                                 );
303                 } else {
304                         _body->queue_draw_area (
305                                 component_to_parent_x (port_name_x()) - 1,
306                                 component_to_parent_y (channel_y (bc)) - 1,
307                                 _longest_port_name + name_pad() * 2 + 2,
308                                 grid_spacing() + 2
309                                 );
310                 }
311         }
312
313 }
314
315 void
316 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const &)
317 {
318         clear_channel_highlights ();
319         if (_body->mouseover().column.bundle && _body->mouseover().row.bundle) {
320                 add_channel_highlight (_body->mouseover().row);
321         }
322 }
323
324 void
325 PortMatrixRowLabels::draw_extra (cairo_t* cr)
326 {
327         PortMatrixLabels::draw_extra (cr);
328         
329         /* PORT GROUP NAMES */
330
331         double x = 0;
332         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
333                 x = component_to_parent_x (0);
334         } else {
335                 x = component_to_parent_x (_width - _highest_group_name - 2 * name_pad());
336         }
337
338         double y = component_to_parent_y (0);
339         int g = 0;
340         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
341
342                 /* compute height of this group */
343                 double h = 0;
344                 if (!(*i)->visible()) {
345                         h = grid_spacing ();
346                 } else {
347                         if (_matrix->show_only_bundles()) {
348                                 h = (*i)->bundles().size() * grid_spacing();
349                         } else {
350                                 h = (*i)->total_channels () * grid_spacing();
351                         }
352                 }
353
354                 if (h == 0) {
355                         continue;
356                 }
357
358                 /* rectangle */
359                 set_source_rgb (cr, get_a_group_colour (g));
360                 double const rw = _highest_group_name + 2 * name_pad();
361                 cairo_rectangle (cr, x, y, rw, h);
362                 cairo_fill (cr);
363
364                 /* y area available to draw the label in (trying to keep it visible) */
365                 double const ty = max (y, 0.0);
366                 double const by = min (y + h, double (_body->alloc_scroll_height ()));
367
368                 /* hence what abbreviation (or not) we need for the group name */
369                 string const upper = Glib::ustring ((*i)->name).uppercase ();
370                 pair<string, double> display = fit_to_pixels (cr, upper, by - ty);
371
372                 /* plot it */
373                 set_source_rgb (cr, text_colour());
374                 cairo_move_to (cr, x + rw - name_pad(), (by + ty + display.second) / 2);
375                 cairo_save (cr);
376                 cairo_rotate (cr, - M_PI / 2);
377                 cairo_show_text (cr, display.first.c_str());
378                 cairo_restore (cr);
379
380                 y += h;
381                 ++g;
382         }
383 }