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