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