Merged with trunk R1736.
[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 /* need _BSD_SOURCE to get timersub macros */
24
25 #ifdef _BSD_SOURCE
26 #include <sys/time.h>
27 #else
28 #define _BSD_SOURCE
29 #include <sys/time.h>
30 #undef _BSD_SOURCE
31 #endif
32
33 #include <list>
34
35 #include <cmath>
36
37 #include <libgnomecanvasmm/canvas.h>
38
39 #include <pbd/xml++.h>
40 #include <gtkmm/box.h>
41 #include <gtkmm/frame.h>
42 #include <gtkmm/label.h>
43 #include <gtkmm/table.h>
44 #include <gtkmm/fixed.h>
45 #include <gtkmm/drawingarea.h>
46 #include <gtkmm/eventbox.h>
47 #include <gtkmm/menu.h>
48 #include <gtkmm/menuitem.h>
49 #include <gtkmm/button.h>
50 #include <gtkmm/togglebutton.h>
51 #include <gtkmm/treeview.h>
52 #include <gtkmm/menubar.h>
53 #include <gtkmm/adjustment.h>
54 #include <gtkmm2ext/gtk_ui.h>
55 #include <gtkmm2ext/click_box.h>
56 #include <gtkmm2ext/stateful_button.h>
57 #include <gtkmm2ext/bindable_button.h>
58 #include <ardour/ardour.h>
59 #include <ardour/session.h>
60
61 #include "audio_clock.h"
62 #include "ardour_dialog.h"
63 #include "editing.h"
64
65 class AudioClock;
66 class PublicEditor;
67 class Keyboard;
68 class OptionEditor;
69 class Mixer_UI;
70 class ConnectionEditor;
71 class RouteParams_UI;
72 class About;
73 class AddRouteDialog;
74 class NewSessionDialog;
75 class LocationUI;
76 class ColorManager;
77
78 namespace Gtkmm2ext {
79         class TearOff;
80 }
81
82 namespace ARDOUR {
83         class AudioEngine;
84         class Route;
85         class Port;
86         class IO;
87         class ControlProtocolInfo;
88 }
89
90 namespace ALSA {
91         class MultiChannelDevice;
92 }
93
94 #define FRAME_NAME "BaseFrame"
95
96 class ARDOUR_UI : public Gtkmm2ext::UI
97 {
98   public:
99         ARDOUR_UI (int *argcp, char **argvp[], string rcfile);
100         ~ARDOUR_UI();
101
102         void show ();
103         bool shown() { return shown_flag; }
104         
105         void show_splash ();
106         void hide_splash ();
107         
108         int load_session (const string & path, const string & snapshot, string* mix_template = 0);
109         bool session_loaded;
110         int build_session (const string & path, const string & snapshot, 
111                            uint32_t ctl_chns, 
112                            uint32_t master_chns,
113                            ARDOUR::AutoConnectOption input_connect,
114                            ARDOUR::AutoConnectOption output_connect,
115                            uint32_t nphysin,
116                            uint32_t nphysout,
117                            nframes_t initial_length);
118         bool session_is_new() const { return _session_is_new; }
119
120         ARDOUR::Session* the_session() { return session; }
121
122         bool will_create_new_session_automatically() const {
123                 return _will_create_new_session_automatically;
124         }
125
126         void set_will_create_new_session_automatically (bool yn) {
127                 _will_create_new_session_automatically = yn;
128         }
129
130         bool new_session(std::string path = string());
131         gint cmdline_new_session (string path);
132         int  unload_session ();
133         void close_session(); 
134
135         int  save_state_canfail (string state_name = "");
136         void save_state (const string & state_name = "");
137         void restore_state (string state_name = "");
138
139         static double gain_to_slider_position (ARDOUR::gain_t g);
140         static ARDOUR::gain_t slider_position_to_gain (double pos);
141
142         static ARDOUR_UI *instance () { return theArdourUI; }
143
144         PublicEditor&     the_editor(){return *editor;}
145         Mixer_UI* the_mixer() { return mixer; }
146         
147         void toggle_location_window ();
148         void toggle_color_manager ();
149         void toggle_big_clock_window ();
150         void toggle_connection_editor ();
151         void toggle_route_params_window ();
152         void toggle_editing_space();
153
154         Gtk::Tooltips& tooltips() { return _tooltips; }
155
156         static sigc::signal<void,bool> Blink;
157         static sigc::signal<void>      RapidScreenUpdate;
158         static sigc::signal<void>      SuperRapidScreenUpdate;
159         static sigc::signal<void,nframes_t> Clock;
160
161         /* this is a helper function to centralize the (complex) logic for
162            blinking rec-enable buttons.
163         */
164
165         void rec_enable_button_blink (bool onoff, ARDOUR::AudioDiskstream *, Gtk::Widget *w);
166
167         void name_io_setup (ARDOUR::AudioEngine&, string&, ARDOUR::IO& io, bool in);
168
169         static gint hide_and_quit (GdkEventAny *ev, ArdourDialog *);
170
171         XMLNode* editor_settings() const;
172         XMLNode* mixer_settings () const;
173         XMLNode* keyboard_settings () const;
174
175         void save_ardour_state ();
176         gboolean configure_handler (GdkEventConfigure* conf);
177
178         void do_transport_locate (nframes_t position);
179         void halt_on_xrun_message ();
180
181         AudioClock primary_clock;
182         AudioClock secondary_clock;
183         AudioClock preroll_clock;
184         AudioClock postroll_clock;
185
186         void store_clock_modes ();
187         void restore_clock_modes ();
188
189         void add_route (Gtk::Window* float_window);
190         
191         void session_add_audio_track (int input_channels, int32_t output_channels, ARDOUR::TrackMode mode, uint32_t how_many) {
192                 session_add_audio_route (true, input_channels, output_channels, mode, how_many);
193         }
194
195         void session_add_audio_bus (int input_channels, int32_t output_channels, uint32_t how_many) {
196                 session_add_audio_route (false, input_channels, output_channels, ARDOUR::Normal, how_many);
197         }
198
199         void session_add_midi_track (uint32_t how_many) {
200                 session_add_midi_route (true, how_many);
201         }
202
203         /*void session_add_midi_bus () {
204                 session_add_midi_route (false);
205         }*/
206
207         void set_engine (ARDOUR::AudioEngine&);
208         gint start_engine ();
209
210         gint exit_on_main_window_close (GdkEventAny *);
211
212         void maximise_editing_space ();
213         void restore_editing_space ();
214
215         void set_native_file_header_format (ARDOUR::HeaderFormat sf);
216         void set_native_file_data_format (ARDOUR::SampleFormat sf);
217
218         void set_keybindings_path (std::string path);
219         void save_keybindings ();
220
221         void setup_profile ();
222
223   protected:
224         friend class PublicEditor;
225
226         void toggle_clocking ();
227         void toggle_auto_play ();
228         void toggle_auto_input ();
229         void toggle_punch_in ();
230         void toggle_punch_out ();
231         void toggle_auto_return ();
232         void toggle_click ();
233
234         void toggle_session_auto_loop ();
235         
236         void toggle_options_window ();
237
238   private:
239         struct GlobalClickBox : public Gtk::VBox {
240             Gtkmm2ext::ClickBox  *box;
241             Gtk::Frame      frame;
242             Gtk::Label      label;
243             vector<string> &strings;
244             Gtk::Adjustment adjustment;
245
246             static void printer (char buf[32], Gtk::Adjustment &adj, void *arg);
247
248             GlobalClickBox (const string &str, vector<string> &vs)
249                     : strings (vs),
250                       adjustment (0, 0, vs.size() - 1, 1, 1, 0) {
251                     box = new Gtkmm2ext::ClickBox (&adjustment, "ClickButton");
252                     label.set_text (str);
253                     label.set_name ("GlobalButtonLabel");
254                     frame.add (*box);
255                     frame.set_shadow_type (Gtk::SHADOW_IN);
256                     pack_start (label);
257                     pack_start (frame);
258                     box->set_print_func (printer, this);
259                     box->set_wrap (true);
260             };
261         };
262
263         ARDOUR::AudioEngine                 *engine;
264         ARDOUR::Session                     *session;
265
266         Gtk::Tooltips          _tooltips;
267
268         void                     goto_editor_window ();
269         void                     goto_mixer_window ();
270         
271         Gtk::Table               adjuster_table;
272         Gtk::Frame               adjuster_frame;
273         Gtk::Fixed               adjuster_base;
274
275         GlobalClickBox     *online_control_button;
276         vector<string>      online_control_strings;
277
278         GlobalClickBox    *crossfade_time_button;
279         vector<string>     crossfade_time_strings;
280
281         Gtk::ToggleButton   preroll_button;
282         Gtk::ToggleButton   postroll_button;
283
284         Gtk::Table          transport_table;
285         Gtk::Table          option_table;
286
287         int  setup_windows ();
288         void setup_transport ();
289         void setup_clock ();
290
291         static ARDOUR_UI *theArdourUI;
292
293         void startup ();
294         void shutdown ();
295
296         void finish();
297         int  ask_about_saving_session (const string & why);
298         int  save_the_session;
299
300         /* periodic safety backup, to be precise */
301         gint autosave_session();
302         void update_autosave();
303         sigc::connection _autosave_connection;
304
305         void queue_transport_change ();
306         void map_transport_state ();
307         int32_t do_engine_start ();
308         
309         void engine_halted ();
310         void engine_stopped ();
311         void engine_running ();
312
313         void use_config ();
314
315         static gint _blink  (void *);
316         void blink ();
317         gint blink_timeout_tag;
318         bool blink_on;
319         void start_blinking ();
320         void stop_blinking ();
321
322         void about_signal_response(int response);
323
324   private:
325         Gtk::VBox     top_packer;
326
327         sigc::connection clock_signal_connection;
328         void         update_clocks ();
329         void         start_clocking ();
330         void         stop_clocking ();
331
332         void manage_window (Gtk::Window&);
333         
334         AudioClock   big_clock;
335         Gtk::Frame   big_clock_frame;
336         Gtk::Window* big_clock_window;
337
338         void update_transport_clocks (nframes_t pos);
339         void record_state_changed ();
340
341         /* Transport Control */
342
343         void detach_tearoff (Gtk::Box* parent, Gtk::Widget* contents);
344         void reattach_tearoff (Gtk::Box* parent, Gtk::Widget* contents, int32_t order);
345
346         Gtkmm2ext::TearOff*      transport_tearoff;
347         Gtk::Frame               transport_frame;
348         Gtk::HBox                transport_tearoff_hbox;
349         Gtk::HBox                transport_hbox;
350         Gtk::Fixed               transport_base;
351         Gtk::Fixed               transport_button_base;
352         Gtk::Frame               transport_button_frame;
353         Gtk::HBox                transport_button_hbox;
354         Gtk::VBox                transport_button_vbox;
355         Gtk::HBox                transport_option_button_hbox;
356         Gtk::VBox                transport_option_button_vbox;
357         Gtk::HBox                transport_clock_hbox;
358         Gtk::VBox                transport_clock_vbox;
359         Gtk::HBox                primary_clock_hbox;
360         Gtk::HBox                secondary_clock_hbox;
361
362
363         struct TransportControllable : public PBD::Controllable {
364             enum ToggleType {
365                     Roll = 0,
366                     Stop,
367                     RecordEnable,
368                     GotoStart,
369                     GotoEnd,
370                     AutoLoop,
371                     PlaySelection,
372                     ShuttleControl
373                     
374             };
375             
376             TransportControllable (std::string name, ARDOUR_UI&, ToggleType);
377             void set_value (float);
378             float get_value (void) const;
379             
380             void set_id (const std::string&);
381             
382             ARDOUR_UI& ui;
383             ToggleType type;
384         };
385
386         TransportControllable roll_controllable;
387         TransportControllable stop_controllable;
388         TransportControllable goto_start_controllable;
389         TransportControllable goto_end_controllable;
390         TransportControllable auto_loop_controllable;
391         TransportControllable play_selection_controllable;
392         TransportControllable rec_controllable;
393         TransportControllable shuttle_controllable;
394         BindingProxy shuttle_controller_binding_proxy;
395
396         void set_transport_controllable_state (const XMLNode&);
397         XMLNode& get_transport_controllable_state ();
398
399         BindableButton roll_button;
400         BindableButton stop_button;
401         BindableButton goto_start_button;
402         BindableButton goto_end_button;
403         BindableButton auto_loop_button;
404         BindableButton play_selection_button;
405         BindableButton rec_button;
406
407         Gtk::ComboBoxText sync_option_combo;
408
409         void sync_option_changed ();
410         void toggle_time_master ();
411         void toggle_video_sync ();
412
413         Gtk::DrawingArea  shuttle_box;
414         Gtk::EventBox     speed_display_box;
415         Gtk::Label        speed_display_label;
416         Gtk::Button       shuttle_units_button;
417         Gtk::ComboBoxText shuttle_style_button;
418         Gtk::Menu*        shuttle_unit_menu;
419         Gtk::Menu*        shuttle_style_menu;
420         float             shuttle_max_speed;
421         Gtk::Menu*        shuttle_context_menu;
422
423         void build_shuttle_context_menu ();
424         void show_shuttle_context_menu ();
425         void shuttle_style_changed();
426         void shuttle_unit_clicked ();
427         void set_shuttle_max_speed (float);
428         void update_speed_display ();
429         float last_speed_displayed;
430
431         gint shuttle_box_button_press (GdkEventButton*);
432         gint shuttle_box_button_release (GdkEventButton*);
433         gint shuttle_box_scroll (GdkEventScroll*);
434         gint shuttle_box_motion (GdkEventMotion*);
435         gint shuttle_box_expose (GdkEventExpose*);
436         gint mouse_shuttle (double x, bool force);
437         void use_shuttle_fract (bool force);
438         void set_shuttle_fract (double);
439
440         bool   shuttle_grabbed;
441         double shuttle_fract;
442
443         Gtkmm2ext::StatefulToggleButton punch_in_button;
444         Gtkmm2ext::StatefulToggleButton punch_out_button;
445         Gtkmm2ext::StatefulToggleButton auto_return_button;
446         Gtkmm2ext::StatefulToggleButton auto_play_button;
447         Gtkmm2ext::StatefulToggleButton auto_input_button;
448         Gtkmm2ext::StatefulToggleButton click_button;
449         Gtkmm2ext::StatefulToggleButton time_master_button;
450
451         Gtk::ToggleButton auditioning_alert_button;
452         Gtk::ToggleButton solo_alert_button;
453
454         Gtk::VBox alert_box;
455
456         void solo_blink (bool);
457         void audition_blink (bool);
458
459         void soloing_changed (bool);
460         void auditioning_changed (bool);
461         void _auditioning_changed (bool);
462
463         void solo_alert_toggle ();
464         void audition_alert_toggle ();
465
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<string,string> a, std::pair<string,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 build_menu_bar ();
508         void build_control_surface_menu ();
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         NewSessionDialog* new_session_dialog;
544         
545         void open_session ();
546         void open_recent_session ();
547         void save_template ();
548
549         void session_add_audio_route (bool disk, int32_t input_channels, int32_t output_channels, ARDOUR::TrackMode mode, uint32_t how_many);
550         void session_add_midi_route (bool disk, uint32_t how_many);
551
552         void set_transport_sensitivity (bool);
553
554         void remove_last_capture ();
555
556         void transport_goto_zero ();
557         void transport_goto_start ();
558         void transport_goto_end ();
559         void transport_stop ();
560         void transport_stop_and_forget_capture ();
561         void transport_record ();
562         void transport_roll ();
563         void transport_play_selection(); 
564         void transport_forward (int option);
565         void transport_rewind (int option);
566         void transport_loop ();
567
568         void transport_rolling ();
569         void transport_rewinding ();
570         void transport_forwarding ();
571         void transport_stopped ();
572
573         bool _session_is_new;
574         void connect_to_session (ARDOUR::Session *);
575         void connect_dependents_to_session (ARDOUR::Session *);
576         void we_have_dependents ();
577         
578         std::string keybindings_path;
579
580         void setup_keybindings ();
581         void setup_session_options ();
582         
583         guint32  last_key_press_time;
584
585         void snapshot_session ();
586
587         Mixer_UI   *mixer;
588         int         create_mixer ();
589         
590         PublicEditor     *editor;
591         int         create_editor ();
592
593         RouteParams_UI *route_params;
594         int             create_route_params ();
595
596         ConnectionEditor *connection_editor;
597         int               create_connection_editor ();
598
599         LocationUI *location_ui;
600         int         create_location_ui ();
601         void        handle_locations_change (ARDOUR::Location*);
602
603         ColorManager* color_manager;
604
605         /* Options window */
606         
607         OptionEditor *option_editor;
608         
609         /* route dialog */
610
611         AddRouteDialog *add_route_dialog;
612         
613         /* Keyboard Handling */
614         
615         Keyboard* keyboard;
616
617         /* Keymap handling */
618
619         void install_actions ();
620
621         void toggle_record_enable (uint32_t);
622
623         uint32_t rec_enabled_streams;
624         void count_recenabled_streams (ARDOUR::Route&);
625
626         About* about;
627         bool shown_flag;
628         /* cleanup */
629
630         Gtk::MenuItem *cleanup_item;
631
632         void display_cleanup_results (ARDOUR::Session::cleanup_report& rep, const gchar* list_title, const string & msg);
633         void cleanup ();
634         void flush_trash ();
635
636         bool have_configure_timeout;
637         struct timeval last_configure_time;
638         gint configure_timeout ();
639
640         struct timeval last_peak_grab;
641         struct timeval 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         int pending_state_dialog ();
649         
650         void disconnect_from_jack ();
651         void reconnect_to_jack ();
652         void set_jack_buffer_size (nframes_t);
653
654         Gtk::MenuItem* jack_disconnect_item;
655         Gtk::MenuItem* jack_reconnect_item;
656         Gtk::Menu*     jack_bufsize_menu;
657
658         int make_session_clean ();
659         bool filter_ardour_session_dirs (const Gtk::FileFilter::Info&);
660
661         Glib::RefPtr<Gtk::ActionGroup> common_actions;
662
663         void editor_realized ();
664
665         std::vector<std::string> positional_sync_strings;
666
667         void toggle_send_midi_feedback ();
668         void toggle_use_mmc ();
669         void toggle_send_mmc ();
670         void toggle_send_mtc ();
671
672         void toggle_use_osc ();
673
674         void set_input_auto_connect (ARDOUR::AutoConnectOption);
675         void set_output_auto_connect (ARDOUR::AutoConnectOption);
676         void set_solo_model (ARDOUR::SoloModel);
677         void set_monitor_model (ARDOUR::MonitorModel);
678         void set_remote_model (ARDOUR::RemoteModel);
679
680         void toggle_StopPluginsWithTransport();
681         void toggle_DoNotRunPluginsWhileRecording();
682         void toggle_VerifyRemoveLastCapture();
683         void toggle_PeriodicSafetyBackups();
684         void toggle_StopRecordingOnXrun();
685         void toggle_StopTransportAtEndOfSession();
686         void toggle_GainReduceFastTransport();
687         void toggle_LatchedSolo();
688         void toggle_ShowSoloMutes();
689         void toggle_LatchedRecordEnable ();
690         void toggle_RegionEquivalentsOverlap ();
691
692         void mtc_port_changed ();
693         void map_solo_model ();
694         void map_monitor_model ();
695         void map_remote_model ();
696         void map_file_header_format ();
697         void map_file_data_format ();
698         void map_input_auto_connect ();
699         void map_output_auto_connect ();
700         void parameter_changed (const char*);
701
702         void set_meter_hold (ARDOUR::MeterHold);
703         void set_meter_falloff (ARDOUR::MeterFalloff);
704         void map_meter_hold ();
705         void map_meter_falloff ();
706
707         void toggle_control_protocol (ARDOUR::ControlProtocolInfo*);
708         void toggle_control_protocol_feedback (ARDOUR::ControlProtocolInfo*, const char* group_name, std::string action_name);
709
710         bool can_save_keybindings;
711         bool first_idle ();
712
713         void no_memory_warning ();
714         void check_memory_locking ();
715 };
716
717 #endif /* __ardour_gui_h__ */
718