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