Return key-focus to piano-keyboard
[ardour.git] / gtk2_ardour / virtual_keyboard_window.h
1 /*
2  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #ifndef _virtual_keyboard_window_h_
20 #define _virtual_keyboard_window_h_
21
22 #include <gtkmm/box.h>
23 #include <gtkmm/spinbutton.h>
24
25 #include "pbd/controllable.h"
26 #include "pbd/signals.h"
27
28 #include "gtkmm2ext/persistent_tooltip.h"
29
30 #include "widgets/ardour_button.h"
31 #include "widgets/ardour_dropdown.h"
32 #include "widgets/ardour_knob.h"
33 #include "widgets/slider_controller.h"
34
35 #include "ardour_window.h"
36 #include "gtk_pianokeyboard.h"
37
38 namespace ARDOUR {
39         class Session;
40 }
41
42 class VKBDControl : public PBD::Controllable
43 {
44 public:
45         VKBDControl (const std::string& name, double normal = 127, double upper = 127)
46                 : PBD::Controllable (name, Flag (0))
47                 , _lower (0)
48                 , _upper (upper)
49                 , _normal (normal)
50                 , _value (normal)
51         {}
52
53         /* Controllable API */
54         void set_value (double v, PBD::Controllable::GroupControlDisposition gcd)
55         {
56                 if (v != _value) {
57                         _value = std::max (_lower, std::min (_upper, v));
58                         Changed (true, gcd);        /* EMIT SIGNAL */
59                         ValueChanged ((int)_value); /* EMIT SIGNAL */
60                 }
61         }
62
63         std::string get_user_string () const
64         {
65                 char buf[32];
66                 sprintf (buf, "%.0f", get_value ());
67                 return std::string (buf);
68         }
69
70         double get_value () const { return _value; }
71         double lower () const { return _lower; }
72         double upper () const { return _upper; }
73         double normal () const { return _normal; }
74
75         PBD::Signal1<void, int> ValueChanged;
76
77 protected:
78         double _lower;
79         double _upper;
80         double _normal;
81         double _value;
82 };
83
84 class VirtualKeyboardWindow : public ArdourWindow
85 {
86 public:
87         VirtualKeyboardWindow ();
88         ~VirtualKeyboardWindow ();
89
90         void set_session (ARDOUR::Session*);
91
92         XMLNode& get_state ();
93         void     set_state (const XMLNode&);
94
95 protected:
96         bool on_focus_in_event (GdkEventFocus*);
97
98 private:
99         static void _note_on_event_handler (GtkWidget*, int note, int vel, gpointer arg)
100         {
101                 static_cast<VirtualKeyboardWindow*> (arg)->note_on_event_handler (note, vel);
102         }
103
104         static void _note_off_event_handler (GtkWidget*, int note, gpointer arg)
105         {
106                 static_cast<VirtualKeyboardWindow*> (arg)->note_off_event_handler (note);
107         }
108
109         void on_unmap ();
110         bool on_key_press_event (GdkEventKey*);
111
112         void note_on_event_handler (int, int);
113         void note_off_event_handler (int);
114         void control_change_event_handler (int, int);
115         void pitch_bend_event_handler (int);
116
117         void select_keyboard_layout (std::string const&);
118         void update_velocity_settings (int);
119         void update_octave_key ();
120         void update_octave_range ();
121         void bank_patch ();
122         void update_sensitivity ();
123         void pitch_slider_adjusted ();
124         bool toggle_config (GdkEventButton*);
125         bool toggle_bankpatch (GdkEventButton*);
126         bool toggle_yaxis_velocity (GdkEventButton*);
127         bool toggle_highlight_piano (GdkEventButton*);
128         bool toggle_highlight_key (GdkEventButton*);
129         bool toggle_note_label (GdkEventButton*);
130         bool send_panic_message (GdkEventButton*);
131
132         PianoKeyboard*  _piano;
133         Gtk::Widget*    _pianomm;
134         Gtk::SpinButton _piano_channel;
135
136         Gtk::SpinButton _transpose_output;
137
138         Gtk::SpinButton _bank_msb;
139         Gtk::SpinButton _bank_lsb;
140         Gtk::SpinButton _patchpgm;
141
142         Gtk::HBox* _cfg_box;
143         Gtk::HBox* _pgm_box;
144
145         ArdourWidgets::ArdourButton   _cfg_display;
146         ArdourWidgets::ArdourButton   _pgm_display;
147         ArdourWidgets::ArdourButton   _yaxis_velocity;
148         ArdourWidgets::ArdourButton   _highlight_grand_piano;
149         ArdourWidgets::ArdourButton   _highlight_key_range;
150         ArdourWidgets::ArdourButton   _show_note_label;
151         ArdourWidgets::ArdourButton   _send_panic;
152         ArdourWidgets::ArdourDropdown _keyboard_layout;
153
154         Gtk::SpinButton _piano_key_velocity;
155         Gtk::SpinButton _piano_min_velocity;
156         Gtk::SpinButton _piano_max_velocity;
157
158         Gtk::SpinButton _piano_octave_key;
159         Gtk::SpinButton _piano_octave_range;
160
161         boost::shared_ptr<VKBDControl>    _pitchbend;
162         Gtk::Adjustment                   _pitch_adjustment;
163         ArdourWidgets::VSliderController* _pitch_slider;
164         Gtkmm2ext::PersistentTooltip*     _pitch_slider_tooltip;
165
166 #define VKBD_NCTRLS 8
167
168         boost::shared_ptr<VKBDControl> _cc[VKBD_NCTRLS];
169         ArdourWidgets::ArdourKnob*     _cc_knob[VKBD_NCTRLS];
170         ArdourWidgets::ArdourDropdown  _cc_key[VKBD_NCTRLS];
171
172         PBD::ScopedConnectionList _cc_connections;
173 };
174
175 #endif