enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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
23 #include "global_port_matrix.h"
24 #include "utils.h"
25
26 #include "ardour/bundle.h"
27 #include "ardour/session.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/port.h"
30
31 #include "pbd/i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace ARDOUR_UI_UTILS;
36
37 GlobalPortMatrix::GlobalPortMatrix (Gtk::Window* p, Session* s, DataType t)
38         : PortMatrix (p, s, t)
39 {
40         setup_all_ports ();
41         init ();
42 }
43
44 void
45 GlobalPortMatrix::setup_ports (int dim)
46 {
47         if (!_session) {
48                 return;
49         }
50
51         _ports[dim].suspend_signals ();
52         _ports[dim].gather (_session, type(), dim == FLOW_IN, false, show_only_bundles ());
53         _ports[dim].resume_signals ();
54 }
55
56 void
57 GlobalPortMatrix::set_state (BundleChannel c[2], bool s)
58 {
59         if (!_session) {
60                 return;
61         }
62
63         Bundle::PortList const & in_ports = c[FLOW_IN].bundle->channel_ports (c[FLOW_IN].channel);
64         Bundle::PortList const & out_ports = c[FLOW_OUT].bundle->channel_ports (c[FLOW_OUT].channel);
65
66         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
67                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
68
69                         boost::shared_ptr<Port> p = _session->engine().get_port_by_name (*i);
70                         boost::shared_ptr<Port> q = _session->engine().get_port_by_name (*j);
71
72                         if (p) {
73                                 if (s) {
74                                         p->connect (*j);
75                                 } else {
76                                         p->disconnect (*j);
77                                 }
78                         } else if (q) {
79                                 if (s) {
80                                         q->connect (*i);
81                                 } else {
82                                         q->disconnect (*i);
83                                 }
84                         } else {
85                                 /* two non-Ardour ports */
86                                 if (s) {
87                                         AudioEngine::instance()->connect (*j, *i);
88                                 } else {
89                                         AudioEngine::instance()->disconnect (*j, *i);
90                                 }
91                         }
92                 }
93         }
94 }
95
96 PortMatrixNode::State
97 GlobalPortMatrix::get_state (BundleChannel c[2]) const
98 {
99         if (_session == 0) {
100                 return PortMatrixNode::NOT_ASSOCIATED;
101         }
102
103         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
104                 return PortMatrixNode::NOT_ASSOCIATED;
105         }
106
107         Bundle::PortList const & in_ports = c[FLOW_IN].bundle->channel_ports (c[FLOW_IN].channel);
108         Bundle::PortList const & out_ports = c[FLOW_OUT].bundle->channel_ports (c[FLOW_OUT].channel);
109         if (in_ports.empty() || out_ports.empty()) {
110                 /* we're looking at a bundle with no parts associated with this channel,
111                    so nothing to connect */
112                 return PortMatrixNode::NOT_ASSOCIATED;
113         }
114
115         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
116                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
117
118                         boost::shared_ptr<Port> p = AudioEngine::instance()->get_port_by_name (*i);
119                         boost::shared_ptr<Port> q = AudioEngine::instance()->get_port_by_name (*j);
120
121                         if (!p && !q) {
122                                 /* two non-Ardour ports; things are slightly more involved */
123
124                                 /* get a port handle for one of them .. */
125
126                                 PortEngine::PortHandle ph = AudioEngine::instance()->port_engine().get_port_by_name (*i);
127                                 if (!ph) {
128                                         return PortMatrixNode::NOT_ASSOCIATED;
129                                 }
130
131                                 /* see if it is connected to the other one ... */
132
133                                 if (AudioEngine::instance()->port_engine().connected_to (ph, *j, false)) {
134                                         return PortMatrixNode::ASSOCIATED;
135                                 }
136
137                                 return PortMatrixNode::NOT_ASSOCIATED;
138                         }
139
140                         if (p && p->connected_to (*j) == false) {
141                                 return PortMatrixNode::NOT_ASSOCIATED;
142                         } else if (q && q->connected_to (*i) == false) {
143                                 return PortMatrixNode::NOT_ASSOCIATED;
144                         }
145
146                 }
147         }
148
149         return PortMatrixNode::ASSOCIATED;
150 }
151
152 GlobalPortMatrixWindow::GlobalPortMatrixWindow (Session* s, DataType t)
153         : ArdourWindow (X_("reset me soon"))
154         , _port_matrix (this, s, t)
155 {
156         switch (t) {
157         case DataType::AUDIO:
158                 set_title (_("Audio Connection Manager"));
159                 break;
160         case DataType::MIDI:
161                 set_title (_("MIDI Connection Manager"));
162                 break;
163         }
164
165         signal_key_press_event().connect (sigc::mem_fun (_port_matrix, &PortMatrix::key_press));
166
167         add (_port_matrix);
168         _port_matrix.show ();
169 }
170
171 void
172 GlobalPortMatrixWindow::on_show ()
173 {
174         Gtk::Window::on_show ();
175         pair<uint32_t, uint32_t> const pm_max = _port_matrix.max_size ();
176         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
177 }
178
179 void
180 GlobalPortMatrixWindow::set_session (Session* s)
181 {
182         _port_matrix.set_session (s);
183
184         if (!s) {
185                 hide ();
186         }
187 }
188
189 void
190 GlobalPortMatrix::set_session (Session *s)
191 {
192         SessionHandlePtr::set_session (s);
193         if (!s) return;
194         setup_all_ports ();
195         init();
196 }
197
198 string
199 GlobalPortMatrix::disassociation_verb () const
200 {
201         return _("Disconnect");
202 }
203
204 string
205 GlobalPortMatrix::channel_noun () const
206 {
207         return _("port");
208 }
209