Vkeybd: use ArdourWidgets for all GUI elements
[ardour.git] / gtk2_ardour / virtual_keyboard_window.cc
index 24dad70cf2e5701584e6ca7b4878140a7eb406fb..49e207eecab7b8c191fa7f322c74e23b4b6d8a43 100644 (file)
 
 #include <gtkmm/box.h>
 
+
+#include "pbd/convert.h"
+#include "pbd/compose.h"
+
 #include "ardour/async_midi_port.h"
 #include "ardour/session.h"
-#include "pbd/convert.h"
+
+#include "gtkmm2ext/utils.h"
 #include "widgets/tooltips.h"
 
 #include "ardour_ui.h"
@@ -38,142 +43,96 @@ using namespace ArdourWidgets;
 
 VirtualKeyboardWindow::VirtualKeyboardWindow ()
        : ArdourWindow (_("Virtual MIDI Keyboard"))
-       , _piano_channel (*manage (new Adjustment (1, 1, 16, 1, 1)))
-       , _transpose_output (*manage (new Adjustment (0, -12, 12, 1, 1)))
-       , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
-       , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
-       , _patchpgm (*manage (new Adjustment (1, 1, 128, 1, 16)))
-       , _cfg_display ("Config", ArdourButton::led_default_elements)
-       , _pgm_display ("Bank/Patch", ArdourButton::led_default_elements)
-       , _yaxis_velocity ("Y-Axis", ArdourButton::led_default_elements)
-       , _highlight_grand_piano ("Grand Piano", ArdourButton::led_default_elements)
-       , _highlight_key_range ("Key Bindings", ArdourButton::led_default_elements)
-       , _show_note_label ("Label C Key", ArdourButton::led_default_elements)
-       , _send_panic ("Panic", ArdourButton::default_elements)
-       , _piano_key_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
-       , _piano_min_velocity (*manage (new Adjustment (  1, 1, 127, 1, 16)))
-       , _piano_max_velocity (*manage (new Adjustment (127, 1, 127, 1, 16)))
-       , _piano_octave_key (*manage (new Adjustment (4, -1, 7, 1, 1)))
-       , _piano_octave_range (*manage (new Adjustment (7, 2, 11, 1, 1)))
+       , _send_panic (_("Panic"), ArdourButton::default_elements)
        , _pitch_adjustment (8192, 0, 16383, 1, 256)
+       , _modwheel_adjustment (0, 0, 127, 1, 8)
 {
+       UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::parameter_changed));
+
        _piano.set_flags (Gtk::CAN_FOCUS);
 
-       _piano.set_keyboard_layout(PianoKeyboard::QWERTY);
-       _piano.show_note_label (true);
-
-       using namespace Menu_Helpers;
-       _keyboard_layout.AddMenuElem (MenuElem ("QWERTY",
-                                               sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTY")));
-       _keyboard_layout.AddMenuElem (MenuElem ("QWERTZ",
-                                               sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTZ")));
-       _keyboard_layout.AddMenuElem (MenuElem ("AZERTY",
-                                               sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "AZERTY")));
-       _keyboard_layout.AddMenuElem (MenuElem ("DVORAK",
-                                               sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "DVORAK")));
-       _keyboard_layout.set_active ("QWERTY");
-
-       _cfg_display.set_active (false);
-       _pgm_display.set_active (false);
-       _yaxis_velocity.set_active (false);
-       _highlight_grand_piano.set_active (false);
-       _highlight_key_range.set_active (false);
-       _show_note_label.set_active (true);
+       _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
+       _piano.set_annotate_octave (true);
+       _piano.set_grand_piano_highlight (false);
+       _piano.set_annotate_layout (true);
+       _piano.set_annotate_octave (true);
 
-       _send_panic.set_can_focus (false);
+       for (int c = 0; c < 16; ++c) {
+               char buf[16];
+               sprintf (buf, "%d", c + 1);
+               _midi_channel.append_text_item (buf);
+       }
+       for (int v = 1; v < 128; ++v) {
+               char buf[16];
+               sprintf (buf, "%d", v);
+               _piano_velocity.append_text_item (buf);
+       }
+       for (int k = -1; k < 8; ++k) {
+               char buf[16];
+               sprintf (buf, "%d", k);
+               _piano_octave_key.append_text_item (buf);
+       }
+       for (int r = 2; r < 12; ++r) {
+               char buf[16];
+               sprintf (buf, "%d", r);
+               _piano_octave_range.append_text_item (buf);
+       }
+       for (int t = -12; t < 13; ++t) {
+               char buf[16];
+               sprintf (buf, "%d", t);
+               _transpose_output.append_text_item (buf);
+       }
+
+       _midi_channel.set_active ("1");
+       _piano_velocity.set_active ("100");
+       _piano_octave_key.set_active ("4");
+       _piano_octave_range.set_active ("7");
+       _transpose_output.set_active ("0");
 
        _pitchbend            = boost::shared_ptr<VKBDControl> (new VKBDControl ("PB", 8192, 16383));
        _pitch_slider         = manage (new VSliderController (&_pitch_adjustment, _pitchbend, 0, PX_SCALE (15)));
        _pitch_slider_tooltip = new Gtkmm2ext::PersistentTooltip (_pitch_slider);
 
-       _pitch_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
-       _pitchbend->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
-
-       set_tooltip (_highlight_grand_piano, "Shade keys outside the range of a Grand Piano (A0-C8).");
-       set_tooltip (_highlight_key_range, "Indicate which notes can be controlled by keyboard-shortcuts.");
-       set_tooltip (_show_note_label, "When enabled, print octave number on C-Keys");
-       set_tooltip (_yaxis_velocity, "When enabled, mouse-click y-axis position defines the velocity.");
+       _modwheel         = boost::shared_ptr<VKBDControl> (new VKBDControl ("MW", 0, 127));
+       _modwheel_slider  = manage (new VSliderController (&_modwheel_adjustment, _modwheel, 0, PX_SCALE (15)));
+       _modwheel_tooltip = new Gtkmm2ext::PersistentTooltip (_modwheel_slider);
 
-       set_tooltip (_piano_octave_key, "The center octave, and lowest octave for keyboard control.");
-       set_tooltip (_piano_octave_range, "Available octave range, centered around the key-octave.");
-       set_tooltip (_keyboard_layout, "Keyboard layout to use for keyboard control.");
+       /* tooltips */
+       set_tooltip (_midi_channel, _("Set the MIDI Channel of the produced MIDI events"));
+       set_tooltip (_piano_octave_key, _("The center octave, and lowest octave for keyboard control. Change with Arrow left/right."));
+       set_tooltip (_piano_octave_range, _("Available octave range, centered around the key-octave."));
+       set_tooltip (_piano_velocity, _("The default velocity to use with keyboard control, and when y-axis click-position is disabled."));
+       set_tooltip (_transpose_output, _("Chromatic transpose note events. Notes transposed outside the range of 0,,127 are discarded."));
 
-       set_tooltip (_piano_key_velocity, "The default velocity to use with keyboard control, and when y-axis click-position is disabled.");
-       set_tooltip (_piano_min_velocity, "Velocity to use when clicking at the top-end of a key.");
-       set_tooltip (_piano_max_velocity, "Velocity to use when clicking at the bottom-end of a key.");
+       set_tooltip (_send_panic, _("Send MIDI Panic message for current channel"));
 
-       set_tooltip (_send_panic, "Send MIDI Panic message for current channel");
+       modwheel_update_tooltip (0);
+       pitch_bend_update_tooltip (8192);
 
-       _pitch_slider_tooltip->set_tip ("Pitchbend: 8192");
+       /* prevent focus grab, let MIDI keyboard to handle key events */
+       _send_panic.set_can_focus (false);
+       _modwheel_slider->set_can_focus (false);
        _pitch_slider->set_can_focus (false);
 
-       /* config */
-       Table* cfg_tbl = manage (new Table);
-       cfg_tbl->attach (_yaxis_velocity,                      0,  1, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Velocity:"))), 0,  1, 1, 2, SHRINK, SHRINK, 4, 0);
-
-       cfg_tbl->attach (_piano_min_velocity,                  1,  2, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Min"))),       1,  2, 1, 2, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (_piano_max_velocity,                  2,  3, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Max"))),       2,  3, 1, 2, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (_piano_key_velocity,                  3,  4, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Key"))),       3,  4, 1, 2, SHRINK, SHRINK, 4, 0);
-
-       cfg_tbl->attach (*manage (new ArdourVSpacer),          4,  5, 0, 2, SHRINK, FILL,   4, 0);
-
-       cfg_tbl->attach (_piano_octave_key,                    5,  6, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Octave"))),    5,  6, 1, 2, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (_piano_octave_range,                  6,  7, 0, 1, SHRINK, SHRINK, 4, 0);
-       cfg_tbl->attach (*manage (new Label (_("Range"))),     6,  7, 1, 2, SHRINK, SHRINK, 4, 0);
-
-       cfg_tbl->attach (*manage (new ArdourVSpacer),          7,  8, 0, 2, SHRINK, FILL,   4, 0);
-
-       cfg_tbl->attach (_highlight_grand_piano,               8,  9, 0, 1, FILL,   SHRINK, 4, 1);
-       cfg_tbl->attach (_highlight_key_range,                 8,  9, 1, 2, FILL,   SHRINK, 4, 1);
-
-       cfg_tbl->attach (*manage (new ArdourVSpacer),          9, 10, 0, 2, SHRINK, FILL,   4, 0);
-
-       cfg_tbl->attach (_show_note_label,                    10, 11, 0, 1, FILL,   SHRINK, 4, 1);
-       cfg_tbl->attach (_keyboard_layout,                    10, 11, 1, 2, FILL,   SHRINK, 4, 1);
-       cfg_tbl->show_all ();
-
-       /* bank/patch */
-       Table* pgm_tbl = manage (new Table);
-
-       Label* lbl = manage (new Label (_("Note: Prefer\nTrack-controls")));
-       lbl->set_justify (JUSTIFY_CENTER);
-
-       pgm_tbl->attach (*lbl,                             0, 1, 0, 2, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (*manage (new ArdourVSpacer),      1, 2, 0, 2, SHRINK, FILL,   4, 0);
-       pgm_tbl->attach (_bank_msb,                        2, 3, 0, 1, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (_bank_lsb,                        3, 4, 0, 1, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (_patchpgm,                        4, 5, 0, 1, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (*manage (new Label (_("MSB"))),   2, 3, 1, 2, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (*manage (new Label (_("LSB"))),   3, 4, 1, 2, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (*manage (new Label (_("PGM"))),   4, 5, 1, 2, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->attach (*manage (new ArdourVSpacer),      5, 6, 0, 2, SHRINK, FILL,   4, 0);
-       pgm_tbl->attach (_send_panic,                      6, 7, 0, 2, SHRINK, SHRINK, 4, 0);
-       pgm_tbl->show_all ();
-
-       /* settings */
+       /* layout */
        Table* tbl = manage (new Table);
-       tbl->attach (_piano_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
+       tbl->attach (_midi_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
        tbl->attach (*manage (new Label (_("Channel"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
        tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
        tbl->attach (*_pitch_slider, 2, 3, 0, 2, SHRINK, FILL, 4, 0);
+       tbl->attach (*_modwheel_slider, 3, 4, 0, 2, SHRINK, FILL, 4, 0);
 
-       const char* default_cc[VKBD_NCTRLS] = { "7", "8", "1", "11", "91", "92", "93", "94" };
+       const int default_cc[VKBD_NCTRLS] = { 7, 8, 91, 93};
 
-       int col = 3;
-       for (int i = 0; i < VKBD_NCTRLS; ++i, ++col) {
+       int col = 4;
+       for (size_t i = 0; i < VKBD_NCTRLS; ++i, ++col) {
                _cc[i]      = boost::shared_ptr<VKBDControl> (new VKBDControl ("CC"));
                _cc_knob[i] = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Flags (0)));
                _cc_knob[i]->set_controllable (_cc[i]);
                _cc_knob[i]->set_size_request (PX_SCALE (21), PX_SCALE (21));
-               _cc_knob[i]->set_tooltip_prefix (_("CC: "));
                _cc_knob[i]->set_name ("monitor section knob");
 
-               for (int c = 1; c < 120; ++c) {
+               for (int c = 2; c < 120; ++c) {
                        if (c == 32) {
                                continue;
                        }
@@ -181,68 +140,72 @@ VirtualKeyboardWindow::VirtualKeyboardWindow ()
                        sprintf (key, "%d", c);
                        _cc_key[i].append_text_item (key);
                }
-               _cc_key[i].set_active (default_cc[i]);
+               update_cc (i, default_cc[i]);
 
                tbl->attach (*_cc_knob[i], col, col + 1, 0, 1, SHRINK, SHRINK, 4, 2);
                tbl->attach (_cc_key[i],   col, col + 1, 1, 2, SHRINK, SHRINK, 4, 2);
 
+               _cc_key[i].StateChanged.connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::cc_key_changed), i));
                _cc[i]->ValueChanged.connect_same_thread (_cc_connections,
-                                                         boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, i, _1));
+                                                         boost::bind (&VirtualKeyboardWindow::control_change_knob_event_handler, this, i, _1));
        }
 
-       tbl->attach (*manage (new ArdourVSpacer), col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
+       tbl->attach (*manage (new ArdourVSpacer),       col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
+       ++col;
+       tbl->attach (_piano_octave_key,                 col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
+       tbl->attach (*manage (new Label (_("Octave"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
+       ++col;
+       tbl->attach (_piano_octave_range,               col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
+       tbl->attach (*manage (new Label (_("Range"))),  col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
+       ++col;
+
+       tbl->attach (*manage (new ArdourVSpacer),     col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
+       ++col;
+       tbl->attach (_piano_velocity,                 col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
+       tbl->attach (*manage (new Label (_("Vel."))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
        ++col;
-       tbl->attach (_transpose_output,                      col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
-       tbl->attach (*manage (new Label (_("Transpose"))),   col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
+
+       tbl->attach (*manage (new ArdourVSpacer),          col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
+       ++col;
+       tbl->attach (_transpose_output,                    col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
+       tbl->attach (*manage (new Label (_("Transpose"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
+       ++col;
+       tbl->attach (_send_panic,                      col, col + 1, 0, 2, SHRINK, SHRINK, 4, 0);
 
        /* main layout */
        Box* box1 = manage (new HBox ());
        box1->pack_start (*tbl, true, false);
 
-       Box* box2 = manage (new VBox ());
-       box2->pack_start (_pgm_display, false, false, 1);
-       box2->pack_start (_cfg_display, false, false, 1);
-       box1->pack_start (*box2, false, false);
-
-       _cfg_box = manage (new HBox ());
-       _cfg_box->pack_start (*cfg_tbl, true, false);
-       _cfg_box->set_no_show_all (true);
-
-       _pgm_box = manage (new HBox ());
-       _pgm_box->pack_start (*pgm_tbl, true, false);
-       _pgm_box->set_no_show_all (true);
-
        VBox* vbox = manage (new VBox);
        vbox->pack_start (*box1, false, false, 4);
-       vbox->pack_start (*_pgm_box, false, false, 4);
-       vbox->pack_start (*_cfg_box, false, false, 4);
        vbox->pack_start (_piano, true, true);
        add (*vbox);
 
-       _bank_msb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
-       _bank_lsb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
-       _patchpgm.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
+       /* GUI signals */
+
+       _pitch_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
+       _pitchbend->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
+       _pitch_slider->StopGesture.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_bend_release));
 
-       _piano_key_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 0));
-       _piano_min_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 1));
-       _piano_max_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 2));
+       _modwheel_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::modwheel_slider_adjusted));
+       _modwheel->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, 1, _1));
 
-       _piano_octave_key.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_key));
-       _piano_octave_range.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_range));
+       _piano_velocity.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings));
+       _piano_octave_key.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_key));
+       _piano_octave_range.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_range));
 
-       _cfg_display.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_config), false);
-       _pgm_display.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_bankpatch), false);
-       _yaxis_velocity.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_yaxis_velocity), false);
-       _highlight_grand_piano.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_highlight_piano), false);
-       _highlight_key_range.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_highlight_key), false);
-       _show_note_label.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_note_label), false);
        _send_panic.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::send_panic_message), false);
 
+       /* piano keyboard signals */
 
        _piano.NoteOn.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_on_event_handler));
        _piano.NoteOff.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_off_event_handler));
+       _piano.SwitchOctave.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::octave_key_event_handler));
+       _piano.PitchBend.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_bend_key_event_handler));
+
+       /* initialize GUI */
 
-       update_velocity_settings (0);
+       update_velocity_settings ();
        update_octave_range ();
 
        set_keep_above (true);
@@ -268,23 +231,23 @@ VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
                set_state (*node);
        }
 }
+void
+VirtualKeyboardWindow::parameter_changed (std::string const& p)
+{
+       if (p == "vkeybd-layout") {
+               select_keyboard_layout (UIConfiguration::instance().get_vkeybd_layout ());
+       }
+}
 
 XMLNode&
 VirtualKeyboardWindow::get_state ()
 {
        XMLNode* node = new XMLNode (X_("VirtualKeyboard"));
-       node->set_property (X_("YAxisVelocity"), _yaxis_velocity.get_active ());
-       node->set_property (X_("HighlightGrandPiano"), _highlight_grand_piano.get_active ());
-       node->set_property (X_("HighlightKeyRange"), _highlight_key_range.get_active ());
-       node->set_property (X_("ShowNoteLabel"), _show_note_label.get_active ());
-       node->set_property (X_("Layout"), _keyboard_layout.get_text ());
-       node->set_property (X_("Channel"), _piano_channel.get_value_as_int ());
-       node->set_property (X_("Transpose"), _transpose_output.get_value_as_int ());
-       node->set_property (X_("MinVelocity"), _piano_min_velocity.get_value_as_int ());
-       node->set_property (X_("MaxVelocity"), _piano_max_velocity.get_value_as_int ());
-       node->set_property (X_("KeyVelocity"), _piano_key_velocity.get_value_as_int ());
-       node->set_property (X_("Octave"), _piano_octave_key.get_value_as_int ());
-       node->set_property (X_("Range"), _piano_octave_range.get_value_as_int ());
+       node->set_property (X_("Channel"), _midi_channel.get_text ());
+       node->set_property (X_("Transpose"), _transpose_output.get_text ());
+       node->set_property (X_("KeyVelocity"), _piano_velocity.get_text ());
+       node->set_property (X_("Octave"), _piano_octave_key.get_text ());
+       node->set_property (X_("Range"), _piano_octave_range.get_text ());
        for (int i = 0; i < VKBD_NCTRLS; ++i) {
                char buf[16];
                sprintf (buf, "CC-%d", i);
@@ -302,70 +265,45 @@ VirtualKeyboardWindow::set_state (const XMLNode& root)
 
        XMLNode const* node = &root;
 
-       std::string layout;
-       if (node->get_property (X_("Layout"), layout)) {
-               select_keyboard_layout (layout);
-       }
-
        for (int i = 0; i < VKBD_NCTRLS; ++i) {
                char buf[16];
                sprintf (buf, "CC-%d", i);
                std::string cckey;
                if (node->get_property (buf, cckey)) {
-                       _cc_key[i].set_active (cckey);
+                       update_cc (i, PBD::atoi (cckey));
                }
        }
 
-       bool a;
-       if (node->get_property (X_("YAxisVelocity"), a)) {
-               _yaxis_velocity.set_active (a);
-       }
-       if (node->get_property (X_("HighlightGrandPiano"), a)) {
-               _highlight_grand_piano.set_active (a);
-               _piano.set_grand_piano_highlight (a);
-       }
-       if (node->get_property (X_("HighlightKeyRange"), a)) {
-               _highlight_key_range.set_active (a);
-               _piano.set_keyboard_cue (a);
-       }
-       if (node->get_property (X_("ShowNoteLabel"), a)) {
-               _show_note_label.set_active (a);
-               _piano.show_note_label (a);
-       }
-
-       int v;
-       if (node->get_property (X_("Channel"), v)) {
-               _piano_channel.set_value (v);
-       }
-       if (node->get_property (X_("Transpose"), v)) {
-               _transpose_output.set_value (v);
-       }
-       if (node->get_property (X_("MinVelocity"), v)) {
-               _piano_min_velocity.set_value (v);
+       std::string s;
+       if (node->get_property (X_("Channel"), s)) {
+               uint8_t channel = PBD::atoi (_midi_channel.get_text ());
+               if (channel > 0 && channel < 17) {
+                       _midi_channel.set_active (s);
+               }
        }
-       if (node->get_property (X_("MaxVelocity"), v)) {
-               _piano_max_velocity.set_value (v);
+       if (node->get_property (X_("Transpose"), s)) {
+               _transpose_output.set_active (s);
        }
-       if (node->get_property (X_("KeyVelocity"), v)) {
-               _piano_key_velocity.set_value (v);
+       if (node->get_property (X_("KeyVelocity"), s)) {
+               _piano_velocity.set_active (s);
        }
-       if (node->get_property (X_("Octave"), v)) {
-               _piano_octave_key.set_value (v);
+       if (node->get_property (X_("Octave"), s)) {
+               _piano_octave_key.set_active (s);
        }
-       if (node->get_property (X_("Range"), v)) {
-               _piano_octave_range.set_value (v);
+       if (node->get_property (X_("Range"), s)) {
+               _piano_octave_range.set_active (s);
        }
 
-       update_velocity_settings (0);
+       update_velocity_settings ();
        update_octave_range ();
        update_octave_key ();
 }
 
 bool
-VirtualKeyboardWindow::on_focus_in_event (GdkEventFocus *ev)
+VirtualKeyboardWindow::on_focus_in_event (GdkEventFocusev)
 {
        _piano.grab_focus ();
-       return ArdourWindow::on_focus_in_event(ev);
+       return ArdourWindow::on_focus_in_event (ev);
 }
 
 void
@@ -378,103 +316,71 @@ VirtualKeyboardWindow::on_unmap ()
 bool
 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
 {
+       /* try propagate unmodified events first */
+       if ((ev->state & 0xf) == 0) {
+               if (gtk_window_propagate_key_event (gobj(), ev)) {
+                       return true;
+               }
+       }
+
        _piano.grab_focus ();
+
        return ARDOUR_UI_UTILS::relay_key_press (ev, this);
 }
 
+bool
+VirtualKeyboardWindow::on_key_release_event (GdkEventKey* ev)
+{
+       /* try propagate unmodified events first */
+       if ((ev->state & 0xf) == 0) {
+               if (gtk_window_propagate_key_event (gobj(), ev)) {
+                       return true;
+               }
+       }
+
+       _piano.grab_focus ();
+
+       return ArdourWindow::on_key_release_event (ev);
+}
+
 void
 VirtualKeyboardWindow::select_keyboard_layout (std::string const& l)
 {
        if (l == "QWERTY") {
-               _piano.set_keyboard_layout (PianoKeyboard::QWERTY);
+               _piano.set_keyboard_layout (APianoKeyboard::QWERTY);
        } else if (l == "QWERTZ") {
-               _piano.set_keyboard_layout (PianoKeyboard::QWERTZ);
+               _piano.set_keyboard_layout (APianoKeyboard::QWERTZ);
        } else if (l == "AZERTY") {
-               _piano.set_keyboard_layout (PianoKeyboard::AZERTY);
+               _piano.set_keyboard_layout (APianoKeyboard::AZERTY);
        } else if (l == "DVORAK") {
-               _piano.set_keyboard_layout (PianoKeyboard::DVORAK);
-       }
-}
-
-bool
-VirtualKeyboardWindow::toggle_config (GdkEventButton* ev)
-{
-       bool a = !_cfg_display.get_active ();
-       _cfg_display.set_active (a);
-       if (a) {
-               _cfg_box->show ();
-       } else {
-               _cfg_box->hide ();
-       }
-       return false;
-}
-
-bool
-VirtualKeyboardWindow::toggle_bankpatch (GdkEventButton*)
-{
-       bool a = !_pgm_display.get_active ();
-       _pgm_display.set_active (a);
-       if (a) {
-               _pgm_box->show ();
-       } else {
-               _pgm_box->hide ();
+               _piano.set_keyboard_layout (APianoKeyboard::DVORAK);
+       } else if (l == "QWERTY Single") {
+               _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
+       } else if (l == "QWERTZ Single") {
+               _piano.set_keyboard_layout (APianoKeyboard::S_QWERTZ);
        }
-       return false;
+       _piano.grab_focus ();
 }
 
 void
 VirtualKeyboardWindow::update_octave_key ()
 {
-       _piano.set_octave (_piano_octave_key.get_value_as_int ());
+       _piano.set_octave (PBD::atoi (_piano_octave_key.get_text ()));
        _piano.grab_focus ();
 }
 
 void
 VirtualKeyboardWindow::update_octave_range ()
 {
-       _piano.set_octave_range (_piano_octave_range.get_value_as_int ());
+       _piano.set_octave_range (PBD::atoi (_piano_octave_range.get_text ()));
+       _piano.set_grand_piano_highlight (PBD::atoi (_piano_octave_range.get_text ()) > 3);
        _piano.grab_focus ();
 }
 
-bool
-VirtualKeyboardWindow::toggle_yaxis_velocity (GdkEventButton*)
-{
-       _yaxis_velocity.set_active (!_yaxis_velocity.get_active ());
-       update_velocity_settings (0);
-       return false;
-}
-
-bool
-VirtualKeyboardWindow::toggle_highlight_piano (GdkEventButton*)
-{
-       bool a = !_highlight_grand_piano.get_active ();
-       _highlight_grand_piano.set_active (a);
-       _piano.set_grand_piano_highlight (a);
-       return false;
-}
-
-bool
-VirtualKeyboardWindow::toggle_highlight_key (GdkEventButton*)
-{
-       bool a = !_highlight_key_range.get_active ();
-       _highlight_key_range.set_active (a);
-       _piano.set_keyboard_cue (a);
-       return false;
-}
-
-bool
-VirtualKeyboardWindow::toggle_note_label (GdkEventButton*)
-{
-       bool a = !_show_note_label.get_active ();
-       _show_note_label.set_active (a);
-       _piano.show_note_label (a);
-       return false;
-}
-
 bool
 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
 {
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
+       uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
        uint8_t ev[3];
        ev[0] = MIDI_CMD_CONTROL | channel;
        ev[1] = MIDI_CTL_SUSTAIN;
@@ -488,67 +394,110 @@ VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
 }
 
 void
-VirtualKeyboardWindow::bank_patch ()
+VirtualKeyboardWindow::update_velocity_settings ()
 {
-       int msb = _bank_msb.get_value_as_int ();
-       int lsb = _bank_lsb.get_value_as_int ();
-       int pgm = _patchpgm.get_value_as_int () - 1;
+       int v = PBD::atoi (_piano_velocity.get_text ());
+       _piano.set_velocities (v, v, v);
+}
 
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
-       uint8_t ev[3];
-       ev[0] = MIDI_CMD_CONTROL | channel;
-       ev[1] = MIDI_CTL_MSB_BANK;
-       ev[2] = (msb >> 7) & 0x7f;
-       _session->vkbd_output_port ()->write (ev, 3, 0);
-       ev[1] = MIDI_CTL_LSB_BANK | channel;
-       ev[2] = lsb & 0x7f;
-       _session->vkbd_output_port ()->write (ev, 3, 0);
-       ev[0] = MIDI_CMD_PGM_CHANGE | channel;
-       ev[1] = pgm & 0x7f;
-       _session->vkbd_output_port ()->write (ev, 2, 0);
+void
+VirtualKeyboardWindow::cc_key_changed (size_t i)
+{
+       int ctrl = PBD::atoi (_cc_key[i].get_text ());
+       update_cc (i, ctrl);
 }
 
 void
-VirtualKeyboardWindow::update_velocity_settings (int ctrl)
+VirtualKeyboardWindow::update_cc (size_t i, int cc)
 {
-       if (_piano_min_velocity.get_value_as_int () > _piano_max_velocity.get_value_as_int ()) {
-               if (ctrl == 2) {
-                       _piano_min_velocity.set_value (_piano_max_velocity.get_value_as_int ());
-                       return;
-               } else {
-                       _piano_max_velocity.set_value (_piano_min_velocity.get_value_as_int ());
-                       return;
-               }
+       assert (i < VKBD_NCTRLS);
+       if (cc < 0 || cc > 120) {
+               return;
        }
+       char buf[16];
+       sprintf (buf, "%d", cc);
+       _cc_key[i].set_active (buf);
+       _cc_knob[i]->set_tooltip_prefix (string_compose (_("CC-%1: "), cc));
+       // TODO update _cc[i]->normal
+}
 
-       if (_yaxis_velocity.get_active ()) {
-               _piano.set_velocities (_piano_min_velocity.get_value_as_int (),
-                                      _piano_max_velocity.get_value_as_int (),
-                                      _piano_key_velocity.get_value_as_int ());
-       } else {
-               _piano.set_velocities (_piano_key_velocity.get_value_as_int (),
-                                      _piano_key_velocity.get_value_as_int (),
-                                      _piano_key_velocity.get_value_as_int ());
-       }
-       update_sensitivity ();
+void
+VirtualKeyboardWindow::octave_key_event_handler (bool up)
+{
+       int k = PBD::atoi (_piano_octave_key.get_text ()) + (up ? 1 : -1);
+       k = std::min (7, std::max (-1, k));
+       char buf[16];
+       sprintf (buf, "%d", k);
+       _piano_octave_key.set_active (buf);
 }
 
 void
-VirtualKeyboardWindow::update_sensitivity ()
+VirtualKeyboardWindow::pitch_bend_key_event_handler (int target, bool interpolate)
 {
-       bool c = _yaxis_velocity.get_active ();
-       _piano_min_velocity.set_sensitive (c);
-       _piano_max_velocity.set_sensitive (c);
-       _piano.grab_focus ();
+       int cur = _pitch_adjustment.get_value();
+       if (cur == target) {
+               return;
+       }
+       if (interpolate) {
+               _pitch_bend_target = target;
+               if (!_bender_connection.connected ()) {
+                       float tc = _pitch_bend_target == 8192 ? .35 : .51;
+                       cur = rintf (cur + tc * (_pitch_bend_target - cur));
+                       _pitch_adjustment.set_value (cur);
+                       _bender_connection =  Glib::signal_timeout().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::pitch_bend_timeout), 20 /*ms*/);
+               }
+               return;
+       }
+               _bender_connection.disconnect ();
+       _pitch_adjustment.set_value (target);
+       _pitch_bend_target = target;
+}
+
+bool
+VirtualKeyboardWindow::pitch_bend_timeout ()
+{
+       int cur = _pitch_adjustment.get_value();
+
+       /* a spring would be 2nd order with overshoot,
+        * but we assume it's critically damped */
+       float tc = _pitch_bend_target == 8192 ? .35 : .51;
+       cur = rintf (cur + tc * (_pitch_bend_target - cur));
+       if (abs (cur - _pitch_bend_target) < 2) {
+               cur = _pitch_bend_target;
+       }
+       _pitch_adjustment.set_value (cur);
+       return _pitch_bend_target != cur;
 }
 
 void
 VirtualKeyboardWindow::pitch_slider_adjusted ()
 {
        _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
-       char buf[64];
-       snprintf (buf, sizeof (buf), "Pitchbend: %.0f", _pitch_adjustment.get_value ());
-       _pitch_slider_tooltip->set_tip (buf);
+       pitch_bend_update_tooltip (_pitch_adjustment.get_value ());
+}
+
+void
+VirtualKeyboardWindow::pitch_bend_update_tooltip (int value)
+{
+       _pitch_slider_tooltip->set_tip (string_compose (
+             _("Pitchbend: %1\n"
+               "Use mouse-drag for sprung mode,\n"
+               "mouse-wheel for presisent bends.\n"
+               "F1-F4 and arrow-up/down keys jump\n"
+               "to select values."), value));
+}
+
+void
+VirtualKeyboardWindow::modwheel_slider_adjusted ()
+{
+       _modwheel->set_value (_modwheel_adjustment.get_value (), PBD::Controllable::NoGroup);
+       modwheel_update_tooltip (_modwheel_adjustment.get_value ());
+}
+
+void
+VirtualKeyboardWindow::modwheel_update_tooltip (int value)
+{
+       _modwheel_tooltip->set_tip (string_compose (_("Modulation: %1"), value));
 }
 
 void
@@ -558,11 +507,11 @@ VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
        if (!_session) {
                return;
        }
-       note += _transpose_output.get_value_as_int ();
+       note += PBD::atoi (_transpose_output.get_text ());
        if (note < 0 || note > 127) {
                return;
        }
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
+       uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
        uint8_t ev[3];
        ev[0] = MIDI_CMD_NOTE_ON | channel;
        ev[1] = note;
@@ -576,11 +525,11 @@ VirtualKeyboardWindow::note_off_event_handler (int note)
        if (!_session) {
                return;
        }
-       note += _transpose_output.get_value_as_int ();
+       note += PBD::atoi (_transpose_output.get_text ());
        if (note < 0 || note > 127) {
                return;
        }
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
+       uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
        uint8_t ev[3];
        ev[0] = MIDI_CMD_NOTE_OFF | channel;
        ev[1] = note;
@@ -589,19 +538,25 @@ VirtualKeyboardWindow::note_off_event_handler (int note)
 }
 
 void
-VirtualKeyboardWindow::control_change_event_handler (int key, int val)
+VirtualKeyboardWindow::control_change_knob_event_handler (int key, int val)
 {
-       if (!_session) {
-               return;
-       }
        assert (key >= 0 && key < VKBD_NCTRLS);
        int ctrl = PBD::atoi (_cc_key[key].get_text ());
        assert (ctrl > 0 && ctrl < 127);
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
+       control_change_event_handler (ctrl, val);
+}
+
+void
+VirtualKeyboardWindow::control_change_event_handler (int ctrl, int val)
+{
+       if (!_session) {
+               return;
+       }
+       uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
        uint8_t ev[3];
        ev[0] = MIDI_CMD_CONTROL | channel;
-       ev[1] = ctrl;
-       ev[2] = val;
+       ev[1] = ctrl & 0x7f;
+       ev[2] = val & 0x7f;
        _session->vkbd_output_port ()->write (ev, 3, 0);
 }
 
@@ -611,10 +566,16 @@ VirtualKeyboardWindow::pitch_bend_event_handler (int val)
        if (!_session) {
                return;
        }
-       uint8_t channel = _piano_channel.get_value_as_int () - 1;
+       uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
        uint8_t ev[3];
        ev[0] = MIDI_CMD_BENDER | channel;
        ev[1] = val & 0x7f;
        ev[2] = (val >> 7) & 0x7f;
        _session->vkbd_output_port ()->write (ev, 3, 0);
 }
+
+void
+VirtualKeyboardWindow::pitch_bend_release ()
+{
+       _pitch_adjustment.set_value (8192);
+}