Add a missing #define to our MSVC project (portaudio_backend)
[ardour.git] / gtk2_ardour / piano_roll_header.cc
index 9d0f1e504a3d2dae51a28674c1e54e18a76338f9..043f6c546e95a61d0ddf161b4faab79cf5671665 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2008 Paul Davis 
+    Copyright (C) 2008 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 */
 
 #include <iostream>
-#include <evoral/midi_events.h>
-#include <ardour/midi_track.h>
+#include "evoral/midi_events.h"
+#include "ardour/midi_track.h"
 
+#include "gtkmm2ext/keyboard.h"
+
+#include "editing.h"
 #include "piano_roll_header.h"
 #include "midi_time_axis.h"
 #include "midi_streamview.h"
-
-const int no_note = 0xff;
+#include "public_editor.h"
 
 using namespace std;
+using namespace Gtkmm2ext;
 
 PianoRollHeader::Color PianoRollHeader::white = PianoRollHeader::Color(0.77f, 0.78f, 0.76f);
-PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(0.87f, 0.88f, 0.86f);
+PianoRollHeader::Color PianoRollHeader::white_highlight = PianoRollHeader::Color(1.00f, 0.40f, 0.40f);
 PianoRollHeader::Color PianoRollHeader::white_shade_light = PianoRollHeader::Color(0.95f, 0.95f, 0.95f);
 PianoRollHeader::Color PianoRollHeader::white_shade_dark = PianoRollHeader::Color(0.56f, 0.56f, 0.56f);
 
 PianoRollHeader::Color PianoRollHeader::black = PianoRollHeader::Color(0.24f, 0.24f, 0.24f);
-PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.30f, 0.30f, 0.30f);
+PianoRollHeader::Color PianoRollHeader::black_highlight = PianoRollHeader::Color(0.60f, 0.10f, 0.10f);
 PianoRollHeader::Color PianoRollHeader::black_shade_light = PianoRollHeader::Color(0.46f, 0.46f, 0.46f);
 PianoRollHeader::Color PianoRollHeader::black_shade_dark = PianoRollHeader::Color(0.1f, 0.1f, 0.1f);
 
@@ -60,10 +63,10 @@ PianoRollHeader::Color::set(const PianoRollHeader::Color& c)
        b = c.b;
 }
 
-PianoRollHeader::PianoRollHeader(MidiStreamView& v) 
-       : _view(v) 
-       , _highlighted_note(no_note)
-       , _clicked_note(no_note)
+PianoRollHeader::PianoRollHeader(MidiStreamView& v)
+       : _view(v)
+       , _highlighted_note(NO_MIDI_NOTE)
+       , _clicked_note(NO_MIDI_NOTE)
        , _dragging(false)
 {
        add_events (Gdk::BUTTON_PRESS_MASK |
@@ -77,7 +80,7 @@ PianoRollHeader::PianoRollHeader(MidiStreamView& v)
                _active_notes[i] = false;
        }
 
-       _view.NoteRangeChanged.connect (mem_fun (*this, &PianoRollHeader::note_range_changed));
+       _view.NoteRangeChanged.connect (sigc::mem_fun (*this, &PianoRollHeader::note_range_changed));
 }
 
 inline void
@@ -90,8 +93,8 @@ create_path(Cairo::RefPtr<Cairo::Context> cr, double x[], double y[], int start,
        }
 }
 
-inline void 
-render_rect(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
+inline void
+render_rect(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
             PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
 {
        cr->set_source_rgb(bg.r, bg.g, bg.b);
@@ -108,7 +111,7 @@ render_rect(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
 }
 
 inline void
-render_cf(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
+render_cf(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
                PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
 {
        cr->set_source_rgb(bg.r, bg.g, bg.b);
@@ -125,7 +128,7 @@ render_cf(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
 }
 
 inline void
-render_eb(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
+render_eb(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
                PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
 {
        cr->set_source_rgb(bg.r, bg.g, bg.b);
@@ -146,7 +149,7 @@ render_eb(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
 }
 
 inline void
-render_dga(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
+render_dga(Cairo::RefPtr<Cairo::Context> cr, int /*note*/, double x[], double y[],
                 PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow)
 {
        cr->set_source_rgb(bg.r, bg.g, bg.b);
@@ -169,16 +172,16 @@ render_dga(Cairo::RefPtr<Cairo::Context> cr, int note, double x[], double y[],
 void
 PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[])
 {
-       double y_pos = floor(_view.note_to_y(note)) - 0.5f;
+       double y_pos = floor(_view.note_to_y(note)) + 1.5f;
        double note_height;
-       double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 0.5f;
-       double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) - 1.0f;
+       double other_y1 = floor(_view.note_to_y(note+1)) + floor(_note_height / 2.0f) + 2.5f;
+       double other_y2 = floor(_view.note_to_y(note-1)) + floor(_note_height / 2.0f) + 1.0f;
        double width = get_width();
 
        if (note == 0) {
-               note_height = floor(_view.contents_height()) - y_pos;
+               note_height = floor(_view.contents_height()) - y_pos + 2.;
        } else {
-               note_height = floor(_view.note_to_y(note - 1)) - y_pos;
+               note_height = floor(_view.note_to_y(note - 1)) - y_pos + 2.;
        }
 
        switch (note_type) {
@@ -441,10 +444,10 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev)
                        std::stringstream s;
                        double y = floor(_view.note_to_y(i)) - 0.5f;
                        double note_height = floor(_view.note_to_y(i - 1)) - y;
-                       
-                       int cn = i / 12;
+
+                       int cn = i / 12 - 1;
                        s << "C" << cn;
-                       
+
                        //cr->get_text_extents(s.str(), te);
                        cr->set_source_rgb(0.30f, 0.30f, 0.30f);
                        cr->move_to(2.0f, y + note_height - 1.0f - (note_height - font_size) / 2.0f);
@@ -459,27 +462,32 @@ bool
 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
 {
        int note = _view.y_to_note(ev->y);
+       set_note_highlight (note);
 
-       if (_highlighted_note != no_note) {
-               if (note > _highlighted_note) {
-                       invalidate_note_range(_highlighted_note, note);
-               } else {
-                       invalidate_note_range(note, _highlighted_note);
-               }
+       if (_dragging) {
 
-               _highlighted_note = note;
-       }
+               if ( false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) {   //ToDo:  fix this.  this mode is buggy, and of questionable utility anyway
 
-       /* redraw already taken care of above */
-       if (_clicked_note != no_note && _clicked_note != note) {
-               _active_notes[_clicked_note] = false;
-               send_note_off(_clicked_note);
+                       /* select note range */
 
-               _clicked_note = note;
+                       if (Keyboard::no_modifiers_active (ev->state)) {
+                               AddNoteSelection (note); // EMIT SIGNAL
+                       }
 
-               if (!_active_notes[note]) {
-                       _active_notes[note] = true;
-                       send_note_on(note);
+               } else {
+                       /* play notes */
+                       /* redraw already taken care of above in set_note_highlight */
+                       if (_clicked_note != NO_MIDI_NOTE && _clicked_note != note) {
+                               _active_notes[_clicked_note] = false;
+                               send_note_off(_clicked_note);
+
+                               _clicked_note = note;
+
+                               if (!_active_notes[note]) {
+                                       _active_notes[note] = true;
+                                       send_note_on(note);
+                               }
+                       }
                }
        }
 
@@ -492,8 +500,15 @@ bool
 PianoRollHeader::on_button_press_event (GdkEventButton* ev)
 {
        int note = _view.y_to_note(ev->y);
-
-       if (ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
+       bool tertiary = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
+
+       if (ev->button == 2 && Keyboard::no_modifiers_active (ev->state)) {
+               SetNoteSelection (note); // EMIT SIGNAL
+               return true;
+       } else if (tertiary && (ev->button == 1 || ev->button == 2)) {
+               ExtendNoteSelection (note); // EMIT SIGNAL
+               return true;
+       } else if (ev->button == 1 && note >= 0 && note < 128) {
                add_modal_grab();
                _dragging = true;
 
@@ -501,10 +516,10 @@ PianoRollHeader::on_button_press_event (GdkEventButton* ev)
                        _active_notes[note] = true;
                        _clicked_note = note;
                        send_note_on(note);
-                       
+
                        invalidate_note_range(note, note);
                } else {
-                       _clicked_note = no_note;
+                       reset_clicked_note(note);
                }
        }
 
@@ -516,28 +531,56 @@ PianoRollHeader::on_button_release_event (GdkEventButton* ev)
 {
        int note = _view.y_to_note(ev->y);
 
-       if (_dragging) {
-               remove_modal_grab();
-               _dragging = false;
-
-               if (note == _clicked_note) {
-                       _active_notes[note] = false;
-                       _clicked_note = no_note;
-                       send_note_off(note);
-                       
-                       invalidate_note_range(note, note);
+       if (false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) {  //Todo:  this mode is buggy, and of questionable utility anyway
+
+               if (Keyboard::no_modifiers_active (ev->state)) {
+                       AddNoteSelection (note); // EMIT SIGNAL
+               } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                       ToggleNoteSelection (note); // EMIT SIGNAL
+               } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
+                       ExtendNoteSelection (note); // EMIT SIGNAL
+               }
+
+       } else {
+
+               if (_dragging) {
+                       remove_modal_grab();
+
+                       if (note == _clicked_note) {
+                               reset_clicked_note(note);
+                       }
                }
        }
 
+       _dragging = false;
        return true;
 }
 
+void
+PianoRollHeader::set_note_highlight (uint8_t note) {
+       if (_highlighted_note == note) {
+               return;
+       }
+
+       if (_highlighted_note != NO_MIDI_NOTE) {
+               if (note > _highlighted_note) {
+                       invalidate_note_range (_highlighted_note, note);
+               } else {
+                       invalidate_note_range (note, _highlighted_note);
+               }
+       }
+
+       _highlighted_note = note;
+
+       if (_highlighted_note != NO_MIDI_NOTE) {
+               invalidate_note_range (_highlighted_note, _highlighted_note);
+       }
+}
+
 bool
 PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev)
 {
-       _highlighted_note = _view.y_to_note(ev->y);
-
-       invalidate_note_range(_highlighted_note, _highlighted_note);
+       set_note_highlight (_view.y_to_note(ev->y));
        return true;
 }
 
@@ -545,24 +588,17 @@ bool
 PianoRollHeader::on_leave_notify_event (GdkEventCrossing*)
 {
        invalidate_note_range(_highlighted_note, _highlighted_note);
-       
-       if (_clicked_note != no_note) {
-               _active_notes[_clicked_note] = false;
-               send_note_off(_clicked_note);
-
-               if (_clicked_note != _highlighted_note) {
-                       invalidate_note_range(_clicked_note, _clicked_note);
-               }
 
-               _clicked_note = no_note;
+       if (_clicked_note != NO_MIDI_NOTE) {
+               reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
        }
 
-       _highlighted_note = no_note;
+       _highlighted_note = NO_MIDI_NOTE;
        return true;
 }
 
 bool
-PianoRollHeader::on_scroll_event (GdkEventScroll* ev)
+PianoRollHeader::on_scroll_event (GdkEventScroll*)
 {
        return true;
 }
@@ -571,14 +607,7 @@ void
 PianoRollHeader::note_range_changed()
 {
        _note_height = floor(_view.note_height()) + 0.5f;
-
        queue_draw();
-
-       Glib::RefPtr<Gdk::Window> win = get_window();
-
-       if (win) {
-               win->process_updates(false);
-       }
 }
 
 void
@@ -639,7 +668,7 @@ void
 PianoRollHeader::on_size_allocate(Gtk::Allocation& a)
 {
        DrawingArea::on_size_allocate(a);
-       
+
        _black_note_width = floor(0.7 * get_width()) + 0.5f;
 }
 
@@ -647,11 +676,12 @@ void
 PianoRollHeader::send_note_on(uint8_t note)
 {
        boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
+       MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
 
        //cerr << "note on: " << (int) note << endl;
 
        if (track) {
-               _event[0] = MIDI_CMD_NOTE_ON;
+               _event[0] = (MIDI_CMD_NOTE_ON | mtv->get_channel_for_add ());
                _event[1] = note;
                _event[2] = 100;
 
@@ -663,12 +693,30 @@ void
 PianoRollHeader::send_note_off(uint8_t note)
 {
        boost::shared_ptr<ARDOUR::MidiTrack> track = _view.trackview().midi_track();
+       MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&_view.trackview ());
 
        if (track) {
-               _event[0] = MIDI_CMD_NOTE_OFF;
+               _event[0] = (MIDI_CMD_NOTE_OFF | mtv->get_channel_for_add ());
                _event[1] = note;
                _event[2] = 100;
 
                track->write_immediate_event(3, _event);
        }
 }
+
+void
+PianoRollHeader::reset_clicked_note (uint8_t note, bool invalidate)
+{
+       _active_notes[note] = false;
+       _clicked_note = NO_MIDI_NOTE;
+       send_note_off (note);
+       if (invalidate) {
+               invalidate_note_range (note, note);
+       }
+}
+
+PublicEditor&
+PianoRollHeader::editor() const
+{
+       return _view.trackview().editor();
+}