+ /* Find the IOs which have bundles for routes and their processors. We store
+ these IOs in a RouteIOs class so that we can then sort the results by route
+ order key.
+ */
+
+ boost::shared_ptr<RouteList> routes = session->get_routes ();
+ list<RouteIOs> route_ios;
+
+ for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
+
+ /* we never show the monitor bus inputs */
+
+ if (inputs && (*i)->is_monitor()) {
+ continue;
+ }
+
+ /* keep track of IOs that we have taken bundles from,
+ so that we can avoid taking the same IO from both
+ Route::output() and the main_outs Delivery
+ */
+
+ set<boost::shared_ptr<IO> > used_io;
+ boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
+ used_io.insert (io);
+
+ RouteIOs rb (*i, io);
+ (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
+
+ route_ios.push_back (rb);
+ }
+
+ /* Sort RouteIOs by the routes' editor order keys */
+ route_ios.sort (RouteIOsComparator ());
+
+ /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
+ Note that if the RouteIO's bundles are multi-type, we may make new Bundles
+ with only the ports of one type.
+ */
+
+ for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
+ TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
+
+ /* Work out which group to put these IOs' bundles in */
+ boost::shared_ptr<PortGroup> g;
+ if (boost::dynamic_pointer_cast<Track> (i->route)) {
+ g = track;
+ } else {
+ g = bus;
+ }
+
+ for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
+ if (tv) {
+ g->add_bundle ((*j)->bundle(), *j, tv->color ());
+ } else {
+ g->add_bundle ((*j)->bundle(), *j);