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