b3b86af4f3116e46644b6dbd9aab23154aab4e66
[ardour.git] / gtk2_ardour / ardour_ui.h
1 /*
2     Copyright (C) 1999-2002 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 #ifndef __ardour_gui_h__
21 #define __ardour_gui_h__
22
23 #include <time.h>
24
25 /* need _BSD_SOURCE to get timersub macros */
26
27 #ifdef _BSD_SOURCE
28 #include <sys/time.h>
29 #else
30 #define _BSD_SOURCE
31 #include <sys/time.h>
32 #undef _BSD_SOURCE
33 #endif
34
35 #include <list>
36 #include <cmath>
37
38
39 #include "pbd/xml++.h"
40 #include "pbd/controllable.h"
41 #include <gtkmm/box.h>
42 #include <gtkmm/frame.h>
43 #include <gtkmm/label.h>
44 #include <gtkmm/table.h>
45 #include <gtkmm/fixed.h>
46 #include <gtkmm/drawingarea.h>
47 #include <gtkmm/eventbox.h>
48 #include <gtkmm/menu.h>
49 #include <gtkmm/menuitem.h>
50 #include <gtkmm/button.h>
51 #include <gtkmm/togglebutton.h>
52 #include <gtkmm/treeview.h>
53 #include <gtkmm/menubar.h>
54 #include <gtkmm/textbuffer.h>
55 #include <gtkmm/adjustment.h>
56 #include <gtkmm2ext/gtk_ui.h>
57 #include <gtkmm2ext/click_box.h>
58 #include <gtkmm2ext/stateful_button.h>
59 #include <gtkmm2ext/bindable_button.h>
60
61 #include "ardour/ardour.h"
62 #include "ardour/types.h"
63 #include "ardour/utils.h"
64 #include "ardour/plugin.h"
65 #include "ardour/session_handle.h"
66
67 #include "video_timeline.h"
68
69 #include "ardour_button.h"
70 #include "ardour_dialog.h"
71 #include "ardour_window.h"
72 #include "editing.h"
73 #include "meterbridge.h"
74 #include "nsm.h"
75 #include "ui_config.h"
76 #include "enums.h"
77 #include "visibility_group.h"
78 #include "window_manager.h"
79
80 class About;
81 class AddRouteDialog;
82 class AddVideoDialog;
83 class VideoTimeLine;
84 class SystemExec;
85 class ArdourStartup;
86 class ArdourKeyboard;
87 class AudioClock;
88 class BigClockWindow;
89 class BundleManager;
90 class ButtonJoiner;
91 class ConnectionEditor;
92 class KeyEditor;
93 class LocationUIWindow;
94 class MainClock;
95 class Mixer_UI;
96 class PublicEditor;
97 class RCOptionEditor;
98 class RouteParams_UI;
99 class SessionOptionEditor;
100 class ShuttleControl;
101 class Splash;
102 class SpeakerDialog;
103 class ThemeManager;
104 class TimeInfoBox;
105 class MidiTracer;
106 class GlobalPortMatrixWindow;
107 class GUIObjectState;
108
109 namespace Gtkmm2ext {
110         class TearOff;
111 }
112
113 namespace ARDOUR {
114         class ControlProtocolInfo;
115         class IO;
116         class Port;
117         class Route;
118         class RouteGroup;
119         class Location;
120         class ProcessThread;
121 }
122
123 class ARDOUR_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
124 {
125   public:
126         ARDOUR_UI (int *argcp, char **argvp[], const char* localedir);
127         ~ARDOUR_UI();
128
129         bool run_startup (bool should_be_new, std::string load_template);
130
131         void show_splash ();
132         void hide_splash ();
133
134         void launch_chat ();
135         void launch_manual ();
136         void launch_reference ();
137         void show_about ();
138         void hide_about ();
139
140         void idle_load (const std::string& path);
141         void finish();
142
143         int load_session (const std::string& path, const std::string& snapshot, std::string mix_template = std::string());
144         bool session_loaded;
145         int build_session (const std::string& path, const std::string& snapshot, ARDOUR::BusProfile&);
146         bool session_is_new() const { return _session_is_new; }
147
148         ARDOUR::Session* the_session() { return _session; }
149
150         bool get_smart_mode () const;
151         
152         int get_session_parameters (bool quit_on_cancel, bool should_be_new = false, std::string load_template = "");
153         int  build_session_from_nsd (const std::string& session_name, const std::string& session_path);
154         bool ask_about_loading_existing_session (const std::string& session_path);
155
156         /// @return true if session was successfully unloaded.
157         int unload_session (bool hide_stuff = false);
158         void close_session();
159
160         int  save_state_canfail (std::string state_name = "", bool switch_to_it = false);
161         void save_state (const std::string & state_name = "", bool switch_to_it = false);
162
163         static ARDOUR_UI *instance () { return theArdourUI; }
164         static UIConfiguration *config () { return ui_config; }
165
166         PublicEditor&     the_editor(){return *editor;}
167         Mixer_UI* the_mixer() { return mixer; }
168
169         void new_midi_tracer_window ();
170         void toggle_editing_space();
171         void toggle_keep_tearoffs();
172
173         Gtk::Tooltips& tooltips() { return _tooltips; }
174
175         Gtk::HBox& editor_transport_box() { return _editor_transport_box; }
176
177         static PublicEditor* _instance;
178         static sigc::signal<void,bool> Blink;
179         static sigc::signal<void>      RapidScreenUpdate;
180         static sigc::signal<void>      SuperRapidScreenUpdate;
181         /** Emitted frequently with the audible frame, false, and the edit point as
182          *  parameters respectively.
183          */
184         static sigc::signal<void, framepos_t, bool, framepos_t> Clock;
185
186         static void close_all_dialogs () { CloseAllDialogs(); }
187         static sigc::signal<void> CloseAllDialogs;
188
189         XMLNode* editor_settings() const;
190         XMLNode* mixer_settings () const;
191         XMLNode* keyboard_settings () const;
192         XMLNode* tearoff_settings (const char*) const;
193
194         void save_ardour_state ();
195         gboolean configure_handler (GdkEventConfigure* conf);
196
197         void halt_on_xrun_message ();
198         void xrun_handler (framepos_t);
199         void create_xrun_marker (framepos_t);
200
201         GUIObjectState* gui_object_state;
202
203         MainClock* primary_clock;
204         MainClock* secondary_clock;
205         void focus_on_clock ();
206         AudioClock*   big_clock;
207
208         TimeInfoBox* time_info_box;
209
210         VideoTimeLine *video_timeline;
211
212         void store_clock_modes ();
213         void restore_clock_modes ();
214         void reset_main_clocks ();
215
216         void synchronize_sync_source_and_video_pullup ();
217
218         void add_route (Gtk::Window* float_window);
219         void add_routes_part_two ();
220         void add_routes_thread ();
221
222         void add_video (Gtk::Window* float_window);
223         void remove_video ();
224         void start_video_server_menu (Gtk::Window* float_window);
225         bool start_video_server (Gtk::Window* float_window, bool popup_msg);
226         void stop_video_server (bool ask_confirm=false);
227         void flush_videotimeline_cache (bool localcacheonly=false);
228
229         void session_add_audio_track (
230                 int input_channels,
231                 int32_t output_channels,
232                 ARDOUR::TrackMode mode,
233                 ARDOUR::RouteGroup* route_group,
234                 uint32_t how_many,
235                 std::string const & name_template
236                 ) {
237
238                 session_add_audio_route (true, input_channels, output_channels, mode, route_group, how_many, name_template);
239         }
240
241         void session_add_audio_bus (int input_channels, int32_t output_channels, ARDOUR::RouteGroup* route_group,
242                                     uint32_t how_many, std::string const & name_template) {
243                 session_add_audio_route (false, input_channels, output_channels, ARDOUR::Normal, route_group, how_many, name_template);
244         }
245
246         void session_add_midi_track (ARDOUR::RouteGroup* route_group, uint32_t how_many, std::string const & name_template,
247                                      ARDOUR::PluginInfoPtr instrument) {
248                 session_add_midi_route (true, route_group, how_many, name_template, instrument);
249         }
250
251         void session_add_mixed_track (const ARDOUR::ChanCount& input, const ARDOUR::ChanCount& output, ARDOUR::RouteGroup* route_group, uint32_t how_many, std::string const & name_template,
252                                       ARDOUR::PluginInfoPtr instrument);
253
254         /*void session_add_midi_bus () {
255                 session_add_midi_route (false);
256         }*/
257
258         int  create_engine ();
259         void post_engine ();
260
261         gint exit_on_main_window_close (GdkEventAny *);
262
263         void maximise_editing_space ();
264         void restore_editing_space ();
265
266         void update_tearoff_visibility ();
267
268         void setup_profile ();
269         void setup_tooltips ();
270
271         void set_shuttle_fract (double);
272
273         void get_process_buffers ();
274         void drop_process_buffers ();
275
276         const std::string& announce_string() const { return _announce_string; }
277
278   protected:
279         friend class PublicEditor;
280
281         void toggle_auto_play ();
282         void toggle_auto_input ();
283         void toggle_punch ();
284         void unset_dual_punch ();
285         bool ignore_dual_punch;
286         void toggle_punch_in ();
287         void toggle_punch_out ();
288         void show_loop_punch_ruler_and_disallow_hide ();
289         void reenable_hide_loop_punch_ruler_if_appropriate ();
290         void toggle_auto_return ();
291         void toggle_click ();
292
293         void toggle_session_auto_loop ();
294
295         void toggle_rc_options_window ();
296         void toggle_session_options_window ();
297
298   private:
299         ArdourStartup*      _startup;
300         ARDOUR::AudioEngine *engine;
301         Gtk::Tooltips        _tooltips;
302         NSM_Client          *nsm;
303         bool                 _was_dirty;
304         bool                 _mixer_on_top;
305
306         void goto_editor_window ();
307         void goto_mixer_window ();
308         void toggle_mixer_window ();
309         void toggle_meterbridge ();
310         void toggle_editor_mixer ();
311
312         int  setup_windows ();
313         void setup_transport ();
314         void setup_clock ();
315
316         static ARDOUR_UI *theArdourUI;
317
318         void startup ();
319         void shutdown ();
320
321         int  ask_about_saving_session (const std::vector<std::string>& actions);
322
323         /* periodic safety backup, to be precise */
324         gint autosave_session();
325         void update_autosave();
326         sigc::connection _autosave_connection;
327
328         void map_transport_state ();
329         int32_t do_engine_start ();
330
331         void engine_halted (const char* reason, bool free_reason);
332         void engine_stopped ();
333         void engine_running ();
334
335         void use_config ();
336
337         static gint _blink  (void *);
338         void blink ();
339         gint blink_timeout_tag;
340         bool blink_on;
341         void start_blinking ();
342         void stop_blinking ();
343
344         void about_signal_response(int response);
345
346         Gtk::VBox     top_packer;
347
348         sigc::connection clock_signal_connection;
349         void         update_clocks ();
350         void         start_clocking ();
351         void         stop_clocking ();
352
353         bool main_window_state_event_handler (GdkEventWindowState*, bool window_was_editor);
354
355         void update_transport_clocks (framepos_t pos);
356         void record_state_changed ();
357
358         std::list<MidiTracer*> _midi_tracer_windows;
359
360         /* Transport Control */
361
362         void detach_tearoff (Gtk::Box* parent, Gtk::Widget* contents);
363         void reattach_tearoff (Gtk::Box* parent, Gtk::Widget* contents, int32_t order);
364
365         Gtkmm2ext::TearOff*      transport_tearoff;
366         Gtk::Frame               transport_frame;
367         Gtk::HBox                transport_tearoff_hbox;
368         Gtk::HBox               _editor_transport_box;
369         Gtk::HBox                transport_hbox;
370         Gtk::Fixed               transport_base;
371         Gtk::Fixed               transport_button_base;
372         Gtk::Frame               transport_button_frame;
373         Gtk::HBox                transport_button_hbox;
374         Gtk::VBox                transport_button_vbox;
375         Gtk::HBox                transport_option_button_hbox;
376         Gtk::VBox                transport_option_button_vbox;
377         Gtk::HBox                transport_clock_hbox;
378         Gtk::VBox                transport_clock_vbox;
379         Gtk::HBox                primary_clock_hbox;
380         Gtk::HBox                secondary_clock_hbox;
381
382         struct TransportControllable : public PBD::Controllable {
383             enum ToggleType {
384                     Roll = 0,
385                     Stop,
386                     RecordEnable,
387                     GotoStart,
388                     GotoEnd,
389                     AutoLoop,
390                     PlaySelection,
391             };
392
393             TransportControllable (std::string name, ARDOUR_UI&, ToggleType);
394             void set_value (double);
395             double get_value (void) const;
396
397             ARDOUR_UI& ui;
398             ToggleType type;
399         };
400
401         boost::shared_ptr<TransportControllable> roll_controllable;
402         boost::shared_ptr<TransportControllable> stop_controllable;
403         boost::shared_ptr<TransportControllable> goto_start_controllable;
404         boost::shared_ptr<TransportControllable> goto_end_controllable;
405         boost::shared_ptr<TransportControllable> auto_loop_controllable;
406         boost::shared_ptr<TransportControllable> play_selection_controllable;
407         boost::shared_ptr<TransportControllable> rec_controllable;
408
409         void toggle_always_play_range ();
410
411         void set_transport_controllable_state (const XMLNode&);
412         XMLNode& get_transport_controllable_state ();
413
414         ArdourButton roll_button;
415         ArdourButton stop_button;
416         ArdourButton goto_start_button;
417         ArdourButton goto_end_button;
418         ArdourButton auto_loop_button;
419         ArdourButton play_selection_button;
420         ArdourButton rec_button;
421
422         void toggle_external_sync ();
423         void toggle_time_master ();
424         void toggle_video_sync ();
425
426         ShuttleControl* shuttle_box;
427
428         ArdourButton auto_return_button;
429         ArdourButton follow_edits_button;
430         ArdourButton auto_input_button;
431         ArdourButton click_button;
432         ArdourButton sync_button;
433
434         ArdourButton auditioning_alert_button;
435         ArdourButton solo_alert_button;
436         ArdourButton feedback_alert_button;
437
438         Gtk::VBox alert_box;
439
440         void solo_blink (bool);
441         void sync_blink (bool);
442         void audition_blink (bool);
443         void feedback_blink (bool);
444
445         void soloing_changed (bool);
446         void auditioning_changed (bool);
447         void _auditioning_changed (bool);
448         
449         bool solo_alert_press (GdkEventButton* ev);
450         bool audition_alert_press (GdkEventButton* ev);
451         bool feedback_alert_press (GdkEventButton *);
452
453         void big_clock_value_changed ();
454         void primary_clock_value_changed ();
455         void secondary_clock_value_changed ();
456
457         /* called by Blink signal */
458
459         void transport_rec_enable_blink (bool onoff);
460
461         Gtk::Menu*        session_popup_menu;
462
463         struct RecentSessionModelColumns : public Gtk::TreeModel::ColumnRecord {
464             RecentSessionModelColumns() {
465                     add (visible_name);
466                     add (tip);
467                     add (fullpath);
468             }
469             Gtk::TreeModelColumn<std::string> visible_name;
470             Gtk::TreeModelColumn<std::string> tip;
471             Gtk::TreeModelColumn<std::string> fullpath;
472         };
473
474         RecentSessionModelColumns    recent_session_columns;
475         Gtk::TreeView                recent_session_display;
476         Glib::RefPtr<Gtk::TreeStore> recent_session_model;
477
478         ArdourDialog*     session_selector_window;
479         Gtk::FileChooserDialog* open_session_selector;
480
481         void build_session_selector();
482         void redisplay_recent_sessions();
483         void recent_session_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col);
484
485         struct RecentSessionsSorter {
486                 bool operator() (std::pair<std::string,std::string> a, std::pair<std::string,std::string> b) const {
487                     return cmp_nocase(a.first, b.first) == -1;
488             }
489         };
490
491         /* menu bar and associated stuff */
492
493         Gtk::MenuBar* menu_bar;
494         Gtk::EventBox menu_bar_base;
495         Gtk::HBox     menu_hbox;
496
497         void use_menubar_as_top_menubar ();
498         void build_menu_bar ();
499
500         Gtk::Label   wall_clock_label;
501         gint update_wall_clock ();
502
503         Gtk::Label   disk_space_label;
504         void update_disk_space ();
505
506         Gtk::Label   timecode_format_label;
507         void update_timecode_format ();
508
509         Gtk::Label   cpu_load_label;
510         void update_cpu_load ();
511
512         Gtk::Label   buffer_load_label;
513         void update_buffer_load ();
514
515         Gtk::Label   sample_rate_label;
516         void update_sample_rate (ARDOUR::framecnt_t);
517
518         Gtk::Label    format_label;
519         void update_format ();
520         
521         gint every_second ();
522         gint every_point_one_seconds ();
523         gint every_point_zero_one_seconds ();
524
525         sigc::connection second_connection;
526         sigc::connection point_one_second_connection;
527         sigc::connection point_oh_five_second_connection;
528         sigc::connection point_zero_one_second_connection;
529
530         void open_session ();
531         void open_recent_session ();
532         void save_template ();
533
534         void edit_metadata ();
535         void import_metadata ();
536
537         void session_add_audio_route (bool, int32_t, int32_t, ARDOUR::TrackMode, ARDOUR::RouteGroup *, uint32_t, std::string const &);
538         void session_add_midi_route (bool, ARDOUR::RouteGroup *, uint32_t, std::string const &, ARDOUR::PluginInfoPtr);
539
540         void set_transport_sensitivity (bool);
541
542         void transport_goto_zero ();
543         void transport_goto_start ();
544         void transport_goto_end ();
545         void transport_goto_wallclock ();
546         void transport_stop ();
547         void transport_record (bool roll);
548         void transport_roll ();
549         void transport_play_selection();
550         void transport_play_preroll(); 
551         void transport_forward (int option);
552         void transport_rewind (int option);
553         void transport_loop ();
554         void toggle_roll (bool with_abort, bool roll_out_of_bounded_mode);
555
556         bool _session_is_new;
557         void set_session (ARDOUR::Session *);
558         void connect_dependents_to_session (ARDOUR::Session *);
559         void we_have_dependents ();
560
561         void setup_session_options ();
562
563         guint32  last_key_press_time;
564
565         void snapshot_session (bool switch_to_it);
566         void rename_session ();
567
568         Mixer_UI   *mixer;
569         int         create_mixer ();
570
571         PublicEditor     *editor;
572         int         create_editor ();
573
574         Meterbridge  *meterbridge;
575         int         create_meterbridge ();
576         /* Dialogs that can be created via new<T> */
577
578         WM::Proxy<SpeakerDialog> speaker_config_window;
579         WM::Proxy<ThemeManager> theme_manager;
580         WM::Proxy<KeyEditor> key_editor;
581         WM::Proxy<RCOptionEditor> rc_option_editor;
582         WM::Proxy<AddRouteDialog> add_route_dialog;
583         WM::Proxy<About> about;
584         WM::Proxy<LocationUIWindow> location_ui;
585         WM::Proxy<RouteParams_UI> route_params;
586
587         /* Windows/Dialogs that require a creator method */
588
589         WM::ProxyWithConstructor<SessionOptionEditor> session_option_editor;
590         WM::ProxyWithConstructor<AddVideoDialog> add_video_dialog;
591         WM::ProxyWithConstructor<BundleManager> bundle_manager;
592         WM::ProxyWithConstructor<BigClockWindow> big_clock_window;
593         WM::ProxyWithConstructor<GlobalPortMatrixWindow> audio_port_matrix;
594         WM::ProxyWithConstructor<GlobalPortMatrixWindow> midi_port_matrix;
595
596         /* creator methods */
597
598         SessionOptionEditor*    create_session_option_editor ();
599         BundleManager*          create_bundle_manager ();
600         AddVideoDialog*         create_add_video_dialog ();
601         BigClockWindow*         create_big_clock_window(); 
602         GlobalPortMatrixWindow* create_global_port_matrix (ARDOUR::DataType);
603
604         static UIConfiguration *ui_config;
605
606         SystemExec *video_server_process;
607
608         void handle_locations_change (ARDOUR::Location*);
609
610         /* Keyboard Handling */
611
612         ArdourKeyboard* keyboard;
613
614         /* Keymap handling */
615
616         void install_actions ();
617
618         void toggle_record_enable (uint32_t);
619
620         uint32_t rec_enabled_streams;
621         void count_recenabled_streams (ARDOUR::Route&);
622
623         Splash* splash;
624
625         void pop_back_splash (Gtk::Window&);
626
627         /* cleanup */
628
629         Gtk::MenuItem *cleanup_item;
630
631         void display_cleanup_results (ARDOUR::CleanupReport& rep, const gchar* list_title, const bool msg_delete);
632         void cleanup ();
633         void flush_trash ();
634
635         bool have_configure_timeout;
636         ARDOUR::microseconds_t last_configure_time;
637         gint configure_timeout ();
638
639         ARDOUR::microseconds_t last_peak_grab;
640         ARDOUR::microseconds_t last_shuttle_request;
641
642         bool have_disk_speed_dialog_displayed;
643         void disk_speed_dialog_gone (int ignored_response, Gtk::MessageDialog*);
644         void disk_overrun_handler ();
645         void disk_underrun_handler ();
646
647         void session_format_mismatch (std::string, std::string);
648
649         void session_dialog (std::string);
650         int pending_state_dialog ();
651         int sr_mismatch_dialog (ARDOUR::framecnt_t, ARDOUR::framecnt_t);
652
653         void disconnect_from_jack ();
654         void reconnect_to_jack ();
655         void set_jack_buffer_size (ARDOUR::pframes_t);
656
657         Gtk::MenuItem* jack_disconnect_item;
658         Gtk::MenuItem* jack_reconnect_item;
659         Gtk::Menu*     jack_bufsize_menu;
660
661         Glib::RefPtr<Gtk::ActionGroup> common_actions;
662
663         void editor_realized ();
664
665         std::vector<std::string> positional_sync_strings;
666
667         void toggle_send_midi_feedback ();
668         void toggle_use_mmc ();
669         void toggle_send_mmc ();
670         void toggle_send_mtc ();
671         void toggle_send_midi_clock ();
672
673         void toggle_use_osc ();
674
675         void parameter_changed (std::string);
676         void session_parameter_changed (std::string);
677
678         bool first_idle ();
679
680         void no_memory_warning ();
681         void check_memory_locking ();
682
683         bool check_audioengine();
684         void audioengine_setup ();
685
686         void display_message (const char *prefix, gint prefix_len,
687                         Glib::RefPtr<Gtk::TextBuffer::Tag> ptag, Glib::RefPtr<Gtk::TextBuffer::Tag> mtag,
688                         const char *msg);
689         Gtk::Label status_bar_label;
690         bool status_bar_button_press (GdkEventButton*);
691         Gtk::ToggleButton error_log_button;
692
693         void loading_message (const std::string& msg);
694
695         PBD::ScopedConnectionList forever_connections;
696
697         void step_edit_status_change (bool);
698
699         void platform_specific ();
700         void platform_setup ();
701
702         /* these are used only in response to a platform-specific "ShouldQuit" signal
703          */
704         bool idle_finish ();
705         void queue_finish ();
706         void fontconfig_dialog ();
707
708         int missing_file (ARDOUR::Session*s, std::string str, ARDOUR::DataType type);
709         int ambiguous_file (std::string file, std::string path, std::vector<std::string> hits);
710
711         bool click_button_clicked (GdkEventButton *);
712
713         VisibilityGroup _status_bar_visibility;
714
715         /** A ProcessThread so that we have some thread-local buffers for use by
716          *  PluginEqGui::impulse_analysis ().
717          */
718         ARDOUR::ProcessThread* _process_thread;
719
720         void feedback_detected ();
721
722         ArdourButton             midi_panic_button;
723         void                     midi_panic ();
724
725         void successful_graph_sort ();
726         bool _feedback_exists;
727
728         void resize_text_widgets ();
729
730         std::string _announce_string;
731         void check_announcements ();
732 };
733
734 #endif /* __ardour_gui_h__ */
735