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