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/text.h"
26 #include "note_base.h"
27 #include "midi_region_view.h"
28 #include "public_editor.h"
29 #include "editing_syms.h"
33 using namespace Gtkmm2ext;
34 using ARDOUR::MidiModel;
35 using namespace ArdourCanvas;
37 PBD::Signal1<void,NoteBase*> NoteBase::NoteBaseDeleted;
39 /// dividing the hue circle in 16 parts, hand adjusted for equal look, courtesy Thorsten Wilms
40 const uint32_t NoteBase::midi_channel_colors[16] = {
41 0xd32d2dff, 0xd36b2dff, 0xd3972dff, 0xd3d12dff,
42 0xa0d32dff, 0x7dd32dff, 0x2dd45eff, 0x2dd3c4ff,
43 0x2da5d3ff, 0x2d6fd3ff, 0x432dd3ff, 0x662dd3ff,
44 0x832dd3ff, 0xa92dd3ff, 0xd32dbfff, 0xd32d67ff
47 NoteBase::NoteBase(MidiRegionView& region, bool with_events, const boost::shared_ptr<NoteType> note)
51 // , _channel_selector_widget()
54 , _with_events (with_events)
57 , _mouse_x_fraction (-1.0)
58 , _mouse_y_fraction (-1.0)
64 NoteBaseDeleted (this);
69 // delete _channel_selector_widget;
73 NoteBase::set_item (Item* item)
76 _item->set_data ("notebase", this);
79 _item->Event.connect (sigc::mem_fun (*this, &NoteBase::event_handler));
84 NoteBase::invalidate ()
96 NoteBase::show_velocity()
99 _text = new Text (_item->parent ());
100 _text->set_ignore_events (true);
101 _text->set_color (ARDOUR_UI::config()->canvasvar_MidiNoteVelocityText.get());
102 _text->set_alignment (Pango::ALIGN_CENTER);
105 _text->set_x_position ((x0() + x1()) / 2);
106 _text->set_y_position ((y0() + y1()) / 2);
107 ostringstream velo(ios::ate);
108 velo << int(_note->velocity());
109 _text->set (velo.str ());
111 _text->raise_to_top();
115 NoteBase::hide_velocity()
122 NoteBase::on_channel_selection_change(uint16_t selection)
124 // make note change its color if its channel is not marked active
125 if ( (selection & (1 << _note->channel())) == 0 ) {
126 set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get());
127 set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get()));
129 // set the color according to the notes selection state
130 set_selected(_selected);
132 // this forces the item to update..... maybe slow...
138 NoteBase::on_channel_change(uint8_t channel)
140 _region.note_selected(this, true);
141 hide_channel_selector();
142 _region.change_channel(channel);
146 NoteBase::show_channel_selector ()
149 if (_channel_selector_widget == 0) {
151 if(_region.channel_selector_scoped_note() != 0){
152 _region.channel_selector_scoped_note()->hide_channel_selector();
153 _region.set_channel_selector_scoped_note(0);
156 SingleMidiChannelSelector* _channel_selector = new SingleMidiChannelSelector(_note->channel());
157 _channel_selector->show_all();
158 _channel_selector->channel_selected.connect(
159 sigc::mem_fun(this, &NoteBase::on_channel_change));
161 _channel_selector->clicked.connect (
162 sigc::mem_fun (this, &NoteBase::hide_channel_selector));
164 _channel_selector_widget = new Widget(*(_item->property_parent()),
167 (Gtk::Widget &) *_channel_selector);
169 _channel_selector_widget->hide();
170 _channel_selector_widget->property_height() = 100;
171 _channel_selector_widget->property_width() = 100;
172 _channel_selector_widget->raise_to_top();
173 _channel_selector_widget->show();
175 _region.set_channel_selector_scoped_note(this);
177 hide_channel_selector();
183 NoteBase::hide_channel_selector ()
186 if (_channel_selector_widget) {
187 _channel_selector_widget->hide();
188 delete _channel_selector_widget;
189 _channel_selector_widget = 0;
195 NoteBase::set_selected(bool selected)
201 _selected = selected;
202 set_fill_color (base_color ());
205 set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get()));
207 if(_region.channel_selector_scoped_note() != 0){
208 _region.channel_selector_scoped_note()->hide_channel_selector();
209 _region.set_channel_selector_scoped_note(0);
212 set_outline_color(calculate_outline(base_color()));
213 hide_channel_selector();
218 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
221 NoteBase::base_color()
223 using namespace ARDOUR;
225 ColorMode mode = _region.color_mode();
227 const uint8_t min_opacity = 15;
228 uint8_t opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
233 Gdk::Color color = _region.midi_stream_view()->get_region_color();
234 return UINT_INTERPOLATE (RGBA_TO_UINT(
235 SCALE_USHORT_TO_UINT8_T(color.get_red()),
236 SCALE_USHORT_TO_UINT8_T(color.get_green()),
237 SCALE_USHORT_TO_UINT8_T(color.get_blue()),
239 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
243 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (NoteBase::midi_channel_colors[_note->channel()],
245 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
248 return meter_style_fill_color(_note->velocity(), selected());
255 NoteBase::set_mouse_fractions (GdkEvent* ev)
258 bool set_cursor = false;
261 case GDK_MOTION_NOTIFY:
266 case GDK_ENTER_NOTIFY:
271 case GDK_BUTTON_PRESS:
272 case GDK_BUTTON_RELEASE:
277 _mouse_x_fraction = -1.0;
278 _mouse_y_fraction = -1.0;
282 boost::optional<Rect> bbox = _item->bounding_box ();
285 _item->canvas_to_item (ix, iy);
287 /* hmm, something wrong here. w2i should give item-local coordinates
288 but it doesn't. for now, finesse this.
290 ix = ix - bbox.get().x0;
291 iy = iy - bbox.get().y0;
293 /* fraction of width/height */
298 xf = ix / bbox.get().width ();
299 yf = iy / bbox.get().height ();
301 if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
305 _mouse_x_fraction = xf;
306 _mouse_y_fraction = yf;
309 if (big_enough_to_trim()) {
310 _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
312 /* pretend the mouse is in the middle, because this is not big enough
315 _region.note_mouse_position (0.5, 0.5, set_cursor);
321 NoteBase::event_handler (GdkEvent* ev)
323 if (!_region.get_time_axis_view().editor().internal_editing()) {
328 case GDK_ENTER_NOTIFY:
329 set_mouse_fractions (ev);
330 _region.note_entered (this);
333 case GDK_LEAVE_NOTIFY:
334 set_mouse_fractions (ev);
335 _region.note_left (this);
338 case GDK_MOTION_NOTIFY:
339 set_mouse_fractions (ev);
342 case GDK_BUTTON_PRESS:
343 set_mouse_fractions (ev);
344 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state) && _selected) {
345 show_channel_selector();
350 case GDK_BUTTON_RELEASE:
351 set_mouse_fractions (ev);
352 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state)) {
361 return _region.get_time_axis_view().editor().canvas_note_event (ev, _item);
365 NoteBase::mouse_near_ends () const
367 return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
368 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
372 NoteBase::big_enough_to_trim () const
374 return (x1() - x0()) > 10;