Fix DSP load sorting with inactive plugins
[ardour.git] / gtk2_ardour / pianokeyboard.h
1 /*
2  * Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #ifndef _PIANO_KEYBOARD_H_
21 #define _PIANO_KEYBOARD_H_
22
23 #include <map>
24 #include <gtkmm/drawingarea.h>
25
26 #define NNOTES (128)
27
28 class APianoKeyboard : public Gtk::DrawingArea
29 {
30 public:
31         APianoKeyboard ();
32         ~APianoKeyboard ();
33
34         sigc::signal<void, int, int>  NoteOn;
35         sigc::signal<void, int>       NoteOff;
36         sigc::signal<void>            Rest;
37         sigc::signal<void,bool>       SustainChanged;
38         sigc::signal<void, int, bool> PitchBend;
39         sigc::signal<void, bool>      SwitchOctave;
40
41         enum Layout {
42                 QWERTY,
43                 QWERTZ,
44                 AZERTY,
45                 DVORAK,
46                 S_QWERTY,
47                 S_QWERTZ
48         };
49
50         void sustain_press ();
51         void sustain_release ();
52
53         void set_note_on (int note);
54         void set_note_off (int note);
55
56         void set_grand_piano_highlight (bool enabled);
57         void set_annotate_layout (bool enabled);
58         void set_annotate_octave (bool enabled);
59
60         void set_monophonic (bool monophonic);
61         void set_octave (int octave);
62         void set_octave_range (int octave_range);
63         void set_keyboard_layout (Layout layout);
64         void set_velocities (int min_vel, int max_vel, int key_vel);
65
66 protected:
67         bool on_key_press_event (GdkEventKey*);
68         bool on_key_release_event (GdkEventKey*);
69         bool on_button_press_event (GdkEventButton*);
70         bool on_button_release_event (GdkEventButton*);
71         bool on_motion_notify_event (GdkEventMotion*);
72         bool on_expose_event (GdkEventExpose*);
73
74         void on_size_request (Gtk::Requisition*);
75         void on_size_allocate (Gtk::Allocation&);
76
77 private:
78         void annotate_layout (cairo_t* cr, int note) const;
79         void annotate_note (cairo_t* cr, int note) const;
80         void draw_note (cairo_t* cr, int note) const;
81
82         void queue_note_draw (int note);
83
84         bool handle_fixed_keys (GdkEventKey*);
85
86         void press_key (int key, int vel);
87         void release_key (int key);
88         void stop_sustained_notes ();
89         void stop_unsustained_notes ();
90
91         int  key_binding (const char* key) const;
92         void bind_key (const char* key, int note);
93         void clear_notes ();
94
95         void bind_keys_qwerty ();
96         void bind_keys_qwertz ();
97         void bind_keys_azerty ();
98         void bind_keys_dvorak ();
99
100         void bind_keys_basic_qwerty ();
101         void bind_keys_basic_qwertz ();
102
103         int get_note_for_xy (int x, int y) const;
104         int get_velocity_for_note_at_y (int note, int y) const;
105
106         int    is_black (int key) const;
107         double black_key_left_shift (int key) const;
108
109         void recompute_dimensions ();
110
111         struct PKNote {
112                 PKNote ()
113                         : pressed (false)
114                         , sustained (false)
115                         , white (false)
116                         , x (0)
117                         , w (0)
118                         , h (0)
119                 {}
120
121                 bool pressed;   /* true if key is in pressed down state. */
122                 bool sustained; /* true if note is sustained. */
123                 bool white;     /* true if key is white; 0 otherwise. */
124                 int  x;         /* Distance between the left edge of the key and the left edge of the widget, in pixels. */
125                 int  w;         /* Width of the key, in pixels. */
126                 int  h;         /* Height of the key, in pixels. */
127         };
128
129         bool _sustain_new_notes;
130         bool _highlight_grand_piano_range;
131         bool _annotate_layout;
132         bool _annotate_octave;
133         int  _octave;
134         int  _octave_range;
135         int  _note_being_pressed_using_mouse;
136         int  _min_note;
137         int  _max_note;
138         int  _last_key;
139         bool _monophonic;
140         int  _min_velocity;
141         int  _max_velocity;
142         int  _key_velocity;
143
144         PKNote _notes[NNOTES];
145
146         std::map<std::string, int> _key_bindings;  /**< Table used to translate from PC keyboard character to MIDI note number. */
147         std::map<int, std::string> _note_bindings; /**< Table to translate from MIDI note number to PC keyboard character. */
148         std::map<std::string, int> _note_stack;
149
150         /* these are only valid during expose/draw */
151         PangoFontDescription* _font_cue;
152         PangoFontDescription* _font_octave;
153 };
154
155 #endif