cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[ardour.git] / gtk2_ardour / io_selector.cc
index b0ecd05077994be991656b4713afc658cecbcb34..aa0d1c57ad55e01ad8b0ed3c23579e2a2374c9e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002-2003 Paul Davis 
+    Copyright (C) 2002-2007 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
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
-#include <map>
-#include <vector>
-
 #include <gtkmm/messagedialog.h>
-
-#include <glibmm/thread.h>
-
-#include <ardour/io.h>
-#include <ardour/route.h>
-#include <ardour/audioengine.h>
-#include <ardour/port.h>
-#include <ardour/insert.h>
-#include <ardour/session.h>
-#include <ardour/audio_diskstream.h>
+#include <glibmm/objectbase.h>
 
 #include <gtkmm2ext/doi.h>
-#include <gtkmm2ext/gtk_ui.h>
-#include <gtkmm2ext/utils.h>
 
-#include "utils.h"
+#include "ardour/port_insert.h"
+#include "ardour/session.h"
+#include "ardour/io.h"
+#include "ardour/audioengine.h"
+#include "ardour/track.h"
+#include "ardour/audio_track.h"
+#include "ardour/midi_track.h"
+#include "ardour/data_type.h"
+#include "ardour/port.h"
+#include "ardour/bundle.h"
+
 #include "io_selector.h"
-#include "keyboard.h"
+#include "utils.h"
 #include "gui_thread.h"
-
 #include "i18n.h"
 
-using namespace std;
-using namespace Gtk;
-using namespace Glib;
-using namespace sigc;
 using namespace ARDOUR;
-using namespace PBD;
-using namespace Gtkmm2ext;
-
-IOSelectorWindow::IOSelectorWindow (Session& sess, IO& ior, bool input, bool can_cancel)
-       : ArdourDialog ("i/o selector"),
-         _selector (sess, ior, input),
-         ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
+using namespace Gtk;
 
+IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
+       : PortMatrix (p, session, io->default_type())
+       , _io (io)
 {
-       add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
-       set_name ("IOSelectorWindow");
+       /* signal flow from 0 to 1 */
 
-       string title;
-       if (input) {
-               title = string_compose(_("%1 input"), ior.name());
-       } else {
-               title = string_compose(_("%1 output"), ior.name());
-       }
+       _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
 
-       ok_button.set_name ("IOSelectorButton");
-       cancel_button.set_name ("IOSelectorButton");
-       rescan_button.set_name ("IOSelectorButton");
-
-       button_box.set_spacing (5);
-       button_box.set_border_width (5);
-       button_box.set_homogeneous (true);
-       button_box.pack_start (rescan_button);
-
-       if (can_cancel) {
-               button_box.pack_start (cancel_button);
+       if (_find_inputs_for_io_outputs) {
+               _other = 1;
+               _ours = 0;
        } else {
-               cancel_button.hide();
+               _other = 0;
+               _ours = 1;
        }
-               
-       button_box.pack_start (ok_button);
-
-       get_vbox()->pack_start (_selector);
-       get_vbox()->pack_start (button_box, false, false);
-
-       ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
-       cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
-       rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
-
-       set_title (title);
-       set_position (WIN_POS_MOUSE);
-
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
-}
-
-IOSelectorWindow::~IOSelectorWindow()
-{
-}
-
-void
-IOSelectorWindow::rescan ()
-{
-       _selector.redisplay ();
-}
 
-void
-IOSelectorWindow::cancel ()
-{
-       _selector.Finished(IOSelector::Cancelled);
-       hide ();
-}
+       _port_group.reset (new PortGroup (io->name()));
+       _ports[_ours].add_group (_port_group);
 
-void
-IOSelectorWindow::accept ()
-{
-       _selector.Finished(IOSelector::Accepted);
-       hide ();
+       setup_all_ports ();
+       init ();
 }
 
 void
-IOSelectorWindow::on_map ()
-{
-       _selector.redisplay ();
-       Window::on_map ();
-}
-
-/*************************
-  The IO Selector "widget"
- *************************/  
-
-IOSelector::IOSelector (Session& sess, IO& ior, bool input)
-       : session (sess),
-         io (ior),
-         for_input (input),
-         port_frame (for_input? _("Inputs") : _("Outputs")),
-         add_port_button (for_input? _("Add Input") : _("Add Output")),
-         remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
-         clear_connections_button (_("Disconnect All"))
+IOSelector::setup_ports (int dim)
 {
-       selected_port = 0;
-
-       notebook.set_name ("IOSelectorNotebook");
-       notebook.set_size_request (-1, 125);
-
-       clear_connections_button.set_name ("IOSelectorButton");
-       add_port_button.set_name ("IOSelectorButton");
-       remove_port_button.set_name ("IOSelectorButton");
-
-       selector_frame.set_name ("IOSelectorFrame");
-       port_frame.set_name ("IOSelectorFrame");
-
-       selector_frame.set_label (_("Available connections"));
-       
-       selector_button_box.set_spacing (5);
-       selector_button_box.set_border_width (5);
-
-       selector_box.set_spacing (5);
-       selector_box.set_border_width (5);
-       selector_box.pack_start (notebook);
-       selector_box.pack_start (selector_button_box, false, false);
-
-       selector_frame.add (selector_box);
-
-       port_box.set_spacing (5);
-       port_box.set_border_width (5);
-
-       port_display_scroller.set_name ("IOSelectorNotebook");
-       port_display_scroller.set_border_width (0);
-       port_display_scroller.set_size_request (-1, 170);
-       port_display_scroller.add (port_box);
-       port_display_scroller.set_policy (POLICY_NEVER,
-                                         POLICY_AUTOMATIC);
-
-       port_button_box.set_spacing (5);
-       port_button_box.set_border_width (5);
-
-       port_button_box.pack_start (add_port_button, false, false);
-
-       if (for_input) {
-               if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
-                       add_port_button.set_sensitive (true);
-               } else {
-                       add_port_button.set_sensitive (false);
-               }
-
-       } else {
-               if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
-                       add_port_button.set_sensitive (true);
-               } else {
-                       add_port_button.set_sensitive (false);
-               }
-                       
-       }
-
-       port_button_box.pack_start (remove_port_button, false, false);
-
-       if (for_input) {
-               if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
-                       remove_port_button.set_sensitive (true);
-               } else {
-                       remove_port_button.set_sensitive (false);
-               }
-                       
-       } else {
-               if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
-                       remove_port_button.set_sensitive (true);
-               } else {
-                       remove_port_button.set_sensitive (false);
-               }
-       }
-
-       port_button_box.pack_start (clear_connections_button, false, false);
-
-       port_and_button_box.set_border_width (5);
-       port_and_button_box.pack_start (port_button_box, false, false);
-       port_and_button_box.pack_start (port_display_scroller);
-
-       port_frame.add (port_and_button_box);
-
-       port_and_selector_box.set_spacing (5);
-       port_and_selector_box.pack_start (port_frame);
-       port_and_selector_box.pack_start (selector_frame);
-
-       set_spacing (5);
-       set_border_width (5);
-       pack_start (port_and_selector_box);
-
-       rescan();
-       display_ports ();
-
-       clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
-
-       add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
-       remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
-
-       if (for_input) {
-               io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
-       } else {
-               io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
-       }
-
-       io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
-}
-
-IOSelector::~IOSelector ()
-{
-}
-
-void
-IOSelector::name_changed (void* src)
-{
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
-       
-       display_ports ();
-}
-
-void
-IOSelector::clear_connections ()
-{
-       if (for_input) {
-               io.disconnect_inputs (this);
-       } else {
-               io.disconnect_outputs (this);
-       }
-}
-
-void
-IOSelector::rescan ()
-{
-       using namespace Notebook_Helpers;
-
-       typedef std::map<string,vector<pair<string,string> > > PortMap;
-       PortMap portmap;
-       const char **ports;
-       PageList& pages = notebook.pages();
-       gint current_page;
-       vector<string> rowdata;
-
-       current_page = notebook.get_current_page ();
-       pages.clear ();
-
-       /* get relevant current JACK ports */
-
-       ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
-
-       if (ports == 0) {
+       if (!_session) {
                return;
        }
-
-       /* find all the client names and group their ports into a list-by-client */
        
-       for (int n = 0; ports[n]; ++n) {
-
-               pair<string,vector<pair<string,string> > > newpair;
-               pair<string,string> strpair;
-               pair<PortMap::iterator,bool> result;
-
-               string str = ports[n];
-               string::size_type pos;
-               string portname;
-
-               pos = str.find (':');
+       _ports[dim].suspend_signals ();
 
-               newpair.first = str.substr (0, pos);
-               portname = str.substr (pos+1);
+       if (dim == _other) {
 
-               result = portmap.insert (newpair);
+               _ports[_other].gather (_session, _find_inputs_for_io_outputs, false);
 
-               strpair.first = portname;
-               strpair.second = str;
-
-               result.first->second.push_back (strpair);
-       }
-
-       PortMap::iterator i;
-
-       for (i = portmap.begin(); i != portmap.end(); ++i) {
-               
-               Box *client_box = manage (new VBox);
-               TreeView *display = manage (new TreeView);
-               RefPtr<ListStore> model = ListStore::create (port_display_columns);
-               ScrolledWindow *scroller = manage (new ScrolledWindow);
-
-               display->set_model (model);
-               display->append_column (X_("notvisible"), port_display_columns.displayed_name);
-               display->set_headers_visible (false);
-               display->get_selection()->set_mode (SELECTION_SINGLE);
-               display->set_name ("IOSelectorList");
-
-               for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
-                       
-                       TreeModel::Row row = *(model->append ());
-
-                       row[port_display_columns.displayed_name] = s->first;
-                       row[port_display_columns.full_name] = s->second;
-               }
-
-               //display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
-               display->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
-               Label *tab_label = manage (new Label);
-
-               tab_label->set_name ("IOSelectorNotebookTab");
-               tab_label->set_text ((*i).first);
-
-               scroller->add (*display);
-               scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
-
-               client_box->pack_start (*scroller);
+       } else {
 
-               pages.push_back (TabElem (*client_box, *tab_label));
+               _port_group->clear ();
+               _port_group->add_bundle (_io->bundle (), _io);
        }
 
-       notebook.set_current_page (current_page);
-       //notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
-       selector_box.show_all ();
-}      
+       _ports[dim].resume_signals ();
+}
 
 void
-IOSelector::display_ports ()
+IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
 {
-       TreeView *firsttview = 0;
-       TreeView *selected_port_tview = 0;
-
-       {
-               Glib::Mutex::Lock lm  (port_display_lock);
-               Port *port;
-               uint32_t limit;
-
-               if (for_input) {
-                       limit = io.n_inputs();
-               } else {
-                       limit = io.n_outputs();
-               }
+       ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
+       ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
 
-               for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
-               
-                       slist<TreeView *>::iterator tmp;
+       for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
+               for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
 
-                       tmp = i;
-                       ++tmp;
-
-                       port_box.remove (**i);
-                       delete *i;
-                       port_displays.erase (i);
-
-                       i = tmp;
-               } 
-
-
-               for (uint32_t n = 0; n < limit; ++n) {
-                       
-                       TreeView* tview;
-                       //ScrolledWindow *scroller;
-                       string really_short_name;
-                       
-                       if (for_input) {
-                               port = io.input (n);
-                       } else {
-                               port = io.output (n);
+                       Port* f = _session->engine().get_port_by_name (*i);
+                       if (!f) {
+                               return;
                        }
-                       
-                       /* we know there is '/' because we put it there */
-
-                       really_short_name = port->short_name();
-                       really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
 
-                       tview = manage (new TreeView());
-                       RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
-
-                       if (!firsttview) {
-                               firsttview = tview;
-                       }
-                       
-                       tview->set_model (port_model);
-                       tview->append_column (really_short_name, port_display_columns.displayed_name);
-                       tview->get_selection()->set_mode (SELECTION_SINGLE);
-                       tview->set_data ("port", port);
-                       tview->set_headers_visible (true);
-                       tview->set_name ("IOSelectorPortList");
-                       
-                       port_box.pack_start (*tview);
-                       port_displays.insert (port_displays.end(), tview);
-                       
-                       /* now fill the clist with the current connections */
-                       
-
-                       const char **connections = port->get_connections ();
-
-                       if (connections) {
-                               for (uint32_t c = 0; connections[c]; ++c) {
-                                       TreeModel::Row row = *(port_model->append());
-                                       row[port_display_columns.displayed_name] = connections[c];
-                                       row[port_display_columns.full_name] = connections[c];
-                               }
-                       }
-                       
-                       if (for_input) {
-                               
-                               if (io.input_maximum() == 1) {
-                                       selected_port = port;
-                                       selected_port_tview = tview;
-                               } else {
-                                       if (port == selected_port) {
-                                               selected_port_tview = tview;
-                                       }
-                               }
-                       
+                       if (s) {
+                               _io->connect (f, *j, 0);
                        } else {
-
-                               if (io.output_maximum() == 1) {
-                                       selected_port = port;
-                                       selected_port_tview = tview;
-                               } else {
-                                       if (port == selected_port) {
-                                               selected_port_tview = tview;
-                                       }
-                               }
-                       }
-
-                       TreeViewColumn* col = tview->get_column (0);
-                       
-                       col->set_clickable (true);
-
-                       /* handle button events on the column header and within the treeview itself */
-                       col->signal_clicked().connect (bind (mem_fun(*this, &IOSelector::select_treeview), tview));
-                       tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
-               }
-
-               port_box.show_all ();
-
-               if (selected_port_tview) {
-                       // GTK2FIX
-                       // selected_port_tview->click_column(0);
-                       selected_port_tview->set_name ("IOSelectorPortListSelected");
-                       for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
-                               if (*i != selected_port_tview) {
-                                       (*i)->set_name ("IOSelectorPortList");
-                                       (*i)->queue_draw ();
-                               }
+                               _io->disconnect (f, *j, 0);
                        }
-               } else {
-                       selected_port = 0;
-                       selector_box.hide_all ();
                }
        }
-       
-       if (selected_port_tview) {
-               select_treeview (selected_port_tview);
-       } else if (firsttview) {
-               // select first
-               select_treeview (firsttview);
-       }
 }
 
-bool
-IOSelector::port_selection_changed (GdkEventButton *ev, TreeView* treeview)
+PortMatrixNode::State
+IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
 {
-       TreeModel::iterator i = treeview->get_selection()->get_selected();
-       int status;
+       ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
+       ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
 
-       if (!i) {
-               return 0;
+       if (our_ports.empty() || other_ports.empty()) {
+               /* we're looking at a bundle with no parts associated with this channel,
+                  so nothing to connect */
+               return PortMatrixNode::NOT_ASSOCIATED;
        }
 
-       if (selected_port == 0) {
-               return 0;
-       }
+       for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
+               for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
 
-       ustring other_port_name = (*i)[port_display_columns.full_name];
-       
-       if (for_input) {
-               if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
-                       Port *p = session.engine().get_port_by_name (other_port_name);
-                       p->enable_metering();
-               }
-       } else {
-               status = io.connect_output (selected_port, other_port_name, this);
-       }
+                       Port* f = _session->engine().get_port_by_name (*i);
 
-       if (status == 0) {
-               select_next_treeview ();
-       }
-       
-       treeview->get_selection()->unselect_all();
-       return 0;
-}
-
-void
-IOSelector::ports_changed (IOChange change, void *src)
-{
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
-       
-       display_ports ();
-}
+                       /* since we are talking about an IO, our ports should all have an associated Port *,
+                          so the above call should never fail */
+                       assert (f);
 
-void
-IOSelector::add_port ()
-{
-       /* add a new port, then hide the button if we're up to the maximum allowed */
-
-       if (for_input) {
-
-               try {
-                       io.add_input_port ("", this);
-               }
-
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       MessageDialog msg (0,  _("There are no more JACK ports available."));
-                       msg.run ();
-               }
-
-               if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
-                       add_port_button.set_sensitive (false);
-               }
-               
-               if (io.input_minimum() < (int) io.n_inputs()) {
-                       remove_port_button.set_sensitive (true);
-               }
-
-       } else {
-
-               try {
-                       io.add_output_port ("", this);
-               }
-
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       MessageDialog msg (0, _("There are no more JACK ports available."));
-                       msg.run ();
-               }
-
-               if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
-                       add_port_button.set_sensitive (false);
+                       if (!f->connected_to (*j)) {
+                               /* if any one thing is not connected, all bets are off */
+                               return PortMatrixNode::NOT_ASSOCIATED;
+                       }
                }
        }
+
+       return PortMatrixNode::ASSOCIATED;
 }
 
-void
-IOSelector::remove_port ()
+uint32_t
+IOSelector::n_io_ports () const
 {
-       uint32_t nports;
-
-       // always remove last port
-       
-       if (for_input) {
-               if ((nports = io.n_inputs()) > 0) {
-                       io.remove_input_port (io.input(nports-1), this);
-               }
-               if (io.input_minimum() == (int) io.n_inputs()) {
-                       remove_port_button.set_sensitive (false);
-               }
+       if (!_find_inputs_for_io_outputs) {
+               return _io->n_ports().get (_io->default_type());
        } else {
-               if ((nports = io.n_outputs()) > 0) {
-                       io.remove_output_port (io.output(nports-1), this);
-               }
+               return _io->n_ports().get (_io->default_type());
        }
 }
 
-gint
-IOSelector::remove_port_when_idle (Port *port)
+bool
+IOSelector::list_is_global (int dim) const
 {
-       if (for_input) {
-               io.remove_input_port (port, this);
-       } else {
-               io.remove_output_port (port, this);
-       }
-
-       return FALSE;
+       return (dim == _other);
 }
 
-gint
-IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
+IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
+       : _selector (this, session, io)
 {
-       /* this handles button release on a port name row: i.e. a connection
-          between the named port and the port represented by the treeview.
-       */
+       set_name ("IOSelectorWindow2");
+       set_title (_("I/O selector"));
 
-       Gtk::TreeModel::iterator iter;
-       TreeModel::Path path;
-       TreeViewColumn* column;
-       int cellx;
-       int celly;
+       add (_selector);
 
-       /* only handle button1 events here */
+       set_position (Gtk::WIN_POS_MOUSE);
 
-       if (ev->button != 1) {
-               return false;
-       }
-       
-       if (!(Keyboard::is_delete_event (ev))) {
-               //return false;
-       }
+       io_name_changed (this);
 
-       if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
-               return false;
-       }
-       cerr << "path = " << path.to_string() << endl;
-       
-       if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
-
-               /* path is valid */
-               ustring connected_port_name = (*iter)[port_display_columns.full_name];
-               Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
-               
-               if (for_input) {
-                       Port *p = session.engine().get_port_by_name (connected_port_name);
-                       p->disable_metering();
-                       io.disconnect_input (port, connected_port_name, this);
-               } else {
-                       io.disconnect_output (port, connected_port_name, this);
-               }
-       }
+       show_all ();
 
-       return true;
+       signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
 }
 
-gint
-IOSelector::port_column_button_release (GdkEventButton* event, TreeView* treeview)
+bool
+IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
 {
-       /* this handles button release on the button at the top of a single-column
-          treeview (representing a port)
-       */
-       cerr << "IOSelector::port_column_button_release() called" << endl;
-       
-       if (Keyboard::is_delete_event (event)) {
-               Port* port;
-               {
-                       Glib::Mutex::Lock lm  (port_display_lock);
-                       
-                       port = static_cast<Port *> (treeview->get_data (_("port")));
-                       
-                       if (port == selected_port) {
-                               selected_port = 0;
-                               treeview->set_name ("IOSelectorPortList");
-                               treeview->queue_draw();
-                       }
-               }
-
-               /* remove the port when idle - if we do it here, we will destroy the widget
-                  for whom we are handling an event. not good.
-               */
-
-               signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
-
-       } else {
-               select_treeview (treeview);
-       }
-
-       return TRUE;
+       _selector.Finished (IOSelector::Accepted);
+       hide ();
+       return true;
 }
 
+
 void
-IOSelector::select_next_treeview ()
+IOSelectorWindow::on_map ()
 {
-       slist<TreeView*>::iterator next;
-
-       for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
-
-               if ((*i)->get_name() == "IOSelectorPortListSelected") {
-
-                       ++i;
-
-                       if (i == port_displays.end()) {
-                               select_treeview (port_displays.front());
-                       } else {
-                               select_treeview (*i);
-                       }
-                       
-                       break;
-               }
-       }
+       _selector.setup_all_ports ();
+       Window::on_map ();
 }
 
 void
-IOSelector::select_treeview (TreeView* tview)
+IOSelectorWindow::on_show ()
 {
-       /* Gack. TreeView's don't respond visually to a change
-          in their state, so rename them to force a style
-          switch.
-       */
-
-       Glib::Mutex::Lock lm  (port_display_lock);
-       Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
-       
-       if (port != selected_port) {
-               selected_port = port;
-               
-               tview->set_name ("IOSelectorPortListSelected");
-               
-               for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
-                       if (*i != tview) {
-                               (*i)->set_name ("IOSelectorPortList");
-                               (*i)->queue_draw ();
-                       }
-               }
-               selector_box.show_all ();
-       }
+       Gtk::Window::on_show ();
+       pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
+       resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
 }
 
 void
-IOSelector::redisplay ()
+IOSelectorWindow::io_name_changed (void* src)
 {
-       display_ports ();
+       ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
 
-       if (for_input) {
-               if (io.input_maximum() != 0) {
-                       rescan ();
-               }
+       string title;
+
+       if (!_selector.find_inputs_for_io_outputs()) {
+               title = string_compose(_("%1 input"), _selector.io()->name());
        } else {
-               if (io.output_maximum() != 0) {
-                       rescan();
-               }
+               title = string_compose(_("%1 output"), _selector.io()->name());
        }
+
+       set_title (title);
 }
 
-PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
-       : input_selector (sess, pi, true),
-         output_selector (sess, pi, false)
+PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
+       : input_selector (parent, sess, pi->input())
+       , output_selector (parent, sess, pi->output())
 {
-       hbox.pack_start (output_selector, true, true);
-       hbox.pack_start (input_selector, true, true);
+       output_selector.set_min_height_divisor (2);
+       input_selector.set_min_height_divisor (2);
 
-
-       pack_start (hbox);
+       pack_start (output_selector, true, true);
+       pack_start (input_selector, true, true);
 }
 
 void
-PortInsertUI::redisplay()
+PortInsertUI::redisplay ()
 {
-
-       input_selector.redisplay();
-       output_selector.redisplay();
+       input_selector.setup_ports (input_selector.other());
+       output_selector.setup_ports (output_selector.other());
 }
 
 void
-PortInsertUI::finished(IOSelector::Result r)
+PortInsertUI::finished (IOSelector::Result r)
 {
        input_selector.Finished (r);
        output_selector.Finished (r);
 }
 
 
-PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
+PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
        : ArdourDialog ("port insert dialog"),
-         _portinsertui(sess, pi),
+         _portinsertui (this, sess, pi),
          ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
+         cancel_button (_("Cancel"))
 {
 
        set_name ("IOSelectorWindow");
        string title = _("ardour: ");
-       title += pi.name();
+       title += pi->name();
        set_title (title);
-       
+
        ok_button.set_name ("IOSelectorButton");
+       if (!can_cancel) {
+               ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
+       }
        cancel_button.set_name ("IOSelectorButton");
-       rescan_button.set_name ("IOSelectorButton");
 
-       button_box.set_spacing (5);
-       button_box.set_border_width (5);
-       button_box.set_homogeneous (true);
-       button_box.pack_start (rescan_button);
        if (can_cancel) {
-               button_box.pack_start (cancel_button);
-       }
-       else {
+               cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
+               get_action_area()->pack_start (cancel_button, false, false);
+       else {
                cancel_button.hide();
        }
-       button_box.pack_start (ok_button);
+       get_action_area()->pack_start (ok_button, false, false);
 
        get_vbox()->pack_start (_portinsertui);
-       get_vbox()->pack_start (button_box, false, false);
 
-       ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
-       cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
-       rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
+       ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
+       cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
+
+       signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
+
+       pi->DropReferences.connect (going_away_connection, boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
+}
 
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
-       pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
+bool
+PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
+{
+       accept ();
+       return true;
 }
 
 void
-PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
+PortInsertWindow::plugin_going_away ()
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
-       
+       ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
+
+       going_away_connection.disconnect ();
        delete_when_idle (this);
 }
 
@@ -842,22 +299,17 @@ PortInsertWindow::on_map ()
 }
 
 
-void
-PortInsertWindow::rescan ()
-{
-       _portinsertui.redisplay();
-}
-
 void
 PortInsertWindow::cancel ()
 {
-       _portinsertui.finished(IOSelector::Cancelled);
+       _portinsertui.finished (IOSelector::Cancelled);
        hide ();
 }
 
 void
 PortInsertWindow::accept ()
 {
-       _portinsertui.finished(IOSelector::Accepted);
+       _portinsertui.finished (IOSelector::Accepted);
        hide ();
 }
+