Fix a couple of crashes with empty matrices. Some small optimisations.
[ardour.git] / gtk2_ardour / port_matrix_body.cc
index a29f2483e34afd081001e0cc9ce961b5da22100e..72da040ed70f6b5c8ebbd90309c8cba8840a2a73 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002-2009 Paul Davis 
+    Copyright (C) 2002-2009 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -30,108 +30,105 @@ using namespace std;
 
 PortMatrixBody::PortMatrixBody (PortMatrix* p)
        : _matrix (p),
+         _alloc_width (0),
+         _alloc_height (0),
          _xoffset (0),
          _yoffset (0),
-         _mouse_over_grid (false)
+         _column_labels_border_x (0),
+         _column_labels_height (0),
+         _ignore_component_size_changed (false)
 {
        _column_labels = new PortMatrixColumnLabels (p, this);
        _row_labels = new PortMatrixRowLabels (p, this);
        _grid = new PortMatrixGrid (p, this);
-       
+
+       _components.push_back (_column_labels);
+       _components.push_back (_row_labels);
+       _components.push_back (_grid);
+
        add_events (Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
 }
 
 
 PortMatrixBody::~PortMatrixBody ()
 {
-       delete _column_labels;
-       delete _row_labels;
-       delete _grid;
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               delete *i;
+       }
 }
 
 bool
 PortMatrixBody::on_expose_event (GdkEventExpose* event)
 {
+       if (
+               _matrix->visible_columns() == 0 || _matrix->visible_rows() == 0 ||
+               _matrix->visible_columns()->bundles().empty() || _matrix->visible_rows()->bundles().empty()
+               ) {
+
+               /* nothing to connect */
+
+               cairo_t* cr = gdk_cairo_create (get_window()->gobj());
+
+               cairo_set_source_rgb (cr, 0, 0, 0);
+               cairo_rectangle (cr, 0, 0, _alloc_width, _alloc_height);
+               cairo_fill (cr);
+
+               stringstream t;
+               t << _("There are no ") << (_matrix->type() == ARDOUR::DataType::AUDIO ? _("audio") : _("MIDI")) << _(" ports to connect.");
+
+               cairo_text_extents_t ext;
+               cairo_text_extents (cr, t.str().c_str(), &ext);
+
+               cairo_set_source_rgb (cr, 1, 1, 1);
+               cairo_move_to (cr, (_alloc_width - ext.width) / 2, (_alloc_height + ext.height) / 2);
+               cairo_show_text (cr, t.str().c_str ());
+
+               cairo_destroy (cr);
+
+               return true;
+       }
+
        Gdk::Rectangle const exposure (
                event->area.x, event->area.y, event->area.width, event->area.height
                );
 
        bool intersects;
-       
-       Gdk::Rectangle r = exposure;
-       /* the get_pixmap call may cause things to be rerendered and sizes to change,
-          so fetch the pixmap before calculating where to put it */
-       GdkPixmap* p = _column_labels->get_pixmap (get_window()->gobj());
-       r.intersect (_column_labels->parent_rectangle(), intersects);
-
-       if (intersects) {
-
-               gdk_draw_drawable (
-                       get_window()->gobj(),
-                       get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
-                       p,
-                       _column_labels->parent_to_component_x (r.get_x()),
-                       _column_labels->parent_to_component_y (r.get_y()),
-                       r.get_x(),
-                       r.get_y(),
-                       r.get_width(),
-                       r.get_height()
-                       );
-       }
 
-       r = exposure;
-       p = _row_labels->get_pixmap (get_window()->gobj());
-       r.intersect (_row_labels->parent_rectangle(), intersects);
-
-       if (intersects) {
-               gdk_draw_drawable (
-                       get_window()->gobj(),
-                       get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
-                       p,
-                       _row_labels->parent_to_component_x (r.get_x()),
-                       _row_labels->parent_to_component_y (r.get_y()),
-                       r.get_x(),
-                       r.get_y(),
-                       r.get_width(),
-                       r.get_height()
-                       );
-       }
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+
+               Gdk::Rectangle r = exposure;
+
+               /* the get_pixmap call may cause things to be rerendered and sizes to change,
+                  so fetch the pixmap before calculating where to put it */
+               GdkPixmap* p = (*i)->get_pixmap (get_window()->gobj());
+               r.intersect ((*i)->parent_rectangle(), intersects);
+
+               if (intersects) {
+                       
+                       gdk_draw_drawable (
+                               get_window()->gobj(),
+                               get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
+                               p,
+                               (*i)->parent_to_component_x (r.get_x()),
+                               (*i)->parent_to_component_y (r.get_y()),
+                               r.get_x(),
+                               r.get_y(),
+                               r.get_width(),
+                               r.get_height()
+                               );
+               }
 
-       r = exposure;
-       p = _grid->get_pixmap (get_window()->gobj());
-       r.intersect (_grid->parent_rectangle(), intersects);
-
-       if (intersects) {
-               gdk_draw_drawable (
-                       get_window()->gobj(),
-                       get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
-                       p,
-                       _grid->parent_to_component_x (r.get_x()),
-                       _grid->parent_to_component_y (r.get_y()),
-                       r.get_x(),
-                       r.get_y(),
-                       r.get_width(),
-                       r.get_height()
-                       );
        }
 
        cairo_t* cr = gdk_cairo_create (get_window()->gobj());
 
-       cairo_save (cr);
-       set_cairo_clip (cr, _grid->parent_rectangle ());
-       _grid->draw_extra (cr);
-       cairo_restore (cr);
-
-       cairo_save (cr);
-       set_cairo_clip (cr, _row_labels->parent_rectangle ());
-       _row_labels->draw_extra (cr);
-       cairo_restore (cr);
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               cairo_save (cr);
+               set_cairo_clip (cr, (*i)->parent_rectangle ());
+               (*i)->draw_extra (cr);
+               cairo_restore (cr);
+       }
 
-       cairo_save (cr);
-       set_cairo_clip (cr, _column_labels->parent_rectangle ());
-       _column_labels->draw_extra (cr);
-       cairo_restore (cr);
-       
        cairo_destroy (cr);
 
        return true;
@@ -144,6 +141,13 @@ PortMatrixBody::on_size_request (Gtk::Requisition *req)
        pair<int, int> const row = _row_labels->dimensions ();
        pair<int, int> const grid = _grid->dimensions ();
 
+       if (grid.first == 0 && grid.second == 0) {
+               /* nothing to display */
+               req->width = 256;
+               req->height = 64;
+               return;
+       }
+
        /* don't ask for the maximum size of our contents, otherwise GTK won't
           let the containing window shrink below this size */
 
@@ -172,7 +176,7 @@ PortMatrixBody::compute_rectangles ()
 {
        /* full sizes of components */
        pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
-       uint32_t col_overhang = _column_labels->overhang ();
+       uint32_t const col_overhang = _column_labels->overhang ();
        pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
        pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
 
@@ -183,6 +187,7 @@ PortMatrixBody::compute_rectangles ()
        if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
 
                col_rect.set_x (0);
+               _column_labels_border_x = col_overhang;
                col_rect.set_y (0);
                grid_rect.set_x (0);
 
@@ -205,12 +210,12 @@ PortMatrixBody::compute_rectangles ()
                grid_rect.set_width (x);
                row_rect.set_x (x);
                row_rect.set_width (_alloc_width - x);
-                       
+
 
        } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
 
                col_rect.set_height (min (_alloc_height, col.second));
-               
+
                row_rect.set_x (0);
                row_rect.set_y (0);
                row_rect.set_width (min (_alloc_width, row.first));
@@ -223,49 +228,56 @@ PortMatrixBody::compute_rectangles ()
 
                col_rect.set_width (grid_rect.get_width () + col_overhang);
                col_rect.set_x (row_rect.get_width() + grid_rect.get_width() - col_rect.get_width());
+               _column_labels_border_x = col_rect.get_x () >= 0 ? col_rect.get_x () : 0;
                col_rect.set_y (row_rect.get_height());
-               
        }
 
+       _column_labels_height = col_rect.get_height ();
+
        _row_labels->set_parent_rectangle (row_rect);
        _column_labels->set_parent_rectangle (col_rect);
        _grid->set_parent_rectangle (grid_rect);
+
+       DimensionsChanged (); /* EMIT SIGNAL */
 }
 
 void
 PortMatrixBody::setup ()
 {
        /* Discard any old connections to bundles */
-       
+
        for (list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
                i->disconnect ();
        }
        _bundle_connections.clear ();
 
        /* Connect to bundles so that we find out when their names change */
-       
-       PortGroup::BundleList r = _matrix->rows()->bundles ();
+
+       PortGroup::BundleList r = _matrix->visible_rows()->bundles ();
        for (PortGroup::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
-               
+
                _bundle_connections.push_back (
                        i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels)))
                        );
-               
+
        }
 
-       PortGroup::BundleList c = _matrix->columns()->bundles ();
+       PortGroup::BundleList c = _matrix->visible_columns()->bundles ();
        for (PortGroup::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
                _bundle_connections.push_back (
                        i->bundle->Changed.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels)))
                        );
        }
-       
-       _column_labels->setup ();
-       _row_labels->setup ();
-       _grid->setup ();
+
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               (*i)->setup ();
+       }
 
        set_mouseover (PortMatrixNode ());
+       
+       _ignore_component_size_changed = true;
        compute_rectangles ();
+       _ignore_component_size_changed = false;
 }
 
 uint32_t
@@ -293,6 +305,7 @@ PortMatrixBody::alloc_scroll_height ()
        return _grid->parent_rectangle().get_height();
 }
 
+/** Set x offset (for scrolling) */
 void
 PortMatrixBody::set_xoffset (uint32_t xo)
 {
@@ -300,6 +313,7 @@ PortMatrixBody::set_xoffset (uint32_t xo)
        queue_draw ();
 }
 
+/** Set y offset (for scrolling) */
 void
 PortMatrixBody::set_yoffset (uint32_t yo)
 {
@@ -310,29 +324,14 @@ PortMatrixBody::set_yoffset (uint32_t yo)
 bool
 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
 {
-       if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
-
-               _grid->button_press (
-                       _grid->parent_to_component_x (ev->x),
-                       _grid->parent_to_component_y (ev->y),
-                       ev->button, ev->time
-                       );
-
-       } else if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
-
-               _row_labels->button_press (
-                       _row_labels->parent_to_component_x (ev->x),
-                       _row_labels->parent_to_component_y (ev->y),
-                       ev->button, ev->time
-                       );
-       
-       } else if (Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
-
-               _column_labels->button_press (
-                       _column_labels->parent_to_component_x (ev->x),
-                       _column_labels->parent_to_component_y (ev->y),
-                       ev->button, ev->time
-                       );
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               if (Gdk::Region ((*i)->parent_rectangle()).point_in (ev->x, ev->y)) {
+                       (*i)->button_press (
+                               (*i)->parent_to_component_x (ev->x),
+                               (*i)->parent_to_component_y (ev->y),
+                               ev->button, ev->time, ev->state
+                               );
+               }
        }
 
        return true;
@@ -341,20 +340,19 @@ PortMatrixBody::on_button_press_event (GdkEventButton* ev)
 bool
 PortMatrixBody::on_button_release_event (GdkEventButton* ev)
 {
-       if (Gdk::Region (_row_labels->parent_rectangle()).point_in (ev->x, ev->y) ||
-           Gdk::Region (_column_labels->parent_rectangle()).point_in (ev->x, ev->y)) {
-
-               _row_labels->clear_channel_highlights ();
-               _column_labels->clear_channel_highlights ();
-               
-       } else if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
-
-               _grid->button_release (
-                       _grid->parent_to_component_x (ev->x),
-                       _grid->parent_to_component_y (ev->y),
-                       ev->button, ev->time
-                       );
-
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               if (Gdk::Region ((*i)->parent_rectangle()).point_in (ev->x, ev->y)) {
+                       (*i)->button_release (
+                               (*i)->parent_to_component_x (ev->x),
+                               (*i)->parent_to_component_y (ev->y),
+                               ev->button, ev->time, ev->state
+                               );
+               } else {
+                       (*i)->button_release (
+                               -1, -1,
+                               ev->button, ev->time, ev->state
+                               );
+               }
        }
 
        return true;
@@ -394,38 +392,59 @@ PortMatrixBody::on_leave_notify_event (GdkEventCrossing* ev)
 bool
 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
 {
-       if (Gdk::Region (_grid->parent_rectangle()).point_in (ev->x, ev->y)) {
-               
-               _grid->motion (
-                       _grid->parent_to_component_x (ev->x),
-                       _grid->parent_to_component_y (ev->y)
-                       );
-               
-               _mouse_over_grid = true;
-               
-       } else {
-               if (_mouse_over_grid) {
-                       set_mouseover (PortMatrixNode ());
-                       _mouse_over_grid = false;
+       bool done = false;
+       
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               if (Gdk::Region ((*i)->parent_rectangle()).point_in (ev->x, ev->y)) {
+                       (*i)->motion (
+                               (*i)->parent_to_component_x (ev->x),
+                               (*i)->parent_to_component_y (ev->y)
+                               );
+
+                       done = true;
                }
        }
+                       
+
+       if (!done) {
+               set_mouseover (PortMatrixNode ());
+       }
 
        return true;
 }
 
 void
 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
+{
+       list<PortMatrixNode> m;
+       m.push_back (n);
+       set_mouseover (m);
+}
+
+void
+PortMatrixBody::set_mouseover (list<PortMatrixNode> const & n)
 {
        if (n == _mouseover) {
                return;
        }
 
-       PortMatrixNode old = _mouseover;
+       /* Channel highlights are set up only on mouseovers, so
+          it's reasonable to remove all channel highlights here.
+          We can't let individual components clear their own highlights
+          because of the case where, say, the row labels set up some column
+          highlights, and then we ask the column labels to set up their
+          own highlights and they clear them out before they start.
+       */
+
+       _row_labels->clear_channel_highlights ();
+       _column_labels->clear_channel_highlights ();
+
+       list<PortMatrixNode> old = _mouseover;
        _mouseover = n;
        
-       _grid->mouseover_changed (old);
-       _row_labels->mouseover_changed (old);
-       _column_labels->mouseover_changed (old);
+       for (list<PortMatrixComponent*>::iterator i = _components.begin(); i != _components.end(); ++i) {
+               (*i)->mouseover_changed (old);
+       }
 }
 
 void
@@ -444,7 +463,7 @@ PortMatrixBody::highlight_associated_channels (int dim, ARDOUR::BundleChannel h)
                _row_labels->add_channel_highlight (bc[dim]);
        }
 
-       PortGroup::BundleList const b = _matrix->ports(1 - dim)->bundles ();
+       PortGroup::BundleList const b = _matrix->visible_ports(1 - dim)->bundles ();
 
        for (PortGroup::BundleList::const_iterator i = b.begin(); i != b.end(); ++i) {
                for (uint32_t j = 0; j < i->bundle->nchannels(); ++j) {
@@ -470,6 +489,10 @@ PortMatrixBody::set_cairo_clip (cairo_t* cr, Gdk::Rectangle const & r) const
 void
 PortMatrixBody::component_size_changed ()
 {
+       if (_ignore_component_size_changed) {
+               return;
+       }
+       
        compute_rectangles ();
        _matrix->setup_scrollbars ();
 }
@@ -480,6 +503,19 @@ PortMatrixBody::max_size () const
        pair<uint32_t, uint32_t> const col = _column_labels->dimensions ();
        pair<uint32_t, uint32_t> const row = _row_labels->dimensions ();
        pair<uint32_t, uint32_t> const grid = _grid->dimensions ();
-       
+
        return make_pair (std::max (row.first, _column_labels->overhang()) + grid.first, col.second + grid.second);
 }
+
+/** @return x position at which the column labels meet the border of the matrix */
+uint32_t
+PortMatrixBody::column_labels_border_x () const
+{
+       return _column_labels_border_x;
+}
+
+uint32_t
+PortMatrixBody::column_labels_height () const
+{
+       return _column_labels_height;
+}