Snap note dragging vertically to note values (rows).
[ardour.git] / gtk2_ardour / canvas-midi-event.cc
1 /*
2     Copyright (C) 2007 Paul Davis 
3     Author: Dave Robillard
4
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.
9
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.
14
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.
18 */
19
20 #include <iostream>
21 #include "canvas-midi-event.h"
22 #include "midi_region_view.h"
23 #include "public_editor.h"
24 #include "editing_syms.h"
25 #include "keyboard.h"
26
27 using namespace std;
28 using ARDOUR::MidiModel;
29
30 namespace Gnome {
31 namespace Canvas {
32
33
34 CanvasMidiEvent::CanvasMidiEvent(MidiRegionView& region, Item* item, const ARDOUR::MidiModel::Note* note)
35         : _region(region)
36         , _item(item)
37         , _state(None)
38         , _note(note)
39 {       
40 }
41
42
43 bool
44 CanvasMidiEvent::on_event(GdkEvent* ev)
45 {
46         static double last_x, last_y;
47         double event_x, event_y, dx, dy;
48
49         if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
50                 return false;
51
52         switch (ev->type) {
53         case GDK_KEY_PRESS:
54                 if (_note && ev->key.keyval == GDK_Delete) {
55                         _region.start_remove_command();
56                         _region.command_remove_note(this);
57                 }
58                 break;
59         
60         case GDK_KEY_RELEASE:
61                 if (ev->key.keyval == GDK_Delete) {
62                         _region.apply_command();
63                 }
64                 break;
65         
66         case GDK_ENTER_NOTIFY:
67                 Keyboard::magic_widget_grab_focus();
68                 _item->grab_focus();
69                 _region.note_entered(this);
70                 break;
71
72         case GDK_LEAVE_NOTIFY:
73                 Keyboard::magic_widget_drop_focus();
74                 _region.get_canvas_group()->grab_focus();
75                 break;
76
77         case GDK_BUTTON_PRESS:
78                 _state = Pressed;
79                 return true;
80
81         case GDK_MOTION_NOTIFY:
82                 event_x = ev->motion.x;
83                 event_y = ev->motion.y;
84                 //cerr << "MOTION @ " << event_x << ", " << event_y << endl;
85                 _item->property_parent().get_value()->w2i(event_x, event_y);
86
87                 switch (_state) {
88                 case Pressed:
89                         _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
90                                         Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
91                         _state = Dragging;
92                         last_x = event_x;
93                         last_y = event_y;
94                         return true;
95                 case Dragging:
96                         if (ev->motion.is_hint) {
97                                 int t_x;
98                                 int t_y;
99                                 GdkModifierType state;
100                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
101                                 event_x = t_x;
102                                 event_y = t_y;
103                         }
104
105                         dx = event_x - last_x;
106                         dy = event_y - last_y;
107                         
108                         last_x = event_x;
109
110                         // Snap to note rows
111                         if (abs(dy) < _region.note_height()) {
112                                 dy = 0.0;
113                         } else {
114                                 dy = _region.note_height() * ((dy > 0) ? 1 : -1);
115                                 last_y = event_y;
116                         }
117
118                         _item->move(dx, dy);
119
120                         return true;
121                 default:
122                         break;
123                 }
124                 break;
125         
126         case GDK_BUTTON_RELEASE:
127                 switch (_state) {
128                 case Pressed: // Clicked
129                         _state = None;
130                         return true;
131                 case Dragging: // Dropped
132                         _item->ungrab(ev->button.time);
133                         _state = None;
134                         if (_note) {
135                                 cerr << "Move and stuff." << endl;
136                                 // This would be nicer with a MoveCommand that doesn't need to copy...
137                                 /*_region.start_delta_command();
138                                 _region.command_remove_note(this);
139                                 Note copy_of_me(*this); 
140                                 copy_of_me.time = trackview.editor.pixel_to_frame(property_x1());
141                                 copy_of_me.note = stuff;
142                                 _region.apply_command();
143                                 */
144                         }
145                         return true;
146                 default:
147                         break;
148                 }
149
150         default:
151                 break;
152         }
153
154         return false;
155 }
156
157 } // namespace Canvas
158 } // namespace Gnome
159