add code to display announcements, and parameterize several URL's used in the program
[ardour.git] / gtk2_ardour / ardour_ui.cc
1 /*
2     Copyright (C) 1999-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <stdint.h>
25
26 #include <algorithm>
27 #include <cmath>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <cerrno>
33 #include <fstream>
34
35 #include <iostream>
36
37 #include <sys/resource.h>
38 #include <sys/types.h>
39 #include <sys/sysctl.h>
40
41 #include <gtkmm/messagedialog.h>
42 #include <gtkmm/accelmap.h>
43
44 #include "pbd/error.h"
45 #include "pbd/basename.h"
46 #include "pbd/compose.h"
47 #include "pbd/failed_constructor.h"
48 #include "pbd/enumwriter.h"
49 #include "pbd/memento_command.h"
50 #include "pbd/openuri.h"
51 #include "pbd/file_utils.h"
52
53 #include "gtkmm2ext/application.h"
54 #include "gtkmm2ext/bindings.h"
55 #include "gtkmm2ext/gtk_ui.h"
56 #include "gtkmm2ext/utils.h"
57 #include "gtkmm2ext/click_box.h"
58 #include "gtkmm2ext/fastmeter.h"
59 #include "gtkmm2ext/popup.h"
60 #include "gtkmm2ext/window_title.h"
61
62 #include "midi++/manager.h"
63
64 #include "ardour/ardour.h"
65 #include "ardour/audioengine.h"
66 #include "ardour/audiofilesource.h"
67 #include "ardour/automation_watch.h"
68 #include "ardour/diskstream.h"
69 #include "ardour/filename_extensions.h"
70 #include "ardour/filesystem_paths.h"
71 #include "ardour/port.h"
72 #include "ardour/process_thread.h"
73 #include "ardour/profile.h"
74 #include "ardour/recent_sessions.h"
75 #include "ardour/session_directory.h"
76 #include "ardour/session_route.h"
77 #include "ardour/session_state_utils.h"
78 #include "ardour/session_utils.h"
79 #include "ardour/slave.h"
80
81 #include "timecode/time.h"
82
83 typedef uint64_t microseconds_t;
84
85 #include "about.h"
86 #include "actions.h"
87 #include "add_route_dialog.h"
88 #include "ambiguous_file_dialog.h"
89 #include "ardour_ui.h"
90 #include "audio_clock.h"
91 #include "bundle_manager.h"
92 #include "engine_dialog.h"
93 #include "gain_meter.h"
94 #include "global_port_matrix.h"
95 #include "gui_object.h"
96 #include "gui_thread.h"
97 #include "keyboard.h"
98 #include "location_ui.h"
99 #include "main_clock.h"
100 #include "missing_file_dialog.h"
101 #include "missing_plugin_dialog.h"
102 #include "mixer_ui.h"
103 #include "mouse_cursors.h"
104 #include "opts.h"
105 #include "pingback.h"
106 #include "processor_box.h"
107 #include "prompter.h"
108 #include "public_editor.h"
109 #include "route_time_axis.h"
110 #include "session_metadata_dialog.h"
111 #include "shuttle_control.h"
112 #include "speaker_dialog.h"
113 #include "splash.h"
114 #include "startup.h"
115 #include "theme_manager.h"
116 #include "time_axis_view_item.h"
117 #include "utils.h"
118 #include "window_proxy.h"
119
120 #include "i18n.h"
121
122 using namespace ARDOUR;
123 using namespace PBD;
124 using namespace Gtkmm2ext;
125 using namespace Gtk;
126 using namespace std;
127
128 ARDOUR_UI *ARDOUR_UI::theArdourUI = 0;
129 UIConfiguration *ARDOUR_UI::ui_config = 0;
130
131 sigc::signal<void,bool> ARDOUR_UI::Blink;
132 sigc::signal<void>      ARDOUR_UI::RapidScreenUpdate;
133 sigc::signal<void>      ARDOUR_UI::SuperRapidScreenUpdate;
134 sigc::signal<void, framepos_t, bool, framepos_t> ARDOUR_UI::Clock;
135
136 ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
137
138         : Gtkmm2ext::UI (PROGRAM_NAME, argcp, argvp)
139
140         , gui_object_state (new GUIObjectState)
141         , primary_clock (new MainClock (X_("primary"), false, X_("transport"), true, true, true, false, true))
142         , secondary_clock (new MainClock (X_("secondary"), false, X_("secondary"), true, true, false, false, true))
143
144           /* big clock */
145
146         , big_clock (new AudioClock (X_("bigclock"), false, "big", true, true, false, false))
147
148           /* transport */
149
150         , roll_controllable (new TransportControllable ("transport roll", *this, TransportControllable::Roll))
151         , stop_controllable (new TransportControllable ("transport stop", *this, TransportControllable::Stop))
152         , goto_start_controllable (new TransportControllable ("transport goto start", *this, TransportControllable::GotoStart))
153         , goto_end_controllable (new TransportControllable ("transport goto end", *this, TransportControllable::GotoEnd))
154         , auto_loop_controllable (new TransportControllable ("transport auto loop", *this, TransportControllable::AutoLoop))
155         , play_selection_controllable (new TransportControllable ("transport play selection", *this, TransportControllable::PlaySelection))
156         , rec_controllable (new TransportControllable ("transport rec-enable", *this, TransportControllable::RecordEnable))
157
158         , auto_return_button (ArdourButton::led_default_elements)
159         , follow_edits_button (ArdourButton::led_default_elements)
160         , auto_input_button (ArdourButton::led_default_elements)
161
162         , auditioning_alert_button (_("audition"))
163         , solo_alert_button (_("solo"))
164         , feedback_alert_button (_("feedback"))
165
166         , error_log_button (_("Errors"))
167
168         , _status_bar_visibility (X_("status-bar"))
169         , _feedback_exists (false)
170
171 {
172         Gtkmm2ext::init(localedir);
173
174         about = 0;
175         splash = 0;
176         _startup = 0;
177
178         if (theArdourUI == 0) {
179                 theArdourUI = this;
180         }
181
182         ui_config = new UIConfiguration();
183         theme_manager = new ThemeManager();
184
185         key_editor = 0;
186
187         editor = 0;
188         mixer = 0;
189         editor = 0;
190         engine = 0;
191         _session_is_new = false;
192         big_clock_window = 0;
193         big_clock_height = 0;
194         big_clock_resize_in_progress = false;
195         session_selector_window = 0;
196         last_key_press_time = 0;
197         add_route_dialog = 0;
198         route_params = 0;
199         bundle_manager = 0;
200         rc_option_editor = 0;
201         session_option_editor = 0;
202         location_ui = 0;
203         open_session_selector = 0;
204         have_configure_timeout = false;
205         have_disk_speed_dialog_displayed = false;
206         session_loaded = false;
207         ignore_dual_punch = false;
208         original_big_clock_width = -1;
209         original_big_clock_height = -1;
210         original_big_clock_font_size = 0;
211
212         roll_button.set_controllable (roll_controllable);
213         stop_button.set_controllable (stop_controllable);
214         goto_start_button.set_controllable (goto_start_controllable);
215         goto_end_button.set_controllable (goto_end_controllable);
216         auto_loop_button.set_controllable (auto_loop_controllable);
217         play_selection_button.set_controllable (play_selection_controllable);
218         rec_button.set_controllable (rec_controllable);
219
220         roll_button.set_name ("transport button");
221         stop_button.set_name ("transport button");
222         goto_start_button.set_name ("transport button");
223         goto_end_button.set_name ("transport button");
224         auto_loop_button.set_name ("transport button");
225         play_selection_button.set_name ("transport button");
226         rec_button.set_name ("transport recenable button");
227         midi_panic_button.set_name ("transport button");
228
229         goto_start_button.set_tweaks (ArdourButton::ShowClick);
230         goto_end_button.set_tweaks (ArdourButton::ShowClick);
231         midi_panic_button.set_tweaks (ArdourButton::ShowClick);
232         
233         last_configure_time= 0;
234         last_peak_grab = 0;
235
236         ARDOUR::Diskstream::DiskOverrun.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::disk_overrun_handler, this), gui_context());
237         ARDOUR::Diskstream::DiskUnderrun.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::disk_underrun_handler, this), gui_context());
238
239         ARDOUR::Session::VersionMismatch.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::session_format_mismatch, this, _1, _2), gui_context());
240
241         /* handle dialog requests */
242
243         ARDOUR::Session::Dialog.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::session_dialog, this, _1), gui_context());
244
245         /* handle pending state with a dialog (PROBLEM: needs to return a value and thus cannot be x-thread) */
246
247         ARDOUR::Session::AskAboutPendingState.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::pending_state_dialog, this));
248
249         /* handle sr mismatch with a dialog (PROBLEM: needs to return a value and thus cannot be x-thread) */
250
251         ARDOUR::Session::AskAboutSampleRateMismatch.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::sr_mismatch_dialog, this, _1, _2));
252
253         /* handle requests to quit (coming from JACK session) */
254
255         ARDOUR::Session::Quit.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::finish, this), gui_context ());
256
257         /* tell the user about feedback */
258
259         ARDOUR::Session::FeedbackDetected.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::feedback_detected, this), gui_context ());
260         ARDOUR::Session::SuccessfulGraphSort.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::successful_graph_sort, this), gui_context ());
261
262         /* handle requests to deal with missing files */
263
264         ARDOUR::Session::MissingFile.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::missing_file, this, _1, _2, _3));
265
266         /* and ambiguous files */
267
268         ARDOUR::FileSource::AmbiguousFileName.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::ambiguous_file, this, _1, _2, _3));
269
270         /* lets get this party started */
271
272         try {
273                 if (ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir)) {
274                         throw failed_constructor ();
275                 }
276
277                 setup_gtk_ardour_enums ();
278                 setup_profile ();
279
280                 SessionEvent::create_per_thread_pool ("GUI", 512);
281
282         } catch (failed_constructor& err) {
283                 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
284                 // pass it on up
285                 throw;
286         }
287
288         /* we like keyboards */
289
290         keyboard = new ArdourKeyboard(*this);
291
292         XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
293         if (node) {
294                 keyboard->set_state (*node, Stateful::loading_state_version);
295         }
296
297         /* we don't like certain modifiers */
298         Bindings::set_ignored_state (GDK_LOCK_MASK|GDK_MOD2_MASK|GDK_MOD3_MASK);
299
300         reset_dpi();
301
302         TimeAxisViewItem::set_constant_heights ();
303
304         /* The following must happen after ARDOUR::init() so that Config is set up */
305
306         location_ui = new ActionWindowProxy<LocationUIWindow> (X_("locations"), Config->extra_xml (X_("UI")), X_("ToggleLocations"));
307         big_clock_window = new ActionWindowProxy<Gtk::Window> (X_("bigclock"), Config->extra_xml (X_("UI")), X_("ToggleBigClock"));
308         speaker_config_window = new ActionWindowProxy<SpeakerDialog> (X_("speakerconf"), Config->extra_xml (X_("UI")), X_("toggle-speaker-config"));
309
310         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
311                 _global_port_matrix[*i] = new ActionWindowProxy<GlobalPortMatrixWindow> (
312                         string_compose ("GlobalPortMatrix-%1", (*i).to_string()),
313                         Config->extra_xml (X_("UI")),
314                         string_compose ("toggle-%1-connection-manager", (*i).to_string())
315                         );
316         }
317
318         setup_clock ();
319
320         SpeakerDialog* s = new SpeakerDialog ();
321         s->signal_unmap().connect (sigc::bind (sigc::ptr_fun (&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/toggle-speaker-config")));
322         speaker_config_window->set (s);
323
324         starting.connect (sigc::mem_fun(*this, &ARDOUR_UI::startup));
325         stopping.connect (sigc::mem_fun(*this, &ARDOUR_UI::shutdown));
326
327         _process_thread = new ProcessThread ();
328         _process_thread->init ();
329
330         DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
331 }
332
333
334 int
335 ARDOUR_UI::create_engine ()
336 {
337         // this gets called every time by new_session()
338
339         if (engine) {
340                 return 0;
341         }
342
343         loading_message (_("Starting audio engine"));
344
345         try {
346                 engine = new ARDOUR::AudioEngine (ARDOUR_COMMAND_LINE::jack_client_name, ARDOUR_COMMAND_LINE::jack_session_uuid);
347
348         } catch (...) {
349
350                 return -1;
351         }
352
353         engine->Stopped.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_stopped, this), gui_context());
354         engine->Running.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_running, this), gui_context());
355         engine->SampleRateChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::update_sample_rate, this, _1), gui_context());
356
357         engine->Halted.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
358
359         ARDOUR::Port::set_connecting_blocked (ARDOUR_COMMAND_LINE::no_connect_ports);
360
361         post_engine ();
362
363         return 0;
364 }
365
366 void
367 ARDOUR_UI::post_engine ()
368 {
369         /* Things to be done once we create the AudioEngine
370          */
371
372         ARDOUR::init_post_engine ();
373
374         /* load up the UI manager */
375
376         ActionManager::init ();
377
378         _tooltips.enable();
379
380         if (setup_windows ()) {
381                 throw failed_constructor ();
382         }
383
384         /* Do this after setup_windows (), as that's when the _status_bar_visibility is created */
385         XMLNode* n = Config->extra_xml (X_("UI"));
386         if (n) {
387                 _status_bar_visibility.set_state (*n);
388         }
389         
390         check_memory_locking();
391
392         /* this is the first point at which all the keybindings are available */
393
394         if (ARDOUR_COMMAND_LINE::show_key_actions) {
395                 vector<string> names;
396                 vector<string> paths;
397                 vector<string> tooltips;
398                 vector<string> keys;
399                 vector<AccelKey> bindings;
400
401                 ActionManager::get_all_actions (names, paths, tooltips, keys, bindings);
402
403                 vector<string>::iterator n;
404                 vector<string>::iterator k;
405                 for (n = names.begin(), k = keys.begin(); n != names.end(); ++n, ++k) {
406                         cout << "Action: " << (*n) << " bound to " << (*k) << endl;
407                 }
408
409                 exit (0);
410         }
411
412         blink_timeout_tag = -1;
413
414         /* this being a GUI and all, we want peakfiles */
415
416         AudioFileSource::set_build_peakfiles (true);
417         AudioFileSource::set_build_missing_peakfiles (true);
418
419         /* set default clock modes */
420
421         if (Profile->get_sae()) {
422                 primary_clock->set_mode (AudioClock::BBT);
423                 secondary_clock->set_mode (AudioClock::MinSec);
424         }  else {
425                 primary_clock->set_mode (AudioClock::Timecode);
426                 secondary_clock->set_mode (AudioClock::BBT);
427         }
428
429         /* start the time-of-day-clock */
430
431 #ifndef GTKOSX
432         /* OS X provides a nearly-always visible wallclock, so don't be stupid */
433         update_wall_clock ();
434         Glib::signal_timeout().connect_seconds (sigc::mem_fun(*this, &ARDOUR_UI::update_wall_clock), 1);
435 #endif
436
437         update_disk_space ();
438         update_cpu_load ();
439         update_sample_rate (engine->frame_rate());
440         update_timecode_format ();
441
442         Config->ParameterChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::parameter_changed, this, _1), gui_context());
443         boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
444         Config->map_parameters (pc);
445
446         /* now start and maybe save state */
447
448         if (do_engine_start () == 0) {
449                 if (_session && _session_is_new) {
450                         /* we need to retain initial visual
451                            settings for a new session
452                         */
453                         _session->save_state ("");
454                 }
455         }
456 }
457
458 ARDOUR_UI::~ARDOUR_UI ()
459 {
460         delete keyboard;
461         delete editor;
462         delete mixer;
463         delete add_route_dialog;
464 }
465
466 void
467 ARDOUR_UI::pop_back_splash (Gtk::Window& win)
468 {
469         if (Splash::instance()) {
470                 Splash::instance()->pop_back_for (win);
471         }
472 }
473
474 gint
475 ARDOUR_UI::configure_timeout ()
476 {
477         if (last_configure_time == 0) {
478                 /* no configure events yet */
479                 return true;
480         }
481
482         /* force a gap of 0.5 seconds since the last configure event
483          */
484
485         if (get_microseconds() - last_configure_time < 500000) {
486                 return true;
487         } else {
488                 have_configure_timeout = false;
489                 save_ardour_state ();
490                 return false;
491         }
492 }
493
494 gboolean
495 ARDOUR_UI::configure_handler (GdkEventConfigure* /*conf*/)
496 {
497         if (have_configure_timeout) {
498                 last_configure_time = get_microseconds();
499         } else {
500                 Glib::signal_timeout().connect (sigc::mem_fun(*this, &ARDOUR_UI::configure_timeout), 100);
501                 have_configure_timeout = true;
502         }
503
504         return FALSE;
505 }
506
507 void
508 ARDOUR_UI::set_transport_controllable_state (const XMLNode& node)
509 {
510         const XMLProperty* prop;
511
512         if ((prop = node.property ("roll")) != 0) {
513                 roll_controllable->set_id (prop->value());
514         }
515         if ((prop = node.property ("stop")) != 0) {
516                 stop_controllable->set_id (prop->value());
517         }
518         if ((prop = node.property ("goto-start")) != 0) {
519                 goto_start_controllable->set_id (prop->value());
520         }
521         if ((prop = node.property ("goto-end")) != 0) {
522                 goto_end_controllable->set_id (prop->value());
523         }
524         if ((prop = node.property ("auto-loop")) != 0) {
525                 auto_loop_controllable->set_id (prop->value());
526         }
527         if ((prop = node.property ("play-selection")) != 0) {
528                 play_selection_controllable->set_id (prop->value());
529         }
530         if ((prop = node.property ("rec")) != 0) {
531                 rec_controllable->set_id (prop->value());
532         }
533         if ((prop = node.property ("shuttle")) != 0) {
534                 shuttle_box->controllable()->set_id (prop->value());
535         }
536 }
537
538 XMLNode&
539 ARDOUR_UI::get_transport_controllable_state ()
540 {
541         XMLNode* node = new XMLNode(X_("TransportControllables"));
542         char buf[64];
543
544         roll_controllable->id().print (buf, sizeof (buf));
545         node->add_property (X_("roll"), buf);
546         stop_controllable->id().print (buf, sizeof (buf));
547         node->add_property (X_("stop"), buf);
548         goto_start_controllable->id().print (buf, sizeof (buf));
549         node->add_property (X_("goto_start"), buf);
550         goto_end_controllable->id().print (buf, sizeof (buf));
551         node->add_property (X_("goto_end"), buf);
552         auto_loop_controllable->id().print (buf, sizeof (buf));
553         node->add_property (X_("auto_loop"), buf);
554         play_selection_controllable->id().print (buf, sizeof (buf));
555         node->add_property (X_("play_selection"), buf);
556         rec_controllable->id().print (buf, sizeof (buf));
557         node->add_property (X_("rec"), buf);
558         shuttle_box->controllable()->id().print (buf, sizeof (buf));
559         node->add_property (X_("shuttle"), buf);
560
561         return *node;
562 }
563
564
565 gint
566 ARDOUR_UI::autosave_session ()
567 {
568         if (g_main_depth() > 1) {
569                 /* inside a recursive main loop,
570                    give up because we may not be able to
571                    take a lock.
572                 */
573                 return 1;
574         }
575
576         if (!Config->get_periodic_safety_backups()) {
577                 return 1;
578         }
579
580         if (_session) {
581                 _session->maybe_write_autosave();
582         }
583
584         return 1;
585 }
586
587 void
588 ARDOUR_UI::update_autosave ()
589 {
590         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::update_autosave)
591
592         if (_session && _session->dirty()) {
593                 if (_autosave_connection.connected()) {
594                         _autosave_connection.disconnect();
595                 }
596
597                 _autosave_connection = Glib::signal_timeout().connect (sigc::mem_fun (*this, &ARDOUR_UI::autosave_session),
598                                 Config->get_periodic_safety_backup_interval() * 1000);
599
600         } else {
601                 if (_autosave_connection.connected()) {
602                         _autosave_connection.disconnect();
603                 }
604         }
605 }
606
607 void
608 ARDOUR_UI::check_announcements ()
609 {
610 #ifdef PHONE_HOME
611         string _annc_filename;
612
613 #ifdef __APPLE__
614         _annc_filename = PROGRAM_NAME "_announcements_osx_";
615 #else
616         _annc_filename = PROGRAM_NAME "_announcements_linux_";
617 #endif
618         _annc_filename.append (VERSIONSTRING);
619
620         std::string path = Glib::build_filename (user_config_directory(), _annc_filename);
621         std::ifstream announce_file (path.c_str());
622         if ( announce_file.fail() )
623                 _announce_string = "";
624         else {
625                 std::stringstream oss;
626                 oss << announce_file.rdbuf();
627                 _announce_string = oss.str();
628         }
629
630         pingback (VERSIONSTRING, path);
631 #endif
632 }
633
634 void
635 ARDOUR_UI::startup ()
636 {
637         Application* app = Application::instance ();
638
639         app->ShouldQuit.connect (sigc::mem_fun (*this, &ARDOUR_UI::queue_finish));
640         app->ShouldLoad.connect (sigc::mem_fun (*this, &ARDOUR_UI::idle_load));
641
642         if (ARDOUR_COMMAND_LINE::check_announcements) {
643                 check_announcements ();
644         }
645
646         app->ready ();
647
648         if (get_session_parameters (true, ARDOUR_COMMAND_LINE::new_session, ARDOUR_COMMAND_LINE::load_template)) {
649                 exit (1);
650         }
651
652         use_config ();
653
654         goto_editor_window ();
655
656         /* Add the window proxies here; their addition may cause windows to be opened, and we want them
657            to be opened on top of the editor window that goto_editor_window() just opened.
658         */
659         add_window_proxy (location_ui);
660         add_window_proxy (big_clock_window);
661         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
662                 add_window_proxy (_global_port_matrix[*i]);
663         }
664
665         /* We have to do this here since goto_editor_window() ends up calling show_all() on the
666          * editor window, and we may want stuff to be hidden.
667          */
668         _status_bar_visibility.update ();
669
670         BootMessage (string_compose (_("%1 is ready for use"), PROGRAM_NAME));
671 }
672
673 void
674 ARDOUR_UI::no_memory_warning ()
675 {
676         XMLNode node (X_("no-memory-warning"));
677         Config->add_instant_xml (node);
678 }
679
680 void
681 ARDOUR_UI::check_memory_locking ()
682 {
683 #ifdef __APPLE__
684         /* OS X doesn't support mlockall(2), and so testing for memory locking capability there is pointless */
685         return;
686 #else // !__APPLE__
687
688         XMLNode* memory_warning_node = Config->instant_xml (X_("no-memory-warning"));
689
690         if (engine->is_realtime() && memory_warning_node == 0) {
691
692                 struct rlimit limits;
693                 int64_t ram;
694                 long pages, page_size;
695 #ifdef __FreeBSD__
696                 size_t pages_len=sizeof(pages);
697                 if ((page_size = getpagesize()) < 0 ||
698                                 sysctlbyname("hw.availpages", &pages, &pages_len, NULL, 0))
699 #else
700                 if ((page_size = sysconf (_SC_PAGESIZE)) < 0 ||(pages = sysconf (_SC_PHYS_PAGES)) < 0)
701 #endif
702                 {
703                         ram = 0;
704                 } else {
705                         ram = (int64_t) pages * (int64_t) page_size;
706                 }
707
708                 if (getrlimit (RLIMIT_MEMLOCK, &limits)) {
709                         return;
710                 }
711
712                 if (limits.rlim_cur != RLIM_INFINITY) {
713
714                         if (ram == 0 || ((double) limits.rlim_cur / ram) < 0.75) {
715
716                                 MessageDialog msg (
717                                         string_compose (
718                                                 _("WARNING: Your system has a limit for maximum amount of locked memory. "
719                                                   "This might cause %1 to run out of memory before your system "
720                                                   "runs out of memory. \n\n"
721                                                   "You can view the memory limit with 'ulimit -l', "
722                                                   "and it is normally controlled by %2"),
723                                                 PROGRAM_NAME).c_str(), 
724 #ifdef __FreeBSD__
725                                         X_("/etc/login.conf")
726 #else
727                                         X_(" /etc/security/limits.conf")
728 #endif
729                                         );
730
731                                 msg.set_default_response (RESPONSE_OK);
732
733                                 VBox* vbox = msg.get_vbox();
734                                 HBox hbox;
735                                 CheckButton cb (_("Do not show this window again"));
736
737                                 cb.signal_toggled().connect (sigc::mem_fun (*this, &ARDOUR_UI::no_memory_warning));
738                                 
739                                 hbox.pack_start (cb, true, false);
740                                 vbox->pack_start (hbox);
741                                 cb.show();
742                                 vbox->show();
743                                 hbox.show ();
744
745                                 pop_back_splash (msg);
746
747                                 editor->ensure_float (msg);
748                                 msg.run ();
749                         }
750                 }
751         }
752 #endif // !__APPLE__
753 }
754
755
756 void
757 ARDOUR_UI::queue_finish ()
758 {
759         Glib::signal_idle().connect (mem_fun (*this, &ARDOUR_UI::idle_finish));
760 }
761
762 bool
763 ARDOUR_UI::idle_finish ()
764 {
765         finish ();
766         return false; /* do not call again */
767 }
768
769 void
770 ARDOUR_UI::finish()
771 {
772         if (_session) {
773
774                 if (_session->dirty()) {
775                         vector<string> actions;
776                         actions.push_back (_("Don't quit"));
777                         actions.push_back (_("Just quit"));
778                         actions.push_back (_("Save and quit"));
779                         switch (ask_about_saving_session(actions)) {
780                         case -1:
781                                 return;
782                                 break;
783                         case 1:
784                                 /* use the default name */
785                                 if (save_state_canfail ("")) {
786                                         /* failed - don't quit */
787                                         MessageDialog msg (*editor,
788                                                            string_compose (_("\
789 %1 was unable to save your session.\n\n\
790 If you still wish to quit, please use the\n\n\
791 \"Just quit\" option."), PROGRAM_NAME));
792                                         pop_back_splash(msg);
793                                         msg.run ();
794                                         return;
795                                 }
796                                 break;
797                         case 0:
798                                 break;
799                         }
800                 }
801
802                 second_connection.disconnect ();
803                 point_one_second_connection.disconnect ();
804                 point_oh_five_second_connection.disconnect ();
805                 point_zero_one_second_connection.disconnect();
806         }
807
808         /* Save state before deleting the session, as that causes some
809            windows to be destroyed before their visible state can be
810            saved.
811         */
812         save_ardour_state ();
813
814         loading_message (string_compose (_("Please wait while %1 cleans up..."), PROGRAM_NAME));
815
816         if (_session) {
817                 // _session->set_deletion_in_progress ();
818                 _session->set_clean ();
819                 _session->remove_pending_capture_state ();
820                 delete _session;
821                 _session = 0;
822         }
823
824         ArdourDialog::close_all_dialogs ();
825         engine->stop (true);
826         quit ();
827 }
828
829 int
830 ARDOUR_UI::ask_about_saving_session (const vector<string>& actions)
831 {
832         ArdourDialog window (_("Unsaved Session"));
833         Gtk::HBox dhbox;  // the hbox for the image and text
834         Gtk::Label  prompt_label;
835         Gtk::Image* dimage = manage (new Gtk::Image(Stock::DIALOG_WARNING,  Gtk::ICON_SIZE_DIALOG));
836
837         string msg;
838
839         assert (actions.size() >= 3);
840
841         window.add_button (actions[0], RESPONSE_REJECT);
842         window.add_button (actions[1], RESPONSE_APPLY);
843         window.add_button (actions[2], RESPONSE_ACCEPT);
844
845         window.set_default_response (RESPONSE_ACCEPT);
846
847         Gtk::Button noquit_button (msg);
848         noquit_button.set_name ("EditorGTKButton");
849
850         string prompt;
851
852         if (_session->snap_name() == _session->name()) {
853                 prompt = string_compose(_("The session \"%1\"\nhas not been saved.\n\nAny changes made this time\nwill be lost unless you save it.\n\nWhat do you want to do?"),
854                                         _session->snap_name());
855         } else {
856                 prompt = string_compose(_("The snapshot \"%1\"\nhas not been saved.\n\nAny changes made this time\nwill be lost unless you save it.\n\nWhat do you want to do?"),
857                                         _session->snap_name());
858         }
859
860         prompt_label.set_text (prompt);
861         prompt_label.set_name (X_("PrompterLabel"));
862         prompt_label.set_alignment(ALIGN_LEFT, ALIGN_TOP);
863
864         dimage->set_alignment(ALIGN_CENTER, ALIGN_TOP);
865         dhbox.set_homogeneous (false);
866         dhbox.pack_start (*dimage, false, false, 5);
867         dhbox.pack_start (prompt_label, true, false, 5);
868         window.get_vbox()->pack_start (dhbox);
869
870         window.set_name (_("Prompter"));
871         window.set_position (Gtk::WIN_POS_MOUSE);
872         window.set_modal (true);
873         window.set_resizable (false);
874
875         dhbox.show();
876         prompt_label.show();
877         dimage->show();
878         window.show();
879         window.set_keep_above (true);
880         window.present ();
881
882         ResponseType r = (ResponseType) window.run();
883
884         window.hide ();
885
886         switch (r) {
887         case RESPONSE_ACCEPT: // save and get out of here
888                 return 1;
889         case RESPONSE_APPLY:  // get out of here
890                 return 0;
891         default:
892                 break;
893         }
894
895         return -1;
896 }
897
898
899 gint
900 ARDOUR_UI::every_second ()
901 {
902         update_cpu_load ();
903         update_buffer_load ();
904         update_disk_space ();
905         update_timecode_format ();
906         return TRUE;
907 }
908
909 gint
910 ARDOUR_UI::every_point_one_seconds ()
911 {
912         shuttle_box->update_speed_display ();
913         RapidScreenUpdate(); /* EMIT_SIGNAL */
914         return TRUE;
915 }
916
917 gint
918 ARDOUR_UI::every_point_zero_one_seconds ()
919 {
920         // august 2007: actual update frequency: 40Hz, not 100Hz
921
922         SuperRapidScreenUpdate(); /* EMIT_SIGNAL */
923         return TRUE;
924 }
925
926 void
927 ARDOUR_UI::update_sample_rate (framecnt_t)
928 {
929         char buf[64];
930
931         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::update_sample_rate, ignored)
932
933         if (!engine->connected()) {
934
935                 snprintf (buf, sizeof (buf), "%s", _("disconnected"));
936
937         } else {
938
939                 framecnt_t rate = engine->frame_rate();
940
941                 if (fmod (rate, 1000.0) != 0.0) {
942                         snprintf (buf, sizeof (buf), _("JACK: <span foreground=\"green\">%.1f kHz / %4.1f ms</span>"),
943                                   (float) rate/1000.0f,
944                                   (engine->frames_per_cycle() / (float) rate) * 1000.0f);
945                 } else {
946                         snprintf (buf, sizeof (buf), _("JACK: <span foreground=\"green\">%" PRId64 " kHz / %4.1f ms</span>"),
947                                   rate/1000,
948                                   (engine->frames_per_cycle() / (float) rate) * 1000.0f);
949                 }
950         }
951
952         sample_rate_label.set_markup (buf);
953 }
954
955 void
956 ARDOUR_UI::update_format ()
957 {
958         if (!_session) {
959                 format_label.set_text ("");
960                 return;
961         }
962
963         stringstream s;
964         s << _("File:") << X_(" <span foreground=\"green\">");
965
966         switch (_session->config.get_native_file_header_format ()) {
967         case BWF:
968                 s << _("BWF");
969                 break;
970         case WAVE:
971                 s << _("WAV");
972                 break;
973         case WAVE64:
974                 s << _("WAV64");
975                 break;
976         case CAF:
977                 s << _("CAF");
978                 break;
979         case AIFF:
980                 s << _("AIFF");
981                 break;
982         case iXML:
983                 s << _("iXML");
984                 break;
985         case RF64:
986                 s << _("RF64");
987                 break;
988         }
989
990         s << " ";
991         
992         switch (_session->config.get_native_file_data_format ()) {
993         case FormatFloat:
994                 s << _("32-float");
995                 break;
996         case FormatInt24:
997                 s << _("24-int");
998                 break;
999         case FormatInt16:
1000                 s << _("16-int");
1001                 break;
1002         }
1003
1004         s << X_("</span>");
1005
1006         format_label.set_markup (s.str ());
1007 }
1008
1009 void
1010 ARDOUR_UI::update_cpu_load ()
1011 {
1012         char buf[64];
1013
1014         /* If this text is changed, the set_size_request_to_display_given_text call in ARDOUR_UI::resize_text_widgets
1015            should also be changed.
1016         */
1017
1018         float const c = engine->get_cpu_load ();
1019         snprintf (buf, sizeof (buf), _("DSP: <span foreground=\"%s\">%5.1f%%</span>"), c >= 90 ? X_("red") : X_("green"), c);
1020         cpu_load_label.set_markup (buf);
1021 }
1022
1023 void
1024 ARDOUR_UI::update_buffer_load ()
1025 {
1026         char buf[256];
1027
1028         uint32_t const playback = _session ? _session->playback_load () : 100;
1029         uint32_t const capture = _session ? _session->capture_load () : 100;
1030
1031         /* If this text is changed, the set_size_request_to_display_given_text call in ARDOUR_UI::resize_text_widgets
1032            should also be changed.
1033         */
1034         
1035         if (_session) {
1036                 snprintf (
1037                         buf, sizeof (buf),
1038                         _("Buffers: <span foreground=\"green\">p:</span><span foreground=\"%s\">%" PRIu32 "%%</span> "
1039                                    "<span foreground=\"green\">c:</span><span foreground=\"%s\">%" PRIu32 "%%</span>"),
1040                         playback <= 5 ? X_("red") : X_("green"),
1041                         playback,
1042                         capture <= 5 ? X_("red") : X_("green"),
1043                         capture
1044                         );
1045
1046                 buffer_load_label.set_markup (buf);
1047         } else {
1048                 buffer_load_label.set_text ("");
1049         }
1050 }
1051
1052 void
1053 ARDOUR_UI::count_recenabled_streams (Route& route)
1054 {
1055         Track* track = dynamic_cast<Track*>(&route);
1056         if (track && track->record_enabled()) {
1057                 rec_enabled_streams += track->n_inputs().n_total();
1058         }
1059 }
1060
1061 void
1062 ARDOUR_UI::update_disk_space()
1063 {
1064         if (_session == 0) {
1065                 return;
1066         }
1067
1068         boost::optional<framecnt_t> opt_frames = _session->available_capture_duration();
1069         char buf[64];
1070         framecnt_t fr = _session->frame_rate();
1071
1072         if (!opt_frames) {
1073                 /* Available space is unknown */
1074                 snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">Unknown</span>"));
1075         } else if (opt_frames.get_value_or (0) == max_framecnt) {
1076                 snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">24hrs+</span>"));
1077         } else {
1078                 rec_enabled_streams = 0;
1079                 _session->foreach_route (this, &ARDOUR_UI::count_recenabled_streams);
1080
1081                 framecnt_t frames = opt_frames.get_value_or (0);
1082
1083                 if (rec_enabled_streams) {
1084                         frames /= rec_enabled_streams;
1085                 }
1086
1087                 int hrs;
1088                 int mins;
1089                 int secs;
1090
1091                 hrs  = frames / (fr * 3600);
1092
1093                 if (hrs > 24) {
1094                         snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">&gt;24 hrs</span>"));
1095                 } else {
1096                         frames -= hrs * fr * 3600;
1097                         mins = frames / (fr * 60);
1098                         frames -= mins * fr * 60;
1099                         secs = frames / fr;
1100                         
1101                         bool const low = (hrs == 0 && mins <= 30);
1102                         
1103                         snprintf (
1104                                 buf, sizeof(buf),
1105                                 _("Disk: <span foreground=\"%s\">%02dh:%02dm:%02ds</span>"),
1106                                 low ? X_("red") : X_("green"),
1107                                 hrs, mins, secs
1108                                 );
1109                 }
1110         }
1111
1112         disk_space_label.set_markup (buf);
1113 }
1114
1115 void
1116 ARDOUR_UI::update_timecode_format ()
1117 {
1118         char buf[64];
1119
1120         if (_session) {
1121                 bool matching;
1122                 TimecodeSlave* tcslave;
1123                 SyncSource sync_src = Config->get_sync_source();
1124
1125                 if ((sync_src == LTC || sync_src == MTC) && (tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1126                         matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1127                 } else {
1128                         matching = true;
1129                 }
1130                         
1131                 snprintf (buf, sizeof (buf), S_("Timecode|TC: <span foreground=\"%s\">%s</span>"),
1132                           matching ? X_("green") : X_("red"),
1133                           Timecode::timecode_format_name (_session->config.get_timecode_format()).c_str());
1134         } else {
1135                 snprintf (buf, sizeof (buf), "TC: n/a");
1136         }
1137
1138         timecode_format_label.set_markup (buf);
1139 }       
1140
1141 gint
1142 ARDOUR_UI::update_wall_clock ()
1143 {
1144         time_t now;
1145         struct tm *tm_now;
1146         static int last_min = -1;
1147
1148         time (&now);
1149         tm_now = localtime (&now);
1150         if (last_min != tm_now->tm_min) {
1151                 char buf[16];
1152                 sprintf (buf, "%02d:%02d", tm_now->tm_hour, tm_now->tm_min);
1153                 wall_clock_label.set_text (buf);
1154                 last_min = tm_now->tm_min;
1155         }
1156
1157         return TRUE;
1158 }
1159
1160 void
1161 ARDOUR_UI::redisplay_recent_sessions ()
1162 {
1163         std::vector<std::string> session_directories;
1164         RecentSessionsSorter cmp;
1165
1166         recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
1167         recent_session_model->clear ();
1168
1169         ARDOUR::RecentSessions rs;
1170         ARDOUR::read_recent_sessions (rs);
1171
1172         if (rs.empty()) {
1173                 recent_session_display.set_model (recent_session_model);
1174                 return;
1175         }
1176
1177         // sort them alphabetically
1178         sort (rs.begin(), rs.end(), cmp);
1179
1180         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
1181                 session_directories.push_back ((*i).second);
1182         }
1183
1184         for (vector<std::string>::const_iterator i = session_directories.begin();
1185                         i != session_directories.end(); ++i)
1186         {
1187                 std::vector<std::string> state_file_paths;
1188
1189                 // now get available states for this session
1190
1191                 get_state_files_in_directory (*i, state_file_paths);
1192
1193                 vector<string*>* states;
1194                 vector<const gchar*> item;
1195                 string fullpath = *i;
1196
1197                 /* remove any trailing / */
1198
1199                 if (fullpath[fullpath.length() - 1] == '/') {
1200                         fullpath = fullpath.substr (0, fullpath.length() - 1);
1201                 }
1202
1203                 /* check whether session still exists */
1204                 if (!Glib::file_test(fullpath.c_str(), Glib::FILE_TEST_EXISTS)) {
1205                         /* session doesn't exist */
1206                         continue;
1207                 }
1208
1209                 /* now get available states for this session */
1210
1211                 if ((states = Session::possible_states (fullpath)) == 0) {
1212                         /* no state file? */
1213                         continue;
1214                 }
1215
1216                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
1217
1218                 Gtk::TreeModel::Row row = *(recent_session_model->append());
1219
1220                 row[recent_session_columns.visible_name] = Glib::path_get_basename (fullpath);
1221                 row[recent_session_columns.fullpath] = fullpath;
1222                 row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
1223
1224                 if (state_file_names.size() > 1) {
1225
1226                         // add the children
1227
1228                         for (std::vector<std::string>::iterator i2 = state_file_names.begin();
1229                                         i2 != state_file_names.end(); ++i2)
1230                         {
1231
1232                                 Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
1233
1234                                 child_row[recent_session_columns.visible_name] = *i2;
1235                                 child_row[recent_session_columns.fullpath] = fullpath;
1236                                 child_row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
1237                         }
1238                 }
1239         }
1240
1241         recent_session_display.set_tooltip_column(1); // recent_session_columns.tip
1242         recent_session_display.set_model (recent_session_model);
1243 }
1244
1245 void
1246 ARDOUR_UI::build_session_selector ()
1247 {
1248         session_selector_window = new ArdourDialog (_("Recent Sessions"));
1249
1250         Gtk::ScrolledWindow *scroller = manage (new Gtk::ScrolledWindow);
1251
1252         session_selector_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
1253         session_selector_window->add_button (Stock::OPEN, RESPONSE_ACCEPT);
1254         session_selector_window->set_default_response (RESPONSE_ACCEPT);
1255         recent_session_model = TreeStore::create (recent_session_columns);
1256         recent_session_display.set_model (recent_session_model);
1257         recent_session_display.append_column (_("Recent Sessions"), recent_session_columns.visible_name);
1258         recent_session_display.set_headers_visible (false);
1259         recent_session_display.get_selection()->set_mode (SELECTION_BROWSE);
1260         recent_session_display.signal_row_activated().connect (sigc::mem_fun (*this, &ARDOUR_UI::recent_session_row_activated));
1261
1262         scroller->add (recent_session_display);
1263         scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1264
1265         session_selector_window->set_name ("SessionSelectorWindow");
1266         session_selector_window->set_size_request (200, 400);
1267         session_selector_window->get_vbox()->pack_start (*scroller);
1268
1269         recent_session_display.show();
1270         scroller->show();
1271 }
1272
1273 void
1274 ARDOUR_UI::recent_session_row_activated (const TreePath& /*path*/, TreeViewColumn* /*col*/)
1275 {
1276         session_selector_window->response (RESPONSE_ACCEPT);
1277 }
1278
1279 void
1280 ARDOUR_UI::open_recent_session ()
1281 {
1282         bool can_return = (_session != 0);
1283
1284         if (session_selector_window == 0) {
1285                 build_session_selector ();
1286         }
1287
1288         redisplay_recent_sessions ();
1289
1290         while (true) {
1291
1292                 session_selector_window->set_position (WIN_POS_MOUSE);
1293
1294                 ResponseType r = (ResponseType) session_selector_window->run ();
1295
1296                 switch (r) {
1297                 case RESPONSE_ACCEPT:
1298                         break;
1299                 default:
1300                         if (can_return) {
1301                                 session_selector_window->hide();
1302                                 return;
1303                         } else {
1304                                 exit (1);
1305                         }
1306                 }
1307
1308                 if (recent_session_display.get_selection()->count_selected_rows() == 0) {
1309                         continue;
1310                 }
1311
1312                 session_selector_window->hide();
1313
1314                 Gtk::TreeModel::iterator i = recent_session_display.get_selection()->get_selected();
1315
1316                 if (i == recent_session_model->children().end()) {
1317                         return;
1318                 }
1319
1320                 std::string path = (*i)[recent_session_columns.fullpath];
1321                 std::string state = (*i)[recent_session_columns.visible_name];
1322
1323                 _session_is_new = false;
1324
1325                 if (load_session (path, state) == 0) {
1326                         break;
1327                 }
1328
1329                 can_return = false;
1330         }
1331 }
1332
1333 bool
1334 ARDOUR_UI::check_audioengine ()
1335 {
1336         if (engine) {
1337                 if (!engine->connected()) {
1338                         MessageDialog msg (string_compose (
1339                                                    _("%1 is not connected to JACK\n"
1340                                                      "You cannot open or close sessions in this condition"),
1341                                                    PROGRAM_NAME));
1342                         pop_back_splash (msg);
1343                         msg.run ();
1344                         return false;
1345                 }
1346                 return true;
1347         } else {
1348                 return false;
1349         }
1350 }
1351
1352 void
1353 ARDOUR_UI::open_session ()
1354 {
1355         if (!check_audioengine()) {
1356                 return;
1357
1358         }
1359
1360         /* popup selector window */
1361
1362         if (open_session_selector == 0) {
1363
1364                 /* ardour sessions are folders */
1365
1366                 open_session_selector = new Gtk::FileChooserDialog (_("Open Session"), FILE_CHOOSER_ACTION_OPEN);
1367                 open_session_selector->add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1368                 open_session_selector->add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
1369                 open_session_selector->set_default_response(Gtk::RESPONSE_ACCEPT);
1370                 
1371                 if (_session) {
1372                         string session_parent_dir = Glib::path_get_dirname(_session->path());
1373                         string::size_type last_dir_sep = session_parent_dir.rfind(G_DIR_SEPARATOR);
1374                         session_parent_dir = session_parent_dir.substr(0, last_dir_sep);
1375                         open_session_selector->set_current_folder(session_parent_dir);
1376                 } else {
1377                         open_session_selector->set_current_folder(Config->get_default_session_parent_dir());
1378                 }
1379
1380                 open_session_selector->add_shortcut_folder (Config->get_default_session_parent_dir());
1381
1382                 FileFilter session_filter;
1383                 session_filter.add_pattern ("*.ardour");
1384                 session_filter.set_name (string_compose (_("%1 sessions"), PROGRAM_NAME));
1385                 open_session_selector->add_filter (session_filter);
1386                 open_session_selector->set_filter (session_filter);
1387         }
1388
1389         int response = open_session_selector->run();
1390         open_session_selector->hide ();
1391
1392         switch (response) {
1393         case RESPONSE_ACCEPT:
1394                 break;
1395         default:
1396                 open_session_selector->hide();
1397                 return;
1398         }
1399
1400         open_session_selector->hide();
1401         string session_path = open_session_selector->get_filename();
1402         string path, name;
1403         bool isnew;
1404
1405         if (session_path.length() > 0) {
1406                 if (ARDOUR::find_session (session_path, path, name, isnew) == 0) {
1407                         _session_is_new = isnew;
1408                         load_session (path, name);
1409                 }
1410         }
1411 }
1412
1413
1414 void
1415 ARDOUR_UI::session_add_mixed_track (const ChanCount& input, const ChanCount& output, RouteGroup* route_group, 
1416                                     uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
1417 {
1418         list<boost::shared_ptr<MidiTrack> > tracks;
1419
1420         if (_session == 0) {
1421                 warning << _("You cannot add a track without a session already loaded.") << endmsg;
1422                 return;
1423         }
1424
1425         try {
1426                 tracks = _session->new_midi_track (input, output, instrument, ARDOUR::Normal, route_group, how_many, name_template);
1427                 
1428                 if (tracks.size() != how_many) {
1429                         error << string_compose(P_("could not create %1 new mixed track", "could not create %1 new mixed tracks", how_many), how_many) << endmsg;
1430                 }
1431         }
1432
1433         catch (...) {
1434                 MessageDialog msg (*editor,
1435                                    string_compose (_("There are insufficient JACK ports available\n\
1436 to create a new track or bus.\n\
1437 You should save %1, exit and\n\
1438 restart JACK with more ports."), PROGRAM_NAME));
1439                 msg.run ();
1440         }
1441 }
1442         
1443
1444 void
1445 ARDOUR_UI::session_add_midi_route (bool disk, RouteGroup* route_group, uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
1446 {
1447         ChanCount one_midi_channel;
1448         one_midi_channel.set (DataType::MIDI, 1);
1449
1450         if (disk) {
1451                 session_add_mixed_track (one_midi_channel, one_midi_channel, route_group, how_many, name_template, instrument);
1452         }
1453 }
1454
1455 void
1456 ARDOUR_UI::session_add_audio_route (
1457         bool track,
1458         int32_t input_channels,
1459         int32_t output_channels,
1460         ARDOUR::TrackMode mode,
1461         RouteGroup* route_group,
1462         uint32_t how_many,
1463         string const & name_template
1464         )
1465 {
1466         list<boost::shared_ptr<AudioTrack> > tracks;
1467         RouteList routes;
1468
1469         if (_session == 0) {
1470                 warning << _("You cannot add a track or bus without a session already loaded.") << endmsg;
1471                 return;
1472         }
1473
1474         try {
1475                 if (track) {
1476                         tracks = _session->new_audio_track (input_channels, output_channels, mode, route_group, how_many, name_template);
1477
1478                         if (tracks.size() != how_many) {
1479                                 error << string_compose (P_("could not create %1 new audio track", "could not create %1 new audio tracks", how_many), how_many) 
1480                                       << endmsg;
1481                         }
1482
1483                 } else {
1484
1485                         routes = _session->new_audio_route (input_channels, output_channels, route_group, how_many, name_template);
1486
1487                         if (routes.size() != how_many) {
1488                                 error << string_compose (P_("could not create %1 new audio bus", "could not create %1 new audio busses", how_many), how_many)
1489                                       << endmsg;
1490                         }
1491                 }
1492         }
1493
1494         catch (...) {
1495                 MessageDialog msg (*editor,
1496                                    string_compose (_("There are insufficient JACK ports available\n\
1497 to create a new track or bus.\n\
1498 You should save %1, exit and\n\
1499 restart JACK with more ports."), PROGRAM_NAME));
1500                 pop_back_splash (msg);
1501                 msg.run ();
1502         }
1503 }
1504
1505 void
1506 ARDOUR_UI::transport_goto_start ()
1507 {
1508         if (_session) {
1509                 _session->goto_start();
1510
1511                 /* force displayed area in editor to start no matter
1512                    what "follow playhead" setting is.
1513                 */
1514
1515                 if (editor) {
1516                         editor->center_screen (_session->current_start_frame ());
1517                 }
1518         }
1519 }
1520
1521 void
1522 ARDOUR_UI::transport_goto_zero ()
1523 {
1524         if (_session) {
1525                 _session->request_locate (0);
1526
1527                 /* force displayed area in editor to start no matter
1528                    what "follow playhead" setting is.
1529                 */
1530
1531                 if (editor) {
1532                         editor->reset_x_origin (0);
1533                 }
1534         }
1535 }
1536
1537 void
1538 ARDOUR_UI::transport_goto_wallclock ()
1539 {
1540         if (_session && editor) {
1541
1542                 time_t now;
1543                 struct tm tmnow;
1544                 framepos_t frames;
1545
1546                 time (&now);
1547                 localtime_r (&now, &tmnow);
1548
1549                 frames = tmnow.tm_hour * (60 * 60 * _session->frame_rate());
1550                 frames += tmnow.tm_min * (60 * _session->frame_rate());
1551                 frames += tmnow.tm_sec * _session->frame_rate();
1552
1553                 _session->request_locate (frames, _session->transport_rolling ());
1554
1555                 /* force displayed area in editor to start no matter
1556                    what "follow playhead" setting is.
1557                 */
1558
1559                 if (editor) {
1560                         editor->center_screen (frames);
1561                 }
1562         }
1563 }
1564
1565 void
1566 ARDOUR_UI::transport_goto_end ()
1567 {
1568         if (_session) {
1569                 framepos_t const frame = _session->current_end_frame();
1570                 _session->request_locate (frame);
1571
1572                 /* force displayed area in editor to start no matter
1573                    what "follow playhead" setting is.
1574                 */
1575
1576                 if (editor) {
1577                         editor->center_screen (frame);
1578                 }
1579         }
1580 }
1581
1582 void
1583 ARDOUR_UI::transport_stop ()
1584 {
1585         if (!_session) {
1586                 return;
1587         }
1588
1589         if (_session->is_auditioning()) {
1590                 _session->cancel_audition ();
1591                 return;
1592         }
1593
1594         _session->request_stop (false, true);
1595 }
1596
1597 void
1598 ARDOUR_UI::transport_record (bool roll)
1599 {
1600
1601         if (_session) {
1602                 switch (_session->record_status()) {
1603                 case Session::Disabled:
1604                         if (_session->ntracks() == 0) {
1605                                 MessageDialog msg (*editor, _("Please create one or more tracks before trying to record.\nYou can do this with the \"Add Track or Bus\" option in the Session menu."));
1606                                 msg.run ();
1607                                 return;
1608                         }
1609                         _session->maybe_enable_record ();
1610                         if (roll) {
1611                                 transport_roll ();
1612                         }
1613                         break;
1614                 case Session::Recording:
1615                         if (roll) {
1616                                 _session->request_stop();
1617                         } else {
1618                                 _session->disable_record (false, true);
1619                         }
1620                         break;
1621
1622                 case Session::Enabled:
1623                         _session->disable_record (false, true);
1624                 }
1625         }
1626 }
1627
1628 void
1629 ARDOUR_UI::transport_roll ()
1630 {
1631         if (!_session) {
1632                 return;
1633         }
1634
1635         if (_session->is_auditioning()) {
1636                 return;
1637         }
1638
1639 #if 0
1640         if (_session->config.get_external_sync()) {
1641                 switch (Config->get_sync_source()) {
1642                 case JACK:
1643                         break;
1644                 default:
1645                         /* transport controlled by the master */
1646                         return;
1647                 }
1648         }
1649 #endif
1650
1651         bool rolling = _session->transport_rolling();
1652
1653         if (_session->get_play_loop()) {
1654                 /* XXX it is not possible to just leave seamless loop and keep
1655                    playing at present (nov 4th 2009)
1656                 */
1657                 if (!Config->get_seamless_loop()) {
1658                         _session->request_play_loop (false, true);
1659                 }
1660         } else if (_session->get_play_range () && !Config->get_always_play_range()) {
1661                 /* stop playing a range if we currently are */
1662                 _session->request_play_range (0, true);
1663         }
1664
1665         if (!rolling) {
1666                 _session->request_transport_speed (1.0f);
1667         }
1668 }
1669
1670 bool
1671 ARDOUR_UI::get_smart_mode() const
1672 {
1673         return ( editor->get_smart_mode() );
1674 }
1675
1676
1677 void
1678 ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
1679 {
1680
1681         if (!_session) {
1682                 return;
1683         }
1684
1685         if (_session->is_auditioning()) {
1686                 _session->cancel_audition ();
1687                 return;
1688         }
1689
1690         if (_session->config.get_external_sync()) {
1691                 switch (Config->get_sync_source()) {
1692                 case JACK:
1693                         break;
1694                 default:
1695                         /* transport controlled by the master */
1696                         return;
1697                 }
1698         }
1699
1700         bool rolling = _session->transport_rolling();
1701         bool affect_transport = true;
1702
1703         if (rolling && roll_out_of_bounded_mode) {
1704                 /* drop out of loop/range playback but leave transport rolling */
1705                 if (_session->get_play_loop()) {
1706                         if (Config->get_seamless_loop()) {
1707                                 /* the disk buffers contain copies of the loop - we can't
1708                                    just keep playing, so stop the transport. the user
1709                                    can restart as they wish.
1710                                 */
1711                                 affect_transport = true;
1712                         } else {
1713                                 /* disk buffers are normal, so we can keep playing */
1714                                 affect_transport = false;
1715                         }
1716                         _session->request_play_loop (false, true);
1717                 } else if (_session->get_play_range ()) {
1718                         affect_transport = false;
1719                         _session->request_play_range (0, true);
1720                 }
1721         }
1722
1723         if (affect_transport) {
1724                 if (rolling) {
1725                         _session->request_stop (with_abort, true);
1726                 } else {
1727                         if ( Config->get_always_play_range() ) {
1728                                 _session->request_play_range (&editor->get_selection().time, true);
1729                         }
1730
1731                         _session->request_transport_speed (1.0f);
1732                 }
1733         }
1734 }
1735
1736 void
1737 ARDOUR_UI::toggle_session_auto_loop ()
1738 {
1739         Location * looploc = _session->locations()->auto_loop_location();
1740
1741         if (!_session || !looploc) {
1742                 return;
1743         }
1744
1745         if (_session->get_play_loop()) {
1746
1747                 if (_session->transport_rolling()) {
1748
1749                         _session->request_locate (looploc->start(), true);
1750                         _session->request_play_loop (false);
1751
1752                 } else {
1753                         _session->request_play_loop (false);
1754                 }
1755         } else {
1756                 _session->request_play_loop (true);
1757         }
1758         
1759         //show the loop markers
1760         looploc->set_hidden (false, this);
1761 }
1762
1763 void
1764 ARDOUR_UI::transport_play_selection ()
1765 {
1766         if (!_session) {
1767                 return;
1768         }
1769
1770         editor->play_selection ();
1771 }
1772
1773 void
1774 ARDOUR_UI::transport_play_preroll ()
1775 {
1776         if (!_session) {
1777                 return;
1778         }
1779         editor->play_with_preroll ();
1780 }
1781
1782 void
1783 ARDOUR_UI::transport_rewind (int option)
1784 {
1785         float current_transport_speed;
1786
1787         if (_session) {
1788                 current_transport_speed = _session->transport_speed();
1789
1790                 if (current_transport_speed >= 0.0f) {
1791                         switch (option) {
1792                         case 0:
1793                                 _session->request_transport_speed (-1.0f);
1794                                 break;
1795                         case 1:
1796                                 _session->request_transport_speed (-4.0f);
1797                                 break;
1798                         case -1:
1799                                 _session->request_transport_speed (-0.5f);
1800                                 break;
1801                         }
1802                 } else {
1803                         /* speed up */
1804                         _session->request_transport_speed (current_transport_speed * 1.5f);
1805                 }
1806         }
1807 }
1808
1809 void
1810 ARDOUR_UI::transport_forward (int option)
1811 {
1812         if (!_session) {
1813                 return;
1814         }
1815         
1816         float current_transport_speed = _session->transport_speed();
1817         
1818         if (current_transport_speed <= 0.0f) {
1819                 switch (option) {
1820                 case 0:
1821                         _session->request_transport_speed (1.0f);
1822                         break;
1823                 case 1:
1824                         _session->request_transport_speed (4.0f);
1825                         break;
1826                 case -1:
1827                         _session->request_transport_speed (0.5f);
1828                         break;
1829                 }
1830         } else {
1831                 /* speed up */
1832                 _session->request_transport_speed (current_transport_speed * 1.5f);
1833         }
1834 }
1835
1836 void
1837 ARDOUR_UI::toggle_record_enable (uint32_t rid)
1838 {
1839         if (!_session) {
1840                 return;
1841         }
1842
1843         boost::shared_ptr<Route> r;
1844
1845         if ((r = _session->route_by_remote_id (rid)) != 0) {
1846
1847                 Track* t;
1848
1849                 if ((t = dynamic_cast<Track*>(r.get())) != 0) {
1850                         t->set_record_enabled (!t->record_enabled(), this);
1851                 }
1852         }
1853 }
1854
1855 void
1856 ARDOUR_UI::map_transport_state ()
1857 {
1858         if (!_session) {
1859                 auto_loop_button.unset_active_state ();
1860                 play_selection_button.unset_active_state ();
1861                 roll_button.unset_active_state ();
1862                 stop_button.set_active_state (Gtkmm2ext::ExplicitActive);
1863                 return;
1864         }
1865
1866         shuttle_box->map_transport_state ();
1867
1868         float sp = _session->transport_speed();
1869
1870         if (sp != 0.0f) {
1871
1872                 /* we're rolling */
1873
1874                 if (_session->get_play_range()) {
1875
1876                         play_selection_button.set_active_state (Gtkmm2ext::ExplicitActive);
1877                         roll_button.unset_active_state ();
1878                         auto_loop_button.unset_active_state ();
1879
1880                 } else if (_session->get_play_loop ()) {
1881
1882                         auto_loop_button.set_active (true);
1883                         play_selection_button.set_active (false);
1884                         roll_button.set_active (false);
1885
1886                 } else {
1887
1888                         roll_button.set_active (true);
1889                         play_selection_button.set_active (false);
1890                         auto_loop_button.set_active (false);
1891                 }
1892
1893                 if (Config->get_always_play_range()) {
1894                         /* light up both roll and play-selection if they are joined */
1895                         roll_button.set_active (true);
1896                         play_selection_button.set_active (true);
1897                 }
1898
1899                 stop_button.set_active (false);
1900
1901         } else {
1902
1903                 stop_button.set_active (true);
1904                 roll_button.set_active (false);
1905                 play_selection_button.set_active (false);
1906                 auto_loop_button.set_active (false);
1907                 update_disk_space ();
1908         }
1909 }
1910
1911 void
1912 ARDOUR_UI::engine_stopped ()
1913 {
1914         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_stopped)
1915         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, false);
1916         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, true);
1917 }
1918
1919 void
1920 ARDOUR_UI::engine_running ()
1921 {
1922         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_running)
1923         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, true);
1924         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, false);
1925
1926         Glib::RefPtr<Action> action;
1927         const char* action_name = 0;
1928
1929         switch (engine->frames_per_cycle()) {
1930         case 32:
1931                 action_name = X_("JACKLatency32");
1932                 break;
1933         case 64:
1934                 action_name = X_("JACKLatency64");
1935                 break;
1936         case 128:
1937                 action_name = X_("JACKLatency128");
1938                 break;
1939         case 512:
1940                 action_name = X_("JACKLatency512");
1941                 break;
1942         case 1024:
1943                 action_name = X_("JACKLatency1024");
1944                 break;
1945         case 2048:
1946                 action_name = X_("JACKLatency2048");
1947                 break;
1948         case 4096:
1949                 action_name = X_("JACKLatency4096");
1950                 break;
1951         case 8192:
1952                 action_name = X_("JACKLatency8192");
1953                 break;
1954         default:
1955                 /* XXX can we do anything useful ? */
1956                 break;
1957         }
1958
1959         if (action_name) {
1960
1961                 action = ActionManager::get_action (X_("JACK"), action_name);
1962
1963                 if (action) {
1964                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
1965                         ract->set_active ();
1966                 }
1967         }
1968 }
1969
1970 void
1971 ARDOUR_UI::engine_halted (const char* reason, bool free_reason)
1972 {
1973         if (!Gtkmm2ext::UI::instance()->caller_is_ui_thread()) {
1974                 /* we can't rely on the original string continuing to exist when we are called
1975                    again in the GUI thread, so make a copy and note that we need to
1976                    free it later.
1977                 */
1978                 char *copy = strdup (reason);
1979                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&ARDOUR_UI::engine_halted, this, copy, true));
1980                 return;
1981         }
1982
1983         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, false);
1984         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, true);
1985
1986         update_sample_rate (0);
1987
1988         string msgstr;
1989
1990         /* if the reason is a non-empty string, it means that the backend was shutdown
1991            rather than just Ardour.
1992         */
1993
1994         if (strlen (reason)) {
1995                 msgstr = string_compose (_("The audio backend (JACK) was shutdown because:\n\n%1"), reason);
1996         } else {
1997                 msgstr = string_compose (_("\
1998 JACK has either been shutdown or it\n\
1999 disconnected %1 because %1\n\
2000 was not fast enough. Try to restart\n\
2001 JACK, reconnect and save the session."), PROGRAM_NAME);
2002         }
2003
2004         MessageDialog msg (*editor, msgstr);
2005         pop_back_splash (msg);
2006         msg.set_keep_above (true);
2007         msg.run ();
2008         
2009         if (free_reason) {
2010                 free (const_cast<char*> (reason));
2011         }
2012 }
2013
2014 int32_t
2015 ARDOUR_UI::do_engine_start ()
2016 {
2017         try {
2018                 engine->start();
2019         }
2020
2021         catch (...) {
2022                 engine->stop ();
2023                 error << _("Unable to start the session running")
2024                       << endmsg;
2025                 unload_session ();
2026                 return -2;
2027         }
2028
2029         return 0;
2030 }
2031
2032 void
2033 ARDOUR_UI::update_clocks ()
2034 {
2035         if (!editor || !editor->dragging_playhead()) {
2036                 Clock (_session->audible_frame(), false, editor->get_preferred_edit_position()); /* EMIT_SIGNAL */
2037         }
2038 }
2039
2040 void
2041 ARDOUR_UI::start_clocking ()
2042 {
2043         if (Config->get_super_rapid_clock_update()) {
2044                 clock_signal_connection = SuperRapidScreenUpdate.connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
2045         } else {
2046                 clock_signal_connection = RapidScreenUpdate.connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
2047         }
2048 }
2049
2050 void
2051 ARDOUR_UI::stop_clocking ()
2052 {
2053         clock_signal_connection.disconnect ();
2054 }
2055
2056 gint
2057 ARDOUR_UI::_blink (void *arg)
2058 {
2059         ((ARDOUR_UI *) arg)->blink ();
2060         return TRUE;
2061 }
2062
2063 void
2064 ARDOUR_UI::blink ()
2065 {
2066         Blink (blink_on = !blink_on); /* EMIT_SIGNAL */
2067 }
2068
2069 void
2070 ARDOUR_UI::start_blinking ()
2071 {
2072         /* Start the blink signal. Everybody with a blinking widget
2073            uses Blink to drive the widget's state.
2074         */
2075
2076         if (blink_timeout_tag < 0) {
2077                 blink_on = false;
2078                 blink_timeout_tag = g_timeout_add (240, _blink, this);
2079         }
2080 }
2081
2082 void
2083 ARDOUR_UI::stop_blinking ()
2084 {
2085         if (blink_timeout_tag >= 0) {
2086                 g_source_remove (blink_timeout_tag);
2087                 blink_timeout_tag = -1;
2088         }
2089 }
2090
2091
2092 /** Ask the user for the name of a new snapshot and then take it.
2093  */
2094
2095 void
2096 ARDOUR_UI::snapshot_session (bool switch_to_it)
2097 {
2098         ArdourPrompter prompter (true);
2099         string snapname;
2100
2101         prompter.set_name ("Prompter");
2102         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2103         prompter.set_title (_("Take Snapshot"));
2104         prompter.set_prompt (_("Name of new snapshot"));
2105
2106         if (!switch_to_it) {
2107                 char timebuf[128];
2108                 time_t n;
2109                 struct tm local_time;
2110
2111                 time (&n);
2112                 localtime_r (&n, &local_time);
2113                 strftime (timebuf, sizeof(timebuf), "%FT%H.%M.%S", &local_time);
2114                 prompter.set_initial_text (timebuf);
2115         }
2116
2117   again:
2118         switch (prompter.run()) {
2119         case RESPONSE_ACCEPT:
2120         {
2121                 prompter.get_result (snapname);
2122
2123                 bool do_save = (snapname.length() != 0);
2124
2125                 if (do_save) {
2126                         char illegal = Session::session_name_is_legal(snapname);
2127                         if (illegal) {
2128                                 MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
2129                                                      "snapshot names may not contain a '%1' character"), illegal));
2130                                 msg.run ();
2131                                 goto again;
2132                         }
2133                 }
2134
2135                 vector<std::string> p;
2136                 get_state_files_in_directory (_session->session_directory().root_path(), p);
2137                 vector<string> n = get_file_names_no_extension (p);
2138                 if (find (n.begin(), n.end(), snapname) != n.end()) {
2139
2140                         ArdourDialog confirm (_("Confirm Snapshot Overwrite"), true);
2141                         Label m (_("A snapshot already exists with that name.  Do you want to overwrite it?"));
2142                         confirm.get_vbox()->pack_start (m, true, true);
2143                         confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2144                         confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
2145                         confirm.show_all ();
2146                         switch (confirm.run()) {
2147                         case RESPONSE_CANCEL:
2148                                 do_save = false;
2149                         }
2150                 }
2151
2152                 if (do_save) {
2153                         save_state (snapname, switch_to_it);
2154                 }
2155                 break;
2156         }
2157
2158         default:
2159                 break;
2160         }
2161 }
2162
2163 /** Ask the user for a new session name and then rename the session to it.
2164  */
2165
2166 void
2167 ARDOUR_UI::rename_session ()
2168 {
2169         if (!_session) {
2170                 return;
2171         }
2172
2173         ArdourPrompter prompter (true);
2174         string name;
2175
2176         prompter.set_name ("Prompter");
2177         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2178         prompter.set_title (_("Rename Session"));
2179         prompter.set_prompt (_("New session name"));
2180
2181   again:
2182         switch (prompter.run()) {
2183         case RESPONSE_ACCEPT:
2184         {
2185                 prompter.get_result (name);
2186
2187                 bool do_rename = (name.length() != 0);
2188
2189                 if (do_rename) {
2190                         char illegal = Session::session_name_is_legal (name);
2191
2192                         if (illegal) {
2193                                 MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
2194                                                                      "session names may not contain a '%1' character"), illegal));
2195                                 msg.run ();
2196                                 goto again;
2197                         }
2198
2199                         switch (_session->rename (name)) {
2200                         case -1: {
2201                                 MessageDialog msg (_("That name is already in use by another directory/folder. Please try again."));
2202                                 msg.set_position (WIN_POS_MOUSE);
2203                                 msg.run ();
2204                                 goto again;
2205                                 break;
2206                         }
2207                         case 0:
2208                                 break;
2209                         default: {
2210                                 MessageDialog msg (_("Renaming this session failed.\nThings could be seriously messed up at this point"));
2211                                 msg.set_position (WIN_POS_MOUSE);
2212                                 msg.run ();
2213                                 break;
2214                         }
2215                         }
2216                 }
2217                 
2218                 break;
2219         }
2220
2221         default:
2222                 break;
2223         }
2224 }
2225
2226 void
2227 ARDOUR_UI::save_state (const string & name, bool switch_to_it)
2228 {
2229         XMLNode* node = new XMLNode (X_("UI"));
2230
2231         for (list<WindowProxyBase*>::iterator i = _window_proxies.begin(); i != _window_proxies.end(); ++i) {
2232                 if (!(*i)->rc_configured()) {
2233                         node->add_child_nocopy (*((*i)->get_state ()));
2234                 }
2235         }
2236
2237         node->add_child_nocopy (gui_object_state->get_state());
2238
2239         _session->add_extra_xml (*node);
2240
2241         save_state_canfail (name, switch_to_it);
2242 }
2243
2244 int
2245 ARDOUR_UI::save_state_canfail (string name, bool switch_to_it)
2246 {
2247         if (_session) {
2248                 int ret;
2249
2250                 if (name.length() == 0) {
2251                         name = _session->snap_name();
2252                 }
2253
2254                 if ((ret = _session->save_state (name, false, switch_to_it)) != 0) {
2255                         return ret;
2256                 }
2257         }
2258
2259         save_ardour_state (); /* XXX cannot fail? yeah, right ... */
2260         return 0;
2261 }
2262
2263 void
2264 ARDOUR_UI::primary_clock_value_changed ()
2265 {
2266         if (_session) {
2267                 _session->request_locate (primary_clock->current_time ());
2268         }
2269 }
2270
2271 void
2272 ARDOUR_UI::big_clock_value_changed ()
2273 {
2274         if (_session) {
2275                 _session->request_locate (big_clock->current_time ());
2276         }
2277 }
2278
2279 void
2280 ARDOUR_UI::secondary_clock_value_changed ()
2281 {
2282         if (_session) {
2283                 _session->request_locate (secondary_clock->current_time ());
2284         }
2285 }
2286
2287 void
2288 ARDOUR_UI::transport_rec_enable_blink (bool onoff)
2289 {
2290         if (_session == 0) {
2291                 return;
2292         }
2293
2294         if (_session->step_editing()) {
2295                 return;
2296         }
2297
2298         Session::RecordState const r = _session->record_status ();
2299         bool const h = _session->have_rec_enabled_track ();
2300
2301         if (r == Session::Enabled || (r == Session::Recording && !h)) {
2302                 if (onoff) {
2303                         rec_button.set_active_state (Gtkmm2ext::ExplicitActive);
2304                 } else {
2305                         rec_button.set_active_state (Gtkmm2ext::ImplicitActive);
2306                 }
2307         } else if (r == Session::Recording && h) {
2308                 rec_button.set_active_state (Gtkmm2ext::ExplicitActive);
2309         } else {
2310                 rec_button.unset_active_state ();
2311         }
2312 }
2313
2314 void
2315 ARDOUR_UI::save_template ()
2316 {
2317         ArdourPrompter prompter (true);
2318         string name;
2319
2320         if (!check_audioengine()) {
2321                 return;
2322         }
2323
2324         prompter.set_name (X_("Prompter"));
2325         prompter.set_title (_("Save Template"));
2326         prompter.set_prompt (_("Name for template:"));
2327         prompter.set_initial_text(_session->name() + _("-template"));
2328         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2329
2330         switch (prompter.run()) {
2331         case RESPONSE_ACCEPT:
2332                 prompter.get_result (name);
2333
2334                 if (name.length()) {
2335                         _session->save_template (name);
2336                 }
2337                 break;
2338
2339         default:
2340                 break;
2341         }
2342 }
2343
2344 void
2345 ARDOUR_UI::edit_metadata ()
2346 {
2347         SessionMetadataEditor dialog;
2348         dialog.set_session (_session);
2349         editor->ensure_float (dialog);
2350         dialog.run ();
2351 }
2352
2353 void
2354 ARDOUR_UI::import_metadata ()
2355 {
2356         SessionMetadataImporter dialog;
2357         dialog.set_session (_session);
2358         editor->ensure_float (dialog);
2359         dialog.run ();
2360 }
2361
2362 bool
2363 ARDOUR_UI::ask_about_loading_existing_session (const std::string& session_path)
2364 {
2365         std::string str = string_compose (_("This session\n%1\nalready exists. Do you want to open it?"), session_path);
2366
2367         MessageDialog msg (str,
2368                            false,
2369                            Gtk::MESSAGE_WARNING,
2370                            Gtk::BUTTONS_YES_NO,
2371                            true);
2372
2373
2374         msg.set_name (X_("OpenExistingDialog"));
2375         msg.set_title (_("Open Existing Session"));
2376         msg.set_wmclass (X_("existing_session"), PROGRAM_NAME);
2377         msg.set_position (Gtk::WIN_POS_MOUSE);
2378         pop_back_splash (msg);
2379
2380         switch (msg.run()) {
2381         case RESPONSE_YES:
2382                 return true;
2383                 break;
2384         }
2385         return false;
2386 }
2387
2388 int
2389 ARDOUR_UI::build_session_from_nsd (const std::string& session_path, const std::string& session_name)
2390 {
2391         BusProfile bus_profile;
2392
2393         if (Profile->get_sae()) {
2394
2395                 bus_profile.master_out_channels = 2;
2396                 bus_profile.input_ac = AutoConnectPhysical;
2397                 bus_profile.output_ac = AutoConnectMaster;
2398                 bus_profile.requested_physical_in = 0; // use all available
2399                 bus_profile.requested_physical_out = 0; // use all available
2400
2401         } else {
2402
2403                 /* get settings from advanced section of NSD */
2404
2405                 if (_startup->create_master_bus()) {
2406                         bus_profile.master_out_channels = (uint32_t) _startup->master_channel_count();
2407                 } else {
2408                         bus_profile.master_out_channels = 0;
2409                 }
2410
2411                 if (_startup->connect_inputs()) {
2412                         bus_profile.input_ac = AutoConnectPhysical;
2413                 } else {
2414                         bus_profile.input_ac = AutoConnectOption (0);
2415                 }
2416
2417                 bus_profile.output_ac = AutoConnectOption (0);
2418
2419                 if (_startup->connect_outputs ()) {
2420                         if (_startup->connect_outs_to_master()) {
2421                                 bus_profile.output_ac = AutoConnectMaster;
2422                         } else if (_startup->connect_outs_to_physical()) {
2423                                 bus_profile.output_ac = AutoConnectPhysical;
2424                         }
2425                 }
2426
2427                 bus_profile.requested_physical_in = (uint32_t) _startup->input_limit_count();
2428                 bus_profile.requested_physical_out = (uint32_t) _startup->output_limit_count();
2429         }
2430
2431         if (build_session (session_path, session_name, bus_profile)) {
2432                 return -1;
2433         }
2434
2435         return 0;
2436 }
2437
2438 void
2439 ARDOUR_UI::idle_load (const std::string& path)
2440 {
2441         if (_session) {
2442                 if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) {
2443                         /* /path/to/foo => /path/to/foo, foo */
2444                         load_session (path, basename_nosuffix (path));
2445                 } else {
2446                         /* /path/to/foo/foo.ardour => /path/to/foo, foo */
2447                         load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
2448                 }
2449
2450         } else {
2451                 ARDOUR_COMMAND_LINE::session_name = path;
2452         }
2453 }
2454
2455 /** @param quit_on_cancel true if exit() should be called if the user clicks `cancel' in the new session dialog */
2456 int
2457 ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, string load_template)
2458 {
2459         string session_name;
2460         string session_path;
2461         string template_name;
2462         int ret = -1;
2463         bool likely_new = false;
2464
2465         /* deal with any existing DIRTY session now, rather than later. don't
2466          * treat a non-dirty session this way, so that it stays visible 
2467          * as we bring up the new session dialog.
2468          */
2469
2470         if (_session && _session->dirty()) {
2471                 if (unload_session (false)) {
2472                         /* unload cancelled by user */
2473                         return 0;
2474                 }
2475                 ARDOUR_COMMAND_LINE::session_name = "";
2476         }
2477
2478         if (!load_template.empty()) {
2479                 should_be_new = true;
2480                 template_name = load_template;
2481         }
2482
2483         while (ret != 0) {
2484
2485                 if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
2486
2487                         /* if they named a specific statefile, use it, otherwise they are
2488                            just giving a session folder, and we want to use it as is
2489                            to find the session.
2490                         */
2491
2492                         string::size_type suffix = ARDOUR_COMMAND_LINE::session_name.find (statefile_suffix);
2493
2494                         if (suffix != string::npos) {
2495                                 session_path = Glib::path_get_dirname (ARDOUR_COMMAND_LINE::session_name);
2496                                 session_name = ARDOUR_COMMAND_LINE::session_name.substr (0, suffix);
2497                                 session_name = Glib::path_get_basename (session_name);
2498                         } else {
2499                                 session_path = ARDOUR_COMMAND_LINE::session_name;
2500                                 session_name = Glib::path_get_basename (ARDOUR_COMMAND_LINE::session_name);
2501                         }
2502                 } else {
2503                         session_path = "";
2504                         session_name = "";
2505                 }
2506
2507                 delete _startup;
2508                 _startup = new ArdourStartup (should_be_new, session_name, session_path, load_template);
2509                 
2510                 if (!_startup->ready_without_display()) {
2511                         _startup->present ();
2512                         main().run();
2513                         _startup->hide ();
2514                 }
2515                 
2516                 switch (_startup->response()) {
2517                 case RESPONSE_OK:
2518                         break;
2519                 default:
2520                         if (quit_on_cancel) {
2521                                 exit (1);
2522                         } else {
2523                                 return ret;
2524                         }
2525                 }
2526
2527                 /* if we run the startup dialog again, offer more than just "new session" */
2528                 
2529                 should_be_new = false;
2530                 
2531                 session_name = _startup->session_name (likely_new);
2532                 
2533                 string::size_type suffix = session_name.find (statefile_suffix);
2534                 
2535                 if (suffix != string::npos) {
2536                         session_name = session_name.substr (0, suffix);
2537                 }
2538                 
2539                 /* this shouldn't happen, but we catch it just in case it does */
2540                 
2541                 if (session_name.empty()) {
2542                         continue;
2543                 }
2544                 
2545                 if (_startup->use_session_template()) {
2546                         template_name = _startup->session_template_name();
2547                         _session_is_new = true;
2548                 }
2549                 
2550                 if (session_name[0] == G_DIR_SEPARATOR ||
2551                     (session_name.length() > 2 && session_name[0] == '.' && session_name[1] == G_DIR_SEPARATOR) ||
2552                     (session_name.length() > 3 && session_name[0] == '.' && session_name[1] == '.' && session_name[2] == G_DIR_SEPARATOR)) {
2553                         
2554                         /* absolute path or cwd-relative path specified for session name: infer session folder
2555                            from what was given.
2556                         */
2557                         
2558                         session_path = Glib::path_get_dirname (session_name);
2559                         session_name = Glib::path_get_basename (session_name);
2560                         
2561                 } else {
2562
2563                         session_path = _startup->session_folder();
2564                         
2565                         char illegal = Session::session_name_is_legal (session_name);
2566                         
2567                         if (illegal) {
2568                                 MessageDialog msg (*_startup,
2569                                                    string_compose (_("To ensure compatibility with various systems\n"
2570                                                                      "session names may not contain a '%1' character"),
2571                                                                    illegal));
2572                                 msg.run ();
2573                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2574                                 continue;
2575                         }
2576                 }
2577         
2578                 if (create_engine ()) {
2579                         break;
2580                 }
2581
2582                 if (Glib::file_test (session_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
2583
2584                         if (likely_new) {
2585
2586                                 std::string existing = Glib::build_filename (session_path, session_name);
2587
2588                                 if (!ask_about_loading_existing_session (existing)) {
2589                                         ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2590                                         continue;
2591                                 }
2592                         }
2593
2594                         _session_is_new = false;
2595
2596                 } else {
2597
2598                         if (!likely_new) {
2599                                 if (_startup) {
2600                                         pop_back_splash (*_startup);
2601                                 } else {
2602                                         hide_splash ();
2603                                 }
2604
2605                                 MessageDialog msg (string_compose (_("There is no existing session at \"%1\""), session_path));
2606                                 msg.run ();
2607                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2608                                 continue;
2609                         }
2610
2611                         char illegal = Session::session_name_is_legal(session_name);
2612                         if (illegal) {
2613                                 pop_back_splash (*_startup);
2614                                 MessageDialog msg (*_startup, string_compose(_("To ensure compatibility with various systems\n"
2615                                                      "session names may not contain a '%1' character"), illegal));
2616                                 msg.run ();
2617                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2618                                 continue;
2619                         }
2620
2621                         _session_is_new = true;
2622                 }
2623
2624                 if (likely_new && template_name.empty()) {
2625
2626                         ret = build_session_from_nsd (session_path, session_name);
2627
2628                 } else {
2629
2630                         ret = load_session (session_path, session_name, template_name);
2631
2632                         if (ret == -2) {
2633                                 /* not connected to the AudioEngine, so quit to avoid an infinite loop */
2634                                 exit (1);
2635                         }
2636
2637                         if (!ARDOUR_COMMAND_LINE::immediate_save.empty()) {
2638                                 _session->save_state (ARDOUR_COMMAND_LINE::immediate_save, false);
2639                                 exit (1);
2640                         }
2641                 }
2642         }
2643
2644         return ret;
2645 }
2646
2647 void
2648 ARDOUR_UI::close_session()
2649 {
2650         if (!check_audioengine()) {
2651                 return;
2652         }
2653
2654         if (unload_session (true)) {
2655                 return;
2656         }
2657
2658         ARDOUR_COMMAND_LINE::session_name = "";
2659
2660         if (get_session_parameters (true, false)) {
2661                 exit (1);
2662         }
2663
2664         goto_editor_window ();
2665 }
2666
2667 /** @param snap_name Snapshot name (without .ardour suffix).
2668  *  @return -2 if the load failed because we are not connected to the AudioEngine.
2669  */
2670 int
2671 ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name, std::string mix_template)
2672 {
2673         Session *new_session;
2674         int unload_status;
2675         int retval = -1;
2676
2677         session_loaded = false;
2678
2679         if (!check_audioengine()) {
2680                 return -2;
2681         }
2682
2683         unload_status = unload_session ();
2684
2685         if (unload_status < 0) {
2686                 goto out;
2687         } else if (unload_status > 0) {
2688                 retval = 0;
2689                 goto out;
2690         }
2691
2692         loading_message (string_compose (_("Please wait while %1 loads your session"), PROGRAM_NAME));
2693
2694         try {
2695                 new_session = new Session (*engine, path, snap_name, 0, mix_template);
2696         }
2697
2698         /* this one is special */
2699
2700         catch (AudioEngine::PortRegistrationFailure& err) {
2701
2702                 MessageDialog msg (err.what(),
2703                                    true,
2704                                    Gtk::MESSAGE_INFO,
2705                                    Gtk::BUTTONS_CLOSE);
2706
2707                 msg.set_title (_("Port Registration Error"));
2708                 msg.set_secondary_text (_("Click the Close button to try again."));
2709                 msg.set_position (Gtk::WIN_POS_CENTER);
2710                 pop_back_splash (msg);
2711                 msg.present ();
2712
2713                 int response = msg.run ();
2714
2715                 msg.hide ();
2716
2717                 switch (response) {
2718                 case RESPONSE_CANCEL:
2719                         exit (1);
2720                 default:
2721                         break;
2722                 }
2723                 goto out;
2724         }
2725
2726         catch (...) {
2727
2728                 MessageDialog msg (string_compose(
2729                                            _("Session \"%1 (snapshot %2)\" did not load successfully"),
2730                                            path, snap_name),
2731                                    true,
2732                                    Gtk::MESSAGE_INFO,
2733                                    BUTTONS_OK);
2734
2735                 msg.set_title (_("Loading Error"));
2736                 msg.set_secondary_text (_("Click the Refresh button to try again."));
2737                 msg.add_button (Stock::REFRESH, 1);
2738                 msg.set_position (Gtk::WIN_POS_CENTER);
2739                 pop_back_splash (msg);
2740                 msg.present ();
2741
2742                 int response = msg.run ();
2743
2744                 switch (response) {
2745                 case 1:
2746                         break;
2747                 default:
2748                         exit (1);
2749                 }
2750
2751                 msg.hide ();
2752
2753                 goto out;
2754         }
2755
2756         {
2757                 list<string> const u = new_session->unknown_processors ();
2758                 if (!u.empty()) {
2759                         MissingPluginDialog d (_session, u);
2760                         d.run ();
2761                 }
2762         }
2763
2764         /* Now the session been created, add the transport controls */
2765         new_session->add_controllable(roll_controllable);
2766         new_session->add_controllable(stop_controllable);
2767         new_session->add_controllable(goto_start_controllable);
2768         new_session->add_controllable(goto_end_controllable);
2769         new_session->add_controllable(auto_loop_controllable);
2770         new_session->add_controllable(play_selection_controllable);
2771         new_session->add_controllable(rec_controllable);
2772
2773         set_session (new_session);
2774
2775         session_loaded = true;
2776
2777         goto_editor_window ();
2778
2779         if (_session) {
2780                 _session->set_clean ();
2781         }
2782
2783         flush_pending ();
2784         retval = 0;
2785
2786   out:
2787         return retval;
2788 }
2789
2790 int
2791 ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name, BusProfile& bus_profile)
2792 {
2793         Session *new_session;
2794         int x;
2795
2796         if (!check_audioengine()) {
2797                 return -1;
2798         }
2799
2800         session_loaded = false;
2801
2802         x = unload_session ();
2803
2804         if (x < 0) {
2805                 return -1;
2806         } else if (x > 0) {
2807                 return 0;
2808         }
2809
2810         _session_is_new = true;
2811
2812         try {
2813                 new_session = new Session (*engine, path, snap_name, &bus_profile);
2814         }
2815
2816         catch (...) {
2817
2818                 MessageDialog msg (string_compose(_("Could not create session in \"%1\""), path));
2819                 pop_back_splash (msg);
2820                 msg.run ();
2821                 return -1;
2822         }
2823
2824         /* Give the new session the default GUI state, if such things exist */
2825
2826         XMLNode* n;
2827         n = Config->instant_xml (X_("Editor"));
2828         if (n) {
2829                 new_session->add_instant_xml (*n, false);
2830         }
2831         n = Config->instant_xml (X_("Mixer"));
2832         if (n) {
2833                 new_session->add_instant_xml (*n, false);
2834         }
2835
2836         /* Put the playhead at 0 and scroll fully left */
2837         n = new_session->instant_xml (X_("Editor"));
2838         if (n) {
2839                 n->add_property (X_("playhead"), X_("0"));
2840                 n->add_property (X_("left-frame"), X_("0"));
2841         }
2842
2843         set_session (new_session);
2844
2845         session_loaded = true;
2846
2847         new_session->save_state(new_session->name());
2848
2849         return 0;
2850 }
2851
2852 void
2853 ARDOUR_UI::launch_chat ()
2854 {
2855 #ifdef __APPLE__
2856         open_uri("http://webchat.freenode.net/?channels=ardour-osx");
2857 #else
2858         open_uri("http://webchat.freenode.net/?channels=ardour");
2859 #endif
2860 }
2861
2862 void
2863 ARDOUR_UI::show_about ()
2864 {
2865         if (about == 0) {
2866                 about = new About;
2867                 about->signal_response().connect (sigc::mem_fun (*this, &ARDOUR_UI::about_signal_response));
2868         }
2869
2870         about->set_transient_for(*editor);
2871         about->show_all ();
2872 }
2873
2874 void
2875 ARDOUR_UI::launch_manual ()
2876 {
2877         PBD::open_uri (Config->get_tutorial_manual_url());
2878 }
2879
2880 void
2881 ARDOUR_UI::launch_reference ()
2882 {
2883         PBD::open_uri (Config->get_reference_manual_url());
2884 }
2885
2886 void
2887 ARDOUR_UI::hide_about ()
2888 {
2889         if (about) {
2890                 about->get_window()->set_cursor ();
2891                 about->hide ();
2892         }
2893 }
2894
2895 void
2896 ARDOUR_UI::about_signal_response (int /*response*/)
2897 {
2898         hide_about();
2899 }
2900
2901 void
2902 ARDOUR_UI::loading_message (const std::string& msg)
2903 {
2904         if (ARDOUR_COMMAND_LINE::no_splash) {
2905                 return;
2906         }
2907
2908         if (!splash) {
2909                 show_splash ();
2910         }
2911
2912         splash->message (msg);
2913 }
2914
2915 void
2916 ARDOUR_UI::show_splash ()
2917 {
2918         if (splash == 0) {
2919                 try {
2920                         splash = new Splash;
2921                 } catch (...) {
2922                         return;
2923                 }
2924         }
2925
2926         splash->display ();
2927 }
2928
2929 void
2930 ARDOUR_UI::hide_splash ()
2931 {
2932         delete splash;
2933         splash = 0;
2934 }
2935
2936 void
2937 ARDOUR_UI::display_cleanup_results (ARDOUR::CleanupReport& rep, const gchar* list_title, const bool msg_delete)
2938 {
2939         size_t removed;
2940
2941         removed = rep.paths.size();
2942
2943         if (removed == 0) {
2944                 MessageDialog msgd (*editor,
2945                                     _("No files were ready for clean-up"),
2946                                     true,
2947                                     Gtk::MESSAGE_INFO,
2948                                     Gtk::BUTTONS_OK);
2949                 msgd.set_title (_("Clean-up"));
2950                 msgd.set_secondary_text (_("If this seems suprising, \n\
2951 check for any existing snapshots.\n\
2952 These may still include regions that\n\
2953 require some unused files to continue to exist."));
2954
2955                 msgd.run ();
2956                 return;
2957         }
2958
2959         ArdourDialog results (_("Clean-up"), true, false);
2960
2961         struct CleanupResultsModelColumns : public Gtk::TreeModel::ColumnRecord {
2962             CleanupResultsModelColumns() {
2963                     add (visible_name);
2964                     add (fullpath);
2965             }
2966             Gtk::TreeModelColumn<std::string> visible_name;
2967             Gtk::TreeModelColumn<std::string> fullpath;
2968         };
2969
2970
2971         CleanupResultsModelColumns results_columns;
2972         Glib::RefPtr<Gtk::ListStore> results_model;
2973         Gtk::TreeView results_display;
2974
2975         results_model = ListStore::create (results_columns);
2976         results_display.set_model (results_model);
2977         results_display.append_column (list_title, results_columns.visible_name);
2978
2979         results_display.set_name ("CleanupResultsList");
2980         results_display.set_headers_visible (true);
2981         results_display.set_headers_clickable (false);
2982         results_display.set_reorderable (false);
2983
2984         Gtk::ScrolledWindow list_scroller;
2985         Gtk::Label txt;
2986         Gtk::VBox dvbox;
2987         Gtk::HBox dhbox;  // the hbox for the image and text
2988         Gtk::HBox ddhbox; // the hbox we eventually pack into the dialog's vbox
2989         Gtk::Image* dimage = manage (new Gtk::Image(Stock::DIALOG_INFO,  Gtk::ICON_SIZE_DIALOG));
2990
2991         dimage->set_alignment(ALIGN_LEFT, ALIGN_TOP);
2992
2993         const string dead_directory = _session->session_directory().dead_path();
2994
2995         /* subst:
2996            %1 - number of files removed
2997            %2 - location of "dead"
2998            %3 - size of files affected
2999            %4 - prefix for "bytes" to produce sensible results (e.g. mega, kilo, giga)
3000         */
3001
3002         const char* bprefix;
3003         double space_adjusted = 0;
3004
3005         if (rep.space < 1000) {
3006                 bprefix = X_("");
3007                 space_adjusted = rep.space;
3008         } else if (rep.space < 1000000) {
3009                 bprefix = _("kilo");
3010                 space_adjusted = truncf((float)rep.space / 1000.0);
3011         } else if (rep.space < 1000000 * 1000) {
3012                 bprefix = _("mega");
3013                 space_adjusted = truncf((float)rep.space / (1000.0 * 1000.0));
3014         } else {
3015                 bprefix = _("giga");
3016                 space_adjusted = truncf((float)rep.space / (1000.0 * 1000 * 1000.0));
3017         }
3018
3019         if (msg_delete) {
3020                 txt.set_markup (string_compose (P_("\
3021 The following file was deleted from %2,\n\
3022 releasing %3 %4bytes of disk space", "\
3023 The following %1 files were deleted from %2,\n\
3024 releasing %3 %4bytes of disk space", removed),
3025                                         removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
3026         } else {
3027                 txt.set_markup (string_compose (P_("\
3028 The following file was not in use and \n\
3029 has been moved to: %2\n\n\
3030 After a restart of %5\n\n\
3031 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
3032 will release an additional %3 %4bytes of disk space.\n", "\
3033 The following %1 files were not in use and \n\
3034 have been moved to: %2\n\n\
3035 After a restart of %5\n\n\
3036 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
3037 will release an additional %3 %4bytes of disk space.\n", removed),
3038                                         removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
3039         }
3040
3041         dhbox.pack_start (*dimage, true, false, 5);
3042         dhbox.pack_start (txt, true, false, 5);
3043
3044         for (vector<string>::iterator i = rep.paths.begin(); i != rep.paths.end(); ++i) {
3045                 TreeModel::Row row = *(results_model->append());
3046                 row[results_columns.visible_name] = *i;
3047                 row[results_columns.fullpath] = *i;
3048         }
3049
3050         list_scroller.add (results_display);
3051         list_scroller.set_size_request (-1, 150);
3052         list_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
3053
3054         dvbox.pack_start (dhbox, true, false, 5);
3055         dvbox.pack_start (list_scroller, true, false, 5);
3056         ddhbox.pack_start (dvbox, true, false, 5);
3057
3058         results.get_vbox()->pack_start (ddhbox, true, false, 5);
3059         results.add_button (Stock::CLOSE, RESPONSE_CLOSE);
3060         results.set_default_response (RESPONSE_CLOSE);
3061         results.set_position (Gtk::WIN_POS_MOUSE);
3062
3063         results_display.show();
3064         list_scroller.show();
3065         txt.show();
3066         dvbox.show();
3067         dhbox.show();
3068         ddhbox.show();
3069         dimage->show();
3070
3071         //results.get_vbox()->show();
3072         results.set_resizable (false);
3073
3074         results.run ();
3075
3076 }
3077
3078 void
3079 ARDOUR_UI::cleanup ()
3080 {
3081         if (_session == 0) {
3082                 /* shouldn't happen: menu item is insensitive */
3083                 return;
3084         }
3085
3086
3087         MessageDialog checker (_("Are you sure you want to clean-up?"),
3088                                 true,
3089                                 Gtk::MESSAGE_QUESTION,
3090                                 Gtk::BUTTONS_NONE);
3091
3092         checker.set_title (_("Clean-up"));
3093
3094         checker.set_secondary_text(_("Clean-up is a destructive operation.\n\
3095 ALL undo/redo information will be lost if you clean-up.\n\
3096 Clean-up will move all unused files to a \"dead\" location."));
3097
3098         checker.add_button (Stock::CANCEL, RESPONSE_CANCEL);
3099         checker.add_button (_("Clean-up"), RESPONSE_ACCEPT);
3100         checker.set_default_response (RESPONSE_CANCEL);
3101
3102         checker.set_name (_("CleanupDialog"));
3103         checker.set_wmclass (X_("ardour_cleanup"), PROGRAM_NAME);
3104         checker.set_position (Gtk::WIN_POS_MOUSE);
3105
3106         switch (checker.run()) {
3107         case RESPONSE_ACCEPT:
3108                 break;
3109         default:
3110                 return;
3111         }
3112
3113         ARDOUR::CleanupReport rep;
3114
3115         editor->prepare_for_cleanup ();
3116
3117         /* do not allow flush until a session is reloaded */
3118
3119         Glib::RefPtr<Action> act = ActionManager::get_action (X_("Main"), X_("FlushWastebasket"));
3120         if (act) {
3121                 act->set_sensitive (false);
3122         }
3123
3124         if (_session->cleanup_sources (rep)) {
3125                 editor->finish_cleanup ();
3126                 return;
3127         }
3128
3129         editor->finish_cleanup ();
3130
3131         checker.hide();
3132         display_cleanup_results (rep, _("Cleaned Files"), false);
3133 }
3134
3135 void
3136 ARDOUR_UI::flush_trash ()
3137 {
3138         if (_session == 0) {
3139                 /* shouldn't happen: menu item is insensitive */
3140                 return;
3141         }
3142
3143         ARDOUR::CleanupReport rep;
3144
3145         if (_session->cleanup_trash_sources (rep)) {
3146                 return;
3147         }
3148
3149         display_cleanup_results (rep, _("deleted file"), true);
3150 }
3151
3152 void
3153 ARDOUR_UI::add_route (Gtk::Window* float_window)
3154 {
3155         int count;
3156
3157         if (!_session) {
3158                 return;
3159         }
3160
3161         if (add_route_dialog == 0) {
3162                 add_route_dialog = new AddRouteDialog (_session);
3163                 add_route_dialog->set_position (WIN_POS_MOUSE);
3164                 if (float_window) {
3165                         add_route_dialog->set_transient_for (*float_window);
3166                 }
3167         }
3168
3169         if (add_route_dialog->is_visible()) {
3170                 /* we're already doing this */
3171                 return;
3172         }
3173
3174         ResponseType r = (ResponseType) add_route_dialog->run ();
3175
3176         add_route_dialog->hide();
3177
3178         switch (r) {
3179                 case RESPONSE_ACCEPT:
3180                         break;
3181                 default:
3182                         return;
3183                         break;
3184         }
3185
3186         if ((count = add_route_dialog->count()) <= 0) {
3187                 return;
3188         }
3189
3190         PBD::ScopedConnection idle_connection;
3191
3192         if (count > 8) {
3193                 ARDOUR::GUIIdle.connect (idle_connection, MISSING_INVALIDATOR, boost::bind (&Gtkmm2ext::UI::flush_pending, this), gui_context());
3194         }
3195
3196         string template_path = add_route_dialog->track_template();
3197
3198         if (!template_path.empty()) {
3199                 _session->new_route_from_template (count, template_path);
3200                 return;
3201         }
3202
3203         ChanCount input_chan= add_route_dialog->channels ();
3204         ChanCount output_chan;
3205         string name_template = add_route_dialog->name_template ();
3206         PluginInfoPtr instrument = add_route_dialog->requested_instrument ();
3207         RouteGroup* route_group = add_route_dialog->route_group ();
3208         AutoConnectOption oac = Config->get_output_auto_connect();
3209
3210         if (oac & AutoConnectMaster) {
3211                 output_chan.set (DataType::AUDIO, (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan.n_audio()));
3212                 output_chan.set (DataType::MIDI, 0);
3213         } else {
3214                 output_chan = input_chan;
3215         }
3216
3217         /* XXX do something with name template */
3218
3219         switch (add_route_dialog->type_wanted()) {
3220         case AddRouteDialog::AudioTrack:
3221                 session_add_audio_track (input_chan.n_audio(), output_chan.n_audio(), add_route_dialog->mode(), route_group, count, name_template);
3222                 break;
3223         case AddRouteDialog::MidiTrack:
3224                 session_add_midi_track (route_group, count, name_template, instrument);
3225                 break;
3226         case AddRouteDialog::MixedTrack:
3227                 session_add_mixed_track (input_chan, output_chan, route_group, count, name_template, instrument);
3228                 break;
3229         case AddRouteDialog::AudioBus:
3230                 session_add_audio_bus (input_chan.n_audio(), output_chan.n_audio(), route_group, count, name_template);
3231                 break;
3232         }
3233
3234         /* idle connection will end at scope end */
3235 }
3236
3237 XMLNode*
3238 ARDOUR_UI::mixer_settings () const
3239 {
3240         XMLNode* node = 0;
3241
3242         if (_session) {
3243                 node = _session->instant_xml(X_("Mixer"));
3244         } else {
3245                 node = Config->instant_xml(X_("Mixer"));
3246         }
3247
3248         if (!node) {
3249                 node = new XMLNode (X_("Mixer"));
3250         }
3251
3252         return node;
3253 }
3254
3255 XMLNode*
3256 ARDOUR_UI::editor_settings () const
3257 {
3258         XMLNode* node = 0;
3259
3260         if (_session) {
3261                 node = _session->instant_xml(X_("Editor"));
3262         } else {
3263                 node = Config->instant_xml(X_("Editor"));
3264         }
3265
3266         if (!node) {
3267                 if (getenv("ARDOUR_INSTANT_XML_PATH")) {
3268                         node = Config->instant_xml(getenv("ARDOUR_INSTANT_XML_PATH"));
3269                 }
3270         }
3271
3272         if (!node) {
3273                 node = new XMLNode (X_("Editor"));
3274         }
3275
3276         return node;
3277 }
3278
3279 XMLNode*
3280 ARDOUR_UI::keyboard_settings () const
3281 {
3282         XMLNode* node = 0;
3283
3284         node = Config->extra_xml(X_("Keyboard"));
3285
3286         if (!node) {
3287                 node = new XMLNode (X_("Keyboard"));
3288         }
3289
3290         return node;
3291 }
3292
3293 void
3294 ARDOUR_UI::create_xrun_marker (framepos_t where)
3295 {
3296         editor->mouse_add_new_marker (where, false, true);
3297 }
3298
3299 void
3300 ARDOUR_UI::halt_on_xrun_message ()
3301 {
3302         MessageDialog msg (*editor,
3303                            _("Recording was stopped because your system could not keep up."));
3304         msg.run ();
3305 }
3306
3307 void
3308 ARDOUR_UI::xrun_handler (framepos_t where)
3309 {
3310         if (!_session) {
3311                 return;
3312         }
3313
3314         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::xrun_handler, where)
3315
3316         if (_session && Config->get_create_xrun_marker() && _session->actively_recording()) {
3317                 create_xrun_marker(where);
3318         }
3319
3320         if (_session && Config->get_stop_recording_on_xrun() && _session->actively_recording()) {
3321                 halt_on_xrun_message ();
3322         }
3323 }
3324
3325 void
3326 ARDOUR_UI::disk_overrun_handler ()
3327 {
3328         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::disk_overrun_handler)
3329
3330         if (!have_disk_speed_dialog_displayed) {
3331                 have_disk_speed_dialog_displayed = true;
3332                 MessageDialog* msg = new MessageDialog (*editor, string_compose (_("\
3333 The disk system on your computer\n\
3334 was not able to keep up with %1.\n\
3335 \n\
3336 Specifically, it failed to write data to disk\n\
3337 quickly enough to keep up with recording.\n"), PROGRAM_NAME));
3338                 msg->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::disk_speed_dialog_gone), msg));
3339                 msg->show ();
3340         }
3341 }
3342
3343 void
3344 ARDOUR_UI::disk_underrun_handler ()
3345 {
3346         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::disk_underrun_handler)
3347
3348         if (!have_disk_speed_dialog_displayed) {
3349                 have_disk_speed_dialog_displayed = true;
3350                 MessageDialog* msg = new MessageDialog (
3351                         *editor, string_compose (_("The disk system on your computer\n\
3352 was not able to keep up with %1.\n\
3353 \n\
3354 Specifically, it failed to read data from disk\n\
3355 quickly enough to keep up with playback.\n"), PROGRAM_NAME));
3356                 msg->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::disk_speed_dialog_gone), msg));
3357                 msg->show ();
3358         }
3359 }
3360
3361 void
3362 ARDOUR_UI::disk_speed_dialog_gone (int /*ignored_response*/, MessageDialog* msg)
3363 {
3364         have_disk_speed_dialog_displayed = false;
3365         delete msg;
3366 }
3367
3368 void
3369 ARDOUR_UI::session_dialog (std::string msg)
3370 {
3371         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::session_dialog, msg)
3372
3373         MessageDialog* d;
3374
3375         if (editor) {
3376                 d = new MessageDialog (*editor, msg, false, MESSAGE_INFO, BUTTONS_OK, true);
3377         } else {
3378                 d = new MessageDialog (msg, false, MESSAGE_INFO, BUTTONS_OK, true);
3379         }
3380
3381         d->show_all ();
3382         d->run ();
3383         delete d;
3384 }
3385
3386 int
3387 ARDOUR_UI::pending_state_dialog ()
3388 {
3389         HBox* hbox = new HBox();
3390         Image* image = new Image (Stock::DIALOG_QUESTION, ICON_SIZE_DIALOG);
3391         ArdourDialog dialog (_("Crash Recovery"), true);
3392         Label  message (string_compose (_("\
3393 This session appears to have been in the\n\
3394 middle of recording when %1 or\n\
3395 the computer was shutdown.\n\
3396 \n\
3397 %1 can recover any captured audio for\n\
3398 you, or it can ignore it. Please decide\n\
3399 what you would like to do.\n"), PROGRAM_NAME));
3400         image->set_alignment(ALIGN_CENTER, ALIGN_TOP);
3401         hbox->pack_start (*image, PACK_EXPAND_WIDGET, 12);
3402         hbox->pack_end (message, PACK_EXPAND_PADDING, 12);
3403         dialog.get_vbox()->pack_start(*hbox, PACK_EXPAND_PADDING, 6);
3404         dialog.add_button (_("Ignore crash data"), RESPONSE_REJECT);
3405         dialog.add_button (_("Recover from crash"), RESPONSE_ACCEPT);
3406         dialog.set_default_response (RESPONSE_ACCEPT);
3407         dialog.set_position (WIN_POS_CENTER);
3408         message.show();
3409         image->show();
3410         hbox->show();
3411
3412         switch (dialog.run ()) {
3413         case RESPONSE_ACCEPT:
3414                 return 1;
3415         default:
3416                 return 0;
3417         }
3418 }
3419
3420 int
3421 ARDOUR_UI::sr_mismatch_dialog (framecnt_t desired, framecnt_t actual)
3422 {
3423         HBox* hbox = new HBox();
3424         Image* image = new Image (Stock::DIALOG_WARNING, ICON_SIZE_DIALOG);
3425         ArdourDialog dialog (_("Sample Rate Mismatch"), true);
3426         Label  message (string_compose (_("\
3427 This session was created with a sample rate of %1 Hz, but\n\
3428 %2 is currently running at %3 Hz.  If you load this session,\n\
3429 audio may be played at the wrong sample rate.\n"), desired, PROGRAM_NAME, actual));
3430
3431         image->set_alignment(ALIGN_CENTER, ALIGN_TOP);
3432         hbox->pack_start (*image, PACK_EXPAND_WIDGET, 12);
3433         hbox->pack_end (message, PACK_EXPAND_PADDING, 12);
3434         dialog.get_vbox()->pack_start(*hbox, PACK_EXPAND_PADDING, 6);
3435         dialog.add_button (_("Do not load session"), RESPONSE_REJECT);
3436         dialog.add_button (_("Load session anyway"), RESPONSE_ACCEPT);
3437         dialog.set_default_response (RESPONSE_ACCEPT);
3438         dialog.set_position (WIN_POS_CENTER);
3439         message.show();
3440         image->show();
3441         hbox->show();
3442
3443         switch (dialog.run ()) {
3444         case RESPONSE_ACCEPT:
3445                 return 0;
3446         default:
3447                 return 1;
3448         }
3449 }
3450
3451
3452 void
3453 ARDOUR_UI::disconnect_from_jack ()
3454 {
3455         if (engine) {
3456                 if (engine->disconnect_from_jack ()) {
3457                         MessageDialog msg (*editor, _("Could not disconnect from JACK"));
3458                         msg.run ();
3459                 }
3460
3461                 update_sample_rate (0);
3462         }
3463 }
3464
3465 void
3466 ARDOUR_UI::reconnect_to_jack ()
3467 {
3468         if (engine) {
3469                 if (engine->reconnect_to_jack ()) {
3470                         MessageDialog msg (*editor,  _("Could not reconnect to JACK"));
3471                         msg.run ();
3472                 }
3473
3474                 update_sample_rate (0);
3475         }
3476 }
3477
3478 void
3479 ARDOUR_UI::use_config ()
3480 {
3481         XMLNode* node = Config->extra_xml (X_("TransportControllables"));
3482         if (node) {
3483                 set_transport_controllable_state (*node);
3484         }
3485 }
3486
3487 void
3488 ARDOUR_UI::update_transport_clocks (framepos_t pos)
3489 {
3490         if (Config->get_primary_clock_delta_edit_cursor()) {
3491                 primary_clock->set (pos, false, editor->get_preferred_edit_position());
3492         } else {
3493                 primary_clock->set (pos);
3494         }
3495
3496         if (Config->get_secondary_clock_delta_edit_cursor()) {
3497                 secondary_clock->set (pos, false, editor->get_preferred_edit_position());
3498         } else {
3499                 secondary_clock->set (pos);
3500         }
3501
3502         if (big_clock_window->get()) {
3503                 big_clock->set (pos);
3504         }
3505 }
3506
3507
3508 void
3509 ARDOUR_UI::step_edit_status_change (bool yn)
3510 {
3511         // XXX should really store pre-step edit status of things
3512         // we make insensitive
3513
3514         if (yn) {
3515                 rec_button.set_active_state (Gtkmm2ext::ImplicitActive);
3516                 rec_button.set_sensitive (false);
3517         } else {
3518                 rec_button.unset_active_state ();;
3519                 rec_button.set_sensitive (true);
3520         }
3521 }
3522
3523 void
3524 ARDOUR_UI::record_state_changed ()
3525 {
3526         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::record_state_changed);
3527
3528         if (!_session || !big_clock_window->get()) {
3529                 /* why bother - the clock isn't visible */
3530                 return;
3531         }
3532
3533         if (_session->record_status () == Session::Recording && _session->have_rec_enabled_track ()) {
3534                 big_clock->set_active (true);
3535         } else {
3536                 big_clock->set_active (false);
3537         }
3538 }
3539
3540 bool
3541 ARDOUR_UI::first_idle ()
3542 {
3543         if (_session) {
3544                 _session->allow_auto_play (true);
3545         }
3546
3547         if (editor) {
3548                 editor->first_idle();
3549         }
3550
3551         Keyboard::set_can_save_keybindings (true);
3552         return false;
3553 }
3554
3555 void
3556 ARDOUR_UI::store_clock_modes ()
3557 {
3558         XMLNode* node = new XMLNode(X_("ClockModes"));
3559
3560         for (vector<AudioClock*>::iterator x = AudioClock::clocks.begin(); x != AudioClock::clocks.end(); ++x) {
3561                 XMLNode* child = new XMLNode (X_("Clock"));
3562                 
3563                 child->add_property (X_("name"), (*x)->name());
3564                 child->add_property (X_("mode"), enum_2_string ((*x)->mode()));
3565                 child->add_property (X_("on"), ((*x)->off() ? X_("no") : X_("yes")));
3566
3567                 node->add_child_nocopy (*child);
3568         }
3569
3570         _session->add_extra_xml (*node);
3571         _session->set_dirty ();
3572 }
3573
3574 ARDOUR_UI::TransportControllable::TransportControllable (std::string name, ARDOUR_UI& u, ToggleType tp)
3575         : Controllable (name), ui (u), type(tp)
3576 {
3577
3578 }
3579
3580 void
3581 ARDOUR_UI::TransportControllable::set_value (double val)
3582 {
3583         if (val < 0.5) {
3584                 /* do nothing: these are radio-style actions */
3585                 return;
3586         }
3587
3588         const char *action = 0;
3589
3590         switch (type) {
3591         case Roll:
3592                 action = X_("Roll");
3593                 break;
3594         case Stop:
3595                 action = X_("Stop");
3596                 break;
3597         case GotoStart:
3598                 action = X_("GotoStart");
3599                 break;
3600         case GotoEnd:
3601                 action = X_("GotoEnd");
3602                 break;
3603         case AutoLoop:
3604                 action = X_("Loop");
3605                 break;
3606         case PlaySelection:
3607                 action = X_("PlaySelection");
3608                 break;
3609         case RecordEnable:
3610                 action = X_("Record");
3611                 break;
3612         default:
3613                 break;
3614         }
3615
3616         if (action == 0) {
3617                 return;
3618         }
3619
3620         Glib::RefPtr<Action> act = ActionManager::get_action ("Transport", action);
3621
3622         if (act) {
3623                 act->activate ();
3624         }
3625 }
3626
3627 double
3628 ARDOUR_UI::TransportControllable::get_value (void) const
3629 {
3630         float val = 0.0;
3631
3632         switch (type) {
3633         case Roll:
3634                 break;
3635         case Stop:
3636                 break;
3637         case GotoStart:
3638                 break;
3639         case GotoEnd:
3640                 break;
3641         case AutoLoop:
3642                 break;
3643         case PlaySelection:
3644                 break;
3645         case RecordEnable:
3646                 break;
3647         default:
3648                 break;
3649         }
3650
3651         return val;
3652 }
3653
3654 void
3655 ARDOUR_UI::setup_profile ()
3656 {
3657         if (gdk_screen_width() < 1200 || getenv ("ARDOUR_NARROW_SCREEN")) {
3658                 Profile->set_small_screen ();
3659         }
3660
3661         if (getenv ("ARDOUR_SAE")) {
3662                 Profile->set_sae ();
3663                 Profile->set_single_package ();
3664         }
3665 }
3666
3667 /** Add a window proxy to our list, so that its state will be saved.
3668  *  This call also causes the window to be created and opened if its
3669  *  state was saved as `visible'.
3670  */
3671 void
3672 ARDOUR_UI::add_window_proxy (WindowProxyBase* p)
3673 {
3674         _window_proxies.push_back (p);
3675         p->maybe_show ();
3676 }
3677
3678 /** Remove a window proxy from our list.  Must be called if a WindowProxy
3679  *  is deleted, to prevent hanging pointers.
3680  */
3681 void
3682 ARDOUR_UI::remove_window_proxy (WindowProxyBase* p)
3683 {
3684         _window_proxies.remove (p);
3685 }
3686
3687 int
3688 ARDOUR_UI::missing_file (Session*s, std::string str, DataType type)
3689 {
3690         MissingFileDialog dialog (s, str, type);
3691
3692         dialog.show ();
3693         dialog.present ();
3694
3695         int result = dialog.run ();
3696         dialog.hide ();
3697
3698         switch (result) {
3699         case RESPONSE_OK:
3700                 break;
3701         default:
3702                 return 1; // quit entire session load
3703         }
3704
3705         result = dialog.get_action ();
3706
3707         return result;
3708 }
3709
3710 int
3711 ARDOUR_UI::ambiguous_file (std::string file, std::string /*path*/, std::vector<std::string> hits)
3712 {
3713         AmbiguousFileDialog dialog (file, hits);
3714
3715         dialog.show ();
3716         dialog.present ();
3717
3718         dialog.run ();
3719         return dialog.get_which ();
3720 }
3721
3722 /** Allocate our thread-local buffers */
3723 void
3724 ARDOUR_UI::get_process_buffers ()
3725 {
3726         _process_thread->get_buffers ();
3727 }
3728
3729 /** Drop our thread-local buffers */
3730 void
3731 ARDOUR_UI::drop_process_buffers ()
3732 {
3733         _process_thread->drop_buffers ();
3734 }
3735
3736 void
3737 ARDOUR_UI::feedback_detected ()
3738 {
3739         _feedback_exists = true;
3740 }
3741
3742 void
3743 ARDOUR_UI::successful_graph_sort ()
3744 {
3745         _feedback_exists = false;
3746 }
3747
3748 void
3749 ARDOUR_UI::midi_panic ()
3750 {
3751         if (_session) {
3752                 _session->midi_panic();
3753         }
3754 }
3755
3756 void
3757 ARDOUR_UI::session_format_mismatch (std::string xml_path, std::string backup_path)
3758 {
3759         const char* start_big = "<span size=\"x-large\" weight=\"bold\">";
3760         const char* end_big = "</span>";
3761         const char* start_mono = "<tt>";
3762         const char* end_mono = "</tt>";
3763
3764         MessageDialog msg (string_compose (_("%4This is a session from an older version of %3%5\n\n"
3765                                              "%3 has copied the old session file\n\n%6%1%7\n\nto\n\n%6%2%7\n\n"
3766                                              "From now on, use the -2000 version with older versions of %3"),
3767                                            xml_path, backup_path, PROGRAM_NAME,
3768                                            start_big, end_big,
3769                                            start_mono, end_mono), true);
3770
3771         msg.run ();
3772 }