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