use new Canvas::Note object for (sustained) note display
[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
23 #include "canvas/note.h"
24 #include "canvas/debug.h"
25
26 using namespace std;
27 using namespace ArdourCanvas;
28
29 bool Note::_show_velocity_bars = true;
30
31 Note::Note (Canvas* c)
32         : Rectangle (c)
33         , _velocity (0.5)
34 {
35 }
36
37 Note::Note (Item* parent)
38         : Rectangle (parent)
39         , _velocity (0.5)
40 {
41 }
42
43 void
44 Note::set_velocity (double fract)
45 {
46         _velocity = max (0.0, min (1.0, fract));
47         redraw ();
48 }
49
50 void
51 Note::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
52 {
53         Rectangle::render (area, context);
54
55         if (_show_velocity_bars && _velocity > 0.0) {
56
57                 Rect self (item_to_window (Rectangle::get().translate (_position), false));
58
59                 if ((self.y1 - self.y0) < 5) {
60                         /* not tall enough to show a velocity bar */
61                         return;
62                 }
63
64                 /* 2 pixel margin above and below
65                    outline_width() margin on left
66                    set width based on velocity
67                 */
68
69                 self.y0  = self.y0 + 2;
70                 self.y1  = self.y1 - 2;
71                 const double width = (self.x1 - self.x0) - (2 * outline_width());
72                 self.x0  = self.x0 + outline_width();
73                 self.x1  = self.x0 + (width * _velocity);
74
75                 const Rect draw = self.intersection (area);
76
77                 if (!draw) {
78                         return;
79                 }
80
81                 Gtkmm2ext::set_source_rgba (context, Gtkmm2ext::contrasting_text_color (fill_color()));
82                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
83                 context->fill ();
84         }
85 }