Show cursor in Cut mode on button press at cutting position
[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 (end_frame == (framepos_t) UINT32_MAX) {
116                 const double tempo_at = _tempo.note_types_per_minute();
117                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
118
119                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
120                 points->push_back (ArdourCanvas::Duple (ArdourCanvas::COORD_MAX - 5.0, y_pos));
121
122         } else if (_tempo.type() == ARDOUR::TempoSection::Constant) {
123                 const double tempo_at = _tempo.note_types_per_minute();
124                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
125
126                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
127                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_frame - frame), y_pos));
128         } else {
129
130                 const framepos_t frame_step = max ((end_frame - frame) / 5, (framepos_t) 1);
131                 framepos_t current_frame = frame;
132
133                 while (current_frame < (end_frame - frame_step)) {
134                         const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_frame (current_frame)).note_types_per_minute();
135                         const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
136
137                         points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_frame - frame), min (y_pos, curve_height)));
138
139                         current_frame += frame_step;
140                 }
141
142                 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_frame (end_frame)).note_types_per_minute();
143                 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
144
145                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel ((end_frame - 1) - frame), min (y_pos, curve_height)));
146         }
147
148         _curve->set (*points);
149 }
150
151 void
152 TempoCurve::reposition ()
153 {
154         set_position (frame_position, _end_frame);
155 }
156
157 void
158 TempoCurve::show ()
159 {
160         _shown = true;
161
162         group->show ();
163 }
164
165 void
166 TempoCurve::hide ()
167 {
168         _shown = false;
169
170         group->hide ();
171 }
172
173 void
174 TempoCurve::set_color_rgba (uint32_t c)
175 {
176         _color = c;
177         _curve->set_fill_color (UIConfiguration::instance().color_mod ("tempo curve", "selection rect"));
178         _curve->set_outline_color (_color);
179
180 }