Gracefully handle templates that lack contents in their description or created_with...
[ardour.git] / gtk2_ardour / port_matrix_component.cc
index 6a60110f3e4cd36909ff01b6d314ce09ab14236a..5e11527d299fefc4f4369d7021905a0541320979 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
@@ -24,7 +24,8 @@
 using namespace std;
 
 /** Constructor.
- *  @param p Port matrix that we're in.
+ *  @param m Port matrix that we're in.
+ *  @param b Port matrix body that we're in.
  */
 PortMatrixComponent::PortMatrixComponent (PortMatrix* m, PortMatrixBody* b)
        : _matrix (m),
@@ -33,14 +34,14 @@ PortMatrixComponent::PortMatrixComponent (PortMatrix* m, PortMatrixBody* b)
          _render_required (true),
          _dimension_computation_required (true)
 {
-       
+
 }
 
 /** Destructor */
 PortMatrixComponent::~PortMatrixComponent ()
 {
        if (_pixmap) {
-               gdk_pixmap_unref (_pixmap);
+               g_object_unref (_pixmap);
        }
 }
 
@@ -73,7 +74,7 @@ PortMatrixComponent::get_pixmap (GdkDrawable *drawable)
 
                /* make a pixmap of the right size */
                if (_pixmap) {
-                       gdk_pixmap_unref (_pixmap);
+                       g_object_unref (_pixmap);
                }
                _pixmap = gdk_pixmap_new (drawable, _width, _height, -1);
 
@@ -118,94 +119,98 @@ PortMatrixComponent::background_colour ()
        return _matrix->get_style()->get_bg (Gtk::STATE_NORMAL);
 }
 
+/** @param g Group.
+ *  @return Visible size of the group in grid units, taking visibility and show_only_bundles into account.
+ */
 uint32_t
-PortMatrixComponent::group_width (boost::shared_ptr<const PortGroup> g) const
+PortMatrixComponent::group_size (boost::shared_ptr<const PortGroup> g) const
 {
-       uint32_t width = 0;
-       
-       if (g->visible()) {
-               PortGroup::BundleList const & bundles = g->bundles ();
-               if (_matrix->show_only_bundles()) {
-                       width = bundles.size() * column_width ();
-               } else {
-                       for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
-                               width += i->bundle->nchannels() * column_width ();
-                       }
-               }
+       uint32_t s = 0;
+
+       PortGroup::BundleList const & bundles = g->bundles ();
+       if (_matrix->show_only_bundles()) {
+               s = bundles.size();
        } else {
-               width = column_width ();
+               for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
+                       s += _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels());
+               }
        }
 
-       return width;
+       return s;
 }
 
+/** @param bc Channel.
+ *  @param group Group.
+ *  @return Position of bc in groups in grid units, taking show_only_bundles into account.
+ */
 uint32_t
-PortMatrixComponent::group_height (boost::shared_ptr<const PortGroup> g) const
+PortMatrixComponent::channel_to_position (ARDOUR::BundleChannel bc, boost::shared_ptr<const PortGroup> group) const
 {
-       uint32_t height = 0;
+       uint32_t p = 0;
+
+       PortGroup::BundleList const & bundles = group->bundles ();
+
+       for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
+
+               if ((*i)->bundle == bc.bundle) {
+
+                       /* found the bundle */
+
+                       if (_matrix->show_only_bundles()) {
+                               return p;
+                       } else {
+                               return p + bc.bundle->overall_channel_to_type (_matrix->type (), bc.channel);
+                       }
+
+               }
+
+               /* move past this bundle */
 
-       if (g->visible ()) {
-               PortGroup::BundleList const & bundles = g->bundles ();
                if (_matrix->show_only_bundles()) {
-                       height = bundles.size() * row_height ();
+                       p += 1;
                } else {
-                       for (PortGroup::BundleList::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
-                               height += i->bundle->nchannels() * row_height ();
-                       }
+                       p += _matrix->count_of_our_type_min_1 ((*i)->bundle->nchannels());
                }
-       } else {
-               height = row_height ();
        }
 
-       return height;
+       return 0;
 }
 
 
-pair<boost::shared_ptr<PortGroup>, ARDOUR::BundleChannel>
-PortMatrixComponent::y_position_to_group_and_channel (double y) const
+ARDOUR::BundleChannel
+PortMatrixComponent::position_to_channel (double p, double, boost::shared_ptr<const PortGroup> group) const
 {
-       PortGroupList::List::const_iterator i = _matrix->rows()->begin();
+       p /= grid_spacing ();
 
-       while (i != _matrix->rows()->end()) {
+       PortGroup::BundleList const & bundles = group->bundles ();
+       for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
 
-               uint32_t const gh = group_height (*i);
+               if (_matrix->show_only_bundles()) {
 
-               if (y < gh) {
+                       if (p < 1) {
+                               return ARDOUR::BundleChannel ((*j)->bundle, -1);
+                       } else {
+                               p -= 1;
+                       }
 
-                       /* it's in this group */
+               } else {
 
-                       PortGroup::BundleList const & bundles = (*i)->bundles ();
-                       for (PortGroup::BundleList::const_iterator j = bundles.begin(); j != bundles.end(); ++j) {
+                       ARDOUR::ChanCount const N = (*j)->bundle->nchannels ();
 
-                               if (_matrix->show_only_bundles()) {
-                                       
-                                       if (y < row_height()) {
-                                               return make_pair (*i, ARDOUR::BundleChannel (j->bundle, 0));
-                                       } else {
-                                               y -= row_height ();
-                                       }
-                                       
+                       uint32_t const s = _matrix->count_of_our_type_min_1 (N);
+                       if (p < s) {
+                               if (p < _matrix->count_of_our_type (N)) {
+                                       return ARDOUR::BundleChannel ((*j)->bundle, (*j)->bundle->type_channel_to_overall (_matrix->type (), p));
                                } else {
-
-                                       uint32_t const h = j->bundle->nchannels () * row_height ();
-                                       if (y < h) {
-                                               return make_pair (*i, ARDOUR::BundleChannel (j->bundle, y / row_height()));
-                                       } else {
-                                               y -= h;
-                                       }
-
+                                       return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), -1);
                                }
-
+                       } else {
+                               p -= s;
                        }
 
-               } else {
-
-                       y -= gh;
-
                }
 
-               ++i;
        }
 
-       return make_pair (boost::shared_ptr<PortGroup> (), ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0));
+       return ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), -1);
 }