2 Copyright (C) 2002-2003 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 <glibmm/objectbase.h>
26 #include <gtkmm2ext/doi.h>
27 #include <ardour/port_insert.h>
28 #include "ardour/session.h"
29 #include "ardour/io.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/track.h"
32 #include "io_selector.h"
34 #include "gui_thread.h"
37 RotatedLabelSet::RotatedLabelSet (GroupedPortList& g)
38 : Glib::ObjectBase ("RotatedLabelSet"), Gtk::Widget (), _port_list (g), _base_start (64), _base_width (128)
40 set_flags (Gtk::NO_WINDOW);
44 RotatedLabelSet::~RotatedLabelSet ()
50 /** Set the angle that the labels are drawn at.
51 * @param degrees New angle in degrees.
55 RotatedLabelSet::set_angle (int degrees)
57 _angle_degrees = degrees;
58 _angle_radians = M_PI * _angle_degrees / 180;
64 RotatedLabelSet::on_size_request (Gtk::Requisition* requisition)
66 *requisition = Gtk::Requisition ();
68 if (_pango_layout == 0) {
72 /* Our height is the highest label */
73 requisition->height = 0;
74 for (GroupedPortList::const_iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
75 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
76 std::pair<int, int> const d = setup_layout (*j);
77 if (d.second > requisition->height) {
78 requisition->height = d.second;
83 /* And our width is the base plus the width of the last label */
84 requisition->width = _base_start + _base_width;
85 int const n = _port_list.n_ports ();
87 std::pair<int, int> const d = setup_layout (_port_list.get_port_by_index (n - 1, false));
88 requisition->width += d.first;
93 RotatedLabelSet::on_size_allocate (Gtk::Allocation& allocation)
95 set_allocation (allocation);
98 _gdk_window->move_resize (
99 allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height()
105 RotatedLabelSet::on_realize ()
107 Gtk::Widget::on_realize ();
109 Glib::RefPtr<Gtk::Style> style = get_style ();
112 GdkWindowAttr attributes;
113 memset (&attributes, 0, sizeof (attributes));
115 Gtk::Allocation allocation = get_allocation ();
116 attributes.x = allocation.get_x ();
117 attributes.y = allocation.get_y ();
118 attributes.width = allocation.get_width ();
119 attributes.height = allocation.get_height ();
121 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
122 attributes.window_type = GDK_WINDOW_CHILD;
123 attributes.wclass = GDK_INPUT_OUTPUT;
125 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
126 unset_flags (Gtk::NO_WINDOW);
127 set_window (_gdk_window);
129 _bg_colour = style->get_bg (Gtk::STATE_NORMAL );
130 modify_bg (Gtk::STATE_NORMAL, _bg_colour);
131 _fg_colour = style->get_fg (Gtk::STATE_NORMAL);
133 _gdk_window->set_user_data (gobj ());
135 /* Set up Pango stuff */
136 _pango_context = create_pango_context ();
138 Pango::Matrix matrix = PANGO_MATRIX_INIT;
139 pango_matrix_rotate (&matrix, _angle_degrees);
140 _pango_context->set_matrix (matrix);
142 _pango_layout = Pango::Layout::create (_pango_context);
143 _gc = Gdk::GC::create (get_window ());
148 RotatedLabelSet::on_unrealize()
150 _gdk_window.clear ();
152 Gtk::Widget::on_unrealize ();
157 * Set up our Pango layout to plot a given string, and compute its dimensions once it has been rotated.
158 * @param s String to use.
159 * @return width and height of the rotated string, in pixels.
163 RotatedLabelSet::setup_layout (std::string const & s)
165 _pango_layout->set_text (s);
167 /* Here's the unrotated size */
170 _pango_layout->get_pixel_size (w, h);
172 /* Rotate the width and height as appropriate. I thought Pango might be able to do this for us,
173 but I can't find out how... */
174 std::pair<int, int> d;
175 d.first = int (w * cos (_angle_radians) - h * sin (_angle_radians));
176 d.second = int (w * sin (_angle_radians) + h * cos (_angle_radians));
182 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
188 int const height = get_allocation().get_height ();
189 double const spacing = double (_base_width) / _port_list.n_ports();
191 /* Plot all the labels; really we should clip for efficiency */
193 for (GroupedPortList::const_iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
194 for (uint32_t j = 0; j < i->ports.size(); ++j) {
195 std::pair<int, int> const d = setup_layout (i->ports[j]);
196 get_window()->draw_layout (_gc, _base_start + int ((n + 0.25) * spacing), height - d.second, _pango_layout, _fg_colour, _bg_colour);
205 * Set the `base dimensions'. These are the dimensions of the area at which the labels start, and
206 * have to be set up to match whatever they are labelling.
208 * Roughly speaking, we have
219 RotatedLabelSet::set_base_dimensions (int s, int w)
228 * Construct an IOSelector.
229 * @param session Session to operate on.
230 * @param io IO to operate on.
231 * @param for_input true if the selector is for an input, otherwise false.
234 IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input)
235 : _session (session), _port_list (session, io, for_input), _io (io), _for_input (for_input),
236 _width (0), _height (0), _column_labels (_port_list),
237 _ignore_check_button_toggle (false), _add_remove_box_added (false)
240 pack_start (_column_labels, true, true);
242 /* Buttons for adding and removing ports */
243 _add_port_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_MENU)));
244 _add_port_button.set_label (_("Add port"));
245 _add_port_button.signal_clicked().connect (mem_fun (*this, &IOSelector::add_port_button_clicked));
246 _add_remove_box.pack_start (_add_port_button);
247 _remove_port_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REMOVE, Gtk::ICON_SIZE_MENU)));
248 _remove_port_button.set_label (_("Remove port"));
249 _remove_port_button.signal_clicked().connect (mem_fun (*this, &IOSelector::remove_port_button_clicked));
250 _add_remove_box.pack_start (_remove_port_button);
251 set_button_sensitivity ();
253 /* Table. We need to put in a HBox, with a dummy label to its right,
254 so that the rotated column labels can overhang the right hand side of the table. */
258 setup_check_button_states ();
259 _table_hbox.pack_start (_table, false, false);
260 _table_hbox.pack_start (_dummy);
261 _table.set_col_spacings (4);
262 pack_start (_table_hbox);
266 update_column_label_dimensions ();
268 /* Listen for ports changing on the IO */
270 _io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
272 _io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
277 IOSelector::~IOSelector ()
279 for (std::vector<Gtk::Label*>::iterator i = _row_labels.begin(); i != _row_labels.end(); ++i) {
283 for (uint32_t x = 0; x < _check_buttons.size(); ++x) {
284 for (uint32_t y = 0; y < _check_buttons[x].size(); ++y) {
285 delete _check_buttons[x][y];
292 * Sets up the sizing of the column label widget to match the table that's underneath it.
296 IOSelector::update_column_label_dimensions ()
298 if (_row_labels.empty() || _check_buttons.empty() || _check_buttons[0].empty()) {
302 _column_labels.set_base_dimensions (
303 /* width of the row label + a column spacing */
304 _row_labels.front()->get_width() + _table.get_col_spacing (0),
305 /* width of a check button + a column spacing for each column */
306 _check_buttons.size() * ( _check_buttons[0][0]->get_width() + _table.get_col_spacing(1))
311 IOSelector::setup_table ()
313 if (_add_remove_box_added) {
314 _table.remove (_add_remove_box);
317 for (std::vector<Gtk::EventBox*>::iterator i = _group_labels.begin(); i != _group_labels.end(); ++i) {
322 _group_labels.clear ();
326 int const old_width = _width;
327 _width = _port_list.n_ports ();
331 int const old_height = _height;
333 ARDOUR::DataType const t = _io->default_type();
336 _height = _io->n_inputs().get(t);
338 _height = _io->n_outputs().get(t);
341 _table.resize (_width + 1, _height + 1);
343 /* Add checkbuttons where required, and remove those that aren't */
344 for (int x = _width; x < old_width; ++x) {
345 for (int y = 0; y < old_height; ++y) {
346 delete _check_buttons[x][y];
349 _check_buttons.resize (_width);
351 for (int x = 0; x < _width; x++) {
353 for (int y = _height; y < old_height; ++y) {
354 delete _check_buttons[x][y];
356 _check_buttons[x].resize (_height);
358 for (int y = 0; y < _height; y++) {
360 if (x >= old_width || y >= old_height) {
361 Gtk::CheckButton* button = new Gtk::CheckButton;
362 button->signal_toggled().connect (
363 sigc::bind (sigc::mem_fun (*this, &IOSelector::check_button_toggled), x, y)
365 _check_buttons[x][y] = button;
366 _table.attach (*button, x + 1, x + 2, y, y + 1);
371 /* Add more row labels where required, and remove those that aren't */
372 for (int y = _height; y < old_height; ++y) {
373 delete _row_labels[y];
375 _row_labels.resize (_height);
376 for (int y = old_height; y < _height; ++y) {
377 Gtk::Label* label = new Gtk::Label;
378 _row_labels[y] = label;
379 _table.attach (*label, 0, 1, y, y + 1);
382 _table.attach (_add_remove_box, 0, 1, _height, _height + 1);
383 _add_remove_box_added = true;
385 /* Add group labels */
389 /* XXX: this is a bit of a hack; should probably use a configurable colour here */
390 Gdk::Color alt_bg = get_style()->get_bg (Gtk::STATE_NORMAL);
391 alt_bg.set_rgb (alt_bg.get_red() + 4096, alt_bg.get_green() + 4096, alt_bg.get_blue () + 4096);
392 for (GroupedPortList::iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
393 if (i->ports.empty() == false) {
394 Gtk::Label* label = new Gtk::Label ("<b>" + i->name + "</b>");
395 label->set_use_markup (true);
396 Gtk::EventBox* box = new Gtk::EventBox ();
397 box->add (*Gtk::manage (label));
399 box->modify_bg (Gtk::STATE_NORMAL, alt_bg);
401 _group_labels.push_back (box);
402 _table.attach (*box, n, n + i->ports.size(), _height, _height + 1);
403 n += i->ports.size();
413 * Write the correct text to the row labels.
417 IOSelector::setup_row_labels ()
419 for (int y = 0; y < _height; y++) {
420 _row_labels[y]->set_text (_for_input ? _io->input(y)->name() : _io->output(y)->name());
426 * Set up the state of each check button according to what connections are made.
430 IOSelector::setup_check_button_states ()
432 _ignore_check_button_toggle = true;
434 /* Set the state of the check boxes according to current connections */
435 for (int i = 0; i < _height; ++i) {
436 const char **connections = _for_input ? _io->input(i)->get_connections() : _io->output(i)->get_connections();
437 for (int j = 0; j < _width; ++j) {
439 std::string const t = _port_list.get_port_by_index (j);
441 bool required_state = false;
443 while (connections && connections[k]) {
444 if (std::string(connections[k]) == t) {
445 required_state = true;
451 _check_buttons[j][i]->set_active (required_state);
455 _ignore_check_button_toggle = false;
460 * Handle a toggle of a check button.
464 IOSelector::check_button_toggled (int x, int y)
466 if (_ignore_check_button_toggle) {
470 bool const new_state = _check_buttons[x][y]->get_active ();
471 std::string const port = _port_list.get_port_by_index (x);
475 _io->connect_input (_io->input(y), port, 0);
477 _io->connect_output (_io->output(y), port, 0);
481 _io->disconnect_input (_io->input(y), port, 0);
483 _io->disconnect_output (_io->output(y), port, 0);
489 IOSelector::add_port_button_clicked ()
491 /* add a new port, then hide the button if we're up to the maximum allowed */
493 // The IO selector only works for single typed IOs
494 const ARDOUR::DataType t = _io->default_type();
499 _io->add_input_port ("", this);
502 catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
503 Gtk::MessageDialog msg (0, _("There are no more JACK ports available."));
510 _io->add_output_port ("", this);
513 catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
514 Gtk::MessageDialog msg (0, _("There are no more JACK ports available."));
519 set_button_sensitivity ();
524 IOSelector::remove_port_button_clicked ()
528 // The IO selector only works for single typed IOs
529 const ARDOUR::DataType t = _io->default_type();
531 // always remove last port
534 if ((nports = _io->n_inputs().get(t)) > 0) {
535 _io->remove_input_port (_io->input(nports - 1), this);
538 if ((nports = _io->n_outputs().get(t)) > 0) {
539 _io->remove_output_port (_io->output(nports - 1), this);
543 set_button_sensitivity ();
548 IOSelector::set_button_sensitivity ()
550 ARDOUR::DataType const t = _io->default_type();
554 _add_port_button.set_sensitive (
555 _io->input_maximum().get(t) > _io->n_inputs().get(t)
559 _add_port_button.set_sensitive (
560 _io->output_maximum().get(t) > _io->n_outputs().get(t)
566 _remove_port_button.set_sensitive (
567 _io->n_inputs().get(t) && _io->input_minimum().get(t) < _io->n_inputs().get(t)
571 _remove_port_button.set_sensitive (
572 _io->n_outputs().get(t) && _io->output_minimum().get(t) < _io->n_outputs().get(t)
578 IOSelector::ports_changed (ARDOUR::IOChange change, void *src)
580 ENSURE_GUI_THREAD (bind (mem_fun (*this, &IOSelector::ports_changed), change, src));
587 IOSelector::redisplay ()
589 _port_list.refresh ();
592 setup_check_button_states ();
593 update_column_label_dimensions ();
599 GroupedPortList::GroupedPortList (ARDOUR::Session & session, boost::shared_ptr<ARDOUR::IO> io, bool for_input)
600 : _session (session), _io (io), _for_input (for_input)
606 GroupedPortList::refresh ()
610 /* Find the ports provided by ardour; we can't derive their type just from their
611 names, so we'll have to be more devious. */
613 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
615 PortGroup buss (_("Buss"), "ardour:");
616 PortGroup track (_("Track"), "ardour:");
618 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
620 PortGroup& g = dynamic_cast<ARDOUR::Track*> ((*i).get()) == 0 ? buss : track;
622 ARDOUR::PortSet const & p = _for_input ? ((*i)->outputs()) : ((*i)->inputs());
623 for (uint32_t j = 0; j < p.num_ports(); ++j) {
624 std::string const n = p.port(j)->name ();
625 g.ports.push_back (n.substr(strlen ("ardour:")));
628 std::sort (g.ports.begin(), g.ports.end());
632 /* XXX: inserts, sends, plugin inserts? */
634 /* Now we need to find the non-ardour ports; we do this by first
635 finding all the ports that we can connect to. */
636 const char **ports = _session.engine().get_ports (
637 "", _io->default_type().to_jack_type(), _for_input ? JackPortIsOutput : JackPortIsInput
640 PortGroup system (_("System"), "system:");
641 PortGroup other (_("Other"), "");
651 for (int i = 0; i < n; ++i) {
652 std::string const p = ports[i];
654 if (p.substr(0, strlen ("system:")) == "system:") {
656 system.ports.push_back (p.substr (strlen ("system:")));
658 if (p.substr(0, strlen("ardour:")) != "ardour:") {
659 /* other (non-ardour) prefix */
660 other.ports.push_back (p);
673 GroupedPortList::n_ports () const
677 for (const_iterator i = begin(); i != end(); ++i) {
678 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
687 GroupedPortList::get_port_by_index (int n, bool with_prefix) const
689 /* XXX: slightly inefficient algorithm */
691 for (const_iterator i = begin(); i != end(); ++i) {
692 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
695 return i->prefix + *j;
708 IOSelectorWindow::IOSelectorWindow (
709 ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input, bool can_cancel
711 : ArdourDialog ("I/O selector"),
712 _selector (session, io, for_input),
713 ok_button (can_cancel ? _("OK"): _("Close")),
714 cancel_button (_("Cancel")),
715 rescan_button (_("Rescan"))
718 add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
719 set_name ("IOSelectorWindow2");
723 title = string_compose(_("%1 input"), io->name());
725 title = string_compose(_("%1 output"), io->name());
728 ok_button.set_name ("IOSelectorButton");
730 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
732 cancel_button.set_name ("IOSelectorButton");
733 rescan_button.set_name ("IOSelectorButton");
734 rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
736 get_action_area()->pack_start (rescan_button, false, false);
739 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
740 get_action_area()->pack_start (cancel_button, false, false);
742 cancel_button.hide();
745 get_action_area()->pack_start (ok_button, false, false);
747 get_vbox()->set_spacing (8);
748 get_vbox()->pack_start (_selector);
750 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
751 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
752 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
755 set_position (Gtk::WIN_POS_MOUSE);
759 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
762 IOSelectorWindow::~IOSelectorWindow()
768 IOSelectorWindow::rescan ()
770 _selector.redisplay ();
774 IOSelectorWindow::cancel ()
776 _selector.Finished (IOSelector::Cancelled);
781 IOSelectorWindow::accept ()
783 _selector.Finished (IOSelector::Accepted);
788 IOSelectorWindow::on_map ()
790 _selector.redisplay ();
795 PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
796 : input_selector (sess, pi->io(), true),
797 output_selector (sess, pi->io(), false)
799 hbox.pack_start (output_selector, true, true);
800 hbox.pack_start (input_selector, true, true);
806 PortInsertUI::redisplay ()
808 input_selector.redisplay();
809 output_selector.redisplay();
813 PortInsertUI::finished (IOSelector::Result r)
815 input_selector.Finished (r);
816 output_selector.Finished (r);
820 PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
821 : ArdourDialog ("port insert dialog"),
822 _portinsertui (sess, pi),
823 ok_button (can_cancel ? _("OK"): _("Close")),
824 cancel_button (_("Cancel")),
825 rescan_button (_("Rescan"))
828 set_name ("IOSelectorWindow");
829 string title = _("ardour: ");
833 ok_button.set_name ("IOSelectorButton");
835 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
837 cancel_button.set_name ("IOSelectorButton");
838 rescan_button.set_name ("IOSelectorButton");
839 rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
841 get_action_area()->pack_start (rescan_button, false, false);
843 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
844 get_action_area()->pack_start (cancel_button, false, false);
846 cancel_button.hide();
848 get_action_area()->pack_start (ok_button, false, false);
850 get_vbox()->pack_start (_portinsertui);
852 ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
853 cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
854 rescan_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::rescan));
856 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
858 going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
862 PortInsertWindow::plugin_going_away ()
864 ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
866 going_away_connection.disconnect ();
867 delete_when_idle (this);
871 PortInsertWindow::on_map ()
873 _portinsertui.redisplay ();
879 PortInsertWindow::rescan ()
881 _portinsertui.redisplay ();
885 PortInsertWindow::cancel ()
887 _portinsertui.finished (IOSelector::Cancelled);
892 PortInsertWindow::accept ()
894 _portinsertui.finished (IOSelector::Accepted);