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