2 Copyright (C) 2002-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <gtkmm/label.h>
21 #include <gtkmm/enums.h>
22 #include <gtkmm/image.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/messagedialog.h>
25 #include <gtkmm/menu.h>
26 #include <gtkmm/menu_elems.h>
27 #include <gtkmm/menuitem.h>
28 #include <gtkmm/menushell.h>
29 #include <glibmm/objectbase.h>
30 #include <gtkmm2ext/doi.h>
31 #include <ardour/port_insert.h>
32 #include "ardour/session.h"
33 #include "ardour/io.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/track.h"
36 #include "ardour/audio_track.h"
37 #include "ardour/midi_track.h"
38 #include "ardour/data_type.h"
39 #include "io_selector.h"
42 #include "gui_thread.h"
47 /** Add a port to a group.
48 * @param p Port name, with or without prefix.
52 PortGroup::add (std::string const & p)
54 if (prefix.empty() == false && p.substr (0, prefix.length()) == prefix) {
55 ports.push_back (p.substr (prefix.length()));
61 /** PortGroupUI constructor.
62 * @param m PortMatrix to work for.
63 * @Param g PortGroup to represent.
66 PortGroupUI::PortGroupUI (PortMatrix& m, PortGroup& g)
69 , _ignore_check_button_toggle (false)
70 , _visibility_checkbutton (g.name)
72 _port_group.visible = true;
73 _ignore_check_button_toggle = false;
74 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
77 /** The visibility of a PortGroupUI has been toggled */
79 PortGroupUI::visibility_checkbutton_toggled ()
81 _port_group.visible = _visibility_checkbutton.get_active ();
84 /** @return Checkbutton used to toggle visibility */
86 PortGroupUI::get_visibility_checkbutton ()
88 return _visibility_checkbutton;
92 /** Handle a toggle of a port check button */
94 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
96 if (_ignore_check_button_toggle == false) {
97 // _port_matrix.hide_group (_port_group);
101 /** Set up visibility of the port group according to PortGroup::visible */
103 PortGroupUI::setup_visibility ()
105 if (_visibility_checkbutton.get_active () != _port_group.visible) {
106 _visibility_checkbutton.set_active (_port_group.visible);
110 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
111 : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type)
113 _row_labels_vbox = 0;
116 _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
117 pack_start (_visibility_checkbutton_box, false, false);
119 _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
120 _scrolled_window.set_shadow_type (SHADOW_NONE);
122 VBox* b = manage (new VBox);
124 b->pack_start (_port_group_hbox, false, false);
125 b->pack_start (_port_group_hbox, false, false);
127 _scrolled_window.add (matrix);
130 _overall_hbox.pack_start (_side_vbox, false, false, 6);
131 _overall_hbox.pack_start (_scrolled_window, true, true);
133 _overall_hbox.pack_start (_scrolled_window, true, true, 6);
134 _overall_hbox.pack_start (_side_vbox, false, false);
137 pack_start (_overall_hbox);
139 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
142 PortMatrix::~PortMatrix ()
147 /** Clear out the things that change when the number of source or destination ports changes */
151 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
154 _row_labels.clear ();
156 if (_row_labels_vbox) {
157 _side_vbox.remove (*_row_labels_vbox);
158 delete _row_labels_vbox;
159 _row_labels_vbox = 0;
162 /* remove lurking, invisible label and padding */
164 _side_vbox.children().clear ();
166 if (_side_vbox_pad) {
167 delete _side_vbox_pad;
171 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
172 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
176 _port_group_ui.clear ();
180 /** Set up dimensions of some of our widgets which depend on other dimensions
181 * within the dialogue.
184 PortMatrix::setup_dimensions ()
187 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
188 (*j)->get_child()->set_size_request (-1, matrix.row_spacing());
191 if (_side_vbox_pad) {
193 _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
195 _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
201 /** Set up the dialogue */
207 int const rows = n_rows ();
211 _row_labels_vbox = new VBox;
212 int const run_rows = std::max (1, rows);
214 for (int j = 0; j < run_rows; ++j) {
216 /* embolden the port/channel name */
222 Label* label = manage (new Label (s));
223 EventBox* b = manage (new EventBox);
225 label->set_use_markup (true);
227 b->set_events (Gdk::BUTTON_PRESS_MASK);
228 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
231 _row_labels.push_back (b);
232 _row_labels_vbox->pack_start (*b, false, false);
235 _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
238 _side_vbox.pack_start (*_side_vbox_pad, false, false);
239 _side_vbox.pack_start (*_row_labels_vbox, false, false);
240 _side_vbox.pack_start (*manage (new Label ("")));
242 _side_vbox.pack_start (*manage (new Label ("")));
243 _side_vbox.pack_start (*_row_labels_vbox, false, false);
244 _side_vbox.pack_start (*_side_vbox_pad, false, false);
247 /* Checkbutton tables and visibility checkbuttons */
248 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
250 PortGroupUI* t = new PortGroupUI (*this, **i);
252 _port_group_ui.push_back (t);
254 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
256 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
259 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
269 PortMatrix::reset_visibility ()
271 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
273 (*i)->setup_visibility ();
275 if ((*i)->port_group().visible) {
276 matrix.show_group ((*i)->port_group());
278 matrix.hide_group ((*i)->port_group());
284 PortMatrix::redisplay ()
286 _port_group_list.refresh ();
291 /** Handle a button press on a row label */
293 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
295 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
299 Menu* menu = manage (new Menu);
300 Menu_Helpers::MenuList& items = menu->items ();
301 menu->set_name ("ArdourContextMenu");
303 bool const can_add = maximum_rows () > n_rows ();
304 bool const can_remove = minimum_rows () < n_rows ();
305 std::string const name = row_name (r);
308 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
311 items.back().set_sensitive (can_add);
314 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
317 items.back().set_sensitive (can_remove);
319 menu->popup (e->button, e->time);
325 PortMatrix::set_type (ARDOUR::DataType t)
328 _port_group_list.set_type (t);
333 PortMatrix::set_offer_inputs (bool i)
336 _port_group_list.set_offer_inputs (i);
340 /** PortGroupList constructor.
341 * @param session Session to get ports from.
342 * @param type Type of ports to offer (audio or MIDI)
343 * @param offer_inputs true to offer output ports, otherwise false.
344 * @param mask Mask of groups to make visible by default.
347 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
348 : _session (session), _type (type), _offer_inputs (offer_inputs),
349 buss (_("Bus"), "ardour:", mask & BUSS),
350 track (_("Track"), "ardour:", mask & TRACK),
351 system (_("System"), "system:", mask & SYSTEM),
352 other (_("Other"), "", mask & OTHER)
358 PortGroupList::refresh ()
363 track.ports.clear ();
364 system.ports.clear ();
365 other.ports.clear ();
367 /* Find the ports provided by ardour; we can't derive their type just from their
368 names, so we'll have to be more devious.
371 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
373 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
377 if (_type == ARDOUR::DataType::AUDIO) {
379 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
381 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
386 } else if (_type == ARDOUR::DataType::MIDI) {
388 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
392 /* No MIDI busses yet */
396 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
397 for (uint32_t j = 0; j < p.num_ports(); ++j) {
398 g->add (p.port(j)->name ());
401 std::sort (g->ports.begin(), g->ports.end());
405 /* XXX: inserts, sends, plugin inserts? */
407 /* Now we need to find the non-ardour ports; we do this by first
408 finding all the ports that we can connect to.
411 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
412 JackPortIsInput : JackPortIsOutput);
416 string client_matching_string;
418 client_matching_string = _session.engine().client_name();
419 client_matching_string += ':';
422 std::string const p = ports[n];
424 if (p.substr(0, strlen ("system:")) == "system:") {
428 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
429 /* other (non-ardour) prefix */
447 PortGroupList::n_visible_ports () const
451 for (const_iterator i = begin(); i != end(); ++i) {
453 n += (*i)->ports.size();
461 PortGroupList::get_port_by_index (int n, bool with_prefix) const
463 /* XXX: slightly inefficient algorithm */
465 for (const_iterator i = begin(); i != end(); ++i) {
466 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
469 return (*i)->prefix + *j;
482 PortGroupList::set_type (ARDOUR::DataType t)
488 PortGroupList::set_offer_inputs (bool i)