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"
41 #include "gui_thread.h"
46 /** Add a port to a group.
47 * @param p Port name, with or without prefix.
51 PortGroup::add (std::string const & p)
53 if (prefix.empty() == false && p.substr (0, prefix.length()) == prefix) {
54 ports.push_back (p.substr (prefix.length()));
60 /** PortGroupUI constructor.
61 * @param m PortMatrix to work for.
62 * @Param g PortGroup to represent.
65 PortGroupUI::PortGroupUI (PortMatrix& m, PortGroup& g)
66 : _port_matrix (m), _port_group (g), _ignore_check_button_toggle (false),
67 _visibility_checkbutton (g.name)
69 int const ports = _port_group.ports.size();
70 int const rows = _port_matrix.n_rows ();
72 if (rows == 0 || ports == 0) {
76 /* Sort out the table and the checkbuttons inside it */
78 _table.resize (rows, ports);
79 _port_checkbuttons.resize (rows);
80 for (int i = 0; i < rows; ++i) {
81 _port_checkbuttons[i].resize (ports);
84 for (int i = 0; i < rows; ++i) {
85 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
86 CheckButton* b = new CheckButton;
88 b->signal_toggled().connect (
89 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_toggled), b, i, j)
92 _port_checkbuttons[i][j] = b;
93 _table.attach (*b, j, j + 1, i, i + 1);
97 _table_box.add (_table);
99 _ignore_check_button_toggle = true;
101 /* Set the state of the check boxes according to current connections */
102 for (int i = 0; i < rows; ++i) {
103 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
104 std::string const t = _port_group.prefix + _port_group.ports[j];
105 bool const s = _port_matrix.get_state (i, t);
106 _port_checkbuttons[i][j]->set_active (s);
108 _port_group.visible = true;
113 _ignore_check_button_toggle = false;
115 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
118 /** The visibility of a PortGroupUI has been toggled */
120 PortGroupUI::visibility_checkbutton_toggled ()
122 _port_group.visible = _visibility_checkbutton.get_active ();
125 /** @return Width and height of a single checkbutton in a port group table */
127 PortGroupUI::unit_size () const
129 if (_port_checkbuttons.empty() || _port_checkbuttons[0].empty())
131 return std::pair<int, int> (0, 0);
135 /* We can't ask for row spacing unless there >1 rows, otherwise we get a warning */
136 if (_table.property_n_rows() > 1) {
137 r = _table.get_row_spacing (0);
140 return std::make_pair (
141 _port_checkbuttons[0][0]->get_width() + _table.get_col_spacing (0),
142 _port_checkbuttons[0][0]->get_height() + r
146 /** @return Table widget containing the port checkbuttons */
148 PortGroupUI::get_table ()
153 /** @return Checkbutton used to toggle visibility */
155 PortGroupUI::get_visibility_checkbutton ()
157 return _visibility_checkbutton;
161 /** Handle a toggle of a port check button */
163 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
165 if (_ignore_check_button_toggle == false) {
166 _port_matrix.set_state (r, _port_group.prefix + _port_group.ports[c], b->get_active());
170 /** Set up visibility of the port group according to PortGroup::visible */
172 PortGroupUI::setup_visibility ()
174 if (!_port_group.ports.empty() && _port_group.visible) {
180 if (_visibility_checkbutton.get_active () != _port_group.visible) {
182 _visibility_checkbutton.set_active (_port_group.visible);
186 RotatedLabelSet::RotatedLabelSet (PortGroupList& g)
187 : Glib::ObjectBase ("RotatedLabelSet"), Widget (), _port_group_list (g), _base_width (128)
189 set_flags (NO_WINDOW);
193 RotatedLabelSet::~RotatedLabelSet ()
199 /** Set the angle that the labels are drawn at.
200 * @param degrees New angle in degrees.
204 RotatedLabelSet::set_angle (int degrees)
206 _angle_degrees = degrees;
207 _angle_radians = M_PI * _angle_degrees / 180;
213 RotatedLabelSet::on_size_request (Requisition* requisition)
215 *requisition = Requisition ();
217 if (_pango_layout == 0) {
221 /* Our height is the highest label */
222 requisition->height = 0;
223 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
224 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
225 std::pair<int, int> const d = setup_layout (*j);
226 if (d.second > requisition->height) {
227 requisition->height = d.second;
232 /* And our width is the base plus the width of the last label */
233 requisition->width = _base_width;
234 int const n = _port_group_list.n_visible_ports ();
236 std::pair<int, int> const d = setup_layout (_port_group_list.get_port_by_index (n - 1, false));
237 requisition->width += d.first;
242 RotatedLabelSet::on_size_allocate (Allocation& allocation)
244 set_allocation (allocation);
247 _gdk_window->move_resize (
248 allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height()
254 RotatedLabelSet::on_realize ()
256 Widget::on_realize ();
258 Glib::RefPtr<Style> style = get_style ();
261 GdkWindowAttr attributes;
262 memset (&attributes, 0, sizeof (attributes));
264 Allocation allocation = get_allocation ();
265 attributes.x = allocation.get_x ();
266 attributes.y = allocation.get_y ();
267 attributes.width = allocation.get_width ();
268 attributes.height = allocation.get_height ();
270 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
271 attributes.window_type = GDK_WINDOW_CHILD;
272 attributes.wclass = GDK_INPUT_OUTPUT;
274 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
275 unset_flags (NO_WINDOW);
276 set_window (_gdk_window);
278 _bg_colour = style->get_bg (STATE_NORMAL );
279 modify_bg (STATE_NORMAL, _bg_colour);
280 _fg_colour = style->get_fg (STATE_NORMAL);
282 _gdk_window->set_user_data (gobj ());
284 /* Set up Pango stuff */
285 _pango_context = create_pango_context ();
287 Pango::Matrix matrix = PANGO_MATRIX_INIT;
288 pango_matrix_rotate (&matrix, _angle_degrees);
289 _pango_context->set_matrix (matrix);
291 _pango_layout = Pango::Layout::create (_pango_context);
292 _gc = Gdk::GC::create (get_window ());
297 RotatedLabelSet::on_unrealize()
299 _gdk_window.clear ();
301 Widget::on_unrealize ();
305 /** Set up our Pango layout to plot a given string, and compute its dimensions once
306 * it has been rotated.
307 * @param s String to use.
308 * @return width and height of the rotated string, in pixels.
312 RotatedLabelSet::setup_layout (std::string const & s)
314 _pango_layout->set_text (s);
316 /* Here's the unrotated size */
319 _pango_layout->get_pixel_size (w, h);
321 /* Rotate the width and height as appropriate. I thought Pango might be able
322 to do this for us, but I can't find out how... */
323 std::pair<int, int> d;
324 d.first = int (w * cos (_angle_radians) - h * sin (_angle_radians));
325 d.second = int (w * sin (_angle_radians) + h * cos (_angle_radians));
331 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
337 int const height = get_allocation().get_height ();
338 double const spacing = double (_base_width) / _port_group_list.n_visible_ports();
340 /* Plot all the visible labels; really we should clip for efficiency */
342 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
344 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
345 std::pair<int, int> const d = setup_layout ((*i)->ports[j]);
346 get_window()->draw_layout (_gc, int ((n + 0.25) * spacing), height - d.second, _pango_layout, _fg_colour, _bg_colour);
355 /** Set the `base width'. This is the width of the base of the label set, ie:
366 RotatedLabelSet::set_base_width (int w)
373 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
374 : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type),
375 _column_labels (_port_group_list)
377 _row_labels_vbox = 0;
380 _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
381 pack_start (_visibility_checkbutton_box, false, false);
383 _side_vbox.pack_start (*manage (new Label ("")));
384 _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
385 _scrolled_window.set_shadow_type (SHADOW_NONE);
386 VBox* b = manage (new VBox);
387 b->pack_start (_column_labels, false, false);
388 b->pack_start (_port_group_hbox, false, false);
389 Alignment* a = manage (new Alignment (0, 1, 0, 0));
391 _scrolled_window.add (*a);
394 _overall_hbox.pack_start (_side_vbox, false, false, 10);
395 _overall_hbox.pack_start (_scrolled_window, true, true);
397 _overall_hbox.pack_start (_scrolled_window, true, true, 10);
398 _overall_hbox.pack_start (_side_vbox, false, false);
401 pack_start (_overall_hbox);
403 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
406 PortMatrix::~PortMatrix ()
411 /** Clear out the things that change when the number of source or destination ports changes */
415 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
418 _row_labels.clear ();
420 if (_row_labels_vbox) {
421 _side_vbox.remove (*_row_labels_vbox);
423 delete _row_labels_vbox;
424 _row_labels_vbox = 0;
426 if (_side_vbox_pad) {
427 _side_vbox.remove (*_side_vbox_pad);
429 delete _side_vbox_pad;
432 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
433 _port_group_hbox.remove ((*i)->get_table());
434 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
438 _port_group_ui.clear ();
442 /** Set up dimensions of some of our widgets which depend on other dimensions
443 * within the dialogue.
446 PortMatrix::setup_dimensions ()
448 /* Get some dimensions from various places */
449 int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
451 std::pair<int, int> unit_size (0, 0);
452 int port_group_tables_height = 0;
453 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
454 std::pair<int, int> const u = (*i)->unit_size ();
455 unit_size.first = std::max (unit_size.first, u.first);
456 unit_size.second = std::max (unit_size.second, u.second);
457 port_group_tables_height = std::max (
458 port_group_tables_height, (*i)->get_table().get_height()
463 _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
465 /* Scrolled window */
466 /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
467 the window starts up very small.
468 The constant value in the set_size_request() computation will control
469 how big the scrolled window will be if we fill the port matrix will a gajillion
473 _scrolled_window.set_size_request (
474 std::min (_column_labels.get_width(), 400),
475 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
479 for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
480 (*j)->get_child()->set_size_request (-1, unit_size.second);
483 if (_side_vbox_pad) {
484 _side_vbox_pad->set_size_request (-1, scrollbar_height + unit_size.second / 4);
489 /** Set up the dialogue */
495 int const rows = n_rows ();
499 _row_labels_vbox = new VBox;
500 int const run_rows = std::max (1, rows);
502 for (int j = 0; j < run_rows; ++j) {
504 /* embolden the port/channel name */
510 Label* label = manage (new Label (s));
511 EventBox* b = manage (new EventBox);
513 label->set_use_markup (true);
515 b->set_events (Gdk::BUTTON_PRESS_MASK);
516 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
519 _row_labels.push_back (b);
520 _row_labels_vbox->pack_start (*b, false, false);
523 _side_vbox.pack_start (*_row_labels_vbox, false, false);
524 _side_vbox_pad = new Label ("");
525 _side_vbox.pack_start (*_side_vbox_pad, false, false);
527 /* Checkbutton tables and visibility checkbuttons */
528 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
530 PortGroupUI* t = new PortGroupUI (*this, **i);
532 _port_group_ui.push_back (t);
533 _port_group_hbox.pack_start (t->get_table(), false, false);
535 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
537 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
539 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
549 PortMatrix::reset_visibility ()
551 /* now adjust visibility and coloring */
554 Gdk::Color odd_bg (color_from_style ("OddPortGroups", STATE_NORMAL, "fg"));
555 Gdk::Color even_bg (color_from_style ("EvenPortGroups", STATE_NORMAL, "fg"));
557 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
559 (*i)->setup_visibility ();
561 if ((*i)->port_group().visible) {
563 (*i)->get_table().modify_bg (STATE_NORMAL, even_bg);
565 (*i)->get_table().modify_bg (STATE_NORMAL, odd_bg);
573 PortMatrix::redisplay ()
575 _port_group_list.refresh ();
580 /** Handle a button press on a row label */
582 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
584 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
588 Menu* menu = manage (new Menu);
589 Menu_Helpers::MenuList& items = menu->items ();
590 menu->set_name ("ArdourContextMenu");
592 bool const can_add = maximum_rows () > n_rows ();
593 bool const can_remove = minimum_rows () < n_rows ();
594 std::string const name = row_name (r);
597 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
600 items.back().set_sensitive (can_add);
603 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
606 items.back().set_sensitive (can_remove);
608 menu->popup (e->button, e->time);
614 PortMatrix::set_type (ARDOUR::DataType t)
617 _port_group_list.set_type (t);
622 PortMatrix::set_offer_inputs (bool i)
625 _port_group_list.set_offer_inputs (i);
629 /** PortGroupList constructor.
630 * @param session Session to get ports from.
631 * @param type Type of ports to offer (audio or MIDI)
632 * @param offer_inputs true to offer output ports, otherwise false.
633 * @param mask Mask of groups to make visible by default.
636 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
637 : _session (session), _type (type), _offer_inputs (offer_inputs),
638 buss (_("Bus"), "ardour:", mask & BUSS),
639 track (_("Track"), "ardour:", mask & TRACK),
640 system (_("System"), "system:", mask & SYSTEM),
641 other (_("Other"), "", mask & OTHER)
647 PortGroupList::refresh ()
652 track.ports.clear ();
653 system.ports.clear ();
654 other.ports.clear ();
656 /* Find the ports provided by ardour; we can't derive their type just from their
657 names, so we'll have to be more devious.
660 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
662 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
666 if (_type == ARDOUR::DataType::AUDIO) {
668 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
670 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
675 } else if (_type == ARDOUR::DataType::MIDI) {
677 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
681 /* No MIDI busses yet */
685 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
686 for (uint32_t j = 0; j < p.num_ports(); ++j) {
687 g->add (p.port(j)->name ());
690 std::sort (g->ports.begin(), g->ports.end());
694 /* XXX: inserts, sends, plugin inserts? */
696 /* Now we need to find the non-ardour ports; we do this by first
697 finding all the ports that we can connect to.
700 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
701 JackPortIsInput : JackPortIsOutput);
705 string client_matching_string;
707 client_matching_string = _session.engine().client_name();
708 client_matching_string += ':';
711 std::string const p = ports[n];
713 if (p.substr(0, strlen ("system:")) == "system:") {
717 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
718 /* other (non-ardour) prefix */
736 PortGroupList::n_visible_ports () const
740 for (const_iterator i = begin(); i != end(); ++i) {
742 n += (*i)->ports.size();
750 PortGroupList::get_port_by_index (int n, bool with_prefix) const
752 /* XXX: slightly inefficient algorithm */
754 for (const_iterator i = begin(); i != end(); ++i) {
755 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
758 return (*i)->prefix + *j;
771 PortGroupList::set_type (ARDOUR::DataType t)
777 PortGroupList::set_offer_inputs (bool i)