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 set_angle (atoi (getenv ("AD_ANGLE")));
258 RotatedLabelSet::~RotatedLabelSet ()
264 /** Set the angle that the labels are drawn at.
265 * @param degrees New angle in degrees.
269 RotatedLabelSet::set_angle (int degrees)
271 _angle_degrees = degrees;
272 _angle_radians = M_PI * _angle_degrees / 180;
278 RotatedLabelSet::on_size_request (Requisition* requisition)
280 *requisition = Requisition ();
282 if (_pango_layout == 0) {
286 /* Our height is the highest label */
287 requisition->height = 0;
288 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
289 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
290 std::pair<int, int> const d = setup_layout (*j);
291 if (d.second > requisition->height) {
292 requisition->height = d.second;
297 /* And our width is the base plus the width of the last label */
298 requisition->width = _base_width;
299 int const n = _port_group_list.n_visible_ports ();
301 std::pair<int, int> const d = setup_layout (_port_group_list.get_port_by_index (n - 1, false));
302 requisition->width += d.first;
305 cerr << "Labels will be " << requisition->width << " x " << requisition->height << endl;
309 RotatedLabelSet::on_size_allocate (Allocation& allocation)
311 set_allocation (allocation);
314 _gdk_window->move_resize (allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height());
319 RotatedLabelSet::on_realize ()
321 Widget::on_realize ();
323 Glib::RefPtr<Style> style = get_style ();
326 GdkWindowAttr attributes;
327 memset (&attributes, 0, sizeof (attributes));
329 Allocation allocation = get_allocation ();
330 attributes.x = allocation.get_x ();
331 attributes.y = allocation.get_y ();
332 attributes.width = allocation.get_width ();
333 attributes.height = allocation.get_height ();
335 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
336 attributes.window_type = GDK_WINDOW_CHILD;
337 attributes.wclass = GDK_INPUT_OUTPUT;
339 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
340 unset_flags (NO_WINDOW);
341 set_window (_gdk_window);
343 _bg_colour = style->get_bg (STATE_NORMAL );
344 modify_bg (STATE_NORMAL, _bg_colour);
345 _fg_colour = style->get_fg (STATE_NORMAL);
347 _gdk_window->set_user_data (gobj ());
349 /* Set up Pango stuff */
350 _pango_context = create_pango_context ();
352 Pango::Matrix matrix = PANGO_MATRIX_INIT;
353 pango_matrix_rotate (&matrix, _angle_degrees);
354 _pango_context->set_matrix (matrix);
356 _pango_layout = Pango::Layout::create (_pango_context);
357 _gc = Gdk::GC::create (get_window ());
362 RotatedLabelSet::on_unrealize()
364 _gdk_window.clear ();
366 Widget::on_unrealize ();
370 /** Set up our Pango layout to plot a given string, and compute its dimensions once
371 * it has been rotated.
372 * @param s String to use.
373 * @return width and height of the rotated string, in pixels.
377 RotatedLabelSet::setup_layout (std::string const & s)
379 _pango_layout->set_text (s);
381 /* Here's the unrotated size */
384 _pango_layout->get_pixel_size (w, h);
386 /* Rotate the width and height as appropriate. I thought Pango might be able
387 to do this for us, but I can't find out how...
390 std::pair<int, int> d;
392 // cerr << "\"" << s << "\" was " << w << " x " << h << endl;
394 d.first = int (fabs (w * cos (_angle_radians) + h * sin (_angle_radians)));
395 d.second = int (fabs (w * sin (_angle_radians) + h * cos (_angle_radians)));
397 // cerr << "\trotated by " << _angle_degrees << " = " << d.first << " x " << d.second << endl;
403 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
409 int const height = get_allocation().get_height ();
410 double const spacing = double (_base_width) / _port_group_list.n_visible_ports();
412 /* Plot all the visible labels; really we should clip for efficiency */
414 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
416 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
417 std::pair<int, int> const d = setup_layout ((*i)->ports[j]);
418 int x = atoi (getenv ("AD_X_SHIFT"));
419 int y = atoi (getenv ("AD_Y_SHIFT"));
420 get_window()->draw_layout (_gc, int ((n + 0.25) * spacing) + x, height - d.second + y, _pango_layout, _fg_colour, _bg_colour);
429 /** Set the `base width'. This is the width of the base of the label set, ie:
440 RotatedLabelSet::set_base_width (int w)
447 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
448 : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type),
449 _column_labels (_port_group_list)
451 _row_labels_vbox = 0;
454 _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
455 pack_start (_visibility_checkbutton_box, false, false);
457 _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
458 _scrolled_window.set_shadow_type (SHADOW_NONE);
460 VBox* b = manage (new VBox);
463 b->pack_start (_port_group_hbox, false, false);
464 b->pack_start (_column_labels, false, false);
466 b->pack_start (_column_labels, false, false);
467 b->pack_start (_port_group_hbox, false, false);
473 a = manage (new Alignment (1, 0, 0, 0));
475 a = manage (new Alignment (0, 1, 0, 0));
480 _scrolled_window.add (*a);
483 _overall_hbox.pack_start (_side_vbox, false, false, 6);
484 _overall_hbox.pack_start (_scrolled_window, true, true);
486 _overall_hbox.pack_start (_scrolled_window, true, true, 6);
487 _overall_hbox.pack_start (_side_vbox, false, false);
490 pack_start (_overall_hbox);
492 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
495 PortMatrix::~PortMatrix ()
500 /** Clear out the things that change when the number of source or destination ports changes */
504 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
507 _row_labels.clear ();
509 if (_row_labels_vbox) {
510 _side_vbox.remove (*_row_labels_vbox);
511 delete _row_labels_vbox;
512 _row_labels_vbox = 0;
515 /* remove lurking, invisible label and padding */
517 _side_vbox.children().clear ();
519 if (_side_vbox_pad) {
520 delete _side_vbox_pad;
524 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
525 _port_group_hbox.remove ((*i)->get_table());
526 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
530 _port_group_ui.clear ();
534 /** Set up dimensions of some of our widgets which depend on other dimensions
535 * within the dialogue.
538 PortMatrix::setup_dimensions ()
540 /* Get some dimensions from various places */
541 int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
543 std::pair<int, int> unit_size (0, 0);
544 int port_group_tables_height = 0;
545 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
546 std::pair<int, int> const u = (*i)->unit_size ();
547 unit_size.first = std::max (unit_size.first, u.first);
548 unit_size.second = std::max (unit_size.second, u.second);
549 port_group_tables_height = std::max (
550 port_group_tables_height, (*i)->get_table().get_height()
555 _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
557 /* Scrolled window */
558 /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
559 the window starts up very small.
560 The constant value in the set_size_request() computation will control
561 how big the scrolled window will be if we fill the port matrix will a gajillion
565 _scrolled_window.set_size_request (
566 std::min (_column_labels.get_width(), 400),
567 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
571 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
572 (*j)->get_child()->set_size_request (-1, unit_size.second);
575 if (_side_vbox_pad) {
577 _side_vbox_pad->set_size_request (-1, unit_size.second / 4);
579 _side_vbox_pad->set_size_request (-1, scrollbar_height + unit_size.second / 4);
585 /** Set up the dialogue */
591 int const rows = n_rows ();
595 _row_labels_vbox = new VBox;
596 int const run_rows = std::max (1, rows);
598 for (int j = 0; j < run_rows; ++j) {
600 /* embolden the port/channel name */
606 Label* label = manage (new Label (s));
607 EventBox* b = manage (new EventBox);
609 label->set_use_markup (true);
611 b->set_events (Gdk::BUTTON_PRESS_MASK);
612 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
615 _row_labels.push_back (b);
616 _row_labels_vbox->pack_start (*b, false, false);
619 _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
622 _side_vbox.pack_start (*_side_vbox_pad, false, false);
623 _side_vbox.pack_start (*_row_labels_vbox, false, false);
624 _side_vbox.pack_start (*manage (new Label ("")));
626 _side_vbox.pack_start (*manage (new Label ("")));
627 _side_vbox.pack_start (*_row_labels_vbox, false, false);
628 _side_vbox.pack_start (*_side_vbox_pad, false, false);
631 /* Checkbutton tables and visibility checkbuttons */
632 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
634 PortGroupUI* t = new PortGroupUI (*this, **i);
636 _port_group_ui.push_back (t);
637 _port_group_hbox.pack_start (t->get_table(), false, false);
639 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
641 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
644 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
654 PortMatrix::reset_visibility ()
656 /* now adjust visibility and coloring */
659 Gdk::Color odd_bg (color_from_style ("OddPortGroups", STATE_NORMAL, "fg"));
660 Gdk::Color even_bg (color_from_style ("EvenPortGroups", STATE_NORMAL, "fg"));
662 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
664 (*i)->setup_visibility ();
666 if ((*i)->port_group().visible) {
668 (*i)->get_table().modify_bg (STATE_NORMAL, even_bg);
670 (*i)->get_table().modify_bg (STATE_NORMAL, odd_bg);
678 PortMatrix::redisplay ()
680 _port_group_list.refresh ();
685 /** Handle a button press on a row label */
687 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
689 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
693 Menu* menu = manage (new Menu);
694 Menu_Helpers::MenuList& items = menu->items ();
695 menu->set_name ("ArdourContextMenu");
697 bool const can_add = maximum_rows () > n_rows ();
698 bool const can_remove = minimum_rows () < n_rows ();
699 std::string const name = row_name (r);
702 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
705 items.back().set_sensitive (can_add);
708 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
711 items.back().set_sensitive (can_remove);
713 menu->popup (e->button, e->time);
719 PortMatrix::set_type (ARDOUR::DataType t)
722 _port_group_list.set_type (t);
727 PortMatrix::set_offer_inputs (bool i)
730 _port_group_list.set_offer_inputs (i);
734 /** PortGroupList constructor.
735 * @param session Session to get ports from.
736 * @param type Type of ports to offer (audio or MIDI)
737 * @param offer_inputs true to offer output ports, otherwise false.
738 * @param mask Mask of groups to make visible by default.
741 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
742 : _session (session), _type (type), _offer_inputs (offer_inputs),
743 buss (_("Bus"), "ardour:", mask & BUSS),
744 track (_("Track"), "ardour:", mask & TRACK),
745 system (_("System"), "system:", mask & SYSTEM),
746 other (_("Other"), "", mask & OTHER)
752 PortGroupList::refresh ()
757 track.ports.clear ();
758 system.ports.clear ();
759 other.ports.clear ();
761 /* Find the ports provided by ardour; we can't derive their type just from their
762 names, so we'll have to be more devious.
765 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
767 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
771 if (_type == ARDOUR::DataType::AUDIO) {
773 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
775 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
780 } else if (_type == ARDOUR::DataType::MIDI) {
782 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
786 /* No MIDI busses yet */
790 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
791 for (uint32_t j = 0; j < p.num_ports(); ++j) {
792 g->add (p.port(j)->name ());
795 std::sort (g->ports.begin(), g->ports.end());
799 /* XXX: inserts, sends, plugin inserts? */
801 /* Now we need to find the non-ardour ports; we do this by first
802 finding all the ports that we can connect to.
805 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
806 JackPortIsInput : JackPortIsOutput);
810 string client_matching_string;
812 client_matching_string = _session.engine().client_name();
813 client_matching_string += ':';
816 std::string const p = ports[n];
818 if (p.substr(0, strlen ("system:")) == "system:") {
822 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
823 /* other (non-ardour) prefix */
841 PortGroupList::n_visible_ports () const
845 for (const_iterator i = begin(); i != end(); ++i) {
847 n += (*i)->ports.size();
855 PortGroupList::get_port_by_index (int n, bool with_prefix) const
857 /* XXX: slightly inefficient algorithm */
859 for (const_iterator i = begin(); i != end(); ++i) {
860 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
863 return (*i)->prefix + *j;
876 PortGroupList::set_type (ARDOUR::DataType t)
882 PortGroupList::set_offer_inputs (bool i)