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