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