Use central method to check for engine for processor operations
[ardour.git] / gtk2_ardour / route_ui.cc
index 1a807967b0fddea95d8b42a2cedf5c16f659994c..157bb40a308b7646bc5b5a0b8fdf47dc1a93600b 100644 (file)
 #include <map>
 #include <boost/algorithm/string.hpp>
 
-#include <gtkmm2ext/gtk_ui.h>
-#include <gtkmm2ext/choice.h>
-#include <gtkmm2ext/doi.h>
-#include <gtkmm2ext/bindable_button.h>
-#include <gtkmm2ext/barcontroller.h>
-#include <gtkmm2ext/gtk_ui.h>
-#include <gtkmm2ext/utils.h>
+#include <gtkmm/stock.h>
 
 #include "pbd/memento_command.h"
 #include "pbd/stacktrace.h"
 #include "ardour/session.h"
 #include "ardour/template_utils.h"
 
-#include "ardour_button.h"
+#include "gtkmm2ext/gtk_ui.h"
+#include "gtkmm2ext/doi.h"
+#include "gtkmm2ext/gtk_ui.h"
+#include "gtkmm2ext/utils.h"
+
+#include "widgets/ardour_button.h"
+#include "widgets/binding_proxy.h"
+#include "widgets/prompter.h"
+
 #include "ardour_dialog.h"
 #include "ardour_ui.h"
 #include "automation_time_axis.h"
 #include "keyboard.h"
 #include "latency_gui.h"
 #include "mixer_strip.h"
+#include "patch_change_widget.h"
 #include "plugin_pin_dialog.h"
-#include "prompter.h"
 #include "rgb_macros.h"
 #include "route_time_axis.h"
 #include "route_ui.h"
+#include "save_template_dialog.h"
 #include "timers.h"
 #include "ui_config.h"
 #include "utils.h"
 
-
 #include "pbd/i18n.h"
+
 using namespace Gtk;
 using namespace Gtkmm2ext;
 using namespace ARDOUR;
 using namespace ARDOUR_UI_UTILS;
+using namespace ArdourWidgets;
 using namespace PBD;
 using namespace std;
 
@@ -118,6 +122,8 @@ RouteUI::~RouteUI()
                ARDOUR_UI::instance()->gui_object_state->remove_node (route_state_id());
        }
 
+       delete_patch_change_dialog ();
+
        _route.reset (); /* drop reference to route, so that it can be cleaned up */
        route_connections.drop_connections ();
 
@@ -243,6 +249,7 @@ RouteUI::reset ()
        delete mute_menu;
        mute_menu = 0;
 
+       delete_patch_change_dialog ();
        _color_picker.reset ();
 
        denormal_menu_item = 0;
@@ -326,9 +333,6 @@ RouteUI::set_route (boost::shared_ptr<Route> rp)
 
        if (is_track()) {
                track()->FreezeChange.connect (*this, invalidator (*this), boost::bind (&RouteUI::map_frozen, this), gui_context());
-#ifdef XXX_OLD_DESTRUCTIVE_API_XXX
-               track()->TrackModeChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::track_mode_changed, this), gui_context());
-#endif
                track_mode_changed();
        }
 
@@ -347,10 +351,10 @@ RouteUI::set_route (boost::shared_ptr<Route> rp)
                rec_enable_button->show();
                rec_enable_button->set_controllable (t->rec_enable_control());
 
-                if (is_midi_track()) {
-                        midi_track()->StepEditStatusChange.connect (route_connections, invalidator (*this),
-                                                                    boost::bind (&RouteUI::step_edit_changed, this, _1), gui_context());
-                }
+               if (is_midi_track()) {
+                       midi_track()->StepEditStatusChange.connect (route_connections, invalidator (*this),
+                                       boost::bind (&RouteUI::step_edit_changed, this, _1), gui_context());
+               }
 
        }
 
@@ -400,9 +404,9 @@ RouteUI::set_route (boost::shared_ptr<Route> rp)
 void
 RouteUI::polarity_changed ()
 {
-        if (!_route) {
-                return;
-        }
+       if (!_route) {
+               return;
+       }
 
        set_invert_button_state ();
 }
@@ -415,7 +419,7 @@ RouteUI::mute_press (GdkEventButton* ev)
        }
 
        //if this is a binding action, let the ArdourButton handle it
-       if ( BindingProxy::is_bind_action(ev) )
+       if (BindingProxy::is_bind_action(ev) )
                return false;
 
        multiple_mute_change = false;
@@ -495,7 +499,7 @@ RouteUI::mute_press (GdkEventButton* ev)
                                        }
 
                                        boost::shared_ptr<MuteControl> mc = _route->mute_control();
-                                       mc->start_touch (_session->audible_frame ());
+                                       mc->start_touch (_session->audible_sample ());
                                        _session->set_controls (route_list_to_control_list (rl, &Stripable::mute_control), _route->muted_by_self() ? 0.0 : 1.0, Controllable::InverseGroup);
                                }
 
@@ -511,7 +515,7 @@ RouteUI::mute_press (GdkEventButton* ev)
                                }
 
                                boost::shared_ptr<MuteControl> mc = _route->mute_control();
-                               mc->start_touch (_session->audible_frame ());
+                               mc->start_touch (_session->audible_sample ());
                                mc->set_value (!_route->muted_by_self(), Controllable::UseGroup);
                        }
                }
@@ -529,7 +533,7 @@ RouteUI::mute_release (GdkEventButton* /*ev*/)
                _mute_release = 0;
        }
 
-       _route->mute_control()->stop_touch (false, _session->audible_frame ());
+       _route->mute_control()->stop_touch (_session->audible_sample ());
 
        return false;
 }
@@ -590,7 +594,7 @@ RouteUI::solo_press(GdkEventButton* ev)
        }
 
        //if this is a binding action, let the ArdourButton handle it
-       if ( BindingProxy::is_bind_action(ev) )
+       if (BindingProxy::is_bind_action(ev) )
                return false;
 
        multiple_solo_change = false;
@@ -766,11 +770,11 @@ RouteUI::rec_enable_press(GdkEventButton* ev)
        }
 
        //if this is a binding action, let the ArdourButton handle it
-       if ( BindingProxy::is_bind_action(ev) )
+       if (BindingProxy::is_bind_action(ev) )
                return false;
 
        if (!_session->engine().connected()) {
-               MessageDialog msg (_("Not connected to AudioEngine - cannot engage record"));
+               MessageDialog msg (_("Not connected to AudioEngine - cannot engage record"));
                msg.run ();
                return false;
        }
@@ -902,36 +906,27 @@ RouteUI::monitor_release (GdkEventButton* ev, MonitorChoice monitor_choice)
        MonitorChoice mc;
        boost::shared_ptr<RouteList> rl;
 
-       /* XXX for now, monitoring choices are orthogonal. cue monitoring
-          will follow in 3.X but requires mixing the input and playback (disk)
-          signal together, which requires yet more buffers.
-       */
-
        if (t->monitoring_control()->monitoring_choice() & monitor_choice) {
                mc = MonitorChoice (t->monitoring_control()->monitoring_choice() & ~monitor_choice);
        } else {
-               /* this line will change when the options are non-orthogonal */
-               // mc = MonitorChoice (t->monitoring_choice() | monitor_choice);
-               mc = monitor_choice;
+               mc = MonitorChoice (t->monitoring_control()->monitoring_choice() | monitor_choice);
        }
 
        if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
+               /* Primary-Tertiary-click applies change to all routes */
                rl = _session->get_routes ();
-
+               _session->set_controls (route_list_to_control_list (rl, &Stripable::monitoring_control), (double) mc, Controllable::NoGroup);
        } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
-               if (_route->route_group() && _route->route_group()->is_monitoring()) {
-                       rl = _route->route_group()->route_list();
-               } else {
-                       rl.reset (new RouteList);
-                       rl->push_back (route());
-               }
+               /* Primary-click overrides group */
+               rl.reset (new RouteList);
+               rl->push_back (route());
+               _session->set_controls (route_list_to_control_list (rl, &Stripable::monitoring_control), (double) mc, Controllable::InverseGroup);
        } else {
                rl.reset (new RouteList);
                rl->push_back (route());
+               _session->set_controls (route_list_to_control_list (rl, &Stripable::monitoring_control), (double) mc, Controllable::UseGroup);
        }
 
-       _session->set_controls (route_list_to_control_list (rl, &Stripable::monitoring_control), (double) mc, Controllable::UseGroup);
-
        return false;
 }
 
@@ -1342,11 +1337,11 @@ RouteUI::mute_active_state (Session*, boost::shared_ptr<Stripable> s)
 void
 RouteUI::update_mute_display ()
 {
-        if (!_route) {
-                return;
-        }
+       if (!_route) {
+               return;
+       }
 
-        mute_button->set_active_state (mute_active_state (_session, _route));
+       mute_button->set_active_state (mute_active_state (_session, _route));
 }
 
 
@@ -1378,31 +1373,31 @@ RouteUI::blink_rec_display (bool blinkOn)
        }
 
        if (track()->rec_enable_control()->get_value()) {
-                switch (_session->record_status ()) {
-                case Session::Recording:
-                        rec_enable_button->set_active_state (Gtkmm2ext::ExplicitActive);
-                        break;
-
-                case Session::Disabled:
-                case Session::Enabled:
-                       if (UIConfiguration::instance().get_blink_rec_arm()) {
-                               rec_enable_button->set_active_state ( blinkOn ? Gtkmm2ext::ExplicitActive : Gtkmm2ext::Off );
-                       } else {
-                               rec_enable_button->set_active_state ( ImplicitActive );
-                       }
-                        break;
-                }
-
-                if (step_edit_item) {
-                        step_edit_item->set_sensitive (false);
-                }
+               switch (_session->record_status ()) {
+                       case Session::Recording:
+                               rec_enable_button->set_active_state (Gtkmm2ext::ExplicitActive);
+                               break;
+
+                       case Session::Disabled:
+                       case Session::Enabled:
+                               if (UIConfiguration::instance().get_blink_rec_arm()) {
+                                       rec_enable_button->set_active_state ( blinkOn ? Gtkmm2ext::ExplicitActive : Gtkmm2ext::Off );
+                               } else {
+                                       rec_enable_button->set_active_state ( ImplicitActive );
+                               }
+                               break;
+               }
+
+               if (step_edit_item) {
+                       step_edit_item->set_sensitive (false);
+               }
 
        } else {
                rec_enable_button->unset_active_state ();
 
-                if (step_edit_item) {
-                        step_edit_item->set_sensitive (true);
-                }
+               if (step_edit_item) {
+                       step_edit_item->set_sensitive (true);
+               }
        }
 
        check_rec_enable_sensitivity ();
@@ -1422,19 +1417,15 @@ RouteUI::build_solo_menu (void)
        check->set_active (_route->solo_isolate_control()->solo_isolated());
        check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_isolated), check));
        items.push_back (CheckMenuElem(*check));
-        solo_isolated_check = dynamic_cast<Gtk::CheckMenuItem*>(&items.back());
+       solo_isolated_check = dynamic_cast<Gtk::CheckMenuItem*>(&items.back());
        check->show_all();
 
        check = new Gtk::CheckMenuItem(_("Solo Safe"));
        check->set_active (_route->solo_safe_control()->solo_safe());
        check->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::toggle_solo_safe), check));
        items.push_back (CheckMenuElem(*check));
-        solo_safe_check = dynamic_cast<Gtk::CheckMenuItem*>(&items.back());
+       solo_safe_check = dynamic_cast<Gtk::CheckMenuItem*>(&items.back());
        check->show_all();
-
-       //items.push_back (SeparatorElem());
-       // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
-
 }
 
 void
@@ -1471,9 +1462,6 @@ RouteUI::build_mute_menu(void)
        items.push_back (CheckMenuElem(*main_mute_check));
        main_mute_check->show_all();
 
-       //items.push_back (SeparatorElem());
-       // items.push_back (MenuElem (_("MIDI Bind"), sigc::mem_fun (*mute_button, &BindableToggleButton::midi_learn)));
-
        _route->mute_points_changed.connect (route_connections, invalidator (*this), boost::bind (&RouteUI::muting_change, this), gui_context());
 }
 
@@ -1606,14 +1594,14 @@ RouteUI::solo_safe_button_release (GdkEventButton* ev)
 void
 RouteUI::toggle_solo_isolated (Gtk::CheckMenuItem* check)
 {
-        bool view = check->get_active();
-        bool model = _route->solo_isolate_control()->solo_isolated();
+       bool view = check->get_active();
+       bool model = _route->solo_isolate_control()->solo_isolated();
 
-        /* called AFTER the view has changed */
+       /* called AFTER the view has changed */
 
-        if (model != view) {
-               _route->solo_isolate_control()->set_value (view ? 1.0 : 0.0, Controllable::UseGroup);
-        }
+       if (model != view) {
+               _route->solo_isolate_control()->set_value (view ? 1.0 : 0.0, Controllable::UseGroup);
+       }
 }
 
 void
@@ -1622,8 +1610,40 @@ RouteUI::toggle_solo_safe (Gtk::CheckMenuItem* check)
        _route->solo_safe_control()->set_value (check->get_active() ? 1.0 : 0.0, Controllable::UseGroup);
 }
 
-/** Ask the user to choose a colour, and then apply that color to my route
- */
+void
+RouteUI::delete_patch_change_dialog ()
+{
+       if (!_route) {
+               return;
+       }
+       delete _route->patch_selector_dialog ();
+       _route->set_patch_selector_dialog (0);
+}
+
+PatchChangeGridDialog*
+RouteUI::patch_change_dialog () const
+{
+       return _route->patch_selector_dialog ();
+}
+
+void
+RouteUI::select_midi_patch ()
+{
+       if (patch_change_dialog ()) {
+               patch_change_dialog()->present ();
+               return;
+       }
+
+       /* note: RouteTimeAxisView is resoponsible to updating
+        * the Dialog (PatchChangeGridDialog::refresh())
+        * when the midnam model changes.
+        */
+       PatchChangeGridDialog* d = new PatchChangeGridDialog (_route);
+       _route->set_patch_selector_dialog (d);
+       d->present ();
+}
+
+/** Ask the user to choose a colour, and then apply that color to my route */
 void
 RouteUI::choose_color ()
 {
@@ -1678,7 +1698,7 @@ RouteUI::verify_new_route_name (const std::string& name)
 void
 RouteUI::route_rename ()
 {
-       ArdourPrompter name_prompter (true);
+       ArdourWidgets::Prompter name_prompter (true);
        string result;
        bool done = false;
 
@@ -1900,67 +1920,47 @@ RouteUI::map_frozen ()
 void
 RouteUI::adjust_latency ()
 {
-       LatencyDialog dialog (_route->name() + _(" latency"), *(_route->output()), _session->frame_rate(), AudioEngine::instance()->samples_per_cycle());
+       LatencyDialog dialog (_route->name() + _(" latency"), *(_route->output()), _session->sample_rate(), AudioEngine::instance()->samples_per_cycle());
 }
 
-bool
-RouteUI::process_save_template_prompter (ArdourPrompter& prompter, const std::string& dir)
-{
-       std::string path;
-       std::string safe_name;
-       std::string name;
 
-       prompter.get_result (name, true);
-
-       safe_name = legalize_for_path (name);
-       safe_name += template_suffix;
-
-       path = Glib::build_filename (dir, safe_name);
+void
+RouteUI::save_as_template_dialog_response (int response, SaveTemplateDialog* d)
+{
+       if (response == RESPONSE_ACCEPT) {
+               const string name = d->get_template_name ();
+               const string desc = d->get_description ();
+               const string path = Glib::build_filename(ARDOUR::user_route_template_directory (), name + ARDOUR::template_suffix);
 
-       if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
-               bool overwrite = overwrite_file_dialog (prompter,
-                                                       _("Confirm Template Overwrite"),
-                                                       _("A template already exists with that name. Do you want to overwrite it?"));
+               if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) { /* file already exists. */
+                       bool overwrite = overwrite_file_dialog (*d,
+                                                               _("Confirm Template Overwrite"),
+                                                               _("A template already exists with that name. Do you want to overwrite it?"));
 
-               if (!overwrite) {
-                       return false;
+                       if (!overwrite) {
+                               d->show ();
+                               return;
+                       }
                }
+               _route->save_as_template (path, name, desc);
        }
 
-       _route->save_as_template (path, name);
-
-       return true;
+       delete d;
 }
 
 void
 RouteUI::save_as_template ()
 {
-       std::string dir;
-
-       dir = ARDOUR::user_route_template_directory ();
+       const std::string dir = ARDOUR::user_route_template_directory ();
 
        if (g_mkdir_with_parents (dir.c_str(), 0755)) {
                error << string_compose (_("Cannot create route template directory %1"), dir) << endmsg;
                return;
        }
 
-       ArdourPrompter prompter (true); // modal
-
-       prompter.set_title (_("Save As Template"));
-       prompter.set_prompt (_("Template name:"));
-       prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
-
-       bool finished = false;
-       while (!finished) {
-               switch (prompter.run()) {
-               case RESPONSE_ACCEPT:
-                       finished = process_save_template_prompter (prompter, dir);
-                       break;
-               default:
-                       finished = true;
-                       break;
-               }
-       }
+       SaveTemplateDialog* d = new SaveTemplateDialog (_route->name(), _route->comment());
+       d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &RouteUI::save_as_template_dialog_response), d));
+       d->show ();
 }
 
 void
@@ -2014,30 +2014,6 @@ RouteUI::parameter_changed (string const & p)
        }
 }
 
-void
-RouteUI::step_gain_up ()
-{
-       _route->gain_control()->set_value (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.1), Controllable::UseGroup);
-}
-
-void
-RouteUI::page_gain_up ()
-{
-       _route->gain_control()->set_value (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) + 0.5), Controllable::UseGroup);
-}
-
-void
-RouteUI::step_gain_down ()
-{
-       _route->gain_control()->set_value (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.1), Controllable::UseGroup);
-}
-
-void
-RouteUI::page_gain_down ()
-{
-       _route->gain_control()->set_value (dB_to_coefficient (accurate_coefficient_to_dB (_route->gain_control()->get_value()) - 0.5), Controllable::UseGroup);
-}
-
 void
 RouteUI::setup_invert_buttons ()
 {
@@ -2179,16 +2155,8 @@ RouteUI::invert_menu_toggled (uint32_t c)
 void
 RouteUI::set_invert_sensitive (bool yn)
 {
-        for (vector<ArdourButton*>::iterator b = _invert_buttons.begin(); b != _invert_buttons.end(); ++b) {
-                (*b)->set_sensitive (yn);
-        }
-}
-
-void
-RouteUI::request_redraw ()
-{
-       if (_route) {
-               _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
+       for (vector<ArdourButton*>::iterator b = _invert_buttons.begin(); b != _invert_buttons.end(); ++b) {
+               (*b)->set_sensitive (yn);
        }
 }
 
@@ -2220,9 +2188,8 @@ RouteUI::track_mode_changed (void)
 }
 
 /** @return the color that this route should use; it maybe its own,
-    or it maybe that of its route group.
-*/
-
+ *  or it maybe that of its route group.
+ */
 Gdk::Color
 RouteUI::route_color () const
 {
@@ -2356,6 +2323,10 @@ RouteUI::manage_pins ()
 void
 RouteUI::fan_out (bool to_busses, bool group)
 {
+       if (ARDOUR_UI_UTILS::no_engine_notify ()) {
+               return;
+       }
+
        DisplaySuspender ds;
        boost::shared_ptr<ARDOUR::Route> route = _route;
        boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (route->the_instrument ());
@@ -2401,19 +2372,27 @@ RouteUI::fan_out (bool to_busses, bool group)
                std::string bn = BUSNAME;
                boost::shared_ptr<Route> r = _session->route_by_name (bn);
                if (!r) {
-                       if (to_busses) {
-                               RouteList rl = _session->new_audio_route (busnames[bn], outputs, NULL, 1, bn, PresentationInfo::AudioBus, PresentationInfo::max_order);
-                               r = rl.front ();
-                               assert (r);
-                       } else {
-                               list<boost::shared_ptr<AudioTrack> > tl =
-                                       _session->new_audio_track (busnames[bn], outputs, NULL, 1, bn, PresentationInfo::max_order, Normal);
-                               r = tl.front ();
-                               assert (r);
-
-                               boost::shared_ptr<ControlList> cl (new ControlList);
-                               cl->push_back (r->monitoring_control ());
-                               _session->set_controls (cl, (double) MonitorInput, Controllable::NoGroup);
+                       try {
+                               if (to_busses) {
+                                       RouteList rl = _session->new_audio_route (busnames[bn], outputs, NULL, 1, bn, PresentationInfo::AudioBus, PresentationInfo::max_order);
+                                       r = rl.front ();
+                                       assert (r);
+                               } else {
+                                       list<boost::shared_ptr<AudioTrack> > tl =
+                                               _session->new_audio_track (busnames[bn], outputs, NULL, 1, bn, PresentationInfo::max_order, Normal);
+                                       r = tl.front ();
+                                       assert (r);
+
+                                       boost::shared_ptr<ControlList> cl (new ControlList);
+                                       cl->push_back (r->monitoring_control ());
+                                       _session->set_controls (cl, (double) MonitorInput, Controllable::NoGroup);
+                               }
+                       } catch (...) {
+                               if (!to_group.empty()) {
+                                       boost::shared_ptr<RouteList> rl (&to_group);
+                                       _session->remove_routes (rl);
+                               }
+                               return;
                        }
                        r->input ()->disconnect (this);
                }
@@ -2460,3 +2439,10 @@ RouteUI::stripable () const
        return _route;
 }
 
+void
+RouteUI::set_disk_io_point (DiskIOPoint diop)
+{
+       if (_route && is_track()) {
+               track()->set_disk_io_point (diop);
+       }
+}