segfault fixes and some functionality for the IOSelector
[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         /* get relevant current JACK ports */
294
295         ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
296
297         if (ports == 0) {
298                 return;
299         }
300
301         /* find all the client names and group their ports into a list-by-client */
302         
303         for (int n = 0; ports[n]; ++n) {
304
305                 pair<string,vector<pair<string,string> > > newpair;
306                 pair<string,string> strpair;
307                 pair<PortMap::iterator,bool> result;
308
309                 string str = ports[n];
310                 string::size_type pos;
311                 string portname;
312
313                 pos = str.find (':');
314
315                 newpair.first = str.substr (0, pos); 
316                 portname = str.substr (pos+1);
317
318                 result = portmap.insert (newpair);
319
320                 strpair.first = portname;
321                 strpair.second = str;
322
323                 result.first->second.push_back (strpair);
324         }
325
326         PortMap::iterator i;
327
328         for (i = portmap.begin(); i != portmap.end(); ++i) {
329                 
330                 Box *client_box = manage (new VBox);
331                 TreeView *display = manage (new TreeView);
332                 RefPtr<ListStore> model = ListStore::create (port_display_columns);
333                 ScrolledWindow *scroller = manage (new ScrolledWindow);
334
335                 display->set_model (model);
336                 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
337                 display->set_headers_visible (false);
338                 display->get_selection()->set_mode (SELECTION_SINGLE);
339                 display->set_name ("IOSelectorList");
340
341                 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
342                         
343                         TreeModel::Row row = *(model->append ());
344
345                         row[port_display_columns.displayed_name] = s->first;
346                         row[port_display_columns.full_name] = s->second;
347                 }
348
349                 display->get_selection()->signal_changed().connect 
350                         (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
351                 
352                 Label *tab_label = manage (new Label);
353
354                 tab_label->set_name ("IOSelectorNotebookTab");
355                 tab_label->set_text ((*i).first);
356
357                 scroller->add (*display);
358                 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
359
360                 client_box->pack_start (*scroller);
361
362                 pages.push_back (TabElem (*client_box, *tab_label));
363         }
364
365         notebook.set_current_page (current_page);
366         notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
367         selector_box.show_all ();
368 }       
369
370 void
371 IOSelector::display_ports ()
372 {
373         TreeView *firsttview = 0;
374         TreeView *selected_port_tview = 0;
375
376         {
377                 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
378                 Port *port;
379                 uint32_t limit;
380
381                 if (for_input) {
382                         limit = io.n_inputs();
383                 } else {
384                         limit = io.n_outputs();
385                 }
386
387                 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
388                 
389                         slist<TreeView *>::iterator tmp;
390
391                         tmp = i;
392                         ++tmp;
393
394                         port_box.remove (**i);
395                         delete *i;
396                         port_displays.erase (i);
397
398                         i = tmp;
399                 } 
400
401
402                 for (uint32_t n = 0; n < limit; ++n) {
403                         
404                         TreeView* tview;
405                         ScrolledWindow *scroller;
406                         string really_short_name;
407                         
408                         if (for_input) {
409                                 port = io.input (n);
410                         } else {
411                                 port = io.output (n);
412                         }
413                         
414                         /* we know there is '/' because we put it there */
415
416                         really_short_name = port->short_name();
417                         really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
418
419                         tview = manage (new TreeView());
420                         RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
421
422                         if (!firsttview) {
423                                 firsttview = tview;
424                         }
425                         
426                         tview->set_model (port_model);
427                         tview->append_column (really_short_name, port_display_columns.displayed_name);
428                         tview->get_selection()->set_mode (SELECTION_SINGLE);
429                         tview->set_data ("port", port);
430                         tview->set_headers_visible (true);
431                         tview->set_name ("IOSelectorPortList");
432                         
433                         scroller = manage (new ScrolledWindow);
434                         
435                         scroller->add (*tview);
436                         scroller->set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
437                         
438                         port_displays.insert (port_displays.end(), tview);
439                         port_box.pack_start (*scroller);
440                         
441                         scroller->set_size_request (-1, 75);
442                         
443                         /* now fill the clist with the current connections */
444                         
445
446                         const char **connections = port->get_connections ();
447
448                         if (connections) {
449                                 for (uint32_t c = 0; connections[c]; ++c) {
450                                         TreeModel::Row row = *(port_model->append());
451                                         row[port_display_columns.displayed_name] = connections[c];
452                                 }
453                         }
454                         
455                         if (for_input) {
456                                 
457                                 if (io.input_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                         } else {
467
468                                 if (io.output_maximum() == 1) {
469                                         selected_port = port;
470                                         selected_port_tview = tview;
471                                 } else {
472                                         if (port == selected_port) {
473                                                 selected_port_tview = tview;
474                                         }
475                                 }
476                         }
477
478                         TreeViewColumn* col = tview->get_column (0);
479                         
480                         col->set_clickable (true);
481
482                         /* handle button events on the column header and within the treeview itself */
483
484                         //col->get_widget()->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
485                         tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));            
486                 }
487
488                 port_box.show_all ();
489
490                 if (selected_port_tview) {
491                         // GTK2FIX
492                         // selected_port_tview->click_column(0);
493                         selected_port_tview->set_name ("IOSelectorPortListSelected");
494                         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
495                                 if (*i != selected_port_tview) {
496                                         (*i)->set_name ("IOSelectorPortList");
497                                         (*i)->queue_draw ();
498                                 }
499                         }
500                 } else {
501                         selected_port = 0;
502                         selector_box.hide_all ();
503                 }
504         }
505         
506         if (selected_port_tview) {
507                 select_treeview (selected_port_tview);
508         } else if (firsttview) {
509                 // select first
510                 select_treeview (firsttview);
511         }
512 }
513
514 void
515 IOSelector::port_selection_changed (TreeView* treeview)
516 {
517         TreeModel::iterator i = treeview->get_selection()->get_selected();
518         int status;
519
520         if (!i) {
521                 return;
522         }
523
524         if (selected_port == 0) {
525                 return;
526         }
527
528         ustring other_port_name = (*i)[port_display_columns.full_name];
529         
530         if (for_input) {
531                 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
532                         Port *p = session.engine().get_port_by_name (other_port_name);
533                         p->enable_metering();
534                 }
535         } else {
536                 status = io.connect_output (selected_port, other_port_name, this);
537         }
538
539         if (status == 0) {
540                 select_next_treeview ();
541         }
542 }
543
544 void
545 IOSelector::ports_changed (IOChange change, void *src)
546 {
547         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
548         
549         display_ports ();
550 }
551
552 void
553 IOSelector::add_port ()
554 {
555         /* add a new port, then hide the button if we're up to the maximum allowed */
556
557         if (for_input) {
558
559                 try {
560                         io.add_input_port ("", this);
561                 }
562
563                 catch (AudioEngine::PortRegistrationFailure& err) {
564                         ArdourMessage msg (0,  X_("noport dialog"), _("There are no more JACK ports available."));
565                 }
566
567                 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
568                         add_port_button.set_sensitive (false);
569                 }
570                 
571                 if (io.input_minimum() < (int) io.n_inputs()) {
572                         remove_port_button.set_sensitive (true);
573                 }
574
575         } else {
576
577                 try {
578                         io.add_output_port ("", this);
579                 }
580
581                 catch (AudioEngine::PortRegistrationFailure& err) {
582                         ArdourMessage msg (0, X_("noport dialog"),
583                                            _("There are no more JACK ports available."));
584                 }
585
586                 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
587                         add_port_button.set_sensitive (false);
588                 }
589         }
590 }
591
592 void
593 IOSelector::remove_port ()
594 {
595         uint32_t nports;
596
597         // always remove last port
598         
599         if (for_input) {
600                 if ((nports = io.n_inputs()) > 0) {
601                         io.remove_input_port (io.input(nports-1), this);
602                 }
603                 if (io.input_minimum() == (int) io.n_inputs()) {
604                         remove_port_button.set_sensitive (false);
605                 }
606         } else {
607                 if ((nports = io.n_outputs()) > 0) {
608                         io.remove_output_port (io.output(nports-1), this);
609                 }
610         }
611 }
612
613 gint
614 IOSelector::remove_port_when_idle (Port *port)
615 {
616         if (for_input) {
617                 io.remove_input_port (port, this);
618         } else {
619                 io.remove_output_port (port, this);
620         }
621
622         return FALSE;
623 }
624
625 gint
626 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
627 {
628         /* this handles button release on a port name row: i.e. a connection
629            between the named port and the port represented by the treeview.
630         */
631
632         TreeIter iter;
633         TreeModel::Path path;
634         TreeViewColumn* column;
635         int cellx;
636         int celly;
637
638         /* only handle button1 events here */
639
640         if (ev->button != 1) {
641                 return false;
642         }
643
644         if (!(Keyboard::is_delete_event (ev))) {
645                 return false;
646         }
647
648         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
649                 return false;
650         }
651
652         if ((iter = treeview->get_model()->get_iter (path))) {
653
654                 /* path is valid */
655                 
656                 ustring connected_port_name = (*iter)[port_display_columns.full_name];
657                 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
658                 
659                 if (for_input) {
660                         Port *p = session.engine().get_port_by_name (connected_port_name);
661                         p->disable_metering();
662                         io.disconnect_input (port, connected_port_name, this);
663                 } else {
664                         io.disconnect_output (port, connected_port_name, this);
665                 }
666         }
667
668         return true;
669 }
670
671 gint
672 IOSelector::port_column_button_release (GdkEventButton *event, TreeView* treeview)
673 {
674         /* this handles button release on the button at the top of a single-column
675            treeview (representing a port)
676         */
677
678         if (Keyboard::is_delete_event (event)) {
679                 Port* port;
680                 {
681                         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
682                         
683                         port = static_cast<Port *> (treeview->get_data (_("port")));
684                         
685                         if (port == selected_port) {
686                                 selected_port = 0;
687                                 treeview->set_name ("IOSelectorPortList");
688                                 treeview->queue_draw();
689                         }
690                 }
691
692                 /* remove the port when idle - if we do it here, we will destroy the widget
693                    for whom we are handling an event. not good.
694                 */
695
696                 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
697
698         } else {
699                 select_treeview (treeview);
700         }
701
702         return TRUE;
703 }
704
705 void
706 IOSelector::select_next_treeview ()
707 {
708         slist<TreeView*>::iterator next;
709
710         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
711
712                 if ((*i)->get_name() == "IOSelectorPortListSelected") {
713
714                         ++i;
715
716                         if (i == port_displays.end()) {
717                                 select_treeview (port_displays.front());
718                         } else {
719                                 select_treeview (*i);
720                         }
721                         
722                         break;
723                 }
724         }
725 }
726
727 void
728 IOSelector::select_treeview (TreeView* tview)
729 {
730         /* Gack. TreeView's don't respond visually to a change
731            in their state, so rename them to force a style
732            switch.
733         */
734
735         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
736         Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
737         
738         if (port != selected_port) {
739                 selected_port = port;
740                 
741                 tview->set_name ("IOSelectorPortListSelected");
742                 
743                 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
744                         if (*i != tview) {
745                                 (*i)->set_name ("IOSelectorPortList");
746                                 (*i)->queue_draw ();
747                         }
748                 }
749                 selector_box.show_all ();
750         }
751 }
752
753 void
754 IOSelector::redisplay ()
755 {
756         display_ports ();
757
758         if (for_input) {
759                 if (io.input_maximum() != 0) {
760                         rescan ();
761                 }
762         } else {
763                 if (io.output_maximum() != 0) {
764                         rescan();
765                 }
766         }
767 }
768
769 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
770         : input_selector (sess, pi, true),
771           output_selector (sess, pi, false)
772 {
773         hbox.pack_start (output_selector, true, true);
774         hbox.pack_start (input_selector, true, true);
775
776
777         pack_start (hbox);
778 }
779
780 void
781 PortInsertUI::redisplay()
782 {
783
784         input_selector.redisplay();
785         output_selector.redisplay();
786 }
787
788 void
789 PortInsertUI::finished(IOSelector::Result r)
790 {
791         input_selector.Finished (r);
792         output_selector.Finished (r);
793 }
794
795
796 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
797         : ArdourDialog ("port insert dialog"),
798           _portinsertui(sess, pi),
799           ok_button (can_cancel ? _("OK"): _("Close")),
800           cancel_button (_("Cancel")),
801           rescan_button (_("Rescan"))
802 {
803
804         set_name ("IOSelectorWindow");
805         string title = _("ardour: ");
806         title += pi.name();
807         set_title (title);
808         
809         ok_button.set_name ("IOSelectorButton");
810         cancel_button.set_name ("IOSelectorButton");
811         rescan_button.set_name ("IOSelectorButton");
812
813         button_box.set_spacing (5);
814         button_box.set_border_width (5);
815         button_box.set_homogeneous (true);
816         button_box.pack_start (rescan_button);
817         if (can_cancel) {
818                 button_box.pack_start (cancel_button);
819         }
820         else {
821                 cancel_button.hide();
822         }
823         button_box.pack_start (ok_button);
824
825         vbox.pack_start (_portinsertui);
826         vbox.pack_start (button_box, false, false);
827
828         add (vbox);
829
830         ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
831         cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
832         rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
833
834         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
835         pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
836 }
837
838 void
839 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
840 {
841         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
842         
843         delete_when_idle (this);
844 }
845
846 void
847 PortInsertWindow::on_map ()
848 {
849         _portinsertui.redisplay ();
850         Window::on_map ();
851 }
852
853
854 void
855 PortInsertWindow::rescan ()
856 {
857         _portinsertui.redisplay();
858 }
859
860 void
861 PortInsertWindow::cancel ()
862 {
863         _portinsertui.finished(IOSelector::Cancelled);
864         hide ();
865 }
866
867 void
868 PortInsertWindow::accept ()
869 {
870         _portinsertui.finished(IOSelector::Accepted);
871         hide ();
872 }