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 Gdk::Rectangle const exposure (
59 event->area.x, event->area.y, event->area.width, event->area.height
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);
74 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
76 _column_labels->parent_to_component_x (r.get_x()),
77 _column_labels->parent_to_component_y (r.get_y()),
86 p = _row_labels->get_pixmap (get_window()->gobj());
87 r.intersect (_row_labels->parent_rectangle(), intersects);
92 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
94 _row_labels->parent_to_component_x (r.get_x()),
95 _row_labels->parent_to_component_y (r.get_y()),
104 p = _grid->get_pixmap (get_window()->gobj());
105 r.intersect (_grid->parent_rectangle(), intersects);
109 get_window()->gobj(),
110 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
112 _grid->parent_to_component_x (r.get_x()),
113 _grid->parent_to_component_y (r.get_y()),
121 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
124 set_cairo_clip (cr, _grid->parent_rectangle ());
125 _grid->draw_extra (cr);
129 set_cairo_clip (cr, _row_labels->parent_rectangle ());
130 _row_labels->draw_extra (cr);
134 set_cairo_clip (cr, _column_labels->parent_rectangle ());
135 _column_labels->draw_extra (cr);
144 PortMatrixBody::on_size_request (Gtk::Requisition *req)
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 ();
150 /* don't ask for the maximum size of our contents, otherwise GTK won't
151 let the containing window shrink below this size */
153 /* XXX these shouldn't be hard-coded */
154 int const min_width = 512;
155 int const min_height = 512;
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);
162 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
164 Gtk::EventBox::on_size_allocate (alloc);
166 _alloc_width = alloc.get_width ();
167 _alloc_height = alloc.get_height ();
169 compute_rectangles ();
170 _matrix->setup_scrollbars ();
174 PortMatrixBody::compute_rectangles ()
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 ();
182 Gdk::Rectangle col_rect;
183 Gdk::Rectangle row_rect;
184 Gdk::Rectangle grid_rect;
186 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
192 col_rect.set_width (min (col.first, _alloc_width));
194 uint32_t const y = min (_alloc_height, col.second);
195 col_rect.set_height (y);
197 row_rect.set_height (_alloc_height - y);
199 grid_rect.set_height (_alloc_height - y);
202 if (_alloc_width > (grid.first + row.first)) {
204 } else if (_alloc_width > row.first) {
205 x = _alloc_width - row.first;
208 grid_rect.set_width (x);
210 row_rect.set_width (_alloc_width - x);
213 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
215 col_rect.set_height (min (_alloc_height, col.second));
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));
222 grid_rect.set_x (row_rect.get_width());
224 grid_rect.set_width (std::min (_alloc_width - row_rect.get_width(), grid.first));
225 grid_rect.set_height (row_rect.get_height ());
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());
233 _row_labels->set_parent_rectangle (row_rect);
234 _column_labels->set_parent_rectangle (col_rect);
235 _grid->set_parent_rectangle (grid_rect);
239 PortMatrixBody::setup ()
241 /* Discard any old connections to bundles */
243 for (list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
246 _bundle_connections.clear ();
248 /* Connect to bundles so that we find out when their names change */
250 PortGroup::BundleList r = _matrix->rows()->bundles ();
251 for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
253 _bundle_connections.push_back (
254 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
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)))
266 _column_labels->setup ();
267 _row_labels->setup ();
270 set_mouseover (PortMatrixNode ());
272 _ignore_component_size_changed = true;
273 compute_rectangles ();
274 _ignore_component_size_changed = false;
278 PortMatrixBody::full_scroll_width ()
280 return _grid->dimensions().first;
285 PortMatrixBody::alloc_scroll_width ()
287 return _grid->parent_rectangle().get_width();
291 PortMatrixBody::full_scroll_height ()
293 return _grid->dimensions().second;
297 PortMatrixBody::alloc_scroll_height ()
299 return _grid->parent_rectangle().get_height();
303 PortMatrixBody::set_xoffset (uint32_t xo)
310 PortMatrixBody::set_yoffset (uint32_t yo)
317 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
319 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
321 _grid->button_press (
322 _grid->parent_to_component_x (ev->x),
323 _grid->parent_to_component_y (ev->y),
327 } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
329 _row_labels->button_press (
330 _row_labels->parent_to_component_x (ev->x),
331 _row_labels->parent_to_component_y (ev->y),
335 } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
337 _column_labels->button_press (
338 _column_labels->parent_to_component_x (ev->x),
339 _column_labels->parent_to_component_y (ev->y),
348 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
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)) {
353 _row_labels->clear_channel_highlights ();
354 _column_labels->clear_channel_highlights ();
356 } else if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
358 _grid->button_release (
359 _grid->parent_to_component_x (ev->x),
360 _grid->parent_to_component_y (ev->y),
370 PortMatrixBody::rebuild_and_draw_grid ()
372 _grid->require_rebuild ();
377 PortMatrixBody::rebuild_and_draw_column_labels ()
379 _column_labels->require_rebuild ();
384 PortMatrixBody::rebuild_and_draw_row_labels ()
386 _row_labels->require_rebuild ();
391 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
393 if (ev->type == GDK_LEAVE_NOTIFY) {
394 set_mouseover (PortMatrixNode ());
401 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
403 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
406 _grid->parent_to_component_x (ev->x),
407 _grid->parent_to_component_y (ev->y)
410 _mouse_over_grid = true;
413 if (_mouse_over_grid) {
414 set_mouseover (PortMatrixNode ());
415 _mouse_over_grid = false;
423 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
425 if (n == _mouseover) {
429 PortMatrixNode old = _mouseover;
432 _grid->mouseover_changed (old);
433 _row_labels->mouseover_changed (old);
434 _column_labels->mouseover_changed (old);
438 PortMatrixBody::highlight_associated_channels (int dim, ARDOUR::BundleChannel h)
440 ARDOUR::BundleChannel bc[2];
443 if (!bc[dim].bundle) {
447 if (dim == _matrix->column_index()) {
448 _column_labels->add_channel_highlight (bc[dim]);
450 _row_labels->add_channel_highlight (bc[dim]);
453 PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
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]);
462 _column_labels->add_channel_highlight (bc[1 - dim]);
470 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
472 cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
477 PortMatrixBody::component_size_changed ()
479 if (_ignore_component_size_changed) {
483 compute_rectangles ();
484 _matrix->setup_scrollbars ();
487 pair<uint32_t, uint32_t>
488 PortMatrixBody::max_size () const
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 ();
494 return make_pair (std::max (row.first, _column_labels->overhang()) + grid.first, col.second + grid.second);