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