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