Support for the port matrix working at the bundle level and hiding details of ports.
[ardour.git] / gtk2_ardour / global_port_matrix.cc
1 /*
2     Copyright (C) 2009 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <gtkmm/image.h>
21 #include <gtkmm/stock.h>
22 #include "global_port_matrix.h"
23 #include "i18n.h"
24 #include "ardour/bundle.h"
25 #include "ardour/session.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/port.h"
28
29 GlobalPortMatrix::GlobalPortMatrix (ARDOUR::Session& s, ARDOUR::DataType t)
30         : PortMatrix (s, t)
31 {
32         setup_all_ports ();
33 }
34
35 void
36 GlobalPortMatrix::setup_ports (int dim)
37 {
38         _ports[dim].suspend_signals ();
39         _ports[dim].gather (_session, dim == IN);
40         _ports[dim].resume_signals ();
41 }
42
43 void
44 GlobalPortMatrix::set_state (ARDOUR::BundleChannel c[2], bool s)
45 {
46         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
47         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
48
49         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
50                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
51
52                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
53                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
54
55                         if (p) {
56                                 if (s) {
57                                         p->connect (*j);
58                                 } else {
59                                         p->disconnect (*j);
60                                 }
61                         } else if (q) {
62                                 if (s) {
63                                         q->connect (*i);
64                                 } else {
65                                         q->disconnect (*i);
66                                 }
67                         }
68
69                         /* we don't handle connections between two non-Ardour ports */
70                 }
71         }
72 }
73
74 PortMatrixNode::State
75 GlobalPortMatrix::get_state (ARDOUR::BundleChannel c[2]) const
76 {
77         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
78         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
79         if (in_ports.empty() || out_ports.empty()) {
80                 /* we're looking at a bundle with no parts associated with this channel,
81                    so nothing to connect */
82                 return PortMatrixNode::UNKNOWN;
83         }
84                 
85         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
86                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
87
88                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
89                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
90
91                         /* we don't know the state of connections between two non-Ardour ports */
92                         if (!p && !q) {
93                                 return PortMatrixNode::UNKNOWN;
94                         }
95
96                         if (p && p->connected_to (*j) == false) {
97                                 return PortMatrixNode::NOT_ASSOCIATED;
98                         } else if (q && q->connected_to (*i) == false) {
99                                 return PortMatrixNode::NOT_ASSOCIATED;
100                         }
101
102                 }
103         }
104
105         return PortMatrixNode::ASSOCIATED;
106 }
107
108 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
109         : _port_matrix (s, t)
110 {
111         switch (t) {
112         case ARDOUR::DataType::AUDIO:
113                 set_title (_("Audio Connections Manager"));
114                 break;
115         case ARDOUR::DataType::MIDI:
116                 set_title (_("MIDI Connections Manager"));
117                 break;
118         }
119
120         Gtk::HBox* buttons_hbox = Gtk::manage (new Gtk::HBox);
121
122         _rescan_button.set_label (_("Rescan"));
123         _rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
124         _rescan_button.signal_clicked().connect (sigc::mem_fun (_port_matrix, &GlobalPortMatrix::setup_all_ports));
125         buttons_hbox->pack_start (_rescan_button, Gtk::PACK_SHRINK);
126
127         _show_ports_button.set_label (_("Show individual ports"));
128         _show_ports_button.set_active (true);
129         _show_ports_button.signal_toggled().connect (sigc::mem_fun (*this, &GlobalPortMatrixWindow::show_ports_toggled));
130         buttons_hbox->pack_start (_show_ports_button, Gtk::PACK_SHRINK);
131         
132         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox);
133         vbox->pack_start (_port_matrix);
134         vbox->pack_start (*buttons_hbox, Gtk::PACK_SHRINK);
135         add (*vbox);
136         show_all ();
137 }
138
139 void
140 GlobalPortMatrixWindow::show_ports_toggled ()
141 {
142         _port_matrix.set_show_only_bundles (!_show_ports_button.get_active());
143 }