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.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] = _row_labels_vbox[1] = 0;
378 _side_vbox_pad[0] = _side_vbox_pad[1] = 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[0].pack_start (*manage (new Label ("")));
384 _overall_hbox.pack_start (_side_vbox[0], false, false);
385 _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_NEVER);
386 _scrolled_window.set_shadow_type (SHADOW_NONE);
388 b->pack_start (_column_labels, false, false);
389 b->pack_start (_port_group_hbox, false, false);
390 Alignment* a = new Alignment (0, 1, 0, 0);
391 a->add (*manage (b));
392 _scrolled_window.add (*manage (a));
393 _overall_hbox.pack_start (_scrolled_window);
394 _side_vbox[1].pack_start (*manage (new Label ("")));
395 // _overall_hbox.pack_start (_side_vbox[1]);
396 pack_start (_overall_hbox);
398 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
401 PortMatrix::~PortMatrix ()
406 /** Clear out the things that change when the number of source or destination ports changes */
410 for (int i = 0; i < 2; ++i) {
412 for (std::vector<EventBox*>::iterator j = _row_labels[i].begin(); j != _row_labels[i].end(); ++j) {
415 _row_labels[i].clear ();
417 if (_row_labels_vbox[i]) {
418 _side_vbox[i].remove (*_row_labels_vbox[i]);
420 delete _row_labels_vbox[i];
421 _row_labels_vbox[i] = 0;
423 if (_side_vbox_pad[i]) {
424 _side_vbox[i].remove (*_side_vbox_pad[i]);
426 delete _side_vbox_pad[i];
427 _side_vbox_pad[i] = 0;
430 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
431 _port_group_hbox.remove ((*i)->get_table());
432 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
436 _port_group_ui.clear ();
440 /** Set up dimensions of some of our widgets which depend on other dimensions
441 * within the dialogue.
444 PortMatrix::setup_dimensions ()
446 /* Get some dimensions from various places */
447 int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
449 std::pair<int, int> unit_size (0, 0);
450 int port_group_tables_height = 0;
451 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
452 std::pair<int, int> const u = (*i)->unit_size ();
453 unit_size.first = std::max (unit_size.first, u.first);
454 unit_size.second = std::max (unit_size.second, u.second);
455 port_group_tables_height = std::max (
456 port_group_tables_height, (*i)->get_table().get_height()
461 _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
463 /* Scrolled window */
464 /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
465 the window starts up very small */
466 _scrolled_window.set_size_request (
467 std::min (_column_labels.get_width(), 640),
468 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
472 for (int i = 0; i < 2; ++i) {
473 for (std::vector<EventBox*>::iterator j = _row_labels[i].begin(); j != _row_labels[i].end(); ++j) {
474 (*j)->get_child()->set_size_request (-1, unit_size.second);
477 if (_side_vbox_pad[i]) {
478 _side_vbox_pad[i]->set_size_request (-1, scrollbar_height + unit_size.second / 4);
484 /** Set up the dialogue */
490 int const rows = n_rows ();
493 for (int i = 0; i < 2; ++i) {
494 _row_labels_vbox[i] = new VBox;
495 int const run_rows = std::max (1, rows);
496 for (int j = 0; j < run_rows; ++j) {
498 /* embolden the port/channel name */
504 Label* label = manage (new Label (s));
505 EventBox* b = manage (new EventBox);
507 label->set_use_markup (true);
509 b->set_events (Gdk::BUTTON_PRESS_MASK);
510 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
513 _row_labels[i].push_back (b);
514 _row_labels_vbox[i]->pack_start (*b, false, false);
517 _side_vbox[i].pack_start (*_row_labels_vbox[i], false, false);
518 _side_vbox_pad[i] = new Label ("");
519 _side_vbox[i].pack_start (*_side_vbox_pad[i], false, false);
522 /* Checkbutton tables and visibility checkbuttons */
523 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
525 PortGroupUI* t = new PortGroupUI (*this, **i);
527 _port_group_ui.push_back (t);
528 _port_group_hbox.pack_start (t->get_table(), false, false);
530 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
532 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
534 chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
544 PortMatrix::reset_visibility ()
546 /* now adjust visibility and coloring */
549 Gdk::Color odd_bg (color_from_style ("OddPortGroups", STATE_NORMAL, "fg"));
550 Gdk::Color even_bg (color_from_style ("EvenPortGroups", STATE_NORMAL, "fg"));
552 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
554 (*i)->setup_visibility ();
556 if ((*i)->port_group().visible) {
558 (*i)->get_table().modify_bg (STATE_NORMAL, even_bg);
560 (*i)->get_table().modify_bg (STATE_NORMAL, odd_bg);
568 PortMatrix::redisplay ()
570 _port_group_list.refresh ();
575 /** Handle a button press on a row label */
577 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
579 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
583 Menu* menu = manage (new Menu);
584 Menu_Helpers::MenuList& items = menu->items ();
585 menu->set_name ("ArdourContextMenu");
587 bool const can_add = maximum_rows () > n_rows ();
588 bool const can_remove = minimum_rows () < n_rows ();
589 std::string const name = row_name (r);
592 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
595 items.back().set_sensitive (can_add);
598 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
601 items.back().set_sensitive (can_remove);
603 menu->popup (e->button, e->time);
609 PortMatrix::set_type (ARDOUR::DataType t)
612 _port_group_list.set_type (t);
617 PortMatrix::set_offer_inputs (bool i)
620 _port_group_list.set_offer_inputs (i);
624 /** PortGroupList constructor.
625 * @param session Session to get ports from.
626 * @param type Type of ports to offer (audio or MIDI)
627 * @param offer_inputs true to offer output ports, otherwise false.
628 * @param mask Mask of groups to make visible by default.
631 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
632 : _session (session), _type (type), _offer_inputs (offer_inputs),
633 buss (_("Bus"), "ardour:", mask & BUSS),
634 track (_("Track"), "ardour:", mask & TRACK),
635 system (_("System"), "system:", mask & SYSTEM),
636 other (_("Other"), "", mask & OTHER)
642 PortGroupList::refresh ()
647 track.ports.clear ();
648 system.ports.clear ();
649 other.ports.clear ();
651 /* Find the ports provided by ardour; we can't derive their type just from their
652 names, so we'll have to be more devious.
655 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
657 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
661 if (_type == ARDOUR::DataType::AUDIO) {
663 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
665 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
670 } else if (_type == ARDOUR::DataType::MIDI) {
672 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
676 /* No MIDI busses yet */
680 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
681 for (uint32_t j = 0; j < p.num_ports(); ++j) {
682 g->add (p.port(j)->name ());
685 std::sort (g->ports.begin(), g->ports.end());
689 /* XXX: inserts, sends, plugin inserts? */
691 /* Now we need to find the non-ardour ports; we do this by first
692 finding all the ports that we can connect to.
695 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
696 JackPortIsInput : JackPortIsOutput);
700 string client_matching_string;
702 client_matching_string = _session.engine().client_name();
703 client_matching_string += ':';
706 std::string const p = ports[n];
708 if (p.substr(0, strlen ("system:")) == "system:") {
712 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
713 /* other (non-ardour) prefix */
731 PortGroupList::n_visible_ports () const
735 for (const_iterator i = begin(); i != end(); ++i) {
737 n += (*i)->ports.size();
745 PortGroupList::get_port_by_index (int n, bool with_prefix) const
747 /* XXX: slightly inefficient algorithm */
749 for (const_iterator i = begin(); i != end(); ++i) {
750 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
753 return (*i)->prefix + *j;
766 PortGroupList::set_type (ARDOUR::DataType t)
772 PortGroupList::set_offer_inputs (bool i)