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