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