05addb98e3addfc936fdf59444f675faef126c55
[ardour.git] / gtk2_ardour / ardour_ui2.cc
1 /*
2     Copyright (C) 1999 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <cerrno>
28 #include <iostream>
29 #include <cmath>
30
31 #include <sigc++/bind.h>
32 #include "pbd/error.h"
33 #include "pbd/basename.h"
34 #include "pbd/fastlog.h"
35 #include <gtkmm2ext/cairocell.h>
36 #include <gtkmm2ext/utils.h>
37 #include <gtkmm2ext/click_box.h>
38 #include <gtkmm2ext/tearoff.h>
39
40 #include "ardour/session.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/ardour.h"
43 #include "ardour/profile.h"
44 #include "ardour/route.h"
45
46 #include "ardour_ui.h"
47 #include "keyboard.h"
48 #include "public_editor.h"
49 #include "audio_clock.h"
50 #include "actions.h"
51 #include "button_joiner.h"
52 #include "utils.h"
53 #include "theme_manager.h"
54 #include "midi_tracer.h"
55 #include "shuttle_control.h"
56 #include "global_port_matrix.h"
57 #include "location_ui.h"
58 #include "rc_option_editor.h"
59 #include "time_info_box.h"
60
61 #include "i18n.h"
62
63 using namespace std;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtkmm2ext;
67 using namespace Gtk;
68 using namespace Glib;
69
70 int
71 ARDOUR_UI::setup_windows ()
72 {
73         if (create_editor ()) {
74                 error << _("UI: cannot setup editor") << endmsg;
75                 return -1;
76         }
77
78         if (create_mixer ()) {
79                 error << _("UI: cannot setup mixer") << endmsg;
80                 return -1;
81         }
82
83         /* all other dialogs are created conditionally */
84
85         we_have_dependents ();
86
87         theme_manager->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleThemeManager")));
88
89 #ifdef TOP_MENUBAR
90         HBox* status_bar_packer = manage (new HBox);
91         EventBox* status_bar_event_box = manage (new EventBox);
92
93         status_bar_event_box->add (status_bar_label);
94         status_bar_event_box->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
95         status_bar_label.set_size_request (300, -1);
96         status_bar_packer->pack_start (*status_bar_event_box, true, true, 6);
97         status_bar_packer->pack_start (error_log_button, false, false);
98
99         status_bar_label.show ();
100         status_bar_event_box->show ();
101         status_bar_packer->show ();
102         error_log_button.show ();
103
104         error_log_button.signal_clicked().connect (mem_fun (*this, &UI::toggle_errors));
105         status_bar_event_box->signal_button_press_event().connect (mem_fun (*this, &ARDOUR_UI::status_bar_button_press));
106
107         editor->get_status_bar_packer().pack_start (*status_bar_packer, true, true);
108         editor->get_status_bar_packer().pack_start (menu_bar_base, false, false, 6);
109 #else
110         top_packer.pack_start (menu_bar_base, false, false);
111 #endif
112
113         top_packer.pack_start (transport_frame, false, false);
114
115         editor->add_toplevel_controls (top_packer);
116
117         setup_transport();
118
119         build_menu_bar ();
120
121         setup_tooltips ();
122
123         return 0;
124 }
125
126 void
127 ARDOUR_UI::setup_tooltips ()
128 {
129         set_tip (roll_button, _("Play from playhead"));
130         set_tip (stop_button, _("Stop playback"));
131         set_tip (rec_button, _("Toggle record"));
132         set_tip (play_selection_button, _("Play range/selection"));
133         set_tip (goto_start_button, _("Go to start of session"));
134         set_tip (goto_end_button, _("Go to end of session"));
135         set_tip (auto_loop_button, _("Play loop range"));
136         set_tip (midi_panic_button, _("MIDI Panic\nSend note off and reset controller messages on all MIDI channels"));
137         set_tip (*transport_joiner, _("Always Play Range Selection (if any)"));
138         set_tip (auto_return_button, _("Return to last playback start when stopped"));
139         set_tip (auto_play_button, _("Start playback after any locate"));
140         set_tip (auto_input_button, _("Be sensible about input monitoring"));
141         set_tip (click_button, _("Enable/Disable audio click"));
142         set_tip (solo_alert_button, _("When active, something is soloed.\nClick to de-solo everything"));
143         set_tip (auditioning_alert_button, _("When active, auditioning is taking place\nClick to stop the audition"));
144         set_tip (feedback_alert_button, _("When active, there is a feedback loop."));
145         set_tip (primary_clock, _("Primary Clock"));
146         set_tip (secondary_clock, _("Secondary Clock"));
147
148         synchronize_sync_source_and_video_pullup ();
149
150         editor->setup_tooltips ();
151 }
152
153 bool
154 ARDOUR_UI::status_bar_button_press (GdkEventButton* ev)
155 {
156         bool handled = false;
157
158         switch (ev->button) {
159         case 1:
160                 status_bar_label.set_text ("");
161                 handled = true;
162                 break;
163         default:
164                 break;
165         }
166
167         return handled;
168 }
169
170 void
171 ARDOUR_UI::display_message (const char *prefix, gint prefix_len, RefPtr<TextBuffer::Tag> ptag, RefPtr<TextBuffer::Tag> mtag, const char *msg)
172 {
173         string text;
174
175         UI::display_message (prefix, prefix_len, ptag, mtag, msg);
176 #ifdef TOP_MENUBAR
177
178         if (strcmp (prefix, _("[ERROR]: ")) == 0) {
179                 text = "<span color=\"red\" weight=\"bold\">";
180         } else if (strcmp (prefix, _("[WARNING]: ")) == 0) {
181                 text = "<span color=\"yellow\" weight=\"bold\">";
182         } else if (strcmp (prefix, _("[INFO]: ")) == 0) {
183                 text = "<span color=\"green\" weight=\"bold\">";
184         } else {
185                 text = "<span color=\"white\" weight=\"bold\">???";
186         }
187
188         text += prefix;
189         text += "</span>";
190         text += msg;
191
192         status_bar_label.set_markup (text);
193 #endif
194 }
195
196 XMLNode*
197 ARDOUR_UI::tearoff_settings (const char* name) const
198 {
199         XMLNode* ui_node = Config->extra_xml(X_("UI"));
200
201         if (ui_node) {
202                 XMLNode* tearoff_node = ui_node->child (X_("Tearoffs"));
203                 if (tearoff_node) {
204                         XMLNode* mnode = tearoff_node->child (name);
205                         return mnode;
206                 }
207         }
208
209         return 0;
210 }
211
212 void
213 ARDOUR_UI::setup_transport ()
214 {
215         RefPtr<Action> act;
216
217         transport_tearoff_hbox.set_border_width (3);
218         transport_tearoff_hbox.set_spacing (3);
219
220         transport_tearoff = manage (new TearOff (transport_tearoff_hbox));
221         transport_tearoff->set_name ("TransportBase");
222         transport_tearoff->tearoff_window().signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), &transport_tearoff->tearoff_window()), false);
223
224         if (Profile->get_sae()) {
225                 transport_tearoff->set_can_be_torn_off (false);
226         }
227
228         transport_hbox.pack_start (*transport_tearoff, true, false);
229
230         transport_base.set_name ("TransportBase");
231         transport_base.add (transport_hbox);
232
233         transport_frame.set_shadow_type (SHADOW_OUT);
234         transport_frame.set_name ("BaseFrame");
235         transport_frame.add (transport_base);
236
237         transport_tearoff->Detach.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::detach_tearoff), static_cast<Box*>(&top_packer),
238                                                  static_cast<Widget*>(&transport_frame)));
239         transport_tearoff->Attach.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer),
240                                                  static_cast<Widget*> (&transport_frame), 1));
241         transport_tearoff->Hidden.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::detach_tearoff), static_cast<Box*>(&top_packer),
242                                                  static_cast<Widget*>(&transport_frame)));
243         transport_tearoff->Visible.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer),
244                                                   static_cast<Widget*> (&transport_frame), 1));
245
246         auto_return_button.set_text(_("Auto Return"));
247         auto_play_button.set_text(_("Auto Play"));
248         auto_input_button.set_text (_("Auto Input"));
249
250         click_button.set_image (get_icon (X_("metronome")));
251         act = ActionManager::get_action ("Transport", "ToggleClick");
252         click_button.set_related_action (act);
253         click_button.signal_button_press_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::click_button_clicked), false);
254
255         auto_return_button.set_name ("transport option button");
256         auto_play_button.set_name ("transport option button");
257         auto_input_button.set_name ("transport option button");
258
259         /* these have to provide a clear indication of active state */
260
261         click_button.set_name ("transport active option button");
262         sync_button.set_name ("transport active option button");
263
264         stop_button.set_active (true);
265
266         goto_start_button.set_image (get_icon (X_("transport_start")));
267         goto_end_button.set_image (get_icon (X_("transport_end")));
268         roll_button.set_image (get_icon (X_("transport_play")));
269         stop_button.set_image (get_icon (X_("transport_stop")));
270         play_selection_button.set_image (get_icon (X_("transport_range")));
271         rec_button.set_image (get_icon (X_("transport_record")));
272         auto_loop_button.set_image (get_icon (X_("transport_loop")));
273
274         midi_panic_button.set_image (get_icon (X_("midi_panic")));
275         /* the icon for this has an odd aspect ratio, so fatten up the button */
276         midi_panic_button.set_size_request (25, -1);
277         
278         act = ActionManager::get_action (X_("Transport"), X_("Stop"));
279         stop_button.set_related_action (act);
280         act = ActionManager::get_action (X_("Transport"), X_("Roll"));
281         roll_button.set_related_action (act);
282         act = ActionManager::get_action (X_("Transport"), X_("Record"));
283         rec_button.set_related_action (act);
284         act = ActionManager::get_action (X_("Transport"), X_("GotoStart"));
285         goto_start_button.set_related_action (act);
286         act = ActionManager::get_action (X_("Transport"), X_("GotoEnd"));
287         goto_end_button.set_related_action (act);
288         act = ActionManager::get_action (X_("Transport"), X_("Loop"));
289         auto_loop_button.set_related_action (act);
290         act = ActionManager::get_action (X_("Transport"), X_("PlaySelection"));
291         play_selection_button.set_related_action (act);
292         act = ActionManager::get_action (X_("MIDI"), X_("panic"));
293         midi_panic_button.set_related_action (act);
294         act = ActionManager::get_action (X_("Transport"), X_("ToggleExternalSync"));
295         sync_button.set_related_action (act);
296
297         transport_joiner = manage (new ButtonJoiner ("transport button", play_selection_button, roll_button));
298
299         act = ActionManager::get_action (X_("Transport"), X_("AlwaysPlayRange"));
300         transport_joiner->set_related_action (act);
301
302         /* clocks, etc. */
303
304         ARDOUR_UI::Clock.connect (sigc::mem_fun (primary_clock, &AudioClock::set));
305         ARDOUR_UI::Clock.connect (sigc::mem_fun (secondary_clock, &AudioClock::set));
306
307         primary_clock->ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::primary_clock_value_changed));
308         secondary_clock->ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::secondary_clock_value_changed));
309         big_clock->ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::big_clock_value_changed));
310
311         act = ActionManager::get_action ("Transport", "ToggleAutoReturn");
312         auto_return_button.set_related_action (act);
313         act = ActionManager::get_action ("Transport", "ToggleAutoPlay");
314         auto_play_button.set_related_action (act);
315         act = ActionManager::get_action ("Transport", "ToggleAutoInput");
316         auto_input_button.set_related_action (act);
317
318         /* alerts */
319
320         /* CANNOT sigc::bind these to clicked or toggled, must use pressed or released */
321
322         solo_alert_button.set_name ("rude solo");
323         solo_alert_button.signal_button_press_event().connect (sigc::mem_fun(*this,&ARDOUR_UI::solo_alert_press), false);
324         auditioning_alert_button.set_name ("rude audition");
325         auditioning_alert_button.signal_button_press_event().connect (sigc::mem_fun(*this,&ARDOUR_UI::audition_alert_press), false);
326         feedback_alert_button.set_name ("feedback alert");
327         feedback_alert_button.signal_button_press_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::feedback_alert_press), false);
328
329         alert_box.pack_start (solo_alert_button, true, false);
330         alert_box.pack_start (auditioning_alert_button, true, false);
331         alert_box.pack_start (feedback_alert_button, true, false);
332
333         /* all transport buttons should be the same size vertically and
334          * horizontally 
335          */
336
337         Glib::RefPtr<SizeGroup> transport_button_size_group = SizeGroup::create (SIZE_GROUP_BOTH);
338         transport_button_size_group->add_widget (goto_start_button);
339         transport_button_size_group->add_widget (goto_end_button);
340         transport_button_size_group->add_widget (auto_loop_button);
341         transport_button_size_group->add_widget (rec_button);
342         transport_button_size_group->add_widget (play_selection_button);
343         transport_button_size_group->add_widget (roll_button);
344         transport_button_size_group->add_widget (stop_button);
345
346         HBox* tbox1 = manage (new HBox);
347         HBox* tbox2 = manage (new HBox);
348         HBox* tbox3 = manage (new HBox);
349         HBox* tbox = manage (new HBox);
350
351         VBox* vbox1 = manage (new VBox);
352         VBox* vbox2 = manage (new VBox);
353         VBox* vbox3 = manage (new VBox);
354
355         Alignment* a1 = manage (new Alignment);
356         Alignment* a2 = manage (new Alignment);
357         Alignment* a3 = manage (new Alignment);
358
359         tbox1->set_spacing (2);
360         tbox2->set_spacing (2);
361         tbox3->set_spacing (2);
362         tbox->set_spacing (2);
363
364         tbox1->pack_start (midi_panic_button, false, false);
365         tbox1->pack_start (goto_start_button, false, false);
366         tbox1->pack_start (goto_end_button, false, false);
367         tbox1->pack_start (auto_loop_button, false, false);
368
369         play_selection_button.set_rounded_corner_mask (0x1); /* upper left only */
370         roll_button.set_rounded_corner_mask (0x2); /* upper right only */
371
372         tbox2->pack_start (*transport_joiner, false, false);
373
374         tbox3->pack_start (stop_button, false, false);
375         tbox3->pack_start (rec_button, false, false, 6);
376
377         vbox1->pack_start (*tbox1, false, false);
378         vbox2->pack_start (*tbox2, false, false);
379         vbox3->pack_start (*tbox3, false, false);
380
381         a1->add (*vbox1);
382         a1->set (0.5, 1.0, 0.0, 0.0);
383         a2->add (*vbox2);
384         a2->set (0.5, 1.0, 0.0, 0.0);
385         a3->add (*vbox3);
386         a3->set (0.5, 1.0, 0.0, 0.0);
387
388         tbox->pack_start (*a1, false, false);
389         tbox->pack_start (*a2, false, false);
390         tbox->pack_start (*a3, false, false);
391
392         HBox* clock_box = manage (new HBox);
393
394         primary_clock->set_border_width (2);
395         clock_box->pack_start (*primary_clock, false, false);
396         if (!ARDOUR::Profile->get_small_screen()) {
397                 secondary_clock->set_border_width (2);
398                 clock_box->pack_start (*secondary_clock, false, false);
399         }
400
401         shuttle_box = new ShuttleControl;
402         shuttle_box->show ();
403
404         VBox* transport_vbox = manage (new VBox);
405         transport_vbox->set_name ("TransportBase");
406         transport_vbox->set_border_width (3);
407         transport_vbox->set_spacing (3);
408         transport_vbox->pack_start (*tbox, true, true, 0);
409         transport_vbox->pack_start (*shuttle_box, false, false, 0);
410
411         transport_tearoff_hbox.pack_start (*transport_vbox, false, false);
412
413         /* transport related toggle controls */
414
415         VBox* auto_box = manage (new VBox);
416         auto_box->set_homogeneous (true);
417         auto_box->set_spacing (2);
418         auto_box->pack_start (sync_button, false, false);
419         auto_box->pack_start (auto_play_button, false, false);
420         auto_box->pack_start (auto_return_button, false, false);
421
422         transport_tearoff_hbox.pack_start (*auto_box, false, false);
423         transport_tearoff_hbox.pack_start (*clock_box, false, false);
424         transport_tearoff_hbox.pack_start (click_button, false, false);
425
426         time_info_box = manage (new TimeInfoBox);
427         transport_tearoff_hbox.pack_start (*time_info_box, false, false);
428
429         if (Profile->get_small_screen()) {
430                 transport_tearoff_hbox.pack_start (_editor_transport_box, false, false);
431         }
432         transport_tearoff_hbox.pack_start (alert_box, false, false);
433
434         if (Profile->get_sae()) {
435                 Image* img = manage (new Image ((::get_icon (X_("sae")))));
436                 transport_tearoff_hbox.pack_end (*img, false, false);
437         }
438
439         /* desensitize */
440
441         set_transport_sensitivity (false);
442
443         XMLNode* tnode = tearoff_settings ("transport");
444         if (tnode) {
445                 transport_tearoff->set_state (*tnode);
446         }
447 }
448
449 void
450 ARDOUR_UI::manage_window (Window& win)
451 {
452         win.signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), &win));
453         win.signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (Keyboard::the_keyboard(), &Keyboard::enter_window), &win));
454         win.signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (Keyboard::the_keyboard(), &Keyboard::leave_window), &win));
455 }
456
457 void
458 ARDOUR_UI::detach_tearoff (Box* b, Widget* w)
459 {
460 //      editor->ensure_float (transport_tearoff->tearoff_window());
461         b->remove (*w);
462 }
463
464 void
465 ARDOUR_UI::reattach_tearoff (Box* b, Widget* w, int32_t n)
466 {
467         b->pack_start (*w);
468         b->reorder_child (*w, n);
469 }
470
471 void
472 ARDOUR_UI::soloing_changed (bool onoff)
473 {
474         if (solo_alert_button.get_active() != onoff) {
475                 solo_alert_button.set_active (onoff);
476         }
477 }
478
479 void
480 ARDOUR_UI::_auditioning_changed (bool onoff)
481 {
482         auditioning_alert_button.set_active (onoff);
483         set_transport_sensitivity (!onoff);
484 }
485
486 void
487 ARDOUR_UI::auditioning_changed (bool onoff)
488 {
489         UI::instance()->call_slot (MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::_auditioning_changed, this, onoff));
490 }
491
492 bool
493 ARDOUR_UI::audition_alert_press (GdkEventButton*)
494 {
495         if (_session) {
496                 _session->cancel_audition();
497         }
498         return true;
499 }
500
501 bool
502 ARDOUR_UI::solo_alert_press (GdkEventButton*)
503 {
504         if (_session) {
505                 if (_session->soloing()) {
506                         _session->set_solo (_session->get_routes(), false);
507                 } else if (_session->listening()) {
508                         _session->set_listen (_session->get_routes(), false);
509                 }
510         }
511         return true;
512 }
513
514 bool
515 ARDOUR_UI::feedback_alert_press (GdkEventButton *)
516 {
517         return true;
518 }
519
520 void
521 ARDOUR_UI::solo_blink (bool onoff)
522 {
523         if (_session == 0) {
524                 return;
525         }
526
527         if (_session->soloing() || _session->listening()) {
528                 if (onoff) {
529                         solo_alert_button.set_active (true);
530                 } else {
531                         solo_alert_button.set_active (false);
532                 }
533         } else {
534                 solo_alert_button.set_active (false);
535         }
536 }
537
538 void
539 ARDOUR_UI::sync_blink (bool onoff)
540 {
541         if (_session == 0 || !_session->config.get_external_sync()) {
542                 /* internal sync */
543                 sync_button.set_active (false);
544                 return;
545         }
546
547         if (!_session->transport_locked()) {
548                 /* not locked, so blink on and off according to the onoff argument */
549
550                 if (onoff) {
551                         sync_button.set_active (true);
552                 } else {
553                         sync_button.set_active (false);
554                 }
555         } else {
556                 /* locked */
557                 sync_button.set_active (true);
558         }
559 }
560
561 void
562 ARDOUR_UI::audition_blink (bool onoff)
563 {
564         if (_session == 0) {
565                 return;
566         }
567
568         if (_session->is_auditioning()) {
569                 if (onoff) {
570                         auditioning_alert_button.set_active (true);
571                 } else {
572                         auditioning_alert_button.set_active (false);
573                 }
574         } else {
575                 auditioning_alert_button.set_active (false);
576         }
577 }
578
579 void
580 ARDOUR_UI::feedback_blink (bool onoff)
581 {
582         if (_feedback_exists) {
583                 if (onoff) {
584                         feedback_alert_button.set_active (true);
585                 } else {
586                         feedback_alert_button.set_active (false);
587                 }
588         } else {
589                 feedback_alert_button.set_active (false);
590         }
591 }
592
593 void
594 ARDOUR_UI::set_transport_sensitivity (bool yn)
595 {
596         ActionManager::set_sensitive (ActionManager::transport_sensitive_actions, yn);
597         shuttle_box->set_sensitive (yn);
598 }
599
600 void
601 ARDOUR_UI::editor_realized ()
602 {
603         boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
604         Config->map_parameters (pc);
605
606         reset_dpi ();
607 }
608
609 void
610 ARDOUR_UI::maximise_editing_space ()
611 {
612         if (editor) {
613                 editor->maximise_editing_space ();
614         }
615 }
616
617 void
618 ARDOUR_UI::restore_editing_space ()
619 {
620         if (editor) {
621                 editor->restore_editing_space ();
622         }
623 }
624
625 bool
626 ARDOUR_UI::click_button_clicked (GdkEventButton* ev)
627 {
628         if (ev->button != 3) {
629                 /* this handler is just for button-3 clicks */
630                 return false;
631         }
632
633         RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleRCOptionsEditor"));
634         assert (act);
635
636         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic (act);
637         tact->set_active ();
638
639         rc_option_editor->set_current_page (_("Misc"));
640         return true;
641 }
642
643 void
644 ARDOUR_UI::toggle_always_play_range ()
645 {
646         RefPtr<Action> act = ActionManager::get_action (X_("Transport"), X_("AlwaysPlayRange"));
647         assert (act);
648
649         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic (act);
650         assert (tact);
651
652         Config->set_always_play_range (tact->get_active ());
653 }
654
655