fixes for 98% of all the warnings/errors reported by OS X gcc on tiger
[ardour.git] / gtk2_ardour / gtk_pianokeyboard.c
index 58e742e32c4e7e9b3e6d2781b64bfffc16efc197..7332299bfbff40c5fe40b4778bf4476b8b6085d4 100644 (file)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2007, 2008 Edward Tomasz Napierała <trasz@FreeBSD.org>
+ * Copyright (c) 2007, 2008 Edward Tomasz Napierała <trasz@FreeBSD.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <strings.h>
 #include <stdint.h>
+#include <cairo/cairo.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
 enum {
        NOTE_ON_SIGNAL,
        NOTE_OFF_SIGNAL,
+       REST_SIGNAL,
        LAST_SIGNAL
 };
 
 static guint   piano_keyboard_signals[LAST_SIGNAL] = { 0 };
 
 static void
-draw_keyboard_cue(PianoKeyboard *pk)
+draw_keyboard_cue(PianoKeyboard *pk, cairo_t* cr)
 {
        int             w = pk->notes[0].w;
        int             h = pk->notes[0].h;
 
-       GdkGC          *gc = GTK_WIDGET(pk)->style->fg_gc[0];
-
        int             first_note_in_lower_row = (pk->octave + 5) * 12;
        int             last_note_in_lower_row = (pk->octave + 6) * 12 - 1;
        int             first_note_in_higher_row = (pk->octave + 6) * 12;
        int             last_note_in_higher_row = (pk->octave + 7) * 12 + 4;
 
-       gdk_draw_line(GTK_WIDGET(pk)->window, gc, pk->notes[first_note_in_lower_row].x + 3,
-                       h - 6, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
+       cairo_set_source_rgb (cr, 1.0f, 0.0f, 0.0f);
+       cairo_move_to (cr, pk->notes[first_note_in_lower_row].x + 3, h - 6);
+       cairo_line_to (cr, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
+       cairo_stroke (cr);
 
-       gdk_draw_line(GTK_WIDGET(pk)->window, gc, pk->notes[first_note_in_higher_row].x + 3,
-                       h - 9, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
+       cairo_set_source_rgb (cr, 0.0f, 0.0f, 1.0f);
+       cairo_move_to (cr, pk->notes[first_note_in_higher_row].x + 3, h - 9);
+       cairo_line_to (cr, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
+       cairo_stroke (cr);
 }
 
-static void 
-draw_note(PianoKeyboard *pk, int note)
+static void
+queue_note_draw (PianoKeyboard* pk, int note)
 {
-       GdkColor        black = {0, 0, 0, 0};
-       GdkColor        white = {0, 65535, 65535, 65535};
+       GdkWindow* w = GTK_WIDGET(pk)->window;
 
-       GdkGC           *gc = GTK_WIDGET(pk)->style->fg_gc[0];
-       GtkWidget       *widget;
+       if (w) {
+               GdkRectangle r;
 
+               r.x = pk->notes[note].x;
+               r.y = 0;
+               r.width = pk->notes[note].w;
+               r.height = pk->notes[note].h;
+
+               gdk_window_invalidate_rect (w, &r, TRUE);
+       }
+}
+
+static void
+draw_note(PianoKeyboard *pk, cairo_t* cr, int note)
+{
        int             is_white = pk->notes[note].white;
 
        int             x = pk->notes[note].x;
        int             w = pk->notes[note].w;
        int             h = pk->notes[note].h;
 
-       if (pk->notes[note].pressed || pk->notes[note].sustained)
-               is_white = !is_white;
+       if (pk->notes[note].pressed || pk->notes[note].sustained) {
+               if (is_white) {
+                       cairo_set_source_rgb (cr, 0.60f, 0.60f, 0.60f);
+               } else {
+                       cairo_set_source_rgb (cr, 0.50f, 0.50f, 0.50f);
+               }
+       } else {
+               if (is_white) {
+                       cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
+               } else {
+                       cairo_set_source_rgb (cr, 0.0f, 0.0f, 0.0f);
+               }
+       }
+
+       cairo_set_line_width (cr, 1.0);
 
-       if (is_white)
-               gdk_gc_set_rgb_fg_color(gc, &white);
-       else
-               gdk_gc_set_rgb_fg_color(gc, &black);
+       cairo_rectangle (cr, x, 0, w, h);
+       cairo_fill (cr);
 
-       gdk_draw_rectangle(GTK_WIDGET(pk)->window, gc, TRUE, x, 0, w, h);
-       gdk_gc_set_rgb_fg_color(gc, &black);
-       gdk_draw_rectangle(GTK_WIDGET(pk)->window, gc, FALSE, x, 0, w, h);
+       cairo_set_source_rgb(cr, 0.0f, 0.0f, 0.0f); /* black outline */
+       cairo_rectangle (cr, x, 0, w, h);
+       cairo_stroke (cr);
 
-       if (pk->enable_keyboard_cue)
-               draw_keyboard_cue(pk);
+       if (pk->enable_keyboard_cue) {
+               draw_keyboard_cue (pk, cr);
+       }
 
        /* We need to redraw black keys that partially obscure the white one. */
-       if (note < NNOTES - 2 && !pk->notes[note + 1].white)
-               draw_note(pk, note + 1);
-
-       if (note > 0 && !pk->notes[note - 1].white)
-               draw_note(pk, note - 1);
+       if (note < NNOTES - 2 && !pk->notes[note + 1].white) {
+               draw_note(pk, cr, note + 1);
+       }
 
-       /*
-        * XXX: This doesn't really belong here.  Originally I wanted to pack PianoKeyboard into GtkFrame
-        * packed into GtkAlignment.  I failed to make it behave the way I want.  GtkFrame would need
-        * to adapt to the "proper" size of PianoKeyboard, i.e. to the useful_width, not allocated width;
-        * that didn't work.
-        */
-       widget = GTK_WIDGET(pk);
-       gtk_paint_shadow(widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, NULL, widget, NULL, pk->widget_margin, 0,
-               widget->allocation.width - pk->widget_margin * 2 + 1, widget->allocation.height);
+       if (note > 0 && !pk->notes[note - 1].white) {
+               draw_note(pk, cr, note - 1);
+       }
 }
 
-static int 
+static int
 press_key(PianoKeyboard *pk, int key)
 {
        assert(key >= 0);
@@ -139,12 +158,12 @@ press_key(PianoKeyboard *pk, int key)
        pk->notes[key].pressed = 1;
 
        g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
-       draw_note(pk, key);
+       queue_note_draw(pk, key);
 
        return 1;
 }
 
-static int 
+static int
 release_key(PianoKeyboard *pk, int key)
 {
        assert(key >= 0);
@@ -164,12 +183,18 @@ release_key(PianoKeyboard *pk, int key)
                return 0;
 
        g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", key);
-       draw_note(pk, key);
+       queue_note_draw(pk, key);
 
        return 1;
 }
 
-static void 
+static void
+rest (PianoKeyboard* pk)
+{
+       g_signal_emit_by_name(GTK_WIDGET(pk), "rest");
+}
+
+static void
 stop_unsustained_notes(PianoKeyboard *pk)
 {
        int             i;
@@ -178,12 +203,12 @@ stop_unsustained_notes(PianoKeyboard *pk)
                if (pk->notes[i].pressed && !pk->notes[i].sustained) {
                        pk->notes[i].pressed = 0;
                        g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
-                       draw_note(pk, i);
+                       queue_note_draw(pk, i);
                }
        }
 }
 
-static void 
+static void
 stop_sustained_notes(PianoKeyboard *pk)
 {
        int             i;
@@ -193,7 +218,7 @@ stop_sustained_notes(PianoKeyboard *pk)
                        pk->notes[i].pressed = 0;
                        pk->notes[i].sustained = 0;
                        g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
-                       draw_note(pk, i);
+                       queue_note_draw(pk, i);
                }
        }
 }
@@ -230,11 +255,13 @@ clear_notes(PianoKeyboard *pk)
        g_hash_table_remove_all(pk->key_bindings);
 }
 
-static void 
+static void
 bind_keys_qwerty(PianoKeyboard *pk)
 {
        clear_notes(pk);
 
+       bind_key(pk, "space", 128);
+
        /* Lower keyboard row - "zxcvbnm". */
        bind_key(pk, "z", 12);  /* C0 */
        bind_key(pk, "s", 13);
@@ -271,7 +298,7 @@ bind_keys_qwerty(PianoKeyboard *pk)
        bind_key(pk, "p", 40);
 }
 
-static void 
+static void
 bind_keys_qwertz(PianoKeyboard *pk)
 {
        bind_keys_qwerty(pk);
@@ -286,6 +313,8 @@ bind_keys_azerty(PianoKeyboard *pk)
 {
        clear_notes(pk);
 
+       bind_key(pk, "space", 128);
+
        /* Lower keyboard row - "wxcvbn,". */
        bind_key(pk, "w", 12);  /* C0 */
        bind_key(pk, "s", 13);
@@ -322,8 +351,8 @@ bind_keys_azerty(PianoKeyboard *pk)
        bind_key(pk, "p", 40);
 }
 
-static gint 
-keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
+static gint
+keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer ignored)
 {
        int             note;
        char            *key;
@@ -353,6 +382,14 @@ keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
                return FALSE;
        }
 
+       if (note == 128) {
+               if (event->type == GDK_KEY_RELEASE) {
+                       rest (pk);
+               }
+
+               return TRUE;
+       }
+
        note += pk->octave * 12;
 
        assert(note >= 0);
@@ -368,7 +405,7 @@ keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
        return TRUE;
 }
 
-static int 
+static int
 get_note_for_xy(PianoKeyboard *pk, int x, int y)
 {
        int             height = GTK_WIDGET(pk)->allocation.height;
@@ -395,8 +432,8 @@ get_note_for_xy(PianoKeyboard *pk, int x, int y)
        return -1;
 }
 
-static gboolean 
-mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer notused)
+static gboolean
+mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer ignored)
 {
        int             x = event->x;
        int             y = event->y;
@@ -435,8 +472,8 @@ mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer no
        return TRUE;
 }
 
-static gboolean 
-mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer notused)
+static gboolean
+mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer ignored)
 {
        int             note;
 
@@ -446,7 +483,7 @@ mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer no
        note = get_note_for_xy(pk, event->x, event->y);
 
        if (note != pk->note_being_pressed_using_mouse && note >= 0) {
-               
+
                if (pk->note_being_pressed_using_mouse >= 0)
                        release_key(pk, pk->note_being_pressed_using_mouse);
                press_key(pk, note);
@@ -461,15 +498,36 @@ piano_keyboard_expose(GtkWidget *widget, GdkEventExpose *event)
 {
        int i;
        PianoKeyboard *pk = PIANO_KEYBOARD(widget);
+       cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (GTK_WIDGET(pk)->window));
+
+       gdk_cairo_region (cr, event->region);
+       cairo_clip (cr);
+
+       for (i = 0; i < NNOTES; i++) {
+               GdkRectangle r;
+
+               r.x = pk->notes[i].x;
+               r.y = 0;
+               r.width = pk->notes[i].w;
+               r.height = pk->notes[i].h;
+
+               switch (gdk_region_rect_in (event->region, &r)) {
+               case GDK_OVERLAP_RECTANGLE_PART:
+               case GDK_OVERLAP_RECTANGLE_IN:
+                       draw_note (pk, cr, i);
+                       break;
+               default:
+                       break;
+               }
+       }
 
-       for (i = 0; i < NNOTES; i++)
-               draw_note(pk, i);
+       cairo_destroy (cr);
 
        return TRUE;
 }
 
-static void 
-piano_keyboard_size_request(GtkWidget *widget, GtkRequisition *requisition)
+static void
+piano_keyboard_size_request(GtkWidgetwidget, GtkRequisition *requisition)
 {
        requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
        requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
@@ -551,7 +609,11 @@ piano_keyboard_class_init(PianoKeyboardClass *klass)
                G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
 
-       widget_klass = (GtkWidgetClass*) klass; 
+       piano_keyboard_signals[REST_SIGNAL] = g_signal_new ("rest",
+                G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
+                0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+
+       widget_klass = (GtkWidgetClass*) klass;
 
        widget_klass->expose_event = piano_keyboard_expose;
        widget_klass->size_request = piano_keyboard_size_request;
@@ -566,8 +628,8 @@ piano_keyboard_init(GtkWidget *mk)
        g_signal_connect(G_OBJECT(mk), "button-press-event", G_CALLBACK(mouse_button_event_handler), NULL);
        g_signal_connect(G_OBJECT(mk), "button-release-event", G_CALLBACK(mouse_button_event_handler), NULL);
        g_signal_connect(G_OBJECT(mk), "motion-notify-event", G_CALLBACK(mouse_motion_event_handler), NULL);
-        g_signal_connect(G_OBJECT(mk), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
-        g_signal_connect(G_OBJECT(mk), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
+       g_signal_connect(G_OBJECT(mk), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
+       g_signal_connect(G_OBJECT(mk), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
 }
 
 GType
@@ -586,6 +648,7 @@ piano_keyboard_get_type(void)
                        sizeof (PianoKeyboard),
                        0,    /* n_preallocs */
                        (GInstanceInitFunc) piano_keyboard_init,
+                        0,    /* value_table */
                };
 
                mk_type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "PianoKeyboard", &mk_info, 0);
@@ -628,7 +691,7 @@ piano_keyboard_sustain_press(PianoKeyboard *pk)
        }
 }
 
-void   
+void
 piano_keyboard_sustain_release(PianoKeyboard *pk)
 {
        if (pk->maybe_stop_sustained_notes)
@@ -642,7 +705,7 @@ piano_keyboard_set_note_on(PianoKeyboard *pk, int note)
 {
        if (pk->notes[note].pressed == 0) {
                pk->notes[note].pressed = 1;
-               draw_note(pk, note);
+               queue_note_draw (pk, note);
        }
 }
 
@@ -652,7 +715,7 @@ piano_keyboard_set_note_off(PianoKeyboard *pk, int note)
        if (pk->notes[note].pressed || pk->notes[note].sustained) {
                pk->notes[note].pressed = 0;
                pk->notes[note].sustained = 0;
-               draw_note(pk, note);
+               queue_note_draw (pk, note);
        }
 }
 
@@ -685,4 +748,3 @@ piano_keyboard_set_keyboard_layout(PianoKeyboard *pk, const char *layout)
 
        return FALSE;
 }
-