1 #include <sigc++/bind.h>
2 #include "ardour/tempo.h"
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"
10 #include "ui_config.h"
12 #include "tempo_curve.h"
13 #include "public_editor.h"
15 #include "rgb_macros.h"
17 #include <gtkmm2ext/utils.h>
21 PBD::Signal1<void,TempoCurve*> TempoCurve::CatchDeletion;
23 static double curve_height = 13.0;
25 void TempoCurve::setup_sizes(const double timebar_height)
27 curve_height = floor (timebar_height) - 2.5;
29 /* ignores Tempo note type - only note_types_per_minute is potentially curved */
30 TempoCurve::TempoCurve (PublicEditor& ed, ArdourCanvas::Container& parent, guint32 rgba, ARDOUR::TempoSection& temp, samplepos_t sample, bool handle_events)
37 , _min_tempo (temp.note_types_per_minute())
38 , _max_tempo (temp.note_types_per_minute())
43 sample_position = sample;
44 unit_position = editor.sample_to_pixel (sample);
46 group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 1));
48 group->name = string_compose ("TempoCurve::group for %1", _tempo.note_types_per_minute());
51 _curve = new ArdourCanvas::FramedCurve (group);
53 _curve->name = string_compose ("TempoCurve::curve for %1", _tempo.note_types_per_minute());
55 _curve->set_points_per_segment (3);
56 points = new ArdourCanvas::Points ();
57 _curve->set (*points);
59 _start_text = new ArdourCanvas::Text (group);
60 _end_text = new ArdourCanvas::Text (group);
61 _start_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
62 _end_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
63 _start_text->set_color (RGBA_TO_UINT (255,255,255,255));
64 _end_text->set_color (RGBA_TO_UINT (255,255,255,255));
66 snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
67 _start_text->set (buf);
68 snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
71 set_color_rgba (rgba);
73 editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
75 /* events will be handled by both the group and the mark itself, so
76 * make sure they can both be used to lookup this object.
79 _curve->set_data ("tempo curve", this);
82 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
85 group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
89 TempoCurve::~TempoCurve ()
91 CatchDeletion (this); /* EMIT SIGNAL */
93 /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
97 void TempoCurve::reparent(ArdourCanvas::Container & parent)
99 group->reparent (&parent);
104 TempoCurve::canvas_height_set (double h)
110 TempoCurve::the_item() const
116 TempoCurve::set_position (samplepos_t sample, samplepos_t end_sample)
118 unit_position = editor.sample_to_pixel (sample);
119 group->set_x_position (unit_position);
120 sample_position = sample;
121 _end_sample = end_sample;
124 points = new ArdourCanvas::Points ();
126 points->push_back (ArdourCanvas::Duple (0.0, curve_height));
128 if (sample >= end_sample) {
129 /* shouldn't happen but ..*/
130 const double tempo_at = _tempo.note_types_per_minute();
131 const double y_pos = (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
133 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
134 points->push_back (ArdourCanvas::Duple (1.0, y_pos));
136 } else if (_tempo.type() == ARDOUR::TempoSection::Constant || _tempo.c() == 0.0) {
137 const double tempo_at = _tempo.note_types_per_minute();
138 const double y_pos = (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
140 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
141 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), y_pos));
144 const samplepos_t sample_step = max ((end_sample - sample) / 5, (samplepos_t) 1);
145 samplepos_t current_sample = sample;
147 while (current_sample < end_sample) {
148 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (current_sample)).note_types_per_minute();
149 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
151 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_sample - sample), min (y_pos, curve_height)));
153 current_sample += sample_step;
156 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (end_sample)).note_types_per_minute();
157 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
159 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), min (y_pos, curve_height)));
162 _curve->set (*points);
165 snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
166 _start_text->set (buf);
167 snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
168 _end_text->set (buf);
170 _start_text->set_position (ArdourCanvas::Duple (10, .5 ));
171 _end_text->set_position (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample) - _end_text->text_width() - 10, .5 ));
173 if (_end_text->text_width() + _start_text->text_width() + 20 > editor.sample_to_pixel (end_sample - sample)) {
183 TempoCurve::reposition ()
185 set_position (sample_position, _end_sample);
205 TempoCurve::set_color_rgba (uint32_t c)
208 _curve->set_fill_color (UIConfiguration::instance().color_mod (_color, "selection rect"));
209 _curve->set_outline_color (_color);