0e48b5b6b2a6b786cf193376c7f5be76b0d9c4fb
[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 "i18n.h"
27
28 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* m, PortMatrixBody* b)
29         : PortMatrixComponent (m, b)
30 {
31         
32 }
33
34 void
35 PortMatrixRowLabels::compute_dimensions ()
36 {
37         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
38         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
39         cairo_t* cr = gdk_cairo_create (pm);
40         
41         _longest_port_name = 0;
42         _longest_bundle_name = 0;
43         _height = 0;
44         ARDOUR::BundleList const r = _matrix->rows()->bundles();
45         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
46                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
47                         cairo_text_extents_t ext;
48                         cairo_text_extents (cr, (*i)->channel_name(j).c_str(), &ext);
49                         if (ext.width > _longest_port_name) {
50                                 _longest_port_name = ext.width;
51                         }
52                 }
53
54                 cairo_text_extents_t ext;
55                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
56                 if (ext.width > _longest_bundle_name) {
57                         _longest_bundle_name = ext.width;
58                 }
59
60                 _height += (*i)->nchannels() * row_height();
61         }
62
63         _highest_group_name = 0;
64         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
65                 if ((*i)->visible()) {
66                         cairo_text_extents_t ext;
67                         cairo_text_extents (cr, (*i)->name.c_str(), &ext);
68                         if (ext.height > _highest_group_name) {
69                                 _highest_group_name = ext.height;
70                         }
71                 }
72         }
73                         
74         cairo_destroy (cr);
75         gdk_pixmap_unref (pm);
76
77         _width = _highest_group_name +
78                 _longest_port_name +
79                 _longest_bundle_name +
80                 name_pad() * 6;
81 }
82
83
84 void
85 PortMatrixRowLabels::render (cairo_t* cr)
86 {
87         /* BACKGROUND */
88         
89         set_source_rgb (cr, background_colour());
90         cairo_rectangle (cr, 0, 0, _width, _height);
91         cairo_fill (cr);
92
93         /* PORT GROUP NAMES */
94
95         double x = 0;
96         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
97                 x = 0;
98         } else {
99                 x = _width - _highest_group_name - 2 * name_pad();
100         }
101
102         double y = 0;
103         int g = 0;
104         for (PortGroupList::List::const_iterator i = _matrix->rows()->begin(); i != _matrix->rows()->end(); ++i) {
105
106                 if (!(*i)->visible() || ((*i)->bundles().empty() && (*i)->ports.empty()) ) {
107                         continue;
108                 }
109                         
110                 /* compute height of this group */
111                 double h = 0;
112                 for (ARDOUR::BundleList::const_iterator j = (*i)->bundles().begin(); j != (*i)->bundles().end(); ++j) {
113                         h += (*j)->nchannels() * row_height();
114                 }
115                 h += (*i)->ports.size() * row_height();
116
117                 /* rectangle */
118                 set_source_rgb (cr, get_a_group_colour (g));
119                 double const rw = _highest_group_name + 2 * name_pad();
120                 cairo_rectangle (cr, x, y, rw, h);
121                 cairo_fill (cr);
122                     
123                 /* hence what abbreviation (or not) we need for the group name */
124                 std::pair<std::string, double> display = display_port_name (cr, (*i)->name, h);
125
126                 /* plot it */
127                 set_source_rgb (cr, text_colour());
128                 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
129                 cairo_save (cr);
130                 cairo_rotate (cr, - M_PI / 2);
131                 cairo_show_text (cr, display.first.c_str());
132                 cairo_restore (cr);
133
134                 y += h;
135                 ++g;
136         }
137
138         /* BUNDLE NAMES */
139
140         x = 0;
141         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
142                 x = _highest_group_name + 2 * name_pad();
143         } else {
144                 x = _longest_port_name + name_pad() * 2;
145         }
146
147         y = 0;
148         ARDOUR::BundleList const r = _matrix->rows()->bundles();
149         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
150
151                 Gdk::Color const colour = get_a_bundle_colour (i - r.begin ());
152                 set_source_rgb (cr, colour);
153                 cairo_rectangle (cr, x, y, _longest_bundle_name + name_pad() * 2, row_height() * (*i)->nchannels());
154                 cairo_fill_preserve (cr);
155                 set_source_rgb (cr, background_colour());
156                 cairo_set_line_width (cr, label_border_width ());
157                 cairo_stroke (cr);
158
159                 double off = 0;
160                 if ((*i)->nchannels () > 0) {
161                         /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
162                         cairo_text_extents_t ext;
163                         cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
164                         off = (row_height() - ext.height) / 2;
165                 } else {
166                         off = row_height() / 2;
167                 }
168
169                 set_source_rgb (cr, text_colour());
170                 cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
171                 cairo_show_text (cr, (*i)->name().c_str());
172                 
173                 y += row_height() * (*i)->nchannels ();
174         }
175         
176
177         /* PORT NAMES */
178
179         y = 0;
180         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
181                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
182                         render_port_name (cr, get_a_bundle_colour (i - r.begin()), 0, y, ARDOUR::BundleChannel (*i, j));
183                         y += row_height();
184                 }
185         }
186 }
187
188 void
189 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
190 {
191         if (b != 3) {
192                 return;
193         }
194
195         if (!_matrix->can_rename_channels (_matrix->row_index()) &&
196             !_matrix->can_remove_channels (_matrix->row_index())) {
197                 return;
198         }
199
200         if ( (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM && x > (_longest_bundle_name + name_pad() * 2)) ||
201              (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT && x < (_longest_port_name + name_pad() * 2))
202                 ) {
203
204                 _matrix->popup_channel_context_menu (_matrix->row_index(), y / row_height(), t);
205                 
206         }
207 }
208
209 double
210 PortMatrixRowLabels::component_to_parent_x (double x) const
211 {
212         return x + _parent_rectangle.get_x();
213 }
214
215 double
216 PortMatrixRowLabels::parent_to_component_x (double x) const
217 {
218         return x - _parent_rectangle.get_x();
219 }
220
221 double
222 PortMatrixRowLabels::component_to_parent_y (double y) const
223 {
224         return y - _body->yoffset() + _parent_rectangle.get_y();
225 }
226
227 double
228 PortMatrixRowLabels::parent_to_component_y (double y) const
229 {
230         return y + _body->yoffset() - _parent_rectangle.get_y();
231 }
232
233 double
234 PortMatrixRowLabels::port_name_x () const
235 {
236         if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
237                 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
238         } else {
239                 return 0;
240         }
241
242         return 0;
243 }
244
245 void
246 PortMatrixRowLabels::render_port_name (
247         cairo_t* cr, Gdk::Color colour, double xoff, double yoff, ARDOUR::BundleChannel const& bc
248         )
249 {
250         set_source_rgb (cr, colour);
251         cairo_rectangle (cr, port_name_x() + xoff, yoff, _longest_port_name + name_pad() * 2, row_height());
252         cairo_fill_preserve (cr);
253         set_source_rgb (cr, background_colour());
254         cairo_set_line_width (cr, label_border_width ());
255         cairo_stroke (cr);
256         
257         cairo_text_extents_t ext;
258         cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
259         double const off = (row_height() - ext.height) / 2;
260         
261         set_source_rgb (cr, text_colour());
262         cairo_move_to (cr, port_name_x() + xoff + name_pad(), yoff + name_pad() + off);
263         cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
264 }
265
266 double
267 PortMatrixRowLabels::channel_y (ARDOUR::BundleChannel const& bc) const
268 {
269         uint32_t n = 0;
270
271         ARDOUR::BundleList::const_iterator i = _matrix->rows()->bundles().begin();
272         while (i != _matrix->rows()->bundles().end() && *i != bc.bundle) {
273                 n += (*i)->nchannels ();
274                 ++i;
275         }
276         
277         n += bc.channel;
278         return n * row_height();
279 }
280
281 void
282 PortMatrixRowLabels::queue_draw_for (PortMatrixNode const& n)
283 {
284         if (n.row.bundle) {
285
286                 _body->queue_draw_area (
287                         component_to_parent_x (port_name_x()),
288                         component_to_parent_y (channel_y (n.row)),
289                         _longest_port_name + name_pad() * 2,
290                         row_height()
291                         );
292         }
293
294 }
295
296 void
297 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const& old)
298 {
299         queue_draw_for (old);
300         queue_draw_for (_body->mouseover());
301 }
302
303 void
304 PortMatrixRowLabels::draw_extra (cairo_t* cr)
305 {
306         if (_body->mouseover().row.bundle) {
307                 render_port_name (
308                         cr,
309                         mouseover_port_colour (),
310                         component_to_parent_x (0),
311                         component_to_parent_y (channel_y (_body->mouseover().row)),
312                         _body->mouseover().row
313                         );
314         }
315 }