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"
44 /** Add a port to a group.
45 * @param p Port name, with or without prefix.
49 PortGroup::add (std::string const & p)
51 if (prefix.empty() == false && p.substr (0, prefix.length()) == prefix) {
52 ports.push_back (p.substr (prefix.length()));
58 /** PortGroupUI constructor.
59 * @param m PortMatrix to work for.
60 * @Param g PortGroup to represent.
63 PortGroupUI::PortGroupUI (PortMatrix& m, PortGroup& g)
64 : _port_matrix (m), _port_group (g), _ignore_check_button_toggle (false),
65 _visibility_checkbutton (g.name)
67 int const ports = _port_group.ports.size();
68 int const rows = _port_matrix.n_rows ();
70 if (rows == 0 || ports == 0) {
74 /* Sort out the table and the checkbuttons inside it */
76 _table.resize (rows, ports);
77 _port_checkbuttons.resize (rows);
78 for (int i = 0; i < rows; ++i) {
79 _port_checkbuttons[i].resize (ports);
82 for (int i = 0; i < rows; ++i) {
83 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
84 Gtk::CheckButton* b = new Gtk::CheckButton;
86 b->signal_toggled().connect (
87 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_toggled), b, i, j)
90 _port_checkbuttons[i][j] = b;
91 _table.attach (*b, j, j + 1, i, i + 1);
95 _table_box.add (_table);
97 _ignore_check_button_toggle = true;
99 /* Set the state of the check boxes according to current connections */
100 for (int i = 0; i < rows; ++i) {
101 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
102 std::string const t = _port_group.prefix + _port_group.ports[j];
103 bool const s = _port_matrix.get_state (i, t);
104 _port_checkbuttons[i][j]->set_active (s);
106 _port_group.visible = true;
111 _ignore_check_button_toggle = false;
113 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
116 /** The visibility of a PortGroupUI has been toggled */
118 PortGroupUI::visibility_checkbutton_toggled ()
120 _port_group.visible = _visibility_checkbutton.get_active ();
124 /** @return Width and height of a single checkbutton in a port group table */
126 PortGroupUI::unit_size () const
128 if (_port_checkbuttons.empty() || _port_checkbuttons[0].empty())
130 return std::pair<int, int> (0, 0);
134 /* We can't ask for row spacing unless there >1 rows, otherwise we get a warning */
135 if (_table.property_n_rows() > 1) {
136 r = _table.get_row_spacing (0);
139 return std::make_pair (
140 _port_checkbuttons[0][0]->get_width() + _table.get_col_spacing (0),
141 _port_checkbuttons[0][0]->get_height() + r
145 /** @return Table widget containing the port checkbuttons */
147 PortGroupUI::get_table ()
152 /** @return Checkbutton used to toggle visibility */
154 PortGroupUI::get_visibility_checkbutton ()
156 return _visibility_checkbutton;
160 /** Handle a toggle of a port check button */
162 PortGroupUI::port_checkbutton_toggled (Gtk::CheckButton* b, int r, int c)
164 if (_ignore_check_button_toggle == false) {
165 _port_matrix.set_state (r, _port_group.prefix + _port_group.ports[c], b->get_active());
169 /** Set up visibility of the port group according to PortGroup::visible */
171 PortGroupUI::setup_visibility ()
173 if (_port_group.visible) {
179 if (_visibility_checkbutton.get_active () != _port_group.visible) {
180 _visibility_checkbutton.set_active (_port_group.visible);
185 RotatedLabelSet::RotatedLabelSet (PortGroupList& g)
186 : Glib::ObjectBase ("RotatedLabelSet"), Gtk::Widget (), _port_group_list (g), _base_width (128)
188 set_flags (Gtk::NO_WINDOW);
192 RotatedLabelSet::~RotatedLabelSet ()
198 /** Set the angle that the labels are drawn at.
199 * @param degrees New angle in degrees.
203 RotatedLabelSet::set_angle (int degrees)
205 _angle_degrees = degrees;
206 _angle_radians = M_PI * _angle_degrees / 180;
212 RotatedLabelSet::on_size_request (Gtk::Requisition* requisition)
214 *requisition = Gtk::Requisition ();
216 if (_pango_layout == 0) {
220 /* Our height is the highest label */
221 requisition->height = 0;
222 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
223 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
224 std::pair<int, int> const d = setup_layout (*j);
225 if (d.second > requisition->height) {
226 requisition->height = d.second;
231 /* And our width is the base plus the width of the last label */
232 requisition->width = _base_width;
233 int const n = _port_group_list.n_visible_ports ();
235 std::pair<int, int> const d = setup_layout (_port_group_list.get_port_by_index (n - 1, false));
236 requisition->width += d.first;
241 RotatedLabelSet::on_size_allocate (Gtk::Allocation& allocation)
243 set_allocation (allocation);
246 _gdk_window->move_resize (
247 allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height()
253 RotatedLabelSet::on_realize ()
255 Gtk::Widget::on_realize ();
257 Glib::RefPtr<Gtk::Style> style = get_style ();
260 GdkWindowAttr attributes;
261 memset (&attributes, 0, sizeof (attributes));
263 Gtk::Allocation allocation = get_allocation ();
264 attributes.x = allocation.get_x ();
265 attributes.y = allocation.get_y ();
266 attributes.width = allocation.get_width ();
267 attributes.height = allocation.get_height ();
269 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
270 attributes.window_type = GDK_WINDOW_CHILD;
271 attributes.wclass = GDK_INPUT_OUTPUT;
273 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
274 unset_flags (Gtk::NO_WINDOW);
275 set_window (_gdk_window);
277 _bg_colour = style->get_bg (Gtk::STATE_NORMAL );
278 modify_bg (Gtk::STATE_NORMAL, _bg_colour);
279 _fg_colour = style->get_fg (Gtk::STATE_NORMAL);
281 _gdk_window->set_user_data (gobj ());
283 /* Set up Pango stuff */
284 _pango_context = create_pango_context ();
286 Pango::Matrix matrix = PANGO_MATRIX_INIT;
287 pango_matrix_rotate (&matrix, _angle_degrees);
288 _pango_context->set_matrix (matrix);
290 _pango_layout = Pango::Layout::create (_pango_context);
291 _gc = Gdk::GC::create (get_window ());
296 RotatedLabelSet::on_unrealize()
298 _gdk_window.clear ();
300 Gtk::Widget::on_unrealize ();
304 /** Set up our Pango layout to plot a given string, and compute its dimensions once
305 * it has been rotated.
306 * @param s String to use.
307 * @return width and height of the rotated string, in pixels.
311 RotatedLabelSet::setup_layout (std::string const & s)
313 _pango_layout->set_text (s);
315 /* Here's the unrotated size */
318 _pango_layout->get_pixel_size (w, h);
320 /* Rotate the width and height as appropriate. I thought Pango might be able
321 to do this for us, but I can't find out how... */
322 std::pair<int, int> d;
323 d.first = int (w * cos (_angle_radians) - h * sin (_angle_radians));
324 d.second = int (w * sin (_angle_radians) + h * cos (_angle_radians));
330 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
336 int const height = get_allocation().get_height ();
337 double const spacing = double (_base_width) / _port_group_list.n_visible_ports();
339 /* Plot all the visible labels; really we should clip for efficiency */
341 for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
343 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
344 std::pair<int, int> const d = setup_layout ((*i)->ports[j]);
345 get_window()->draw_layout (_gc, int ((n + 0.25) * spacing), height - d.second, _pango_layout, _fg_colour, _bg_colour);
354 /** Set the `base width'. This is the width of the base of the label set, ie:
365 RotatedLabelSet::set_base_width (int w)
372 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
373 : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type),
374 _column_labels (_port_group_list)
376 _row_labels_vbox[0] = _row_labels_vbox[1] = 0;
377 _side_vbox_pad[0] = _side_vbox_pad[1] = 0;
379 pack_start (_visibility_checkbutton_box, false, false);
381 _side_vbox[0].pack_start (*Gtk::manage (new Gtk::Label ("")));
382 _overall_hbox.pack_start (_side_vbox[0], false, false);
383 _scrolled_window.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_NEVER);
384 _scrolled_window.set_shadow_type (Gtk::SHADOW_NONE);
385 Gtk::VBox* b = new Gtk::VBox;
386 b->pack_start (_column_labels, false, false);
387 b->pack_start (_port_group_hbox, false, false);
388 Gtk::Alignment* a = new Gtk::Alignment (0, 1, 0, 0);
389 a->add (*Gtk::manage (b));
390 _scrolled_window.add (*Gtk::manage (a));
391 _overall_hbox.pack_start (_scrolled_window);
392 _side_vbox[1].pack_start (*Gtk::manage (new Gtk::Label ("")));
393 _overall_hbox.pack_start (_side_vbox[1]);
394 pack_start (_overall_hbox);
396 _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
399 PortMatrix::~PortMatrix ()
404 /** Clear out the things that change when the number of source or destination ports changes */
408 for (int i = 0; i < 2; ++i) {
410 for (std::vector<Gtk::EventBox*>::iterator j = _row_labels[i].begin(); j != _row_labels[i].end(); ++j) {
413 _row_labels[i].clear ();
415 if (_row_labels_vbox[i]) {
416 _side_vbox[i].remove (*_row_labels_vbox[i]);
418 delete _row_labels_vbox[i];
419 _row_labels_vbox[i] = 0;
421 if (_side_vbox_pad[i]) {
422 _side_vbox[i].remove (*_side_vbox_pad[i]);
424 delete _side_vbox_pad[i];
425 _side_vbox_pad[i] = 0;
428 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
429 _port_group_hbox.remove ((*i)->get_table());
430 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
434 _port_group_ui.clear ();
438 /** Set up dimensions of some of our widgets which depend on other dimensions
439 * within the dialogue.
442 PortMatrix::setup_dimensions ()
444 /* Get some dimensions from various places */
445 int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
447 std::pair<int, int> unit_size (0, 0);
448 int port_group_tables_height = 0;
449 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
450 std::pair<int, int> const u = (*i)->unit_size ();
451 unit_size.first = std::max (unit_size.first, u.first);
452 unit_size.second = std::max (unit_size.second, u.second);
453 port_group_tables_height = std::max (
454 port_group_tables_height, (*i)->get_table().get_height()
459 _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
461 /* Scrolled window */
462 /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
463 the window starts up very small */
464 _scrolled_window.set_size_request (
465 std::min (_column_labels.get_width(), 640),
466 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
470 for (int i = 0; i < 2; ++i) {
471 for (std::vector<Gtk::EventBox*>::iterator j = _row_labels[i].begin(); j != _row_labels[i].end(); ++j) {
472 (*j)->get_child()->set_size_request (-1, unit_size.second);
475 if (_side_vbox_pad[i]) {
476 _side_vbox_pad[i]->set_size_request (-1, scrollbar_height + unit_size.second / 4);
482 /** Set up the dialogue */
488 int const rows = n_rows ();
491 for (int i = 0; i < 2; ++i) {
492 _row_labels_vbox[i] = new Gtk::VBox;
493 int const run_rows = std::max (1, rows);
494 for (int j = 0; j < run_rows; ++j) {
495 Gtk::Label* label = new Gtk::Label (rows == 0 ? "Quim" : row_name (j));
496 Gtk::EventBox* b = new Gtk::EventBox;
497 b->set_events (Gdk::BUTTON_PRESS_MASK);
498 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
499 b->add (*Gtk::manage (label));
500 _row_labels[i].push_back (b);
501 _row_labels_vbox[i]->pack_start (*b, false, false);
504 _side_vbox[i].pack_start (*_row_labels_vbox[i], false, false);
505 _side_vbox_pad[i] = new Gtk::Label ("");
506 _side_vbox[i].pack_start (*_side_vbox_pad[i], false, false);
509 /* Checkbutton tables and visibility checkbuttons */
511 for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
513 PortGroupUI* t = new PortGroupUI (*this, **i);
515 /* XXX: this is a bit of a hack; should probably use a configurable colour here */
516 Gdk::Color alt_bg = get_style()->get_bg (Gtk::STATE_NORMAL);
517 alt_bg.set_rgb (alt_bg.get_red() + 4096, alt_bg.get_green() + 4096, alt_bg.get_blue () + 4096);
519 t->get_table().modify_bg (Gtk::STATE_NORMAL, alt_bg);
522 _port_group_ui.push_back (t);
523 _port_group_hbox.pack_start (t->get_table(), false, false);
525 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
532 for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
533 (*i)->setup_visibility ();
539 PortMatrix::redisplay ()
541 _port_group_list.refresh ();
546 /** Handle a button press on a row label */
548 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
550 if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
554 Gtk::Menu* menu = Gtk::manage (new Gtk::Menu);
555 Gtk::Menu_Helpers::MenuList& items = menu->items ();
556 menu->set_name ("ArdourContextMenu");
558 bool const can_add = maximum_rows () > n_rows ();
559 bool const can_remove = minimum_rows () < n_rows ();
560 std::string const name = row_name (r);
563 Gtk::Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
566 items.back().set_sensitive (can_add);
569 Gtk::Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
572 items.back().set_sensitive (can_remove);
574 menu->popup (e->button, e->time);
580 PortMatrix::set_type (ARDOUR::DataType t)
583 _port_group_list.set_type (t);
588 PortMatrix::set_offer_inputs (bool i)
591 _port_group_list.set_offer_inputs (i);
595 /** PortGroupList constructor.
596 * @param session Session to get ports from.
597 * @param type Type of ports to offer (audio or MIDI)
598 * @param offer_inputs true to offer output ports, otherwise false.
599 * @param mask Mask of groups to make visible by default.
602 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
603 : _session (session), _type (type), _offer_inputs (offer_inputs),
604 buss (_("Buss"), "ardour:", mask & BUSS),
605 track (_("Track"), "ardour:", mask & TRACK),
606 system (_("System"), "system:", mask & SYSTEM),
607 other (_("Other"), "", mask & OTHER)
613 PortGroupList::refresh ()
618 track.ports.clear ();
619 system.ports.clear ();
620 other.ports.clear ();
622 /* Find the ports provided by ardour; we can't derive their type just from their
623 names, so we'll have to be more devious. */
625 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
627 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
630 if (_type == ARDOUR::DataType::AUDIO && dynamic_cast<ARDOUR::AudioTrack*> ((*i).get())) {
631 /* Audio track for an audio IO */
633 } else if (_type == ARDOUR::DataType::MIDI && dynamic_cast<ARDOUR::MidiTrack*> ((*i).get())) {
634 /* Midi track for a MIDI IO */
636 } else if (_type == ARDOUR::DataType::AUDIO && dynamic_cast<ARDOUR::MidiTrack*> ((*i).get()) == 0) {
637 /* Non-MIDI track for an Audio IO; must be an audio buss */
642 ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
643 for (uint32_t j = 0; j < p.num_ports(); ++j) {
644 g->add (p.port(j)->name ());
647 std::sort (g->ports.begin(), g->ports.end());
652 /* XXX: inserts, sends, plugin inserts? */
654 /* Now we need to find the non-ardour ports; we do this by first
655 finding all the ports that we can connect to. */
656 const char **ports = _session.engine().get_ports (
657 "", _type.to_jack_type(), _offer_inputs ? JackPortIsInput : JackPortIsOutput
664 std::string const p = ports[n];
666 if (p.substr(0, strlen ("system:")) == "system:") {
670 if (p.substr(0, strlen("ardour:")) != "ardour:") {
671 /* other (non-ardour) prefix */
687 PortGroupList::n_visible_ports () const
691 for (const_iterator i = begin(); i != end(); ++i) {
693 n += (*i)->ports.size();
701 PortGroupList::get_port_by_index (int n, bool with_prefix) const
703 /* XXX: slightly inefficient algorithm */
705 for (const_iterator i = begin(); i != end(); ++i) {
706 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
709 return (*i)->prefix + *j;
722 PortGroupList::set_type (ARDOUR::DataType t)
728 PortGroupList::set_offer_inputs (bool i)