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