Some refactoring. Add port group headers 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 "global_port_matrix.h"
21 #include "i18n.h"
22 #include "ardour/bundle.h"
23 #include "ardour/session.h"
24 #include "ardour/audioengine.h"
25 #include "ardour/port.h"
26
27 GlobalPortMatrix::GlobalPortMatrix (ARDOUR::Session& s, ARDOUR::DataType t)
28         : PortMatrix (s, t, true),
29           _session (s),
30           _our_port_group_list (t, false)
31 {
32         setup ();
33
34         _column_ports.VisibilityChanged.connect (sigc::mem_fun (*this, &GlobalPortMatrix::group_visibility_changed));
35 }
36
37 void
38 GlobalPortMatrix::group_visibility_changed ()
39 {
40         _row_ports.take_visibility_from (_column_ports);
41         setup ();
42 }
43
44
45 void
46 GlobalPortMatrix::setup ()
47 {
48         _row_ports.gather (_session);
49         PortMatrix::setup ();
50 }
51
52 void
53 GlobalPortMatrix::set_state (
54         boost::shared_ptr<ARDOUR::Bundle> ab,
55         uint32_t ac,
56         boost::shared_ptr<ARDOUR::Bundle> bb,
57         uint32_t bc,
58         bool s,
59         uint32_t k
60         )
61 {
62         ARDOUR::Bundle::PortList const& our_ports = ab->channel_ports (ac);
63         ARDOUR::Bundle::PortList const& other_ports = bb->channel_ports (bc);
64
65         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
66                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
67
68                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
69                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
70
71                         if (p) {
72                                 if (s) {
73                                         p->connect (*j);
74                                 } else {
75                                         p->disconnect (*j);
76                                 }
77                         } else if (q) {
78                                 if (s) {
79                                         q->connect (*i);
80                                 } else {
81                                         q->disconnect (*j);
82                                 }
83                         }
84
85                         /* we don't handle connections between two non-Ardour ports */
86                 }
87         }
88 }
89
90
91 PortMatrix::State
92 GlobalPortMatrix::get_state (
93         boost::shared_ptr<ARDOUR::Bundle> ab,
94         uint32_t ac,
95         boost::shared_ptr<ARDOUR::Bundle> bb,
96         uint32_t bc
97         ) const
98 {
99         ARDOUR::Bundle::PortList const& our_ports = ab->channel_ports (ac);
100         ARDOUR::Bundle::PortList const& other_ports = bb->channel_ports (bc);
101
102         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
103                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
104
105                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
106                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
107
108                         /* we don't know the state of connections between two non-Ardour ports */
109                         if (!p && !q) {
110                                 return UNKNOWN;
111                         }
112
113                         if (p && p->connected_to (*j) == false) {
114                                 return NOT_ASSOCIATED;
115                         } else if (q && q->connected_to (*i) == false) {
116                                 return NOT_ASSOCIATED;
117                         }
118
119                 }
120         }
121
122         return ASSOCIATED;
123 }
124
125
126 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
127         : ArdourDialog (
128                 t == ARDOUR::DataType::AUDIO ?
129                 _("Audio Connections Manager") :
130                 _("MIDI Connections Manager")),
131           _port_matrix (s, t)
132 {
133         get_vbox()->pack_start (_port_matrix);
134         show_all ();
135 }