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 add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
43 PortMatrixBody::~PortMatrixBody ()
45 delete _column_labels;
51 PortMatrixBody::on_expose_event (GdkEventExpose* event)
53 Gdk::Rectangle const exposure (
54 event->area.x, event->area.y, event->area.width, event->area.height
59 Gdk::Rectangle r = exposure;
60 /* the get_pixmap call may cause things to be rerendered and sizes to change,
61 so fetch the pixmap before calculating where to put it */
62 GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
63 r.intersect (_column_labels->parent_rectangle(), intersects);
69 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
71 _column_labels->parent_to_component_x (r.get_x()),
72 _column_labels->parent_to_component_y (r.get_y()),
81 p = _row_labels->get_pixmap (get_window()->gobj());
82 r.intersect (_row_labels->parent_rectangle(), intersects);
87 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
89 _row_labels->parent_to_component_x (r.get_x()),
90 _row_labels->parent_to_component_y (r.get_y()),
99 p = _grid->get_pixmap (get_window()->gobj());
100 r.intersect (_grid->parent_rectangle(), intersects);
104 get_window()->gobj(),
105 get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
107 _grid->parent_to_component_x (r.get_x()),
108 _grid->parent_to_component_y (r.get_y()),
116 cairo_t* cr = gdk_cairo_create (get_window()->gobj());
119 set_cairo_clip (cr, _grid->parent_rectangle ());
120 _grid->draw_extra (cr);
124 set_cairo_clip (cr, _row_labels->parent_rectangle ());
125 _row_labels->draw_extra (cr);
129 set_cairo_clip (cr, _column_labels->parent_rectangle ());
130 _column_labels->draw_extra (cr);
139 PortMatrixBody::on_size_request (Gtk::Requisition *req)
141 std::pair<int, int> const col = _column_labels->dimensions ();
142 std::pair<int, int> const row = _row_labels->dimensions ();
143 std::pair<int, int> const grid = _grid->dimensions ();
145 /* don't ask for the maximum size of our contents, otherwise GTK won't
146 let the containing window shrink below this size */
148 /* XXX these shouldn't be hard-coded */
149 int const min_width = 512;
150 int const min_height = 512;
152 req->width = std::min (min_width, std::max (col.first, grid.first + row.first));
153 req->height = std::min (min_height / _matrix->min_height_divisor(), col.second + grid.second);
157 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
159 Gtk::EventBox::on_size_allocate (alloc);
161 _alloc_width = alloc.get_width ();
162 _alloc_height = alloc.get_height ();
164 compute_rectangles ();
165 _matrix->setup_scrollbars ();
169 PortMatrixBody::compute_rectangles ()
171 /* full sizes of components */
172 std::pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
173 std::pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
174 std::pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
176 Gdk::Rectangle col_rect;
177 Gdk::Rectangle row_rect;
178 Gdk::Rectangle grid_rect;
180 if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
182 /* build from top left */
188 if (_alloc_width > col.first) {
189 col_rect.set_width (col.first);
191 col_rect.set_width (_alloc_width);
194 /* move down to y division */
197 if (_alloc_height > col.second) {
203 col_rect.set_height (y);
205 row_rect.set_height (_alloc_height - y);
207 grid_rect.set_height (_alloc_height - y);
209 /* move right to x division */
212 if (_alloc_width > (grid.first + row.first)) {
214 } else if (_alloc_width > row.first) {
215 x = _alloc_width - row.first;
218 grid_rect.set_width (x);
220 row_rect.set_width (_alloc_width - x);
223 } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
225 /* build from bottom right */
227 /* move left to x division */
230 if (_alloc_width > (grid.first + row.first)) {
232 } else if (_alloc_width > row.first) {
233 x = _alloc_width - row.first;
236 grid_rect.set_x (_alloc_width - x);
237 grid_rect.set_width (x);
238 col_rect.set_width (col.first - grid.first + x);
239 col_rect.set_x (_alloc_width - col_rect.get_width());
241 row_rect.set_width (std::min (_alloc_width - x, row.first));
242 row_rect.set_x (_alloc_width - x - row_rect.get_width());
244 /* move up to the y division */
247 if (_alloc_height > col.second) {
253 col_rect.set_y (_alloc_height - y);
254 col_rect.set_height (y);
256 grid_rect.set_height (std::min (grid.second, _alloc_height - y));
257 grid_rect.set_y (_alloc_height - y - grid_rect.get_height());
259 row_rect.set_height (grid_rect.get_height());
260 row_rect.set_y (grid_rect.get_y());
263 _row_labels->set_parent_rectangle (row_rect);
264 _column_labels->set_parent_rectangle (col_rect);
265 _grid->set_parent_rectangle (grid_rect);
269 PortMatrixBody::setup ()
271 /* Discard any old connections to bundles */
273 for (std::list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
276 _bundle_connections.clear ();
278 /* Connect to bundles so that we find out when their names change */
280 PortGroup::BundleList r = _matrix->rows()->bundles ();
281 for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
283 _bundle_connections.push_back (
284 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
289 PortGroup::BundleList c = _matrix->columns()->bundles ();
290 for (PortGroup::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
291 _bundle_connections.push_back (
292 i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels)))
296 _column_labels->setup ();
297 _row_labels->setup ();
300 set_mouseover (PortMatrixNode ());
301 compute_rectangles ();
305 PortMatrixBody::full_scroll_width ()
307 return _grid->dimensions().first;
312 PortMatrixBody::alloc_scroll_width ()
314 return _grid->parent_rectangle().get_width();
318 PortMatrixBody::full_scroll_height ()
320 return _grid->dimensions().second;
324 PortMatrixBody::alloc_scroll_height ()
326 return _grid->parent_rectangle().get_height();
330 PortMatrixBody::set_xoffset (uint32_t xo)
337 PortMatrixBody::set_yoffset (uint32_t yo)
344 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
346 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
348 _grid->button_press (
349 _grid->parent_to_component_x (ev->x),
350 _grid->parent_to_component_y (ev->y),
354 } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
356 _row_labels->button_press (
357 _row_labels->parent_to_component_x (ev->x),
358 _row_labels->parent_to_component_y (ev->y),
362 } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
364 _column_labels->button_press (
365 _column_labels->parent_to_component_x (ev->x),
366 _column_labels->parent_to_component_y (ev->y),
375 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
377 if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
378 Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
380 _row_labels->clear_channel_highlights ();
381 _column_labels->clear_channel_highlights ();
389 PortMatrixBody::rebuild_and_draw_grid ()
391 _grid->require_rebuild ();
396 PortMatrixBody::rebuild_and_draw_column_labels ()
398 _column_labels->require_rebuild ();
403 PortMatrixBody::rebuild_and_draw_row_labels ()
405 _row_labels->require_rebuild ();
410 PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
412 if (ev->type == GDK_LEAVE_NOTIFY) {
413 set_mouseover (PortMatrixNode ());
420 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
422 if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
423 _grid->mouseover_event (
424 _grid->parent_to_component_x (ev->x),
425 _grid->parent_to_component_y (ev->y)
427 _mouse_over_grid = true;
429 if (_mouse_over_grid) {
430 set_mouseover (PortMatrixNode ());
431 _mouse_over_grid = false;
439 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
441 if (n == _mouseover) {
445 PortMatrixNode old = _mouseover;
448 _grid->mouseover_changed (old);
449 _row_labels->mouseover_changed (old);
450 _column_labels->mouseover_changed (old);
456 PortMatrixBody::highlight_associated_channels (int dim, uint32_t N)
458 ARDOUR::BundleChannel bc[2];
460 PortGroup::BundleList const a = _matrix->ports(dim)->bundles ();
461 for (PortGroup::BundleList::const_iterator i = a.begin(); i != a.end(); ++i) {
462 if (N < i->bundle->nchannels ()) {
463 bc[dim] = ARDOUR::BundleChannel (i->bundle, N);
466 N -= i->bundle->nchannels ();
470 if (!bc[dim].bundle) {
474 if (dim == _matrix->column_index()) {
475 _column_labels->add_channel_highlight (bc[dim]);
477 _row_labels->add_channel_highlight (bc[dim]);
480 PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
482 for (PortGroup::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
483 for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
484 bc[1 - dim] = ARDOUR::BundleChannel (i->bundle, j);
485 if (_matrix->get_state (bc) == PortMatrixNode::ASSOCIATED) {
486 if (dim == _matrix->column_index()) {
487 _row_labels->add_channel_highlight (bc[1 - dim]);
489 _column_labels->add_channel_highlight (bc[1 - dim]);
497 PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
499 cairo_rectangle (cr, r.get_x(), r.get_y(), r.get_width(), r.get_height());
504 PortMatrixBody::component_size_changed ()
506 compute_rectangles ();
507 _matrix->setup_scrollbars ();