Remove some extraneous includes. reorder some others (fixes compilation on fedora...
[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 GlobalPortMatrix::GlobalPortMatrix (ARDOUR::Session& s, ARDOUR::DataType t)
32         : PortMatrix (s, t)
33 {
34         setup_all_ports ();
35 }
36
37 void
38 GlobalPortMatrix::setup_ports (int dim)
39 {
40         _ports[dim].suspend_signals ();
41         _ports[dim].gather (_session, dim == IN);
42         _ports[dim].resume_signals ();
43 }
44
45 void
46 GlobalPortMatrix::set_state (ARDOUR::BundleChannel c[2], bool s)
47 {
48         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
49         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
50
51         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
52                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
53
54                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
55                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
56
57                         if (p) {
58                                 if (s) {
59                                         p->connect (*j);
60                                 } else {
61                                         p->disconnect (*j);
62                                 }
63                         } else if (q) {
64                                 if (s) {
65                                         q->connect (*i);
66                                 } else {
67                                         q->disconnect (*i);
68                                 }
69                         }
70
71                         /* we don't handle connections between two non-Ardour ports */
72                 }
73         }
74 }
75
76 PortMatrixNode::State
77 GlobalPortMatrix::get_state (ARDOUR::BundleChannel c[2]) const
78 {
79         ARDOUR::Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
80         ARDOUR::Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
81         if (in_ports.empty() || out_ports.empty()) {
82                 /* we're looking at a bundle with no parts associated with this channel,
83                    so nothing to connect */
84                 return PortMatrixNode::UNKNOWN;
85         }
86                 
87         for (ARDOUR::Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
88                 for (ARDOUR::Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
89
90                         ARDOUR::Port* p = _session.engine().get_port_by_name (*i);
91                         ARDOUR::Port* q = _session.engine().get_port_by_name (*j);
92
93                         /* we don't know the state of connections between two non-Ardour ports */
94                         if (!p && !q) {
95                                 return PortMatrixNode::UNKNOWN;
96                         }
97
98                         if (p && p->connected_to (*j) == false) {
99                                 return PortMatrixNode::NOT_ASSOCIATED;
100                         } else if (q && q->connected_to (*i) == false) {
101                                 return PortMatrixNode::NOT_ASSOCIATED;
102                         }
103
104                 }
105         }
106
107         return PortMatrixNode::ASSOCIATED;
108 }
109
110 GlobalPortMatrixWindow::GlobalPortMatrixWindow (ARDOUR::Session& s, ARDOUR::DataType t)
111         : _port_matrix (s, t)
112 {
113         switch (t) {
114         case ARDOUR::DataType::AUDIO:
115                 set_title (_("Audio Connections Manager"));
116                 break;
117         case ARDOUR::DataType::MIDI:
118                 set_title (_("MIDI Connections Manager"));
119                 break;
120         }
121
122         Gtk::HBox* buttons_hbox = Gtk::manage (new Gtk::HBox);
123
124         _rescan_button.set_label (_("Rescan"));
125         _rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
126         _rescan_button.signal_clicked().connect (sigc::mem_fun (_port_matrix, &GlobalPortMatrix::setup_all_ports));
127         buttons_hbox->pack_start (_rescan_button, Gtk::PACK_SHRINK);
128
129         _show_ports_button.set_label (_("Show individual ports"));
130         _show_ports_button.set_active (true);
131         _show_ports_button.signal_toggled().connect (sigc::mem_fun (*this, &GlobalPortMatrixWindow::show_ports_toggled));
132         buttons_hbox->pack_start (_show_ports_button, Gtk::PACK_SHRINK);
133         
134         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox);
135         vbox->pack_start (_port_matrix);
136         vbox->pack_start (*buttons_hbox, Gtk::PACK_SHRINK);
137         add (*vbox);
138         show_all ();
139 }
140
141 void
142 GlobalPortMatrixWindow::show_ports_toggled ()
143 {
144         _port_matrix.set_show_only_bundles (!_show_ports_button.get_active());
145 }