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