NOOP, remove trailing whitespace, replace tabs in python scripts
[ardour.git] / gtk2_ardour / ardour_ui.cc
index 18fc273656e9f05828995c2c6b753f0359f0826f..c6a0f4f27aebe3f27ccf88ebe74273db21a2b156 100644 (file)
@@ -26,7 +26,6 @@
 #include <cmath>
 #include <iostream>
 #include <cerrno>
-#include <fstream>
 
 #include <stdarg.h>
 
@@ -41,7 +40,7 @@
 #include <time.h>
 
 #include <glib.h>
-#include <pbd/gstdio_compat.h>
+#include "pbd/gstdio_compat.h"
 
 #include <gtkmm/messagedialog.h>
 #include <gtkmm/accelmap.h>
@@ -112,6 +111,7 @@ typedef uint64_t microseconds_t;
 #include "audio_region_view.h"
 #include "big_clock_window.h"
 #include "bundle_manager.h"
+#include "duplicate_routes_dialog.h"
 #include "engine_dialog.h"
 #include "export_video_dialog.h"
 #include "export_video_infobox.h"
@@ -285,6 +285,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        , _status_bar_visibility (X_("status-bar"))
        , _feedback_exists (false)
        , _log_not_acknowledged (LogLevelNone)
+       , duplicate_routes_dialog (0)
 {
        Gtkmm2ext::init (localedir);
 
@@ -305,7 +306,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
 
        xmlSetGenericErrorFunc (this, libxml_generic_error_func);
        xmlSetStructuredErrorFunc (this, libxml_structured_error_func);
-       
+
        UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &ARDOUR_UI::parameter_changed));
        boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
        UIConfiguration::instance().map_parameters (pc);
@@ -441,7 +442,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        /* Trigger setting up the color scheme and loading the GTK RC file */
 
        UIConfiguration::instance().load_rc_file (false);
-       
+
        _process_thread = new ProcessThread ();
        _process_thread->init ();
 
@@ -472,11 +473,14 @@ ARDOUR_UI::engine_stopped ()
        ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_stopped)
        ActionManager::set_sensitive (ActionManager::engine_sensitive_actions, false);
        ActionManager::set_sensitive (ActionManager::engine_opposite_sensitive_actions, true);
+       update_sample_rate (0);
+       update_cpu_load ();
 }
 
 void
 ARDOUR_UI::engine_running ()
 {
+       ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_running)
        if (first_time_engine_run) {
                post_engine();
                first_time_engine_run = false;
@@ -491,6 +495,8 @@ ARDOUR_UI::engine_running ()
        update_sample_rate (AudioEngine::instance()->sample_rate());
        update_timecode_format ();
        update_peak_thread_work ();
+       ActionManager::set_sensitive (ActionManager::engine_sensitive_actions, true);
+       ActionManager::set_sensitive (ActionManager::engine_opposite_sensitive_actions, false);
 }
 
 void
@@ -1662,10 +1668,10 @@ ARDOUR_UI::open_recent_session ()
 }
 
 bool
-ARDOUR_UI::check_audioengine ()
+ARDOUR_UI::check_audioengine (Gtk::Window& parent)
 {
        if (!AudioEngine::instance()->connected()) {
-               MessageDialog msg (string_compose (
+               MessageDialog msg (parent, string_compose (
                                           _("%1 is not connected to any audio backend.\n"
                                             "You cannot open or close sessions in this condition"),
                                           PROGRAM_NAME));
@@ -1679,9 +1685,8 @@ ARDOUR_UI::check_audioengine ()
 void
 ARDOUR_UI::open_session ()
 {
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
-
        }
 
        /* ardour sessions are folders */
@@ -1690,8 +1695,6 @@ ARDOUR_UI::open_session ()
        open_session_selector.add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
        open_session_selector.set_default_response(Gtk::RESPONSE_ACCEPT);
 
-
-
        if (_session) {
                string session_parent_dir = Glib::path_get_dirname(_session->path());
                open_session_selector.set_current_folder(session_parent_dir);
@@ -1699,11 +1702,9 @@ ARDOUR_UI::open_session ()
                open_session_selector.set_current_folder(Config->get_default_session_parent_dir());
        }
 
+       Gtkmm2ext::add_volume_shortcuts (open_session_selector);
        try {
                /* add_shortcut_folder throws an exception if the folder being added already has a shortcut */
-#ifdef GTKOSX
-               open_session_selector.add_shortcut_folder_uri("file:///Volumes");
-#endif
                string default_session_folder = Config->get_default_session_parent_dir();
                open_session_selector.add_shortcut_folder (default_session_folder);
        }
@@ -2109,7 +2110,7 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
        if (affect_transport) {
                if (rolling) {
                        _session->request_stop (with_abort, true);
-               } else {
+               } else if (!_session->config.get_external_sync()) {
                        if (UIConfiguration::instance().get_follow_edits() && ( editor->get_selection().time.front().start == _session->transport_frame() ) ) {  //if playhead is exactly at the start of a range, we can assume it was placed there by follow_edits
                                _session->request_play_range (&editor->get_selection().time, true);
                                _session->set_requested_return_frame( editor->get_selection().time.front().start );  //force an auto-return here
@@ -2177,7 +2178,7 @@ ARDOUR_UI::transport_rewind (int option)
 {
        float current_transport_speed;
 
-               if (_session) {
+       if (_session) {
                current_transport_speed = _session->transport_speed();
 
                if (current_transport_speed >= 0.0f) {
@@ -2287,7 +2288,7 @@ ARDOUR_UI::map_transport_state ()
                        auto_loop_button.set_active (false);
                }
 
-               if (UIConfiguration::instance().get_follow_edits()) {
+               if (UIConfiguration::instance().get_follow_edits() && !_session->config.get_external_sync()) {
                        /* light up both roll and play-selection if they are joined */
                        roll_button.set_active (true);
                        play_selection_button.set_active (true);
@@ -2390,7 +2391,7 @@ ARDOUR_UI::save_session_as ()
        }
 
 
-       Session::SaveAs sa;
+       Session::SaveAs sa;
 
        sa.new_parent_folder = save_as_dialog->new_parent_folder ();
        sa.new_name = save_as_dialog->new_name ();
@@ -2443,6 +2444,47 @@ ARDOUR_UI::save_session_as ()
        }
 }
 
+bool
+ARDOUR_UI::process_snapshot_session_prompter (ArdourPrompter& prompter, bool switch_to_it)
+{
+       string snapname;
+
+       prompter.get_result (snapname);
+
+       bool do_save = (snapname.length() != 0);
+
+       if (do_save) {
+               char illegal = Session::session_name_is_legal(snapname);
+               if (illegal) {
+                       MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
+                                            "snapshot names may not contain a '%1' character"), illegal));
+                       msg.run ();
+                       return false;
+               }
+       }
+
+       vector<std::string> p;
+       get_state_files_in_directory (_session->session_directory().root_path(), p);
+       vector<string> n = get_file_names_no_extension (p);
+
+       if (find (n.begin(), n.end(), snapname) != n.end()) {
+
+               do_save = overwrite_file_dialog (prompter,
+                                                _("Confirm Snapshot Overwrite"),
+                                                _("A snapshot already exists with that name. Do you want to overwrite it?"));
+       }
+
+       if (do_save) {
+               save_state (snapname, switch_to_it);
+       }
+       else {
+               return false;
+       }
+
+       return true;
+}
+
+
 /** Ask the user for the name of a new snapshot and then take it.
  */
 
@@ -2450,7 +2492,6 @@ void
 ARDOUR_UI::snapshot_session (bool switch_to_it)
 {
        ArdourPrompter prompter (true);
-       string snapname;
 
        prompter.set_name ("Prompter");
        prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
@@ -2473,49 +2514,19 @@ ARDOUR_UI::snapshot_session (bool switch_to_it)
                prompter.set_initial_text (timebuf);
        }
 
-  again:
-       switch (prompter.run()) {
-       case RESPONSE_ACCEPT:
-       {
-               prompter.get_result (snapname);
-
-               bool do_save = (snapname.length() != 0);
-
-               if (do_save) {
-                       char illegal = Session::session_name_is_legal(snapname);
-                       if (illegal) {
-                               MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
-                                                    "snapshot names may not contain a '%1' character"), illegal));
-                               msg.run ();
-                               goto again;
-                       }
-               }
-
-               vector<std::string> p;
-               get_state_files_in_directory (_session->session_directory().root_path(), p);
-               vector<string> n = get_file_names_no_extension (p);
-               if (find (n.begin(), n.end(), snapname) != n.end()) {
-
-                       ArdourDialog confirm (_("Confirm Snapshot Overwrite"), true);
-                       Label m (_("A snapshot already exists with that name.  Do you want to overwrite it?"));
-                       confirm.get_vbox()->pack_start (m, true, true);
-                       confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-                       confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
-                       confirm.show_all ();
-                       switch (confirm.run()) {
-                       case RESPONSE_CANCEL:
-                               do_save = false;
-                       }
+       bool finished = false;
+       while (!finished) {
+               switch (prompter.run()) {
+               case RESPONSE_ACCEPT:
+               {
+                       finished = process_snapshot_session_prompter (prompter, switch_to_it);
+                       break;
                }
 
-               if (do_save) {
-                       save_state (snapname, switch_to_it);
+               default:
+                       finished = true;
+                       break;
                }
-               break;
-       }
-
-       default:
-               break;
        }
 }
 
@@ -2610,10 +2621,6 @@ ARDOUR_UI::save_state_canfail (string name, bool switch_to_it)
        if (_session) {
                int ret;
 
-               if (name.length() == 0) {
-                       name = _session->snap_name();
-               }
-
                if ((ret = _session->save_state (name, false, switch_to_it)) != 0) {
                        return ret;
                }
@@ -2674,13 +2681,39 @@ ARDOUR_UI::transport_rec_enable_blink (bool onoff)
        }
 }
 
+bool
+ARDOUR_UI::process_save_template_prompter (ArdourPrompter& prompter)
+{
+       string name;
+
+       prompter.get_result (name);
+
+       if (name.length()) {
+               int failed = _session->save_template (name);
+
+               if (failed == -2) { /* file already exists. */
+                       bool overwrite = overwrite_file_dialog (prompter,
+                                                               _("Confirm Template Overwrite"),
+                                                               _("A template already exists with that name. Do you want to overwrite it?"));
+
+                       if (overwrite) {
+                               _session->save_template (name, true);
+                       }
+                       else {
+                               return false;
+                       }
+               }
+       }
+
+       return true;
+}
+
 void
 ARDOUR_UI::save_template ()
 {
        ArdourPrompter prompter (true);
-       string name;
 
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
        }
 
@@ -2690,17 +2723,17 @@ ARDOUR_UI::save_template ()
        prompter.set_initial_text(_session->name() + _("-template"));
        prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
 
-       switch (prompter.run()) {
-       case RESPONSE_ACCEPT:
-               prompter.get_result (name);
+       bool finished = false;
+       while (!finished) {
+               switch (prompter.run()) {
+               case RESPONSE_ACCEPT:
+                       finished = process_save_template_prompter (prompter);
+                       break;
 
-               if (name.length()) {
-                       _session->save_template (name);
+               default:
+                       finished = true;
+                       break;
                }
-               break;
-
-       default:
-               break;
        }
 }
 
@@ -2800,14 +2833,49 @@ ARDOUR_UI::build_session_from_dialog (SessionDialog& sd, const std::string& sess
 void
 ARDOUR_UI::load_from_application_api (const std::string& path)
 {
+       printf("ARDOUR_UI::load_from_application_api\n");
        ARDOUR_COMMAND_LINE::session_name = path;
+       /* Cancel SessionDialog if it's visible to make OSX delegates work.
+        *
+        * ARDOUR_UI::starting connects app->ShouldLoad signal and then shows a SessionDialog
+        * race-condition:
+        *  - ShouldLoad does not arrive in time, ARDOUR_COMMAND_LINE::session_name is empty:
+        *    -> ARDOUR_UI::get_session_parameters starts a SessionDialog.
+        *  - ShouldLoad signal arrives, this function is called and sets ARDOUR_COMMAND_LINE::session_name
+        *    -> SessionDialog is not displayed
+        */
 
+       if (_session_dialog) {
+               std::string session_name = basename_nosuffix (ARDOUR_COMMAND_LINE::session_name);
+               std::string session_path = path;
+               if (Glib::file_test (session_path, Glib::FILE_TEST_IS_REGULAR)) {
+                       session_path = Glib::path_get_dirname (session_path);
+               }
+               // signal the existing dialog in ARDOUR_UI::get_session_parameters()
+               _session_dialog->set_provided_session (session_name, session_path);
+               _session_dialog->response (RESPONSE_NONE);
+               _session_dialog->hide();
+               return;
+       }
+
+       int rv;
        if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) {
                /* /path/to/foo => /path/to/foo, foo */
-               load_session (path, basename_nosuffix (path));
+               rv = load_session (path, basename_nosuffix (path));
        } else {
                /* /path/to/foo/foo.ardour => /path/to/foo, foo */
-               load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
+               rv =load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
+       }
+
+       // if load_session fails -> pop up SessionDialog.
+       if (rv) {
+               ARDOUR_COMMAND_LINE::session_name = "";
+
+               if (get_session_parameters (true, false)) {
+                       exit (1);
+               }
+
+               goto_editor_window ();
        }
 }
 
@@ -2863,6 +2931,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
 
        SessionDialog session_dialog (should_be_new, session_name, session_path, load_template, cancel_not_quit);
 
+       _session_dialog = &session_dialog;
        while (ret != 0) {
 
                if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
@@ -2896,6 +2965,10 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                        switch (session_dialog.run()) {
                        case RESPONSE_ACCEPT:
                                break;
+                       case RESPONSE_NONE:
+                               /* this is used for async * app->ShouldLoad(). */
+                               continue; // while loop
+                               break;
                        default:
                                if (quit_on_cancel) {
                                        // JE - Currently (July 2014) this section can only get reached if the
@@ -3041,13 +3114,15 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                }
        }
 
+       _session_dialog = NULL;
+
        return ret;
 }
 
 void
 ARDOUR_UI::close_session()
 {
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
        }
 
@@ -3208,7 +3283,10 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
        fst_stop_threading();
 #endif
 
-       flush_pending ();
+       {
+               Timers::TimerSuspender t;
+               flush_pending ();
+       }
 
 #ifdef WINDOWS_VST_SUPPORT
        fst_start_threading();
@@ -3291,13 +3369,24 @@ ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name,
 void
 ARDOUR_UI::launch_chat ()
 {
+       MessageDialog dialog(_("<b>Just ask and wait for an answer.\nIt may take from minutes to hours.</b>"), true);
+
+       dialog.set_title (_("About the Chat"));
+       dialog.set_secondary_text (_("When you're inside the chat just ask your question and wait for an answer. The chat is occupied by real people with real lives so many of them are passively online and might not read your question before minutes or hours later.\nSo please be patient and wait for an answer.\n\nYou should just leave the chat window open and check back regularly until someone has answered your question."));
+
+       switch (dialog.run()) {
+       case RESPONSE_OK:
 #ifdef __APPLE__
-       open_uri("http://webchat.freenode.net/?channels=ardour-osx");
+               open_uri("http://webchat.freenode.net/?channels=ardour-osx");
 #elif defined PLATFORM_WINDOWS
-       open_uri("http://webchat.freenode.net/?channels=ardour-windows");
+               open_uri("http://webchat.freenode.net/?channels=ardour-windows");
 #else
-       open_uri("http://webchat.freenode.net/?channels=ardour");
+               open_uri("http://webchat.freenode.net/?channels=ardour");
 #endif
+               break;
+       default:
+               break;
+       }
 }
 
 void
@@ -3315,7 +3404,7 @@ ARDOUR_UI::launch_reference ()
 void
 ARDOUR_UI::launch_tracker ()
 {
-       PBD::open_uri ("http://tracker.ardour.org/bug_report_page.php");
+       PBD::open_uri ("http://tracker.ardour.org");
 }
 
 void
@@ -3482,7 +3571,7 @@ The following file was deleted from %2,\n\
 releasing %3 %4bytes of disk space", "\
 The following %1 files were deleted from %2,\n\
 releasing %3 %4bytes of disk space", removed),
-                                       removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
+                                       removed, Gtkmm2ext::markup_escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
        } else {
                txt.set_markup (string_compose (P_("\
 The following file was not in use and \n\
@@ -3495,7 +3584,7 @@ have been moved to: %2\n\n\
 After a restart of %5\n\n\
 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
 will release an additional %3 %4bytes of disk space.\n", removed),
-                                       removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
+                                       removed, Gtkmm2ext::markup_escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
        }
 
        dhbox.pack_start (*dimage, true, false, 5);
@@ -3705,6 +3794,20 @@ ARDOUR_UI::setup_order_hint (AddRouteDialog::InsertAt place)
        }
 }
 
+void
+ARDOUR_UI::start_duplicate_routes ()
+{
+       if (!duplicate_routes_dialog) {
+               duplicate_routes_dialog = new DuplicateRouteDialog;
+       }
+
+       if (duplicate_routes_dialog->restart (_session)) {
+               return;
+       }
+
+       duplicate_routes_dialog->present ();
+}
+
 void
 ARDOUR_UI::add_route (Gtk::Window* /* ignored */)
 {
@@ -4445,7 +4548,7 @@ what you would like to do.\n"), PROGRAM_NAME));
 int
 ARDOUR_UI::sr_mismatch_dialog (framecnt_t desired, framecnt_t actual)
 {
-       HBox* hbox = new HBox();
+       HBox* hbox = new HBox();
        Image* image = new Image (Stock::DIALOG_WARNING, ICON_SIZE_DIALOG);
        ArdourDialog dialog (_("Sample Rate Mismatch"), true);
        Label  message (string_compose (_("\
@@ -4475,46 +4578,6 @@ audio may be played at the wrong sample rate.\n"), desired, PROGRAM_NAME, actual
         return 1;
 }
 
-int
-ARDOUR_UI::disconnect_from_engine ()
-{
-       /* drop connection to AudioEngine::Halted so that we don't act
-        *  as if the engine unexpectedly shut down
-        */
-
-       halt_connection.disconnect ();
-
-       if (AudioEngine::instance()->stop ()) {
-               MessageDialog msg (*editor, _("Could not disconnect from Audio/MIDI engine"));
-               msg.run ();
-               return -1;
-       } else {
-               AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
-       }
-
-       update_sample_rate (0);
-       return 0;
-}
-
-int
-ARDOUR_UI::reconnect_to_engine ()
-{
-       if (AudioEngine::instance()->start ()) {
-               // TODO somehow make this the topmost window (above any dialogs currently visible)
-               if (editor) {
-                       MessageDialog msg (*editor,  _("Could not reconnect to the Audio/MIDI engine"));
-                       msg.run ();
-               } else {
-                       MessageDialog msg (_("Could not reconnect to the Audio/MIDI engine"));
-                       msg.run ();
-               }
-               return -1;
-       }
-
-       update_sample_rate (0);
-       return 0;
-}
-
 void
 ARDOUR_UI::use_config ()
 {
@@ -4744,6 +4807,7 @@ ARDOUR_UI::ambiguous_file (std::string file, std::vector<std::string> hits)
        dialog.present ();
 
        dialog.run ();
+
        return dialog.get_which ();
 }
 
@@ -4833,13 +4897,21 @@ ARDOUR_UI::do_audio_midi_setup (uint32_t desired_sample_rate)
        audio_midi_setup->set_desired_sample_rate (desired_sample_rate);
        audio_midi_setup->set_position (WIN_POS_CENTER);
 
-       switch (audio_midi_setup->run()) {
-       case Gtk::RESPONSE_OK:
-               return 0;
-       case Gtk::RESPONSE_APPLY:
-               return 0;
-       default:
-               return -1;
+       int response;
+
+       while (true) {
+               response = audio_midi_setup->run();
+               switch (response) {
+               case Gtk::RESPONSE_OK:
+                       if (!AudioEngine::instance()->running()) {
+                               continue;
+                       } else {
+                               return 0;
+                       }
+                       return 0;
+               default:
+                       return -1;
+               }
        }
 }
 
@@ -4875,14 +4947,14 @@ ARDOUR_UI::transport_numpad_event (int num)
                _pending_locate_num = _pending_locate_num*10 + num;
        } else {
                switch (num) {
-                       case 0:  toggle_roll(false, false);             break;
-                       case 1:  transport_rewind(1);                           break;
-                       case 2:  transport_forward(1);                          break;
-                       case 3:  transport_record(true);                        break;
+                       case 0:  toggle_roll(false, false);             break;
+                       case 1:  transport_rewind(1);                           break;
+                       case 2:  transport_forward(1);                          break;
+                       case 3:  transport_record(true);                        break;
                        case 4:  toggle_session_auto_loop();            break;
-                       case 5:  transport_record(false); toggle_session_auto_loop();   break;
-                       case 6:  toggle_punch();                                        break;
-                       case 7:  toggle_click();                                break;
+                       case 5:  transport_record(false); toggle_session_auto_loop();   break;
+                       case 6:  toggle_punch();                                        break;
+                       case 7:  toggle_click();                                break;
                        case 8:  toggle_auto_return();                  break;
                        case 9:  toggle_follow_edits();         break;
                }
@@ -4957,3 +5029,17 @@ ARDOUR_UI::hide_application ()
 {
     Application::instance ()-> hide ();
 }
+
+void
+ARDOUR_UI::cancel_solo ()
+{
+       if (_session) {
+               if (_session->soloing()) {
+                       _session->set_solo (_session->get_routes(), false);
+               } else if (_session->listening()) {
+                       _session->set_listen (_session->get_routes(), false);
+               }
+
+               _session->clear_all_solo_state (_session->get_routes()); // safeguard, ideally this won't do anything, check the log-window
+       }
+}