correct name the event loop used by the GUI
[ardour.git] / gtk2_ardour / ardour_ui.cc
index 89f0bb4f909a9c177836f702cc998e92db39923c..10be9afb5af2441b5fcd87f16db2fe91ef09ac68 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 <glib/gstdio.h>
+#include "pbd/gstdio_compat.h"
 
 #include <gtkmm/messagedialog.h>
 #include <gtkmm/accelmap.h>
@@ -77,6 +76,7 @@
 #include "ardour/diskstream.h"
 #include "ardour/filename_extensions.h"
 #include "ardour/filesystem_paths.h"
+#include "ardour/ltc_file_reader.h"
 #include "ardour/port.h"
 #include "ardour/plugin_manager.h"
 #include "ardour/process_thread.h"
@@ -86,6 +86,7 @@
 #include "ardour/session_route.h"
 #include "ardour/session_state_utils.h"
 #include "ardour/session_utils.h"
+#include "ardour/source_factory.h"
 #include "ardour/slave.h"
 #include "ardour/system_exec.h"
 
@@ -107,8 +108,10 @@ typedef uint64_t microseconds_t;
 #include "ambiguous_file_dialog.h"
 #include "ardour_ui.h"
 #include "audio_clock.h"
+#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"
@@ -123,6 +126,7 @@ typedef uint64_t microseconds_t;
 #include "missing_file_dialog.h"
 #include "missing_plugin_dialog.h"
 #include "mixer_ui.h"
+#include "meterbridge.h"
 #include "mouse_cursors.h"
 #include "nsm.h"
 #include "opts.h"
@@ -164,8 +168,6 @@ ARDOUR_UI *ARDOUR_UI::theArdourUI = 0;
 sigc::signal<void, framepos_t, bool, framepos_t> ARDOUR_UI::Clock;
 sigc::signal<void>      ARDOUR_UI::CloseAllDialogs;
 
-float ARDOUR_UI::ui_scale = 1.0;
-
 static bool
 ask_about_configuration_copy (string const & old_dir, string const & new_dir, int version)
 {
@@ -221,10 +223,9 @@ libxml_structured_error_func (void* /* parsing_context*/,
 }
 
 
-ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfiguration* uic)
+ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
 
-       : Gtkmm2ext::UI (PROGRAM_NAME, argcp, argvp)
-       , ui_config (uic->post_gui_init ())
+       : Gtkmm2ext::UI (X_("gui"), argcp, argvp)
        , session_loaded (false)
        , gui_object_state (new GUIObjectState)
        , primary_clock   (new MainClock (X_("primary"),   X_("transport"), true ))
@@ -255,7 +256,6 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfi
        , error_alert_button ( ArdourButton::just_led_default_elements )
        , editor_meter(0)
        , editor_meter_peak_display()
-       , session_selector_window (0)
        , _numpad_locate_happening (false)
        , _session_is_new (false)
        , last_key_press_time (0)
@@ -285,9 +285,12 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfi
        , _status_bar_visibility (X_("status-bar"))
        , _feedback_exists (false)
        , _log_not_acknowledged (LogLevelNone)
+       , duplicate_routes_dialog (0)
 {
        Gtkmm2ext::init (localedir);
 
+       UIConfiguration::instance().post_gui_init ();
+
        if (ARDOUR::handle_old_configuration_files (boost::bind (ask_about_configuration_copy, _1, _2, _3))) {
                MessageDialog msg (string_compose (_("Your configuration files were copied. You can now restart %1."), PROGRAM_NAME), true);
                msg.run ();
@@ -304,9 +307,9 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfi
        xmlSetGenericErrorFunc (this, libxml_generic_error_func);
        xmlSetStructuredErrorFunc (this, libxml_structured_error_func);
 
-       ui_config->ParameterChanged.connect (sigc::mem_fun (*this, &ARDOUR_UI::parameter_changed));
+       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));
-       ui_config->map_parameters (pc);
+       UIConfiguration::instance().map_parameters (pc);
 
        roll_button.set_controllable (roll_controllable);
        stop_button.set_controllable (stop_controllable);
@@ -391,7 +394,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfi
        /* we don't like certain modifiers */
        Bindings::set_ignored_state (GDK_LOCK_MASK|GDK_MOD2_MASK|GDK_MOD3_MASK);
 
-       reset_dpi();
+       UIConfiguration::instance().reset_dpi ();
 
        TimeAxisViewItem::set_constant_heights ();
 
@@ -438,12 +441,12 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir, UIConfi
 
        /* Trigger setting up the color scheme and loading the GTK RC file */
 
-       ARDOUR_UI::config()->load_rc_file (false);
+       UIConfiguration::instance().load_rc_file (false);
 
        _process_thread = new ProcessThread ();
        _process_thread->init ();
 
-       DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
+       UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
 
        attach_to_engine ();
 }
@@ -470,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;
@@ -488,6 +494,9 @@ ARDOUR_UI::engine_running ()
        update_xrun_count ();
        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
@@ -557,8 +566,6 @@ ARDOUR_UI::post_engine ()
        AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
        AudioEngine::instance()->BecameSilent.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::audioengine_became_silent, this), gui_context());
 
-       _tooltips.enable();
-
        if (setup_windows ()) {
                throw failed_constructor ();
        }
@@ -623,13 +630,13 @@ ARDOUR_UI::post_engine ()
                boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
                Config->map_parameters (pc);
 
-               ui_config->map_parameters (pc);
+               UIConfiguration::instance().map_parameters (pc);
        }
 }
 
 ARDOUR_UI::~ARDOUR_UI ()
 {
-       ui_config->save_state();
+       UIConfiguration::instance().save_state();
 
        stop_video_server();
 
@@ -1260,6 +1267,7 @@ ARDOUR_UI::every_second ()
        update_buffer_load ();
        update_disk_space ();
        update_timecode_format ();
+       update_peak_thread_work ();
 
        if (nsm && nsm->is_active ()) {
                nsm->check ();
@@ -1287,10 +1295,10 @@ ARDOUR_UI::every_point_zero_something_seconds ()
 {
        // august 2007: actual update frequency: 25Hz (40ms), not 100Hz
 
-       if (editor_meter && ARDOUR_UI::config()->get_show_editor_meter()) {
+       if (editor_meter && UIConfiguration::instance().get_show_editor_meter()) {
                float mpeak = editor_meter->update_meters();
                if (mpeak > editor_meter_max_peak) {
-                       if (mpeak >= ARDOUR_UI::config()->get_meter_peak()) {
+                       if (mpeak >= UIConfiguration::instance().get_meter_peak()) {
                                editor_meter_peak_display.set_active_state ( Gtkmm2ext::ExplicitActive );
                        }
                }
@@ -1460,6 +1468,19 @@ ARDOUR_UI::update_cpu_load ()
        cpu_load_label.set_markup (buf);
 }
 
+void
+ARDOUR_UI::update_peak_thread_work ()
+{
+       char buf[64];
+       const int c = SourceFactory::peak_work_queue_length ();
+       if (c > 0) {
+               snprintf (buf, sizeof (buf), _("PkBld: <span foreground=\"%s\">%d</span>"), c >= 2 ? X_("red") : X_("green"), c);
+               peak_thread_work_label.set_markup (buf);
+       } else {
+               peak_thread_work_label.set_markup (X_(""));
+       }
+}
+
 void
 ARDOUR_UI::update_buffer_load ()
 {
@@ -1602,171 +1623,40 @@ ARDOUR_UI::update_wall_clock ()
        return TRUE;
 }
 
-void
-ARDOUR_UI::redisplay_recent_sessions ()
-{
-       std::vector<std::string> session_directories;
-       RecentSessionsSorter cmp;
-
-       recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
-       recent_session_model->clear ();
-
-       ARDOUR::RecentSessions rs;
-       ARDOUR::read_recent_sessions (rs);
-
-       if (rs.empty()) {
-               recent_session_display.set_model (recent_session_model);
-               return;
-       }
-
-       // sort them alphabetically
-       sort (rs.begin(), rs.end(), cmp);
-
-       for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
-               session_directories.push_back ((*i).second);
-       }
-
-       for (vector<std::string>::const_iterator i = session_directories.begin();
-                       i != session_directories.end(); ++i)
-       {
-               std::vector<std::string> state_file_paths;
-
-               // now get available states for this session
-
-               get_state_files_in_directory (*i, state_file_paths);
-
-               vector<string> states;
-               vector<const gchar*> item;
-               string fullpath = *i;
-
-               /* remove any trailing / */
-
-               if (fullpath[fullpath.length() - 1] == '/') {
-                       fullpath = fullpath.substr (0, fullpath.length() - 1);
-               }
-
-               /* check whether session still exists */
-               if (!Glib::file_test(fullpath.c_str(), Glib::FILE_TEST_EXISTS)) {
-                       /* session doesn't exist */
-                       continue;
-               }
-
-               /* now get available states for this session */
-               states = Session::possible_states (fullpath);
-
-               if (states.empty()) {
-                       /* no state file? */
-                       continue;
-               }
-
-               std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
-
-               Gtk::TreeModel::Row row = *(recent_session_model->append());
-
-               row[recent_session_columns.fullpath] = fullpath;
-               row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
-
-               if (state_file_names.size() > 1) {
-                       // multiple session files in the session directory - show the directory name.
-                       row[recent_session_columns.visible_name] = Glib::path_get_basename (fullpath);
-
-                       // add the children
-                       for (std::vector<std::string>::iterator i2 = state_file_names.begin();
-                                       i2 != state_file_names.end(); ++i2)
-                       {
-
-                               Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
-
-                               child_row[recent_session_columns.visible_name] = *i2;
-                               child_row[recent_session_columns.fullpath] = fullpath;
-                               child_row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
-                       }
-               } else {
-                       // only a single session file in the directory - show its actual name.
-                       row[recent_session_columns.visible_name] = state_file_names.front ();
-               }
-       }
-
-       recent_session_display.set_tooltip_column(1); // recent_session_columns.tip
-       recent_session_display.set_model (recent_session_model);
-}
-
-void
-ARDOUR_UI::build_session_selector ()
-{
-       session_selector_window = new ArdourDialog (_("Recent Sessions"));
-
-       Gtk::ScrolledWindow *scroller = manage (new Gtk::ScrolledWindow);
-
-       session_selector_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
-       session_selector_window->add_button (Stock::OPEN, RESPONSE_ACCEPT);
-       session_selector_window->set_default_response (RESPONSE_ACCEPT);
-       recent_session_model = TreeStore::create (recent_session_columns);
-       recent_session_display.set_model (recent_session_model);
-       recent_session_display.append_column (_("Recent Sessions"), recent_session_columns.visible_name);
-       recent_session_display.set_headers_visible (false);
-       recent_session_display.get_selection()->set_mode (SELECTION_BROWSE);
-       recent_session_display.signal_row_activated().connect (sigc::mem_fun (*this, &ARDOUR_UI::recent_session_row_activated));
-
-       scroller->add (recent_session_display);
-       scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
-
-       session_selector_window->set_name ("SessionSelectorWindow");
-       session_selector_window->set_size_request (200, 400);
-       session_selector_window->get_vbox()->pack_start (*scroller);
-
-       recent_session_display.show();
-       scroller->show();
-}
-
-void
-ARDOUR_UI::recent_session_row_activated (const TreePath& /*path*/, TreeViewColumn* /*col*/)
-{
-       session_selector_window->response (RESPONSE_ACCEPT);
-}
-
 void
 ARDOUR_UI::open_recent_session ()
 {
        bool can_return = (_session != 0);
 
-       if (session_selector_window == 0) {
-               build_session_selector ();
-       }
-
-       redisplay_recent_sessions ();
+       SessionDialog recent_session_dialog;
 
        while (true) {
 
-               ResponseType r = (ResponseType) session_selector_window->run ();
+               ResponseType r = (ResponseType) recent_session_dialog.run ();
 
                switch (r) {
                case RESPONSE_ACCEPT:
                        break;
                default:
                        if (can_return) {
-                               session_selector_window->hide();
+                               recent_session_dialog.hide();
                                return;
                        } else {
                                exit (1);
                        }
                }
 
-               if (recent_session_display.get_selection()->count_selected_rows() == 0) {
-                       continue;
-               }
+               recent_session_dialog.hide();
 
-               session_selector_window->hide();
+               bool should_be_new;
 
-               Gtk::TreeModel::iterator i = recent_session_display.get_selection()->get_selected();
+               std::string path = recent_session_dialog.session_folder();
+               std::string state = recent_session_dialog.session_name (should_be_new);
 
-               if (i == recent_session_model->children().end()) {
-                       return;
+               if (should_be_new == true) {
+                       continue;
                }
 
-               std::string path = (*i)[recent_session_columns.fullpath];
-               std::string state = (*i)[recent_session_columns.visible_name];
-
                _session_is_new = false;
 
                if (load_session (path, state) == 0) {
@@ -1778,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));
@@ -1795,9 +1685,8 @@ ARDOUR_UI::check_audioengine ()
 void
 ARDOUR_UI::open_session ()
 {
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
-
        }
 
        /* ardour sessions are folders */
@@ -1806,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);
@@ -1815,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);
        }
@@ -2196,17 +2081,26 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
        if (rolling && roll_out_of_bounded_mode) {
                /* drop out of loop/range playback but leave transport rolling */
                if (_session->get_play_loop()) {
-                       if (Config->get_seamless_loop()) {
-                               /* the disk buffers contain copies of the loop - we can't
-                                  just keep playing, so stop the transport. the user
-                                  can restart as they wish.
-                               */
-                               affect_transport = true;
+                       if (_session->actively_recording()) {
+
+                               /* just stop using the loop, then actually stop
+                                * below
+                                */
+                               _session->request_play_loop (false, affect_transport);
+
                        } else {
-                               /* disk buffers are normal, so we can keep playing */
-                               affect_transport = false;
+                               if (Config->get_seamless_loop()) {
+                                       /* the disk buffers contain copies of the loop - we can't
+                                          just keep playing, so stop the transport. the user
+                                          can restart as they wish.
+                                       */
+                                       affect_transport = true;
+                               } else {
+                                       /* disk buffers are normal, so we can keep playing */
+                                       affect_transport = false;
+                               }
+                               _session->request_play_loop (false, affect_transport);
                        }
-                       _session->request_play_loop (false, affect_transport);
                } else if (_session->get_play_range ()) {
                        affect_transport = false;
                        _session->request_play_range (0, true);
@@ -2216,8 +2110,8 @@ 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 {
-                       if (ARDOUR_UI::config()->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
+               } 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
                        }
@@ -2229,9 +2123,13 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
 void
 ARDOUR_UI::toggle_session_auto_loop ()
 {
+       if (!_session) {
+               return;
+       }
+
        Location * looploc = _session->locations()->auto_loop_location();
 
-       if (!_session || !looploc) {
+       if (!looploc) {
                return;
        }
 
@@ -2284,7 +2182,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) {
@@ -2394,7 +2292,7 @@ ARDOUR_UI::map_transport_state ()
                        auto_loop_button.set_active (false);
                }
 
-               if (ARDOUR_UI::config()->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);
@@ -2440,7 +2338,7 @@ ARDOUR_UI::update_clocks ()
 void
 ARDOUR_UI::start_clocking ()
 {
-       if (ui_config->get_super_rapid_clock_update()) {
+       if (UIConfiguration::instance().get_super_rapid_clock_update()) {
                clock_signal_connection = Timers::fps_connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
        } else {
                clock_signal_connection = Timers::rapid_connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
@@ -2497,7 +2395,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 ();
@@ -2550,6 +2448,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.
  */
 
@@ -2557,7 +2496,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);
@@ -2580,49 +2518,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;
        }
 }
 
@@ -2692,6 +2600,10 @@ ARDOUR_UI::rename_session ()
 void
 ARDOUR_UI::save_state (const string & name, bool switch_to_it)
 {
+       if (!_session || _session->deletion_in_progress()) {
+               return;
+       }
+
        XMLNode* node = new XMLNode (X_("UI"));
 
        WM::Manager::instance().add_state (*node);
@@ -2713,10 +2625,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;
                }
@@ -2777,13 +2685,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;
        }
 
@@ -2793,17 +2727,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;
        }
 }
 
@@ -2903,14 +2837,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 ();
        }
 }
 
@@ -2966,6 +2935,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()) {
@@ -2999,6 +2969,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
@@ -3144,13 +3118,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;
        }
 
@@ -3311,7 +3287,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();
@@ -3394,13 +3373,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
@@ -3418,7 +3408,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
@@ -3585,7 +3575,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\
@@ -3598,7 +3588,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);
@@ -3712,6 +3702,47 @@ ARDOUR_UI::flush_trash ()
        display_cleanup_results (rep, _("deleted file"), true);
 }
 
+void
+ARDOUR_UI::cleanup_peakfiles ()
+{
+       if (_session == 0) {
+               /* shouldn't happen: menu item is insensitive */
+               return;
+       }
+
+       if (! _session->can_cleanup_peakfiles ()) {
+               return;
+       }
+
+       // get all region-views in this session
+       RegionSelection rs;
+       TrackViewList empty;
+       empty.clear();
+       editor->get_regions_after(rs, (framepos_t) 0, empty);
+       std::list<RegionView*> views = rs.by_layer();
+
+       // remove displayed audio-region-views waveforms
+       for (list<RegionView*>::iterator i = views.begin(); i != views.end(); ++i) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
+               if (!arv) { continue ; }
+               arv->delete_waves();
+       }
+
+       // cleanup peak files:
+       // - stop pending peakfile threads
+       // - close peakfiles if any
+       // - remove peak dir in session
+       // - setup peakfiles (background thread)
+       _session->cleanup_peakfiles ();
+
+       // re-add waves to ARV
+       for (list<RegionView*>::iterator i = views.begin(); i != views.end(); ++i) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
+               if (!arv) { continue ; }
+               arv->create_waves();
+       }
+}
+
 void
 ARDOUR_UI::setup_order_hint (AddRouteDialog::InsertAt place)
 {
@@ -3767,6 +3798,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 */)
 {
@@ -4030,6 +4075,9 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                return;
        }
 
+       std::string audio_from_video;
+       bool detect_ltc = false;
+
        switch (add_video_dialog->import_option()) {
                case VTL_IMPORT_TRANSCODE:
                        {
@@ -4041,9 +4089,15 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                                        delete transcode_video_dialog;
                                        return;
                                }
-                               if (!transcode_video_dialog->get_audiofile().empty()) {
+
+                               audio_from_video = transcode_video_dialog->get_audiofile();
+
+                               if (!audio_from_video.empty() && transcode_video_dialog->detect_ltc()) {
+                                       detect_ltc = true;
+                               }
+                               else if (!audio_from_video.empty()) {
                                        editor->embed_audio_from_video(
-                                                       transcode_video_dialog->get_audiofile(),
+                                                       audio_from_video,
                                                        video_timeline->get_offset(),
                                                        (transcode_video_dialog->import_option() != VTL_IMPORT_NO_VIDEO)
                                                        );
@@ -4076,6 +4130,7 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
        }
 
        video_timeline->set_update_session_fps(auto_set_session_fps);
+
        if (video_timeline->video_file_info(path, local_file)) {
                XMLNode* node = new XMLNode(X_("Videotimeline"));
                node->add_property (X_("Filename"), path);
@@ -4089,6 +4144,40 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                _session->add_extra_xml (*node);
                _session->set_dirty ();
 
+               if (!audio_from_video.empty() && detect_ltc) {
+                       std::vector<LTCFileReader::LTCMap> ltc_seq;
+
+                       try {
+                               /* TODO ask user about TV standard (LTC alignment if any) */
+                               LTCFileReader ltcr (audio_from_video, video_timeline->get_video_file_fps());
+                               /* TODO ASK user which channel:  0 .. ltcr->channels() - 1 */
+
+                               ltc_seq = ltcr.read_ltc (/*channel*/ 0, /*max LTC frames to decode*/ 15);
+
+                               /* TODO seek near end of file, and read LTC until end.
+                                * if it fails to find any LTC frames, scan complete file
+                                *
+                                * calculate drift of LTC compared to video-duration,
+                                * ask user for reference (timecode from start/mid/end)
+                                */
+                       } catch (...) {
+                               // LTCFileReader will have written error messages
+                       }
+
+                       ::g_unlink(audio_from_video.c_str());
+
+                       if (ltc_seq.size() == 0) {
+                               PBD::error << _("No LTC detected, video will not be aligned.") << endmsg;
+                       } else {
+                               /* the very first TC in the file is somteimes not aligned properly */
+                               int i = ltc_seq.size() -1;
+                               ARDOUR::frameoffset_t video_start_offset =
+                                       _session->nominal_frame_rate() * (ltc_seq[i].timecode_sec - ltc_seq[i].framepos_sec);
+                               PBD::info << string_compose (_("Align video-start to %1 [samples]"), video_start_offset) << endmsg;
+                               video_timeline->set_offset(video_start_offset);
+                       }
+               }
+
                _session->maybe_update_session_range(
                        std::max(video_timeline->get_offset(), (ARDOUR::frameoffset_t) 0),
                        std::max(video_timeline->get_offset() + video_timeline->get_duration(), (ARDOUR::frameoffset_t) 0));
@@ -4313,7 +4402,7 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
        }
 
        const bool cancelled = PluginManager::instance().cancelled();
-       if (type != X_("closeme") && (!ui_config->get_show_plugin_scan_window()) && !_initial_verbose_plugin_scan) {
+       if (type != X_("closeme") && (!UIConfiguration::instance().get_show_plugin_scan_window()) && !_initial_verbose_plugin_scan) {
                if (cancelled && scan_dlg->is_mapped()) {
                        scan_dlg->hide();
                        gui_idle_handler();
@@ -4463,7 +4552,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 (_("\
@@ -4493,46 +4582,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 ()
 {
@@ -4545,13 +4594,13 @@ ARDOUR_UI::use_config ()
 void
 ARDOUR_UI::update_transport_clocks (framepos_t pos)
 {
-       if (ui_config->get_primary_clock_delta_edit_cursor()) {
+       if (UIConfiguration::instance().get_primary_clock_delta_edit_cursor()) {
                primary_clock->set (pos, false, editor->get_preferred_edit_position (EDIT_IGNORE_PHEAD));
        } else {
                primary_clock->set (pos);
        }
 
-       if (ui_config->get_secondary_clock_delta_edit_cursor()) {
+       if (UIConfiguration::instance().get_secondary_clock_delta_edit_cursor()) {
                secondary_clock->set (pos, false, editor->get_preferred_edit_position (EDIT_IGNORE_PHEAD));
        } else {
                secondary_clock->set (pos);
@@ -4762,6 +4811,7 @@ ARDOUR_UI::ambiguous_file (std::string file, std::vector<std::string> hits)
        dialog.present ();
 
        dialog.run ();
+
        return dialog.get_which ();
 }
 
@@ -4851,13 +4901,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;
+               }
        }
 }
 
@@ -4893,14 +4951,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;
                }
@@ -4910,7 +4968,7 @@ ARDOUR_UI::transport_numpad_event (int num)
 void
 ARDOUR_UI::set_flat_buttons ()
 {
-       CairoWidget::set_flat_buttons( config()->get_flat_buttons() );
+       CairoWidget::set_flat_buttons( UIConfiguration::instance().get_flat_buttons() );
 }
 
 void
@@ -4975,3 +5033,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
+       }
+}