fix operation of right-click on selected MIDI note to bring up the note editor. this...
[ardour.git] / gtk2_ardour / canvas-note-event.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David 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
22 #include "gtkmm2ext/keyboard.h"
23
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"
29 #include "keyboard.h"
30
31 using namespace std;
32 using namespace Gtkmm2ext;
33 using ARDOUR::MidiModel;
34
35 namespace Gnome {
36 namespace Canvas {
37
38 PBD::Signal1<void,CanvasNoteEvent*> CanvasNoteEvent::CanvasNoteEventDeleted;
39
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
46         };
47
48 CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item, const boost::shared_ptr<NoteType> note)
49         : _region(region)
50         , _item(item)
51         , _text(0)
52         , _channel_selector_widget()
53         , _state(None)
54         , _note(note)
55         , _selected(false)
56         , _valid (true)
57         , _mouse_x_fraction (-1.0)
58         , _mouse_y_fraction (-1.0)
59         , _channel_selection (0xffff)
60 {
61 }
62
63 CanvasNoteEvent::~CanvasNoteEvent()
64 {
65         CanvasNoteEventDeleted (this);
66
67         if (_text) {
68                 _text->hide();
69                 delete _text;
70         }
71
72         delete _channel_selector_widget;
73 }
74
75 void
76 CanvasNoteEvent::invalidate ()
77 {
78         _valid = false;
79 }
80
81 void
82 CanvasNoteEvent::validate ()
83 {
84         _valid = true;
85 }
86
87 void
88 CanvasNoteEvent::show_velocity()
89 {
90         if (!_text) {
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;
94         }
95
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();
101         _text->show();
102         _text->raise_to_top();
103 }
104
105 void
106 CanvasNoteEvent::hide_velocity()
107 {
108         if (_text) {
109                 _text->hide();
110                 delete _text;
111                 _text = 0;
112         }
113 }
114
115 void
116 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
117 {
118         _channel_selection = selection;
119         
120         /* this takes into account whether or not the note should be drawn as inactive */
121         set_selected (_selected);
122
123         // this forces the item to update..... maybe slow...
124         _item->hide();
125         _item->show();
126 }
127
128 void
129 CanvasNoteEvent::on_channel_change(uint8_t channel)
130 {
131         _region.note_selected(this, true);
132         hide_channel_selector();
133         _region.change_channel(channel);
134 }
135
136 void
137 CanvasNoteEvent::show_channel_selector(void)
138 {
139         if (_channel_selector_widget == 0) {
140
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);
144                 }
145
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));
150
151                 _channel_selector->clicked.connect (
152                         sigc::mem_fun (this, &CanvasNoteEvent::hide_channel_selector));
153
154                 _channel_selector_widget = new Widget(*(_item->property_parent()),
155                                                       x1(),
156                                                       y2() + 2,
157                                                       (Gtk::Widget &) *_channel_selector);
158
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();
164
165                 _region.set_channel_selector_scoped_note(this);
166         } else {
167                 hide_channel_selector();
168         }
169 }
170
171 void
172 CanvasNoteEvent::hide_channel_selector(void)
173 {
174         if (_channel_selector_widget) {
175                 _channel_selector_widget->hide();
176                 delete _channel_selector_widget;
177                 _channel_selector_widget = 0;
178         }
179 }
180
181 void
182 CanvasNoteEvent::set_selected(bool selected)
183 {
184         if (!_note) {
185                 return;
186         }
187
188         _selected = selected;
189
190         bool const active = (_channel_selection & (1 << _note->channel())) != 0;
191
192         if (_selected && active) {
193                 set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get()));
194
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);
198                 }
199
200                 set_fill_color (base_color ());
201
202         } else {
203
204                 if (active) {
205                         set_fill_color(base_color());
206                         set_outline_color(calculate_outline(base_color()));
207                 } else {
208                         set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get());
209                         set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get()));
210                 }
211                 
212                 hide_channel_selector();
213         }
214 }
215
216 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
217
218 uint32_t
219 CanvasNoteEvent::base_color()
220 {
221         using namespace ARDOUR;
222
223         ColorMode mode = _region.color_mode();
224
225         const uint8_t min_opacity = 15;
226         uint8_t       opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
227
228         switch (mode) {
229         case TrackColor:
230         {
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()),
236                                                  opacity),
237                                          ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
238         }
239
240         case ChannelColors:
241                 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (CanvasNoteEvent::midi_channel_colors[_note->channel()],
242                                                              opacity),
243                                          ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
244
245         default:
246                 return meter_style_fill_color(_note->velocity(), selected());
247         };
248
249         return 0;
250 }
251
252 void
253 CanvasNoteEvent::set_mouse_fractions (GdkEvent* ev)
254 {
255         double ix, iy;
256         double bx1, bx2, by1, by2;
257         bool set_cursor = false;
258
259         switch (ev->type) {
260         case GDK_MOTION_NOTIFY:
261                 ix = ev->motion.x;
262                 iy = ev->motion.y;
263                 set_cursor = true;
264                 break;
265         case GDK_ENTER_NOTIFY:
266                 ix = ev->crossing.x;
267                 iy = ev->crossing.y;
268                 set_cursor = true;
269                 break;
270         case GDK_BUTTON_PRESS:
271         case GDK_BUTTON_RELEASE:
272                 ix = ev->button.x;
273                 iy = ev->button.y;
274                 break;
275         default:
276                 _mouse_x_fraction = -1.0;
277                 _mouse_y_fraction = -1.0;
278                 return;
279         }
280
281         _item->get_bounds (bx1, by1, bx2, by2);
282         _item->w2i (ix, iy);
283         /* hmm, something wrong here. w2i should give item-local coordinates
284            but it doesn't. for now, finesse this.
285         */
286         ix = ix - bx1;
287         iy = iy - by1;
288
289         /* fraction of width/height */
290         double xf;
291         double yf;
292         bool notify = false;
293
294         xf = ix / (bx2 - bx1);
295         yf = iy / (by2 - by1);
296
297         if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
298                 notify = true;
299         }
300
301         _mouse_x_fraction = xf;
302         _mouse_y_fraction = yf;
303
304         if (notify) {
305                 if (big_enough_to_trim()) {
306                         _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
307                 } else {
308                         /* pretend the mouse is in the middle, because this is not big enough
309                            to trim right now.
310                         */
311                         _region.note_mouse_position (0.5, 0.5, set_cursor);
312                 }
313         }
314 }
315
316 bool
317 CanvasNoteEvent::on_event(GdkEvent* ev)
318 {
319         if (!_region.get_time_axis_view().editor().internal_editing()) {
320                 return false;
321         }
322
323         switch (ev->type) {
324         case GDK_ENTER_NOTIFY:
325                 set_mouse_fractions (ev);
326                 _region.note_entered (this);
327                 break;
328
329         case GDK_LEAVE_NOTIFY:
330                 set_mouse_fractions (ev);
331                 _region.note_left (this);
332                 break;
333
334         case GDK_MOTION_NOTIFY:
335                 set_mouse_fractions (ev);
336                 break;
337
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);
342                         return true;
343                 }
344                 break;
345
346         case GDK_BUTTON_RELEASE:
347                 set_mouse_fractions (ev);
348                 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state)) {
349                         return true;
350                 }
351                 break;
352
353         default:
354                 break;
355         }
356
357         return false;
358 }
359
360 bool
361 CanvasNoteEvent::mouse_near_ends () const
362 {
363         return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
364                 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
365 }
366
367 bool
368 CanvasNoteEvent::big_enough_to_trim () const
369 {
370         return (x2() - x1()) > 20; /* canvas units, really pixels */
371 }
372
373 } // namespace Canvas
374 } // namespace Gnome
375