forward port 2.X changes up to and including rev 6842
[ardour.git] / gtk2_ardour / ardour_ui_ed.cc
1 /*
2     Copyright (C) 20002-2004 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 /* This file contains any ARDOUR_UI methods that require knowledge of
21    the editor, and exists so that no compilation dependency exists
22    between the main ARDOUR_UI modules and the PublicEditor class. This
23    is to cut down on the nasty compile times for both these classes.
24 */
25
26 #include <cmath>
27
28 #include <glibmm/miscutils.h>
29 #include <gtk/gtk.h>
30
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/window_title.h"
33 #include "gtkmm2ext/tearoff.h"
34
35 #include "pbd/file_utils.h"
36 #include "pbd/fpu.h"
37 #include "pbd/convert.h"
38
39 #include "ardour_ui.h"
40 #include "public_editor.h"
41 #include "audio_clock.h"
42 #include "keyboard.h"
43 #include "monitor_section.h"
44 #include "engine_dialog.h"
45 #include "editor.h"
46 #include "actions.h"
47 #include "mixer_ui.h"
48 #include "startup.h"
49 #include "utils.h"
50
51 #include <gtkmm2ext/application.h>
52
53 #include "ardour/session.h"
54 #include "ardour/profile.h"
55 #include "ardour/audioengine.h"
56 #include "ardour/control_protocol_manager.h"
57
58 #include "control_protocol/control_protocol.h"
59
60 #include "i18n.h"
61
62 using namespace std;
63 using namespace ARDOUR;
64 using namespace PBD;
65 using namespace Gtkmm2ext;
66 using namespace Gtk;
67 using namespace Glib;
68
69 int
70 ARDOUR_UI::create_editor ()
71
72 {
73         try {
74                 editor = new Editor ();
75         }
76
77         catch (failed_constructor& err) {
78                 return -1;
79         }
80
81         editor->Realized.connect (sigc::mem_fun (*this, &ARDOUR_UI::editor_realized));
82         editor->signal_window_state_event().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::main_window_state_event_handler), true));
83
84         return 0;
85 }
86
87 void
88 ARDOUR_UI::install_actions ()
89 {
90         Glib::RefPtr<ActionGroup> main_actions = ActionGroup::create (X_("Main"));
91         Glib::RefPtr<Action> act;
92
93         /* menus + submenus that need action items */
94
95         ActionManager::register_action (main_actions, X_("Session"), _("Session"));
96         act = ActionManager::register_action (main_actions, X_("Cleanup"), _("Cleanup"));
97         ActionManager::write_sensitive_actions.push_back (act);
98         ActionManager::register_action (main_actions, X_("Sync"), _("Sync"));
99         ActionManager::register_action (main_actions, X_("TransportOptions"), _("Options"));
100         ActionManager::register_action (main_actions, X_("Help"), _("Help"));
101         ActionManager::register_action (main_actions, X_("KeyMouseActions"), _("Misc. Shortcuts"));
102         ActionManager::register_action (main_actions, X_("AudioFileFormat"), _("Audio File Format"));
103         ActionManager::register_action (main_actions, X_("AudioFileFormatHeader"), _("File Type"));
104         ActionManager::register_action (main_actions, X_("AudioFileFormatData"), _("Sample Format"));
105         ActionManager::register_action (main_actions, X_("ControlSurfaces"), _("Control Surfaces"));
106         ActionManager::register_action (main_actions, X_("Plugins"), _("Plugins"));
107         ActionManager::register_action (main_actions, X_("Metering"), _("Metering"));
108         ActionManager::register_action (main_actions, X_("MeteringFallOffRate"), _("Fall Off Rate"));
109         ActionManager::register_action (main_actions, X_("MeteringHoldTime"), _("Hold Time"));
110         ActionManager::register_action (main_actions, X_("Denormals"), _("Denormal Handling"));
111
112         /* the real actions */
113
114         act = ActionManager::register_action (main_actions, X_("New"), _("New..."),  hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::get_session_parameters), false, true, "")));
115
116         ActionManager::register_action (main_actions, X_("Open"), _("Open..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_session));
117         ActionManager::register_action (main_actions, X_("Recent"), _("Recent..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_recent_session));
118         act = ActionManager::register_action (main_actions, X_("Close"), _("Close"),  sigc::mem_fun(*this, &ARDOUR_UI::close_session));
119         ActionManager::session_sensitive_actions.push_back (act);
120
121         act = ActionManager::register_action (main_actions, X_("AddTrackBus"), _("Add Track or Bus..."),
122                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_route), (Gtk::Window*) 0));
123         ActionManager::session_sensitive_actions.push_back (act);
124         ActionManager::write_sensitive_actions.push_back (act);
125
126 #ifdef WITH_CMT
127
128         sys::path anicomp_file_path;
129
130         if (PBD::find_file_in_search_path (Glib::getenv("PATH"), "AniComp", anicomp_file_path)) {
131                 act = ActionManager::register_action (main_actions, X_("aniConnect"), _("Connect"),  (sigc::mem_fun (*editor, &PublicEditor::connect_to_image_compositor)));
132                 ActionManager::session_sensitive_actions.push_back (act);
133         }
134
135 #endif
136
137         act = ActionManager::register_action (main_actions, X_("Snapshot"), _("Snapshot..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), false));
138         ActionManager::session_sensitive_actions.push_back (act);
139         ActionManager::write_sensitive_actions.push_back (act);
140
141         act = ActionManager::register_action (main_actions, X_("SaveAs"), _("Save As..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), true));
142         ActionManager::session_sensitive_actions.push_back (act);
143         ActionManager::write_sensitive_actions.push_back (act);
144
145         act = ActionManager::register_action (main_actions, X_("SaveTemplate"), _("Save Template..."),  sigc::mem_fun(*this, &ARDOUR_UI::save_template));
146         ActionManager::session_sensitive_actions.push_back (act);
147
148         act = ActionManager::register_action (main_actions, X_("Metadata"), _("Metadata"));
149         ActionManager::session_sensitive_actions.push_back (act);
150
151         act = ActionManager::register_action (main_actions, X_("EditMetadata"), _("Edit Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::edit_metadata));
152         ActionManager::session_sensitive_actions.push_back (act);
153
154         act = ActionManager::register_action (main_actions, X_("ImportMetadata"), _("Import Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::import_metadata));
155         ActionManager::session_sensitive_actions.push_back (act);
156
157         act = ActionManager::register_action (main_actions, X_("ExportAudio"), _("Export To Audio File(s)..."),  sigc::mem_fun (*editor, &PublicEditor::export_audio));
158         ActionManager::session_sensitive_actions.push_back (act);
159
160         act = ActionManager::register_action (main_actions, X_("Export"), _("Export"));
161         ActionManager::session_sensitive_actions.push_back (act);
162
163         act = ActionManager::register_action (main_actions, X_("CleanupUnused"), _("Cleanup Unused Sources..."),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::cleanup));
164         ActionManager::session_sensitive_actions.push_back (act);
165         ActionManager::write_sensitive_actions.push_back (act);
166
167         act = ActionManager::register_action (main_actions, X_("FlushWastebasket"), _("Flush Wastebasket"),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::flush_trash));
168         ActionManager::write_sensitive_actions.push_back (act);
169         ActionManager::session_sensitive_actions.push_back (act);
170
171         /* JACK actions for controlling ... JACK */
172
173         Glib::RefPtr<ActionGroup> jack_actions = ActionGroup::create (X_("JACK"));
174         ActionManager::register_action (jack_actions, X_("JACK"), _("JACK"));
175         ActionManager::register_action (jack_actions, X_("Latency"), _("Latency"));
176
177         act = ActionManager::register_action (jack_actions, X_("JACKReconnect"), _("Reconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::reconnect_to_jack));
178         ActionManager::jack_opposite_sensitive_actions.push_back (act);
179
180         act = ActionManager::register_action (jack_actions, X_("JACKDisconnect"), _("Disconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::disconnect_from_jack));
181         ActionManager::jack_sensitive_actions.push_back (act);
182
183         RadioAction::Group jack_latency_group;
184
185         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency32"), X_("32"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 32));
186         ActionManager::jack_sensitive_actions.push_back (act);
187         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency64"), X_("64"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 64));
188         ActionManager::jack_sensitive_actions.push_back (act);
189         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency128"), X_("128"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 128));
190         ActionManager::jack_sensitive_actions.push_back (act);
191         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency256"), X_("256"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 256));
192         ActionManager::jack_sensitive_actions.push_back (act);
193         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency512"), X_("512"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 512));
194         ActionManager::jack_sensitive_actions.push_back (act);
195         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency1024"), X_("1024"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 1024));
196         ActionManager::jack_sensitive_actions.push_back (act);
197         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency2048"), X_("2048"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 2048));
198         ActionManager::jack_sensitive_actions.push_back (act);
199         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency4096"), X_("4096"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 4096));
200         ActionManager::jack_sensitive_actions.push_back (act);
201         act = ActionManager::register_radio_action (jack_actions, jack_latency_group, X_("JACKLatency8192"), X_("8192"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::set_jack_buffer_size), (nframes_t) 8192));
202         ActionManager::jack_sensitive_actions.push_back (act);
203
204         /* these actions are intended to be shared across all windows */
205
206         common_actions = ActionGroup::create (X_("Common"));
207         ActionManager::register_action (main_actions, X_("WindowMenu"), _("Window"));
208         ActionManager::register_action (common_actions, X_("Quit"), _("Quit"), (hide_return (sigc::mem_fun(*this, &ARDOUR_UI::finish))));
209
210         /* windows visibility actions */
211
212         ActionManager::register_toggle_action (common_actions, X_("ToggleMaximalEditor"), _("Maximise Editor Space"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_editing_space));
213         act = ActionManager::register_toggle_action (common_actions, X_("KeepTearoffs"), _("Toolbars when Maximised"), mem_fun (*this, &ARDOUR_UI::toggle_keep_tearoffs));
214         ActionManager::session_sensitive_actions.push_back (act);
215
216         ActionManager::register_action (common_actions, X_("goto-editor"), _("Show Editor"),  sigc::mem_fun(*this, &ARDOUR_UI::goto_editor_window));
217         ActionManager::register_action (common_actions, X_("goto-mixer"), _("Show Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::goto_mixer_window));
218         ActionManager::register_action (common_actions, X_("toggle-editor-mixer-on-top"), _("Toggle Editor Mixer on Top"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_editor_mixer_on_top));
219         ActionManager::register_toggle_action (common_actions, X_("ToggleRCOptionsEditor"), _("Preferences"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_rc_options_window));
220         ActionManager::register_toggle_action (common_actions, X_("ToggleSessionOptionsEditor"), _("Properties"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_options_window));
221         act = ActionManager::register_toggle_action (common_actions, X_("ToggleInspector"), _("Tracks and Busses"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_route_params_window));
222         ActionManager::session_sensitive_actions.push_back (act);
223         ActionManager::session_sensitive_actions.push_back (act);
224         act = ActionManager::register_toggle_action (common_actions, X_("ToggleLocations"), _("Locations"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_location_window));
225         ActionManager::session_sensitive_actions.push_back (act);
226         act = ActionManager::register_toggle_action (common_actions, X_("ToggleBigClock"), _("Big Clock"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_big_clock_window));
227         ActionManager::session_sensitive_actions.push_back (act);
228         act = ActionManager::register_action (common_actions, X_("NewMIDITracer"), _("MIDI Tracer"), sigc::mem_fun(*this, &ARDOUR_UI::new_midi_tracer_window));
229         ActionManager::session_sensitive_actions.push_back (act);
230         ActionManager::register_action (common_actions, X_("About"), _("About"),  sigc::mem_fun(*this, &ARDOUR_UI::show_about));
231         ActionManager::register_action (common_actions, X_("Chat"), _("Chat"),  sigc::mem_fun(*this, &ARDOUR_UI::launch_chat));
232         ActionManager::register_action (common_actions, X_("Manual"), _("Manual"),  mem_fun(*this, &ARDOUR_UI::launch_manual));
233         ActionManager::register_action (common_actions, X_("Reference"), _("Reference"),  mem_fun(*this, &ARDOUR_UI::launch_reference));
234         ActionManager::register_toggle_action (common_actions, X_("ToggleThemeManager"), _("Theme Manager"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_theme_manager));
235         ActionManager::register_toggle_action (common_actions, X_("ToggleKeyEditor"), _("Key Bindings"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_key_editor));
236         ActionManager::register_toggle_action (common_actions, X_("ToggleBundleManager"), _("Bundle Manager"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_bundle_manager));
237
238 #if 0
239         act = ActionManager::register_action (common_actions, X_("AddAudioTrack"), _("Add Audio Track"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::session_add_audio_track), 1, 1, ARDOUR::Normal, (ARDOUR::RouteGroup *) 0, 1));
240         ActionManager::session_sensitive_actions.push_back (act);
241         act = ActionManager::register_action (common_actions, X_("AddAudioBus"), _("Add Audio Bus"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::session_add_audio_bus), 1, 1, (ARDOUR::RouteGroup *) 0, 1));
242         ActionManager::session_sensitive_actions.push_back (act);
243         act = ActionManager::register_action (common_actions, X_("AddMIDITrack"), _("Add MIDI Track"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::session_add_midi_track), (ARDOUR::RouteGroup *) 0, 1));
244         ActionManager::session_sensitive_actions.push_back (act);
245         //act = ActionManager::register_action (common_actions, X_("AddMidiBus"), _("Add Midi Bus"), sigc::mem_fun(*this, &ARDOUR_UI::session_add_midi_bus));
246         //ActionManager::session_sensitive_actions.push_back (act);
247 #endif
248         act = ActionManager::register_action (common_actions, X_("Save"), _("Save"),  sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::save_state), string(""), false));
249         ActionManager::session_sensitive_actions.push_back (act);
250         ActionManager::write_sensitive_actions.push_back (act);
251         act = ActionManager::register_action (common_actions, X_("RemoveLastCapture"), _("Remove Last Capture"), sigc::mem_fun(*this, &ARDOUR_UI::remove_last_capture));
252         ActionManager::session_sensitive_actions.push_back (act);
253
254         Glib::RefPtr<ActionGroup> transport_actions = ActionGroup::create (X_("Transport"));
255
256         /* do-nothing action for the "transport" menu bar item */
257
258         ActionManager::register_action (transport_actions, X_("Transport"), _("Transport"));
259
260         /* these two are not used by key bindings, instead use ToggleRoll for that. these two do show up in
261            menus and via button proxies.
262         */
263
264         act = ActionManager::register_action (transport_actions, X_("Stop"), _("Stop"), sigc::mem_fun(*this, &ARDOUR_UI::transport_stop));
265         ActionManager::session_sensitive_actions.push_back (act);
266         ActionManager::transport_sensitive_actions.push_back (act);
267         act = ActionManager::register_action (transport_actions, X_("Roll"), _("Roll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_roll));
268         ActionManager::session_sensitive_actions.push_back (act);
269         ActionManager::transport_sensitive_actions.push_back (act);
270
271         act = ActionManager::register_action (transport_actions, X_("ToggleRoll"), _("Start/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, false));
272         ActionManager::session_sensitive_actions.push_back (act);
273         ActionManager::transport_sensitive_actions.push_back (act);
274         act = ActionManager::register_action (transport_actions, X_("ToggleRollMaybe"), _("Start/Continue/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, true));
275         ActionManager::session_sensitive_actions.push_back (act);
276         ActionManager::transport_sensitive_actions.push_back (act);
277         act = ActionManager::register_action (transport_actions, X_("ToggleRollForgetCapture"), _("Stop and Forget Capture"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::toggle_roll), true, false));
278         ActionManager::session_sensitive_actions.push_back (act);
279         ActionManager::transport_sensitive_actions.push_back (act);
280
281         /* these two behave as follows:
282
283            - if transport speed != 1.0 or != -1.0, change speed to 1.0 or -1.0 (respectively)
284            - otherwise do nothing
285         */
286
287         act = ActionManager::register_action (transport_actions, X_("TransitionToRoll"), _("Transition To Roll"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), true));
288         ActionManager::session_sensitive_actions.push_back (act);
289         ActionManager::transport_sensitive_actions.push_back (act);
290
291         act = ActionManager::register_action (transport_actions, X_("TransitionToReverse"), _("Transition To Reverse"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), false));
292         ActionManager::session_sensitive_actions.push_back (act);
293         ActionManager::transport_sensitive_actions.push_back (act);
294
295         act = ActionManager::register_action (transport_actions, X_("Loop"), _("Play Loop Range"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_auto_loop));
296         ActionManager::session_sensitive_actions.push_back (act);
297         ActionManager::transport_sensitive_actions.push_back (act);
298         act = ActionManager::register_action (transport_actions, X_("PlaySelection"), _("Play Selection"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_selection));
299         ActionManager::session_sensitive_actions.push_back (act);
300         ActionManager::transport_sensitive_actions.push_back (act);
301
302         act = ActionManager::register_action (transport_actions, X_("Record"), _("Enable Record"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), false));
303         ActionManager::session_sensitive_actions.push_back (act);
304         ActionManager::write_sensitive_actions.push_back (act);
305         act = ActionManager::register_action (transport_actions, X_("record-roll"), _("Start Recording"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), true));
306         ActionManager::session_sensitive_actions.push_back (act);
307         ActionManager::write_sensitive_actions.push_back (act);
308         ActionManager::transport_sensitive_actions.push_back (act);
309         act = ActionManager::register_action (transport_actions, X_("Rewind"), _("Rewind"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 0));
310         ActionManager::session_sensitive_actions.push_back (act);
311         ActionManager::transport_sensitive_actions.push_back (act);
312         act = ActionManager::register_action (transport_actions, X_("RewindSlow"), _("Rewind (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), -1));
313         ActionManager::session_sensitive_actions.push_back (act);
314         ActionManager::transport_sensitive_actions.push_back (act);
315         act = ActionManager::register_action (transport_actions, X_("RewindFast"), _("Rewind (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 1));
316         ActionManager::session_sensitive_actions.push_back (act);
317         ActionManager::transport_sensitive_actions.push_back (act);
318         act = ActionManager::register_action (transport_actions, X_("Forward"), _("Forward"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 0));
319         ActionManager::session_sensitive_actions.push_back (act);
320         ActionManager::transport_sensitive_actions.push_back (act);
321         act = ActionManager::register_action (transport_actions, X_("ForwardSlow"), _("Forward (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), -1));
322         ActionManager::session_sensitive_actions.push_back (act);
323         ActionManager::transport_sensitive_actions.push_back (act);
324         act = ActionManager::register_action (transport_actions, X_("ForwardFast"), _("Forward (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 1));
325         ActionManager::session_sensitive_actions.push_back (act);
326         ActionManager::transport_sensitive_actions.push_back (act);
327         act = ActionManager::register_action (transport_actions, X_("GotoZero"), _("Goto Zero"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_zero));
328         ActionManager::session_sensitive_actions.push_back (act);
329         ActionManager::transport_sensitive_actions.push_back (act);
330         act = ActionManager::register_action (transport_actions, X_("GotoStart"), _("Goto Start"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_start));
331         ActionManager::session_sensitive_actions.push_back (act);
332         ActionManager::transport_sensitive_actions.push_back (act);
333         act = ActionManager::register_action (transport_actions, X_("GotoEnd"), _("Goto End"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_end));
334         ActionManager::session_sensitive_actions.push_back (act);
335         ActionManager::transport_sensitive_actions.push_back (act);
336         act = ActionManager::register_action (transport_actions, X_("GotoWallClock"), _("Goto Wall Clock"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_wallclock));
337         ActionManager::session_sensitive_actions.push_back (act);
338         ActionManager::transport_sensitive_actions.push_back (act);
339
340         act = ActionManager::register_action (transport_actions, X_("focus-on-clock"), _("Focus On Clock"), sigc::mem_fun(primary_clock, &AudioClock::focus));
341         ActionManager::session_sensitive_actions.push_back (act);
342         ActionManager::transport_sensitive_actions.push_back (act);
343
344         act = ActionManager::register_action (transport_actions, X_("primary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::BBT));
345         ActionManager::session_sensitive_actions.push_back (act);
346         act = ActionManager::register_action (transport_actions, X_("primary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::MinSec));
347         ActionManager::session_sensitive_actions.push_back (act);
348         act = ActionManager::register_action (transport_actions, X_("primary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Frames));
349         ActionManager::session_sensitive_actions.push_back (act);
350         act = ActionManager::register_action (transport_actions, X_("primary-clock-off"), _("Off"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Off));
351         ActionManager::session_sensitive_actions.push_back (act);
352
353         act = ActionManager::register_action (transport_actions, X_("secondary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::BBT));
354         ActionManager::session_sensitive_actions.push_back (act);
355         act = ActionManager::register_action (transport_actions, X_("secondary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::MinSec));
356         ActionManager::session_sensitive_actions.push_back (act);
357         act = ActionManager::register_action (transport_actions, X_("secondary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Frames));
358         ActionManager::session_sensitive_actions.push_back (act);
359         act = ActionManager::register_action (transport_actions, X_("secondary-clock-off"), _("Off"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Off));
360         ActionManager::session_sensitive_actions.push_back (act);
361
362         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchIn"), _("Punch In"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_in));
363         ActionManager::session_sensitive_actions.push_back (act);
364         ActionManager::transport_sensitive_actions.push_back (act);
365         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchOut"), _("Punch Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_out));
366         ActionManager::session_sensitive_actions.push_back (act);
367         ActionManager::transport_sensitive_actions.push_back (act);
368         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunch"), _("Punch In/Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch));
369         ActionManager::session_sensitive_actions.push_back (act);
370         ActionManager::transport_sensitive_actions.push_back (act);
371         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleClick"), _("Click"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_click));
372         ActionManager::session_sensitive_actions.push_back (act);
373         ActionManager::transport_sensitive_actions.push_back (act);
374         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoInput"), _("Auto Input"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_input));
375         ActionManager::session_sensitive_actions.push_back (act);
376         ActionManager::transport_sensitive_actions.push_back (act);
377         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoPlay"), _("Auto Play"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_play));
378         ActionManager::session_sensitive_actions.push_back (act);
379         ActionManager::transport_sensitive_actions.push_back (act);
380         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoReturn"), _("Auto Return"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_return));
381         ActionManager::session_sensitive_actions.push_back (act);
382         ActionManager::transport_sensitive_actions.push_back (act);
383
384         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleVideoSync"), _("Sync Startup to Video"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_video_sync));
385         ActionManager::session_sensitive_actions.push_back (act);
386         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleTimeMaster"), _("Time Master"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_time_master));
387         ActionManager::session_sensitive_actions.push_back (act);
388         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleExternalSync"), _(""), sigc::mem_fun(*this, &ARDOUR_UI::toggle_external_sync));
389         ActionManager::session_sensitive_actions.push_back (act);
390
391         for (int i = 1; i <= 32; ++i) {
392                 string const a = string_compose (X_("ToggleRecordEnableTrack%1"), i);
393                 string const n = string_compose (_("Toggle Record Enable Track %1"), i);
394                 act = ActionManager::register_action (common_actions, a.c_str(), n.c_str(), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_record_enable), i - 1));
395                 ActionManager::session_sensitive_actions.push_back (act);
396         }
397
398         Glib::RefPtr<ActionGroup> shuttle_actions = ActionGroup::create ("ShuttleActions");
399
400         shuttle_actions->add (Action::create (X_("SetShuttleUnitsPercentage"), _("Percentage")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Percentage)));
401         shuttle_actions->add (Action::create (X_("SetShuttleUnitsSemitones"), _("Semitones")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Semitones)));
402
403         Glib::RefPtr<ActionGroup> option_actions = ActionGroup::create ("options");
404
405         act = ActionManager::register_toggle_action (option_actions, X_("SendMTC"), _("Send MTC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mtc));
406         ActionManager::session_sensitive_actions.push_back (act);
407         act = ActionManager::register_toggle_action (option_actions, X_("SendMMC"), _("Send MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mmc));
408         ActionManager::session_sensitive_actions.push_back (act);
409         act = ActionManager::register_toggle_action (option_actions, X_("UseMMC"), _("Use MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_use_mmc));
410         ActionManager::session_sensitive_actions.push_back (act);
411         act = ActionManager::register_toggle_action (option_actions, X_("SendMidiClock"), _("Send MIDI Clock"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_clock));
412         ActionManager::session_sensitive_actions.push_back (act);
413         act = ActionManager::register_toggle_action (option_actions, X_("SendMIDIfeedback"), _("Send MIDI Feedback"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_feedback));
414         ActionManager::session_sensitive_actions.push_back (act);
415
416
417         if (getenv ("ARDOUR_BUNDLED")) {
418                 act = ActionManager::register_toggle_action (main_actions, X_("EnableTranslation"), _("Enable Translations"), mem_fun (*this, &ARDOUR_UI::toggle_translations));
419                 if (act) {
420                         RefPtr<ToggleAction> ract = RefPtr<ToggleAction>::cast_dynamic (act);
421                         if (ract) {
422                                 ract->set_active (!ARDOUR::translations_are_disabled());
423                         }
424                 }
425         }
426
427         ActionManager::add_action_group (shuttle_actions);
428         ActionManager::add_action_group (option_actions);
429         ActionManager::add_action_group (jack_actions);
430         ActionManager::add_action_group (transport_actions);
431         ActionManager::add_action_group (main_actions);
432         ActionManager::add_action_group (common_actions);
433 }
434
435 void
436 ARDOUR_UI::set_jack_buffer_size (nframes_t nframes)
437 {
438         Glib::RefPtr<Action> action;
439         const char* action_name = 0;
440
441         switch (nframes) {
442         case 32:
443                 action_name = X_("JACKLatency32");
444                 break;
445         case 64:
446                 action_name = X_("JACKLatency64");
447                 break;
448         case 128:
449                 action_name = X_("JACKLatency128");
450                 break;
451         case 256:
452                 action_name = X_("JACKLatency256");
453                 break;
454         case 512:
455                 action_name = X_("JACKLatency512");
456                 break;
457         case 1024:
458                 action_name = X_("JACKLatency1024");
459                 break;
460         case 2048:
461                 action_name = X_("JACKLatency2048");
462                 break;
463         case 4096:
464                 action_name = X_("JACKLatency4096");
465                 break;
466         case 8192:
467                 action_name = X_("JACKLatency8192");
468                 break;
469         default:
470                 /* XXX can we do anything useful ? */
471                 break;
472         }
473
474         if (action_name) {
475
476                 action = ActionManager::get_action (X_("JACK"), action_name);
477
478                 if (action) {
479                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
480
481                         if (ract && ract->get_active()) {
482                                 engine->request_buffer_size (nframes);
483                                 update_sample_rate (0);
484                         }
485                 }
486         }
487 }
488
489 void
490 ARDOUR_UI::build_menu_bar ()
491 {
492         menu_bar = dynamic_cast<MenuBar*> (ActionManager::get_widget (X_("/Main")));
493         menu_bar->set_name ("MainMenuBar");
494
495         /*
496          * This is needed because this property does not get installed
497          * until the Menu GObject class is registered, which happens
498          * when the first menu instance is created.
499          */
500         // XXX bug in gtkmm causes this to popup an error message
501         // Gtk::Settings::get_default()->property_gtk_can_change_accels() = true;
502         // so use this instead ...
503         gtk_settings_set_long_property (gtk_settings_get_default(), "gtk-can-change-accels", 1, "Ardour:designers");
504
505         wall_clock_box.add (wall_clock_label);
506         wall_clock_box.set_name ("WallClock");
507         wall_clock_label.set_name ("WallClock");
508
509         disk_space_box.add (disk_space_label);
510         disk_space_box.set_name ("WallClock");
511         disk_space_label.set_name ("WallClock");
512
513         cpu_load_box.add (cpu_load_label);
514         cpu_load_box.set_name ("CPULoad");
515         cpu_load_label.set_name ("CPULoad");
516
517         buffer_load_box.add (buffer_load_label);
518         buffer_load_box.set_name ("BufferLoad");
519         buffer_load_label.set_name ("BufferLoad");
520
521         sample_rate_box.add (sample_rate_label);
522         sample_rate_box.set_name ("SampleRate");
523         sample_rate_label.set_name ("SampleRate");
524
525 #ifndef TOP_MENUBAR
526         menu_hbox.pack_start (*menu_bar, true, true);
527 #else
528         use_menubar_as_top_menubar ();
529 #endif
530
531         if (!Profile->get_small_screen()) {
532 #ifndef GTKOSX
533                 // OSX provides its own wallclock, thank you very much
534                 menu_hbox.pack_end (wall_clock_box, false, false, 2);
535 #endif
536                 menu_hbox.pack_end (disk_space_box, false, false, 4);
537         }
538
539         menu_hbox.pack_end (cpu_load_box, false, false, 4);
540         menu_hbox.pack_end (buffer_load_box, false, false, 4);
541         menu_hbox.pack_end (sample_rate_box, false, false, 4);
542
543         menu_bar_base.set_name ("MainMenuBar");
544         menu_bar_base.add (menu_hbox);
545 }
546
547 void
548 ARDOUR_UI::use_menubar_as_top_menubar ()
549 {
550         gtk_application_set_menu_bar ((GtkMenuShell*) menu_bar->gobj());
551 }
552
553 void
554 ARDOUR_UI::setup_clock ()
555 {
556         ARDOUR_UI::Clock.connect (sigc::bind (sigc::mem_fun (big_clock, &AudioClock::set), false));
557
558         big_clock_window = new Window (WINDOW_TOPLEVEL);
559
560         big_clock_window->set_keep_above (true);
561         big_clock_window->set_border_width (0);
562         big_clock_window->add  (big_clock);
563
564         big_clock_window->set_title (_("Big Clock"));
565         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
566         big_clock_window->signal_realize().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_realized));
567         big_clock_window->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
568         big_clock_window->signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), big_clock_window), false);
569         big_clock_window->signal_size_allocate().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_size_allocate));
570
571         manage_window (*big_clock_window);
572 }
573
574 void
575 ARDOUR_UI::big_clock_realized ()
576 {
577         int x, y, w, d;
578
579         set_decoration (big_clock_window, (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
580         big_clock_window->get_window()->get_geometry (x, y, w, big_clock_height, d);
581
582         original_big_clock_height = big_clock_height;
583         original_big_clock_width = w;
584
585         Pango::FontDescription fd (big_clock.get_style()->get_font());
586         original_big_clock_font_size = fd.get_size ();
587         
588         if (!fd.get_size_is_absolute ()) {
589                 original_big_clock_font_size /= PANGO_SCALE;
590         }
591
592         /* we can't set the real size until we know the original one, with the UI rc-file-set font 
593            size, so do this here.
594         */
595
596         XMLNode* node = Config->extra_xml (X_("UI"));
597
598         if (node) {
599
600                 const XMLProperty* prop;
601                 int w, h, x, y;
602                 int have_pos = 0;
603
604                 w = h = x = y = 0;
605
606                 if ((prop = node->property ("big-clock-x-size")) != 0) {
607                         w = atoi (prop->value());
608                 }
609                 if ((prop = node->property ("big-clock-y-size")) != 0) {
610                         h = atoi (prop->value());
611                 }
612
613                 if (w && h) {
614                         big_clock_window->set_default_size (w, h);
615                 }
616
617                 if ((prop = node->property ("big-clock-x-off")) != 0) {
618                         x = atoi (prop->value());
619                         have_pos++;
620                 }
621                 if ((prop = node->property ("big-clock-y-off")) != 0) {
622                         y = atoi (prop->value());
623                         have_pos++;
624                 }
625
626                 if (have_pos == 2) {
627                         big_clock_window->move (x, y);
628                 }
629         }
630 }
631
632 void
633 ARDOUR_UI::float_big_clock (Gtk::Window* parent)
634 {
635         if (big_clock_window) {
636                 if (parent) {
637                         big_clock_window->set_transient_for (*parent);
638                 } else {
639                         gtk_window_set_transient_for (big_clock_window->gobj(), (GtkWindow*) 0);
640                 }
641         }
642 }
643
644 void
645 ARDOUR_UI::big_clock_size_allocate (Gtk::Allocation& allocation)
646 {
647         if (!big_clock_resize_in_progress) {
648                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::idle_big_clock_text_resizer), 0, 0));
649                 big_clock_resize_in_progress = true;
650         }
651 }
652
653 bool
654 ARDOUR_UI::idle_big_clock_text_resizer (int win_w, int win_h)
655 {
656         big_clock_resize_in_progress = false;
657
658         Glib::RefPtr<Gdk::Window> win = big_clock_window->get_window();
659         Pango::FontDescription fd (big_clock.get_style()->get_font());
660         int current_size = fd.get_size ();
661         int x, y, w, h, d;
662
663         if (!fd.get_size_is_absolute ()) {
664                 current_size /= PANGO_SCALE;
665         }
666
667         win->get_geometry (x, y, w, h, d);
668
669         double scale  = min (((double) w / (double) original_big_clock_width), 
670                              ((double) h / (double) original_big_clock_height));
671
672         int size = (int) lrintf (original_big_clock_font_size * scale);
673
674         if (size != current_size) {
675
676                 string family = fd.get_family();
677                 char buf[family.length()+16];
678                 snprintf (buf, family.length()+16, "%s %d", family.c_str(), size);
679                 
680                 try { 
681                         Pango::FontDescription fd (buf);
682                         Glib::RefPtr<Gtk::RcStyle> rcstyle = big_clock.get_modifier_style ();
683                         rcstyle->set_font (fd);
684                         big_clock.modify_style (rcstyle);
685                 } 
686                 
687                 catch (...) {
688                         /* oh well, do nothing */
689                 }
690         }
691
692         return false;
693 }
694
695 void
696 ARDOUR_UI::save_ardour_state ()
697 {
698         if (!keyboard || !mixer || !editor) {
699                 return;
700         }
701
702         /* XXX this is all a bit dubious. add_extra_xml() uses
703            a different lifetime model from add_instant_xml().
704         */
705
706         XMLNode* node = new XMLNode (keyboard->get_state());
707         Config->add_extra_xml (*node);
708         Config->add_extra_xml (get_transport_controllable_state());
709
710         XMLNode* window_node = new XMLNode (X_("UI"));
711         
712         window_node->add_property ("show-big-clock", (big_clock_window && big_clock_window->is_visible() ? "yes" : "no"));
713
714         Glib::RefPtr<Gdk::Window> win;
715
716         if (big_clock_window && (win = big_clock_window->get_window())) {
717
718                 int w, h;
719                 int xoff, yoff;
720                 char buf[32];
721
722                 win->get_size (w, h);
723                 win->get_position (xoff, yoff);
724
725                 snprintf (buf, sizeof (buf), "%d", w);
726                 window_node->add_property ("big-clock-x-size", buf);
727                 snprintf (buf, sizeof (buf), "%d", h);
728                 window_node->add_property ("big-clock-y-size", buf);
729                 snprintf (buf, sizeof (buf), "%d", xoff);
730                 window_node->add_property ("big-clock-x-off", buf);
731                 snprintf (buf, sizeof (buf), "%d", yoff);
732                 window_node->add_property ("big-clock-y-off", buf);
733         }
734
735         /* tearoffs */
736
737         XMLNode* tearoff_node = new XMLNode (X_("Tearoffs"));
738
739         if (transport_tearoff) {
740                 XMLNode* t = new XMLNode (X_("transport"));
741                 transport_tearoff->add_state (*t);
742                 tearoff_node->add_child_nocopy (*t);
743         } 
744
745         if (mixer && mixer->monitor_section()) {
746                 XMLNode* t = new XMLNode (X_("monitor-section"));
747                 mixer->monitor_section()->tearoff().add_state (*t);
748                 tearoff_node->add_child_nocopy (*t);
749         } 
750
751         if (editor && editor->mouse_mode_tearoff()) {
752                 XMLNode* t = new XMLNode (X_("mouse-mode"));
753                 editor->mouse_mode_tearoff ()->add_state (*t);
754                 tearoff_node->add_child_nocopy (*t);
755         } 
756         
757         window_node->add_child_nocopy (*tearoff_node);
758
759         Config->add_extra_xml (*window_node);
760
761         if (_startup && _startup->engine_control() && _startup->engine_control()->was_used()) {
762                 Config->add_extra_xml (_startup->engine_control()->get_state());
763         }
764         Config->save_state();
765         ui_config->save_state ();
766
767         XMLNode enode(static_cast<Stateful*>(editor)->get_state());
768         XMLNode mnode(mixer->get_state());
769
770         if (_session) {
771                 _session->add_instant_xml (enode);
772                 _session->add_instant_xml (mnode);
773         } else {
774                 Config->add_instant_xml (enode);
775                 Config->add_instant_xml (mnode);
776         }
777
778         Keyboard::save_keybindings ();
779 }
780