+ AUTHORS: updated
[calf.git] / src / calf / ctl_keyboard.h
blobce3fc611c5810fb6deeef568c53e3191c9ed1e64
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., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307, 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;
71 /// Null implementation of CalfKeyboard::EventSink
72 struct EventAdapter: public EventSink
74 CalfKeyboard *kb;
75 virtual void set_instance(CalfKeyboard *_kb) { kb = _kb; }
76 virtual bool pre_draw(cairo_t *c, KeyInfo &ki) { return false; }
77 virtual bool pre_draw_outline(cairo_t *c, KeyInfo &ki) { return false; }
78 virtual void post_draw(cairo_t *c, KeyInfo &ki) {}
79 virtual void post_all(cairo_t *c) {}
80 virtual void note_on(int note, int vel) {}
81 virtual void note_off(int note) {}
84 /// Debug/example implementation of CalfKeyboard::EventSink
85 struct EventTester: public EventAdapter
87 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; }
88 virtual void post_draw(cairo_t *c, KeyInfo &ki) {
89 if (ki.note % 12 != 0 && ki.note % 12 != 3 && ki.note % 12 != 7)
90 return;
91 cairo_rectangle(c, ki.x + ki.width / 2 - 2, ki.y + ki.height - 8, 4, 4);
92 if (ki.black)
93 cairo_set_source_rgb(c, 1.0, 1.0, 1.0);
94 else
95 cairo_set_source_rgb(c, 0.0, 0.0, 0.0);
96 cairo_fill(c);
98 virtual void note_on(int note, int vel) { g_message("note on %d %d", note, vel); }
99 virtual void note_off(int note) { g_message("note off %d", note); }
102 /// Parent instance members
103 GtkWidget parent;
104 /// Range (number of white keys = number of octaves * 7 + 1)
105 int nkeys;
106 EventSink *sink;
107 /// The note currently pressed via mouse selection
108 int last_key;
109 /// If true, the keyboard accepts mouse clicks and keys
110 bool interactive;
113 /// Class-specific data for CalfKeyboard
114 struct CalfKeyboardClass
116 /// Parent class members
117 GtkWidgetClass parent_class;
120 /// Create new keyboard object;
121 extern GtkWidget *calf_keyboard_new();
123 /// Return a GType for CalfKeyboard
124 extern GType calf_keyboard_get_type();
126 G_END_DECLS
128 #endif