Merged with trunk R1283.
[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                         const char **connections = port->get_connections ();
482                         
483                         if (connections) {
484                                 for (uint32_t c = 0; connections[c]; ++c) {
485                                         TreeModel::Row row = *(port_model->append());
486                                         row[port_display_columns.displayed_name] = connections[c];
487                                         row[port_display_columns.full_name] = connections[c];
488                                 }
489                         } 
490                         
491                         if (for_input) {
492                                 
493                                 if (io->input_maximum().get(io->default_type()) == 1) {
494                                         selected_port = port;
495                                         selected_port_tview = tview;
496                                 } else {
497                                         if (port == selected_port) {
498                                                 selected_port_tview = tview;
499                                         }
500                                 }
501                                 
502                         } else {
503                                 
504                                 if (io->output_maximum().get(t) == 1) {
505                                         selected_port = port;
506                                         selected_port_tview = tview;
507                                 } else {
508                                         if (port == selected_port) {
509                                                 selected_port_tview = tview;
510                                         }
511                                 }
512                         }
513                         
514                         TreeViewColumn* col = tview->get_column (0);
515                         
516                         col->set_clickable (true);
517                         
518                         /* handle button events on the column header ... */
519                         col->signal_clicked().connect (bind (mem_fun(*this, &IOSelector::select_treeview), tview));
520
521                         /* ... and within the treeview itself */
522                         tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
523                 }
524                 
525                 port_box.show_all ();
526         }
527         
528         if (!selected_port_tview) {
529                 selected_port_tview = firsttview;
530         }
531
532         if (selected_port_tview) {
533                 select_treeview (selected_port_tview);
534         }
535 }
536
537 bool
538 IOSelector::port_selection_changed (GdkEventButton *ev, TreeView* treeview)
539 {
540         TreeModel::iterator i = treeview->get_selection()->get_selected();
541         int status;
542
543         if (!i) {
544                 return 0;
545         }
546
547         if (selected_port == 0) {
548                 return 0;
549         }
550
551         ustring other_port_name = (*i)[port_display_columns.full_name];
552         
553         if (for_input) {
554                 if ((status = io->connect_input (selected_port, other_port_name, this)) == 0) {
555                         Port *p = session.engine().get_port_by_name (other_port_name);
556                         if (p) {
557                                 p->enable_metering();
558                         }
559                 }
560         } else {
561                 status = io->connect_output (selected_port, other_port_name, this);
562         }
563
564         if (status == 0) {
565                 select_next_treeview ();
566         }
567         
568         treeview->get_selection()->unselect_all();
569         return 0;
570 }
571
572 void
573 IOSelector::ports_changed (IOChange change, void *src)
574 {
575         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
576         
577         display_ports ();
578 }
579
580 void
581 IOSelector::add_port ()
582 {
583         /* add a new port, then hide the button if we're up to the maximum allowed */
584
585         // The IO selector only works for single typed IOs
586         const ARDOUR::DataType t = io->default_type();
587
588         if (for_input) {
589
590                 try {
591                         io->add_input_port ("", this);
592                 }
593
594                 catch (AudioEngine::PortRegistrationFailure& err) {
595                         MessageDialog msg (0,  _("There are no more JACK ports available."));
596                         msg.run ();
597                 }
598
599         } else {
600
601                 try {
602                         io->add_output_port ("", this);
603                 }
604
605                 catch (AudioEngine::PortRegistrationFailure& err) {
606                         MessageDialog msg (0, _("There are no more JACK ports available."));
607                         msg.run ();
608                 }
609         }
610                 
611         set_button_sensitivity ();
612 }
613
614 void
615 IOSelector::remove_port ()
616 {
617         uint32_t nports;
618
619         // The IO selector only works for single typed IOs
620         const ARDOUR::DataType t = io->default_type();
621         
622         // always remove last port
623         
624         if (for_input) {
625                 if ((nports = io->n_inputs().get(t)) > 0) {
626                         io->remove_input_port (io->input(nports-1), this);
627                 }
628         } else {
629                 if ((nports = io->n_outputs().get(t)) > 0) {
630                         io->remove_output_port (io->output(nports-1), this);
631                 }
632         }
633         
634         set_button_sensitivity ();
635 }
636
637 gint
638 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
639 {
640         /* this handles button release on a port name row: i.e. a connection
641            between the named port and the port represented by the treeview.
642         */
643
644         Gtk::TreeModel::iterator iter;
645         TreeModel::Path path;
646         TreeViewColumn* column;
647         int cellx;
648         int celly;
649
650         /* only handle button1 events here */
651
652         if (ev->button != 1) {
653                 return false;
654         }
655         
656         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
657                 return false;
658         }
659
660         if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
661
662                 /* path is valid */
663                 ustring connected_port_name = (*iter)[port_display_columns.full_name];
664                 Port *port = reinterpret_cast<Port *> (treeview->get_data (X_("port")));
665                 
666                 if (for_input) {
667                         Port *p = session.engine().get_port_by_name (connected_port_name);
668                         if (p) {
669                                 p->disable_metering();
670                         }
671                         io->disconnect_input (port, connected_port_name, this);
672                 } else {
673                         io->disconnect_output (port, connected_port_name, this);
674                 }
675         }
676
677         return true;
678 }
679
680 void
681 IOSelector::select_next_treeview ()
682 {
683         slist<TreeView*>::iterator next;
684
685         if (port_displays.empty() || port_displays.size() == 1) {
686                 return;
687         }
688
689         for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
690
691                 if ((*i)->get_name() == "IOSelectorPortListSelected") {
692
693                         ++i;
694
695                         if (i == port_displays.end()) {
696                                 select_treeview (port_displays.front());
697                         } else {
698                                 select_treeview (*i);
699                         }
700                         
701                         break;
702                 }
703         }
704 }
705
706 void
707 IOSelector::select_treeview (TreeView* tview)
708 {
709         /* Gack. TreeView's don't respond visually to a change
710            in their state, so rename them to force a style
711            switch.
712         */
713
714         Glib::Mutex::Lock lm  (port_display_lock);
715         Port* port = reinterpret_cast<Port *> (tview->get_data (X_("port")));
716
717         selected_port = port;
718
719         tview->set_name ("IOSelectorPortListSelected");
720         tview->queue_draw ();
721
722         /* ugly hack to force the column header button to change as well */
723
724         TreeViewColumn* col = tview->get_column (0);
725         GtkTreeViewColumn* ccol = col->gobj();
726         
727         if (ccol->button) {
728                 gtk_widget_set_name (ccol->button, "IOSelectorPortListSelected");       
729                 gtk_widget_queue_draw (ccol->button);
730         }
731
732         for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
733                 if (*i == tview) {
734                         continue;
735                 }
736                 
737                 col = (*i)->get_column (0);
738                 ccol = col->gobj();
739                 
740                 if (ccol->button) {
741                         gtk_widget_set_name (ccol->button, "IOSelectorPortList");
742                         gtk_widget_queue_draw (ccol->button);
743                 }
744                 
745                 (*i)->set_name ("IOSelectorPortList");
746                 (*i)->queue_draw ();
747         }
748
749         selector_box.show_all ();
750 }
751
752 void
753 IOSelector::redisplay ()
754 {
755         display_ports ();
756
757         if (for_input) {
758                 if (io->input_maximum().get(io->default_type()) != 0) {
759                         rescan ();
760                 }
761         } else {
762                 if (io->output_maximum().get(io->default_type()) != 0) {
763                         rescan();
764                 }
765         }
766 }
767
768 PortInsertUI::PortInsertUI (Session& sess, boost::shared_ptr<PortInsert> pi)
769         : input_selector (sess, pi, true),
770           output_selector (sess, pi, false)
771 {
772         hbox.pack_start (output_selector, true, true);
773         hbox.pack_start (input_selector, true, true);
774
775
776         pack_start (hbox);
777 }
778
779 void
780 PortInsertUI::redisplay()
781 {
782
783         input_selector.redisplay();
784         output_selector.redisplay();
785 }
786
787 void
788 PortInsertUI::finished(IOSelector::Result r)
789 {
790         input_selector.Finished (r);
791         output_selector.Finished (r);
792 }
793
794
795 PortInsertWindow::PortInsertWindow (Session& sess, boost::shared_ptr<PortInsert> pi, bool can_cancel)
796         : ArdourDialog ("port insert dialog"),
797           _portinsertui (sess, pi),
798           ok_button (can_cancel ? _("OK"): _("Close")),
799           cancel_button (_("Cancel")),
800           rescan_button (_("Rescan"))
801 {
802
803         set_name ("IOSelectorWindow");
804         string title = _("ardour: ");
805         title += pi->name();
806         set_title (title);
807         
808         ok_button.set_name ("IOSelectorButton");
809         cancel_button.set_name ("IOSelectorButton");
810         rescan_button.set_name ("IOSelectorButton");
811
812         button_box.set_spacing (5);
813         button_box.set_border_width (5);
814         button_box.set_homogeneous (true);
815         button_box.pack_start (rescan_button);
816         if (can_cancel) {
817                 button_box.pack_start (cancel_button);
818         }
819         else {
820                 cancel_button.hide();
821         }
822         button_box.pack_start (ok_button);
823
824         get_vbox()->pack_start (_portinsertui);
825         get_vbox()->pack_start (button_box, false, false);
826
827         ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
828         cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
829         rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
830
831         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
832         pi->GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
833 }
834
835 void
836 PortInsertWindow::plugin_going_away ()
837 {
838         ENSURE_GUI_THREAD(mem_fun(*this, &PortInsertWindow::plugin_going_away));
839         
840         delete_when_idle (this);
841 }
842
843 void
844 PortInsertWindow::on_map ()
845 {
846         _portinsertui.redisplay ();
847         Window::on_map ();
848 }
849
850
851 void
852 PortInsertWindow::rescan ()
853 {
854         _portinsertui.redisplay();
855 }
856
857 void
858 PortInsertWindow::cancel ()
859 {
860         _portinsertui.finished(IOSelector::Cancelled);
861         hide ();
862 }
863
864 void
865 PortInsertWindow::accept ()
866 {
867         _portinsertui.finished(IOSelector::Accepted);
868         hide ();
869 }