If a bundle's channel has no ports associated with it, you can't connect
[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 PortMatrix::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 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 UNKNOWN;
94                         }
95
96                         if (p && p->connected_to (*j) == false) {
97                                 return NOT_ASSOCIATED;
98                         } else if (q && q->connected_to (*i) == false) {
99                                 return NOT_ASSOCIATED;
100                         }
101
102                 }
103         }
104
105         return ASSOCIATED;
106 }
107
108
109 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
110         : _port_matrix (s, t)
111 {
112         switch (t) {
113         case ARDOUR::DataType::AUDIO:
114                 set_title (_("Audio Connections Manager"));
115                 break;
116         case ARDOUR::DataType::MIDI:
117                 set_title (_("MIDI Connections Manager"));
118                 break;
119         }
120
121         Gtk::HBox* buttons_hbox = Gtk::manage (new Gtk::HBox);
122
123         _rescan_button.set_label (_("Rescan"));
124         _rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
125         _rescan_button.signal_clicked().connect (sigc::mem_fun (_port_matrix, &GlobalPortMatrix::setup_all_ports));
126         buttons_hbox->pack_start (_rescan_button, Gtk::PACK_SHRINK);
127         
128         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox);
129         vbox->pack_start (_port_matrix);
130         vbox->pack_start (*buttons_hbox, Gtk::PACK_SHRINK);
131         add (*vbox);
132         show_all ();
133 }