2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
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"
29 PortMatrixBody::PortMatrixBody (PortMatrix* p)
33 _mouse_over_grid (false)
35 _column_labels = new PortMatrixColumnLabels (p, this);
36 _row_labels = new PortMatrixRowLabels (p, this);
37 _grid = new PortMatrixGrid (p, this);
39 modify_bg (Gtk::STATE_NORMAL, Gdk::Color ("#00000"));
40 add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
44 PortMatrixBody::~PortMatrixBody ()
46 delete _column_labels;
52 PortMatrixBody::on_expose_event (GdkEventExpose* event)
54 Gdk::Rectangle const exposure (
55 event->area.x, event->area.y, event->area.width, event->area.height
60 Gdk::Rectangle r = exposure;
61 /* the get_pixmap call may cause things to be rerendered and sizes to change,
62 so fetch the pixmaps before calculating where to put it */
63 GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
64 r.intersect (_column_labels->parent_rectangle(), intersects);
70 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
72 _column_labels->parent_to_component_x (r.get_x()),
73 _column_labels->parent_to_component_y (r.get_y()),
82 p = _row_labels->get_pixmap (get_window()->gobj());
83 r.intersect (_row_labels->parent_rectangle(), intersects);
88 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
90 _row_labels->parent_to_component_x (r.get_x()),
91 _row_labels->parent_to_component_y (r.get_y()),
100 p = _grid->get_pixmap (get_window()->gobj());
101 r.intersect (_grid->parent_rectangle(), intersects);
105 get_window()->gobj(),
106 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
108 _grid->parent_to_component_x (r.get_x()),
109 _grid->parent_to_component_y (r.get_y()),
117 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
120 set_cairo_clip (cr, _grid->parent_rectangle ());
121 _grid->draw_extra (cr);
125 set_cairo_clip (cr, _row_labels->parent_rectangle ());
126 _row_labels->draw_extra (cr);
130 set_cairo_clip (cr, _column_labels->parent_rectangle ());
131 _column_labels->draw_extra (cr);
140 PortMatrixBody::on_size_request (Gtk::Requisition *req)
142 std::pair<int, int> const col = _column_labels->dimensions ();
143 std::pair<int, int> const row = _row_labels->dimensions ();
144 std::pair<int, int> const grid = _grid->dimensions ();
146 /* don't ask for the maximum size of our contents, otherwise GTK won't
147 let the containing window shrink below this size */
149 req->width = std::min (512, std::max (col.first, grid.first + row.first));
150 req->height = std::min (512, col.second + grid.second);
154 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
156 Gtk::EventBox::on_size_allocate (alloc);
158 _alloc_width = alloc.get_width ();
159 _alloc_height = alloc.get_height ();
161 compute_rectangles ();
162 _matrix->setup_scrollbars ();
166 PortMatrixBody::compute_rectangles ()
168 /* full sizes of components */
169 std::pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
170 std::pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
171 std::pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
173 Gdk::Rectangle col_rect;
174 Gdk::Rectangle row_rect;
175 Gdk::Rectangle grid_rect;
177 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
179 /* build from top left */
185 if (_alloc_width > col.first) {
186 col_rect.set_width (col.first);
188 col_rect.set_width (_alloc_width);
191 /* move down to y division */
194 if (_alloc_height > col.second) {
200 col_rect.set_height (y);
202 row_rect.set_height (_alloc_height - y);
204 grid_rect.set_height (_alloc_height - y);
206 /* move right to x division */
209 if (_alloc_width > (grid.first + row.first)) {
211 } else if (_alloc_width > row.first) {
212 x = _alloc_width - row.first;
215 grid_rect.set_width (x);
217 row_rect.set_width (_alloc_width - x);
220 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
222 /* build from bottom right */
224 /* move left to x division */
227 if (_alloc_width > (grid.first + row.first)) {
229 } else if (_alloc_width > row.first) {
230 x = _alloc_width - row.first;
233 grid_rect.set_x (_alloc_width - x);
234 grid_rect.set_width (x);
235 col_rect.set_width (col.first - grid.first + x);
236 col_rect.set_x (_alloc_width - col_rect.get_width());
238 row_rect.set_width (std::min (_alloc_width - x, row.first));
239 row_rect.set_x (_alloc_width - x - row_rect.get_width());
241 /* move up to the y division */
244 if (_alloc_height > col.second) {
250 col_rect.set_y (_alloc_height - y);
251 col_rect.set_height (y);
253 grid_rect.set_height (std::min (grid.second, _alloc_height - y));
254 grid_rect.set_y (_alloc_height - y - grid_rect.get_height());
256 row_rect.set_height (grid_rect.get_height());
257 row_rect.set_y (grid_rect.get_y());
260 _row_labels->set_parent_rectangle (row_rect);
261 _column_labels->set_parent_rectangle (col_rect);
262 _grid->set_parent_rectangle (grid_rect);
266 PortMatrixBody::setup ()
268 /* Discard any old connections to bundles */
270 for (std::list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
273 _bundle_connections.clear ();
275 /* Connect to bundles so that we find out when their names change */
277 ARDOUR::BundleList r = _matrix->rows()->bundles ();
278 for (ARDOUR::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
280 _bundle_connections.push_back (
281 (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels))
286 ARDOUR::BundleList c = _matrix->columns()->bundles ();
287 for (ARDOUR::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
288 _bundle_connections.push_back (
289 (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels))
293 _column_labels->setup ();
294 _row_labels->setup ();
297 set_mouseover (PortMatrixNode ());
298 compute_rectangles ();
302 PortMatrixBody::full_scroll_width ()
304 return _grid->dimensions().first;
309 PortMatrixBody::alloc_scroll_width ()
311 return _grid->parent_rectangle().get_width();
315 PortMatrixBody::full_scroll_height ()
317 return _grid->dimensions().second;
321 PortMatrixBody::alloc_scroll_height ()
323 return _grid->parent_rectangle().get_height();
327 PortMatrixBody::set_xoffset (uint32_t xo)
334 PortMatrixBody::set_yoffset (uint32_t yo)
341 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
343 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
345 _grid->button_press (
346 _grid->parent_to_component_x (ev->x),
347 _grid->parent_to_component_y (ev->y),
351 } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
353 _row_labels->button_press (
354 _row_labels->parent_to_component_x (ev->x),
355 _row_labels->parent_to_component_y (ev->y),
359 } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
361 _column_labels->button_press (
362 _column_labels->parent_to_component_x (ev->x),
363 _column_labels->parent_to_component_y (ev->y),
372 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
374 if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
375 Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
377 _row_labels->clear_channel_highlights ();
378 _column_labels->clear_channel_highlights ();
386 PortMatrixBody::rebuild_and_draw_grid ()
388 _grid->require_rebuild ();
393 PortMatrixBody::rebuild_and_draw_column_labels ()
395 _column_labels->require_rebuild ();
400 PortMatrixBody::rebuild_and_draw_row_labels ()
402 _row_labels->require_rebuild ();
407 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
409 if (ev->type == GDK_LEAVE_NOTIFY) {
410 set_mouseover (PortMatrixNode ());
417 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
419 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
420 _grid->mouseover_event (
421 _grid->parent_to_component_x (ev->x),
422 _grid->parent_to_component_y (ev->y)
424 _mouse_over_grid = true;
426 if (_mouse_over_grid) {
427 set_mouseover (PortMatrixNode ());
428 _mouse_over_grid = false;
436 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
438 if (n == _mouseover) {
442 PortMatrixNode old = _mouseover;
445 _grid->mouseover_changed (old);
446 _row_labels->mouseover_changed (old);
447 _column_labels->mouseover_changed (old);
453 PortMatrixBody::highlight_associated_channels (int dim, uint32_t N)
455 ARDOUR::BundleChannel bc[2];
457 ARDOUR::BundleList const a = _matrix->ports(dim)->bundles ();
458 for (ARDOUR::BundleList::const_iterator i = a.begin(); i != a.end(); ++i) {
459 if (N < (*i)->nchannels ()) {
460 bc[dim] = ARDOUR::BundleChannel (*i, N);
463 N -= (*i)->nchannels ();
467 if (!bc[dim].bundle) {
471 if (dim == _matrix->column_index()) {
472 _column_labels->add_channel_highlight (bc[dim]);
474 _row_labels->add_channel_highlight (bc[dim]);
477 ARDOUR::BundleList const b = _matrix->ports(1 - dim)->bundles ();
479 for (ARDOUR::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
480 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
481 bc[1 - dim] = ARDOUR::BundleChannel (*i, j);
482 if (_matrix->get_state (bc) == PortMatrix::ASSOCIATED) {
483 if (dim == _matrix->column_index()) {
484 _row_labels->add_channel_highlight (bc[1 - dim]);
486 _column_labels->add_channel_highlight (bc[1 - dim]);
494 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
496 cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
501 PortMatrixBody::component_size_changed ()
503 compute_rectangles ();
504 _matrix->setup_scrollbars ();