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