Various tweaks to the port matrix.
[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
24 #include "ardour/bundle.h"
25 #include "ardour/session.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/port.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32
33 GlobalPortMatrix::GlobalPortMatrix (ARDOUR::Session& s, ARDOUR::DataType t)
34         : PortMatrix (s, t)
35 {
36         setup_all_ports ();
37 }
38
39 void
40 GlobalPortMatrix::setup_ports (int dim)
41 {
42         _ports[dim].suspend_signals ();
43         _ports[dim].gather (_session, dim == IN);
44         _ports[dim].resume_signals ();
45 }
46
47 void
48 GlobalPortMatrix::set_state (ARDOUR::BundleChannel c[2], bool s)
49 {
50         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
51         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
52
53         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
54                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
55
56                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
57                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
58
59                         if (p) {
60                                 if (s) {
61                                         p->connect (*j);
62                                 } else {
63                                         p->disconnect (*j);
64                                 }
65                         } else if (q) {
66                                 if (s) {
67                                         q->connect (*i);
68                                 } else {
69                                         q->disconnect (*i);
70                                 }
71                         }
72
73                         /* we don't handle connections between two non-Ardour ports */
74                 }
75         }
76 }
77
78 PortMatrixNode::State
79 GlobalPortMatrix::get_state (ARDOUR::BundleChannel c[2]) const
80 {
81         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
82         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
83         if (in_ports.empty() || out_ports.empty()) {
84                 /* we're looking at a bundle with no parts associated with this channel,
85                    so nothing to connect */
86                 return PortMatrixNode::UNKNOWN;
87         }
88                 
89         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
90                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
91
92                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
93                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
94
95                         /* we don't know the state of connections between two non-Ardour ports */
96                         if (!p && !q) {
97                                 return PortMatrixNode::UNKNOWN;
98                         }
99
100                         if (p && p->connected_to (*j) == false) {
101                                 return PortMatrixNode::NOT_ASSOCIATED;
102                         } else if (q && q->connected_to (*i) == false) {
103                                 return PortMatrixNode::NOT_ASSOCIATED;
104                         }
105
106                 }
107         }
108
109         return PortMatrixNode::ASSOCIATED;
110 }
111
112 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
113         : _port_matrix (s, t)
114 {
115         switch (t) {
116         case ARDOUR::DataType::AUDIO:
117                 set_title (_("Audio Connections Manager"));
118                 break;
119         case ARDOUR::DataType::MIDI:
120                 set_title (_("MIDI Connections Manager"));
121                 break;
122         }
123
124         add (_port_matrix);
125         show_all ();
126
127         /* XXX: hack to make the window full-size on opening.  This may not work for
128            people with very large monitors */
129         
130         resize (32768, 32768);
131
132         _port_matrix.MaxSizeChanged.connect (mem_fun (*this, &GlobalPortMatrixWindow::max_size_changed));
133 }
134
135 void
136 GlobalPortMatrixWindow::on_realize ()
137 {
138         Window::on_realize ();
139         set_max_size ();
140 }
141
142 void
143 GlobalPortMatrixWindow::max_size_changed ()
144 {
145         set_max_size ();
146         resize (32768, 32768);
147 }
148
149 void
150 GlobalPortMatrixWindow::set_max_size ()
151 {
152         if ((get_flags() & Gtk::REALIZED) == 0) {
153                 return;
154         }
155         
156         pair<uint32_t, uint32_t> const m = _port_matrix.max_size ();
157         
158         GdkGeometry g;
159         g.max_width = m.first;
160         g.max_height = m.second;
161
162         set_geometry_hints (*this, g, Gdk::HINT_MAX_SIZE);
163 }