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