Group destination ports by track / buss / system etc.
[ardour.git] / gtk2_ardour / io_selector.cc
1 /*
2     Copyright (C) 2002-2003 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/label.h>
21 #include <gtkmm/enums.h>
22 #include <gtkmm/image.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/messagedialog.h>
25 #include <glibmm/objectbase.h>
26 #include <gtkmm2ext/doi.h>
27 #include <ardour/port_insert.h>
28 #include "ardour/session.h"
29 #include "ardour/io.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/track.h"
32 #include "io_selector.h"
33 #include "utils.h"
34 #include "gui_thread.h"
35 #include "i18n.h"
36
37 RotatedLabelSet::RotatedLabelSet (GroupedPortList& g)
38         : Glib::ObjectBase ("RotatedLabelSet"), Gtk::Widget (), _port_list (g), _base_start (64), _base_width (128)
39 {
40         set_flags (Gtk::NO_WINDOW);
41         set_angle (30);
42 }
43
44 RotatedLabelSet::~RotatedLabelSet ()
45 {
46         
47 }
48
49
50 /** Set the angle that the labels are drawn at.
51  * @param degrees New angle in degrees.
52  */
53
54 void
55 RotatedLabelSet::set_angle (int degrees)
56 {
57         _angle_degrees = degrees;
58         _angle_radians = M_PI * _angle_degrees / 180;
59
60         queue_resize ();
61 }
62
63 void
64 RotatedLabelSet::on_size_request (Gtk::Requisition* requisition)
65 {
66         *requisition = Gtk::Requisition ();
67
68         if (_pango_layout == 0) {
69                 return;
70         }
71
72         /* Our height is the highest label */
73         requisition->height = 0;
74         for (GroupedPortList::const_iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
75                 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
76                         std::pair<int, int> const d = setup_layout (*j);
77                         if (d.second > requisition->height) {
78                                 requisition->height = d.second;
79                         }
80                 }
81         }
82
83         /* And our width is the base plus the width of the last label */
84         requisition->width = _base_start + _base_width;
85         int const n = _port_list.n_ports ();
86         if (n > 0) {
87                 std::pair<int, int> const d = setup_layout (_port_list.get_port_by_index (n - 1, false));
88                 requisition->width += d.first;
89         }
90 }
91
92 void
93 RotatedLabelSet::on_size_allocate (Gtk::Allocation& allocation)
94 {
95         set_allocation (allocation);
96
97         if (_gdk_window) {
98                 _gdk_window->move_resize (
99                         allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height()
100                         );
101         }
102 }
103
104 void
105 RotatedLabelSet::on_realize ()
106 {
107         Gtk::Widget::on_realize ();
108
109         Glib::RefPtr<Gtk::Style> style = get_style ();
110
111         if (!_gdk_window) {
112                 GdkWindowAttr attributes;
113                 memset (&attributes, 0, sizeof (attributes));
114
115                 Gtk::Allocation allocation = get_allocation ();
116                 attributes.x = allocation.get_x ();
117                 attributes.y = allocation.get_y ();
118                 attributes.width = allocation.get_width ();
119                 attributes.height = allocation.get_height ();
120
121                 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK; 
122                 attributes.window_type = GDK_WINDOW_CHILD;
123                 attributes.wclass = GDK_INPUT_OUTPUT;
124
125                 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
126                 unset_flags (Gtk::NO_WINDOW);
127                 set_window (_gdk_window);
128
129                 _bg_colour = style->get_bg (Gtk::STATE_NORMAL );
130                 modify_bg (Gtk::STATE_NORMAL, _bg_colour);
131                 _fg_colour = style->get_fg (Gtk::STATE_NORMAL);
132 ;
133                 _gdk_window->set_user_data (gobj ());
134
135                 /* Set up Pango stuff */
136                 _pango_context = create_pango_context ();
137
138                 Pango::Matrix matrix = PANGO_MATRIX_INIT;
139                 pango_matrix_rotate (&matrix, _angle_degrees);
140                 _pango_context->set_matrix (matrix);
141
142                 _pango_layout = Pango::Layout::create (_pango_context);
143                 _gc = Gdk::GC::create (get_window ());
144         }
145 }
146
147 void
148 RotatedLabelSet::on_unrealize()
149 {
150         _gdk_window.clear ();
151
152         Gtk::Widget::on_unrealize ();
153 }
154
155
156 /**
157  *    Set up our Pango layout to plot a given string, and compute its dimensions once it has been rotated.
158  *    @param s String to use.
159  *    @return width and height of the rotated string, in pixels.
160  */
161
162 std::pair<int, int>
163 RotatedLabelSet::setup_layout (std::string const & s)
164 {
165         _pango_layout->set_text (s);
166
167         /* Here's the unrotated size */
168         int w;
169         int h;
170         _pango_layout->get_pixel_size (w, h);
171
172         /* Rotate the width and height as appropriate.  I thought Pango might be able to do this for us,
173            but I can't find out how... */
174         std::pair<int, int> d;
175         d.first = int (w * cos (_angle_radians) - h * sin (_angle_radians));
176         d.second = int (w * sin (_angle_radians) + h * cos (_angle_radians));
177
178         return d;
179 }
180
181 bool
182 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
183 {
184         if (!_gdk_window) {
185                 return true;
186         }
187
188         int const height = get_allocation().get_height ();
189         double const spacing = double (_base_width) / _port_list.n_ports();
190
191         /* Plot all the labels; really we should clip for efficiency */
192         int n = 0;
193         for (GroupedPortList::const_iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
194                 for (uint32_t j = 0; j < i->ports.size(); ++j) {
195                         std::pair<int, int> const d = setup_layout (i->ports[j]);
196                         get_window()->draw_layout (_gc, _base_start + int ((n + 0.25) * spacing), height - d.second, _pango_layout, _fg_colour, _bg_colour);
197                         ++n;
198                 }
199         }
200
201         return true;
202 }
203
204 /**
205  *  Set the `base dimensions'.  These are the dimensions of the area at which the labels start, and
206  *  have to be set up to match whatever they are labelling.
207  *
208  *  Roughly speaking, we have
209  *
210  *             L L L L
211  *            E E E E
212  *           B B B B
213  *          A A A A
214  *         L L L L
215  * <--s--><--w--->
216  */
217     
218 void
219 RotatedLabelSet::set_base_dimensions (int s, int w)
220 {
221         _base_start = s;
222         _base_width = w;
223         queue_resize ();
224 }
225
226
227 /**
228  *  Construct an IOSelector.
229  *  @param session Session to operate on.
230  *  @param io IO to operate on.
231  *  @param for_input true if the selector is for an input, otherwise false.
232  */
233  
234 IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input)
235         : _session (session), _port_list (session, io, for_input), _io (io), _for_input (for_input),
236           _width (0), _height (0), _column_labels (_port_list),
237           _ignore_check_button_toggle (false), _add_remove_box_added (false)
238 {
239         /* Column labels */
240         pack_start (_column_labels, true, true);
241
242         /* Buttons for adding and removing ports */
243         _add_port_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_MENU)));
244         _add_port_button.set_label (_("Add port"));
245         _add_port_button.signal_clicked().connect (mem_fun (*this, &IOSelector::add_port_button_clicked));
246         _add_remove_box.pack_start (_add_port_button);
247         _remove_port_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REMOVE, Gtk::ICON_SIZE_MENU)));
248         _remove_port_button.set_label (_("Remove port"));
249         _remove_port_button.signal_clicked().connect (mem_fun (*this, &IOSelector::remove_port_button_clicked));
250         _add_remove_box.pack_start (_remove_port_button);
251         set_button_sensitivity ();
252
253         /* Table.  We need to put in a HBox, with a dummy label to its right,
254            so that the rotated column labels can overhang the right hand side of the table. */
255
256         setup_table ();
257         setup_row_labels ();
258         setup_check_button_states ();
259         _table_hbox.pack_start (_table, false, false);
260         _table_hbox.pack_start (_dummy);
261         _table.set_col_spacings (4);
262         pack_start (_table_hbox);
263
264         show_all ();
265
266         update_column_label_dimensions ();
267
268         /* Listen for ports changing on the IO */
269         if (_for_input) {
270                 _io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
271         } else {
272                 _io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
273         }
274         
275 }
276
277 IOSelector::~IOSelector ()
278 {
279         for (std::vector<Gtk::Label*>::iterator i = _row_labels.begin(); i != _row_labels.end(); ++i) {
280                 delete *i;
281         }
282
283         for (uint32_t x = 0; x < _check_buttons.size(); ++x) {
284                 for (uint32_t y = 0; y < _check_buttons[x].size(); ++y) {
285                         delete _check_buttons[x][y];
286                 }
287         }
288 }
289
290
291 /**
292  *    Sets up the sizing of the column label widget to match the table that's underneath it.
293  */
294
295 void
296 IOSelector::update_column_label_dimensions ()
297 {
298         if (_row_labels.empty() || _check_buttons.empty() || _check_buttons[0].empty()) {
299                 return;
300         }
301         
302         _column_labels.set_base_dimensions (
303                 /* width of the row label + a column spacing */
304                 _row_labels.front()->get_width() + _table.get_col_spacing (0),
305                 /* width of a check button + a column spacing for each column */
306                 _check_buttons.size() * ( _check_buttons[0][0]->get_width() + _table.get_col_spacing(1))
307                 );
308 }
309
310 void
311 IOSelector::setup_table ()
312 {
313         if (_add_remove_box_added) {
314                 _table.remove (_add_remove_box);
315         }
316
317         for (std::vector<Gtk::EventBox*>::iterator i = _group_labels.begin(); i != _group_labels.end(); ++i) {
318                 _table.remove (**i);
319                 delete *i;
320         }
321
322         _group_labels.clear ();
323         
324         /* New width */
325         
326         int const old_width = _width;
327         _width = _port_list.n_ports ();
328
329         /* New height */
330
331         int const old_height = _height;
332
333         ARDOUR::DataType const t = _io->default_type();
334         
335         if (_for_input) {
336                 _height = _io->n_inputs().get(t);
337         } else {
338                 _height = _io->n_outputs().get(t);
339         }
340
341         _table.resize (_width + 1, _height + 1);
342
343         /* Add checkbuttons where required, and remove those that aren't */
344         for (int x = _width; x < old_width; ++x) {
345                 for (int y = 0; y < old_height; ++y) {
346                         delete _check_buttons[x][y];
347                 }
348         }
349         _check_buttons.resize (_width);
350         
351         for (int x = 0; x < _width; x++) {
352
353                 for (int y = _height; y < old_height; ++y) {
354                         delete _check_buttons[x][y];
355                 }
356                 _check_buttons[x].resize (_height);
357                 
358                 for (int y = 0; y < _height; y++) {
359
360                         if (x >= old_width || y >= old_height) {
361                                 Gtk::CheckButton* button = new Gtk::CheckButton;
362                                 button->signal_toggled().connect (
363                                         sigc::bind (sigc::mem_fun (*this, &IOSelector::check_button_toggled), x, y)
364                                         );
365                                 _check_buttons[x][y] = button;
366                                 _table.attach (*button, x + 1, x + 2, y, y + 1);
367                         }
368                 }
369         }
370
371         /* Add more row labels where required, and remove those that aren't */
372         for (int y = _height; y < old_height; ++y) {
373                 delete _row_labels[y];
374         }
375         _row_labels.resize (_height);
376         for (int y = old_height; y < _height; ++y) {
377                 Gtk::Label* label = new Gtk::Label;
378                 _row_labels[y] = label;
379                 _table.attach (*label, 0, 1, y, y + 1);
380         }
381
382         _table.attach (_add_remove_box, 0, 1, _height, _height + 1);
383         _add_remove_box_added = true;
384
385         /* Add group labels */
386         int n = 1;
387         int m = 0;
388
389         /* XXX: this is a bit of a hack; should probably use a configurable colour here */
390         Gdk::Color alt_bg = get_style()->get_bg (Gtk::STATE_NORMAL);
391         alt_bg.set_rgb (alt_bg.get_red() + 4096, alt_bg.get_green() + 4096, alt_bg.get_blue () + 4096);
392         for (GroupedPortList::iterator i = _port_list.begin(); i != _port_list.end(); ++i) {
393                 if (i->ports.empty() == false) {
394                         Gtk::Label* label = new Gtk::Label ("<b>" + i->name + "</b>");
395                         label->set_use_markup (true);
396                         Gtk::EventBox* box = new Gtk::EventBox ();
397                         box->add (*Gtk::manage (label));
398                         if (m % 2 == 0) {
399                                 box->modify_bg (Gtk::STATE_NORMAL, alt_bg);
400                         }
401                         _group_labels.push_back (box);
402                         _table.attach (*box, n, n + i->ports.size(), _height, _height + 1);
403                         n += i->ports.size();
404                         ++m;
405                 }
406         }
407
408         show_all ();
409 }
410
411
412 /**
413  *  Write the correct text to the row labels.
414  */
415
416 void
417 IOSelector::setup_row_labels ()
418 {
419         for (int y = 0; y < _height; y++) {
420                 _row_labels[y]->set_text (_for_input ? _io->input(y)->name() : _io->output(y)->name());
421         }
422 }
423
424
425 /**
426  *  Set up the state of each check button according to what connections are made.
427  */
428
429 void
430 IOSelector::setup_check_button_states ()
431 {
432         _ignore_check_button_toggle = true;
433
434         /* Set the state of the check boxes according to current connections */
435         for (int i = 0; i < _height; ++i) {
436                 const char **connections = _for_input ? _io->input(i)->get_connections() : _io->output(i)->get_connections();
437                 for (int j = 0; j < _width; ++j) {
438
439                         std::string const t = _port_list.get_port_by_index (j);
440                         int k = 0;
441                         bool required_state = false;
442
443                         while (connections && connections[k]) {
444                                 if (std::string(connections[k]) == t) {
445                                         required_state = true;
446                                         break;
447                                 }
448                                 ++k;
449                         }
450
451                         _check_buttons[j][i]->set_active (required_state);
452                 }
453         }
454
455         _ignore_check_button_toggle = false;
456 }
457
458
459 /**
460  *  Handle a toggle of a check button.
461  */
462
463 void
464 IOSelector::check_button_toggled (int x, int y)
465 {
466         if (_ignore_check_button_toggle) {
467                 return;
468         }
469         
470         bool const new_state = _check_buttons[x][y]->get_active ();
471         std::string const port = _port_list.get_port_by_index (x);
472
473         if (new_state) {
474                 if (_for_input) {
475                         _io->connect_input (_io->input(y), port, 0);
476                 } else {
477                         _io->connect_output (_io->output(y), port, 0);
478                 }
479         } else {
480                 if (_for_input) {
481                         _io->disconnect_input (_io->input(y), port, 0);
482                 } else {
483                         _io->disconnect_output (_io->output(y), port, 0);
484                 }
485         }
486 }
487
488 void
489 IOSelector::add_port_button_clicked ()
490 {
491         /* add a new port, then hide the button if we're up to the maximum allowed */
492
493         // The IO selector only works for single typed IOs
494         const ARDOUR::DataType t = _io->default_type();
495
496         if (_for_input) {
497
498                 try {
499                         _io->add_input_port ("", this);
500                 }
501
502                 catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
503                         Gtk::MessageDialog msg (0,  _("There are no more JACK ports available."));
504                         msg.run ();
505                 }
506
507         } else {
508
509                 try {
510                         _io->add_output_port ("", this);
511                 }
512
513                 catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
514                         Gtk::MessageDialog msg (0, _("There are no more JACK ports available."));
515                         msg.run ();
516                 }
517         }
518                 
519         set_button_sensitivity ();
520 }
521
522
523 void
524 IOSelector::remove_port_button_clicked ()
525 {
526         uint32_t nports;
527
528         // The IO selector only works for single typed IOs
529         const ARDOUR::DataType t = _io->default_type();
530         
531         // always remove last port
532         
533         if (_for_input) {
534                 if ((nports = _io->n_inputs().get(t)) > 0) {
535                         _io->remove_input_port (_io->input(nports - 1), this);
536                 }
537         } else {
538                 if ((nports = _io->n_outputs().get(t)) > 0) {
539                         _io->remove_output_port (_io->output(nports - 1), this);
540                 }
541         }
542         
543         set_button_sensitivity ();
544 }
545
546
547 void 
548 IOSelector::set_button_sensitivity ()
549 {
550         ARDOUR::DataType const t = _io->default_type();
551
552         if (_for_input) {
553
554                 _add_port_button.set_sensitive (
555                         _io->input_maximum().get(t) > _io->n_inputs().get(t)
556                         );
557         } else {
558
559                 _add_port_button.set_sensitive (
560                         _io->output_maximum().get(t) > _io->n_outputs().get(t)
561                         );
562         }
563
564         if (_for_input) {
565                 
566                 _remove_port_button.set_sensitive (
567                         _io->n_inputs().get(t) && _io->input_minimum().get(t) < _io->n_inputs().get(t)
568                         );
569         } else {
570
571                 _remove_port_button.set_sensitive (
572                         _io->n_outputs().get(t) && _io->output_minimum().get(t) < _io->n_outputs().get(t)
573                         );
574         }
575 }
576
577 void
578 IOSelector::ports_changed (ARDOUR::IOChange change, void *src)
579 {
580         ENSURE_GUI_THREAD (bind (mem_fun (*this, &IOSelector::ports_changed), change, src));
581
582         redisplay ();
583 }
584
585
586 void
587 IOSelector::redisplay ()
588 {
589         _port_list.refresh ();
590         setup_table ();
591         setup_row_labels ();
592         setup_check_button_states ();
593         update_column_label_dimensions ();
594 }
595
596
597
598
599 GroupedPortList::GroupedPortList (ARDOUR::Session & session, boost::shared_ptr<ARDOUR::IO> io, bool for_input)
600         : _session (session), _io (io), _for_input (for_input)
601 {
602         refresh ();
603 }
604
605 void
606 GroupedPortList::refresh ()
607 {
608         clear ();
609
610         /* Find the ports provided by ardour; we can't derive their type just from their
611            names, so we'll have to be more devious. */
612
613         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
614
615         PortGroup buss (_("Buss"), "ardour:");
616         PortGroup track (_("Track"), "ardour:");
617
618         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
619
620                 PortGroup& g = dynamic_cast<ARDOUR::Track*> ((*i).get()) == 0 ? buss : track;
621                 
622                 ARDOUR::PortSet const & p = _for_input ? ((*i)->outputs()) : ((*i)->inputs());
623                 for (uint32_t j = 0; j < p.num_ports(); ++j) {
624                         std::string const n = p.port(j)->name ();
625                         g.ports.push_back (n.substr(strlen ("ardour:")));
626                 }
627
628                 std::sort (g.ports.begin(), g.ports.end());
629         }
630         
631
632         /* XXX: inserts, sends, plugin inserts? */
633         
634         /* Now we need to find the non-ardour ports; we do this by first
635            finding all the ports that we can connect to. */
636         const char **ports = _session.engine().get_ports (
637                 "", _io->default_type().to_jack_type(), _for_input ? JackPortIsOutput : JackPortIsInput
638                 );
639
640         PortGroup system (_("System"), "system:");
641         PortGroup other (_("Other"), "");
642         
643         if (ports) {
644
645                 /* Count them */
646                 int n = 0;
647                 while (ports[n]) {
648                         ++n;
649                 }
650                 
651                 for (int i = 0; i < n; ++i) {
652                         std::string const p = ports[i];
653
654                         if (p.substr(0, strlen ("system:")) == "system:") {
655                                 /* system: prefix */
656                                 system.ports.push_back (p.substr (strlen ("system:")));
657                         } else {
658                                 if (p.substr(0, strlen("ardour:")) != "ardour:") {
659                                         /* other (non-ardour) prefix */
660                                         other.ports.push_back (p);
661                                 }
662                         }
663                 }
664         }
665
666         push_back (buss);
667         push_back (track);
668         push_back (system);
669         push_back (other);
670 }
671
672 int
673 GroupedPortList::n_ports () const
674 {
675         int n = 0;
676         
677         for (const_iterator i = begin(); i != end(); ++i) {
678                 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
679                         ++n;
680                 }
681         }
682
683         return n;
684 }
685
686 std::string
687 GroupedPortList::get_port_by_index (int n, bool with_prefix) const
688 {
689         /* XXX: slightly inefficient algorithm */
690
691         for (const_iterator i = begin(); i != end(); ++i) {
692                 for (std::vector<std::string>::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
693                         if (n == 0) {
694                                 if (with_prefix) {
695                                         return i->prefix + *j;
696                                 } else {
697                                         return *j;
698                                 }
699                         }
700                         --n;
701                 }
702         }
703
704         return "";
705 }
706
707
708 IOSelectorWindow::IOSelectorWindow (
709         ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input, bool can_cancel
710         )
711         : ArdourDialog ("I/O selector"),
712           _selector (session, io, for_input),
713           ok_button (can_cancel ? _("OK"): _("Close")),
714           cancel_button (_("Cancel")),
715           rescan_button (_("Rescan"))
716
717 {
718         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
719         set_name ("IOSelectorWindow2");
720
721         string title;
722         if (for_input) {
723                 title = string_compose(_("%1 input"), io->name());
724         } else {
725                 title = string_compose(_("%1 output"), io->name());
726         }
727
728         ok_button.set_name ("IOSelectorButton");
729         if (!can_cancel) {
730                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
731         }
732         cancel_button.set_name ("IOSelectorButton");
733         rescan_button.set_name ("IOSelectorButton");
734         rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
735
736         get_action_area()->pack_start (rescan_button, false, false);
737
738         if (can_cancel) {
739                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
740                 get_action_area()->pack_start (cancel_button, false, false);
741         } else {
742                 cancel_button.hide();
743         }
744                 
745         get_action_area()->pack_start (ok_button, false, false);
746
747         get_vbox()->set_spacing (8);
748         get_vbox()->pack_start (_selector);
749
750         ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
751         cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
752         rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
753
754         set_title (title);
755         set_position (Gtk::WIN_POS_MOUSE);
756
757         show_all ();
758
759         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
760 }
761
762 IOSelectorWindow::~IOSelectorWindow()
763 {
764         
765 }
766
767 void
768 IOSelectorWindow::rescan ()
769 {
770         _selector.redisplay ();
771 }
772
773 void
774 IOSelectorWindow::cancel ()
775 {
776         _selector.Finished (IOSelector::Cancelled);
777         hide ();
778 }
779
780 void
781 IOSelectorWindow::accept ()
782 {
783         _selector.Finished (IOSelector::Accepted);
784         hide ();
785 }
786
787 void
788 IOSelectorWindow::on_map ()
789 {
790         _selector.redisplay ();
791         Window::on_map ();
792 }
793
794
795 PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
796         : input_selector (sess, pi->io(), true),
797           output_selector (sess, pi->io(), false)
798 {
799         hbox.pack_start (output_selector, true, true);
800         hbox.pack_start (input_selector, true, true);
801
802         pack_start (hbox);
803 }
804
805 void
806 PortInsertUI::redisplay ()
807 {
808         input_selector.redisplay();
809         output_selector.redisplay();
810 }
811
812 void
813 PortInsertUI::finished (IOSelector::Result r)
814 {
815         input_selector.Finished (r);
816         output_selector.Finished (r);
817 }
818
819
820 PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
821         : ArdourDialog ("port insert dialog"),
822           _portinsertui (sess, pi),
823           ok_button (can_cancel ? _("OK"): _("Close")),
824           cancel_button (_("Cancel")),
825           rescan_button (_("Rescan"))
826 {
827
828         set_name ("IOSelectorWindow");
829         string title = _("ardour: ");
830         title += pi->name();
831         set_title (title);
832         
833         ok_button.set_name ("IOSelectorButton");
834         if (!can_cancel) {
835                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
836         }
837         cancel_button.set_name ("IOSelectorButton");
838         rescan_button.set_name ("IOSelectorButton");
839         rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
840
841         get_action_area()->pack_start (rescan_button, false, false);
842         if (can_cancel) {
843                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
844                 get_action_area()->pack_start (cancel_button, false, false);
845         } else {
846                 cancel_button.hide();
847         }
848         get_action_area()->pack_start (ok_button, false, false);
849
850         get_vbox()->pack_start (_portinsertui);
851
852         ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
853         cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
854         rescan_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::rescan));
855
856         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
857
858         going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
859 }
860
861 void
862 PortInsertWindow::plugin_going_away ()
863 {
864         ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
865         
866         going_away_connection.disconnect ();
867         delete_when_idle (this);
868 }
869
870 void
871 PortInsertWindow::on_map ()
872 {
873         _portinsertui.redisplay ();
874         Window::on_map ();
875 }
876
877
878 void
879 PortInsertWindow::rescan ()
880 {
881         _portinsertui.redisplay ();
882 }
883
884 void
885 PortInsertWindow::cancel ()
886 {
887         _portinsertui.finished (IOSelector::Cancelled);
888         hide ();
889 }
890
891 void
892 PortInsertWindow::accept ()
893 {
894         _portinsertui.finished (IOSelector::Accepted);
895         hide ();
896 }