Add test for #3356.
[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 #ifdef GTKOSX
52 #include <gtkmm2ext/sync-menu.h>
53 #endif
54
55 #include "ardour/session.h"
56 #include "ardour/profile.h"
57 #include "ardour/audioengine.h"
58 #include "ardour/control_protocol_manager.h"
59
60 #include "control_protocol/control_protocol.h"
61
62 #include "i18n.h"
63
64 using namespace std;
65 using namespace ARDOUR;
66 using namespace PBD;
67 using namespace Gtkmm2ext;
68 using namespace Gtk;
69 using namespace Glib;
70
71 int
72 ARDOUR_UI::create_editor ()
73
74 {
75         try {
76                 editor = new Editor ();
77         }
78
79         catch (failed_constructor& err) {
80                 return -1;
81         }
82
83         editor->Realized.connect (sigc::mem_fun (*this, &ARDOUR_UI::editor_realized));
84         editor->signal_window_state_event().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::main_window_state_event_handler), true));
85
86         return 0;
87 }
88
89 void
90 ARDOUR_UI::install_actions ()
91 {
92         Glib::RefPtr<ActionGroup> main_actions = ActionGroup::create (X_("Main"));
93         Glib::RefPtr<Action> act;
94
95         /* menus + submenus that need action items */
96
97         ActionManager::register_action (main_actions, X_("Session"), _("Session"));
98         act = ActionManager::register_action (main_actions, X_("Cleanup"), _("Cleanup"));
99         ActionManager::write_sensitive_actions.push_back (act);
100         ActionManager::register_action (main_actions, X_("Sync"), _("Sync"));
101         ActionManager::register_action (main_actions, X_("TransportOptions"), _("Options"));
102         ActionManager::register_action (main_actions, X_("Help"), _("Help"));
103         ActionManager::register_action (main_actions, X_("KeyMouseActions"), _("Misc. Shortcuts"));
104         ActionManager::register_action (main_actions, X_("AudioFileFormat"), _("Audio File Format"));
105         ActionManager::register_action (main_actions, X_("AudioFileFormatHeader"), _("File Type"));
106         ActionManager::register_action (main_actions, X_("AudioFileFormatData"), _("Sample Format"));
107         ActionManager::register_action (main_actions, X_("ControlSurfaces"), _("Control Surfaces"));
108         ActionManager::register_action (main_actions, X_("Plugins"), _("Plugins"));
109         ActionManager::register_action (main_actions, X_("Metering"), _("Metering"));
110         ActionManager::register_action (main_actions, X_("MeteringFallOffRate"), _("Fall Off Rate"));
111         ActionManager::register_action (main_actions, X_("MeteringHoldTime"), _("Hold Time"));
112         ActionManager::register_action (main_actions, X_("Denormals"), _("Denormal Handling"));
113
114         /* the real actions */
115
116         act = ActionManager::register_action (main_actions, X_("New"), _("New..."),  hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::get_session_parameters), false, true, "")));
117
118         ActionManager::register_action (main_actions, X_("Open"), _("Open..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_session));
119         ActionManager::register_action (main_actions, X_("Recent"), _("Recent..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_recent_session));
120         act = ActionManager::register_action (main_actions, X_("Close"), _("Close"),  sigc::mem_fun(*this, &ARDOUR_UI::close_session));
121         ActionManager::session_sensitive_actions.push_back (act);
122
123         act = ActionManager::register_action (main_actions, X_("AddTrackBus"), _("Add Track or Bus..."),
124                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_route), (Gtk::Window*) 0));
125         ActionManager::session_sensitive_actions.push_back (act);
126         ActionManager::write_sensitive_actions.push_back (act);
127
128 #ifdef WITH_CMT
129
130         sys::path anicomp_file_path;
131
132         if (PBD::find_file_in_search_path (Glib::getenv("PATH"), "AniComp", anicomp_file_path)) {
133                 act = ActionManager::register_action (main_actions, X_("aniConnect"), _("Connect"),  (sigc::mem_fun (*editor, &PublicEditor::connect_to_image_compositor)));
134                 ActionManager::session_sensitive_actions.push_back (act);
135         }
136
137 #endif
138
139         act = ActionManager::register_action (main_actions, X_("Snapshot"), _("Snapshot..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), false));
140         ActionManager::session_sensitive_actions.push_back (act);
141         ActionManager::write_sensitive_actions.push_back (act);
142
143         act = ActionManager::register_action (main_actions, X_("SaveAs"), _("Save As..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), true));
144         ActionManager::session_sensitive_actions.push_back (act);
145         ActionManager::write_sensitive_actions.push_back (act);
146
147         act = ActionManager::register_action (main_actions, X_("SaveTemplate"), _("Save Template..."),  sigc::mem_fun(*this, &ARDOUR_UI::save_template));
148         ActionManager::session_sensitive_actions.push_back (act);
149
150         act = ActionManager::register_action (main_actions, X_("Metadata"), _("Metadata"));
151         ActionManager::session_sensitive_actions.push_back (act);
152
153         act = ActionManager::register_action (main_actions, X_("EditMetadata"), _("Edit Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::edit_metadata));
154         ActionManager::session_sensitive_actions.push_back (act);
155
156         act = ActionManager::register_action (main_actions, X_("ImportMetadata"), _("Import Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::import_metadata));
157         ActionManager::session_sensitive_actions.push_back (act);
158
159         act = ActionManager::register_action (main_actions, X_("ExportAudio"), _("Export To Audio File(s)..."),  sigc::mem_fun (*editor, &PublicEditor::export_audio));
160         ActionManager::session_sensitive_actions.push_back (act);
161
162         act = ActionManager::register_action (main_actions, X_("Export"), _("Export"));
163         ActionManager::session_sensitive_actions.push_back (act);
164
165         act = ActionManager::register_action (main_actions, X_("CleanupUnused"), _("Cleanup Unused Sources..."),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::cleanup));
166         ActionManager::session_sensitive_actions.push_back (act);
167         ActionManager::write_sensitive_actions.push_back (act);
168
169         act = ActionManager::register_action (main_actions, X_("FlushWastebasket"), _("Flush Wastebasket"),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::flush_trash));
170         ActionManager::write_sensitive_actions.push_back (act);
171         ActionManager::session_sensitive_actions.push_back (act);
172
173         /* JACK actions for controlling ... JACK */
174
175         Glib::RefPtr<ActionGroup> jack_actions = ActionGroup::create (X_("JACK"));
176         ActionManager::register_action (jack_actions, X_("JACK"), _("JACK"));
177         ActionManager::register_action (jack_actions, X_("Latency"), _("Latency"));
178
179         act = ActionManager::register_action (jack_actions, X_("JACKReconnect"), _("Reconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::reconnect_to_jack));
180         ActionManager::jack_opposite_sensitive_actions.push_back (act);
181
182         act = ActionManager::register_action (jack_actions, X_("JACKDisconnect"), _("Disconnect"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::disconnect_from_jack));
183         ActionManager::jack_sensitive_actions.push_back (act);
184
185         RadioAction::Group jack_latency_group;
186
187         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));
188         ActionManager::jack_sensitive_actions.push_back (act);
189         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));
190         ActionManager::jack_sensitive_actions.push_back (act);
191         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));
192         ActionManager::jack_sensitive_actions.push_back (act);
193         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));
194         ActionManager::jack_sensitive_actions.push_back (act);
195         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));
196         ActionManager::jack_sensitive_actions.push_back (act);
197         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));
198         ActionManager::jack_sensitive_actions.push_back (act);
199         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));
200         ActionManager::jack_sensitive_actions.push_back (act);
201         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));
202         ActionManager::jack_sensitive_actions.push_back (act);
203         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));
204         ActionManager::jack_sensitive_actions.push_back (act);
205
206         /* these actions are intended to be shared across all windows */
207
208         common_actions = ActionGroup::create (X_("Common"));
209         ActionManager::register_action (main_actions, X_("WindowMenu"), _("Window"));
210         ActionManager::register_action (common_actions, X_("Quit"), _("Quit"), (hide_return (sigc::mem_fun(*this, &ARDOUR_UI::finish))));
211
212         /* windows visibility actions */
213
214         ActionManager::register_toggle_action (common_actions, X_("ToggleMaximalEditor"), _("Maximise Editor Space"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_editing_space));
215         act = ActionManager::register_toggle_action (common_actions, X_("KeepTearoffs"), _("Toolbars when Maximised"), mem_fun (*this, &ARDOUR_UI::toggle_keep_tearoffs));
216         ActionManager::session_sensitive_actions.push_back (act);
217
218         ActionManager::register_action (common_actions, X_("goto-editor"), _("Show Editor"),  sigc::mem_fun(*this, &ARDOUR_UI::goto_editor_window));
219         ActionManager::register_action (common_actions, X_("goto-mixer"), _("Show Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::goto_mixer_window));
220         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));
221         ActionManager::register_toggle_action (common_actions, X_("ToggleRCOptionsEditor"), _("Preferences"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_rc_options_window));
222         ActionManager::register_toggle_action (common_actions, X_("ToggleSessionOptionsEditor"), _("Properties"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_options_window));
223         act = ActionManager::register_toggle_action (common_actions, X_("ToggleInspector"), _("Tracks and Busses"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_route_params_window));
224         ActionManager::session_sensitive_actions.push_back (act);
225         ActionManager::session_sensitive_actions.push_back (act);
226         act = ActionManager::register_toggle_action (common_actions, X_("ToggleLocations"), _("Locations"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_location_window));
227         ActionManager::session_sensitive_actions.push_back (act);
228         act = ActionManager::register_toggle_action (common_actions, X_("ToggleBigClock"), _("Big Clock"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_big_clock_window));
229         ActionManager::session_sensitive_actions.push_back (act);
230         act = ActionManager::register_action (common_actions, X_("NewMIDITracer"), _("MIDI Tracer"), sigc::mem_fun(*this, &ARDOUR_UI::new_midi_tracer_window));
231         ActionManager::session_sensitive_actions.push_back (act);
232         ActionManager::register_action (common_actions, X_("About"), _("About"),  sigc::mem_fun(*this, &ARDOUR_UI::show_about));
233         ActionManager::register_action (common_actions, X_("Chat"), _("Chat"),  sigc::mem_fun(*this, &ARDOUR_UI::launch_chat));
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         ActionManager::add_action_group (shuttle_actions);
417         ActionManager::add_action_group (option_actions);
418         ActionManager::add_action_group (jack_actions);
419         ActionManager::add_action_group (transport_actions);
420         ActionManager::add_action_group (main_actions);
421         ActionManager::add_action_group (common_actions);
422 }
423
424 void
425 ARDOUR_UI::set_jack_buffer_size (nframes_t nframes)
426 {
427         Glib::RefPtr<Action> action;
428         const char* action_name = 0;
429
430         switch (nframes) {
431         case 32:
432                 action_name = X_("JACKLatency32");
433                 break;
434         case 64:
435                 action_name = X_("JACKLatency64");
436                 break;
437         case 128:
438                 action_name = X_("JACKLatency128");
439                 break;
440         case 256:
441                 action_name = X_("JACKLatency256");
442                 break;
443         case 512:
444                 action_name = X_("JACKLatency512");
445                 break;
446         case 1024:
447                 action_name = X_("JACKLatency1024");
448                 break;
449         case 2048:
450                 action_name = X_("JACKLatency2048");
451                 break;
452         case 4096:
453                 action_name = X_("JACKLatency4096");
454                 break;
455         case 8192:
456                 action_name = X_("JACKLatency8192");
457                 break;
458         default:
459                 /* XXX can we do anything useful ? */
460                 break;
461         }
462
463         if (action_name) {
464
465                 action = ActionManager::get_action (X_("JACK"), action_name);
466
467                 if (action) {
468                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
469
470                         if (ract && ract->get_active()) {
471                                 engine->request_buffer_size (nframes);
472                                 update_sample_rate (0);
473                         }
474                 }
475         }
476 }
477
478 void
479 ARDOUR_UI::build_menu_bar ()
480 {
481         menu_bar = dynamic_cast<MenuBar*> (ActionManager::get_widget (X_("/Main")));
482         menu_bar->set_name ("MainMenuBar");
483
484         /*
485          * This is needed because this property does not get installed
486          * until the Menu GObject class is registered, which happens
487          * when the first menu instance is created.
488          */
489         // XXX bug in gtkmm causes this to popup an error message
490         // Gtk::Settings::get_default()->property_gtk_can_change_accels() = true;
491         // so use this instead ...
492         gtk_settings_set_long_property (gtk_settings_get_default(), "gtk-can-change-accels", 1, "Ardour:designers");
493
494         wall_clock_box.add (wall_clock_label);
495         wall_clock_box.set_name ("WallClock");
496         wall_clock_label.set_name ("WallClock");
497
498         disk_space_box.add (disk_space_label);
499         disk_space_box.set_name ("WallClock");
500         disk_space_label.set_name ("WallClock");
501
502         cpu_load_box.add (cpu_load_label);
503         cpu_load_box.set_name ("CPULoad");
504         cpu_load_label.set_name ("CPULoad");
505
506         buffer_load_box.add (buffer_load_label);
507         buffer_load_box.set_name ("BufferLoad");
508         buffer_load_label.set_name ("BufferLoad");
509
510         sample_rate_box.add (sample_rate_label);
511         sample_rate_box.set_name ("SampleRate");
512         sample_rate_label.set_name ("SampleRate");
513
514 #ifndef TOP_MENUBAR
515         menu_hbox.pack_start (*menu_bar, true, true);
516 #else
517         use_menubar_as_top_menubar ();
518 #endif
519
520         if (!Profile->get_small_screen()) {
521 #ifndef GTKOSX
522                 // OSX provides its own wallclock, thank you very much
523                 menu_hbox.pack_end (wall_clock_box, false, false, 2);
524 #endif
525                 menu_hbox.pack_end (disk_space_box, false, false, 4);
526         }
527
528         menu_hbox.pack_end (cpu_load_box, false, false, 4);
529         menu_hbox.pack_end (buffer_load_box, false, false, 4);
530         menu_hbox.pack_end (sample_rate_box, false, false, 4);
531
532         menu_bar_base.set_name ("MainMenuBar");
533         menu_bar_base.add (menu_hbox);
534 }
535
536 void
537 ARDOUR_UI::use_menubar_as_top_menubar ()
538 {
539 #ifdef GTKOSX
540         ige_mac_menu_set_menu_bar ((GtkMenuShell*) menu_bar->gobj());
541         // ige_mac_menu_set_quit_menu_item (some_item->gobj());
542 #endif
543 }
544
545 void
546 ARDOUR_UI::setup_clock ()
547 {
548         ARDOUR_UI::Clock.connect (sigc::bind (sigc::mem_fun (big_clock, &AudioClock::set), false));
549
550         big_clock_window = new Window (WINDOW_TOPLEVEL);
551
552         big_clock_window->set_keep_above (true);
553         big_clock_window->set_border_width (0);
554         big_clock_window->add  (big_clock);
555
556         big_clock_window->set_title (_("Big Clock"));
557         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
558         big_clock_window->signal_realize().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_realized));
559         big_clock_window->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
560         big_clock_window->signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), big_clock_window), false);
561         big_clock_window->signal_size_allocate().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_size_allocate));
562
563         manage_window (*big_clock_window);
564 }
565
566 void
567 ARDOUR_UI::big_clock_realized ()
568 {
569         int x, y, w, d;
570
571         set_decoration (big_clock_window, (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
572         big_clock_window->get_window()->get_geometry (x, y, w, big_clock_height, d);
573
574         original_big_clock_height = big_clock_height;
575         original_big_clock_width = w;
576
577         Pango::FontDescription fd (big_clock.get_style()->get_font());
578         original_big_clock_font_size = fd.get_size ();
579         
580         if (!fd.get_size_is_absolute ()) {
581                 original_big_clock_font_size /= PANGO_SCALE;
582         }
583
584         /* we can't set the real size until we know the original one, with the UI rc-file-set font 
585            size, so do this here.
586         */
587
588         XMLNode* node = Config->extra_xml (X_("UI"));
589
590         if (node) {
591
592                 const XMLProperty* prop;
593                 int w, h, x, y;
594                 int have_pos = 0;
595
596                 w = h = x = y = 0;
597
598                 if ((prop = node->property ("big-clock-x-size")) != 0) {
599                         w = atoi (prop->value());
600                 }
601                 if ((prop = node->property ("big-clock-y-size")) != 0) {
602                         h = atoi (prop->value());
603                 }
604
605                 if (w && h) {
606                         big_clock_window->set_default_size (w, h);
607                 }
608
609                 if ((prop = node->property ("big-clock-x-off")) != 0) {
610                         x = atoi (prop->value());
611                         have_pos++;
612                 }
613                 if ((prop = node->property ("big-clock-y-off")) != 0) {
614                         y = atoi (prop->value());
615                         have_pos++;
616                 }
617
618                 if (have_pos == 2) {
619                         big_clock_window->move (x, y);
620                 }
621         }
622 }
623
624 void
625 ARDOUR_UI::float_big_clock (Gtk::Window* parent)
626 {
627         if (big_clock_window) {
628                 if (parent) {
629                         big_clock_window->set_transient_for (*parent);
630                 } else {
631                         gtk_window_set_transient_for (big_clock_window->gobj(), (GtkWindow*) 0);
632                 }
633         }
634 }
635
636 void
637 ARDOUR_UI::big_clock_size_allocate (Gtk::Allocation& allocation)
638 {
639         if (!big_clock_resize_in_progress) {
640                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::idle_big_clock_text_resizer), 0, 0));
641                 big_clock_resize_in_progress = true;
642         }
643 }
644
645 bool
646 ARDOUR_UI::idle_big_clock_text_resizer (int win_w, int win_h)
647 {
648         big_clock_resize_in_progress = false;
649
650         Glib::RefPtr<Gdk::Window> win = big_clock_window->get_window();
651         Pango::FontDescription fd (big_clock.get_style()->get_font());
652         int current_size = fd.get_size ();
653         int x, y, w, h, d;
654
655         if (!fd.get_size_is_absolute ()) {
656                 current_size /= PANGO_SCALE;
657         }
658
659         win->get_geometry (x, y, w, h, d);
660
661         double scale  = min (((double) w / (double) original_big_clock_width), 
662                              ((double) h / (double) original_big_clock_height));
663
664         int size = (int) lrintf (original_big_clock_font_size * scale);
665
666         if (size != current_size) {
667
668                 string family = fd.get_family();
669                 char buf[family.length()+16];
670                 snprintf (buf, family.length()+16, "%s %d", family.c_str(), size);
671                 
672                 try { 
673                         Pango::FontDescription fd (buf);
674                         Glib::RefPtr<Gtk::RcStyle> rcstyle = big_clock.get_modifier_style ();
675                         rcstyle->set_font (fd);
676                         big_clock.modify_style (rcstyle);
677                 } 
678                 
679                 catch (...) {
680                         /* oh well, do nothing */
681                 }
682         }
683
684         return false;
685 }
686
687 void
688 ARDOUR_UI::save_ardour_state ()
689 {
690         if (!keyboard || !mixer || !editor) {
691                 return;
692         }
693
694         /* XXX this is all a bit dubious. add_extra_xml() uses
695            a different lifetime model from add_instant_xml().
696         */
697
698         XMLNode* node = new XMLNode (keyboard->get_state());
699         Config->add_extra_xml (*node);
700         Config->add_extra_xml (get_transport_controllable_state());
701
702         XMLNode* window_node = new XMLNode (X_("UI"));
703         
704         window_node->add_property ("show-big-clock", (big_clock_window && big_clock_window->is_visible() ? "yes" : "no"));
705
706         Glib::RefPtr<Gdk::Window> win;
707
708         if (big_clock_window && (win = big_clock_window->get_window())) {
709
710                 int w, h;
711                 int xoff, yoff;
712                 char buf[32];
713
714                 win->get_size (w, h);
715                 win->get_position (xoff, yoff);
716
717                 snprintf (buf, sizeof (buf), "%d", w);
718                 window_node->add_property ("big-clock-x-size", buf);
719                 snprintf (buf, sizeof (buf), "%d", h);
720                 window_node->add_property ("big-clock-y-size", buf);
721                 snprintf (buf, sizeof (buf), "%d", xoff);
722                 window_node->add_property ("big-clock-x-off", buf);
723                 snprintf (buf, sizeof (buf), "%d", yoff);
724                 window_node->add_property ("big-clock-y-off", buf);
725         }
726
727         /* tearoffs */
728
729         XMLNode* tearoff_node = new XMLNode (X_("Tearoffs"));
730
731         if (transport_tearoff) {
732                 XMLNode* t = new XMLNode (X_("transport"));
733                 transport_tearoff->add_state (*t);
734                 tearoff_node->add_child_nocopy (*t);
735         } 
736
737         if (mixer && mixer->monitor_section()) {
738                 XMLNode* t = new XMLNode (X_("monitor-section"));
739                 mixer->monitor_section()->tearoff().add_state (*t);
740                 tearoff_node->add_child_nocopy (*t);
741         } 
742
743         if (editor && editor->mouse_mode_tearoff()) {
744                 XMLNode* t = new XMLNode (X_("mouse-mode"));
745                 editor->mouse_mode_tearoff ()->add_state (*t);
746                 tearoff_node->add_child_nocopy (*t);
747         } 
748         
749         window_node->add_child_nocopy (*tearoff_node);
750
751         Config->add_extra_xml (*window_node);
752
753         if (_startup && _startup->engine_control() && _startup->engine_control()->was_used()) {
754                 Config->add_extra_xml (_startup->engine_control()->get_state());
755         }
756         Config->save_state();
757         ui_config->save_state ();
758
759         XMLNode enode(static_cast<Stateful*>(editor)->get_state());
760         XMLNode mnode(mixer->get_state());
761
762         if (_session) {
763                 _session->add_instant_xml (enode);
764                 _session->add_instant_xml (mnode);
765         } else {
766                 Config->add_instant_xml (enode);
767                 Config->add_instant_xml (mnode);
768         }
769
770         Keyboard::save_keybindings ();
771 }
772