Factor out sequencing related things into an independant new library: "evoral".
[ardour.git] / gtk2_ardour / canvas-note-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-note-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 CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item,
35                 const boost::shared_ptr<Evoral::Note> note)
36         : _region(region)
37         , _item(item)
38         , _text(0)
39         , _channel_selector_widget()
40         , _state(None)
41         , _note(note)
42         , _selected(false)
43 {
44 }
45
46 CanvasNoteEvent::~CanvasNoteEvent() 
47
48         if (_text) {
49                 _text->hide();
50                 delete _text;
51         }
52         
53         if (_channel_selector_widget)
54                 delete _channel_selector_widget;
55 }
56
57 void 
58 CanvasNoteEvent::move_event(double dx, double dy)
59 {
60         _item->move(dx, dy);
61         if (_text) {
62                 _text->hide();
63                 _text->move(dx, dy);
64                 _text->show();
65         }
66 }
67
68 void
69 CanvasNoteEvent::show_velocity(void)
70 {
71         hide_velocity();
72         _text = new Text(*(_item->property_parent()));
73         _text->property_x() = (x1() + x2()) /2;
74         _text->property_y() = (y1() + y2()) /2;
75         ostringstream velo(ios::ate);
76         velo << int(_note->velocity());
77         _text->property_text() = velo.str();
78         _text->property_justification() = Gtk::JUSTIFY_CENTER;
79         _text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
80         _text->show();
81         _text->lower_to_bottom();
82         _text->raise(2);
83 }
84
85 void
86 CanvasNoteEvent::hide_velocity(void)
87 {
88         if(_text) {
89                 _text->hide();
90                 delete _text;
91         }
92         _text = 0;
93 }
94
95 void 
96 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
97 {
98         // make note change its color if its channel is not marked active
99         if ( (selection & (1 << _note->channel())) == 0 ) {
100                 set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteFillInactiveChannel.get());
101                 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteOutlineInactiveChannel.get());
102         } else {
103                 // set the color according to the notes selection state
104                 selected(_selected);
105         }
106         // this forces the item to update..... maybe slow...
107         _item->hide();
108         _item->show();
109 }
110
111 void 
112 CanvasNoteEvent::on_channel_change(uint8_t channel)
113 {
114         _region.note_selected(this, true);
115         hide_channel_selector();
116         _region.change_channel(channel);
117 }
118
119 void
120 CanvasNoteEvent::show_channel_selector(void)
121 {
122         if (_channel_selector_widget == 0) {
123                 cerr << "Note has channel: " << int(_note->channel()) << endl;
124                 SingleMidiChannelSelector* _channel_selector = new SingleMidiChannelSelector(_note->channel());
125                 _channel_selector->show_all();
126                 _channel_selector->channel_selected.connect(
127                         sigc::mem_fun(this, &CanvasNoteEvent::on_channel_change));
128
129                 _channel_selector_widget = 
130                         new Widget(*(_item->property_parent()), 
131                                    x1(), 
132                                    y2() + 2, 
133                                    (Gtk::Widget &) *_channel_selector);
134                 
135                 _channel_selector_widget->hide();
136                 _channel_selector_widget->property_height() = 100;
137                 _channel_selector_widget->property_width() = 100;
138                 _channel_selector_widget->raise_to_top();
139                 _channel_selector_widget->show();
140         } else {
141                 hide_channel_selector();
142         }
143 }
144
145 void
146 CanvasNoteEvent::hide_channel_selector(void)
147 {
148         if (_channel_selector_widget) {
149                 _channel_selector_widget->hide();
150                 delete _channel_selector_widget;
151                 _channel_selector_widget = 0;
152         }
153 }
154
155 void
156 CanvasNoteEvent::selected(bool yn)
157 {
158         if (!_note) {
159                 return;
160         } else if (yn) {
161                 set_fill_color(UINT_INTERPOLATE(note_fill_color(_note->velocity()),
162                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(), 0.1));
163                 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get());
164                 show_velocity();
165         } else {
166                 set_fill_color(note_fill_color(_note->velocity()));
167                 set_outline_color(note_outline_color(_note->velocity()));
168                 hide_velocity();
169         }
170
171         _selected = yn;
172 }
173
174
175 bool
176 CanvasNoteEvent::on_event(GdkEvent* ev)
177 {
178         MidiStreamView *streamview = _region.midi_stream_view();
179         static uint8_t drag_delta_note = 0;
180         static double  drag_delta_x = 0;
181         static double last_x, last_y;
182         double event_x, event_y, dx, dy;
183         bool select_mod;
184         uint8_t d_velocity = 10;
185
186         if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
187                 return false;
188
189         switch (ev->type) {
190         case GDK_SCROLL:
191                 if (Keyboard::modifier_state_equals (ev->scroll.state, Keyboard::Level4Modifier)) {
192                         d_velocity = 1;
193                 }
194
195                 if (ev->scroll.direction == GDK_SCROLL_UP) {
196                         _region.note_selected(this, true);
197                         if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
198                                 // TODO: absolute velocity
199                         } else {
200                                 _region.change_velocity(d_velocity, true);
201                         }
202                         return true;
203                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
204                         _region.note_selected(this, true);
205                         if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
206                                 // TODO: absolute velocity
207                         } else {
208                                 _region.change_velocity(-d_velocity, true);
209                         }
210                         return true;
211                 } else {
212                         return false;
213                 }
214                 
215         case GDK_KEY_PRESS:
216                 if (_note && ev->key.keyval == GDK_Delete) {
217                         selected(true);
218                         _region.start_delta_command();
219                         _region.command_remove_note(this);
220                 }
221                 break;
222
223         case GDK_KEY_RELEASE:
224                 if (ev->key.keyval == GDK_Delete) {
225                         _region.apply_command();
226                 }
227                 break;
228
229         case GDK_ENTER_NOTIFY:
230                 _region.note_entered(this);
231                 _item->grab_focus();
232                 show_velocity();
233                 Keyboard::magic_widget_grab_focus();
234                 break;
235
236         case GDK_LEAVE_NOTIFY:
237                 Keyboard::magic_widget_drop_focus();
238                 if (! selected()) {
239                         hide_velocity();
240                 }
241                 _region.get_canvas_group()->grab_focus();
242                 break;
243
244         case GDK_BUTTON_PRESS:
245                 if (ev->button.button == 1) {
246                         _state = Pressed;
247                 } else if (ev->button.button == 3) {
248                         show_channel_selector();
249                 }
250                 return true;
251
252         case GDK_MOTION_NOTIFY:
253                 event_x = ev->motion.x;
254                 event_y = ev->motion.y;
255
256                 switch (_state) {
257                 case Pressed: // Drag begin
258                         if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect
259                                         && _region.mouse_state() != MidiRegionView::SelectTouchDragging
260                                         && _region.mouse_state() != MidiRegionView::EraseTouchDragging) {
261                                 _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
262                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
263                                 _state = Dragging;
264                                 _item->property_parent().get_value()->w2i(event_x, event_y);
265                                 event_x = _region.snap_to_pixel(event_x); 
266                                 last_x = event_x;
267                                 last_y = event_y;
268                                 drag_delta_x = 0;
269                                 drag_delta_note = 0;
270                                 _region.note_selected(this, true);
271                         }
272                         return true;
273
274                 case Dragging: // Drag motion
275                         if (ev->motion.is_hint) {
276                                 int t_x;
277                                 int t_y;
278                                 GdkModifierType state;
279                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
280                                 event_x = t_x;
281                                 event_y = t_y;
282                         }
283                         _item->property_parent().get_value()->w2i(event_x, event_y);
284                         
285                         // Snap
286                         event_x = _region.snap_to_pixel(event_x); 
287
288                         dx = event_x - last_x;
289                         dy = event_y - last_y;
290
291                         last_x = event_x;
292
293                         drag_delta_x += dx;
294
295                         // Snap to note rows
296                         if (abs(dy) < streamview->note_height()) {
297                                 dy = 0.0;
298                         } else {
299                                 int8_t this_delta_note;
300                                 if (dy > 0) {
301                                         this_delta_note = (int8_t)ceil(dy / streamview->note_height() / 2.0);
302                                 } else {
303                                         this_delta_note = (int8_t)floor(dy / streamview->note_height() / 2.0);
304                                 }
305                                 drag_delta_note -= this_delta_note;
306                                 dy = streamview->note_height() * this_delta_note;
307                                 last_y = last_y + dy;
308                         }
309
310                         _region.move_selection(dx, dy);
311
312                         return true;
313                 default:
314                         break;
315                 }
316                 break;
317
318         case GDK_BUTTON_RELEASE:
319                 select_mod = (ev->motion.state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK));
320                 event_x = ev->button.x;
321                 event_y = ev->button.y;
322                 _item->property_parent().get_value()->w2i(event_x, event_y);
323                 
324                 if (ev->button.button == 3) {
325                         return true;
326                 }
327                 
328                 switch (_state) {
329                 case Pressed: // Clicked
330                         if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect) {
331                                 _state = None;
332
333                                 if (_selected && !select_mod && _region.selection_size() > 1)
334                                         _region.unique_select(this);
335                                 else if (_selected)
336                                         _region.note_deselected(this, select_mod);
337                                 else
338                                         _region.note_selected(this, select_mod);
339                         } else if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditErase) {
340                                 _region.start_delta_command();
341                                 _region.command_remove_note(this);
342                                 _region.apply_command();
343                         }
344
345                         return true;
346                 case Dragging: // Dropped
347                         _item->ungrab(ev->button.time);
348                         _state = None;
349
350                         if (_note)
351                                 _region.note_dropped(this,
352                                                 _region.midi_view()->editor.pixel_to_frame(abs(drag_delta_x))
353                                                                 * ((drag_delta_x < 0.0) ? -1 : 1),
354                                                 drag_delta_note);
355                         return true;
356                 default:
357                         break;
358                 }
359
360         default:
361                 break;
362         }
363
364         return false;
365 }
366
367 } // namespace Canvas
368 } // namespace Gnome
369