tweaks for OS X menu building
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 /* This file contains any ARDOUR_UI methods that require knowledge of
25    the editor, and exists so that no compilation dependency exists
26    between the main ARDOUR_UI modules and the PublicEditor class. This
27    is to cut down on the nasty compile times for both these classes.
28 */
29
30 #include <cmath>
31
32 #include <glibmm/miscutils.h>
33 #include <gtk/gtk.h>
34
35 #include "gtkmm2ext/utils.h"
36 #include "gtkmm2ext/window_title.h"
37 #include "gtkmm2ext/tearoff.h"
38 #include "gtkmm2ext/cairo_packer.h"
39
40 #include "pbd/file_utils.h"
41 #include "pbd/fpu.h"
42 #include "pbd/convert.h"
43
44 #include "ardour_ui.h"
45 #include "public_editor.h"
46 #include "audio_clock.h"
47 #include "keyboard.h"
48 #include "monitor_section.h"
49 #include "engine_dialog.h"
50 #include "editor.h"
51 #include "actions.h"
52 #include "mixer_ui.h"
53 #include "startup.h"
54 #include "utils.h"
55 #include "window_manager.h"
56 #include "global_port_matrix.h"
57 #include "location_ui.h"
58 #include "main_clock.h"
59
60 #include <gtkmm2ext/application.h>
61
62 #include "ardour/session.h"
63 #include "ardour/profile.h"
64 #include "ardour/audioengine.h"
65
66 #include "control_protocol/control_protocol.h"
67
68 #include "i18n.h"
69
70 using namespace std;
71 using namespace ARDOUR;
72 using namespace PBD;
73 using namespace Gtkmm2ext;
74 using namespace Gtk;
75 using namespace Glib;
76
77 int
78 ARDOUR_UI::create_editor ()
79 {
80         try {
81                 editor = new Editor ();
82         }
83
84         catch (failed_constructor& err) {
85                 return -1;
86         }
87
88         editor->Realized.connect (sigc::mem_fun (*this, &ARDOUR_UI::editor_realized));
89         editor->signal_window_state_event().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::main_window_state_event_handler), true));
90
91         return 0;
92 }
93
94 void
95 ARDOUR_UI::install_actions ()
96 {
97         Glib::RefPtr<ActionGroup> main_actions = ActionGroup::create (X_("Main"));
98         Glib::RefPtr<ActionGroup> main_menu_actions = ActionGroup::create (X_("Main_menu"));
99         Glib::RefPtr<Action> act;
100
101         /* menus + submenus that need action items */
102
103         ActionManager::register_action (main_menu_actions, X_("Session"), _("Session"));
104         act = ActionManager::register_action (main_menu_actions, X_("Cleanup"), _("Clean-up"));
105         ActionManager::write_sensitive_actions.push_back (act);
106         ActionManager::register_action (main_menu_actions, X_("Sync"), _("Sync"));
107         ActionManager::register_action (main_menu_actions, X_("TransportOptions"), _("Options"));
108         ActionManager::register_action (main_menu_actions, X_("WindowMenu"), _("Window"));
109         ActionManager::register_action (main_menu_actions, X_("Help"), _("Help"));
110         ActionManager::register_action (main_menu_actions, X_("KeyMouseActions"), _("Misc. Shortcuts"));
111         ActionManager::register_action (main_menu_actions, X_("AudioFileFormat"), _("Audio File Format"));
112         ActionManager::register_action (main_menu_actions, X_("AudioFileFormatHeader"), _("File Type"));
113         ActionManager::register_action (main_menu_actions, X_("AudioFileFormatData"), _("Sample Format"));
114         ActionManager::register_action (main_menu_actions, X_("ControlSurfaces"), _("Control Surfaces"));
115         ActionManager::register_action (main_menu_actions, X_("Plugins"), _("Plugins"));
116         ActionManager::register_action (main_menu_actions, X_("Metering"), _("Metering"));
117         ActionManager::register_action (main_menu_actions, X_("MeteringFallOffRate"), _("Fall Off Rate"));
118         ActionManager::register_action (main_menu_actions, X_("MeteringHoldTime"), _("Hold Time"));
119         ActionManager::register_action (main_menu_actions, X_("Denormals"), _("Denormal Handling"));
120
121         /* the real actions */
122
123         act = ActionManager::register_action (main_actions, X_("New"), _("New..."),  hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::get_session_parameters), false, true, "")));
124
125         ActionManager::register_action (main_actions, X_("Open"), _("Open..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_session));
126         ActionManager::register_action (main_actions, X_("Recent"), _("Recent..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_recent_session));
127         act = ActionManager::register_action (main_actions, X_("Close"), _("Close"),  sigc::mem_fun(*this, &ARDOUR_UI::close_session));
128         ActionManager::session_sensitive_actions.push_back (act);
129
130         act = ActionManager::register_action (main_actions, X_("AddTrackBus"), _("Add Track or Bus..."),
131                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_route), (Gtk::Window*) 0));
132         ActionManager::session_sensitive_actions.push_back (act);
133         ActionManager::write_sensitive_actions.push_back (act);
134
135         act = ActionManager::register_action (main_actions, X_("OpenVideo"), _("Open Video"),
136                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_video), (Gtk::Window*) 0));
137         ActionManager::session_sensitive_actions.push_back (act);
138         act = ActionManager::register_action (main_actions, X_("CloseVideo"), _("Remove Video"),
139                                               sigc::mem_fun (*this, &ARDOUR_UI::remove_video));
140         ActionManager::session_sensitive_actions.push_back (act);
141         act = ActionManager::register_action (main_actions, X_("ExportVideo"), _("Export To Video File"),
142                                               sigc::mem_fun (*editor, &PublicEditor::export_video));
143         ActionManager::session_sensitive_actions.push_back (act);
144
145         act = ActionManager::register_action (main_actions, X_("Snapshot"), _("Snapshot..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), false));
146         ActionManager::session_sensitive_actions.push_back (act);
147         ActionManager::write_sensitive_actions.push_back (act);
148
149         act = ActionManager::register_action (main_actions, X_("SaveAs"), _("Save As..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), true));
150         ActionManager::session_sensitive_actions.push_back (act);
151         ActionManager::write_sensitive_actions.push_back (act);
152
153         act = ActionManager::register_action (main_actions, X_("Rename"), _("Rename..."), sigc::mem_fun(*this, &ARDOUR_UI::rename_session));
154         ActionManager::session_sensitive_actions.push_back (act);
155         ActionManager::write_sensitive_actions.push_back (act);
156
157         act = ActionManager::register_action (main_actions, X_("SaveTemplate"), _("Save Template..."),  sigc::mem_fun(*this, &ARDOUR_UI::save_template));
158         ActionManager::session_sensitive_actions.push_back (act);
159
160         act = ActionManager::register_action (main_actions, X_("Metadata"), _("Metadata"));
161         ActionManager::session_sensitive_actions.push_back (act);
162
163         act = ActionManager::register_action (main_actions, X_("EditMetadata"), _("Edit Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::edit_metadata));
164         ActionManager::session_sensitive_actions.push_back (act);
165
166         act = ActionManager::register_action (main_actions, X_("ImportMetadata"), _("Import Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::import_metadata));
167         ActionManager::session_sensitive_actions.push_back (act);
168
169         act = ActionManager::register_action (main_actions, X_("ExportAudio"), _("Export To Audio File(s)..."),  sigc::mem_fun (*editor, &PublicEditor::export_audio));
170         ActionManager::session_sensitive_actions.push_back (act);
171
172         act = ActionManager::register_action (main_actions, X_("StemExport"), _("Stem export..."),  sigc::mem_fun (*editor, &PublicEditor::stem_export));
173         ActionManager::session_sensitive_actions.push_back (act);
174
175         act = ActionManager::register_action (main_actions, X_("Export"), _("Export"));
176         ActionManager::session_sensitive_actions.push_back (act);
177
178         act = ActionManager::register_action (main_actions, X_("CleanupUnused"), _("Clean-up Unused Sources..."),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::cleanup));
179         ActionManager::session_sensitive_actions.push_back (act);
180         ActionManager::write_sensitive_actions.push_back (act);
181
182         act = ActionManager::register_action (main_actions, X_("FlushWastebasket"), _("Flush Wastebasket"),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::flush_trash));
183         ActionManager::write_sensitive_actions.push_back (act);
184         ActionManager::session_sensitive_actions.push_back (act);
185
186         /* JACK actions for controlling ... JACK */
187
188         Glib::RefPtr<ActionGroup> jack_actions = ActionGroup::create (X_("JACK"));
189         ActionManager::register_action (jack_actions, X_("JACK"), _("JACK"));
190         ActionManager::register_action (jack_actions, X_("Latency"), _("Latency"));
191
192         act = ActionManager::register_action (jack_actions, X_("JACKReconnect"), _("Reconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::reconnect_to_jack));
193         ActionManager::jack_opposite_sensitive_actions.push_back (act);
194
195         act = ActionManager::register_action (jack_actions, X_("JACKDisconnect"), _("Disconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::disconnect_from_jack));
196         ActionManager::jack_sensitive_actions.push_back (act);
197
198         RadioAction::Group jack_latency_group;
199
200         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), (pframes_t) 32));
201         ActionManager::jack_sensitive_actions.push_back (act);
202         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), (pframes_t) 64));
203         ActionManager::jack_sensitive_actions.push_back (act);
204         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), (pframes_t) 128));
205         ActionManager::jack_sensitive_actions.push_back (act);
206         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), (pframes_t) 256));
207         ActionManager::jack_sensitive_actions.push_back (act);
208         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), (pframes_t) 512));
209         ActionManager::jack_sensitive_actions.push_back (act);
210         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), (pframes_t) 1024));
211         ActionManager::jack_sensitive_actions.push_back (act);
212         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), (pframes_t) 2048));
213         ActionManager::jack_sensitive_actions.push_back (act);
214         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), (pframes_t) 4096));
215         ActionManager::jack_sensitive_actions.push_back (act);
216         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), (pframes_t) 8192));
217         ActionManager::jack_sensitive_actions.push_back (act);
218
219         /* these actions are intended to be shared across all windows */
220
221         common_actions = ActionGroup::create (X_("Common"));
222         ActionManager::register_action (common_actions, X_("Quit"), _("Quit"), (hide_return (sigc::mem_fun(*this, &ARDOUR_UI::finish))));
223
224         /* windows visibility actions */
225
226         ActionManager::register_toggle_action (common_actions, X_("ToggleMaximalEditor"), _("Maximise Editor Space"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_editing_space));
227         act = ActionManager::register_toggle_action (common_actions, X_("KeepTearoffs"), _("Show Toolbars"), mem_fun (*this, &ARDOUR_UI::toggle_keep_tearoffs));
228         ActionManager::session_sensitive_actions.push_back (act);
229
230         ActionManager::register_toggle_action (common_actions, X_("toggle-mixer"), S_("Window|Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_mixer_window));
231         ActionManager::register_action (common_actions, X_("toggle-editor-mixer"), _("Toggle Editor+Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_editor_mixer));
232
233         act = ActionManager::register_action (common_actions, X_("NewMIDITracer"), _("MIDI Tracer"), sigc::mem_fun(*this, &ARDOUR_UI::new_midi_tracer_window));
234         ActionManager::session_sensitive_actions.push_back (act);
235         ActionManager::register_action (common_actions, X_("Chat"), _("Chat"),  sigc::mem_fun(*this, &ARDOUR_UI::launch_chat));
236         /** TRANSLATORS: This is `Manual' in the sense of an instruction book that tells a user how to use Ardour */
237         ActionManager::register_action (common_actions, X_("Manual"), S_("Help|Manual"),  mem_fun(*this, &ARDOUR_UI::launch_manual));
238         ActionManager::register_action (common_actions, X_("Reference"), _("Reference"),  mem_fun(*this, &ARDOUR_UI::launch_reference));
239
240         act = ActionManager::register_action (common_actions, X_("Save"), _("Save"),  sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::save_state), string(""), false));
241         ActionManager::session_sensitive_actions.push_back (act);
242         ActionManager::write_sensitive_actions.push_back (act);
243
244         Glib::RefPtr<ActionGroup> transport_actions = ActionGroup::create (X_("Transport"));
245
246         /* do-nothing action for the "transport" menu bar item */
247
248         ActionManager::register_action (transport_actions, X_("Transport"), _("Transport"));
249
250         /* these two are not used by key bindings, instead use ToggleRoll for that. these two do show up in
251            menus and via button proxies.
252         */
253
254         act = ActionManager::register_action (transport_actions, X_("Stop"), _("Stop"), sigc::mem_fun(*this, &ARDOUR_UI::transport_stop));
255         ActionManager::session_sensitive_actions.push_back (act);
256         ActionManager::transport_sensitive_actions.push_back (act);
257         act = ActionManager::register_action (transport_actions, X_("Roll"), _("Roll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_roll));
258         ActionManager::session_sensitive_actions.push_back (act);
259         ActionManager::transport_sensitive_actions.push_back (act);
260
261         act = ActionManager::register_action (transport_actions, X_("ToggleRoll"), _("Start/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, false));
262         ActionManager::session_sensitive_actions.push_back (act);
263         ActionManager::transport_sensitive_actions.push_back (act);
264         act = ActionManager::register_action (transport_actions, X_("ToggleRollMaybe"), _("Start/Continue/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, true));
265         ActionManager::session_sensitive_actions.push_back (act);
266         ActionManager::transport_sensitive_actions.push_back (act);
267         act = ActionManager::register_action (transport_actions, X_("ToggleRollForgetCapture"), _("Stop and Forget Capture"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::toggle_roll), true, false));
268         ActionManager::session_sensitive_actions.push_back (act);
269         ActionManager::transport_sensitive_actions.push_back (act);
270
271         /* these two behave as follows:
272
273            - if transport speed != 1.0 or != -1.0, change speed to 1.0 or -1.0 (respectively)
274            - otherwise do nothing
275         */
276
277         act = ActionManager::register_action (transport_actions, X_("TransitionToRoll"), _("Transition To Roll"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), true));
278         ActionManager::session_sensitive_actions.push_back (act);
279         ActionManager::transport_sensitive_actions.push_back (act);
280
281         act = ActionManager::register_action (transport_actions, X_("TransitionToReverse"), _("Transition To Reverse"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), false));
282         ActionManager::session_sensitive_actions.push_back (act);
283         ActionManager::transport_sensitive_actions.push_back (act);
284
285         act = ActionManager::register_action (transport_actions, X_("Loop"), _("Play Loop Range"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_auto_loop));
286         ActionManager::session_sensitive_actions.push_back (act);
287         ActionManager::transport_sensitive_actions.push_back (act);
288         act = ActionManager::register_action (transport_actions, X_("PlaySelection"), _("Play Selected Range"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_selection));
289         ActionManager::session_sensitive_actions.push_back (act);
290         ActionManager::transport_sensitive_actions.push_back (act);
291         act = ActionManager::register_action (transport_actions, X_("PlayPreroll"), _("Play Selection w/Preroll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_preroll));
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_("Record"), _("Enable Record"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), false));
296         ActionManager::session_sensitive_actions.push_back (act);
297         ActionManager::write_sensitive_actions.push_back (act);
298         act = ActionManager::register_action (transport_actions, X_("record-roll"), _("Start Recording"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), true));
299         ActionManager::session_sensitive_actions.push_back (act);
300         ActionManager::write_sensitive_actions.push_back (act);
301         ActionManager::transport_sensitive_actions.push_back (act);
302         act = ActionManager::register_action (transport_actions, X_("Rewind"), _("Rewind"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 0));
303         ActionManager::session_sensitive_actions.push_back (act);
304         ActionManager::transport_sensitive_actions.push_back (act);
305         act = ActionManager::register_action (transport_actions, X_("RewindSlow"), _("Rewind (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), -1));
306         ActionManager::session_sensitive_actions.push_back (act);
307         ActionManager::transport_sensitive_actions.push_back (act);
308         act = ActionManager::register_action (transport_actions, X_("RewindFast"), _("Rewind (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 1));
309         ActionManager::session_sensitive_actions.push_back (act);
310         ActionManager::transport_sensitive_actions.push_back (act);
311         act = ActionManager::register_action (transport_actions, X_("Forward"), _("Forward"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 0));
312         ActionManager::session_sensitive_actions.push_back (act);
313         ActionManager::transport_sensitive_actions.push_back (act);
314         act = ActionManager::register_action (transport_actions, X_("ForwardSlow"), _("Forward (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), -1));
315         ActionManager::session_sensitive_actions.push_back (act);
316         ActionManager::transport_sensitive_actions.push_back (act);
317         act = ActionManager::register_action (transport_actions, X_("ForwardFast"), _("Forward (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 1));
318         ActionManager::session_sensitive_actions.push_back (act);
319         ActionManager::transport_sensitive_actions.push_back (act);
320         act = ActionManager::register_action (transport_actions, X_("GotoZero"), _("Goto Zero"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_zero));
321         ActionManager::session_sensitive_actions.push_back (act);
322         ActionManager::transport_sensitive_actions.push_back (act);
323         act = ActionManager::register_action (transport_actions, X_("GotoStart"), _("Goto Start"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_start));
324         ActionManager::session_sensitive_actions.push_back (act);
325         ActionManager::transport_sensitive_actions.push_back (act);
326         act = ActionManager::register_action (transport_actions, X_("GotoEnd"), _("Goto End"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_end));
327         ActionManager::session_sensitive_actions.push_back (act);
328         ActionManager::transport_sensitive_actions.push_back (act);
329         act = ActionManager::register_action (transport_actions, X_("GotoWallClock"), _("Goto Wall Clock"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_wallclock));
330         ActionManager::session_sensitive_actions.push_back (act);
331         ActionManager::transport_sensitive_actions.push_back (act);
332
333         act = ActionManager::register_action (transport_actions, X_("focus-on-clock"), _("Focus On Clock"), sigc::mem_fun(*this, &ARDOUR_UI::focus_on_clock));
334         ActionManager::session_sensitive_actions.push_back (act);
335         ActionManager::transport_sensitive_actions.push_back (act);
336
337         act = ActionManager::register_action (transport_actions, X_("primary-clock-timecode"), _("Timecode"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Timecode));
338         ActionManager::session_sensitive_actions.push_back (act);
339         act = ActionManager::register_action (transport_actions, X_("primary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::BBT));
340         ActionManager::session_sensitive_actions.push_back (act);
341         act = ActionManager::register_action (transport_actions, X_("primary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::MinSec));
342         ActionManager::session_sensitive_actions.push_back (act);
343         act = ActionManager::register_action (transport_actions, X_("primary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Frames));
344         ActionManager::session_sensitive_actions.push_back (act);
345
346         act = ActionManager::register_action (transport_actions, X_("secondary-clock-timecode"), _("Timecode"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Timecode));
347         ActionManager::session_sensitive_actions.push_back (act);
348         act = ActionManager::register_action (transport_actions, X_("secondary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::BBT));
349         ActionManager::session_sensitive_actions.push_back (act);
350         act = ActionManager::register_action (transport_actions, X_("secondary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::MinSec));
351         ActionManager::session_sensitive_actions.push_back (act);
352         act = ActionManager::register_action (transport_actions, X_("secondary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Frames));
353         ActionManager::session_sensitive_actions.push_back (act);
354
355         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchIn"), _("Punch In"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_in));
356         act->set_short_label (_("In"));
357         ActionManager::session_sensitive_actions.push_back (act);
358         ActionManager::transport_sensitive_actions.push_back (act);
359         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchOut"), _("Punch Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_out));
360         act->set_short_label (_("Out"));
361         ActionManager::session_sensitive_actions.push_back (act);
362         ActionManager::transport_sensitive_actions.push_back (act);
363         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunch"), _("Punch In/Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch));
364         act->set_short_label (_("In/Out"));
365         ActionManager::session_sensitive_actions.push_back (act);
366         ActionManager::transport_sensitive_actions.push_back (act);
367         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleClick"), _("Click"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_click));
368         ActionManager::session_sensitive_actions.push_back (act);
369         ActionManager::transport_sensitive_actions.push_back (act);
370         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoInput"), _("Auto Input"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_input));
371         ActionManager::session_sensitive_actions.push_back (act);
372         ActionManager::transport_sensitive_actions.push_back (act);
373         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoPlay"), _("Auto Play"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_play));
374         ActionManager::session_sensitive_actions.push_back (act);
375         ActionManager::transport_sensitive_actions.push_back (act);
376         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoReturn"), _("Auto Return"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_return));
377         ActionManager::session_sensitive_actions.push_back (act);
378         ActionManager::transport_sensitive_actions.push_back (act);
379         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleFollowEdits"), _("Follow Edits"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_always_play_range));
380         ActionManager::session_sensitive_actions.push_back (act);
381         ActionManager::transport_sensitive_actions.push_back (act);
382
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         /* MIDI */
417
418         Glib::RefPtr<ActionGroup> midi_actions = ActionGroup::create (X_("MIDI"));
419         ActionManager::register_action (midi_actions, X_("panic"), _("Panic"), sigc::mem_fun(*this, &ARDOUR_UI::midi_panic));
420
421         ActionManager::add_action_group (shuttle_actions);
422         ActionManager::add_action_group (option_actions);
423         ActionManager::add_action_group (jack_actions);
424         ActionManager::add_action_group (transport_actions);
425         ActionManager::add_action_group (main_actions);
426         ActionManager::add_action_group (main_menu_actions);
427         ActionManager::add_action_group (common_actions);
428         ActionManager::add_action_group (midi_actions);
429 }
430
431 void
432 ARDOUR_UI::set_jack_buffer_size (pframes_t nframes)
433 {
434         Glib::RefPtr<Action> action;
435         const char* action_name = 0;
436
437         switch (nframes) {
438         case 32:
439                 action_name = X_("JACKLatency32");
440                 break;
441         case 64:
442                 action_name = X_("JACKLatency64");
443                 break;
444         case 128:
445                 action_name = X_("JACKLatency128");
446                 break;
447         case 256:
448                 action_name = X_("JACKLatency256");
449                 break;
450         case 512:
451                 action_name = X_("JACKLatency512");
452                 break;
453         case 1024:
454                 action_name = X_("JACKLatency1024");
455                 break;
456         case 2048:
457                 action_name = X_("JACKLatency2048");
458                 break;
459         case 4096:
460                 action_name = X_("JACKLatency4096");
461                 break;
462         case 8192:
463                 action_name = X_("JACKLatency8192");
464                 break;
465         default:
466                 /* XXX can we do anything useful ? */
467                 break;
468         }
469
470         if (action_name) {
471
472                 action = ActionManager::get_action (X_("JACK"), action_name);
473
474                 if (action) {
475                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
476
477                         if (ract && ract->get_active()) {
478                                 engine->request_buffer_size (nframes);
479                                 update_sample_rate (0);
480                         }
481                 }
482         }
483 }
484
485 void
486 ARDOUR_UI::build_menu_bar ()
487 {
488         menu_bar = dynamic_cast<MenuBar*> (ActionManager::get_widget (X_("/Main")));
489         menu_bar->set_name ("MainMenuBar");
490
491         /*
492          * This is needed because this property does not get installed
493          * until the Menu GObject class is registered, which happens
494          * when the first menu instance is created.
495          */
496         // XXX bug in gtkmm causes this to popup an error message
497         // Gtk::Settings::get_default()->property_gtk_can_change_accels() = true;
498         // so use this instead ...
499         gtk_settings_set_long_property (gtk_settings_get_default(), "gtk-can-change-accels", 1, "Ardour:designers");
500
501         EventBox* ev = manage (new EventBox);
502         ev->show ();
503         CairoHPacker* hbox = manage (new CairoHPacker);
504         hbox->set_name (X_("StatusBarBox"));
505         hbox->show ();
506         hbox->set_border_width (3);
507
508         VBox* vbox = manage (new VBox);
509         vbox->pack_start (*hbox, true, false);
510         vbox->show();
511
512         ev->add (*vbox);
513
514         wall_clock_label.set_name ("WallClock");
515         wall_clock_label.set_use_markup ();
516         disk_space_label.set_name ("WallClock");
517         disk_space_label.set_use_markup ();
518         timecode_format_label.set_name ("WallClock");
519         timecode_format_label.set_use_markup ();
520         cpu_load_label.set_name ("CPULoad");
521         cpu_load_label.set_use_markup ();
522         buffer_load_label.set_name ("BufferLoad");
523         buffer_load_label.set_use_markup ();
524         sample_rate_label.set_name ("SampleRate");
525         sample_rate_label.set_use_markup ();
526         format_label.set_name ("Format");
527         format_label.set_use_markup ();
528
529 #ifndef TOP_MENUBAR
530         menu_hbox.pack_start (*menu_bar, false, false);
531 #else
532         use_menubar_as_top_menubar ();
533 #endif
534
535         bool wall_clock = false;
536         bool disk_space = false;
537
538         if (!Profile->get_small_screen()) {
539 #ifndef GTKOSX
540                 // OSX provides its own wallclock, thank you very much
541                 wall_clock = true;
542 #endif
543                 disk_space = true;
544         }
545         
546         hbox->pack_end (wall_clock_label, false, false, 2);
547         hbox->pack_end (disk_space_label, false, false, 4);
548         hbox->pack_end (cpu_load_label, false, false, 4);
549         hbox->pack_end (buffer_load_label, false, false, 4);
550         hbox->pack_end (sample_rate_label, false, false, 4);
551         hbox->pack_end (timecode_format_label, false, false, 4);
552         hbox->pack_end (format_label, false, false, 4);
553
554         menu_hbox.pack_end (*ev, false, false, 6);
555
556         menu_bar_base.set_name ("MainMenuBar");
557         menu_bar_base.add (menu_hbox);
558
559         _status_bar_visibility.add (&wall_clock_label,      X_("WallClock"), _("Wall Clock"), wall_clock);
560         _status_bar_visibility.add (&disk_space_label,      X_("Disk"),      _("Disk Space"), disk_space);
561         _status_bar_visibility.add (&cpu_load_label,        X_("DSP"),       _("DSP"), true);
562         _status_bar_visibility.add (&buffer_load_label,     X_("Buffers"),   _("Buffers"), true);
563         _status_bar_visibility.add (&sample_rate_label,     X_("JACK"),      _("JACK Sampling Rate and Latency"), true);
564         _status_bar_visibility.add (&timecode_format_label, X_("TCFormat"),  _("Timecode Format"), true);
565         _status_bar_visibility.add (&format_label,          X_("Format"),    _("File Format"), true);
566
567         ev->signal_button_press_event().connect (sigc::mem_fun (_status_bar_visibility, &VisibilityGroup::button_press_event));
568 }
569
570 void
571 ARDOUR_UI::use_menubar_as_top_menubar ()
572 {
573         Gtk::Widget* widget;
574         Application* app = Application::instance ();
575
576         /* Quit will be taken of separately */
577
578         if ((widget = ActionManager::get_widget ("/ui/Main/Session/Quit"))) {
579                 widget->hide ();
580         }
581
582         /* Put items for About and Preferences into App menu (the
583          * ardour.menus.in file does not list them for OS X)
584          */
585
586         GtkApplicationMenuGroup* group = app->add_app_menu_group ();
587
588         if ((widget = ActionManager::get_widget ("/ui/Windows/toggle-about"))) {
589                 app->add_app_menu_item (group, dynamic_cast<MenuItem*>(widget));
590         }
591
592         if ((widget = ActionManager::get_widget ("/ui/Windows/toggle-rc-options-editor"))) {
593                 app->add_app_menu_item (group, dynamic_cast<MenuItem*>(widget));
594         }
595
596         app->set_menu_bar (*menu_bar);
597 }
598
599 void
600 ARDOUR_UI::save_ardour_state ()
601 {
602         if (!keyboard || !mixer || !editor) {
603                 return;
604         }
605
606         /* XXX this is all a bit dubious. add_extra_xml() uses
607            a different lifetime model from add_instant_xml().
608         */
609
610         XMLNode* node = new XMLNode (keyboard->get_state());
611         Config->add_extra_xml (*node);
612         Config->add_extra_xml (get_transport_controllable_state());
613
614         XMLNode* window_node = new XMLNode (X_("UI"));
615         window_node->add_property (_status_bar_visibility.get_state_name().c_str(), _status_bar_visibility.get_state_value ());
616
617         /* Windows */
618
619         WM::Manager::instance().add_state (*window_node);
620
621         /* tearoffs */
622
623         XMLNode* tearoff_node = new XMLNode (X_("Tearoffs"));
624
625         if (transport_tearoff) {
626                 XMLNode* t = new XMLNode (X_("transport"));
627                 transport_tearoff->add_state (*t);
628                 tearoff_node->add_child_nocopy (*t);
629         }
630
631         if (mixer && mixer->monitor_section()) {
632                 XMLNode* t = new XMLNode (X_("monitor-section"));
633                 mixer->monitor_section()->tearoff().add_state (*t);
634                 tearoff_node->add_child_nocopy (*t);
635         }
636
637         if (editor && editor->mouse_mode_tearoff()) {
638                 XMLNode* t = new XMLNode (X_("mouse-mode"));
639                 editor->mouse_mode_tearoff ()->add_state (*t);
640                 tearoff_node->add_child_nocopy (*t);
641         }
642
643         window_node->add_child_nocopy (*tearoff_node);
644
645         Config->add_extra_xml (*window_node);
646
647         if (_startup && _startup->engine_control() && _startup->engine_control()->was_used()) {
648                 Config->add_extra_xml (_startup->engine_control()->get_state());
649         }
650         Config->save_state();
651         if (ui_config->dirty()) {
652                 ui_config->save_state ();
653         }
654
655         XMLNode& enode (static_cast<Stateful*>(editor)->get_state());
656         XMLNode& mnode (mixer->get_state());
657
658         if (_session) {
659                 _session->add_instant_xml (enode);
660                 _session->add_instant_xml (mnode);
661                 if (location_ui) {
662                         _session->add_instant_xml (location_ui->ui().get_state ());
663                 }
664         } else {
665                 Config->add_instant_xml (enode);
666                 Config->add_instant_xml (mnode);
667                 if (location_ui) {
668                         Config->add_instant_xml (location_ui->ui().get_state ());
669                 }
670         }
671
672         Keyboard::save_keybindings ();
673 }
674
675 void
676 ARDOUR_UI::resize_text_widgets ()
677 {
678         set_size_request_to_display_given_text (cpu_load_label, "DSP: 100.0%", 2, 2);
679         set_size_request_to_display_given_text (buffer_load_label, "Buffers: p:100% c:100%", 2, 2);
680 }
681
682 void
683 ARDOUR_UI::focus_on_clock ()
684 {
685         if (editor && primary_clock) {
686                 editor->present ();
687                 primary_clock->focus ();
688         }
689 }