Preferences/Config changes for image-surface settings
[ardour.git] / gtk2_ardour / note_base.cc
1 /*
2  * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
5  * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <iostream>
23
24 #include "gtkmm2ext/keyboard.h"
25
26 #include "evoral/Note.h"
27
28 #include "canvas/text.h"
29
30 #include "note_base.h"
31 #include "public_editor.h"
32 #include "editing_syms.h"
33 #include "keyboard.h"
34 #include "midi_region_view.h"
35
36 using namespace std;
37 using namespace Gtkmm2ext;
38 using ARDOUR::MidiModel;
39 using namespace ArdourCanvas;
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 bool             NoteBase::_color_init = false;
50 Gtkmm2ext::Color NoteBase::_selected_col = 0;
51 Gtkmm2ext::SVAModifier NoteBase::color_modifier;
52 Gtkmm2ext::Color NoteBase::velocity_color_table[128];
53
54 void
55 NoteBase::set_colors ()
56 {
57         for (uint8_t i = 0; i < 128; ++i) {
58                 velocity_color_table[i] = 0; /* out of bounds because zero alpha makes no sense  */
59         }
60         _selected_col = UIConfiguration::instance().color ("midi note selected outline");
61         color_modifier = UIConfiguration::instance().modifier ("midi note");
62 }
63
64 NoteBase::NoteBase(MidiRegionView& region, bool with_events, const boost::shared_ptr<NoteType> note)
65         : _region(region)
66         , _item (0)
67         , _text(0)
68         , _state(None)
69         , _note(note)
70         , _with_events (with_events)
71         , _selected(false)
72         , _valid (true)
73         , _mouse_x_fraction (-1.0)
74         , _mouse_y_fraction (-1.0)
75 {
76         if (!_color_init) {
77                 NoteBase::set_colors();
78                 _color_init = true;
79         }
80 }
81
82 NoteBase::~NoteBase()
83 {
84         _region.note_deleted (this);
85
86         delete _text;
87 }
88
89 void
90 NoteBase::set_item (Item* item)
91 {
92         _item = item;
93         _item->set_data ("notebase", this);
94
95         if (_with_events) {
96                 _item->Event.connect (sigc::mem_fun (*this, &NoteBase::event_handler));
97         }
98 }
99
100 void
101 NoteBase::invalidate ()
102 {
103         _valid = false;
104 }
105
106 void
107 NoteBase::validate ()
108 {
109         _valid = true;
110 }
111
112 void
113 NoteBase::show_velocity()
114 {
115         if (!_text) {
116                 _text = new Text (_item->parent ());
117                 _text->set_ignore_events (true);
118                 _text->set_color (UIConfiguration::instance().color_mod ("midi note velocity text", "midi note velocity text"));
119                 _text->set_alignment (Pango::ALIGN_CENTER);
120         }
121
122         _text->set_x_position ((x0() + x1()) / 2);
123         _text->set_y_position ((y0() + y1()) / 2);
124         ostringstream velo(ios::ate);
125         velo << int(_note->velocity());
126         _text->set (velo.str ());
127         _text->show();
128         _text->raise_to_top();
129 }
130
131 void
132 NoteBase::hide_velocity()
133 {
134         delete _text;
135         _text = 0;
136 }
137
138 void
139 NoteBase::on_channel_selection_change(uint16_t selection)
140 {
141         // make note change its color if its channel is not marked active
142         if ( (selection & (1 << _note->channel())) == 0 ) {
143                 const Gtkmm2ext::Color inactive_ch = UIConfiguration::instance().color ("midi note inactive channel");
144                 set_fill_color(inactive_ch);
145                 set_outline_color(calculate_outline(inactive_ch, _selected));
146         } else {
147                 // set the color according to the notes selection state
148                 set_selected(_selected);
149         }
150         // this forces the item to update..... maybe slow...
151         _item->hide();
152         _item->show();
153 }
154
155 void
156 NoteBase::on_channel_change(uint8_t channel)
157 {
158         _region.note_selected(this, true);
159         _region.change_channel(channel);
160 }
161
162 void
163 NoteBase::set_selected(bool selected)
164 {
165         if (!_note) {
166                 return;
167         }
168
169         _selected = selected;
170
171         const uint32_t base_col = base_color();
172         set_fill_color (base_col);
173
174         set_outline_color(calculate_outline(base_col, _selected));
175 }
176
177 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
178
179 uint32_t
180 NoteBase::base_color()
181 {
182         using namespace ARDOUR;
183
184         ColorMode mode = _region.color_mode();
185
186         const uint8_t min_opacity = 15;
187         uint8_t       opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
188
189         switch (mode) {
190         case TrackColor:
191         {
192                 const uint32_t region_color = _region.midi_stream_view()->get_region_color();
193                 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (region_color, opacity), _selected_col,
194                                          0.5);
195         }
196
197         case ChannelColors:
198                 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (NoteBase::midi_channel_colors[_note->channel()], opacity),
199                                           _selected_col, 0.5);
200
201         default:
202                 if (UIConfiguration::instance().get_use_note_color_for_velocity()) {
203                         return meter_style_fill_color(_note->velocity(), selected());
204                 } else {
205                         const uint32_t region_color = _region.midi_stream_view()->get_region_color();
206                         return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (region_color, opacity), _selected_col,
207                                                  0.5);
208                 }
209         };
210
211         return 0;
212 }
213
214 void
215 NoteBase::set_mouse_fractions (GdkEvent* ev)
216 {
217         double ix, iy;
218         bool set_cursor = false;
219
220         switch (ev->type) {
221         case GDK_MOTION_NOTIFY:
222                 ix = ev->motion.x;
223                 iy = ev->motion.y;
224                 set_cursor = true;
225                 break;
226         case GDK_ENTER_NOTIFY:
227                 ix = ev->crossing.x;
228                 iy = ev->crossing.y;
229                 set_cursor = true;
230                 break;
231         case GDK_BUTTON_PRESS:
232         case GDK_BUTTON_RELEASE:
233                 ix = ev->button.x;
234                 iy = ev->button.y;
235                 break;
236         default:
237                 _mouse_x_fraction = -1.0;
238                 _mouse_y_fraction = -1.0;
239                 return;
240         }
241
242         boost::optional<ArdourCanvas::Rect> bbox = _item->bounding_box ();
243         assert (bbox);
244
245         _item->canvas_to_item (ix, iy);
246         /* XXX: CANVAS */
247         /* hmm, something wrong here. w2i should give item-local coordinates
248            but it doesn't. for now, finesse this.
249         */
250         ix = ix - bbox.get().x0;
251         iy = iy - bbox.get().y0;
252
253         /* fraction of width/height */
254         double xf;
255         double yf;
256         bool notify = false;
257
258         xf = ix / bbox.get().width ();
259         yf = iy / bbox.get().height ();
260
261         if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
262                 notify = true;
263         }
264
265         _mouse_x_fraction = xf;
266         _mouse_y_fraction = yf;
267
268         if (notify) {
269                 if (big_enough_to_trim()) {
270                         _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
271                 } else {
272                         /* pretend the mouse is in the middle, because this is not big enough
273                            to trim right now.
274                         */
275                         _region.note_mouse_position (0.5, 0.5, set_cursor);
276                 }
277         }
278 }
279
280 bool
281 NoteBase::event_handler (GdkEvent* ev)
282 {
283         PublicEditor& editor = _region.get_time_axis_view().editor();
284         if (!editor.internal_editing()) {
285                 return false;
286         }
287
288         switch (ev->type) {
289         case GDK_ENTER_NOTIFY:
290                 _region.note_entered (this);
291                 set_mouse_fractions (ev);
292                 break;
293
294         case GDK_LEAVE_NOTIFY:
295                 set_mouse_fractions (ev);
296                 _region.note_left (this);
297                 break;
298
299         case GDK_MOTION_NOTIFY:
300                 set_mouse_fractions (ev);
301                 break;
302
303         case GDK_BUTTON_PRESS:
304                 set_mouse_fractions (ev);
305                 break;
306
307         case GDK_BUTTON_RELEASE:
308                 set_mouse_fractions (ev);
309                 break;
310
311         default:
312                 break;
313         }
314
315         return editor.canvas_note_event (ev, _item);
316 }
317
318 bool
319 NoteBase::mouse_near_ends () const
320 {
321         return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
322                 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
323 }
324
325 bool
326 NoteBase::big_enough_to_trim () const
327 {
328         return (x1() - x0()) > 10;
329 }
330
331
332 Gtkmm2ext::Color
333 NoteBase::meter_style_fill_color(uint8_t vel, bool /* selected */)
334 {
335         /* note that because vel is uint8_t, we don't need bounds checking for
336            the color lookup table.
337         */
338
339         if (velocity_color_table[vel] == 0) {
340
341                 Gtkmm2ext::Color col;
342
343                 if (vel < 32) {
344                         col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color0"), UIConfiguration::instance().color ("midi meter color1"), (vel / 32.0));
345                         col = Gtkmm2ext::change_alpha (col, color_modifier.a());
346                 } else if (vel < 64) {
347                         col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color2"), UIConfiguration::instance().color ("midi meter color3"), ((vel-32) / 32.0));
348                         col = Gtkmm2ext::change_alpha (col, color_modifier.a());
349                 } else if (vel < 100) {
350                         col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color4"), UIConfiguration::instance().color ("midi meter color5"), ((vel-64) / 36.0));
351                         col = Gtkmm2ext::change_alpha (col, color_modifier.a());
352                 } else if (vel < 112) {
353                         col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color6"), UIConfiguration::instance().color ("midi meter color7"), ((vel-100) / 12.0));
354                         col = Gtkmm2ext::change_alpha (col, color_modifier.a());
355                 } else {
356                         col =  UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color8"), UIConfiguration::instance().color ("midi meter color9"), ((vel-112) / 17.0));
357                         col = Gtkmm2ext::change_alpha (col, color_modifier.a());
358                 }
359
360                 velocity_color_table[vel] = col;
361                 return col;
362         }
363
364         return velocity_color_table[vel];
365 }
366