enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / io_selector.cc
index b8598659d7c6ba7614093c33bfdfd9336175aaa2..6d0ede316c7a4240e1b0ebc1f57d39259d3701d3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002-2007 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
 
 */
 
+#include <stdint.h>
+
 #include <glibmm/objectbase.h>
+
 #include <gtkmm2ext/doi.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/bundle.h"
 #include "ardour/data_type.h"
+#include "ardour/io.h"
+#include "ardour/port.h"
+#include "ardour/session.h"
+
 #include "io_selector.h"
 #include "utils.h"
 #include "gui_thread.h"
-#include "i18n.h"
-
-IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool offer_inputs)
-       : PortMatrix (
-               session, io->default_type(), offer_inputs,
-               PortGroupList::Mask (PortGroupList::BUSS | PortGroupList::SYSTEM | PortGroupList::OTHER)
-               ),
-         _io (io)
-{
-       /* Listen for ports changing on the IO */
-       if (!offer_inputs) {
-               _io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
-       } else {
-               _io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
-       }
-}
+#include "pbd/i18n.h"
 
+using namespace ARDOUR;
+using namespace ARDOUR_UI_UTILS;
+using namespace Gtk;
 
-void
-IOSelector::ports_changed (ARDOUR::IOChange change, void *src)
+IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
+       : PortMatrix (p, session, DataType::NIL)
+       , _io (io)
 {
-       ENSURE_GUI_THREAD (bind (mem_fun (*this, &IOSelector::ports_changed), change, src));
-
-       redisplay ();
-}
+       setup_type ();
 
+       /* signal flow from 0 to 1 */
 
+       _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
 
-void
-IOSelector::set_state (int r, std::string const & p, bool s)
-{
-       if (s) {
-               if (!_offer_inputs) {
-                       _io->connect_input (_io->input(r), p, 0);
-               } else {
-                       _io->connect_output (_io->output(r), p, 0);
-               }
-       } else {
-               if (!_offer_inputs) {
-                       _io->disconnect_input (_io->input(r), p, 0);
-               } else {
-                       _io->disconnect_output (_io->output(r), p, 0);
-               }
-       }
-}
+       if (_find_inputs_for_io_outputs) {
+               _other = 1;
+               _ours = 0;
+       } else {
+               _other = 0;
+               _ours = 1;
+       }
 
-bool
-IOSelector::get_state (int r, std::string const & p) const
-{
-       const char **connections = _offer_inputs ? _io->output(r)->get_connections() : _io->input(r)->get_connections();
+       _port_group.reset (new PortGroup (io->name()));
+       _ports[_ours].add_group (_port_group);
 
-       int k = 0;
-       while (connections && connections[k]) {
-               if (std::string (connections[k]) == p) {
-                       return true;
-               }
+       io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ());
 
-               ++k;
-       }
-
-       return false;
+       setup_all_ports ();
+       init ();
 }
 
-uint32_t
-IOSelector::n_rows () const
+void
+IOSelector::setup_type ()
 {
-       if (!_offer_inputs) {
-               return _io->inputs().num_ports (_io->default_type());
-       } else {
-               return _io->outputs().num_ports (_io->default_type());
+       /* set type according to what's in the IO */
+
+       int N = 0;
+       DataType type_with_ports = DataType::NIL;
+       for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+               if (_io->ports().num_ports (*i)) {
+                       type_with_ports = *i;
+                       ++N;
+               }
        }
-}
 
-uint32_t
-IOSelector::maximum_rows () const
-{
-       if (!_offer_inputs) {
-               return _io->input_maximum ().get (_io->default_type());
+       if (N <= 1) {
+               set_type (type_with_ports);
        } else {
-               return _io->output_maximum ().get (_io->default_type());
+               set_type (DataType::NIL);
        }
 }
 
-
-uint32_t
-IOSelector::minimum_rows () const
+void
+IOSelector::io_changed_proxy ()
 {
-       if (!_offer_inputs) {
-               return _io->input_minimum ().get (_io->default_type());
-       } else {
-               return _io->output_minimum ().get (_io->default_type());
-       }
+       /* The IO's changed signal is emitted from code that holds its route's processor lock,
+          so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
+          without a deadlock unless we break things up with this idle handler.
+       */
+
+       Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed));
 }
 
-std::string
-IOSelector::row_name (int r) const
+void
+IOSelector::io_changed ()
 {
-       if (!_offer_inputs) {
-               return _io->input(r)->name();
-       } else {
-               return _io->output(r)->name();
-       }
-               
+       setup_type ();
+       setup_all_ports ();
 }
 
 void
-IOSelector::add_row ()
+IOSelector::setup_ports (int dim)
 {
-       // The IO selector only works for single typed IOs
-       const ARDOUR::DataType t = _io->default_type ();
+       if (!_session) {
+               return;
+       }
 
-       if (!_offer_inputs) {
+       _ports[dim].suspend_signals ();
 
-               try {
-                       _io->add_input_port ("", this);
-               }
+       if (dim == _other) {
 
-               catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
-                       Gtk::MessageDialog msg (0,  _("There are no more JACK ports available."));
-                       msg.run ();
-               }
+               _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false, show_only_bundles ());
 
        } else {
 
-               try {
-                       _io->add_output_port ("", this);
-               }
-
-               catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
-                       Gtk::MessageDialog msg (0, _("There are no more JACK ports available."));
-                       msg.run ();
-               }
+               _port_group->clear ();
+               _port_group->add_bundle (_io->bundle (), _io);
        }
-}
 
+       _ports[dim].resume_signals ();
+}
 
 void
-IOSelector::remove_row (int r)
+IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
 {
-       // The IO selector only works for single typed IOs
-       const ARDOUR::DataType t = _io->default_type ();
-       
-       if (!_offer_inputs) {
-               _io->remove_input_port (_io->input (r), this);
-       } else {
-               _io->remove_output_port (_io->output (r), this);
+       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 (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) {
+
+                       boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
+                       if (!f) {
+                               return;
+                       }
+
+                        if (s) {
+                               if (!f->connected_to (*j)) {
+                                       _io->connect (f, *j, 0);
+                               }
+                        } else {
+                               if (f->connected_to (*j)) {
+                                       _io->disconnect (f, *j, 0);
+                               }
+                        }
+               }
        }
 }
 
-std::string
-IOSelector::row_descriptor () const
-{
-       return _("port");
-}
-
-
-
-IOSelectorWindow::IOSelectorWindow (
-       ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input, bool can_cancel
-       )
-       : ArdourDialog ("I/O selector"),
-         _selector (session, io, !for_input),
-         ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
-
+PortMatrixNode::State
+IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
 {
-       add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
-       set_name ("IOSelectorWindow2");
-
-       string title;
-       if (for_input) {
-               title = string_compose(_("%1 input"), io->name());
-       } else {
-               title = string_compose(_("%1 output"), io->name());
-       }
-
-       ok_button.set_name ("IOSelectorButton");
-       if (!can_cancel) {
-               ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
+       if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
+               return PortMatrixNode::NOT_ASSOCIATED;
        }
-       cancel_button.set_name ("IOSelectorButton");
-       rescan_button.set_name ("IOSelectorButton");
-       rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
 
-       get_action_area()->pack_start (rescan_button, false, false);
+       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 (can_cancel) {
-               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();
+       if (!_session || 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;
        }
-               
-       get_action_area()->pack_start (ok_button, false, false);
 
-       get_vbox()->set_spacing (8);
-       get_vbox()->pack_start (_selector);
+       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) {
 
-       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));
+                       boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
 
-       set_title (title);
-       set_position (Gtk::WIN_POS_MOUSE);
+                       /* since we are talking about an IO, our ports should all have an associated Port *,
+                          so the above call should never fail */
+                       assert (f);
 
-       show_all ();
+                       if (!f->connected_to (*j)) {
+                               /* if any one thing is not connected, all bets are off */
+                               return PortMatrixNode::NOT_ASSOCIATED;
+                       }
+               }
+       }
 
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
+       return PortMatrixNode::ASSOCIATED;
 }
 
-IOSelectorWindow::~IOSelectorWindow()
+uint32_t
+IOSelector::n_io_ports () const
 {
-       
+       if (!_find_inputs_for_io_outputs) {
+               return _io->n_ports().get (_io->default_type());
+       } else {
+               return _io->n_ports().get (_io->default_type());
+       }
 }
 
-void
-IOSelectorWindow::rescan ()
+bool
+IOSelector::list_is_global (int dim) const
 {
-       _selector.redisplay ();
+       return (dim == _other);
 }
 
-void
-IOSelectorWindow::cancel ()
+std::string
+IOSelector::disassociation_verb () const
 {
-       _selector.Finished (IOSelector::Cancelled);
-       hide ();
+       return _("Disconnect");
 }
 
-void
-IOSelectorWindow::accept ()
+std::string
+IOSelector::channel_noun () const
 {
-       _selector.Finished (IOSelector::Accepted);
-       hide ();
+       return _("port");
 }
 
-void
-IOSelectorWindow::on_map ()
+IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
+       : ArdourWindow (_("I/O selector"))
+       , _selector (this, session, io)
 {
-       _selector.redisplay ();
-       Window::on_map ();
-}
+       set_name ("IOSelectorWindow2");
 
+       add (_selector);
 
-PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
-       : input_selector (sess, pi->io(), true),
-         output_selector (sess, pi->io(), false)
-{
-       hbox.pack_start (output_selector, true, true);
-       hbox.pack_start (input_selector, true, true);
+       io_name_changed (this);
 
-       pack_start (hbox);
-}
+       show_all ();
 
-void
-PortInsertUI::redisplay ()
-{
-       input_selector.redisplay();
-       output_selector.redisplay();
+       signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
 }
 
-void
-PortInsertUI::finished (IOSelector::Result r)
+bool
+IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
 {
-       input_selector.Finished (r);
-       output_selector.Finished (r);
+       _selector.Finished (IOSelector::Accepted);
+       return false;
 }
 
 
-PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
-       : ArdourDialog ("port insert dialog"),
-         _portinsertui (sess, pi),
-         ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
+void
+IOSelectorWindow::on_map ()
 {
-
-       set_name ("IOSelectorWindow");
-       string title = _("ardour: ");
-       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");
-       rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
-
-       get_action_area()->pack_start (rescan_button, false, false);
-       if (can_cancel) {
-               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();
-       }
-       get_action_area()->pack_start (ok_button, false, false);
-
-       get_vbox()->pack_start (_portinsertui);
-
-       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));
-
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
-
-       going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
+       _selector.setup_all_ports ();
+       Window::on_map ();
 }
 
 void
-PortInsertWindow::plugin_going_away ()
+IOSelectorWindow::on_show ()
 {
-       ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
-       
-       going_away_connection.disconnect ();
-       delete_when_idle (this);
+       Gtk::Window::on_show ();
+       std::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
-PortInsertWindow::on_map ()
+IOSelectorWindow::io_name_changed (void*)
 {
-       _portinsertui.redisplay ();
-       Window::on_map ();
-}
+       ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
 
+       std::string title;
 
-void
-PortInsertWindow::rescan ()
-{
-       _portinsertui.redisplay ();
-}
+       if (!_selector.find_inputs_for_io_outputs()) {
+               title = string_compose(_("%1 input"), _selector.io()->name());
+       } else {
+               title = string_compose(_("%1 output"), _selector.io()->name());
+       }
 
-void
-PortInsertWindow::cancel ()
-{
-       _portinsertui.finished (IOSelector::Cancelled);
-       hide ();
+       set_title (title);
 }
 
-void
-PortInsertWindow::accept ()
-{
-       _portinsertui.finished (IOSelector::Accepted);
-       hide ();
-}