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-midi-event.h"
22 #include "midi_region_view.h"
23 #include "public_editor.h"
24 #include "editing_syms.h"
28 using ARDOUR::MidiModel;
34 CanvasMidiEvent::CanvasMidiEvent(MidiRegionView& region, Item* item, const ARDOUR::MidiModel::Note* note)
44 CanvasMidiEvent::on_event(GdkEvent* ev)
46 static uint8_t drag_delta_note = 0;
47 static double drag_delta_x = 0;
48 static double last_x, last_y;
49 double event_x, event_y, dx, dy;
51 if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
56 if (_note && ev->key.keyval == GDK_Delete) {
57 _region.start_remove_command();
58 _region.command_remove_note(this);
63 if (ev->key.keyval == GDK_Delete) {
64 _region.apply_command();
68 case GDK_ENTER_NOTIFY:
69 Keyboard::magic_widget_grab_focus();
71 _region.note_entered(this);
74 case GDK_LEAVE_NOTIFY:
75 Keyboard::magic_widget_drop_focus();
76 _region.get_canvas_group()->grab_focus();
79 case GDK_BUTTON_PRESS:
83 case GDK_MOTION_NOTIFY:
84 event_x = ev->motion.x;
85 event_y = ev->motion.y;
86 //cerr << "MOTION @ " << event_x << ", " << event_y << endl;
87 _item->property_parent().get_value()->w2i(event_x, event_y);
90 case Pressed: // Drag begin
91 _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
92 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
100 case Dragging: // Drag motion
101 if (ev->motion.is_hint) {
104 GdkModifierType state;
105 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
110 dx = event_x - last_x;
111 dy = event_y - last_y;
118 if (abs(dy) < _region.note_height()) {
121 int8_t this_delta_note;
123 this_delta_note = (int8_t)ceil(dy / _region.note_height());
125 this_delta_note = (int8_t)floor(dy / _region.note_height());
126 drag_delta_note -= this_delta_note;
127 dy = _region.note_height() * this_delta_note;
128 last_y = last_y + dy;
139 case GDK_BUTTON_RELEASE:
140 event_x = ev->button.x;
141 event_y = ev->button.y;
142 _item->property_parent().get_value()->w2i(event_x, event_y);
145 case Pressed: // Clicked
148 case Dragging: // Dropped
149 _item->ungrab(ev->button.time);
152 // This would be nicer with a MoveCommand that doesn't need to copy...
153 _region.start_delta_command();
154 _region.command_remove_note(this);
155 MidiModel::Note copy(*_note);
157 double delta_t = _region.midi_view()->editor.pixel_to_frame(
159 if (drag_delta_x < 0)
162 copy.set_time(_note->time() + delta_t);
163 copy.set_note(_note->note() + drag_delta_note);
165 _region.command_add_note(copy);
166 _region.apply_command();
180 } // namespace Canvas