2 Copyright (C) 2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "canvas-note-event.h"
22 #include "midi_region_view.h"
23 #include "public_editor.h"
24 #include "editing_syms.h"
28 using ARDOUR::MidiModel;
34 CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item,
35 const boost::shared_ptr<Evoral::Note> note)
39 , _channel_selector_widget()
46 CanvasNoteEvent::~CanvasNoteEvent()
53 if (_channel_selector_widget)
54 delete _channel_selector_widget;
58 CanvasNoteEvent::move_event(double dx, double dy)
69 CanvasNoteEvent::show_velocity(void)
72 _text = new Text(*(_item->property_parent()));
73 _text->property_x() = (x1() + x2()) /2;
74 _text->property_y() = (y1() + y2()) /2;
75 ostringstream velo(ios::ate);
76 velo << int(_note->velocity());
77 _text->property_text() = velo.str();
78 _text->property_justification() = Gtk::JUSTIFY_CENTER;
79 _text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
81 _text->lower_to_bottom();
86 CanvasNoteEvent::hide_velocity(void)
96 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
98 // make note change its color if its channel is not marked active
99 if ( (selection & (1 << _note->channel())) == 0 ) {
100 set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteFillInactiveChannel.get());
101 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteOutlineInactiveChannel.get());
103 // set the color according to the notes selection state
106 // this forces the item to update..... maybe slow...
112 CanvasNoteEvent::on_channel_change(uint8_t channel)
114 _region.note_selected(this, true);
115 hide_channel_selector();
116 _region.change_channel(channel);
120 CanvasNoteEvent::show_channel_selector(void)
122 if (_channel_selector_widget == 0) {
123 cerr << "Note has channel: " << int(_note->channel()) << endl;
124 SingleMidiChannelSelector* _channel_selector = new SingleMidiChannelSelector(_note->channel());
125 _channel_selector->show_all();
126 _channel_selector->channel_selected.connect(
127 sigc::mem_fun(this, &CanvasNoteEvent::on_channel_change));
129 _channel_selector_widget =
130 new Widget(*(_item->property_parent()),
133 (Gtk::Widget &) *_channel_selector);
135 _channel_selector_widget->hide();
136 _channel_selector_widget->property_height() = 100;
137 _channel_selector_widget->property_width() = 100;
138 _channel_selector_widget->raise_to_top();
139 _channel_selector_widget->show();
141 hide_channel_selector();
146 CanvasNoteEvent::hide_channel_selector(void)
148 if (_channel_selector_widget) {
149 _channel_selector_widget->hide();
150 delete _channel_selector_widget;
151 _channel_selector_widget = 0;
156 CanvasNoteEvent::selected(bool yn)
161 set_fill_color(UINT_INTERPOLATE(note_fill_color(_note->velocity()),
162 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(), 0.1));
163 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get());
166 set_fill_color(note_fill_color(_note->velocity()));
167 set_outline_color(note_outline_color(_note->velocity()));
176 CanvasNoteEvent::on_event(GdkEvent* ev)
178 MidiStreamView *streamview = _region.midi_stream_view();
179 static uint8_t drag_delta_note = 0;
180 static double drag_delta_x = 0;
181 static double last_x, last_y;
182 double event_x, event_y, dx, dy;
184 uint8_t d_velocity = 10;
186 if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
191 if (Keyboard::modifier_state_equals (ev->scroll.state, Keyboard::Level4Modifier)) {
195 if (ev->scroll.direction == GDK_SCROLL_UP) {
196 _region.note_selected(this, true);
197 if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
198 // TODO: absolute velocity
200 _region.change_velocity(d_velocity, true);
203 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
204 _region.note_selected(this, true);
205 if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
206 // TODO: absolute velocity
208 _region.change_velocity(-d_velocity, true);
216 if (_note && ev->key.keyval == GDK_Delete) {
218 _region.start_delta_command();
219 _region.command_remove_note(this);
223 case GDK_KEY_RELEASE:
224 if (ev->key.keyval == GDK_Delete) {
225 _region.apply_command();
229 case GDK_ENTER_NOTIFY:
230 _region.note_entered(this);
233 Keyboard::magic_widget_grab_focus();
236 case GDK_LEAVE_NOTIFY:
237 Keyboard::magic_widget_drop_focus();
241 _region.get_canvas_group()->grab_focus();
244 case GDK_BUTTON_PRESS:
245 if (ev->button.button == 1) {
247 } else if (ev->button.button == 3) {
248 show_channel_selector();
252 case GDK_MOTION_NOTIFY:
253 event_x = ev->motion.x;
254 event_y = ev->motion.y;
257 case Pressed: // Drag begin
258 if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect
259 && _region.mouse_state() != MidiRegionView::SelectTouchDragging
260 && _region.mouse_state() != MidiRegionView::EraseTouchDragging) {
261 _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
262 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
264 _item->property_parent().get_value()->w2i(event_x, event_y);
265 event_x = _region.snap_to_pixel(event_x);
270 _region.note_selected(this, true);
274 case Dragging: // Drag motion
275 if (ev->motion.is_hint) {
278 GdkModifierType state;
279 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
283 _item->property_parent().get_value()->w2i(event_x, event_y);
286 event_x = _region.snap_to_pixel(event_x);
288 dx = event_x - last_x;
289 dy = event_y - last_y;
296 if (abs(dy) < streamview->note_height()) {
299 int8_t this_delta_note;
301 this_delta_note = (int8_t)ceil(dy / streamview->note_height() / 2.0);
303 this_delta_note = (int8_t)floor(dy / streamview->note_height() / 2.0);
305 drag_delta_note -= this_delta_note;
306 dy = streamview->note_height() * this_delta_note;
307 last_y = last_y + dy;
310 _region.move_selection(dx, dy);
318 case GDK_BUTTON_RELEASE:
319 select_mod = (ev->motion.state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK));
320 event_x = ev->button.x;
321 event_y = ev->button.y;
322 _item->property_parent().get_value()->w2i(event_x, event_y);
324 if (ev->button.button == 3) {
329 case Pressed: // Clicked
330 if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect) {
333 if (_selected && !select_mod && _region.selection_size() > 1)
334 _region.unique_select(this);
336 _region.note_deselected(this, select_mod);
338 _region.note_selected(this, select_mod);
339 } else if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditErase) {
340 _region.start_delta_command();
341 _region.command_remove_note(this);
342 _region.apply_command();
346 case Dragging: // Dropped
347 _item->ungrab(ev->button.time);
351 _region.note_dropped(this,
352 _region.midi_view()->editor.pixel_to_frame(abs(drag_delta_x))
353 * ((drag_delta_x < 0.0) ? -1 : 1),
367 } // namespace Canvas