Tempo ramps - improve constraint mod + bbt dragging begavior.
[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         _curve->set_data ("tempo curve", this);
82         _background->set_data ("tempo curve", this);
83
84         if (handle_events) {
85                 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
86         }
87
88         set_position (_tempo.frame(), UINT32_MAX);
89         _curve->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
90         _background->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _background, this));
91
92 }
93
94 TempoCurve::~TempoCurve ()
95 {
96         CatchDeletion (this); /* EMIT SIGNAL */
97
98         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
99         delete group;
100 }
101
102 void TempoCurve::reparent(ArdourCanvas::Container & parent)
103 {
104         group->reparent (&parent);
105         _parent = &parent;
106 }
107
108 void
109 TempoCurve::canvas_height_set (double h)
110 {
111         _canvas_height = h;
112 }
113
114 ArdourCanvas::Item&
115 TempoCurve::the_item() const
116 {
117         return *group;
118 }
119
120 void
121 TempoCurve::set_position (framepos_t frame, framepos_t end_frame)
122 {
123         points->clear();
124         unit_position = editor.sample_to_pixel (frame);
125         group->set_x_position (unit_position);
126         frame_position = frame;
127         _end_frame = end_frame;
128
129         const double tempo_delta = max (10.0, _max_tempo - _min_tempo);
130         double max_y = 0.0;
131         points = new ArdourCanvas::Points ();
132
133         if (end_frame == UINT32_MAX) {
134                 _curve->set_fill_mode (ArdourCanvas::Curve::None);
135                 const double tempo_at = _tempo.tempo_at_frame (frame, editor.session()->frame_rate()) * _tempo.note_type();
136                 const double y2_pos =  (curve_height + 2.0) - (((tempo_at - _min_tempo) / (tempo_delta)) * curve_height);
137                 max_y = y2_pos;
138                 points->push_back (ArdourCanvas::Duple (0.0, y2_pos));
139                 points->push_back (ArdourCanvas::Duple (ArdourCanvas::COORD_MAX - 5.0, y2_pos));
140
141         } else {
142                 _curve->set_fill_mode (ArdourCanvas::Curve::Inside);
143                 const framepos_t frame_step = (end_frame - frame) / 32;
144                 framepos_t current_frame = frame;
145                 while (current_frame < end_frame) {
146                         const double tempo_at = _tempo.tempo_at_frame (current_frame, editor.session()->frame_rate()) * _tempo.note_type();
147                         const double y2_pos = (curve_height + 2.0) - (((tempo_at - _min_tempo) / (tempo_delta)) * curve_height);
148
149                         points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_frame - frame), y2_pos));
150                         max_y = max (y2_pos, max_y);
151                         current_frame += frame_step;
152                 }
153         }
154
155         /* the background fills the gap between the bottom of the curve and the time bar */
156         _background->set_x0 (0.0);
157         _background->set_x1 (editor.sample_to_pixel (end_frame - frame));
158         _background->set_y0 (max_y + 1.0);
159         _background->set_y1 (curve_height + 2.0);
160
161         if (max_y == curve_height + 2.0) {
162                 _background->hide();
163         } else {
164                 _background->show();
165         }
166
167         _curve->set (*points);
168 }
169
170 void
171 TempoCurve::reposition ()
172 {
173         set_position (frame_position, _end_frame);
174 }
175
176 void
177 TempoCurve::show ()
178 {
179         _shown = true;
180
181         group->show ();
182 }
183
184 void
185 TempoCurve::hide ()
186 {
187         _shown = false;
188
189         group->hide ();
190 }
191
192 void
193 TempoCurve::set_color_rgba (uint32_t c)
194 {
195         _color = c;
196         _curve->set_fill_color (UIConfiguration::instance().color_mod ("selection rect", "selection rect"));
197         _curve->set_outline_color (_color);
198
199         _background->set_fill (true);
200         _background->set_fill_color (UIConfiguration::instance().color_mod ("selection rect", "selection rect"));
201 }