Rework port matrix to use Gtk notebook tabs to select visible groups.
[ardour.git] / gtk2_ardour / io_selector.cc
1 /*
2     Copyright (C) 2002-2007 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/messagedialog.h>
21 #include <glibmm/objectbase.h>
22
23 #include <gtkmm2ext/doi.h>
24
25 #include "ardour/port_insert.h"
26 #include "ardour/session.h"
27 #include "ardour/io.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/track.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/data_type.h"
33 #include "ardour/port.h"
34 #include "ardour/bundle.h"
35
36 #include "io_selector.h"
37 #include "utils.h"
38 #include "gui_thread.h"
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace Gtk;
43
44 IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io)
45         : PortMatrix (p, session, io->default_type())
46         , _io (io)
47 {
48         /* signal flow from 0 to 1 */
49
50         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
51
52         if (_find_inputs_for_io_outputs) {
53                 _other = 1;
54                 _ours = 0;
55         } else {
56                 _other = 0;
57                 _ours = 1;
58         }
59
60         _port_group.reset (new PortGroup (io->name()));
61         _ports[_ours].add_group (_port_group);
62
63         setup_all_ports ();
64         init ();
65 }
66
67 void
68 IOSelector::setup_ports (int dim)
69 {
70         _ports[dim].suspend_signals ();
71
72         if (dim == _other) {
73
74                 _ports[_other].gather (_session, _find_inputs_for_io_outputs, false);
75
76         } else {
77
78                 _port_group->clear ();
79                 _port_group->add_bundle (_io->bundle (), _io);
80         }
81
82         _ports[dim].resume_signals ();
83 }
84
85 void
86 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
87 {
88         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
89         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
90
91         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
92                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
93
94                         Port* f = _session.engine().get_port_by_name (*i);
95                         if (!f) {
96                                 return;
97                         }
98
99                         if (s) {
100                                 _io->connect (f, *j, 0);
101                         } else {
102                                 _io->disconnect (f, *j, 0);
103                         }
104                 }
105         }
106 }
107
108 PortMatrixNode::State
109 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
110 {
111         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
112         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
113
114         if (our_ports.empty() || other_ports.empty()) {
115                 /* we're looking at a bundle with no parts associated with this channel,
116                    so nothing to connect */
117                 return PortMatrixNode::NOT_ASSOCIATED;
118         }
119
120         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
121                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
122
123                         Port* f = _session.engine().get_port_by_name (*i);
124
125                         /* since we are talking about an IO, our ports should all have an associated Port *,
126                            so the above call should never fail */
127                         assert (f);
128
129                         if (!f->connected_to (*j)) {
130                                 /* if any one thing is not connected, all bets are off */
131                                 return PortMatrixNode::NOT_ASSOCIATED;
132                         }
133                 }
134         }
135
136         return PortMatrixNode::ASSOCIATED;
137 }
138
139 uint32_t
140 IOSelector::n_io_ports () const
141 {
142         if (!_find_inputs_for_io_outputs) {
143                 return _io->n_ports().get (_io->default_type());
144         } else {
145                 return _io->n_ports().get (_io->default_type());
146         }
147 }
148
149 bool
150 IOSelector::list_is_global (int dim) const
151 {
152         return (dim == _other);
153 }
154
155 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
156         : _selector (this, session, io)
157 {
158         set_name ("IOSelectorWindow2");
159         set_title (_("I/O selector"));
160
161         add (_selector);
162
163         set_position (Gtk::WIN_POS_MOUSE);
164
165         io_name_changed (this);
166
167         show_all ();
168
169         signal_delete_event().connect (mem_fun (*this, &IOSelectorWindow::wm_delete));
170 }
171
172 bool
173 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
174 {
175         _selector.Finished (IOSelector::Accepted);
176         hide ();
177         return true;
178 }
179
180
181 void
182 IOSelectorWindow::on_map ()
183 {
184         _selector.setup_all_ports ();
185         Window::on_map ();
186 }
187
188 void
189 IOSelectorWindow::on_show ()
190 {
191         Gtk::Window::on_show ();
192         pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
193         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
194 }
195
196 void
197 IOSelectorWindow::io_name_changed (void* src)
198 {
199         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelectorWindow::io_name_changed), src));
200
201         string title;
202
203         if (!_selector.find_inputs_for_io_outputs()) {
204                 title = string_compose(_("%1 input"), _selector.io()->name());
205         } else {
206                 title = string_compose(_("%1 output"), _selector.io()->name());
207         }
208
209         set_title (title);
210 }
211
212 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
213         : input_selector (parent, sess, pi->input())
214         , output_selector (parent, sess, pi->output())
215 {
216         output_selector.set_min_height_divisor (2);
217         input_selector.set_min_height_divisor (2);
218
219         pack_start (output_selector, true, true);
220         pack_start (input_selector, true, true);
221 }
222
223 void
224 PortInsertUI::redisplay ()
225 {
226         input_selector.setup_ports (input_selector.other());
227         output_selector.setup_ports (output_selector.other());
228 }
229
230 void
231 PortInsertUI::finished (IOSelector::Result r)
232 {
233         input_selector.Finished (r);
234         output_selector.Finished (r);
235 }
236
237
238 PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
239         : ArdourDialog ("port insert dialog"),
240           _portinsertui (this, sess, pi),
241           ok_button (can_cancel ? _("OK"): _("Close")),
242           cancel_button (_("Cancel"))
243 {
244
245         set_name ("IOSelectorWindow");
246         string title = _("ardour: ");
247         title += pi->name();
248         set_title (title);
249
250         ok_button.set_name ("IOSelectorButton");
251         if (!can_cancel) {
252                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
253         }
254         cancel_button.set_name ("IOSelectorButton");
255
256         if (can_cancel) {
257                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
258                 get_action_area()->pack_start (cancel_button, false, false);
259         } else {
260                 cancel_button.hide();
261         }
262         get_action_area()->pack_start (ok_button, false, false);
263
264         get_vbox()->pack_start (_portinsertui);
265
266         ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
267         cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
268
269         signal_delete_event().connect (mem_fun (*this, &PortInsertWindow::wm_delete), false);
270
271         going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
272 }
273
274 bool
275 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
276 {
277         accept ();
278         return true;
279 }
280
281 void
282 PortInsertWindow::plugin_going_away ()
283 {
284         ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
285
286         going_away_connection.disconnect ();
287         delete_when_idle (this);
288 }
289
290 void
291 PortInsertWindow::on_map ()
292 {
293         _portinsertui.redisplay ();
294         Window::on_map ();
295 }
296
297
298 void
299 PortInsertWindow::cancel ()
300 {
301         _portinsertui.finished (IOSelector::Cancelled);
302         hide ();
303 }
304
305 void
306 PortInsertWindow::accept ()
307 {
308         _portinsertui.finished (IOSelector::Accepted);
309         hide ();
310 }
311