Merged with trunk R1141
[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 <sigc++/bind.h>
25
26 #include <gtkmm/messagedialog.h>
27
28 #include <glibmm/thread.h>
29
30 #include <ardour/io.h>
31 #include <ardour/route.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/port.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 Input") : _("Add Output")),
146           remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
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
188         // The IO selector only works for single typed IOs
189         const ARDOUR::DataType t = io->default_type();
190
191         if (for_input) {
192                 if (io->input_maximum().get(t) < 0 || io->input_maximum().get(t) > (size_t) io->n_inputs().get(t)) {
193                         add_port_button.set_sensitive (true);
194                 } else {
195                         add_port_button.set_sensitive (false);
196                 }
197
198         } else {
199                 if (io->output_maximum().get(t) < 0 || io->output_maximum().get(t) > (size_t) io->n_outputs().get(t)) {
200                         add_port_button.set_sensitive (true);
201                 } else {
202                         add_port_button.set_sensitive (false);
203                 }
204                         
205         }
206
207         port_button_box.pack_start (remove_port_button, false, false);
208
209         if (for_input) {
210                 if (io->input_minimum().get(t) < 0 || io->input_minimum().get(t) < (size_t) io->n_inputs().get(t)) {
211                         remove_port_button.set_sensitive (true);
212                 } else {
213                         remove_port_button.set_sensitive (false);
214                 }
215                         
216         } else {
217                 if (io->output_minimum().get(t) < 0 || io->output_minimum().get(t) < (size_t) io->n_outputs().get(t)) {
218                         remove_port_button.set_sensitive (true);
219                 } else {
220                         remove_port_button.set_sensitive (false);
221                 }
222         }
223
224         port_button_box.pack_start (clear_connections_button, false, false);
225
226         port_and_button_box.set_border_width (5);
227         port_and_button_box.pack_start (port_button_box, false, false);
228         port_and_button_box.pack_start (port_display_scroller);
229
230         port_frame.add (port_and_button_box);
231
232         port_and_selector_box.set_spacing (5);
233         port_and_selector_box.pack_start (port_frame);
234         port_and_selector_box.pack_start (selector_frame);
235
236         set_spacing (5);
237         set_border_width (5);
238         pack_start (port_and_selector_box);
239
240         rescan();
241         display_ports ();
242
243         clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
244
245         add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
246         remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
247
248         if (for_input) {
249                 io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
250         } else {
251                 io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
252         }
253
254         io->name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
255 }
256
257 IOSelector::~IOSelector ()
258 {
259 }
260
261 void 
262 IOSelector::set_button_sensitivity ()
263 {
264         DataType t = io->default_type();
265
266         if (for_input) {
267
268                 if (io->input_maximum().get(t) < 0 || io->input_maximum().get(t) > io->n_inputs().get(t)) {
269                         add_port_button.set_sensitive (true);
270                 } else {
271                         add_port_button.set_sensitive (false);
272                 }
273
274         } else {
275
276                 if (io->output_maximum().get(t) < 0 || io->output_maximum().get(t) > io->n_outputs().get(t)) {
277                         add_port_button.set_sensitive (true);
278                 } else {
279                         add_port_button.set_sensitive (false);
280                 }
281                         
282         }
283
284         if (for_input) {
285                 if (io->n_inputs().get(t) && (io->input_minimum().get(t) < 0 || io->input_minimum().get(t) < io->n_inputs().get(t))) {
286                         remove_port_button.set_sensitive (true);
287                 } else {
288                         remove_port_button.set_sensitive (false);
289                 }
290                         
291         } else {
292                 if (io->n_outputs().get(t) && (io->output_minimum().get(t) < 0 || io->output_minimum().get(t) < io->n_outputs().get(t))) {
293                         remove_port_button.set_sensitive (true);
294                 } else {
295                         remove_port_button.set_sensitive (false);
296                 }
297         }
298 }
299
300
301 void
302 IOSelector::name_changed (void* src)
303 {
304         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
305         
306         display_ports ();
307 }
308
309 void
310 IOSelector::clear_connections ()
311 {
312         if (for_input) {
313                 io->disconnect_inputs (this);
314         } else {
315                 io->disconnect_outputs (this);
316         }
317 }
318
319 void
320 IOSelector::rescan ()
321 {
322         using namespace Notebook_Helpers;
323
324         typedef std::map<string,vector<pair<string,string> > > PortMap;
325         PortMap portmap;
326         const char **ports;
327         PageList& pages = notebook.pages();
328         gint current_page;
329         vector<string> rowdata;
330
331         page_selection_connection.disconnect ();
332
333         current_page = notebook.get_current_page ();
334
335         pages.clear ();
336
337         /* get relevant current JACK ports */
338
339         ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
340
341         if (ports == 0) {
342                 return;
343         }
344
345         /* find all the client names and group their ports into a list-by-client */
346         
347         for (int n = 0; ports[n]; ++n) {
348
349                 pair<string,vector<pair<string,string> > > newpair;
350                 pair<string,string> strpair;
351                 pair<PortMap::iterator,bool> result;
352
353                 string str = ports[n];
354                 string::size_type pos;
355                 string portname;
356
357                 pos = str.find (':');
358
359                 newpair.first = str.substr (0, pos);
360                 portname = str.substr (pos+1);
361
362                 result = portmap.insert (newpair);
363
364                 strpair.first = portname;
365                 strpair.second = str;
366
367                 result.first->second.push_back (strpair);
368         }
369
370         PortMap::iterator i;
371
372         for (i = portmap.begin(); i != portmap.end(); ++i) {
373                 
374                 Box *client_box = manage (new VBox);
375                 TreeView *display = manage (new TreeView);
376                 RefPtr<ListStore> model = ListStore::create (port_display_columns);
377                 ScrolledWindow *scroller = manage (new ScrolledWindow);
378
379                 display->set_model (model);
380                 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
381                 display->set_headers_visible (false);
382                 display->get_selection()->set_mode (SELECTION_SINGLE);
383                 display->set_name ("IOSelectorList");
384
385                 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
386                         
387                         TreeModel::Row row = *(model->append ());
388
389                         row[port_display_columns.displayed_name] = s->first;
390                         row[port_display_columns.full_name] = s->second;
391                 }
392
393                 display->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
394                 Label *tab_label = manage (new Label);
395
396                 tab_label->set_name ("IOSelectorNotebookTab");
397                 tab_label->set_text ((*i).first);
398
399                 scroller->add (*display);
400                 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
401
402                 client_box->pack_start (*scroller);
403
404                 pages.push_back (TabElem (*client_box, *tab_label));
405         }
406
407         notebook.set_current_page (current_page);
408         page_selection_connection = notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
409         selector_box.show_all ();
410 }       
411
412 void
413 IOSelector::display_ports ()
414 {
415         TreeView *firsttview = 0;
416         TreeView *selected_port_tview = 0;
417         {
418                 Glib::Mutex::Lock lm  (port_display_lock);
419                 Port *port;
420                 uint32_t limit;
421                 
422                 // The IO selector only works for single typed IOs
423                 const ARDOUR::DataType t = io->default_type();
424
425                 if (for_input) {
426                         limit = io->n_inputs().get(t);
427                 } else {
428                         limit = io->n_outputs().get(t);
429                 }
430                 
431                 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
432                         
433                         slist<TreeView *>::iterator tmp;
434                         
435                         tmp = i;
436                         ++tmp;
437                         
438                         port_box.remove (**i);
439                         delete *i;
440                         port_displays.erase (i);
441                         
442                         i = tmp;
443                 } 
444                 
445                 for (uint32_t n = 0; n < limit; ++n) {
446                         
447                         TreeView* tview;
448                         //ScrolledWindow *scroller;
449                         string really_short_name;
450                         
451                         if (for_input) {
452                                 port = io->input (n);
453                         } else {
454                                 port = io->output (n);
455                         }
456                         
457                         /* we know there is '/' because we put it there */
458                         
459                         really_short_name = port->short_name();
460                         really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
461                         
462                         tview = manage (new TreeView());
463                         RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
464                         
465                         if (!firsttview) {
466                                 firsttview = tview;
467                         }
468                         
469                         tview->set_model (port_model);
470                         tview->append_column (really_short_name, port_display_columns.displayed_name);
471                         tview->get_selection()->set_mode (SELECTION_SINGLE);
472                         tview->set_data (X_("port"), port);
473                         tview->set_headers_visible (true);
474                         tview->set_name (X_("IOSelectorPortList"));
475                         
476                         port_box.pack_start (*tview);
477                         port_displays.insert (port_displays.end(), tview);
478                         
479                         /* now fill the clist with the current connections */
480                         
481                         
482                         const char **connections = port->get_connections ();
483                         
484                         if (connections) {
485                                 for (uint32_t c = 0; connections[c]; ++c) {
486                                         TreeModel::Row row = *(port_model->append());
487                                         row[port_display_columns.displayed_name] = connections[c];
488                                         row[port_display_columns.full_name] = connections[c];
489                                 }
490                         }
491                         
492                         if (for_input) {
493                                 
494                                 if (io->input_maximum().get(io->default_type()) == 1) {
495                                         selected_port = port;
496                                         selected_port_tview = tview;
497                                 } else {
498                                         if (port == selected_port) {
499                                                 selected_port_tview = tview;
500                                         }
501                                 }
502                                 
503                         } else {
504                                 
505                                 if (io->output_maximum().get(t) == 1) {
506                                         selected_port = port;
507                                         selected_port_tview = tview;
508                                 } else {
509                                         if (port == selected_port) {
510                                                 selected_port_tview = tview;
511                                         }
512                                 }
513                         }
514                         
515                         TreeViewColumn* col = tview->get_column (0);
516                         
517                         col->set_clickable (true);
518                         
519                         /* handle button events on the column header ... */
520                         col->signal_clicked().connect (bind (mem_fun(*this, &IOSelector::select_treeview), tview));
521
522                         /* ... and within the treeview itself */
523                         tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
524                 }
525                 
526                 port_box.show_all ();
527         }
528         
529         if (!selected_port_tview) {
530                 selected_port_tview = firsttview;
531         }
532
533         if (selected_port_tview) {
534                 select_treeview (selected_port_tview);
535         }
536 }
537
538 bool
539 IOSelector::port_selection_changed (GdkEventButton *ev, TreeView* treeview)
540 {
541         TreeModel::iterator i = treeview->get_selection()->get_selected();
542         int status;
543
544         if (!i) {
545                 return 0;
546         }
547
548         if (selected_port == 0) {
549                 return 0;
550         }
551
552         ustring other_port_name = (*i)[port_display_columns.full_name];
553         
554         if (for_input) {
555                 if ((status = io->connect_input (selected_port, other_port_name, this)) == 0) {
556                         Port *p = session.engine().get_port_by_name (other_port_name);
557                         if (p) {
558                                 p->enable_metering();
559                         }
560                 }
561         } else {
562                 status = io->connect_output (selected_port, other_port_name, this);
563         }
564
565         if (status == 0) {
566                 select_next_treeview ();
567         }
568         
569         treeview->get_selection()->unselect_all();
570         return 0;
571 }
572
573 void
574 IOSelector::ports_changed (IOChange change, void *src)
575 {
576         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
577         
578         display_ports ();
579 }
580
581 void
582 IOSelector::add_port ()
583 {
584         /* add a new port, then hide the button if we're up to the maximum allowed */
585
586         // The IO selector only works for single typed IOs
587         const ARDOUR::DataType t = io->default_type();
588
589         if (for_input) {
590
591                 try {
592                         io->add_input_port ("", this);
593                 }
594
595                 catch (AudioEngine::PortRegistrationFailure& err) {
596                         MessageDialog msg (0,  _("There are no more JACK ports available."));
597                         msg.run ();
598                 }
599
600         } else {
601
602                 try {
603                         io->add_output_port ("", this);
604                 }
605
606                 catch (AudioEngine::PortRegistrationFailure& err) {
607                         MessageDialog msg (0, _("There are no more JACK ports available."));
608                         msg.run ();
609                 }
610         }
611                 
612         set_button_sensitivity ();
613 }
614
615 void
616 IOSelector::remove_port ()
617 {
618         uint32_t nports;
619
620         // The IO selector only works for single typed IOs
621         const ARDOUR::DataType t = io->default_type();
622         
623         // always remove last port
624         
625         if (for_input) {
626                 if ((nports = io->n_inputs().get(t)) > 0) {
627                         io->remove_input_port (io->input(nports-1), this);
628                 }
629         } else {
630                 if ((nports = io->n_outputs().get(t)) > 0) {
631                         io->remove_output_port (io->output(nports-1), this);
632                 }
633         }
634         
635         set_button_sensitivity ();
636 }
637
638 gint
639 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
640 {
641         /* this handles button release on a port name row: i.e. a connection
642            between the named port and the port represented by the treeview.
643         */
644
645         Gtk::TreeModel::iterator iter;
646         TreeModel::Path path;
647         TreeViewColumn* column;
648         int cellx;
649         int celly;
650
651         /* only handle button1 events here */
652
653         if (ev->button != 1) {
654                 return false;
655         }
656         
657         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
658                 return false;
659         }
660
661         if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
662
663                 /* path is valid */
664                 ustring connected_port_name = (*iter)[port_display_columns.full_name];
665                 Port *port = reinterpret_cast<Port *> (treeview->get_data (X_("port")));
666                 
667                 if (for_input) {
668                         Port *p = session.engine().get_port_by_name (connected_port_name);
669                         if (p) {
670                                 p->disable_metering();
671                         }
672                         io->disconnect_input (port, connected_port_name, this);
673                 } else {
674                         io->disconnect_output (port, connected_port_name, this);
675                 }
676         }
677
678         return true;
679 }
680
681 void
682 IOSelector::select_next_treeview ()
683 {
684         slist<TreeView*>::iterator next;
685
686         if (port_displays.empty() || port_displays.size() == 1) {
687                 return;
688         }
689
690         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
691
692                 if ((*i)->get_name() == "IOSelectorPortListSelected") {
693
694                         ++i;
695
696                         if (i == port_displays.end()) {
697                                 select_treeview (port_displays.front());
698                         } else {
699                                 select_treeview (*i);
700                         }
701                         
702                         break;
703                 }
704         }
705 }
706
707 void
708 IOSelector::select_treeview (TreeView* tview)
709 {
710         /* Gack. TreeView's don't respond visually to a change
711            in their state, so rename them to force a style
712            switch.
713         */
714
715         Glib::Mutex::Lock lm  (port_display_lock);
716         Port* port = reinterpret_cast<Port *> (tview->get_data (X_("port")));
717
718         selected_port = port;
719
720         tview->set_name ("IOSelectorPortListSelected");
721         tview->queue_draw ();
722
723         /* ugly hack to force the column header button to change as well */
724
725         TreeViewColumn* col = tview->get_column (0);
726         GtkTreeViewColumn* ccol = col->gobj();
727         
728         if (ccol->button) {
729                 gtk_widget_set_name (ccol->button, "IOSelectorPortListSelected");       
730                 gtk_widget_queue_draw (ccol->button);
731         }
732
733         for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
734                 if (*i == tview) {
735                         continue;
736                 }
737                 
738                 col = (*i)->get_column (0);
739                 ccol = col->gobj();
740                 
741                 if (ccol->button) {
742                         gtk_widget_set_name (ccol->button, "IOSelectorPortList");
743                         gtk_widget_queue_draw (ccol->button);
744                 }
745                 
746                 (*i)->set_name ("IOSelectorPortList");
747                 (*i)->queue_draw ();
748         }
749
750         selector_box.show_all ();
751 }
752
753 void
754 IOSelector::redisplay ()
755 {
756         display_ports ();
757
758         if (for_input) {
759                 if (io->input_maximum().get(io->default_type()) != 0) {
760                         rescan ();
761                 }
762         } else {
763                 if (io->output_maximum().get(io->default_type()) != 0) {
764                         rescan();
765                 }
766         }
767 }
768
769 PortInsertUI::PortInsertUI (Session& sess, boost::shared_ptr<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, boost::shared_ptr<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         get_vbox()->pack_start (_portinsertui);
826         get_vbox()->pack_start (button_box, false, false);
827
828         ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
829         cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
830         rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
831
832         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
833         pi->GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
834 }
835
836 void
837 PortInsertWindow::plugin_going_away ()
838 {
839         ENSURE_GUI_THREAD(mem_fun(*this, &PortInsertWindow::plugin_going_away));
840         
841         delete_when_idle (this);
842 }
843
844 void
845 PortInsertWindow::on_map ()
846 {
847         _portinsertui.redisplay ();
848         Window::on_map ();
849 }
850
851
852 void
853 PortInsertWindow::rescan ()
854 {
855         _portinsertui.redisplay();
856 }
857
858 void
859 PortInsertWindow::cancel ()
860 {
861         _portinsertui.finished(IOSelector::Cancelled);
862         hide ();
863 }
864
865 void
866 PortInsertWindow::accept ()
867 {
868         _portinsertui.finished(IOSelector::Accepted);
869         hide ();
870 }