X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Fpiano_roll_header.cc;h=cdd956dddd70f8649709b209e7c61cd81c03a7ca;hb=bf7ed81a39daf433dc9c9123627afd2381e58386;hp=5032e97e918f5a63b2a884b53b4bb37aca175e23;hpb=1b657585572298d1a69a7b43e611f59b7e185df3;p=ardour.git diff --git a/gtk2_ardour/piano_roll_header.cc b/gtk2_ardour/piano_roll_header.cc index 5032e97e91..cdd956dddd 100644 --- a/gtk2_ardour/piano_roll_header.cc +++ b/gtk2_ardour/piano_roll_header.cc @@ -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 @@ -16,53 +16,60 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include +#include +#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 - -const int no_note = 0xff; +#include "public_editor.h" +#include "ui_config.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); PianoRollHeader::Color::Color() : r(1.0f) , g(1.0f) - , b(1.0f) { + , b(1.0f) +{ } PianoRollHeader::Color::Color(double _r, double _g, double _b) : r(_r) , g(_g) - , b(_b) { + , b(_b) +{ } inline void -PianoRollHeader::Color::set(const PianoRollHeader::Color& c) { +PianoRollHeader::Color::set(const PianoRollHeader::Color& c) +{ r = c.r; g = c.g; b = c.b; } -PianoRollHeader::PianoRollHeader(MidiStreamView& v) - : _view(v) - , _highlighted_note(no_note) - , _clicked_note(no_note) - , _dragging(false) { - +PianoRollHeader::PianoRollHeader(MidiStreamView& v) + : _view(v) + , _highlighted_note(NO_MIDI_NOTE) + , _clicked_note(NO_MIDI_NOTE) + , _dragging(false) +{ add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | @@ -70,25 +77,27 @@ PianoRollHeader::PianoRollHeader(MidiStreamView& v) Gdk::LEAVE_NOTIFY_MASK | Gdk::SCROLL_MASK); - for(int i = 0; i < 128; ++i) { + for (int i = 0; i < 128; ++i) { _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 -create_path(Cairo::RefPtr cr, double x[], double y[], int start, int stop) { +create_path(Cairo::RefPtr cr, double x[], double y[], int start, int stop) +{ cr->move_to(x[start], y[start]); - for(int i = start+1; i <= stop; ++i) { + for (int i = start+1; i <= stop; ++i) { cr->line_to(x[i], y[i]); } } -inline void -render_rect(Cairo::RefPtr cr, int note, double x[], double y[], - PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) { +inline void +render_rect(Cairo::RefPtr 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); create_path(cr, x, y, 0, 4); cr->fill(); @@ -103,8 +112,9 @@ render_rect(Cairo::RefPtr cr, int note, double x[], double y[], } inline void -render_cf(Cairo::RefPtr cr, int note, double x[], double y[], - PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) { +render_cf(Cairo::RefPtr 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); create_path(cr, x, y, 0, 6); cr->fill(); @@ -119,8 +129,9 @@ render_cf(Cairo::RefPtr cr, int note, double x[], double y[], } inline void -render_eb(Cairo::RefPtr cr, int note, double x[], double y[], - PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) { +render_eb(Cairo::RefPtr 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); create_path(cr, x, y, 0, 6); cr->fill(); @@ -139,8 +150,9 @@ render_eb(Cairo::RefPtr cr, int note, double x[], double y[], } inline void -render_dga(Cairo::RefPtr cr, int note, double x[], double y[], - PianoRollHeader::Color& bg, PianoRollHeader::Color& tl_shadow, PianoRollHeader::Color& br_shadow) { +render_dga(Cairo::RefPtr 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); create_path(cr, x, y, 0, 8); cr->fill(); @@ -159,21 +171,21 @@ render_dga(Cairo::RefPtr 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; +PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double x[], double y[]) +{ + 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; - } - else { - note_height = floor(_view.note_to_y(note - 1)) - y_pos; + if (note == 0) { + note_height = floor(_view.contents_height()) - y_pos + 2.; + } else { + note_height = floor(_view.note_to_y(note - 1)) - y_pos + 2.; } - switch(note_type) { + switch (note_type) { case BLACK_SEPARATOR: x[0] = 1.5f; y[0] = y_pos; @@ -274,7 +286,8 @@ PianoRollHeader::get_path(PianoRollHeader::ItemType note_type, int note, double } bool -PianoRollHeader::on_expose_event (GdkEventExpose* ev) { +PianoRollHeader::on_expose_event (GdkEventExpose* ev) +{ GdkRectangle& rect = ev->area; double font_size; int lowest, highest; @@ -291,7 +304,7 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { lowest = max(_view.lowest_note(), _view.y_to_note(y2)); highest = min(_view.highest_note(), _view.y_to_note(y1)); - if(lowest > 127) { + if (lowest > 127) { lowest = 0; } @@ -322,28 +335,26 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { //pat->add_color_stop_rgb(1.0, 0.22, 0.22, 0.22); //cr->set_source(pat); - for(int i = lowest; i <= highest; ++i) { + for (int i = lowest; i <= highest; ++i) { oct_rel = i % 12; - switch(oct_rel) { + switch (oct_rel) { case 1: case 3: case 6: case 8: case 10: /* black note */ - if(i == _highlighted_note) { + if (i == _highlighted_note) { bg.set(black_highlight); - } - else { + } else { bg.set(black); } - if(_active_notes[i]) { + if (_active_notes[i]) { tl_shadow.set(black_shade_dark); br_shadow.set(black_shade_light); - } - else { + } else { tl_shadow.set(black_shade_light); br_shadow.set(black_shade_dark); } @@ -364,18 +375,16 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { default: /* white note */ - if(i == _highlighted_note) { + if (i == _highlighted_note) { bg.set(white_highlight); - } - else { + } else { bg.set(white); } - if(_active_notes[i]) { + if (_active_notes[i]) { tl_shadow.set(white_shade_dark); br_shadow.set(white_shade_light); - } - else { + } else { tl_shadow.set(white_shade_light); br_shadow.set(white_shade_dark); } @@ -383,11 +392,10 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { switch(oct_rel) { case 0: case 5: - if(i == _view.highest_note()) { + if (i == _view.highest_note()) { get_path(WHITE_RECT, i, x, y); render_rect(cr, i, x, y, bg, tl_shadow, br_shadow); - } - else { + } else { get_path(WHITE_CF, i, x, y); render_cf(cr, i, x, y, bg, tl_shadow, br_shadow); } @@ -396,15 +404,13 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { case 2: case 7: case 9: - if(i == _view.highest_note()) { + if (i == _view.highest_note()) { get_path(WHITE_EB, i, x, y); render_eb(cr, i, x, y, bg, tl_shadow, br_shadow); - } - else if(i == _view.lowest_note()) { + } else if (i == _view.lowest_note()) { get_path(WHITE_CF, i, x, y); render_cf(cr, i, x, y, bg, tl_shadow, br_shadow); - } - else { + } else { get_path(WHITE_DGA, i, x, y); render_dga(cr, i, x, y, bg, tl_shadow, br_shadow); } @@ -417,11 +423,10 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { create_path(cr, x, y, 0, 1); cr->stroke(); - if(i == _view.lowest_note()) { + if (i == _view.lowest_note()) { get_path(WHITE_RECT, i, x, y); render_rect(cr, i, x, y, bg, tl_shadow, br_shadow); - } - else { + } else { get_path(WHITE_EB, i, x, y); render_eb(cr, i, x, y, bg, tl_shadow, br_shadow); } @@ -436,14 +441,14 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { } /* render the name of which C this is */ - if(oct_rel == 0) { + if (oct_rel == 0) { 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); @@ -455,30 +460,35 @@ PianoRollHeader::on_expose_event (GdkEventExpose* ev) { } bool -PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev) { +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); + } + } } } @@ -488,22 +498,29 @@ PianoRollHeader::on_motion_notify_event (GdkEventMotion* ev) { } bool -PianoRollHeader::on_button_press_event (GdkEventButton* ev) { +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; - if(!_active_notes[note]) { + if (!_active_notes[note]) { _active_notes[note] = true; _clicked_note = note; send_note_on(note); - + invalidate_note_range(note, note); - } - else { - _clicked_note = no_note; + } else { + reset_clicked_note(note); } } @@ -511,72 +528,92 @@ PianoRollHeader::on_button_press_event (GdkEventButton* ev) { } bool -PianoRollHeader::on_button_release_event (GdkEventButton* ev) { +PianoRollHeader::on_button_release_event (GdkEventButton* ev) +{ int note = _view.y_to_note(ev->y); - if(_dragging) { - remove_modal_grab(); - _dragging = false; + if (false /*editor().current_mouse_mode() == Editing::MouseRange*/ ) { //Todo: this mode is buggy, and of questionable utility anyway - if(note == _clicked_note) { - _active_notes[note] = false; - _clicked_note = no_note; - send_note_off(note); - - invalidate_note_range(note, note); + 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; } -bool -PianoRollHeader::on_enter_notify_event (GdkEventCrossing* ev) { - _highlighted_note = _view.y_to_note(ev->y); +void +PianoRollHeader::set_note_highlight (uint8_t note) { + if (_highlighted_note == note) { + return; + } - invalidate_note_range(_highlighted_note, _highlighted_note); + 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) +{ + set_note_highlight (_view.y_to_note(ev->y)); return true; } bool -PianoRollHeader::on_leave_notify_event (GdkEventCrossing*) { +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; } void -PianoRollHeader::note_range_changed() { +PianoRollHeader::note_range_changed() +{ _note_height = floor(_view.note_height()) + 0.5f; - queue_draw(); - - Glib::RefPtr win = get_window(); - - if(win) { - win->process_updates(false); - } } void -PianoRollHeader::invalidate_note_range(int lowest, int highest) { +PianoRollHeader::invalidate_note_range(int lowest, int highest) +{ Glib::RefPtr win = get_window(); Gdk::Rectangle rect; @@ -617,31 +654,35 @@ PianoRollHeader::invalidate_note_range(int lowest, int highest) { rect.set_y((int) floor(y)); rect.set_height((int) floor(height)); - if(win) { + if (win) { win->invalidate_rect(rect, false); } } void -PianoRollHeader::on_size_request(Gtk::Requisition* r) { - r->width = 20; +PianoRollHeader::on_size_request(Gtk::Requisition* r) +{ + r->width = std::max (20.f, rintf (20.f * UIConfiguration::instance().get_ui_scale())); } void -PianoRollHeader::on_size_allocate(Gtk::Allocation& a) { +PianoRollHeader::on_size_allocate(Gtk::Allocation& a) +{ DrawingArea::on_size_allocate(a); - + _black_note_width = floor(0.7 * get_width()) + 0.5f; } void -PianoRollHeader::send_note_on(uint8_t note) { +PianoRollHeader::send_note_on(uint8_t note) +{ boost::shared_ptr track = _view.trackview().midi_track(); + MidiTimeAxisView* mtv = dynamic_cast (&_view.trackview ()); //cerr << "note on: " << (int) note << endl; - if(track) { - _event[0] = MIDI_CMD_NOTE_ON; + if (track) { + _event[0] = (MIDI_CMD_NOTE_ON | mtv->get_channel_for_add ()); _event[1] = note; _event[2] = 100; @@ -650,14 +691,33 @@ PianoRollHeader::send_note_on(uint8_t note) { } void -PianoRollHeader::send_note_off(uint8_t note) { +PianoRollHeader::send_note_off(uint8_t note) +{ boost::shared_ptr track = _view.trackview().midi_track(); + MidiTimeAxisView* mtv = dynamic_cast (&_view.trackview ()); - if(track) { - _event[0] = MIDI_CMD_NOTE_OFF; + if (track) { + _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(); +}