cairo-ify the PianoKeyboard widget
[ardour.git] / gtk2_ardour / gtk_pianokeyboard.c
1 /*-
2  * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * This is piano_keyboard, piano keyboard-like GTK+ widget.  It contains
29  * no MIDI-specific code.
30  *
31  * For questions and comments, contact Edward Tomasz Napierala <trasz@FreeBSD.org>.
32  */
33
34 #include <assert.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <stdint.h>
38 #include <cairo/cairo.h>
39 #include <gtk/gtk.h>
40 #include <gdk/gdkkeysyms.h>
41
42 #include "gtk_pianokeyboard.h"
43
44 #define PIANO_KEYBOARD_DEFAULT_WIDTH 730
45 #define PIANO_KEYBOARD_DEFAULT_HEIGHT 70
46
47 enum {
48         NOTE_ON_SIGNAL,
49         NOTE_OFF_SIGNAL,
50         LAST_SIGNAL
51 };
52
53 static guint    piano_keyboard_signals[LAST_SIGNAL] = { 0 };
54
55 static void
56 draw_keyboard_cue(PianoKeyboard *pk, cairo_t* cr)
57 {
58         int             w = pk->notes[0].w;
59         int             h = pk->notes[0].h;
60
61         int             first_note_in_lower_row = (pk->octave + 5) * 12;
62         int             last_note_in_lower_row = (pk->octave + 6) * 12 - 1;
63         int             first_note_in_higher_row = (pk->octave + 6) * 12;
64         int             last_note_in_higher_row = (pk->octave + 7) * 12 + 4;
65
66         cairo_set_source_rgb (cr, 1.0f, 0.0f, 0.0f);
67         cairo_move_to (cr, pk->notes[first_note_in_lower_row].x + 3, h - 6);
68         cairo_line_to (cr, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
69         cairo_stroke (cr);
70
71         cairo_set_source_rgb (cr, 0.0f, 0.0f, 1.0f);
72         cairo_move_to (cr, pk->notes[first_note_in_higher_row].x + 3, h - 9);
73         cairo_line_to (cr, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
74         cairo_stroke (cr);
75 }
76
77 static void
78 queue_note_draw (PianoKeyboard* pk, int note)
79 {
80         GdkWindow* w = GTK_WIDGET(pk)->window;
81
82         if (w) {
83                 GdkRectangle r;
84
85                 r.x = pk->notes[note].x;
86                 r.y = 0;
87                 r.width = pk->notes[note].w;
88                 r.height = pk->notes[note].h;
89
90                 gdk_window_invalidate_rect (w, &r, TRUE);
91         }
92 }
93
94 static void 
95 draw_note(PianoKeyboard *pk, cairo_t* cr, int note)
96 {
97         int             is_white = pk->notes[note].white;
98
99         int             x = pk->notes[note].x;
100         int             w = pk->notes[note].w;
101         int             h = pk->notes[note].h;
102
103         if (pk->notes[note].pressed || pk->notes[note].sustained) {
104                 is_white = !is_white;
105         }
106
107         cairo_set_line_width (cr, 1.0);
108
109         if (is_white) {
110                 cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
111         } else {
112                 cairo_set_source_rgb (cr, 0.0f, 0.0f, 0.0f);
113         }
114
115         cairo_rectangle (cr, x, 0, w, h);
116         cairo_fill (cr);
117
118         cairo_set_source_rgb(cr, 0.0f, 0.0f, 0.0f); /* black outline */
119         cairo_rectangle (cr, x, 0, w, h);
120         cairo_stroke (cr);
121
122         if (pk->enable_keyboard_cue) {
123                 draw_keyboard_cue (pk, cr);
124         }
125
126         /* We need to redraw black keys that partially obscure the white one. */
127         if (note < NNOTES - 2 && !pk->notes[note + 1].white) {
128                 draw_note(pk, cr, note + 1);
129         }
130
131         if (note > 0 && !pk->notes[note - 1].white) {
132                 draw_note(pk, cr, note - 1);
133         }
134 }
135
136 static int 
137 press_key(PianoKeyboard *pk, int key)
138 {
139         assert(key >= 0);
140         assert(key < NNOTES);
141
142         pk->maybe_stop_sustained_notes = 0;
143
144         /* This is for keyboard autorepeat protection. */
145         if (pk->notes[key].pressed)
146                 return 0;
147
148         if (pk->sustain_new_notes)
149                 pk->notes[key].sustained = 1;
150         else
151                 pk->notes[key].sustained = 0;
152
153         pk->notes[key].pressed = 1;
154
155         g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
156         queue_note_draw(pk, key);
157
158         return 1;
159 }
160
161 static int 
162 release_key(PianoKeyboard *pk, int key)
163 {
164         assert(key >= 0);
165         assert(key < NNOTES);
166
167         pk->maybe_stop_sustained_notes = 0;
168
169         if (!pk->notes[key].pressed)
170                 return 0;
171
172         if (pk->sustain_new_notes)
173                 pk->notes[key].sustained = 1;
174
175         pk->notes[key].pressed = 0;
176
177         if (pk->notes[key].sustained)
178                 return 0;
179
180         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", key);
181         queue_note_draw(pk, key);
182
183         return 1;
184 }
185
186 static void 
187 stop_unsustained_notes(PianoKeyboard *pk)
188 {
189         int             i;
190
191         for (i = 0; i < NNOTES; i++) {
192                 if (pk->notes[i].pressed && !pk->notes[i].sustained) {
193                         pk->notes[i].pressed = 0;
194                         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
195                         queue_note_draw(pk, i);
196                 }
197         }
198 }
199
200 static void 
201 stop_sustained_notes(PianoKeyboard *pk)
202 {
203         int             i;
204
205         for (i = 0; i < NNOTES; i++) {
206                 if (pk->notes[i].sustained) {
207                         pk->notes[i].pressed = 0;
208                         pk->notes[i].sustained = 0;
209                         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
210                         queue_note_draw(pk, i);
211                 }
212         }
213 }
214
215 static int
216 key_binding(PianoKeyboard *pk, const char *key)
217 {
218         gpointer notused, note;
219         gboolean found;
220
221         assert(pk->key_bindings != NULL);
222
223         found = g_hash_table_lookup_extended(pk->key_bindings, key, &notused, &note);
224
225         if (!found)
226                 return -1;
227
228         return (intptr_t)note;
229 }
230
231 static void
232 bind_key(PianoKeyboard *pk, const char *key, int note)
233 {
234         assert(pk->key_bindings != NULL);
235
236         g_hash_table_insert(pk->key_bindings, (gpointer)key, (gpointer)((intptr_t)note));
237 }
238
239 static void
240 clear_notes(PianoKeyboard *pk)
241 {
242         assert(pk->key_bindings != NULL);
243
244         g_hash_table_remove_all(pk->key_bindings);
245 }
246
247 static void 
248 bind_keys_qwerty(PianoKeyboard *pk)
249 {
250         clear_notes(pk);
251
252         /* Lower keyboard row - "zxcvbnm". */
253         bind_key(pk, "z", 12);  /* C0 */
254         bind_key(pk, "s", 13);
255         bind_key(pk, "x", 14);
256         bind_key(pk, "d", 15);
257         bind_key(pk, "c", 16);
258         bind_key(pk, "v", 17);
259         bind_key(pk, "g", 18);
260         bind_key(pk, "b", 19);
261         bind_key(pk, "h", 20);
262         bind_key(pk, "n", 21);
263         bind_key(pk, "j", 22);
264         bind_key(pk, "m", 23);
265
266         /* Upper keyboard row, first octave - "qwertyu". */
267         bind_key(pk, "q", 24);
268         bind_key(pk, "2", 25);
269         bind_key(pk, "w", 26);
270         bind_key(pk, "3", 27);
271         bind_key(pk, "e", 28);
272         bind_key(pk, "r", 29);
273         bind_key(pk, "5", 30);
274         bind_key(pk, "t", 31);
275         bind_key(pk, "6", 32);
276         bind_key(pk, "y", 33);
277         bind_key(pk, "7", 34);
278         bind_key(pk, "u", 35);
279
280         /* Upper keyboard row, the rest - "iop". */
281         bind_key(pk, "i", 36);
282         bind_key(pk, "9", 37);
283         bind_key(pk, "o", 38);
284         bind_key(pk, "0", 39);
285         bind_key(pk, "p", 40);
286 }
287
288 static void 
289 bind_keys_qwertz(PianoKeyboard *pk)
290 {
291         bind_keys_qwerty(pk);
292
293         /* The only difference between QWERTY and QWERTZ is that the "y" and "z" are swapped together. */
294         bind_key(pk, "y", 12);
295         bind_key(pk, "z", 33);
296 }
297
298 static void
299 bind_keys_azerty(PianoKeyboard *pk)
300 {
301         clear_notes(pk);
302
303         /* Lower keyboard row - "wxcvbn,". */
304         bind_key(pk, "w", 12);  /* C0 */
305         bind_key(pk, "s", 13);
306         bind_key(pk, "x", 14);
307         bind_key(pk, "d", 15);
308         bind_key(pk, "c", 16);
309         bind_key(pk, "v", 17);
310         bind_key(pk, "g", 18);
311         bind_key(pk, "b", 19);
312         bind_key(pk, "h", 20);
313         bind_key(pk, "n", 21);
314         bind_key(pk, "j", 22);
315         bind_key(pk, "comma", 23);
316
317         /* Upper keyboard row, first octave - "azertyu". */
318         bind_key(pk, "a", 24);
319         bind_key(pk, "eacute", 25);
320         bind_key(pk, "z", 26);
321         bind_key(pk, "quotedbl", 27);
322         bind_key(pk, "e", 28);
323         bind_key(pk, "r", 29);
324         bind_key(pk, "parenleft", 30);
325         bind_key(pk, "t", 31);
326         bind_key(pk, "minus", 32);
327         bind_key(pk, "y", 33);
328         bind_key(pk, "egrave", 34);
329         bind_key(pk, "u", 35);
330
331         /* Upper keyboard row, the rest - "iop". */
332         bind_key(pk, "i", 36);
333         bind_key(pk, "ccedilla", 37);
334         bind_key(pk, "o", 38);
335         bind_key(pk, "agrave", 39);
336         bind_key(pk, "p", 40);
337 }
338
339 static gint 
340 keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
341 {
342         int             note;
343         char            *key;
344         guint           keyval;
345         GdkKeymapKey    kk;
346         PianoKeyboard   *pk = PIANO_KEYBOARD(mk);
347
348         /* We're not using event->keyval, because we need keyval with level set to 0.
349            E.g. if user holds Shift and presses '7', we want to get a '7', not '&'. */
350         kk.keycode = event->hardware_keycode;
351         kk.level = 0;
352         kk.group = 0;
353
354         keyval = gdk_keymap_lookup_key(NULL, &kk);
355
356         key = gdk_keyval_name(gdk_keyval_to_lower(keyval));
357
358         if (key == NULL) {
359                 g_message("gtk_keyval_name() returned NULL; please report this.");
360                 return FALSE;
361         }
362
363         note = key_binding(pk, key);
364
365         if (note < 0) {
366                 /* Key was not bound.  Maybe it's one of the keys handled in jack-keyboard.c. */
367                 return FALSE;
368         }
369
370         note += pk->octave * 12;
371
372         assert(note >= 0);
373         assert(note < NNOTES);
374
375         if (event->type == GDK_KEY_PRESS) {
376                 press_key(pk, note);
377
378         } else if (event->type == GDK_KEY_RELEASE) {
379                 release_key(pk, note);
380         }
381
382         return TRUE;
383 }
384
385 static int 
386 get_note_for_xy(PianoKeyboard *pk, int x, int y)
387 {
388         int             height = GTK_WIDGET(pk)->allocation.height;
389         int             note;
390
391         if (y <= height / 2) {
392                 for (note = 0; note < NNOTES - 1; note++) {
393                         if (pk->notes[note].white)
394                                 continue;
395
396                         if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
397                                 return note;
398                 }
399         }
400
401         for (note = 0; note < NNOTES - 1; note++) {
402                 if (!pk->notes[note].white)
403                         continue;
404
405                 if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
406                         return note;
407         }
408
409         return -1;
410 }
411
412 static gboolean 
413 mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer notused)
414 {
415         int             x = event->x;
416         int             y = event->y;
417
418         int             note = get_note_for_xy(pk, x, y);
419
420         if (event->button != 1)
421                 return TRUE;
422
423         if (event->type == GDK_BUTTON_PRESS) {
424                 /* This is possible when you make the window a little wider and then click
425                    on the grey area. */
426                 if (note < 0) {
427                         return TRUE;
428                 }
429
430                 if (pk->note_being_pressed_using_mouse >= 0)
431                         release_key(pk, pk->note_being_pressed_using_mouse);
432
433                 press_key(pk, note);
434                 pk->note_being_pressed_using_mouse = note;
435
436         } else if (event->type == GDK_BUTTON_RELEASE) {
437                 if (note >= 0) {
438                         release_key(pk, note);
439
440                 } else {
441                         if (pk->note_being_pressed_using_mouse >= 0)
442                                 release_key(pk, pk->note_being_pressed_using_mouse);
443                 }
444
445                 pk->note_being_pressed_using_mouse = -1;
446
447         }
448
449         return TRUE;
450 }
451
452 static gboolean 
453 mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer notused)
454 {
455         int             note;
456
457         if ((event->state & GDK_BUTTON1_MASK) == 0)
458                 return TRUE;
459
460         note = get_note_for_xy(pk, event->x, event->y);
461
462         if (note != pk->note_being_pressed_using_mouse && note >= 0) {
463                 
464                 if (pk->note_being_pressed_using_mouse >= 0)
465                         release_key(pk, pk->note_being_pressed_using_mouse);
466                 press_key(pk, note);
467                 pk->note_being_pressed_using_mouse = note;
468         }
469
470         return TRUE;
471 }
472
473 static gboolean
474 piano_keyboard_expose(GtkWidget *widget, GdkEventExpose *event)
475 {
476         int i;
477         PianoKeyboard *pk = PIANO_KEYBOARD(widget);
478         cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (GTK_WIDGET(pk)->window));
479         
480         gdk_cairo_region (cr, event->region);
481         cairo_clip (cr);
482
483         for (i = 0; i < NNOTES; i++) {
484                 GdkRectangle r;
485
486                 r.x = pk->notes[i].x;
487                 r.y = 0;
488                 r.width = pk->notes[i].w;
489                 r.height = pk->notes[i].h;
490
491                 switch (gdk_region_rect_in (event->region, &r)) {
492                 case GDK_OVERLAP_RECTANGLE_PART:
493                 case GDK_OVERLAP_RECTANGLE_IN:
494                         draw_note (pk, cr, i);
495                         break;
496                 default:
497                         break;
498                 }
499         }
500
501         cairo_destroy (cr);
502
503         return TRUE;
504 }
505
506 static void 
507 piano_keyboard_size_request(GtkWidget *widget, GtkRequisition *requisition)
508 {
509         requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
510         requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
511 }
512
513 static void
514 recompute_dimensions(PianoKeyboard *pk)
515 {
516         int             number_of_white_keys = (NNOTES - 1) * (7.0 / 12.0);
517
518         int             key_width;
519         int             black_key_width;
520         int             useful_width;
521
522         int             note;
523         int             white_key = 0;
524         int             note_in_octave;
525
526         int             width = GTK_WIDGET(pk)->allocation.width;
527         int             height = GTK_WIDGET(pk)->allocation.height;
528
529         key_width = width / number_of_white_keys;
530         black_key_width = key_width * 0.8;
531         useful_width = number_of_white_keys * key_width;
532         pk->widget_margin = (width - useful_width) / 2;
533
534         for (note = 0, white_key = 0; note < NNOTES - 2; note++) {
535                 note_in_octave = note % 12;
536
537                 if (note_in_octave == 1 || note_in_octave == 3 || note_in_octave == 6 ||
538                         note_in_octave == 8 || note_in_octave == 10) {
539
540                         /* This note is black key. */
541                         pk->notes[note].x = pk->widget_margin + white_key * key_width - black_key_width / 2;
542                         pk->notes[note].w = black_key_width;
543                         pk->notes[note].h = height / 2;
544                         pk->notes[note].white = 0;
545
546                         continue;
547                 }
548
549                 /* This note is white key. */
550                 pk->notes[note].x = pk->widget_margin + white_key * key_width;
551                 pk->notes[note].w = key_width;
552                 pk->notes[note].h = height;
553                 pk->notes[note].white = 1;
554
555                 white_key++;
556         }
557 }
558
559 static void
560 piano_keyboard_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
561 {
562         /* XXX: Are these two needed? */
563         g_return_if_fail(widget != NULL);
564         g_return_if_fail(allocation != NULL);
565
566         widget->allocation = *allocation;
567
568         recompute_dimensions(PIANO_KEYBOARD(widget));
569
570         if (GTK_WIDGET_REALIZED(widget)) {
571                 gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height);
572         }
573 }
574
575 static void
576 piano_keyboard_class_init(PianoKeyboardClass *klass)
577 {
578         GtkWidgetClass  *widget_klass;
579
580         /* Set up signals. */
581         piano_keyboard_signals[NOTE_ON_SIGNAL] = g_signal_new ("note-on",
582                 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583                 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
584
585         piano_keyboard_signals[NOTE_OFF_SIGNAL] = g_signal_new ("note-off",
586                 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
587                 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
588
589         widget_klass = (GtkWidgetClass*) klass; 
590
591         widget_klass->expose_event = piano_keyboard_expose;
592         widget_klass->size_request = piano_keyboard_size_request;
593         widget_klass->size_allocate = piano_keyboard_size_allocate;
594 }
595
596 static void
597 piano_keyboard_init(GtkWidget *mk)
598 {
599         gtk_widget_add_events(mk, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
600
601         g_signal_connect(G_OBJECT(mk), "button-press-event", G_CALLBACK(mouse_button_event_handler), NULL);
602         g_signal_connect(G_OBJECT(mk), "button-release-event", G_CALLBACK(mouse_button_event_handler), NULL);
603         g_signal_connect(G_OBJECT(mk), "motion-notify-event", G_CALLBACK(mouse_motion_event_handler), NULL);
604         g_signal_connect(G_OBJECT(mk), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
605         g_signal_connect(G_OBJECT(mk), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
606 }
607
608 GType
609 piano_keyboard_get_type(void)
610 {
611         static GType mk_type = 0;
612
613         if (!mk_type) {
614                 static const GTypeInfo mk_info = {
615                         sizeof(PianoKeyboardClass),
616                         NULL, /* base_init */
617                         NULL, /* base_finalize */
618                         (GClassInitFunc) piano_keyboard_class_init,
619                         NULL, /* class_finalize */
620                         NULL, /* class_data */
621                         sizeof (PianoKeyboard),
622                         0,    /* n_preallocs */
623                         (GInstanceInitFunc) piano_keyboard_init,
624                 };
625
626                 mk_type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "PianoKeyboard", &mk_info, 0);
627         }
628
629         return mk_type;
630 }
631
632 GtkWidget *
633 piano_keyboard_new(void)
634 {
635         GtkWidget *widget = gtk_type_new(piano_keyboard_get_type());
636
637         PianoKeyboard *pk = PIANO_KEYBOARD(widget);
638
639         pk->maybe_stop_sustained_notes = 0;
640         pk->sustain_new_notes = 0;
641         pk->enable_keyboard_cue = 0;
642         pk->octave = 4;
643         pk->note_being_pressed_using_mouse = -1;
644         memset((void *)pk->notes, 0, sizeof(struct Note) * NNOTES);
645         pk->key_bindings = g_hash_table_new(g_str_hash, g_str_equal);
646         bind_keys_qwerty(pk);
647
648         return widget;
649 }
650
651 void
652 piano_keyboard_set_keyboard_cue(PianoKeyboard *pk, int enabled)
653 {
654         pk->enable_keyboard_cue = enabled;
655 }
656
657 void
658 piano_keyboard_sustain_press(PianoKeyboard *pk)
659 {
660         if (!pk->sustain_new_notes) {
661                 pk->sustain_new_notes = 1;
662                 pk->maybe_stop_sustained_notes = 1;
663         }
664 }
665
666 void    
667 piano_keyboard_sustain_release(PianoKeyboard *pk)
668 {
669         if (pk->maybe_stop_sustained_notes)
670                 stop_sustained_notes(pk);
671
672         pk->sustain_new_notes = 0;
673 }
674
675 void
676 piano_keyboard_set_note_on(PianoKeyboard *pk, int note)
677 {
678         if (pk->notes[note].pressed == 0) {
679                 pk->notes[note].pressed = 1;
680                 queue_note_draw (pk, note);
681         }
682 }
683
684 void
685 piano_keyboard_set_note_off(PianoKeyboard *pk, int note)
686 {
687         if (pk->notes[note].pressed || pk->notes[note].sustained) {
688                 pk->notes[note].pressed = 0;
689                 pk->notes[note].sustained = 0;
690                 queue_note_draw (pk, note);
691         }
692 }
693
694 void
695 piano_keyboard_set_octave(PianoKeyboard *pk, int octave)
696 {
697         stop_unsustained_notes(pk);
698         pk->octave = octave;
699         gtk_widget_queue_draw(GTK_WIDGET(pk));
700 }
701
702 gboolean
703 piano_keyboard_set_keyboard_layout(PianoKeyboard *pk, const char *layout)
704 {
705         assert(layout);
706
707         if (!strcasecmp(layout, "QWERTY")) {
708                 bind_keys_qwerty(pk);
709
710         } else if (!strcasecmp(layout, "QWERTZ")) {
711                 bind_keys_qwertz(pk);
712
713         } else if (!strcasecmp(layout, "AZERTY")) {
714                 bind_keys_azerty(pk);
715
716         } else {
717                 /* Unknown layout name. */
718                 return TRUE;
719         }
720
721         return FALSE;
722 }
723