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