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 int const ports = _port_group.ports.size();
73 int const rows = _port_matrix.n_rows ();
75 if (rows == 0 || ports == 0) {
79 /* Sort out the table and the checkbuttons inside it */
81 _table.resize (rows, ports);
82 _port_checkbuttons.resize (rows);
83 for (int i = 0; i < rows; ++i) {
84 _port_checkbuttons[i].resize (ports);
88 for (int i = 0; i < rows; ++i) {
89 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
90 CheckButton* b = new CheckButton;
92 b->signal_toggled().connect (
93 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_toggled), b, i, j));
95 b->signal_button_release_event().connect (
96 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_release), b, i, j), false);
98 _port_checkbuttons[i][j] = b;
100 cerr << this << " bind to " << &_port_checkbuttons << " via " << b
103 _table.attach (*b, j, j + 1, i, i + 1);
107 _table_box.add (_table);
109 _ignore_check_button_toggle = true;
111 /* Set the state of the check boxes according to current connections */
112 for (int i = 0; i < rows; ++i) {
113 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
114 std::string const t = _port_group.prefix + _port_group.ports[j];
115 bool const s = _port_matrix.get_state (i, t);
116 _port_checkbuttons[i][j]->set_active (s);
118 _port_group.visible = true;
123 _ignore_check_button_toggle = false;
125 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
128 /** The visibility of a PortGroupUI has been toggled */
130 PortGroupUI::visibility_checkbutton_toggled ()
132 _port_group.visible = _visibility_checkbutton.get_active ();
135 /** @return Width and height of a single checkbutton in a port group table */
137 PortGroupUI::unit_size () const
139 if (_port_checkbuttons.empty() || _port_checkbuttons[0].empty())
141 return std::pair<int, int> (0, 0);
145 /* We can't ask for row spacing unless there >1 rows, otherwise we get a warning */
146 if (_table.property_n_rows() > 1) {
147 r = _table.get_row_spacing (0);
150 return std::make_pair (
151 _port_checkbuttons[0][0]->get_width() + _table.get_col_spacing (0),
152 _port_checkbuttons[0][0]->get_height() + r
156 /** @return Table widget containing the port checkbuttons */
158 PortGroupUI::get_table ()
163 /** @return Checkbutton used to toggle visibility */
165 PortGroupUI::get_visibility_checkbutton ()
167 return _visibility_checkbutton;
171 /** Handle a toggle of a port check button */
173 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
175 if (_ignore_check_button_toggle == false) {
176 _port_matrix.set_state (r, _port_group.prefix + _port_group.ports[c], b->get_active(), 0);
181 PortGroupUI::port_checkbutton_release (GdkEventButton* ev, CheckButton* b, int r, int c)
183 cerr << this << " RELEASE on " << b << " state = " << ev->state << endl;
185 if (ev->state == 0) {
186 /* let usual toggle handler take care of it */
190 /* The fun starts here
193 const size_t ports = _port_group.ports.size();
194 const size_t rows = _port_matrix.n_rows ();
196 if (rows == 0 || ports == 0) {
200 /* For each port in the group, change the state of
201 the connection for the corresponding "connector" port.
204 for (size_t j = c; j < ports; ++j) {
206 /* we've got a port to connect, now lets find the thing to
207 connect it too ... (search "down" the rows)
210 cerr << "we're going to connect port " << j << " of " << ports << endl;
212 for (size_t i = r; i < rows; ++i) {
214 cerr << this << " going to connect to row " << i << " of " << rows << endl;
215 cerr << "access [" << i << "][" << j << "]\n";
216 cerr << " @ " << &_port_checkbuttons << endl;
218 _port_checkbuttons[i][j]->set_active (!_port_checkbuttons[i][j]->get_active());
220 /* next time, get at least as far as this port before looking
226 /* changed connection state, stop looking for more */
235 /** Set up visibility of the port group according to PortGroup::visible */
237 PortGroupUI::setup_visibility ()
239 if (!_port_group.ports.empty() && _port_group.visible) {
245 if (_visibility_checkbutton.get_active () != _port_group.visible) {
247 _visibility_checkbutton.set_active (_port_group.visible);
251 RotatedLabelSet::RotatedLabelSet (PortGroupList& g)
252 : Glib::ObjectBase ("RotatedLabelSet"), Widget (), _port_group_list (g), _base_width (128)
254 set_flags (NO_WINDOW);
255 if (getenv ("AD_ANGLE") != 0) {
256 set_angle (atoi (getenv ("AD_ANGLE")));
262 RotatedLabelSet::~RotatedLabelSet ()
268 /** Set the angle that the labels are drawn at.
269 * @param degrees New angle in degrees.
273 RotatedLabelSet::set_angle (int degrees)
275 _angle_degrees = degrees;
276 _angle_radians = M_PI * _angle_degrees / 180;
282 RotatedLabelSet::on_size_request (Requisition* requisition)
284 *requisition = Requisition ();
286 if (_pango_layout == 0) {
290 /* Our height is the highest label */
291 requisition->height = 0;
292 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
293 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
294 std::pair<int, int> const d = setup_layout (*j);
295 if (d.second > requisition->height) {
296 requisition->height = d.second;
301 /* And our width is the base plus the width of the last label */
302 requisition->width = _base_width;
303 int const n = _port_group_list.n_visible_ports ();
305 std::pair<int, int> const d = setup_layout (_port_group_list.get_port_by_index (n - 1, false));
306 requisition->width += d.first;
309 cerr << "Labels will be " << requisition->width << " x " << requisition->height << endl;
313 RotatedLabelSet::on_size_allocate (Allocation& allocation)
315 set_allocation (allocation);
318 _gdk_window->move_resize (allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height());
323 RotatedLabelSet::on_realize ()
325 Widget::on_realize ();
327 Glib::RefPtr<Style> style = get_style ();
330 GdkWindowAttr attributes;
331 memset (&attributes, 0, sizeof (attributes));
333 Allocation allocation = get_allocation ();
334 attributes.x = allocation.get_x ();
335 attributes.y = allocation.get_y ();
336 attributes.width = allocation.get_width ();
337 attributes.height = allocation.get_height ();
339 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
340 attributes.window_type = GDK_WINDOW_CHILD;
341 attributes.wclass = GDK_INPUT_OUTPUT;
343 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
344 unset_flags (NO_WINDOW);
345 set_window (_gdk_window);
347 _bg_colour = style->get_bg (STATE_NORMAL );
348 modify_bg (STATE_NORMAL, _bg_colour);
349 _fg_colour = style->get_fg (STATE_NORMAL);
351 _gdk_window->set_user_data (gobj ());
353 /* Set up Pango stuff */
354 _pango_context = create_pango_context ();
356 Pango::Matrix matrix = PANGO_MATRIX_INIT;
357 pango_matrix_rotate (&matrix, _angle_degrees);
358 _pango_context->set_matrix (matrix);
360 _pango_layout = Pango::Layout::create (_pango_context);
361 _gc = Gdk::GC::create (get_window ());
366 RotatedLabelSet::on_unrealize()
368 _gdk_window.clear ();
370 Widget::on_unrealize ();
374 /** Set up our Pango layout to plot a given string, and compute its dimensions once
375 * it has been rotated.
376 * @param s String to use.
377 * @return width and height of the rotated string, in pixels.
381 RotatedLabelSet::setup_layout (std::string const & s)
383 _pango_layout->set_text (s);
385 /* Here's the unrotated size */
388 _pango_layout->get_pixel_size (w, h);
390 /* Rotate the width and height as appropriate. I thought Pango might be able
391 to do this for us, but I can't find out how...
394 std::pair<int, int> d;
396 // cerr << "\"" << s << "\" was " << w << " x " << h << endl;
398 d.first = int (fabs (w * cos (_angle_radians) + h * sin (_angle_radians)));
399 d.second = int (fabs (w * sin (_angle_radians) + h * cos (_angle_radians)));
401 // cerr << "\trotated by " << _angle_degrees << " = " << d.first << " x " << d.second << endl;
407 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
413 int const height = get_allocation().get_height ();
414 double const spacing = double (_base_width) / _port_group_list.n_visible_ports();
416 /* Plot all the visible labels; really we should clip for efficiency */
418 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
420 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
421 std::pair<int, int> const d = setup_layout ((*i)->ports[j]);
423 if (getenv ("AD_X_SHIFT") != 0) {
424 x = atoi (getenv ("AD_X_SHIFT"));
428 if (getenv ("AD_Y_SHIFT") != 0) {
429 y = atoi (getenv ("AD_Y_SHIFT"));
433 get_window()->draw_layout (_gc, int ((n + 0.25) * spacing) + x, height - d.second + y, _pango_layout, _fg_colour, _bg_colour);
442 /** Set the `base width'. This is the width of the base of the label set, ie:
453 RotatedLabelSet::set_base_width (int w)
460 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
461 : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type),
462 _column_labels (_port_group_list)
464 _row_labels_vbox = 0;
467 _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
468 pack_start (_visibility_checkbutton_box, false, false);
470 _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
471 _scrolled_window.set_shadow_type (SHADOW_NONE);
473 VBox* b = manage (new VBox);
476 b->pack_start (_port_group_hbox, false, false);
477 b->pack_start (_column_labels, false, false);
479 b->pack_start (_column_labels, false, false);
480 b->pack_start (_port_group_hbox, false, false);
486 a = manage (new Alignment (1, 0, 0, 0));
488 a = manage (new Alignment (0, 1, 0, 0));
493 _scrolled_window.add (*a);
496 _overall_hbox.pack_start (_side_vbox, false, false, 6);
497 _overall_hbox.pack_start (_scrolled_window, true, true);
499 _overall_hbox.pack_start (_scrolled_window, true, true, 6);
500 _overall_hbox.pack_start (_side_vbox, false, false);
503 pack_start (_overall_hbox);
505 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
508 PortMatrix::~PortMatrix ()
513 /** Clear out the things that change when the number of source or destination ports changes */
517 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
520 _row_labels.clear ();
522 if (_row_labels_vbox) {
523 _side_vbox.remove (*_row_labels_vbox);
524 delete _row_labels_vbox;
525 _row_labels_vbox = 0;
528 /* remove lurking, invisible label and padding */
530 _side_vbox.children().clear ();
532 if (_side_vbox_pad) {
533 delete _side_vbox_pad;
537 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
538 _port_group_hbox.remove ((*i)->get_table());
539 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
543 _port_group_ui.clear ();
547 /** Set up dimensions of some of our widgets which depend on other dimensions
548 * within the dialogue.
551 PortMatrix::setup_dimensions ()
553 /* Get some dimensions from various places */
554 int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
556 std::pair<int, int> unit_size (0, 0);
557 int port_group_tables_height = 0;
558 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
559 std::pair<int, int> const u = (*i)->unit_size ();
560 unit_size.first = std::max (unit_size.first, u.first);
561 unit_size.second = std::max (unit_size.second, u.second);
562 port_group_tables_height = std::max (
563 port_group_tables_height, (*i)->get_table().get_height()
568 _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
570 /* Scrolled window */
571 /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
572 the window starts up very small.
573 The constant value in the set_size_request() computation will control
574 how big the scrolled window will be if we fill the port matrix will a gajillion
578 _scrolled_window.set_size_request (
579 std::min (_column_labels.get_width(), 400),
580 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
584 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
585 (*j)->get_child()->set_size_request (-1, unit_size.second);
588 if (_side_vbox_pad) {
590 _side_vbox_pad->set_size_request (-1, unit_size.second / 4);
592 _side_vbox_pad->set_size_request (-1, scrollbar_height + unit_size.second / 4);
598 /** Set up the dialogue */
604 int const rows = n_rows ();
608 _row_labels_vbox = new VBox;
609 int const run_rows = std::max (1, rows);
611 for (int j = 0; j < run_rows; ++j) {
613 /* embolden the port/channel name */
619 Label* label = manage (new Label (s));
620 EventBox* b = manage (new EventBox);
622 label->set_use_markup (true);
624 b->set_events (Gdk::BUTTON_PRESS_MASK);
625 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
628 _row_labels.push_back (b);
629 _row_labels_vbox->pack_start (*b, false, false);
632 _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
635 _side_vbox.pack_start (*_side_vbox_pad, false, false);
636 _side_vbox.pack_start (*_row_labels_vbox, false, false);
637 _side_vbox.pack_start (*manage (new Label ("")));
639 _side_vbox.pack_start (*manage (new Label ("")));
640 _side_vbox.pack_start (*_row_labels_vbox, false, false);
641 _side_vbox.pack_start (*_side_vbox_pad, false, false);
644 /* Checkbutton tables and visibility checkbuttons */
645 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
647 PortGroupUI* t = new PortGroupUI (*this, **i);
649 _port_group_ui.push_back (t);
650 _port_group_hbox.pack_start (t->get_table(), false, false);
652 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
654 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
657 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
667 PortMatrix::reset_visibility ()
669 /* now adjust visibility and coloring */
672 Gdk::Color odd_bg (color_from_style ("OddPortGroups", STATE_NORMAL, "fg"));
673 Gdk::Color even_bg (color_from_style ("EvenPortGroups", STATE_NORMAL, "fg"));
675 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
677 (*i)->setup_visibility ();
679 if ((*i)->port_group().visible) {
681 (*i)->get_table().modify_bg (STATE_NORMAL, even_bg);
683 (*i)->get_table().modify_bg (STATE_NORMAL, odd_bg);
691 PortMatrix::redisplay ()
693 _port_group_list.refresh ();
698 /** Handle a button press on a row label */
700 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
702 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
706 Menu* menu = manage (new Menu);
707 Menu_Helpers::MenuList& items = menu->items ();
708 menu->set_name ("ArdourContextMenu");
710 bool const can_add = maximum_rows () > n_rows ();
711 bool const can_remove = minimum_rows () < n_rows ();
712 std::string const name = row_name (r);
715 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
718 items.back().set_sensitive (can_add);
721 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
724 items.back().set_sensitive (can_remove);
726 menu->popup (e->button, e->time);
732 PortMatrix::set_type (ARDOUR::DataType t)
735 _port_group_list.set_type (t);
740 PortMatrix::set_offer_inputs (bool i)
743 _port_group_list.set_offer_inputs (i);
747 /** PortGroupList constructor.
748 * @param session Session to get ports from.
749 * @param type Type of ports to offer (audio or MIDI)
750 * @param offer_inputs true to offer output ports, otherwise false.
751 * @param mask Mask of groups to make visible by default.
754 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
755 : _session (session), _type (type), _offer_inputs (offer_inputs),
756 buss (_("Bus"), "ardour:", mask & BUSS),
757 track (_("Track"), "ardour:", mask & TRACK),
758 system (_("System"), "system:", mask & SYSTEM),
759 other (_("Other"), "", mask & OTHER)
765 PortGroupList::refresh ()
770 track.ports.clear ();
771 system.ports.clear ();
772 other.ports.clear ();
774 /* Find the ports provided by ardour; we can't derive their type just from their
775 names, so we'll have to be more devious.
778 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
780 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
784 if (_type == ARDOUR::DataType::AUDIO) {
786 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
788 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
793 } else if (_type == ARDOUR::DataType::MIDI) {
795 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
799 /* No MIDI busses yet */
803 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
804 for (uint32_t j = 0; j < p.num_ports(); ++j) {
805 g->add (p.port(j)->name ());
808 std::sort (g->ports.begin(), g->ports.end());
812 /* XXX: inserts, sends, plugin inserts? */
814 /* Now we need to find the non-ardour ports; we do this by first
815 finding all the ports that we can connect to.
818 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
819 JackPortIsInput : JackPortIsOutput);
823 string client_matching_string;
825 client_matching_string = _session.engine().client_name();
826 client_matching_string += ':';
829 std::string const p = ports[n];
831 if (p.substr(0, strlen ("system:")) == "system:") {
835 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
836 /* other (non-ardour) prefix */
854 PortGroupList::n_visible_ports () const
858 for (const_iterator i = begin(); i != end(); ++i) {
860 n += (*i)->ports.size();
868 PortGroupList::get_port_by_index (int n, bool with_prefix) const
870 /* XXX: slightly inefficient algorithm */
872 for (const_iterator i = begin(); i != end(); ++i) {
873 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
876 return (*i)->prefix + *j;
889 PortGroupList::set_type (ARDOUR::DataType t)
895 PortGroupList::set_offer_inputs (bool i)