Tempo ramps - clean up tempo curve a bit.
[ardour.git] / gtk2_ardour / tempo_curve.cc
1 #include <sigc++/bind.h>
2 #include "ardour/tempo.h"
3
4 #include "canvas/rectangle.h"
5 #include "canvas/container.h"
6 #include "canvas/curve.h"
7 #include "canvas/canvas.h"
8 #include "canvas/debug.h"
9
10 #include "ui_config.h"
11
12 #include "tempo_curve.h"
13 #include "public_editor.h"
14 #include "utils.h"
15 #include "rgb_macros.h"
16
17 #include <gtkmm2ext/utils.h>
18
19 #include "i18n.h"
20
21 PBD::Signal1<void,TempoCurve*> TempoCurve::CatchDeletion;
22
23 static double curve_height = 13.0;
24
25 void TempoCurve::setup_sizes(const double timebar_height)
26 {
27         curve_height = floor (timebar_height) - 2;
28 }
29
30 TempoCurve::TempoCurve (PublicEditor& ed, ArdourCanvas::Container& parent, guint32 rgba, ARDOUR::TempoSection& temp, framepos_t frame, bool handle_events)
31
32         : editor (ed)
33         , _parent (&parent)
34         , _curve (0)
35         , _shown (false)
36         , _color (rgba)
37         , _min_tempo (temp.beats_per_minute())
38         , _max_tempo (temp.beats_per_minute())
39         , _tempo (temp)
40
41
42 {
43
44         points = new ArdourCanvas::Points ();
45         points->push_back (ArdourCanvas::Duple (0.0, 0.0));
46         points->push_back (ArdourCanvas::Duple (1.0, 0.0));
47         points->push_back (ArdourCanvas::Duple (1.0, curve_height));
48         points->push_back (ArdourCanvas::Duple (0.0, curve_height));
49
50         frame_position = frame;
51         unit_position = editor.sample_to_pixel (frame);
52
53         group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 0));
54 #ifdef CANVAS_DEBUG
55         group->name = string_compose ("Marker::group for %1", _tempo.beats_per_minute());
56 #endif
57
58         _background = new ArdourCanvas::Rectangle (group);
59 #ifdef CANVAS_DEBUG
60         _background->name = string_compose ("TempoCurve::_background for %1", _tempo.beats_per_minute());
61 #endif
62         _background->set_x0 (0.0);
63         _background->set_x1 (ArdourCanvas::COORD_MAX);
64         _background->set_outline_what (ArdourCanvas::Rectangle::What(0));
65         _curve = new ArdourCanvas::Curve (group);
66 #ifdef CANVAS_DEBUG
67         _curve->name = string_compose ("TempoCurve::_curve for %1", _tempo.beats_per_minute());
68 #endif
69         _curve->set_fill_mode (ArdourCanvas::Curve::Inside);
70         _curve->set_points_per_segment (32);
71         _curve->set (*points);
72
73         set_color_rgba (rgba);
74
75         editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
76
77         /* events will be handled by both the group and the mark itself, so
78          * make sure they can both be used to lookup this object.
79          */
80
81         group->set_data ("marker", this);
82
83         if (handle_events) {
84                 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
85         }
86         set_position (_tempo.frame(), UINT32_MAX);
87         _curve->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), group, this));
88         _background->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), group, this));
89
90 }
91
92 TempoCurve::~TempoCurve ()
93 {
94         CatchDeletion (this); /* EMIT SIGNAL */
95
96         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
97         delete group;
98 }
99
100 void TempoCurve::reparent(ArdourCanvas::Container & parent)
101 {
102         group->reparent (&parent);
103         _parent = &parent;
104 }
105
106 void
107 TempoCurve::canvas_height_set (double h)
108 {
109         _canvas_height = h;
110 }
111
112 ArdourCanvas::Item&
113 TempoCurve::the_item() const
114 {
115         return *group;
116 }
117
118 void
119 TempoCurve::set_position (framepos_t frame, framepos_t end_frame)
120 {
121         points->clear();
122         unit_position = editor.sample_to_pixel (frame);
123         group->set_x_position (unit_position);
124         frame_position = frame;
125         _end_frame = end_frame;
126
127         const double tempo_delta = max (10.0, _max_tempo - _min_tempo);
128         double max_y = 0.0;
129         points = new ArdourCanvas::Points ();
130
131         if (end_frame == UINT32_MAX) {
132                 _curve->set_fill_mode (ArdourCanvas::Curve::None);
133                 const double tempo_at = _tempo.tempo_at_frame (frame, editor.session()->frame_rate()) * _tempo.note_type();
134                 const double y2_pos =  (curve_height + 2.0) - (((tempo_at - _min_tempo) / (tempo_delta)) * curve_height);
135                 max_y = y2_pos;
136                 points->push_back (ArdourCanvas::Duple (0.0, y2_pos));
137                 points->push_back (ArdourCanvas::Duple (ArdourCanvas::COORD_MAX - 5.0, y2_pos));
138
139         } else {
140                 _curve->set_fill_mode (ArdourCanvas::Curve::Inside);
141                 const framepos_t frame_step = (end_frame - frame) / 32;
142                 framepos_t current_frame = frame;
143                 while (current_frame < end_frame) {
144                         const double tempo_at = _tempo.tempo_at_frame (current_frame, editor.session()->frame_rate()) * _tempo.note_type();
145                         const double y2_pos = (curve_height + 2.0) - (((tempo_at - _min_tempo) / (tempo_delta)) * curve_height);
146
147                         points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_frame - frame), y2_pos));
148                         max_y = max (y2_pos, max_y);
149                         current_frame += frame_step;
150                 }
151         }
152
153         /* the background fills the gap between the bottom of the curve and the time bar */
154         _background->set_x0 (0.0);
155         _background->set_x1 (editor.sample_to_pixel (end_frame - frame));
156         _background->set_y0 (max_y + 1.0);
157         _background->set_y1 (curve_height + 2.0);
158
159         if (max_y == curve_height + 2.0) {
160                 _background->hide();
161         } else {
162                 _background->show();
163         }
164
165         _curve->set (*points);
166 }
167
168 void
169 TempoCurve::reposition ()
170 {
171         set_position (frame_position, _end_frame);
172 }
173
174 void
175 TempoCurve::show ()
176 {
177         _shown = true;
178
179         group->show ();
180 }
181
182 void
183 TempoCurve::hide ()
184 {
185         _shown = false;
186
187         group->hide ();
188 }
189
190 void
191 TempoCurve::set_color_rgba (uint32_t c)
192 {
193         _color = c;
194         _curve->set_fill_color (UIConfiguration::instance().color_mod ("selection rect", "selection rect"));
195         _curve->set_outline_color (_color);
196
197         _background->set_fill (true);
198         _background->set_fill_color (UIConfiguration::instance().color_mod ("selection rect", "selection rect"));
199 }