probably fix cleanup issues, but testing required
[ardour.git] / gtk2_ardour / io_selector.cc
index e800f9311bcddb7dd1ca16cac8e5b518bd674043..7ff12612e60c30dee554a382f311fa766ef8c6e8 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <map>
@@ -184,40 +183,7 @@ IOSelector::IOSelector (Session& sess, boost::shared_ptr<IO> ior, bool input)
        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);
@@ -255,6 +221,44 @@ IOSelector::~IOSelector ()
 {
 }
 
+void 
+IOSelector::set_button_sensitivity ()
+{
+       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);
+               }
+                       
+       }
+
+       if (for_input) {
+               if (io->n_inputs() && (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->n_outputs() && (io->output_minimum() < 0 || io->output_minimum() < (int) io->n_outputs())) {
+                       remove_port_button.set_sensitive (true);
+               } else {
+                       remove_port_button.set_sensitive (false);
+               }
+       }
+}
+
+
 void
 IOSelector::name_changed (void* src)
 {
@@ -285,7 +289,10 @@ IOSelector::rescan ()
        gint current_page;
        vector<string> rowdata;
 
+       page_selection_connection.disconnect ();
+
        current_page = notebook.get_current_page ();
+
        pages.clear ();
 
        /* get relevant current JACK ports */
@@ -359,7 +366,7 @@ IOSelector::rescan ()
        }
 
        notebook.set_current_page (current_page);
-       notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
+       page_selection_connection = notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
        selector_box.show_all ();
 }      
 
@@ -409,7 +416,7 @@ IOSelector::display_ports ()
                        
                        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);
                        
@@ -420,16 +427,15 @@ IOSelector::display_ports ()
                        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_data (X_("port"), port);
                        tview->set_headers_visible (true);
-                       tview->set_name ("IOSelectorPortList");
+                       tview->set_name (X_("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) {
@@ -438,7 +444,7 @@ IOSelector::display_ports ()
                                        row[port_display_columns.displayed_name] = connections[c];
                                        row[port_display_columns.full_name] = connections[c];
                                }
-                       }
+                       } 
                        
                        if (for_input) {
                                
@@ -480,8 +486,10 @@ IOSelector::display_ports ()
        if (!selected_port_tview) {
                selected_port_tview = firsttview;
        }
-       
-       select_treeview (selected_port_tview);
+
+       if (selected_port_tview) {
+               select_treeview (selected_port_tview);
+       }
 }
 
 bool
@@ -541,14 +549,6 @@ IOSelector::add_port ()
                        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 {
@@ -559,11 +559,9 @@ IOSelector::add_port ()
                        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);
-               }
        }
+               
+       set_button_sensitivity ();
 }
 
 void
@@ -577,26 +575,14 @@ IOSelector::remove_port ()
                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);
-               }
+
        } else {
                if ((nports = io->n_outputs()) > 0) {
                        io->remove_output_port (io->output(nports-1), this);
                }
        }
-}
-
-gint
-IOSelector::remove_port_when_idle (Port *port)
-{
-       if (for_input) {
-               io->remove_input_port (port, this);
-       } else {
-               io->remove_output_port (port, this);
-       }
-
-       return FALSE;
+       
+       set_button_sensitivity ();
 }
 
 gint
@@ -626,7 +612,7 @@ IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
 
                /* path is valid */
                ustring connected_port_name = (*iter)[port_display_columns.full_name];
-               Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
+               Port *port = reinterpret_cast<Port *> (treeview->get_data (X_("port")));
                
                if (for_input) {
                        Port *p = session.engine().get_port_by_name (connected_port_name);
@@ -645,6 +631,10 @@ IOSelector::select_next_treeview ()
 {
        slist<TreeView*>::iterator next;
 
+       if (port_displays.empty() || port_displays.size() == 1) {
+               return;
+       }
+
        for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
 
                if ((*i)->get_name() == "IOSelectorPortListSelected") {
@@ -671,7 +661,7 @@ IOSelector::select_treeview (TreeView* tview)
        */
 
        Glib::Mutex::Lock lm  (port_display_lock);
-       Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
+       Port* port = reinterpret_cast<Port *> (tview->get_data (X_("port")));
 
        selected_port = port;
 
@@ -701,8 +691,6 @@ IOSelector::select_treeview (TreeView* tview)
                        gtk_widget_queue_draw (ccol->button);
                }
                
-               Port* port = reinterpret_cast<Port *> ((*i)->get_data (_("port")));
-               
                (*i)->set_name ("IOSelectorPortList");
                (*i)->queue_draw ();
        }