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