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