Start of sfdb_v3.
[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     $Id$
19 */
20
21 #ifndef __ardour_gui_h__
22 #define __ardour_gui_h__
23
24 /* need _BSD_SOURCE to get timersub macros */
25
26 #ifdef _BSD_SOURCE
27 #include <sys/time.h>
28 #else
29 #define _BSD_SOURCE
30 #include <sys/time.h>
31 #undef _BSD_SOURCE
32 #endif
33
34 #include <list>
35
36 #include <cmath>
37
38 #include <libgnomecanvasmm/canvas.h>
39
40 #include <pbd/xml++.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/adjustment.h>
55 #include <gtkmm2ext/gtk_ui.h>
56 #include <gtkmm2ext/pix.h>
57 #include <gtkmm2ext/click_box.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 MeterBridge;
69 class OptionEditor;
70 class Mixer_UI;
71 class ConnectionEditor;
72 class RouteParams_UI;
73 class SoundFileChooser;
74 class About;
75 class AddRouteDialog;
76 class NewSessionDialog;
77 class LocationUI;
78
79 namespace Gtkmm2ext {
80         class TearOff;
81 };
82
83 namespace ARDOUR {
84         class AudioEngine;
85         class Route;
86         class Port;
87         class IO;
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 (string path, string snapshot, string* mix_template = 0);
109         bool session_loaded;
110         int build_session (string path, string snapshot, 
111                            uint32_t ctl_chns, 
112                            uint32_t master_chns,
113                            ARDOUR::Session::AutoConnectOption input_connect,
114                            ARDOUR::Session::AutoConnectOption output_connect,
115                            uint32_t nphysin,
116                            uint32_t nphysout,
117                            jack_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         void new_session (bool startup = false, std::string path = string());
131         gint cmdline_new_session (std::string path);
132         int  unload_session ();
133         void close_session() { unload_session(); }
134
135         int  save_state_canfail (string state_name = "");
136         void save_state (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 allow_focus (bool yn);
148
149         static gint generic_focus_in_event (GdkEventFocus *);
150         static gint generic_focus_out_event (GdkEventFocus *);
151
152         void toggle_location_window ();
153         void toggle_big_clock_window ();
154         void toggle_connection_editor ();
155         void toggle_route_params_window ();
156         void toggle_tempo_window ();
157
158         gint32 select_diskstream (GdkEventButton *ev);
159
160         Gtk::Tooltips& tooltips() { return _tooltips; }
161
162         static sigc::signal<void,bool> Blink;
163         static sigc::signal<void>      RapidScreenUpdate;
164         static sigc::signal<void>      SuperRapidScreenUpdate;
165         static sigc::signal<void,jack_nframes_t> Clock;
166
167         /* this is a helper function to centralize the (complex) logic for
168            blinking rec-enable buttons.
169         */
170
171         void rec_enable_button_blink (bool onoff, ARDOUR::DiskStream *, Gtk::Widget *w);
172
173         void name_io_setup (ARDOUR::AudioEngine&, string&, ARDOUR::IO& io, bool in);
174         void choose_io (ARDOUR::IO&, bool input);
175
176         static gint hide_and_quit (GdkEventAny *ev, ArdourDialog *);
177
178         XMLNode* editor_settings() const;
179         XMLNode* mixer_settings () const;
180         XMLNode* keyboard_settings () const;
181
182         void save_ardour_state ();
183         gboolean configure_handler (GdkEventConfigure* conf);
184
185         void do_transport_locate (jack_nframes_t position);
186         void halt_on_xrun_message ();
187
188         AudioClock primary_clock;
189         AudioClock secondary_clock;
190         AudioClock preroll_clock;
191         AudioClock postroll_clock;
192
193         void add_route ();
194         
195         void session_add_audio_track (int input_channels, int32_t output_channels) {
196                 session_add_audio_route (true, input_channels, output_channels);
197         }
198
199         void session_add_audio_bus (int input_channels, int32_t output_channels) {
200                 session_add_audio_route (false, input_channels, output_channels);
201         }
202
203         void session_add_midi_track ();
204
205         void set_engine (ARDOUR::AudioEngine&);
206
207         gint exit_on_main_window_close (GdkEventAny *);
208
209   protected:
210         friend class PublicEditor;
211
212         void toggle_metering ();
213         void toggle_clocking ();
214         void toggle_auto_play ();
215         void toggle_auto_input ();
216         void toggle_punch_in ();
217         void toggle_punch_out ();
218         void toggle_auto_return ();
219         void toggle_click ();
220         void toggle_follow ();
221
222         void toggle_session_auto_loop ();
223         void toggle_session_punch_in ();
224         
225         void toggle_options_window ();
226
227   private:
228         struct GlobalClickBox : public Gtk::VBox {
229             Gtkmm2ext::ClickBox  *box;
230             Gtk::Frame      frame;
231             Gtk::Label      label;
232             vector<string> &strings;
233             Gtk::Adjustment adjustment;
234
235             static void printer (char buf[32], Gtk::Adjustment &adj, void *arg);
236
237             GlobalClickBox (const string &str, vector<string> &vs)
238                     : strings (vs),
239                       adjustment (0, 0, vs.size() - 1, 1, 1, 0) {
240                     box = new Gtkmm2ext::ClickBox (&adjustment, "ClickButton");
241                     label.set_text (str);
242                     label.set_name ("GlobalButtonLabel");
243                     frame.add (*box);
244                     frame.set_shadow_type (Gtk::SHADOW_IN);
245                     pack_start (label);
246                     pack_start (frame);
247                     box->set_print_func (printer, this);
248                     box->set_wrap (true);
249             };
250         };
251
252         ARDOUR::AudioEngine                 *engine;
253         ARDOUR::Session                     *session;
254
255         Gtk::Tooltips          _tooltips;
256
257         void                     goto_editor_window ();
258         void                     goto_mixer_window ();
259         
260         Gtk::Table               adjuster_table;
261         Gtk::Frame               adjuster_frame;
262         Gtk::Fixed               adjuster_base;
263
264         GlobalClickBox     *online_control_button;
265         vector<string>      online_control_strings;
266
267         GlobalClickBox    *crossfade_time_button;
268         vector<string>     crossfade_time_strings;
269
270         GlobalClickBox    *mmc_id_button;
271         vector<string>     mmc_id_strings;
272
273         Gtk::ToggleButton   preroll_button;
274         Gtk::ToggleButton   postroll_button;
275
276         Gtk::Table          transport_table;
277         Gtk::Table          option_table;
278
279         int  setup_windows ();
280         void setup_session_menu ();
281         void setup_transport ();
282         void setup_clock ();
283         void setup_session_info ();
284         void setup_adjustables ();
285
286         Gtk::MenuBar* make_menubar ();
287         
288         static ARDOUR_UI *theArdourUI;
289
290         void startup ();
291         void shutdown ();
292
293         void finish();
294         int  ask_about_saving_session (string why);
295         gint ask_about_save_deleted (GdkEventAny*);
296         void save_session_choice_made (int);
297         int  save_the_session;
298
299         void queue_transport_change ();
300         void map_transport_state ();
301         int32_t do_engine_start ();
302         gint start_engine ();
303         
304         void engine_halted ();
305         void engine_stopped ();
306         void engine_running ();
307
308         void map_some_session_state (Gtk::ToggleButton& button,
309                                      bool (ARDOUR::Session::*get)() const);
310
311         void toggle_some_session_state (Gtk::ToggleButton& button,
312                                      bool (ARDOUR::Session::*get)() const,
313                                      void (ARDOUR::Session::*set)(bool));
314         void map_button_state ();
315
316         void clear_meters ();
317
318         static gint _blink  (void *);
319         void blink ();
320         gint blink_timeout_tag;
321         bool blink_on;
322         void start_blinking ();
323         void stop_blinking ();
324
325         void control_methods_adjusted ();
326         void mmc_device_id_adjusted ();
327
328   private:
329         Gtk::VBox     top_packer;
330
331         sigc::connection clock_signal_connection;
332         void         update_clocks ();
333         void         start_clocking ();
334         void         stop_clocking ();
335
336         AudioClock               big_clock;
337         Gtk::Frame               big_clock_frame;
338         ArdourDialog*          big_clock_window;
339
340         /* Transport Control */
341
342         void detach_tearoff (Gtk::Box* parent, Gtk::Widget* contents);
343         void reattach_tearoff (Gtk::Box* parent, Gtk::Widget* contents, int32_t order);
344
345         Gtkmm2ext::TearOff*       transport_tearoff;
346         Gtk::Frame               transport_frame;
347         Gtk::HBox                transport_tearoff_hbox;
348         Gtk::HBox                transport_hbox;
349         Gtk::Fixed               transport_base;
350         Gtk::Fixed               transport_button_base;
351         Gtk::Frame               transport_button_frame;
352         Gtk::HBox                transport_button_hbox;
353         Gtk::VBox                transport_button_vbox;
354         Gtk::HBox                transport_option_button_hbox;
355         Gtk::VBox                transport_option_button_vbox;
356         Gtk::HBox                transport_clock_hbox;
357         Gtk::VBox                transport_clock_vbox;
358         Gtk::HBox                primary_clock_hbox;
359         Gtk::HBox                secondary_clock_hbox;
360
361         Gtk::Button goto_start_button;
362         Gtk::Button goto_end_button;
363         Gtk::Button rewind_button;
364         Gtk::Button forward_button;
365         Gtk::Button stop_button;
366
367         enum ShuttleBehaviour {
368                 Sprung,
369                 Wheel
370         };
371
372         enum ShuttleUnits {
373                 Percentage,
374                 Semitones
375         };
376
377         Gtk::DrawingArea shuttle_box;
378         Gtk::EventBox    speed_display_box;
379         Gtk::Label       speed_display_label;
380         Gtk::Button      shuttle_units_button;
381         Gtk::Button      shuttle_style_button;
382         Gtk::Menu*       shuttle_unit_menu;
383         Gtk::Menu*       shuttle_style_menu;
384         ShuttleBehaviour shuttle_behaviour;
385         ShuttleUnits     shuttle_units;
386
387         void shuttle_style_clicked ();
388         void shuttle_unit_clicked ();
389         void set_shuttle_behaviour (ShuttleBehaviour);
390         void set_shuttle_units (ShuttleUnits);
391         void update_speed_display ();
392
393         gint shuttle_box_button_press (GdkEventButton*);
394         gint shuttle_box_button_release (GdkEventButton*);
395         gint shuttle_box_motion (GdkEventMotion*);
396         gint shuttle_box_expose (GdkEventExpose*);
397         gint mouse_shuttle (double x, bool force);
398         void use_shuttle_fract (bool force);
399
400         bool   shuttle_grabbed;
401         double shuttle_fract;
402
403         Gtk::ToggleButton auto_loop_button;
404         Gtk::ToggleButton play_selection_button;
405         Gtk::ToggleButton roll_button;
406
407         Gtk::ToggleButton rec_button;
408         Gtk::ToggleButton punch_in_button;
409         Gtk::ToggleButton punch_out_button;
410         Gtk::ToggleButton auto_return_button;
411         Gtk::ToggleButton auto_play_button;
412         Gtk::ToggleButton auto_input_button;
413         Gtk::ToggleButton click_button;
414         Gtk::ToggleButton follow_button;
415         Gtk::ToggleButton auditioning_alert_button;
416         Gtk::ToggleButton solo_alert_button;
417
418         Gtk::VBox alert_box;
419         
420         void follow_changed ();
421
422         void solo_blink (bool);
423         void audition_blink (bool);
424
425         void soloing_changed (bool);
426         void auditioning_changed (bool);
427         void _auditioning_changed (bool);
428
429         void solo_alert_toggle ();
430         void audition_alert_toggle ();
431
432         void primary_clock_value_changed ();
433         void secondary_clock_value_changed ();
434
435         /* called by Blink signal */
436
437         void transport_rec_enable_blink (bool onoff);
438
439         /* These change where we accept control from:
440            MMC, X (local) or both.
441         */
442
443         void allow_mmc_only ();
444         void allow_mmc_and_local ();
445         void allow_local_only ();
446
447         static void rate_printer (char buf[32], Gtk::Adjustment &, void *);
448
449         Gtk::Menu*        session_popup_menu;
450
451         struct RecentSessionModelColumns : public Gtk::TreeModel::ColumnRecord {
452             RecentSessionModelColumns() { 
453                     add (visible_name);
454                     add (fullpath);
455             }
456             Gtk::TreeModelColumn<Glib::ustring> visible_name;
457             Gtk::TreeModelColumn<Glib::ustring> fullpath;
458         };
459
460         RecentSessionModelColumns    recent_session_columns;
461         Gtk::TreeView                recent_session_display;
462         Glib::RefPtr<Gtk::TreeStore> recent_session_model;
463
464         ArdourDialog*     session_selector_window;
465         Gtk::FileChooserDialog* open_session_selector;
466         
467         void build_session_selector();
468         void recent_session_selection_changed ();
469         void redisplay_recent_sessions();
470
471         struct RecentSessionsSorter {
472             bool operator() (std::pair<string,string> a, std::pair<string,string> b) const {
473                     return cmp_nocase(a.first, b.first) == -1;
474             }
475         };
476
477         /* menu bar and associated stuff */
478
479         Gtk::MenuBar* menu_bar;
480         Gtk::Fixed    menu_bar_base;
481         Gtk::HBox     menu_hbox;
482
483         void build_menu_bar ();
484         void pack_toplevel_controls();
485
486         /* handles on the menu bar items that need to 
487            be sensitive to whether or not we have
488            a session loaded.
489         */
490
491         Gtk::MenuItem *add_track_item;
492         Gtk::MenuItem *save_item;
493         Gtk::MenuItem *snapshot_item;
494         Gtk::MenuItem *save_as_item;
495         Gtk::MenuItem *save_template_item;
496         Gtk::MenuItem *export_item;
497         Gtk::MenuItem *close_item;
498
499         Gtk::CheckMenuItem *meter_bridge_dialog_check;
500         Gtk::CheckMenuItem *connection_editor_check;
501         Gtk::CheckMenuItem *route_params_check;
502         Gtk::CheckMenuItem *locations_dialog_check;
503         Gtk::CheckMenuItem *big_clock_check;
504         Gtk::CheckMenuItem *tempo_editor_check;
505 //      Gtk::CheckMenuItem *sfdb_check;
506         Gtk::CheckMenuItem *options_window_check;
507         
508         /* <CMT Additions> */
509         Gtk::MenuItem *image_compositor_item ;
510         /* </CMT Additions> */
511
512
513         Gtk::Label   wall_clock_label;
514         Gtk::EventBox wall_clock_box;
515         gint update_wall_clock ();
516
517         Gtk::Label   disk_space_label;
518         Gtk::EventBox disk_space_box;
519         void update_disk_space ();
520
521         Gtk::Label   cpu_load_label;
522         Gtk::EventBox cpu_load_box;
523         void update_cpu_load ();
524
525         Gtk::Label   disk_rate_label;
526         Gtk::EventBox disk_rate_box;
527         void update_disk_rate();
528
529         Gtk::Label   buffer_load_label;
530         Gtk::EventBox buffer_load_box;
531         void update_buffer_load ();
532
533         Gtk::Label   sample_rate_label;
534         Gtk::EventBox sample_rate_box;
535         void update_sample_rate (jack_nframes_t);
536
537         gint every_second ();
538         gint every_point_one_seconds ();
539         gint every_point_zero_one_seconds ();
540
541         sigc::connection second_connection;
542         sigc::connection point_one_second_connection;
543         sigc::connection point_zero_one_second_connection;
544
545         void diskstream_added (ARDOUR::DiskStream*);
546
547         gint session_menu (GdkEventButton *);
548
549         bool _will_create_new_session_automatically;
550
551         NewSessionDialog* new_session_window;
552         string template_name;
553
554         void new_session_ok_clicked ();
555         void new_session_template_choice (Gtk::TreeView&, Glib::RefPtr<Gtk::TreeSelection>);
556         void hide_dialog (ArdourDialog *dialog);
557
558         void fs_cancel_clicked (Gtk::FileSelection*);
559         gint fs_delete_event (GdkEventAny*, Gtk::FileSelection*);
560
561         void open_session ();
562         void open_recent_session ();
563         void open_ok_clicked ();
564
565         void save_template ();
566
567
568         void session_add_audio_route (bool disk, int32_t input_channels, int32_t output_channels);
569
570         void add_diskstream_to_menu (ARDOUR::DiskStream&);
571         void diskstream_selected (gint32);
572         Gtk::Menu *diskstream_menu;
573         gint32 selected_dstream;
574         
575         gint mouse_transport_goto_start (GdkEventButton *) {
576                 transport_goto_start (); return TRUE;
577         }
578         gint mouse_transport_goto_end (GdkEventButton *) {
579                 transport_goto_end (); return TRUE;
580         }
581         gint mouse_transport_record (GdkEventButton *) {
582                 transport_record (); return TRUE;
583         }
584         gint mouse_transport_forward (GdkEventButton *) {
585                 transport_forward (0); return TRUE;
586         }
587         gint mouse_transport_rewind (GdkEventButton *) {
588                 transport_rewind (0); return TRUE;
589         }
590         gint mouse_transport_loop (GdkEventButton *) {
591                 transport_loop (); return TRUE;
592         }
593         gint mouse_transport_play_selection (GdkEventButton *) {
594                 transport_play_selection (); return TRUE;
595         }
596
597         gint mouse_transport_roll (GdkEventButton *);
598         gint mouse_transport_stop (GdkEventButton *);
599
600         void set_transport_sensitivity (bool);
601
602         void remove_last_capture ();
603
604         void transport_goto_start ();
605         void transport_goto_end ();
606         void transport_stop ();
607         void transport_stop_and_forget_capture ();
608         void transport_record ();
609         void transport_roll ();
610         void transport_play_selection(); 
611         void transport_forward (int option);
612         void transport_rewind (int option);
613         void transport_loop ();
614
615         void transport_locating ();
616         void transport_rolling ();
617         void transport_rewinding ();
618         void transport_forwarding ();
619         void transport_stopped ();
620
621         void send_all_midi_feedback ();
622         
623         bool _session_is_new;
624         void connect_to_session (ARDOUR::Session *);
625         void connect_dependents_to_session (ARDOUR::Session *);
626         void we_have_dependents ();
627         void setup_keybindings ();
628
629         guint32  last_key_press_time;
630
631         void snapshot_session ();
632
633         void map_control_change (ARDOUR::Session::ControlType);
634         void queue_map_control_change (ARDOUR::Session::ControlType);
635         void map_record_state ();
636         void queue_map_record_state ();
637
638         Mixer_UI   *mixer;
639         int         create_mixer ();
640         
641         PublicEditor     *editor;
642         int         create_editor ();
643
644         MeterBridge  *meter_bridge;
645         int          create_meter_bridge ();
646
647         RouteParams_UI *route_params;
648         int             create_route_params ();
649
650         ConnectionEditor *connection_editor;
651         int               create_connection_editor ();
652
653         LocationUI *location_ui;
654         int         create_location_ui ();
655
656         void meter_bridge_hiding ();
657         void location_ui_hiding ();
658         void big_clock_hiding ();
659         void route_params_hiding ();
660         void connection_editor_hiding ();
661         void option_hiding ();
662
663         /* Various options */
664
665         void toggle_recording_plugins ();
666
667         /* Options window */
668         
669         OptionEditor *option_editor;
670         
671         /* route dialog */
672
673         AddRouteDialog *add_route_dialog;
674         void add_route_dialog_done (int status);
675         
676         /* Keyboard Handling */
677         
678         Keyboard* keyboard;
679
680         /* Keymap handling */
681
682         void install_keybindings ();
683         void install_actions ();
684         void test_binding_action (const char *);
685         void start_keyboard_prefix();
686
687         void toggle_record_enable (guint32);
688         void toggle_monitor_enable (guint32);
689
690         uint32_t rec_enabled_diskstreams;
691         void count_recenabled_diskstreams (ARDOUR::DiskStream&);
692
693         About* about;
694         bool shown_flag;
695         /* cleanup */
696
697         Gtk::MenuItem *cleanup_item;
698
699         void display_cleanup_results (ARDOUR::Session::cleanup_report& rep, const gchar* list_title, string msg);
700         void cleanup ();
701         void flush_trash ();
702
703         bool have_configure_timeout;
704         struct timeval last_configure_time;
705         gint configure_timeout ();
706
707         struct timeval last_peak_grab;
708         struct timeval last_shuttle_request;
709
710         void cannot_record_no_input (ARDOUR::DiskStream*);
711
712         void delete_sources_in_the_right_thread (list<ARDOUR::Source*>*);
713
714         void editor_display_control_changed (Editing::DisplayControl c);
715
716         bool have_disk_overrun_displayed;
717         bool have_disk_underrun_displayed;
718
719         void disk_overrun_message_gone ();
720         void disk_underrun_message_gone ();
721         void disk_overrun_handler ();
722         void disk_underrun_handler ();
723
724         int pending_state_dialog ();
725         
726         void disconnect_from_jack ();
727         void reconnect_to_jack ();
728         void set_jack_buffer_size (jack_nframes_t);
729
730         Gtk::MenuItem* jack_disconnect_item;
731         Gtk::MenuItem* jack_reconnect_item;
732         Gtk::Menu*     jack_bufsize_menu;
733
734         int make_session_clean ();
735         bool filter_ardour_session_dirs (const Gtk::FileFilter::Info&);
736
737         Glib::RefPtr<Gtk::UIManager> ui_manager;
738 };
739
740 #endif /* __ardour_gui_h__ */