bf9a88d29e8ff433d05475ca3349797a4259bc1a
[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 extern sigc::signal<void>  ColorsChanged;
106 extern sigc::signal<void>  DPIReset;
107
108 class ARDOUR_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
109 {
110   public:
111         ARDOUR_UI (int *argcp, char **argvp[]);
112         ~ARDOUR_UI();
113
114         bool run_startup (bool should_be_new, std::string load_template);
115
116         void show_splash ();
117         void hide_splash ();
118
119         void launch_chat ();
120         void launch_manual ();
121         void launch_reference ();
122         void show_about ();
123         void hide_about ();
124
125         void idle_load (const Glib::ustring& path);
126         void finish();
127
128         int load_session (const Glib::ustring& path, const Glib::ustring& snapshot, Glib::ustring mix_template = Glib::ustring());
129         bool session_loaded;
130         int build_session (const Glib::ustring& path, const Glib::ustring& snapshot, ARDOUR::BusProfile&);
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, std::string load_template = "");
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 = "", bool switch_to_it = false);
154         void save_state (const std::string & state_name = "", bool switch_to_it = false);
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 new_midi_tracer_window ();
171         void toggle_route_params_window ();
172         void toggle_editing_space();
173         void toggle_keep_tearoffs();
174
175         Gtk::Tooltips& tooltips() { return _tooltips; }
176
177         static sigc::signal<void,bool> Blink;
178         static sigc::signal<void>      RapidScreenUpdate;
179         static sigc::signal<void>      SuperRapidScreenUpdate;
180         static sigc::signal<void,nframes_t, bool, nframes_t> Clock;
181
182         XMLNode* editor_settings() const;
183         XMLNode* mixer_settings () const;
184         XMLNode* keyboard_settings () const;
185         XMLNode* tearoff_settings (const char*) const;
186
187         void save_ardour_state ();
188         gboolean configure_handler (GdkEventConfigure* conf);
189
190         void do_transport_locate (nframes_t position);
191         void halt_on_xrun_message ();
192         void xrun_handler (nframes_t);
193         void create_xrun_marker (nframes_t);
194
195         AudioClock primary_clock;
196         AudioClock secondary_clock;
197         AudioClock preroll_clock;
198         AudioClock postroll_clock;
199
200         void store_clock_modes ();
201         void restore_clock_modes ();
202         void reset_main_clocks ();
203
204         void add_route (Gtk::Window* float_window);
205
206         void session_add_audio_track (int input_channels, int32_t output_channels, ARDOUR::TrackMode mode, ARDOUR::RouteGroup* route_group, uint32_t how_many) {
207                 session_add_audio_route (true, false, input_channels, output_channels, mode, route_group, how_many);
208         }
209
210         void session_add_audio_bus (bool aux,  int input_channels, int32_t output_channels, ARDOUR::RouteGroup* route_group, uint32_t how_many) {
211                 session_add_audio_route (false, aux, input_channels, output_channels, ARDOUR::Normal, route_group, how_many);
212         }
213
214         void session_add_midi_track (ARDOUR::RouteGroup* route_group, uint32_t how_many) {
215                 session_add_midi_route (true, route_group, how_many);
216         }
217
218         /*void session_add_midi_bus () {
219                 session_add_midi_route (false);
220         }*/
221
222         int  create_engine ();
223         void post_engine ();
224
225         gint exit_on_main_window_close (GdkEventAny *);
226
227         void maximise_editing_space ();
228         void restore_editing_space ();
229
230         void setup_profile ();
231         void setup_theme ();
232         void setup_tooltips ();
233
234         void set_shuttle_fract (double);
235
236         void add_window_proxy (WindowProxyBase *);
237         void remove_window_proxy (WindowProxyBase *);
238         
239   protected:
240         friend class PublicEditor;
241
242         void toggle_clocking ();
243         void toggle_auto_play ();
244         void toggle_auto_input ();
245         void toggle_punch ();
246         void unset_dual_punch ();
247         bool ignore_dual_punch;
248         void toggle_punch_in ();
249         void toggle_punch_out ();
250         void show_loop_punch_ruler_and_disallow_hide ();
251         void reenable_hide_loop_punch_ruler_if_appropriate ();
252         void toggle_auto_return ();
253         void toggle_click ();
254
255         void toggle_session_auto_loop ();
256
257         void toggle_rc_options_window ();
258         void toggle_session_options_window ();
259
260   private:
261         ArdourStartup*      _startup;
262         ARDOUR::AudioEngine *engine;
263         Gtk::Tooltips        _tooltips;
264
265         void                goto_editor_window ();
266         void                goto_mixer_window ();
267         void                toggle_editor_mixer_on_top ();
268         bool                _mixer_on_top;
269
270         Gtk::ToggleButton   preroll_button;
271         Gtk::ToggleButton   postroll_button;
272
273         int  setup_windows ();
274         void setup_transport ();
275         void setup_clock ();
276
277         static ARDOUR_UI *theArdourUI;
278
279         void backend_audio_error (bool we_set_params, Gtk::Window* toplevel = 0);
280         void startup ();
281         void shutdown ();
282
283         int  ask_about_saving_session (const std::string & why);
284
285         /* periodic safety backup, to be precise */
286         gint autosave_session();
287         void update_autosave();
288         sigc::connection _autosave_connection;
289
290         void map_transport_state ();
291         int32_t do_engine_start ();
292
293         void engine_halted (const char* reason, bool free_reason);
294         void engine_stopped ();
295         void engine_running ();
296
297         void use_config ();
298
299         static gint _blink  (void *);
300         void blink ();
301         gint blink_timeout_tag;
302         bool blink_on;
303         void start_blinking ();
304         void stop_blinking ();
305
306         void about_signal_response(int response);
307
308   private:
309         Gtk::VBox     top_packer;
310
311         sigc::connection clock_signal_connection;
312         void         update_clocks ();
313         void         start_clocking ();
314         void         stop_clocking ();
315
316         void manage_window (Gtk::Window&);
317
318         AudioClock   big_clock;
319         ActionWindowProxy<Gtk::Window>* big_clock_window;
320         int original_big_clock_width;
321         int original_big_clock_height;
322         double original_big_clock_font_size;
323
324         void big_clock_size_allocate (Gtk::Allocation&);
325         bool idle_big_clock_text_resizer (int width, int height);
326         void big_clock_realized ();
327         bool big_clock_resize_in_progress;
328         int  big_clock_height;
329
330         void float_big_clock (Gtk::Window* parent);
331         bool main_window_state_event_handler (GdkEventWindowState*, bool window_was_editor);
332
333         void update_transport_clocks (nframes_t pos);
334         void record_state_changed ();
335
336         std::list<MidiTracer*> _midi_tracer_windows;
337
338         /* Transport Control */
339
340         void detach_tearoff (Gtk::Box* parent, Gtk::Widget* contents);
341         void reattach_tearoff (Gtk::Box* parent, Gtk::Widget* contents, int32_t order);
342
343         Gtkmm2ext::TearOff*      transport_tearoff;
344         Gtk::Frame               transport_frame;
345         Gtk::HBox                transport_tearoff_hbox;
346         Gtk::HBox                play_range_hbox;
347         Gtk::VBox                play_range_vbox;
348         Gtk::HBox                transport_hbox;
349         Gtk::Fixed               transport_base;
350         Gtk::Fixed               transport_button_base;
351         Gtk::Frame               transport_button_frame;
352         Gtk::HBox                transport_button_hbox;
353         Gtk::VBox                transport_button_vbox;
354         Gtk::HBox                transport_option_button_hbox;
355         Gtk::VBox                transport_option_button_vbox;
356         Gtk::HBox                transport_clock_hbox;
357         Gtk::VBox                transport_clock_vbox;
358         Gtk::HBox                primary_clock_hbox;
359         Gtk::HBox                secondary_clock_hbox;
360
361
362         struct TransportControllable : public PBD::Controllable {
363             enum ToggleType {
364                     Roll = 0,
365                     Stop,
366                     RecordEnable,
367                     GotoStart,
368                     GotoEnd,
369                     AutoLoop,
370                     PlaySelection,
371                     ShuttleControl
372
373             };
374
375             TransportControllable (std::string name, ARDOUR_UI&, ToggleType);
376             void set_value (double);
377             double get_value (void) const;
378
379             void set_id (const std::string&);
380
381             ARDOUR_UI& ui;
382             ToggleType type;
383         };
384
385         boost::shared_ptr<TransportControllable> roll_controllable;
386         boost::shared_ptr<TransportControllable> stop_controllable;
387         boost::shared_ptr<TransportControllable> goto_start_controllable;
388         boost::shared_ptr<TransportControllable> goto_end_controllable;
389         boost::shared_ptr<TransportControllable> auto_loop_controllable;
390         boost::shared_ptr<TransportControllable> play_selection_controllable;
391         boost::shared_ptr<TransportControllable> rec_controllable;
392         boost::shared_ptr<TransportControllable> shuttle_controllable;
393         BindingProxy shuttle_controller_binding_proxy;
394
395         void set_transport_controllable_state (const XMLNode&);
396         XMLNode& get_transport_controllable_state ();
397
398         BindableButton roll_button;
399         BindableButton stop_button;
400         BindableButton goto_start_button;
401         BindableButton goto_end_button;
402         BindableButton auto_loop_button;
403         BindableButton play_selection_button;
404         BindableButton rec_button;
405         Gtk::ToggleButton join_play_range_button;
406
407         void toggle_external_sync ();
408         void toggle_time_master ();
409         void toggle_video_sync ();
410
411         Gtk::DrawingArea  shuttle_box;
412         Gtk::EventBox     speed_display_box;
413         Gtk::Label        speed_display_label;
414         Gtk::Button       shuttle_units_button;
415         Gtk::ComboBoxText shuttle_style_button;
416         Gtk::Menu*        shuttle_unit_menu;
417         Gtk::Menu*        shuttle_style_menu;
418         float             shuttle_max_speed;
419         Gtk::Menu*        shuttle_context_menu;
420
421         void build_shuttle_context_menu ();
422         void show_shuttle_context_menu ();
423         void shuttle_style_changed();
424         void shuttle_unit_clicked ();
425         void set_shuttle_max_speed (float);
426         void update_speed_display ();
427         float last_speed_displayed;
428
429         gint shuttle_box_button_press (GdkEventButton*);
430         gint shuttle_box_button_release (GdkEventButton*);
431         gint shuttle_box_scroll (GdkEventScroll*);
432         gint shuttle_box_motion (GdkEventMotion*);
433         gint shuttle_box_expose (GdkEventExpose*);
434         gint mouse_shuttle (double x, bool force);
435         void use_shuttle_fract (bool force);
436
437         bool   shuttle_grabbed;
438         double shuttle_fract;
439
440         Gtkmm2ext::StatefulToggleButton punch_in_button;
441         Gtkmm2ext::StatefulToggleButton punch_out_button;
442         Gtkmm2ext::StatefulToggleButton auto_return_button;
443         Gtkmm2ext::StatefulToggleButton auto_play_button;
444         Gtkmm2ext::StatefulToggleButton auto_input_button;
445         Gtkmm2ext::StatefulToggleButton click_button;
446         Gtkmm2ext::StatefulToggleButton time_master_button;
447         Gtkmm2ext::StatefulToggleButton sync_button;
448
449         Gtk::ToggleButton auditioning_alert_button;
450         Gtk::ToggleButton solo_alert_button;
451
452         Gtk::VBox alert_box;
453
454         void solo_blink (bool);
455         void sync_blink (bool);
456         void audition_blink (bool);
457
458         void soloing_changed (bool);
459         void auditioning_changed (bool);
460         void _auditioning_changed (bool);
461
462         bool solo_alert_press (GdkEventButton* ev);
463         bool audition_alert_press (GdkEventButton* ev);
464
465         void big_clock_value_changed ();
466         void primary_clock_value_changed ();
467         void secondary_clock_value_changed ();
468
469         /* called by Blink signal */
470
471         void transport_rec_enable_blink (bool onoff);
472
473         Gtk::Menu*        session_popup_menu;
474
475         struct RecentSessionModelColumns : public Gtk::TreeModel::ColumnRecord {
476             RecentSessionModelColumns() {
477                     add (visible_name);
478                     add (fullpath);
479             }
480             Gtk::TreeModelColumn<Glib::ustring> visible_name;
481             Gtk::TreeModelColumn<Glib::ustring> fullpath;
482         };
483
484         RecentSessionModelColumns    recent_session_columns;
485         Gtk::TreeView                recent_session_display;
486         Glib::RefPtr<Gtk::TreeStore> recent_session_model;
487
488         ArdourDialog*     session_selector_window;
489         Gtk::FileChooserDialog* open_session_selector;
490
491         void build_session_selector();
492         void redisplay_recent_sessions();
493         void recent_session_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col);
494
495         struct RecentSessionsSorter {
496                 bool operator() (std::pair<std::string,std::string> a, std::pair<std::string,std::string> b) const {
497                     return cmp_nocase(a.first, b.first) == -1;
498             }
499         };
500
501         /* menu bar and associated stuff */
502
503         Gtk::MenuBar* menu_bar;
504         Gtk::EventBox menu_bar_base;
505         Gtk::HBox     menu_hbox;
506
507         void use_menubar_as_top_menubar ();
508         void build_menu_bar ();
509
510         Gtk::Label   wall_clock_label;
511         Gtk::EventBox wall_clock_box;
512         gint update_wall_clock ();
513
514         Gtk::Label   disk_space_label;
515         Gtk::EventBox disk_space_box;
516         void update_disk_space ();
517
518         Gtk::Label   cpu_load_label;
519         Gtk::EventBox cpu_load_box;
520         void update_cpu_load ();
521
522         Gtk::Label   buffer_load_label;
523         Gtk::EventBox buffer_load_box;
524         void update_buffer_load ();
525
526         Gtk::Label   sample_rate_label;
527         Gtk::EventBox sample_rate_box;
528         void update_sample_rate (nframes_t);
529
530         gint every_second ();
531         gint every_point_one_seconds ();
532         gint every_point_zero_one_seconds ();
533
534         sigc::connection second_connection;
535         sigc::connection point_one_second_connection;
536         sigc::connection point_oh_five_second_connection;
537         sigc::connection point_zero_one_second_connection;
538
539         gint session_menu (GdkEventButton *);
540
541         bool _will_create_new_session_automatically;
542
543         void open_session ();
544         void open_recent_session ();
545         void save_template ();
546
547         void edit_metadata ();
548         void import_metadata ();
549
550         void session_add_audio_route (bool disk, bool aux, int32_t input_channels, int32_t output_channels, ARDOUR::TrackMode mode, ARDOUR::RouteGroup *, uint32_t how_many);
551         void session_add_midi_route (bool disk, ARDOUR::RouteGroup *, uint32_t how_many);
552
553         void set_transport_sensitivity (bool);
554
555         void remove_last_capture ();
556
557         void transport_goto_zero ();
558         void transport_goto_start ();
559         void transport_goto_end ();
560         void transport_goto_wallclock ();
561         void transport_stop ();
562         void transport_stop_and_forget_capture ();
563         void transport_record (bool roll);
564         void transport_roll ();
565         void transport_play_selection();
566         void transport_forward (int option);
567         void transport_rewind (int option);
568         void transport_loop ();
569         void toggle_roll (bool with_abort, bool roll_out_of_bounded_mode);
570
571         bool _session_is_new;
572         void set_session (ARDOUR::Session *);
573         void connect_dependents_to_session (ARDOUR::Session *);
574         void we_have_dependents ();
575
576         void setup_session_options ();
577
578         guint32  last_key_press_time;
579
580         void snapshot_session (bool switch_to_it);
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 (nframes_t, nframes_t);
659
660         void disconnect_from_jack ();
661         void reconnect_to_jack ();
662         void set_jack_buffer_size (nframes_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
684         bool first_idle ();
685
686         void no_memory_warning ();
687         void check_memory_locking ();
688
689         bool check_audioengine();
690         void audioengine_setup ();
691
692         void display_message (const char *prefix, gint prefix_len,
693                         Glib::RefPtr<Gtk::TextBuffer::Tag> ptag, Glib::RefPtr<Gtk::TextBuffer::Tag> mtag,
694                         const char *msg);
695         Gtk::Label status_bar_label;
696         bool status_bar_button_press (GdkEventButton*);
697         Gtk::ToggleButton error_log_button;
698
699         void loading_message (const std::string& msg);
700         void end_loading_messages ();
701
702         void platform_specific ();
703         void platform_setup ();
704         void fontconfig_dialog ();
705         void toggle_translations ();
706
707         PBD::ScopedConnectionList forever_connections;
708
709         void step_edit_status_change (bool);
710
711         /* these are used only in response to a platform-specific "ShouldQuit" signal
712          */
713         bool idle_finish ();
714         void queue_finish ();
715
716         std::list<WindowProxyBase*> _window_proxies;
717 };
718
719 #endif /* __ardour_gui_h__ */
720