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