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