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