better system for managing kbd focus after cmd-w closing a dialog.
[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 "meterbridge.h"
53 #include "mixer_ui.h"
54 #include "startup.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         editor->signal_event().connect (sigc::bind (sigc::ptr_fun (&Keyboard::catch_user_event_for_pre_dialog_focus), editor));
91
92         return 0;
93 }
94
95 void
96 ARDOUR_UI::install_actions ()
97 {
98         Glib::RefPtr<ActionGroup> main_actions = ActionGroup::create (X_("Main"));
99         Glib::RefPtr<ActionGroup> main_menu_actions = ActionGroup::create (X_("Main_menu"));
100         Glib::RefPtr<Action> act;
101
102         /* menus + submenus that need action items */
103
104         ActionManager::register_action (main_menu_actions, X_("Session"), _("Session"));
105         act = ActionManager::register_action (main_menu_actions, X_("Cleanup"), _("Clean-up"));
106         ActionManager::write_sensitive_actions.push_back (act);
107         ActionManager::register_action (main_menu_actions, X_("Sync"), _("Sync"));
108         ActionManager::register_action (main_menu_actions, X_("TransportOptions"), _("Options"));
109         ActionManager::register_action (main_menu_actions, X_("WindowMenu"), _("Window"));
110         ActionManager::register_action (main_menu_actions, X_("Help"), _("Help"));
111         ActionManager::register_action (main_menu_actions, X_("KeyMouseActions"), _("Misc. Shortcuts"));
112         ActionManager::register_action (main_menu_actions, X_("AudioFileFormat"), _("Audio File Format"));
113         ActionManager::register_action (main_menu_actions, X_("AudioFileFormatHeader"), _("File Type"));
114         ActionManager::register_action (main_menu_actions, X_("AudioFileFormatData"), _("Sample Format"));
115         ActionManager::register_action (main_menu_actions, X_("ControlSurfaces"), _("Control Surfaces"));
116         ActionManager::register_action (main_menu_actions, X_("Plugins"), _("Plugins"));
117         ActionManager::register_action (main_menu_actions, X_("Metering"), _("Metering"));
118         ActionManager::register_action (main_menu_actions, X_("MeteringFallOffRate"), _("Fall Off Rate"));
119         ActionManager::register_action (main_menu_actions, X_("MeteringHoldTime"), _("Hold Time"));
120         ActionManager::register_action (main_menu_actions, X_("Denormals"), _("Denormal Handling"));
121
122         /* the real actions */
123
124         act = ActionManager::register_action (main_actions, X_("New"), _("New..."),  hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::get_session_parameters), false, true, "")));
125
126         ActionManager::register_action (main_actions, X_("Open"), _("Open..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_session));
127         ActionManager::register_action (main_actions, X_("Recent"), _("Recent..."),  sigc::mem_fun(*this, &ARDOUR_UI::open_recent_session));
128         act = ActionManager::register_action (main_actions, X_("Close"), _("Close"),  sigc::mem_fun(*this, &ARDOUR_UI::close_session));
129         ActionManager::session_sensitive_actions.push_back (act);
130
131         act = ActionManager::register_action (main_actions, X_("AddTrackBus"), _("Add Track or Bus..."),
132                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_route), (Gtk::Window*) 0));
133         ActionManager::session_sensitive_actions.push_back (act);
134         ActionManager::write_sensitive_actions.push_back (act);
135
136         act = ActionManager::register_action (main_actions, X_("duplicate-routes"), _("Duplicate Tracks/Busses..."),
137                                               sigc::mem_fun(*this, &ARDOUR_UI::start_duplicate_routes));
138         ActionManager::session_sensitive_actions.push_back (act);
139         ActionManager::write_sensitive_actions.push_back (act);
140         ActionManager::track_selection_sensitive_actions.push_back (act);
141
142         act = ActionManager::register_action (main_actions, X_("cancel-solo"), _("Cancel Solo"), sigc::mem_fun(*this, &ARDOUR_UI::cancel_solo));
143         ActionManager::session_sensitive_actions.push_back (act);
144         ActionManager::write_sensitive_actions.push_back (act);
145
146
147         act = ActionManager::register_action (main_actions, X_("OpenVideo"), _("Open Video"),
148                                               sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::add_video), (Gtk::Window*) 0));
149         ActionManager::session_sensitive_actions.push_back (act);
150         act = ActionManager::register_action (main_actions, X_("CloseVideo"), _("Remove Video"),
151                                               sigc::mem_fun (*this, &ARDOUR_UI::remove_video));
152         act->set_sensitive (false);
153         act = ActionManager::register_action (main_actions, X_("ExportVideo"), _("Export to Video File"),
154                         hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::export_video), false)));
155         ActionManager::session_sensitive_actions.push_back (act);
156
157         act = ActionManager::register_action (main_actions, X_("SnapshotStay"), _("Snapshot (& keep working on current version) ..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), false));
158         ActionManager::session_sensitive_actions.push_back (act);
159         ActionManager::write_sensitive_actions.push_back (act);
160
161         act = ActionManager::register_action (main_actions, X_("SnapshotSwitch"), _("Snapshot (& switch to new version) ..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::snapshot_session), true));
162         ActionManager::session_sensitive_actions.push_back (act);
163         ActionManager::write_sensitive_actions.push_back (act);
164
165         act = ActionManager::register_action (main_actions, X_("QuickSnapshotStay"), _("Quick Snapshot (& keep working on current version) ..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::quick_snapshot_session), false));
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_("QuickSnapshotSwitch"), _("Quick Snapshot (& switch to new version) ..."), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::quick_snapshot_session), true));
170         ActionManager::session_sensitive_actions.push_back (act);
171         ActionManager::write_sensitive_actions.push_back (act);
172
173         act = ActionManager::register_action (main_actions, X_("SaveAs"), _("Save As..."), sigc::mem_fun(*this, &ARDOUR_UI::save_session_as));
174         ActionManager::session_sensitive_actions.push_back (act);
175         ActionManager::write_sensitive_actions.push_back (act);
176
177         act = ActionManager::register_action (main_actions, X_("Rename"), _("Rename..."), sigc::mem_fun(*this, &ARDOUR_UI::rename_session));
178         ActionManager::session_sensitive_actions.push_back (act);
179         ActionManager::write_sensitive_actions.push_back (act);
180
181         act = ActionManager::register_action (main_actions, X_("SaveTemplate"), _("Save Template..."),  sigc::mem_fun(*this, &ARDOUR_UI::save_template));
182         ActionManager::session_sensitive_actions.push_back (act);
183
184         act = ActionManager::register_action (main_actions, X_("Metadata"), _("Metadata"));
185         ActionManager::session_sensitive_actions.push_back (act);
186
187         act = ActionManager::register_action (main_actions, X_("EditMetadata"), _("Edit Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::edit_metadata));
188         ActionManager::session_sensitive_actions.push_back (act);
189
190         act = ActionManager::register_action (main_actions, X_("ImportMetadata"), _("Import Metadata..."),  sigc::mem_fun(*this, &ARDOUR_UI::import_metadata));
191         ActionManager::session_sensitive_actions.push_back (act);
192
193         act = ActionManager::register_action (main_actions, X_("ExportAudio"), _("Export to Audio File(s)..."),  sigc::mem_fun (*editor, &PublicEditor::export_audio));
194         ActionManager::session_sensitive_actions.push_back (act);
195
196         act = ActionManager::register_action (main_actions, X_("StemExport"), _("Stem export..."),  sigc::mem_fun (*editor, &PublicEditor::stem_export));
197         ActionManager::session_sensitive_actions.push_back (act);
198
199         act = ActionManager::register_action (main_actions, X_("Export"), _("Export"));
200         ActionManager::session_sensitive_actions.push_back (act);
201
202         act = ActionManager::register_action (main_actions, X_("CleanupUnused"), _("Clean-up Unused Sources..."),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::cleanup));
203         ActionManager::session_sensitive_actions.push_back (act);
204         ActionManager::write_sensitive_actions.push_back (act);
205
206         act = ActionManager::register_action (main_actions, X_("CleanupPeakFiles"), _("Reset Peak Files"),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::cleanup_peakfiles));
207         ActionManager::session_sensitive_actions.push_back (act);
208         ActionManager::write_sensitive_actions.push_back (act);
209
210         act = ActionManager::register_action (main_actions, X_("FlushWastebasket"), _("Flush Wastebasket"),  sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::flush_trash));
211         ActionManager::write_sensitive_actions.push_back (act);
212         ActionManager::session_sensitive_actions.push_back (act);
213
214         /* these actions are intended to be shared across all windows */
215
216         common_actions = ActionGroup::create (X_("Common"));
217         ActionManager::register_action (common_actions, X_("Quit"), _("Quit"), (hide_return (sigc::mem_fun(*this, &ARDOUR_UI::finish))));
218         ActionManager::register_action (common_actions, X_("Hide"), _("Hide"), sigc::mem_fun (*this, &ARDOUR_UI::hide_application));
219
220         /* windows visibility actions */
221
222         ActionManager::register_toggle_action (common_actions, X_("ToggleMaximalEditor"), _("Maximise Editor Space"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_editing_space));
223         ActionManager::register_toggle_action (common_actions, X_("ToggleMaximalMixer"), _("Maximise Mixer Space"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_mixer_space));
224         act = ActionManager::register_toggle_action (common_actions, X_("KeepTearoffs"), _("Show Toolbars"), mem_fun (*this, &ARDOUR_UI::toggle_keep_tearoffs));
225         ActionManager::session_sensitive_actions.push_back (act);
226
227         act = ActionManager::register_toggle_action (common_actions, X_("ToggleMixerList"), _("Toggle Mixer List"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_mixer_list));
228         ActionManager::session_sensitive_actions.push_back (act);
229
230         act = ActionManager::register_toggle_action (common_actions, X_("ToggleMonitorSection"), _("Toggle Monitor Section Visibility"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_monitor_section_visibility));
231         act->set_sensitive (false);
232
233 if (Profile->get_mixbus())
234         ActionManager::register_action (common_actions, X_("show-ui-prefs"), _("Show more UI preferences"), sigc::mem_fun (*this, &ARDOUR_UI::show_ui_prefs));
235
236         ActionManager::register_action (common_actions, X_("toggle-mixer"), S_("Window|Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_mixer_window));
237         ActionManager::register_action (common_actions, X_("toggle-editor-mixer"), _("Toggle Editor+Mixer"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_editor_mixer));
238         ActionManager::register_action (common_actions, X_("toggle-meterbridge"), S_("Window|Meterbridge"),  sigc::mem_fun(*this, &ARDOUR_UI::toggle_meterbridge));
239
240         ActionManager::register_action (common_actions, X_("reattach-all-tearoffs"), _("Reattach All Tearoffs"), sigc::mem_fun (*this, &ARDOUR_UI::reattach_all_tearoffs));
241
242         act = ActionManager::register_action (common_actions, X_("NewMIDITracer"), _("MIDI Tracer"), sigc::mem_fun(*this, &ARDOUR_UI::new_midi_tracer_window));
243         ActionManager::session_sensitive_actions.push_back (act);
244         ActionManager::register_action (common_actions, X_("chat"), _("Chat"),  sigc::mem_fun(*this, &ARDOUR_UI::launch_chat));
245         /** TRANSLATORS: This is `Manual' in the sense of an instruction book that tells a user how to use Ardour */
246         ActionManager::register_action (common_actions, X_("manual"), S_("Help|Manual"),  mem_fun(*this, &ARDOUR_UI::launch_manual));
247         ActionManager::register_action (common_actions, X_("reference"), S_("Manual|Reference"),  mem_fun(*this, &ARDOUR_UI::launch_reference));
248         ActionManager::register_action (common_actions, X_("tracker"), _("Report a Bug"), mem_fun(*this, &ARDOUR_UI::launch_tracker));
249         ActionManager::register_action (common_actions, X_("cheat-sheet"), _("Cheat Sheet"), mem_fun(*this, &ARDOUR_UI::launch_cheat_sheet));
250         ActionManager::register_action (common_actions, X_("website"), _("Ardour Website"), mem_fun(*this, &ARDOUR_UI::launch_website));
251         ActionManager::register_action (common_actions, X_("website-dev"), _("Ardour Development"), mem_fun(*this, &ARDOUR_UI::launch_website_dev));
252         ActionManager::register_action (common_actions, X_("forums"), _("User Forums"), mem_fun(*this, &ARDOUR_UI::launch_forums));
253         ActionManager::register_action (common_actions, X_("howto-report"), _("How to Report a Bug"), mem_fun(*this, &ARDOUR_UI::launch_howto_report));
254
255         act = ActionManager::register_action (common_actions, X_("Save"), _("Save"),  sigc::hide_return (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::save_state), string(""), false)));
256         ActionManager::session_sensitive_actions.push_back (act);
257         ActionManager::write_sensitive_actions.push_back (act);
258
259         Glib::RefPtr<ActionGroup> transport_actions = ActionGroup::create (X_("Transport"));
260
261         /* do-nothing action for the "transport" menu bar item */
262
263         ActionManager::register_action (transport_actions, X_("Transport"), _("Transport"));
264
265         /* these two are not used by key bindings, instead use ToggleRoll for that. these two do show up in
266            menus and via button proxies.
267         */
268
269         act = ActionManager::register_action (transport_actions, X_("Stop"), _("Stop"), sigc::mem_fun(*this, &ARDOUR_UI::transport_stop));
270         ActionManager::session_sensitive_actions.push_back (act);
271         ActionManager::transport_sensitive_actions.push_back (act);
272         act = ActionManager::register_action (transport_actions, X_("Roll"), _("Roll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_roll));
273         ActionManager::session_sensitive_actions.push_back (act);
274         ActionManager::transport_sensitive_actions.push_back (act);
275
276         act = ActionManager::register_action (transport_actions, X_("ToggleRoll"), _("Start/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, false));
277         ActionManager::session_sensitive_actions.push_back (act);
278         ActionManager::transport_sensitive_actions.push_back (act);
279         act = ActionManager::register_action (transport_actions, X_("alternate-ToggleRoll"), _("Start/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, false));
280         ActionManager::session_sensitive_actions.push_back (act);
281         ActionManager::transport_sensitive_actions.push_back (act);
282         act = ActionManager::register_action (transport_actions, X_("ToggleRollMaybe"), _("Start/Continue/Stop"), sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::toggle_roll), false, true));
283         ActionManager::session_sensitive_actions.push_back (act);
284         ActionManager::transport_sensitive_actions.push_back (act);
285         act = ActionManager::register_action (transport_actions, X_("ToggleRollForgetCapture"), _("Stop and Forget Capture"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::toggle_roll), true, false));
286         ActionManager::session_sensitive_actions.push_back (act);
287         ActionManager::transport_sensitive_actions.push_back (act);
288
289         /* these two behave as follows:
290
291            - if transport speed != 1.0 or != -1.0, change speed to 1.0 or -1.0 (respectively)
292            - otherwise do nothing
293         */
294
295         act = ActionManager::register_action (transport_actions, X_("TransitionToRoll"), _("Transition to Roll"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), true));
296         ActionManager::session_sensitive_actions.push_back (act);
297         ActionManager::transport_sensitive_actions.push_back (act);
298
299         act = ActionManager::register_action (transport_actions, X_("TransitionToReverse"), _("Transition to Reverse"), sigc::bind (sigc::mem_fun (*editor, &PublicEditor::transition_to_rolling), false));
300         ActionManager::session_sensitive_actions.push_back (act);
301         ActionManager::transport_sensitive_actions.push_back (act);
302
303         act = ActionManager::register_action (transport_actions, X_("Loop"), _("Play Loop Range"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_session_auto_loop));
304         ActionManager::session_sensitive_actions.push_back (act);
305         ActionManager::transport_sensitive_actions.push_back (act);
306         act = ActionManager::register_action (transport_actions, X_("PlaySelection"), _("Play Selection"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_selection));
307         ActionManager::session_sensitive_actions.push_back (act);
308         ActionManager::transport_sensitive_actions.push_back (act);
309         act = ActionManager::register_action (transport_actions, X_("PlayPreroll"), _("Play Selection w/Preroll"), sigc::mem_fun(*this, &ARDOUR_UI::transport_play_preroll));
310         ActionManager::session_sensitive_actions.push_back (act);
311         ActionManager::transport_sensitive_actions.push_back (act);
312
313         act = ActionManager::register_action (transport_actions, X_("Record"), _("Enable Record"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), false));
314         ActionManager::session_sensitive_actions.push_back (act);
315         ActionManager::write_sensitive_actions.push_back (act);
316         act = ActionManager::register_action (transport_actions, X_("record-roll"), _("Start Recording"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), true));
317         ActionManager::session_sensitive_actions.push_back (act);
318         ActionManager::write_sensitive_actions.push_back (act);
319         ActionManager::transport_sensitive_actions.push_back (act);
320         act = ActionManager::register_action (transport_actions, X_("alternate-record-roll"), _("Start Recording"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_record), true));
321         ActionManager::session_sensitive_actions.push_back (act);
322         ActionManager::write_sensitive_actions.push_back (act);
323         ActionManager::transport_sensitive_actions.push_back (act);
324         act = ActionManager::register_action (transport_actions, X_("Rewind"), _("Rewind"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 0));
325         ActionManager::session_sensitive_actions.push_back (act);
326         ActionManager::transport_sensitive_actions.push_back (act);
327         act = ActionManager::register_action (transport_actions, X_("RewindSlow"), _("Rewind (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), -1));
328         ActionManager::session_sensitive_actions.push_back (act);
329         ActionManager::transport_sensitive_actions.push_back (act);
330         act = ActionManager::register_action (transport_actions, X_("RewindFast"), _("Rewind (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_rewind), 1));
331         ActionManager::session_sensitive_actions.push_back (act);
332         ActionManager::transport_sensitive_actions.push_back (act);
333         act = ActionManager::register_action (transport_actions, X_("Forward"), _("Forward"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 0));
334         ActionManager::session_sensitive_actions.push_back (act);
335         ActionManager::transport_sensitive_actions.push_back (act);
336         act = ActionManager::register_action (transport_actions, X_("ForwardSlow"), _("Forward (Slow)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), -1));
337         ActionManager::session_sensitive_actions.push_back (act);
338         ActionManager::transport_sensitive_actions.push_back (act);
339         act = ActionManager::register_action (transport_actions, X_("ForwardFast"), _("Forward (Fast)"), sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::transport_forward), 1));
340         ActionManager::session_sensitive_actions.push_back (act);
341         ActionManager::transport_sensitive_actions.push_back (act);
342         act = ActionManager::register_action (transport_actions, X_("GotoZero"), _("Go to Zero"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_zero));
343         ActionManager::session_sensitive_actions.push_back (act);
344         ActionManager::transport_sensitive_actions.push_back (act);
345         act = ActionManager::register_action (transport_actions, X_("GotoStart"), _("Go to Start"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_start));
346         ActionManager::session_sensitive_actions.push_back (act);
347         ActionManager::transport_sensitive_actions.push_back (act);
348         act = ActionManager::register_action (transport_actions, X_("alternate-GotoStart"), _("Go to Start"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_start));
349         ActionManager::session_sensitive_actions.push_back (act);
350         ActionManager::transport_sensitive_actions.push_back (act);
351         act = ActionManager::register_action (transport_actions, X_("GotoEnd"), _("Go to End"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_end));
352         ActionManager::session_sensitive_actions.push_back (act);
353         ActionManager::transport_sensitive_actions.push_back (act);
354         act = ActionManager::register_action (transport_actions, X_("GotoWallClock"), _("Go to Wall Clock"), sigc::mem_fun(*this, &ARDOUR_UI::transport_goto_wallclock));
355         ActionManager::session_sensitive_actions.push_back (act);
356         ActionManager::transport_sensitive_actions.push_back (act);
357
358         //these actions handle the numpad events, ProTools style
359         act = ActionManager::register_action (transport_actions, X_("numpad-decimal"), _("Numpad Decimal"), mem_fun(*this, &ARDOUR_UI::transport_numpad_decimal));
360         ActionManager::session_sensitive_actions.push_back (act);
361         ActionManager::transport_sensitive_actions.push_back (act);
362         act = ActionManager::register_action (transport_actions, X_("alternate-numpad-decimal"), _("Numpad Decimal"), mem_fun(*this, &ARDOUR_UI::transport_numpad_decimal));
363         ActionManager::session_sensitive_actions.push_back (act);
364         ActionManager::transport_sensitive_actions.push_back (act);
365         act = ActionManager::register_action (transport_actions, X_("numpad-0"), _("Numpad 0"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 0));
366         ActionManager::session_sensitive_actions.push_back (act);
367         ActionManager::transport_sensitive_actions.push_back (act);
368         act = ActionManager::register_action (transport_actions, X_("numpad-1"), _("Numpad 1"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 1));
369         ActionManager::session_sensitive_actions.push_back (act);
370         ActionManager::transport_sensitive_actions.push_back (act);
371         act = ActionManager::register_action (transport_actions, X_("numpad-2"), _("Numpad 2"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 2));
372         ActionManager::session_sensitive_actions.push_back (act);
373         ActionManager::transport_sensitive_actions.push_back (act);
374         act = ActionManager::register_action (transport_actions, X_("numpad-3"), _("Numpad 3"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 3));
375         ActionManager::session_sensitive_actions.push_back (act);
376         ActionManager::transport_sensitive_actions.push_back (act);
377         act = ActionManager::register_action (transport_actions, X_("numpad-4"), _("Numpad 4"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 4));
378         ActionManager::session_sensitive_actions.push_back (act);
379         ActionManager::transport_sensitive_actions.push_back (act);
380         act = ActionManager::register_action (transport_actions, X_("numpad-5"), _("Numpad 5"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 5));
381         ActionManager::session_sensitive_actions.push_back (act);
382         ActionManager::transport_sensitive_actions.push_back (act);
383         act = ActionManager::register_action (transport_actions, X_("numpad-6"), _("Numpad 6"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 6));
384         ActionManager::session_sensitive_actions.push_back (act);
385         ActionManager::transport_sensitive_actions.push_back (act);
386         act = ActionManager::register_action (transport_actions, X_("numpad-7"), _("Numpad 7"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 7));
387         ActionManager::session_sensitive_actions.push_back (act);
388         ActionManager::transport_sensitive_actions.push_back (act);
389         act = ActionManager::register_action (transport_actions, X_("numpad-8"), _("Numpad 8"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 8));
390         ActionManager::session_sensitive_actions.push_back (act);
391         ActionManager::transport_sensitive_actions.push_back (act);
392         act = ActionManager::register_action (transport_actions, X_("numpad-9"), _("Numpad 9"), bind (mem_fun(*this, &ARDOUR_UI::transport_numpad_event), 9));
393         ActionManager::session_sensitive_actions.push_back (act);
394         ActionManager::transport_sensitive_actions.push_back (act);
395
396         act = ActionManager::register_action (transport_actions, X_("focus-on-clock"), _("Focus On Clock"), sigc::mem_fun(*this, &ARDOUR_UI::focus_on_clock));
397         ActionManager::session_sensitive_actions.push_back (act);
398         ActionManager::transport_sensitive_actions.push_back (act);
399
400         act = ActionManager::register_action (transport_actions, X_("primary-clock-timecode"), _("Timecode"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Timecode));
401         ActionManager::session_sensitive_actions.push_back (act);
402         act = ActionManager::register_action (transport_actions, X_("primary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::BBT));
403         ActionManager::session_sensitive_actions.push_back (act);
404         act = ActionManager::register_action (transport_actions, X_("primary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::MinSec));
405         ActionManager::session_sensitive_actions.push_back (act);
406         act = ActionManager::register_action (transport_actions, X_("primary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(primary_clock, &AudioClock::set_mode), AudioClock::Frames));
407         ActionManager::session_sensitive_actions.push_back (act);
408
409         act = ActionManager::register_action (transport_actions, X_("secondary-clock-timecode"), _("Timecode"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Timecode));
410         ActionManager::session_sensitive_actions.push_back (act);
411         act = ActionManager::register_action (transport_actions, X_("secondary-clock-bbt"), _("Bars & Beats"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::BBT));
412         ActionManager::session_sensitive_actions.push_back (act);
413         act = ActionManager::register_action (transport_actions, X_("secondary-clock-minsec"), _("Minutes & Seconds"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::MinSec));
414         ActionManager::session_sensitive_actions.push_back (act);
415         act = ActionManager::register_action (transport_actions, X_("secondary-clock-samples"), _("Samples"), sigc::bind (sigc::mem_fun(secondary_clock, &AudioClock::set_mode), AudioClock::Frames));
416         ActionManager::session_sensitive_actions.push_back (act);
417
418         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchIn"), _("Punch In"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_in));
419         act->set_short_label (_("In"));
420         ActionManager::session_sensitive_actions.push_back (act);
421         ActionManager::transport_sensitive_actions.push_back (act);
422         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunchOut"), _("Punch Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch_out));
423         act->set_short_label (_("Out"));
424         ActionManager::session_sensitive_actions.push_back (act);
425         ActionManager::transport_sensitive_actions.push_back (act);
426         act = ActionManager::register_toggle_action (transport_actions, X_("TogglePunch"), _("Punch In/Out"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_punch));
427         act->set_short_label (_("In/Out"));
428         ActionManager::session_sensitive_actions.push_back (act);
429         ActionManager::transport_sensitive_actions.push_back (act);
430         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleClick"), _("Click"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_click));
431         ActionManager::session_sensitive_actions.push_back (act);
432         ActionManager::transport_sensitive_actions.push_back (act);
433         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoInput"), _("Auto Input"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_input));
434         ActionManager::session_sensitive_actions.push_back (act);
435         ActionManager::transport_sensitive_actions.push_back (act);
436         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoPlay"), _("Auto Play"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_play));
437         ActionManager::session_sensitive_actions.push_back (act);
438         ActionManager::transport_sensitive_actions.push_back (act);
439         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleAutoReturn"), _("Auto Return"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_auto_return));
440         ActionManager::session_sensitive_actions.push_back (act);
441         ActionManager::transport_sensitive_actions.push_back (act);
442         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleFollowEdits"), _("Follow Edits"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_follow_edits));
443         ActionManager::session_sensitive_actions.push_back (act);
444         ActionManager::transport_sensitive_actions.push_back (act);
445
446
447         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleVideoSync"), _("Sync Startup to Video"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_video_sync));
448         ActionManager::session_sensitive_actions.push_back (act);
449         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleTimeMaster"), _("Time Master"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_time_master));
450         ActionManager::session_sensitive_actions.push_back (act);
451         act = ActionManager::register_toggle_action (transport_actions, X_("ToggleExternalSync"), _("Use External Positional Sync Source"), sigc::mem_fun(*this, &ARDOUR_UI::toggle_external_sync));
452         ActionManager::session_sensitive_actions.push_back (act);
453
454         for (int i = 1; i <= 32; ++i) {
455                 string const a = string_compose (X_("ToggleRecordEnableTrack%1"), i);
456                 string const n = string_compose (_("Toggle Record Enable Track %1"), i);
457                 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));
458                 ActionManager::session_sensitive_actions.push_back (act);
459         }
460
461         Glib::RefPtr<ActionGroup> shuttle_actions = ActionGroup::create ("ShuttleActions");
462
463         shuttle_actions->add (Action::create (X_("SetShuttleUnitsPercentage"), _("Percentage")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Percentage)));
464         shuttle_actions->add (Action::create (X_("SetShuttleUnitsSemitones"), _("Semitones")), hide_return (sigc::bind (sigc::mem_fun (*Config, &RCConfiguration::set_shuttle_units), Semitones)));
465
466         Glib::RefPtr<ActionGroup> option_actions = ActionGroup::create ("options");
467
468         act = ActionManager::register_toggle_action (option_actions, X_("SendMTC"), _("Send MTC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mtc));
469         ActionManager::session_sensitive_actions.push_back (act);
470         act = ActionManager::register_toggle_action (option_actions, X_("SendMMC"), _("Send MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_mmc));
471         ActionManager::session_sensitive_actions.push_back (act);
472         act = ActionManager::register_toggle_action (option_actions, X_("UseMMC"), _("Use MMC"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_use_mmc));
473         ActionManager::session_sensitive_actions.push_back (act);
474         act = ActionManager::register_toggle_action (option_actions, X_("SendMidiClock"), _("Send MIDI Clock"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_clock));
475         ActionManager::session_sensitive_actions.push_back (act);
476         act = ActionManager::register_toggle_action (option_actions, X_("SendMIDIfeedback"), _("Send MIDI Feedback"), sigc::mem_fun (*this, &ARDOUR_UI::toggle_send_midi_feedback));
477         ActionManager::session_sensitive_actions.push_back (act);
478
479         /* MIDI */
480
481         Glib::RefPtr<ActionGroup> midi_actions = ActionGroup::create (X_("MIDI"));
482         ActionManager::register_action (midi_actions, X_("panic"), _("Panic"), sigc::mem_fun(*this, &ARDOUR_UI::midi_panic));
483
484         ActionManager::add_action_group (shuttle_actions);
485         ActionManager::add_action_group (option_actions);
486         ActionManager::add_action_group (transport_actions);
487         ActionManager::add_action_group (main_actions);
488         ActionManager::add_action_group (main_menu_actions);
489         ActionManager::add_action_group (common_actions);
490         ActionManager::add_action_group (midi_actions);
491 }
492
493 void
494 ARDOUR_UI::build_menu_bar ()
495 {
496         menu_bar = dynamic_cast<MenuBar*> (ActionManager::get_widget (X_("/Main")));
497         menu_bar->set_name ("MainMenuBar");
498
499         /*
500          * This is needed because this property does not get installed
501          * until the Menu GObject class is registered, which happens
502          * when the first menu instance is created.
503          */
504         // XXX bug in gtkmm causes this to popup an error message
505         // Gtk::Settings::get_default()->property_gtk_can_change_accels() = true;
506         // so use this instead ...
507         gtk_settings_set_long_property (gtk_settings_get_default(), "gtk-can-change-accels", 1, "Ardour:designers");
508
509         EventBox* ev = manage (new EventBox);
510         ev->show ();
511         CairoHPacker* hbox = manage (new CairoHPacker);
512         hbox->set_name (X_("StatusBarBox"));
513         hbox->show ();
514         hbox->set_border_width (3);
515
516         VBox* vbox = manage (new VBox);
517         vbox->pack_start (*hbox, true, false);
518         vbox->show();
519
520         ev->add (*vbox);
521
522         wall_clock_label.set_name ("WallClock");
523         wall_clock_label.set_use_markup ();
524         disk_space_label.set_name ("WallClock");
525         disk_space_label.set_use_markup ();
526         timecode_format_label.set_name ("WallClock");
527         timecode_format_label.set_use_markup ();
528         cpu_load_label.set_name ("CPULoad");
529         cpu_load_label.set_use_markup ();
530         xrun_label.set_name ("XrunLabel");
531         xrun_label.set_use_markup ();
532         peak_thread_work_label.set_name ("PeakThreadWork");
533         peak_thread_work_label.set_use_markup ();
534         buffer_load_label.set_name ("BufferLoad");
535         buffer_load_label.set_use_markup ();
536         sample_rate_label.set_name ("SampleRate");
537         sample_rate_label.set_use_markup ();
538         format_label.set_name ("Format");
539         format_label.set_use_markup ();
540
541 #ifndef TOP_MENUBAR
542         menu_hbox.pack_start (*menu_bar, false, false);
543 #else
544         use_menubar_as_top_menubar ();
545 #endif
546
547         bool wall_clock = false;
548         bool disk_space = false;
549
550         if (!Profile->get_small_screen()) {
551 #ifndef GTKOSX
552                 // OSX provides its own wallclock, thank you very much
553                 wall_clock = true;
554 #endif
555                 disk_space = true;
556         }
557
558         hbox->pack_end (error_alert_button, false, false, 2);
559
560         hbox->pack_end (wall_clock_label, false, false, 2);
561         hbox->pack_end (disk_space_label, false, false, 4);
562         hbox->pack_end (xrun_label, false, false, 4);
563         hbox->pack_end (peak_thread_work_label, false, false, 4);
564         hbox->pack_end (cpu_load_label, false, false, 4);
565         hbox->pack_end (buffer_load_label, false, false, 4);
566         hbox->pack_end (sample_rate_label, false, false, 4);
567         hbox->pack_end (timecode_format_label, false, false, 4);
568         hbox->pack_end (format_label, false, false, 4);
569
570         menu_hbox.pack_end (*ev, false, false, 2);
571
572         menu_bar_base.set_name ("MainMenuBar");
573         menu_bar_base.add (menu_hbox);
574
575 #ifndef GTKOSX
576         _status_bar_visibility.add (&wall_clock_label,      X_("WallClock"), _("Wall Clock"), wall_clock);
577 #endif
578         _status_bar_visibility.add (&disk_space_label,      X_("Disk"),      _("Disk Space"), disk_space);
579         _status_bar_visibility.add (&cpu_load_label,        X_("DSP"),       _("DSP"), true);
580         _status_bar_visibility.add (&xrun_label,            X_("XRun"),      _("X-run"), false);
581         _status_bar_visibility.add (&peak_thread_work_label,X_("Peakfile"),  _("Active Peak-file Work"), false);
582         _status_bar_visibility.add (&buffer_load_label,     X_("Buffers"),   _("Buffers"), true);
583         _status_bar_visibility.add (&sample_rate_label,     X_("Audio"),     _("Audio"), true);
584         _status_bar_visibility.add (&timecode_format_label, X_("TCFormat"),  _("Timecode Format"), true);
585         _status_bar_visibility.add (&format_label,          X_("Format"),    _("File Format"), true);
586
587         ev->signal_button_press_event().connect (sigc::mem_fun (_status_bar_visibility, &VisibilityGroup::button_press_event));
588         ev->signal_button_release_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::xrun_button_release));
589 }
590
591 void
592 ARDOUR_UI::use_menubar_as_top_menubar ()
593 {
594         Gtk::Widget* widget;
595         Application* app = Application::instance ();
596
597         /* the addresses ("/ui/Main...") used below are based on the menu definitions in the menus file
598          */
599
600         /* Quit will be taken care of separately */
601
602         if ((widget = ActionManager::get_widget ("/ui/Main/Session/Quit"))) {
603                 widget->hide ();
604         }
605
606         /* Put items for About and Preferences into App menu (the
607          * ardour.menus.in file does not list them for OS X)
608          */
609
610         GtkApplicationMenuGroup* group = app->add_app_menu_group ();
611
612         if ((widget = ActionManager::get_widget ("/ui/Main/Session/toggle-about"))) {
613                 app->add_app_menu_item (group, dynamic_cast<MenuItem*>(widget));
614         }
615
616         if ((widget = ActionManager::get_widget ("/ui/Main/Session/toggle-rc-options-editor"))) {
617                 app->add_app_menu_item (group, dynamic_cast<MenuItem*>(widget));
618         }
619
620         app->set_menu_bar (*menu_bar);
621 }
622
623 void
624 ARDOUR_UI::save_ardour_state ()
625 {
626         if (!keyboard || !mixer || !editor || !meterbridge) {
627                 return;
628         }
629
630         /* XXX this is all a bit dubious. add_extra_xml() uses
631            a different lifetime model from add_instant_xml().
632         */
633
634         XMLNode* node = new XMLNode (keyboard->get_state());
635         Config->add_extra_xml (*node);
636         Config->add_extra_xml (get_transport_controllable_state());
637
638         XMLNode* window_node = new XMLNode (X_("UI"));
639         window_node->add_property (_status_bar_visibility.get_state_name().c_str(), _status_bar_visibility.get_state_value ());
640
641         /* Windows */
642
643         WM::Manager::instance().add_state (*window_node);
644
645         /* tearoffs */
646
647         XMLNode* tearoff_node = new XMLNode (X_("Tearoffs"));
648
649         if (transport_tearoff) {
650                 XMLNode* t = new XMLNode (X_("transport"));
651                 transport_tearoff->add_state (*t);
652                 tearoff_node->add_child_nocopy (*t);
653         }
654
655         if (mixer->monitor_section()) {
656                 XMLNode* t = new XMLNode (X_("monitor-section"));
657                 mixer->monitor_section()->tearoff().add_state (*t);
658                 tearoff_node->add_child_nocopy (*t);
659         }
660
661         if (editor->mouse_mode_tearoff()) {
662                 XMLNode* t = new XMLNode (X_("mouse-mode"));
663                 editor->mouse_mode_tearoff ()->add_state (*t);
664                 tearoff_node->add_child_nocopy (*t);
665         }
666
667         window_node->add_child_nocopy (*tearoff_node);
668
669         Config->add_extra_xml (*window_node);
670         Config->add_extra_xml (audio_midi_setup->get_state());
671
672         Config->save_state();
673
674         UIConfiguration::instance().save_state ();
675
676         XMLNode& enode (static_cast<Stateful*>(editor)->get_state());
677         XMLNode& mnode (mixer->get_state());
678         XMLNode& bnode (meterbridge->get_state());
679
680         if (_session) {
681                 _session->add_instant_xml (enode);
682                 _session->add_instant_xml (mnode);
683                 _session->add_instant_xml (bnode);
684                 if (location_ui) {
685                         _session->add_instant_xml (location_ui->ui().get_state ());
686                 }
687         } else {
688                 Config->add_instant_xml (enode);
689                 Config->add_instant_xml (mnode);
690                 if (location_ui) {
691                         Config->add_instant_xml (location_ui->ui().get_state ());
692                 }
693         }
694         delete &enode;
695
696         Keyboard::save_keybindings ();
697 }
698
699 void
700 ARDOUR_UI::resize_text_widgets ()
701 {
702         set_size_request_to_display_given_text (cpu_load_label, "DSP: 100.0%", 2, 2);
703         set_size_request_to_display_given_text (buffer_load_label, "Buffers: p:100% c:100%", 2, 2);
704         set_size_request_to_display_given_text (xrun_label, "X: 9999", 2, 2);
705 }
706
707 void
708 ARDOUR_UI::focus_on_clock ()
709 {
710         if (editor && primary_clock) {
711                 editor->present ();
712                 primary_clock->focus ();
713         }
714 }
715
716 bool
717 ARDOUR_UI::xrun_button_release (GdkEventButton* ev)
718 {
719         if (ev->button != 1 || !Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
720                 return false;
721         }
722
723         if (_session) {
724                 _session->reset_xrun_count ();
725                 update_xrun_count ();
726         }
727         return true;
728 }