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