2 Copyright (C) 2007 Paul Davis
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.
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.
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.
21 #include "canvas-note-event.h"
22 #include "midi_region_view.h"
23 #include "public_editor.h"
24 #include "editing_syms.h"
28 using ARDOUR::MidiModel;
34 CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item,
35 const boost::shared_ptr<ARDOUR::Note> note)
39 , _channel_selector_widget()
46 CanvasNoteEvent::~CanvasNoteEvent()
51 if (_channel_selector_widget)
52 delete _channel_selector_widget;
56 CanvasNoteEvent::move_event(double dx, double dy)
64 CanvasNoteEvent::show_velocity(void)
66 _text = new Text(*(_item->property_parent()));
67 _text->property_x() = (x1() + x2()) /2;
68 _text->property_y() = (y1() + y2()) /2;
69 ostringstream velo(ios::ate);
70 velo << int(_note->velocity());
71 _text->property_text() = velo.str();
72 _text->property_justification() = Gtk::JUSTIFY_CENTER;
73 _text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
75 _text->lower_to_bottom();
80 CanvasNoteEvent::hide_velocity(void)
87 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
89 // make note change its color if its channel is not marked active
90 if ( (selection & (1 << _note->channel())) == 0 ) {
91 set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteFillInactiveChannel.get());
92 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteOutlineInactiveChannel.get());
94 set_fill_color(note_fill_color(_note->velocity()));
95 set_outline_color(note_outline_color(_note->velocity()));
97 // this forces the item to update..... maybe slow...
103 CanvasNoteEvent::on_channel_change(uint8_t channel)
105 _region.note_selected(this, true);
106 hide_channel_selector();
107 _region.change_channel(channel);
111 CanvasNoteEvent::show_channel_selector(void)
113 if (_channel_selector_widget == 0) {
114 cerr << "Note has channel: " << int(_note->channel()) << endl;
115 SingleMidiChannelSelector* _channel_selector = new SingleMidiChannelSelector(_note->channel());
116 _channel_selector->show_all();
117 _channel_selector->channel_selected.connect(
118 sigc::mem_fun(this, &CanvasNoteEvent::on_channel_change));
120 _channel_selector_widget =
121 new Widget(*(_item->property_parent()),
124 (Gtk::Widget &) *_channel_selector);
126 _channel_selector_widget->hide();
127 _channel_selector_widget->property_height() = 100;
128 _channel_selector_widget->property_width() = 100;
129 _channel_selector_widget->raise_to_top();
130 _channel_selector_widget->show();
132 hide_channel_selector();
137 CanvasNoteEvent::hide_channel_selector(void)
139 if (_channel_selector_widget) {
140 _channel_selector_widget->hide();
141 delete _channel_selector_widget;
142 _channel_selector_widget = 0;
147 CanvasNoteEvent::selected(bool yn)
152 set_fill_color(UINT_INTERPOLATE(note_fill_color(_note->velocity()),
153 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(), 0.1));
154 set_outline_color(ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get());
157 set_fill_color(note_fill_color(_note->velocity()));
158 set_outline_color(note_outline_color(_note->velocity()));
167 CanvasNoteEvent::on_event(GdkEvent* ev)
169 MidiStreamView *streamview = _region.midi_stream_view();
170 static uint8_t drag_delta_note = 0;
171 static double drag_delta_x = 0;
172 static double last_x, last_y;
173 double event_x, event_y, dx, dy;
175 uint8_t d_velocity = 10;
177 if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
182 if (Keyboard::modifier_state_equals (ev->scroll.state, Keyboard::Level4Modifier)) {
186 if (ev->scroll.direction == GDK_SCROLL_UP) {
187 _region.note_selected(this, true);
188 if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
189 // TODO: absolute velocity
191 _region.change_velocity(d_velocity, true);
194 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
195 _region.note_selected(this, true);
196 if (_region.mouse_state() == MidiRegionView::SelectTouchDragging) {
197 // TODO: absolute velocity
199 _region.change_velocity(-d_velocity, true);
207 if (_note && ev->key.keyval == GDK_Delete) {
209 _region.start_delta_command();
210 _region.command_remove_note(this);
214 case GDK_KEY_RELEASE:
215 if (ev->key.keyval == GDK_Delete) {
216 _region.apply_command();
220 case GDK_ENTER_NOTIFY:
221 _region.note_entered(this);
224 Keyboard::magic_widget_grab_focus();
227 case GDK_LEAVE_NOTIFY:
228 Keyboard::magic_widget_drop_focus();
232 _region.get_canvas_group()->grab_focus();
235 case GDK_BUTTON_PRESS:
236 if (ev->button.button == 1) {
238 } else if (ev->button.button == 3) {
239 show_channel_selector();
243 case GDK_MOTION_NOTIFY:
244 event_x = ev->motion.x;
245 event_y = ev->motion.y;
248 case Pressed: // Drag begin
249 if (_region.mouse_state() != MidiRegionView::SelectTouchDragging) {
250 _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
251 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
253 _item->property_parent().get_value()->w2i(event_x, event_y);
254 event_x = _region.snap_to_pixel(event_x);
259 _region.note_selected(this, true);
263 case Dragging: // Drag motion
264 if (ev->motion.is_hint) {
267 GdkModifierType state;
268 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
272 _item->property_parent().get_value()->w2i(event_x, event_y);
275 event_x = _region.snap_to_pixel(event_x);
277 dx = event_x - last_x;
278 dy = event_y - last_y;
285 if (abs(dy) < streamview->note_height()) {
288 int8_t this_delta_note;
290 this_delta_note = (int8_t)ceil(dy / streamview->note_height() / 2.0);
292 this_delta_note = (int8_t)floor(dy / streamview->note_height() / 2.0);
294 drag_delta_note -= this_delta_note;
295 dy = streamview->note_height() * this_delta_note;
296 last_y = last_y + dy;
299 _region.move_selection(dx, dy);
307 case GDK_BUTTON_RELEASE:
308 select_mod = (ev->motion.state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK));
309 event_x = ev->button.x;
310 event_y = ev->button.y;
311 _item->property_parent().get_value()->w2i(event_x, event_y);
313 if (ev->button.button == 3) {
318 case Pressed: // Clicked
319 if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditSelect) {
322 if (_selected && !select_mod && _region.selection_size() > 1)
323 _region.unique_select(this);
325 _region.note_deselected(this, select_mod);
327 _region.note_selected(this, select_mod);
328 } else if (_region.midi_view()->editor.current_midi_edit_mode() == Editing::MidiEditErase) {
329 _region.start_delta_command();
330 _region.command_remove_note(this);
331 _region.apply_command();
335 case Dragging: // Dropped
336 _item->ungrab(ev->button.time);
340 _region.note_dropped(this,
341 _region.midi_view()->editor.pixel_to_frame(abs(drag_delta_x))
342 * ((drag_delta_x < 0.0) ? -1 : 1),
356 } // namespace Canvas