Remove special handling of session range in export. Fixes things when the session...
[ardour.git] / gtk2_ardour / piano_roll_header.cc
index 39c48f016491871e4b242b075273e3286ce2ae9b..3243f893c7622a9d27dfcf6b38c263e1b4000b29 100644 (file)
 #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"
+#include "public_editor.h"
 
 const int no_note = 0xff;
 
 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);
@@ -442,7 +447,7 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev)
                        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);
@@ -458,28 +463,44 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev)
 bool
 PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev)
 {
-       int note = _view.y_to_note(ev->y);
+       if (_dragging) {
 
-       if (_highlighted_note != no_note) {
-               if (note > _highlighted_note) {
-                       invalidate_note_range(_highlighted_note, note);
-               } else {
-                       invalidate_note_range(note, _highlighted_note);
-               }
+               int note = _view.y_to_note(ev->y);
 
-               _highlighted_note = note;
-       }
+               if (editor().current_mouse_mode() == Editing::MouseRange) {
+                       
+                       /* select note range */
 
-       /* redraw already taken care of above */
-       if (_clicked_note != no_note && _clicked_note != note) {
-               _active_notes[_clicked_note] = false;
-               send_note_off(_clicked_note);
+                       if (Keyboard::no_modifiers_active (ev->state)) {
+                               AddNoteSelection (note); // EMIT SIGNAL
+                       }
 
-               _clicked_note = note;
+               } else {
+                       
+                       /* play notes */
 
-               if (!_active_notes[note]) {
-                       _active_notes[note] = true;
-                       send_note_on(note);
+                       if (_highlighted_note != no_note) {
+                               if (note > _highlighted_note) {
+                                       invalidate_note_range(_highlighted_note, note);
+                               } else {
+                                       invalidate_note_range(note, _highlighted_note);
+                               }
+                               
+                               _highlighted_note = note;
+                       }
+                       
+                       /* redraw already taken care of above */
+                       if (_clicked_note != no_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);
+                               }
+                       }
                }
        }
 
@@ -493,18 +514,31 @@ 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) {
-               add_modal_grab();
+       if (ev->button != 1) {
+               return false;
+       }
+
+       if (editor().current_mouse_mode() == Editing::MouseRange) {
+               if (Keyboard::no_modifiers_active (ev->state)) {
+                       SetNoteSelection (note); // EMIT SIGNAL
+               }
                _dragging = true;
+       } else {
 
-               if (!_active_notes[note]) {
-                       _active_notes[note] = true;
-                       _clicked_note = note;
-                       send_note_on(note);
+               if (ev->type == GDK_BUTTON_PRESS && note >= 0 && note < 128) {
 
-                       invalidate_note_range(note, note);
-               } else {
-                       _clicked_note = no_note;
+                       add_modal_grab();
+                       _dragging = true;
+
+                       if (!_active_notes[note]) {
+                               _active_notes[note] = true;
+                               _clicked_note = note;
+                               send_note_on(note);
+
+                               invalidate_note_range(note, note);
+                       } else {
+                               reset_clicked_note(note);
+                       }
                }
        }
 
@@ -516,19 +550,28 @@ PianoRollHeader::on_button_release_event (GdkEventButton* ev)
 {
        int note = _view.y_to_note(ev->y);
 
-       if (_dragging) {
-               remove_modal_grab();
-               _dragging = false;
+       if (editor().current_mouse_mode() == Editing::MouseRange) {
+
+               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 (note == _clicked_note) {
-                       _active_notes[note] = false;
-                       _clicked_note = no_note;
-                       send_note_off(note);
+               if (_dragging) {
+                       remove_modal_grab();
 
-                       invalidate_note_range(note, note);
+                       if (note == _clicked_note) {
+                               reset_clicked_note(note);
+                       }
                }
        }
 
+       _dragging = false;
        return true;
 }
 
@@ -547,14 +590,7 @@ 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;
+               reset_clicked_note(_clicked_note, _clicked_note != _highlighted_note);
        }
 
        _highlighted_note = no_note;
@@ -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
@@ -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 | track->default_channel());
+               _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 | track->default_channel());
+               _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_note;
+       send_note_off (note);
+       if (invalidate) {
+               invalidate_note_range (note, note);
+       }
+}
+
+PublicEditor&
+PianoRollHeader::editor() const
+{
+       return _view.trackview().editor();
+}