Outline highlighted labels in red.
[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         /* PORT GROUP NAMES */
103
104         double x = 0;
105         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
106                 x = 0;
107         } else {
108                 x = _width - _highest_group_name - 2 * name_pad();
109         }
110
111         double y = 0;
112         int g = 0;
113         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
114
115                 /* compute height of this group */
116                 double h = 0;
117                 if (!(*i)->visible()) {
118                         h = grid_spacing ();
119                 } else {
120                         if (_matrix->show_only_bundles()) {
121                                 h = (*i)->bundles().size() * grid_spacing();
122                         } else {
123                                 h = (*i)->total_channels () * grid_spacing();
124                         }
125                 }
126
127                 if (h == 0) {
128                         continue;
129                 }
130                 
131                 /* rectangle */
132                 set_source_rgb (cr, get_a_group_colour (g));
133                 double const rw = _highest_group_name + 2 * name_pad();
134                 cairo_rectangle (cr, x, y, rw, h);
135                 cairo_fill (cr);
136                 
137                 /* hence what abbreviation (or not) we need for the group name */
138                 string const upper = Glib::ustring ((*i)->name).uppercase ();
139                 pair<string, double> display = fit_to_pixels (cr, upper, h);
140                 
141                 /* plot it */
142                 set_source_rgb (cr, text_colour());
143                 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
144                 cairo_save (cr);
145                 cairo_rotate (cr, - M_PI / 2);
146                 cairo_show_text (cr, display.first.c_str());
147                 cairo_restore (cr);
148                 
149                 y += h;
150                 ++g;
151         }
152
153         /* BUNDLE AND PORT NAMES */
154
155         y = 0;
156         int N = 0;
157         int M = 0;
158         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
159
160                 if ((*i)->visible ()) {
161                         
162                         PortGroup::BundleList const & bundles = (*i)->bundles ();
163                         for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
164                                 render_bundle_name (cr, background_colour (), j->has_colour ? j->colour : get_a_bundle_colour (N), 0, y, j->bundle);
165
166                                 if (!_matrix->show_only_bundles()) {
167                                         for (uint32_t k = 0; k < j->bundle->nchannels(); ++k) {
168                                                 Gdk::Color c = j->has_colour ? j->colour : get_a_bundle_colour (M);
169                                                 render_channel_name (cr, background_colour (), c, 0, y, ARDOUR::BundleChannel (j->bundle, k));
170                                                 y += grid_spacing();
171                                                 ++M;
172                                         }
173                                 } else {
174                                         y += grid_spacing();
175                                 }
176                                 
177                                 ++N;
178                         }
179                         
180                 } else {
181
182                         y += grid_spacing ();
183                 }
184         }
185 }
186
187 void
188 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
189 {
190         uint32_t const gw = (_highest_group_name + 2 * name_pad());
191
192         pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel> w = position_to_group_and_channel (y / grid_spacing (), _matrix->rows());
193
194         if (
195                 (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x < gw) ||
196                 (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x > (_width - gw))
197                 ) {
198
199                 w.second.bundle.reset ();
200         }
201         
202         if (b == 1) {
203
204                 if (w.second.bundle) {
205                         _body->highlight_associated_channels (_matrix->row_index(), w.second);
206                 } else {
207                         if (w.first) {
208                                 w.first->set_visible (!w.first->visible());
209                         }
210                 }
211
212         } else if (b == 3) {
213
214                 _matrix->popup_menu (
215                         make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel ()),
216                         w,
217                         t
218                         );
219         }
220 }
221
222 double
223 PortMatrixRowLabels::component_to_parent_x (double x) const
224 {
225         return x + _parent_rectangle.get_x();
226 }
227
228 double
229 PortMatrixRowLabels::parent_to_component_x (double x) const
230 {
231         return x - _parent_rectangle.get_x();
232 }
233
234 double
235 PortMatrixRowLabels::component_to_parent_y (double y) const
236 {
237         return y - _body->yoffset() + _parent_rectangle.get_y();
238 }
239
240 double
241 PortMatrixRowLabels::parent_to_component_y (double y) const
242 {
243         return y + _body->yoffset() - _parent_rectangle.get_y();
244 }
245
246
247 double
248 PortMatrixRowLabels::bundle_name_x () const
249 {
250         double x = 0;
251         
252         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
253                 x = _highest_group_name + 2 * name_pad();
254         } else {
255                 if (_matrix->show_only_bundles()) {
256                         x = 0;
257                 } else {
258                         x = _longest_port_name + name_pad() * 2;
259                 }
260         }
261
262         return x;
263 }
264
265 double
266 PortMatrixRowLabels::port_name_x () const
267 {
268         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
269                 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
270         } else {
271                 return 0;
272         }
273
274         return 0;
275 }
276
277 void
278 PortMatrixRowLabels::render_bundle_name (
279         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, boost::shared_ptr<ARDOUR::Bundle> b
280         )
281 {
282         double const x = bundle_name_x ();
283         
284         int const n = _matrix->show_only_bundles() ? 1 : b->nchannels();
285         set_source_rgb (cr, bg_colour);
286         cairo_rectangle (cr, xoff + x, yoff, _longest_bundle_name + name_pad() * 2, grid_spacing() * n);
287         cairo_fill_preserve (cr);
288         set_source_rgb (cr, fg_colour);
289         cairo_set_line_width (cr, label_border_width ());
290         cairo_stroke (cr);
291
292         double const off = grid_spacing() / 2;
293
294 //      if ((*i)->nchannels () > 0 && !_matrix->show_only_bundles()) {
295 //              /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
296 //              cairo_text_extents_t ext;
297 //              cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
298 //              off = (grid_spacing() - ext.height) / 2;
299 //      }
300
301         set_source_rgb (cr, text_colour());
302         cairo_move_to (cr, xoff + x + name_pad(), yoff + name_pad() + off);
303         cairo_show_text (cr, b->name().c_str());
304 }
305
306 void
307 PortMatrixRowLabels::render_channel_name (
308         cairo_t* cr, Gdk::Color fg_colour, Gdk::Color bg_colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
309         )
310 {
311         set_source_rgb (cr, bg_colour);
312         cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, grid_spacing());
313         cairo_fill_preserve (cr);
314         set_source_rgb (cr, fg_colour);
315         cairo_set_line_width (cr, label_border_width ());
316         cairo_stroke (cr);
317         
318         cairo_text_extents_t ext;
319         cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
320         double const off = (grid_spacing() - ext.height) / 2;
321         
322         set_source_rgb (cr, text_colour());
323         cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
324         cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
325 }
326
327 double
328 PortMatrixRowLabels::channel_x (ARDOUR::BundleChannel const& bc) const
329 {
330         return 0;
331 }
332
333 double
334 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
335 {
336         return channel_to_position (bc, _matrix->rows()) * grid_spacing ();
337 }
338
339 void
340 PortMatrixRowLabels::queue_draw_for (ARDOUR::BundleChannel const & bc)
341 {
342         if (bc.bundle) {
343
344                 if (_matrix->show_only_bundles()) {
345                         _body->queue_draw_area (
346                                 component_to_parent_x (bundle_name_x()) - 1,
347                                 component_to_parent_y (channel_y (bc)) - 1,
348                                 _longest_bundle_name + name_pad() * 2 + 2,
349                                 grid_spacing() + 2
350                                 );
351                 } else {
352                         _body->queue_draw_area (
353                                 component_to_parent_x (port_name_x()) - 1, 
354                                 component_to_parent_y (channel_y (bc)) - 1,
355                                 _longest_port_name + name_pad() * 2 + 2,
356                                 grid_spacing() + 2
357                                 );
358                 }
359         }
360
361 }
362
363 void
364 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const &)
365 {
366         clear_channel_highlights ();
367         if (_body->mouseover().column.bundle && _body->mouseover().row.bundle) {
368                 add_channel_highlight (_body->mouseover().row);
369         }
370 }