improve sysex data display.
[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 "pbd/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.5;
28 }
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, 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.note_types_per_minute())
38         , _max_tempo (temp.note_types_per_minute())
39         , _tempo (temp)
40
41 {
42         frame_position = frame;
43         unit_position = editor.sample_to_pixel (frame);
44
45         group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 1));
46 #ifdef CANVAS_DEBUG
47         group->name = string_compose ("TempoCurve::group for %1", _tempo.note_types_per_minute());
48 #endif
49
50         _curve = new ArdourCanvas::FramedCurve (group);
51 #ifdef CANVAS_DEBUG
52         _curve->name = string_compose ("TempoCurve::curve for %1", _tempo.note_types_per_minute());
53 #endif
54         _curve->set_points_per_segment (3);
55         points = new ArdourCanvas::Points ();
56         _curve->set (*points);
57
58         set_color_rgba (rgba);
59
60         editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
61
62         /* events will be handled by both the group and the mark itself, so
63          * make sure they can both be used to lookup this object.
64          */
65
66         _curve->set_data ("tempo curve", this);
67
68         if (handle_events) {
69                 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
70         }
71
72         _curve->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
73
74 }
75
76 TempoCurve::~TempoCurve ()
77 {
78         CatchDeletion (this); /* EMIT SIGNAL */
79
80         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
81         delete group;
82 }
83
84 void TempoCurve::reparent(ArdourCanvas::Container & parent)
85 {
86         group->reparent (&parent);
87         _parent = &parent;
88 }
89
90 void
91 TempoCurve::canvas_height_set (double h)
92 {
93         _canvas_height = h;
94 }
95
96 ArdourCanvas::Item&
97 TempoCurve::the_item() const
98 {
99         return *group;
100 }
101
102 void
103 TempoCurve::set_position (framepos_t frame, framepos_t end_frame)
104 {
105         unit_position = editor.sample_to_pixel (frame);
106         group->set_x_position (unit_position);
107         frame_position = frame;
108         _end_frame = end_frame;
109
110         points->clear();
111         points = new ArdourCanvas::Points ();
112
113         points->push_back (ArdourCanvas::Duple (0.0, curve_height));
114
115         if (frame >= end_frame) {
116                 /* shouldn't happen but ..*/
117                 const double tempo_at = _tempo.note_types_per_minute();
118                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
119
120                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
121                 points->push_back (ArdourCanvas::Duple (1.0, y_pos));
122
123         } else if (_tempo.type() == ARDOUR::TempoSection::Constant || _tempo.c() == 0.0) {
124                 const double tempo_at = _tempo.note_types_per_minute();
125                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
126
127                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
128                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_frame - frame), y_pos));
129         } else {
130
131                 const framepos_t frame_step = max ((end_frame - frame) / 5, (framepos_t) 1);
132                 framepos_t current_frame = frame;
133
134                 while (current_frame < end_frame) {
135                         const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_frame (current_frame)).note_types_per_minute();
136                         const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
137
138                         points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_frame - frame), min (y_pos, curve_height)));
139
140                         current_frame += frame_step;
141                 }
142
143                 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_frame (end_frame)).note_types_per_minute();
144                 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
145
146                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_frame - frame), min (y_pos, curve_height)));
147         }
148
149         _curve->set (*points);
150 }
151
152 void
153 TempoCurve::reposition ()
154 {
155         set_position (frame_position, _end_frame);
156 }
157
158 void
159 TempoCurve::show ()
160 {
161         _shown = true;
162
163         group->show ();
164 }
165
166 void
167 TempoCurve::hide ()
168 {
169         _shown = false;
170
171         group->hide ();
172 }
173
174 void
175 TempoCurve::set_color_rgba (uint32_t c)
176 {
177         _color = c;
178         _curve->set_fill_color (UIConfiguration::instance().color_mod ("tempo curve", "selection rect"));
179         _curve->set_outline_color (_color);
180
181 }