Only show user-presets in favorite sidebar
[ardour.git] / libs / canvas / note.cc
1 /*
2     Copyright (C) 2018 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cairomm/context.h>
20
21 #include "gtkmm2ext/colors.h"
22 #include "gtkmm2ext/rgb_macros.h"
23
24 #include "canvas/note.h"
25 #include "canvas/debug.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 bool Note::_show_velocity_bars = true;
31
32 void
33 Note::set_show_velocity_bars (bool yn)
34 {
35         _show_velocity_bars = yn;
36 }
37
38 Note::Note (Canvas* c)
39         : Rectangle (c)
40         , _velocity (0.0)
41         , _velocity_color (0)
42 {
43 }
44
45 Note::Note (Item* parent)
46         : Rectangle (parent)
47         , _velocity (0.0)
48         , _velocity_color (0)
49 {
50 }
51
52 void
53 Note::set_velocity (double fract)
54 {
55         _velocity = max (0.0, min (1.0, fract));
56         redraw ();
57 }
58
59 void
60 Note::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
61 {
62         Rectangle::render (area, context);
63
64         if (_show_velocity_bars && _velocity > 0.0) {
65
66                 Rect self (item_to_window (Rectangle::get().translate (_position), false));
67
68                 if ((self.y1 - self.y0) < ((outline_width() * 2) + 1)) {
69                         /* not tall enough to show a velocity bar */
70                         return;
71                 }
72
73                 /* 2 pixel margin above and below (taking outline width into account).
74                    outline_width() margin on left.
75                    set width based on velocity.
76                 */
77                 const double center = (self.y1 - self.y0) * 0.5;
78                 self.y1  = self.y0 + center + 2;
79                 self.y0  = self.y0 + center - 1;
80                 const double width = (self.x1 - self.x0) - (2 * outline_width());
81                 self.x0  = self.x0 + outline_width()+1;
82                 self.x1  = self.x0 + ((width-2) * _velocity);
83
84                 const Rect draw = self.intersection (area);
85
86                 if (!draw) {
87                         return;
88                 }
89
90                 Gtkmm2ext::set_source_rgba (context, _velocity_color);
91                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
92                 context->fill ();
93         }
94 }
95
96 void
97 Note::set_fill_color (Gtkmm2ext::Color c)
98 {
99         Fill::set_fill_color (c);
100         _velocity_color = UINT_INTERPOLATE (c, 0x000000ff, 0.5);
101 }