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