Fix crash when X11 is not available for VST UIs
[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 "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         void on_unmap ();
100         bool on_key_press_event (GdkEventKey*);
101         bool on_key_release_event (GdkEventKey*);
102
103         void parameter_changed (std::string const&);
104
105         void note_on_event_handler (int, int);
106         void note_off_event_handler (int);
107         void control_change_event_handler (int, int);
108         void control_change_knob_event_handler (int, int);
109
110         void modwheel_update_tooltip (int);
111         void modwheel_slider_adjusted ();
112
113         void octave_key_event_handler (bool);
114         void pitch_bend_key_event_handler (int, bool);
115         bool pitch_bend_timeout ();
116
117         void pitch_bend_event_handler (int);
118         void pitch_bend_release ();
119         void pitch_bend_update_tooltip (int);
120         void pitch_slider_adjusted ();
121
122         void select_keyboard_layout (std::string const&);
123         void update_velocity_settings ();
124         void update_octave_key ();
125         void update_octave_range ();
126         void cc_key_changed (size_t);
127         void update_cc (size_t, int);
128         bool send_panic_message (GdkEventButton*);
129         bool on_velocity_scroll_event (GdkEventScroll*);
130
131         APianoKeyboard  _piano;
132
133         ArdourWidgets::ArdourDropdown  _midi_channel;
134         ArdourWidgets::ArdourDropdown  _piano_velocity;
135         ArdourWidgets::ArdourDropdown  _piano_octave_key;
136         ArdourWidgets::ArdourDropdown  _piano_octave_range;
137         ArdourWidgets::ArdourDropdown  _transpose_output;
138         ArdourWidgets::ArdourButton    _send_panic;
139
140         boost::shared_ptr<VKBDControl>    _pitchbend;
141         Gtk::Adjustment                   _pitch_adjustment;
142         ArdourWidgets::VSliderController* _pitch_slider;
143         Gtkmm2ext::PersistentTooltip*     _pitch_slider_tooltip;
144
145         boost::shared_ptr<VKBDControl>    _modwheel;
146         Gtk::Adjustment                   _modwheel_adjustment;
147         ArdourWidgets::VSliderController* _modwheel_slider;
148         Gtkmm2ext::PersistentTooltip*     _modwheel_tooltip;
149 #define VKBD_NCTRLS 4
150
151         boost::shared_ptr<VKBDControl> _cc[VKBD_NCTRLS];
152         ArdourWidgets::ArdourKnob*     _cc_knob[VKBD_NCTRLS];
153         ArdourWidgets::ArdourDropdown  _cc_key[VKBD_NCTRLS];
154
155         PBD::ScopedConnectionList _cc_connections;
156
157         sigc::connection _bender_connection;
158         int              _pitch_bend_target;
159 };
160
161 #endif