Setup fixed ports for MIDI control data; hence remove configuration of those ports...
[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
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_toggle_action (common_actions, X_("ToggleMIDITracer"), _("MIDI Tracer"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_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_toggle_action (common_actions, X_("ToggleThemeManager"), _("Theme Manager"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_theme_manager));
233         ActionManager::register_toggle_action (common_actions, X_("ToggleKeyEditor"), _("Key Bindings"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_key_editor));
234         ActionManager::register_toggle_action (common_actions, X_("ToggleBundleManager"), _("Bundle Manager"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_bundle_manager));
235
236 #if 0
237         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));
238         ActionManager::session_sensitive_actions.push_back (act);
239         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));
240         ActionManager::session_sensitive_actions.push_back (act);
241         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));
242         ActionManager::session_sensitive_actions.push_back (act);
243         //act = ActionManager::register_action (common_actions, X_("AddMidiBus"), _("Add Midi Bus"), sigc::mem_fun(*this, &ARDOUR_UI::session_add_midi_bus));
244         //ActionManager::session_sensitive_actions.push_back (act);
245 #endif
246         act = ActionManager::register_action (common_actions, X_("Save"), _("Save"),  sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::save_state), string(""), false));
247         ActionManager::session_sensitive_actions.push_back (act);
248         ActionManager::write_sensitive_actions.push_back (act);
249         act = ActionManager::register_action (common_actions, X_("RemoveLastCapture"), _("Remove Last Capture"), sigc::mem_fun(*this, &ARDOUR_UI::remove_last_capture));
250         ActionManager::session_sensitive_actions.push_back (act);
251
252         Glib::RefPtr<ActionGroup> transport_actions = ActionGroup::create (X_("Transport"));
253
254         /* do-nothing action for the "transport" menu bar item */
255
256         ActionManager::register_action (transport_actions, X_("Transport"), _("Transport"));
257
258         /* these two are not used by key bindings, instead use ToggleRoll for that. these two do show up in
259            menus and via button proxies.
260         */
261
262         act = ActionManager::register_action (transport_actions, X_("Stop"), _("Stop"), sigc::mem_fun(*this, &ARDOUR_UI::transport_stop));
263         ActionManager::session_sensitive_actions.push_back (act);
264         ActionManager::transport_sensitive_actions.push_back (act);
265         act = ActionManager::register_action (transport_actions, X_("Roll"), _("Roll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_roll));
266         ActionManager::session_sensitive_actions.push_back (act);
267         ActionManager::transport_sensitive_actions.push_back (act);
268
269         act = ActionManager::register_action (transport_actions, X_("ToggleRoll"), _("Start/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, false));
270         ActionManager::session_sensitive_actions.push_back (act);
271         ActionManager::transport_sensitive_actions.push_back (act);
272         act = ActionManager::register_action (transport_actions, X_("ToggleRollMaybe"), _("Start/Continue/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, true));
273         ActionManager::session_sensitive_actions.push_back (act);
274         ActionManager::transport_sensitive_actions.push_back (act);
275         act = ActionManager::register_action (transport_actions, X_("ToggleRollForgetCapture"), _("Stop and Forget Capture"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::toggle_roll), true, false));
276         ActionManager::session_sensitive_actions.push_back (act);
277         ActionManager::transport_sensitive_actions.push_back (act);
278
279         /* these two behave as follows:
280
281            - if transport speed != 1.0 or != -1.0, change speed to 1.0 or -1.0 (respectively)
282            - otherwise do nothing
283         */
284
285         act = ActionManager::register_action (transport_actions, X_("TransitionToRoll"), _("Transition To Roll"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), true));
286         ActionManager::session_sensitive_actions.push_back (act);
287         ActionManager::transport_sensitive_actions.push_back (act);
288
289         act = ActionManager::register_action (transport_actions, X_("TransitionToReverse"), _("Transition To Reverse"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), false));
290         ActionManager::session_sensitive_actions.push_back (act);
291         ActionManager::transport_sensitive_actions.push_back (act);
292
293         act = ActionManager::register_action (transport_actions, X_("Loop"), _("Play Loop Range"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_auto_loop));
294         ActionManager::session_sensitive_actions.push_back (act);
295         ActionManager::transport_sensitive_actions.push_back (act);
296         act = ActionManager::register_action (transport_actions, X_("PlaySelection"), _("Play Selection"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_selection));
297         ActionManager::session_sensitive_actions.push_back (act);
298         ActionManager::transport_sensitive_actions.push_back (act);
299
300         act = ActionManager::register_action (transport_actions, X_("Record"), _("Enable Record"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), false));
301         ActionManager::session_sensitive_actions.push_back (act);
302         ActionManager::write_sensitive_actions.push_back (act);
303         act = ActionManager::register_action (transport_actions, X_("record-roll"), _("Start Recording"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), true));
304         ActionManager::session_sensitive_actions.push_back (act);
305         ActionManager::write_sensitive_actions.push_back (act);
306         ActionManager::transport_sensitive_actions.push_back (act);
307         act = ActionManager::register_action (transport_actions, X_("Rewind"), _("Rewind"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 0));
308         ActionManager::session_sensitive_actions.push_back (act);
309         ActionManager::transport_sensitive_actions.push_back (act);
310         act = ActionManager::register_action (transport_actions, X_("RewindSlow"), _("Rewind (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), -1));
311         ActionManager::session_sensitive_actions.push_back (act);
312         ActionManager::transport_sensitive_actions.push_back (act);
313         act = ActionManager::register_action (transport_actions, X_("RewindFast"), _("Rewind (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 1));
314         ActionManager::session_sensitive_actions.push_back (act);
315         ActionManager::transport_sensitive_actions.push_back (act);
316         act = ActionManager::register_action (transport_actions, X_("Forward"), _("Forward"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 0));
317         ActionManager::session_sensitive_actions.push_back (act);
318         ActionManager::transport_sensitive_actions.push_back (act);
319         act = ActionManager::register_action (transport_actions, X_("ForwardSlow"), _("Forward (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), -1));
320         ActionManager::session_sensitive_actions.push_back (act);
321         ActionManager::transport_sensitive_actions.push_back (act);
322         act = ActionManager::register_action (transport_actions, X_("ForwardFast"), _("Forward (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 1));
323         ActionManager::session_sensitive_actions.push_back (act);
324         ActionManager::transport_sensitive_actions.push_back (act);
325         act = ActionManager::register_action (transport_actions, X_("GotoZero"), _("Goto Zero"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_zero));
326         ActionManager::session_sensitive_actions.push_back (act);
327         ActionManager::transport_sensitive_actions.push_back (act);
328         act = ActionManager::register_action (transport_actions, X_("GotoStart"), _("Goto Start"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_start));
329         ActionManager::session_sensitive_actions.push_back (act);
330         ActionManager::transport_sensitive_actions.push_back (act);
331         act = ActionManager::register_action (transport_actions, X_("GotoEnd"), _("Goto End"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_end));
332         ActionManager::session_sensitive_actions.push_back (act);
333         ActionManager::transport_sensitive_actions.push_back (act);
334         act = ActionManager::register_action (transport_actions, X_("GotoWallClock"), _("Goto Wall Clock"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_wallclock));
335         ActionManager::session_sensitive_actions.push_back (act);
336         ActionManager::transport_sensitive_actions.push_back (act);
337
338         act = ActionManager::register_action (transport_actions, X_("focus-on-clock"), _("Focus On Clock"), sigc::mem_fun(primary_clock, &AudioClock::focus));
339         ActionManager::session_sensitive_actions.push_back (act);
340         ActionManager::transport_sensitive_actions.push_back (act);
341
342         act = ActionManager::register_action (transport_actions, X_("primary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::BBT));
343         ActionManager::session_sensitive_actions.push_back (act);
344         act = ActionManager::register_action (transport_actions, X_("primary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::MinSec));
345         ActionManager::session_sensitive_actions.push_back (act);
346         act = ActionManager::register_action (transport_actions, X_("primary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Frames));
347         ActionManager::session_sensitive_actions.push_back (act);
348         act = ActionManager::register_action (transport_actions, X_("primary-clock-off"), _("Off"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Off));
349         ActionManager::session_sensitive_actions.push_back (act);
350
351         act = ActionManager::register_action (transport_actions, X_("secondary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::BBT));
352         ActionManager::session_sensitive_actions.push_back (act);
353         act = ActionManager::register_action (transport_actions, X_("secondary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::MinSec));
354         ActionManager::session_sensitive_actions.push_back (act);
355         act = ActionManager::register_action (transport_actions, X_("secondary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Frames));
356         ActionManager::session_sensitive_actions.push_back (act);
357         act = ActionManager::register_action (transport_actions, X_("secondary-clock-off"), _("Off"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Off));
358         ActionManager::session_sensitive_actions.push_back (act);
359
360         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchIn"), _("Punch In"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_in));
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_("TogglePunchOut"), _("Punch Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_out));
364         ActionManager::session_sensitive_actions.push_back (act);
365         ActionManager::transport_sensitive_actions.push_back (act);
366         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunch"), _("Punch In/Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch));
367         ActionManager::session_sensitive_actions.push_back (act);
368         ActionManager::transport_sensitive_actions.push_back (act);
369         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleClick"), _("Click"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_click));
370         ActionManager::session_sensitive_actions.push_back (act);
371         ActionManager::transport_sensitive_actions.push_back (act);
372         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoInput"), _("Auto Input"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_input));
373         ActionManager::session_sensitive_actions.push_back (act);
374         ActionManager::transport_sensitive_actions.push_back (act);
375         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoPlay"), _("Auto Play"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_play));
376         ActionManager::session_sensitive_actions.push_back (act);
377         ActionManager::transport_sensitive_actions.push_back (act);
378         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoReturn"), _("Auto Return"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_return));
379         ActionManager::session_sensitive_actions.push_back (act);
380         ActionManager::transport_sensitive_actions.push_back (act);
381
382         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleVideoSync"), _("Sync Startup to Video"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_video_sync));
383         ActionManager::session_sensitive_actions.push_back (act);
384         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleTimeMaster"), _("Time Master"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_time_master));
385         ActionManager::session_sensitive_actions.push_back (act);
386         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleExternalSync"), _(""), sigc::mem_fun(*this, &ARDOUR_UI::toggle_external_sync));
387         ActionManager::session_sensitive_actions.push_back (act);
388
389         for (int i = 1; i <= 32; ++i) {
390                 string const a = string_compose (X_("ToggleRecordEnableTrack%1"), i);
391                 string const n = string_compose (_("Toggle Record Enable Track %1"), i);
392                 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));
393                 ActionManager::session_sensitive_actions.push_back (act);
394         }
395
396         Glib::RefPtr<ActionGroup> shuttle_actions = ActionGroup::create ("ShuttleActions");
397
398         shuttle_actions->add (Action::create (X_("SetShuttleUnitsPercentage"), _("Percentage")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Percentage)));
399         shuttle_actions->add (Action::create (X_("SetShuttleUnitsSemitones"), _("Semitones")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Semitones)));
400
401         Glib::RefPtr<ActionGroup> option_actions = ActionGroup::create ("options");
402
403         act = ActionManager::register_toggle_action (option_actions, X_("SendMTC"), _("Send MTC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mtc));
404         ActionManager::session_sensitive_actions.push_back (act);
405         act = ActionManager::register_toggle_action (option_actions, X_("SendMMC"), _("Send MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mmc));
406         ActionManager::session_sensitive_actions.push_back (act);
407         act = ActionManager::register_toggle_action (option_actions, X_("UseMMC"), _("Use MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_use_mmc));
408         ActionManager::session_sensitive_actions.push_back (act);
409         act = ActionManager::register_toggle_action (option_actions, X_("SendMidiClock"), _("Send MIDI Clock"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_clock));
410         ActionManager::session_sensitive_actions.push_back (act);
411         act = ActionManager::register_toggle_action (option_actions, X_("SendMIDIfeedback"), _("Send MIDI Feedback"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_feedback));
412         ActionManager::session_sensitive_actions.push_back (act);
413
414         ActionManager::add_action_group (shuttle_actions);
415         ActionManager::add_action_group (option_actions);
416         ActionManager::add_action_group (jack_actions);
417         ActionManager::add_action_group (transport_actions);
418         ActionManager::add_action_group (main_actions);
419         ActionManager::add_action_group (common_actions);
420 }
421
422 void
423 ARDOUR_UI::set_jack_buffer_size (nframes_t nframes)
424 {
425         Glib::RefPtr<Action> action;
426         const char* action_name = 0;
427
428         switch (nframes) {
429         case 32:
430                 action_name = X_("JACKLatency32");
431                 break;
432         case 64:
433                 action_name = X_("JACKLatency64");
434                 break;
435         case 128:
436                 action_name = X_("JACKLatency128");
437                 break;
438         case 256:
439                 action_name = X_("JACKLatency256");
440                 break;
441         case 512:
442                 action_name = X_("JACKLatency512");
443                 break;
444         case 1024:
445                 action_name = X_("JACKLatency1024");
446                 break;
447         case 2048:
448                 action_name = X_("JACKLatency2048");
449                 break;
450         case 4096:
451                 action_name = X_("JACKLatency4096");
452                 break;
453         case 8192:
454                 action_name = X_("JACKLatency8192");
455                 break;
456         default:
457                 /* XXX can we do anything useful ? */
458                 break;
459         }
460
461         if (action_name) {
462
463                 action = ActionManager::get_action (X_("JACK"), action_name);
464
465                 if (action) {
466                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
467
468                         if (ract && ract->get_active()) {
469                                 engine->request_buffer_size (nframes);
470                                 update_sample_rate (0);
471                         }
472                 }
473         }
474 }
475
476 void
477 ARDOUR_UI::build_menu_bar ()
478 {
479         menu_bar = dynamic_cast<MenuBar*> (ActionManager::get_widget (X_("/Main")));
480         menu_bar->set_name ("MainMenuBar");
481
482         /*
483          * This is needed because this property does not get installed
484          * until the Menu GObject class is registered, which happens
485          * when the first menu instance is created.
486          */
487         // XXX bug in gtkmm causes this to popup an error message
488         // Gtk::Settings::get_default()->property_gtk_can_change_accels() = true;
489         // so use this instead ...
490         gtk_settings_set_long_property (gtk_settings_get_default(), "gtk-can-change-accels", 1, "Ardour:designers");
491
492         wall_clock_box.add (wall_clock_label);
493         wall_clock_box.set_name ("WallClock");
494         wall_clock_label.set_name ("WallClock");
495
496         disk_space_box.add (disk_space_label);
497         disk_space_box.set_name ("WallClock");
498         disk_space_label.set_name ("WallClock");
499
500         cpu_load_box.add (cpu_load_label);
501         cpu_load_box.set_name ("CPULoad");
502         cpu_load_label.set_name ("CPULoad");
503
504         buffer_load_box.add (buffer_load_label);
505         buffer_load_box.set_name ("BufferLoad");
506         buffer_load_label.set_name ("BufferLoad");
507
508         sample_rate_box.add (sample_rate_label);
509         sample_rate_box.set_name ("SampleRate");
510         sample_rate_label.set_name ("SampleRate");
511
512 #ifndef TOP_MENUBAR
513         menu_hbox.pack_start (*menu_bar, true, true);
514 #else
515         use_menubar_as_top_menubar ();
516 #endif
517
518         if (!Profile->get_small_screen()) {
519 #ifndef GTKOSX
520                 // OSX provides its own wallclock, thank you very much
521                 menu_hbox.pack_end (wall_clock_box, false, false, 2);
522 #endif
523                 menu_hbox.pack_end (disk_space_box, false, false, 4);
524         }
525
526         menu_hbox.pack_end (cpu_load_box, false, false, 4);
527         menu_hbox.pack_end (buffer_load_box, false, false, 4);
528         menu_hbox.pack_end (sample_rate_box, false, false, 4);
529
530         menu_bar_base.set_name ("MainMenuBar");
531         menu_bar_base.add (menu_hbox);
532 }
533
534 void
535 ARDOUR_UI::use_menubar_as_top_menubar ()
536 {
537 #ifdef GTKOSX
538         ige_mac_menu_set_menu_bar ((GtkMenuShell*) menu_bar->gobj());
539         // ige_mac_menu_set_quit_menu_item (some_item->gobj());
540 #endif
541 }
542
543 void
544 ARDOUR_UI::setup_clock ()
545 {
546         ARDOUR_UI::Clock.connect (sigc::bind (sigc::mem_fun (big_clock, &AudioClock::set), false));
547
548         big_clock_window = new Window (WINDOW_TOPLEVEL);
549
550         big_clock_window->set_keep_above (true);
551         big_clock_window->set_border_width (0);
552         big_clock_window->add  (big_clock);
553
554         big_clock_window->set_title (_("Big Clock"));
555         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
556         big_clock_window->signal_realize().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_realized));
557         big_clock_window->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
558         big_clock_window->signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), big_clock_window), false);
559         big_clock_window->signal_size_allocate().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_size_allocate));
560
561         manage_window (*big_clock_window);
562 }
563
564 void
565 ARDOUR_UI::big_clock_realized ()
566 {
567         int x, y, w, d;
568
569         set_decoration (big_clock_window, (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
570         big_clock_window->get_window()->get_geometry (x, y, w, big_clock_height, d);
571
572         original_big_clock_height = big_clock_height;
573         original_big_clock_width = w;
574
575         Pango::FontDescription fd (big_clock.get_style()->get_font());
576         original_big_clock_font_size = fd.get_size ();
577         
578         if (!fd.get_size_is_absolute ()) {
579                 original_big_clock_font_size /= PANGO_SCALE;
580         }
581
582         /* we can't set the real size until we know the original one, with the UI rc-file-set font 
583            size, so do this here.
584         */
585
586         XMLNode* node = Config->extra_xml (X_("UI"));
587
588         if (node) {
589
590                 const XMLProperty* prop;
591                 int w, h, x, y;
592                 int have_pos = 0;
593
594                 w = h = x = y = 0;
595
596                 if ((prop = node->property ("big-clock-x-size")) != 0) {
597                         w = atoi (prop->value());
598                 }
599                 if ((prop = node->property ("big-clock-y-size")) != 0) {
600                         h = atoi (prop->value());
601                 }
602
603                 if (w && h) {
604                         big_clock_window->set_default_size (w, h);
605                 }
606
607                 if ((prop = node->property ("big-clock-x-off")) != 0) {
608                         x = atoi (prop->value());
609                         have_pos++;
610                 }
611                 if ((prop = node->property ("big-clock-y-off")) != 0) {
612                         y = atoi (prop->value());
613                         have_pos++;
614                 }
615
616                 if (have_pos == 2) {
617                         big_clock_window->move (x, y);
618                 }
619         }
620 }
621
622 void
623 ARDOUR_UI::float_big_clock (Gtk::Window* parent)
624 {
625         if (big_clock_window) {
626                 if (parent) {
627                         big_clock_window->set_transient_for (*parent);
628                 } else {
629                         gtk_window_set_transient_for (big_clock_window->gobj(), (GtkWindow*) 0);
630                 }
631         }
632 }
633
634 void
635 ARDOUR_UI::big_clock_size_allocate (Gtk::Allocation& allocation)
636 {
637         if (!big_clock_resize_in_progress) {
638                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::idle_big_clock_text_resizer), 0, 0));
639                 big_clock_resize_in_progress = true;
640         }
641 }
642
643 bool
644 ARDOUR_UI::idle_big_clock_text_resizer (int win_w, int win_h)
645 {
646         big_clock_resize_in_progress = false;
647
648         Glib::RefPtr<Gdk::Window> win = big_clock_window->get_window();
649         Pango::FontDescription fd (big_clock.get_style()->get_font());
650         int current_size = fd.get_size ();
651         int x, y, w, h, d;
652
653         if (!fd.get_size_is_absolute ()) {
654                 current_size /= PANGO_SCALE;
655         }
656
657         win->get_geometry (x, y, w, h, d);
658
659         double scale  = min (((double) w / (double) original_big_clock_width), 
660                              ((double) h / (double) original_big_clock_height));
661
662         int size = (int) lrintf (original_big_clock_font_size * scale);
663
664         if (size != current_size) {
665
666                 string family = fd.get_family();
667                 char buf[family.length()+16];
668                 snprintf (buf, family.length()+16, "%s %d", family.c_str(), size);
669                 
670                 try { 
671                         Pango::FontDescription fd (buf);
672                         Glib::RefPtr<Gtk::RcStyle> rcstyle = big_clock.get_modifier_style ();
673                         rcstyle->set_font (fd);
674                         big_clock.modify_style (rcstyle);
675                 } 
676                 
677                 catch (...) {
678                         /* oh well, do nothing */
679                 }
680         }
681
682         return false;
683 }
684
685 void
686 ARDOUR_UI::save_ardour_state ()
687 {
688         if (!keyboard || !mixer || !editor) {
689                 return;
690         }
691
692         /* XXX this is all a bit dubious. add_extra_xml() uses
693            a different lifetime model from add_instant_xml().
694         */
695
696         XMLNode* node = new XMLNode (keyboard->get_state());
697         Config->add_extra_xml (*node);
698         Config->add_extra_xml (get_transport_controllable_state());
699
700         XMLNode* window_node = new XMLNode (X_("UI"));
701         
702         window_node->add_property ("show-big-clock", (big_clock_window && big_clock_window->is_visible() ? "yes" : "no"));
703
704         Glib::RefPtr<Gdk::Window> win;
705
706         if (big_clock_window && (win = big_clock_window->get_window())) {
707
708                 int w, h;
709                 int xoff, yoff;
710                 char buf[32];
711
712                 win->get_size (w, h);
713                 win->get_position (xoff, yoff);
714
715                 snprintf (buf, sizeof (buf), "%d", w);
716                 window_node->add_property ("big-clock-x-size", buf);
717                 snprintf (buf, sizeof (buf), "%d", h);
718                 window_node->add_property ("big-clock-y-size", buf);
719                 snprintf (buf, sizeof (buf), "%d", xoff);
720                 window_node->add_property ("big-clock-x-off", buf);
721                 snprintf (buf, sizeof (buf), "%d", yoff);
722                 window_node->add_property ("big-clock-y-off", buf);
723         }
724
725         /* tearoffs */
726
727         XMLNode* tearoff_node = new XMLNode (X_("Tearoffs"));
728
729         if (transport_tearoff) {
730                 XMLNode* t = new XMLNode (X_("transport"));
731                 transport_tearoff->add_state (*t);
732                 tearoff_node->add_child_nocopy (*t);
733         } 
734
735         if (mixer && mixer->monitor_section()) {
736                 XMLNode* t = new XMLNode (X_("monitor-section"));
737                 mixer->monitor_section()->tearoff().add_state (*t);
738                 tearoff_node->add_child_nocopy (*t);
739         } 
740
741         if (editor && editor->mouse_mode_tearoff()) {
742                 XMLNode* t = new XMLNode (X_("mouse-mode"));
743                 editor->mouse_mode_tearoff ()->add_state (*t);
744                 tearoff_node->add_child_nocopy (*t);
745         } 
746         
747         window_node->add_child_nocopy (*tearoff_node);
748
749         Config->add_extra_xml (*window_node);
750
751         if (_startup && _startup->engine_control() && _startup->engine_control()->was_used()) {
752                 Config->add_extra_xml (_startup->engine_control()->get_state());
753         }
754         Config->save_state();
755         ui_config->save_state ();
756
757         XMLNode enode(static_cast<Stateful*>(editor)->get_state());
758         XMLNode mnode(mixer->get_state());
759
760         if (_session) {
761                 _session->add_instant_xml (enode);
762                 _session->add_instant_xml (mnode);
763         } else {
764                 Config->add_instant_xml (enode);
765                 Config->add_instant_xml (mnode);
766         }
767
768         Keyboard::save_keybindings ();
769 }
770