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)
35 _mouse_over_grid (false)
37 _column_labels = new PortMatrixColumnLabels (p, this);
38 _row_labels = new PortMatrixRowLabels (p, this);
39 _grid = new PortMatrixGrid (p, this);
41 add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
45 PortMatrixBody::~PortMatrixBody ()
47 delete _column_labels;
53 PortMatrixBody::on_expose_event (GdkEventExpose* event)
55 Gdk::Rectangle const exposure (
56 event->area.x, event->area.y, event->area.width, event->area.height
61 Gdk::Rectangle r = exposure;
62 /* the get_pixmap call may cause things to be rerendered and sizes to change,
63 so fetch the pixmap before calculating where to put it */
64 GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
65 r.intersect (_column_labels->parent_rectangle(), intersects);
71 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
73 _column_labels->parent_to_component_x (r.get_x()),
74 _column_labels->parent_to_component_y (r.get_y()),
83 p = _row_labels->get_pixmap (get_window()->gobj());
84 r.intersect (_row_labels->parent_rectangle(), intersects);
89 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
91 _row_labels->parent_to_component_x (r.get_x()),
92 _row_labels->parent_to_component_y (r.get_y()),
101 p = _grid->get_pixmap (get_window()->gobj());
102 r.intersect (_grid->parent_rectangle(), intersects);
106 get_window()->gobj(),
107 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
109 _grid->parent_to_component_x (r.get_x()),
110 _grid->parent_to_component_y (r.get_y()),
118 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
121 set_cairo_clip (cr, _grid->parent_rectangle ());
122 _grid->draw_extra (cr);
126 set_cairo_clip (cr, _row_labels->parent_rectangle ());
127 _row_labels->draw_extra (cr);
131 set_cairo_clip (cr, _column_labels->parent_rectangle ());
132 _column_labels->draw_extra (cr);
141 PortMatrixBody::on_size_request (Gtk::Requisition *req)
143 pair<int, int> const col = _column_labels->dimensions ();
144 pair<int, int> const row = _row_labels->dimensions ();
145 pair<int, int> const grid = _grid->dimensions ();
147 /* don't ask for the maximum size of our contents, otherwise GTK won't
148 let the containing window shrink below this size */
150 /* XXX these shouldn't be hard-coded */
151 int const min_width = 512;
152 int const min_height = 512;
154 req->width = min (min_width, max (col.first, grid.first + row.first));
155 req->height = min (min_height / _matrix->min_height_divisor(), col.second + grid.second);
159 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
161 Gtk::EventBox::on_size_allocate (alloc);
163 _alloc_width = alloc.get_width ();
164 _alloc_height = alloc.get_height ();
166 compute_rectangles ();
167 _matrix->setup_scrollbars ();
171 PortMatrixBody::compute_rectangles ()
173 /* full sizes of components */
174 pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
175 uint32_t col_overhang = _column_labels->overhang ();
176 pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
177 pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
179 Gdk::Rectangle col_rect;
180 Gdk::Rectangle row_rect;
181 Gdk::Rectangle grid_rect;
183 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
189 col_rect.set_width (min (col.first, _alloc_width));
191 uint32_t const y = min (_alloc_height, col.second);
192 col_rect.set_height (y);
194 row_rect.set_height (_alloc_height - y);
196 grid_rect.set_height (_alloc_height - y);
199 if (_alloc_width > (grid.first + row.first)) {
201 } else if (_alloc_width > row.first) {
202 x = _alloc_width - row.first;
205 grid_rect.set_width (x);
207 row_rect.set_width (_alloc_width - x);
210 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
212 col_rect.set_height (min (_alloc_height, col.second));
216 row_rect.set_width (min (_alloc_width, row.first));
217 row_rect.set_height (std::min (_alloc_height - col_rect.get_height(), row.second));
219 grid_rect.set_x (row_rect.get_width());
221 grid_rect.set_width (std::min (_alloc_width - row_rect.get_width(), grid.first));
222 grid_rect.set_height (row_rect.get_height ());
224 col_rect.set_width (grid_rect.get_width () + col_overhang);
225 col_rect.set_x (row_rect.get_width() + grid_rect.get_width() - col_rect.get_width());
226 col_rect.set_y (row_rect.get_height());
230 _row_labels->set_parent_rectangle (row_rect);
231 _column_labels->set_parent_rectangle (col_rect);
232 _grid->set_parent_rectangle (grid_rect);
236 PortMatrixBody::setup ()
238 /* Discard any old connections to bundles */
240 for (list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
243 _bundle_connections.clear ();
245 /* Connect to bundles so that we find out when their names change */
247 PortGroup::BundleList r = _matrix->rows()->bundles ();
248 for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
250 _bundle_connections.push_back (
251 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
256 PortGroup::BundleList c = _matrix->columns()->bundles ();
257 for (PortGroup::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
258 _bundle_connections.push_back (
259 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels)))
263 _column_labels->setup ();
264 _row_labels->setup ();
267 set_mouseover (PortMatrixNode ());
268 compute_rectangles ();
272 PortMatrixBody::full_scroll_width ()
274 return _grid->dimensions().first;
279 PortMatrixBody::alloc_scroll_width ()
281 return _grid->parent_rectangle().get_width();
285 PortMatrixBody::full_scroll_height ()
287 return _grid->dimensions().second;
291 PortMatrixBody::alloc_scroll_height ()
293 return _grid->parent_rectangle().get_height();
297 PortMatrixBody::set_xoffset (uint32_t xo)
304 PortMatrixBody::set_yoffset (uint32_t yo)
311 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
313 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
315 _grid->button_press (
316 _grid->parent_to_component_x (ev->x),
317 _grid->parent_to_component_y (ev->y),
321 } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
323 _row_labels->button_press (
324 _row_labels->parent_to_component_x (ev->x),
325 _row_labels->parent_to_component_y (ev->y),
329 } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
331 _column_labels->button_press (
332 _column_labels->parent_to_component_x (ev->x),
333 _column_labels->parent_to_component_y (ev->y),
342 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
344 if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
345 Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
347 _row_labels->clear_channel_highlights ();
348 _column_labels->clear_channel_highlights ();
350 } else if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
352 _grid->button_release (
353 _grid->parent_to_component_x (ev->x),
354 _grid->parent_to_component_y (ev->y),
364 PortMatrixBody::rebuild_and_draw_grid ()
366 _grid->require_rebuild ();
371 PortMatrixBody::rebuild_and_draw_column_labels ()
373 _column_labels->require_rebuild ();
378 PortMatrixBody::rebuild_and_draw_row_labels ()
380 _row_labels->require_rebuild ();
385 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
387 if (ev->type == GDK_LEAVE_NOTIFY) {
388 set_mouseover (PortMatrixNode ());
395 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
397 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
400 _grid->parent_to_component_x (ev->x),
401 _grid->parent_to_component_y (ev->y)
404 _mouse_over_grid = true;
407 if (_mouse_over_grid) {
408 set_mouseover (PortMatrixNode ());
409 _mouse_over_grid = false;
417 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
419 if (n == _mouseover) {
423 PortMatrixNode old = _mouseover;
426 _grid->mouseover_changed (old);
427 _row_labels->mouseover_changed (old);
428 _column_labels->mouseover_changed (old);
432 PortMatrixBody::highlight_associated_channels (int dim, ARDOUR::BundleChannel h)
434 ARDOUR::BundleChannel bc[2];
437 if (!bc[dim].bundle) {
441 if (dim == _matrix->column_index()) {
442 _column_labels->add_channel_highlight (bc[dim]);
444 _row_labels->add_channel_highlight (bc[dim]);
447 PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
449 for (PortGroup::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
450 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
451 bc[1 - dim] = ARDOUR::BundleChannel (i->bundle, j);
452 if (_matrix->get_state (bc) == PortMatrixNode::ASSOCIATED) {
453 if (dim == _matrix->column_index()) {
454 _row_labels->add_channel_highlight (bc[1 - dim]);
456 _column_labels->add_channel_highlight (bc[1 - dim]);
464 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
466 cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
471 PortMatrixBody::component_size_changed ()
473 compute_rectangles ();
474 _matrix->setup_max_size ();
475 _matrix->setup_scrollbars ();
478 pair<uint32_t, uint32_t>
479 PortMatrixBody::max_size () const
481 pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
482 pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
483 pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
485 return make_pair (std::max (row.first, _column_labels->overhang()) + grid.first, col.second + grid.second);