298743509085c871066360a00f4491253d2c7552
[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,
35                 const boost::shared_ptr<ARDOUR::Note> note)
36         : _region(region)
37         , _item(item)
38         , _state(None)
39         , _note(note)
40         , _selected(false)
41 {       
42 }
43
44
45 void
46 CanvasMidiEvent::selected(bool yn)
47 {
48         if (!_note) {
49                 return;
50         } else if (yn) {
51                 set_fill_color(UINT_INTERPOLATE(note_fill_color(_note->velocity()),
52                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(), 0.85));
53                 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get());
54         } else {
55                 set_fill_color(note_fill_color(_note->velocity()));
56                 set_outline_color(note_outline_color(_note->velocity()));
57         }
58
59         _selected = yn;
60 }
61
62
63 bool
64 CanvasMidiEvent::on_event(GdkEvent* ev)
65 {
66         static uint8_t drag_delta_note = 0;
67         static double  drag_delta_x = 0;
68         static double last_x, last_y;
69         double event_x, event_y, dx, dy;
70         nframes_t event_frame;
71         bool select_mod;
72
73         if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
74                 return false;
75
76         switch (ev->type) {
77         case GDK_KEY_PRESS:
78                 cerr << "EV KEY PRESS\n";
79                 if (_note && ev->key.keyval == GDK_Delete) {
80                         cerr << "EV DELETE KEY\n";
81                         selected(true);
82                         _region.start_remove_command();
83                         _region.command_remove_note(this);
84                 }
85                 break;
86         
87         case GDK_KEY_RELEASE:
88                 cerr << "EV KEY RELEASE\n";
89                 if (ev->key.keyval == GDK_Delete) {
90                         _region.apply_command();
91                 }
92                 break;
93         
94         case GDK_ENTER_NOTIFY:
95                 _region.note_entered(this);
96                 _item->grab_focus();
97                 Keyboard::magic_widget_grab_focus();
98                 break;
99
100         case GDK_LEAVE_NOTIFY:
101                 Keyboard::magic_widget_drop_focus();
102                 _region.get_canvas_group()->grab_focus();
103                 break;
104
105         case GDK_BUTTON_PRESS:
106                 _state = Pressed;
107                 return true;
108
109         case GDK_MOTION_NOTIFY:
110                 event_x = ev->motion.x;
111                 event_y = ev->motion.y;
112                 //cerr << "MOTION @ " << event_x << ", " << event_y << endl;
113                 _item->property_parent().get_value()->w2i(event_x, event_y);
114
115                 switch (_state) {
116                 case Pressed: // Drag begin
117                         if (_region.mouse_state() != MidiRegionView::SelectTouchDragging) {
118                                 _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
119                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
120                                 _state = Dragging;
121                                 last_x = event_x;
122                                 last_y = event_y;
123                                 drag_delta_x = 0;
124                                 drag_delta_note = 0;
125                                 _region.note_selected(this, true);
126                         }
127                         return true;
128
129                 case Dragging: // Drag motion
130                         if (ev->motion.is_hint) {
131                                 int t_x;
132                                 int t_y;
133                                 GdkModifierType state;
134                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
135                                 event_x = t_x;
136                                 event_y = t_y;
137                         }
138                         
139                         // Snap
140                         event_frame = _region.midi_view()->editor.pixel_to_frame(event_x);
141                         _region.midi_view()->editor.snap_to(event_frame);
142                         event_x = _region.midi_view()->editor.frame_to_pixel(event_frame);
143
144                         dx = event_x - last_x;
145                         dy = event_y - last_y;
146                         
147                         last_x = event_x;
148
149                         drag_delta_x += dx;
150
151                         // Snap to note rows
152                         if (abs(dy) < _region.midi_stream_view()->note_height()) {
153                                 dy = 0.0;
154                         } else {
155                                 int8_t this_delta_note;
156                                 if (dy > 0)
157                                         this_delta_note = (int8_t)ceil(dy / _region.midi_stream_view()->note_height() / 2.0);
158                                 else
159                                         this_delta_note = (int8_t)floor(dy / _region.midi_stream_view()->note_height() / 2.0);
160                                 drag_delta_note -= this_delta_note;
161                                 dy = _region.midi_stream_view()->note_height() * this_delta_note;
162                                 last_y = last_y + dy;
163                         }
164
165                         _region.move_selection(dx, dy);
166
167                         return true;
168                 default:
169                         break;
170                 }
171                 break;
172         
173         case GDK_BUTTON_RELEASE:
174                 select_mod = (ev->motion.state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK));
175                 event_x = ev->button.x;
176                 event_y = ev->button.y;
177                 _item->property_parent().get_value()->w2i(event_x, event_y);
178
179                 switch (_state) {
180                 case Pressed: // Clicked
181                         if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect) {
182                                 _state = None;
183
184                                 if (_selected && !select_mod && _region.selection_size() > 1)
185                                         _region.unique_select(this);
186                                 else if (_selected)
187                                         _region.note_deselected(this, select_mod);
188                                 else
189                                         _region.note_selected(this, select_mod);
190                         } else if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditErase) {
191                                 _region.start_remove_command();
192                                 _region.command_remove_note(this);
193                                 _region.apply_command();
194                         }
195
196                         return true;
197                 case Dragging: // Dropped
198                         _item->ungrab(ev->button.time);
199                         _state = None;
200
201                         if (_note)
202                                 _region.note_dropped(this,
203                                                 _region.midi_view()->editor.pixel_to_frame(abs(drag_delta_x))
204                                                                 * ((drag_delta_x < 0.0) ? -1 : 1),
205                                                 drag_delta_note);
206                         return true;
207                 default:
208                         break;
209                 }
210
211         default:
212                 break;
213         }
214
215         return false;
216 }
217
218 } // namespace Canvas
219 } // namespace Gnome
220