Make Analyzer UI require instance-access
[calf.git] / src / calf / ctl_keyboard.h
blob5566ad047f23c5c81a4acf1be9671468c2addd29
1 /* Calf DSP Library
2 * Barely started keyboard widget. Planned to be usable as
3 * a ruler for curves, and possibly as input widget in future
4 * as well (that's what event sink interface is for, at least).
6 * Copyright (C) 2008 Krzysztof Foltman
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA
23 #ifndef CALF_CTL_KEYBOARD_H
24 #define CALF_CTL_KEYBOARD_H
26 #include <gtk/gtk.h>
28 G_BEGIN_DECLS
30 #define CALF_TYPE_KEYBOARD (calf_keyboard_get_type())
31 #define CALF_KEYBOARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CALF_TYPE_KEYBOARD, CalfKeyboard))
32 #define CALF_IS_KEYBOARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CALF_TYPE_KEYBOARD))
33 #define CALF_KEYBOARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CALF_TYPE_KEYBOARD, CalfKeyboardClass))
34 #define CALF_IS_KEYBOARD_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((klass), CALF_TYPE_KEYBOARD))
36 /// Instance-specific data for CalfKeyboard
37 struct CalfKeyboard
39 /// Structure with information needed for drawing a single key
40 struct KeyInfo
42 double x; ///< X coordinate of the top-left point of the key
43 double y; ///< Y coordinate of the top-left point of the key
44 double width; ///< key width
45 double height; ///< key height
46 int note; ///< MIDI note number
47 bool black; ///< true if it's a black key, false if it's a white key
50 /// Set of user-defined callbacks for customizing display and operation of CalfKeyboard
51 struct EventSink
53 /// (will be) called on attachment of sink to CalfKeyboard object
54 virtual void set_instance(CalfKeyboard *kb)=0;
55 /// called before drawing key interior
56 /// @retval true do not draw the key
57 virtual bool pre_draw(cairo_t *c, KeyInfo &ki)=0;
58 /// @retval true do not draw the outline
59 /// called before drawing key outline of white keys
60 virtual bool pre_draw_outline(cairo_t *c, KeyInfo &ki)=0;
61 /// called after key is drawn using standard method (but not if drawing is skipped)
62 virtual void post_draw(cairo_t *c, KeyInfo &ki)=0;
63 /// called after key is drawn
64 virtual void post_all(cairo_t *c)=0;
65 /// key was pressed
66 virtual void note_on(int note, int vel) = 0;
67 /// key was released
68 virtual void note_off(int note) = 0;
70 virtual ~EventSink() {}
73 /// Null implementation of CalfKeyboard::EventSink
74 struct EventAdapter: public EventSink
76 CalfKeyboard *kb;
77 virtual void set_instance(CalfKeyboard *_kb) { kb = _kb; }
78 virtual bool pre_draw(cairo_t *c, KeyInfo &ki) { return false; }
79 virtual bool pre_draw_outline(cairo_t *c, KeyInfo &ki) { return false; }
80 virtual void post_draw(cairo_t *c, KeyInfo &ki) {}
81 virtual void post_all(cairo_t *c) {}
82 virtual void note_on(int note, int vel) {}
83 virtual void note_off(int note) {}
86 /// Debug/example implementation of CalfKeyboard::EventSink
87 struct EventTester: public EventAdapter
89 virtual bool pre_draw(cairo_t *c, KeyInfo &ki) { if (ki.note == 60) cairo_set_source_rgb(c, 1.0, 1.0, 0.5); return false; }
90 virtual void post_draw(cairo_t *c, KeyInfo &ki) {
91 if (ki.note % 12 != 0 && ki.note % 12 != 3 && ki.note % 12 != 7)
92 return;
93 cairo_rectangle(c, ki.x + ki.width / 2 - 2, ki.y + ki.height - 8, 4, 4);
94 if (ki.black)
95 cairo_set_source_rgb(c, 1.0, 1.0, 1.0);
96 else
97 cairo_set_source_rgb(c, 0.0, 0.0, 0.0);
98 cairo_fill(c);
100 virtual void note_on(int note, int vel) { g_message("note on %d %d", note, vel); }
101 virtual void note_off(int note) { g_message("note off %d", note); }
104 /// Parent instance members
105 GtkWidget parent;
106 /// Range (number of white keys = number of octaves * 7 + 1)
107 int nkeys;
108 EventSink *sink;
109 /// The note currently pressed via mouse selection
110 int last_key;
111 /// If true, the keyboard accepts mouse clicks and keys
112 bool interactive;
115 /// Class-specific data for CalfKeyboard
116 struct CalfKeyboardClass
118 /// Parent class members
119 GtkWidgetClass parent_class;
122 /// Create new keyboard object;
123 extern GtkWidget *calf_keyboard_new();
125 /// Return a GType for CalfKeyboard
126 extern GType calf_keyboard_get_type();
128 G_END_DECLS
130 #endif