another get_vbox() fix, this time for PortSelectorWindow
[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     $Id$
19 */
20
21 #include <map>
22 #include <vector>
23
24 #include <pbd/lockmonitor.h>
25
26 #include <ardour/io.h>
27 #include <ardour/route.h>
28 #include <ardour/audioengine.h>
29 #include <ardour/port.h>
30 #include <ardour/insert.h>
31 #include <ardour/session.h>
32 #include <ardour/diskstream.h>
33
34 #include <gtkmm2ext/doi.h>
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/utils.h>
37
38 #include "utils.h"
39 #include "ardour_message.h"
40 #include "io_selector.h"
41 #include "keyboard.h"
42 #include "gui_thread.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace Glib;
49 using namespace sigc;
50 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
52
53 IOSelectorWindow::IOSelectorWindow (Session& sess, IO& ior, bool input, bool can_cancel)
54         : ArdourDialog ("i/o selector"),
55           _selector (sess, ior, input),
56           ok_button (can_cancel ? _("OK"): _("Close")),
57           cancel_button (_("Cancel")),
58           rescan_button (_("Rescan"))
59
60 {
61         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
62         set_name ("IOSelectorWindow");
63
64         string title;
65         if (input) {
66                 title = string_compose(_("%1 input"), ior.name());
67         } else {
68                 title = string_compose(_("%1 output"), ior.name());
69         }
70
71         ok_button.set_name ("IOSelectorButton");
72         cancel_button.set_name ("IOSelectorButton");
73         rescan_button.set_name ("IOSelectorButton");
74
75         button_box.set_spacing (5);
76         button_box.set_border_width (5);
77         button_box.set_homogeneous (true);
78         button_box.pack_start (rescan_button);
79
80         if (can_cancel) {
81                 button_box.pack_start (cancel_button);
82         } else {
83                 cancel_button.hide();
84         }
85                 
86         button_box.pack_start (ok_button);
87
88         get_vbox()->pack_start (_selector);
89         get_vbox()->pack_start (button_box, false, false);
90
91         ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
92         cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
93         rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
94
95         set_title (title);
96         set_position (WIN_POS_MOUSE);
97
98         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
99 }
100
101 IOSelectorWindow::~IOSelectorWindow()
102 {
103 }
104
105 void
106 IOSelectorWindow::rescan ()
107 {
108         _selector.redisplay ();
109 }
110
111 void
112 IOSelectorWindow::cancel ()
113 {
114         _selector.Finished(IOSelector::Cancelled);
115         hide ();
116 }
117
118 void
119 IOSelectorWindow::accept ()
120 {
121         _selector.Finished(IOSelector::Accepted);
122         hide ();
123 }
124
125 void
126 IOSelectorWindow::on_map ()
127 {
128         _selector.redisplay ();
129         Window::on_map ();
130 }
131
132 /*************************
133   The IO Selector "widget"
134  *************************/  
135
136 IOSelector::IOSelector (Session& sess, IO& ior, bool input)
137         : session (sess),
138           io (ior),
139           for_input (input),
140           port_frame (for_input? _("Inputs") : _("Outputs")),
141           add_port_button (for_input? _("Add Input") : _("Add Output")),
142           remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
143           clear_connections_button (_("Disconnect All"))
144 {
145         selected_port = 0;
146
147         notebook.set_name ("IOSelectorNotebook");
148         notebook.set_size_request (-1, 125);
149
150         clear_connections_button.set_name ("IOSelectorButton");
151         add_port_button.set_name ("IOSelectorButton");
152         remove_port_button.set_name ("IOSelectorButton");
153
154         selector_frame.set_name ("IOSelectorFrame");
155         port_frame.set_name ("IOSelectorFrame");
156
157         selector_frame.set_label (_("Available connections"));
158         
159         selector_button_box.set_spacing (5);
160         selector_button_box.set_border_width (5);
161
162         selector_box.set_spacing (5);
163         selector_box.set_border_width (5);
164         selector_box.pack_start (notebook);
165         selector_box.pack_start (selector_button_box, false, false);
166
167         selector_frame.add (selector_box);
168
169         port_box.set_spacing (5);
170         port_box.set_border_width (5);
171
172         port_display_scroller.set_name ("IOSelectorNotebook");
173         port_display_scroller.set_border_width (0);
174         port_display_scroller.set_size_request (-1, 170);
175         port_display_scroller.add (port_box);
176         port_display_scroller.set_policy (POLICY_NEVER,
177                                           POLICY_AUTOMATIC);
178
179         port_button_box.set_spacing (5);
180         port_button_box.set_border_width (5);
181
182         port_button_box.pack_start (add_port_button, false, false);
183
184         if (for_input) {
185                 if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
186                         add_port_button.set_sensitive (true);
187                 } else {
188                         add_port_button.set_sensitive (false);
189                 }
190
191         } else {
192                 if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
193                         add_port_button.set_sensitive (true);
194                 } else {
195                         add_port_button.set_sensitive (false);
196                 }
197                         
198         }
199
200         port_button_box.pack_start (remove_port_button, false, false);
201
202         if (for_input) {
203                 if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
204                         remove_port_button.set_sensitive (true);
205                 } else {
206                         remove_port_button.set_sensitive (false);
207                 }
208                         
209         } else {
210                 if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
211                         remove_port_button.set_sensitive (true);
212                 } else {
213                         remove_port_button.set_sensitive (false);
214                 }
215         }
216
217         port_button_box.pack_start (clear_connections_button, false, false);
218
219         port_and_button_box.set_border_width (5);
220         port_and_button_box.pack_start (port_button_box, false, false);
221         port_and_button_box.pack_start (port_display_scroller);
222
223         port_frame.add (port_and_button_box);
224
225         port_and_selector_box.set_spacing (5);
226         port_and_selector_box.pack_start (port_frame);
227         port_and_selector_box.pack_start (selector_frame);
228
229         set_spacing (5);
230         set_border_width (5);
231         pack_start (port_and_selector_box);
232
233         rescan();
234         display_ports ();
235
236         clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
237
238         add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
239         remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
240
241         if (for_input) {
242                 io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
243         } else {
244                 io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
245         }
246
247         io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
248 }
249
250 IOSelector::~IOSelector ()
251 {
252 }
253
254 void
255 IOSelector::name_changed (void* src)
256 {
257         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
258         
259         display_ports ();
260 }
261
262 void
263 IOSelector::clear_connections ()
264 {
265         if (for_input) {
266                 io.disconnect_inputs (this);
267         } else {
268                 io.disconnect_outputs (this);
269         }
270 }
271
272 void
273 IOSelector::rescan ()
274 {
275         using namespace Notebook_Helpers;
276
277         typedef std::map<string,vector<pair<string,string> > > PortMap;
278         PortMap portmap;
279         const char **ports;
280         PageList& pages = notebook.pages();
281         gint current_page;
282         vector<string> rowdata;
283
284         current_page = notebook.get_current_page ();
285         pages.clear ();
286
287         /* get relevant current JACK ports */
288
289         ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
290
291         if (ports == 0) {
292                 return;
293         }
294
295         /* find all the client names and group their ports into a list-by-client */
296         
297         for (int n = 0; ports[n]; ++n) {
298
299                 pair<string,vector<pair<string,string> > > newpair;
300                 pair<string,string> strpair;
301                 pair<PortMap::iterator,bool> result;
302
303                 string str = ports[n];
304                 string::size_type pos;
305                 string portname;
306
307                 pos = str.find (':');
308
309                 newpair.first = str.substr (0, pos);
310                 portname = str.substr (pos+1);
311
312                 result = portmap.insert (newpair);
313
314                 strpair.first = portname;
315                 strpair.second = str;
316
317                 result.first->second.push_back (strpair);
318         }
319
320         PortMap::iterator i;
321
322         for (i = portmap.begin(); i != portmap.end(); ++i) {
323                 
324                 Box *client_box = manage (new VBox);
325                 TreeView *display = manage (new TreeView);
326                 RefPtr<ListStore> model = ListStore::create (port_display_columns);
327                 ScrolledWindow *scroller = manage (new ScrolledWindow);
328
329                 display->set_model (model);
330                 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
331                 display->set_headers_visible (false);
332                 display->get_selection()->set_mode (SELECTION_SINGLE);
333                 display->set_name ("IOSelectorList");
334
335                 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
336                         
337                         TreeModel::Row row = *(model->append ());
338
339                         row[port_display_columns.displayed_name] = s->first;
340                         row[port_display_columns.full_name] = s->second;
341                 }
342
343                 display->get_selection()->signal_changed().connect 
344                         (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
345                 
346                 Label *tab_label = manage (new Label);
347
348                 tab_label->set_name ("IOSelectorNotebookTab");
349                 tab_label->set_text ((*i).first);
350
351                 scroller->add (*display);
352                 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
353
354                 client_box->pack_start (*scroller);
355
356                 pages.push_back (TabElem (*client_box, *tab_label));
357         }
358
359         notebook.set_current_page (current_page);
360         notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
361         selector_box.show_all ();
362 }       
363
364 void
365 IOSelector::display_ports ()
366 {
367         TreeView *firsttview = 0;
368         TreeView *selected_port_tview = 0;
369
370         {
371                 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
372                 Port *port;
373                 uint32_t limit;
374
375                 if (for_input) {
376                         limit = io.n_inputs();
377                 } else {
378                         limit = io.n_outputs();
379                 }
380
381                 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
382                 
383                         slist<TreeView *>::iterator tmp;
384
385                         tmp = i;
386                         ++tmp;
387
388                         port_box.remove (**i);
389                         delete *i;
390                         port_displays.erase (i);
391
392                         i = tmp;
393                 } 
394
395
396                 for (uint32_t n = 0; n < limit; ++n) {
397                         
398                         TreeView* tview;
399                         //ScrolledWindow *scroller;
400                         string really_short_name;
401                         
402                         if (for_input) {
403                                 port = io.input (n);
404                         } else {
405                                 port = io.output (n);
406                         }
407                         
408                         /* we know there is '/' because we put it there */
409
410                         really_short_name = port->short_name();
411                         really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
412
413                         tview = manage (new TreeView());
414                         RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
415
416                         if (!firsttview) {
417                                 firsttview = tview;
418                         }
419                         
420                         tview->set_model (port_model);
421                         tview->append_column (really_short_name, port_display_columns.displayed_name);
422                         tview->get_selection()->set_mode (SELECTION_SINGLE);
423                         tview->set_data ("port", port);
424                         tview->set_headers_visible (true);
425                         tview->set_name ("IOSelectorPortList");
426                         
427                         port_box.pack_start (*tview);
428                         //scroller = manage (new ScrolledWindow);
429                         
430                         //scroller->add (*tview);
431                         //scroller->set_policy (POLICY_NEVER, POLICY_NEVER);
432                         
433                         port_displays.insert (port_displays.end(), tview);
434                         //port_box.pack_start (*scroller);
435                         
436                         //scroller->set_size_request (-1, 75);
437                         
438                         /* now fill the clist with the current connections */
439                         
440
441                         const char **connections = port->get_connections ();
442
443                         if (connections) {
444                                 for (uint32_t c = 0; connections[c]; ++c) {
445                                         TreeModel::Row row = *(port_model->append());
446                                         row[port_display_columns.displayed_name] = connections[c];
447                                         row[port_display_columns.full_name] = connections[c];
448                                 }
449                         }
450                         
451                         if (for_input) {
452                                 
453                                 if (io.input_maximum() == 1) {
454                                         selected_port = port;
455                                         selected_port_tview = tview;
456                                 } else {
457                                         if (port == selected_port) {
458                                                 selected_port_tview = tview;
459                                         }
460                                 }
461                         
462                         } else {
463
464                                 if (io.output_maximum() == 1) {
465                                         selected_port = port;
466                                         selected_port_tview = tview;
467                                 } else {
468                                         if (port == selected_port) {
469                                                 selected_port_tview = tview;
470                                         }
471                                 }
472                         }
473
474                         TreeViewColumn* col = tview->get_column (0);
475                         
476                         col->set_clickable (true);
477                         //set_treeview_header_as_default_label(col);
478                         /* handle button events on the column header and within the treeview itself */
479
480                         //col->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
481                         tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
482                 }
483
484                 port_box.show_all ();
485
486                 if (selected_port_tview) {
487                         // GTK2FIX
488                         // selected_port_tview->click_column(0);
489                         selected_port_tview->set_name ("IOSelectorPortListSelected");
490                         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
491                                 if (*i != selected_port_tview) {
492                                         (*i)->set_name ("IOSelectorPortList");
493                                         (*i)->queue_draw ();
494                                 }
495                         }
496                 } else {
497                         selected_port = 0;
498                         selector_box.hide_all ();
499                 }
500         }
501         
502         if (selected_port_tview) {
503                 select_treeview (selected_port_tview);
504         } else if (firsttview) {
505                 // select first
506                 select_treeview (firsttview);
507         }
508 }
509
510 void
511 IOSelector::port_selection_changed (TreeView* treeview)
512 {
513         TreeModel::iterator i = treeview->get_selection()->get_selected();
514         int status;
515
516         if (!i) {
517                 return;
518         }
519
520         if (selected_port == 0) {
521                 return;
522         }
523
524         ustring other_port_name = (*i)[port_display_columns.full_name];
525         
526         if (for_input) {
527                 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
528                         Port *p = session.engine().get_port_by_name (other_port_name);
529                         p->enable_metering();
530                 }
531         } else {
532                 status = io.connect_output (selected_port, other_port_name, this);
533         }
534
535         if (status == 0) {
536                 select_next_treeview ();
537         }
538 }
539
540 void
541 IOSelector::ports_changed (IOChange change, void *src)
542 {
543         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
544         
545         display_ports ();
546 }
547
548 void
549 IOSelector::add_port ()
550 {
551         /* add a new port, then hide the button if we're up to the maximum allowed */
552
553         if (for_input) {
554
555                 try {
556                         io.add_input_port ("", this);
557                 }
558
559                 catch (AudioEngine::PortRegistrationFailure& err) {
560                         ArdourMessage msg (0,  X_("noport dialog"), _("There are no more JACK ports available."));
561                 }
562
563                 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
564                         add_port_button.set_sensitive (false);
565                 }
566                 
567                 if (io.input_minimum() < (int) io.n_inputs()) {
568                         remove_port_button.set_sensitive (true);
569                 }
570
571         } else {
572
573                 try {
574                         io.add_output_port ("", this);
575                 }
576
577                 catch (AudioEngine::PortRegistrationFailure& err) {
578                         ArdourMessage msg (0, X_("noport dialog"),
579                                            _("There are no more JACK ports available."));
580                 }
581
582                 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
583                         add_port_button.set_sensitive (false);
584                 }
585         }
586 }
587
588 void
589 IOSelector::remove_port ()
590 {
591         uint32_t nports;
592
593         // always remove last port
594         
595         if (for_input) {
596                 if ((nports = io.n_inputs()) > 0) {
597                         io.remove_input_port (io.input(nports-1), this);
598                 }
599                 if (io.input_minimum() == (int) io.n_inputs()) {
600                         remove_port_button.set_sensitive (false);
601                 }
602         } else {
603                 if ((nports = io.n_outputs()) > 0) {
604                         io.remove_output_port (io.output(nports-1), this);
605                 }
606         }
607 }
608
609 gint
610 IOSelector::remove_port_when_idle (Port *port)
611 {
612         if (for_input) {
613                 io.remove_input_port (port, this);
614         } else {
615                 io.remove_output_port (port, this);
616         }
617
618         return FALSE;
619 }
620
621 gint
622 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
623 {
624         /* this handles button release on a port name row: i.e. a connection
625            between the named port and the port represented by the treeview.
626         */
627
628         Gtk::TreeModel::iterator iter;
629         TreeModel::Path path;
630         TreeViewColumn* column;
631         int cellx;
632         int celly;
633
634         /* only handle button1 events here */
635
636         if (ev->button != 1) {
637                 return false;
638         }
639         
640         if (!(Keyboard::is_delete_event (ev))) {
641                 //return false;
642         }
643
644         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
645                 return false;
646         }
647         cerr << "path = " << path.to_string() << endl;
648         
649         if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
650
651                 /* path is valid */
652                 ustring connected_port_name = (*iter)[port_display_columns.full_name];
653                 cerr << "selected row displayed_name: " << (*iter)[port_display_columns.displayed_name] << endl;
654                 cerr << "selected row string was " << connected_port_name << endl;
655                 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
656                 
657                 if (for_input) {
658                         Port *p = session.engine().get_port_by_name (connected_port_name);
659                         p->disable_metering();
660                         io.disconnect_input (port, connected_port_name, this);
661                 } else {
662                         io.disconnect_output (port, connected_port_name, this);
663                 }
664         }
665
666         return true;
667 }
668
669 gint
670 IOSelector::port_column_button_release (GdkEventButton* event, TreeView* treeview)
671 {
672         /* this handles button release on the button at the top of a single-column
673            treeview (representing a port)
674         */
675         cerr << "IOSelector::port_column_button_release() called" << endl;
676         
677         if (Keyboard::is_delete_event (event)) {
678                 Port* port;
679                 {
680                         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
681                         
682                         port = static_cast<Port *> (treeview->get_data (_("port")));
683                         
684                         if (port == selected_port) {
685                                 selected_port = 0;
686                                 treeview->set_name ("IOSelectorPortList");
687                                 treeview->queue_draw();
688                         }
689                 }
690
691                 /* remove the port when idle - if we do it here, we will destroy the widget
692                    for whom we are handling an event. not good.
693                 */
694
695                 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
696
697         } else {
698                 select_treeview (treeview);
699         }
700
701         return TRUE;
702 }
703
704 void
705 IOSelector::select_next_treeview ()
706 {
707         slist<TreeView*>::iterator next;
708
709         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
710
711                 if ((*i)->get_name() == "IOSelectorPortListSelected") {
712
713                         ++i;
714
715                         if (i == port_displays.end()) {
716                                 select_treeview (port_displays.front());
717                         } else {
718                                 select_treeview (*i);
719                         }
720                         
721                         break;
722                 }
723         }
724 }
725
726 void
727 IOSelector::select_treeview (TreeView* tview)
728 {
729         /* Gack. TreeView's don't respond visually to a change
730            in their state, so rename them to force a style
731            switch.
732         */
733
734         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
735         Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
736         
737         if (port != selected_port) {
738                 selected_port = port;
739                 
740                 tview->set_name ("IOSelectorPortListSelected");
741                 
742                 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
743                         if (*i != tview) {
744                                 (*i)->set_name ("IOSelectorPortList");
745                                 (*i)->queue_draw ();
746                         }
747                 }
748                 selector_box.show_all ();
749         }
750 }
751
752 void
753 IOSelector::redisplay ()
754 {
755         display_ports ();
756
757         if (for_input) {
758                 if (io.input_maximum() != 0) {
759                         rescan ();
760                 }
761         } else {
762                 if (io.output_maximum() != 0) {
763                         rescan();
764                 }
765         }
766 }
767
768 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
769         : input_selector (sess, pi, true),
770           output_selector (sess, pi, false)
771 {
772         hbox.pack_start (output_selector, true, true);
773         hbox.pack_start (input_selector, true, true);
774
775
776         pack_start (hbox);
777 }
778
779 void
780 PortInsertUI::redisplay()
781 {
782
783         input_selector.redisplay();
784         output_selector.redisplay();
785 }
786
787 void
788 PortInsertUI::finished(IOSelector::Result r)
789 {
790         input_selector.Finished (r);
791         output_selector.Finished (r);
792 }
793
794
795 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
796         : ArdourDialog ("port insert dialog"),
797           _portinsertui(sess, pi),
798           ok_button (can_cancel ? _("OK"): _("Close")),
799           cancel_button (_("Cancel")),
800           rescan_button (_("Rescan"))
801 {
802
803         set_name ("IOSelectorWindow");
804         string title = _("ardour: ");
805         title += pi.name();
806         set_title (title);
807         
808         ok_button.set_name ("IOSelectorButton");
809         cancel_button.set_name ("IOSelectorButton");
810         rescan_button.set_name ("IOSelectorButton");
811
812         button_box.set_spacing (5);
813         button_box.set_border_width (5);
814         button_box.set_homogeneous (true);
815         button_box.pack_start (rescan_button);
816         if (can_cancel) {
817                 button_box.pack_start (cancel_button);
818         }
819         else {
820                 cancel_button.hide();
821         }
822         button_box.pack_start (ok_button);
823
824         get_vbox()->pack_start (_portinsertui);
825         get_vbox()->pack_start (button_box, false, false);
826
827         ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
828         cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
829         rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
830
831         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
832         pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
833 }
834
835 void
836 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
837 {
838         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
839         
840         delete_when_idle (this);
841 }
842
843 void
844 PortInsertWindow::on_map ()
845 {
846         _portinsertui.redisplay ();
847         Window::on_map ();
848 }
849
850
851 void
852 PortInsertWindow::rescan ()
853 {
854         _portinsertui.redisplay();
855 }
856
857 void
858 PortInsertWindow::cancel ()
859 {
860         _portinsertui.finished(IOSelector::Cancelled);
861         hide ();
862 }
863
864 void
865 PortInsertWindow::accept ()
866 {
867         _portinsertui.finished(IOSelector::Accepted);
868         hide ();
869 }