more fixes for gobject stuff plus doug's patch for redirect box menus, minus ardour...
[ardour.git] / gtk2_ardour / redirect_box.cc
1 /*
2     Copyright (C) 2000-2004 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 <cmath>
22 #include <glib.h>
23
24 #include <sigc++/bind.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27 #include <gtkmm2ext/utils.h>
28 #include <gtkmm2ext/choice.h>
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/stop_signal.h>
31 #include <gtkmm2ext/doi.h>
32
33 #include <ardour/ardour.h>
34 #include <ardour/session.h>
35 #include <ardour/audioengine.h>
36 #include <ardour/route.h>
37 #include <ardour/audio_track.h>
38 #include <ardour/diskstream.h>
39 #include <ardour/send.h>
40 #include <ardour/insert.h>
41 #include <ardour/ladspa_plugin.h>
42 #include <ardour/connection.h>
43 #include <ardour/session_connection.h>
44
45 #include "ardour_ui.h"
46 #include "ardour_dialog.h"
47 #include "ardour_message.h"
48 #include "public_editor.h"
49 #include "redirect_box.h"
50 #include "keyboard.h"
51 #include "plugin_selector.h"
52 #include "route_redirect_selection.h"
53 #include "mixer_ui.h"
54 #include "actions.h"
55
56 #include "plugin_ui.h"
57 #include "send_ui.h"
58 #include "io_selector.h"
59 #include "utils.h"
60 #include "gui_thread.h"
61
62 #include "i18n.h"
63
64 using namespace sigc;
65 using namespace ARDOUR;
66 using namespace Gtk;
67 using namespace Gtkmm2ext;
68
69
70
71 RedirectBox::RedirectBox (Placement pcmnt, Session& sess, Route& rt, PluginSelector &plugsel, RouteRedirectSelection & rsel, bool owner_is_mixer)
72         : _route(rt), 
73           _session(sess), 
74           _owner_is_mixer (owner_is_mixer), 
75           _placement(pcmnt), 
76           _plugin_selector(plugsel),
77           _rr_selection(rsel)
78           //redirect_display (1)
79 {
80         _width = Wide;
81         redirect_menu = 0;
82         send_action_menu = 0;
83         redirect_drag_in_progress = false;
84         
85         model = ListStore::create(columns);
86         selection = redirect_display.get_selection();
87         selection->set_mode (Gtk::SELECTION_MULTIPLE);
88         redirect_display.set_model (model);
89         redirect_display.append_column (NULL, columns.text);
90         redirect_display.set_name ("MixerRedirectSelector");
91         redirect_display.set_headers_visible (false);
92         redirect_display.set_reorderable (true);
93         redirect_display.set_size_request (-1, 48);
94         redirect_display.add_object_drag (columns.redirect.index(), "redirects");
95         redirect_display.signal_object_drop.connect (mem_fun (*this, &RedirectBox::object_drop));
96
97         // Does this adequately replace the drag start/stop signal handlers?
98         model->signal_rows_reordered().connect (mem_fun (*this, &RedirectBox::redirects_reordered));
99         redirect_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
100
101         redirect_scroller.add (redirect_display);
102         redirect_eventbox.add (redirect_scroller);
103         pack_start (redirect_eventbox, true, true);
104
105         redirect_scroller.show ();
106         redirect_display.show ();
107         redirect_eventbox.show ();
108         show_all ();
109
110         _route.redirects_changed.connect (mem_fun(*this, &RedirectBox::redirects_changed));
111
112         redirect_display.signal_button_press_event().connect (mem_fun(*this, &RedirectBox::redirect_button));
113         redirect_display.signal_button_release_event().connect (mem_fun(*this, &RedirectBox::redirect_button));
114
115         //redirect_display.signal_button_release_event().connect_after (ptr_fun (do_not_propagate));
116         //_plugin_selector.hide.connect(mem_fun(*this,&RedirectBox::disconnect_newplug));
117         set_stuff_from_route ();
118
119         /* start off as a passthru strip. we'll correct this, if necessary,
120            in update_diskstream_display().
121         */
122
123         //set_name ("AudioTrackStripBase");
124
125         /* now force an update of all the various elements */
126
127         redirects_changed (0);
128
129         //add_events (Gdk::BUTTON_RELEASE_MASK);
130 }
131
132 RedirectBox::~RedirectBox ()
133 {
134 //      GoingAway(); /* EMIT_SIGNAL */
135
136 }
137
138 void
139 RedirectBox::object_drop (string type, uint32_t cnt, void** ptr)
140 {
141         if (type != "redirects") {
142                 return;
143         }
144 }
145
146 void
147 RedirectBox::set_stuff_from_route ()
148 {
149 }
150
151 void
152 RedirectBox::set_title (const std::string & title)
153 {
154         redirect_display.get_column(0)->set_title (title);
155 }
156
157 void
158 RedirectBox::set_title_shown (bool flag)
159 {
160 }
161
162
163 void
164 RedirectBox::update()
165 {
166         redirects_changed(0);
167 }
168
169
170 void
171 RedirectBox::set_width (Width w)
172 {
173         if (_width == w) {
174                 return;
175         }
176         _width = w;
177
178         redirects_changed(0);
179 }
180
181
182 void
183 RedirectBox::remove_redirect_gui (Redirect *redirect)
184 {
185         Insert *insert = 0;
186         Send *send = 0;
187         PortInsert *port_insert = 0;
188
189         if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
190
191                 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
192                         PortInsertUI *io_selector = reinterpret_cast<PortInsertUI *> (port_insert->get_gui());
193                         port_insert->set_gui (0);
194                         delete io_selector;
195                 } 
196
197         } else if ((send = dynamic_cast<Send *> (insert)) != 0) {
198                 SendUIWindow *sui = reinterpret_cast<SendUIWindow*> (send->get_gui());
199                 send->set_gui (0);
200                 delete sui;
201         }
202 }
203
204 void 
205 RedirectBox::build_send_action_menu ()
206
207 {
208         using namespace Menu_Helpers;
209
210         send_action_menu = new Menu;
211         send_action_menu->set_name ("ArdourContextMenu");
212         MenuList& items = send_action_menu->items();
213
214         items.push_back (MenuElem (_("New send"), mem_fun(*this, &RedirectBox::new_send)));
215         items.push_back (MenuElem (_("Show send controls"), mem_fun(*this, &RedirectBox::show_send_controls)));
216 }
217
218 void
219 RedirectBox::show_send_controls ()
220
221 {
222 }
223
224 void
225 RedirectBox::new_send ()
226
227 {
228 }
229
230 void
231 RedirectBox::show_redirect_menu (gint arg)
232 {
233         if (redirect_menu == 0) {
234                 redirect_menu = build_redirect_menu ();
235         }
236
237         redirect_menu->popup (1, 0);
238 }
239
240 void
241 RedirectBox::redirect_drag_begin (GdkDragContext *context)
242 {
243         redirect_drag_in_progress = true;
244 }
245
246 void
247 RedirectBox::redirect_drag_end (GdkDragContext *context)
248 {
249         redirect_drag_in_progress = false;
250 }
251
252 gint
253 RedirectBox::redirect_button (GdkEventButton *ev)
254 {
255         Redirect *redirect;
256         TreeModel::Row row = *(redirect_display.get_selection()->get_selected());
257         redirect = row[columns.redirect];
258
259         switch (ev->type) {
260         case GDK_BUTTON_PRESS:
261                 if (ev->button == 3) {
262                         show_redirect_menu (0); // Handle the context-click menu here as well
263                         return TRUE;
264                 }
265                 else
266                         return FALSE;
267
268         case GDK_2BUTTON_PRESS:
269                 if (ev->state != 0) {
270                         return FALSE;
271                 }
272                 /* might be edit event, see below */
273                 break;
274
275         case GDK_BUTTON_RELEASE:
276                 break;
277
278         default:
279                 /* shouldn't be here, but gcc complains */
280                 return FALSE;
281         }
282
283         if (redirect && Keyboard::is_delete_event (ev)) {
284                 
285                 Glib::signal_idle().connect (bind (mem_fun(*this, &RedirectBox::idle_delete_redirect), redirect));
286                 return TRUE;
287
288         } else if (redirect && (Keyboard::is_edit_event (ev) || ev->type == GDK_2BUTTON_PRESS)) {
289                 
290                 if (_session.engine().connected()) {
291                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
292                         edit_redirect (redirect);
293                 }
294                 return TRUE;
295
296         } else if (Keyboard::is_context_menu_event (ev)) {
297                 show_redirect_menu(0);
298                 return TRUE; //stop_signal (*clist, "button-release-event");
299
300         } else {
301                 switch (ev->button) {
302                 case 1:
303                         return FALSE;
304                         break;
305
306                 case 2:
307                         if (redirect) {
308                                 redirect->set_active (!redirect->active(), this);
309                         }
310                         break;
311
312                 case 3:
313                         break;
314
315                 default:
316                         return FALSE;
317                 }
318         }
319
320         return TRUE;
321 }
322
323 Menu *
324 RedirectBox::build_redirect_menu ()
325 {
326         popup_act_grp = Gtk::ActionGroup::create();
327         
328         /* new stuff */
329
330         popup_act_grp->add (Gtk::Action::create("newplugin", _("New Plugin ...")), mem_fun(*this, &RedirectBox::choose_plugin));
331         popup_act_grp->add (Gtk::Action::create("newinsert", _("New Insert")), mem_fun(*this, &RedirectBox::choose_insert));
332         popup_act_grp->add (Gtk::Action::create("newsend", _("New Send ...")), mem_fun(*this, &RedirectBox::choose_send));
333         popup_act_grp->add (Gtk::Action::create("clear", _("Clear")), mem_fun(*this, &RedirectBox::clear_redirects));
334         
335         /* standard editing stuff */
336         
337         popup_act_grp->add (Gtk::Action::create("cut", _("Cut")), mem_fun(*this, &RedirectBox::cut_redirects));
338         popup_act_grp->add (Gtk::Action::create("copy", _("Copy")), mem_fun(*this, &RedirectBox::copy_redirects));
339         popup_act_grp->add (Gtk::Action::create("paste", _("Paste")), mem_fun(*this, &RedirectBox::paste_redirects));
340         popup_act_grp->add (Gtk::Action::create("rename", _("Rename")), mem_fun(*this, &RedirectBox::rename_redirects));
341         popup_act_grp->add (Gtk::Action::create("selectall", _("Select All")), mem_fun(*this, &RedirectBox::select_all_redirects));
342         popup_act_grp->add (Gtk::Action::create("deselectall", _("Deselect All")), mem_fun(*this, &RedirectBox::deselect_all_redirects));
343         
344         /* activation */
345         
346         popup_act_grp->add (Gtk::Action::create("activate", _("Activate")), bind (mem_fun(*this, &RedirectBox::for_selected_redirects), &RedirectBox::activate_redirect));
347         popup_act_grp->add (Gtk::Action::create("deactivate", _("Deactivate")), bind (mem_fun(*this, &RedirectBox::for_selected_redirects), &RedirectBox::deactivate_redirect));
348         popup_act_grp->add (Gtk::Action::create("activateall", _("Activate All")), bind (mem_fun(*this, &RedirectBox::all_redirects_active), true));
349         popup_act_grp->add (Gtk::Action::create("deactivateall", _("Deactivate All")), bind (mem_fun(*this, &RedirectBox::all_redirects_active), false));
350         
351         /* show editors */
352         
353         popup_act_grp->add (Gtk::Action::create("edit", _("Edit")), bind (mem_fun(*this, &RedirectBox::for_selected_redirects), &RedirectBox::edit_redirect));
354         
355         selection_dependent_items.push_back (popup_act_grp->get_action("cut"));
356         selection_dependent_items.push_back (popup_act_grp->get_action("copy"));
357         selection_dependent_items.push_back (popup_act_grp->get_action("rename"));
358         selection_dependent_items.push_back (popup_act_grp->get_action("activate"));
359         selection_dependent_items.push_back (popup_act_grp->get_action("deactivate"));
360         selection_dependent_items.push_back (popup_act_grp->get_action("edit"));
361
362         //popup_ui_mgr = Gtk::UIManager::create();
363         //popup_ui_mgr->insert_action_group(popup_act_grp);
364         
365     redirect_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/redirectmenu") );
366     redirect_menu->signal_map_event().connect (mem_fun(*this, &RedirectBox::redirect_menu_map_handler));
367     redirect_menu->set_name ("ArdourContextMenu");
368     show_all_children();
369     return redirect_menu;
370 }
371
372 gint
373 RedirectBox::redirect_menu_map_handler (GdkEventAny *ev)
374 {
375         bool sensitive = (redirect_display.get_selection()->count_selected_rows()) ? true : false;
376
377         for (vector<Glib::RefPtr<Gtk::Action> >::iterator i = selection_dependent_items.begin(); i != selection_dependent_items.end(); ++i) {
378                 (*i)->set_sensitive (sensitive);
379         }
380
381         popup_act_grp->get_action("paste")->set_sensitive (!_rr_selection.redirects.empty());
382         return FALSE;
383 }
384
385 void
386 RedirectBox::select_all_redirects ()
387 {
388         redirect_display.get_selection()->select_all();
389 }
390
391 void
392 RedirectBox::deselect_all_redirects ()
393 {
394         redirect_display.get_selection()->unselect_all();
395 }
396
397 void
398 RedirectBox::choose_plugin ()
399 {
400         show_plugin_selector();
401 }
402
403 void
404 RedirectBox::insert_plugin_chosen (Plugin *plugin)
405 {
406         if (plugin) {
407
408                 Redirect *redirect = new PluginInsert (_session, *plugin, _placement);
409                 
410                 redirect->active_changed.connect (mem_fun(*this, &RedirectBox::show_redirect_active));
411
412                 uint32_t err_streams;
413
414                 if (_route.add_redirect (redirect, this, &err_streams)) {
415                         wierd_plugin_dialog (*plugin, err_streams, _route);
416                         delete redirect;
417                 }
418         }
419 }
420
421 void
422 RedirectBox::wierd_plugin_dialog (Plugin& p, uint32_t streams, IO& io)
423 {
424         ArdourDialog dialog ("wierd plugin dialog");
425         Label label;
426         Button button (_("OK"));
427         VBox vpacker;
428         HBox button_box;
429
430         /* i hate this kind of code */
431
432         if (streams > p.get_info().n_inputs) {
433                 label.set_text (string_compose (_(
434 "You attempted to add a plugin (%1).\n"
435 "The plugin has %2 inputs\n"
436 "but at the insertion point, there are\n"
437 "%3 active signal streams.\n"
438 "\n"
439 "This makes no sense - you are throwing away\n"
440 "part of the signal."),
441                                          p.name(),
442                                          p.get_info().n_inputs,
443                                          streams));
444         } else if (streams < p.get_info().n_inputs) {
445                 label.set_text (string_compose (_(
446 "You attempted to add a plugin (%1).\n"
447 "The plugin has %2 inputs\n"
448 "but at the insertion point there are\n"
449 "only %3 active signal streams.\n"
450 "\n"
451 "This makes no sense - unless the plugin supports\n"
452 "side-chain inputs. A future version of Ardour will\n"
453 "support this type of configuration."),
454                                          p.name(),
455                                          p.get_info().n_inputs,
456                                          streams));
457         } else {
458                 label.set_text (string_compose (_(
459 "You attempted to add a plugin (%1).\n"
460 "\n"
461 "The I/O configuration doesn't make sense:\n"
462 "\n" 
463 "The plugin has %2 inputs and %3 outputs.\n"
464 "The track/bus has %4 inputs and %5 outputs.\n"
465 "The insertion point, has %6 active signals.\n"
466 "\n"
467 "Ardour does not understand what to do in such situations.\n"),
468                                          p.name(),
469                                          p.get_info().n_inputs,
470                                          p.get_info().n_outputs,
471                                          io.n_inputs(),
472                                          io.n_outputs(),
473                                          streams));
474         }
475
476         button_box.pack_start (button, false, true);
477
478         vpacker.set_spacing (12);
479         vpacker.set_border_width (12);
480         vpacker.pack_start (label);
481         vpacker.pack_start (button_box);
482
483         button.signal_clicked().connect (bind (mem_fun (dialog, &ArdourDialog::stop), 0));
484
485         dialog.add (vpacker);
486         dialog.set_name (X_("PluginIODialog"));
487         dialog.set_position (Gtk::WIN_POS_MOUSE);
488         dialog.set_modal (true);
489         dialog.show_all ();
490
491         // GTK2FIX
492         //dialog.realize();
493         //dialog.get_window()->set_decorations (Gdk::WMDecoration (GDK_DECOR_BORDER|GDK_DECOR_RESIZEH));
494
495         dialog.run ();
496 }
497
498 void
499 RedirectBox::choose_insert ()
500 {
501         Redirect *redirect = new PortInsert (_session, _placement);
502         redirect->active_changed.connect (mem_fun(*this, &RedirectBox::show_redirect_active));
503         _route.add_redirect (redirect, this);
504 }
505
506 void
507 RedirectBox::choose_send ()
508 {
509         Send *send = new Send (_session, _placement);
510
511         /* XXX need redirect lock on route */
512
513         send->ensure_io (0, _route.max_redirect_outs(), false, this);
514         
515         IOSelectorWindow *ios = new IOSelectorWindow (_session, *send, false, true);
516         
517         ios->show_all ();
518         ios->selector().Finished.connect (bind (mem_fun(*this, &RedirectBox::send_io_finished), static_cast<Redirect*>(send), ios));
519 }
520
521 void
522 RedirectBox::send_io_finished (IOSelector::Result r, Redirect* redirect, IOSelectorWindow* ios)
523 {
524         switch (r) {
525         case IOSelector::Cancelled:
526                 delete redirect;
527                 break;
528
529         case IOSelector::Accepted:
530                 _route.add_redirect (redirect, this);
531                 break;
532         }
533
534         delete_when_idle (ios);
535 }
536
537 void 
538 RedirectBox::disconnect_newplug ()
539 {
540     newplug_connection.disconnect();
541 }
542 void
543 RedirectBox::show_plugin_selector ()
544 {
545         newplug_connection = _plugin_selector.PluginCreated.connect (mem_fun(*this,&RedirectBox::insert_plugin_chosen));
546         _plugin_selector.show_all ();
547 }
548
549 void
550 RedirectBox::redirects_changed (void *src)
551 {
552         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::redirects_changed), src));
553         
554         //redirect_display.freeze ();
555         model.clear ();
556         redirect_active_connections.clear ();
557         redirect_name_connections.clear ();
558
559         _route.foreach_redirect (this, &RedirectBox::add_redirect_to_display);
560
561         switch (_placement) {
562         case PreFader:
563                 build_redirect_tooltip(redirect_eventbox, _("Pre-fader inserts, sends & plugins:"));
564                 break;
565         case PostFader:
566                 build_redirect_tooltip(redirect_eventbox, _("Post-fader inserts, sends & plugins:"));
567                 break;
568         }
569         //redirect_display.thaw ();
570 }
571
572 void
573 RedirectBox::add_redirect_to_display (Redirect *redirect)
574 {
575         if (redirect->placement() != _placement) {
576                 return;
577         }
578         
579         Gtk::TreeModel::Row row = *(model->append());
580         row[columns.text] = redirect_name (*redirect);
581         row[columns.redirect] = redirect;
582         
583         show_redirect_active (redirect, this);
584
585         redirect_active_connections.push_back (redirect->active_changed.connect (mem_fun(*this, &RedirectBox::show_redirect_active)));
586         redirect_name_connections.push_back (redirect->name_changed.connect (bind (mem_fun(*this, &RedirectBox::show_redirect_name), redirect)));
587 }
588
589 string
590 RedirectBox::redirect_name (Redirect& redirect)
591 {
592         Send *send;
593         string name_display;
594
595         if (!redirect.active()) {
596                 name_display = " (";
597         }
598
599         if ((send = dynamic_cast<Send *> (&redirect)) != 0) {
600
601                 name_display += '>';
602
603                 /* grab the send name out of its overall name */
604
605                 string::size_type lbracket, rbracket;
606                 lbracket = send->name().find ('[');
607                 rbracket = send->name().find (']');
608
609                 switch (_width) {
610                 case Wide:
611                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
612                         break;
613                 case Narrow:
614                         name_display += short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
615                         break;
616                 }
617
618         } else {
619
620                 switch (_width) {
621                 case Wide:
622                         name_display += redirect.name();
623                         break;
624                 case Narrow:
625                         name_display += short_version (redirect.name(), 5);
626                         break;
627                 }
628
629         }
630
631         if (!redirect.active()) {
632                 name_display += ')';
633         }
634
635         return name_display;
636 }
637
638 void
639 RedirectBox::build_redirect_tooltip (EventBox& box, string start)
640 {
641         string tip(start);
642
643         Gtk::TreeModel::Children children = model->children();
644         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
645                 Gtk::TreeModel::Row row = *iter;
646                 tip += '\n';
647                 tip += row[columns.text];
648         }
649         ARDOUR_UI::instance()->tooltips().set_tip (box, tip);
650 }
651
652 void
653 RedirectBox::show_redirect_name (void* src, Redirect *redirect)
654 {
655         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::show_redirect_name), src, redirect));
656         
657         show_redirect_active (redirect, src);
658 }
659
660 void
661 RedirectBox::show_redirect_active (Redirect *redirect, void *src)
662 {
663         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::show_redirect_active), redirect, src));
664
665         Gtk::TreeModel::Children children = model->children();
666         Gtk::TreeModel::Children::iterator iter = children.begin();
667
668         while( iter != children.end())
669         {
670                 if ((*iter)[columns.redirect] == redirect)
671                         break;
672                 iter++;
673         }
674
675         (*iter)[columns.text] = redirect_name (*redirect);
676
677         if (redirect->active()) {
678                 redirect_display.get_selection()->select (iter);
679         } else {
680                 redirect_display.get_selection()->unselect (iter);
681         }
682 }
683
684 void
685 RedirectBox::redirects_reordered (const TreeModel::Path& path,const TreeModel::iterator& iter ,int* hmm)
686 {
687         /* this is called before the reorder has been done, so just queue
688            something for idle time.
689         */
690
691         Glib::signal_idle().connect (mem_fun(*this, &RedirectBox::compute_redirect_sort_keys));
692 }
693
694 gint
695 RedirectBox::compute_redirect_sort_keys ()
696 {
697         uint32_t sort_key = 0;
698         Gtk::TreeModel::Children children = model->children();
699
700         for (Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
701                 Redirect *redirect = (*iter)[columns.redirect];
702                 redirect->set_sort_key (sort_key, this);
703                 sort_key++;
704         }
705
706         if (_route.sort_redirects ()) {
707
708                 redirects_changed (0);
709
710                 /* now tell them about the problem */
711
712                 ArdourDialog dialog ("wierd plugin dialog");
713                 Label label;
714                 Button button (_("OK"));
715                 VBox vpacker;
716                 HBox button_box;
717
718                 label.set_text (_("\
719 You cannot reorder this set of redirects\n\
720 in that way because the inputs and\n\
721 outputs do not work correctly."));
722
723                 button_box.pack_start (button, false, true);
724                 
725                 vpacker.set_spacing (12);
726                 vpacker.set_border_width (12);
727                 vpacker.pack_start (label);
728                 vpacker.pack_start (button_box);
729                 
730                 button.signal_clicked().connect (bind (mem_fun (dialog, &ArdourDialog::stop), 0));
731                 
732                 dialog.add (vpacker);
733                 dialog.set_name (X_("PluginIODialog"));
734                 dialog.set_position (Gtk::WIN_POS_MOUSE);
735                 dialog.set_modal (true);
736                 dialog.show_all ();
737
738                 // GTK2FIX
739                 //dialog.realize();
740                 //dialog.get_window()->set_decorations (Gdk::WMDecoration (GDK_DECOR_BORDER|GDK_DECOR_RESIZEH));
741                 
742                 dialog.run ();
743         }
744
745         return FALSE;
746 }
747
748 void
749 RedirectBox::rename_redirects ()
750 {
751         vector<Redirect*> to_be_renamed;
752         
753         get_selected_redirects (to_be_renamed);
754
755         if (to_be_renamed.empty()) {
756                 return;
757         }
758
759         for (vector<Redirect*>::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
760                 rename_redirect (*i);
761         }
762 }
763
764 void
765 RedirectBox::cut_redirects ()
766 {
767         vector<Redirect*> to_be_removed;
768         
769         get_selected_redirects (to_be_removed);
770
771         if (to_be_removed.empty()) {
772                 return;
773         }
774
775         /* this essentially transfers ownership of the redirect
776            of the redirect from the route to the mixer
777            selection.
778         */
779         
780         _rr_selection.set (to_be_removed);
781
782         for (vector<Redirect*>::iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
783                 
784                 void* gui = (*i)->get_gui ();
785                 
786                 if (gui) {
787                         static_cast<Gtk::Widget*>(gui)->hide ();
788                 }
789                 
790                 if (_route.remove_redirect (*i, this)) {
791                         /* removal failed */
792                         _rr_selection.remove (*i);
793                 }
794
795         }
796 }
797
798 void
799 RedirectBox::copy_redirects ()
800 {
801         vector<Redirect*> to_be_copied;
802         vector<Redirect*> copies;
803
804         get_selected_redirects (to_be_copied);
805
806         if (to_be_copied.empty()) {
807                 return;
808         }
809
810         for (vector<Redirect*>::iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
811                 copies.push_back (Redirect::clone (**i));
812         }
813
814         _rr_selection.set (copies);
815 }
816
817 gint
818 RedirectBox::idle_delete_redirect (Redirect *redirect)
819 {
820         /* NOT copied to _mixer.selection() */
821
822         if (_route.remove_redirect (redirect, this)) {
823                 /* removal failed */
824                 return FALSE;
825         }
826
827         delete redirect;
828         return FALSE;
829 }
830
831 void
832 RedirectBox::rename_redirect (Redirect* redirect)
833 {
834         ArdourDialog dialog ("rename redirect dialog");
835         Entry  entry;
836         VBox   vbox;
837         HBox   hbox;
838         Button ok_button (_("OK"));
839         Button cancel_button (_("Cancel"));
840
841         dialog.set_title (_("ardour: rename redirect"));
842         dialog.set_name ("RedirectRenameWindow");
843         dialog.set_size_request (300, -1);
844         dialog.set_position (Gtk::WIN_POS_MOUSE);
845         dialog.set_modal (true);
846
847         vbox.set_border_width (12);
848         vbox.set_spacing (12);
849         vbox.pack_start (entry, false, false);
850         vbox.pack_start (hbox, false, false);
851         hbox.pack_start (ok_button);
852         hbox.pack_start (cancel_button);
853         
854         dialog.add (vbox);
855
856         entry.set_name ("RedirectNameDisplay");
857         entry.set_text (redirect->name());
858         entry.select_region (0, -1);
859         entry.grab_focus ();
860
861         ok_button.set_name ("EditorGTKButton");
862         cancel_button.set_name ("EditorGTKButton");
863
864         entry.signal_activate().connect (bind (mem_fun (dialog, &ArdourDialog::stop), 1));
865         cancel_button.signal_clicked().connect (bind (mem_fun (dialog, &ArdourDialog::stop), -1));
866         ok_button.signal_clicked().connect (bind (mem_fun (dialog, &ArdourDialog::stop), 1));
867
868         /* recurse */
869         
870         dialog.set_keyboard_input (true);
871         dialog.run ();
872
873         if (dialog.run_status() == 1) {
874                 redirect->set_name (entry.get_text(), this);
875         }
876 }
877
878 void
879 RedirectBox::cut_redirect (Redirect *redirect)
880 {
881         /* this essentially transfers ownership of the redirect
882            of the redirect from the route to the mixer
883            selection.
884         */
885
886         _rr_selection.add (redirect);
887         
888         void* gui = redirect->get_gui ();
889
890         if (gui) {
891                 static_cast<Gtk::Widget*>(gui)->hide ();
892         }
893         
894         if (_route.remove_redirect (redirect, this)) {
895                 _rr_selection.remove (redirect);
896         }
897 }
898
899 void
900 RedirectBox::copy_redirect (Redirect *redirect)
901 {
902         Redirect* copy = Redirect::clone (*redirect);
903         _rr_selection.add (copy);
904 }
905
906 void
907 RedirectBox::paste_redirects ()
908 {
909         if (_rr_selection.redirects.empty()) {
910                 return;
911         }
912
913         RedirectSelection& sel (_rr_selection.redirects);
914         list<Redirect*> others;
915
916         for (list<Redirect*>::iterator i = sel.begin(); i != sel.end(); ++i) {
917
918                 Redirect* copy = Redirect::clone (**i);
919
920                 copy->set_placement (_placement, this);
921                 others.push_back (copy);
922         }
923
924         if (_route.add_redirects (others, this)) {
925                 for (list<Redirect*>::iterator i = others.begin(); i != others.end(); ++i) {
926                         delete *i;
927                 }
928
929                 string msg = _(
930                         "Copying the set of redirects on the clipboard failed,\n\
931 probably because the I/O configuration of the plugins\n\
932 could not match the configuration of this track.");
933                 ArdourMessage am (0, X_("bad redirect copy dialog"), msg);
934         }
935 }
936
937 void
938 RedirectBox::activate_redirect (Redirect *r)
939 {
940         r->set_active (true, 0);
941 }
942
943 void
944 RedirectBox::deactivate_redirect (Redirect *r)
945 {
946         r->set_active (false, 0);
947 }
948
949 void
950 RedirectBox::get_selected_redirects (vector<Redirect*>& redirects)
951 {
952     vector<Gtk::TreeModel::Path> pathlist = redirect_display.get_selection()->get_selected_rows();
953  
954         for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter)
955                 redirects.push_back ((*(model->get_iter(*iter)))[columns.redirect]);
956 }
957
958 void
959 RedirectBox::for_selected_redirects (void (RedirectBox::*pmf)(Redirect*))
960 {
961     vector<Gtk::TreeModel::Path> pathlist = redirect_display.get_selection()->get_selected_rows();
962
963         for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
964                 Redirect* redirect = (*(model->get_iter(*iter)))[columns.redirect];
965                 (this->*pmf)(redirect);
966         }
967 }
968
969 void
970 RedirectBox::clone_redirects ()
971 {
972         RouteSelection& routes (_rr_selection.routes);
973
974         if (!routes.empty()) {
975                 if (_route.copy_redirects (*routes.front(), _placement)) {
976                         string msg = _(
977 "Copying the set of redirects on the clipboard failed,\n\
978 probably because the I/O configuration of the plugins\n\
979 could not match the configuration of this track.");
980                         ArdourMessage am (0, X_("bad redirect copy dialog"), msg);
981                 }
982         }
983 }
984
985 void
986 RedirectBox::all_redirects_active (bool state)
987 {
988         _route.all_redirects_active (state);
989 }
990
991 void
992 RedirectBox::clear_redirects()
993 {
994         string prompt;
995         vector<string> choices;
996
997         if (dynamic_cast<AudioTrack*>(&_route) != 0) {
998                 prompt = _("Do you really want to remove all redirects from this track?\n"
999                            "(this cannot be undone)");
1000         } else {
1001                 prompt = _("Do you really want to remove all redirects from this bus?\n"
1002                            "(this cannot be undone)");
1003         }
1004
1005         choices.push_back (_("Yes, remove them all"));
1006         choices.push_back (_("Cancel"));
1007
1008         Gtkmm2ext::Choice prompter (prompt, choices);
1009
1010         prompter.chosen.connect(sigc::ptr_fun(Gtk::Main::quit));
1011         prompter.show_all ();
1012
1013         Gtk::Main::run ();
1014
1015         if (prompter.get_choice() == 0) {
1016                 _route.clear_redirects (this);
1017         }
1018 }
1019
1020
1021 void
1022 RedirectBox::edit_redirect (Redirect* redirect)
1023 {
1024         Insert *insert;
1025
1026         if (dynamic_cast<AudioTrack*>(&_route) != 0) {
1027
1028                 if (dynamic_cast<AudioTrack*> (&_route)->freeze_state() == AudioTrack::Frozen) {
1029                         return;
1030                 }
1031         }
1032         
1033         if ((insert = dynamic_cast<Insert *> (redirect)) == 0) {
1034                 
1035                 /* its a send */
1036                 
1037                 if (!_session.engine().connected()) {
1038                         return;
1039                 }
1040
1041                 Send *send = dynamic_cast<Send*> (redirect);
1042                 
1043                 SendUIWindow *send_ui;
1044                 
1045                 if (send->get_gui() == 0) {
1046                         
1047                         string title;
1048                         title = string_compose(_("ardour: %1"), send->name());  
1049                         
1050                         send_ui = new SendUIWindow (*send, _session);
1051                         send_ui->set_title (title);
1052                         send->set_gui (send_ui);
1053                         
1054                 } else {
1055                         send_ui = reinterpret_cast<SendUIWindow *> (send->get_gui());
1056                 }
1057                 
1058                 if (send_ui->is_visible()) {
1059                         send_ui->get_window()->raise ();
1060                 } else {
1061                         send_ui->show_all ();
1062                 }
1063                 
1064         } else {
1065                 
1066                 /* its an insert */
1067                 
1068                 PluginInsert *plugin_insert;
1069                 PortInsert *port_insert;
1070                 
1071                 if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
1072                         
1073                         PluginUIWindow *plugin_ui;
1074                         
1075                         if (plugin_insert->get_gui() == 0) {
1076                                 
1077                                 string title;
1078                                 string maker = plugin_insert->plugin().maker();
1079                                 string::size_type email_pos;
1080                                 
1081                                 if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1082                                         maker = maker.substr (0, email_pos - 1);
1083                                 }
1084                                 
1085                                 if (maker.length() > 32) {
1086                                         maker = maker.substr (0, 32);
1087                                         maker += " ...";
1088                                 }
1089
1090                                 title = string_compose(_("ardour: %1: %2 (by %3)"), _route.name(), plugin_insert->name(), maker);       
1091                                 
1092                                 plugin_ui = new PluginUIWindow (_session.engine(), *plugin_insert);
1093                                 if (_owner_is_mixer) {
1094                                         ARDOUR_UI::instance()->the_mixer()->ensure_float (*plugin_ui);
1095                                 } else {
1096                                         ARDOUR_UI::instance()->the_editor().ensure_float (*plugin_ui);
1097                                 }
1098                                 plugin_ui->set_title (title);
1099                                 plugin_insert->set_gui (plugin_ui);
1100                                 
1101                         } else {
1102                                 plugin_ui = reinterpret_cast<PluginUIWindow *> (plugin_insert->get_gui());
1103                         }
1104                         
1105                         if (plugin_ui->is_visible()) {
1106                                 plugin_ui->get_window()->raise ();
1107                         } else {
1108                                 plugin_ui->show_all ();
1109                         }
1110                         
1111                 } else if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
1112                         
1113                         if (!_session.engine().connected()) {
1114                                 ArdourMessage msg (NULL, "nojackdialog", _("Not connected to JACK - no I/O changes are possible"));
1115                                 return;
1116                         }
1117
1118                         PortInsertWindow *io_selector;
1119
1120                         if (port_insert->get_gui() == 0) {
1121                                 io_selector = new PortInsertWindow (_session, *port_insert);
1122                                 port_insert->set_gui (io_selector);
1123                                 
1124                         } else {
1125                                 io_selector = reinterpret_cast<PortInsertWindow *> (port_insert->get_gui());
1126                         }
1127                         
1128                         if (io_selector->is_visible()) {
1129                                 io_selector->get_window()->raise ();
1130                         } else {
1131                                 io_selector->show_all ();
1132                         }
1133                 }
1134         }
1135 }
1136
1137