enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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
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         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.beats_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.beats_per_minute());
53 #endif
54         _curve->set_fill_mode (ArdourCanvas::FramedCurve::Inside);
55         _curve->set_points_per_segment (3);
56
57         points = new ArdourCanvas::Points ();
58         points->push_back (ArdourCanvas::Duple (0.0, 0.0));
59         points->push_back (ArdourCanvas::Duple (1.0, 0.0));
60         points->push_back (ArdourCanvas::Duple (1.0, curve_height));
61         points->push_back (ArdourCanvas::Duple (0.0, curve_height));
62
63         _curve->set (*points);
64
65         set_color_rgba (rgba);
66
67         editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
68
69         /* events will be handled by both the group and the mark itself, so
70          * make sure they can both be used to lookup this object.
71          */
72
73         _curve->set_data ("tempo curve", this);
74
75         if (handle_events) {
76                 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
77         }
78
79         set_position (_tempo.frame(), UINT32_MAX);
80         _curve->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
81
82 }
83
84 TempoCurve::~TempoCurve ()
85 {
86         CatchDeletion (this); /* EMIT SIGNAL */
87
88         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
89         delete group;
90 }
91
92 void TempoCurve::reparent(ArdourCanvas::Container & parent)
93 {
94         group->reparent (&parent);
95         _parent = &parent;
96 }
97
98 void
99 TempoCurve::canvas_height_set (double h)
100 {
101         _canvas_height = h;
102 }
103
104 ArdourCanvas::Item&
105 TempoCurve::the_item() const
106 {
107         return *group;
108 }
109
110 void
111 TempoCurve::set_position (framepos_t frame, framepos_t end_frame)
112 {
113         unit_position = editor.sample_to_pixel (frame);
114         group->set_x_position (unit_position);
115         frame_position = frame;
116         _end_frame = end_frame;
117
118         points->clear();
119
120         points = new ArdourCanvas::Points ();
121         points->push_back (ArdourCanvas::Duple (0.0, curve_height));
122
123         if (end_frame == UINT32_MAX) {
124                 const double tempo_at = _tempo.tempo_at_frame (frame, editor.session()->frame_rate()) * _tempo.note_type();
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 (ArdourCanvas::COORD_MAX - 5.0, y_pos));
129
130         } else {
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 - frame_step)) {
135                         const double tempo_at = _tempo.tempo_at_frame (current_frame, editor.session()->frame_rate()) * _tempo.note_type();
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_frame (end_frame, editor.session()->frame_rate()) * _tempo.note_type();
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 - 1) - 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 }