Use track colours in the port matrix.
[ardour.git] / gtk2_ardour / port_matrix_body.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_body.h"
24 #include "port_matrix.h"
25 #include "port_matrix_column_labels.h"
26 #include "port_matrix_row_labels.h"
27 #include "port_matrix_grid.h"
28
29 PortMatrixBody::PortMatrixBody (PortMatrix* p)
30         : _matrix (p),
31           _xoffset (0),
32           _yoffset (0),
33           _mouse_over_grid (false)
34 {
35         _column_labels = new PortMatrixColumnLabels (p, this);
36         _row_labels = new PortMatrixRowLabels (p, this);
37         _grid = new PortMatrixGrid (p, this);
38         
39         modify_bg (Gtk::STATE_NORMAL, Gdk::Color ("#00000"));
40         add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
41 }
42
43
44 PortMatrixBody::~PortMatrixBody ()
45 {
46         delete _column_labels;
47         delete _row_labels;
48         delete _grid;
49 }
50
51 bool
52 PortMatrixBody::on_expose_event (GdkEventExpose* event)
53 {
54         Gdk::Rectangle const exposure (
55                 event->area.x, event->area.y, event->area.width, event->area.height
56                 );
57
58         bool intersects;
59         
60         Gdk::Rectangle r = exposure;
61         /* the get_pixmap call may cause things to be rerendered and sizes to change,
62            so fetch the pixmap before calculating where to put it */
63         GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
64         r.intersect (_column_labels->parent_rectangle(), intersects);
65
66         if (intersects) {
67
68                 gdk_draw_drawable (
69                         get_window()->gobj(),
70                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
71                         p,
72                         _column_labels->parent_to_component_x (r.get_x()),
73                         _column_labels->parent_to_component_y (r.get_y()),
74                         r.get_x(),
75                         r.get_y(),
76                         r.get_width(),
77                         r.get_height()
78                         );
79         }
80
81         r = exposure;
82         p = _row_labels->get_pixmap (get_window()->gobj());
83         r.intersect (_row_labels->parent_rectangle(), intersects);
84
85         if (intersects) {
86                 gdk_draw_drawable (
87                         get_window()->gobj(),
88                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
89                         p,
90                         _row_labels->parent_to_component_x (r.get_x()),
91                         _row_labels->parent_to_component_y (r.get_y()),
92                         r.get_x(),
93                         r.get_y(),
94                         r.get_width(),
95                         r.get_height()
96                         );
97         }
98
99         r = exposure;
100         p = _grid->get_pixmap (get_window()->gobj());
101         r.intersect (_grid->parent_rectangle(), intersects);
102
103         if (intersects) {
104                 gdk_draw_drawable (
105                         get_window()->gobj(),
106                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
107                         p,
108                         _grid->parent_to_component_x (r.get_x()),
109                         _grid->parent_to_component_y (r.get_y()),
110                         r.get_x(),
111                         r.get_y(),
112                         r.get_width(),
113                         r.get_height()
114                         );
115         }
116
117         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
118
119         cairo_save (cr);
120         set_cairo_clip (cr, _grid->parent_rectangle ());
121         _grid->draw_extra (cr);
122         cairo_restore (cr);
123
124         cairo_save (cr);
125         set_cairo_clip (cr, _row_labels->parent_rectangle ());
126         _row_labels->draw_extra (cr);
127         cairo_restore (cr);
128
129         cairo_save (cr);
130         set_cairo_clip (cr, _column_labels->parent_rectangle ());
131         _column_labels->draw_extra (cr);
132         cairo_restore (cr);
133         
134         cairo_destroy (cr);
135
136         return true;
137 }
138
139 void
140 PortMatrixBody::on_size_request (Gtk::Requisition *req)
141 {
142         std::pair<int, int> const col = _column_labels->dimensions ();
143         std::pair<int, int> const row = _row_labels->dimensions ();
144         std::pair<int, int> const grid = _grid->dimensions ();
145
146         /* don't ask for the maximum size of our contents, otherwise GTK won't
147            let the containing window shrink below this size */
148
149         /* XXX these shouldn't be hard-coded */
150         int const min_width = 512;
151         int const min_height = 512;
152
153         req->width = std::min (min_width, std::max (col.first, grid.first + row.first));
154         req->height = std::min (min_height / _matrix->min_height_divisor(), col.second + grid.second);
155 }
156
157 void
158 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
159 {
160         Gtk::EventBox::on_size_allocate (alloc);
161
162         _alloc_width = alloc.get_width ();
163         _alloc_height = alloc.get_height ();
164
165         compute_rectangles ();
166         _matrix->setup_scrollbars ();
167 }
168
169 void
170 PortMatrixBody::compute_rectangles ()
171 {
172         /* full sizes of components */
173         std::pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
174         std::pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
175         std::pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
176
177         Gdk::Rectangle col_rect;
178         Gdk::Rectangle row_rect;
179         Gdk::Rectangle grid_rect;
180
181         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
182
183                 /* build from top left */
184
185                 col_rect.set_x (0);
186                 col_rect.set_y (0);
187                 grid_rect.set_x (0);
188
189                 if (_alloc_width > col.first) {
190                         col_rect.set_width (col.first);
191                 } else {
192                         col_rect.set_width (_alloc_width);
193                 }
194
195                 /* move down to y division */
196                 
197                 uint32_t y = 0;
198                 if (_alloc_height > col.second) {
199                         y = col.second;
200                 } else {
201                         y = _alloc_height;
202                 }
203
204                 col_rect.set_height (y);
205                 row_rect.set_y (y);
206                 row_rect.set_height (_alloc_height - y);
207                 grid_rect.set_y (y);
208                 grid_rect.set_height (_alloc_height - y);
209
210                 /* move right to x division */
211
212                 uint32_t x = 0;
213                 if (_alloc_width > (grid.first + row.first)) {
214                         x = grid.first;
215                 } else if (_alloc_width > row.first) {
216                         x = _alloc_width - row.first;
217                 }
218
219                 grid_rect.set_width (x);
220                 row_rect.set_x (x);
221                 row_rect.set_width (_alloc_width - x);
222                         
223
224         } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
225
226                 /* build from bottom right */
227
228                 /* move left to x division */
229
230                 uint32_t x = 0;
231                 if (_alloc_width > (grid.first + row.first)) {
232                         x = grid.first;
233                 } else if (_alloc_width > row.first) {
234                         x = _alloc_width - row.first;
235                 }
236
237                 grid_rect.set_x (_alloc_width - x);
238                 grid_rect.set_width (x);
239                 col_rect.set_width (col.first - grid.first + x);
240                 col_rect.set_x (_alloc_width - col_rect.get_width());
241
242                 row_rect.set_width (std::min (_alloc_width - x, row.first));
243                 row_rect.set_x (_alloc_width - x - row_rect.get_width());
244
245                 /* move up to the y division */
246                 
247                 uint32_t y = 0;
248                 if (_alloc_height > col.second) {
249                         y = col.second;
250                 } else {
251                         y = _alloc_height;
252                 }
253
254                 col_rect.set_y (_alloc_height - y);
255                 col_rect.set_height (y);
256
257                 grid_rect.set_height (std::min (grid.second, _alloc_height - y));
258                 grid_rect.set_y (_alloc_height - y - grid_rect.get_height());
259
260                 row_rect.set_height (grid_rect.get_height());
261                 row_rect.set_y (grid_rect.get_y());
262         }
263
264         _row_labels->set_parent_rectangle (row_rect);
265         _column_labels->set_parent_rectangle (col_rect);
266         _grid->set_parent_rectangle (grid_rect);
267 }
268
269 void
270 PortMatrixBody::setup ()
271 {
272         /* Discard any old connections to bundles */
273         
274         for (std::list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
275                 i->disconnect ();
276         }
277         _bundle_connections.clear ();
278
279         /* Connect to bundles so that we find out when their names change */
280         
281         PortGroup::BundleList r = _matrix->rows()->bundles ();
282         for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
283                 
284                 _bundle_connections.push_back (
285                         i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
286                         );
287                 
288         }
289
290         PortGroup::BundleList c = _matrix->columns()->bundles ();
291         for (PortGroup::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
292                 _bundle_connections.push_back (
293                         i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels)))
294                         );
295         }
296         
297         _column_labels->setup ();
298         _row_labels->setup ();
299         _grid->setup ();
300
301         set_mouseover (PortMatrixNode ());
302         compute_rectangles ();
303 }
304
305 uint32_t
306 PortMatrixBody::full_scroll_width ()
307 {
308         return _grid->dimensions().first;
309
310 }
311
312 uint32_t
313 PortMatrixBody::alloc_scroll_width ()
314 {
315         return _grid->parent_rectangle().get_width();
316 }
317
318 uint32_t
319 PortMatrixBody::full_scroll_height ()
320 {
321         return _grid->dimensions().second;
322 }
323
324 uint32_t
325 PortMatrixBody::alloc_scroll_height ()
326 {
327         return _grid->parent_rectangle().get_height();
328 }
329
330 void
331 PortMatrixBody::set_xoffset (uint32_t xo)
332 {
333         _xoffset = xo;
334         queue_draw ();
335 }
336
337 void
338 PortMatrixBody::set_yoffset (uint32_t yo)
339 {
340         _yoffset = yo;
341         queue_draw ();
342 }
343
344 bool
345 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
346 {
347         if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
348
349                 _grid->button_press (
350                         _grid->parent_to_component_x (ev->x),
351                         _grid->parent_to_component_y (ev->y),
352                         ev->button
353                         );
354
355         } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
356
357                 _row_labels->button_press (
358                         _row_labels->parent_to_component_x (ev->x),
359                         _row_labels->parent_to_component_y (ev->y),
360                         ev->button, ev->time
361                         );
362         
363         } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
364
365                 _column_labels->button_press (
366                         _column_labels->parent_to_component_x (ev->x),
367                         _column_labels->parent_to_component_y (ev->y),
368                         ev->button, ev->time
369                         );
370         }
371
372         return true;
373 }
374
375 bool
376 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
377 {
378         if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
379             Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
380
381                 _row_labels->clear_channel_highlights ();
382                 _column_labels->clear_channel_highlights ();
383                 
384         }
385
386         return true;
387 }
388
389 void
390 PortMatrixBody::rebuild_and_draw_grid ()
391 {
392         _grid->require_rebuild ();
393         queue_draw ();
394 }
395
396 void
397 PortMatrixBody::rebuild_and_draw_column_labels ()
398 {
399         _column_labels->require_rebuild ();
400         queue_draw ();
401 }
402
403 void
404 PortMatrixBody::rebuild_and_draw_row_labels ()
405 {
406         _row_labels->require_rebuild ();
407         queue_draw ();
408 }
409
410 bool
411 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
412 {
413         if (ev->type == GDK_LEAVE_NOTIFY) {
414                 set_mouseover (PortMatrixNode ());
415         }
416
417         return true;
418 }
419
420 bool
421 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
422 {
423         if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
424                 _grid->mouseover_event (
425                         _grid->parent_to_component_x (ev->x),
426                         _grid->parent_to_component_y (ev->y)
427                         );
428                 _mouse_over_grid = true;
429         } else {
430                 if (_mouse_over_grid) {
431                         set_mouseover (PortMatrixNode ());
432                         _mouse_over_grid = false;
433                 }
434         }
435
436         return true;
437 }
438
439 void
440 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
441 {
442         if (n == _mouseover) {
443                 return;
444         }
445
446         PortMatrixNode old = _mouseover;
447         _mouseover = n;
448         
449         _grid->mouseover_changed (old);
450         _row_labels->mouseover_changed (old);
451         _column_labels->mouseover_changed (old);
452 }
453
454
455
456 void
457 PortMatrixBody::highlight_associated_channels (int dim, uint32_t N)
458 {
459         ARDOUR::BundleChannel bc[2];
460         
461         PortGroup::BundleList const a = _matrix->ports(dim)->bundles ();
462         for (PortGroup::BundleList::const_iterator i = a.begin(); i != a.end(); ++i) {
463                 if (N < i->bundle->nchannels ()) {
464                         bc[dim] = ARDOUR::BundleChannel (i->bundle, N);
465                         break;
466                 } else {
467                         N -= i->bundle->nchannels ();
468                 }
469         }
470
471         if (!bc[dim].bundle) {
472                 return;
473         }
474
475         if (dim == _matrix->column_index()) {
476                 _column_labels->add_channel_highlight (bc[dim]);
477         } else {
478                 _row_labels->add_channel_highlight (bc[dim]);
479         }
480
481         PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
482
483         for (PortGroup::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
484                 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
485                         bc[1 - dim] = ARDOUR::BundleChannel (i->bundle, j);
486                         if (_matrix->get_state (bc) == PortMatrixNode::ASSOCIATED) {
487                                 if (dim == _matrix->column_index()) {
488                                         _row_labels->add_channel_highlight (bc[1 - dim]);
489                                 } else {
490                                         _column_labels->add_channel_highlight (bc[1 - dim]);
491                                 }
492                         }
493                 }
494         }
495 }
496
497 void
498 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
499 {
500         cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
501         cairo_clip (cr);
502 }
503
504 void
505 PortMatrixBody::component_size_changed ()
506 {
507         compute_rectangles ();
508         _matrix->setup_scrollbars ();
509 }
510
511