2 Copyright (C) 2007 Paul Davis
3 Author: David Robillard
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.
22 #include "gtkmm2ext/keyboard.h"
24 #include "canvas-note-event.h"
25 #include "midi_channel_selector.h"
26 #include "midi_region_view.h"
27 #include "public_editor.h"
28 #include "editing_syms.h"
32 using namespace Gtkmm2ext;
33 using ARDOUR::MidiModel;
38 PBD::Signal1<void,CanvasNoteEvent*> CanvasNoteEvent::CanvasNoteEventDeleted;
40 /// dividing the hue circle in 16 parts, hand adjusted for equal look, courtesy Thorsten Wilms
41 const uint32_t CanvasNoteEvent::midi_channel_colors[16] = {
42 0xd32d2dff, 0xd36b2dff, 0xd3972dff, 0xd3d12dff,
43 0xa0d32dff, 0x7dd32dff, 0x2dd45eff, 0x2dd3c4ff,
44 0x2da5d3ff, 0x2d6fd3ff, 0x432dd3ff, 0x662dd3ff,
45 0x832dd3ff, 0xa92dd3ff, 0xd32dbfff, 0xd32d67ff
48 CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item, const boost::shared_ptr<NoteType> note)
52 , _channel_selector_widget()
57 , _mouse_x_fraction (-1.0)
58 , _mouse_y_fraction (-1.0)
59 , _channel_selection (0xffff)
63 CanvasNoteEvent::~CanvasNoteEvent()
65 CanvasNoteEventDeleted (this);
72 delete _channel_selector_widget;
76 CanvasNoteEvent::invalidate ()
82 CanvasNoteEvent::validate ()
88 CanvasNoteEvent::show_velocity()
91 _text = new NoEventText (*(_item->property_parent()));
92 _text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiNoteVelocityText.get();
93 _text->property_justification() = Gtk::JUSTIFY_CENTER;
96 _text->property_x() = (x1() + x2()) /2;
97 _text->property_y() = (y1() + y2()) /2;
98 ostringstream velo(ios::ate);
99 velo << int(_note->velocity());
100 _text->property_text() = velo.str();
102 _text->raise_to_top();
106 CanvasNoteEvent::hide_velocity()
116 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
118 _channel_selection = selection;
120 /* this takes into account whether or not the note should be drawn as inactive */
121 set_selected (_selected);
123 // this forces the item to update..... maybe slow...
129 CanvasNoteEvent::on_channel_change(uint8_t channel)
131 _region.note_selected(this, true);
132 hide_channel_selector();
133 _region.change_channel(channel);
137 CanvasNoteEvent::show_channel_selector(void)
139 if (_channel_selector_widget == 0) {
141 if(_region.channel_selector_scoped_note() != 0){
142 _region.channel_selector_scoped_note()->hide_channel_selector();
143 _region.set_channel_selector_scoped_note(0);
146 SingleMidiChannelSelector* _channel_selector = new SingleMidiChannelSelector(_note->channel());
147 _channel_selector->show_all();
148 _channel_selector->channel_selected.connect(
149 sigc::mem_fun(this, &CanvasNoteEvent::on_channel_change));
151 _channel_selector->clicked.connect (
152 sigc::mem_fun (this, &CanvasNoteEvent::hide_channel_selector));
154 _channel_selector_widget = new Widget(*(_item->property_parent()),
157 (Gtk::Widget &) *_channel_selector);
159 _channel_selector_widget->hide();
160 _channel_selector_widget->property_height() = 100;
161 _channel_selector_widget->property_width() = 100;
162 _channel_selector_widget->raise_to_top();
163 _channel_selector_widget->show();
165 _region.set_channel_selector_scoped_note(this);
167 hide_channel_selector();
172 CanvasNoteEvent::hide_channel_selector(void)
174 if (_channel_selector_widget) {
175 _channel_selector_widget->hide();
176 delete _channel_selector_widget;
177 _channel_selector_widget = 0;
182 CanvasNoteEvent::set_selected(bool selected)
188 _selected = selected;
190 bool const active = (_channel_selection & (1 << _note->channel())) != 0;
192 if (_selected && active) {
193 set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get()));
195 if(_region.channel_selector_scoped_note() != 0){
196 _region.channel_selector_scoped_note()->hide_channel_selector();
197 _region.set_channel_selector_scoped_note(0);
200 set_fill_color (base_color ());
205 set_fill_color(base_color());
206 set_outline_color(calculate_outline(base_color()));
208 set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get());
209 set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get()));
212 hide_channel_selector();
216 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
219 CanvasNoteEvent::base_color()
221 using namespace ARDOUR;
223 ColorMode mode = _region.color_mode();
225 const uint8_t min_opacity = 15;
226 uint8_t opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
231 Gdk::Color color = _region.midi_stream_view()->get_region_color();
232 return UINT_INTERPOLATE (RGBA_TO_UINT(
233 SCALE_USHORT_TO_UINT8_T(color.get_red()),
234 SCALE_USHORT_TO_UINT8_T(color.get_green()),
235 SCALE_USHORT_TO_UINT8_T(color.get_blue()),
237 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
241 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (CanvasNoteEvent::midi_channel_colors[_note->channel()],
243 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
246 return meter_style_fill_color(_note->velocity(), selected());
253 CanvasNoteEvent::set_mouse_fractions (GdkEvent* ev)
256 double bx1, bx2, by1, by2;
257 bool set_cursor = false;
260 case GDK_MOTION_NOTIFY:
265 case GDK_ENTER_NOTIFY:
270 case GDK_BUTTON_PRESS:
271 case GDK_BUTTON_RELEASE:
276 _mouse_x_fraction = -1.0;
277 _mouse_y_fraction = -1.0;
281 _item->get_bounds (bx1, by1, bx2, by2);
283 /* hmm, something wrong here. w2i should give item-local coordinates
284 but it doesn't. for now, finesse this.
289 /* fraction of width/height */
294 xf = ix / (bx2 - bx1);
295 yf = iy / (by2 - by1);
297 if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
301 _mouse_x_fraction = xf;
302 _mouse_y_fraction = yf;
305 if (big_enough_to_trim()) {
306 _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
308 /* pretend the mouse is in the middle, because this is not big enough
311 _region.note_mouse_position (0.5, 0.5, set_cursor);
317 CanvasNoteEvent::on_event(GdkEvent* ev)
319 if (!_region.get_time_axis_view().editor().internal_editing()) {
324 case GDK_ENTER_NOTIFY:
325 set_mouse_fractions (ev);
326 _region.note_entered (this);
329 case GDK_LEAVE_NOTIFY:
330 set_mouse_fractions (ev);
331 _region.note_left (this);
334 case GDK_MOTION_NOTIFY:
335 set_mouse_fractions (ev);
338 case GDK_BUTTON_PRESS:
339 set_mouse_fractions (ev);
340 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state) && _selected) {
341 _region.get_time_axis_view().editor().edit_notes (_region);
346 case GDK_BUTTON_RELEASE:
347 set_mouse_fractions (ev);
348 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state)) {
361 CanvasNoteEvent::mouse_near_ends () const
363 return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
364 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
368 CanvasNoteEvent::big_enough_to_trim () const
370 return (x2() - x1()) > 20; /* canvas units, really pixels */
373 } // namespace Canvas