Allow multiple bundles with the same ports in the bundle editor, otherwise sometimes...
[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 "utils.h"
24
25 #include "ardour/bundle.h"
26 #include "ardour/session.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/port.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33
34 GlobalPortMatrix::GlobalPortMatrix (Gtk::Window* p, ARDOUR::Session& s, ARDOUR::DataType t)
35         : PortMatrix (p, s, t)
36 {
37         setup_all_ports ();
38 }
39
40 void
41 GlobalPortMatrix::setup_ports (int dim)
42 {
43         _ports[dim].suspend_signals ();
44         _ports[dim].gather (_session, dim == IN, false);
45         _ports[dim].resume_signals ();
46 }
47
48 void
49 GlobalPortMatrix::set_state (ARDOUR::BundleChannel c[2], bool s)
50 {
51         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
52         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
53
54         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
55                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
56
57                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
58                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
59
60                         if (p) {
61                                 if (s) {
62                                         p->connect (*j);
63                                 } else {
64                                         p->disconnect (*j);
65                                 }
66                         } else if (q) {
67                                 if (s) {
68                                         q->connect (*i);
69                                 } else {
70                                         q->disconnect (*i);
71                                 }
72                         }
73
74                         /* we don't handle connections between two non-Ardour ports */
75                 }
76         }
77 }
78
79 PortMatrixNode::State
80 GlobalPortMatrix::get_state (ARDOUR::BundleChannel c[2]) const
81 {
82         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
83         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
84         if (in_ports.empty() || out_ports.empty()) {
85                 /* we're looking at a bundle with no parts associated with this channel,
86                    so nothing to connect */
87                 return PortMatrixNode::UNKNOWN;
88         }
89
90         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
91                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
92
93                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
94                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
95
96                         /* we don't know the state of connections between two non-Ardour ports */
97                         if (!p && !q) {
98                                 return PortMatrixNode::UNKNOWN;
99                         }
100
101                         if (p && p->connected_to (*j) == false) {
102                                 return PortMatrixNode::NOT_ASSOCIATED;
103                         } else if (q && q->connected_to (*i) == false) {
104                                 return PortMatrixNode::NOT_ASSOCIATED;
105                         }
106
107                 }
108         }
109
110         return PortMatrixNode::ASSOCIATED;
111 }
112
113 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
114         : _port_matrix (this, s, t)
115 {
116         switch (t) {
117         case ARDOUR::DataType::AUDIO:
118                 set_title (_("Audio Connection Manager"));
119                 break;
120         case ARDOUR::DataType::MIDI:
121                 set_title (_("MIDI Connection Manager"));
122                 break;
123         }
124
125         add (_port_matrix);
126         show_all ();
127 }
128
129 void
130 GlobalPortMatrixWindow::on_show ()
131 {
132         Gtk::Window::on_show ();
133         pair<uint32_t, uint32_t> const pm_max = _port_matrix.max_size ();
134         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
135 }