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"
31 PortMatrixBody::PortMatrixBody (PortMatrix* p)
37 _mouse_over_grid (false),
38 _ignore_component_size_changed (false)
40 _column_labels = new PortMatrixColumnLabels (p, this);
41 _row_labels = new PortMatrixRowLabels (p, this);
42 _grid = new PortMatrixGrid (p, this);
44 add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
48 PortMatrixBody::~PortMatrixBody ()
50 delete _column_labels;
56 PortMatrixBody::on_expose_event (GdkEventExpose* event)
58 if (_matrix->columns()->empty() || _matrix->rows()->empty()) {
60 /* nothing to connect */
62 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
64 cairo_set_source_rgb (cr, 0, 0, 0);
65 cairo_rectangle (cr, 0, 0, _alloc_width, _alloc_height);
69 t << _("There are no ") << (_matrix->type() == ARDOUR::DataType::AUDIO ? _("audio") : _("MIDI")) << _(" ports to connect.");
71 cairo_text_extents_t ext;
72 cairo_text_extents (cr, t.str().c_str(), &ext);
74 cairo_set_source_rgb (cr, 1, 1, 1);
75 cairo_move_to (cr, (_alloc_width - ext.width) / 2, (_alloc_height + ext.height) / 2);
76 cairo_show_text (cr, t.str().c_str ());
83 Gdk::Rectangle const exposure (
84 event->area.x, event->area.y, event->area.width, event->area.height
89 Gdk::Rectangle r = exposure;
90 /* the get_pixmap call may cause things to be rerendered and sizes to change,
91 so fetch the pixmap before calculating where to put it */
92 GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
93 r.intersect (_column_labels->parent_rectangle(), intersects);
99 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
101 _column_labels->parent_to_component_x (r.get_x()),
102 _column_labels->parent_to_component_y (r.get_y()),
111 p = _row_labels->get_pixmap (get_window()->gobj());
112 r.intersect (_row_labels->parent_rectangle(), intersects);
116 get_window()->gobj(),
117 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
119 _row_labels->parent_to_component_x (r.get_x()),
120 _row_labels->parent_to_component_y (r.get_y()),
129 p = _grid->get_pixmap (get_window()->gobj());
130 r.intersect (_grid->parent_rectangle(), intersects);
134 get_window()->gobj(),
135 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
137 _grid->parent_to_component_x (r.get_x()),
138 _grid->parent_to_component_y (r.get_y()),
146 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
149 set_cairo_clip (cr, _grid->parent_rectangle ());
150 _grid->draw_extra (cr);
154 set_cairo_clip (cr, _row_labels->parent_rectangle ());
155 _row_labels->draw_extra (cr);
159 set_cairo_clip (cr, _column_labels->parent_rectangle ());
160 _column_labels->draw_extra (cr);
169 PortMatrixBody::on_size_request (Gtk::Requisition *req)
171 pair<int, int> const col = _column_labels->dimensions ();
172 pair<int, int> const row = _row_labels->dimensions ();
173 pair<int, int> const grid = _grid->dimensions ();
175 if (grid.first == 0 && grid.second == 0) {
176 /* nothing to display */
182 /* don't ask for the maximum size of our contents, otherwise GTK won't
183 let the containing window shrink below this size */
185 /* XXX these shouldn't be hard-coded */
186 int const min_width = 512;
187 int const min_height = 512;
189 req->width = min (min_width, max (col.first, grid.first + row.first));
190 req->height = min (min_height / _matrix->min_height_divisor(), col.second + grid.second);
194 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
196 Gtk::EventBox::on_size_allocate (alloc);
198 _alloc_width = alloc.get_width ();
199 _alloc_height = alloc.get_height ();
201 compute_rectangles ();
202 _matrix->setup_scrollbars ();
206 PortMatrixBody::compute_rectangles ()
208 /* full sizes of components */
209 pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
210 uint32_t col_overhang = _column_labels->overhang ();
211 pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
212 pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
214 Gdk::Rectangle col_rect;
215 Gdk::Rectangle row_rect;
216 Gdk::Rectangle grid_rect;
218 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
224 col_rect.set_width (min (col.first, _alloc_width));
226 uint32_t const y = min (_alloc_height, col.second);
227 col_rect.set_height (y);
229 row_rect.set_height (_alloc_height - y);
231 grid_rect.set_height (_alloc_height - y);
234 if (_alloc_width > (grid.first + row.first)) {
236 } else if (_alloc_width > row.first) {
237 x = _alloc_width - row.first;
240 grid_rect.set_width (x);
242 row_rect.set_width (_alloc_width - x);
245 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
247 col_rect.set_height (min (_alloc_height, col.second));
251 row_rect.set_width (min (_alloc_width, row.first));
252 row_rect.set_height (std::min (_alloc_height - col_rect.get_height(), row.second));
254 grid_rect.set_x (row_rect.get_width());
256 grid_rect.set_width (std::min (_alloc_width - row_rect.get_width(), grid.first));
257 grid_rect.set_height (row_rect.get_height ());
259 col_rect.set_width (grid_rect.get_width () + col_overhang);
260 col_rect.set_x (row_rect.get_width() + grid_rect.get_width() - col_rect.get_width());
261 col_rect.set_y (row_rect.get_height());
265 _row_labels->set_parent_rectangle (row_rect);
266 _column_labels->set_parent_rectangle (col_rect);
267 _grid->set_parent_rectangle (grid_rect);
271 PortMatrixBody::setup ()
273 /* Discard any old connections to bundles */
275 for (list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
278 _bundle_connections.clear ();
280 /* Connect to bundles so that we find out when their names change */
282 PortGroup::BundleList r = _matrix->rows()->bundles ();
283 for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
285 _bundle_connections.push_back (
286 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
291 PortGroup::BundleList c = _matrix->columns()->bundles ();
292 for (PortGroup::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
293 _bundle_connections.push_back (
294 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels)))
298 _column_labels->setup ();
299 _row_labels->setup ();
302 set_mouseover (PortMatrixNode ());
304 _ignore_component_size_changed = true;
305 compute_rectangles ();
306 _ignore_component_size_changed = false;
310 PortMatrixBody::full_scroll_width ()
312 return _grid->dimensions().first;
317 PortMatrixBody::alloc_scroll_width ()
319 return _grid->parent_rectangle().get_width();
323 PortMatrixBody::full_scroll_height ()
325 return _grid->dimensions().second;
329 PortMatrixBody::alloc_scroll_height ()
331 return _grid->parent_rectangle().get_height();
335 PortMatrixBody::set_xoffset (uint32_t xo)
342 PortMatrixBody::set_yoffset (uint32_t yo)
349 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
351 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
353 _grid->button_press (
354 _grid->parent_to_component_x (ev->x),
355 _grid->parent_to_component_y (ev->y),
359 } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
361 _row_labels->button_press (
362 _row_labels->parent_to_component_x (ev->x),
363 _row_labels->parent_to_component_y (ev->y),
367 } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
369 _column_labels->button_press (
370 _column_labels->parent_to_component_x (ev->x),
371 _column_labels->parent_to_component_y (ev->y),
380 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
382 if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
383 Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
385 _row_labels->clear_channel_highlights ();
386 _column_labels->clear_channel_highlights ();
388 } else if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
390 _grid->button_release (
391 _grid->parent_to_component_x (ev->x),
392 _grid->parent_to_component_y (ev->y),
402 PortMatrixBody::rebuild_and_draw_grid ()
404 _grid->require_rebuild ();
409 PortMatrixBody::rebuild_and_draw_column_labels ()
411 _column_labels->require_rebuild ();
416 PortMatrixBody::rebuild_and_draw_row_labels ()
418 _row_labels->require_rebuild ();
423 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
425 if (ev->type == GDK_LEAVE_NOTIFY) {
426 set_mouseover (PortMatrixNode ());
433 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
435 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
438 _grid->parent_to_component_x (ev->x),
439 _grid->parent_to_component_y (ev->y)
442 _mouse_over_grid = true;
445 if (_mouse_over_grid) {
446 set_mouseover (PortMatrixNode ());
447 _mouse_over_grid = false;
455 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
457 if (n == _mouseover) {
461 PortMatrixNode old = _mouseover;
464 _grid->mouseover_changed (old);
465 _row_labels->mouseover_changed (old);
466 _column_labels->mouseover_changed (old);
470 PortMatrixBody::highlight_associated_channels (int dim, ARDOUR::BundleChannel h)
472 ARDOUR::BundleChannel bc[2];
475 if (!bc[dim].bundle) {
479 if (dim == _matrix->column_index()) {
480 _column_labels->add_channel_highlight (bc[dim]);
482 _row_labels->add_channel_highlight (bc[dim]);
485 PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
487 for (PortGroup::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
488 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
489 bc[1 - dim] = ARDOUR::BundleChannel (i->bundle, j);
490 if (_matrix->get_state (bc) == PortMatrixNode::ASSOCIATED) {
491 if (dim == _matrix->column_index()) {
492 _row_labels->add_channel_highlight (bc[1 - dim]);
494 _column_labels->add_channel_highlight (bc[1 - dim]);
502 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
504 cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
509 PortMatrixBody::component_size_changed ()
511 if (_ignore_component_size_changed) {
515 compute_rectangles ();
516 _matrix->setup_scrollbars ();
519 pair<uint32_t, uint32_t>
520 PortMatrixBody::max_size () const
522 pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
523 pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
524 pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
526 return make_pair (std::max (row.first, _column_labels->overhang()) + grid.first, col.second + grid.second);