Trim the include tree.
[ardour.git] / gtk2_ardour / note_base.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 "evoral/Note.hpp"
25
26 #include "canvas/text.h"
27
28 #include "note_base.h"
29 #include "public_editor.h"
30 #include "editing_syms.h"
31 #include "keyboard.h"
32 #include "midi_region_view.h"
33
34 using namespace std;
35 using namespace Gtkmm2ext;
36 using ARDOUR::MidiModel;
37 using namespace ArdourCanvas;
38
39 PBD::Signal1<void,NoteBase*> NoteBase::NoteBaseDeleted;
40
41 /// dividing the hue circle in 16 parts, hand adjusted for equal look, courtesy Thorsten Wilms
42 const uint32_t NoteBase::midi_channel_colors[16] = {
43           0xd32d2dff,  0xd36b2dff,  0xd3972dff,  0xd3d12dff,
44           0xa0d32dff,  0x7dd32dff,  0x2dd45eff,  0x2dd3c4ff,
45           0x2da5d3ff,  0x2d6fd3ff,  0x432dd3ff,  0x662dd3ff,
46           0x832dd3ff,  0xa92dd3ff,  0xd32dbfff,  0xd32d67ff
47         };
48
49 NoteBase::NoteBase(MidiRegionView& region, bool with_events, const boost::shared_ptr<NoteType> note)
50         : _region(region)
51         , _item (0)
52         , _text(0)
53         , _state(None)
54         , _note(note)
55         , _with_events (with_events)
56         , _selected(false)
57         , _valid (true)
58         , _mouse_x_fraction (-1.0)
59         , _mouse_y_fraction (-1.0)
60 {
61 }
62
63 NoteBase::~NoteBase()
64 {
65         NoteBaseDeleted (this);
66
67         delete _text;
68 }
69
70 void
71 NoteBase::set_item (Item* item)
72 {
73         _item = item;
74         _item->set_data ("notebase", this);
75
76         if (_with_events) {
77                 _item->Event.connect (sigc::mem_fun (*this, &NoteBase::event_handler));
78         }
79 }
80
81 void
82 NoteBase::invalidate ()
83 {
84         _valid = false;
85 }
86
87 void
88 NoteBase::validate ()
89 {
90         _valid = true;
91 }
92
93 void
94 NoteBase::show_velocity()
95 {
96         if (!_text) {
97                 _text = new Text (_item->parent ());
98                 _text->set_ignore_events (true);
99                 _text->set_color (ARDOUR_UI::config()->get_MidiNoteVelocityText());
100                 _text->set_alignment (Pango::ALIGN_CENTER);
101         }
102
103         _text->set_x_position ((x0() + x1()) / 2);
104         _text->set_y_position ((y0() + y1()) / 2);
105         ostringstream velo(ios::ate);
106         velo << int(_note->velocity());
107         _text->set (velo.str ());
108         _text->show();
109         _text->raise_to_top();
110 }
111
112 void
113 NoteBase::hide_velocity()
114 {
115         delete _text;
116         _text = 0;
117 }
118
119 void
120 NoteBase::on_channel_selection_change(uint16_t selection)
121 {
122         // make note change its color if its channel is not marked active
123         if ( (selection & (1 << _note->channel())) == 0 ) {
124                 set_fill_color(ARDOUR_UI::config()->get_MidiNoteInactiveChannel());
125                 set_outline_color(calculate_outline(ARDOUR_UI::config()->get_MidiNoteInactiveChannel()));
126         } else {
127                 // set the color according to the notes selection state
128                 set_selected(_selected);
129         }
130         // this forces the item to update..... maybe slow...
131         _item->hide();
132         _item->show();
133 }
134
135 void
136 NoteBase::on_channel_change(uint8_t channel)
137 {
138         _region.note_selected(this, true);
139         _region.change_channel(channel);
140 }
141
142 void
143 NoteBase::set_selected(bool selected)
144 {
145         if (!_note) {
146                 return;
147         }
148
149         _selected = selected;
150         set_fill_color (base_color());
151         
152         if (_selected) {
153                 set_outline_color(calculate_outline(ARDOUR_UI::config()->get_MidiNoteSelected()));
154         } else {
155                 set_outline_color(calculate_outline(base_color()));
156         }
157
158 }
159
160 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
161
162 uint32_t
163 NoteBase::base_color()
164 {
165         using namespace ARDOUR;
166
167         ColorMode mode = _region.color_mode();
168
169         const uint8_t min_opacity = 15;
170         uint8_t       opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
171
172         switch (mode) {
173         case TrackColor:
174         {
175                 uint32_t color = _region.midi_stream_view()->get_region_color();
176                 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (color, opacity), 
177                                          ARDOUR_UI::config()->get_MidiNoteSelected(), 
178                                          0.5);
179         }
180
181         case ChannelColors:
182                 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (NoteBase::midi_channel_colors[_note->channel()], opacity), 
183                                          ARDOUR_UI::config()->get_MidiNoteSelected(), 0.5);
184
185         default:
186                 return meter_style_fill_color(_note->velocity(), selected());
187         };
188
189         return 0;
190 }
191
192 void
193 NoteBase::set_mouse_fractions (GdkEvent* ev)
194 {
195         double ix, iy;
196         bool set_cursor = false;
197
198         switch (ev->type) {
199         case GDK_MOTION_NOTIFY:
200                 ix = ev->motion.x;
201                 iy = ev->motion.y;
202                 set_cursor = true;
203                 break;
204         case GDK_ENTER_NOTIFY:
205                 ix = ev->crossing.x;
206                 iy = ev->crossing.y;
207                 set_cursor = true;
208                 break;
209         case GDK_BUTTON_PRESS:
210         case GDK_BUTTON_RELEASE:
211                 ix = ev->button.x;
212                 iy = ev->button.y;
213                 break;
214         default:
215                 _mouse_x_fraction = -1.0;
216                 _mouse_y_fraction = -1.0;
217                 return;
218         }
219
220         boost::optional<ArdourCanvas::Rect> bbox = _item->bounding_box ();
221         assert (bbox);
222
223         _item->canvas_to_item (ix, iy);
224         /* XXX: CANVAS */
225         /* hmm, something wrong here. w2i should give item-local coordinates
226            but it doesn't. for now, finesse this.
227         */
228         ix = ix - bbox.get().x0;
229         iy = iy - bbox.get().y0;
230
231         /* fraction of width/height */
232         double xf;
233         double yf;
234         bool notify = false;
235
236         xf = ix / bbox.get().width ();
237         yf = iy / bbox.get().height ();
238
239         if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
240                 notify = true;
241         }
242
243         _mouse_x_fraction = xf;
244         _mouse_y_fraction = yf;
245
246         if (notify) {
247                 if (big_enough_to_trim()) {
248                         _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
249                 } else {
250                         /* pretend the mouse is in the middle, because this is not big enough
251                            to trim right now.
252                         */
253                         _region.note_mouse_position (0.5, 0.5, set_cursor);
254                 }
255         }
256 }
257
258 bool
259 NoteBase::event_handler (GdkEvent* ev)
260 {
261         if (!_region.get_time_axis_view().editor().internal_editing()) {
262                 return false;
263         }
264
265         switch (ev->type) {
266         case GDK_ENTER_NOTIFY:
267                 _region.note_entered (this);
268                 set_mouse_fractions (ev);
269                 break;
270
271         case GDK_LEAVE_NOTIFY:
272                 set_mouse_fractions (ev);
273                 _region.note_left (this);
274                 break;
275
276         case GDK_MOTION_NOTIFY:
277                 set_mouse_fractions (ev);
278                 break;
279
280         case GDK_BUTTON_PRESS:
281                 set_mouse_fractions (ev);
282                 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state) && _selected) {
283                         _region.get_time_axis_view().editor().edit_notes (_region);
284                         return true;
285                 }
286                 break;
287
288         case GDK_BUTTON_RELEASE:
289                 set_mouse_fractions (ev);
290                 if (ev->button.button == 3 && Keyboard::no_modifiers_active (ev->button.state)) {
291                         return true;
292                 }
293                 break;
294
295         default:
296                 break;
297         }
298
299         return _region.get_time_axis_view().editor().canvas_note_event (ev, _item);
300 }
301
302 bool
303 NoteBase::mouse_near_ends () const
304 {
305         return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
306                 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
307 }
308
309 bool
310 NoteBase::big_enough_to_trim () const
311 {
312         return (x1() - x0()) > 10;
313 }
314