Enable Menu > Quit to work again after startup on macOS
[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
39         enum Layout {
40                 QWERTY,
41                 QWERTZ,
42                 AZERTY,
43                 DVORAK,
44                 S_QWERTY,
45                 S_QWERTZ
46         };
47
48         void sustain_press ();
49         void sustain_release ();
50
51         void set_note_on (int note);
52         void set_note_off (int note);
53
54         void set_grand_piano_highlight (bool enabled);
55         void set_annotate_layout (bool enabled);
56         void set_annotate_octave (bool enabled);
57
58         void set_monophonic (bool monophonic);
59         void set_octave (int octave);
60         void set_octave_range (int octave_range);
61         void set_keyboard_layout (Layout layout);
62         void set_velocities (int min_vel, int max_vel, int key_vel);
63
64 protected:
65         bool on_key_press_event (GdkEventKey*);
66         bool on_key_release_event (GdkEventKey*);
67         bool on_button_press_event (GdkEventButton*);
68         bool on_button_release_event (GdkEventButton*);
69         bool on_motion_notify_event (GdkEventMotion*);
70         bool on_expose_event (GdkEventExpose*);
71
72         void on_size_request (Gtk::Requisition*);
73         void on_size_allocate (Gtk::Allocation&);
74
75 private:
76         void annotate_layout (cairo_t* cr, int note) const;
77         void annotate_note (cairo_t* cr, int note) const;
78         void draw_note (cairo_t* cr, int note) const;
79
80         void queue_note_draw (int note);
81
82         void press_key (int key, int vel);
83         void release_key (int key);
84         void stop_sustained_notes ();
85         void stop_unsustained_notes ();
86
87         int  key_binding (const char* key) const;
88         void bind_key (const char* key, int note);
89         void clear_notes ();
90
91         void bind_keys_qwerty ();
92         void bind_keys_qwertz ();
93         void bind_keys_azerty ();
94         void bind_keys_dvorak ();
95
96         void bind_keys_basic_qwerty ();
97         void bind_keys_basic_qwertz ();
98
99         int get_note_for_xy (int x, int y) const;
100         int get_velocity_for_note_at_y (int note, int y) const;
101
102         int    is_black (int key) const;
103         double black_key_left_shift (int key) const;
104
105         void recompute_dimensions ();
106
107         struct PKNote {
108                 PKNote ()
109                         : pressed (false)
110                         , sustained (false)
111                         , white (false)
112                         , x (0)
113                         , w (0)
114                         , h (0)
115                 {}
116
117                 bool pressed;   /* true if key is in pressed down state. */
118                 bool sustained; /* true if note is sustained. */
119                 bool white;     /* true if key is white; 0 otherwise. */
120                 int  x;         /* Distance between the left edge of the key and the left edge of the widget, in pixels. */
121                 int  w;         /* Width of the key, in pixels. */
122                 int  h;         /* Height of the key, in pixels. */
123         };
124
125         bool _sustain_new_notes;
126         bool _highlight_grand_piano_range;
127         bool _annotate_layout;
128         bool _annotate_octave;
129         int  _octave;
130         int  _octave_range;
131         int  _note_being_pressed_using_mouse;
132         int  _min_note;
133         int  _max_note;
134         int  _last_key;
135         bool _monophonic;
136         int  _min_velocity;
137         int  _max_velocity;
138         int  _key_velocity;
139
140         PKNote _notes[NNOTES];
141
142         std::map<std::string, int> _key_bindings;  /**< Table used to translate from PC keyboard character to MIDI note number. */
143         std::map<int, std::string> _note_bindings; /**< Table to translate from MIDI note number to PC keyboard character. */
144         std::map<std::string, int> _note_stack;
145
146         /* these are only valid during expose/draw */
147         PangoFontDescription* _font_cue;
148         PangoFontDescription* _font_octave;
149 };
150
151 #endif