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 ()
148 PortMatrix::set_ports (const std::list<std::string>& ports)
150 matrix.set_ports (ports);
153 /** Clear out the things that change when the number of source or destination ports changes */
157 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
160 _row_labels.clear ();
162 if (_row_labels_vbox) {
163 _side_vbox.remove (*_row_labels_vbox);
164 delete _row_labels_vbox;
165 _row_labels_vbox = 0;
168 /* remove lurking, invisible label and padding */
170 _side_vbox.children().clear ();
172 if (_side_vbox_pad) {
173 delete _side_vbox_pad;
177 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
178 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
182 _port_group_ui.clear ();
186 /** Set up dimensions of some of our widgets which depend on other dimensions
187 * within the dialogue.
190 PortMatrix::setup_dimensions ()
193 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
194 (*j)->get_child()->set_size_request (-1, matrix.row_spacing());
197 if (_side_vbox_pad) {
199 _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
201 _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
207 /** Set up the dialogue */
213 int const rows = n_rows ();
217 _row_labels_vbox = new VBox;
218 int const run_rows = std::max (1, rows);
220 for (int j = 0; j < run_rows; ++j) {
222 /* embolden the port/channel name */
228 Label* label = manage (new Label (s));
229 EventBox* b = manage (new EventBox);
231 label->set_use_markup (true);
233 b->set_events (Gdk::BUTTON_PRESS_MASK);
234 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
237 _row_labels.push_back (b);
238 _row_labels_vbox->pack_start (*b, false, false);
241 _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
244 _side_vbox.pack_start (*_side_vbox_pad, false, false);
245 _side_vbox.pack_start (*_row_labels_vbox, false, false);
246 _side_vbox.pack_start (*manage (new Label ("")));
248 _side_vbox.pack_start (*manage (new Label ("")));
249 _side_vbox.pack_start (*_row_labels_vbox, false, false);
250 _side_vbox.pack_start (*_side_vbox_pad, false, false);
255 /* Checkbutton tables and visibility checkbuttons */
256 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
258 PortGroupUI* t = new PortGroupUI (*this, **i);
260 _port_group_ui.push_back (t);
262 matrix.add_group (**i);
264 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
266 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
269 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
279 PortMatrix::reset_visibility ()
281 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
283 (*i)->setup_visibility ();
285 if ((*i)->port_group().visible) {
286 matrix.show_group ((*i)->port_group());
288 matrix.hide_group ((*i)->port_group());
294 PortMatrix::redisplay ()
296 _port_group_list.refresh ();
301 /** Handle a button press on a row label */
303 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
305 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
309 Menu* menu = manage (new Menu);
310 Menu_Helpers::MenuList& items = menu->items ();
311 menu->set_name ("ArdourContextMenu");
313 bool const can_add = maximum_rows () > n_rows ();
314 bool const can_remove = minimum_rows () < n_rows ();
315 std::string const name = row_name (r);
318 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
321 items.back().set_sensitive (can_add);
324 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
327 items.back().set_sensitive (can_remove);
329 menu->popup (e->button, e->time);
335 PortMatrix::set_type (ARDOUR::DataType t)
338 _port_group_list.set_type (t);
343 PortMatrix::set_offer_inputs (bool i)
346 _port_group_list.set_offer_inputs (i);
350 /** PortGroupList constructor.
351 * @param session Session to get ports from.
352 * @param type Type of ports to offer (audio or MIDI)
353 * @param offer_inputs true to offer output ports, otherwise false.
354 * @param mask Mask of groups to make visible by default.
357 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
358 : _session (session), _type (type), _offer_inputs (offer_inputs),
359 buss (_("Bus"), "ardour:", mask & BUSS),
360 track (_("Track"), "ardour:", mask & TRACK),
361 system (_("System"), "system:", mask & SYSTEM),
362 other (_("Other"), "", mask & OTHER)
368 PortGroupList::refresh ()
373 track.ports.clear ();
374 system.ports.clear ();
375 other.ports.clear ();
377 /* Find the ports provided by ardour; we can't derive their type just from their
378 names, so we'll have to be more devious.
381 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
383 cerr << "Looking for arour routes\n";
385 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
389 if (_type == ARDOUR::DataType::AUDIO) {
391 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
393 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
398 } else if (_type == ARDOUR::DataType::MIDI) {
400 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
404 /* No MIDI busses yet */
408 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
409 for (uint32_t j = 0; j < p.num_ports(); ++j) {
410 g->add (p.port(j)->name ());
413 std::sort (g->ports.begin(), g->ports.end());
417 /* XXX: inserts, sends, plugin inserts? */
419 /* Now we need to find the non-ardour ports; we do this by first
420 finding all the ports that we can connect to.
423 cerr << "Looking for non-ardour ports\n";
425 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
426 JackPortIsInput : JackPortIsOutput);
430 string client_matching_string;
432 cerr << "Got some\n";
434 client_matching_string = _session.engine().client_name();
435 client_matching_string += ':';
438 std::string const p = ports[n];
440 if (p.substr(0, strlen ("system:")) == "system:") {
444 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
445 /* other (non-ardour) prefix */
456 cerr << "at end of refresh, we have " << buss.ports.size () << " buss\n";
457 cerr << "at end of refresh, we have " << track.ports.size () << " track\n";
458 cerr << "at end of refresh, we have " << system.ports.size () << " system\n";
459 cerr << "at end of refresh, we have " << other.ports.size () << " other\n";
468 PortGroupList::n_visible_ports () const
472 for (const_iterator i = begin(); i != end(); ++i) {
474 n += (*i)->ports.size();
482 PortGroupList::get_port_by_index (int n, bool with_prefix) const
484 /* XXX: slightly inefficient algorithm */
486 for (const_iterator i = begin(); i != end(); ++i) {
487 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
490 return (*i)->prefix + *j;
503 PortGroupList::set_type (ARDOUR::DataType t)
509 PortGroupList::set_offer_inputs (bool i)