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